blob: 6d464901dc44f2cc664e5d671d51f94c0a24c29e [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_audio/audio_converter.h"
18#include "common_audio/channel_buffer.h"
19#include "common_audio/include/audio_util.h"
20#include "common_audio/signal_processing/include/signal_processing_library.h"
21#include "modules/audio_processing/aec/aec_core.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_processing/agc/agc_manager_direct.h"
Alex Loikob5c9a792018-04-16 16:31:22 +020023#include "modules/audio_processing/agc2/gain_applier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/audio_buffer.h"
25#include "modules/audio_processing/beamformer/nonlinear_beamformer.h"
26#include "modules/audio_processing/common.h"
27#include "modules/audio_processing/echo_cancellation_impl.h"
28#include "modules/audio_processing/echo_control_mobile_impl.h"
29#include "modules/audio_processing/gain_control_for_experimental_agc.h"
30#include "modules/audio_processing/gain_control_impl.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010031#include "modules/audio_processing/gain_controller2.h"
Per Åhgren13735822018-02-12 21:42:56 +010032#include "modules/audio_processing/logging/apm_data_dumper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/checks.h"
34#include "rtc_base/logging.h"
35#include "rtc_base/platform_file.h"
Niels Möller84255bb2017-10-06 13:43:23 +020036#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "rtc_base/trace_event.h"
peah1bcfce52016-08-26 07:16:04 -070038#if WEBRTC_INTELLIGIBILITY_ENHANCER
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "modules/audio_processing/intelligibility/intelligibility_enhancer.h"
peah1bcfce52016-08-26 07:16:04 -070040#endif
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "modules/audio_processing/level_estimator_impl.h"
42#include "modules/audio_processing/low_cut_filter.h"
43#include "modules/audio_processing/noise_suppression_impl.h"
44#include "modules/audio_processing/residual_echo_detector.h"
45#include "modules/audio_processing/transient/transient_suppressor.h"
46#include "modules/audio_processing/voice_detection_impl.h"
Per Åhgren13735822018-02-12 21:42:56 +010047#include "rtc_base/atomicops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020048#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000049
peah1bcfce52016-08-26 07:16:04 -070050// Check to verify that the define for the intelligibility enhancer is properly
51// set.
52#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
53 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
54 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
55#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
56#endif
57
Michael Graczyk86c6d332015-07-23 11:41:39 -070058#define RETURN_ON_ERR(expr) \
59 do { \
60 int err = (expr); \
61 if (err != kNoError) { \
62 return err; \
63 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000064 } while (0)
65
niklase@google.com470e71d2011-07-07 08:21:25 +000066namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070067
kwibergd59d3bb2016-09-13 07:49:33 -070068constexpr int AudioProcessing::kNativeSampleRatesHz[];
Alex Loiko73ec0192018-05-15 10:52:28 +020069constexpr int kRuntimeSettingQueueSize = 100;
aluebsdf6416a2016-03-16 18:26:35 -070070
Michael Graczyk86c6d332015-07-23 11:41:39 -070071namespace {
72
73static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
74 switch (layout) {
75 case AudioProcessing::kMono:
76 case AudioProcessing::kStereo:
77 return false;
78 case AudioProcessing::kMonoAndKeyboard:
79 case AudioProcessing::kStereoAndKeyboard:
80 return true;
81 }
82
kwiberg9e2be5f2016-09-14 05:23:22 -070083 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070084 return false;
85}
aluebsdf6416a2016-03-16 18:26:35 -070086
peah2ace3f92016-09-10 04:42:27 -070087bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070088 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
89 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
90}
91
peah2ace3f92016-09-10 04:42:27 -070092int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) {
93#ifdef WEBRTC_ARCH_ARM_FAMILY
kwibergd59d3bb2016-09-13 07:49:33 -070094 constexpr int kMaxSplittingNativeProcessRate =
95 AudioProcessing::kSampleRate32kHz;
peah2ace3f92016-09-10 04:42:27 -070096#else
kwibergd59d3bb2016-09-13 07:49:33 -070097 constexpr int kMaxSplittingNativeProcessRate =
98 AudioProcessing::kSampleRate48kHz;
peah2ace3f92016-09-10 04:42:27 -070099#endif
kwibergd59d3bb2016-09-13 07:49:33 -0700100 static_assert(
101 kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz,
102 "");
peah2ace3f92016-09-10 04:42:27 -0700103 const int uppermost_native_rate = band_splitting_required
104 ? kMaxSplittingNativeProcessRate
105 : AudioProcessing::kSampleRate48kHz;
106
107 for (auto rate : AudioProcessing::kNativeSampleRatesHz) {
108 if (rate >= uppermost_native_rate) {
109 return uppermost_native_rate;
110 }
111 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700112 return rate;
113 }
114 }
peah2ace3f92016-09-10 04:42:27 -0700115 RTC_NOTREACHED();
116 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700117}
118
peah9e6a2902017-05-15 07:19:21 -0700119// Maximum lengths that frame of samples being passed from the render side to
120// the capture side can have (does not apply to AEC3).
121static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
122static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
123
peah764e3642016-10-22 05:04:30 -0700124// Maximum number of frames to buffer in the render queue.
125// TODO(peah): Decrease this once we properly handle hugely unbalanced
126// reverse and forward call numbers.
127static const size_t kMaxNumFramesToBuffer = 100;
128
peah8271d042016-11-22 07:24:52 -0800129class HighPassFilterImpl : public HighPassFilter {
130 public:
131 explicit HighPassFilterImpl(AudioProcessingImpl* apm) : apm_(apm) {}
132 ~HighPassFilterImpl() override = default;
133
134 // HighPassFilter implementation.
135 int Enable(bool enable) override {
136 apm_->MutateConfig([enable](AudioProcessing::Config* config) {
137 config->high_pass_filter.enabled = enable;
138 });
139
140 return AudioProcessing::kNoError;
141 }
142
143 bool is_enabled() const override {
144 return apm_->GetConfig().high_pass_filter.enabled;
145 }
146
147 private:
148 AudioProcessingImpl* apm_;
149 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(HighPassFilterImpl);
150};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700151} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000152
153// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000154static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000155
Sam Zackrisson0beac582017-09-25 12:04:02 +0200156AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100157 bool capture_post_processor_enabled,
158 bool render_pre_processor_enabled)
159 : capture_post_processor_enabled_(capture_post_processor_enabled),
160 render_pre_processor_enabled_(render_pre_processor_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700161
162bool AudioProcessingImpl::ApmSubmoduleStates::Update(
peah8271d042016-11-22 07:24:52 -0800163 bool low_cut_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700164 bool echo_canceller_enabled,
165 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700166 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700167 bool noise_suppressor_enabled,
168 bool intelligibility_enhancer_enabled,
169 bool beamformer_enabled,
170 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700171 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200172 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200173 bool echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -0700174 bool voice_activity_detector_enabled,
175 bool level_estimator_enabled,
176 bool transient_suppressor_enabled) {
177 bool changed = false;
peah8271d042016-11-22 07:24:52 -0800178 changed |= (low_cut_filter_enabled != low_cut_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700179 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
180 changed |=
181 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700182 changed |=
183 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700184 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
185 changed |=
186 (intelligibility_enhancer_enabled != intelligibility_enhancer_enabled_);
187 changed |= (beamformer_enabled != beamformer_enabled_);
188 changed |=
189 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
alessiob3ec96df2017-05-22 06:57:06 -0700190 changed |=
191 (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200192 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200193 changed |= (echo_controller_enabled != echo_controller_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700194 changed |= (level_estimator_enabled != level_estimator_enabled_);
195 changed |=
196 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
197 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
198 if (changed) {
peah8271d042016-11-22 07:24:52 -0800199 low_cut_filter_enabled_ = low_cut_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700200 echo_canceller_enabled_ = echo_canceller_enabled;
201 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700202 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700203 noise_suppressor_enabled_ = noise_suppressor_enabled;
204 intelligibility_enhancer_enabled_ = intelligibility_enhancer_enabled;
205 beamformer_enabled_ = beamformer_enabled;
206 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700207 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200208 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200209 echo_controller_enabled_ = echo_controller_enabled;
peah2ace3f92016-09-10 04:42:27 -0700210 level_estimator_enabled_ = level_estimator_enabled;
211 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
212 transient_suppressor_enabled_ = transient_suppressor_enabled;
213 }
214
215 changed |= first_update_;
216 first_update_ = false;
217 return changed;
218}
219
220bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
221 const {
222#if WEBRTC_INTELLIGIBILITY_ENHANCER
223 return CaptureMultiBandProcessingActive() ||
peah52775842017-05-16 06:14:09 -0700224 intelligibility_enhancer_enabled_ || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700225#else
peah52775842017-05-16 06:14:09 -0700226 return CaptureMultiBandProcessingActive() || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700227#endif
228}
229
230bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
231 const {
peah8271d042016-11-22 07:24:52 -0800232 return low_cut_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700233 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
peahe0eae3c2016-12-14 01:16:23 -0800234 beamformer_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200235 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700236}
237
peah23ac8b42017-05-23 05:33:56 -0700238bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
239 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200240 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
241 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700242}
243
peah2ace3f92016-09-10 04:42:27 -0700244bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
245 const {
246 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800247 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200248 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700249}
250
Alex Loiko5825aa62017-12-18 16:02:40 +0100251bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive()
252 const {
253 return render_pre_processor_enabled_;
254}
255
peah2ace3f92016-09-10 04:42:27 -0700256bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
257 const {
258#if WEBRTC_INTELLIGIBILITY_ENHANCER
259 return intelligibility_enhancer_enabled_;
260#else
261 return false;
262#endif
263}
264
solenberg5e465c32015-12-08 13:22:33 -0800265struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800266 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800267 // Accessed externally of APM without any lock acquired.
peahb624d8c2016-03-05 03:01:14 -0800268 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
peahbb9edbd2016-03-10 12:54:25 -0800269 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
peahbfa97112016-03-10 21:09:04 -0800270 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800271 std::unique_ptr<LevelEstimatorImpl> level_estimator;
272 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
273 std::unique_ptr<VoiceDetectionImpl> voice_detection;
274 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800275 gain_control_for_experimental_agc;
solenberg5e465c32015-12-08 13:22:33 -0800276
277 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800278 std::unique_ptr<TransientSuppressor> transient_suppressor;
peah1bcfce52016-08-26 07:16:04 -0700279#if WEBRTC_INTELLIGIBILITY_ENHANCER
kwiberg88788ad2016-02-19 07:04:49 -0800280 std::unique_ptr<IntelligibilityEnhancer> intelligibility_enhancer;
peah1bcfce52016-08-26 07:16:04 -0700281#endif
solenberg5e465c32015-12-08 13:22:33 -0800282};
283
284struct AudioProcessingImpl::ApmPrivateSubmodules {
Sam Zackrisson0beac582017-09-25 12:04:02 +0200285 ApmPrivateSubmodules(NonlinearBeamformer* beamformer,
Alex Loiko5825aa62017-12-18 16:02:40 +0100286 std::unique_ptr<CustomProcessing> capture_post_processor,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100287 std::unique_ptr<CustomProcessing> render_pre_processor,
288 std::unique_ptr<EchoDetector> echo_detector)
Sam Zackrisson0beac582017-09-25 12:04:02 +0200289 : beamformer(beamformer),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100290 echo_detector(std::move(echo_detector)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100291 capture_post_processor(std::move(capture_post_processor)),
292 render_pre_processor(std::move(render_pre_processor)) {}
solenberg5e465c32015-12-08 13:22:33 -0800293 // Accessed internally from capture or during initialization
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700294 std::unique_ptr<NonlinearBeamformer> beamformer;
kwiberg88788ad2016-02-19 07:04:49 -0800295 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700296 std::unique_ptr<GainController2> gain_controller2;
peah8271d042016-11-22 07:24:52 -0800297 std::unique_ptr<LowCutFilter> low_cut_filter;
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100298 std::unique_ptr<EchoDetector> echo_detector;
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +0200299 std::unique_ptr<EchoControl> echo_controller;
Alex Loiko5825aa62017-12-18 16:02:40 +0100300 std::unique_ptr<CustomProcessing> capture_post_processor;
301 std::unique_ptr<CustomProcessing> render_pre_processor;
Alex Loikob5c9a792018-04-16 16:31:22 +0200302 std::unique_ptr<GainApplier> pre_amplifier;
solenberg5e465c32015-12-08 13:22:33 -0800303};
304
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100305AudioProcessingBuilder::AudioProcessingBuilder() = default;
306AudioProcessingBuilder::~AudioProcessingBuilder() = default;
307
308AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
309 std::unique_ptr<CustomProcessing> capture_post_processing) {
310 capture_post_processing_ = std::move(capture_post_processing);
311 return *this;
312}
313
314AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
315 std::unique_ptr<CustomProcessing> render_pre_processing) {
316 render_pre_processing_ = std::move(render_pre_processing);
317 return *this;
318}
319
320AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
321 std::unique_ptr<EchoControlFactory> echo_control_factory) {
322 echo_control_factory_ = std::move(echo_control_factory);
323 return *this;
324}
325
326AudioProcessingBuilder& AudioProcessingBuilder::SetNonlinearBeamformer(
327 std::unique_ptr<NonlinearBeamformer> nonlinear_beamformer) {
328 nonlinear_beamformer_ = std::move(nonlinear_beamformer);
329 return *this;
330}
331
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100332AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
333 std::unique_ptr<EchoDetector> echo_detector) {
334 echo_detector_ = std::move(echo_detector);
335 return *this;
336}
337
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100338AudioProcessing* AudioProcessingBuilder::Create() {
339 webrtc::Config config;
340 return Create(config);
341}
342
343AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100344 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
345 config, std::move(capture_post_processing_),
346 std::move(render_pre_processing_), std::move(echo_control_factory_),
347 std::move(echo_detector_), nonlinear_beamformer_.release());
348 if (apm->Initialize() != AudioProcessing::kNoError) {
349 delete apm;
350 apm = nullptr;
351 }
352 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100353}
354
peah88ac8532016-09-12 16:47:25 -0700355AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100356 : AudioProcessingImpl(config, nullptr, nullptr, nullptr, nullptr, nullptr) {
357}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000358
Per Åhgren13735822018-02-12 21:42:56 +0100359int AudioProcessingImpl::instance_count_ = 0;
360
Sam Zackrisson0beac582017-09-25 12:04:02 +0200361AudioProcessingImpl::AudioProcessingImpl(
362 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100363 std::unique_ptr<CustomProcessing> capture_post_processor,
364 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200365 std::unique_ptr<EchoControlFactory> echo_control_factory,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100366 std::unique_ptr<EchoDetector> echo_detector,
Sam Zackrisson0beac582017-09-25 12:04:02 +0200367 NonlinearBeamformer* beamformer)
Per Åhgren13735822018-02-12 21:42:56 +0100368 : data_dumper_(
369 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200370 capture_runtime_settings_(kRuntimeSettingQueueSize),
371 render_runtime_settings_(kRuntimeSettingQueueSize),
372 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
373 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Per Åhgren13735822018-02-12 21:42:56 +0100374 high_pass_filter_impl_(new HighPassFilterImpl(this)),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200375 echo_control_factory_(std::move(echo_control_factory)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100376 submodule_states_(!!capture_post_processor, !!render_pre_processor),
peah8271d042016-11-22 07:24:52 -0800377 public_submodules_(new ApmPublicSubmodules()),
Sam Zackrisson0beac582017-09-25 12:04:02 +0200378 private_submodules_(
379 new ApmPrivateSubmodules(beamformer,
Alex Loiko5825aa62017-12-18 16:02:40 +0100380 std::move(capture_post_processor),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100381 std::move(render_pre_processor),
382 std::move(echo_detector))),
peahdf3efa82015-11-28 12:35:15 -0800383 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800384 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000385#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700386 false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000387#else
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700388 config.Get<ExperimentalAgc>().enabled),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000389#endif
andrew1c7075f2015-06-24 18:14:14 -0700390#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
aluebs2a346882016-01-11 18:04:30 -0800391 capture_(false,
andrew1c7075f2015-06-24 18:14:14 -0700392#else
aluebs2a346882016-01-11 18:04:30 -0800393 capture_(config.Get<ExperimentalNs>().enabled,
andrew1c7075f2015-06-24 18:14:14 -0700394#endif
aluebs2a346882016-01-11 18:04:30 -0800395 config.Get<Beamforming>().array_geometry,
aluebsb2328d12016-01-11 20:32:29 -0800396 config.Get<Beamforming>().target_direction),
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700397 capture_nonlocked_(config.Get<Beamforming>().enabled,
peah88ac8532016-09-12 16:47:25 -0700398 config.Get<Intelligibility>().enabled) {
peahdf3efa82015-11-28 12:35:15 -0800399 {
400 rtc::CritScope cs_render(&crit_render_);
401 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000402
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200403 // Mark Echo Controller enabled if a factory is injected.
404 capture_nonlocked_.echo_controller_enabled =
405 static_cast<bool>(echo_control_factory_);
406
peahb624d8c2016-03-05 03:01:14 -0800407 public_submodules_->echo_cancellation.reset(
peahb58a1582016-03-15 09:34:24 -0700408 new EchoCancellationImpl(&crit_render_, &crit_capture_));
peahbb9edbd2016-03-10 12:54:25 -0800409 public_submodules_->echo_control_mobile.reset(
peah253534d2016-03-15 04:32:28 -0700410 new EchoControlMobileImpl(&crit_render_, &crit_capture_));
peahbfa97112016-03-10 21:09:04 -0800411 public_submodules_->gain_control.reset(
peahb8fbb542016-03-15 02:28:08 -0700412 new GainControlImpl(&crit_capture_, &crit_capture_));
solenberg949028f2015-12-15 11:39:38 -0800413 public_submodules_->level_estimator.reset(
414 new LevelEstimatorImpl(&crit_capture_));
solenberg5e465c32015-12-08 13:22:33 -0800415 public_submodules_->noise_suppression.reset(
416 new NoiseSuppressionImpl(&crit_capture_));
solenberga29386c2015-12-16 03:31:12 -0800417 public_submodules_->voice_detection.reset(
418 new VoiceDetectionImpl(&crit_capture_));
peahbe615622016-02-13 16:40:47 -0800419 public_submodules_->gain_control_for_experimental_agc.reset(
peahbfa97112016-03-10 21:09:04 -0800420 new GainControlForExperimentalAgc(
421 public_submodules_->gain_control.get(), &crit_capture_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100422
423 // If no echo detector is injected, use the ResidualEchoDetector.
424 if (!private_submodules_->echo_detector) {
425 private_submodules_->echo_detector.reset(new ResidualEchoDetector());
426 }
peahca4cac72016-06-29 15:26:12 -0700427
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200428 // TODO(alessiob): Move the injected gain controller once injection is
429 // implemented.
430 private_submodules_->gain_controller2.reset(new GainController2());
431
Mirko Bonadei675513b2017-11-09 11:09:25 +0100432 RTC_LOG(LS_INFO) << "Capture post processor activated: "
Jonas Olsson645b0272018-02-15 15:16:27 +0100433 << !!private_submodules_->capture_post_processor
434 << "\nRender pre processor activated: "
Alex Loiko5825aa62017-12-18 16:02:40 +0100435 << !!private_submodules_->render_pre_processor;
peahdf3efa82015-11-28 12:35:15 -0800436 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000437
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000438 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
441AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800442 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800443 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800444 private_submodules_->agc_manager.reset();
445 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800446 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
niklase@google.com470e71d2011-07-07 08:21:25 +0000449int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800450 // Run in a single-threaded manner during initialization.
451 rtc::CritScope cs_render(&crit_render_);
452 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000453 return InitializeLocked();
454}
455
peahde65ddc2016-09-16 15:02:15 -0700456int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
457 int capture_output_sample_rate_hz,
458 int render_input_sample_rate_hz,
459 ChannelLayout capture_input_layout,
460 ChannelLayout capture_output_layout,
461 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700462 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700463 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
464 LayoutHasKeyboard(capture_input_layout)},
465 {capture_output_sample_rate_hz,
466 ChannelsFromLayout(capture_output_layout),
467 LayoutHasKeyboard(capture_output_layout)},
468 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
469 LayoutHasKeyboard(render_input_layout)},
470 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
471 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700472
473 return Initialize(processing_config);
474}
475
476int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800477 // Run in a single-threaded manner during initialization.
478 rtc::CritScope cs_render(&crit_render_);
479 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700480 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000481}
482
peahdf3efa82015-11-28 12:35:15 -0800483int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800484 const ProcessingConfig& processing_config) {
peah2ace3f92016-09-10 04:42:27 -0700485 return MaybeInitialize(processing_config, false);
peah81b9bfe2015-11-27 02:47:28 -0800486}
487
peahdf3efa82015-11-28 12:35:15 -0800488int AudioProcessingImpl::MaybeInitializeCapture(
peah2ace3f92016-09-10 04:42:27 -0700489 const ProcessingConfig& processing_config,
490 bool force_initialization) {
491 return MaybeInitialize(processing_config, force_initialization);
peah81b9bfe2015-11-27 02:47:28 -0800492}
493
peah192164e2015-11-17 02:16:45 -0800494// Calls InitializeLocked() if any of the audio parameters have changed from
peahdf3efa82015-11-28 12:35:15 -0800495// their current values (needs to be called while holding the crit_render_lock).
496int AudioProcessingImpl::MaybeInitialize(
peah2ace3f92016-09-10 04:42:27 -0700497 const ProcessingConfig& processing_config,
498 bool force_initialization) {
peahdf3efa82015-11-28 12:35:15 -0800499 // Called from both threads. Thread check is therefore not possible.
peah2ace3f92016-09-10 04:42:27 -0700500 if (processing_config == formats_.api_format && !force_initialization) {
peah192164e2015-11-17 02:16:45 -0800501 return kNoError;
502 }
peahdf3efa82015-11-28 12:35:15 -0800503
504 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800505 return InitializeLocked(processing_config);
506}
507
niklase@google.com470e71d2011-07-07 08:21:25 +0000508int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200509 UpdateActiveSubmoduleStates();
510
peah522d71b2017-02-23 05:16:26 -0800511 const int capture_audiobuffer_num_channels =
512 capture_nonlocked_.beamformer_enabled
513 ? formats_.api_format.input_stream().num_channels()
514 : formats_.api_format.output_stream().num_channels();
515
peahde65ddc2016-09-16 15:02:15 -0700516 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800517 formats_.api_format.reverse_output_stream().num_frames() == 0
peahde65ddc2016-09-16 15:02:15 -0700518 ? formats_.render_processing_format.num_frames()
peahdf3efa82015-11-28 12:35:15 -0800519 : formats_.api_format.reverse_output_stream().num_frames();
520 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
521 render_.render_audio.reset(new AudioBuffer(
522 formats_.api_format.reverse_input_stream().num_frames(),
523 formats_.api_format.reverse_input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700524 formats_.render_processing_format.num_frames(),
525 formats_.render_processing_format.num_channels(),
526 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700527 if (formats_.api_format.reverse_input_stream() !=
528 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800529 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800530 formats_.api_format.reverse_input_stream().num_channels(),
531 formats_.api_format.reverse_input_stream().num_frames(),
532 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800533 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700534 } else {
peahdf3efa82015-11-28 12:35:15 -0800535 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700536 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700537 } else {
peahdf3efa82015-11-28 12:35:15 -0800538 render_.render_audio.reset(nullptr);
539 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700540 }
peahce4d9152017-05-19 01:28:05 -0700541
peahdf3efa82015-11-28 12:35:15 -0800542 capture_.capture_audio.reset(
543 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
544 formats_.api_format.input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700545 capture_nonlocked_.capture_processing_format.num_frames(),
546 capture_audiobuffer_num_channels,
peahdf3efa82015-11-28 12:35:15 -0800547 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000548
peahde65ddc2016-09-16 15:02:15 -0700549 public_submodules_->echo_cancellation->Initialize(
550 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
551 num_proc_channels());
peah764e3642016-10-22 05:04:30 -0700552 AllocateRenderQueue();
553
ivoc3e9a5372016-10-28 07:55:33 -0700554 int success = public_submodules_->echo_cancellation->enable_metrics(true);
555 RTC_DCHECK_EQ(0, success);
556 success = public_submodules_->echo_cancellation->enable_delay_logging(true);
557 RTC_DCHECK_EQ(0, success);
peahde65ddc2016-09-16 15:02:15 -0700558 public_submodules_->echo_control_mobile->Initialize(
559 proc_split_sample_rate_hz(), num_reverse_channels(),
560 num_output_channels());
peah135259a2016-10-28 03:12:11 -0700561
562 public_submodules_->gain_control->Initialize(num_proc_channels(),
563 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700564 if (constants_.use_experimental_agc) {
565 if (!private_submodules_->agc_manager.get()) {
566 private_submodules_->agc_manager.reset(new AgcManagerDirect(
567 public_submodules_->gain_control.get(),
568 public_submodules_->gain_control_for_experimental_agc.get(),
henrik.lundinbd681b92016-12-05 09:08:42 -0800569 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min));
peahde65ddc2016-09-16 15:02:15 -0700570 }
571 private_submodules_->agc_manager->Initialize();
572 private_submodules_->agc_manager->SetCaptureMuted(
573 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700574 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700575 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200576 InitializeTransient();
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000577 InitializeBeamformer();
peah1bcfce52016-08-26 07:16:04 -0700578#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -0700579 InitializeIntelligibility();
peah1bcfce52016-08-26 07:16:04 -0700580#endif
peah8271d042016-11-22 07:24:52 -0800581 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700582 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
583 proc_sample_rate_hz());
584 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
585 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700586 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200587 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700588 InitializeGainController2();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200589 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100590 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800591
aleloi868f32f2017-05-23 07:20:05 -0700592 if (aec_dump_) {
Alex Loiko1aec5942018-05-15 13:13:22 +0200593 aec_dump_->WriteInitMessage(formats_.api_format);
aleloi868f32f2017-05-23 07:20:05 -0700594 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000595 return kNoError;
596}
597
Michael Graczyk86c6d332015-07-23 11:41:39 -0700598int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200599 UpdateActiveSubmoduleStates();
600
Michael Graczyk86c6d332015-07-23 11:41:39 -0700601 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700602 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
603 return kBadSampleRateError;
604 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000605 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700606
Peter Kasting69558702016-01-12 16:26:35 -0800607 const size_t num_in_channels = config.input_stream().num_channels();
608 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700609
610 // Need at least one input channel.
611 // Need either one output channel or as many outputs as there are inputs.
612 if (num_in_channels == 0 ||
613 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700614 return kBadNumberChannelsError;
615 }
616
aluebsb2328d12016-01-11 20:32:29 -0800617 if (capture_nonlocked_.beamformer_enabled &&
Peter Kasting69558702016-01-12 16:26:35 -0800618 num_in_channels != capture_.array_geometry.size()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700619 return kBadNumberChannelsError;
620 }
621
peahdf3efa82015-11-28 12:35:15 -0800622 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000623
peahde65ddc2016-09-16 15:02:15 -0700624 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700625 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700626 formats_.api_format.output_stream().sample_rate_hz()),
627 submodule_states_.CaptureMultiBandSubModulesActive() ||
628 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000629
peahde65ddc2016-09-16 15:02:15 -0700630 capture_nonlocked_.capture_processing_format =
631 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700632
peah2ce640f2017-04-07 03:57:48 -0700633 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200634 if (!capture_nonlocked_.echo_controller_enabled) {
peah2ce640f2017-04-07 03:57:48 -0700635 render_processing_rate = FindNativeProcessRateToUse(
636 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
637 formats_.api_format.reverse_output_stream().sample_rate_hz()),
638 submodule_states_.CaptureMultiBandSubModulesActive() ||
639 submodule_states_.RenderMultiBandSubModulesActive());
640 } else {
641 render_processing_rate = capture_processing_rate;
642 }
643
aluebseb3603b2016-04-20 15:27:58 -0700644 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
645 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700646 if (render_processing_rate > kSampleRate32kHz &&
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200647 !capture_nonlocked_.echo_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -0700648 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
649 ? kSampleRate32kHz
650 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700651 }
peah2ce640f2017-04-07 03:57:48 -0700652
peahde65ddc2016-09-16 15:02:15 -0700653 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700654 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700655 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
656 kSampleRate8kHz) {
657 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000658 } else {
peahde65ddc2016-09-16 15:02:15 -0700659 render_processing_rate =
660 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000661 }
662
peahde65ddc2016-09-16 15:02:15 -0700663 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000664 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700665 if (submodule_states_.RenderMultiBandSubModulesActive()) {
666 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
667 } else {
668 formats_.render_processing_format = StreamConfig(
669 formats_.api_format.reverse_input_stream().sample_rate_hz(),
670 formats_.api_format.reverse_input_stream().num_channels());
671 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000672
peahde65ddc2016-09-16 15:02:15 -0700673 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
674 kSampleRate32kHz ||
675 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
676 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800677 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000678 } else {
peahdf3efa82015-11-28 12:35:15 -0800679 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700680 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000681 }
682
683 return InitializeLocked();
684}
685
peah88ac8532016-09-12 16:47:25 -0700686void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peahc19f3122016-10-07 14:54:10 -0700687 config_ = config;
peah88ac8532016-09-12 16:47:25 -0700688
peah88ac8532016-09-12 16:47:25 -0700689 // Run in a single-threaded manner when applying the settings.
690 rtc::CritScope cs_render(&crit_render_);
691 rtc::CritScope cs_capture(&crit_capture_);
692
peah8271d042016-11-22 07:24:52 -0800693 InitializeLowCutFilter();
694
Mirko Bonadei675513b2017-11-09 11:09:25 +0100695 RTC_LOG(LS_INFO) << "Highpass filter activated: "
696 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800697
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100698 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700699 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100700 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
701 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100702 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100703 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700704 config_.gain_controller2 = AudioProcessing::Config::GainController2();
705 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200706 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200707 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200708 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100709 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
710 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200711 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
712 << config_.pre_amplifier.enabled;
peah88ac8532016-09-12 16:47:25 -0700713}
714
715void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800716 // Run in a single-threaded manner when setting the extra options.
717 rtc::CritScope cs_render(&crit_render_);
718 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000719
peahb624d8c2016-03-05 03:01:14 -0800720 public_submodules_->echo_cancellation->SetExtraOptions(config);
721
peahdf3efa82015-11-28 12:35:15 -0800722 if (capture_.transient_suppressor_enabled !=
723 config.Get<ExperimentalNs>().enabled) {
724 capture_.transient_suppressor_enabled =
725 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000726 InitializeTransient();
727 }
aluebs2a346882016-01-11 18:04:30 -0800728
peah1bcfce52016-08-26 07:16:04 -0700729#if WEBRTC_INTELLIGIBILITY_ENHANCER
alessiob3ec96df2017-05-22 06:57:06 -0700730 if (capture_nonlocked_.intelligibility_enabled !=
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700731 config.Get<Intelligibility>().enabled) {
732 capture_nonlocked_.intelligibility_enabled =
733 config.Get<Intelligibility>().enabled;
734 InitializeIntelligibility();
735 }
peah1bcfce52016-08-26 07:16:04 -0700736#endif
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700737
aluebs2a346882016-01-11 18:04:30 -0800738#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
aluebsb2328d12016-01-11 20:32:29 -0800739 if (capture_nonlocked_.beamformer_enabled !=
740 config.Get<Beamforming>().enabled) {
741 capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled;
aluebs2a346882016-01-11 18:04:30 -0800742 if (config.Get<Beamforming>().array_geometry.size() > 1) {
743 capture_.array_geometry = config.Get<Beamforming>().array_geometry;
744 }
745 capture_.target_direction = config.Get<Beamforming>().target_direction;
746 InitializeBeamformer();
747 }
748#endif // WEBRTC_ANDROID_PLATFORM_BUILD
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000749}
750
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000751int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800752 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700753 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000754}
755
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000756int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800757 // Used as callback from submodules, hence locking is not allowed.
758 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000759}
760
Peter Kasting69558702016-01-12 16:26:35 -0800761size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800762 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700763 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000764}
765
Peter Kasting69558702016-01-12 16:26:35 -0800766size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800767 // Used as callback from submodules, hence locking is not allowed.
768 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000769}
770
Peter Kasting69558702016-01-12 16:26:35 -0800771size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800772 // Used as callback from submodules, hence locking is not allowed.
peahedddac52017-05-16 01:08:58 -0700773 return (capture_nonlocked_.beamformer_enabled ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200774 capture_nonlocked_.echo_controller_enabled)
peahedddac52017-05-16 01:08:58 -0700775 ? 1
776 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800777}
778
Peter Kasting69558702016-01-12 16:26:35 -0800779size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800780 // Used as callback from submodules, hence locking is not allowed.
781 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000782}
783
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000784void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800785 rtc::CritScope cs(&crit_capture_);
786 capture_.output_will_be_muted = muted;
787 if (private_submodules_->agc_manager.get()) {
788 private_submodules_->agc_manager->SetCaptureMuted(
789 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000790 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000791}
792
Alessio Bazzicac054e782018-04-16 12:10:09 +0200793void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200794 switch (setting.type()) {
795 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
796 render_runtime_settings_enqueuer_.Enqueue(setting);
797 return;
798 case RuntimeSetting::Type::kNotSpecified:
799 RTC_NOTREACHED();
800 return;
801 case RuntimeSetting::Type::kCapturePreGain:
802 capture_runtime_settings_enqueuer_.Enqueue(setting);
803 return;
804 }
805 // The language allows the enum to have a non-enumerator
806 // value. Check that this doesn't happen.
807 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200808}
809
810AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
811 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200812 : runtime_settings_(*runtime_settings) {
813 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200814}
815
816AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
817 default;
818
819void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
820 RuntimeSetting setting) {
821 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200822 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200823 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200824 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200825 RTC_LOG(LS_ERROR)
826 << "The runtime settings queue is full. Oldest setting discarded.";
827 }
828 if (remaining_attempts == 0)
829 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
830}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000831
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000832int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700833 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000834 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000835 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000836 int output_sample_rate_hz,
837 ChannelLayout output_layout,
838 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800839 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800840 StreamConfig input_stream;
841 StreamConfig output_stream;
842 {
843 // Access the formats_.api_format.input_stream beneath the capture lock.
844 // The lock must be released as it is later required in the call
845 // to ProcessStream(,,,);
846 rtc::CritScope cs(&crit_capture_);
847 input_stream = formats_.api_format.input_stream();
848 output_stream = formats_.api_format.output_stream();
849 }
850
Michael Graczyk86c6d332015-07-23 11:41:39 -0700851 input_stream.set_sample_rate_hz(input_sample_rate_hz);
852 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
853 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700854 output_stream.set_sample_rate_hz(output_sample_rate_hz);
855 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
856 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
857
858 if (samples_per_channel != input_stream.num_frames()) {
859 return kBadDataLengthError;
860 }
861 return ProcessStream(src, input_stream, output_stream, dest);
862}
863
864int AudioProcessingImpl::ProcessStream(const float* const* src,
865 const StreamConfig& input_config,
866 const StreamConfig& output_config,
867 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800868 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800869 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700870 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800871 {
872 // Acquire the capture lock in order to safely call the function
873 // that retrieves the render side data. This function accesses apm
874 // getters that need the capture lock held when being called.
875 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700876 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800877
878 if (!src || !dest) {
879 return kNullPointerError;
880 }
881
882 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700883 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000884 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000885
Michael Graczyk86c6d332015-07-23 11:41:39 -0700886 processing_config.input_stream() = input_config;
887 processing_config.output_stream() = output_config;
888
peahdf3efa82015-11-28 12:35:15 -0800889 {
890 // Do conditional reinitialization.
891 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -0700892 RETURN_ON_ERR(
893 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -0800894 }
895 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700896 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
897 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000898
aleloi868f32f2017-05-23 07:20:05 -0700899 if (aec_dump_) {
900 RecordUnprocessedCaptureStream(src);
901 }
902
peahdf3efa82015-11-28 12:35:15 -0800903 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700904 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800905 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000906
aleloi868f32f2017-05-23 07:20:05 -0700907 if (aec_dump_) {
908 RecordProcessedCaptureStream(dest);
909 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000910 return kNoError;
911}
912
Alex Loiko73ec0192018-05-15 10:52:28 +0200913void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200914 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200915 while (capture_runtime_settings_.Remove(&setting)) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200916 switch (setting.type()) {
917 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200918 if (config_.pre_amplifier.enabled) {
919 float value;
920 setting.GetFloat(&value);
921 private_submodules_->pre_amplifier->SetGainFactor(value);
922 }
923 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200924 break;
Alex Loiko73ec0192018-05-15 10:52:28 +0200925 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
926 RTC_NOTREACHED();
927 break;
928 case RuntimeSetting::Type::kNotSpecified:
929 RTC_NOTREACHED();
930 break;
931 }
932 }
933}
934
935void AudioProcessingImpl::HandleRenderRuntimeSettings() {
936 RuntimeSetting setting;
937 while (render_runtime_settings_.Remove(&setting)) {
938 switch (setting.type()) {
939 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
940 if (private_submodules_->render_pre_processor) {
941 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
942 }
943 break;
944 case RuntimeSetting::Type::kCapturePreGain:
945 RTC_NOTREACHED();
946 break;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200947 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +0200948 RTC_NOTREACHED();
949 break;
950 }
951 }
952}
953
peah9e6a2902017-05-15 07:19:21 -0700954void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
peah764e3642016-10-22 05:04:30 -0700955 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
956 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700957 &aec_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700958
kwibergaf476c72016-11-28 15:21:39 -0800959 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -0700960
961 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700962 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700963 // The data queue is full and needs to be emptied.
964 EmptyQueuedRenderAudio();
965
966 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700967 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700968 RTC_DCHECK(result);
969 }
970
971 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
972 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700973 &aecm_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700974
975 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700976 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700977 // The data queue is full and needs to be emptied.
978 EmptyQueuedRenderAudio();
979
980 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700981 bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700982 RTC_DCHECK(result);
983 }
peah701d6282016-10-25 05:42:20 -0700984
985 if (!constants_.use_experimental_agc) {
986 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
987 // Insert the samples into the queue.
988 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
989 // The data queue is full and needs to be emptied.
990 EmptyQueuedRenderAudio();
991
992 // Retry the insert (should always work).
993 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
994 RTC_DCHECK(result);
995 }
996 }
peah9e6a2902017-05-15 07:19:21 -0700997}
ivoc9f4a4a02016-10-28 05:39:16 -0700998
peah9e6a2902017-05-15 07:19:21 -0700999void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001000 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1001
1002 // Insert the samples into the queue.
1003 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1004 // The data queue is full and needs to be emptied.
1005 EmptyQueuedRenderAudio();
1006
1007 // Retry the insert (should always work).
1008 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1009 RTC_DCHECK(result);
1010 }
peah764e3642016-10-22 05:04:30 -07001011}
1012
1013void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001014 const size_t new_aec_render_queue_element_max_size =
peah764e3642016-10-22 05:04:30 -07001015 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -07001016 kMaxAllowedValuesOfSamplesPerBand *
peah764e3642016-10-22 05:04:30 -07001017 EchoCancellationImpl::NumCancellersRequired(
1018 num_output_channels(), num_reverse_channels()));
1019
peah701d6282016-10-25 05:42:20 -07001020 const size_t new_aecm_render_queue_element_max_size =
peaha0624602016-10-25 04:45:24 -07001021 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -07001022 kMaxAllowedValuesOfSamplesPerBand *
peaha0624602016-10-25 04:45:24 -07001023 EchoControlMobileImpl::NumCancellersRequired(
1024 num_output_channels(), num_reverse_channels()));
peah764e3642016-10-22 05:04:30 -07001025
peah701d6282016-10-25 05:42:20 -07001026 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001027 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001028
ivoc9f4a4a02016-10-28 05:39:16 -07001029 const size_t new_red_render_queue_element_max_size =
1030 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1031
peaha0624602016-10-25 04:45:24 -07001032 // Reallocate the queues if the queue item sizes are too small to fit the
1033 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001034 if (aec_render_queue_element_max_size_ <
1035 new_aec_render_queue_element_max_size) {
1036 aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
peah764e3642016-10-22 05:04:30 -07001037
peaha0624602016-10-25 04:45:24 -07001038 std::vector<float> template_queue_element(
peah701d6282016-10-25 05:42:20 -07001039 aec_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001040
peah701d6282016-10-25 05:42:20 -07001041 aec_render_signal_queue_.reset(
peah764e3642016-10-22 05:04:30 -07001042 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1043 kMaxNumFramesToBuffer, template_queue_element,
peaha0624602016-10-25 04:45:24 -07001044 RenderQueueItemVerifier<float>(
peah701d6282016-10-25 05:42:20 -07001045 aec_render_queue_element_max_size_)));
peah764e3642016-10-22 05:04:30 -07001046
peah701d6282016-10-25 05:42:20 -07001047 aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
1048 aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
peah764e3642016-10-22 05:04:30 -07001049 } else {
peah701d6282016-10-25 05:42:20 -07001050 aec_render_signal_queue_->Clear();
peaha0624602016-10-25 04:45:24 -07001051 }
1052
peah701d6282016-10-25 05:42:20 -07001053 if (aecm_render_queue_element_max_size_ <
1054 new_aecm_render_queue_element_max_size) {
1055 aecm_render_queue_element_max_size_ =
1056 new_aecm_render_queue_element_max_size;
peaha0624602016-10-25 04:45:24 -07001057
1058 std::vector<int16_t> template_queue_element(
peah701d6282016-10-25 05:42:20 -07001059 aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001060
peah701d6282016-10-25 05:42:20 -07001061 aecm_render_signal_queue_.reset(
peaha0624602016-10-25 04:45:24 -07001062 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1063 kMaxNumFramesToBuffer, template_queue_element,
1064 RenderQueueItemVerifier<int16_t>(
peah701d6282016-10-25 05:42:20 -07001065 aecm_render_queue_element_max_size_)));
peaha0624602016-10-25 04:45:24 -07001066
peah701d6282016-10-25 05:42:20 -07001067 aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
1068 aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001069 } else {
peah701d6282016-10-25 05:42:20 -07001070 aecm_render_signal_queue_->Clear();
1071 }
1072
1073 if (agc_render_queue_element_max_size_ <
1074 new_agc_render_queue_element_max_size) {
1075 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1076
1077 std::vector<int16_t> template_queue_element(
1078 agc_render_queue_element_max_size_);
1079
1080 agc_render_signal_queue_.reset(
1081 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1082 kMaxNumFramesToBuffer, template_queue_element,
1083 RenderQueueItemVerifier<int16_t>(
1084 agc_render_queue_element_max_size_)));
1085
1086 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1087 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1088 } else {
1089 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001090 }
ivoc9f4a4a02016-10-28 05:39:16 -07001091
1092 if (red_render_queue_element_max_size_ <
1093 new_red_render_queue_element_max_size) {
1094 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1095
1096 std::vector<float> template_queue_element(
1097 red_render_queue_element_max_size_);
1098
1099 red_render_signal_queue_.reset(
1100 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1101 kMaxNumFramesToBuffer, template_queue_element,
1102 RenderQueueItemVerifier<float>(
1103 red_render_queue_element_max_size_)));
1104
1105 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1106 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1107 } else {
1108 red_render_signal_queue_->Clear();
1109 }
peah764e3642016-10-22 05:04:30 -07001110}
1111
1112void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1113 rtc::CritScope cs_capture(&crit_capture_);
peah701d6282016-10-25 05:42:20 -07001114 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -07001115 public_submodules_->echo_cancellation->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001116 aec_capture_queue_buffer_);
peaha0624602016-10-25 04:45:24 -07001117 }
1118
peah701d6282016-10-25 05:42:20 -07001119 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -07001120 public_submodules_->echo_control_mobile->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001121 aecm_capture_queue_buffer_);
1122 }
1123
1124 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1125 public_submodules_->gain_control->ProcessRenderAudio(
1126 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001127 }
ivoc9f4a4a02016-10-28 05:39:16 -07001128
1129 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001130 RTC_DCHECK(private_submodules_->echo_detector);
1131 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001132 red_capture_queue_buffer_);
1133 }
peah764e3642016-10-22 05:04:30 -07001134}
1135
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001136int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001137 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001138 {
1139 // Acquire the capture lock in order to safely call the function
1140 // that retrieves the render side data. This function accesses apm
1141 // getters that need the capture lock held when being called.
1142 // The lock needs to be released as
1143 // public_submodules_->echo_control_mobile->is_enabled() aquires this lock
1144 // as well.
1145 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001146 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001147 }
peahfa6228e2015-11-16 16:27:42 -08001148
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001149 if (!frame) {
1150 return kNullPointerError;
1151 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001152 // Must be a native rate.
1153 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1154 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001155 frame->sample_rate_hz_ != kSampleRate32kHz &&
1156 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001157 return kBadSampleRateError;
1158 }
peah192164e2015-11-17 02:16:45 -08001159
peahdf3efa82015-11-28 12:35:15 -08001160 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001161 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001162 {
1163 // Aquire lock for the access of api_format.
1164 // The lock is released immediately due to the conditional
1165 // reinitialization.
1166 rtc::CritScope cs_capture(&crit_capture_);
1167 // TODO(ajm): The input and output rates and channels are currently
1168 // constrained to be identical in the int16 interface.
1169 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001170
1171 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001172 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001173 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1174 processing_config.input_stream().set_num_channels(frame->num_channels_);
1175 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1176 processing_config.output_stream().set_num_channels(frame->num_channels_);
1177
peahdf3efa82015-11-28 12:35:15 -08001178 {
1179 // Do conditional reinitialization.
1180 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -07001181 RETURN_ON_ERR(
1182 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -08001183 }
1184 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001185 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001186 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001187 return kBadDataLengthError;
1188 }
1189
aleloi868f32f2017-05-23 07:20:05 -07001190 if (aec_dump_) {
1191 RecordUnprocessedCaptureStream(*frame);
1192 }
1193
peahdf3efa82015-11-28 12:35:15 -08001194 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001195 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001196 capture_.capture_audio->InterleaveTo(
peah23ac8b42017-05-23 05:33:56 -07001197 frame, submodule_states_.CaptureMultiBandProcessingActive() ||
1198 submodule_states_.CaptureFullBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001199
aleloi868f32f2017-05-23 07:20:05 -07001200 if (aec_dump_) {
1201 RecordProcessedCaptureStream(*frame);
1202 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001203
1204 return kNoError;
1205}
1206
peahde65ddc2016-09-16 15:02:15 -07001207int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001208 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001209
peahb58a1582016-03-15 09:34:24 -07001210 // Ensure that not both the AEC and AECM are active at the same time.
1211 // TODO(peah): Simplify once the public API Enable functions for these
1212 // are moved to APM.
1213 RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&
1214 public_submodules_->echo_control_mobile->is_enabled()));
1215
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001216 MaybeUpdateHistograms();
1217
peahde65ddc2016-09-16 15:02:15 -07001218 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001219
Alex Loikob5c9a792018-04-16 16:31:22 +02001220 if (private_submodules_->pre_amplifier) {
1221 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
1222 capture_buffer->channels_f(), capture_buffer->num_channels(),
1223 capture_buffer->num_frames()));
1224 }
1225
peah1b08dc32016-12-20 13:45:58 -08001226 capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>(
henrik.lundin290d43a2016-11-29 08:09:09 -08001227 capture_buffer->channels_const()[0],
1228 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001229 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1230 if (log_rms) {
1231 capture_rms_interval_counter_ = 0;
1232 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001233 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1234 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1235 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1236 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001237 }
1238
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001239 if (private_submodules_->echo_controller) {
Per Åhgren9aed31c2017-06-29 20:23:27 +02001240 // TODO(peah): Reactivate analogue AGC gain detection once the analogue AGC
1241 // issues have been addressed.
1242 capture_.echo_path_gain_change = false;
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001243 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001244 }
1245
peahbe615622016-02-13 16:40:47 -08001246 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001247 public_submodules_->gain_control->is_enabled()) {
1248 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001249 capture_buffer->channels()[0], capture_buffer->num_channels(),
1250 capture_nonlocked_.capture_processing_format.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001251 }
1252
peah2ace3f92016-09-10 04:42:27 -07001253 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1254 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001255 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1256 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001257 }
1258
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001259 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001260 // Force down-mixing of the number of channels after the detection of
1261 // capture signal saturation.
1262 // TODO(peah): Look into ensuring that this kind of tampering with the
1263 // AudioBuffer functionality should not be needed.
1264 capture_buffer->set_num_channels(1);
1265 }
1266
aluebsb2328d12016-01-11 20:32:29 -08001267 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001268 private_submodules_->beamformer->AnalyzeChunk(
1269 *capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001270 // Discards all channels by the leftmost one.
peahde65ddc2016-09-16 15:02:15 -07001271 capture_buffer->set_num_channels(1);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001272 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001273
peahe0eae3c2016-12-14 01:16:23 -08001274 // TODO(peah): Move the AEC3 low-cut filter to this place.
1275 if (private_submodules_->low_cut_filter &&
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001276 !private_submodules_->echo_controller) {
peah8271d042016-11-22 07:24:52 -08001277 private_submodules_->low_cut_filter->Process(capture_buffer);
1278 }
peahde65ddc2016-09-16 15:02:15 -07001279 RETURN_ON_ERR(
1280 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1281 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001282
1283 // Ensure that the stream delay was set before the call to the
1284 // AEC ProcessCaptureAudio function.
1285 if (public_submodules_->echo_cancellation->is_enabled() &&
Per Åhgren0dfd3722018-05-06 18:16:01 +02001286 !private_submodules_->echo_controller && !was_stream_delay_set()) {
peahb58a1582016-03-15 09:34:24 -07001287 return AudioProcessing::kStreamParameterNotSetError;
1288 }
1289
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001290 if (private_submodules_->echo_controller) {
Per Åhgren13735822018-02-12 21:42:56 +01001291 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1292
Per Åhgrend0fa8202018-04-18 09:35:13 +02001293 if (was_stream_delay_set()) {
1294 private_submodules_->echo_controller->SetAudioBufferDelay(
1295 stream_delay_ms());
1296 }
1297
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001298 private_submodules_->echo_controller->ProcessCapture(
peah67995532017-04-10 14:12:41 -07001299 capture_buffer, capture_.echo_path_gain_change);
peah61202ac2017-02-06 03:39:42 -08001300 } else {
1301 RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(
1302 capture_buffer, stream_delay_ms()));
peahe0eae3c2016-12-14 01:16:23 -08001303 }
1304
peahdf3efa82015-11-28 12:35:15 -08001305 if (public_submodules_->echo_control_mobile->is_enabled() &&
1306 public_submodules_->noise_suppression->is_enabled()) {
peahde65ddc2016-09-16 15:02:15 -07001307 capture_buffer->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +00001308 }
peahde65ddc2016-09-16 15:02:15 -07001309 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah1bcfce52016-08-26 07:16:04 -07001310#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001311 if (capture_nonlocked_.intelligibility_enabled) {
aluebsc466bad2016-02-10 12:03:00 -08001312 RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());
Sam Zackrissonab1aee02018-03-05 15:59:06 +01001313 const int gain_db =
1314 public_submodules_->gain_control->is_enabled()
1315 ? public_submodules_->gain_control->compression_gain_db()
1316 : 0;
1317 const float gain = DbToRatio(gain_db);
aluebsc466bad2016-02-10 12:03:00 -08001318 public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -07001319 public_submodules_->noise_suppression->NoiseEstimate(), gain);
aluebsc466bad2016-02-10 12:03:00 -08001320 }
peah1bcfce52016-08-26 07:16:04 -07001321#endif
peah253534d2016-03-15 04:32:28 -07001322
1323 // Ensure that the stream delay was set before the call to the
1324 // AECM ProcessCaptureAudio function.
1325 if (public_submodules_->echo_control_mobile->is_enabled() &&
1326 !was_stream_delay_set()) {
1327 return AudioProcessing::kStreamParameterNotSetError;
1328 }
1329
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001330 if (!(private_submodules_->echo_controller ||
Per Åhgren46537a32017-06-07 10:08:10 +02001331 public_submodules_->echo_cancellation->is_enabled())) {
1332 RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(
1333 capture_buffer, stream_delay_ms()));
1334 }
ivoc9f4a4a02016-10-28 05:39:16 -07001335
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001336 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001337 private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001338 }
1339
peahde65ddc2016-09-16 15:02:15 -07001340 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001341
peahbe615622016-02-13 16:40:47 -08001342 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001343 public_submodules_->gain_control->is_enabled() &&
aluebsb2328d12016-01-11 20:32:29 -08001344 (!capture_nonlocked_.beamformer_enabled ||
peahdf3efa82015-11-28 12:35:15 -08001345 private_submodules_->beamformer->is_target_present())) {
1346 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001347 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1348 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001349 }
peahb8fbb542016-03-15 02:28:08 -07001350 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001351 capture_buffer, echo_cancellation()->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001352
peah2ace3f92016-09-10 04:42:27 -07001353 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1354 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001355 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1356 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001357 }
1358
peah9e6a2902017-05-15 07:19:21 -07001359 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001360 RTC_DCHECK(private_submodules_->echo_detector);
1361 private_submodules_->echo_detector->AnalyzeCaptureAudio(
peah9e6a2902017-05-15 07:19:21 -07001362 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
1363 capture_buffer->num_frames()));
1364 }
1365
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001366 // TODO(aluebs): Investigate if the transient suppression placement should be
1367 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001368 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001369 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001370 private_submodules_->agc_manager.get()
1371 ? private_submodules_->agc_manager->voice_probability()
1372 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001373
peahdf3efa82015-11-28 12:35:15 -08001374 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001375 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1376 capture_buffer->num_channels(),
1377 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1378 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1379 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001380 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001381 }
1382
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001383 if (config_.gain_controller2.enabled) {
alessiob3ec96df2017-05-22 06:57:06 -07001384 private_submodules_->gain_controller2->Process(capture_buffer);
1385 }
1386
Sam Zackrisson0beac582017-09-25 12:04:02 +02001387 if (private_submodules_->capture_post_processor) {
1388 private_submodules_->capture_post_processor->Process(capture_buffer);
1389 }
1390
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001391 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001392 public_submodules_->level_estimator->ProcessStream(capture_buffer);
ajm@google.com808e0e02011-08-03 21:08:51 +00001393
peah1b08dc32016-12-20 13:45:58 -08001394 capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>(
1395 capture_buffer->channels_const()[0],
1396 capture_nonlocked_.capture_processing_format.num_frames()));
1397 if (log_rms) {
1398 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1399 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1400 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1401 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1402 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1403 }
1404
peahdf3efa82015-11-28 12:35:15 -08001405 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001406 return kNoError;
1407}
1408
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001409int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001410 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001411 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001412 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001413 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001414 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001415 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001416 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001417 };
1418 if (samples_per_channel != reverse_config.num_frames()) {
1419 return kBadDataLengthError;
1420 }
peahdf3efa82015-11-28 12:35:15 -08001421 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001422}
1423
peahde65ddc2016-09-16 15:02:15 -07001424int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1425 const StreamConfig& input_config,
1426 const StreamConfig& output_config,
1427 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001428 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001429 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001430 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001431 if (submodule_states_.RenderMultiBandProcessingActive() ||
1432 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001433 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1434 dest);
peah2ace3f92016-09-10 04:42:27 -07001435 } else if (formats_.api_format.reverse_input_stream() !=
1436 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001437 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1438 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001439 } else {
peahde65ddc2016-09-16 15:02:15 -07001440 CopyAudioIfNeeded(src, input_config.num_frames(),
1441 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001442 }
1443
1444 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001445}
1446
peahdf3efa82015-11-28 12:35:15 -08001447int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001448 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001449 const StreamConfig& input_config,
1450 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001451 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001452 return kNullPointerError;
1453 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001454
peahde65ddc2016-09-16 15:02:15 -07001455 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001456 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001457 }
1458
peahdf3efa82015-11-28 12:35:15 -08001459 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001460 processing_config.reverse_input_stream() = input_config;
1461 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001462
peahdf3efa82015-11-28 12:35:15 -08001463 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001464 RTC_DCHECK_EQ(input_config.num_frames(),
1465 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001466
aleloi868f32f2017-05-23 07:20:05 -07001467 if (aec_dump_) {
1468 const size_t channel_size =
1469 formats_.api_format.reverse_input_stream().num_frames();
1470 const size_t num_channels =
1471 formats_.api_format.reverse_input_stream().num_channels();
1472 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001473 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001474 }
peahdf3efa82015-11-28 12:35:15 -08001475 render_.render_audio->CopyFrom(src,
1476 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001477 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001478}
1479
1480int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001481 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001482 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001483 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001484 return kNullPointerError;
1485 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001486 // Must be a native rate.
1487 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1488 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001489 frame->sample_rate_hz_ != kSampleRate32kHz &&
1490 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001491 return kBadSampleRateError;
1492 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001493
Michael Graczyk86c6d332015-07-23 11:41:39 -07001494 if (frame->num_channels_ <= 0) {
1495 return kBadNumberChannelsError;
1496 }
1497
peahdf3efa82015-11-28 12:35:15 -08001498 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001499 processing_config.reverse_input_stream().set_sample_rate_hz(
1500 frame->sample_rate_hz_);
1501 processing_config.reverse_input_stream().set_num_channels(
1502 frame->num_channels_);
1503 processing_config.reverse_output_stream().set_sample_rate_hz(
1504 frame->sample_rate_hz_);
1505 processing_config.reverse_output_stream().set_num_channels(
1506 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001507
peahdf3efa82015-11-28 12:35:15 -08001508 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001509 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001510 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001511 return kBadDataLengthError;
1512 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001513
aleloi868f32f2017-05-23 07:20:05 -07001514 if (aec_dump_) {
1515 aec_dump_->WriteRenderStreamMessage(*frame);
1516 }
1517
peahdf3efa82015-11-28 12:35:15 -08001518 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001519 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001520 render_.render_audio->InterleaveTo(
Alex Loiko5825aa62017-12-18 16:02:40 +01001521 frame, submodule_states_.RenderMultiBandProcessingActive() ||
1522 submodule_states_.RenderFullBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001523 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001524}
niklase@google.com470e71d2011-07-07 08:21:25 +00001525
peahde65ddc2016-09-16 15:02:15 -07001526int AudioProcessingImpl::ProcessRenderStreamLocked() {
1527 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001528
1529 QueueNonbandedRenderAudio(render_buffer);
1530
Alex Loiko73ec0192018-05-15 10:52:28 +02001531 HandleRenderRuntimeSettings();
1532
Alex Loiko5825aa62017-12-18 16:02:40 +01001533 if (private_submodules_->render_pre_processor) {
1534 private_submodules_->render_pre_processor->Process(render_buffer);
1535 }
1536
peah2ace3f92016-09-10 04:42:27 -07001537 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001538 SampleRateSupportsMultiBand(
1539 formats_.render_processing_format.sample_rate_hz())) {
1540 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001541 }
1542
peah1bcfce52016-08-26 07:16:04 -07001543#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001544 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001545 public_submodules_->intelligibility_enhancer->ProcessRenderAudio(
Alejandro Luebsef009252016-09-20 14:51:56 -07001546 render_buffer);
ekmeyerson60d9b332015-08-14 10:35:55 -07001547 }
peah1bcfce52016-08-26 07:16:04 -07001548#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001549
peahce4d9152017-05-19 01:28:05 -07001550 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1551 QueueBandedRenderAudio(render_buffer);
1552 }
1553
peahe0eae3c2016-12-14 01:16:23 -08001554 // TODO(peah): Perform the queueing ínside QueueRenderAudiuo().
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001555 if (private_submodules_->echo_controller) {
1556 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001557 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001558
peah2ace3f92016-09-10 04:42:27 -07001559 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001560 SampleRateSupportsMultiBand(
1561 formats_.render_processing_format.sample_rate_hz())) {
1562 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001563 }
1564
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001565 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001566}
1567
1568int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001569 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001570 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001571 capture_.was_stream_delay_set = true;
1572 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001573
niklase@google.com470e71d2011-07-07 08:21:25 +00001574 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001575 delay = 0;
1576 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001577 }
1578
1579 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1580 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001581 delay = 500;
1582 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001583 }
1584
peahdf3efa82015-11-28 12:35:15 -08001585 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001586 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001587}
1588
1589int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001590 // Used as callback from submodules, hence locking is not allowed.
1591 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001592}
1593
1594bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001595 // Used as callback from submodules, hence locking is not allowed.
1596 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001597}
1598
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001599void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001600 rtc::CritScope cs(&crit_capture_);
1601 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001602}
1603
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001604void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001605 rtc::CritScope cs(&crit_capture_);
1606 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001607}
1608
1609int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001610 rtc::CritScope cs(&crit_capture_);
1611 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001612}
1613
aleloi868f32f2017-05-23 07:20:05 -07001614void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1615 RTC_DCHECK(aec_dump);
1616 rtc::CritScope cs_render(&crit_render_);
1617 rtc::CritScope cs_capture(&crit_capture_);
1618
1619 // The previously attached AecDump will be destroyed with the
1620 // 'aec_dump' parameter, which is after locks are released.
1621 aec_dump_.swap(aec_dump);
1622 WriteAecDumpConfigMessage(true);
Alex Loiko1aec5942018-05-15 13:13:22 +02001623 aec_dump_->WriteInitMessage(formats_.api_format);
aleloi868f32f2017-05-23 07:20:05 -07001624}
1625
1626void AudioProcessingImpl::DetachAecDump() {
1627 // The d-tor of a task-queue based AecDump blocks until all pending
1628 // tasks are done. This construction avoids blocking while holding
1629 // the render and capture locks.
1630 std::unique_ptr<AecDump> aec_dump = nullptr;
1631 {
1632 rtc::CritScope cs_render(&crit_render_);
1633 rtc::CritScope cs_capture(&crit_capture_);
1634 aec_dump = std::move(aec_dump_);
1635 }
1636}
1637
Sam Zackrisson4d364492018-03-02 16:03:21 +01001638void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1639 std::unique_ptr<AudioGenerator> audio_generator) {
1640 // TODO(bugs.webrtc.org/8882) Stub.
1641 // Reset internal audio generator with audio_generator.
1642}
1643
1644void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1645 // TODO(bugs.webrtc.org/8882) Stub.
1646 // Delete audio generator, if one is attached.
1647}
1648
ivoc4e477a12017-01-15 08:29:46 -08001649AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics() {
1650 residual_echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1651 echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1652 echo_return_loss_enhancement.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1653 a_nlp.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1654}
1655
1656AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics(
1657 const AudioProcessingStatistics& other) = default;
1658
1659AudioProcessing::AudioProcessingStatistics::~AudioProcessingStatistics() =
1660 default;
1661
ivoc3e9a5372016-10-28 07:55:33 -07001662// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
1663AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics()
1664 const {
1665 return AudioProcessingStatistics();
1666}
1667
Ivo Creusenae026092017-11-20 13:07:16 +01001668// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
Ivo Creusen56d46092017-11-24 17:29:59 +01001669AudioProcessingStats AudioProcessing::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001670 bool has_remote_tracks) const {
1671 return AudioProcessingStats();
1672}
1673
ivoc3e9a5372016-10-28 07:55:33 -07001674AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics()
1675 const {
1676 AudioProcessingStatistics stats;
1677 EchoCancellation::Metrics metrics;
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001678 if (private_submodules_->echo_controller) {
1679 rtc::CritScope cs_capture(&crit_capture_);
1680 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1681 float erl = static_cast<float>(ec_metrics.echo_return_loss);
1682 float erle = static_cast<float>(ec_metrics.echo_return_loss_enhancement);
1683 // Instant value will also be used for min, max and average.
1684 stats.echo_return_loss.Set(erl, erl, erl, erl);
1685 stats.echo_return_loss_enhancement.Set(erle, erle, erle, erle);
1686 } else if (public_submodules_->echo_cancellation->GetMetrics(&metrics) ==
1687 Error::kNoError) {
ivocd0a151c2016-11-02 09:14:37 -07001688 stats.a_nlp.Set(metrics.a_nlp);
1689 stats.divergent_filter_fraction = metrics.divergent_filter_fraction;
1690 stats.echo_return_loss.Set(metrics.echo_return_loss);
1691 stats.echo_return_loss_enhancement.Set(
1692 metrics.echo_return_loss_enhancement);
1693 stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss);
1694 }
ivoc9c192b22017-03-16 04:22:14 -07001695 {
1696 rtc::CritScope cs_capture(&crit_capture_);
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001697 RTC_DCHECK(private_submodules_->echo_detector);
1698 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1699 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
ivoc9c192b22017-03-16 04:22:14 -07001700 stats.residual_echo_likelihood_recent_max =
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001701 ed_metrics.echo_likelihood_recent_max;
ivoc9c192b22017-03-16 04:22:14 -07001702 }
ivoc3e9a5372016-10-28 07:55:33 -07001703 public_submodules_->echo_cancellation->GetDelayMetrics(
1704 &stats.delay_median, &stats.delay_standard_deviation,
1705 &stats.fraction_poor_delays);
1706 return stats;
1707}
1708
Ivo Creusen56d46092017-11-24 17:29:59 +01001709AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001710 bool has_remote_tracks) const {
1711 AudioProcessingStats stats;
1712 if (has_remote_tracks) {
1713 EchoCancellation::Metrics metrics;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001714 if (private_submodules_->echo_controller) {
1715 rtc::CritScope cs_capture(&crit_capture_);
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001716 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1717 stats.echo_return_loss = ec_metrics.echo_return_loss;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001718 stats.echo_return_loss_enhancement =
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001719 ec_metrics.echo_return_loss_enhancement;
Per Åhgren83c4a022017-11-27 12:07:09 +01001720 stats.delay_ms = ec_metrics.delay_ms;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001721 } else if (public_submodules_->echo_cancellation->GetMetrics(&metrics) ==
1722 Error::kNoError) {
Ivo Creusenae026092017-11-20 13:07:16 +01001723 if (metrics.divergent_filter_fraction != -1.0f) {
1724 stats.divergent_filter_fraction =
1725 rtc::Optional<double>(metrics.divergent_filter_fraction);
1726 }
1727 if (metrics.echo_return_loss.instant != -100) {
1728 stats.echo_return_loss =
1729 rtc::Optional<double>(metrics.echo_return_loss.instant);
1730 }
1731 if (metrics.echo_return_loss_enhancement.instant != -100) {
1732 stats.echo_return_loss_enhancement =
1733 rtc::Optional<double>(metrics.echo_return_loss_enhancement.instant);
1734 }
1735 }
1736 if (config_.residual_echo_detector.enabled) {
1737 rtc::CritScope cs_capture(&crit_capture_);
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001738 RTC_DCHECK(private_submodules_->echo_detector);
1739 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1740 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
Ivo Creusenae026092017-11-20 13:07:16 +01001741 stats.residual_echo_likelihood_recent_max =
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001742 ed_metrics.echo_likelihood_recent_max;
Ivo Creusenae026092017-11-20 13:07:16 +01001743 }
1744 int delay_median, delay_std;
1745 float fraction_poor_delays;
1746 if (public_submodules_->echo_cancellation->GetDelayMetrics(
1747 &delay_median, &delay_std, &fraction_poor_delays) ==
1748 Error::kNoError) {
1749 if (delay_median >= 0) {
1750 stats.delay_median_ms = rtc::Optional<int32_t>(delay_median);
1751 }
1752 if (delay_std >= 0) {
1753 stats.delay_standard_deviation_ms = rtc::Optional<int32_t>(delay_std);
1754 }
1755 }
1756 }
1757 return stats;
1758}
1759
niklase@google.com470e71d2011-07-07 08:21:25 +00001760EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
peahb624d8c2016-03-05 03:01:14 -08001761 return public_submodules_->echo_cancellation.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001762}
1763
1764EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
peahbb9edbd2016-03-10 12:54:25 -08001765 return public_submodules_->echo_control_mobile.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001766}
1767
1768GainControl* AudioProcessingImpl::gain_control() const {
peahbe615622016-02-13 16:40:47 -08001769 if (constants_.use_experimental_agc) {
1770 return public_submodules_->gain_control_for_experimental_agc.get();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001771 }
peahbfa97112016-03-10 21:09:04 -08001772 return public_submodules_->gain_control.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001773}
1774
1775HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
peah8271d042016-11-22 07:24:52 -08001776 return high_pass_filter_impl_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001777}
1778
1779LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001780 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001781}
1782
1783NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
solenberg5e465c32015-12-08 13:22:33 -08001784 return public_submodules_->noise_suppression.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001785}
1786
1787VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001788 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001789}
1790
peah8271d042016-11-22 07:24:52 -08001791void AudioProcessingImpl::MutateConfig(
1792 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1793 rtc::CritScope cs_render(&crit_render_);
1794 rtc::CritScope cs_capture(&crit_capture_);
1795 mutator(&config_);
1796 ApplyConfig(config_);
1797}
1798
1799AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1800 rtc::CritScope cs_render(&crit_render_);
1801 rtc::CritScope cs_capture(&crit_capture_);
1802 return config_;
1803}
1804
peah2ace3f92016-09-10 04:42:27 -07001805bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1806 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001807 config_.high_pass_filter.enabled,
peah2ace3f92016-09-10 04:42:27 -07001808 public_submodules_->echo_cancellation->is_enabled(),
1809 public_submodules_->echo_control_mobile->is_enabled(),
ivoc9f4a4a02016-10-28 05:39:16 -07001810 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001811 public_submodules_->noise_suppression->is_enabled(),
1812 capture_nonlocked_.intelligibility_enabled,
1813 capture_nonlocked_.beamformer_enabled,
1814 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001815 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001816 capture_nonlocked_.echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -07001817 public_submodules_->voice_detection->is_enabled(),
1818 public_submodules_->level_estimator->is_enabled(),
1819 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001820}
1821
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001822
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001823void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001824 if (capture_.transient_suppressor_enabled) {
1825 if (!public_submodules_->transient_suppressor.get()) {
1826 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001827 }
peahdf3efa82015-11-28 12:35:15 -08001828 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001829 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1830 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001831 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001832}
1833
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001834void AudioProcessingImpl::InitializeBeamformer() {
aluebsb2328d12016-01-11 20:32:29 -08001835 if (capture_nonlocked_.beamformer_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001836 if (!private_submodules_->beamformer) {
1837 private_submodules_->beamformer.reset(new NonlinearBeamformer(
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001838 capture_.array_geometry, 1u, capture_.target_direction));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001839 }
peahdf3efa82015-11-28 12:35:15 -08001840 private_submodules_->beamformer->Initialize(kChunkSizeMs,
1841 capture_nonlocked_.split_rate);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001842 }
1843}
1844
ekmeyerson60d9b332015-08-14 10:35:55 -07001845void AudioProcessingImpl::InitializeIntelligibility() {
peah1bcfce52016-08-26 07:16:04 -07001846#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001847 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001848 public_submodules_->intelligibility_enhancer.reset(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -08001849 new IntelligibilityEnhancer(capture_nonlocked_.split_rate,
Alex Luebs57ae8292016-03-09 16:24:34 +01001850 render_.render_audio->num_channels(),
Alejandro Luebsef009252016-09-20 14:51:56 -07001851 render_.render_audio->num_bands(),
Alex Luebs57ae8292016-03-09 16:24:34 +01001852 NoiseSuppressionImpl::num_noise_bins()));
ekmeyerson60d9b332015-08-14 10:35:55 -07001853 }
peah1bcfce52016-08-26 07:16:04 -07001854#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001855}
1856
peah8271d042016-11-22 07:24:52 -08001857void AudioProcessingImpl::InitializeLowCutFilter() {
1858 if (config_.high_pass_filter.enabled) {
1859 private_submodules_->low_cut_filter.reset(
1860 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1861 } else {
1862 private_submodules_->low_cut_filter.reset();
1863 }
1864}
alessiob3ec96df2017-05-22 06:57:06 -07001865
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001866void AudioProcessingImpl::InitializeEchoController() {
Gustaf Ullberg002ef282017-10-12 15:13:17 +02001867 if (echo_control_factory_) {
1868 private_submodules_->echo_controller =
1869 echo_control_factory_->Create(proc_sample_rate_hz());
peahe0eae3c2016-12-14 01:16:23 -08001870 } else {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001871 private_submodules_->echo_controller.reset();
peahe0eae3c2016-12-14 01:16:23 -08001872 }
1873}
peah8271d042016-11-22 07:24:52 -08001874
alessiob3ec96df2017-05-22 06:57:06 -07001875void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001876 if (config_.gain_controller2.enabled) {
1877 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001878 }
1879}
1880
Alex Loikob5c9a792018-04-16 16:31:22 +02001881void AudioProcessingImpl::InitializePreAmplifier() {
1882 if (config_.pre_amplifier.enabled) {
1883 private_submodules_->pre_amplifier.reset(
1884 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1885 } else {
1886 private_submodules_->pre_amplifier.reset();
1887 }
1888}
1889
ivoc9f4a4a02016-10-28 05:39:16 -07001890void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001891 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001892 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001893 proc_sample_rate_hz(), 1,
1894 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001895}
1896
Sam Zackrisson0beac582017-09-25 12:04:02 +02001897void AudioProcessingImpl::InitializePostProcessor() {
1898 if (private_submodules_->capture_post_processor) {
1899 private_submodules_->capture_post_processor->Initialize(
1900 proc_sample_rate_hz(), num_proc_channels());
1901 }
1902}
1903
Alex Loiko5825aa62017-12-18 16:02:40 +01001904void AudioProcessingImpl::InitializePreProcessor() {
1905 if (private_submodules_->render_pre_processor) {
1906 private_submodules_->render_pre_processor->Initialize(
1907 formats_.render_processing_format.sample_rate_hz(),
1908 formats_.render_processing_format.num_channels());
1909 }
1910}
1911
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001912void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001913 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001914
1915 if (echo_cancellation()->is_enabled()) {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001916 // Activate delay_jumps_ counters if we know echo_cancellation is running.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001917 // If a stream has echo we know that the echo_cancellation is in process.
peahdf3efa82015-11-28 12:35:15 -08001918 if (capture_.stream_delay_jumps == -1 &&
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001919 echo_cancellation()->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001920 capture_.stream_delay_jumps = 0;
1921 }
1922 if (capture_.aec_system_delay_jumps == -1 &&
1923 echo_cancellation()->stream_has_echo()) {
1924 capture_.aec_system_delay_jumps = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001925 }
1926
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001927 // Detect a jump in platform reported system delay and log the difference.
peahdf3efa82015-11-28 12:35:15 -08001928 const int diff_stream_delay_ms =
1929 capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
1930 if (diff_stream_delay_ms > kMinDiffDelayMs &&
1931 capture_.last_stream_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001932 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1933 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
peahdf3efa82015-11-28 12:35:15 -08001934 if (capture_.stream_delay_jumps == -1) {
1935 capture_.stream_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001936 }
peahdf3efa82015-11-28 12:35:15 -08001937 capture_.stream_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001938 }
peahdf3efa82015-11-28 12:35:15 -08001939 capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001940
1941 // Detect a jump in AEC system delay and log the difference.
peah20028c42016-03-04 11:50:54 -08001942 const int samples_per_ms =
peahdf3efa82015-11-28 12:35:15 -08001943 rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
peah20028c42016-03-04 11:50:54 -08001944 RTC_DCHECK_LT(0, samples_per_ms);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001945 const int aec_system_delay_ms =
peah20028c42016-03-04 11:50:54 -08001946 public_submodules_->echo_cancellation->GetSystemDelayInSamples() /
1947 samples_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001948 const int diff_aec_system_delay_ms =
peahdf3efa82015-11-28 12:35:15 -08001949 aec_system_delay_ms - capture_.last_aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001950 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
peahdf3efa82015-11-28 12:35:15 -08001951 capture_.last_aec_system_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001952 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1953 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1954 100);
peahdf3efa82015-11-28 12:35:15 -08001955 if (capture_.aec_system_delay_jumps == -1) {
1956 capture_.aec_system_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001957 }
peahdf3efa82015-11-28 12:35:15 -08001958 capture_.aec_system_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001959 }
peahdf3efa82015-11-28 12:35:15 -08001960 capture_.last_aec_system_delay_ms = aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001961 }
1962}
1963
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001964void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
peahdf3efa82015-11-28 12:35:15 -08001965 // Run in a single-threaded manner.
1966 rtc::CritScope cs_render(&crit_render_);
1967 rtc::CritScope cs_capture(&crit_capture_);
1968
1969 if (capture_.stream_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001970 RTC_HISTOGRAM_ENUMERATION(
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001971 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
peahdf3efa82015-11-28 12:35:15 -08001972 capture_.stream_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001973 }
peahdf3efa82015-11-28 12:35:15 -08001974 capture_.stream_delay_jumps = -1;
1975 capture_.last_stream_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001976
peahdf3efa82015-11-28 12:35:15 -08001977 if (capture_.aec_system_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001978 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1979 capture_.aec_system_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001980 }
peahdf3efa82015-11-28 12:35:15 -08001981 capture_.aec_system_delay_jumps = -1;
1982 capture_.last_aec_system_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001983}
1984
aleloi868f32f2017-05-23 07:20:05 -07001985void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1986 if (!aec_dump_) {
1987 return;
1988 }
1989 std::string experiments_description =
1990 public_submodules_->echo_cancellation->GetExperimentsDescription();
1991 // TODO(peah): Add semicolon-separated concatenations of experiment
1992 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001993 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1994 experiments_description += "AgcClippingLevelExperiment;";
1995 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001996 if (capture_nonlocked_.echo_controller_enabled) {
1997 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001998 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001999 if (config_.gain_controller2.enabled) {
2000 experiments_description += "GainController2;";
2001 }
aleloi868f32f2017-05-23 07:20:05 -07002002
2003 InternalAPMConfig apm_config;
2004
2005 apm_config.aec_enabled = public_submodules_->echo_cancellation->is_enabled();
2006 apm_config.aec_delay_agnostic_enabled =
2007 public_submodules_->echo_cancellation->is_delay_agnostic_enabled();
2008 apm_config.aec_drift_compensation_enabled =
2009 public_submodules_->echo_cancellation->is_drift_compensation_enabled();
2010 apm_config.aec_extended_filter_enabled =
2011 public_submodules_->echo_cancellation->is_extended_filter_enabled();
2012 apm_config.aec_suppression_level = static_cast<int>(
2013 public_submodules_->echo_cancellation->suppression_level());
2014
2015 apm_config.aecm_enabled =
2016 public_submodules_->echo_control_mobile->is_enabled();
2017 apm_config.aecm_comfort_noise_enabled =
2018 public_submodules_->echo_control_mobile->is_comfort_noise_enabled();
2019 apm_config.aecm_routing_mode =
2020 static_cast<int>(public_submodules_->echo_control_mobile->routing_mode());
2021
2022 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2023 apm_config.agc_mode =
2024 static_cast<int>(public_submodules_->gain_control->mode());
2025 apm_config.agc_limiter_enabled =
2026 public_submodules_->gain_control->is_limiter_enabled();
2027 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2028
2029 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2030
2031 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2032 apm_config.ns_level =
2033 static_cast<int>(public_submodules_->noise_suppression->level());
2034
2035 apm_config.transient_suppression_enabled =
2036 capture_.transient_suppressor_enabled;
2037 apm_config.intelligibility_enhancer_enabled =
2038 capture_nonlocked_.intelligibility_enabled;
2039 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002040 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2041 apm_config.pre_amplifier_fixed_gain_factor =
2042 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002043
2044 if (!forced && apm_config == apm_config_for_aec_dump_) {
2045 return;
2046 }
2047 aec_dump_->WriteConfig(apm_config);
2048 apm_config_for_aec_dump_ = apm_config;
2049}
2050
2051void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2052 const float* const* src) {
2053 RTC_DCHECK(aec_dump_);
2054 WriteAecDumpConfigMessage(false);
2055
2056 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2057 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2058 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002059 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002060 RecordAudioProcessingState();
2061}
2062
2063void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2064 const AudioFrame& capture_frame) {
2065 RTC_DCHECK(aec_dump_);
2066 WriteAecDumpConfigMessage(false);
2067
2068 aec_dump_->AddCaptureStreamInput(capture_frame);
2069 RecordAudioProcessingState();
2070}
2071
2072void AudioProcessingImpl::RecordProcessedCaptureStream(
2073 const float* const* processed_capture_stream) {
2074 RTC_DCHECK(aec_dump_);
2075
2076 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2077 const size_t num_channels =
2078 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002079 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2080 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002081 aec_dump_->WriteCaptureStreamMessage();
2082}
2083
2084void AudioProcessingImpl::RecordProcessedCaptureStream(
2085 const AudioFrame& processed_capture_frame) {
2086 RTC_DCHECK(aec_dump_);
2087
2088 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2089 aec_dump_->WriteCaptureStreamMessage();
2090}
2091
2092void AudioProcessingImpl::RecordAudioProcessingState() {
2093 RTC_DCHECK(aec_dump_);
2094 AecDump::AudioProcessingState audio_proc_state;
2095 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2096 audio_proc_state.drift =
2097 public_submodules_->echo_cancellation->stream_drift_samples();
2098 audio_proc_state.level = gain_control()->stream_analog_level();
2099 audio_proc_state.keypress = capture_.key_pressed;
2100 aec_dump_->AddAudioProcessingState(audio_proc_state);
2101}
2102
kwiberg83ffe452016-08-29 14:46:07 -07002103AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
2104 bool transient_suppressor_enabled,
2105 const std::vector<Point>& array_geometry,
2106 SphericalPointf target_direction)
2107 : aec_system_delay_jumps(-1),
2108 delay_offset_ms(0),
2109 was_stream_delay_set(false),
2110 last_stream_delay_ms(0),
2111 last_aec_system_delay_ms(0),
2112 stream_delay_jumps(-1),
2113 output_will_be_muted(false),
2114 key_pressed(false),
2115 transient_suppressor_enabled(transient_suppressor_enabled),
2116 array_geometry(array_geometry),
2117 target_direction(target_direction),
peahde65ddc2016-09-16 15:02:15 -07002118 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002119 split_rate(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002120 echo_path_gain_change(false) {}
kwiberg83ffe452016-08-29 14:46:07 -07002121
2122AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2123
2124AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2125
2126AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2127
niklase@google.com470e71d2011-07-07 08:21:25 +00002128} // namespace webrtc