AEC3: Support negative delay with external delay estimator

Bug: b/130016532
Change-Id: If41121e9ad95f235200c4b6b146ba8a8749daac4
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/158220
Reviewed-by: Per Ã…hgren <peah@webrtc.org>
Commit-Queue: Gustaf Ullberg <gustaf@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29602}
diff --git a/api/audio/echo_control.h b/api/audio/echo_control.h
index c8f73b8..9872889 100644
--- a/api/audio/echo_control.h
+++ b/api/audio/echo_control.h
@@ -39,7 +39,11 @@
   virtual Metrics GetMetrics() const = 0;
 
   // Provides an optional external estimate of the audio buffer delay.
-  virtual void SetAudioBufferDelay(size_t delay_ms) = 0;
+  virtual void SetAudioBufferDelay(size_t delay_ms) {}
+  virtual void SetAudioBufferDelay(int delay_ms) {
+    // Default to old implementation.
+    SetAudioBufferDelay(static_cast<size_t>(delay_ms));
+  }
 
   // Returns wheter the signal is altered.
   virtual bool ActiveProcessing() const = 0;
diff --git a/modules/audio_processing/aec3/block_processor.cc b/modules/audio_processing/aec3/block_processor.cc
index fa9c7b3..8942570 100644
--- a/modules/audio_processing/aec3/block_processor.cc
+++ b/modules/audio_processing/aec3/block_processor.cc
@@ -61,7 +61,7 @@
 
   void GetMetrics(EchoControl::Metrics* metrics) const override;
 
-  void SetAudioBufferDelay(size_t delay_ms) override;
+  void SetAudioBufferDelay(int delay_ms) override;
 
  private:
   static int instance_count_;
@@ -230,7 +230,7 @@
   metrics->delay_ms = delay ? static_cast<int>(*delay) * block_size_ms : 0;
 }
 
-void BlockProcessorImpl::SetAudioBufferDelay(size_t delay_ms) {
+void BlockProcessorImpl::SetAudioBufferDelay(int delay_ms) {
   render_buffer_->SetAudioBufferDelay(delay_ms);
 }
 
diff --git a/modules/audio_processing/aec3/block_processor.h b/modules/audio_processing/aec3/block_processor.h
index 3ae5a75..755444a 100644
--- a/modules/audio_processing/aec3/block_processor.h
+++ b/modules/audio_processing/aec3/block_processor.h
@@ -53,7 +53,7 @@
   virtual void GetMetrics(EchoControl::Metrics* metrics) const = 0;
 
   // Provides an optional external estimate of the audio buffer delay.
-  virtual void SetAudioBufferDelay(size_t delay_ms) = 0;
+  virtual void SetAudioBufferDelay(int delay_ms) = 0;
 
   // Processes a block of capture data.
   virtual void ProcessCapture(
diff --git a/modules/audio_processing/aec3/echo_canceller3.cc b/modules/audio_processing/aec3/echo_canceller3.cc
index a7a76d3..2b50e61 100644
--- a/modules/audio_processing/aec3/echo_canceller3.cc
+++ b/modules/audio_processing/aec3/echo_canceller3.cc
@@ -374,7 +374,7 @@
   return metrics;
 }
 
-void EchoCanceller3::SetAudioBufferDelay(size_t delay_ms) {
+void EchoCanceller3::SetAudioBufferDelay(int delay_ms) {
   RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
   block_processor_->SetAudioBufferDelay(delay_ms);
 }
diff --git a/modules/audio_processing/aec3/echo_canceller3.h b/modules/audio_processing/aec3/echo_canceller3.h
index 7a7df07..ce36cc1 100644
--- a/modules/audio_processing/aec3/echo_canceller3.h
+++ b/modules/audio_processing/aec3/echo_canceller3.h
@@ -109,7 +109,7 @@
   // Collect current metrics from the echo canceller.
   Metrics GetMetrics() const override;
   // Provides an optional external estimate of the audio buffer delay.
-  void SetAudioBufferDelay(size_t delay_ms) override;
+  void SetAudioBufferDelay(int delay_ms) override;
 
   bool ActiveProcessing() const override;
 
diff --git a/modules/audio_processing/aec3/echo_canceller3_unittest.cc b/modules/audio_processing/aec3/echo_canceller3_unittest.cc
index c7a71c3..e7b9ddb 100644
--- a/modules/audio_processing/aec3/echo_canceller3_unittest.cc
+++ b/modules/audio_processing/aec3/echo_canceller3_unittest.cc
@@ -121,7 +121,7 @@
 
   void GetMetrics(EchoControl::Metrics* metrics) const override {}
 
-  void SetAudioBufferDelay(size_t delay_ms) override {}
+  void SetAudioBufferDelay(int delay_ms) override {}
 
  private:
   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(CaptureTransportVerificationProcessor);
@@ -153,7 +153,7 @@
 
   void GetMetrics(EchoControl::Metrics* metrics) const override {}
 
-  void SetAudioBufferDelay(size_t delay_ms) override {}
+  void SetAudioBufferDelay(int delay_ms) override {}
 
  private:
   std::deque<std::vector<std::vector<std::vector<float>>>>
diff --git a/modules/audio_processing/aec3/mock/mock_block_processor.h b/modules/audio_processing/aec3/mock/mock_block_processor.h
index cb93714..634d26e 100644
--- a/modules/audio_processing/aec3/mock/mock_block_processor.h
+++ b/modules/audio_processing/aec3/mock/mock_block_processor.h
@@ -33,7 +33,7 @@
                void(const std::vector<std::vector<std::vector<float>>>& block));
   MOCK_METHOD1(UpdateEchoLeakageStatus, void(bool leakage_detected));
   MOCK_CONST_METHOD1(GetMetrics, void(EchoControl::Metrics* metrics));
-  MOCK_METHOD1(SetAudioBufferDelay, void(size_t delay_ms));
+  MOCK_METHOD1(SetAudioBufferDelay, void(int delay_ms));
 };
 
 }  // namespace test
