In tests replace AudioProcessingBuilder with BuiltinAudioProcessingBuilder

To move towards deprecating AudioProcessingBuilder

Bug: webrtc:369904700
Change-Id: I7998b331eca26c2185c94c39c1310ef7b6faa717
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/367221
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Auto-Submit: Danil Chapovalov <danilchap@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#43347}
diff --git a/call/BUILD.gn b/call/BUILD.gn
index ab93575..6ce4327 100644
--- a/call/BUILD.gn
+++ b/call/BUILD.gn
@@ -661,6 +661,7 @@
         "../api:simulated_network_api",
         "../api/audio:audio_device",
         "../api/audio:audio_processing",
+        "../api/audio:builtin_audio_processing_builder",
         "../api/audio_codecs:builtin_audio_encoder_factory",
         "../api/environment",
         "../api/numerics",
diff --git a/call/call_perf_tests.cc b/call/call_perf_tests.cc
index 6ec41fe..58c578d 100644
--- a/call/call_perf_tests.cc
+++ b/call/call_perf_tests.cc
@@ -24,7 +24,7 @@
 #include "absl/strings/string_view.h"
 #include "api/array_view.h"
 #include "api/audio/audio_device.h"
-#include "api/audio/audio_processing.h"
+#include "api/audio/builtin_audio_processing_builder.h"
 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
 #include "api/environment/environment.h"
 #include "api/field_trials_view.h"
@@ -246,7 +246,7 @@
     AudioState::Config send_audio_state_config;
     send_audio_state_config.audio_mixer = AudioMixerImpl::Create();
     send_audio_state_config.audio_processing =
-        AudioProcessingBuilder().Create();
+        BuiltinAudioProcessingBuilder().Build(env());
     send_audio_state_config.audio_device_module = fake_audio_device;
     CallConfig sender_config = SendCallConfig();
 
diff --git a/modules/audio_processing/BUILD.gn b/modules/audio_processing/BUILD.gn
index e4bb0c7..03a63bb 100644
--- a/modules/audio_processing/BUILD.gn
+++ b/modules/audio_processing/BUILD.gn
@@ -349,6 +349,7 @@
         "../../api/audio:audio_processing",
         "../../api/audio:builtin_audio_processing_builder",
         "../../api/audio:echo_detector_creator",
+        "../../api/environment",
         "../../api/environment:environment_factory",
         "../../common_audio",
         "../../common_audio:common_audio_c",
diff --git a/modules/audio_processing/audio_processing_impl_unittest.cc b/modules/audio_processing/audio_processing_impl_unittest.cc
index cebc60a..d450338 100644
--- a/modules/audio_processing/audio_processing_impl_unittest.cc
+++ b/modules/audio_processing/audio_processing_impl_unittest.cc
@@ -18,6 +18,7 @@
 
 #include "api/audio/audio_processing.h"
 #include "api/audio/builtin_audio_processing_builder.h"
+#include "api/environment/environment.h"
 #include "api/environment/environment_factory.h"
 #include "api/make_ref_counted.h"
 #include "api/scoped_refptr.h"
@@ -644,7 +645,7 @@
   webrtc::AudioProcessing::Config config;
   config.gain_controller1.enabled = false;
   config.gain_controller2.enabled = false;
