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 | |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 14 | #ifdef WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 15 | #include <ostream> // no-presubmit-check TODO(webrtc:8982) |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 16 | #endif // WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 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: |
Danil Chapovalov | e638ada | 2020-02-17 15:00:07 +0100 | [diff] [blame] | 34 | template <typename T> |
| 35 | static constexpr DataRate BitsPerSec(T value) { |
| 36 | static_assert(std::is_arithmetic<T>::value, ""); |
| 37 | return FromValue(value); |
| 38 | } |
| 39 | template <typename T> |
| 40 | static constexpr DataRate BytesPerSec(T value) { |
| 41 | static_assert(std::is_arithmetic<T>::value, ""); |
| 42 | return FromFraction(8, value); |
| 43 | } |
| 44 | template <typename T> |
| 45 | static constexpr DataRate KilobitsPerSec(T value) { |
| 46 | static_assert(std::is_arithmetic<T>::value, ""); |
| 47 | return FromFraction(1000, value); |
| 48 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 49 | static constexpr DataRate Infinity() { return PlusInfinity(); } |
Danil Chapovalov | e638ada | 2020-02-17 15:00:07 +0100 | [diff] [blame] | 50 | |
| 51 | DataRate() = delete; |
Danil Chapovalov | 1db70d5 | 2020-02-20 14:54:28 +0000 | [diff] [blame] | 52 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 53 | template <typename T = int64_t> |
| 54 | constexpr T bps() const { |
| 55 | return ToValue<T>(); |
| 56 | } |
| 57 | template <typename T = int64_t> |
Sebastian Jansson | 8fe7995 | 2019-01-31 11:09:35 +0100 | [diff] [blame] | 58 | constexpr T bytes_per_sec() const { |
| 59 | return ToFraction<8, T>(); |
| 60 | } |
| 61 | template <typename T = int64_t> |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 62 | constexpr T kbps() const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 63 | return ToFraction<1000, T>(); |
| 64 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 65 | constexpr int64_t bps_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 66 | return ToValueOr(fallback_value); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 67 | } |
| 68 | constexpr int64_t kbps_or(int64_t fallback_value) const { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 69 | return ToFractionOr<1000>(fallback_value); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | private: |
| 73 | // Bits per second used internally to simplify debugging by making the value |
| 74 | // more recognizable. |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 75 | friend class rtc_units_impl::UnitBase<DataRate>; |
| 76 | using RelativeUnit::RelativeUnit; |
| 77 | static constexpr bool one_sided = true; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 78 | }; |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 79 | |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 80 | namespace data_rate_impl { |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 81 | inline constexpr int64_t Microbits(const DataSize& size) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 82 | constexpr int64_t kMaxBeforeConversion = |
| 83 | std::numeric_limits<int64_t>::max() / 8000000; |
| 84 | RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion) |
| 85 | << "size is too large to be expressed in microbits"; |
| 86 | return size.bytes() * 8000000; |
| 87 | } |
| 88 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 89 | inline constexpr int64_t MillibytePerSec(const DataRate& size) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 90 | constexpr int64_t kMaxBeforeConversion = |
| 91 | std::numeric_limits<int64_t>::max() / (1000 / 8); |
| 92 | RTC_DCHECK_LE(size.bps(), kMaxBeforeConversion) |
| 93 | << "rate is too large to be expressed in microbytes per second"; |
| 94 | return size.bps() * (1000 / 8); |
| 95 | } |
| 96 | } // namespace data_rate_impl |
| 97 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 98 | inline constexpr DataRate operator/(const DataSize size, |
| 99 | const TimeDelta duration) { |
Danil Chapovalov | e638ada | 2020-02-17 15:00:07 +0100 | [diff] [blame] | 100 | return DataRate::BitsPerSec(data_rate_impl::Microbits(size) / duration.us()); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 101 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 102 | inline constexpr TimeDelta operator/(const DataSize size, const DataRate rate) { |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 103 | return TimeDelta::Micros(data_rate_impl::Microbits(size) / rate.bps()); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 104 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 105 | inline constexpr DataSize operator*(const DataRate rate, |
| 106 | const TimeDelta duration) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 107 | int64_t microbits = rate.bps() * duration.us(); |
Danil Chapovalov | e638ada | 2020-02-17 15:00:07 +0100 | [diff] [blame] | 108 | return DataSize::Bytes((microbits + 4000000) / 8000000); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 109 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 110 | inline constexpr DataSize operator*(const TimeDelta duration, |
| 111 | const DataRate rate) { |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 112 | return rate * duration; |
| 113 | } |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 114 | |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 115 | inline constexpr DataSize operator/(const DataRate rate, |
| 116 | const Frequency frequency) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 117 | int64_t millihertz = frequency.millihertz<int64_t>(); |
| 118 | // Note that the value is truncated here reather than rounded, potentially |
| 119 | // introducing an error of .5 bytes if rounding were expected. |
Danil Chapovalov | e638ada | 2020-02-17 15:00:07 +0100 | [diff] [blame] | 120 | return DataSize::Bytes(data_rate_impl::MillibytePerSec(rate) / millihertz); |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 121 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 122 | inline constexpr Frequency operator/(const DataRate rate, const DataSize size) { |
Danil Chapovalov | 2517a47 | 2020-02-14 13:52:46 +0100 | [diff] [blame] | 123 | return Frequency::MilliHertz(data_rate_impl::MillibytePerSec(rate) / |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 124 | size.bytes()); |
| 125 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 126 | inline constexpr DataRate operator*(const DataSize size, |
| 127 | const Frequency frequency) { |
Sebastian Jansson | cf41eb1 | 2019-06-10 11:30:59 +0200 | [diff] [blame] | 128 | RTC_DCHECK(frequency.IsZero() || |
| 129 | size.bytes() <= std::numeric_limits<int64_t>::max() / 8 / |
| 130 | frequency.millihertz<int64_t>()); |
| 131 | int64_t millibits_per_second = |
| 132 | size.bytes() * 8 * frequency.millihertz<int64_t>(); |
Danil Chapovalov | e638ada | 2020-02-17 15:00:07 +0100 | [diff] [blame] | 133 | return DataRate::BitsPerSec((millibits_per_second + 500) / 1000); |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 134 | } |
Sebastian Jansson | d7fade5 | 2020-01-29 10:44:51 +0100 | [diff] [blame] | 135 | inline constexpr DataRate operator*(const Frequency frequency, |
| 136 | const DataSize size) { |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 137 | return size * frequency; |
| 138 | } |
| 139 | |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 140 | std::string ToString(DataRate value); |
Sebastian Jansson | b113862 | 2019-04-11 16:48:15 +0200 | [diff] [blame] | 141 | inline std::string ToLogString(DataRate value) { |
| 142 | return ToString(value); |
| 143 | } |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 144 | |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 145 | #ifdef WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 146 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 147 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 148 | DataRate value) { |
| 149 | return stream << ToString(value); |
| 150 | } |
Andrey Logvin | b95d90b | 2020-12-09 12:49:39 +0000 | [diff] [blame] | 151 | #endif // WEBRTC_UNIT_TEST |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 152 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 153 | } // namespace webrtc |
| 154 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 155 | #endif // API_UNITS_DATA_RATE_H_ |