Robustify the faster alignment in AEC3 to avoid resets

The faster AEC3 alignment introduced recently may in
cases cause the alignment (and the AEC3) to repeatedly
reset. This CL avoids these resets by handling buffer
issues (which are triggering the resets) separately
during the initial coarse alignment phase.



Change-Id: Idf5e2ffda2591906da8060d03ec8ca73cdaedf53
Bug: webrtc:8798,chromium:805815
Reviewed-on: https://webrtc-review.googlesource.com/43480
Commit-Queue: Per Åhgren <peah@webrtc.org>
Reviewed-by: Gustaf Ullberg <gustaf@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21758}
diff --git a/modules/audio_processing/aec3/render_delay_buffer.cc b/modules/audio_processing/aec3/render_delay_buffer.cc
index 3181bae..1373729 100644
--- a/modules/audio_processing/aec3/render_delay_buffer.cc
+++ b/modules/audio_processing/aec3/render_delay_buffer.cc
@@ -47,7 +47,7 @@
     return low_rate_;
   }
 
-  bool CausalDelay() const override;
+  bool CausalDelay(size_t delay) const override;
 
  private:
   static int instance_count_;
@@ -304,10 +304,14 @@
 }
 
 // Returns whether the specified delay is causal.
-bool RenderDelayBufferImpl::CausalDelay() const {
-  return !internal_delay_ ||
-         *internal_delay_ >=
-             static_cast<int>(config_.delay.min_echo_path_delay_blocks);
+bool RenderDelayBufferImpl::CausalDelay(size_t delay) const {
+  // Compute the internal delay and limit the delay to the allowed range.
+  int internal_delay = MaxExternalDelayToInternalDelay(delay);
+  internal_delay =
+      std::min(MaxDelay(), static_cast<size_t>(std::max(internal_delay, 0)));
+
+  return internal_delay >=
+         static_cast<int>(config_.delay.min_echo_path_delay_blocks);
 }
 
 // Maps the externally computed delay to the delay used internally.