Make LevelEstimation not a ProcessingComponent.

BUG=webrtc:5355

Review URL: https://codereview.webrtc.org/1523483002

Cr-Commit-Position: refs/heads/master@{#11033}
diff --git a/webrtc/modules/audio_processing/audio_processing_impl.cc b/webrtc/modules/audio_processing/audio_processing_impl.cc
index 805e05e..e99720d 100644
--- a/webrtc/modules/audio_processing/audio_processing_impl.cc
+++ b/webrtc/modules/audio_processing/audio_processing_impl.cc
@@ -150,14 +150,13 @@
       : echo_cancellation(nullptr),
         echo_control_mobile(nullptr),
         gain_control(nullptr),
-        level_estimator(nullptr),
         voice_detection(nullptr) {}
   // Accessed externally of APM without any lock acquired.
   EchoCancellationImpl* echo_cancellation;
   EchoControlMobileImpl* echo_control_mobile;
   GainControlImpl* gain_control;
   rtc::scoped_ptr<HighPassFilterImpl> high_pass_filter;
-  LevelEstimatorImpl* level_estimator;
+  rtc::scoped_ptr<LevelEstimatorImpl> level_estimator;
   rtc::scoped_ptr<NoiseSuppressionImpl> noise_suppression;
   VoiceDetectionImpl* voice_detection;
   rtc::scoped_ptr<GainControlForNewAgc> gain_control_for_new_agc;
@@ -243,8 +242,8 @@
         new GainControlImpl(this, &crit_capture_, &crit_capture_);
     public_submodules_->high_pass_filter.reset(
         new HighPassFilterImpl(&crit_capture_));
-    public_submodules_->level_estimator =
-        new LevelEstimatorImpl(this, &crit_capture_);
+    public_submodules_->level_estimator.reset(
+        new LevelEstimatorImpl(&crit_capture_));
     public_submodules_->noise_suppression.reset(
         new NoiseSuppressionImpl(&crit_capture_));
     public_submodules_->voice_detection =
@@ -259,8 +258,6 @@
     private_submodules_->component_list.push_back(
         public_submodules_->gain_control);
     private_submodules_->component_list.push_back(
-        public_submodules_->level_estimator);
-    private_submodules_->component_list.push_back(
         public_submodules_->voice_detection);
   }
 
@@ -398,6 +395,7 @@
   InitializeIntelligibility();
   InitializeHighPassFilter();
   InitializeNoiseSuppression();
+  InitializeLevelEstimator();
 
 #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
   if (debug_dump_.debug_file->Open()) {
@@ -804,7 +802,7 @@
   }
 
   // The level estimator operates on the recombined data.
-  RETURN_ON_ERR(public_submodules_->level_estimator->ProcessStream(ca));
+  public_submodules_->level_estimator->ProcessStream(ca);
 
   capture_.was_stream_delay_set = false;
   return kNoError;
@@ -1146,7 +1144,7 @@
 LevelEstimator* AudioProcessingImpl::level_estimator() const {
   // Adding a lock here has no effect as it allows any access to the submodule
   // from the returned pointer.
-  return public_submodules_->level_estimator;
+  return public_submodules_->level_estimator.get();
 }
 
 NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
@@ -1178,6 +1176,9 @@
   if (public_submodules_->noise_suppression->is_enabled()) {
     enabled_count++;
   }
+  if (public_submodules_->level_estimator->is_enabled()) {
+    enabled_count++;
+  }
 
   // Data is unchanged if no components are enabled, or if only
   // public_submodules_->level_estimator
@@ -1302,6 +1303,10 @@
                                                     proc_sample_rate_hz());
 }
 
