data_rate.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. template <typename T>
  30. static constexpr DataRate BitsPerSec(T value) {
  31. static_assert(std::is_arithmetic<T>::value, "");
  32. return FromValue(value);
  33. }
  34. template <typename T>
  35. static constexpr DataRate BytesPerSec(T value) {
  36. static_assert(std::is_arithmetic<T>::value, "");
  37. return FromFraction(8, value);
  38. }
  39. template <typename T>
  40. static constexpr DataRate KilobitsPerSec(T value) {
  41. static_assert(std::is_arithmetic<T>::value, "");
  42. return FromFraction(1000, value);
  43. }
  44. static constexpr DataRate Infinity() { return PlusInfinity(); }
  45. DataRate() = delete;
  46. template <typename T = int64_t>
  47. constexpr T bps() const {
  48. return ToValue<T>();
  49. }
  50. template <typename T = int64_t>
  51. constexpr T bytes_per_sec() const {
  52. return ToFraction<8, T>();
  53. }
  54. template <typename T = int64_t>
  55. constexpr T kbps() const {
  56. return ToFraction<1000, T>();
  57. }
  58. constexpr int64_t bps_or(int64_t fallback_value) const {
  59. return ToValueOr(fallback_value);
  60. }
  61. constexpr int64_t kbps_or(int64_t fallback_value) const {
  62. return ToFractionOr<1000>(fallback_value);
  63. }
  64. private:
  65. // Bits per second used internally to simplify debugging by making the value
  66. // more recognizable.
  67. friend class rtc_units_impl::UnitBase<DataRate>;
  68. using RelativeUnit::RelativeUnit;
  69. static constexpr bool one_sided = true;
  70. };
  71. namespace data_rate_impl {
  72. inline constexpr int64_t Microbits(const DataSize& size) {
  73. constexpr int64_t kMaxBeforeConversion =
  74. std::numeric_limits<int64_t>::max() / 8000000;
  75. RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion)
  76. << "size is too large to be expressed in microbits";
  77. return size.bytes() * 8000000;
  78. }
  79. inline constexpr int64_t MillibytePerSec(const DataRate& size) {
  80. constexpr int64_t kMaxBeforeConversion =
  81. std::numeric_limits<int64_t>::max() / (1000 / 8);
  82. RTC_DCHECK_LE(size.bps(), kMaxBeforeConversion)
  83. << "rate is too large to be expressed in microbytes per second";
  84. return size.bps() * (1000 / 8);
  85. }
  86. } // namespace data_rate_impl
  87. inline constexpr DataRate operator/(const DataSize size,
  88. const TimeDelta duration) {
  89. return DataRate::BitsPerSec(data_rate_impl::Microbits(size) / duration.us());
  90. }
  91. inline constexpr TimeDelta operator/(const DataSize size, const DataRate rate) {
  92. return TimeDelta::Micros(data_rate_impl::Microbits(size) / rate.bps());
  93. }
  94. inline constexpr DataSize operator*(const DataRate rate,
  95. const TimeDelta duration) {
  96. int64_t microbits = rate.bps() * duration.us();
  97. return DataSize::Bytes((microbits + 4000000) / 8000000);
  98. }
  99. inline constexpr DataSize operator*(const TimeDelta duration,
  100. const DataRate rate) {
  101. return rate * duration;
  102. }
  103. inline constexpr DataSize operator/(const DataRate rate,
  104. const Frequency frequency) {
  105. int64_t millihertz = frequency.millihertz<int64_t>();
  106. // Note that the value is truncated here reather than rounded, potentially
  107. // introducing an error of .5 bytes if rounding were expected.
  108. return DataSize::Bytes(data_rate_impl::MillibytePerSec(rate) / millihertz);
  109. }
  110. inline constexpr Frequency operator/(const DataRate rate, const DataSize size) {
  111. return Frequency::MilliHertz(data_rate_impl::MillibytePerSec(rate) /
  112. size.bytes());
  113. }
  114. inline constexpr DataRate operator*(const DataSize size,
  115. const Frequency frequency) {
  116. RTC_DCHECK(frequency.IsZero() ||
  117. size.bytes() <= std::numeric_limits<int64_t>::max() / 8 /
  118. frequency.millihertz<int64_t>());
  119. int64_t millibits_per_second =
  120. size.bytes() * 8 * frequency.millihertz<int64_t>();
  121. return DataRate::BitsPerSec((millibits_per_second + 500) / 1000);
  122. }
  123. inline constexpr DataRate operator*(const Frequency frequency,
  124. const DataSize size) {
  125. return size * frequency;
  126. }
  127. std::string ToString(DataRate value);
  128. inline std::string ToLogString(DataRate value) {
  129. return ToString(value);
  130. }
  131. #ifdef UNIT_TEST
  132. inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
  133. std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
  134. DataRate value) {
  135. return stream << ToString(value);
  136. }
  137. #endif // UNIT_TEST
  138. } // namespace webrtc
  139. #endif // API_UNITS_DATA_RATE_H_