Enforcing a stream delay of 0 to be assumed in the AEC on Chrome OS

This CL forces the AEC2 to assume a stream delay of 0, thereby
avoiding that the incorrect stream delays reported on Chrome OS
causes echo issues.

Bug: chromium:797274, chromium:797272
Change-Id: I10f295c9f1d735622c55fc56be99a14c6cdd88a2
Reviewed-on: https://webrtc-review.googlesource.com/36081
Reviewed-by: Per Åhgren <peah@webrtc.org>
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Commit-Queue: Per Åhgren <peah@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21432}
diff --git a/modules/audio_processing/BUILD.gn b/modules/audio_processing/BUILD.gn
index 5ec09d5..49db002 100644
--- a/modules/audio_processing/BUILD.gn
+++ b/modules/audio_processing/BUILD.gn
@@ -245,6 +245,7 @@
     "../../rtc_base:protobuf_utils",
     "../../rtc_base:sanitizer",
     "../../system_wrappers:cpu_features_api",
+    "../../system_wrappers:field_trial_api",
     "../../system_wrappers:metrics_api",
     "../audio_coding:isac",
   ]
diff --git a/modules/audio_processing/echo_cancellation_impl.cc b/modules/audio_processing/echo_cancellation_impl.cc
index 878d84f..99f676c 100644
--- a/modules/audio_processing/echo_cancellation_impl.cc
+++ b/modules/audio_processing/echo_cancellation_impl.cc
@@ -16,6 +16,7 @@
 #include "modules/audio_processing/aec/echo_cancellation.h"
 #include "modules/audio_processing/audio_buffer.h"
 #include "rtc_base/checks.h"
+#include "system_wrappers/include/field_trial.h"
 
 namespace webrtc {
 
@@ -49,6 +50,14 @@
   }
 }
 
+bool EnforceZeroStreamDelay() {
+#if defined(CHROMEOS)
+  return !field_trial::IsEnabled("WebRTC-Aec2ZeroStreamDelayKillSwitch");
+#else
+  return false;
+#endif
+}
+
 }  // namespace
 
 struct EchoCancellationImpl::StreamProperties {
@@ -106,7 +115,8 @@
       stream_has_echo_(false),
       delay_logging_enabled_(false),
       extended_filter_enabled_(false),
-      delay_agnostic_enabled_(false) {
+      delay_agnostic_enabled_(false),
+      enforce_zero_stream_delay_(EnforceZeroStreamDelay()) {
   RTC_DCHECK(crit_render);
   RTC_DCHECK(crit_capture);
 }
@@ -145,6 +155,9 @@
     return AudioProcessing::kNoError;
   }
 
+  const int stream_delay_ms_use =
+      enforce_zero_stream_delay_ ? 0 : stream_delay_ms;
+
   if (drift_compensation_enabled_ && !was_stream_drift_set_) {
     return AudioProcessing::kStreamParameterNotSetError;
   }
@@ -160,10 +173,11 @@
   stream_has_echo_ = false;
   for (size_t i = 0; i < audio->num_channels(); i++) {
     for (size_t j = 0; j < stream_properties_->num_reverse_channels; j++) {
-      err = WebRtcAec_Process(
-          cancellers_[handle_index]->state(), audio->split_bands_const_f(i),
-          audio->num_bands(), audio->split_bands_f(i),
-          audio->num_frames_per_band(), stream_delay_ms, stream_drift_samples_);
+      err = WebRtcAec_Process(cancellers_[handle_index]->state(),
+                              audio->split_bands_const_f(i), audio->num_bands(),
+                              audio->split_bands_f(i),
+                              audio->num_frames_per_band(), stream_delay_ms_use,
+                              stream_drift_samples_);
 
       if (err != AudioProcessing::kNoError) {
         err = MapError(err);
diff --git a/modules/audio_processing/echo_cancellation_impl.h b/modules/audio_processing/echo_cancellation_impl.h
index d410a11..6700249 100644
--- a/modules/audio_processing/echo_cancellation_impl.h
+++ b/modules/audio_processing/echo_cancellation_impl.h
@@ -105,6 +105,9 @@
   bool delay_agnostic_enabled_ RTC_GUARDED_BY(crit_capture_);
   bool refined_adaptive_filter_enabled_ RTC_GUARDED_BY(crit_capture_) = false;
 
+  // Only active on Chrome OS devices.
+  const bool enforce_zero_stream_delay_ RTC_GUARDED_BY(crit_capture_);
+
   std::vector<std::unique_ptr<Canceller>> cancellers_;
   std::unique_ptr<StreamProperties> stream_properties_;