Cleanup of the APM creation

This CL removes the possibility that APM cannot be created, i.e., that
the create method can return nullptr. That was already the case
implicitly but this CL makes that behavior explicit.


Bug: webrtc:5298
Change-Id: I2706ea538c9d1b4bcd65faecab637640a209a4dc
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/183101
Reviewed-by: Sam Zackrisson <saza@webrtc.org>
Commit-Queue: Per Åhgren <peah@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32029}
diff --git a/modules/audio_processing/audio_processing_builder_impl.cc b/modules/audio_processing/audio_processing_builder_impl.cc
index e89bbec..f55c915 100644
--- a/modules/audio_processing/audio_processing_builder_impl.cc
+++ b/modules/audio_processing/audio_processing_builder_impl.cc
@@ -35,16 +35,10 @@
 #else
 
   // Standard implementation.
-  AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
+  return new rtc::RefCountedObject<AudioProcessingImpl>(
       config, std::move(capture_post_processing_),
       std::move(render_pre_processing_), std::move(echo_control_factory_),
       std::move(echo_detector_), std::move(capture_analyzer_));
-  if (apm->Initialize() != AudioProcessing::kNoError) {
-    delete apm;
-    apm = nullptr;
-  }
-  return apm;
-
 #endif
 }
 
diff --git a/modules/audio_processing/audio_processing_impl.cc b/modules/audio_processing/audio_processing_impl.cc
index 485022a..82cc6f1 100644
--- a/modules/audio_processing/audio_processing_impl.cc
+++ b/modules/audio_processing/audio_processing_impl.cc
@@ -307,6 +307,8 @@
   config_.gain_controller1.analog_gain_controller.enable_digital_adaptive =
       !config.Get<ExperimentalAgc>().digital_adaptive_disabled;
 #endif
+
+  Initialize();
 }
 
 AudioProcessingImpl::~AudioProcessingImpl() = default;
@@ -315,7 +317,8 @@
   // Run in a single-threaded manner during initialization.
   MutexLock lock_render(&mutex_render_);
   MutexLock lock_capture(&mutex_capture_);
-  return InitializeLocked();
+  InitializeLocked();
+  return kNoError;
 }
 
 int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
@@ -356,7 +359,7 @@
   return InitializeLocked(processing_config);
 }
 
-int AudioProcessingImpl::InitializeLocked() {
+void AudioProcessingImpl::InitializeLocked() {
   UpdateActiveSubmoduleStates();
 
   const int render_audiobuffer_sample_rate_hz =
@@ -425,7 +428,6 @@
   if (aec_dump_) {
     aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
   }
-  return kNoError;
 }
 
 int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
@@ -519,7 +521,8 @@
         capture_nonlocked_.capture_processing_format.sample_rate_hz();
   }
 
-  return InitializeLocked();
+  InitializeLocked();
+  return kNoError;
 }
 
 void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
diff --git a/modules/audio_processing/audio_processing_impl.h b/modules/audio_processing/audio_processing_impl.h
index dfd5f63..ac411b4 100644
--- a/modules/audio_processing/audio_processing_impl.h
+++ b/modules/audio_processing/audio_processing_impl.h
@@ -140,7 +140,7 @@
 
  protected:
   // Overridden in a mock.
-  virtual int InitializeLocked()
+  virtual void InitializeLocked()
       RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_render_, mutex_capture_);
 
  private:
diff --git a/modules/audio_processing/audio_processing_impl_unittest.cc b/modules/audio_processing/audio_processing_impl_unittest.cc
index 8f28941..e289c31 100644
--- a/modules/audio_processing/audio_processing_impl_unittest.cc
+++ b/modules/audio_processing/audio_processing_impl_unittest.cc
@@ -37,9 +37,9 @@
   explicit MockInitialize(const webrtc::Config& config)
       : AudioProcessingImpl(config) {}
 
-  MOCK_METHOD(int, InitializeLocked, (), (override));
-  int RealInitializeLocked() RTC_NO_THREAD_SAFETY_ANALYSIS {
-    return AudioProcessingImpl::InitializeLocked();
+  MOCK_METHOD(void, InitializeLocked, (), (override));
+  void RealInitializeLocked() RTC_NO_THREAD_SAFETY_ANALYSIS {
+    AudioProcessingImpl::InitializeLocked();
   }
 
   MOCK_METHOD(void, AddRef, (), (const, override));
diff --git a/modules/audio_processing/include/audio_processing.h b/modules/audio_processing/include/audio_processing.h
index 9e2d6dc..fe45e64 100644
--- a/modules/audio_processing/include/audio_processing.h
+++ b/modules/audio_processing/include/audio_processing.h
@@ -488,6 +488,7 @@
   // rate and number of channels) have changed. Passing updated parameters
   // directly to |ProcessStream()| and |ProcessReverseStream()| is permissible.
   // If the parameters are known at init-time though, they may be provided.
+  // TODO(webrtc:5298): Change to return void.
   virtual int Initialize() = 0;
 
   // The int16 interfaces require:
diff --git a/modules/audio_processing/test/audio_processing_builder_for_testing.cc b/modules/audio_processing/test/audio_processing_builder_for_testing.cc
index 26ed679..faab18f 100644
--- a/modules/audio_processing/test/audio_processing_builder_for_testing.cc
+++ b/modules/audio_processing/test/audio_processing_builder_for_testing.cc
@@ -30,13 +30,10 @@
 
 AudioProcessing* AudioProcessingBuilderForTesting::Create(
     const webrtc::Config& config) {
-  AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
+  return new rtc::RefCountedObject<AudioProcessingImpl>(
       config, std::move(capture_post_processing_),
       std::move(render_pre_processing_), std::move(echo_control_factory_),
       std::move(echo_detector_), std::move(capture_analyzer_));
-  int error = apm->Initialize();
-  RTC_CHECK_EQ(error, AudioProcessing::kNoError);
-  return apm;
 }
 
 #else