diff --git a/modules/audio_processing/aec3/mock/mock_render_delay_buffer.h b/modules/audio_processing/aec3/mock/mock_render_delay_buffer.h
index 8882001..f83c670 100644
--- a/modules/audio_processing/aec3/mock/mock_render_delay_buffer.h
+++ b/modules/audio_processing/aec3/mock/mock_render_delay_buffer.h
@@ -40,7 +40,7 @@
   MOCK_CONST_METHOD0(GetDownsampledRenderBuffer,
                      const DownsampledRenderBuffer&());
   MOCK_CONST_METHOD1(CausalDelay, bool(size_t delay));
-  MOCK_METHOD1(SetAudioBufferDelay, void(size_t delay_ms));
+  MOCK_METHOD1(SetAudioBufferDelay, void(int delay_ms));
   MOCK_METHOD0(HasReceivedBufferDelay, bool());
 
  private:
diff --git a/modules/audio_processing/aec3/render_delay_buffer.cc b/modules/audio_processing/aec3/render_delay_buffer.cc
index 14a6bd5..fb32035 100644
--- a/modules/audio_processing/aec3/render_delay_buffer.cc
+++ b/modules/audio_processing/aec3/render_delay_buffer.cc
@@ -62,7 +62,7 @@
   }
 
   int BufferLatency() const;
-  void SetAudioBufferDelay(size_t delay_ms) override;
+  void SetAudioBufferDelay(int delay_ms) override;
   bool HasReceivedBufferDelay() override;
 
  private:
@@ -90,7 +90,7 @@
   int64_t render_call_counter_ = 0;
   bool render_activity_ = false;
   size_t render_activity_counter_ = 0;
-  absl::optional<size_t> external_audio_buffer_delay_;
+  absl::optional<int> external_audio_buffer_delay_;
   bool external_audio_buffer_delay_verified_after_reset_ = false;
   size_t min_latency_blocks_ = 0;
   size_t excess_render_detection_counter_ = 0;
@@ -165,7 +165,7 @@
 
   // Check for any external audio buffer delay and whether it is feasible.
   if (external_audio_buffer_delay_) {
-    const size_t headroom = 2;
+    const int headroom = 2;
     size_t audio_buffer_delay_to_set;
     // Minimum delay is 1 (like the low-rate render buffer).
     if (*external_audio_buffer_delay_ <= headroom) {
@@ -318,7 +318,7 @@
   return true;
 }
 
-void RenderDelayBufferImpl::SetAudioBufferDelay(size_t delay_ms) {
+void RenderDelayBufferImpl::SetAudioBufferDelay(int delay_ms) {
   if (!external_audio_buffer_delay_) {
     RTC_LOG_V(delay_log_level_)
         << "Receiving a first externally reported audio buffer delay of "
@@ -326,7 +326,7 @@
   }
 
   // Convert delay from milliseconds to blocks (rounded down).
-  external_audio_buffer_delay_ = delay_ms >> 2;
+  external_audio_buffer_delay_ = delay_ms / 4;
 }
 
 bool RenderDelayBufferImpl::HasReceivedBufferDelay() {
diff --git a/modules/audio_processing/aec3/render_delay_buffer.h b/modules/audio_processing/aec3/render_delay_buffer.h
index e53f6d2..0758e9d 100644
--- a/modules/audio_processing/aec3/render_delay_buffer.h
+++ b/modules/audio_processing/aec3/render_delay_buffer.h
@@ -71,7 +71,7 @@
   static int DelayEstimatorOffset(const EchoCanceller3Config& config);
 
   // Provides an optional external estimate of the audio buffer delay.
-  virtual void SetAudioBufferDelay(size_t delay_ms) = 0;
+  virtual void SetAudioBufferDelay(int delay_ms) = 0;
 
   // Returns whether an external delay estimate has been reported via
   // SetAudioBufferDelay.
diff --git a/modules/audio_processing/include/mock_audio_processing.h b/modules/audio_processing/include/mock_audio_processing.h
index ba57d78..6b12392 100644
--- a/modules/audio_processing/include/mock_audio_processing.h
+++ b/modules/audio_processing/include/mock_audio_processing.h
@@ -48,7 +48,7 @@
   MOCK_METHOD2(ProcessCapture,
                void(AudioBuffer* capture, bool echo_path_change));
   MOCK_CONST_METHOD0(GetMetrics, Metrics());
-  MOCK_METHOD1(SetAudioBufferDelay, void(size_t delay_ms));
+  MOCK_METHOD1(SetAudioBufferDelay, void(int delay_ms));
   MOCK_CONST_METHOD0(ActiveProcessing, bool());
 };
 
diff --git a/modules/audio_processing/test/echo_control_mock.h b/modules/audio_processing/test/echo_control_mock.h
index 8479a7c..c2082c2 100644
--- a/modules/audio_processing/test/echo_control_mock.h
+++ b/modules/audio_processing/test/echo_control_mock.h
@@ -25,7 +25,7 @@
   MOCK_METHOD2(ProcessCapture,
                void(AudioBuffer* capture, bool echo_path_change));
   MOCK_CONST_METHOD0(GetMetrics, EchoControl::Metrics());
-  MOCK_METHOD1(SetAudioBufferDelay, void(size_t delay_ms));
+  MOCK_METHOD1(SetAudioBufferDelay, void(int delay_ms));
   MOCK_CONST_METHOD0(ActiveProcessing, bool());
 };