Fixed the render queue item size preallocation and verification, moved constant declarations, removed redundant queue allocation

BUG=

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

Cr-Commit-Position: refs/heads/master@{#10689}
diff --git a/webrtc/modules/audio_processing/echo_cancellation_impl.cc b/webrtc/modules/audio_processing/echo_cancellation_impl.cc
index c6f9200..14d1fc8 100644
--- a/webrtc/modules/audio_processing/echo_cancellation_impl.cc
+++ b/webrtc/modules/audio_processing/echo_cancellation_impl.cc
@@ -53,10 +53,14 @@
       return AudioProcessing::kUnspecifiedError;
   }
 }
-}  // namespace
 
-const size_t EchoCancellationImpl::kAllowedValuesOfSamplesPerFrame1;
-const size_t EchoCancellationImpl::kAllowedValuesOfSamplesPerFrame2;
+// Maximum length that a frame of samples can have.
+static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160;
+// Maximum number of frames to buffer in the render queue.
+// TODO(peah): Decrease this once we properly handle hugely unbalanced
+// reverse and forward call numbers.
+static const size_t kMaxNumFramesToBuffer = 100;
+}  // namespace
 
 EchoCancellationImpl::EchoCancellationImpl(const AudioProcessing* apm,
                                            CriticalSectionWrapper* crit)
@@ -72,9 +76,7 @@
       delay_logging_enabled_(false),
       extended_filter_enabled_(false),
       delay_agnostic_enabled_(false),
-      render_queue_element_max_size_(0) {
-  AllocateRenderQueue();
-}
+      render_queue_element_max_size_(0) {}
 
 EchoCancellationImpl::~EchoCancellationImpl() {}
 
@@ -384,15 +386,13 @@
 }
 
 void EchoCancellationImpl::AllocateRenderQueue() {
-  const size_t max_frame_size = std::max<size_t>(
-      kAllowedValuesOfSamplesPerFrame1, kAllowedValuesOfSamplesPerFrame2);
-
   const size_t new_render_queue_element_max_size = std::max<size_t>(
-      static_cast<size_t>(1), max_frame_size * num_handles_required());
+      static_cast<size_t>(1),
+      kMaxAllowedValuesOfSamplesPerFrame * num_handles_required());
 
   // Reallocate the queue if the queue item size is too small to fit the
   // data to put in the queue.
-  if (new_render_queue_element_max_size > render_queue_element_max_size_) {
+  if (render_queue_element_max_size_ < new_render_queue_element_max_size) {
     render_queue_element_max_size_ = new_render_queue_element_max_size;
 
     std::vector<float> template_queue_element(render_queue_element_max_size_);
@@ -401,12 +401,12 @@
         new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
             kMaxNumFramesToBuffer, template_queue_element,
             RenderQueueItemVerifier<float>(render_queue_element_max_size_)));
+
+    render_queue_buffer_.resize(render_queue_element_max_size_);
+    capture_queue_buffer_.resize(render_queue_element_max_size_);
   } else {
     render_signal_queue_->Clear();
   }
-
-  render_queue_buffer_.resize(new_render_queue_element_max_size);
-  capture_queue_buffer_.resize(new_render_queue_element_max_size);
 }
 
 void EchoCancellationImpl::SetExtraOptions(const Config& config) {
diff --git a/webrtc/modules/audio_processing/echo_cancellation_impl.h b/webrtc/modules/audio_processing/echo_cancellation_impl.h
index d4dfc6d..d77361c 100644
--- a/webrtc/modules/audio_processing/echo_cancellation_impl.h
+++ b/webrtc/modules/audio_processing/echo_cancellation_impl.h
@@ -48,12 +48,6 @@
   void ReadQueuedRenderData();
 
  private:
-  static const size_t kAllowedValuesOfSamplesPerFrame1 = 80;
-  static const size_t kAllowedValuesOfSamplesPerFrame2 = 160;
-  // TODO(peah): Decrease this once we properly handle hugely unbalanced
-  // reverse and forward call numbers.
-  static const size_t kMaxNumFramesToBuffer = 100;
-
   // EchoCancellation implementation.
   int Enable(bool enable) override;
   int enable_drift_compensation(bool enable) override;
diff --git a/webrtc/modules/audio_processing/echo_control_mobile_impl.cc b/webrtc/modules/audio_processing/echo_control_mobile_impl.cc
index b9e1e51..a32c77c 100644
--- a/webrtc/modules/audio_processing/echo_control_mobile_impl.cc
+++ b/webrtc/modules/audio_processing/echo_control_mobile_impl.cc
@@ -56,11 +56,14 @@
       return AudioProcessing::kUnspecifiedError;
   }
 }
