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