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.

89 lines
2.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2019 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_FREQUENCY_H_
  11. #define API_UNITS_FREQUENCY_H_
  12. #ifdef UNIT_TEST
  13. #include <ostream> // no-presubmit-check TODO(webrtc:8982)
  14. #endif // UNIT_TEST
  15. #include <cstdlib>
  16. #include <limits>
  17. #include <string>
  18. #include <type_traits>
  19. #include "api/units/time_delta.h"
  20. #include "rtc_base/units/unit_base.h"
  21. namespace webrtc {
  22. class Frequency final : public rtc_units_impl::RelativeUnit<Frequency> {
  23. public:
  24. Frequency() = delete;
  25. template <int64_t hertz>
  26. static constexpr Frequency Hertz() {
  27. return FromStaticFraction<hertz, 1000>();
  28. }
  29. template <typename T>
  30. static Frequency hertz(T hertz) {
  31. static_assert(std::is_arithmetic<T>::value, "");
  32. return FromFraction<1000>(hertz);
  33. }
  34. template <typename T>
  35. static Frequency millihertz(T hertz) {
  36. static_assert(std::is_arithmetic<T>::value, "");
  37. return FromValue(hertz);
  38. }
  39. template <typename T = int64_t>
  40. T hertz() const {
  41. return ToFraction<1000, T>();
  42. }
  43. template <typename T = int64_t>
  44. T millihertz() const {
  45. return ToValue<T>();
  46. }
  47. private:
  48. friend class rtc_units_impl::UnitBase<Frequency>;
  49. using RelativeUnit::RelativeUnit;
  50. static constexpr bool one_sided = true;
  51. };
  52. inline Frequency operator/(int64_t nominator, const TimeDelta& interval) {
  53. constexpr int64_t kKiloPerMicro = 1000 * 1000000;
  54. RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kKiloPerMicro);
  55. RTC_CHECK(interval.IsFinite());
  56. RTC_CHECK(!interval.IsZero());
  57. return Frequency::millihertz(nominator * kKiloPerMicro / interval.us());
  58. }
  59. inline TimeDelta operator/(int64_t nominator, const Frequency& frequency) {
  60. constexpr int64_t kMegaPerMilli = 1000000 * 1000;
  61. RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kMegaPerMilli);
  62. RTC_CHECK(frequency.IsFinite());
  63. RTC_CHECK(!frequency.IsZero());
  64. return TimeDelta::us(nominator * kMegaPerMilli / frequency.millihertz());
  65. }
  66. std::string ToString(Frequency value);
  67. inline std::string ToLogString(Frequency value) {
  68. return ToString(value);
  69. }
  70. #ifdef UNIT_TEST
  71. inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
  72. std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
  73. Frequency value) {
  74. return stream << ToString(value);
  75. }
  76. #endif // UNIT_TEST
  77. } // namespace webrtc
  78. #endif // API_UNITS_FREQUENCY_H_