diff --git a/modules/audio_coding/neteq/neteq_impl.cc b/modules/audio_coding/neteq/neteq_impl.cc index 163a287a5e..f1cd8015e6 100644 --- a/modules/audio_coding/neteq/neteq_impl.cc +++ b/modules/audio_coding/neteq/neteq_impl.cc @@ -51,6 +51,7 @@ #include "rtc_base/strings/audio_format_to_string.h" #include "rtc_base/trace_event.h" #include "system_wrappers/include/clock.h" +#include "system_wrappers/include/field_trial.h" namespace webrtc { namespace { @@ -73,6 +74,24 @@ std::unique_ptr CreateNetEqController( return controller_factory.CreateNetEqController(config); } +int GetDelayChainLengthMs(int config_extra_delay_ms) { + constexpr char kExtraDelayFieldTrial[] = "WebRTC-Audio-NetEqExtraDelay"; + if (webrtc::field_trial::IsEnabled(kExtraDelayFieldTrial)) { + const auto field_trial_string = + webrtc::field_trial::FindFullName(kExtraDelayFieldTrial); + int extra_delay_ms = -1; + if (sscanf(field_trial_string.c_str(), "Enabled-%d", &extra_delay_ms) == + 1 && + extra_delay_ms >= 0 && extra_delay_ms <= 2000) { + RTC_LOG(LS_INFO) << "Delay chain length set to " << extra_delay_ms + << " ms in field trial"; + return (extra_delay_ms / 10) * 10; // Rounding down to multiple of 10. + } + } + // Field trial not set, or invalid value read. Use value from config. + return config_extra_delay_ms; +} + } // namespace NetEqImpl::Dependencies::Dependencies( @@ -141,9 +160,9 @@ NetEqImpl::NetEqImpl(const NetEq::Config& config, tick_timer_.get()), no_time_stretching_(config.for_test_no_time_stretching), enable_rtx_handling_(config.enable_rtx_handling), - output_delay_chain_( - rtc::CheckedDivExact(config.extra_output_delay_ms, 10)), - output_delay_chain_ms_(config.extra_output_delay_ms) { + output_delay_chain_ms_( + GetDelayChainLengthMs(config.extra_output_delay_ms)), + output_delay_chain_(rtc::CheckedDivExact(output_delay_chain_ms_, 10)) { RTC_LOG(LS_INFO) << "NetEq config: " << config.ToString(); int fs = config.sample_rate_hz; if (fs != 8000 && fs != 16000 && fs != 32000 && fs != 48000) { diff --git a/modules/audio_coding/neteq/neteq_impl.h b/modules/audio_coding/neteq/neteq_impl.h index 7d5ebabb4c..623968aefd 100644 --- a/modules/audio_coding/neteq/neteq_impl.h +++ b/modules/audio_coding/neteq/neteq_impl.h @@ -403,14 +403,14 @@ class NetEqImpl : public webrtc::NetEq { rtc::BufferT concealment_audio_ RTC_GUARDED_BY(crit_sect_); const bool enable_rtx_handling_ RTC_GUARDED_BY(crit_sect_); // Data members used for adding extra delay to the output of NetEq. + // The delay in ms (which is 10 times the number of elements in + // output_delay_chain_). + const int output_delay_chain_ms_ RTC_GUARDED_BY(crit_sect_); // Vector of AudioFrames which contains the delayed audio. Accessed as a // circular buffer. std::vector output_delay_chain_ RTC_GUARDED_BY(crit_sect_); // Index into output_delay_chain_. size_t output_delay_chain_ix_ RTC_GUARDED_BY(crit_sect_) = 0; - // The delay in ms (which is 10 times the number of elements in - // output_delay_chain_). - const int output_delay_chain_ms_ RTC_GUARDED_BY(crit_sect_); // Did output_delay_chain_ get populated yet? bool output_delay_chain_empty_ RTC_GUARDED_BY(crit_sect_) = true; // Contains the sample rate of the AudioFrame last emitted from the delay diff --git a/modules/audio_coding/neteq/neteq_unittest.cc b/modules/audio_coding/neteq/neteq_unittest.cc index f92ed1b2ac..f5fb647965 100644 --- a/modules/audio_coding/neteq/neteq_unittest.cc +++ b/modules/audio_coding/neteq/neteq_unittest.cc @@ -1253,5 +1253,35 @@ TEST(NetEqOutputDelayTest, RunTest) { } } +// Tests the extra output delay functionality of NetEq when configured via +// field trial. +TEST(NetEqOutputDelayTest, RunTestWithFieldTrial) { + test::ScopedFieldTrials field_trial( + "WebRTC-Audio-NetEqExtraDelay/Enabled-50/"); + constexpr int kExpectedDelayMs = 50; + std::vector output; + const auto result = DelayLineNetEqTest(0, &output); + + // The base delay values are taken from the resuts of the non-delayed case in + // NetEqOutputDelayTest.RunTest above. + EXPECT_EQ(10 + kExpectedDelayMs, result.target_delay_ms); + EXPECT_EQ(24 + kExpectedDelayMs, result.filtered_current_delay_ms); +} + +// Set a non-multiple-of-10 value in the field trial, and verify that we don't +// crash, and that the result is rounded down. +TEST(NetEqOutputDelayTest, RunTestWithFieldTrialOddValue) { + test::ScopedFieldTrials field_trial( + "WebRTC-Audio-NetEqExtraDelay/Enabled-103/"); + constexpr int kRoundedDelayMs = 100; + std::vector output; + const auto result = DelayLineNetEqTest(0, &output); + + // The base delay values are taken from the resuts of the non-delayed case in + // NetEqOutputDelayTest.RunTest above. + EXPECT_EQ(10 + kRoundedDelayMs, result.target_delay_ms); + EXPECT_EQ(24 + kRoundedDelayMs, result.filtered_current_delay_ms); +} + } // namespace test } // namespace webrtc