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