Adds Frequency unit type.
Bug: webrtc:10674
Change-Id: Ic0ddca46d8522d994bbeba072a73836b506fe40f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/138261
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#28192}
diff --git a/api/units/BUILD.gn b/api/units/BUILD.gn
index e86999d..1f72579 100644
--- a/api/units/BUILD.gn
+++ b/api/units/BUILD.gn
@@ -17,6 +17,7 @@
deps = [
":data_size",
+ ":frequency",
":time_delta",
"..:array_view",
"../../rtc_base:checks",
@@ -55,6 +56,22 @@
]
}
+rtc_source_set("frequency") {
+ visibility = [ "*" ]
+ sources = [
+ "frequency.cc",
+ "frequency.h",
+ ]
+
+ deps = [
+ ":time_delta",
+ "..:array_view",
+ "../../rtc_base:checks",
+ "../../rtc_base:stringutils",
+ "../../rtc_base/units:unit_base",
+ ]
+}
+
rtc_source_set("timestamp") {
visibility = [ "*" ]
sources = [
@@ -77,12 +94,14 @@
sources = [
"data_rate_unittest.cc",
"data_size_unittest.cc",
+ "frequency_unittest.cc",
"time_delta_unittest.cc",
"timestamp_unittest.cc",
]
deps = [
":data_rate",
":data_size",
+ ":frequency",
":time_delta",
":timestamp",
"../../rtc_base:logging",
diff --git a/api/units/data_rate.h b/api/units/data_rate.h
index ccbebdc..3ecdce6 100644
--- a/api/units/data_rate.h
+++ b/api/units/data_rate.h
@@ -20,21 +20,12 @@
#include <type_traits>
#include "api/units/data_size.h"
+#include "api/units/frequency.h"
#include "api/units/time_delta.h"
#include "rtc_base/checks.h"
#include "rtc_base/units/unit_base.h"
namespace webrtc {
-namespace data_rate_impl {
-inline int64_t Microbits(const DataSize& size) {
- constexpr int64_t kMaxBeforeConversion =
- std::numeric_limits<int64_t>::max() / 8000000;
- RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion)
- << "size is too large to be expressed in microbytes";
- return size.bytes() * 8000000;
-}
-} // namespace data_rate_impl
-
// DataRate is a class that represents a given data rate. This can be used to
// represent bandwidth, encoding bitrate, etc. The internal storage is bits per
// second (bps).
@@ -92,6 +83,24 @@
static constexpr bool one_sided = true;
};
+namespace data_rate_impl {
+inline int64_t Microbits(const DataSize& size) {
+ constexpr int64_t kMaxBeforeConversion =
+ std::numeric_limits<int64_t>::max() / 8000000;
+ RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion)
+ << "size is too large to be expressed in microbits";
+ return size.bytes() * 8000000;
+}
+
+inline int64_t MillibytePerSec(const DataRate& size) {
+ constexpr int64_t kMaxBeforeConversion =
+ std::numeric_limits<int64_t>::max() / (1000 / 8);
+ RTC_DCHECK_LE(size.bps(), kMaxBeforeConversion)
+ << "rate is too large to be expressed in microbytes per second";
+ return size.bps() * (1000 / 8);
+}
+} // namespace data_rate_impl
+
inline DataRate operator/(const DataSize size, const TimeDelta duration) {
return DataRate::bps(data_rate_impl::Microbits(size) / duration.us());
}
@@ -106,6 +115,28 @@
return rate * duration;
}
+inline DataSize operator/(const DataRate rate, const Frequency frequency) {
+ int64_t millihertz = frequency.millihertz<int64_t>();
+ // Note that the value is truncated here reather than rounded, potentially
+ // introducing an error of .5 bytes if rounding were expected.
+ return DataSize::bytes(data_rate_impl::MillibytePerSec(rate) / millihertz);
+}
+inline Frequency operator/(const DataRate rate, const DataSize size) {
+ return Frequency::millihertz(data_rate_impl::MillibytePerSec(rate) /
+ size.bytes());
+}
+inline DataRate operator*(const DataSize size, const Frequency frequency) {
+ int64_t millihertz = frequency.millihertz<int64_t>();
+ int64_t kMaxBeforeConversion =
+ std::numeric_limits<int64_t>::max() / 8 / millihertz;
+ RTC_DCHECK_LE(size.bytes(), kMaxBeforeConversion);
+ int64_t millibits_per_second = size.bytes() * 8 * millihertz;
+ return DataRate::bps((millibits_per_second + 500) / 1000);
+}
+inline DataRate operator*(const Frequency frequency, const DataSize size) {
+ return size * frequency;
+}
+
std::string ToString(DataRate value);
inline std::string ToLogString(DataRate value) {
return ToString(value);
diff --git a/api/units/data_rate_unittest.cc b/api/units/data_rate_unittest.cc
index e5bfc57..a56ccb2 100644
--- a/api/units/data_rate_unittest.cc
+++ b/api/units/data_rate_unittest.cc
@@ -161,6 +161,19 @@
EXPECT_EQ((size_c / rate_b).seconds(), kBytes * 8 / kBitsPerSecond);
}
+TEST(UnitConversionTest, DataRateAndDataSizeAndFrequency) {
+ const int64_t kHertz = 30;
+ const int64_t kBitsPerSecond = 96000;
+ const int64_t kBytes = 1200;
+ const Frequency freq_a = Frequency::hertz(kHertz);
+ const DataRate rate_b = DataRate::bps(kBitsPerSecond);
+ const DataSize size_c = DataSize::bytes(kBytes);
+ EXPECT_EQ((freq_a * size_c).bps(), kHertz * kBytes * 8);
+ EXPECT_EQ((size_c * freq_a).bps(), kHertz * kBytes * 8);
+ EXPECT_EQ((rate_b / size_c).hertz<int64_t>(), kBitsPerSecond / kBytes / 8);
+ EXPECT_EQ((rate_b / freq_a).bytes(), kBitsPerSecond / kHertz / 8);
+}
+
TEST(UnitConversionTest, DivisionFailsOnLargeSize) {
// Note that the failure is expected since the current implementation is
// implementated in a way that does not support division of large sizes. If
diff --git a/api/units/frequency.cc b/api/units/frequency.cc
new file mode 100644
index 0000000..f7e38ca
--- /dev/null
+++ b/api/units/frequency.cc
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+#include "api/units/frequency.h"
+#include "rtc_base/strings/string_builder.h"
+
+namespace webrtc {
+std::string ToString(Frequency value) {
+ char buf[64];
+ rtc::SimpleStringBuilder sb(buf);
+ if (value.IsPlusInfinity()) {
+ sb << "+inf Hz";
+ } else if (value.IsMinusInfinity()) {
+ sb << "-inf Hz";
+ } else if (value.millihertz<int64_t>() % 1000 != 0) {
+ sb.AppendFormat("%.3f Hz", value.hertz<double>());
+ } else {
+ sb << value.hertz<int64_t>() << " Hz";
+ }
+ return sb.str();
+}
+} // namespace webrtc
diff --git a/api/units/frequency.h b/api/units/frequency.h
new file mode 100644
index 0000000..e9aa64a
--- /dev/null
+++ b/api/units/frequency.h
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+#ifndef API_UNITS_FREQUENCY_H_
+#define API_UNITS_FREQUENCY_H_
+
+#ifdef UNIT_TEST
+#include <ostream> // no-presubmit-check TODO(webrtc:8982)
+#endif // UNIT_TEST
+
+#include <cstdlib>
+#include <limits>
+#include <string>
+#include <type_traits>
+
+#include "api/units/time_delta.h"
+#include "rtc_base/units/unit_base.h"
+
+namespace webrtc {
+
+class Frequency final : public rtc_units_impl::RelativeUnit<Frequency> {
+ public:
+ Frequency() = delete;
+ template <int64_t hertz>
+ static constexpr Frequency Hertz() {
+ return FromStaticFraction<hertz, 1000>();
+ }
+ template <typename T>
+ static Frequency hertz(T hertz) {
+ static_assert(std::is_arithmetic<T>::value, "");
+ return FromFraction<1000>(hertz);
+ }
+ template <typename T>
+ static Frequency millihertz(T hertz) {
+ static_assert(std::is_arithmetic<T>::value, "");
+ return FromValue(hertz);
+ }
+ template <typename T = int64_t>
+ T hertz() const {
+ return ToFraction<1000, T>();
+ }
+ template <typename T = int64_t>
+ T millihertz() const {
+ return ToValue<T>();
+ }
+
+ private:
+ friend class rtc_units_impl::UnitBase<Frequency>;
+ using RelativeUnit::RelativeUnit;
+ static constexpr bool one_sided = true;
+};
+
+inline Frequency operator/(int64_t nominator, const TimeDelta& interval) {
+ constexpr int64_t kKiloPerMicro = 1000 * 1000000;
+ RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kKiloPerMicro);
+ RTC_CHECK(interval.IsFinite());
+ RTC_CHECK(!interval.IsZero());
+ return Frequency::millihertz(nominator * kKiloPerMicro / interval.us());
+}
+
+inline TimeDelta operator/(int64_t nominator, const Frequency& frequency) {
+ constexpr int64_t kMegaPerMilli = 1000000 * 1000;
+ RTC_DCHECK_LE(nominator, std::numeric_limits<int64_t>::max() / kMegaPerMilli);
+ RTC_CHECK(frequency.IsFinite());
+ RTC_CHECK(!frequency.IsZero());
+ return TimeDelta::us(nominator * kMegaPerMilli / frequency.millihertz());
+}
+
+std::string ToString(Frequency value);
+inline std::string ToLogString(Frequency value) {
+ return ToString(value);
+}
+
+#ifdef UNIT_TEST
+inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
+ std::ostream& stream, // no-presubmit-check TODO(webrtc:8982)
+ Frequency value) {
+ return stream << ToString(value);
+}
+#endif // UNIT_TEST
+
+} // namespace webrtc
+#endif // API_UNITS_FREQUENCY_H_
diff --git a/api/units/frequency_unittest.cc b/api/units/frequency_unittest.cc
new file mode 100644
index 0000000..cabfdfa
--- /dev/null
+++ b/api/units/frequency_unittest.cc
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+#include "api/units/frequency.h"
+
+#include <limits>
+
+#include "test/gtest.h"
+
+namespace webrtc {
+namespace test {
+TEST(FrequencyTest, ConstExpr) {
+ constexpr Frequency kFrequencyZero = Frequency::Zero();
+ constexpr Frequency kFrequencyPlusInf = Frequency::PlusInfinity();
+ constexpr Frequency kFrequencyMinusInf = Frequency::MinusInfinity();
+ static_assert(kFrequencyZero.IsZero(), "");
+ static_assert(kFrequencyPlusInf.IsPlusInfinity(), "");
+ static_assert(kFrequencyMinusInf.IsMinusInfinity(), "");
+
+ static_assert(kFrequencyPlusInf > kFrequencyZero, "");
+}
+
+TEST(FrequencyTest, GetBackSameValues) {
+ const int64_t kValue = 31;
+ EXPECT_EQ(Frequency::hertz(kValue).hertz<int64_t>(), kValue);
+ EXPECT_EQ(Frequency::Zero().hertz<int64_t>(), 0);
+}
+
+TEST(FrequencyTest, GetDifferentPrefix) {
+ const int64_t kValue = 30000;
+ EXPECT_EQ(Frequency::millihertz(kValue).hertz<int64_t>(), kValue / 1000);
+ EXPECT_EQ(Frequency::hertz(kValue).millihertz(), kValue * 1000);
+}
+
+TEST(FrequencyTest, IdentityChecks) {
+ const int64_t kValue = 31;
+ EXPECT_TRUE(Frequency::Zero().IsZero());
+ EXPECT_FALSE(Frequency::hertz(kValue).IsZero());
+
+ EXPECT_TRUE(Frequency::PlusInfinity().IsInfinite());
+ EXPECT_TRUE(Frequency::MinusInfinity().IsInfinite());
+ EXPECT_FALSE(Frequency::Zero().IsInfinite());
+ EXPECT_FALSE(Frequency::hertz(kValue).IsInfinite());
+
+ EXPECT_FALSE(Frequency::PlusInfinity().IsFinite());
+ EXPECT_FALSE(Frequency::MinusInfinity().IsFinite());
+ EXPECT_TRUE(Frequency::hertz(kValue).IsFinite());
+ EXPECT_TRUE(Frequency::Zero().IsFinite());
+
+ EXPECT_TRUE(Frequency::PlusInfinity().IsPlusInfinity());
+ EXPECT_FALSE(Frequency::MinusInfinity().IsPlusInfinity());
+
+ EXPECT_TRUE(Frequency::MinusInfinity().IsMinusInfinity());
+ EXPECT_FALSE(Frequency::PlusInfinity().IsMinusInfinity());
+}
+
+TEST(FrequencyTest, ComparisonOperators) {
+ const int64_t kSmall = 42;
+ const int64_t kLarge = 45;
+ const Frequency small = Frequency::hertz(kSmall);
+ const Frequency large = Frequency::hertz(kLarge);
+
+ EXPECT_EQ(Frequency::Zero(), Frequency::hertz(0));
+ EXPECT_EQ(Frequency::PlusInfinity(), Frequency::PlusInfinity());
+ EXPECT_EQ(small, Frequency::hertz(kSmall));
+ EXPECT_LE(small, Frequency::hertz(kSmall));
+ EXPECT_GE(small, Frequency::hertz(kSmall));
+ EXPECT_NE(small, Frequency::hertz(kLarge));
+ EXPECT_LE(small, Frequency::hertz(kLarge));
+ EXPECT_LT(small, Frequency::hertz(kLarge));
+ EXPECT_GE(large, Frequency::hertz(kSmall));
+ EXPECT_GT(large, Frequency::hertz(kSmall));
+ EXPECT_LT(Frequency::Zero(), small);
+
+ EXPECT_GT(Frequency::PlusInfinity(), large);
+ EXPECT_LT(Frequency::MinusInfinity(), Frequency::Zero());
+}
+
+TEST(FrequencyTest, Clamping) {
+ const Frequency upper = Frequency::hertz(800);
+ const Frequency lower = Frequency::hertz(100);
+ const Frequency under = Frequency::hertz(100);
+ const Frequency inside = Frequency::hertz(500);
+ const Frequency over = Frequency::hertz(1000);
+ EXPECT_EQ(under.Clamped(lower, upper), lower);
+ EXPECT_EQ(inside.Clamped(lower, upper), inside);
+ EXPECT_EQ(over.Clamped(lower, upper), upper);
+
+ Frequency mutable_frequency = lower;
+ mutable_frequency.Clamp(lower, upper);
+ EXPECT_EQ(mutable_frequency, lower);
+ mutable_frequency = inside;
+ mutable_frequency.Clamp(lower, upper);
+ EXPECT_EQ(mutable_frequency, inside);
+ mutable_frequency = over;
+ mutable_frequency.Clamp(lower, upper);
+ EXPECT_EQ(mutable_frequency, upper);
+}
+
+TEST(FrequencyTest, MathOperations) {
+ const int64_t kValueA = 457;
+ const int64_t kValueB = 260;
+ const Frequency frequency_a = Frequency::hertz(kValueA);
+ const Frequency frequency_b = Frequency::hertz(kValueB);
+ EXPECT_EQ((frequency_a + frequency_b).hertz<int64_t>(), kValueA + kValueB);
+ EXPECT_EQ((frequency_a - frequency_b).hertz<int64_t>(), kValueA - kValueB);
+
+ EXPECT_EQ((Frequency::hertz(kValueA) * kValueB).hertz<int64_t>(),
+ kValueA * kValueB);
+
+ EXPECT_EQ((frequency_b / 10).hertz<int64_t>(), kValueB / 10);
+ EXPECT_EQ(frequency_b / frequency_a, static_cast<double>(kValueB) / kValueA);
+
+ Frequency mutable_frequency = Frequency::hertz(kValueA);
+ mutable_frequency += Frequency::hertz(kValueB);
+ EXPECT_EQ(mutable_frequency, Frequency::hertz(kValueA + kValueB));
+ mutable_frequency -= Frequency::hertz(kValueB);
+ EXPECT_EQ(mutable_frequency, Frequency::hertz(kValueA));
+}
+TEST(FrequencyTest, Rounding) {
+ const Frequency freq_high = Frequency::hertz(23.976);
+ EXPECT_EQ(freq_high.hertz(), 24);
+ EXPECT_EQ(freq_high.RoundDownTo(Frequency::hertz(1)), Frequency::hertz(23));
+ EXPECT_EQ(freq_high.RoundTo(Frequency::hertz(1)), Frequency::hertz(24));
+ EXPECT_EQ(freq_high.RoundUpTo(Frequency::hertz(1)), Frequency::hertz(24));
+
+ const Frequency freq_low = Frequency::hertz(23.4);
+ EXPECT_EQ(freq_low.hertz(), 23);
+ EXPECT_EQ(freq_low.RoundDownTo(Frequency::hertz(1)), Frequency::hertz(23));
+ EXPECT_EQ(freq_low.RoundTo(Frequency::hertz(1)), Frequency::hertz(23));
+ EXPECT_EQ(freq_low.RoundUpTo(Frequency::hertz(1)), Frequency::hertz(24));
+}
+
+TEST(FrequencyTest, InfinityOperations) {
+ const double kValue = 267;
+ const Frequency finite = Frequency::hertz(kValue);
+ EXPECT_TRUE((Frequency::PlusInfinity() + finite).IsPlusInfinity());
+ EXPECT_TRUE((Frequency::PlusInfinity() - finite).IsPlusInfinity());
+ EXPECT_TRUE((finite + Frequency::PlusInfinity()).IsPlusInfinity());
+ EXPECT_TRUE((finite - Frequency::MinusInfinity()).IsPlusInfinity());
+
+ EXPECT_TRUE((Frequency::MinusInfinity() + finite).IsMinusInfinity());
+ EXPECT_TRUE((Frequency::MinusInfinity() - finite).IsMinusInfinity());
+ EXPECT_TRUE((finite + Frequency::MinusInfinity()).IsMinusInfinity());
+ EXPECT_TRUE((finite - Frequency::PlusInfinity()).IsMinusInfinity());
+}
+
+TEST(UnitConversionTest, TimeDeltaAndFrequency) {
+ EXPECT_EQ(1 / Frequency::hertz(50), TimeDelta::ms(20));
+ EXPECT_EQ(1 / TimeDelta::ms(20), Frequency::hertz(50));
+}
+} // namespace test
+} // namespace webrtc
diff --git a/api/units/time_delta.cc b/api/units/time_delta.cc
index 9fe1308..31bf3e0 100644
--- a/api/units/time_delta.cc
+++ b/api/units/time_delta.cc
@@ -32,4 +32,5 @@
}
return sb.str();
}
+
} // namespace webrtc
diff --git a/api/units/time_delta.h b/api/units/time_delta.h
index f8c6af4..4ab83ec 100644
--- a/api/units/time_delta.h
+++ b/api/units/time_delta.h
@@ -22,6 +22,7 @@
#include "rtc_base/units/unit_base.h"
namespace webrtc {
+
// TimeDelta represents the difference between two timestamps. Commonly this can
// be a duration. However since two Timestamps are not guaranteed to have the
// same epoch (they might come from different computers, making exact
diff --git a/rtc_base/units/unit_base.h b/rtc_base/units/unit_base.h
index 37b60a0..5bcc0d8 100644
--- a/rtc_base/units/unit_base.h
+++ b/rtc_base/units/unit_base.h
@@ -67,6 +67,26 @@
constexpr bool operator<(const Unit_T& other) const {
return value_ < other.value_;
}
+ Unit_T RoundTo(const Unit_T& resolution) const {
+ RTC_DCHECK(IsFinite());
+ RTC_DCHECK(resolution.IsFinite());
+ RTC_DCHECK_GT(resolution.value_, 0);
+ return Unit_T((value_ + resolution.value_ / 2) / resolution.value_) *
+ resolution.value_;
+ }
+ Unit_T RoundUpTo(const Unit_T& resolution) const {
+ RTC_DCHECK(IsFinite());
+ RTC_DCHECK(resolution.IsFinite());
+ RTC_DCHECK_GT(resolution.value_, 0);
+ return Unit_T((value_ + resolution.value_ - 1) / resolution.value_) *
+ resolution.value_;
+ }
+ Unit_T RoundDownTo(const Unit_T& resolution) const {
+ RTC_DCHECK(IsFinite());
+ RTC_DCHECK(resolution.IsFinite());
+ RTC_DCHECK_GT(resolution.value_, 0);
+ return Unit_T(value_ / resolution.value_) * resolution.value_;
+ }
protected:
template <int64_t value>