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() { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 38 | return FromStaticValue<bps>(); |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 39 | } |
| 40 | template <int64_t kbps> |
| 41 | static constexpr DataRate KilobitsPerSec() { |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 42 | return FromStaticFraction<kbps, 1000>(); |
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, ""); |
| 52 | return FromFraction<8>(bytes_per_second); |
| 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, ""); |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 57 | return FromFraction<1000>(kilobits_per_sec); |
| 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 | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 68 | T kbps() const { |
| 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 { |
| 87 | inline int64_t Microbits(const DataSize& size) { |
| 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 | |
| 95 | inline int64_t MillibytePerSec(const DataRate& size) { |
| 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 | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 104 | inline DataRate operator/(const DataSize size, const TimeDelta duration) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 105 | return DataRate::bps(data_rate_impl::Microbits(size) / duration.us()); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 106 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 107 | inline TimeDelta operator/(const DataSize size, const DataRate rate) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 108 | return TimeDelta::us(data_rate_impl::Microbits(size) / rate.bps()); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 109 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 110 | inline DataSize operator*(const DataRate rate, const TimeDelta duration) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 111 | int64_t microbits = rate.bps() * duration.us(); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 112 | return DataSize::bytes((microbits + 4000000) / 8000000); |
| 113 | } |
Sebastian Jansson | 72bba62 | 2018-11-19 11:17:12 +0100 | [diff] [blame] | 114 | inline DataSize operator*(const TimeDelta duration, const DataRate rate) { |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 115 | return rate * duration; |
| 116 | } |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 117 | |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 118 | inline DataSize operator/(const DataRate rate, const Frequency frequency) { |
| 119 | int64_t millihertz = frequency.millihertz<int64_t>(); |
| 120 | // Note that the value is truncated here reather than rounded, potentially |
| 121 | // introducing an error of .5 bytes if rounding were expected. |
| 122 | return DataSize::bytes(data_rate_impl::MillibytePerSec(rate) / millihertz); |
| 123 | } |
| 124 | inline Frequency operator/(const DataRate rate, const DataSize size) { |
| 125 | return Frequency::millihertz(data_rate_impl::MillibytePerSec(rate) / |
| 126 | size.bytes()); |
| 127 | } |
| 128 | inline DataRate operator*(const DataSize size, const Frequency frequency) { |
| 129 | int64_t millihertz = frequency.millihertz<int64_t>(); |
| 130 | int64_t kMaxBeforeConversion = |
| 131 | std::numeric_limits<int64_t>::max() / 8 / millihertz; |
| 132 | RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion); |
| 133 | int64_t millibits_per_second = size.bytes() * 8 * millihertz; |
| 134 | return DataRate::bps((millibits_per_second + 500) / 1000); |
| 135 | } |
| 136 | inline DataRate operator*(const Frequency frequency, const DataSize size) { |
| 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 | |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 145 | #ifdef UNIT_TEST |
| 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 | } |
| 151 | #endif // UNIT_TEST |
| 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_ |