diff --git a/modules/congestion_controller/goog_cc/BUILD.gn b/modules/congestion_controller/goog_cc/BUILD.gn index fe8aa54175..858a880cae 100644 --- a/modules/congestion_controller/goog_cc/BUILD.gn +++ b/modules/congestion_controller/goog_cc/BUILD.gn @@ -155,6 +155,7 @@ if (rtc_include_tests) { "../../remote_bitrate_estimator", "../../rtp_rtcp:rtp_rtcp_format", "../network_control", + "../network_control:network_control_test", "//testing/gmock", ] if (!build_with_chromium && is_clang) { diff --git a/modules/congestion_controller/goog_cc/probe_controller_unittest.cc b/modules/congestion_controller/goog_cc/probe_controller_unittest.cc index 7ac6b3f018..89e329bc41 100644 --- a/modules/congestion_controller/goog_cc/probe_controller_unittest.cc +++ b/modules/congestion_controller/goog_cc/probe_controller_unittest.cc @@ -11,6 +11,7 @@ #include "modules/congestion_controller/goog_cc/probe_controller.h" #include "modules/congestion_controller/network_control/include/network_types.h" +#include "modules/congestion_controller/network_control/test/network_ostream_operators.h" #include "rtc_base/logging.h" #include "system_wrappers/include/clock.h" #include "test/gmock.h" diff --git a/modules/congestion_controller/network_control/BUILD.gn b/modules/congestion_controller/network_control/BUILD.gn index 369d9a315a..83641e951d 100644 --- a/modules/congestion_controller/network_control/BUILD.gn +++ b/modules/congestion_controller/network_control/BUILD.gn @@ -26,12 +26,21 @@ rtc_static_library("network_control") { } if (rtc_include_tests) { + rtc_source_set("network_control_test") { + testonly = true + sources = [ + "test/network_ostream_operators.cc", + "test/network_ostream_operators.h", + ] + deps = [ + ":network_control", + ] + } rtc_source_set("network_control_unittests") { testonly = true sources = [ "network_units_unittest.cc", ] - deps = [ ":network_control", "../../../test:test_support", diff --git a/modules/congestion_controller/network_control/include/network_types.h b/modules/congestion_controller/network_control/include/network_types.h index cf90323540..15849c7aca 100644 --- a/modules/congestion_controller/network_control/include/network_types.h +++ b/modules/congestion_controller/network_control/include/network_types.h @@ -11,7 +11,6 @@ #ifndef MODULES_CONGESTION_CONTROLLER_NETWORK_CONTROL_INCLUDE_NETWORK_TYPES_H_ #define MODULES_CONGESTION_CONTROLLER_NETWORK_CONTROL_INCLUDE_NETWORK_TYPES_H_ #include -#include #include #include "modules/congestion_controller/network_control/include/network_units.h" #include "modules/include/module_common_types.h" @@ -144,6 +143,7 @@ struct PacerConfig { // Pacer should send at least pad_window data over time_window duration. DataSize pad_window; DataRate data_rate() const { return data_window / time_window; } + DataRate pad_rate() const { return pad_window / time_window; } }; struct ProbeClusterConfig { @@ -164,10 +164,6 @@ struct TargetTransferRate { struct ProcessInterval { Timestamp at_time; }; - -::std::ostream& operator<<(::std::ostream& os, - const ProbeClusterConfig& config); -::std::ostream& operator<<(::std::ostream& os, const PacerConfig& config); } // namespace webrtc #endif // MODULES_CONGESTION_CONTROLLER_NETWORK_CONTROL_INCLUDE_NETWORK_TYPES_H_ diff --git a/modules/congestion_controller/network_control/include/network_units.h b/modules/congestion_controller/network_control/include/network_units.h index 81054f053c..8145c757a9 100644 --- a/modules/congestion_controller/network_control/include/network_units.h +++ b/modules/congestion_controller/network_control/include/network_units.h @@ -76,9 +76,15 @@ class TimeDelta { TimeDelta operator-(const TimeDelta& other) const { return TimeDelta::us(us() - other.us()); } - TimeDelta operator*(double scalar) const { - return TimeDelta::us(us() * scalar); + TimeDelta& operator-=(const TimeDelta& other) { + microseconds_ -= other.us(); + return *this; } + TimeDelta& operator+=(const TimeDelta& other) { + microseconds_ += other.us(); + return *this; + } + TimeDelta operator*(double scalar) const; TimeDelta operator*(int64_t scalar) const { return TimeDelta::us(us() * scalar); } @@ -121,16 +127,22 @@ inline TimeDelta operator*(const int32_t& scalar, const TimeDelta& delta) { // Timestamp represents the time that has passed since some unspecified epoch. // The epoch is assumed to be before any represented timestamps, this means that // negative values are not valid. The most notable feature is that the -// difference of of two Timestamps results in a TimeDelta. +// difference of two Timestamps results in a TimeDelta. class Timestamp { public: static const Timestamp kPlusInfinity; static const Timestamp kNotInitialized; Timestamp() : Timestamp(kNotInitialized) {} static Timestamp Infinity() { return kPlusInfinity; } - static Timestamp s(int64_t seconds) { return Timestamp(seconds * 1000000); } - static Timestamp ms(int64_t millis) { return Timestamp(millis * 1000); } - static Timestamp us(int64_t micros) { return Timestamp(micros); } + static Timestamp seconds(int64_t seconds) { return Timestamp::s(seconds); } + static Timestamp s(int64_t seconds) { + return Timestamp::us(seconds * 1000000); + } + static Timestamp ms(int64_t millis) { return Timestamp::us(millis * 1000); } + static Timestamp us(int64_t micros) { + RTC_DCHECK_GE(micros, 0); + return Timestamp(micros); + } int64_t s() const { return units_internal::DivideAndRound(us(), 1000000); } int64_t ms() const { return units_internal::DivideAndRound(us(), 1000); } int64_t us() const { @@ -153,6 +165,14 @@ class Timestamp { Timestamp operator+(const TimeDelta& delta) const { return Timestamp::us(us() + delta.us()); } + Timestamp& operator-=(const TimeDelta& other) { + microseconds_ -= other.us(); + return *this; + } + Timestamp& operator+=(const TimeDelta& other) { + microseconds_ += other.us(); + return *this; + } bool operator==(const Timestamp& other) const { return microseconds_ == other.microseconds_; } @@ -181,8 +201,14 @@ class DataSize { DataSize() : DataSize(kNotInitialized) {} static DataSize Zero() { return kZero; } static DataSize Infinity() { return kPlusInfinity; } - static DataSize bytes(int64_t bytes) { return DataSize(bytes); } - static DataSize bits(int64_t bits) { return DataSize(bits / 8); } + static DataSize bytes(int64_t bytes) { + RTC_DCHECK_GE(bytes, 0); + return DataSize(bytes); + } + static DataSize bits(int64_t bits) { + RTC_DCHECK_GE(bits, 0); + return DataSize(bits / 8); + } int64_t bytes() const { RTC_DCHECK(IsFinite()); return bytes_; @@ -204,9 +230,7 @@ class DataSize { DataSize operator+(const DataSize& other) const { return DataSize::bytes(bytes() + other.bytes()); } - DataSize operator*(double scalar) const { - return DataSize::bytes(bytes() * scalar); - } + DataSize operator*(double scalar) const; DataSize operator*(int64_t scalar) const { return DataSize::bytes(bytes() * scalar); } @@ -268,9 +292,11 @@ class DataRate { static DataRate Zero() { return kZero; } static DataRate Infinity() { return kPlusInfinity; } static DataRate bytes_per_second(int64_t bytes_per_sec) { + RTC_DCHECK_GE(bytes_per_sec, 0); return DataRate(bytes_per_sec * 8); } static DataRate bits_per_second(int64_t bits_per_sec) { + RTC_DCHECK_GE(bits_per_sec, 0); return DataRate(bits_per_sec); } static DataRate bps(int64_t bits_per_sec) { @@ -297,9 +323,7 @@ class DataRate { return bits_per_sec_ != kNotInitialized.bits_per_sec_; } bool IsFinite() const { return IsInitialized() && !IsInfinite(); } - DataRate operator*(double scalar) const { - return DataRate::bytes_per_second(bytes_per_second() * scalar); - } + DataRate operator*(double scalar) const; DataRate operator*(int64_t scalar) const { return DataRate::bytes_per_second(bytes_per_second() * scalar); } diff --git a/modules/congestion_controller/network_control/network_types.cc b/modules/congestion_controller/network_control/network_types.cc index e5355598d6..8a0b37e49c 100644 --- a/modules/congestion_controller/network_control/network_types.cc +++ b/modules/congestion_controller/network_control/network_types.cc @@ -51,13 +51,4 @@ std::vector TransportPacketsFeedback::PacketsWithFeedback() return packet_feedbacks; } -::std::ostream& operator<<(::std::ostream& os, - const ProbeClusterConfig& config) { - return os << "ProbeClusterConfig(...)"; -} - -::std::ostream& operator<<(::std::ostream& os, const PacerConfig& config) { - return os << "PacerConfig(...)"; -} - } // namespace webrtc diff --git a/modules/congestion_controller/network_control/network_units.cc b/modules/congestion_controller/network_control/network_units.cc index fd4a30150f..1b01c1e2fb 100644 --- a/modules/congestion_controller/network_control/network_units.cc +++ b/modules/congestion_controller/network_control/network_units.cc @@ -9,6 +9,7 @@ */ #include "modules/congestion_controller/network_control/include/network_units.h" +#include namespace webrtc { namespace { @@ -34,6 +35,18 @@ const DataSize DataSize::kZero = DataSize(0); const DataSize DataSize::kPlusInfinity = DataSize(kPlusInfinityVal); const DataSize DataSize::kNotInitialized = DataSize(kNotInitializedVal); +TimeDelta TimeDelta::operator*(double scalar) const { + return TimeDelta::us(std::round(us() * scalar)); +} + +DataSize DataSize::operator*(double scalar) const { + return DataSize::bytes(std::round(bytes() * scalar)); +} + +DataRate DataRate::operator*(double scalar) const { + return DataRate::bytes_per_second(std::round(bytes_per_second() * scalar)); +} + DataRate operator/(const DataSize& size, const TimeDelta& duration) { RTC_DCHECK(size.bytes() < std::numeric_limits::max() / 1000000) << "size is too large, size: " << size.bytes() << " is not less than " @@ -98,4 +111,5 @@ DataSize operator*(const TimeDelta& duration, const DataRate& rate) { return os << value.ms() << " ms"; } } + } // namespace webrtc diff --git a/modules/congestion_controller/network_control/test/network_ostream_operators.cc b/modules/congestion_controller/network_control/test/network_ostream_operators.cc new file mode 100644 index 0000000000..819628a557 --- /dev/null +++ b/modules/congestion_controller/network_control/test/network_ostream_operators.cc @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2018 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 "modules/congestion_controller/network_control/test/network_ostream_operators.h" + +namespace webrtc { +::std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) + ::std::ostream& os, // no-presubmit-check TODO(webrtc:8982) + const CongestionWindow& window) { + return os << "CongestionWindow(...)"; +} +::std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) + ::std::ostream& os, // no-presubmit-check TODO(webrtc:8982) + const ProbeClusterConfig& config) { + return os << "ProbeClusterConfig(...)"; +} +::std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) + ::std::ostream& os, // no-presubmit-check TODO(webrtc:8982) + const PacerConfig& config) { + return os << "PacerConfig(...)"; +} +::std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) + ::std::ostream& os, // no-presubmit-check TODO(webrtc:8982) + const TargetTransferRate& target_rate) { + return os << "TargetTransferRate(...)"; +} +} // namespace webrtc diff --git a/modules/congestion_controller/network_control/test/network_ostream_operators.h b/modules/congestion_controller/network_control/test/network_ostream_operators.h new file mode 100644 index 0000000000..b7e54b87bd --- /dev/null +++ b/modules/congestion_controller/network_control/test/network_ostream_operators.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018 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 MODULES_CONGESTION_CONTROLLER_NETWORK_CONTROL_TEST_NETWORK_OSTREAM_OPERATORS_H_ +#define MODULES_CONGESTION_CONTROLLER_NETWORK_CONTROL_TEST_NETWORK_OSTREAM_OPERATORS_H_ +// Overloading ostream << operator is required for gtest printing. +#include // no-presubmit-check TODO(webrtc:8982) +#include "modules/congestion_controller/network_control/include/network_types.h" + +namespace webrtc { +::std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) + ::std::ostream& os, // no-presubmit-check TODO(webrtc:8982) + const CongestionWindow& window); +::std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) + ::std::ostream& os, // no-presubmit-check TODO(webrtc:8982) + const ProbeClusterConfig& config); +::std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) + ::std::ostream& os, // no-presubmit-check TODO(webrtc:8982) + const PacerConfig& config); +::std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) + ::std::ostream& os, // no-presubmit-check TODO(webrtc:8982) + const TargetTransferRate& target_rate); +} // namespace webrtc +#endif // MODULES_CONGESTION_CONTROLLER_NETWORK_CONTROL_TEST_NETWORK_OSTREAM_OPERATORS_H_