+// Maximum length that a frame of samples can have.
+static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160;
+// Maximum number of frames to buffer in the render queue.
+// TODO(peah): Decrease this once we properly handle hugely unbalanced
+// reverse and forward call numbers.
+static const size_t kMaxNumFramesToBuffer = 100;
 }  // namespace
 
-const size_t EchoControlMobileImpl::kAllowedValuesOfSamplesPerFrame1;
-const size_t EchoControlMobileImpl::kAllowedValuesOfSamplesPerFrame2;
-
 size_t EchoControlMobile::echo_path_size_bytes() {
     return WebRtcAecm_echo_path_size_bytes();
 }
@@ -73,9 +76,7 @@
       routing_mode_(kSpeakerphone),
       comfort_noise_enabled_(true),
       external_echo_path_(NULL),
-      render_queue_element_max_size_(0) {
-  AllocateRenderQueue();
-}
+      render_queue_element_max_size_(0) {}
 
 EchoControlMobileImpl::~EchoControlMobileImpl() {
     if (external_echo_path_ != NULL) {
@@ -301,14 +302,13 @@
 }
 
 void EchoControlMobileImpl::AllocateRenderQueue() {
-  const size_t max_frame_size = std::max<size_t>(
-      kAllowedValuesOfSamplesPerFrame1, kAllowedValuesOfSamplesPerFrame2);
   const size_t new_render_queue_element_max_size = std::max<size_t>(
-      static_cast<size_t>(1), max_frame_size * num_handles_required());
+      static_cast<size_t>(1),
+      kMaxAllowedValuesOfSamplesPerFrame * num_handles_required());
 
   // Reallocate the queue if the queue item size is too small to fit the
   // data to put in the queue.
-  if (new_render_queue_element_max_size > render_queue_element_max_size_) {
+  if (render_queue_element_max_size_ < new_render_queue_element_max_size) {
     render_queue_element_max_size_ = new_render_queue_element_max_size;
 
     std::vector<int16_t> template_queue_element(render_queue_element_max_size_);
@@ -317,12 +317,12 @@
         new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
             kMaxNumFramesToBuffer, template_queue_element,
             RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_)));
+
+    render_queue_buffer_.resize(render_queue_element_max_size_);
+    capture_queue_buffer_.resize(render_queue_element_max_size_);
   } else {
     render_signal_queue_->Clear();
   }
-
-  render_queue_buffer_.resize(new_render_queue_element_max_size);
-  capture_queue_buffer_.resize(new_render_queue_element_max_size);
 }
 
 void* EchoControlMobileImpl::CreateHandle() const {
diff --git a/webrtc/modules/audio_processing/echo_control_mobile_impl.h b/webrtc/modules/audio_processing/echo_control_mobile_impl.h
index 87c5376..8bfa1d7 100644
--- a/webrtc/modules/audio_processing/echo_control_mobile_impl.h
+++ b/webrtc/modules/audio_processing/echo_control_mobile_impl.h
@@ -43,12 +43,6 @@
   void ReadQueuedRenderData();
 
  private:
-  static const size_t kAllowedValuesOfSamplesPerFrame1 = 80;
-  static const size_t kAllowedValuesOfSamplesPerFrame2 = 160;
-  // TODO(peah): Decrease this once we properly handle hugely unbalanced
-  // reverse and forward call numbers.
-  static const size_t kMaxNumFramesToBuffer = 100;
-
   // EchoControlMobile implementation.
   int Enable(bool enable) override;
   int set_routing_mode(RoutingMode mode) override;
diff --git a/webrtc/modules/audio_processing/gain_control_impl.cc b/webrtc/modules/audio_processing/gain_control_impl.cc
index 4d84b24..0eacd28 100644
--- a/webrtc/modules/audio_processing/gain_control_impl.cc
+++ b/webrtc/modules/audio_processing/gain_control_impl.cc
@@ -33,10 +33,15 @@
   assert(false);
   return -1;
 }
