blob: 78c2e0ed9ab3ec4d7094cde7dadad03f337c15f5 [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
14#ifdef UNIT_TEST
15#include <ostream> // no-presubmit-check TODO(webrtc:8982)
16#endif // UNIT_TEST
17
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"
Sebastian Jansson72bba622018-11-19 11:17:12 +010026#include "rtc_base/units/unit_base.h"
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:
Sebastian Jansson3b69b192018-05-07 13:51:51 +020034 DataRate() = delete;
Sebastian Jansson72bba622018-11-19 11:17:12 +010035 static constexpr DataRate Infinity() { return PlusInfinity(); }
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020036 template <int64_t bps>
37 static constexpr DataRate BitsPerSec() {
Danil Chapovalov7356a562020-01-20 13:02:44 +010038 return FromValue(bps);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020039 }
40 template <int64_t kbps>
41 static constexpr DataRate KilobitsPerSec() {
Danil Chapovalov7356a562020-01-20 13:02:44 +010042 return FromFraction(1000, kbps);
Sebastian Jansson942b3602018-05-30 15:47:44 +020043 }
44 template <typename T>
Sebastian Jansson72bba622018-11-19 11:17:12 +010045 static constexpr DataRate bps(T bits_per_second) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010046 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010047 return FromValue(bits_per_second);
Sebastian Jansson942b3602018-05-30 15:47:44 +020048 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010049 template <typename T>
Sebastian Jansson8fe79952019-01-31 11:09:35 +010050 static constexpr DataRate bytes_per_sec(T bytes_per_second) {
51 static_assert(std::is_arithmetic<T>::value, "");
Danil Chapovalov7356a562020-01-20 13:02:44 +010052 return FromFraction(8, bytes_per_second);
Sebastian Jansson8fe79952019-01-31 11:09:35 +010053 }
54 template <typename T>
Sebastian Jansson72bba622018-11-19 11:17:12 +010055 static constexpr DataRate kbps(T kilobits_per_sec) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010056 static_assert(std::is_arithmetic<T>::value, "");
Danil Chapovalov7356a562020-01-20 13:02:44 +010057 return FromFraction(1000, kilobits_per_sec);
Sebastian Jansson72bba622018-11-19 11:17:12 +010058 }
59 template <typename T = int64_t>
60 constexpr T bps() const {
61 return ToValue<T>();
62 }
63 template <typename T = int64_t>
Sebastian Jansson8fe79952019-01-31 11:09:35 +010064 constexpr T bytes_per_sec() const {
65 return ToFraction<8, T>();
66 }
67 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010068 constexpr T kbps() const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010069 return ToFraction<1000, T>();
70 }
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020071 constexpr int64_t bps_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010072 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020073 }
74 constexpr int64_t kbps_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010075 return ToFractionOr<1000>(fallback_value);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020076 }
77
78 private:
79 // Bits per second used internally to simplify debugging by making the value
80 // more recognizable.
Sebastian Jansson72bba622018-11-19 11:17:12 +010081 friend class rtc_units_impl::UnitBase<DataRate>;
82 using RelativeUnit::RelativeUnit;
83 static constexpr bool one_sided = true;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020084};
Sebastian Jansson66fa5352018-04-30 16:54:57 +020085
Sebastian Jansson26b5e352019-06-07 11:05:31 +020086namespace data_rate_impl {
Sebastian Janssond7fade52020-01-29 10:44:51 +010087inline constexpr int64_t Microbits(const DataSize& size) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020088 constexpr int64_t kMaxBeforeConversion =
89 std::numeric_limits<int64_t>::max() / 8000000;
90 RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion)
91 << "size is too large to be expressed in microbits";
92 return size.bytes() * 8000000;
93}
94
Sebastian Janssond7fade52020-01-29 10:44:51 +010095inline constexpr int64_t MillibytePerSec(const DataRate& size) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020096 constexpr int64_t kMaxBeforeConversion =
97 std::numeric_limits<int64_t>::max() / (1000 / 8);
98 RTC_DCHECK_LE(size.bps(), kMaxBeforeConversion)
99 << "rate is too large to be expressed in microbytes per second";
100 return size.bps() * (1000 / 8);
101}
102} // namespace data_rate_impl
103
Sebastian Janssond7fade52020-01-29 10:44:51 +0100104inline constexpr DataRate operator/(const DataSize size,
105 const TimeDelta duration) {
Sebastian Jansson942b3602018-05-30 15:47:44 +0200106 return DataRate::bps(data_rate_impl::Microbits(size) / duration.us());
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200107}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100108inline constexpr TimeDelta operator/(const DataSize size, const DataRate rate) {
Sebastian Jansson942b3602018-05-30 15:47:44 +0200109 return TimeDelta::us(data_rate_impl::Microbits(size) / rate.bps());
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200110}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100111inline constexpr DataSize operator*(const DataRate rate,
112 const TimeDelta duration) {
Sebastian Jansson942b3602018-05-30 15:47:44 +0200113 int64_t microbits = rate.bps() * duration.us();
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200114 return DataSize::bytes((microbits + 4000000) / 8000000);
115}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100116inline constexpr DataSize operator*(const TimeDelta duration,
117 const DataRate rate) {
Sebastian Jansson66fa5352018-04-30 16:54:57 +0200118 return rate * duration;
119}
Sebastian Janssone31be152018-04-16 12:41:47 +0200120
Sebastian Janssond7fade52020-01-29 10:44:51 +0100121inline constexpr DataSize operator/(const DataRate rate,
122 const Frequency frequency) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +0200123 int64_t millihertz = frequency.millihertz<int64_t>();
124 // Note that the value is truncated here reather than rounded, potentially
125 // introducing an error of .5 bytes if rounding were expected.
126 return DataSize::bytes(data_rate_impl::MillibytePerSec(rate) / millihertz);
127}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100128inline constexpr Frequency operator/(const DataRate rate, const DataSize size) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +0200129 return Frequency::millihertz(data_rate_impl::MillibytePerSec(rate) /
130 size.bytes());
131}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100132inline constexpr DataRate operator*(const DataSize size,
133 const Frequency frequency) {
Sebastian Janssoncf41eb12019-06-10 11:30:59 +0200134 RTC_DCHECK(frequency.IsZero() ||
135 size.bytes() <= std::numeric_limits<int64_t>::max() / 8 /
136 frequency.millihertz<int64_t>());
137 int64_t millibits_per_second =
138 size.bytes() * 8 * frequency.millihertz<int64_t>();
Sebastian Jansson26b5e352019-06-07 11:05:31 +0200139 return DataRate::bps((millibits_per_second + 500) / 1000);
140}
Sebastian Janssond7fade52020-01-29 10:44:51 +0100141inline constexpr DataRate operator*(const Frequency frequency,
142 const DataSize size) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +0200143 return size * frequency;
144}
145
Sebastian Jansson72bba622018-11-19 11:17:12 +0100146std::string ToString(DataRate value);
Sebastian Janssonb1138622019-04-11 16:48:15 +0200147inline std::string ToLogString(DataRate value) {
148 return ToString(value);
149}
Sebastian Janssone31be152018-04-16 12:41:47 +0200150
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200151#ifdef UNIT_TEST
152inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
153 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
154 DataRate value) {
155 return stream << ToString(value);
156}
157#endif // UNIT_TEST
158
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200159} // namespace webrtc
160
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200161#endif // API_UNITS_DATA_RATE_H_