From 41c11e4cad1cf28f948686be24f4d1aeeb30d36c Mon Sep 17 00:00:00 2001 From: Gustaf Ullberg Date: Tue, 22 May 2018 08:59:29 +0200 Subject: [PATCH] AEC3: Rounding of estimated call skew MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL fixes the rounding of the estimated average call skew. Before it was rounded down (toward INT_MIN). Now it is rounded to the nearest integer. This avoids unnecessary fluctuations of the estimated call skew (and unnecessary resets). Bug: webrtc:9283,chromium:888042 Change-Id: Id5b3c593f812f5f9fd3dcdafb7e388a6ef1ac153 Reviewed-on: https://webrtc-review.googlesource.com/77684 Commit-Queue: Gustaf Ullberg Reviewed-by: Per Ã…hgren Cr-Commit-Position: refs/heads/master@{#23338} --- .../audio_processing/aec3/skew_estimator.cc | 6 ++-- .../aec3/skew_estimator_unittest.cc | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/modules/audio_processing/aec3/skew_estimator.cc b/modules/audio_processing/aec3/skew_estimator.cc index 608a707d3e..5a453a051f 100644 --- a/modules/audio_processing/aec3/skew_estimator.cc +++ b/modules/audio_processing/aec3/skew_estimator.cc @@ -38,9 +38,9 @@ rtc::Optional SkewEstimator::GetSkewFromCapture() { sufficient_skew_stored_ = true; } - return sufficient_skew_stored_ - ? rtc::Optional(skew_sum_ >> skew_history_size_log2_) - : rtc::nullopt; + const int bias = static_cast(skew_history_.size()) >> 1; + const int average = (skew_sum_ + bias) >> skew_history_size_log2_; + return sufficient_skew_stored_ ? rtc::Optional(average) : rtc::nullopt; } } // namespace webrtc diff --git a/modules/audio_processing/aec3/skew_estimator_unittest.cc b/modules/audio_processing/aec3/skew_estimator_unittest.cc index a1a679fe25..168470a17b 100644 --- a/modules/audio_processing/aec3/skew_estimator_unittest.cc +++ b/modules/audio_processing/aec3/skew_estimator_unittest.cc @@ -120,5 +120,38 @@ TEST(SkewEstimator, NullEstimate) { EXPECT_FALSE(skew); } } + +// Tests that the skew estimator properly rounds the average skew. +TEST(SkewEstimator, SkewRounding) { + constexpr int kNumSkewsLog2 = 4; + constexpr int kNumSkews = 1 << kNumSkewsLog2; + + SkewEstimator estimator(kNumSkewsLog2); + + rtc::Optional skew; + for (int k = 0; k < kNumSkews; ++k) { + if (k == kNumSkews - 1) { + // Reverse call order once. + skew = estimator.GetSkewFromCapture(); + estimator.LogRenderCall(); + } else { + // Normal call order. + estimator.LogRenderCall(); + skew = estimator.GetSkewFromCapture(); + } + } + EXPECT_EQ(*skew, 0); + + estimator.Reset(); + for (int k = 0; k < kNumSkews; ++k) { + estimator.LogRenderCall(); + estimator.LogRenderCall(); + estimator.LogRenderCall(); + estimator.GetSkewFromCapture(); + estimator.GetSkewFromCapture(); + skew = estimator.GetSkewFromCapture(); + } + EXPECT_EQ(*skew, 1); +} } // namespace aec3 } // namespace webrtc