LCOV - code coverage report
Current view: top level - media/webrtc/trunk/webrtc/modules/audio_processing - audio_processing_impl.cc (source / functions) Hit Total Coverage
Test: output.info Lines: 0 851 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 89 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
       3             :  *
       4             :  *  Use of this source code is governed by a BSD-style license
       5             :  *  that can be found in the LICENSE file in the root of the source
       6             :  *  tree. An additional intellectual property rights grant can be found
       7             :  *  in the file PATENTS.  All contributing project authors may
       8             :  *  be found in the AUTHORS file in the root of the source tree.
       9             :  */
      10             : 
      11             : #include "webrtc/modules/audio_processing/audio_processing_impl.h"
      12             : 
      13             : #include <algorithm>
      14             : 
      15             : #include "webrtc/base/checks.h"
      16             : #include "webrtc/base/platform_file.h"
      17             : #include "webrtc/base/trace_event.h"
      18             : #include "webrtc/common_audio/audio_converter.h"
      19             : #include "webrtc/common_audio/channel_buffer.h"
      20             : #include "webrtc/common_audio/include/audio_util.h"
      21             : #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
      22             : #include "webrtc/modules/audio_processing/aec/aec_core.h"
      23             : #include "webrtc/modules/audio_processing/aec3/echo_canceller3.h"
      24             : #include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
      25             : #include "webrtc/modules/audio_processing/audio_buffer.h"
      26             : #include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h"
      27             : #include "webrtc/modules/audio_processing/common.h"
      28             : #include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
      29             : #include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
      30             : #include "webrtc/modules/audio_processing/gain_control_for_experimental_agc.h"
      31             : #include "webrtc/modules/audio_processing/gain_control_impl.h"
      32             : #if WEBRTC_INTELLIGIBILITY_ENHANCER
      33             : #include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h"
      34             : #endif
      35             : #include "webrtc/modules/audio_processing/level_controller/level_controller.h"
      36             : #include "webrtc/modules/audio_processing/level_estimator_impl.h"
      37             : #include "webrtc/modules/audio_processing/low_cut_filter.h"
      38             : #include "webrtc/modules/audio_processing/noise_suppression_impl.h"
      39             : #include "webrtc/modules/audio_processing/residual_echo_detector.h"
      40             : #include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
      41             : #include "webrtc/modules/audio_processing/voice_detection_impl.h"
      42             : #include "webrtc/modules/include/module_common_types.h"
      43             : #include "webrtc/system_wrappers/include/file_wrapper.h"
      44             : #include "webrtc/system_wrappers/include/logging.h"
      45             : #include "webrtc/system_wrappers/include/metrics.h"
      46             : 
      47             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
      48             : // Files generated at build-time by the protobuf compiler.
      49             : #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
      50             : #include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
      51             : #else
      52             : #include "webrtc/modules/audio_processing/debug.pb.h"
      53             : #endif
      54             : #endif  // WEBRTC_AUDIOPROC_DEBUG_DUMP
      55             : 
      56             : // Check to verify that the define for the intelligibility enhancer is properly
      57             : // set.
      58             : #if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
      59             :     (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 &&     \
      60             :      WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
      61             : #error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
      62             : #endif
      63             : 
      64             : #define RETURN_ON_ERR(expr) \
      65             :   do {                      \
      66             :     int err = (expr);       \
      67             :     if (err != kNoError) {  \
      68             :       return err;           \
      69             :     }                       \
      70             :   } while (0)
      71             : 
      72             : namespace webrtc {
      73             : 
      74             : constexpr int AudioProcessing::kNativeSampleRatesHz[];
      75             : 
      76             : namespace {
      77             : 
      78           0 : static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
      79           0 :   switch (layout) {
      80             :     case AudioProcessing::kMono:
      81             :     case AudioProcessing::kStereo:
      82           0 :       return false;
      83             :     case AudioProcessing::kMonoAndKeyboard:
      84             :     case AudioProcessing::kStereoAndKeyboard:
      85           0 :       return true;
      86             :   }
      87             : 
      88           0 :   RTC_NOTREACHED();
      89           0 :   return false;
      90             : }
      91             : 
      92           0 : bool SampleRateSupportsMultiBand(int sample_rate_hz) {
      93           0 :   return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
      94           0 :          sample_rate_hz == AudioProcessing::kSampleRate48kHz;
      95             : }
      96             : 
      97           0 : int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) {
      98             : #ifdef WEBRTC_ARCH_ARM_FAMILY
      99             :   constexpr int kMaxSplittingNativeProcessRate =
     100             :       AudioProcessing::kSampleRate32kHz;
     101             : #else
     102             :   constexpr int kMaxSplittingNativeProcessRate =
     103           0 :       AudioProcessing::kSampleRate48kHz;
     104             : #endif
     105             :   static_assert(
     106             :       kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz,
     107             :       "");
     108             :   const int uppermost_native_rate = band_splitting_required
     109             :                                         ? kMaxSplittingNativeProcessRate
     110           0 :                                         : AudioProcessing::kSampleRate48kHz;
     111             : 
     112           0 :   for (auto rate : AudioProcessing::kNativeSampleRatesHz) {
     113           0 :     if (rate >= uppermost_native_rate) {
     114           0 :       return uppermost_native_rate;
     115             :     }
     116           0 :     if (rate >= minimum_rate) {
     117           0 :       return rate;
     118             :     }
     119             :   }
     120           0 :   RTC_NOTREACHED();
     121           0 :   return uppermost_native_rate;
     122             : }
     123             : 
     124             : // Maximum length that a frame of samples can have.
     125             : static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160;
     126             : // Maximum number of frames to buffer in the render queue.
     127             : // TODO(peah): Decrease this once we properly handle hugely unbalanced
     128             : // reverse and forward call numbers.
     129             : static const size_t kMaxNumFramesToBuffer = 100;
     130             : 
     131             : class HighPassFilterImpl : public HighPassFilter {
     132             :  public:
     133           0 :   explicit HighPassFilterImpl(AudioProcessingImpl* apm) : apm_(apm) {}
     134           0 :   ~HighPassFilterImpl() override = default;
     135             : 
     136             :   // HighPassFilter implementation.
     137           0 :   int Enable(bool enable) override {
     138           0 :     apm_->MutateConfig([enable](AudioProcessing::Config* config) {
     139           0 :       config->high_pass_filter.enabled = enable;
     140           0 :     });
     141             : 
     142           0 :     return AudioProcessing::kNoError;
     143             :   }
     144             : 
     145           0 :   bool is_enabled() const override {
     146           0 :     return apm_->GetConfig().high_pass_filter.enabled;
     147             :   }
     148             : 
     149             :  private:
     150             :   AudioProcessingImpl* apm_;
     151             :   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(HighPassFilterImpl);
     152             : };
     153             : 
     154             : }  // namespace
     155             : 
     156             : // Throughout webrtc, it's assumed that success is represented by zero.
     157             : static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
     158             : 
     159           0 : AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates() {}
     160             : 
     161           0 : bool AudioProcessingImpl::ApmSubmoduleStates::Update(
     162             :     bool low_cut_filter_enabled,
     163             :     bool echo_canceller_enabled,
     164             :     bool mobile_echo_controller_enabled,
     165             :     bool residual_echo_detector_enabled,
     166             :     bool noise_suppressor_enabled,
     167             :     bool intelligibility_enhancer_enabled,
     168             :     bool beamformer_enabled,
     169             :     bool adaptive_gain_controller_enabled,
     170             :     bool level_controller_enabled,
     171             :     bool echo_canceller3_enabled,
     172             :     bool voice_activity_detector_enabled,
     173             :     bool level_estimator_enabled,
     174             :     bool transient_suppressor_enabled) {
     175           0 :   bool changed = false;
     176           0 :   changed |= (low_cut_filter_enabled != low_cut_filter_enabled_);
     177           0 :   changed |= (echo_canceller_enabled != echo_canceller_enabled_);
     178           0 :   changed |=
     179           0 :       (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
     180           0 :   changed |=
     181           0 :       (residual_echo_detector_enabled != residual_echo_detector_enabled_);
     182           0 :   changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
     183           0 :   changed |=
     184           0 :       (intelligibility_enhancer_enabled != intelligibility_enhancer_enabled_);
     185           0 :   changed |= (beamformer_enabled != beamformer_enabled_);
     186           0 :   changed |=
     187           0 :       (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
     188           0 :   changed |= (level_controller_enabled != level_controller_enabled_);
     189           0 :   changed |= (echo_canceller3_enabled != echo_canceller3_enabled_);
     190           0 :   changed |= (level_estimator_enabled != level_estimator_enabled_);
     191           0 :   changed |=
     192           0 :       (voice_activity_detector_enabled != voice_activity_detector_enabled_);
     193           0 :   changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
     194           0 :   if (changed) {
     195           0 :     low_cut_filter_enabled_ = low_cut_filter_enabled;
     196           0 :     echo_canceller_enabled_ = echo_canceller_enabled;
     197           0 :     mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
     198           0 :     residual_echo_detector_enabled_ = residual_echo_detector_enabled;
     199           0 :     noise_suppressor_enabled_ = noise_suppressor_enabled;
     200           0 :     intelligibility_enhancer_enabled_ = intelligibility_enhancer_enabled;
     201           0 :     beamformer_enabled_ = beamformer_enabled;
     202           0 :     adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
     203           0 :     level_controller_enabled_ = level_controller_enabled;
     204           0 :     echo_canceller3_enabled_ = echo_canceller3_enabled;
     205           0 :     level_estimator_enabled_ = level_estimator_enabled;
     206           0 :     voice_activity_detector_enabled_ = voice_activity_detector_enabled;
     207           0 :     transient_suppressor_enabled_ = transient_suppressor_enabled;
     208             :   }
     209             : 
     210           0 :   changed |= first_update_;
     211           0 :   first_update_ = false;
     212           0 :   return changed;
     213             : }
     214             : 
     215           0 : bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
     216             :     const {
     217             : #if WEBRTC_INTELLIGIBILITY_ENHANCER
     218             :   return CaptureMultiBandProcessingActive() ||
     219             :          intelligibility_enhancer_enabled_ ||
     220             :          voice_activity_detector_enabled_ || residual_echo_detector_enabled_;
     221             : #else
     222           0 :   return CaptureMultiBandProcessingActive() ||
     223           0 :          voice_activity_detector_enabled_ || residual_echo_detector_enabled_;
     224             : #endif
     225             : }
     226             : 
     227           0 : bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
     228             :     const {
     229           0 :   return low_cut_filter_enabled_ || echo_canceller_enabled_ ||
     230           0 :          mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
     231           0 :          beamformer_enabled_ || adaptive_gain_controller_enabled_ ||
     232           0 :          echo_canceller3_enabled_;
     233             : }
     234             : 
     235           0 : bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
     236             :     const {
     237           0 :   return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
     238           0 :          mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
     239           0 :          residual_echo_detector_enabled_ || echo_canceller3_enabled_;
     240             : }
     241             : 
     242           0 : bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
     243             :     const {
     244             : #if WEBRTC_INTELLIGIBILITY_ENHANCER
     245             :   return intelligibility_enhancer_enabled_;
     246             : #else
     247           0 :   return false;
     248             : #endif
     249             : }
     250             : 
     251           0 : struct AudioProcessingImpl::ApmPublicSubmodules {
     252           0 :   ApmPublicSubmodules() {}
     253             :   // Accessed externally of APM without any lock acquired.
     254             :   std::unique_ptr<EchoCancellationImpl> echo_cancellation;
     255             :   std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
     256             :   std::unique_ptr<GainControlImpl> gain_control;
     257             :   std::unique_ptr<LevelEstimatorImpl> level_estimator;
     258             :   std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
     259             :   std::unique_ptr<VoiceDetectionImpl> voice_detection;
     260             :   std::unique_ptr<GainControlForExperimentalAgc>
     261             :       gain_control_for_experimental_agc;
     262             : 
     263             :   // Accessed internally from both render and capture.
     264             :   std::unique_ptr<TransientSuppressor> transient_suppressor;
     265             : #if WEBRTC_INTELLIGIBILITY_ENHANCER
     266             :   std::unique_ptr<IntelligibilityEnhancer> intelligibility_enhancer;
     267             : #endif
     268             : };
     269             : 
     270           0 : struct AudioProcessingImpl::ApmPrivateSubmodules {
     271           0 :   explicit ApmPrivateSubmodules(NonlinearBeamformer* beamformer)
     272           0 :       : beamformer(beamformer) {}
     273             :   // Accessed internally from capture or during initialization
     274             :   std::unique_ptr<NonlinearBeamformer> beamformer;
     275             :   std::unique_ptr<AgcManagerDirect> agc_manager;
     276             :   std::unique_ptr<LowCutFilter> low_cut_filter;
     277             :   std::unique_ptr<LevelController> level_controller;
     278             :   std::unique_ptr<ResidualEchoDetector> residual_echo_detector;
     279             :   std::unique_ptr<EchoCanceller3> echo_canceller3;
     280             : };
     281             : 
     282           0 : AudioProcessing* AudioProcessing::Create() {
     283           0 :   webrtc::Config config;
     284           0 :   return Create(config, nullptr);
     285             : }
     286             : 
     287           0 : AudioProcessing* AudioProcessing::Create(const webrtc::Config& config) {
     288           0 :   return Create(config, nullptr);
     289             : }
     290             : 
     291           0 : AudioProcessing* AudioProcessing::Create(const webrtc::Config& config,
     292             :                                          NonlinearBeamformer* beamformer) {
     293           0 :   AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer);
     294           0 :   if (apm->Initialize() != kNoError) {
     295           0 :     delete apm;
     296           0 :     apm = nullptr;
     297             :   }
     298             : 
     299           0 :   return apm;
     300             : }
     301             : 
     302           0 : AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
     303           0 :     : AudioProcessingImpl(config, nullptr) {}
     304             : 
     305           0 : AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config,
     306           0 :                                          NonlinearBeamformer* beamformer)
     307           0 :     : high_pass_filter_impl_(new HighPassFilterImpl(this)),
     308           0 :       public_submodules_(new ApmPublicSubmodules()),
     309           0 :       private_submodules_(new ApmPrivateSubmodules(beamformer)),
     310           0 :       constants_(config.Get<ExperimentalAgc>().startup_min_volume,
     311           0 :                  config.Get<ExperimentalAgc>().clipped_level_min,
     312             : #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
     313             :                  false),
     314             : #else
     315           0 :                  config.Get<ExperimentalAgc>().enabled),
     316             : #endif
     317             : #if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
     318             :       capture_(false,
     319             : #else
     320           0 :       capture_(config.Get<ExperimentalNs>().enabled,
     321             : #endif
     322           0 :                config.Get<Beamforming>().array_geometry,
     323           0 :                config.Get<Beamforming>().target_direction),
     324           0 :       capture_nonlocked_(config.Get<Beamforming>().enabled,
     325           0 :                          config.Get<Intelligibility>().enabled) {
     326             :   {
     327           0 :     rtc::CritScope cs_render(&crit_render_);
     328           0 :     rtc::CritScope cs_capture(&crit_capture_);
     329             : 
     330           0 :     public_submodules_->echo_cancellation.reset(
     331           0 :         new EchoCancellationImpl(&crit_render_, &crit_capture_));
     332           0 :     public_submodules_->echo_control_mobile.reset(
     333           0 :         new EchoControlMobileImpl(&crit_render_, &crit_capture_));
     334           0 :     public_submodules_->gain_control.reset(
     335           0 :         new GainControlImpl(&crit_capture_, &crit_capture_));
     336           0 :     public_submodules_->level_estimator.reset(
     337           0 :         new LevelEstimatorImpl(&crit_capture_));
     338           0 :     public_submodules_->noise_suppression.reset(
     339           0 :         new NoiseSuppressionImpl(&crit_capture_));
     340           0 :     public_submodules_->voice_detection.reset(
     341           0 :         new VoiceDetectionImpl(&crit_capture_));
     342           0 :     public_submodules_->gain_control_for_experimental_agc.reset(
     343             :         new GainControlForExperimentalAgc(
     344           0 :             public_submodules_->gain_control.get(), &crit_capture_));
     345           0 :     private_submodules_->residual_echo_detector.reset(
     346           0 :         new ResidualEchoDetector());
     347             : 
     348             :     // TODO(peah): Move this creation to happen only when the level controller
     349             :     // is enabled.
     350           0 :     private_submodules_->level_controller.reset(new LevelController());
     351             :   }
     352             : 
     353           0 :   SetExtraOptions(config);
     354           0 : }
     355             : 
     356           0 : AudioProcessingImpl::~AudioProcessingImpl() {
     357             :   // Depends on gain_control_ and
     358             :   // public_submodules_->gain_control_for_experimental_agc.
     359           0 :   private_submodules_->agc_manager.reset();
     360             :   // Depends on gain_control_.
     361           0 :   public_submodules_->gain_control_for_experimental_agc.reset();
     362             : 
     363             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
     364             :   debug_dump_.debug_file->CloseFile();
     365             : #endif
     366           0 : }
     367             : 
     368           0 : int AudioProcessingImpl::Initialize() {
     369             :   // Run in a single-threaded manner during initialization.
     370           0 :   rtc::CritScope cs_render(&crit_render_);
     371           0 :   rtc::CritScope cs_capture(&crit_capture_);
     372           0 :   return InitializeLocked();
     373             : }
     374             : 
     375           0 : int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
     376             :                                     int capture_output_sample_rate_hz,
     377             :                                     int render_input_sample_rate_hz,
     378             :                                     ChannelLayout capture_input_layout,
     379             :                                     ChannelLayout capture_output_layout,
     380             :                                     ChannelLayout render_input_layout) {
     381             :   const ProcessingConfig processing_config = {
     382             :       {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
     383           0 :         LayoutHasKeyboard(capture_input_layout)},
     384             :        {capture_output_sample_rate_hz,
     385             :         ChannelsFromLayout(capture_output_layout),
     386           0 :         LayoutHasKeyboard(capture_output_layout)},
     387             :        {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
     388           0 :         LayoutHasKeyboard(render_input_layout)},
     389             :        {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
     390           0 :         LayoutHasKeyboard(render_input_layout)}}};
     391             : 
     392           0 :   return Initialize(processing_config);
     393             : }
     394             : 
     395           0 : int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
     396             :   // Run in a single-threaded manner during initialization.
     397           0 :   rtc::CritScope cs_render(&crit_render_);
     398           0 :   rtc::CritScope cs_capture(&crit_capture_);
     399           0 :   return InitializeLocked(processing_config);
     400             : }
     401             : 
     402           0 : int AudioProcessingImpl::MaybeInitializeRender(
     403             :     const ProcessingConfig& processing_config) {
     404           0 :   return MaybeInitialize(processing_config, false);
     405             : }
     406             : 
     407           0 : int AudioProcessingImpl::MaybeInitializeCapture(
     408             :     const ProcessingConfig& processing_config,
     409             :     bool force_initialization) {
     410           0 :   return MaybeInitialize(processing_config, force_initialization);
     411             : }
     412             : 
     413             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
     414             : 
     415             : AudioProcessingImpl::ApmDebugDumpThreadState::ApmDebugDumpThreadState()
     416             :     : event_msg(new audioproc::Event()) {}
     417             : 
     418             : AudioProcessingImpl::ApmDebugDumpThreadState::~ApmDebugDumpThreadState() {}
     419             : 
     420             : AudioProcessingImpl::ApmDebugDumpState::ApmDebugDumpState()
     421             :     : debug_file(FileWrapper::Create()) {}
     422             : 
     423             : AudioProcessingImpl::ApmDebugDumpState::~ApmDebugDumpState() {}
     424             : 
     425             : #endif  // WEBRTC_AUDIOPROC_DEBUG_DUMP
     426             : 
     427             : // Calls InitializeLocked() if any of the audio parameters have changed from
     428             : // their current values (needs to be called while holding the crit_render_lock).
     429           0 : int AudioProcessingImpl::MaybeInitialize(
     430             :     const ProcessingConfig& processing_config,
     431             :     bool force_initialization) {
     432             :   // Called from both threads. Thread check is therefore not possible.
     433           0 :   if (processing_config == formats_.api_format && !force_initialization) {
     434           0 :     return kNoError;
     435             :   }
     436             : 
     437           0 :   rtc::CritScope cs_capture(&crit_capture_);
     438           0 :   return InitializeLocked(processing_config);
     439             : }
     440             : 
     441           0 : int AudioProcessingImpl::InitializeLocked() {
     442             :   int capture_audiobuffer_num_channels;
     443           0 :   if (private_submodules_->echo_canceller3) {
     444             :     // TODO(peah): Ensure that the echo canceller can operate on more than one
     445             :     // microphone channel.
     446           0 :     RTC_DCHECK(!capture_nonlocked_.beamformer_enabled);
     447           0 :     capture_audiobuffer_num_channels = 1;
     448             :   } else {
     449           0 :     capture_audiobuffer_num_channels =
     450           0 :         capture_nonlocked_.beamformer_enabled
     451           0 :             ? formats_.api_format.input_stream().num_channels()
     452           0 :             : formats_.api_format.output_stream().num_channels();
     453             :   }
     454             :   const int render_audiobuffer_num_output_frames =
     455           0 :       formats_.api_format.reverse_output_stream().num_frames() == 0
     456           0 :           ? formats_.render_processing_format.num_frames()
     457           0 :           : formats_.api_format.reverse_output_stream().num_frames();
     458           0 :   if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
     459           0 :     render_.render_audio.reset(new AudioBuffer(
     460           0 :         formats_.api_format.reverse_input_stream().num_frames(),
     461           0 :         formats_.api_format.reverse_input_stream().num_channels(),
     462           0 :         formats_.render_processing_format.num_frames(),
     463           0 :         formats_.render_processing_format.num_channels(),
     464           0 :         render_audiobuffer_num_output_frames));
     465           0 :     if (formats_.api_format.reverse_input_stream() !=
     466           0 :         formats_.api_format.reverse_output_stream()) {
     467           0 :       render_.render_converter = AudioConverter::Create(
     468           0 :           formats_.api_format.reverse_input_stream().num_channels(),
     469           0 :           formats_.api_format.reverse_input_stream().num_frames(),
     470           0 :           formats_.api_format.reverse_output_stream().num_channels(),
     471           0 :           formats_.api_format.reverse_output_stream().num_frames());
     472             :     } else {
     473           0 :       render_.render_converter.reset(nullptr);
     474             :     }
     475             :   } else {
     476           0 :     render_.render_audio.reset(nullptr);
     477           0 :     render_.render_converter.reset(nullptr);
     478             :   }
     479           0 :   capture_.capture_audio.reset(
     480           0 :       new AudioBuffer(formats_.api_format.input_stream().num_frames(),
     481           0 :                       formats_.api_format.input_stream().num_channels(),
     482           0 :                       capture_nonlocked_.capture_processing_format.num_frames(),
     483             :                       capture_audiobuffer_num_channels,
     484           0 :                       formats_.api_format.output_stream().num_frames()));
     485             : 
     486           0 :   public_submodules_->echo_cancellation->Initialize(
     487           0 :       proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
     488           0 :       num_proc_channels());
     489           0 :   AllocateRenderQueue();
     490             : 
     491           0 :   int success = public_submodules_->echo_cancellation->enable_metrics(true);
     492           0 :   RTC_DCHECK_EQ(0, success);
     493           0 :   success = public_submodules_->echo_cancellation->enable_delay_logging(true);
     494           0 :   RTC_DCHECK_EQ(0, success);
     495           0 :   public_submodules_->echo_control_mobile->Initialize(
     496           0 :       proc_split_sample_rate_hz(), num_reverse_channels(),
     497           0 :       num_output_channels());
     498             : 
     499           0 :   public_submodules_->gain_control->Initialize(num_proc_channels(),
     500           0 :                                                proc_sample_rate_hz());
     501           0 :   if (constants_.use_experimental_agc) {
     502           0 :     if (!private_submodules_->agc_manager.get()) {
     503           0 :       private_submodules_->agc_manager.reset(new AgcManagerDirect(
     504           0 :           public_submodules_->gain_control.get(),
     505           0 :           public_submodules_->gain_control_for_experimental_agc.get(),
     506           0 :           constants_.agc_startup_min_volume, constants_.agc_clipped_level_min));
     507             :     }
     508           0 :     private_submodules_->agc_manager->Initialize();
     509           0 :     private_submodules_->agc_manager->SetCaptureMuted(
     510           0 :         capture_.output_will_be_muted);
     511           0 :     public_submodules_->gain_control_for_experimental_agc->Initialize();
     512             :   }
     513           0 :   InitializeTransient();
     514           0 :   InitializeBeamformer();
     515             : #if WEBRTC_INTELLIGIBILITY_ENHANCER
     516             :   InitializeIntelligibility();
     517             : #endif
     518           0 :   InitializeLowCutFilter();
     519           0 :   public_submodules_->noise_suppression->Initialize(num_proc_channels(),
     520           0 :                                                     proc_sample_rate_hz());
     521           0 :   public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
     522           0 :   public_submodules_->level_estimator->Initialize();
     523           0 :   InitializeLevelController();
     524           0 :   InitializeResidualEchoDetector();
     525           0 :   InitializeEchoCanceller3();
     526             : 
     527             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
     528             :   if (debug_dump_.debug_file->is_open()) {
     529             :     int err = WriteInitMessage();
     530             :     if (err != kNoError) {
     531             :       return err;
     532             :     }
     533             :   }
     534             : #endif
     535             : 
     536           0 :   return kNoError;
     537             : }
     538             : 
     539           0 : int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
     540           0 :   for (const auto& stream : config.streams) {
     541           0 :     if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
     542           0 :       return kBadSampleRateError;
     543             :     }
     544             :   }
     545             : 
     546           0 :   const size_t num_in_channels = config.input_stream().num_channels();
     547           0 :   const size_t num_out_channels = config.output_stream().num_channels();
     548             : 
     549             :   // Need at least one input channel.
     550             :   // Need either one output channel or as many outputs as there are inputs.
     551           0 :   if (num_in_channels == 0 ||
     552           0 :       !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
     553           0 :     return kBadNumberChannelsError;
     554             :   }
     555             : 
     556           0 :   if (capture_nonlocked_.beamformer_enabled &&
     557           0 :       num_in_channels != capture_.array_geometry.size()) {
     558           0 :     return kBadNumberChannelsError;
     559             :   }
     560             : 
     561           0 :   formats_.api_format = config;
     562             : 
     563           0 :   int capture_processing_rate = FindNativeProcessRateToUse(
     564           0 :       std::min(formats_.api_format.input_stream().sample_rate_hz(),
     565           0 :                formats_.api_format.output_stream().sample_rate_hz()),
     566           0 :       submodule_states_.CaptureMultiBandSubModulesActive() ||
     567           0 :           submodule_states_.RenderMultiBandSubModulesActive());
     568             : 
     569           0 :   capture_nonlocked_.capture_processing_format =
     570           0 :       StreamConfig(capture_processing_rate);
     571             : 
     572           0 :   int render_processing_rate = FindNativeProcessRateToUse(
     573           0 :       std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
     574           0 :                formats_.api_format.reverse_output_stream().sample_rate_hz()),
     575           0 :       submodule_states_.CaptureMultiBandSubModulesActive() ||
     576           0 :           submodule_states_.RenderMultiBandSubModulesActive());
     577             :   // TODO(aluebs): Remove this restriction once we figure out why the 3-band
     578             :   // splitting filter degrades the AEC performance.
     579             :   // TODO(peah): Verify that the band splitting is needed for the AEC3.
     580           0 :   if (render_processing_rate > kSampleRate32kHz &&
     581           0 :       !capture_nonlocked_.echo_canceller3_enabled) {
     582           0 :     render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
     583           0 :                                  ? kSampleRate32kHz
     584             :                                  : kSampleRate16kHz;
     585             :   }
     586             :   // If the forward sample rate is 8 kHz, the render stream is also processed
     587             :   // at this rate.
     588           0 :   if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
     589             :       kSampleRate8kHz) {
     590           0 :     render_processing_rate = kSampleRate8kHz;
     591             :   } else {
     592           0 :     render_processing_rate =
     593           0 :         std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
     594             :   }
     595             : 
     596             :   // Always downmix the render stream to mono for analysis. This has been
     597             :   // demonstrated to work well for AEC in most practical scenarios.
     598           0 :   formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
     599             : 
     600           0 :   if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
     601           0 :           kSampleRate32kHz ||
     602           0 :       capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
     603             :           kSampleRate48kHz) {
     604           0 :     capture_nonlocked_.split_rate = kSampleRate16kHz;
     605             :   } else {
     606           0 :     capture_nonlocked_.split_rate =
     607           0 :         capture_nonlocked_.capture_processing_format.sample_rate_hz();
     608             :   }
     609             : 
     610           0 :   return InitializeLocked();
     611             : }
     612             : 
     613           0 : void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
     614           0 :   config_ = config;
     615             : 
     616           0 :   bool config_ok = LevelController::Validate(config_.level_controller);
     617           0 :   if (!config_ok) {
     618           0 :     LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
     619             :                   << "level_controller: "
     620           0 :                   << LevelController::ToString(config_.level_controller)
     621           0 :                   << std::endl
     622           0 :                   << "Reverting to default parameter set";
     623           0 :     config_.level_controller = AudioProcessing::Config::LevelController();
     624             :   }
     625             : 
     626             :   // Run in a single-threaded manner when applying the settings.
     627           0 :   rtc::CritScope cs_render(&crit_render_);
     628           0 :   rtc::CritScope cs_capture(&crit_capture_);
     629             : 
     630             :   // TODO(peah): Replace the use of capture_nonlocked_.level_controller_enabled
     631             :   // with the value in config_ everywhere in the code.
     632           0 :   if (capture_nonlocked_.level_controller_enabled !=
     633           0 :       config_.level_controller.enabled) {
     634           0 :     capture_nonlocked_.level_controller_enabled =
     635           0 :         config_.level_controller.enabled;
     636             :     // TODO(peah): Remove the conditional initialization to always initialize
     637             :     // the level controller regardless of whether it is enabled or not.
     638           0 :     InitializeLevelController();
     639             :   }
     640           0 :   LOG(LS_INFO) << "Level controller activated: "
     641           0 :                << capture_nonlocked_.level_controller_enabled;
     642             : 
     643           0 :   private_submodules_->level_controller->ApplyConfig(config_.level_controller);
     644             : 
     645           0 :   InitializeLowCutFilter();
     646             : 
     647           0 :   LOG(LS_INFO) << "Highpass filter activated: "
     648           0 :                << config_.high_pass_filter.enabled;
     649             : 
     650           0 :   config_ok = EchoCanceller3::Validate(config_.echo_canceller3);
     651           0 :   if (!config_ok) {
     652           0 :     LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
     653             :                   << "echo canceller 3: "
     654           0 :                   << EchoCanceller3::ToString(config_.echo_canceller3)
     655           0 :                   << std::endl
     656           0 :                   << "Reverting to default parameter set";
     657           0 :     config_.echo_canceller3 = AudioProcessing::Config::EchoCanceller3();
     658             :   }
     659             : 
     660           0 :   if (config.echo_canceller3.enabled !=
     661           0 :       capture_nonlocked_.echo_canceller3_enabled) {
     662           0 :     capture_nonlocked_.echo_canceller3_enabled =
     663           0 :         config_.echo_canceller3.enabled;
     664           0 :     InitializeEchoCanceller3();
     665           0 :     LOG(LS_INFO) << "Echo canceller 3 activated: "
     666           0 :                  << capture_nonlocked_.echo_canceller3_enabled;
     667             :   }
     668           0 : }
     669             : 
     670           0 : void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
     671             :   // Run in a single-threaded manner when setting the extra options.
     672           0 :   rtc::CritScope cs_render(&crit_render_);
     673           0 :   rtc::CritScope cs_capture(&crit_capture_);
     674             : 
     675           0 :   public_submodules_->echo_cancellation->SetExtraOptions(config);
     676             : 
     677           0 :   if (capture_.transient_suppressor_enabled !=
     678           0 :       config.Get<ExperimentalNs>().enabled) {
     679           0 :     capture_.transient_suppressor_enabled =
     680           0 :         config.Get<ExperimentalNs>().enabled;
     681           0 :     InitializeTransient();
     682             :   }
     683             : 
     684             : #if WEBRTC_INTELLIGIBILITY_ENHANCER
     685             :   if(capture_nonlocked_.intelligibility_enabled !=
     686             :      config.Get<Intelligibility>().enabled) {
     687             :     capture_nonlocked_.intelligibility_enabled =
     688             :         config.Get<Intelligibility>().enabled;
     689             :     InitializeIntelligibility();
     690             :   }
     691             : #endif
     692             : 
     693             : #ifdef WEBRTC_ANDROID_PLATFORM_BUILD
     694             :   if (capture_nonlocked_.beamformer_enabled !=
     695             :           config.Get<Beamforming>().enabled) {
     696             :     capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled;
     697             :     if (config.Get<Beamforming>().array_geometry.size() > 1) {
     698             :       capture_.array_geometry = config.Get<Beamforming>().array_geometry;
     699             :     }
     700             :     capture_.target_direction = config.Get<Beamforming>().target_direction;
     701             :     InitializeBeamformer();
     702             :   }
     703             : #endif  // WEBRTC_ANDROID_PLATFORM_BUILD
     704           0 : }
     705             : 
     706           0 : int AudioProcessingImpl::proc_sample_rate_hz() const {
     707             :   // Used as callback from submodules, hence locking is not allowed.
     708           0 :   return capture_nonlocked_.capture_processing_format.sample_rate_hz();
     709             : }
     710             : 
     711           0 : int AudioProcessingImpl::proc_split_sample_rate_hz() const {
     712             :   // Used as callback from submodules, hence locking is not allowed.
     713           0 :   return capture_nonlocked_.split_rate;
     714             : }
     715             : 
     716           0 : size_t AudioProcessingImpl::num_reverse_channels() const {
     717             :   // Used as callback from submodules, hence locking is not allowed.
     718           0 :   return formats_.render_processing_format.num_channels();
     719             : }
     720             : 
     721           0 : size_t AudioProcessingImpl::num_input_channels() const {
     722             :   // Used as callback from submodules, hence locking is not allowed.
     723           0 :   return formats_.api_format.input_stream().num_channels();
     724             : }
     725             : 
     726           0 : size_t AudioProcessingImpl::num_proc_channels() const {
     727             :   // Used as callback from submodules, hence locking is not allowed.
     728           0 :   return capture_nonlocked_.beamformer_enabled ? 1 : num_output_channels();
     729             : }
     730             : 
     731           0 : size_t AudioProcessingImpl::num_output_channels() const {
     732             :   // Used as callback from submodules, hence locking is not allowed.
     733           0 :   return formats_.api_format.output_stream().num_channels();
     734             : }
     735             : 
     736           0 : void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
     737           0 :   rtc::CritScope cs(&crit_capture_);
     738           0 :   capture_.output_will_be_muted = muted;
     739           0 :   if (private_submodules_->agc_manager.get()) {
     740           0 :     private_submodules_->agc_manager->SetCaptureMuted(
     741           0 :         capture_.output_will_be_muted);
     742             :   }
     743           0 : }
     744             : 
     745             : 
     746           0 : int AudioProcessingImpl::ProcessStream(const float* const* src,
     747             :                                        size_t samples_per_channel,
     748             :                                        int input_sample_rate_hz,
     749             :                                        ChannelLayout input_layout,
     750             :                                        int output_sample_rate_hz,
     751             :                                        ChannelLayout output_layout,
     752             :                                        float* const* dest) {
     753           0 :   TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
     754           0 :   StreamConfig input_stream;
     755           0 :   StreamConfig output_stream;
     756             :   {
     757             :     // Access the formats_.api_format.input_stream beneath the capture lock.
     758             :     // The lock must be released as it is later required in the call
     759             :     // to ProcessStream(,,,);
     760           0 :     rtc::CritScope cs(&crit_capture_);
     761           0 :     input_stream = formats_.api_format.input_stream();
     762           0 :     output_stream = formats_.api_format.output_stream();
     763             :   }
     764             : 
     765           0 :   input_stream.set_sample_rate_hz(input_sample_rate_hz);
     766           0 :   input_stream.set_num_channels(ChannelsFromLayout(input_layout));
     767           0 :   input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
     768           0 :   output_stream.set_sample_rate_hz(output_sample_rate_hz);
     769           0 :   output_stream.set_num_channels(ChannelsFromLayout(output_layout));
     770           0 :   output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
     771             : 
     772           0 :   if (samples_per_channel != input_stream.num_frames()) {
     773           0 :     return kBadDataLengthError;
     774             :   }
     775           0 :   return ProcessStream(src, input_stream, output_stream, dest);
     776             : }
     777             : 
     778           0 : int AudioProcessingImpl::ProcessStream(const float* const* src,
     779             :                                        const StreamConfig& input_config,
     780             :                                        const StreamConfig& output_config,
     781             :                                        float* const* dest) {
     782           0 :   TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
     783           0 :   ProcessingConfig processing_config;
     784           0 :   bool reinitialization_required = false;
     785             :   {
     786             :     // Acquire the capture lock in order to safely call the function
     787             :     // that retrieves the render side data. This function accesses apm
     788             :     // getters that need the capture lock held when being called.
     789           0 :     rtc::CritScope cs_capture(&crit_capture_);
     790           0 :     EmptyQueuedRenderAudio();
     791             : 
     792           0 :     if (!src || !dest) {
     793           0 :       return kNullPointerError;
     794             :     }
     795             : 
     796           0 :     processing_config = formats_.api_format;
     797           0 :     reinitialization_required = UpdateActiveSubmoduleStates();
     798             :   }
     799             : 
     800           0 :   processing_config.input_stream() = input_config;
     801           0 :   processing_config.output_stream() = output_config;
     802             : 
     803             :   {
     804             :     // Do conditional reinitialization.
     805           0 :     rtc::CritScope cs_render(&crit_render_);
     806           0 :     RETURN_ON_ERR(
     807             :         MaybeInitializeCapture(processing_config, reinitialization_required));
     808             :   }
     809           0 :   rtc::CritScope cs_capture(&crit_capture_);
     810           0 :   RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
     811           0 :                 formats_.api_format.input_stream().num_frames());
     812             : 
     813             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
     814             :   if (debug_dump_.debug_file->is_open()) {
     815             :     RETURN_ON_ERR(WriteConfigMessage(false));
     816             : 
     817             :     debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM);
     818             :     audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
     819             :     const size_t channel_size =
     820             :         sizeof(float) * formats_.api_format.input_stream().num_frames();
     821             :     for (size_t i = 0; i < formats_.api_format.input_stream().num_channels();
     822             :          ++i)
     823             :       msg->add_input_channel(src[i], channel_size);
     824             :   }
     825             : #endif
     826             : 
     827           0 :   capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
     828           0 :   RETURN_ON_ERR(ProcessCaptureStreamLocked());
     829           0 :   capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
     830             : 
     831             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
     832             :   if (debug_dump_.debug_file->is_open()) {
     833             :     audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
     834             :     const size_t channel_size =
     835             :         sizeof(float) * formats_.api_format.output_stream().num_frames();
     836             :     for (size_t i = 0; i < formats_.api_format.output_stream().num_channels();
     837             :          ++i)
     838             :       msg->add_output_channel(dest[i], channel_size);
     839             :     RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
     840             :                                           &debug_dump_.num_bytes_left_for_log_,
     841             :                                           &crit_debug_, &debug_dump_.capture));
     842             :   }
     843             : #endif
     844             : 
     845           0 :   return kNoError;
     846             : }
     847             : 
     848           0 : void AudioProcessingImpl::QueueRenderAudio(AudioBuffer* audio) {
     849           0 :   EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
     850           0 :                                               num_reverse_channels(),
     851           0 :                                               &aec_render_queue_buffer_);
     852             : 
     853           0 :   RTC_DCHECK_GE(160, audio->num_frames_per_band());
     854             : 
     855             :   // Insert the samples into the queue.
     856           0 :   if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
     857             :     // The data queue is full and needs to be emptied.
     858           0 :     EmptyQueuedRenderAudio();
     859             : 
     860             :     // Retry the insert (should always work).
     861           0 :     bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
     862           0 :     RTC_DCHECK(result);
     863             :   }
     864             : 
     865           0 :   EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
     866           0 :                                                num_reverse_channels(),
     867           0 :                                                &aecm_render_queue_buffer_);
     868             : 
     869             :   // Insert the samples into the queue.
     870           0 :   if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
     871             :     // The data queue is full and needs to be emptied.
     872           0 :     EmptyQueuedRenderAudio();
     873             : 
     874             :     // Retry the insert (should always work).
     875           0 :     bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
     876           0 :     RTC_DCHECK(result);
     877             :   }
     878             : 
     879           0 :   if (!constants_.use_experimental_agc) {
     880           0 :     GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
     881             :     // Insert the samples into the queue.
     882           0 :     if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
     883             :       // The data queue is full and needs to be emptied.
     884           0 :       EmptyQueuedRenderAudio();
     885             : 
     886             :       // Retry the insert (should always work).
     887           0 :       bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
     888           0 :       RTC_DCHECK(result);
     889             :     }
     890             :   }
     891             : 
     892           0 :   ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
     893             : 
     894             :   // Insert the samples into the queue.
     895           0 :   if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
     896             :     // The data queue is full and needs to be emptied.
     897           0 :     EmptyQueuedRenderAudio();
     898             : 
     899             :     // Retry the insert (should always work).
     900           0 :     bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
     901           0 :     RTC_DCHECK(result);
     902             :   }
     903           0 : }
     904             : 
     905           0 : void AudioProcessingImpl::AllocateRenderQueue() {
     906             :   const size_t new_aec_render_queue_element_max_size =
     907             :       std::max(static_cast<size_t>(1),
     908           0 :                kMaxAllowedValuesOfSamplesPerFrame *
     909           0 :                    EchoCancellationImpl::NumCancellersRequired(
     910           0 :                        num_output_channels(), num_reverse_channels()));
     911             : 
     912             :   const size_t new_aecm_render_queue_element_max_size =
     913             :       std::max(static_cast<size_t>(1),
     914           0 :                kMaxAllowedValuesOfSamplesPerFrame *
     915           0 :                    EchoControlMobileImpl::NumCancellersRequired(
     916           0 :                        num_output_channels(), num_reverse_channels()));
     917             : 
     918             :   const size_t new_agc_render_queue_element_max_size =
     919           0 :       std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
     920             : 
     921             :   const size_t new_red_render_queue_element_max_size =
     922           0 :       std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
     923             : 
     924             :   // Reallocate the queues if the queue item sizes are too small to fit the
     925             :   // data to put in the queues.
     926           0 :   if (aec_render_queue_element_max_size_ <
     927             :       new_aec_render_queue_element_max_size) {
     928           0 :     aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
     929             : 
     930             :     std::vector<float> template_queue_element(
     931           0 :         aec_render_queue_element_max_size_);
     932             : 
     933           0 :     aec_render_signal_queue_.reset(
     934             :         new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
     935             :             kMaxNumFramesToBuffer, template_queue_element,
     936           0 :             RenderQueueItemVerifier<float>(
     937           0 :                 aec_render_queue_element_max_size_)));
     938             : 
     939           0 :     aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
     940           0 :     aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
     941             :   } else {
     942           0 :     aec_render_signal_queue_->Clear();
     943             :   }
     944             : 
     945           0 :   if (aecm_render_queue_element_max_size_ <
     946             :       new_aecm_render_queue_element_max_size) {
     947           0 :     aecm_render_queue_element_max_size_ =
     948             :         new_aecm_render_queue_element_max_size;
     949             : 
     950             :     std::vector<int16_t> template_queue_element(
     951           0 :         aecm_render_queue_element_max_size_);
     952             : 
     953           0 :     aecm_render_signal_queue_.reset(
     954             :         new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
     955             :             kMaxNumFramesToBuffer, template_queue_element,
     956           0 :             RenderQueueItemVerifier<int16_t>(
     957           0 :                 aecm_render_queue_element_max_size_)));
     958             : 
     959           0 :     aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
     960           0 :     aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
     961             :   } else {
     962           0 :     aecm_render_signal_queue_->Clear();
     963             :   }
     964             : 
     965           0 :   if (agc_render_queue_element_max_size_ <
     966             :       new_agc_render_queue_element_max_size) {
     967           0 :     agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
     968             : 
     969             :     std::vector<int16_t> template_queue_element(
     970           0 :         agc_render_queue_element_max_size_);
     971             : 
     972           0 :     agc_render_signal_queue_.reset(
     973             :         new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
     974             :             kMaxNumFramesToBuffer, template_queue_element,
     975           0 :             RenderQueueItemVerifier<int16_t>(
     976           0 :                 agc_render_queue_element_max_size_)));
     977             : 
     978           0 :     agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
     979           0 :     agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
     980             :   } else {
     981           0 :     agc_render_signal_queue_->Clear();
     982             :   }
     983             : 
     984           0 :   if (red_render_queue_element_max_size_ <
     985             :       new_red_render_queue_element_max_size) {
     986           0 :     red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
     987             : 
     988             :     std::vector<float> template_queue_element(
     989           0 :         red_render_queue_element_max_size_);
     990             : 
     991           0 :     red_render_signal_queue_.reset(
     992             :         new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
     993             :             kMaxNumFramesToBuffer, template_queue_element,
     994           0 :             RenderQueueItemVerifier<float>(
     995           0 :                 red_render_queue_element_max_size_)));
     996             : 
     997           0 :     red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
     998           0 :     red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
     999             :   } else {
    1000           0 :     red_render_signal_queue_->Clear();
    1001             :   }
    1002           0 : }
    1003             : 
    1004           0 : void AudioProcessingImpl::EmptyQueuedRenderAudio() {
    1005           0 :   rtc::CritScope cs_capture(&crit_capture_);
    1006           0 :   while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
    1007           0 :     public_submodules_->echo_cancellation->ProcessRenderAudio(
    1008           0 :         aec_capture_queue_buffer_);
    1009             :   }
    1010             : 
    1011           0 :   while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
    1012           0 :     public_submodules_->echo_control_mobile->ProcessRenderAudio(
    1013           0 :         aecm_capture_queue_buffer_);
    1014             :   }
    1015             : 
    1016           0 :   while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
    1017           0 :     public_submodules_->gain_control->ProcessRenderAudio(
    1018           0 :         agc_capture_queue_buffer_);
    1019             :   }
    1020             : 
    1021           0 :   while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
    1022           0 :     private_submodules_->residual_echo_detector->AnalyzeRenderAudio(
    1023           0 :         red_capture_queue_buffer_);
    1024             :   }
    1025           0 : }
    1026             : 
    1027           0 : int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
    1028           0 :   TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
    1029             :   {
    1030             :     // Acquire the capture lock in order to safely call the function
    1031             :     // that retrieves the render side data. This function accesses apm
    1032             :     // getters that need the capture lock held when being called.
    1033             :     // The lock needs to be released as
    1034             :     // public_submodules_->echo_control_mobile->is_enabled() aquires this lock
    1035             :     // as well.
    1036           0 :     rtc::CritScope cs_capture(&crit_capture_);
    1037           0 :     EmptyQueuedRenderAudio();
    1038             :   }
    1039             : 
    1040           0 :   if (!frame) {
    1041           0 :     return kNullPointerError;
    1042             :   }
    1043             :   // Must be a native rate.
    1044           0 :   if (frame->sample_rate_hz_ != kSampleRate8kHz &&
    1045           0 :       frame->sample_rate_hz_ != kSampleRate16kHz &&
    1046           0 :       frame->sample_rate_hz_ != kSampleRate32kHz &&
    1047           0 :       frame->sample_rate_hz_ != kSampleRate48kHz) {
    1048           0 :     return kBadSampleRateError;
    1049             :   }
    1050             : 
    1051           0 :   ProcessingConfig processing_config;
    1052           0 :   bool reinitialization_required = false;
    1053             :   {
    1054             :     // Aquire lock for the access of api_format.
    1055             :     // The lock is released immediately due to the conditional
    1056             :     // reinitialization.
    1057           0 :     rtc::CritScope cs_capture(&crit_capture_);
    1058             :     // TODO(ajm): The input and output rates and channels are currently
    1059             :     // constrained to be identical in the int16 interface.
    1060           0 :     processing_config = formats_.api_format;
    1061             : 
    1062           0 :     reinitialization_required = UpdateActiveSubmoduleStates();
    1063             :   }
    1064           0 :   processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
    1065           0 :   processing_config.input_stream().set_num_channels(frame->num_channels_);
    1066           0 :   processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
    1067           0 :   processing_config.output_stream().set_num_channels(frame->num_channels_);
    1068             : 
    1069             :   {
    1070             :     // Do conditional reinitialization.
    1071           0 :     rtc::CritScope cs_render(&crit_render_);
    1072           0 :     RETURN_ON_ERR(
    1073             :         MaybeInitializeCapture(processing_config, reinitialization_required));
    1074             :   }
    1075           0 :   rtc::CritScope cs_capture(&crit_capture_);
    1076           0 :   if (frame->samples_per_channel_ !=
    1077           0 :       formats_.api_format.input_stream().num_frames()) {
    1078           0 :     return kBadDataLengthError;
    1079             :   }
    1080             : 
    1081             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
    1082             :   if (debug_dump_.debug_file->is_open()) {
    1083             :     RETURN_ON_ERR(WriteConfigMessage(false));
    1084             : 
    1085             :     debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM);
    1086             :     audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
    1087             :     const size_t data_size =
    1088             :         sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
    1089             :     msg->set_input_data(frame->data_, data_size);
    1090             :   }
    1091             : #endif
    1092             : 
    1093           0 :   capture_.capture_audio->DeinterleaveFrom(frame);
    1094           0 :   RETURN_ON_ERR(ProcessCaptureStreamLocked());
    1095           0 :   capture_.capture_audio->InterleaveTo(
    1096           0 :       frame, submodule_states_.CaptureMultiBandProcessingActive());
    1097             : 
    1098             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
    1099             :   if (debug_dump_.debug_file->is_open()) {
    1100             :     audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
    1101             :     const size_t data_size =
    1102             :         sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
    1103             :     msg->set_output_data(frame->data_, data_size);
    1104             :     RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
    1105             :                                           &debug_dump_.num_bytes_left_for_log_,
    1106             :                                           &crit_debug_, &debug_dump_.capture));
    1107             :   }
    1108             : #endif
    1109             : 
    1110           0 :   return kNoError;
    1111             : }
    1112             : 
    1113           0 : int AudioProcessingImpl::ProcessCaptureStreamLocked() {
    1114             :   // Ensure that not both the AEC and AECM are active at the same time.
    1115             :   // TODO(peah): Simplify once the public API Enable functions for these
    1116             :   // are moved to APM.
    1117           0 :   RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&
    1118           0 :                public_submodules_->echo_control_mobile->is_enabled()));
    1119             : 
    1120             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
    1121             :   if (debug_dump_.debug_file->is_open()) {
    1122             :     audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
    1123             :     msg->set_delay(capture_nonlocked_.stream_delay_ms);
    1124             :     msg->set_drift(
    1125             :         public_submodules_->echo_cancellation->stream_drift_samples());
    1126             :     msg->set_level(gain_control()->stream_analog_level());
    1127             :     msg->set_keypress(capture_.key_pressed);
    1128             :   }
    1129             : #endif
    1130             : 
    1131           0 :   MaybeUpdateHistograms();
    1132             : 
    1133           0 :   AudioBuffer* capture_buffer = capture_.capture_audio.get();  // For brevity.
    1134             : 
    1135           0 :   capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>(
    1136           0 :       capture_buffer->channels_const()[0],
    1137           0 :       capture_nonlocked_.capture_processing_format.num_frames()));
    1138           0 :   const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
    1139           0 :   if (log_rms) {
    1140           0 :     capture_rms_interval_counter_ = 0;
    1141           0 :     RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
    1142           0 :     RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
    1143             :                                 levels.average, 1, RmsLevel::kMinLevelDb, 64);
    1144           0 :     RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
    1145             :                                 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
    1146             :   }
    1147             : 
    1148           0 :   if (private_submodules_->echo_canceller3) {
    1149           0 :     private_submodules_->echo_canceller3->AnalyzeCapture(capture_buffer);
    1150             :   }
    1151             : 
    1152           0 :   if (constants_.use_experimental_agc &&
    1153           0 :       public_submodules_->gain_control->is_enabled()) {
    1154           0 :     private_submodules_->agc_manager->AnalyzePreProcess(
    1155           0 :         capture_buffer->channels()[0], capture_buffer->num_channels(),
    1156           0 :         capture_nonlocked_.capture_processing_format.num_frames());
    1157             :   }
    1158             : 
    1159           0 :   if (submodule_states_.CaptureMultiBandSubModulesActive() &&
    1160           0 :       SampleRateSupportsMultiBand(
    1161             :           capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
    1162           0 :     capture_buffer->SplitIntoFrequencyBands();
    1163             :   }
    1164             : 
    1165           0 :   if (capture_nonlocked_.beamformer_enabled) {
    1166           0 :     private_submodules_->beamformer->AnalyzeChunk(
    1167           0 :         *capture_buffer->split_data_f());
    1168             :     // Discards all channels by the leftmost one.
    1169           0 :     capture_buffer->set_num_channels(1);
    1170             :   }
    1171             : 
    1172             :   // TODO(peah): Move the AEC3 low-cut filter to this place.
    1173           0 :   if (private_submodules_->low_cut_filter &&
    1174           0 :       !private_submodules_->echo_canceller3) {
    1175           0 :     private_submodules_->low_cut_filter->Process(capture_buffer);
    1176             :   }
    1177           0 :   RETURN_ON_ERR(
    1178             :       public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
    1179           0 :   public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
    1180             : 
    1181             :   // Ensure that the stream delay was set before the call to the
    1182             :   // AEC ProcessCaptureAudio function.
    1183           0 :   if (public_submodules_->echo_cancellation->is_enabled() &&
    1184           0 :       !was_stream_delay_set()) {
    1185           0 :     return AudioProcessing::kStreamParameterNotSetError;
    1186             :   }
    1187             : 
    1188           0 :   if (private_submodules_->echo_canceller3) {
    1189           0 :     private_submodules_->echo_canceller3->ProcessCapture(capture_buffer, false);
    1190             :   }
    1191             : 
    1192           0 :   RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(
    1193             :       capture_buffer, stream_delay_ms()));
    1194             : 
    1195           0 :   if (public_submodules_->echo_control_mobile->is_enabled() &&
    1196           0 :       public_submodules_->noise_suppression->is_enabled()) {
    1197           0 :     capture_buffer->CopyLowPassToReference();
    1198             :   }
    1199           0 :   public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
    1200             : #if WEBRTC_INTELLIGIBILITY_ENHANCER
    1201             :   if (capture_nonlocked_.intelligibility_enabled) {
    1202             :     RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());
    1203             :     int gain_db = public_submodules_->gain_control->is_enabled() ?
    1204             :                   public_submodules_->gain_control->compression_gain_db() :
    1205             :                   0;
    1206             :     float gain = std::pow(10.f, gain_db / 20.f);
    1207             :     gain *= capture_nonlocked_.level_controller_enabled ?
    1208             :             private_submodules_->level_controller->GetLastGain() :
    1209             :             1.f;
    1210             :     public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(
    1211             :         public_submodules_->noise_suppression->NoiseEstimate(), gain);
    1212             :   }
    1213             : #endif
    1214             : 
    1215             :   // Ensure that the stream delay was set before the call to the
    1216             :   // AECM ProcessCaptureAudio function.
    1217           0 :   if (public_submodules_->echo_control_mobile->is_enabled() &&
    1218           0 :       !was_stream_delay_set()) {
    1219           0 :     return AudioProcessing::kStreamParameterNotSetError;
    1220             :   }
    1221             : 
    1222           0 :   RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(
    1223             :       capture_buffer, stream_delay_ms()));
    1224             : 
    1225           0 :   if (config_.residual_echo_detector.enabled) {
    1226           0 :     private_submodules_->residual_echo_detector->AnalyzeCaptureAudio(
    1227             :         rtc::ArrayView<const float>(
    1228           0 :             capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
    1229           0 :             capture_buffer->num_frames_per_band()));
    1230             :   }
    1231             : 
    1232           0 :   if (capture_nonlocked_.beamformer_enabled) {
    1233           0 :     private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f());
    1234             :   }
    1235             : 
    1236           0 :   public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
    1237             : 
    1238           0 :   if (constants_.use_experimental_agc &&
    1239           0 :       public_submodules_->gain_control->is_enabled() &&
    1240           0 :       (!capture_nonlocked_.beamformer_enabled ||
    1241           0 :        private_submodules_->beamformer->is_target_present())) {
    1242           0 :     private_submodules_->agc_manager->Process(
    1243           0 :         capture_buffer->split_bands_const(0)[kBand0To8kHz],
    1244           0 :         capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
    1245             :   }
    1246           0 :   RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
    1247             :       capture_buffer, echo_cancellation()->stream_has_echo()));
    1248             : 
    1249           0 :   if (submodule_states_.CaptureMultiBandProcessingActive() &&
    1250           0 :       SampleRateSupportsMultiBand(
    1251             :           capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
    1252           0 :     capture_buffer->MergeFrequencyBands();
    1253             :   }
    1254             : 
    1255             :   // TODO(aluebs): Investigate if the transient suppression placement should be
    1256             :   // before or after the AGC.
    1257           0 :   if (capture_.transient_suppressor_enabled) {
    1258             :     float voice_probability =
    1259           0 :         private_submodules_->agc_manager.get()
    1260           0 :             ? private_submodules_->agc_manager->voice_probability()
    1261           0 :             : 1.f;
    1262             : 
    1263           0 :     public_submodules_->transient_suppressor->Suppress(
    1264           0 :         capture_buffer->channels_f()[0], capture_buffer->num_frames(),
    1265           0 :         capture_buffer->num_channels(),
    1266           0 :         capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
    1267             :         capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
    1268             :         capture_buffer->num_keyboard_frames(), voice_probability,
    1269           0 :         capture_.key_pressed);
    1270             :   }
    1271             : 
    1272           0 :   if (capture_nonlocked_.level_controller_enabled) {
    1273           0 :     private_submodules_->level_controller->Process(capture_buffer);
    1274             :   }
    1275             : 
    1276             :   // The level estimator operates on the recombined data.
    1277           0 :   public_submodules_->level_estimator->ProcessStream(capture_buffer);
    1278             : 
    1279           0 :   capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>(
    1280           0 :       capture_buffer->channels_const()[0],
    1281           0 :       capture_nonlocked_.capture_processing_format.num_frames()));
    1282           0 :   if (log_rms) {
    1283           0 :     RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
    1284           0 :     RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
    1285             :                                 levels.average, 1, RmsLevel::kMinLevelDb, 64);
    1286           0 :     RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
    1287             :                                 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
    1288             :   }
    1289             : 
    1290           0 :   capture_.was_stream_delay_set = false;
    1291           0 :   return kNoError;
    1292             : }
    1293             : 
    1294           0 : int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
    1295             :                                               size_t samples_per_channel,
    1296             :                                               int sample_rate_hz,
    1297             :                                               ChannelLayout layout) {
    1298           0 :   TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
    1299           0 :   rtc::CritScope cs(&crit_render_);
    1300             :   const StreamConfig reverse_config = {
    1301           0 :       sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
    1302           0 :   };
    1303           0 :   if (samples_per_channel != reverse_config.num_frames()) {
    1304           0 :     return kBadDataLengthError;
    1305             :   }
    1306           0 :   return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
    1307             : }
    1308             : 
    1309           0 : int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
    1310             :                                               const StreamConfig& input_config,
    1311             :                                               const StreamConfig& output_config,
    1312             :                                               float* const* dest) {
    1313           0 :   TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
    1314           0 :   rtc::CritScope cs(&crit_render_);
    1315           0 :   RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
    1316           0 :   if (submodule_states_.RenderMultiBandProcessingActive()) {
    1317           0 :     render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
    1318           0 :                                  dest);
    1319           0 :   } else if (formats_.api_format.reverse_input_stream() !=
    1320           0 :              formats_.api_format.reverse_output_stream()) {
    1321           0 :     render_.render_converter->Convert(src, input_config.num_samples(), dest,
    1322           0 :                                       output_config.num_samples());
    1323             :   } else {
    1324           0 :     CopyAudioIfNeeded(src, input_config.num_frames(),
    1325           0 :                       input_config.num_channels(), dest);
    1326             :   }
    1327             : 
    1328           0 :   return kNoError;
    1329             : }
    1330             : 
    1331           0 : int AudioProcessingImpl::AnalyzeReverseStreamLocked(
    1332             :     const float* const* src,
    1333             :     const StreamConfig& input_config,
    1334             :     const StreamConfig& output_config) {
    1335           0 :   if (src == nullptr) {
    1336           0 :     return kNullPointerError;
    1337             :   }
    1338             : 
    1339           0 :   if (input_config.num_channels() == 0) {
    1340           0 :     return kBadNumberChannelsError;
    1341             :   }
    1342             : 
    1343           0 :   ProcessingConfig processing_config = formats_.api_format;
    1344           0 :   processing_config.reverse_input_stream() = input_config;
    1345           0 :   processing_config.reverse_output_stream() = output_config;
    1346             : 
    1347           0 :   RETURN_ON_ERR(MaybeInitializeRender(processing_config));
    1348           0 :   assert(input_config.num_frames() ==
    1349           0 :          formats_.api_format.reverse_input_stream().num_frames());
    1350             : 
    1351             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
    1352             :   if (debug_dump_.debug_file->is_open()) {
    1353             :     debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM);
    1354             :     audioproc::ReverseStream* msg =
    1355             :         debug_dump_.render.event_msg->mutable_reverse_stream();
    1356             :     const size_t channel_size =
    1357             :         sizeof(float) * formats_.api_format.reverse_input_stream().num_frames();
    1358             :     for (size_t i = 0;
    1359             :          i < formats_.api_format.reverse_input_stream().num_channels(); ++i)
    1360             :       msg->add_channel(src[i], channel_size);
    1361             :     RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
    1362             :                                           &debug_dump_.num_bytes_left_for_log_,
    1363             :                                           &crit_debug_, &debug_dump_.render));
    1364             :   }
    1365             : #endif
    1366             : 
    1367           0 :   render_.render_audio->CopyFrom(src,
    1368           0 :                                  formats_.api_format.reverse_input_stream());
    1369           0 :   return ProcessRenderStreamLocked();
    1370             : }
    1371             : 
    1372           0 : int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
    1373           0 :   TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
    1374           0 :   rtc::CritScope cs(&crit_render_);
    1375           0 :   if (frame == nullptr) {
    1376           0 :     return kNullPointerError;
    1377             :   }
    1378             :   // Must be a native rate.
    1379           0 :   if (frame->sample_rate_hz_ != kSampleRate8kHz &&
    1380           0 :       frame->sample_rate_hz_ != kSampleRate16kHz &&
    1381           0 :       frame->sample_rate_hz_ != kSampleRate32kHz &&
    1382           0 :       frame->sample_rate_hz_ != kSampleRate48kHz) {
    1383           0 :     return kBadSampleRateError;
    1384             :   }
    1385             : 
    1386           0 :   if (frame->num_channels_ <= 0) {
    1387           0 :     return kBadNumberChannelsError;
    1388             :   }
    1389             : 
    1390           0 :   ProcessingConfig processing_config = formats_.api_format;
    1391           0 :   processing_config.reverse_input_stream().set_sample_rate_hz(
    1392           0 :       frame->sample_rate_hz_);
    1393           0 :   processing_config.reverse_input_stream().set_num_channels(
    1394           0 :       frame->num_channels_);
    1395           0 :   processing_config.reverse_output_stream().set_sample_rate_hz(
    1396           0 :       frame->sample_rate_hz_);
    1397           0 :   processing_config.reverse_output_stream().set_num_channels(
    1398           0 :       frame->num_channels_);
    1399             : 
    1400           0 :   RETURN_ON_ERR(MaybeInitializeRender(processing_config));
    1401           0 :   if (frame->samples_per_channel_ !=
    1402           0 :       formats_.api_format.reverse_input_stream().num_frames()) {
    1403           0 :     return kBadDataLengthError;
    1404             :   }
    1405             : 
    1406             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
    1407             :   if (debug_dump_.debug_file->is_open()) {
    1408             :     debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM);
    1409             :     audioproc::ReverseStream* msg =
    1410             :         debug_dump_.render.event_msg->mutable_reverse_stream();
    1411             :     const size_t data_size =
    1412             :         sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
    1413             :     msg->set_data(frame->data_, data_size);
    1414             :     RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
    1415             :                                           &debug_dump_.num_bytes_left_for_log_,
    1416             :                                           &crit_debug_, &debug_dump_.render));
    1417             :   }
    1418             : #endif
    1419           0 :   render_.render_audio->DeinterleaveFrom(frame);
    1420           0 :   RETURN_ON_ERR(ProcessRenderStreamLocked());
    1421           0 :   render_.render_audio->InterleaveTo(
    1422           0 :       frame, submodule_states_.RenderMultiBandProcessingActive());
    1423           0 :   return kNoError;
    1424             : }
    1425             : 
    1426           0 : int AudioProcessingImpl::ProcessRenderStreamLocked() {
    1427           0 :   AudioBuffer* render_buffer = render_.render_audio.get();  // For brevity.
    1428           0 :   if (submodule_states_.RenderMultiBandSubModulesActive() &&
    1429           0 :       SampleRateSupportsMultiBand(
    1430             :           formats_.render_processing_format.sample_rate_hz())) {
    1431           0 :     render_buffer->SplitIntoFrequencyBands();
    1432             :   }
    1433             : 
    1434             : #if WEBRTC_INTELLIGIBILITY_ENHANCER
    1435             :   if (capture_nonlocked_.intelligibility_enabled) {
    1436             :     public_submodules_->intelligibility_enhancer->ProcessRenderAudio(
    1437             :         render_buffer);
    1438             :   }
    1439             : #endif
    1440             : 
    1441           0 :   QueueRenderAudio(render_buffer);
    1442             :   // TODO(peah): Perform the queueing ínside QueueRenderAudiuo().
    1443           0 :   if (private_submodules_->echo_canceller3) {
    1444           0 :     if (!private_submodules_->echo_canceller3->AnalyzeRender(render_buffer)) {
    1445             :       // TODO(peah): Lock and empty render queue, and try again.
    1446             :     }
    1447             :   }
    1448             : 
    1449           0 :   if (submodule_states_.RenderMultiBandProcessingActive() &&
    1450           0 :       SampleRateSupportsMultiBand(
    1451             :           formats_.render_processing_format.sample_rate_hz())) {
    1452           0 :     render_buffer->MergeFrequencyBands();
    1453             :   }
    1454             : 
    1455           0 :   return kNoError;
    1456             : }
    1457             : 
    1458           0 : int AudioProcessingImpl::set_stream_delay_ms(int delay) {
    1459           0 :   rtc::CritScope cs(&crit_capture_);
    1460           0 :   Error retval = kNoError;
    1461           0 :   capture_.was_stream_delay_set = true;
    1462           0 :   delay += capture_.delay_offset_ms;
    1463             : 
    1464           0 :   if (delay < 0) {
    1465           0 :     delay = 0;
    1466           0 :     retval = kBadStreamParameterWarning;
    1467             :   }
    1468             : 
    1469             :   // TODO(ajm): the max is rather arbitrarily chosen; investigate.
    1470           0 :   if (delay > 500) {
    1471           0 :     delay = 500;
    1472           0 :     retval = kBadStreamParameterWarning;
    1473             :   }
    1474             : 
    1475           0 :   capture_nonlocked_.stream_delay_ms = delay;
    1476           0 :   return retval;
    1477             : }
    1478             : 
    1479           0 : int AudioProcessingImpl::stream_delay_ms() const {
    1480             :   // Used as callback from submodules, hence locking is not allowed.
    1481           0 :   return capture_nonlocked_.stream_delay_ms;
    1482             : }
    1483             : 
    1484           0 : bool AudioProcessingImpl::was_stream_delay_set() const {
    1485             :   // Used as callback from submodules, hence locking is not allowed.
    1486           0 :   return capture_.was_stream_delay_set;
    1487             : }
    1488             : 
    1489           0 : void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
    1490           0 :   rtc::CritScope cs(&crit_capture_);
    1491           0 :   capture_.key_pressed = key_pressed;
    1492           0 : }
    1493             : 
    1494           0 : void AudioProcessingImpl::set_delay_offset_ms(int offset) {
    1495           0 :   rtc::CritScope cs(&crit_capture_);
    1496           0 :   capture_.delay_offset_ms = offset;
    1497           0 : }
    1498             : 
    1499           0 : int AudioProcessingImpl::delay_offset_ms() const {
    1500           0 :   rtc::CritScope cs(&crit_capture_);
    1501           0 :   return capture_.delay_offset_ms;
    1502             : }
    1503             : 
    1504           0 : int AudioProcessingImpl::StartDebugRecording(
    1505             :     const char filename[AudioProcessing::kMaxFilenameSize],
    1506             :     int64_t max_log_size_bytes) {
    1507             :   // Run in a single-threaded manner.
    1508           0 :   rtc::CritScope cs_render(&crit_render_);
    1509           0 :   rtc::CritScope cs_capture(&crit_capture_);
    1510             :   static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
    1511             : 
    1512           0 :   if (filename == nullptr) {
    1513           0 :     return kNullPointerError;
    1514             :   }
    1515             : 
    1516             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
    1517             :   debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes;
    1518             :   // Stop any ongoing recording.
    1519             :   debug_dump_.debug_file->CloseFile();
    1520             : 
    1521             :   if (!debug_dump_.debug_file->OpenFile(filename, false)) {
    1522             :     return kFileError;
    1523             :   }
    1524             : 
    1525             :   RETURN_ON_ERR(WriteConfigMessage(true));
    1526             :   RETURN_ON_ERR(WriteInitMessage());
    1527             :   return kNoError;
    1528             : #else
    1529           0 :   return kUnsupportedFunctionError;
    1530             : #endif  // WEBRTC_AUDIOPROC_DEBUG_DUMP
    1531             : }
    1532             : 
    1533           0 : int AudioProcessingImpl::StartDebugRecording(FILE* handle,
    1534             :                                              int64_t max_log_size_bytes) {
    1535             :   // Run in a single-threaded manner.
    1536           0 :   rtc::CritScope cs_render(&crit_render_);
    1537           0 :   rtc::CritScope cs_capture(&crit_capture_);
    1538             : 
    1539           0 :   if (handle == nullptr) {
    1540           0 :     return kNullPointerError;
    1541             :   }
    1542             : 
    1543             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
    1544             :   debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes;
    1545             : 
    1546             :   // Stop any ongoing recording.
    1547             :   debug_dump_.debug_file->CloseFile();
    1548             : 
    1549             :   if (!debug_dump_.debug_file->OpenFromFileHandle(handle)) {
    1550             :     return kFileError;
    1551             :   }
    1552             : 
    1553             :   RETURN_ON_ERR(WriteConfigMessage(true));
    1554             :   RETURN_ON_ERR(WriteInitMessage());
    1555             :   return kNoError;
    1556             : #else
    1557           0 :   return kUnsupportedFunctionError;
    1558             : #endif  // WEBRTC_AUDIOPROC_DEBUG_DUMP
    1559             : }
    1560             : 
    1561           0 : int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
    1562           0 :   return StartDebugRecording(handle, -1);
    1563             : }
    1564             : 
    1565           0 : int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
    1566             :     rtc::PlatformFile handle) {
    1567             :   // Run in a single-threaded manner.
    1568           0 :   rtc::CritScope cs_render(&crit_render_);
    1569           0 :   rtc::CritScope cs_capture(&crit_capture_);
    1570           0 :   FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
    1571           0 :   return StartDebugRecording(stream, -1);
    1572             : }
    1573             : 
    1574           0 : int AudioProcessingImpl::StopDebugRecording() {
    1575             :   // Run in a single-threaded manner.
    1576           0 :   rtc::CritScope cs_render(&crit_render_);
    1577           0 :   rtc::CritScope cs_capture(&crit_capture_);
    1578             : 
    1579             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
    1580             :   // We just return if recording hasn't started.
    1581             :   debug_dump_.debug_file->CloseFile();
    1582             :   return kNoError;
    1583             : #else
    1584           0 :   return kUnsupportedFunctionError;
    1585             : #endif  // WEBRTC_AUDIOPROC_DEBUG_DUMP
    1586             : }
    1587             : 
    1588           0 : AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics() {
    1589           0 :   residual_echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
    1590           0 :   echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
    1591           0 :   echo_return_loss_enhancement.Set(-100.0f, -100.0f, -100.0f, -100.0f);
    1592           0 :   a_nlp.Set(-100.0f, -100.0f, -100.0f, -100.0f);
    1593           0 : }
    1594             : 
    1595             : AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics(
    1596             :     const AudioProcessingStatistics& other) = default;
    1597             : 
    1598             : AudioProcessing::AudioProcessingStatistics::~AudioProcessingStatistics() =
    1599             :     default;
    1600             : 
    1601             : // TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
    1602           0 : AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics()
    1603             :     const {
    1604           0 :   return AudioProcessingStatistics();
    1605             : }
    1606             : 
    1607           0 : AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics()
    1608             :     const {
    1609           0 :   AudioProcessingStatistics stats;
    1610           0 :   EchoCancellation::Metrics metrics;
    1611           0 :   int success = public_submodules_->echo_cancellation->GetMetrics(&metrics);
    1612           0 :   if (success == Error::kNoError) {
    1613           0 :     stats.a_nlp.Set(metrics.a_nlp);
    1614           0 :     stats.divergent_filter_fraction = metrics.divergent_filter_fraction;
    1615           0 :     stats.echo_return_loss.Set(metrics.echo_return_loss);
    1616           0 :     stats.echo_return_loss_enhancement.Set(
    1617           0 :         metrics.echo_return_loss_enhancement);
    1618           0 :     stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss);
    1619             :   }
    1620           0 :   stats.residual_echo_likelihood =
    1621           0 :       private_submodules_->residual_echo_detector->echo_likelihood();
    1622           0 :   stats.residual_echo_likelihood_recent_max =
    1623           0 :       private_submodules_->residual_echo_detector->echo_likelihood_recent_max();
    1624           0 :   public_submodules_->echo_cancellation->GetDelayMetrics(
    1625             :       &stats.delay_median, &stats.delay_standard_deviation,
    1626           0 :       &stats.fraction_poor_delays);
    1627           0 :   return stats;
    1628             : }
    1629             : 
    1630           0 : EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
    1631           0 :   return public_submodules_->echo_cancellation.get();
    1632             : }
    1633             : 
    1634           0 : EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
    1635           0 :   return public_submodules_->echo_control_mobile.get();
    1636             : }
    1637             : 
    1638           0 : GainControl* AudioProcessingImpl::gain_control() const {
    1639           0 :   if (constants_.use_experimental_agc) {
    1640           0 :     return public_submodules_->gain_control_for_experimental_agc.get();
    1641             :   }
    1642           0 :   return public_submodules_->gain_control.get();
    1643             : }
    1644             : 
    1645           0 : HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
    1646           0 :   return high_pass_filter_impl_.get();
    1647             : }
    1648             : 
    1649           0 : LevelEstimator* AudioProcessingImpl::level_estimator() const {
    1650           0 :   return public_submodules_->level_estimator.get();
    1651             : }
    1652             : 
    1653           0 : NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
    1654           0 :   return public_submodules_->noise_suppression.get();
    1655             : }
    1656             : 
    1657           0 : VoiceDetection* AudioProcessingImpl::voice_detection() const {
    1658           0 :   return public_submodules_->voice_detection.get();
    1659             : }
    1660             : 
    1661           0 : void AudioProcessingImpl::MutateConfig(
    1662             :     rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
    1663           0 :   rtc::CritScope cs_render(&crit_render_);
    1664           0 :   rtc::CritScope cs_capture(&crit_capture_);
    1665           0 :   mutator(&config_);
    1666           0 :   ApplyConfig(config_);
    1667           0 : }
    1668             : 
    1669           0 : AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
    1670           0 :   rtc::CritScope cs_render(&crit_render_);
    1671           0 :   rtc::CritScope cs_capture(&crit_capture_);
    1672           0 :   return config_;
    1673             : }
    1674             : 
    1675           0 : bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
    1676           0 :   return submodule_states_.Update(
    1677           0 :       config_.high_pass_filter.enabled,
    1678           0 :       public_submodules_->echo_cancellation->is_enabled(),
    1679           0 :       public_submodules_->echo_control_mobile->is_enabled(),
    1680           0 :       config_.residual_echo_detector.enabled,
    1681           0 :       public_submodules_->noise_suppression->is_enabled(),
    1682           0 :       capture_nonlocked_.intelligibility_enabled,
    1683           0 :       capture_nonlocked_.beamformer_enabled,
    1684           0 :       public_submodules_->gain_control->is_enabled(),
    1685           0 :       capture_nonlocked_.level_controller_enabled,
    1686           0 :       capture_nonlocked_.echo_canceller3_enabled,
    1687           0 :       public_submodules_->voice_detection->is_enabled(),
    1688           0 :       public_submodules_->level_estimator->is_enabled(),
    1689           0 :       capture_.transient_suppressor_enabled);
    1690             : }
    1691             : 
    1692             : 
    1693           0 : void AudioProcessingImpl::InitializeTransient() {
    1694           0 :   if (capture_.transient_suppressor_enabled) {
    1695           0 :     if (!public_submodules_->transient_suppressor.get()) {
    1696           0 :       public_submodules_->transient_suppressor.reset(new TransientSuppressor());
    1697             :     }
    1698           0 :     public_submodules_->transient_suppressor->Initialize(
    1699             :         capture_nonlocked_.capture_processing_format.sample_rate_hz(),
    1700           0 :         capture_nonlocked_.split_rate, num_proc_channels());
    1701             :   }
    1702           0 : }
    1703             : 
    1704           0 : void AudioProcessingImpl::InitializeBeamformer() {
    1705           0 :   if (capture_nonlocked_.beamformer_enabled) {
    1706           0 :     if (!private_submodules_->beamformer) {
    1707           0 :       private_submodules_->beamformer.reset(new NonlinearBeamformer(
    1708           0 :           capture_.array_geometry, 1u, capture_.target_direction));
    1709             :     }
    1710           0 :     private_submodules_->beamformer->Initialize(kChunkSizeMs,
    1711           0 :                                                 capture_nonlocked_.split_rate);
    1712             :   }
    1713           0 : }
    1714             : 
    1715           0 : void AudioProcessingImpl::InitializeIntelligibility() {
    1716             : #if WEBRTC_INTELLIGIBILITY_ENHANCER
    1717             :   if (capture_nonlocked_.intelligibility_enabled) {
    1718             :     public_submodules_->intelligibility_enhancer.reset(
    1719             :         new IntelligibilityEnhancer(capture_nonlocked_.split_rate,
    1720             :                                     render_.render_audio->num_channels(),
    1721             :                                     render_.render_audio->num_bands(),
    1722             :                                     NoiseSuppressionImpl::num_noise_bins()));
    1723             :   }
    1724             : #endif
    1725           0 : }
    1726             : 
    1727           0 : void AudioProcessingImpl::InitializeLowCutFilter() {
    1728           0 :   if (config_.high_pass_filter.enabled) {
    1729           0 :     private_submodules_->low_cut_filter.reset(
    1730           0 :         new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
    1731             :   } else {
    1732           0 :     private_submodules_->low_cut_filter.reset();
    1733             :   }
    1734           0 : }
    1735           0 : void AudioProcessingImpl::InitializeEchoCanceller3() {
    1736           0 :   if (capture_nonlocked_.echo_canceller3_enabled) {
    1737           0 :     private_submodules_->echo_canceller3.reset(
    1738           0 :         new EchoCanceller3(proc_sample_rate_hz(), true));
    1739             :   } else {
    1740           0 :     private_submodules_->echo_canceller3.reset();
    1741             :   }
    1742           0 : }
    1743             : 
    1744           0 : void AudioProcessingImpl::InitializeLevelController() {
    1745           0 :   private_submodules_->level_controller->Initialize(proc_sample_rate_hz());
    1746           0 : }
    1747             : 
    1748           0 : void AudioProcessingImpl::InitializeResidualEchoDetector() {
    1749           0 :   private_submodules_->residual_echo_detector->Initialize();
    1750           0 : }
    1751             : 
    1752           0 : void AudioProcessingImpl::MaybeUpdateHistograms() {
    1753             :   static const int kMinDiffDelayMs = 60;
    1754             : 
    1755           0 :   if (echo_cancellation()->is_enabled()) {
    1756             :     // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
    1757             :     // If a stream has echo we know that the echo_cancellation is in process.
    1758           0 :     if (capture_.stream_delay_jumps == -1 &&
    1759           0 :         echo_cancellation()->stream_has_echo()) {
    1760           0 :       capture_.stream_delay_jumps = 0;
    1761             :     }
    1762           0 :     if (capture_.aec_system_delay_jumps == -1 &&
    1763           0 :         echo_cancellation()->stream_has_echo()) {
    1764           0 :       capture_.aec_system_delay_jumps = 0;
    1765             :     }
    1766             : 
    1767             :     // Detect a jump in platform reported system delay and log the difference.
    1768             :     const int diff_stream_delay_ms =
    1769           0 :         capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
    1770           0 :     if (diff_stream_delay_ms > kMinDiffDelayMs &&
    1771           0 :         capture_.last_stream_delay_ms != 0) {
    1772           0 :       RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
    1773             :                            diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
    1774           0 :       if (capture_.stream_delay_jumps == -1) {
    1775           0 :         capture_.stream_delay_jumps = 0;  // Activate counter if needed.
    1776             :       }
    1777           0 :       capture_.stream_delay_jumps++;
    1778             :     }
    1779           0 :     capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
    1780             : 
    1781             :     // Detect a jump in AEC system delay and log the difference.
    1782             :     const int samples_per_ms =
    1783           0 :         rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
    1784           0 :     RTC_DCHECK_LT(0, samples_per_ms);
    1785             :     const int aec_system_delay_ms =
    1786           0 :         public_submodules_->echo_cancellation->GetSystemDelayInSamples() /
    1787           0 :         samples_per_ms;
    1788             :     const int diff_aec_system_delay_ms =
    1789           0 :         aec_system_delay_ms - capture_.last_aec_system_delay_ms;
    1790           0 :     if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
    1791           0 :         capture_.last_aec_system_delay_ms != 0) {
    1792           0 :       RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
    1793             :                            diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
    1794             :                            100);
    1795           0 :       if (capture_.aec_system_delay_jumps == -1) {
    1796           0 :         capture_.aec_system_delay_jumps = 0;  // Activate counter if needed.
    1797             :       }
    1798           0 :       capture_.aec_system_delay_jumps++;
    1799             :     }
    1800           0 :     capture_.last_aec_system_delay_ms = aec_system_delay_ms;
    1801             :   }
    1802           0 : }
    1803             : 
    1804           0 : void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
    1805             :   // Run in a single-threaded manner.
    1806           0 :   rtc::CritScope cs_render(&crit_render_);
    1807           0 :   rtc::CritScope cs_capture(&crit_capture_);
    1808             : 
    1809           0 :   if (capture_.stream_delay_jumps > -1) {
    1810           0 :     RTC_HISTOGRAM_ENUMERATION(
    1811             :         "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
    1812             :         capture_.stream_delay_jumps, 51);
    1813             :   }
    1814           0 :   capture_.stream_delay_jumps = -1;
    1815           0 :   capture_.last_stream_delay_ms = 0;
    1816             : 
    1817           0 :   if (capture_.aec_system_delay_jumps > -1) {
    1818           0 :     RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
    1819             :                               capture_.aec_system_delay_jumps, 51);
    1820             :   }
    1821           0 :   capture_.aec_system_delay_jumps = -1;
    1822           0 :   capture_.last_aec_system_delay_ms = 0;
    1823           0 : }
    1824             : 
    1825             : #ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
    1826             : int AudioProcessingImpl::WriteMessageToDebugFile(
    1827             :     FileWrapper* debug_file,
    1828             :     int64_t* filesize_limit_bytes,
    1829             :     rtc::CriticalSection* crit_debug,
    1830             :     ApmDebugDumpThreadState* debug_state) {
    1831             :   int32_t size = debug_state->event_msg->ByteSize();
    1832             :   if (size <= 0) {
    1833             :     return kUnspecifiedError;
    1834             :   }
    1835             : #if defined(WEBRTC_ARCH_BIG_ENDIAN)
    1836             : // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
    1837             : //            pretty safe in assuming little-endian.
    1838             : #endif
    1839             : 
    1840             :   if (!debug_state->event_msg->SerializeToString(&debug_state->event_str)) {
    1841             :     return kUnspecifiedError;
    1842             :   }
    1843             : 
    1844             :   {
    1845             :     // Ensure atomic writes of the message.
    1846             :     rtc::CritScope cs_debug(crit_debug);
    1847             : 
    1848             :     RTC_DCHECK(debug_file->is_open());
    1849             :     // Update the byte counter.
    1850             :     if (*filesize_limit_bytes >= 0) {
    1851             :       *filesize_limit_bytes -=
    1852             :           (sizeof(int32_t) + debug_state->event_str.length());
    1853             :       if (*filesize_limit_bytes < 0) {
    1854             :         // Not enough bytes are left to write this message, so stop logging.
    1855             :         debug_file->CloseFile();
    1856             :         return kNoError;
    1857             :       }
    1858             :     }
    1859             :     // Write message preceded by its size.
    1860             :     if (!debug_file->Write(&size, sizeof(int32_t))) {
    1861             :       return kFileError;
    1862             :     }
    1863             :     if (!debug_file->Write(debug_state->event_str.data(),
    1864             :                            debug_state->event_str.length())) {
    1865             :       return kFileError;
    1866             :     }
    1867             :   }
    1868             : 
    1869             :   debug_state->event_msg->Clear();
    1870             : 
    1871             :   return kNoError;
    1872             : }
    1873             : 
    1874             : int AudioProcessingImpl::WriteInitMessage() {
    1875             :   debug_dump_.capture.event_msg->set_type(audioproc::Event::INIT);
    1876             :   audioproc::Init* msg = debug_dump_.capture.event_msg->mutable_init();
    1877             :   msg->set_sample_rate(formats_.api_format.input_stream().sample_rate_hz());
    1878             : 
    1879             :   msg->set_num_input_channels(static_cast<google::protobuf::int32>(
    1880             :       formats_.api_format.input_stream().num_channels()));
    1881             :   msg->set_num_output_channels(static_cast<google::protobuf::int32>(
    1882             :       formats_.api_format.output_stream().num_channels()));
    1883             :   msg->set_num_reverse_channels(static_cast<google::protobuf::int32>(
    1884             :       formats_.api_format.reverse_input_stream().num_channels()));
    1885             :   msg->set_reverse_sample_rate(
    1886             :       formats_.api_format.reverse_input_stream().sample_rate_hz());
    1887             :   msg->set_output_sample_rate(
    1888             :       formats_.api_format.output_stream().sample_rate_hz());
    1889             :   msg->set_reverse_output_sample_rate(
    1890             :       formats_.api_format.reverse_output_stream().sample_rate_hz());
    1891             :   msg->set_num_reverse_output_channels(
    1892             :       formats_.api_format.reverse_output_stream().num_channels());
    1893             : 
    1894             :   RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
    1895             :                                         &debug_dump_.num_bytes_left_for_log_,
    1896             :                                         &crit_debug_, &debug_dump_.capture));
    1897             :   return kNoError;
    1898             : }
    1899             : 
    1900             : int AudioProcessingImpl::WriteConfigMessage(bool forced) {
    1901             :   audioproc::Config config;
    1902             : 
    1903             :   config.set_aec_enabled(public_submodules_->echo_cancellation->is_enabled());
    1904             :   config.set_aec_delay_agnostic_enabled(
    1905             :       public_submodules_->echo_cancellation->is_delay_agnostic_enabled());
    1906             :   config.set_aec_drift_compensation_enabled(
    1907             :       public_submodules_->echo_cancellation->is_drift_compensation_enabled());
    1908             :   config.set_aec_extended_filter_enabled(
    1909             :       public_submodules_->echo_cancellation->is_extended_filter_enabled());
    1910             :   config.set_aec_suppression_level(static_cast<int>(
    1911             :       public_submodules_->echo_cancellation->suppression_level()));
    1912             : 
    1913             :   config.set_aecm_enabled(
    1914             :       public_submodules_->echo_control_mobile->is_enabled());
    1915             :   config.set_aecm_comfort_noise_enabled(
    1916             :       public_submodules_->echo_control_mobile->is_comfort_noise_enabled());
    1917             :   config.set_aecm_routing_mode(static_cast<int>(
    1918             :       public_submodules_->echo_control_mobile->routing_mode()));
    1919             : 
    1920             :   config.set_agc_enabled(public_submodules_->gain_control->is_enabled());
    1921             :   config.set_agc_mode(
    1922             :       static_cast<int>(public_submodules_->gain_control->mode()));
    1923             :   config.set_agc_limiter_enabled(
    1924             :       public_submodules_->gain_control->is_limiter_enabled());
    1925             :   config.set_noise_robust_agc_enabled(constants_.use_experimental_agc);
    1926             : 
    1927             :   config.set_hpf_enabled(config_.high_pass_filter.enabled);
    1928             : 
    1929             :   config.set_ns_enabled(public_submodules_->noise_suppression->is_enabled());
    1930             :   config.set_ns_level(
    1931             :       static_cast<int>(public_submodules_->noise_suppression->level()));
    1932             : 
    1933             :   config.set_transient_suppression_enabled(
    1934             :       capture_.transient_suppressor_enabled);
    1935             :   config.set_intelligibility_enhancer_enabled(
    1936             :       capture_nonlocked_.intelligibility_enabled);
    1937             : 
    1938             :   std::string experiments_description =
    1939             :       public_submodules_->echo_cancellation->GetExperimentsDescription();
    1940             :   // TODO(peah): Add semicolon-separated concatenations of experiment
    1941             :   // descriptions for other submodules.
    1942             :   if (capture_nonlocked_.level_controller_enabled) {
    1943             :     experiments_description += "LevelController;";
    1944             :   }
    1945             :   if (constants_.agc_clipped_level_min != kClippedLevelMin) {
    1946             :     experiments_description += "AgcClippingLevelExperiment;";
    1947             :   }
    1948             :   if (capture_nonlocked_.echo_canceller3_enabled) {
    1949             :     experiments_description += "EchoCanceller3;";
    1950             :   }
    1951             :   config.set_experiments_description(experiments_description);
    1952             : 
    1953             :   std::string serialized_config = config.SerializeAsString();
    1954             :   if (!forced &&
    1955             :       debug_dump_.capture.last_serialized_config == serialized_config) {
    1956             :     return kNoError;
    1957             :   }
    1958             : 
    1959             :   debug_dump_.capture.last_serialized_config = serialized_config;
    1960             : 
    1961             :   debug_dump_.capture.event_msg->set_type(audioproc::Event::CONFIG);
    1962             :   debug_dump_.capture.event_msg->mutable_config()->CopyFrom(config);
    1963             : 
    1964             :   RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
    1965             :                                         &debug_dump_.num_bytes_left_for_log_,
    1966             :                                         &crit_debug_, &debug_dump_.capture));
    1967             :   return kNoError;
    1968             : }
    1969             : #endif  // WEBRTC_AUDIOPROC_DEBUG_DUMP
    1970             : 
    1971           0 : AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
    1972             :     bool transient_suppressor_enabled,
    1973             :     const std::vector<Point>& array_geometry,
    1974           0 :     SphericalPointf target_direction)
    1975             :     : aec_system_delay_jumps(-1),
    1976             :       delay_offset_ms(0),
    1977             :       was_stream_delay_set(false),
    1978             :       last_stream_delay_ms(0),
    1979             :       last_aec_system_delay_ms(0),
    1980             :       stream_delay_jumps(-1),
    1981             :       output_will_be_muted(false),
    1982             :       key_pressed(false),
    1983             :       transient_suppressor_enabled(transient_suppressor_enabled),
    1984             :       array_geometry(array_geometry),
    1985             :       target_direction(target_direction),
    1986             :       capture_processing_format(kSampleRate16kHz),
    1987           0 :       split_rate(kSampleRate16kHz) {}
    1988             : 
    1989             : AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
    1990             : 
    1991             : AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
    1992             : 
    1993             : AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
    1994             : 
    1995             : }  // namespace webrtc

Generated by: LCOV version 1.13