Passing a neural echo residual estimator pointer to the echo control factory.

As we need to provide a default implementation for the added API, the
old ones have not been yet marked as [[deprecated]].

Bug: webrtc:442444736
Change-Id: I86ab70b4ac2ce91ff435e02d76e613d29be5498c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/411461
Reviewed-by: Sam Zackrisson <saza@webrtc.org>
Commit-Queue: Jesus de Vicente Pena <devicentepena@webrtc.org>
Reviewed-by: Per Åhgren <peah@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#45746}
diff --git a/api/audio/BUILD.gn b/api/audio/BUILD.gn
index a40d8a3..5e24cc7 100644
--- a/api/audio/BUILD.gn
+++ b/api/audio/BUILD.gn
@@ -152,6 +152,7 @@
   deps = [
     ":aec3_config",
     ":echo_control",
+    ":neural_residual_echo_estimator_api",
     "../../modules/audio_processing/aec3",
     "../../rtc_base/system:rtc_export",
     "../environment",
@@ -163,6 +164,7 @@
   visibility = [ "*" ]
   sources = [ "echo_control.h" ]
   deps = [
+    ":neural_residual_echo_estimator_api",
     "../../rtc_base:checks",
     "../environment",
     "//third_party/abseil-cpp/absl/base:nullability",
diff --git a/api/audio/echo_canceller3_factory.cc b/api/audio/echo_canceller3_factory.cc
index 6e9cd46..b615c63 100644
--- a/api/audio/echo_canceller3_factory.cc
+++ b/api/audio/echo_canceller3_factory.cc
@@ -15,6 +15,7 @@
 #include "absl/base/nullability.h"
 #include "api/audio/echo_canceller3_config.h"
 #include "api/audio/echo_control.h"
+#include "api/audio/neural_residual_echo_estimator.h"
 #include "api/environment/environment.h"
 #include "modules/audio_processing/aec3/echo_canceller3.h"
 
@@ -41,4 +42,15 @@
       num_render_channels, num_capture_channels);
 }
 
+absl_nonnull std::unique_ptr<EchoControl> EchoCanceller3Factory::Create(
+    const Environment& env,
+    int sample_rate_hz,
+    int num_render_channels,
+    int num_capture_channels,
+    NeuralResidualEchoEstimator* neural_residual_echo_estimator) {
+  return std::make_unique<EchoCanceller3>(
+      env, config_, multichannel_config_, neural_residual_echo_estimator,
+      sample_rate_hz, num_render_channels, num_capture_channels);
+}
+
 }  // namespace webrtc
diff --git a/api/audio/echo_canceller3_factory.h b/api/audio/echo_canceller3_factory.h
index 756fd46..387ef9b 100644
--- a/api/audio/echo_canceller3_factory.h
+++ b/api/audio/echo_canceller3_factory.h
@@ -17,6 +17,7 @@
 #include "absl/base/nullability.h"
 #include "api/audio/echo_canceller3_config.h"
 #include "api/audio/echo_control.h"
+#include "api/audio/neural_residual_echo_estimator.h"
 #include "api/environment/environment.h"
 #include "rtc_base/system/rtc_export.h"
 
@@ -44,6 +45,16 @@
       int num_render_channels,
       int num_capture_channels) override;
 
+  // Creates an EchoCanceller3 with a specified channel count and sampling rate.
+  // If provided, `neural_residual_echo_estimator` is used to estimate the
+  // residual echo.
+  absl_nonnull std::unique_ptr<EchoControl> Create(
+      const Environment& env,
+      int sample_rate_hz,
+      int num_render_channels,
+      int num_capture_channels,
+      NeuralResidualEchoEstimator* neural_residual_echo_estimator) override;
+
  private:
   const EchoCanceller3Config config_;
   const std::optional<EchoCanceller3Config> multichannel_config_;
