blob: 8a3836d0b7bdd2ef7202d599a090e1371eef3b5f [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"
23#include "api/units/time_delta.h"
Yves Gerey988cc082018-10-23 12:03:01 +020024#include "rtc_base/checks.h"
Sebastian Jansson72bba622018-11-19 11:17:12 +010025#include "rtc_base/units/unit_base.h"
Sebastian Janssone31be152018-04-16 12:41:47 +020026
Sebastian Jansson30bd4032018-04-13 13:56:17 +020027namespace webrtc {
28namespace data_rate_impl {
Sebastian Jansson66fa5352018-04-30 16:54:57 +020029inline int64_t Microbits(const DataSize& size) {
30 constexpr int64_t kMaxBeforeConversion =
31 std::numeric_limits<int64_t>::max() / 8000000;
32 RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion)
33 << "size is too large to be expressed in microbytes";
34 return size.bytes() * 8000000;
35}
Sebastian Jansson30bd4032018-04-13 13:56:17 +020036} // namespace data_rate_impl
37
38// DataRate is a class that represents a given data rate. This can be used to
Sebastian Jansson66fa5352018-04-30 16:54:57 +020039// represent bandwidth, encoding bitrate, etc. The internal storage is bits per
40// second (bps).
Sebastian Jansson72bba622018-11-19 11:17:12 +010041class DataRate final : public rtc_units_impl::RelativeUnit<DataRate> {
Sebastian Jansson30bd4032018-04-13 13:56:17 +020042 public:
Sebastian Jansson3b69b192018-05-07 13:51:51 +020043 DataRate() = delete;
Sebastian Jansson72bba622018-11-19 11:17:12 +010044 static constexpr DataRate Infinity() { return PlusInfinity(); }
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020045 template <int64_t bps>
46 static constexpr DataRate BitsPerSec() {
Sebastian Jansson72bba622018-11-19 11:17:12 +010047 return FromStaticValue<bps>();
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020048 }
49 template <int64_t kbps>
50 static constexpr DataRate KilobitsPerSec() {
Sebastian Jansson72bba622018-11-19 11:17:12 +010051 return FromStaticFraction<kbps, 1000>();
Sebastian Jansson942b3602018-05-30 15:47:44 +020052 }
53 template <typename T>
Sebastian Jansson72bba622018-11-19 11:17:12 +010054 static constexpr DataRate bps(T bits_per_second) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010055 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010056 return FromValue(bits_per_second);
Sebastian Jansson942b3602018-05-30 15:47:44 +020057 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010058 template <typename T>
59 static constexpr DataRate kbps(T kilobits_per_sec) {
Sebastian Jansson0c3f4d32018-11-30 10:02:44 +010060 static_assert(std::is_arithmetic<T>::value, "");
Sebastian Jansson72bba622018-11-19 11:17:12 +010061 return FromFraction<1000>(kilobits_per_sec);
62 }
63 template <typename T = int64_t>
64 constexpr T bps() const {
65 return ToValue<T>();
66 }
67 template <typename T = int64_t>
68 T kbps() const {
69 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 Jansson72bba622018-11-19 11:17:12 +010086inline DataRate operator/(const DataSize size, const TimeDelta duration) {
Sebastian Jansson942b3602018-05-30 15:47:44 +020087 return DataRate::bps(data_rate_impl::Microbits(size) / duration.us());
Sebastian Jansson66fa5352018-04-30 16:54:57 +020088}
Sebastian Jansson72bba622018-11-19 11:17:12 +010089inline TimeDelta operator/(const DataSize size, const DataRate rate) {
Sebastian Jansson942b3602018-05-30 15:47:44 +020090 return TimeDelta::us(data_rate_impl::Microbits(size) / rate.bps());
Sebastian Jansson66fa5352018-04-30 16:54:57 +020091}
Sebastian Jansson72bba622018-11-19 11:17:12 +010092inline DataSize operator*(const DataRate rate, const TimeDelta duration) {
Sebastian Jansson942b3602018-05-30 15:47:44 +020093 int64_t microbits = rate.bps() * duration.us();
Sebastian Jansson66fa5352018-04-30 16:54:57 +020094 return DataSize::bytes((microbits + 4000000) / 8000000);
95}
Sebastian Jansson72bba622018-11-19 11:17:12 +010096inline DataSize operator*(const TimeDelta duration, const DataRate rate) {
Sebastian Jansson66fa5352018-04-30 16:54:57 +020097 return rate * duration;
98}
Sebastian Janssone31be152018-04-16 12:41:47 +020099
Sebastian Jansson72bba622018-11-19 11:17:12 +0100100std::string ToString(DataRate value);
Sebastian Janssone31be152018-04-16 12:41:47 +0200101
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200102#ifdef UNIT_TEST
103inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
104 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
105 DataRate value) {
106 return stream << ToString(value);
107}
108#endif // UNIT_TEST
109
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200110} // namespace webrtc
111
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200112#endif // API_UNITS_DATA_RATE_H_