+void AudioProcessingImpl::InitializeLevelEstimator() {
+  public_submodules_->level_estimator->Initialize();
+}
+
 void AudioProcessingImpl::MaybeUpdateHistograms() {
   static const int kMinDiffDelayMs = 60;
 
diff --git a/webrtc/modules/audio_processing/audio_processing_impl.h b/webrtc/modules/audio_processing/audio_processing_impl.h
index c4d5f08..eb34976 100644
--- a/webrtc/modules/audio_processing/audio_processing_impl.h
+++ b/webrtc/modules/audio_processing/audio_processing_impl.h
@@ -185,6 +185,8 @@
       EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
   void InitializeNoiseSuppression()
       EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
+  void InitializeLevelEstimator()
+      EXCLUSIVE_LOCKS_REQUIRED(crit_capture_);
   int InitializeLocked(const ProcessingConfig& config)
       EXCLUSIVE_LOCKS_REQUIRED(crit_render_, crit_capture_);
 
diff --git a/webrtc/modules/audio_processing/level_estimator_impl.cc b/webrtc/modules/audio_processing/level_estimator_impl.cc
index 52f6697..aa676a8 100644
--- a/webrtc/modules/audio_processing/level_estimator_impl.cc
+++ b/webrtc/modules/audio_processing/level_estimator_impl.cc
@@ -11,83 +11,55 @@
 #include "webrtc/modules/audio_processing/level_estimator_impl.h"
 
 #include "webrtc/modules/audio_processing/audio_buffer.h"
-#include "webrtc/modules/audio_processing/include/audio_processing.h"
 #include "webrtc/modules/audio_processing/rms_level.h"
 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
 
 namespace webrtc {
 
-LevelEstimatorImpl::LevelEstimatorImpl(const AudioProcessing* apm,
-                                       rtc::CriticalSection* crit)
-    : ProcessingComponent(), crit_(crit) {
-  RTC_DCHECK(apm);
+LevelEstimatorImpl::LevelEstimatorImpl(rtc::CriticalSection* crit)
+    : crit_(crit), rms_(new RMSLevel()) {
   RTC_DCHECK(crit);
 }
 
 LevelEstimatorImpl::~LevelEstimatorImpl() {}
 
-int LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) {
+void LevelEstimatorImpl::Initialize() {
   rtc::CritScope cs(crit_);
+  rms_->Reset();
+}
 
-  if (!is_component_enabled()) {
-    return AudioProcessing::kNoError;
+void LevelEstimatorImpl::ProcessStream(AudioBuffer* audio) {
+  RTC_DCHECK(audio);
+  rtc::CritScope cs(crit_);
+  if (!enabled_) {
+    return;
   }
 
-  RMSLevel* rms_level = static_cast<RMSLevel*>(handle(0));
-  for (int i = 0; i < audio->num_channels(); ++i) {
-    rms_level->Process(audio->channels_const()[i],
-                       audio->num_frames());
+  for (int i = 0; i < audio->num_channels(); i++) {
+    rms_->Process(audio->channels_const()[i], audio->num_frames());
   }
-
-  return AudioProcessing::kNoError;
 }
 
 int LevelEstimatorImpl::Enable(bool enable) {
   rtc::CritScope cs(crit_);
-  return EnableComponent(enable);
+  if (enable && !enabled_) {
+    rms_->Reset();
+  }
+  enabled_ = enable;
+  return AudioProcessing::kNoError;
 }
 
 bool LevelEstimatorImpl::is_enabled() const {
   rtc::CritScope cs(crit_);
-  return is_component_enabled();
+  return enabled_;
 }
 
 int LevelEstimatorImpl::RMS() {
   rtc::CritScope cs(crit_);
-  if (!is_component_enabled()) {
+  if (!enabled_) {
     return AudioProcessing::kNotEnabledError;
   }
 
-  RMSLevel* rms_level = static_cast<RMSLevel*>(handle(0));
-  return rms_level->RMS();
+  return rms_->RMS();
 }
-
-// The ProcessingComponent implementation is pretty weird in this class since
-// we have only a single instance of the trivial underlying component.
-void* LevelEstimatorImpl::CreateHandle() const {
-  return new RMSLevel;
-}
-
-void LevelEstimatorImpl::DestroyHandle(void* handle) const {
-  delete static_cast<RMSLevel*>(handle);
-}
-
-int LevelEstimatorImpl::InitializeHandle(void* handle) const {
-  rtc::CritScope cs(crit_);
-  static_cast<RMSLevel*>(handle)->Reset();
-  return AudioProcessing::kNoError;
-}
-
-int LevelEstimatorImpl::ConfigureHandle(void* /*handle*/) const {
-  return AudioProcessing::kNoError;
-}
-
-int LevelEstimatorImpl::num_handles_required() const {
-  return 1;
-}
-
-int LevelEstimatorImpl::GetHandleError(void* /*handle*/) const {
-  return AudioProcessing::kUnspecifiedError;
-}
-
 }  // namespace webrtc
diff --git a/webrtc/modules/audio_processing/level_estimator_impl.h b/webrtc/modules/audio_processing/level_estimator_impl.h
index d560223..4401da3 100644
--- a/webrtc/modules/audio_processing/level_estimator_impl.h
+++ b/webrtc/modules/audio_processing/level_estimator_impl.h
@@ -11,42 +11,36 @@
 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_LEVEL_ESTIMATOR_IMPL_H_
 #define WEBRTC_MODULES_AUDIO_PROCESSING_LEVEL_ESTIMATOR_IMPL_H_
 
+#include "webrtc/base/constructormagic.h"
 #include "webrtc/base/criticalsection.h"
+#include "webrtc/base/scoped_ptr.h"
 #include "webrtc/modules/audio_processing/include/audio_processing.h"
-#include "webrtc/modules/audio_processing/processing_component.h"
-#include "webrtc/modules/audio_processing/rms_level.h"
 
 namespace webrtc {
 
 class AudioBuffer;
+class RMSLevel;
 
-class LevelEstimatorImpl : public LevelEstimator,
-                           public ProcessingComponent {
+class LevelEstimatorImpl : public LevelEstimator {
  public:
-  LevelEstimatorImpl(const AudioProcessing* apm, rtc::CriticalSection* crit);
-  virtual ~LevelEstimatorImpl();
+  explicit LevelEstimatorImpl(rtc::CriticalSection* crit);
+  ~LevelEstimatorImpl() override;
 
-  int ProcessStream(AudioBuffer* audio);
+  // TODO(peah): Fold into ctor, once public API is removed.
+  void Initialize();
+  void ProcessStream(AudioBuffer* audio);
 
   // LevelEstimator implementation.
-  bool is_enabled() const override;
-
- private:
-  // LevelEstimator implementation.
   int Enable(bool enable) override;
+  bool is_enabled() const override;
   int RMS() override;
 
-  // ProcessingComponent implementation.
-  void* CreateHandle() const override;
-  int InitializeHandle(void* handle) const override;
-  int ConfigureHandle(void* handle) const override;
-  void DestroyHandle(void* handle) const override;
-  int num_handles_required() const override;
-  int GetHandleError(void* handle) const override;
-
-  rtc::CriticalSection* const crit_;
+ private:
+  rtc::CriticalSection* const crit_ = nullptr;
+  bool enabled_ GUARDED_BY(crit_) = false;
+  rtc::scoped_ptr<RMSLevel> rms_ GUARDED_BY(crit_);
+  RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(LevelEstimatorImpl);
 };
-
 }  // namespace webrtc
 
 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_LEVEL_ESTIMATOR_IMPL_H_