Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 11 | #ifndef API_UNITS_DATA_RATE_H_ |
| 12 | #define API_UNITS_DATA_RATE_H_ |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 13 | |
| 14 | #ifdef UNIT_TEST |
| 15 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
| 16 | #endif // UNIT_TEST |
| 17 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 18 | #include <limits> |
| 19 | #include <string> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 20 | #include <type_traits> |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 21 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 22 | #include "api/units/data_size.h" |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 23 | #include "api/units/frequency.h" |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 24 | #include "api/units/time_delta.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 26 | #include "rtc_base/units/unit_base.h" |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 27 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 28 | namespace webrtc { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 29 | // DataRate is a class that represents a given data rate. This can be used to |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 30 | // represent bandwidth, encoding bitrate, etc. The internal storage is bits per |
| 31 | // second (bps). |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 32 | class DataRate final : public rtc_units_impl::RelativeUnit<DataRate> { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 33 | public: |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 34 | DataRate() = delete; |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 35 | static constexpr DataRate Infinity() { return PlusInfinity(); } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 36 | template <int64_t bps> |
| 37 | static constexpr DataRate BitsPerSec() { |
Danil Chapovalov | 7356a56 | 2020-01-20 13:02:44 +0100 | [diff] [blame] | 38 | return FromValue(bps); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 39 | } |
| 40 | template <int64_t kbps> |
| 41 | static constexpr DataRate KilobitsPerSec() { |
Danil Chapovalov | 7356a56 | 2020-01-20 13:02:44 +0100 | [diff] [blame] | 42 | return FromFraction(1000, kbps); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 43 | } |
| 44 | template <typename T> |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 45 | static constexpr DataRate bps(T bits_per_second) { |
Sebastian Jansson | 0c3f4d3 | 2018-11-30 10:02:44 +0100 | [diff] [blame] | 46 | static_assert(std::is_arithmetic<T>::value, ""); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 47 | return FromValue(bits_per_second); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 48 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 49 | template <typename T> |
Sebastian Jansson | 8fe7995 | 2019-01-31 11:09:35 +0100 | [diff] [blame] | 50 | static constexpr DataRate bytes_per_sec(T bytes_per_second) { |
| 51 | static_assert(std::is_arithmetic<T>::value, ""); |
Danil Chapovalov | 7356a56 | 2020-01-20 13:02:44 +0100 | [diff] [blame] | 52 | return FromFraction(8, bytes_per_second); |
Sebastian Jansson | 8fe7995 | 2019-01-31 11:09:35 +0100 | [diff] [blame] | 53 | } |
| 54 | template <typename T> |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 55 | static constexpr DataRate kbps(T kilobits_per_sec) { |
Sebastian Jansson | 0c3f4d3 | 2018-11-30 10:02:44 +0100 | [diff] [blame] | 56 | static_assert(std::is_arithmetic<T>::value, ""); |
Danil Chapovalov | 7356a56 | 2020-01-20 13:02:44 +0100 | [diff] [blame] | 57 | return FromFraction(1000, kilobits_per_sec); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 58 | } |
| 59 | template <typename T = int64_t> |
| 60 | constexpr T bps() const { |
| 61 | return ToValue<T>(); |
| 62 | } |
| 63 | template <typename T = int64_t> |
Sebastian Jansson | 8fe7995 | 2019-01-31 11:09:35 +0100 | [diff] [blame] | 64 | constexpr T bytes_per_sec() const { |
| 65 | return ToFraction<8, T>(); |
| 66 | } |
| 67 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 68 | constexpr T kbps() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 69 | return ToFraction<1000, T>(); |
| 70 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 71 | constexpr int64_t bps_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 72 | return ToValueOr(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 73 | } |
| 74 | constexpr int64_t kbps_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 75 | return ToFractionOr<1000>(fallback_value); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private: |
| 79 | // Bits per second used internally to simplify debugging by making the value |
| 80 | // more recognizable. |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 81 | friend class rtc_units_impl::UnitBase<DataRate>; |
| 82 | using RelativeUnit::RelativeUnit; |
| 83 | static constexpr bool one_sided = true; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 84 | }; |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 85 | |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 86 | namespace data_rate_impl { |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 87 | inline constexpr int64_t Microbits(const DataSize& size) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 88 | constexpr int64_t kMaxBeforeConversion = |
| 89 | std::numeric_limits<int64_t>::max() / 8000000; |
| 90 | RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion) |
| 91 | << "size is too large to be expressed in microbits"; |
| 92 | return size.bytes() * 8000000; |
| 93 | } |
| 94 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 95 | inline constexpr int64_t MillibytePerSec(const DataRate& size) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 96 | constexpr int64_t kMaxBeforeConversion = |
| 97 | std::numeric_limits<int64_t>::max() / (1000 / 8); |
| 98 | RTC_DCHECK_LE(size.bps(), kMaxBeforeConversion) |
| 99 | << "rate is too large to be expressed in microbytes per second"; |
| 100 | return size.bps() * (1000 / 8); |
| 101 | } |
| 102 | } // namespace data_rate_impl |
| 103 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 104 | inline constexpr DataRate operator/(const DataSize size, |
| 105 | const TimeDelta duration) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 106 | return DataRate::bps(data_rate_impl::Microbits(size) / duration.us()); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 107 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 108 | inline constexpr TimeDelta operator/(const DataSize size, const DataRate rate) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 109 | return TimeDelta::us(data_rate_impl::Microbits(size) / rate.bps()); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 110 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 111 | inline constexpr DataSize operator*(const DataRate rate, |
| 112 | const TimeDelta duration) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 113 | int64_t microbits = rate.bps() * duration.us(); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 114 | return DataSize::bytes((microbits + 4000000) / 8000000); |
| 115 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 116 | inline constexpr DataSize operator*(const TimeDelta duration, |
| 117 | const DataRate rate) { |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 118 | return rate * duration; |
| 119 | } |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 120 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 121 | inline constexpr DataSize operator/(const DataRate rate, |
| 122 | const Frequency frequency) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 123 | int64_t millihertz = frequency.millihertz<int64_t>(); |
| 124 | // Note that the value is truncated here reather than rounded, potentially |
| 125 | // introducing an error of .5 bytes if rounding were expected. |
| 126 | return DataSize::bytes(data_rate_impl::MillibytePerSec(rate) / millihertz); |
| 127 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 128 | inline constexpr Frequency operator/(const DataRate rate, const DataSize size) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 129 | return Frequency::millihertz(data_rate_impl::MillibytePerSec(rate) / |
| 130 | size.bytes()); |
| 131 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 132 | inline constexpr DataRate operator*(const DataSize size, |
| 133 | const Frequency frequency) { |
Sebastian Jansson | cf41eb1 | 2019-06-10 11:30:59 +0200 | [diff] [blame] | 134 | RTC_DCHECK(frequency.IsZero() || |
| 135 | size.bytes() <= std::numeric_limits<int64_t>::max() / 8 / |
| 136 | frequency.millihertz<int64_t>()); |
| 137 | int64_t millibits_per_second = |
| 138 | size.bytes() * 8 * frequency.millihertz<int64_t>(); |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 139 | return DataRate::bps((millibits_per_second + 500) / 1000); |
| 140 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame^] | 141 | inline constexpr DataRate operator*(const Frequency frequency, |
| 142 | const DataSize size) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 143 | return size * frequency; |
| 144 | } |
| 145 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 146 | std::string ToString(DataRate value); |
Sebastian Jansson | b113862 | 2019-04-11 16:48:15 +0200 | [diff] [blame] | 147 | inline std::string ToLogString(DataRate value) { |
| 148 | return ToString(value); |
| 149 | } |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 150 | |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 151 | #ifdef UNIT_TEST |
| 152 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 153 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 154 | DataRate value) { |
| 155 | return stream << ToString(value); |
| 156 | } |
| 157 | #endif // UNIT_TEST |
| 158 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 159 | } // namespace webrtc |
| 160 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 161 | #endif // API_UNITS_DATA_RATE_H_ |