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" |
| 19 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 20 | #include "api/units/data_size.h" |
| 21 | #include "api/units/time_delta.h" |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 22 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 23 | namespace webrtc { |
| 24 | namespace data_rate_impl { |
| 25 | constexpr int64_t kPlusInfinityVal = std::numeric_limits<int64_t>::max(); |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 26 | |
| 27 | inline int64_t Microbits(const DataSize& size) { |
| 28 | constexpr int64_t kMaxBeforeConversion = |
| 29 | std::numeric_limits<int64_t>::max() / 8000000; |
| 30 | RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion) |
| 31 | << "size is too large to be expressed in microbytes"; |
| 32 | return size.bytes() * 8000000; |
| 33 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 34 | } // namespace data_rate_impl |
| 35 | |
| 36 | // 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] | 37 | // represent bandwidth, encoding bitrate, etc. The internal storage is bits per |
| 38 | // second (bps). |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 39 | class DataRate { |
| 40 | public: |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 41 | DataRate() = delete; |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 42 | static DataRate Zero() { return DataRate(0); } |
| 43 | static DataRate Infinity() { |
| 44 | return DataRate(data_rate_impl::kPlusInfinityVal); |
| 45 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 46 | static DataRate bits_per_second(int64_t bits_per_sec) { |
| 47 | RTC_DCHECK_GE(bits_per_sec, 0); |
| 48 | return DataRate(bits_per_sec); |
| 49 | } |
| 50 | static DataRate bps(int64_t bits_per_sec) { |
| 51 | return DataRate::bits_per_second(bits_per_sec); |
| 52 | } |
| 53 | static DataRate kbps(int64_t kilobits_per_sec) { |
| 54 | return DataRate::bits_per_second(kilobits_per_sec * 1000); |
| 55 | } |
| 56 | int64_t bits_per_second() const { |
| 57 | RTC_DCHECK(IsFinite()); |
| 58 | return bits_per_sec_; |
| 59 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 60 | int64_t bps() const { return bits_per_second(); } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 61 | int64_t kbps() const { return (bps() + 500) / 1000; } |
| 62 | bool IsZero() const { return bits_per_sec_ == 0; } |
| 63 | bool IsInfinite() const { |
| 64 | return bits_per_sec_ == data_rate_impl::kPlusInfinityVal; |
| 65 | } |
Sebastian Jansson | 3b69b19 | 2018-05-07 13:51:51 +0200 | [diff] [blame] | 66 | bool IsFinite() const { return !IsInfinite(); } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 67 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 68 | bool operator==(const DataRate& other) const { |
| 69 | return bits_per_sec_ == other.bits_per_sec_; |
| 70 | } |
| 71 | bool operator!=(const DataRate& other) const { |
| 72 | return bits_per_sec_ != other.bits_per_sec_; |
| 73 | } |
| 74 | bool operator<=(const DataRate& other) const { |
| 75 | return bits_per_sec_ <= other.bits_per_sec_; |
| 76 | } |
| 77 | bool operator>=(const DataRate& other) const { |
| 78 | return bits_per_sec_ >= other.bits_per_sec_; |
| 79 | } |
| 80 | bool operator>(const DataRate& other) const { |
| 81 | return bits_per_sec_ > other.bits_per_sec_; |
| 82 | } |
| 83 | bool operator<(const DataRate& other) const { |
| 84 | return bits_per_sec_ < other.bits_per_sec_; |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | // Bits per second used internally to simplify debugging by making the value |
| 89 | // more recognizable. |
| 90 | explicit DataRate(int64_t bits_per_second) : bits_per_sec_(bits_per_second) {} |
| 91 | int64_t bits_per_sec_; |
| 92 | }; |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 93 | |
| 94 | inline DataRate operator*(const DataRate& rate, const double& scalar) { |
| 95 | return DataRate::bits_per_second(std::round(rate.bits_per_second() * scalar)); |
| 96 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 97 | inline DataRate operator*(const double& scalar, const DataRate& rate) { |
| 98 | return rate * scalar; |
| 99 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 100 | inline DataRate operator*(const DataRate& rate, const int64_t& scalar) { |
| 101 | return DataRate::bits_per_second(rate.bits_per_second() * scalar); |
| 102 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 103 | inline DataRate operator*(const int64_t& scalar, const DataRate& rate) { |
| 104 | return rate * scalar; |
| 105 | } |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 106 | inline DataRate operator*(const DataRate& rate, const int32_t& scalar) { |
| 107 | return DataRate::bits_per_second(rate.bits_per_second() * scalar); |
| 108 | } |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 109 | inline DataRate operator*(const int32_t& scalar, const DataRate& rate) { |
| 110 | return rate * scalar; |
| 111 | } |
| 112 | |
Sebastian Jansson | 66fa535 | 2018-04-30 16:54:57 +0200 | [diff] [blame] | 113 | inline DataRate operator/(const DataSize& size, const TimeDelta& duration) { |
| 114 | return DataRate::bits_per_second(data_rate_impl::Microbits(size) / |
| 115 | duration.us()); |
| 116 | } |
| 117 | inline TimeDelta operator/(const DataSize& size, const DataRate& rate) { |
| 118 | return TimeDelta::us(data_rate_impl::Microbits(size) / |
| 119 | rate.bits_per_second()); |
| 120 | } |
| 121 | inline DataSize operator*(const DataRate& rate, const TimeDelta& duration) { |
| 122 | int64_t microbits = rate.bits_per_second() * duration.us(); |
| 123 | return DataSize::bytes((microbits + 4000000) / 8000000); |
| 124 | } |
| 125 | inline DataSize operator*(const TimeDelta& duration, const DataRate& rate) { |
| 126 | return rate * duration; |
| 127 | } |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 128 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 129 | std::string ToString(const DataRate& value); |
Sebastian Jansson | e31be15 | 2018-04-16 12:41:47 +0200 | [diff] [blame] | 130 | |
Sebastian Jansson | 30bd403 | 2018-04-13 13:56:17 +0200 | [diff] [blame] | 131 | } // namespace webrtc |
| 132 | |
Sebastian Jansson | 6fae6ec | 2018-05-08 10:43:18 +0200 | [diff] [blame] | 133 | #endif // API_UNITS_DATA_RATE_H_ |