blob: 8e9cc2b5f4ae1b1372de4f1ab11defac013da72f [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
Andrey Logvinb95d90b2020-12-09 12:49:39 +000013#ifdef WEBRTC_UNIT_TEST
Sebastian Jansson26b5e352019-06-07 11:05:31 +020014#include <ostream> // no-presubmit-check TODO(webrtc:8982)
Andrey Logvinb95d90b2020-12-09 12:49:39 +000015#endif // WEBRTC_UNIT_TEST
Sebastian Jansson26b5e352019-06-07 11:05:31 +020016
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:
Danil Chapovalov2517a472020-02-14 13:52:46 +010029 template <typename T>
30 static constexpr Frequency MilliHertz(T value) {
31 static_assert(std::is_arithmetic<T>::value, "");
32 return FromValue(value);
33 }
34 template <typename T>
35 static constexpr Frequency Hertz(T value) {
36 static_assert(std::is_arithmetic<T>::value, "");
37 return FromFraction(1'000, value);
38 }
39 template <typename T>
40 static constexpr Frequency KiloHertz(T value) {
41 static_assert(std::is_arithmetic<T>::value, "");
42 return FromFraction(1'000'000, value);
43 }
44
Sebastian Jansson26b5e352019-06-07 11:05:31 +020045 Frequency() = delete;
Danil Chapovalov2517a472020-02-14 13:52:46 +010046
Sebastian Jansson26b5e352019-06-07 11:05:31 +020047 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010048 constexpr T hertz() const {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020049 return ToFraction<1000, T>();
50 }
51 template <typename T = int64_t>
Sebastian Janssond7fade52020-01-29 10:44:51 +010052 constexpr T millihertz() const {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020053 return ToValue<T>();
54 }
55
56 private:
57 friend class rtc_units_impl::UnitBase<Frequency>;
58 using RelativeUnit::RelativeUnit;
59 static constexpr bool one_sided = true;
60};
61
Sebastian Janssond7fade52020-01-29 10:44:51 +010062inline constexpr Frequency operator/(int64_t nominator,
63 const TimeDelta& interval) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020064 constexpr int64_t kKiloPerMicro = 1000 * 1000000;
65 RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kKiloPerMicro);
66 RTC_CHECK(interval.IsFinite());
67 RTC_CHECK(!interval.IsZero());
Danil Chapovalov2517a472020-02-14 13:52:46 +010068 return Frequency::MilliHertz(nominator * kKiloPerMicro / interval.us());
Sebastian Jansson26b5e352019-06-07 11:05:31 +020069}
70
Sebastian Janssond7fade52020-01-29 10:44:51 +010071inline constexpr TimeDelta operator/(int64_t nominator,
72 const Frequency& frequency) {
Sebastian Jansson26b5e352019-06-07 11:05:31 +020073 constexpr int64_t kMegaPerMilli = 1000000 * 1000;
74 RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kMegaPerMilli);
75 RTC_CHECK(frequency.IsFinite());
76 RTC_CHECK(!frequency.IsZero());
Danil Chapovalov0c626af2020-02-10 11:16:00 +010077 return TimeDelta::Micros(nominator * kMegaPerMilli / frequency.millihertz());
Sebastian Jansson26b5e352019-06-07 11:05:31 +020078}
79
Sebastian Jansson1cf15bf2020-01-29 10:56:25 +010080inline constexpr double operator*(Frequency frequency, TimeDelta time_delta) {
81 return frequency.hertz<double>() * time_delta.seconds<double>();
82}
83inline constexpr double operator*(TimeDelta time_delta, Frequency frequency) {
84 return frequency * time_delta;
85}
86
Sebastian Jansson26b5e352019-06-07 11:05:31 +020087std::string ToString(Frequency value);
88inline std::string ToLogString(Frequency value) {
89 return ToString(value);
90}
91
Andrey Logvinb95d90b2020-12-09 12:49:39 +000092#ifdef WEBRTC_UNIT_TEST
Sebastian Jansson26b5e352019-06-07 11:05:31 +020093inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
94 std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
95 Frequency value) {
96 return stream << ToString(value);
97}
Andrey Logvinb95d90b2020-12-09 12:49:39 +000098#endif // WEBRTC_UNIT_TEST
Sebastian Jansson26b5e352019-06-07 11:05:31 +020099
100} // namespace webrtc
101#endif // API_UNITS_FREQUENCY_H_