blob: 0786ead34f8b1d6409dc580462c627036635560d [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org40654032012-01-30 20:51:15 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000011#include "webrtc/modules/audio_processing/audio_processing_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
peah103ac7e2017-04-12 05:40:55 -070013#include <math.h>
Michael Graczyk86c6d332015-07-23 11:41:39 -070014#include <algorithm>
alessiob3ec96df2017-05-22 06:57:06 -070015#include <string>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020017#include "webrtc/base/checks.h"
nisse368f5cf2017-04-05 05:00:33 -070018#include "webrtc/base/logging.h"
xians@webrtc.orge46bc772014-10-10 08:36:56 +000019#include "webrtc/base/platform_file.h"
peah369f8282015-12-17 06:42:29 -080020#include "webrtc/base/trace_event.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070021#include "webrtc/common_audio/audio_converter.h"
Michael Graczykdfa36052015-03-25 16:37:27 -070022#include "webrtc/common_audio/channel_buffer.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070023#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000024#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020025#include "webrtc/modules/audio_processing/aec/aec_core.h"
peahe0eae3c2016-12-14 01:16:23 -080026#include "webrtc/modules/audio_processing/aec3/echo_canceller3.h"
alessiob3ec96df2017-05-22 06:57:06 -070027#include "webrtc/modules/audio_processing/agc2/gain_controller2.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000028#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000029#include "webrtc/modules/audio_processing/audio_buffer.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000030#include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000031#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000032#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000033#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
peahbe615622016-02-13 16:40:47 -080034#include "webrtc/modules/audio_processing/gain_control_for_experimental_agc.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000035#include "webrtc/modules/audio_processing/gain_control_impl.h"
peah1bcfce52016-08-26 07:16:04 -070036#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -070037#include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h"
peah1bcfce52016-08-26 07:16:04 -070038#endif
peahca4cac72016-06-29 15:26:12 -070039#include "webrtc/modules/audio_processing/level_controller/level_controller.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000040#include "webrtc/modules/audio_processing/level_estimator_impl.h"
peah8271d042016-11-22 07:24:52 -080041#include "webrtc/modules/audio_processing/low_cut_filter.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000042#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
ivoc9f4a4a02016-10-28 05:39:16 -070043#include "webrtc/modules/audio_processing/residual_echo_detector.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000044#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000045#include "webrtc/modules/audio_processing/voice_detection_impl.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010046#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010047#include "webrtc/system_wrappers/include/file_wrapper.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010048#include "webrtc/system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000049
50#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
51// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000052#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000053#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000054#else
kjellander78ddd732016-02-09 08:13:06 -080055#include "webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000056#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000057#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000058
peah1bcfce52016-08-26 07:16:04 -070059// Check to verify that the define for the intelligibility enhancer is properly
60// set.
61#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
62 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
63 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
64#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
65#endif
66
Michael Graczyk86c6d332015-07-23 11:41:39 -070067#define RETURN_ON_ERR(expr) \
68 do { \
69 int err = (expr); \
70 if (err != kNoError) { \
71 return err; \
72 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000073 } while (0)
74
niklase@google.com470e71d2011-07-07 08:21:25 +000075namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070076
kwibergd59d3bb2016-09-13 07:49:33 -070077constexpr int AudioProcessing::kNativeSampleRatesHz[];
aluebsdf6416a2016-03-16 18:26:35 -070078
Michael Graczyk86c6d332015-07-23 11:41:39 -070079namespace {
80
81static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
82 switch (layout) {
83 case AudioProcessing::kMono:
84 case AudioProcessing::kStereo:
85 return false;
86 case AudioProcessing::kMonoAndKeyboard:
87 case AudioProcessing::kStereoAndKeyboard:
88 return true;
89 }
90
kwiberg9e2be5f2016-09-14 05:23:22 -070091 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070092 return false;
93}
aluebsdf6416a2016-03-16 18:26:35 -070094
peah2ace3f92016-09-10 04:42:27 -070095bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070096 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
97 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
98}
99
peah2ace3f92016-09-10 04:42:27 -0700100int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) {
101#ifdef WEBRTC_ARCH_ARM_FAMILY
kwibergd59d3bb2016-09-13 07:49:33 -0700102 constexpr int kMaxSplittingNativeProcessRate =
103 AudioProcessing::kSampleRate32kHz;
peah2ace3f92016-09-10 04:42:27 -0700104#else
kwibergd59d3bb2016-09-13 07:49:33 -0700105 constexpr int kMaxSplittingNativeProcessRate =
106 AudioProcessing::kSampleRate48kHz;
peah2ace3f92016-09-10 04:42:27 -0700107#endif
kwibergd59d3bb2016-09-13 07:49:33 -0700108 static_assert(
109 kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz,
110 "");
peah2ace3f92016-09-10 04:42:27 -0700111 const int uppermost_native_rate = band_splitting_required
112 ? kMaxSplittingNativeProcessRate
113 : AudioProcessing::kSampleRate48kHz;
114
115 for (auto rate : AudioProcessing::kNativeSampleRatesHz) {
116 if (rate >= uppermost_native_rate) {
117 return uppermost_native_rate;
118 }
119 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700120 return rate;
121 }
122 }
peah2ace3f92016-09-10 04:42:27 -0700123 RTC_NOTREACHED();
124 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700125}
126
peah9e6a2902017-05-15 07:19:21 -0700127// Maximum lengths that frame of samples being passed from the render side to
128// the capture side can have (does not apply to AEC3).
129static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
130static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
131
peah764e3642016-10-22 05:04:30 -0700132// Maximum number of frames to buffer in the render queue.
133// TODO(peah): Decrease this once we properly handle hugely unbalanced
134// reverse and forward call numbers.
135static const size_t kMaxNumFramesToBuffer = 100;
136
peah8271d042016-11-22 07:24:52 -0800137class HighPassFilterImpl : public HighPassFilter {
138 public:
139 explicit HighPassFilterImpl(AudioProcessingImpl* apm) : apm_(apm) {}
140 ~HighPassFilterImpl() override = default;
141
142 // HighPassFilter implementation.
143 int Enable(bool enable) override {
144 apm_->MutateConfig([enable](AudioProcessing::Config* config) {
145 config->high_pass_filter.enabled = enable;
146 });
147
148 return AudioProcessing::kNoError;
149 }
150
151 bool is_enabled() const override {
152 return apm_->GetConfig().high_pass_filter.enabled;
153 }
154
155 private:
156 AudioProcessingImpl* apm_;
157 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(HighPassFilterImpl);
158};
159
Michael Graczyk86c6d332015-07-23 11:41:39 -0700160} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000161
162// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000163static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000164
peah2ace3f92016-09-10 04:42:27 -0700165AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates() {}
166
167bool AudioProcessingImpl::ApmSubmoduleStates::Update(
peah8271d042016-11-22 07:24:52 -0800168 bool low_cut_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700169 bool echo_canceller_enabled,
170 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700171 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700172 bool noise_suppressor_enabled,
173 bool intelligibility_enhancer_enabled,
174 bool beamformer_enabled,
175 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700176 bool gain_controller2_enabled,
peah2ace3f92016-09-10 04:42:27 -0700177 bool level_controller_enabled,
peahe0eae3c2016-12-14 01:16:23 -0800178 bool echo_canceller3_enabled,
peah2ace3f92016-09-10 04:42:27 -0700179 bool voice_activity_detector_enabled,
180 bool level_estimator_enabled,
181 bool transient_suppressor_enabled) {
182 bool changed = false;
peah8271d042016-11-22 07:24:52 -0800183 changed |= (low_cut_filter_enabled != low_cut_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700184 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
185 changed |=
186 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700187 changed |=
188 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700189 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
190 changed |=
191 (intelligibility_enhancer_enabled != intelligibility_enhancer_enabled_);
192 changed |= (beamformer_enabled != beamformer_enabled_);
193 changed |=
194 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
alessiob3ec96df2017-05-22 06:57:06 -0700195 changed |=
196 (gain_controller2_enabled != gain_controller2_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700197 changed |= (level_controller_enabled != level_controller_enabled_);
peahe0eae3c2016-12-14 01:16:23 -0800198 changed |= (echo_canceller3_enabled != echo_canceller3_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700199 changed |= (level_estimator_enabled != level_estimator_enabled_);
200 changed |=
201 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
202 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
203 if (changed) {
peah8271d042016-11-22 07:24:52 -0800204 low_cut_filter_enabled_ = low_cut_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700205 echo_canceller_enabled_ = echo_canceller_enabled;
206 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700207 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700208 noise_suppressor_enabled_ = noise_suppressor_enabled;
209 intelligibility_enhancer_enabled_ = intelligibility_enhancer_enabled;
210 beamformer_enabled_ = beamformer_enabled;
211 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700212 gain_controller2_enabled_ = gain_controller2_enabled;
peah2ace3f92016-09-10 04:42:27 -0700213 level_controller_enabled_ = level_controller_enabled;
peahe0eae3c2016-12-14 01:16:23 -0800214 echo_canceller3_enabled_ = echo_canceller3_enabled;
peah2ace3f92016-09-10 04:42:27 -0700215 level_estimator_enabled_ = level_estimator_enabled;
216 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
217 transient_suppressor_enabled_ = transient_suppressor_enabled;
218 }
219
220 changed |= first_update_;
221 first_update_ = false;
222 return changed;
223}
224
225bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
226 const {
227#if WEBRTC_INTELLIGIBILITY_ENHANCER
228 return CaptureMultiBandProcessingActive() ||
peah52775842017-05-16 06:14:09 -0700229 intelligibility_enhancer_enabled_ || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700230#else
peah52775842017-05-16 06:14:09 -0700231 return CaptureMultiBandProcessingActive() || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700232#endif
233}
234
235bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
236 const {
peah8271d042016-11-22 07:24:52 -0800237 return low_cut_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700238 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
peahe0eae3c2016-12-14 01:16:23 -0800239 beamformer_enabled_ || adaptive_gain_controller_enabled_ ||
240 echo_canceller3_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700241}
242
peah23ac8b42017-05-23 05:33:56 -0700243bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
244 const {
245 return level_controller_enabled_;
246}
247
peah2ace3f92016-09-10 04:42:27 -0700248bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
249 const {
250 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800251 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
peah52775842017-05-16 06:14:09 -0700252 echo_canceller3_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700253}
254
255bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
256 const {
257#if WEBRTC_INTELLIGIBILITY_ENHANCER
258 return intelligibility_enhancer_enabled_;
259#else
260 return false;
261#endif
262}
263
solenberg5e465c32015-12-08 13:22:33 -0800264struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800265 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800266 // Accessed externally of APM without any lock acquired.
peahb624d8c2016-03-05 03:01:14 -0800267 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
peahbb9edbd2016-03-10 12:54:25 -0800268 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
peahbfa97112016-03-10 21:09:04 -0800269 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800270 std::unique_ptr<LevelEstimatorImpl> level_estimator;
271 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
272 std::unique_ptr<VoiceDetectionImpl> voice_detection;
273 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800274 gain_control_for_experimental_agc;
solenberg5e465c32015-12-08 13:22:33 -0800275
276 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800277 std::unique_ptr<TransientSuppressor> transient_suppressor;
peah1bcfce52016-08-26 07:16:04 -0700278#if WEBRTC_INTELLIGIBILITY_ENHANCER
kwiberg88788ad2016-02-19 07:04:49 -0800279 std::unique_ptr<IntelligibilityEnhancer> intelligibility_enhancer;
peah1bcfce52016-08-26 07:16:04 -0700280#endif
solenberg5e465c32015-12-08 13:22:33 -0800281};
282
283struct AudioProcessingImpl::ApmPrivateSubmodules {
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700284 explicit ApmPrivateSubmodules(NonlinearBeamformer* beamformer)
solenberg5e465c32015-12-08 13:22:33 -0800285 : beamformer(beamformer) {}
286 // Accessed internally from capture or during initialization
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700287 std::unique_ptr<NonlinearBeamformer> beamformer;
kwiberg88788ad2016-02-19 07:04:49 -0800288 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700289 std::unique_ptr<GainController2> gain_controller2;
peah8271d042016-11-22 07:24:52 -0800290 std::unique_ptr<LowCutFilter> low_cut_filter;
peahca4cac72016-06-29 15:26:12 -0700291 std::unique_ptr<LevelController> level_controller;
ivoc9f4a4a02016-10-28 05:39:16 -0700292 std::unique_ptr<ResidualEchoDetector> residual_echo_detector;
peahe0eae3c2016-12-14 01:16:23 -0800293 std::unique_ptr<EchoCanceller3> echo_canceller3;
solenberg5e465c32015-12-08 13:22:33 -0800294};
295
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000296AudioProcessing* AudioProcessing::Create() {
peah88ac8532016-09-12 16:47:25 -0700297 webrtc::Config config;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000298 return Create(config, nullptr);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000299}
300
peah88ac8532016-09-12 16:47:25 -0700301AudioProcessing* AudioProcessing::Create(const webrtc::Config& config) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000302 return Create(config, nullptr);
303}
304
peah88ac8532016-09-12 16:47:25 -0700305AudioProcessing* AudioProcessing::Create(const webrtc::Config& config,
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700306 NonlinearBeamformer* beamformer) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000307 AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000308 if (apm->Initialize() != kNoError) {
309 delete apm;
peahdf3efa82015-11-28 12:35:15 -0800310 apm = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000311 }
312
313 return apm;
314}
315
peah88ac8532016-09-12 16:47:25 -0700316AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000317 : AudioProcessingImpl(config, nullptr) {}
318
peah88ac8532016-09-12 16:47:25 -0700319AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config,
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700320 NonlinearBeamformer* beamformer)
peah8271d042016-11-22 07:24:52 -0800321 : high_pass_filter_impl_(new HighPassFilterImpl(this)),
322 public_submodules_(new ApmPublicSubmodules()),
peahdf3efa82015-11-28 12:35:15 -0800323 private_submodules_(new ApmPrivateSubmodules(beamformer)),
324 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800325 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000326#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700327 false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000328#else
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700329 config.Get<ExperimentalAgc>().enabled),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000330#endif
andrew1c7075f2015-06-24 18:14:14 -0700331#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
aluebs2a346882016-01-11 18:04:30 -0800332 capture_(false,
andrew1c7075f2015-06-24 18:14:14 -0700333#else
aluebs2a346882016-01-11 18:04:30 -0800334 capture_(config.Get<ExperimentalNs>().enabled,
andrew1c7075f2015-06-24 18:14:14 -0700335#endif
aluebs2a346882016-01-11 18:04:30 -0800336 config.Get<Beamforming>().array_geometry,
aluebsb2328d12016-01-11 20:32:29 -0800337 config.Get<Beamforming>().target_direction),
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700338 capture_nonlocked_(config.Get<Beamforming>().enabled,
peah88ac8532016-09-12 16:47:25 -0700339 config.Get<Intelligibility>().enabled) {
peahdf3efa82015-11-28 12:35:15 -0800340 {
341 rtc::CritScope cs_render(&crit_render_);
342 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000343
peahb624d8c2016-03-05 03:01:14 -0800344 public_submodules_->echo_cancellation.reset(
peahb58a1582016-03-15 09:34:24 -0700345 new EchoCancellationImpl(&crit_render_, &crit_capture_));
peahbb9edbd2016-03-10 12:54:25 -0800346 public_submodules_->echo_control_mobile.reset(
peah253534d2016-03-15 04:32:28 -0700347 new EchoControlMobileImpl(&crit_render_, &crit_capture_));
peahbfa97112016-03-10 21:09:04 -0800348 public_submodules_->gain_control.reset(
peahb8fbb542016-03-15 02:28:08 -0700349 new GainControlImpl(&crit_capture_, &crit_capture_));
solenberg949028f2015-12-15 11:39:38 -0800350 public_submodules_->level_estimator.reset(
351 new LevelEstimatorImpl(&crit_capture_));
solenberg5e465c32015-12-08 13:22:33 -0800352 public_submodules_->noise_suppression.reset(
353 new NoiseSuppressionImpl(&crit_capture_));
solenberga29386c2015-12-16 03:31:12 -0800354 public_submodules_->voice_detection.reset(
355 new VoiceDetectionImpl(&crit_capture_));
peahbe615622016-02-13 16:40:47 -0800356 public_submodules_->gain_control_for_experimental_agc.reset(
peahbfa97112016-03-10 21:09:04 -0800357 new GainControlForExperimentalAgc(
358 public_submodules_->gain_control.get(), &crit_capture_));
ivoc9f4a4a02016-10-28 05:39:16 -0700359 private_submodules_->residual_echo_detector.reset(
360 new ResidualEchoDetector());
peahca4cac72016-06-29 15:26:12 -0700361
peahc19f3122016-10-07 14:54:10 -0700362 // TODO(peah): Move this creation to happen only when the level controller
363 // is enabled.
peahca4cac72016-06-29 15:26:12 -0700364 private_submodules_->level_controller.reset(new LevelController());
peahdf3efa82015-11-28 12:35:15 -0800365 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000366
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000367 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000368}
369
370AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800371 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800372 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800373 private_submodules_->agc_manager.reset();
374 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800375 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000376
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000377#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700378 debug_dump_.debug_file->CloseFile();
peahdf3efa82015-11-28 12:35:15 -0800379#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000380}
381
niklase@google.com470e71d2011-07-07 08:21:25 +0000382int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800383 // Run in a single-threaded manner during initialization.
384 rtc::CritScope cs_render(&crit_render_);
385 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000386 return InitializeLocked();
387}
388
peahde65ddc2016-09-16 15:02:15 -0700389int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
390 int capture_output_sample_rate_hz,
391 int render_input_sample_rate_hz,
392 ChannelLayout capture_input_layout,
393 ChannelLayout capture_output_layout,
394 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700395 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700396 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
397 LayoutHasKeyboard(capture_input_layout)},
398 {capture_output_sample_rate_hz,
399 ChannelsFromLayout(capture_output_layout),
400 LayoutHasKeyboard(capture_output_layout)},
401 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
402 LayoutHasKeyboard(render_input_layout)},
403 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
404 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700405
406 return Initialize(processing_config);
407}
408
409int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800410 // Run in a single-threaded manner during initialization.
411 rtc::CritScope cs_render(&crit_render_);
412 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700413 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000414}
415
peahdf3efa82015-11-28 12:35:15 -0800416int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800417 const ProcessingConfig& processing_config) {
peah2ace3f92016-09-10 04:42:27 -0700418 return MaybeInitialize(processing_config, false);
peah81b9bfe2015-11-27 02:47:28 -0800419}
420
peahdf3efa82015-11-28 12:35:15 -0800421int AudioProcessingImpl::MaybeInitializeCapture(
peah2ace3f92016-09-10 04:42:27 -0700422 const ProcessingConfig& processing_config,
423 bool force_initialization) {
424 return MaybeInitialize(processing_config, force_initialization);
peah81b9bfe2015-11-27 02:47:28 -0800425}
426
kwiberg83ffe452016-08-29 14:46:07 -0700427#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
428
429AudioProcessingImpl::ApmDebugDumpThreadState::ApmDebugDumpThreadState()
430 : event_msg(new audioproc::Event()) {}
431
432AudioProcessingImpl::ApmDebugDumpThreadState::~ApmDebugDumpThreadState() {}
433
434AudioProcessingImpl::ApmDebugDumpState::ApmDebugDumpState()
435 : debug_file(FileWrapper::Create()) {}
436
437AudioProcessingImpl::ApmDebugDumpState::~ApmDebugDumpState() {}
438
439#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
440
peah192164e2015-11-17 02:16:45 -0800441// Calls InitializeLocked() if any of the audio parameters have changed from
peahdf3efa82015-11-28 12:35:15 -0800442// their current values (needs to be called while holding the crit_render_lock).
443int AudioProcessingImpl::MaybeInitialize(
peah2ace3f92016-09-10 04:42:27 -0700444 const ProcessingConfig& processing_config,
445 bool force_initialization) {
peahdf3efa82015-11-28 12:35:15 -0800446 // Called from both threads. Thread check is therefore not possible.
peah2ace3f92016-09-10 04:42:27 -0700447 if (processing_config == formats_.api_format && !force_initialization) {
peah192164e2015-11-17 02:16:45 -0800448 return kNoError;
449 }
peahdf3efa82015-11-28 12:35:15 -0800450
451 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800452 return InitializeLocked(processing_config);
453}
454
niklase@google.com470e71d2011-07-07 08:21:25 +0000455int AudioProcessingImpl::InitializeLocked() {
peah522d71b2017-02-23 05:16:26 -0800456 const int capture_audiobuffer_num_channels =
457 capture_nonlocked_.beamformer_enabled
458 ? formats_.api_format.input_stream().num_channels()
459 : formats_.api_format.output_stream().num_channels();
460
peahde65ddc2016-09-16 15:02:15 -0700461 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800462 formats_.api_format.reverse_output_stream().num_frames() == 0
peahde65ddc2016-09-16 15:02:15 -0700463 ? formats_.render_processing_format.num_frames()
peahdf3efa82015-11-28 12:35:15 -0800464 : formats_.api_format.reverse_output_stream().num_frames();
465 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
466 render_.render_audio.reset(new AudioBuffer(
467 formats_.api_format.reverse_input_stream().num_frames(),
468 formats_.api_format.reverse_input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700469 formats_.render_processing_format.num_frames(),
470 formats_.render_processing_format.num_channels(),
471 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700472 if (formats_.api_format.reverse_input_stream() !=
473 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800474 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800475 formats_.api_format.reverse_input_stream().num_channels(),
476 formats_.api_format.reverse_input_stream().num_frames(),
477 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800478 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700479 } else {
peahdf3efa82015-11-28 12:35:15 -0800480 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700481 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700482 } else {
peahdf3efa82015-11-28 12:35:15 -0800483 render_.render_audio.reset(nullptr);
484 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700485 }
peahce4d9152017-05-19 01:28:05 -0700486
peahdf3efa82015-11-28 12:35:15 -0800487 capture_.capture_audio.reset(
488 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
489 formats_.api_format.input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700490 capture_nonlocked_.capture_processing_format.num_frames(),
491 capture_audiobuffer_num_channels,
peahdf3efa82015-11-28 12:35:15 -0800492 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000493
peahde65ddc2016-09-16 15:02:15 -0700494 public_submodules_->echo_cancellation->Initialize(
495 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
496 num_proc_channels());
peah764e3642016-10-22 05:04:30 -0700497 AllocateRenderQueue();
498
ivoc3e9a5372016-10-28 07:55:33 -0700499 int success = public_submodules_->echo_cancellation->enable_metrics(true);
500 RTC_DCHECK_EQ(0, success);
501 success = public_submodules_->echo_cancellation->enable_delay_logging(true);
502 RTC_DCHECK_EQ(0, success);
peahde65ddc2016-09-16 15:02:15 -0700503 public_submodules_->echo_control_mobile->Initialize(
504 proc_split_sample_rate_hz(), num_reverse_channels(),
505 num_output_channels());
peah135259a2016-10-28 03:12:11 -0700506
507 public_submodules_->gain_control->Initialize(num_proc_channels(),
508 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700509 if (constants_.use_experimental_agc) {
510 if (!private_submodules_->agc_manager.get()) {
511 private_submodules_->agc_manager.reset(new AgcManagerDirect(
512 public_submodules_->gain_control.get(),
513 public_submodules_->gain_control_for_experimental_agc.get(),
henrik.lundinbd681b92016-12-05 09:08:42 -0800514 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min));
peahde65ddc2016-09-16 15:02:15 -0700515 }
516 private_submodules_->agc_manager->Initialize();
517 private_submodules_->agc_manager->SetCaptureMuted(
518 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700519 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700520 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200521 InitializeTransient();
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000522 InitializeBeamformer();
peah1bcfce52016-08-26 07:16:04 -0700523#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -0700524 InitializeIntelligibility();
peah1bcfce52016-08-26 07:16:04 -0700525#endif
peah8271d042016-11-22 07:24:52 -0800526 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700527 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
528 proc_sample_rate_hz());
529 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
530 public_submodules_->level_estimator->Initialize();
peahca4cac72016-06-29 15:26:12 -0700531 InitializeLevelController();
ivoc9f4a4a02016-10-28 05:39:16 -0700532 InitializeResidualEchoDetector();
peahe0eae3c2016-12-14 01:16:23 -0800533 InitializeEchoCanceller3();
alessiob3ec96df2017-05-22 06:57:06 -0700534 InitializeGainController2();
solenberg70f99032015-12-08 11:07:32 -0800535
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000536#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700537 if (debug_dump_.debug_file->is_open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000538 int err = WriteInitMessage();
539 if (err != kNoError) {
540 return err;
541 }
542 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000543#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000544
niklase@google.com470e71d2011-07-07 08:21:25 +0000545 return kNoError;
546}
547
Michael Graczyk86c6d332015-07-23 11:41:39 -0700548int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
549 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700550 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
551 return kBadSampleRateError;
552 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000553 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700554
Peter Kasting69558702016-01-12 16:26:35 -0800555 const size_t num_in_channels = config.input_stream().num_channels();
556 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700557
558 // Need at least one input channel.
559 // Need either one output channel or as many outputs as there are inputs.
560 if (num_in_channels == 0 ||
561 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700562 return kBadNumberChannelsError;
563 }
564
aluebsb2328d12016-01-11 20:32:29 -0800565 if (capture_nonlocked_.beamformer_enabled &&
Peter Kasting69558702016-01-12 16:26:35 -0800566 num_in_channels != capture_.array_geometry.size()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700567 return kBadNumberChannelsError;
568 }
569
peahdf3efa82015-11-28 12:35:15 -0800570 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000571
peahde65ddc2016-09-16 15:02:15 -0700572 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700573 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700574 formats_.api_format.output_stream().sample_rate_hz()),
575 submodule_states_.CaptureMultiBandSubModulesActive() ||
576 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000577
peahde65ddc2016-09-16 15:02:15 -0700578 capture_nonlocked_.capture_processing_format =
579 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700580
peah2ce640f2017-04-07 03:57:48 -0700581 int render_processing_rate;
582 if (!config_.echo_canceller3.enabled) {
583 render_processing_rate = FindNativeProcessRateToUse(
584 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
585 formats_.api_format.reverse_output_stream().sample_rate_hz()),
586 submodule_states_.CaptureMultiBandSubModulesActive() ||
587 submodule_states_.RenderMultiBandSubModulesActive());
588 } else {
589 render_processing_rate = capture_processing_rate;
590 }
591
aluebseb3603b2016-04-20 15:27:58 -0700592 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
593 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700594 if (render_processing_rate > kSampleRate32kHz &&
595 !config_.echo_canceller3.enabled) {
peahde65ddc2016-09-16 15:02:15 -0700596 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
597 ? kSampleRate32kHz
598 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700599 }
peah2ce640f2017-04-07 03:57:48 -0700600
peahde65ddc2016-09-16 15:02:15 -0700601 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700602 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700603 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
604 kSampleRate8kHz) {
605 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000606 } else {
peahde65ddc2016-09-16 15:02:15 -0700607 render_processing_rate =
608 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000609 }
610
peahde65ddc2016-09-16 15:02:15 -0700611 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000612 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700613 if (submodule_states_.RenderMultiBandSubModulesActive()) {
614 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
615 } else {
616 formats_.render_processing_format = StreamConfig(
617 formats_.api_format.reverse_input_stream().sample_rate_hz(),
618 formats_.api_format.reverse_input_stream().num_channels());
619 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000620
peahde65ddc2016-09-16 15:02:15 -0700621 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
622 kSampleRate32kHz ||
623 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
624 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800625 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000626 } else {
peahdf3efa82015-11-28 12:35:15 -0800627 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700628 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000629 }
630
631 return InitializeLocked();
632}
633
peah88ac8532016-09-12 16:47:25 -0700634void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peahc19f3122016-10-07 14:54:10 -0700635 config_ = config;
peah88ac8532016-09-12 16:47:25 -0700636
peahc19f3122016-10-07 14:54:10 -0700637 bool config_ok = LevelController::Validate(config_.level_controller);
peah88ac8532016-09-12 16:47:25 -0700638 if (!config_ok) {
639 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
640 << "level_controller: "
peahc19f3122016-10-07 14:54:10 -0700641 << LevelController::ToString(config_.level_controller)
peah88ac8532016-09-12 16:47:25 -0700642 << std::endl
643 << "Reverting to default parameter set";
peahc19f3122016-10-07 14:54:10 -0700644 config_.level_controller = AudioProcessing::Config::LevelController();
peah88ac8532016-09-12 16:47:25 -0700645 }
646
647 // Run in a single-threaded manner when applying the settings.
648 rtc::CritScope cs_render(&crit_render_);
649 rtc::CritScope cs_capture(&crit_capture_);
650
peahc19f3122016-10-07 14:54:10 -0700651 // TODO(peah): Replace the use of capture_nonlocked_.level_controller_enabled
652 // with the value in config_ everywhere in the code.
653 if (capture_nonlocked_.level_controller_enabled !=
654 config_.level_controller.enabled) {
peah88ac8532016-09-12 16:47:25 -0700655 capture_nonlocked_.level_controller_enabled =
peahc19f3122016-10-07 14:54:10 -0700656 config_.level_controller.enabled;
657 // TODO(peah): Remove the conditional initialization to always initialize
658 // the level controller regardless of whether it is enabled or not.
659 InitializeLevelController();
peah88ac8532016-09-12 16:47:25 -0700660 }
peahc19f3122016-10-07 14:54:10 -0700661 LOG(LS_INFO) << "Level controller activated: "
662 << capture_nonlocked_.level_controller_enabled;
663
664 private_submodules_->level_controller->ApplyConfig(config_.level_controller);
peah8271d042016-11-22 07:24:52 -0800665
666 InitializeLowCutFilter();
667
668 LOG(LS_INFO) << "Highpass filter activated: "
669 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800670
671 config_ok = EchoCanceller3::Validate(config_.echo_canceller3);
672 if (!config_ok) {
673 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
674 << "echo canceller 3: "
675 << EchoCanceller3::ToString(config_.echo_canceller3)
676 << std::endl
677 << "Reverting to default parameter set";
678 config_.echo_canceller3 = AudioProcessing::Config::EchoCanceller3();
679 }
680
681 if (config.echo_canceller3.enabled !=
682 capture_nonlocked_.echo_canceller3_enabled) {
683 capture_nonlocked_.echo_canceller3_enabled =
684 config_.echo_canceller3.enabled;
685 InitializeEchoCanceller3();
686 LOG(LS_INFO) << "Echo canceller 3 activated: "
687 << capture_nonlocked_.echo_canceller3_enabled;
688 }
alessiob3ec96df2017-05-22 06:57:06 -0700689
690 config_ok = GainController2::Validate(config_.gain_controller2);
691 if (!config_ok) {
692 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
693 << "gain_controller2: "
694 << GainController2::ToString(config_.gain_controller2)
695 << std::endl
696 << "Reverting to default parameter set";
697 config_.gain_controller2 = AudioProcessing::Config::GainController2();
698 }
699
700 if (config.gain_controller2.enabled !=
701 capture_nonlocked_.gain_controller2_enabled) {
702 capture_nonlocked_.gain_controller2_enabled =
703 config_.gain_controller2.enabled;
704 InitializeGainController2();
705 LOG(LS_INFO) << "Gain controller 2 activated: "
706 << capture_nonlocked_.gain_controller2_enabled;
707 }
peah88ac8532016-09-12 16:47:25 -0700708}
709
710void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800711 // Run in a single-threaded manner when setting the extra options.
712 rtc::CritScope cs_render(&crit_render_);
713 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000714
peahb624d8c2016-03-05 03:01:14 -0800715 public_submodules_->echo_cancellation->SetExtraOptions(config);
716
peahdf3efa82015-11-28 12:35:15 -0800717 if (capture_.transient_suppressor_enabled !=
718 config.Get<ExperimentalNs>().enabled) {
719 capture_.transient_suppressor_enabled =
720 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000721 InitializeTransient();
722 }
aluebs2a346882016-01-11 18:04:30 -0800723
peah1bcfce52016-08-26 07:16:04 -0700724#if WEBRTC_INTELLIGIBILITY_ENHANCER
alessiob3ec96df2017-05-22 06:57:06 -0700725 if (capture_nonlocked_.intelligibility_enabled !=
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700726 config.Get<Intelligibility>().enabled) {
727 capture_nonlocked_.intelligibility_enabled =
728 config.Get<Intelligibility>().enabled;
729 InitializeIntelligibility();
730 }
peah1bcfce52016-08-26 07:16:04 -0700731#endif
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700732
aluebs2a346882016-01-11 18:04:30 -0800733#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
aluebsb2328d12016-01-11 20:32:29 -0800734 if (capture_nonlocked_.beamformer_enabled !=
735 config.Get<Beamforming>().enabled) {
736 capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled;
aluebs2a346882016-01-11 18:04:30 -0800737 if (config.Get<Beamforming>().array_geometry.size() > 1) {
738 capture_.array_geometry = config.Get<Beamforming>().array_geometry;
739 }
740 capture_.target_direction = config.Get<Beamforming>().target_direction;
741 InitializeBeamformer();
742 }
743#endif // WEBRTC_ANDROID_PLATFORM_BUILD
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000744}
745
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000746int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800747 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700748 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000749}
750
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000751int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800752 // Used as callback from submodules, hence locking is not allowed.
753 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000754}
755
Peter Kasting69558702016-01-12 16:26:35 -0800756size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800757 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700758 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000759}
760
Peter Kasting69558702016-01-12 16:26:35 -0800761size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800762 // Used as callback from submodules, hence locking is not allowed.
763 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000764}
765
Peter Kasting69558702016-01-12 16:26:35 -0800766size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800767 // Used as callback from submodules, hence locking is not allowed.
peahedddac52017-05-16 01:08:58 -0700768 return (capture_nonlocked_.beamformer_enabled ||
769 capture_nonlocked_.echo_canceller3_enabled)
770 ? 1
771 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800772}
773
Peter Kasting69558702016-01-12 16:26:35 -0800774size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800775 // Used as callback from submodules, hence locking is not allowed.
776 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000777}
778
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000779void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800780 rtc::CritScope cs(&crit_capture_);
781 capture_.output_will_be_muted = muted;
782 if (private_submodules_->agc_manager.get()) {
783 private_submodules_->agc_manager->SetCaptureMuted(
784 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000785 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000786}
787
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000788
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000789int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700790 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000791 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000792 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000793 int output_sample_rate_hz,
794 ChannelLayout output_layout,
795 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800796 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800797 StreamConfig input_stream;
798 StreamConfig output_stream;
799 {
800 // Access the formats_.api_format.input_stream beneath the capture lock.
801 // The lock must be released as it is later required in the call
802 // to ProcessStream(,,,);
803 rtc::CritScope cs(&crit_capture_);
804 input_stream = formats_.api_format.input_stream();
805 output_stream = formats_.api_format.output_stream();
806 }
807
Michael Graczyk86c6d332015-07-23 11:41:39 -0700808 input_stream.set_sample_rate_hz(input_sample_rate_hz);
809 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
810 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700811 output_stream.set_sample_rate_hz(output_sample_rate_hz);
812 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
813 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
814
815 if (samples_per_channel != input_stream.num_frames()) {
816 return kBadDataLengthError;
817 }
818 return ProcessStream(src, input_stream, output_stream, dest);
819}
820
821int AudioProcessingImpl::ProcessStream(const float* const* src,
822 const StreamConfig& input_config,
823 const StreamConfig& output_config,
824 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800825 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800826 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700827 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800828 {
829 // Acquire the capture lock in order to safely call the function
830 // that retrieves the render side data. This function accesses apm
831 // getters that need the capture lock held when being called.
832 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700833 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800834
835 if (!src || !dest) {
836 return kNullPointerError;
837 }
838
839 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700840 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000841 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000842
Michael Graczyk86c6d332015-07-23 11:41:39 -0700843 processing_config.input_stream() = input_config;
844 processing_config.output_stream() = output_config;
845
peahdf3efa82015-11-28 12:35:15 -0800846 {
847 // Do conditional reinitialization.
848 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -0700849 RETURN_ON_ERR(
850 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -0800851 }
852 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700853 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
854 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000855
856#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700857 if (debug_dump_.debug_file->is_open()) {
Minyue13b96ba2015-10-03 00:39:14 +0200858 RETURN_ON_ERR(WriteConfigMessage(false));
859
peahdf3efa82015-11-28 12:35:15 -0800860 debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM);
861 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000862 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -0800863 sizeof(float) * formats_.api_format.input_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -0800864 for (size_t i = 0; i < formats_.api_format.input_stream().num_channels();
865 ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000866 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000867 }
868#endif
869
peahdf3efa82015-11-28 12:35:15 -0800870 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700871 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800872 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000873
874#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700875 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -0800876 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000877 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -0800878 sizeof(float) * formats_.api_format.output_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -0800879 for (size_t i = 0; i < formats_.api_format.output_stream().num_channels();
880 ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000881 msg->add_output_channel(dest[i], channel_size);
peahdf3efa82015-11-28 12:35:15 -0800882 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -0800883 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -0800884 &crit_debug_, &debug_dump_.capture));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000885 }
886#endif
887
888 return kNoError;
889}
890
peah9e6a2902017-05-15 07:19:21 -0700891void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
peah764e3642016-10-22 05:04:30 -0700892 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
893 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700894 &aec_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700895
kwibergaf476c72016-11-28 15:21:39 -0800896 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -0700897
898 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700899 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700900 // The data queue is full and needs to be emptied.
901 EmptyQueuedRenderAudio();
902
903 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700904 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700905 RTC_DCHECK(result);
906 }
907
908 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
909 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700910 &aecm_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700911
912 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700913 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700914 // The data queue is full and needs to be emptied.
915 EmptyQueuedRenderAudio();
916
917 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700918 bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700919 RTC_DCHECK(result);
920 }
peah701d6282016-10-25 05:42:20 -0700921
922 if (!constants_.use_experimental_agc) {
923 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
924 // Insert the samples into the queue.
925 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
926 // The data queue is full and needs to be emptied.
927 EmptyQueuedRenderAudio();
928
929 // Retry the insert (should always work).
930 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
931 RTC_DCHECK(result);
932 }
933 }
peah9e6a2902017-05-15 07:19:21 -0700934}
ivoc9f4a4a02016-10-28 05:39:16 -0700935
peah9e6a2902017-05-15 07:19:21 -0700936void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -0700937 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
938
939 // Insert the samples into the queue.
940 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
941 // The data queue is full and needs to be emptied.
942 EmptyQueuedRenderAudio();
943
944 // Retry the insert (should always work).
945 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
946 RTC_DCHECK(result);
947 }
peah764e3642016-10-22 05:04:30 -0700948}
949
950void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -0700951 const size_t new_aec_render_queue_element_max_size =
peah764e3642016-10-22 05:04:30 -0700952 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -0700953 kMaxAllowedValuesOfSamplesPerBand *
peah764e3642016-10-22 05:04:30 -0700954 EchoCancellationImpl::NumCancellersRequired(
955 num_output_channels(), num_reverse_channels()));
956
peah701d6282016-10-25 05:42:20 -0700957 const size_t new_aecm_render_queue_element_max_size =
peaha0624602016-10-25 04:45:24 -0700958 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -0700959 kMaxAllowedValuesOfSamplesPerBand *
peaha0624602016-10-25 04:45:24 -0700960 EchoControlMobileImpl::NumCancellersRequired(
961 num_output_channels(), num_reverse_channels()));
peah764e3642016-10-22 05:04:30 -0700962
peah701d6282016-10-25 05:42:20 -0700963 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -0700964 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -0700965
ivoc9f4a4a02016-10-28 05:39:16 -0700966 const size_t new_red_render_queue_element_max_size =
967 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
968
peaha0624602016-10-25 04:45:24 -0700969 // Reallocate the queues if the queue item sizes are too small to fit the
970 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -0700971 if (aec_render_queue_element_max_size_ <
972 new_aec_render_queue_element_max_size) {
973 aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
peah764e3642016-10-22 05:04:30 -0700974
peaha0624602016-10-25 04:45:24 -0700975 std::vector<float> template_queue_element(
peah701d6282016-10-25 05:42:20 -0700976 aec_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700977
peah701d6282016-10-25 05:42:20 -0700978 aec_render_signal_queue_.reset(
peah764e3642016-10-22 05:04:30 -0700979 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
980 kMaxNumFramesToBuffer, template_queue_element,
peaha0624602016-10-25 04:45:24 -0700981 RenderQueueItemVerifier<float>(
peah701d6282016-10-25 05:42:20 -0700982 aec_render_queue_element_max_size_)));
peah764e3642016-10-22 05:04:30 -0700983
peah701d6282016-10-25 05:42:20 -0700984 aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
985 aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
peah764e3642016-10-22 05:04:30 -0700986 } else {
peah701d6282016-10-25 05:42:20 -0700987 aec_render_signal_queue_->Clear();
peaha0624602016-10-25 04:45:24 -0700988 }
989
peah701d6282016-10-25 05:42:20 -0700990 if (aecm_render_queue_element_max_size_ <
991 new_aecm_render_queue_element_max_size) {
992 aecm_render_queue_element_max_size_ =
993 new_aecm_render_queue_element_max_size;
peaha0624602016-10-25 04:45:24 -0700994
995 std::vector<int16_t> template_queue_element(
peah701d6282016-10-25 05:42:20 -0700996 aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700997
peah701d6282016-10-25 05:42:20 -0700998 aecm_render_signal_queue_.reset(
peaha0624602016-10-25 04:45:24 -0700999 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1000 kMaxNumFramesToBuffer, template_queue_element,
1001 RenderQueueItemVerifier<int16_t>(
peah701d6282016-10-25 05:42:20 -07001002 aecm_render_queue_element_max_size_)));
peaha0624602016-10-25 04:45:24 -07001003
peah701d6282016-10-25 05:42:20 -07001004 aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
1005 aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001006 } else {
peah701d6282016-10-25 05:42:20 -07001007 aecm_render_signal_queue_->Clear();
1008 }
1009
1010 if (agc_render_queue_element_max_size_ <
1011 new_agc_render_queue_element_max_size) {
1012 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1013
1014 std::vector<int16_t> template_queue_element(
1015 agc_render_queue_element_max_size_);
1016
1017 agc_render_signal_queue_.reset(
1018 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1019 kMaxNumFramesToBuffer, template_queue_element,
1020 RenderQueueItemVerifier<int16_t>(
1021 agc_render_queue_element_max_size_)));
1022
1023 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1024 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1025 } else {
1026 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001027 }
ivoc9f4a4a02016-10-28 05:39:16 -07001028
1029 if (red_render_queue_element_max_size_ <
1030 new_red_render_queue_element_max_size) {
1031 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1032
1033 std::vector<float> template_queue_element(
1034 red_render_queue_element_max_size_);
1035
1036 red_render_signal_queue_.reset(
1037 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1038 kMaxNumFramesToBuffer, template_queue_element,
1039 RenderQueueItemVerifier<float>(
1040 red_render_queue_element_max_size_)));
1041
1042 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1043 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1044 } else {
1045 red_render_signal_queue_->Clear();
1046 }
peah764e3642016-10-22 05:04:30 -07001047}
1048
1049void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1050 rtc::CritScope cs_capture(&crit_capture_);
peah701d6282016-10-25 05:42:20 -07001051 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -07001052 public_submodules_->echo_cancellation->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001053 aec_capture_queue_buffer_);
peaha0624602016-10-25 04:45:24 -07001054 }
1055
peah701d6282016-10-25 05:42:20 -07001056 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -07001057 public_submodules_->echo_control_mobile->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001058 aecm_capture_queue_buffer_);
1059 }
1060
1061 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1062 public_submodules_->gain_control->ProcessRenderAudio(
1063 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001064 }
ivoc9f4a4a02016-10-28 05:39:16 -07001065
1066 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
1067 private_submodules_->residual_echo_detector->AnalyzeRenderAudio(
1068 red_capture_queue_buffer_);
1069 }
peah764e3642016-10-22 05:04:30 -07001070}
1071
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001072int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001073 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001074 {
1075 // Acquire the capture lock in order to safely call the function
1076 // that retrieves the render side data. This function accesses apm
1077 // getters that need the capture lock held when being called.
1078 // The lock needs to be released as
1079 // public_submodules_->echo_control_mobile->is_enabled() aquires this lock
1080 // as well.
1081 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001082 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001083 }
peahfa6228e2015-11-16 16:27:42 -08001084
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001085 if (!frame) {
1086 return kNullPointerError;
1087 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001088 // Must be a native rate.
1089 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1090 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001091 frame->sample_rate_hz_ != kSampleRate32kHz &&
1092 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001093 return kBadSampleRateError;
1094 }
peah192164e2015-11-17 02:16:45 -08001095
peahdf3efa82015-11-28 12:35:15 -08001096 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001097 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001098 {
1099 // Aquire lock for the access of api_format.
1100 // The lock is released immediately due to the conditional
1101 // reinitialization.
1102 rtc::CritScope cs_capture(&crit_capture_);
1103 // TODO(ajm): The input and output rates and channels are currently
1104 // constrained to be identical in the int16 interface.
1105 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001106
1107 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001108 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001109 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1110 processing_config.input_stream().set_num_channels(frame->num_channels_);
1111 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1112 processing_config.output_stream().set_num_channels(frame->num_channels_);
1113
peahdf3efa82015-11-28 12:35:15 -08001114 {
1115 // Do conditional reinitialization.
1116 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -07001117 RETURN_ON_ERR(
1118 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -08001119 }
1120 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001121 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001122 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001123 return kBadDataLengthError;
1124 }
1125
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001126#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001127 if (debug_dump_.debug_file->is_open()) {
peah644fa962016-08-18 06:48:33 -07001128 RETURN_ON_ERR(WriteConfigMessage(false));
1129
peahdf3efa82015-11-28 12:35:15 -08001130 debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM);
1131 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001132 const size_t data_size =
1133 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001134 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001135 }
1136#endif
1137
peahdf3efa82015-11-28 12:35:15 -08001138 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001139 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001140 capture_.capture_audio->InterleaveTo(
peah23ac8b42017-05-23 05:33:56 -07001141 frame, submodule_states_.CaptureMultiBandProcessingActive() ||
1142 submodule_states_.CaptureFullBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001143
1144#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001145 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001146 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001147 const size_t data_size =
1148 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001149 msg->set_output_data(frame->data_, data_size);
peahdf3efa82015-11-28 12:35:15 -08001150 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001151 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001152 &crit_debug_, &debug_dump_.capture));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001153 }
1154#endif
1155
1156 return kNoError;
1157}
1158
peahde65ddc2016-09-16 15:02:15 -07001159int AudioProcessingImpl::ProcessCaptureStreamLocked() {
peahb58a1582016-03-15 09:34:24 -07001160 // Ensure that not both the AEC and AECM are active at the same time.
1161 // TODO(peah): Simplify once the public API Enable functions for these
1162 // are moved to APM.
1163 RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&
1164 public_submodules_->echo_control_mobile->is_enabled()));
1165
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001166#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001167 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001168 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
1169 msg->set_delay(capture_nonlocked_.stream_delay_ms);
1170 msg->set_drift(
1171 public_submodules_->echo_cancellation->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +00001172 msg->set_level(gain_control()->stream_analog_level());
peahdf3efa82015-11-28 12:35:15 -08001173 msg->set_keypress(capture_.key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +00001174 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001175#endif
niklase@google.com470e71d2011-07-07 08:21:25 +00001176
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001177 MaybeUpdateHistograms();
1178
peahde65ddc2016-09-16 15:02:15 -07001179 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001180
peah1b08dc32016-12-20 13:45:58 -08001181 capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>(
henrik.lundin290d43a2016-11-29 08:09:09 -08001182 capture_buffer->channels_const()[0],
1183 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001184 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1185 if (log_rms) {
1186 capture_rms_interval_counter_ = 0;
1187 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001188 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1189 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1190 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1191 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001192 }
1193
peahe0eae3c2016-12-14 01:16:23 -08001194 if (private_submodules_->echo_canceller3) {
peah67995532017-04-10 14:12:41 -07001195 const int new_agc_level = gain_control()->stream_analog_level();
1196 capture_.echo_path_gain_change =
peah103ac7e2017-04-12 05:40:55 -07001197 abs(capture_.previous_agc_level - new_agc_level) > 5;
peah67995532017-04-10 14:12:41 -07001198 capture_.previous_agc_level = new_agc_level;
peahe0eae3c2016-12-14 01:16:23 -08001199 private_submodules_->echo_canceller3->AnalyzeCapture(capture_buffer);
1200 }
1201
peahbe615622016-02-13 16:40:47 -08001202 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001203 public_submodules_->gain_control->is_enabled()) {
1204 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001205 capture_buffer->channels()[0], capture_buffer->num_channels(),
1206 capture_nonlocked_.capture_processing_format.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001207 }
1208
peah2ace3f92016-09-10 04:42:27 -07001209 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1210 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001211 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1212 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001213 }
1214
peah522d71b2017-02-23 05:16:26 -08001215 if (private_submodules_->echo_canceller3) {
1216 // Force down-mixing of the number of channels after the detection of
1217 // capture signal saturation.
1218 // TODO(peah): Look into ensuring that this kind of tampering with the
1219 // AudioBuffer functionality should not be needed.
1220 capture_buffer->set_num_channels(1);
1221 }
1222
aluebsb2328d12016-01-11 20:32:29 -08001223 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001224 private_submodules_->beamformer->AnalyzeChunk(
1225 *capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001226 // Discards all channels by the leftmost one.
peahde65ddc2016-09-16 15:02:15 -07001227 capture_buffer->set_num_channels(1);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001228 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001229
peahe0eae3c2016-12-14 01:16:23 -08001230 // TODO(peah): Move the AEC3 low-cut filter to this place.
1231 if (private_submodules_->low_cut_filter &&
1232 !private_submodules_->echo_canceller3) {
peah8271d042016-11-22 07:24:52 -08001233 private_submodules_->low_cut_filter->Process(capture_buffer);
1234 }
peahde65ddc2016-09-16 15:02:15 -07001235 RETURN_ON_ERR(
1236 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1237 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001238
1239 // Ensure that the stream delay was set before the call to the
1240 // AEC ProcessCaptureAudio function.
1241 if (public_submodules_->echo_cancellation->is_enabled() &&
1242 !was_stream_delay_set()) {
1243 return AudioProcessing::kStreamParameterNotSetError;
1244 }
1245
peahe0eae3c2016-12-14 01:16:23 -08001246 if (private_submodules_->echo_canceller3) {
peah67995532017-04-10 14:12:41 -07001247 private_submodules_->echo_canceller3->ProcessCapture(
1248 capture_buffer, capture_.echo_path_gain_change);
peah61202ac2017-02-06 03:39:42 -08001249 } else {
1250 RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(
1251 capture_buffer, stream_delay_ms()));
peahe0eae3c2016-12-14 01:16:23 -08001252 }
1253
peahdf3efa82015-11-28 12:35:15 -08001254 if (public_submodules_->echo_control_mobile->is_enabled() &&
1255 public_submodules_->noise_suppression->is_enabled()) {
peahde65ddc2016-09-16 15:02:15 -07001256 capture_buffer->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +00001257 }
peahde65ddc2016-09-16 15:02:15 -07001258 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah1bcfce52016-08-26 07:16:04 -07001259#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001260 if (capture_nonlocked_.intelligibility_enabled) {
aluebsc466bad2016-02-10 12:03:00 -08001261 RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001262 int gain_db = public_submodules_->gain_control->is_enabled() ?
1263 public_submodules_->gain_control->compression_gain_db() :
1264 0;
Alejandro Luebs50411102016-06-30 15:35:41 -07001265 float gain = std::pow(10.f, gain_db / 20.f);
1266 gain *= capture_nonlocked_.level_controller_enabled ?
1267 private_submodules_->level_controller->GetLastGain() :
1268 1.f;
aluebsc466bad2016-02-10 12:03:00 -08001269 public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -07001270 public_submodules_->noise_suppression->NoiseEstimate(), gain);
aluebsc466bad2016-02-10 12:03:00 -08001271 }
peah1bcfce52016-08-26 07:16:04 -07001272#endif
peah253534d2016-03-15 04:32:28 -07001273
1274 // Ensure that the stream delay was set before the call to the
1275 // AECM ProcessCaptureAudio function.
1276 if (public_submodules_->echo_control_mobile->is_enabled() &&
1277 !was_stream_delay_set()) {
1278 return AudioProcessing::kStreamParameterNotSetError;
1279 }
1280
1281 RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001282 capture_buffer, stream_delay_ms()));
peah253534d2016-03-15 04:32:28 -07001283
ivoc9f4a4a02016-10-28 05:39:16 -07001284
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001285 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001286 private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001287 }
1288
peahde65ddc2016-09-16 15:02:15 -07001289 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001290
peahbe615622016-02-13 16:40:47 -08001291 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001292 public_submodules_->gain_control->is_enabled() &&
aluebsb2328d12016-01-11 20:32:29 -08001293 (!capture_nonlocked_.beamformer_enabled ||
peahdf3efa82015-11-28 12:35:15 -08001294 private_submodules_->beamformer->is_target_present())) {
1295 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001296 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1297 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001298 }
peahb8fbb542016-03-15 02:28:08 -07001299 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001300 capture_buffer, echo_cancellation()->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001301
peah2ace3f92016-09-10 04:42:27 -07001302 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1303 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001304 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1305 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001306 }
1307
peah9e6a2902017-05-15 07:19:21 -07001308 if (config_.residual_echo_detector.enabled) {
1309 private_submodules_->residual_echo_detector->AnalyzeCaptureAudio(
1310 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
1311 capture_buffer->num_frames()));
1312 }
1313
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001314 // TODO(aluebs): Investigate if the transient suppression placement should be
1315 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001316 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001317 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001318 private_submodules_->agc_manager.get()
1319 ? private_submodules_->agc_manager->voice_probability()
1320 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001321
peahdf3efa82015-11-28 12:35:15 -08001322 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001323 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1324 capture_buffer->num_channels(),
1325 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1326 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1327 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001328 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001329 }
1330
alessiob3ec96df2017-05-22 06:57:06 -07001331 if (capture_nonlocked_.gain_controller2_enabled) {
1332 private_submodules_->gain_controller2->Process(capture_buffer);
1333 }
1334
peahca4cac72016-06-29 15:26:12 -07001335 if (capture_nonlocked_.level_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001336 private_submodules_->level_controller->Process(capture_buffer);
peahca4cac72016-06-29 15:26:12 -07001337 }
1338
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001339 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001340 public_submodules_->level_estimator->ProcessStream(capture_buffer);
ajm@google.com808e0e02011-08-03 21:08:51 +00001341
peah1b08dc32016-12-20 13:45:58 -08001342 capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>(
1343 capture_buffer->channels_const()[0],
1344 capture_nonlocked_.capture_processing_format.num_frames()));
1345 if (log_rms) {
1346 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1347 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1348 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1349 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1350 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1351 }
1352
peahdf3efa82015-11-28 12:35:15 -08001353 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001354 return kNoError;
1355}
1356
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001357int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001358 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001359 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001360 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001361 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001362 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001363 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001364 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001365 };
1366 if (samples_per_channel != reverse_config.num_frames()) {
1367 return kBadDataLengthError;
1368 }
peahdf3efa82015-11-28 12:35:15 -08001369 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001370}
1371
peahde65ddc2016-09-16 15:02:15 -07001372int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1373 const StreamConfig& input_config,
1374 const StreamConfig& output_config,
1375 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001376 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001377 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001378 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
peah2ace3f92016-09-10 04:42:27 -07001379 if (submodule_states_.RenderMultiBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001380 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1381 dest);
peah2ace3f92016-09-10 04:42:27 -07001382 } else if (formats_.api_format.reverse_input_stream() !=
1383 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001384 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1385 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001386 } else {
peahde65ddc2016-09-16 15:02:15 -07001387 CopyAudioIfNeeded(src, input_config.num_frames(),
1388 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001389 }
1390
1391 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001392}
1393
peahdf3efa82015-11-28 12:35:15 -08001394int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001395 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001396 const StreamConfig& input_config,
1397 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001398 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001399 return kNullPointerError;
1400 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001401
peahde65ddc2016-09-16 15:02:15 -07001402 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001403 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001404 }
1405
peahdf3efa82015-11-28 12:35:15 -08001406 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001407 processing_config.reverse_input_stream() = input_config;
1408 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001409
peahdf3efa82015-11-28 12:35:15 -08001410 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
peahde65ddc2016-09-16 15:02:15 -07001411 assert(input_config.num_frames() ==
1412 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001413
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001414#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001415 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001416 debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM);
1417 audioproc::ReverseStream* msg =
1418 debug_dump_.render.event_msg->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +00001419 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -08001420 sizeof(float) * formats_.api_format.reverse_input_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -08001421 for (size_t i = 0;
peahdf3efa82015-11-28 12:35:15 -08001422 i < formats_.api_format.reverse_input_stream().num_channels(); ++i)
ekmeyerson60d9b332015-08-14 10:35:55 -07001423 msg->add_channel(src[i], channel_size);
peahdf3efa82015-11-28 12:35:15 -08001424 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001425 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001426 &crit_debug_, &debug_dump_.render));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001427 }
1428#endif
1429
peahdf3efa82015-11-28 12:35:15 -08001430 render_.render_audio->CopyFrom(src,
1431 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001432 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001433}
1434
1435int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001436 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001437 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001438 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001439 return kNullPointerError;
1440 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001441 // Must be a native rate.
1442 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1443 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001444 frame->sample_rate_hz_ != kSampleRate32kHz &&
1445 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001446 return kBadSampleRateError;
1447 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001448
Michael Graczyk86c6d332015-07-23 11:41:39 -07001449 if (frame->num_channels_ <= 0) {
1450 return kBadNumberChannelsError;
1451 }
1452
peahdf3efa82015-11-28 12:35:15 -08001453 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001454 processing_config.reverse_input_stream().set_sample_rate_hz(
1455 frame->sample_rate_hz_);
1456 processing_config.reverse_input_stream().set_num_channels(
1457 frame->num_channels_);
1458 processing_config.reverse_output_stream().set_sample_rate_hz(
1459 frame->sample_rate_hz_);
1460 processing_config.reverse_output_stream().set_num_channels(
1461 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001462
peahdf3efa82015-11-28 12:35:15 -08001463 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001464 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001465 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001466 return kBadDataLengthError;
1467 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001468
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001469#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001470 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001471 debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM);
1472 audioproc::ReverseStream* msg =
1473 debug_dump_.render.event_msg->mutable_reverse_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001474 const size_t data_size =
1475 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001476 msg->set_data(frame->data_, data_size);
peahdf3efa82015-11-28 12:35:15 -08001477 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001478 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001479 &crit_debug_, &debug_dump_.render));
niklase@google.com470e71d2011-07-07 08:21:25 +00001480 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001481#endif
peahdf3efa82015-11-28 12:35:15 -08001482 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001483 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001484 render_.render_audio->InterleaveTo(
1485 frame, submodule_states_.RenderMultiBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001486 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001487}
niklase@google.com470e71d2011-07-07 08:21:25 +00001488
peahde65ddc2016-09-16 15:02:15 -07001489int AudioProcessingImpl::ProcessRenderStreamLocked() {
1490 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001491
1492 QueueNonbandedRenderAudio(render_buffer);
1493
peah2ace3f92016-09-10 04:42:27 -07001494 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001495 SampleRateSupportsMultiBand(
1496 formats_.render_processing_format.sample_rate_hz())) {
1497 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001498 }
1499
peah1bcfce52016-08-26 07:16:04 -07001500#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001501 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001502 public_submodules_->intelligibility_enhancer->ProcessRenderAudio(
Alejandro Luebsef009252016-09-20 14:51:56 -07001503 render_buffer);
ekmeyerson60d9b332015-08-14 10:35:55 -07001504 }
peah1bcfce52016-08-26 07:16:04 -07001505#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001506
peahce4d9152017-05-19 01:28:05 -07001507 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1508 QueueBandedRenderAudio(render_buffer);
1509 }
1510
peahe0eae3c2016-12-14 01:16:23 -08001511 // TODO(peah): Perform the queueing ínside QueueRenderAudiuo().
1512 if (private_submodules_->echo_canceller3) {
peahcf02cf12017-04-05 14:18:07 -07001513 private_submodules_->echo_canceller3->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001514 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001515
peah2ace3f92016-09-10 04:42:27 -07001516 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001517 SampleRateSupportsMultiBand(
1518 formats_.render_processing_format.sample_rate_hz())) {
1519 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001520 }
1521
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001522 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001523}
1524
1525int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001526 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001527 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001528 capture_.was_stream_delay_set = true;
1529 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001530
niklase@google.com470e71d2011-07-07 08:21:25 +00001531 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001532 delay = 0;
1533 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001534 }
1535
1536 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1537 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001538 delay = 500;
1539 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001540 }
1541
peahdf3efa82015-11-28 12:35:15 -08001542 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001543 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001544}
1545
1546int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001547 // Used as callback from submodules, hence locking is not allowed.
1548 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001549}
1550
1551bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001552 // Used as callback from submodules, hence locking is not allowed.
1553 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001554}
1555
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001556void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001557 rtc::CritScope cs(&crit_capture_);
1558 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001559}
1560
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001561void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001562 rtc::CritScope cs(&crit_capture_);
1563 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001564}
1565
1566int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001567 rtc::CritScope cs(&crit_capture_);
1568 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001569}
1570
niklase@google.com470e71d2011-07-07 08:21:25 +00001571int AudioProcessingImpl::StartDebugRecording(
ivocd66b44d2016-01-15 03:06:36 -08001572 const char filename[AudioProcessing::kMaxFilenameSize],
1573 int64_t max_log_size_bytes) {
peahdf3efa82015-11-28 12:35:15 -08001574 // Run in a single-threaded manner.
1575 rtc::CritScope cs_render(&crit_render_);
1576 rtc::CritScope cs_capture(&crit_capture_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +02001577 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +00001578
peahdf3efa82015-11-28 12:35:15 -08001579 if (filename == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001580 return kNullPointerError;
1581 }
1582
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001583#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ivocd66b44d2016-01-15 03:06:36 -08001584 debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001585 // Stop any ongoing recording.
tommia6219cc2016-06-15 10:30:14 -07001586 debug_dump_.debug_file->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +00001587
tommia6219cc2016-06-15 10:30:14 -07001588 if (!debug_dump_.debug_file->OpenFile(filename, false)) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001589 return kFileError;
1590 }
1591
Minyue13b96ba2015-10-03 00:39:14 +02001592 RETURN_ON_ERR(WriteConfigMessage(true));
1593 RETURN_ON_ERR(WriteInitMessage());
niklase@google.com470e71d2011-07-07 08:21:25 +00001594 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001595#else
1596 return kUnsupportedFunctionError;
1597#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001598}
1599
ivocd66b44d2016-01-15 03:06:36 -08001600int AudioProcessingImpl::StartDebugRecording(FILE* handle,
1601 int64_t max_log_size_bytes) {
peahdf3efa82015-11-28 12:35:15 -08001602 // Run in a single-threaded manner.
1603 rtc::CritScope cs_render(&crit_render_);
1604 rtc::CritScope cs_capture(&crit_capture_);
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001605
peahdf3efa82015-11-28 12:35:15 -08001606 if (handle == nullptr) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001607 return kNullPointerError;
1608 }
1609
1610#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ivocd66b44d2016-01-15 03:06:36 -08001611 debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes;
1612
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001613 // Stop any ongoing recording.
tommia6219cc2016-06-15 10:30:14 -07001614 debug_dump_.debug_file->CloseFile();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001615
tommia6219cc2016-06-15 10:30:14 -07001616 if (!debug_dump_.debug_file->OpenFromFileHandle(handle)) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001617 return kFileError;
1618 }
1619
Minyue13b96ba2015-10-03 00:39:14 +02001620 RETURN_ON_ERR(WriteConfigMessage(true));
1621 RETURN_ON_ERR(WriteInitMessage());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001622 return kNoError;
1623#else
1624 return kUnsupportedFunctionError;
1625#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1626}
1627
peah73a28ee2016-10-12 03:01:49 -07001628int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
1629 return StartDebugRecording(handle, -1);
1630}
1631
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001632int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
1633 rtc::PlatformFile handle) {
peahdf3efa82015-11-28 12:35:15 -08001634 // Run in a single-threaded manner.
1635 rtc::CritScope cs_render(&crit_render_);
1636 rtc::CritScope cs_capture(&crit_capture_);
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001637 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
ivocd66b44d2016-01-15 03:06:36 -08001638 return StartDebugRecording(stream, -1);
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001639}
1640
niklase@google.com470e71d2011-07-07 08:21:25 +00001641int AudioProcessingImpl::StopDebugRecording() {
peahdf3efa82015-11-28 12:35:15 -08001642 // Run in a single-threaded manner.
1643 rtc::CritScope cs_render(&crit_render_);
1644 rtc::CritScope cs_capture(&crit_capture_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001645
1646#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001647 // We just return if recording hasn't started.
tommia6219cc2016-06-15 10:30:14 -07001648 debug_dump_.debug_file->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +00001649 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001650#else
1651 return kUnsupportedFunctionError;
1652#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001653}
1654
ivoc4e477a12017-01-15 08:29:46 -08001655AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics() {
1656 residual_echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1657 echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1658 echo_return_loss_enhancement.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1659 a_nlp.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1660}
1661
1662AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics(
1663 const AudioProcessingStatistics& other) = default;
1664
1665AudioProcessing::AudioProcessingStatistics::~AudioProcessingStatistics() =
1666 default;
1667
ivoc3e9a5372016-10-28 07:55:33 -07001668// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
1669AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics()
1670 const {
1671 return AudioProcessingStatistics();
1672}
1673
1674AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics()
1675 const {
1676 AudioProcessingStatistics stats;
1677 EchoCancellation::Metrics metrics;
ivocd0a151c2016-11-02 09:14:37 -07001678 int success = public_submodules_->echo_cancellation->GetMetrics(&metrics);
1679 if (success == Error::kNoError) {
1680 stats.a_nlp.Set(metrics.a_nlp);
1681 stats.divergent_filter_fraction = metrics.divergent_filter_fraction;
1682 stats.echo_return_loss.Set(metrics.echo_return_loss);
1683 stats.echo_return_loss_enhancement.Set(
1684 metrics.echo_return_loss_enhancement);
1685 stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss);
1686 }
ivoc9c192b22017-03-16 04:22:14 -07001687 {
1688 rtc::CritScope cs_capture(&crit_capture_);
1689 stats.residual_echo_likelihood =
1690 private_submodules_->residual_echo_detector->echo_likelihood();
1691 stats.residual_echo_likelihood_recent_max =
1692 private_submodules_->residual_echo_detector
1693 ->echo_likelihood_recent_max();
1694 }
ivoc3e9a5372016-10-28 07:55:33 -07001695 public_submodules_->echo_cancellation->GetDelayMetrics(
1696 &stats.delay_median, &stats.delay_standard_deviation,
1697 &stats.fraction_poor_delays);
1698 return stats;
1699}
1700
niklase@google.com470e71d2011-07-07 08:21:25 +00001701EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
peahb624d8c2016-03-05 03:01:14 -08001702 return public_submodules_->echo_cancellation.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001703}
1704
1705EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
peahbb9edbd2016-03-10 12:54:25 -08001706 return public_submodules_->echo_control_mobile.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001707}
1708
1709GainControl* AudioProcessingImpl::gain_control() const {
peahbe615622016-02-13 16:40:47 -08001710 if (constants_.use_experimental_agc) {
1711 return public_submodules_->gain_control_for_experimental_agc.get();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001712 }
peahbfa97112016-03-10 21:09:04 -08001713 return public_submodules_->gain_control.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001714}
1715
1716HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
peah8271d042016-11-22 07:24:52 -08001717 return high_pass_filter_impl_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001718}
1719
1720LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001721 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001722}
1723
1724NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
solenberg5e465c32015-12-08 13:22:33 -08001725 return public_submodules_->noise_suppression.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001726}
1727
1728VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001729 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001730}
1731
peah8271d042016-11-22 07:24:52 -08001732void AudioProcessingImpl::MutateConfig(
1733 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1734 rtc::CritScope cs_render(&crit_render_);
1735 rtc::CritScope cs_capture(&crit_capture_);
1736 mutator(&config_);
1737 ApplyConfig(config_);
1738}
1739
1740AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1741 rtc::CritScope cs_render(&crit_render_);
1742 rtc::CritScope cs_capture(&crit_capture_);
1743 return config_;
1744}
1745
peah2ace3f92016-09-10 04:42:27 -07001746bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1747 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001748 config_.high_pass_filter.enabled,
peah2ace3f92016-09-10 04:42:27 -07001749 public_submodules_->echo_cancellation->is_enabled(),
1750 public_submodules_->echo_control_mobile->is_enabled(),
ivoc9f4a4a02016-10-28 05:39:16 -07001751 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001752 public_submodules_->noise_suppression->is_enabled(),
1753 capture_nonlocked_.intelligibility_enabled,
1754 capture_nonlocked_.beamformer_enabled,
1755 public_submodules_->gain_control->is_enabled(),
alessiob3ec96df2017-05-22 06:57:06 -07001756 capture_nonlocked_.gain_controller2_enabled,
peah2ace3f92016-09-10 04:42:27 -07001757 capture_nonlocked_.level_controller_enabled,
peahe0eae3c2016-12-14 01:16:23 -08001758 capture_nonlocked_.echo_canceller3_enabled,
peah2ace3f92016-09-10 04:42:27 -07001759 public_submodules_->voice_detection->is_enabled(),
1760 public_submodules_->level_estimator->is_enabled(),
1761 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001762}
1763
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001764
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001765void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001766 if (capture_.transient_suppressor_enabled) {
1767 if (!public_submodules_->transient_suppressor.get()) {
1768 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001769 }
peahdf3efa82015-11-28 12:35:15 -08001770 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001771 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1772 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001773 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001774}
1775
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001776void AudioProcessingImpl::InitializeBeamformer() {
aluebsb2328d12016-01-11 20:32:29 -08001777 if (capture_nonlocked_.beamformer_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001778 if (!private_submodules_->beamformer) {
1779 private_submodules_->beamformer.reset(new NonlinearBeamformer(
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001780 capture_.array_geometry, 1u, capture_.target_direction));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001781 }
peahdf3efa82015-11-28 12:35:15 -08001782 private_submodules_->beamformer->Initialize(kChunkSizeMs,
1783 capture_nonlocked_.split_rate);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001784 }
1785}
1786
ekmeyerson60d9b332015-08-14 10:35:55 -07001787void AudioProcessingImpl::InitializeIntelligibility() {
peah1bcfce52016-08-26 07:16:04 -07001788#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001789 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001790 public_submodules_->intelligibility_enhancer.reset(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -08001791 new IntelligibilityEnhancer(capture_nonlocked_.split_rate,
Alex Luebs57ae8292016-03-09 16:24:34 +01001792 render_.render_audio->num_channels(),
Alejandro Luebsef009252016-09-20 14:51:56 -07001793 render_.render_audio->num_bands(),
Alex Luebs57ae8292016-03-09 16:24:34 +01001794 NoiseSuppressionImpl::num_noise_bins()));
ekmeyerson60d9b332015-08-14 10:35:55 -07001795 }
peah1bcfce52016-08-26 07:16:04 -07001796#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001797}
1798
peah8271d042016-11-22 07:24:52 -08001799void AudioProcessingImpl::InitializeLowCutFilter() {
1800 if (config_.high_pass_filter.enabled) {
1801 private_submodules_->low_cut_filter.reset(
1802 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1803 } else {
1804 private_submodules_->low_cut_filter.reset();
1805 }
1806}
alessiob3ec96df2017-05-22 06:57:06 -07001807
peahe0eae3c2016-12-14 01:16:23 -08001808void AudioProcessingImpl::InitializeEchoCanceller3() {
1809 if (capture_nonlocked_.echo_canceller3_enabled) {
1810 private_submodules_->echo_canceller3.reset(
1811 new EchoCanceller3(proc_sample_rate_hz(), true));
1812 } else {
1813 private_submodules_->echo_canceller3.reset();
1814 }
1815}
peah8271d042016-11-22 07:24:52 -08001816
alessiob3ec96df2017-05-22 06:57:06 -07001817void AudioProcessingImpl::InitializeGainController2() {
1818 if (capture_nonlocked_.gain_controller2_enabled) {
1819 private_submodules_->gain_controller2.reset(
1820 new GainController2(proc_sample_rate_hz()));
1821 } else {
1822 private_submodules_->gain_controller2.reset();
1823 }
1824}
1825
peahca4cac72016-06-29 15:26:12 -07001826void AudioProcessingImpl::InitializeLevelController() {
1827 private_submodules_->level_controller->Initialize(proc_sample_rate_hz());
1828}
1829
ivoc9f4a4a02016-10-28 05:39:16 -07001830void AudioProcessingImpl::InitializeResidualEchoDetector() {
1831 private_submodules_->residual_echo_detector->Initialize();
1832}
1833
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001834void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001835 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001836
1837 if (echo_cancellation()->is_enabled()) {
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001838 // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
1839 // If a stream has echo we know that the echo_cancellation is in process.
peahdf3efa82015-11-28 12:35:15 -08001840 if (capture_.stream_delay_jumps == -1 &&
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001841 echo_cancellation()->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001842 capture_.stream_delay_jumps = 0;
1843 }
1844 if (capture_.aec_system_delay_jumps == -1 &&
1845 echo_cancellation()->stream_has_echo()) {
1846 capture_.aec_system_delay_jumps = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001847 }
1848
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001849 // Detect a jump in platform reported system delay and log the difference.
peahdf3efa82015-11-28 12:35:15 -08001850 const int diff_stream_delay_ms =
1851 capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
1852 if (diff_stream_delay_ms > kMinDiffDelayMs &&
1853 capture_.last_stream_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001854 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1855 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
peahdf3efa82015-11-28 12:35:15 -08001856 if (capture_.stream_delay_jumps == -1) {
1857 capture_.stream_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001858 }
peahdf3efa82015-11-28 12:35:15 -08001859 capture_.stream_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001860 }
peahdf3efa82015-11-28 12:35:15 -08001861 capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001862
1863 // Detect a jump in AEC system delay and log the difference.
peah20028c42016-03-04 11:50:54 -08001864 const int samples_per_ms =
peahdf3efa82015-11-28 12:35:15 -08001865 rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
peah20028c42016-03-04 11:50:54 -08001866 RTC_DCHECK_LT(0, samples_per_ms);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001867 const int aec_system_delay_ms =
peah20028c42016-03-04 11:50:54 -08001868 public_submodules_->echo_cancellation->GetSystemDelayInSamples() /
1869 samples_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001870 const int diff_aec_system_delay_ms =
peahdf3efa82015-11-28 12:35:15 -08001871 aec_system_delay_ms - capture_.last_aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001872 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
peahdf3efa82015-11-28 12:35:15 -08001873 capture_.last_aec_system_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001874 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1875 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1876 100);
peahdf3efa82015-11-28 12:35:15 -08001877 if (capture_.aec_system_delay_jumps == -1) {
1878 capture_.aec_system_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001879 }
peahdf3efa82015-11-28 12:35:15 -08001880 capture_.aec_system_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001881 }
peahdf3efa82015-11-28 12:35:15 -08001882 capture_.last_aec_system_delay_ms = aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001883 }
1884}
1885
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001886void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
peahdf3efa82015-11-28 12:35:15 -08001887 // Run in a single-threaded manner.
1888 rtc::CritScope cs_render(&crit_render_);
1889 rtc::CritScope cs_capture(&crit_capture_);
1890
1891 if (capture_.stream_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001892 RTC_HISTOGRAM_ENUMERATION(
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001893 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
peahdf3efa82015-11-28 12:35:15 -08001894 capture_.stream_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001895 }
peahdf3efa82015-11-28 12:35:15 -08001896 capture_.stream_delay_jumps = -1;
1897 capture_.last_stream_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001898
peahdf3efa82015-11-28 12:35:15 -08001899 if (capture_.aec_system_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001900 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1901 capture_.aec_system_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001902 }
peahdf3efa82015-11-28 12:35:15 -08001903 capture_.aec_system_delay_jumps = -1;
1904 capture_.last_aec_system_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001905}
1906
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001907#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
peahdf3efa82015-11-28 12:35:15 -08001908int AudioProcessingImpl::WriteMessageToDebugFile(
1909 FileWrapper* debug_file,
ivocd66b44d2016-01-15 03:06:36 -08001910 int64_t* filesize_limit_bytes,
peahdf3efa82015-11-28 12:35:15 -08001911 rtc::CriticalSection* crit_debug,
1912 ApmDebugDumpThreadState* debug_state) {
1913 int32_t size = debug_state->event_msg->ByteSize();
ajm@google.com808e0e02011-08-03 21:08:51 +00001914 if (size <= 0) {
1915 return kUnspecifiedError;
1916 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001917#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Michael Graczyk86c6d332015-07-23 11:41:39 -07001918// TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1919// pretty safe in assuming little-endian.
ajm@google.com808e0e02011-08-03 21:08:51 +00001920#endif
1921
peahdf3efa82015-11-28 12:35:15 -08001922 if (!debug_state->event_msg->SerializeToString(&debug_state->event_str)) {
ajm@google.com808e0e02011-08-03 21:08:51 +00001923 return kUnspecifiedError;
1924 }
1925
peahdf3efa82015-11-28 12:35:15 -08001926 {
1927 // Ensure atomic writes of the message.
ivocd66b44d2016-01-15 03:06:36 -08001928 rtc::CritScope cs_debug(crit_debug);
1929
tommia6219cc2016-06-15 10:30:14 -07001930 RTC_DCHECK(debug_file->is_open());
ivocd66b44d2016-01-15 03:06:36 -08001931 // Update the byte counter.
1932 if (*filesize_limit_bytes >= 0) {
1933 *filesize_limit_bytes -=
1934 (sizeof(int32_t) + debug_state->event_str.length());
1935 if (*filesize_limit_bytes < 0) {
1936 // Not enough bytes are left to write this message, so stop logging.
1937 debug_file->CloseFile();
1938 return kNoError;
1939 }
1940 }
peahdf3efa82015-11-28 12:35:15 -08001941 // Write message preceded by its size.
1942 if (!debug_file->Write(&size, sizeof(int32_t))) {
1943 return kFileError;
1944 }
1945 if (!debug_file->Write(debug_state->event_str.data(),
1946 debug_state->event_str.length())) {
1947 return kFileError;
1948 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001949 }
1950
peahdf3efa82015-11-28 12:35:15 -08001951 debug_state->event_msg->Clear();
ajm@google.com808e0e02011-08-03 21:08:51 +00001952
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001953 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001954}
1955
1956int AudioProcessingImpl::WriteInitMessage() {
peahdf3efa82015-11-28 12:35:15 -08001957 debug_dump_.capture.event_msg->set_type(audioproc::Event::INIT);
1958 audioproc::Init* msg = debug_dump_.capture.event_msg->mutable_init();
1959 msg->set_sample_rate(formats_.api_format.input_stream().sample_rate_hz());
ajm@google.com808e0e02011-08-03 21:08:51 +00001960
mbonadei7c2c8432017-04-07 00:59:12 -07001961 msg->set_num_input_channels(static_cast<int32_t>(
Peter Kasting69558702016-01-12 16:26:35 -08001962 formats_.api_format.input_stream().num_channels()));
mbonadei7c2c8432017-04-07 00:59:12 -07001963 msg->set_num_output_channels(static_cast<int32_t>(
Peter Kasting69558702016-01-12 16:26:35 -08001964 formats_.api_format.output_stream().num_channels()));
mbonadei7c2c8432017-04-07 00:59:12 -07001965 msg->set_num_reverse_channels(static_cast<int32_t>(
Peter Kasting69558702016-01-12 16:26:35 -08001966 formats_.api_format.reverse_input_stream().num_channels()));
peahdf3efa82015-11-28 12:35:15 -08001967 msg->set_reverse_sample_rate(
1968 formats_.api_format.reverse_input_stream().sample_rate_hz());
1969 msg->set_output_sample_rate(
1970 formats_.api_format.output_stream().sample_rate_hz());
peahc7bdf8a2016-04-11 07:05:53 -07001971 msg->set_reverse_output_sample_rate(
1972 formats_.api_format.reverse_output_stream().sample_rate_hz());
1973 msg->set_num_reverse_output_channels(
1974 formats_.api_format.reverse_output_stream().num_channels());
peahdf3efa82015-11-28 12:35:15 -08001975
1976 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001977 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001978 &crit_debug_, &debug_dump_.capture));
Minyue13b96ba2015-10-03 00:39:14 +02001979 return kNoError;
1980}
1981
1982int AudioProcessingImpl::WriteConfigMessage(bool forced) {
1983 audioproc::Config config;
1984
peahdf3efa82015-11-28 12:35:15 -08001985 config.set_aec_enabled(public_submodules_->echo_cancellation->is_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001986 config.set_aec_delay_agnostic_enabled(
peahdf3efa82015-11-28 12:35:15 -08001987 public_submodules_->echo_cancellation->is_delay_agnostic_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001988 config.set_aec_drift_compensation_enabled(
peahdf3efa82015-11-28 12:35:15 -08001989 public_submodules_->echo_cancellation->is_drift_compensation_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001990 config.set_aec_extended_filter_enabled(
peahdf3efa82015-11-28 12:35:15 -08001991 public_submodules_->echo_cancellation->is_extended_filter_enabled());
1992 config.set_aec_suppression_level(static_cast<int>(
1993 public_submodules_->echo_cancellation->suppression_level()));
Minyue13b96ba2015-10-03 00:39:14 +02001994
peahdf3efa82015-11-28 12:35:15 -08001995 config.set_aecm_enabled(
1996 public_submodules_->echo_control_mobile->is_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001997 config.set_aecm_comfort_noise_enabled(
peahdf3efa82015-11-28 12:35:15 -08001998 public_submodules_->echo_control_mobile->is_comfort_noise_enabled());
1999 config.set_aecm_routing_mode(static_cast<int>(
2000 public_submodules_->echo_control_mobile->routing_mode()));
Minyue13b96ba2015-10-03 00:39:14 +02002001
peahdf3efa82015-11-28 12:35:15 -08002002 config.set_agc_enabled(public_submodules_->gain_control->is_enabled());
2003 config.set_agc_mode(
2004 static_cast<int>(public_submodules_->gain_control->mode()));
2005 config.set_agc_limiter_enabled(
2006 public_submodules_->gain_control->is_limiter_enabled());
peahbe615622016-02-13 16:40:47 -08002007 config.set_noise_robust_agc_enabled(constants_.use_experimental_agc);
Minyue13b96ba2015-10-03 00:39:14 +02002008
peah8271d042016-11-22 07:24:52 -08002009 config.set_hpf_enabled(config_.high_pass_filter.enabled);
Minyue13b96ba2015-10-03 00:39:14 +02002010
peahdf3efa82015-11-28 12:35:15 -08002011 config.set_ns_enabled(public_submodules_->noise_suppression->is_enabled());
2012 config.set_ns_level(
2013 static_cast<int>(public_submodules_->noise_suppression->level()));
Minyue13b96ba2015-10-03 00:39:14 +02002014
peahdf3efa82015-11-28 12:35:15 -08002015 config.set_transient_suppression_enabled(
2016 capture_.transient_suppressor_enabled);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07002017 config.set_intelligibility_enhancer_enabled(
2018 capture_nonlocked_.intelligibility_enabled);
Minyue13b96ba2015-10-03 00:39:14 +02002019
peah7789fe72016-04-15 01:19:44 -07002020 std::string experiments_description =
2021 public_submodules_->echo_cancellation->GetExperimentsDescription();
2022 // TODO(peah): Add semicolon-separated concatenations of experiment
2023 // descriptions for other submodules.
peahca4cac72016-06-29 15:26:12 -07002024 if (capture_nonlocked_.level_controller_enabled) {
2025 experiments_description += "LevelController;";
2026 }
henrik.lundinbd681b92016-12-05 09:08:42 -08002027 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
2028 experiments_description += "AgcClippingLevelExperiment;";
2029 }
peahe0eae3c2016-12-14 01:16:23 -08002030 if (capture_nonlocked_.echo_canceller3_enabled) {
2031 experiments_description += "EchoCanceller3;";
2032 }
peah7789fe72016-04-15 01:19:44 -07002033 config.set_experiments_description(experiments_description);
2034
mbonadei7c2c8432017-04-07 00:59:12 -07002035 ProtoString serialized_config = config.SerializeAsString();
peahdf3efa82015-11-28 12:35:15 -08002036 if (!forced &&
2037 debug_dump_.capture.last_serialized_config == serialized_config) {
Minyue13b96ba2015-10-03 00:39:14 +02002038 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00002039 }
2040
peahdf3efa82015-11-28 12:35:15 -08002041 debug_dump_.capture.last_serialized_config = serialized_config;
Minyue13b96ba2015-10-03 00:39:14 +02002042
peahdf3efa82015-11-28 12:35:15 -08002043 debug_dump_.capture.event_msg->set_type(audioproc::Event::CONFIG);
2044 debug_dump_.capture.event_msg->mutable_config()->CopyFrom(config);
Minyue13b96ba2015-10-03 00:39:14 +02002045
peahdf3efa82015-11-28 12:35:15 -08002046 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08002047 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08002048 &crit_debug_, &debug_dump_.capture));
ajm@google.com808e0e02011-08-03 21:08:51 +00002049 return kNoError;
2050}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00002051#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002052
kwiberg83ffe452016-08-29 14:46:07 -07002053AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
2054 bool transient_suppressor_enabled,
2055 const std::vector<Point>& array_geometry,
2056 SphericalPointf target_direction)
2057 : aec_system_delay_jumps(-1),
2058 delay_offset_ms(0),
2059 was_stream_delay_set(false),
2060 last_stream_delay_ms(0),
2061 last_aec_system_delay_ms(0),
2062 stream_delay_jumps(-1),
2063 output_will_be_muted(false),
2064 key_pressed(false),
2065 transient_suppressor_enabled(transient_suppressor_enabled),
2066 array_geometry(array_geometry),
2067 target_direction(target_direction),
peahde65ddc2016-09-16 15:02:15 -07002068 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002069 split_rate(kSampleRate16kHz),
2070 previous_agc_level(0),
2071 echo_path_gain_change(false) {}
kwiberg83ffe452016-08-29 14:46:07 -07002072
2073AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2074
2075AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2076
2077AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2078
niklase@google.com470e71d2011-07-07 08:21:25 +00002079} // namespace webrtc