-}  // namespace
 
-const size_t GainControlImpl::kAllowedValuesOfSamplesPerFrame1;
-const size_t GainControlImpl::kAllowedValuesOfSamplesPerFrame2;
+// Maximum length that a frame of samples can have.
+static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160;
+// Maximum number of frames to buffer in the render queue.
+// TODO(peah): Decrease this once we properly handle hugely unbalanced
+// reverse and forward call numbers.
+static const size_t kMaxNumFramesToBuffer = 100;
+
+}  // namespace
 
 GainControlImpl::GainControlImpl(const AudioProcessing* apm,
                                  CriticalSectionWrapper* crit)
@@ -52,9 +57,7 @@
       analog_capture_level_(0),
       was_analog_level_set_(false),
       stream_is_saturated_(false),
-      render_queue_element_max_size_(0) {
-  AllocateRenderQueue();
-}
+      render_queue_element_max_size_(0) {}
 
 GainControlImpl::~GainControlImpl() {}
 
@@ -217,12 +220,6 @@
 
 // TODO(ajm): ensure this is called under kAdaptiveAnalog.
 int GainControlImpl::set_stream_analog_level(int level) {
-  // TODO(peah): Verify that this is really needed to do the reading
-  // here as well as in ProcessStream. It works since these functions
-  // are called from the same thread, but it is not nice to do it in two
-  // places if not needed.
-  ReadQueuedRenderData();
-
   CriticalSectionScoped crit_scoped(crit_);
   was_analog_level_set_ = true;
   if (level < minimum_capture_level_ || level > maximum_capture_level_) {
@@ -349,25 +346,24 @@
 }
 
 void GainControlImpl::AllocateRenderQueue() {
-  const size_t max_frame_size = std::max<size_t>(
-      kAllowedValuesOfSamplesPerFrame1, kAllowedValuesOfSamplesPerFrame2);
+  const size_t new_render_queue_element_max_size =
+      std::max<size_t>(static_cast<size_t>(1),
+                       kMaxAllowedValuesOfSamplesPerFrame * num_handles());
 
-  const size_t new_render_queue_element_max_size = std::max<size_t>(
-      static_cast<size_t>(1), (max_frame_size * num_handles()));
-
-  if (new_render_queue_element_max_size > render_queue_element_max_size_) {
+  if (render_queue_element_max_size_ < new_render_queue_element_max_size) {
+    render_queue_element_max_size_ = new_render_queue_element_max_size;
     std::vector<int16_t> template_queue_element(render_queue_element_max_size_);
 
     render_signal_queue_.reset(
         new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
             kMaxNumFramesToBuffer, template_queue_element,
             RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_)));
+
+    render_queue_buffer_.resize(render_queue_element_max_size_);
+    capture_queue_buffer_.resize(render_queue_element_max_size_);
   } else {
     render_signal_queue_->Clear();
   }
-
-  render_queue_buffer_.resize(new_render_queue_element_max_size);
-  capture_queue_buffer_.resize(new_render_queue_element_max_size);
 }
 
 void* GainControlImpl::CreateHandle() const {
diff --git a/webrtc/modules/audio_processing/gain_control_impl.h b/webrtc/modules/audio_processing/gain_control_impl.h
index b766ca3..25b16c0 100644
--- a/webrtc/modules/audio_processing/gain_control_impl.h
+++ b/webrtc/modules/audio_processing/gain_control_impl.h
@@ -47,12 +47,6 @@
   void ReadQueuedRenderData();
 
  private:
-  static const size_t kAllowedValuesOfSamplesPerFrame1 = 80;
-  static const size_t kAllowedValuesOfSamplesPerFrame2 = 160;
-  // TODO(peah): Decrease this once we properly handle hugely unbalanced
-  // reverse and forward call numbers.
-  static const size_t kMaxNumFramesToBuffer = 100;
-
   // GainControl implementation.
   int Enable(bool enable) override;
   int set_stream_analog_level(int level) override;