You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.3 KiB

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