Add a hysteresis for the API call skew detection to better handle jittery platforms

Bug: webrtc:8954,chromium:817313
Change-Id: I940d52ac96e5bddf886d47be089a1991ae24b51b
Reviewed-on: https://webrtc-review.googlesource.com/58640
Commit-Queue: Per Åhgren <peah@webrtc.org>
Reviewed-by: Gustaf Ullberg <gustaf@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22228}
diff --git a/api/audio/echo_canceller3_config.h b/api/audio/echo_canceller3_config.h
index 6bd3a95..1acb26f 100644
--- a/api/audio/echo_canceller3_config.h
+++ b/api/audio/echo_canceller3_config.h
@@ -28,6 +28,7 @@
     size_t delay_headroom_blocks = 2;
     size_t hysteresis_limit_1_blocks = 1;
     size_t hysteresis_limit_2_blocks = 1;
+    size_t skew_hysteresis_blocks = 1;
   } delay;
 
   struct Filter {
diff --git a/modules/audio_processing/aec3/render_delay_controller.cc b/modules/audio_processing/aec3/render_delay_controller.cc
index e97386e..db00b9b 100644
--- a/modules/audio_processing/aec3/render_delay_controller.cc
+++ b/modules/audio_processing/aec3/render_delay_controller.cc
@@ -48,6 +48,7 @@
   const int delay_headroom_blocks_;
   const int hysteresis_limit_1_blocks_;
   const int hysteresis_limit_2_blocks_;
+  const int skew_hysteresis_blocks_;
   rtc::Optional<DelayEstimate> delay_;
   EchoPathDelayEstimator delay_estimator_;
   std::vector<float> delay_buf_;
@@ -115,6 +116,8 @@
           static_cast<int>(config.delay.hysteresis_limit_1_blocks)),
       hysteresis_limit_2_blocks_(
           static_cast<int>(config.delay.hysteresis_limit_2_blocks)),
+      skew_hysteresis_blocks_(
+          static_cast<int>(config.delay.skew_hysteresis_blocks)),
       delay_estimator_(data_dumper_.get(), config),
       delay_buf_(kBlockSize * non_causal_offset, 0.f),
       skew_estimator_(kSkewHistorySizeLog2) {
@@ -199,7 +202,9 @@
       delay_samples_->quality == DelayEstimate::Quality::kRefined) {
     // Compute the skew offset and add a margin.
     offset_blocks = *skew_ - *skew;
-    if (offset_blocks != 0 && soft_reset_counter_ > 10 * kNumBlocksPerSecond) {
+    if (abs(offset_blocks) <= skew_hysteresis_blocks_) {
+      offset_blocks = 0;
+    } else if (soft_reset_counter_ > 10 * kNumBlocksPerSecond) {
       // Soft reset the delay estimator if there is a significant offset
       // detected.
       delay_estimator_.Reset(true);