blob: 45561f4aee2b79c20515bd046bfbb27549c68d4a [file] [log] [blame]
Sebastian Jansson26b5e352019-06-07 11:05:31 +02001/*
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
13#ifdef UNIT_TEST
14#include <ostream> // no-presubmit-check TODO(webrtc:8982)
15#endif // UNIT_TEST
16
17#include <cstdlib>
18#include <limits>
19#include <string>
20#include <type_traits>
21
22#include "api/units/time_delta.h"
23#include "rtc_base/units/unit_base.h"
24
25namespace webrtc {
26
27class Frequency final : public rtc_units_impl::RelativeUnit<Frequency> {
28 public:
29 Frequency() = delete;
30 template <int64_t hertz>
31 static constexpr Frequency Hertz() {
Danil Chapovalov7356a562020-01-20 13:02:44 +010032 return FromFraction(1000, hertz);
Sebastian Jansson26b5e352019-06-07 11:05:31 +020033 }
34 template <typename T>
Sebastian Janssond7fade52020-01-29 10:44:51 +010035 static constexpr Frequency hertz(T hertz) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020036 static_assert(std::is_arithmetic<T>::value, "");
Danil Chapovalov7356a562020-01-20 13:02:44 +010037 return FromFraction(1000, hertz);
Sebastian Jansson26b5e352019-06-07 11:05:31 +020038 }
39 template <typename T>
Sebastian Janssond7fade52020-01-29 10:44:51 +010040 static constexpr Frequency millihertz(T hertz) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020041 static_assert(std::is_arithmetic<T>::value, "");
42 return FromValue(hertz);
43 }
44 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010045 constexpr T hertz() const {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020046 return ToFraction<1000, T>();
47 }
48 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010049 constexpr T millihertz() const {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020050 return ToValue<T>();
51 }
52
53 private:
54 friend class rtc_units_impl::UnitBase<Frequency>;
55 using RelativeUnit::RelativeUnit;
56 static constexpr bool one_sided = true;
57};
58
Sebastian Janssond7fade52020-01-29 10:44:51 +010059inline constexpr Frequency operator/(int64_t nominator,
60 const TimeDelta& interval) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020061 constexpr int64_t kKiloPerMicro = 1000 * 1000000;
62 RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kKiloPerMicro);
63 RTC_CHECK(interval.IsFinite());
64 RTC_CHECK(!interval.IsZero());
65 return Frequency::millihertz(nominator * kKiloPerMicro / interval.us());
66}
67
Sebastian Janssond7fade52020-01-29 10:44:51 +010068inline constexpr TimeDelta operator/(int64_t nominator,
69 const Frequency& frequency) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020070 constexpr int64_t kMegaPerMilli = 1000000 * 1000;
71 RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kMegaPerMilli);
72 RTC_CHECK(frequency.IsFinite());
73 RTC_CHECK(!frequency.IsZero());
74 return TimeDelta::us(nominator * kMegaPerMilli / frequency.millihertz());
75}
76
77std::string ToString(Frequency value);
78inline std::string ToLogString(Frequency value) {
79 return ToString(value);
80}
81
82#ifdef UNIT_TEST
83inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
84 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
85 Frequency value) {
86 return stream << ToString(value);
87}
88#endif // UNIT_TEST
89
90} // namespace webrtc
91#endif // API_UNITS_FREQUENCY_H_