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