blob: d813c61156df3065838aea821a2db9136df06064 [file] [log] [blame]
Sebastian Jansson30bd4032018-04-13 13:56:17 +02001/*
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 Jansson6fae6ec2018-05-08 10:43:18 +020011#ifndef API_UNITS_DATA_RATE_H_
12#define API_UNITS_DATA_RATE_H_
Sebastian Jansson2afd2812018-08-23 14:44:05 +020013
Andrey Logvinb95d90b2020-12-09 12:49:39 +000014#ifdef WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +020015#include <ostream> // no-presubmit-check TODO(webrtc:8982)
Andrey Logvinb95d90b2020-12-09 12:49:39 +000016#endif // WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +020017
Sebastian Jansson30bd4032018-04-13 13:56:17 +020018#include <limits>
19#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020020#include <type_traits>
Sebastian Jansson30bd4032018-04-13 13:56:17 +020021
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +020022#include "api/units/data_size.h"
Sebastian Jansson26b5e352019-06-07 11:05:31 +020023#include "api/units/frequency.h"
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +020024#include "api/units/time_delta.h"
Yves Gerey988cc082018-10-23 12:03:01 +020025#include "rtc_base/checks.h"
Danil Chapovalovf6e48bf2022-10-31 15:16:50 +010026#include "rtc_base/units/unit_base.h" // IWYU pragma: export
Sebastian Janssone31be152018-04-16 12:41:47 +020027
Sebastian Jansson30bd4032018-04-13 13:56:17 +020028namespace webrtc {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020029// DataRate is a class that represents a given data rate. This can be used to
Sebastian Jansson66fa5352018-04-30 16:54:57 +020030// represent bandwidth, encoding bitrate, etc. The internal storage is bits per
31// second (bps).
Sebastian Jansson72bba622018-11-19 11:17:12 +010032class DataRate final : public rtc_units_impl::RelativeUnit<DataRate> {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020033 public:
Danil Chapovalove638ada2020-02-17 15:00:07 +010034 template <typename T>
35 static constexpr DataRate BitsPerSec(T value) {
36 static_assert(std::is_arithmetic<T>::value, "");
37 return FromValue(value);
38 }
39 template <typename T>
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>
45 static constexpr DataRate KilobitsPerSec(T value) {
46 static_assert(std::is_arithmetic<T>::value, "");
47 return FromFraction(1000, value);
48 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010049 static constexpr DataRate Infinity() { return PlusInfinity(); }
Danil Chapovalove638ada2020-02-17 15:00:07 +010050
51 DataRate() = delete;
Danil Chapovalov1db70d52020-02-20 14:54:28 +000052
Sebastian Jansson72bba622018-11-19 11:17:12 +010053 template <typename T = int64_t>
54 constexpr T bps() const {
55 return ToValue<T>();
56 }
57 template <typename T = int64_t>
Sebastian Jansson8fe79952019-01-31 11:09:35 +010058 constexpr T bytes_per_sec() const {
59 return ToFraction<8, T>();
60 }
61 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010062 constexpr T kbps() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010063 return ToFraction<1000, T>();
64 }
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020065 constexpr int64_t bps_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010066 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020067 }
68 constexpr int64_t kbps_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010069 return ToFractionOr<1000>(fallback_value);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020070 }
71
72 private:
73 // Bits per second used internally to simplify debugging by making the value
74 // more recognizable.
Sebastian Jansson72bba622018-11-19 11:17:12 +010075 friend class rtc_units_impl::UnitBase<DataRate>;
76 using RelativeUnit::RelativeUnit;
77 static constexpr bool one_sided = true;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020078};
Sebastian Jansson66fa5352018-04-30 16:54:57 +020079
Sebastian Jansson26b5e352019-06-07 11:05:31 +020080namespace data_rate_impl {
Sebastian Janssond7fade52020-01-29 10:44:51 +010081inline constexpr int64_t Microbits(const DataSize& size) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020082 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
Sebastian Janssond7fade52020-01-29 10:44:51 +010089inline constexpr int64_t MillibytePerSec(const DataRate& size) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020090 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
Sebastian Janssond7fade52020-01-29 10:44:51 +010098inline constexpr DataRate operator/(const DataSize size,
99 const TimeDelta duration) {
Danil Chapovalove638ada2020-02-17 15:00:07 +0100100 return DataRate::BitsPerSec(data_rate_impl::Microbits(size) / duration.us());
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200101}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100102inline constexpr TimeDelta operator/(const DataSize size, const DataRate rate) {
Danil Chapovalov0c626af2020-02-10 11:16:00 +0100103 return TimeDelta::Micros(data_rate_impl::Microbits(size) / rate.bps());
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200104}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100105inline constexpr DataSize operator*(const DataRate rate,
106 const TimeDelta duration) {
Sebastian Jansson942b3602018-05-30 15:47:44 +0200107 int64_t microbits = rate.bps() * duration.us();
Danil Chapovalove638ada2020-02-17 15:00:07 +0100108 return DataSize::Bytes((microbits + 4000000) / 8000000);
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200109}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100110inline constexpr DataSize operator*(const TimeDelta duration,
111 const DataRate rate) {
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200112 return rate * duration;
113}
Sebastian Janssone31be152018-04-16 12:41:47 +0200114
Sebastian Janssond7fade52020-01-29 10:44:51 +0100115inline constexpr DataSize operator/(const DataRate rate,
116 const Frequency frequency) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +0200117 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.
Danil Chapovalove638ada2020-02-17 15:00:07 +0100120 return DataSize::Bytes(data_rate_impl::MillibytePerSec(rate) / millihertz);
Sebastian Jansson26b5e352019-06-07 11:05:31 +0200121}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100122inline constexpr Frequency operator/(const DataRate rate, const DataSize size) {
Danil Chapovalov2517a472020-02-14 13:52:46 +0100123 return Frequency::MilliHertz(data_rate_impl::MillibytePerSec(rate) /
Sebastian Jansson26b5e352019-06-07 11:05:31 +0200124 size.bytes());
125}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100126inline constexpr DataRate operator*(const DataSize size,
127 const Frequency frequency) {
Sebastian Janssoncf41eb12019-06-10 11:30:59 +0200128 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>();
Danil Chapovalove638ada2020-02-17 15:00:07 +0100133 return DataRate::BitsPerSec((millibits_per_second + 500) / 1000);
Sebastian Jansson26b5e352019-06-07 11:05:31 +0200134}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100135inline constexpr DataRate operator*(const Frequency frequency,
136 const DataSize size) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +0200137 return size * frequency;
138}
139
Sebastian Jansson72bba622018-11-19 11:17:12 +0100140std::string ToString(DataRate value);
Sebastian Janssonb1138622019-04-11 16:48:15 +0200141inline std::string ToLogString(DataRate value) {
142 return ToString(value);
143}
Sebastian Janssone31be152018-04-16 12:41:47 +0200144
Andrey Logvinb95d90b2020-12-09 12:49:39 +0000145#ifdef WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200146inline 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}
Andrey Logvinb95d90b2020-12-09 12:49:39 +0000151#endif // WEBRTC_UNIT_TEST
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200152
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200153} // namespace webrtc
154
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200155#endif // API_UNITS_DATA_RATE_H_