xref: /aosp_15_r20/external/webrtc/api/units/data_rate.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef API_UNITS_DATA_RATE_H_
12*d9f75844SAndroid Build Coastguard Worker #define API_UNITS_DATA_RATE_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #ifdef WEBRTC_UNIT_TEST
15*d9f75844SAndroid Build Coastguard Worker #include <ostream>  // no-presubmit-check TODO(webrtc:8982)
16*d9f75844SAndroid Build Coastguard Worker #endif              // WEBRTC_UNIT_TEST
17*d9f75844SAndroid Build Coastguard Worker 
18*d9f75844SAndroid Build Coastguard Worker #include <limits>
19*d9f75844SAndroid Build Coastguard Worker #include <string>
20*d9f75844SAndroid Build Coastguard Worker #include <type_traits>
21*d9f75844SAndroid Build Coastguard Worker 
22*d9f75844SAndroid Build Coastguard Worker #include "api/units/data_size.h"
23*d9f75844SAndroid Build Coastguard Worker #include "api/units/frequency.h"
24*d9f75844SAndroid Build Coastguard Worker #include "api/units/time_delta.h"
25*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
26*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/units/unit_base.h"  // IWYU pragma: export
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
29*d9f75844SAndroid Build Coastguard Worker // DataRate is a class that represents a given data rate. This can be used to
30*d9f75844SAndroid Build Coastguard Worker // represent bandwidth, encoding bitrate, etc. The internal storage is bits per
31*d9f75844SAndroid Build Coastguard Worker // second (bps).
32*d9f75844SAndroid Build Coastguard Worker class DataRate final : public rtc_units_impl::RelativeUnit<DataRate> {
33*d9f75844SAndroid Build Coastguard Worker  public:
34*d9f75844SAndroid Build Coastguard Worker   template <typename T>
BitsPerSec(T value)35*d9f75844SAndroid Build Coastguard Worker   static constexpr DataRate BitsPerSec(T value) {
36*d9f75844SAndroid Build Coastguard Worker     static_assert(std::is_arithmetic<T>::value, "");
37*d9f75844SAndroid Build Coastguard Worker     return FromValue(value);
38*d9f75844SAndroid Build Coastguard Worker   }
39*d9f75844SAndroid Build Coastguard Worker   template <typename T>
BytesPerSec(T value)40*d9f75844SAndroid Build Coastguard Worker   static constexpr DataRate BytesPerSec(T value) {
41*d9f75844SAndroid Build Coastguard Worker     static_assert(std::is_arithmetic<T>::value, "");
42*d9f75844SAndroid Build Coastguard Worker     return FromFraction(8, value);
43*d9f75844SAndroid Build Coastguard Worker   }
44*d9f75844SAndroid Build Coastguard Worker   template <typename T>
KilobitsPerSec(T value)45*d9f75844SAndroid Build Coastguard Worker   static constexpr DataRate KilobitsPerSec(T value) {
46*d9f75844SAndroid Build Coastguard Worker     static_assert(std::is_arithmetic<T>::value, "");
47*d9f75844SAndroid Build Coastguard Worker     return FromFraction(1000, value);
48*d9f75844SAndroid Build Coastguard Worker   }
Infinity()49*d9f75844SAndroid Build Coastguard Worker   static constexpr DataRate Infinity() { return PlusInfinity(); }
50*d9f75844SAndroid Build Coastguard Worker 
51*d9f75844SAndroid Build Coastguard Worker   DataRate() = delete;
52*d9f75844SAndroid Build Coastguard Worker 
53*d9f75844SAndroid Build Coastguard Worker   template <typename T = int64_t>
bps()54*d9f75844SAndroid Build Coastguard Worker   constexpr T bps() const {
55*d9f75844SAndroid Build Coastguard Worker     return ToValue<T>();
56*d9f75844SAndroid Build Coastguard Worker   }
57*d9f75844SAndroid Build Coastguard Worker   template <typename T = int64_t>
bytes_per_sec()58*d9f75844SAndroid Build Coastguard Worker   constexpr T bytes_per_sec() const {
59*d9f75844SAndroid Build Coastguard Worker     return ToFraction<8, T>();
60*d9f75844SAndroid Build Coastguard Worker   }
61*d9f75844SAndroid Build Coastguard Worker   template <typename T = int64_t>
kbps()62*d9f75844SAndroid Build Coastguard Worker   constexpr T kbps() const {
63*d9f75844SAndroid Build Coastguard Worker     return ToFraction<1000, T>();
64*d9f75844SAndroid Build Coastguard Worker   }
bps_or(int64_t fallback_value)65*d9f75844SAndroid Build Coastguard Worker   constexpr int64_t bps_or(int64_t fallback_value) const {
66*d9f75844SAndroid Build Coastguard Worker     return ToValueOr(fallback_value);
67*d9f75844SAndroid Build Coastguard Worker   }
kbps_or(int64_t fallback_value)68*d9f75844SAndroid Build Coastguard Worker   constexpr int64_t kbps_or(int64_t fallback_value) const {
69*d9f75844SAndroid Build Coastguard Worker     return ToFractionOr<1000>(fallback_value);
70*d9f75844SAndroid Build Coastguard Worker   }
71*d9f75844SAndroid Build Coastguard Worker 
72*d9f75844SAndroid Build Coastguard Worker  private:
73*d9f75844SAndroid Build Coastguard Worker   // Bits per second used internally to simplify debugging by making the value
74*d9f75844SAndroid Build Coastguard Worker   // more recognizable.
75*d9f75844SAndroid Build Coastguard Worker   friend class rtc_units_impl::UnitBase<DataRate>;
76*d9f75844SAndroid Build Coastguard Worker   using RelativeUnit::RelativeUnit;
77*d9f75844SAndroid Build Coastguard Worker   static constexpr bool one_sided = true;
78*d9f75844SAndroid Build Coastguard Worker };
79*d9f75844SAndroid Build Coastguard Worker 
80*d9f75844SAndroid Build Coastguard Worker namespace data_rate_impl {
Microbits(const DataSize & size)81*d9f75844SAndroid Build Coastguard Worker inline constexpr int64_t Microbits(const DataSize& size) {
82*d9f75844SAndroid Build Coastguard Worker   constexpr int64_t kMaxBeforeConversion =
83*d9f75844SAndroid Build Coastguard Worker       std::numeric_limits<int64_t>::max() / 8000000;
84*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion)
85*d9f75844SAndroid Build Coastguard Worker       << "size is too large to be expressed in microbits";
86*d9f75844SAndroid Build Coastguard Worker   return size.bytes() * 8000000;
87*d9f75844SAndroid Build Coastguard Worker }
88*d9f75844SAndroid Build Coastguard Worker 
MillibytePerSec(const DataRate & size)89*d9f75844SAndroid Build Coastguard Worker inline constexpr int64_t MillibytePerSec(const DataRate& size) {
90*d9f75844SAndroid Build Coastguard Worker   constexpr int64_t kMaxBeforeConversion =
91*d9f75844SAndroid Build Coastguard Worker       std::numeric_limits<int64_t>::max() / (1000 / 8);
92*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(size.bps(), kMaxBeforeConversion)
93*d9f75844SAndroid Build Coastguard Worker       << "rate is too large to be expressed in microbytes per second";
94*d9f75844SAndroid Build Coastguard Worker   return size.bps() * (1000 / 8);
95*d9f75844SAndroid Build Coastguard Worker }
96*d9f75844SAndroid Build Coastguard Worker }  // namespace data_rate_impl
97*d9f75844SAndroid Build Coastguard Worker 
98*d9f75844SAndroid Build Coastguard Worker inline constexpr DataRate operator/(const DataSize size,
99*d9f75844SAndroid Build Coastguard Worker                                     const TimeDelta duration) {
100*d9f75844SAndroid Build Coastguard Worker   return DataRate::BitsPerSec(data_rate_impl::Microbits(size) / duration.us());
101*d9f75844SAndroid Build Coastguard Worker }
102*d9f75844SAndroid Build Coastguard Worker inline constexpr TimeDelta operator/(const DataSize size, const DataRate rate) {
103*d9f75844SAndroid Build Coastguard Worker   return TimeDelta::Micros(data_rate_impl::Microbits(size) / rate.bps());
104*d9f75844SAndroid Build Coastguard Worker }
105*d9f75844SAndroid Build Coastguard Worker inline constexpr DataSize operator*(const DataRate rate,
106*d9f75844SAndroid Build Coastguard Worker                                     const TimeDelta duration) {
107*d9f75844SAndroid Build Coastguard Worker   int64_t microbits = rate.bps() * duration.us();
108*d9f75844SAndroid Build Coastguard Worker   return DataSize::Bytes((microbits + 4000000) / 8000000);
109*d9f75844SAndroid Build Coastguard Worker }
110*d9f75844SAndroid Build Coastguard Worker inline constexpr DataSize operator*(const TimeDelta duration,
111*d9f75844SAndroid Build Coastguard Worker                                     const DataRate rate) {
112*d9f75844SAndroid Build Coastguard Worker   return rate * duration;
113*d9f75844SAndroid Build Coastguard Worker }
114*d9f75844SAndroid Build Coastguard Worker 
115*d9f75844SAndroid Build Coastguard Worker inline constexpr DataSize operator/(const DataRate rate,
116*d9f75844SAndroid Build Coastguard Worker                                     const Frequency frequency) {
117*d9f75844SAndroid Build Coastguard Worker   int64_t millihertz = frequency.millihertz<int64_t>();
118*d9f75844SAndroid Build Coastguard Worker   // Note that the value is truncated here reather than rounded, potentially
119*d9f75844SAndroid Build Coastguard Worker   // introducing an error of .5 bytes if rounding were expected.
120*d9f75844SAndroid Build Coastguard Worker   return DataSize::Bytes(data_rate_impl::MillibytePerSec(rate) / millihertz);
121*d9f75844SAndroid Build Coastguard Worker }
122*d9f75844SAndroid Build Coastguard Worker inline constexpr Frequency operator/(const DataRate rate, const DataSize size) {
123*d9f75844SAndroid Build Coastguard Worker   return Frequency::MilliHertz(data_rate_impl::MillibytePerSec(rate) /
124*d9f75844SAndroid Build Coastguard Worker                                size.bytes());
125*d9f75844SAndroid Build Coastguard Worker }
126*d9f75844SAndroid Build Coastguard Worker inline constexpr DataRate operator*(const DataSize size,
127*d9f75844SAndroid Build Coastguard Worker                                     const Frequency frequency) {
128*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(frequency.IsZero() ||
129*d9f75844SAndroid Build Coastguard Worker              size.bytes() <= std::numeric_limits<int64_t>::max() / 8 /
130*d9f75844SAndroid Build Coastguard Worker                                  frequency.millihertz<int64_t>());
131*d9f75844SAndroid Build Coastguard Worker   int64_t millibits_per_second =
132*d9f75844SAndroid Build Coastguard Worker       size.bytes() * 8 * frequency.millihertz<int64_t>();
133*d9f75844SAndroid Build Coastguard Worker   return DataRate::BitsPerSec((millibits_per_second + 500) / 1000);
134*d9f75844SAndroid Build Coastguard Worker }
135*d9f75844SAndroid Build Coastguard Worker inline constexpr DataRate operator*(const Frequency frequency,
136*d9f75844SAndroid Build Coastguard Worker                                     const DataSize size) {
137*d9f75844SAndroid Build Coastguard Worker   return size * frequency;
138*d9f75844SAndroid Build Coastguard Worker }
139*d9f75844SAndroid Build Coastguard Worker 
140*d9f75844SAndroid Build Coastguard Worker std::string ToString(DataRate value);
ToLogString(DataRate value)141*d9f75844SAndroid Build Coastguard Worker inline std::string ToLogString(DataRate value) {
142*d9f75844SAndroid Build Coastguard Worker   return ToString(value);
143*d9f75844SAndroid Build Coastguard Worker }
144*d9f75844SAndroid Build Coastguard Worker 
145*d9f75844SAndroid Build Coastguard Worker #ifdef WEBRTC_UNIT_TEST
146*d9f75844SAndroid Build Coastguard Worker inline std::ostream& operator<<(  // no-presubmit-check TODO(webrtc:8982)
147*d9f75844SAndroid Build Coastguard Worker     std::ostream& stream,         // no-presubmit-check TODO(webrtc:8982)
148*d9f75844SAndroid Build Coastguard Worker     DataRate value) {
149*d9f75844SAndroid Build Coastguard Worker   return stream << ToString(value);
150*d9f75844SAndroid Build Coastguard Worker }
151*d9f75844SAndroid Build Coastguard Worker #endif  // WEBRTC_UNIT_TEST
152*d9f75844SAndroid Build Coastguard Worker 
153*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
154*d9f75844SAndroid Build Coastguard Worker 
155*d9f75844SAndroid Build Coastguard Worker #endif  // API_UNITS_DATA_RATE_H_
156