Corrected the computation of the headroom in the AEC3 buffer alignment

This CL corrects the computation of the delay headroom so that
it is only updated when the delay is updated. This is important
as otherwise a too large headroom will be reported, which then
could cause buffer access issues.

Bug: webrtc:7878, chromium:736893
Change-Id: Ib37cb608b064dd5d4df3f8fc423448ee80ed0106
Reviewed-on: https://chromium-review.googlesource.com/549335
Commit-Queue: Per Åhgren <peah@webrtc.org>
Reviewed-by: Ivo Creusen <ivoc@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#18781}
diff --git a/webrtc/modules/audio_processing/aec3/render_delay_controller.cc b/webrtc/modules/audio_processing/aec3/render_delay_controller.cc
index 3f7b108..0a64ac2 100644
--- a/webrtc/modules/audio_processing/aec3/render_delay_controller.cc
+++ b/webrtc/modules/audio_processing/aec3/render_delay_controller.cc
@@ -105,20 +105,20 @@
   rtc::Optional<size_t> echo_path_delay_samples =
       delay_estimator_.EstimateDelay(render_buffer, capture);
   if (echo_path_delay_samples) {
+    blocks_since_last_delay_estimate_ = 0;
     echo_path_delay_samples_ = *echo_path_delay_samples;
 
     // Compute and set new render delay buffer delay.
     const size_t new_delay =
         ComputeNewBufferDelay(delay_, echo_path_delay_samples_);
-    if (new_delay != delay_ && align_call_counter_ > kNumBlocksPerSecond) {
+    if (align_call_counter_ > kNumBlocksPerSecond) {
       delay_ = new_delay;
-    }
 
-    // Update render delay buffer headroom.
-    blocks_since_last_delay_estimate_ = 0;
-    const int headroom = echo_path_delay_samples_ - delay_ * kBlockSize;
-    RTC_DCHECK_LE(0, headroom);
-    headroom_samples_ = rtc::Optional<size_t>(headroom);
+      // Update render delay buffer headroom.
+      const int headroom = echo_path_delay_samples_ - delay_ * kBlockSize;
+      RTC_DCHECK_LE(0, headroom);
+      headroom_samples_ = rtc::Optional<size_t>(headroom);
+    }
   } else if (++blocks_since_last_delay_estimate_ > 20 * kNumBlocksPerSecond) {
     headroom_samples_ = rtc::Optional<size_t>();
   }