commit 2d95a8c13582f2991b4af251f231bf1a09d6406c parent 180ed3aebb002386420d0c005cbf64d229035692 Author: Dan Baker <dbaker@mozilla.com> Date: Mon, 27 Oct 2025 14:33:14 -0600 Bug 1995393 - Vendor libwebrtc from f14761075c Upstream commit: https://webrtc.googlesource.com/src/+/f14761075c23dc5e0d68fd410d584bccec79e90b Use injected clock in Smoothing filters in OPUS encoder Bug: webrtc:42223992 Change-Id: Ib98b387bc4647dc05b8b30c6bd2497ded721c950 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/406640 Reviewed-by: Harald Alvestrand <hta@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/main@{#45464} Diffstat:
18 files changed, 125 insertions(+), 101 deletions(-)
diff --git a/third_party/libwebrtc/README.mozilla.last-vendor b/third_party/libwebrtc/README.mozilla.last-vendor @@ -1,4 +1,4 @@ # ./mach python dom/media/webrtc/third_party_build/vendor-libwebrtc.py --from-local /Users/danielbaker/elm/.moz-fast-forward/moz-libwebrtc --commit mozpatches libwebrtc -libwebrtc updated from /Users/danielbaker/elm/.moz-fast-forward/moz-libwebrtc commit mozpatches on 2025-10-27T20:31:01.527389+00:00. +libwebrtc updated from /Users/danielbaker/elm/.moz-fast-forward/moz-libwebrtc commit mozpatches on 2025-10-27T20:33:02.592695+00:00. # base of lastest vendoring -be54081491 +f14761075c diff --git a/third_party/libwebrtc/common_audio/BUILD.gn b/third_party/libwebrtc/common_audio/BUILD.gn @@ -47,13 +47,13 @@ rtc_library("common_audio") { ":sinc_resampler", "../api:array_view", "../api/audio:audio_frame_api", + "../api/units:timestamp", "../rtc_base:checks", "../rtc_base:cpu_info", "../rtc_base:gtest_prod", "../rtc_base:logging", "../rtc_base:safe_conversions", "../rtc_base:sanitizer", - "../rtc_base:timeutils", "../rtc_base/memory:aligned_malloc", "../rtc_base/system:arch", "../rtc_base/system:file_wrapper", @@ -82,6 +82,7 @@ rtc_source_set("mock_common_audio") { ] deps = [ ":common_audio", + "../api/units:timestamp", "../test:test_support", ] } @@ -371,6 +372,7 @@ if (rtc_include_tests && !build_with_chromium) { ":sinc_resampler", "../api/audio:audio_frame_api", "../api/units:time_delta", + "../api/units:timestamp", "../rtc_base:checks", "../rtc_base:cpu_info", "../rtc_base:logging", diff --git a/third_party/libwebrtc/common_audio/mocks/mock_smoothing_filter.h b/third_party/libwebrtc/common_audio/mocks/mock_smoothing_filter.h @@ -13,6 +13,7 @@ #include <optional> +#include "api/units/timestamp.h" #include "common_audio/smoothing_filter.h" #include "test/gmock.h" @@ -20,8 +21,8 @@ namespace webrtc { class MockSmoothingFilter : public SmoothingFilter { public: - MOCK_METHOD(void, AddSample, (float), (override)); - MOCK_METHOD(std::optional<float>, GetAverage, (), (override)); + MOCK_METHOD(void, AddSample, (float, Timestamp), (override)); + MOCK_METHOD(std::optional<float>, GetAverage, (Timestamp), (override)); MOCK_METHOD(bool, SetTimeConstantMs, (int), (override)); }; diff --git a/third_party/libwebrtc/common_audio/smoothing_filter.cc b/third_party/libwebrtc/common_audio/smoothing_filter.cc @@ -14,8 +14,8 @@ #include <cstdint> #include <optional> +#include "api/units/timestamp.h" #include "rtc_base/checks.h" -#include "rtc_base/time_utils.h" namespace webrtc { @@ -39,8 +39,8 @@ SmoothingFilterImpl::SmoothingFilterImpl(int init_time_ms) SmoothingFilterImpl::~SmoothingFilterImpl() = default; -void SmoothingFilterImpl::AddSample(float sample) { - const int64_t now_ms = TimeMillis(); +void SmoothingFilterImpl::AddSample(float sample, Timestamp now) { + const int64_t now_ms = now.ms(); if (!init_end_time_ms_) { // This is equivalent to assuming the filter has been receiving the same @@ -55,12 +55,12 @@ void SmoothingFilterImpl::AddSample(float sample) { last_sample_ = sample; } -std::optional<float> SmoothingFilterImpl::GetAverage() { +std::optional<float> SmoothingFilterImpl::GetAverage(Timestamp now) { if (!init_end_time_ms_) { // `init_end_time_ms_` undefined since we have not received any sample. return std::nullopt; } - ExtrapolateLastSample(TimeMillis()); + ExtrapolateLastSample(now.ms()); return state_; } diff --git a/third_party/libwebrtc/common_audio/smoothing_filter.h b/third_party/libwebrtc/common_audio/smoothing_filter.h @@ -11,17 +11,18 @@ #ifndef COMMON_AUDIO_SMOOTHING_FILTER_H_ #define COMMON_AUDIO_SMOOTHING_FILTER_H_ -#include <stdint.h> - +#include <cstdint> #include <optional> +#include "api/units/timestamp.h" + namespace webrtc { class SmoothingFilter { public: virtual ~SmoothingFilter() = default; - virtual void AddSample(float sample) = 0; - virtual std::optional<float> GetAverage() = 0; + virtual void AddSample(float sample, Timestamp now) = 0; + virtual std::optional<float> GetAverage(Timestamp now) = 0; virtual bool SetTimeConstantMs(int time_constant_ms) = 0; }; @@ -48,8 +49,8 @@ class SmoothingFilterImpl final : public SmoothingFilter { ~SmoothingFilterImpl() override; - void AddSample(float sample) override; - std::optional<float> GetAverage() override; + void AddSample(float sample, Timestamp now) override; + std::optional<float> GetAverage(Timestamp now) override; bool SetTimeConstantMs(int time_constant_ms) override; // Methods used for unittests. diff --git a/third_party/libwebrtc/common_audio/smoothing_filter_unittest.cc b/third_party/libwebrtc/common_audio/smoothing_filter_unittest.cc @@ -11,25 +11,25 @@ #include "common_audio/smoothing_filter.h" #include <cmath> -#include <cstdint> +#include <optional> #include "api/units/time_delta.h" -#include "rtc_base/fake_clock.h" +#include "api/units/timestamp.h" +#include "test/gmock.h" #include "test/gtest.h" namespace webrtc { - namespace { +using ::testing::FloatNear; +using ::testing::Optional; + constexpr float kMaxAbsError = 1e-5f; -constexpr int64_t kClockInitialTime = 123456; struct SmoothingFilterStates { explicit SmoothingFilterStates(int init_time_ms) - : smoothing_filter(init_time_ms) { - fake_clock.AdvanceTime(TimeDelta::Millis(kClockInitialTime)); - } - ScopedFakeClock fake_clock; + : now(Timestamp::Millis(123'456)), smoothing_filter(init_time_ms) {} + Timestamp now; SmoothingFilterImpl smoothing_filter; }; @@ -42,11 +42,10 @@ void CheckOutput(SmoothingFilterStates* states, float sample, int advance_time_ms, float expected_ouput) { - states->smoothing_filter.AddSample(sample); - states->fake_clock.AdvanceTime(TimeDelta::Millis(advance_time_ms)); - auto output = states->smoothing_filter.GetAverage(); - EXPECT_TRUE(output); - EXPECT_NEAR(expected_ouput, *output, kMaxAbsError); + states->smoothing_filter.AddSample(sample, states->now); + states->now += TimeDelta::Millis(advance_time_ms); + EXPECT_THAT(states->smoothing_filter.GetAverage(states->now), + Optional(FloatNear(expected_ouput, kMaxAbsError))); } } // namespace @@ -54,7 +53,7 @@ void CheckOutput(SmoothingFilterStates* states, TEST(SmoothingFilterTest, NoOutputWhenNoSampleAdded) { constexpr int kInitTimeMs = 100; SmoothingFilterStates states(kInitTimeMs); - EXPECT_FALSE(states.smoothing_filter.GetAverage()); + EXPECT_EQ(states.smoothing_filter.GetAverage(states.now), std::nullopt); } // Python script to calculate the reference values used in this test. @@ -131,27 +130,27 @@ TEST(SmoothingFilterTest, InitTimeEqualsOne) { TEST(SmoothingFilterTest, GetAverageOutputsEmptyBeforeFirstSample) { constexpr int kInitTimeMs = 100; SmoothingFilterStates states(kInitTimeMs); - EXPECT_FALSE(states.smoothing_filter.GetAverage()); + EXPECT_FALSE(states.smoothing_filter.GetAverage(states.now)); constexpr float kFirstSample = 1.2345f; - states.smoothing_filter.AddSample(kFirstSample); - EXPECT_EQ(kFirstSample, states.smoothing_filter.GetAverage()); + states.smoothing_filter.AddSample(kFirstSample, states.now); + EXPECT_EQ(kFirstSample, states.smoothing_filter.GetAverage(states.now)); } TEST(SmoothingFilterTest, CannotChangeTimeConstantDuringInitialization) { constexpr int kInitTimeMs = 100; SmoothingFilterStates states(kInitTimeMs); - states.smoothing_filter.AddSample(0.0); + states.smoothing_filter.AddSample(0.0, states.now); // During initialization, `SetTimeConstantMs` does not take effect. - states.fake_clock.AdvanceTime(TimeDelta::Millis(kInitTimeMs - 1)); - states.smoothing_filter.AddSample(0.0); + states.now += TimeDelta::Millis(kInitTimeMs - 1); + states.smoothing_filter.AddSample(0.0, states.now); EXPECT_FALSE(states.smoothing_filter.SetTimeConstantMs(kInitTimeMs * 2)); EXPECT_NE(std::exp(-1.0f / (kInitTimeMs * 2)), states.smoothing_filter.alpha()); - states.fake_clock.AdvanceTime(TimeDelta::Millis(1)); - states.smoothing_filter.AddSample(0.0); + states.now += TimeDelta::Millis(1); + states.smoothing_filter.AddSample(0.0, states.now); // When initialization finishes, the time constant should be come // `kInitTimeConstantMs`. EXPECT_FLOAT_EQ(std::exp(-1.0f / kInitTimeMs), diff --git a/third_party/libwebrtc/modules/audio_coding/BUILD.gn b/third_party/libwebrtc/modules/audio_coding/BUILD.gn @@ -289,6 +289,7 @@ rtc_library("webrtc_opus") { "../../api/audio_codecs/opus:audio_encoder_opus_config", "../../api/environment", "../../api/units:time_delta", + "../../api/units:timestamp", "../../common_audio", "../../rtc_base:buffer", "../../rtc_base:checks", @@ -298,7 +299,6 @@ rtc_library("webrtc_opus") { "../../rtc_base:safe_conversions", "../../rtc_base:safe_minmax", "../../rtc_base:stringutils", - "../../rtc_base:timeutils", "//third_party/abseil-cpp/absl/memory", "//third_party/abseil-cpp/absl/strings", "//third_party/abseil-cpp/absl/strings:string_view", @@ -818,7 +818,6 @@ if (rtc_include_tests) { ":acm_send_test", ":audio_codec_speed_tests", ":audio_decoder_unittests", - ":audio_decoder_unittests", ":g711_test", ":g722_test", ":neteq_opus_quality_test", diff --git a/third_party/libwebrtc/modules/audio_coding/audio_network_adaptor/controller_manager.cc b/third_party/libwebrtc/modules/audio_coding/audio_network_adaptor/controller_manager.cc @@ -52,6 +52,7 @@ namespace { #if WEBRTC_ENABLE_PROTOBUF std::unique_ptr<FecControllerPlrBased> CreateFecControllerPlrBased( + const Environment& env, const audio_network_adaptor::config::FecController& config, bool initial_fec_enabled) { RTC_CHECK(config.has_fec_enabling_threshold()); @@ -70,8 +71,9 @@ std::unique_ptr<FecControllerPlrBased> CreateFecControllerPlrBased( RTC_CHECK(fec_disabling_threshold.has_high_bandwidth_bps()); RTC_CHECK(fec_disabling_threshold.has_high_bandwidth_packet_loss()); - return std::unique_ptr<FecControllerPlrBased>( - new FecControllerPlrBased(FecControllerPlrBased::Config( + return std::make_unique<FecControllerPlrBased>( + env, + FecControllerPlrBased::Config( initial_fec_enabled, ThresholdCurve(fec_enabling_threshold.low_bandwidth_bps(), fec_enabling_threshold.low_bandwidth_packet_loss(), @@ -81,7 +83,7 @@ std::unique_ptr<FecControllerPlrBased> CreateFecControllerPlrBased( fec_disabling_threshold.low_bandwidth_packet_loss(), fec_disabling_threshold.high_bandwidth_bps(), fec_disabling_threshold.high_bandwidth_packet_loss()), - config.time_constant_ms()))); + config.time_constant_ms())); } std::unique_ptr<FrameLengthController> CreateFrameLengthController( @@ -255,7 +257,7 @@ std::unique_ptr<ControllerManager> ControllerManagerImpl::Create( switch (controller_config.controller_case()) { case audio_network_adaptor::config::Controller::kFecController: controller = CreateFecControllerPlrBased( - controller_config.fec_controller(), initial_fec_enabled); + env, controller_config.fec_controller(), initial_fec_enabled); break; case audio_network_adaptor::config::Controller::kFecControllerRplrBased: // FecControllerRplrBased has been removed and can't be used anymore. diff --git a/third_party/libwebrtc/modules/audio_coding/audio_network_adaptor/fec_controller_plr_based.cc b/third_party/libwebrtc/modules/audio_coding/audio_network_adaptor/fec_controller_plr_based.cc @@ -14,6 +14,7 @@ #include <optional> #include <utility> +#include "api/environment/environment.h" #include "common_audio/smoothing_filter.h" #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h" #include "modules/audio_coding/audio_network_adaptor/util/threshold_curve.h" @@ -32,16 +33,20 @@ FecControllerPlrBased::Config::Config( time_constant_ms(time_constant_ms) {} FecControllerPlrBased::FecControllerPlrBased( + const Environment& env, const Config& config, std::unique_ptr<SmoothingFilter> smoothing_filter) - : config_(config), + : env_(env), + config_(config), fec_enabled_(config.initial_fec_enabled), packet_loss_smoother_(std::move(smoothing_filter)) { RTC_DCHECK(config_.fec_disabling_threshold <= config_.fec_enabling_threshold); } -FecControllerPlrBased::FecControllerPlrBased(const Config& config) +FecControllerPlrBased::FecControllerPlrBased(const Environment& env, + const Config& config) : FecControllerPlrBased( + env, config, std::make_unique<SmoothingFilterImpl>(config.time_constant_ms)) {} @@ -53,7 +58,8 @@ void FecControllerPlrBased::UpdateNetworkMetrics( uplink_bandwidth_bps_ = network_metrics.uplink_bandwidth_bps; if (network_metrics.uplink_packet_loss_fraction) { packet_loss_smoother_->AddSample( - *network_metrics.uplink_packet_loss_fraction); + *network_metrics.uplink_packet_loss_fraction, + env_.clock().CurrentTime()); } } @@ -61,7 +67,8 @@ void FecControllerPlrBased::MakeDecision(AudioEncoderRuntimeConfig* config) { RTC_DCHECK(!config->enable_fec); RTC_DCHECK(!config->uplink_packet_loss_fraction); - const auto& packet_loss = packet_loss_smoother_->GetAverage(); + const auto& packet_loss = + packet_loss_smoother_->GetAverage(env_.clock().CurrentTime()); fec_enabled_ = fec_enabled_ ? !FecDisablingDecision(packet_loss) : FecEnablingDecision(packet_loss); diff --git a/third_party/libwebrtc/modules/audio_coding/audio_network_adaptor/fec_controller_plr_based.h b/third_party/libwebrtc/modules/audio_coding/audio_network_adaptor/fec_controller_plr_based.h @@ -14,6 +14,7 @@ #include <memory> #include <optional> +#include "api/environment/environment.h" #include "common_audio/smoothing_filter.h" #include "modules/audio_coding/audio_network_adaptor/controller.h" #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h" @@ -45,10 +46,11 @@ class FecControllerPlrBased final : public Controller { }; // Dependency injection for testing. - FecControllerPlrBased(const Config& config, + FecControllerPlrBased(const Environment& env, + const Config& config, std::unique_ptr<SmoothingFilter> smoothing_filter); - explicit FecControllerPlrBased(const Config& config); + explicit FecControllerPlrBased(const Environment& env, const Config& config); ~FecControllerPlrBased() override; @@ -63,6 +65,7 @@ class FecControllerPlrBased final : public Controller { bool FecEnablingDecision(const std::optional<float>& packet_loss) const; bool FecDisablingDecision(const std::optional<float>& packet_loss) const; + const Environment env_; const Config config_; bool fec_enabled_; std::optional<int> uplink_bandwidth_bps_; diff --git a/third_party/libwebrtc/modules/audio_coding/audio_network_adaptor/fec_controller_plr_based_unittest.cc b/third_party/libwebrtc/modules/audio_coding/audio_network_adaptor/fec_controller_plr_based_unittest.cc @@ -20,17 +20,17 @@ #include "modules/audio_coding/audio_network_adaptor/include/audio_network_adaptor_config.h" #include "modules/audio_coding/audio_network_adaptor/util/threshold_curve.h" #include "rtc_base/checks.h" +#include "test/create_test_environment.h" #include "test/gmock.h" #include "test/gtest.h" namespace webrtc { +namespace { using ::testing::_; using ::testing::NiceMock; using ::testing::Return; -namespace { - // The test uses the following settings: // // packet-loss ^ | | @@ -66,13 +66,14 @@ FecControllerPlrBasedTestStates CreateFecControllerPlrBased( const ThresholdCurve& enabling_curve, const ThresholdCurve& disabling_curve) { FecControllerPlrBasedTestStates states; - std::unique_ptr<MockSmoothingFilter> mock_smoothing_filter( - new NiceMock<MockSmoothingFilter>()); + auto mock_smoothing_filter = + std::make_unique<NiceMock<MockSmoothingFilter>>(); states.packet_loss_smoother = mock_smoothing_filter.get(); - states.controller.reset(new FecControllerPlrBased( + states.controller = std::make_unique<FecControllerPlrBased>( + CreateTestEnvironment(), FecControllerPlrBased::Config(initial_fec_enabled, enabling_curve, disabling_curve, 0), - std::move(mock_smoothing_filter))); + std::move(mock_smoothing_filter)); return states; } @@ -100,10 +101,11 @@ void UpdateNetworkMetrics(FecControllerPlrBasedTestStates* states, if (uplink_packet_loss) { Controller::NetworkMetrics network_metrics; network_metrics.uplink_packet_loss_fraction = uplink_packet_loss; - EXPECT_CALL(*states->packet_loss_smoother, AddSample(*uplink_packet_loss)); + EXPECT_CALL(*states->packet_loss_smoother, + AddSample(*uplink_packet_loss, _)); states->controller->UpdateNetworkMetrics(network_metrics); // This is called during CheckDecision(). - EXPECT_CALL(*states->packet_loss_smoother, GetAverage()) + EXPECT_CALL(*states->packet_loss_smoother, GetAverage) .WillOnce(Return(*uplink_packet_loss)); } } @@ -180,7 +182,7 @@ TEST(FecControllerPlrBasedTest, UpdateMultipleNetworkMetricsAtOnce) { Controller::NetworkMetrics network_metrics; network_metrics.uplink_bandwidth_bps = kEnablingBandwidthHigh; network_metrics.uplink_packet_loss_fraction = kEnablingPacketLossAtHighBw; - EXPECT_CALL(*states.packet_loss_smoother, GetAverage()) + EXPECT_CALL(*states.packet_loss_smoother, GetAverage) .WillOnce(Return(kEnablingPacketLossAtHighBw)); states.controller->UpdateNetworkMetrics(network_metrics); CheckDecision(&states, true, kEnablingPacketLossAtHighBw); @@ -330,10 +332,11 @@ TEST(FecControllerPlrBasedTest, CheckBehaviorOnSpecialCurves) { constexpr int kEnablingBandwidth = kEnablingBandwidthLow; constexpr float kDisablingPacketLoss = kDisablingPacketLossAtHighBw; FecControllerPlrBasedTestStates states; - std::unique_ptr<MockSmoothingFilter> mock_smoothing_filter( - new NiceMock<MockSmoothingFilter>()); + auto mock_smoothing_filter = + std::make_unique<NiceMock<MockSmoothingFilter>>(); states.packet_loss_smoother = mock_smoothing_filter.get(); - states.controller.reset(new FecControllerPlrBased( + states.controller = std::make_unique<FecControllerPlrBased>( + CreateTestEnvironment(), FecControllerPlrBased::Config( true, ThresholdCurve(kEnablingBandwidthLow, kEnablingPacketLossAtLowBw, @@ -341,7 +344,7 @@ TEST(FecControllerPlrBasedTest, CheckBehaviorOnSpecialCurves) { ThresholdCurve(kDisablingBandwidthLow, kDisablingPacketLoss, kDisablingBandwidthHigh, kDisablingPacketLossAtHighBw), 0), - std::move(mock_smoothing_filter))); + std::move(mock_smoothing_filter)); UpdateNetworkMetrics(&states, kDisablingBandwidthLow - 1, 1.0); CheckDecision(&states, false, 1.0); @@ -475,11 +478,12 @@ TEST(FecControllerPlrBasedTest, FecAlwaysOn) { #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) TEST(FecControllerPlrBasedDeathTest, InvalidConfig) { FecControllerPlrBasedTestStates states; - std::unique_ptr<MockSmoothingFilter> mock_smoothing_filter( - new NiceMock<MockSmoothingFilter>()); + auto mock_smoothing_filter = + std::make_unique<NiceMock<MockSmoothingFilter>>(); states.packet_loss_smoother = mock_smoothing_filter.get(); EXPECT_DEATH( - states.controller.reset(new FecControllerPlrBased( + states.controller = std::make_unique<FecControllerPlrBased>( + CreateTestEnvironment(), FecControllerPlrBased::Config( true, ThresholdCurve(kDisablingBandwidthLow - 1, @@ -489,7 +493,7 @@ TEST(FecControllerPlrBasedDeathTest, InvalidConfig) { kDisablingBandwidthLow, kDisablingPacketLossAtLowBw, kDisablingBandwidthHigh, kDisablingPacketLossAtHighBw), 0), - std::move(mock_smoothing_filter))), + std::move(mock_smoothing_filter)), "Check failed"); } #endif diff --git a/third_party/libwebrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc b/third_party/libwebrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.cc @@ -32,6 +32,7 @@ #include "api/environment/environment.h" #include "api/field_trials_view.h" #include "api/units/time_delta.h" +#include "api/units/timestamp.h" #include "common_audio/smoothing_filter.h" #include "modules/audio_coding/audio_network_adaptor/audio_network_adaptor_impl.h" #include "modules/audio_coding/audio_network_adaptor/controller_manager.h" @@ -46,7 +47,6 @@ #include "rtc_base/numerics/safe_minmax.h" #include "rtc_base/string_encode.h" #include "rtc_base/string_to_number.h" -#include "rtc_base/time_utils.h" namespace webrtc { @@ -325,8 +325,8 @@ std::optional<int> AudioEncoderOpusImpl::GetNewBandwidth( class AudioEncoderOpusImpl::PacketLossFractionSmoother { public: - explicit PacketLossFractionSmoother() - : last_sample_time_ms_(TimeMillis()), + explicit PacketLossFractionSmoother(Timestamp now) + : last_sample_time_(now), smoother_(kAlphaForPacketLossFractionSmoother) {} // Gets the smoothed packet loss fraction. @@ -336,15 +336,14 @@ class AudioEncoderOpusImpl::PacketLossFractionSmoother { } // Add new observation to the packet loss fraction smoother. - void AddSample(float packet_loss_fraction) { - int64_t now_ms = TimeMillis(); - smoother_.Apply(static_cast<float>(now_ms - last_sample_time_ms_), + void AddSample(float packet_loss_fraction, Timestamp now) { + smoother_.Apply((now - last_sample_time_).ms<float>(), packet_loss_fraction); - last_sample_time_ms_ = now_ms; + last_sample_time_ = now; } private: - int64_t last_sample_time_ms_; + Timestamp last_sample_time_; // An exponential filter is used to smooth the packet loss fraction. ExpFilter smoother_; @@ -389,7 +388,9 @@ AudioEncoderOpusImpl::AudioEncoderOpusImpl( bitrate_multipliers_(GetBitrateMultipliers(env_.field_trials())), packet_loss_rate_(0.0), inst_(nullptr), - packet_loss_fraction_smoother_(new PacketLossFractionSmoother()), + packet_loss_fraction_smoother_( + std::make_unique<PacketLossFractionSmoother>( + env_.clock().CurrentTime())), audio_network_adaptor_creator_(audio_network_adaptor_creator), bitrate_smoother_(std::move(bitrate_smoother)) { RTC_DCHECK(0 <= payload_type && payload_type <= 127); @@ -494,7 +495,8 @@ void AudioEncoderOpusImpl::OnReceivedUplinkPacketLossFraction( uplink_packet_loss_fraction); ApplyAudioNetworkAdaptor(); } - packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction); + packet_loss_fraction_smoother_->AddSample(uplink_packet_loss_fraction, + env_.clock().CurrentTime()); float average_fraction_loss = packet_loss_fraction_smoother_->GetAverage(); SetProjectedPacketLossRate(average_fraction_loss); } @@ -522,7 +524,8 @@ void AudioEncoderOpusImpl::OnReceivedUplinkBandwidthImpl( // Then 4 * bwe_period_ms is a good choice. if (bwe_period_ms) bitrate_smoother_->SetTimeConstantMs(*bwe_period_ms * 4); - bitrate_smoother_->AddSample(target_audio_bitrate_bps); + bitrate_smoother_->AddSample(target_audio_bitrate_bps, + env_.clock().CurrentTime()); ApplyAudioNetworkAdaptor(); } else { @@ -782,14 +785,15 @@ AudioEncoderOpusImpl::DefaultAudioNetworkAdaptorCreator( void AudioEncoderOpusImpl::MaybeUpdateUplinkBandwidth() { if (audio_network_adaptor_) { - int64_t now_ms = TimeMillis(); + Timestamp now = env_.clock().CurrentTime(); if (!bitrate_smoother_last_update_time_ || - now_ms - *bitrate_smoother_last_update_time_ >= + now.ms() - *bitrate_smoother_last_update_time_ >= config_.uplink_bandwidth_update_interval_ms) { - std::optional<float> smoothed_bitrate = bitrate_smoother_->GetAverage(); + std::optional<float> smoothed_bitrate = + bitrate_smoother_->GetAverage(now); if (smoothed_bitrate) audio_network_adaptor_->SetUplinkBandwidth(*smoothed_bitrate); - bitrate_smoother_last_update_time_ = now_ms; + bitrate_smoother_last_update_time_ = now.ms(); } } } diff --git a/third_party/libwebrtc/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc b/third_party/libwebrtc/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc @@ -47,6 +47,7 @@ namespace webrtc { namespace { +using ::testing::_; using ::testing::NiceMock; using ::testing::Return; @@ -296,7 +297,8 @@ TEST_P(AudioEncoderOpusTest, SetTargetAudioBitrate(kTargetAudioBitrate)); EXPECT_CALL(*states->mock_bitrate_smoother, SetTimeConstantMs(kProbingIntervalMs * 4)); - EXPECT_CALL(*states->mock_bitrate_smoother, AddSample(kTargetAudioBitrate)); + EXPECT_CALL(*states->mock_bitrate_smoother, + AddSample(kTargetAudioBitrate, _)); states->encoder->OnReceivedUplinkBandwidth(kTargetAudioBitrate, kProbingIntervalMs); @@ -505,7 +507,7 @@ TEST_P(AudioEncoderOpusTest, UpdateUplinkBandwidthInAudioNetworkAdaptor) { const size_t opus_rate_khz = CheckedDivExact(sample_rate_hz_, 1000); const std::vector<int16_t> audio(opus_rate_khz * 10 * 2, 0); Buffer encoded; - EXPECT_CALL(*states->mock_bitrate_smoother, GetAverage()) + EXPECT_CALL(*states->mock_bitrate_smoother, GetAverage) .WillOnce(Return(50000)); EXPECT_CALL(*states->mock_audio_network_adaptor, SetUplinkBandwidth(50000)); states->encoder->Encode( @@ -520,7 +522,7 @@ TEST_P(AudioEncoderOpusTest, UpdateUplinkBandwidthInAudioNetworkAdaptor) { 0, ArrayView<const int16_t>(audio.data(), audio.size()), &encoded); // Update when it is time to update. - EXPECT_CALL(*states->mock_bitrate_smoother, GetAverage()) + EXPECT_CALL(*states->mock_bitrate_smoother, GetAverage) .WillOnce(Return(40000)); EXPECT_CALL(*states->mock_audio_network_adaptor, SetUplinkBandwidth(40000)); states->fake_clock->AdvanceTime(TimeDelta::Millis(1)); diff --git a/third_party/libwebrtc/moz-patch-stack/s0007.patch b/third_party/libwebrtc/moz-patch-stack/s0007.patch @@ -9,7 +9,7 @@ Mercurial Revision: https://hg.mozilla.org/mozilla-central/rev/92a7c3eee9f0c80ff 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/audio_coding/codecs/opus/audio_encoder_opus.cc b/modules/audio_coding/codecs/opus/audio_encoder_opus.cc -index afe80d54b0..3dc679bc28 100644 +index c3c0f31ab1..690cc6c30b 100644 --- a/modules/audio_coding/codecs/opus/audio_encoder_opus.cc +++ b/modules/audio_coding/codecs/opus/audio_encoder_opus.cc @@ -245,7 +245,7 @@ AudioCodecInfo AudioEncoderOpusImpl::QueryAudioEncoder( diff --git a/third_party/libwebrtc/moz-patch-stack/s0027.patch b/third_party/libwebrtc/moz-patch-stack/s0027.patch @@ -517,10 +517,10 @@ index c5a24d2c48..6ae8932ca9 100644 #include "api/units/data_rate.h" #include "api/video/encoded_image.h" diff --git a/common_audio/BUILD.gn b/common_audio/BUILD.gn -index 6ddf551658..7eb2aca444 100644 +index e9b469f258..9176dbcc47 100644 --- a/common_audio/BUILD.gn +++ b/common_audio/BUILD.gn -@@ -265,14 +265,10 @@ if (current_cpu == "x86" || current_cpu == "x64") { +@@ -266,14 +266,10 @@ if (current_cpu == "x86" || current_cpu == "x64") { "resampler/sinc_resampler_avx2.cc", ] @@ -723,7 +723,7 @@ index 6d4d4a2363..316408a9b3 100644 : content_hint(VideoTrackInterface::ContentHint::kNone) {} VideoOptions::~VideoOptions() = default; diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn -index 0c56427b29..13c9533524 100644 +index 02c3ccde22..cf992635fe 100644 --- a/modules/audio_coding/BUILD.gn +++ b/modules/audio_coding/BUILD.gn @@ -362,7 +362,7 @@ rtc_library("webrtc_opus_wrapper") { diff --git a/third_party/libwebrtc/moz-patch-stack/s0034.patch b/third_party/libwebrtc/moz-patch-stack/s0034.patch @@ -48,7 +48,7 @@ index 348be89bb9..19ff7e3f98 100644 if (mips_float_abi == "hard") { defines += [ "MIPS_FPU_LE" ] diff --git a/common_audio/BUILD.gn b/common_audio/BUILD.gn -index 7eb2aca444..1e17368bf8 100644 +index 9176dbcc47..a95c90df2c 100644 --- a/common_audio/BUILD.gn +++ b/common_audio/BUILD.gn @@ -67,7 +67,7 @@ rtc_library("common_audio") { @@ -60,7 +60,7 @@ index 7eb2aca444..1e17368bf8 100644 deps += [ ":common_audio_sse2" ] deps += [ ":common_audio_avx2" ] } -@@ -89,7 +89,7 @@ rtc_source_set("mock_common_audio") { +@@ -90,7 +90,7 @@ rtc_source_set("mock_common_audio") { rtc_source_set("common_audio_c_arm_asm") { sources = [] deps = [] @@ -69,7 +69,7 @@ index 7eb2aca444..1e17368bf8 100644 sources += [ "signal_processing/complex_bit_reverse_arm.S" ] if (arm_version >= 7) { -@@ -153,7 +153,7 @@ rtc_library("common_audio_c") { +@@ -154,7 +154,7 @@ rtc_library("common_audio_c") { "vad/webrtc_vad.c", ] @@ -78,7 +78,7 @@ index 7eb2aca444..1e17368bf8 100644 sources += [ "signal_processing/complex_bit_reverse_mips.c", "signal_processing/complex_fft_mips.c", -@@ -171,7 +171,7 @@ rtc_library("common_audio_c") { +@@ -172,7 +172,7 @@ rtc_library("common_audio_c") { sources += [ "signal_processing/complex_fft.c" ] } @@ -87,7 +87,7 @@ index 7eb2aca444..1e17368bf8 100644 sources += [ "signal_processing/complex_bit_reverse.c", "signal_processing/filter_ar_fast_q12.c", -@@ -229,7 +229,7 @@ rtc_library("fir_filter_factory") { +@@ -230,7 +230,7 @@ rtc_library("fir_filter_factory") { "../rtc_base:cpu_info", "../rtc_base/system:arch", ] @@ -96,7 +96,7 @@ index 7eb2aca444..1e17368bf8 100644 deps += [ ":common_audio_sse2" ] deps += [ ":common_audio_avx2" ] } -@@ -238,7 +238,7 @@ rtc_library("fir_filter_factory") { +@@ -239,7 +239,7 @@ rtc_library("fir_filter_factory") { } } @@ -105,7 +105,7 @@ index 7eb2aca444..1e17368bf8 100644 rtc_library("common_audio_sse2") { sources = [ "fir_filter_sse.cc", -@@ -287,7 +287,7 @@ if (rtc_build_with_neon) { +@@ -288,7 +288,7 @@ if (rtc_build_with_neon) { "resampler/sinc_resampler_neon.cc", ] @@ -114,7 +114,7 @@ index 7eb2aca444..1e17368bf8 100644 # Enable compilation for the NEON instruction set. suppressed_configs += [ "//build/config/compiler:compiler_arm_fpu" ] cflags = [ "-mfpu=neon" ] -@@ -310,7 +310,7 @@ if (rtc_build_with_neon) { +@@ -311,7 +311,7 @@ if (rtc_build_with_neon) { "signal_processing/min_max_operations_neon.c", ] diff --git a/third_party/libwebrtc/moz-patch-stack/s0099.patch b/third_party/libwebrtc/moz-patch-stack/s0099.patch @@ -16,7 +16,7 @@ Mercurial Revision: https://hg.mozilla.org/mozilla-central/rev/bbec1b95ddb5e0096 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/modules/audio_coding/BUILD.gn b/modules/audio_coding/BUILD.gn -index 13c9533524..59e121b7b6 100644 +index cf992635fe..851519c279 100644 --- a/modules/audio_coding/BUILD.gn +++ b/modules/audio_coding/BUILD.gn @@ -362,7 +362,7 @@ rtc_library("webrtc_opus_wrapper") { diff --git a/third_party/libwebrtc/moz-patch-stack/s0102.patch b/third_party/libwebrtc/moz-patch-stack/s0102.patch @@ -291,10 +291,10 @@ index eda3161da8..612028625a 100644 rtc_library("audio") { diff --git a/common_audio/BUILD.gn b/common_audio/BUILD.gn -index 1e17368bf8..2dad7c98df 100644 +index a95c90df2c..3ac5126100 100644 --- a/common_audio/BUILD.gn +++ b/common_audio/BUILD.gn -@@ -289,7 +289,7 @@ if (rtc_build_with_neon) { +@@ -290,7 +290,7 @@ if (rtc_build_with_neon) { if (target_cpu != "arm64") { # Enable compilation for the NEON instruction set. @@ -303,7 +303,7 @@ index 1e17368bf8..2dad7c98df 100644 cflags = [ "-mfpu=neon" ] } -@@ -312,7 +312,7 @@ if (rtc_build_with_neon) { +@@ -313,7 +313,7 @@ if (rtc_build_with_neon) { if (target_cpu != "arm64") { # Enable compilation for the NEON instruction set.