-  auto apm = AudioProcessingBuilder().SetConfig(config).Create();
+  auto apm = BuiltinAudioProcessingBuilder(config).Build(CreateEnvironment());
 
   int startup_volume = GetParam();
   int recommended_volume = ProcessInputVolume(
@@ -663,7 +664,7 @@
   webrtc::AudioProcessing::Config config;
   config.gain_controller1.enabled = false;
   config.gain_controller2.enabled = false;
-  auto apm = AudioProcessingBuilder().SetConfig(config).Create();
+  auto apm = BuiltinAudioProcessingBuilder(config).Build(CreateEnvironment());
 
   Random rand_gen(42);
   for (int i = 0; i < 32; ++i) {
@@ -708,7 +709,8 @@
 TEST_P(ApmInputVolumeControllerParametrizedTest,
        EnforceMinInputVolumeAtStartupWithZeroVolume) {
   const StreamConfig stream_config(sample_rate_hz(), num_channels());
-  auto apm = AudioProcessingBuilder().SetConfig(GetConfig()).Create();
+  auto apm =
+      BuiltinAudioProcessingBuilder(GetConfig()).Build(CreateEnvironment());
 
   apm->set_stream_analog_level(0);
   apm->ProcessStream(channel_pointers(), stream_config, stream_config,
@@ -719,7 +721,8 @@
 TEST_P(ApmInputVolumeControllerParametrizedTest,
        EnforceMinInputVolumeAtStartupWithNonZeroVolume) {
   const StreamConfig stream_config(sample_rate_hz(), num_channels());
-  auto apm = AudioProcessingBuilder().SetConfig(GetConfig()).Create();
+  auto apm =
+      BuiltinAudioProcessingBuilder(GetConfig()).Build(CreateEnvironment());
 
   constexpr int kStartupVolume = 3;
   apm->set_stream_analog_level(kStartupVolume);
@@ -737,7 +740,8 @@
     GTEST_SKIP() << "Does not apply to AGC1";
   }
   const StreamConfig stream_config(sample_rate_hz(), num_channels());
-  auto apm = AudioProcessingBuilder().SetConfig(GetConfig()).Create();
+  auto apm =
+      BuiltinAudioProcessingBuilder(GetConfig()).Build(CreateEnvironment());
 
   apm->set_stream_analog_level(20);
   apm->ProcessStream(channel_pointers(), stream_config, stream_config,
@@ -752,7 +756,8 @@
 TEST_P(ApmInputVolumeControllerParametrizedTest,
        DoNotEnforceMinInputVolumeAtStartupWithHighVolume) {
   const StreamConfig stream_config(sample_rate_hz(), num_channels());
-  auto apm = AudioProcessingBuilder().SetConfig(GetConfig()).Create();
+  auto apm =
+      BuiltinAudioProcessingBuilder(GetConfig()).Build(CreateEnvironment());
 
   constexpr int kStartupVolume = 200;
   apm->set_stream_analog_level(kStartupVolume);
@@ -764,7 +769,8 @@
 TEST_P(ApmInputVolumeControllerParametrizedTest,
        DoNotEnforceMinInputVolumeAfterManualVolumeAdjustmentToZero) {
   const StreamConfig stream_config(sample_rate_hz(), num_channels());
-  auto apm = AudioProcessingBuilder().SetConfig(GetConfig()).Create();
+  auto apm =
+      BuiltinAudioProcessingBuilder(GetConfig()).Build(CreateEnvironment());
 
   apm->set_stream_analog_level(100);
   apm->ProcessStream(channel_pointers(), stream_config, stream_config,
@@ -802,10 +808,10 @@
 // active, the recommended volume must always be the applied volume.
 TEST(AudioProcessingImplTest,
      RecommendAppliedInputVolumeWithNoAgcWithNoEmulation) {
-  auto apm = AudioProcessingBuilder()
-                 .SetConfig({.capture_level_adjustment = {.enabled = false},
-                             .gain_controller1 = {.enabled = false}})
-                 .Create();
+  auto apm = BuiltinAudioProcessingBuilder(
+                 {.capture_level_adjustment = {.enabled = false},
+                  .gain_controller1 = {.enabled = false}})
+                 .Build(CreateEnvironment());
 
   constexpr int kOneFrame = 1;
   EXPECT_EQ(ProcessInputVolume(*apm, kOneFrame, /*initial_volume=*/123), 123);
@@ -819,14 +825,13 @@
 // TODO(bugs.webrtc.org/14581): Enable when APM fixed to let this test pass.
 TEST(AudioProcessingImplTest,
      DISABLED_RecommendAppliedInputVolumeWithNoAgcWithEmulation) {
-  auto apm =
-      AudioProcessingBuilder()
-          .SetConfig({.capture_level_adjustment = {.enabled = true,
-                                                   .analog_mic_gain_emulation{
-                                                       .enabled = true,
-                                                       .initial_level = 255}},
-                      .gain_controller1 = {.enabled = false}})
-          .Create();
+  auto apm = BuiltinAudioProcessingBuilder(
+                 {.capture_level_adjustment = {.enabled = true,
+                                               .analog_mic_gain_emulation{
+                                                   .enabled = true,
+                                                   .initial_level = 255}},
+                  .gain_controller1 = {.enabled = false}})
+                 .Build(CreateEnvironment());
 
   constexpr int kOneFrame = 1;
   EXPECT_EQ(ProcessInputVolume(*apm, kOneFrame, /*initial_volume=*/123), 123);
@@ -841,16 +846,15 @@
 // TODO(bugs.webrtc.org/14581): Enable when APM fixed to let this test pass.
 TEST(AudioProcessingImplTest,
      DISABLED_RecommendAppliedInputVolumeWithAgcWithEmulation) {
-  auto apm =
-      AudioProcessingBuilder()
-          .SetConfig({.capture_level_adjustment = {.enabled = true,
-                                                   .analog_mic_gain_emulation{
-                                                       .enabled = true}},
-                      .gain_controller1 = {.enabled = true,
-                                           .analog_gain_controller{
-                                               .enabled = true,
-                                           }}})
-          .Create();
+  auto apm = BuiltinAudioProcessingBuilder(
+                 {.capture_level_adjustment = {.enabled = true,
+                                               .analog_mic_gain_emulation{
+                                                   .enabled = true}},
+                  .gain_controller1 = {.enabled = true,
+                                       .analog_gain_controller{
+                                           .enabled = true,
+                                       }}})
+                 .Build(CreateEnvironment());
 
   constexpr int kOneFrame = 1;
   EXPECT_EQ(ProcessInputVolume(*apm, kOneFrame, /*initial_volume=*/123), 123);
@@ -862,7 +866,8 @@
     : public ::testing::TestWithParam<AudioProcessing::Config> {};
 
 TEST_P(Agc2ParametrizedTest, ProcessSucceedsWhenOneAgcEnabled) {
-  auto apm = AudioProcessingBuilder().SetConfig(GetParam()).Create();
+  auto apm =
+      BuiltinAudioProcessingBuilder(GetParam()).Build(CreateEnvironment());
   constexpr int kSampleRateHz = 48000;
   constexpr int kNumChannels = 1;
   std::array<float, kSampleRateHz / 100> buffer;
@@ -884,16 +889,17 @@
 
 TEST_P(Agc2ParametrizedTest,
        BitExactWithAndWithoutTransientSuppressionEnabledInConfig) {
+  const Environment env = CreateEnvironment();
   // Enable transient suppression in the config (expect no effect).
   auto config = GetParam();
   config.transient_suppression.enabled = true;
-  auto apm = AudioProcessingBuilder().SetConfig(config).Create();
+  auto apm = BuiltinAudioProcessingBuilder(config).Build(env);
   ASSERT_EQ(apm->Initialize(), AudioProcessing::kNoError);
   // Disable transient suppression in the config.
   auto config_reference = GetParam();
   config_reference.transient_suppression.enabled = false;
   auto apm_reference =
-      AudioProcessingBuilder().SetConfig(config_reference).Create();
+      BuiltinAudioProcessingBuilder(config_reference).Build(env);
   ASSERT_EQ(apm_reference->Initialize(), AudioProcessing::kNoError);
 
   constexpr int kSampleRateHz = 16000;
diff --git a/test/BUILD.gn b/test/BUILD.gn
index ed210b3..d5a5314 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -1248,6 +1248,7 @@
       "../api:simulated_network_api",
       "../api:transport_api",
       "../api/audio:audio_device",
+      "../api/audio:builtin_audio_processing_builder",
       "../api/audio_codecs:builtin_audio_decoder_factory",
       "../api/audio_codecs:builtin_audio_encoder_factory",
       "../api/environment",
diff --git a/test/call_test.cc b/test/call_test.cc
index bee23d6..76246e0 100644
--- a/test/call_test.cc
+++ b/test/call_test.cc
@@ -14,6 +14,7 @@
 #include <memory>
 
 #include "api/audio/audio_device.h"
+#include "api/audio/builtin_audio_processing_builder.h"
 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
 #include "api/environment/environment.h"
@@ -111,13 +112,14 @@
     num_flexfec_streams_ = test->GetNumFlexfecStreams();
     RTC_DCHECK(num_video_streams_ > 0 || num_audio_streams_ > 0);
     CallConfig send_config = SendCallConfig();
+    CallConfig recv_config = RecvCallConfig();
     test->ModifySenderBitrateConfig(&send_config.bitrate_config);
     if (num_audio_streams_ > 0) {
       CreateFakeAudioDevices(test->CreateCapturer(), test->CreateRenderer());
       test->OnFakeAudioDevicesCreated(fake_send_audio_device_.get(),
                                       fake_recv_audio_device_.get());
-      apm_send_ = AudioProcessingBuilder().Create();
-      apm_recv_ = AudioProcessingBuilder().Create();
+      apm_send_ = BuiltinAudioProcessingBuilder().Build(send_config.env);
+      apm_recv_ = BuiltinAudioProcessingBuilder().Build(recv_config.env);
       EXPECT_EQ(0, fake_send_audio_device_->Init());
       EXPECT_EQ(0, fake_recv_audio_device_->Init());
       AudioState::Config audio_state_config;
@@ -130,7 +132,6 @@
     }
     CreateSenderCall(std::move(send_config));
     if (test->ShouldCreateReceivers()) {
-      CallConfig recv_config = RecvCallConfig();
       test->ModifyReceiverBitrateConfig(&recv_config.bitrate_config);
       if (num_audio_streams_ > 0) {
         AudioState::Config audio_state_config;
diff --git a/test/scenario/BUILD.gn b/test/scenario/BUILD.gn
index 49b0739..5b7f9a6 100644
--- a/test/scenario/BUILD.gn
+++ b/test/scenario/BUILD.gn
@@ -89,6 +89,7 @@
       "../../api:time_controller",
       "../../api:transport_api",
       "../../api/audio:audio_device",
+      "../../api/audio:builtin_audio_processing_builder",
       "../../api/audio_codecs:builtin_audio_decoder_factory",
       "../../api/audio_codecs:builtin_audio_encoder_factory",
       "../../api/environment",
diff --git a/test/scenario/call_client.cc b/test/scenario/call_client.cc
index 607f3da..b58ae71 100644
--- a/test/scenario/call_client.cc
+++ b/test/scenario/call_client.cc
@@ -13,6 +13,7 @@
 #include <memory>
 #include <utility>
 
+#include "api/audio/builtin_audio_processing_builder.h"
 #include "api/environment/environment.h"
 #include "api/environment/environment_factory.h"
 #include "api/media_types.h"
@@ -42,14 +43,13 @@
 
 constexpr int kEventLogOutputIntervalMs = 5000;
 
-CallClientFakeAudio InitAudio(TimeController* time_controller) {
+CallClientFakeAudio InitAudio(const Environment& env) {
   CallClientFakeAudio setup;
   auto capturer = TestAudioDeviceModule::CreatePulsedNoiseCapturer(256, 48000);
   auto renderer = TestAudioDeviceModule::CreateDiscardRenderer(48000);
   setup.fake_audio_device = TestAudioDeviceModule::Create(
-      time_controller->GetTaskQueueFactory(), std::move(capturer),
-      std::move(renderer), 1.f);
-  setup.apm = AudioProcessingBuilder().Create();
+      &env.task_queue_factory(), std::move(capturer), std::move(renderer), 1.f);
+  setup.apm = BuiltinAudioProcessingBuilder().Build(env);
   setup.fake_audio_device->Init();
   AudioState::Config audio_state_config;
   audio_state_config.audio_mixer = AudioMixerImpl::Create();
@@ -223,7 +223,7 @@
       env_factory.Set(CreateEventLog(env_, *log_writer_factory_));
       env_ = env_factory.Create();
     }
-    fake_audio_setup_ = InitAudio(time_controller_);
+    fake_audio_setup_ = InitAudio(env_);
 
     call_ = CreateCall(env_, config, &network_controller_factory_,
                        fake_audio_setup_.audio_state);
diff --git a/video/BUILD.gn b/video/BUILD.gn
index c1473a5..83780db 100644
--- a/video/BUILD.gn
+++ b/video/BUILD.gn
@@ -520,6 +520,7 @@
         "../api:test_dependency_factory",
         "../api:video_quality_test_fixture_api",
         "../api/audio:audio_device",
+        "../api/audio:builtin_audio_processing_builder",
         "../api/environment",
         "../api/numerics",
         "../api/rtc_event_log:rtc_event_log_factory",
diff --git a/video/video_quality_test.cc b/video/video_quality_test.cc
index 824f088..7d148f6 100644
--- a/video/video_quality_test.cc
+++ b/video/video_quality_test.cc
@@ -23,6 +23,7 @@
 #include <vector>
 
 #include "api/audio/audio_device.h"
+#include "api/audio/builtin_audio_processing_builder.h"
 #include "api/fec_controller_override.h"
 #include "api/rtc_event_log_output_file.h"
 #include "api/task_queue/default_task_queue_factory.h"
@@ -1371,7 +1372,8 @@
 
   AudioState::Config audio_state_config;
   audio_state_config.audio_mixer = AudioMixerImpl::Create();
-  audio_state_config.audio_processing = AudioProcessingBuilder().Create();
+  audio_state_config.audio_processing =
+      BuiltinAudioProcessingBuilder().Build(send_call_config->env);
   audio_state_config.audio_device_module = audio_device;
   send_call_config->audio_state = AudioState::Create(audio_state_config);
   recv_call_config->audio_state = AudioState::Create(audio_state_config);
diff --git a/webrtc_lib_link_test.cc b/webrtc_lib_link_test.cc
index e3a7a7b..c65f153 100644
--- a/webrtc_lib_link_test.cc
+++ b/webrtc_lib_link_test.cc
@@ -10,6 +10,7 @@
 
 #include "api/audio/audio_device.h"
 #include "api/audio/audio_processing.h"
+#include "api/audio/builtin_audio_processing_builder.h"
 #include "api/audio_codecs/audio_decoder_factory_template.h"
 #include "api/audio_codecs/audio_encoder_factory_template.h"
 #include "api/audio_codecs/opus/audio_decoder_opus.h"
@@ -48,7 +49,8 @@
       std::make_unique<VideoDecoderFactoryTemplate<
           LibvpxVp8DecoderTemplateAdapter, LibvpxVp9DecoderTemplateAdapter,
           OpenH264DecoderTemplateAdapter, Dav1dDecoderTemplateAdapter>>();
-  media_deps.audio_processing = webrtc::AudioProcessingBuilder().Create();
+  media_deps.audio_processing_builder =
+      std::make_unique<BuiltinAudioProcessingBuilder>();
 }
 
 webrtc::PeerConnectionFactoryDependencies CreateSomePcfDeps() {