diff --git a/api/audio/echo_control.h b/api/audio/echo_control.h
index d1a1faa..9b28e82 100644
--- a/api/audio/echo_control.h
+++ b/api/audio/echo_control.h
@@ -14,6 +14,7 @@
 #include <memory>
 
 #include "absl/base/nullability.h"
+#include "api/audio/neural_residual_echo_estimator.h"
 #include "api/environment/environment.h"
 
 namespace webrtc {
@@ -72,7 +73,18 @@
       int sample_rate_hz,
       int num_render_channels,
       int num_capture_channels) = 0;
+
+  virtual absl_nonnull std::unique_ptr<EchoControl> Create(
+      const Environment& env,
+      int sample_rate_hz,
+      int num_render_channels,
+      int num_capture_channels,
+      NeuralResidualEchoEstimator* neural_residual_echo_estimator) {
+    return Create(env, sample_rate_hz, num_render_channels,
+                  num_capture_channels);
+  }
 };
+
 }  // namespace webrtc
 
 #endif  // API_AUDIO_ECHO_CONTROL_H_
diff --git a/modules/audio_processing/BUILD.gn b/modules/audio_processing/BUILD.gn
index 26c1970..853040d 100644
--- a/modules/audio_processing/BUILD.gn
+++ b/modules/audio_processing/BUILD.gn
@@ -454,6 +454,7 @@
           ":runtime_settings_protobuf_utils",
           "../../api/audio:audio_frame_api",
           "../../api/audio:echo_control",
+          "../../api/audio:neural_residual_echo_estimator_api",
           "../../rtc_base:rtc_base_tests_utils",
           "aec_dump",
           "aec_dump:aec_dump_unittests",
diff --git a/modules/audio_processing/audio_processing_impl.cc b/modules/audio_processing/audio_processing_impl.cc
index fc28ea9..b4e822b 100644
--- a/modules/audio_processing/audio_processing_impl.cc
+++ b/modules/audio_processing/audio_processing_impl.cc
@@ -1912,7 +1912,8 @@
     if (echo_control_factory_) {
       submodules_.echo_controller = echo_control_factory_->Create(
           env_, proc_sample_rate_hz(), num_reverse_channels(),
-          num_proc_channels());
+          num_proc_channels(),
+          submodules_.neural_residual_echo_estimator.get());
       RTC_DCHECK(submodules_.echo_controller);
     } else {
       EchoCanceller3Config config;
diff --git a/modules/audio_processing/audio_processing_unittest.cc b/modules/audio_processing/audio_processing_unittest.cc
index 8a36515..27d8a13 100644
--- a/modules/audio_processing/audio_processing_unittest.cc
+++ b/modules/audio_processing/audio_processing_unittest.cc
@@ -37,6 +37,7 @@
 #include "api/audio/builtin_audio_processing_builder.h"
 #include "api/audio/echo_control.h"
 #include "api/audio/echo_detector_creator.h"
+#include "api/audio/neural_residual_echo_estimator.h"
 #include "api/environment/environment.h"
 #include "api/environment/environment_factory.h"
 #include "api/make_ref_counted.h"
@@ -2610,12 +2611,16 @@
               Create,
               (const Environment&, int, int, int),
               (override));
+  MOCK_METHOD(std::unique_ptr<EchoControl>,
+              Create,
+              (const Environment&, int, int, int, NeuralResidualEchoEstimator*),
+              (override));
 };
 
 TEST(ApmConfiguration, EchoControlInjection) {
   // Verify that apm uses an injected echo controller if one is provided.
   auto echo_control_factory = std::make_unique<MockEchoControlFactory>();
-  EXPECT_CALL(*echo_control_factory, Create(_, _, _, _))
+  EXPECT_CALL(*echo_control_factory, Create(_, _, _, _, _))
       .WillOnce(WithoutArgs([] {
         auto ec = std::make_unique<test::MockEchoControl>();
         EXPECT_CALL(*ec, AnalyzeRender).Times(1);