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 <stdint.h> |
Sebastian Jansson | 6c19dec | 2018-10-05 13:23:57 +0200 | [diff] [blame^] | 19 | #include <algorithm> |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 20 | #include <cmath> |
| 21 | #include <limits> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "rtc_base/checks.h" |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 25 | #include "rtc_base/numerics/safe_conversions.h" |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 26 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 27 | #include "api/units/data_size.h" |
| 28 | #include "api/units/time_delta.h" |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 29 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 30 | namespace webrtc { |
| 31 | namespace data_rate_impl { |
| 32 | constexpr int64_t kPlusInfinityVal = std::numeric_limits<int64_t>::max(); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 33 | |
| 34 | inline int64_t Microbits(const DataSize& size) { |
| 35 | constexpr int64_t kMaxBeforeConversion = |
| 36 | std::numeric_limits<int64_t>::max() / 8000000; |
| 37 | RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion) |
| 38 | << "size is too large to be expressed in microbytes"; |
| 39 | return size.bytes() * 8000000; |
| 40 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 41 | } // namespace data_rate_impl |
| 42 | |
| 43 | // 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] | 44 | // represent bandwidth, encoding bitrate, etc. The internal storage is bits per |
| 45 | // second (bps). |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 46 | class DataRate { |
| 47 | public: |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 48 | DataRate() = delete; |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 49 | static constexpr DataRate Zero() { return DataRate(0); } |
| 50 | static constexpr DataRate Infinity() { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 51 | return DataRate(data_rate_impl::kPlusInfinityVal); |
| 52 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 53 | template <int64_t bps> |
| 54 | static constexpr DataRate BitsPerSec() { |
| 55 | static_assert(bps >= 0, ""); |
| 56 | static_assert(bps < data_rate_impl::kPlusInfinityVal, ""); |
| 57 | return DataRate(bps); |
| 58 | } |
| 59 | template <int64_t kbps> |
| 60 | static constexpr DataRate KilobitsPerSec() { |
| 61 | static_assert(kbps >= 0, ""); |
| 62 | static_assert(kbps < data_rate_impl::kPlusInfinityVal / 1000, ""); |
| 63 | return DataRate(kbps * 1000); |
| 64 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 65 | |
| 66 | template < |
| 67 | typename T, |
| 68 | typename std::enable_if<std::is_integral<T>::value>::type* = nullptr> |
| 69 | static DataRate bps(T bits_per_second) { |
| 70 | RTC_DCHECK_GE(bits_per_second, 0); |
| 71 | RTC_DCHECK_LT(bits_per_second, data_rate_impl::kPlusInfinityVal); |
| 72 | return DataRate(rtc::dchecked_cast<int64_t>(bits_per_second)); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 73 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 74 | template < |
| 75 | typename T, |
| 76 | typename std::enable_if<std::is_integral<T>::value>::type* = nullptr> |
| 77 | static DataRate kbps(T kilobits_per_sec) { |
| 78 | RTC_DCHECK_GE(kilobits_per_sec, 0); |
| 79 | RTC_DCHECK_LT(kilobits_per_sec, data_rate_impl::kPlusInfinityVal / 1000); |
| 80 | return DataRate::bps(rtc::dchecked_cast<int64_t>(kilobits_per_sec) * 1000); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 81 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 82 | |
| 83 | template <typename T, |
| 84 | typename std::enable_if<std::is_floating_point<T>::value>::type* = |
| 85 | nullptr> |
| 86 | static DataRate bps(T bits_per_second) { |
| 87 | if (bits_per_second == std::numeric_limits<T>::infinity()) { |
| 88 | return Infinity(); |
| 89 | } else { |
| 90 | RTC_DCHECK(!std::isnan(bits_per_second)); |
| 91 | RTC_DCHECK_GE(bits_per_second, 0); |
| 92 | RTC_DCHECK_LT(bits_per_second, data_rate_impl::kPlusInfinityVal); |
| 93 | return DataRate(rtc::dchecked_cast<int64_t>(bits_per_second)); |
| 94 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 95 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 96 | template <typename T, |
| 97 | typename std::enable_if<std::is_floating_point<T>::value>::type* = |
| 98 | nullptr> |
| 99 | static DataRate kbps(T kilobits_per_sec) { |
| 100 | return DataRate::bps(kilobits_per_sec * 1e3); |
| 101 | } |
| 102 | |
| 103 | template <typename T = int64_t> |
| 104 | typename std::enable_if<std::is_integral<T>::value, T>::type bps() const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 105 | RTC_DCHECK(IsFinite()); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 106 | return rtc::dchecked_cast<T>(bits_per_sec_); |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 107 | } |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 108 | template <typename T = int64_t> |
| 109 | typename std::enable_if<std::is_integral<T>::value, T>::type kbps() const { |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 110 | RTC_DCHECK(IsFinite()); |
| 111 | return rtc::dchecked_cast<T>(UnsafeKilobitsPerSec()); |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | template <typename T> |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 115 | typename std::enable_if<std::is_floating_point<T>::value, |
| 116 | T>::type constexpr bps() const { |
| 117 | return IsInfinite() ? std::numeric_limits<T>::infinity() : bits_per_sec_; |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 118 | } |
| 119 | template <typename T> |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 120 | typename std::enable_if<std::is_floating_point<T>::value, |
| 121 | T>::type constexpr kbps() const { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 122 | return bps<T>() * 1e-3; |
| 123 | } |
| 124 | |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 125 | constexpr int64_t bps_or(int64_t fallback_value) const { |
| 126 | return IsFinite() ? bits_per_sec_ : fallback_value; |
| 127 | } |
| 128 | constexpr int64_t kbps_or(int64_t fallback_value) const { |
| 129 | return IsFinite() ? UnsafeKilobitsPerSec() : fallback_value; |
| 130 | } |
| 131 | |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 132 | constexpr bool IsZero() const { return bits_per_sec_ == 0; } |
| 133 | constexpr bool IsInfinite() const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 134 | return bits_per_sec_ == data_rate_impl::kPlusInfinityVal; |
| 135 | } |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 136 | constexpr bool IsFinite() const { return !IsInfinite(); } |
Sebastian Jansson | 6c19dec | 2018-10-05 13:23:57 +0200 | [diff] [blame^] | 137 | DataRate Clamped(DataRate min_rate, DataRate max_rate) const { |
| 138 | return std::max(min_rate, std::min(*this, max_rate)); |
| 139 | } |
| 140 | void Clamp(DataRate min_rate, DataRate max_rate) { |
| 141 | *this = Clamped(min_rate, max_rate); |
| 142 | } |
Sebastian Jansson | af21eab | 2018-09-04 18:34:45 +0200 | [diff] [blame] | 143 | DataRate operator-(const DataRate& other) const { |
| 144 | return DataRate::bps(bps() - other.bps()); |
| 145 | } |
| 146 | DataRate operator+(const DataRate& other) const { |
| 147 | return DataRate::bps(bps() + other.bps()); |
| 148 | } |
| 149 | DataRate& operator-=(const DataRate& other) { |
| 150 | *this = *this - other; |
| 151 | return *this; |
| 152 | } |
| 153 | DataRate& operator+=(const DataRate& other) { |
| 154 | *this = *this + other; |
| 155 | return *this; |
| 156 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 157 | constexpr double operator/(const DataRate& other) const { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 158 | return bps<double>() / other.bps<double>(); |
| 159 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 160 | constexpr bool operator==(const DataRate& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 161 | return bits_per_sec_ == other.bits_per_sec_; |
| 162 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 163 | constexpr bool operator!=(const DataRate& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 164 | return bits_per_sec_ != other.bits_per_sec_; |
| 165 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 166 | constexpr bool operator<=(const DataRate& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 167 | return bits_per_sec_ <= other.bits_per_sec_; |
| 168 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 169 | constexpr bool operator>=(const DataRate& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 170 | return bits_per_sec_ >= other.bits_per_sec_; |
| 171 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 172 | constexpr bool operator>(const DataRate& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 173 | return bits_per_sec_ > other.bits_per_sec_; |
| 174 | } |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 175 | constexpr bool operator<(const DataRate& other) const { |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 176 | return bits_per_sec_ < other.bits_per_sec_; |
| 177 | } |
| 178 | |
| 179 | private: |
| 180 | // Bits per second used internally to simplify debugging by making the value |
| 181 | // more recognizable. |
Sebastian Jansson | 8e06419 | 2018-08-07 12:34:33 +0200 | [diff] [blame] | 182 | explicit constexpr DataRate(int64_t bits_per_second) |
| 183 | : bits_per_sec_(bits_per_second) {} |
Sebastian Jansson | c1c8b8e | 2018-08-07 15:29:04 +0200 | [diff] [blame] | 184 | constexpr int64_t UnsafeKilobitsPerSec() const { |
| 185 | return (bits_per_sec_ + 500) / 1000; |
| 186 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 187 | int64_t bits_per_sec_; |
| 188 | }; |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 189 | |
| 190 | inline DataRate operator*(const DataRate& rate, const double& scalar) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 191 | return DataRate::bps(std::round(rate.bps() * scalar)); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 192 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 193 | inline DataRate operator*(const double& scalar, const DataRate& rate) { |
| 194 | return rate * scalar; |
| 195 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 196 | inline DataRate operator*(const DataRate& rate, const int64_t& scalar) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 197 | return DataRate::bps(rate.bps() * scalar); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 198 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 199 | inline DataRate operator*(const int64_t& scalar, const DataRate& rate) { |
| 200 | return rate * scalar; |
| 201 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 202 | inline DataRate operator*(const DataRate& rate, const int32_t& scalar) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 203 | return DataRate::bps(rate.bps() * scalar); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 204 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 205 | inline DataRate operator*(const int32_t& scalar, const DataRate& rate) { |
| 206 | return rate * scalar; |
| 207 | } |
| 208 | |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 209 | inline DataRate operator/(const DataSize& size, const TimeDelta& duration) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 210 | return DataRate::bps(data_rate_impl::Microbits(size) / duration.us()); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 211 | } |
| 212 | inline TimeDelta operator/(const DataSize& size, const DataRate& rate) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 213 | return TimeDelta::us(data_rate_impl::Microbits(size) / rate.bps()); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 214 | } |
| 215 | inline DataSize operator*(const DataRate& rate, const TimeDelta& duration) { |
Sebastian Jansson | 942b360 | 2018-05-30 15:47:44 +0200 | [diff] [blame] | 216 | int64_t microbits = rate.bps() * duration.us(); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 217 | return DataSize::bytes((microbits + 4000000) / 8000000); |
| 218 | } |
| 219 | inline DataSize operator*(const TimeDelta& duration, const DataRate& rate) { |
| 220 | return rate * duration; |
| 221 | } |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 222 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 223 | std::string ToString(const DataRate& value); |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 224 | |
Sebastian Jansson | 2afd281 | 2018-08-23 14:44:05 +0200 | [diff] [blame] | 225 | #ifdef UNIT_TEST |
| 226 | inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) |
| 227 | std::ostream& stream, // no-presubmit-check TODO(webrtc:8982) |
| 228 | DataRate value) { |
| 229 | return stream << ToString(value); |
| 230 | } |
| 231 | #endif // UNIT_TEST |
| 232 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 233 | } // namespace webrtc |
| 234 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 235 | #endif // API_UNITS_DATA_RATE_H_ |