blob: 7119284874e92b7c54e8d2cda5555892a5a3bf64 [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) {
55 return FromValue(bits_per_second);
Sebastian Jansson942b3602018-05-30 15:47:44 +020056 }
Sebastian Jansson72bba622018-11-19 11:17:12 +010057 template <typename T>
58 static constexpr DataRate kbps(T kilobits_per_sec) {
59 return FromFraction<1000>(kilobits_per_sec);
60 }
61 template <typename T = int64_t>
62 constexpr T bps() const {
63 return ToValue<T>();
64 }
65 template <typename T = int64_t>
66 T kbps() const {
67 return ToFraction<1000, T>();
68 }
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020069 constexpr int64_t bps_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010070 return ToValueOr(fallback_value);
Sebastian Janssonc1c8b8e2018-08-07 15:29:04 +020071 }
72 constexpr int64_t kbps_or(int64_t fallback_value) const {
Sebastian Jansson72bba622018-11-19 11:17:12 +010073 return ToFractionOr<1000>(fallback_value);
Sebastian Jansson30bd4032018-04-13 13:56:17 +020074 }
75
76 private:
77 // Bits per second used internally to simplify debugging by making the value
78 // more recognizable.
Sebastian Jansson72bba622018-11-19 11:17:12 +010079 friend class rtc_units_impl::UnitBase<DataRate>;
80 using RelativeUnit::RelativeUnit;
81 static constexpr bool one_sided = true;
Sebastian Jansson30bd4032018-04-13 13:56:17 +020082};
Sebastian Jansson66fa5352018-04-30 16:54:57 +020083
Sebastian Jansson72bba622018-11-19 11:17:12 +010084inline DataRate operator/(const DataSize size, const TimeDelta duration) {
Sebastian Jansson942b3602018-05-30 15:47:44 +020085 return DataRate::bps(data_rate_impl::Microbits(size) / duration.us());
Sebastian Jansson66fa5352018-04-30 16:54:57 +020086}
Sebastian Jansson72bba622018-11-19 11:17:12 +010087inline TimeDelta operator/(const DataSize size, const DataRate rate) {
Sebastian Jansson942b3602018-05-30 15:47:44 +020088 return TimeDelta::us(data_rate_impl::Microbits(size) / rate.bps());
Sebastian Jansson66fa5352018-04-30 16:54:57 +020089}
Sebastian Jansson72bba622018-11-19 11:17:12 +010090inline DataSize operator*(const DataRate rate, const TimeDelta duration) {
Sebastian Jansson942b3602018-05-30 15:47:44 +020091 int64_t microbits = rate.bps() * duration.us();
Sebastian Jansson66fa5352018-04-30 16:54:57 +020092 return DataSize::bytes((microbits + 4000000) / 8000000);
93}
Sebastian Jansson72bba622018-11-19 11:17:12 +010094inline DataSize operator*(const TimeDelta duration, const DataRate rate) {
Sebastian Jansson66fa5352018-04-30 16:54:57 +020095 return rate * duration;
96}
Sebastian Janssone31be152018-04-16 12:41:47 +020097
Sebastian Jansson72bba622018-11-19 11:17:12 +010098std::string ToString(DataRate value);
Sebastian Janssone31be152018-04-16 12:41:47 +020099
Sebastian Jansson2afd2812018-08-23 14:44:05 +0200100#ifdef UNIT_TEST
101inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
102 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
103 DataRate value) {
104 return stream << ToString(value);
105}
106#endif // UNIT_TEST
107
Sebastian Jansson30bd4032018-04-13 13:56:17 +0200108} // namespace webrtc
109
Sebastian Jansson6fae6ec2018-05-08 10:43:18 +0200110#endif // API_UNITS_DATA_RATE_H_