blob: 0443c114e324117b1cc3df6ee0586416dda2ef49 [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,
Ivo Creusend1f970d2018-06-14 11:02:03 +0200288 rtc::scoped_refptr<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 Creusend1f970d2018-06-14 11:02:03 +0200298 rtc::scoped_refptr<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(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200333 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100334 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 Creusend1f970d2018-06-14 11:02:03 +0200366 rtc::scoped_refptr<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) {
Ivo Creusend1f970d2018-06-14 11:02:03 +0200425 private_submodules_->echo_detector =
426 new rtc::RefCountedObject<ResidualEchoDetector>();
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100427 }
peahca4cac72016-06-29 15:26:12 -0700428
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200429 // TODO(alessiob): Move the injected gain controller once injection is
430 // implemented.
431 private_submodules_->gain_controller2.reset(new GainController2());
432
Mirko Bonadei675513b2017-11-09 11:09:25 +0100433 RTC_LOG(LS_INFO) << "Capture post processor activated: "
Jonas Olsson645b0272018-02-15 15:16:27 +0100434 << !!private_submodules_->capture_post_processor
435 << "\nRender pre processor activated: "
Alex Loiko5825aa62017-12-18 16:02:40 +0100436 << !!private_submodules_->render_pre_processor;
peahdf3efa82015-11-28 12:35:15 -0800437 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000438
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000439 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000440}
441
442AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800443 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800444 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800445 private_submodules_->agc_manager.reset();
446 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800447 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000448}
449
niklase@google.com470e71d2011-07-07 08:21:25 +0000450int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800451 // Run in a single-threaded manner during initialization.
452 rtc::CritScope cs_render(&crit_render_);
453 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000454 return InitializeLocked();
455}
456
peahde65ddc2016-09-16 15:02:15 -0700457int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
458 int capture_output_sample_rate_hz,
459 int render_input_sample_rate_hz,
460 ChannelLayout capture_input_layout,
461 ChannelLayout capture_output_layout,
462 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700463 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700464 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
465 LayoutHasKeyboard(capture_input_layout)},
466 {capture_output_sample_rate_hz,
467 ChannelsFromLayout(capture_output_layout),
468 LayoutHasKeyboard(capture_output_layout)},
469 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
470 LayoutHasKeyboard(render_input_layout)},
471 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
472 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700473
474 return Initialize(processing_config);
475}
476
477int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800478 // Run in a single-threaded manner during initialization.
479 rtc::CritScope cs_render(&crit_render_);
480 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700481 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000482}
483
peahdf3efa82015-11-28 12:35:15 -0800484int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800485 const ProcessingConfig& processing_config) {
peah2ace3f92016-09-10 04:42:27 -0700486 return MaybeInitialize(processing_config, false);
peah81b9bfe2015-11-27 02:47:28 -0800487}
488
peahdf3efa82015-11-28 12:35:15 -0800489int AudioProcessingImpl::MaybeInitializeCapture(
peah2ace3f92016-09-10 04:42:27 -0700490 const ProcessingConfig& processing_config,
491 bool force_initialization) {
492 return MaybeInitialize(processing_config, force_initialization);
peah81b9bfe2015-11-27 02:47:28 -0800493}
494
peah192164e2015-11-17 02:16:45 -0800495// Calls InitializeLocked() if any of the audio parameters have changed from
peahdf3efa82015-11-28 12:35:15 -0800496// their current values (needs to be called while holding the crit_render_lock).
497int AudioProcessingImpl::MaybeInitialize(
peah2ace3f92016-09-10 04:42:27 -0700498 const ProcessingConfig& processing_config,
499 bool force_initialization) {
peahdf3efa82015-11-28 12:35:15 -0800500 // Called from both threads. Thread check is therefore not possible.
peah2ace3f92016-09-10 04:42:27 -0700501 if (processing_config == formats_.api_format && !force_initialization) {
peah192164e2015-11-17 02:16:45 -0800502 return kNoError;
503 }
peahdf3efa82015-11-28 12:35:15 -0800504
505 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800506 return InitializeLocked(processing_config);
507}
508
niklase@google.com470e71d2011-07-07 08:21:25 +0000509int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200510 UpdateActiveSubmoduleStates();
511
peah522d71b2017-02-23 05:16:26 -0800512 const int capture_audiobuffer_num_channels =
513 capture_nonlocked_.beamformer_enabled
514 ? formats_.api_format.input_stream().num_channels()
515 : formats_.api_format.output_stream().num_channels();
516
peahde65ddc2016-09-16 15:02:15 -0700517 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800518 formats_.api_format.reverse_output_stream().num_frames() == 0
peahde65ddc2016-09-16 15:02:15 -0700519 ? formats_.render_processing_format.num_frames()
peahdf3efa82015-11-28 12:35:15 -0800520 : formats_.api_format.reverse_output_stream().num_frames();
521 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
522 render_.render_audio.reset(new AudioBuffer(
523 formats_.api_format.reverse_input_stream().num_frames(),
524 formats_.api_format.reverse_input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700525 formats_.render_processing_format.num_frames(),
526 formats_.render_processing_format.num_channels(),
527 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700528 if (formats_.api_format.reverse_input_stream() !=
529 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800530 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800531 formats_.api_format.reverse_input_stream().num_channels(),
532 formats_.api_format.reverse_input_stream().num_frames(),
533 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800534 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700535 } else {
peahdf3efa82015-11-28 12:35:15 -0800536 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700537 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700538 } else {
peahdf3efa82015-11-28 12:35:15 -0800539 render_.render_audio.reset(nullptr);
540 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700541 }
peahce4d9152017-05-19 01:28:05 -0700542
peahdf3efa82015-11-28 12:35:15 -0800543 capture_.capture_audio.reset(
544 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
545 formats_.api_format.input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700546 capture_nonlocked_.capture_processing_format.num_frames(),
547 capture_audiobuffer_num_channels,
peahdf3efa82015-11-28 12:35:15 -0800548 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000549
peahde65ddc2016-09-16 15:02:15 -0700550 public_submodules_->echo_cancellation->Initialize(
551 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
552 num_proc_channels());
peah764e3642016-10-22 05:04:30 -0700553 AllocateRenderQueue();
554
ivoc3e9a5372016-10-28 07:55:33 -0700555 int success = public_submodules_->echo_cancellation->enable_metrics(true);
556 RTC_DCHECK_EQ(0, success);
557 success = public_submodules_->echo_cancellation->enable_delay_logging(true);
558 RTC_DCHECK_EQ(0, success);
peahde65ddc2016-09-16 15:02:15 -0700559 public_submodules_->echo_control_mobile->Initialize(
560 proc_split_sample_rate_hz(), num_reverse_channels(),
561 num_output_channels());
peah135259a2016-10-28 03:12:11 -0700562
563 public_submodules_->gain_control->Initialize(num_proc_channels(),
564 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700565 if (constants_.use_experimental_agc) {
566 if (!private_submodules_->agc_manager.get()) {
567 private_submodules_->agc_manager.reset(new AgcManagerDirect(
568 public_submodules_->gain_control.get(),
569 public_submodules_->gain_control_for_experimental_agc.get(),
henrik.lundinbd681b92016-12-05 09:08:42 -0800570 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min));
peahde65ddc2016-09-16 15:02:15 -0700571 }
572 private_submodules_->agc_manager->Initialize();
573 private_submodules_->agc_manager->SetCaptureMuted(
574 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700575 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700576 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200577 InitializeTransient();
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000578 InitializeBeamformer();
peah1bcfce52016-08-26 07:16:04 -0700579#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -0700580 InitializeIntelligibility();
peah1bcfce52016-08-26 07:16:04 -0700581#endif
peah8271d042016-11-22 07:24:52 -0800582 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700583 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
584 proc_sample_rate_hz());
585 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
586 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700587 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200588 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700589 InitializeGainController2();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200590 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100591 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800592
aleloi868f32f2017-05-23 07:20:05 -0700593 if (aec_dump_) {
Alex Loiko1aec5942018-05-15 13:13:22 +0200594 aec_dump_->WriteInitMessage(formats_.api_format);
aleloi868f32f2017-05-23 07:20:05 -0700595 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000596 return kNoError;
597}
598
Michael Graczyk86c6d332015-07-23 11:41:39 -0700599int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200600 UpdateActiveSubmoduleStates();
601
Michael Graczyk86c6d332015-07-23 11:41:39 -0700602 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700603 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
604 return kBadSampleRateError;
605 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000606 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700607
Peter Kasting69558702016-01-12 16:26:35 -0800608 const size_t num_in_channels = config.input_stream().num_channels();
609 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700610
611 // Need at least one input channel.
612 // Need either one output channel or as many outputs as there are inputs.
613 if (num_in_channels == 0 ||
614 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700615 return kBadNumberChannelsError;
616 }
617
aluebsb2328d12016-01-11 20:32:29 -0800618 if (capture_nonlocked_.beamformer_enabled &&
Peter Kasting69558702016-01-12 16:26:35 -0800619 num_in_channels != capture_.array_geometry.size()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700620 return kBadNumberChannelsError;
621 }
622
peahdf3efa82015-11-28 12:35:15 -0800623 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000624
peahde65ddc2016-09-16 15:02:15 -0700625 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700626 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700627 formats_.api_format.output_stream().sample_rate_hz()),
628 submodule_states_.CaptureMultiBandSubModulesActive() ||
629 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000630
peahde65ddc2016-09-16 15:02:15 -0700631 capture_nonlocked_.capture_processing_format =
632 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700633
peah2ce640f2017-04-07 03:57:48 -0700634 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200635 if (!capture_nonlocked_.echo_controller_enabled) {
peah2ce640f2017-04-07 03:57:48 -0700636 render_processing_rate = FindNativeProcessRateToUse(
637 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
638 formats_.api_format.reverse_output_stream().sample_rate_hz()),
639 submodule_states_.CaptureMultiBandSubModulesActive() ||
640 submodule_states_.RenderMultiBandSubModulesActive());
641 } else {
642 render_processing_rate = capture_processing_rate;
643 }
644
aluebseb3603b2016-04-20 15:27:58 -0700645 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
646 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700647 if (render_processing_rate > kSampleRate32kHz &&
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200648 !capture_nonlocked_.echo_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -0700649 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
650 ? kSampleRate32kHz
651 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700652 }
peah2ce640f2017-04-07 03:57:48 -0700653
peahde65ddc2016-09-16 15:02:15 -0700654 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700655 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700656 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
657 kSampleRate8kHz) {
658 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000659 } else {
peahde65ddc2016-09-16 15:02:15 -0700660 render_processing_rate =
661 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000662 }
663
peahde65ddc2016-09-16 15:02:15 -0700664 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000665 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700666 if (submodule_states_.RenderMultiBandSubModulesActive()) {
667 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
668 } else {
669 formats_.render_processing_format = StreamConfig(
670 formats_.api_format.reverse_input_stream().sample_rate_hz(),
671 formats_.api_format.reverse_input_stream().num_channels());
672 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000673
peahde65ddc2016-09-16 15:02:15 -0700674 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
675 kSampleRate32kHz ||
676 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
677 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800678 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000679 } else {
peahdf3efa82015-11-28 12:35:15 -0800680 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700681 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000682 }
683
684 return InitializeLocked();
685}
686
peah88ac8532016-09-12 16:47:25 -0700687void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peahc19f3122016-10-07 14:54:10 -0700688 config_ = config;
peah88ac8532016-09-12 16:47:25 -0700689
peah88ac8532016-09-12 16:47:25 -0700690 // Run in a single-threaded manner when applying the settings.
691 rtc::CritScope cs_render(&crit_render_);
692 rtc::CritScope cs_capture(&crit_capture_);
693
peah8271d042016-11-22 07:24:52 -0800694 InitializeLowCutFilter();
695
Mirko Bonadei675513b2017-11-09 11:09:25 +0100696 RTC_LOG(LS_INFO) << "Highpass filter activated: "
697 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800698
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100699 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700700 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100701 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
702 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100703 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100704 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700705 config_.gain_controller2 = AudioProcessing::Config::GainController2();
706 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200707 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200708 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200709 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100710 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
711 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200712 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
713 << config_.pre_amplifier.enabled;
peah88ac8532016-09-12 16:47:25 -0700714}
715
716void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800717 // Run in a single-threaded manner when setting the extra options.
718 rtc::CritScope cs_render(&crit_render_);
719 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000720
peahb624d8c2016-03-05 03:01:14 -0800721 public_submodules_->echo_cancellation->SetExtraOptions(config);
722
peahdf3efa82015-11-28 12:35:15 -0800723 if (capture_.transient_suppressor_enabled !=
724 config.Get<ExperimentalNs>().enabled) {
725 capture_.transient_suppressor_enabled =
726 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000727 InitializeTransient();
728 }
aluebs2a346882016-01-11 18:04:30 -0800729
peah1bcfce52016-08-26 07:16:04 -0700730#if WEBRTC_INTELLIGIBILITY_ENHANCER
alessiob3ec96df2017-05-22 06:57:06 -0700731 if (capture_nonlocked_.intelligibility_enabled !=
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700732 config.Get<Intelligibility>().enabled) {
733 capture_nonlocked_.intelligibility_enabled =
734 config.Get<Intelligibility>().enabled;
735 InitializeIntelligibility();
736 }
peah1bcfce52016-08-26 07:16:04 -0700737#endif
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700738
aluebs2a346882016-01-11 18:04:30 -0800739#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
aluebsb2328d12016-01-11 20:32:29 -0800740 if (capture_nonlocked_.beamformer_enabled !=
741 config.Get<Beamforming>().enabled) {
742 capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled;
aluebs2a346882016-01-11 18:04:30 -0800743 if (config.Get<Beamforming>().array_geometry.size() > 1) {
744 capture_.array_geometry = config.Get<Beamforming>().array_geometry;
745 }
746 capture_.target_direction = config.Get<Beamforming>().target_direction;
747 InitializeBeamformer();
748 }
749#endif // WEBRTC_ANDROID_PLATFORM_BUILD
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000750}
751
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000752int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800753 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700754 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000755}
756
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000757int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800758 // Used as callback from submodules, hence locking is not allowed.
759 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000760}
761
Peter Kasting69558702016-01-12 16:26:35 -0800762size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800763 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700764 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000765}
766
Peter Kasting69558702016-01-12 16:26:35 -0800767size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800768 // Used as callback from submodules, hence locking is not allowed.
769 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000770}
771
Peter Kasting69558702016-01-12 16:26:35 -0800772size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800773 // Used as callback from submodules, hence locking is not allowed.
peahedddac52017-05-16 01:08:58 -0700774 return (capture_nonlocked_.beamformer_enabled ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200775 capture_nonlocked_.echo_controller_enabled)
peahedddac52017-05-16 01:08:58 -0700776 ? 1
777 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800778}
779
Peter Kasting69558702016-01-12 16:26:35 -0800780size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800781 // Used as callback from submodules, hence locking is not allowed.
782 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000783}
784
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000785void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800786 rtc::CritScope cs(&crit_capture_);
787 capture_.output_will_be_muted = muted;
788 if (private_submodules_->agc_manager.get()) {
789 private_submodules_->agc_manager->SetCaptureMuted(
790 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000791 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000792}
793
Alessio Bazzicac054e782018-04-16 12:10:09 +0200794void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200795 switch (setting.type()) {
796 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
797 render_runtime_settings_enqueuer_.Enqueue(setting);
798 return;
799 case RuntimeSetting::Type::kNotSpecified:
800 RTC_NOTREACHED();
801 return;
802 case RuntimeSetting::Type::kCapturePreGain:
803 capture_runtime_settings_enqueuer_.Enqueue(setting);
804 return;
805 }
806 // The language allows the enum to have a non-enumerator
807 // value. Check that this doesn't happen.
808 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200809}
810
811AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
812 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200813 : runtime_settings_(*runtime_settings) {
814 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200815}
816
817AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
818 default;
819
820void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
821 RuntimeSetting setting) {
822 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200823 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200824 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200825 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200826 RTC_LOG(LS_ERROR)
827 << "The runtime settings queue is full. Oldest setting discarded.";
828 }
829 if (remaining_attempts == 0)
830 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
831}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000832
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000833int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700834 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000835 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000836 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000837 int output_sample_rate_hz,
838 ChannelLayout output_layout,
839 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800840 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800841 StreamConfig input_stream;
842 StreamConfig output_stream;
843 {
844 // Access the formats_.api_format.input_stream beneath the capture lock.
845 // The lock must be released as it is later required in the call
846 // to ProcessStream(,,,);
847 rtc::CritScope cs(&crit_capture_);
848 input_stream = formats_.api_format.input_stream();
849 output_stream = formats_.api_format.output_stream();
850 }
851
Michael Graczyk86c6d332015-07-23 11:41:39 -0700852 input_stream.set_sample_rate_hz(input_sample_rate_hz);
853 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
854 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700855 output_stream.set_sample_rate_hz(output_sample_rate_hz);
856 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
857 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
858
859 if (samples_per_channel != input_stream.num_frames()) {
860 return kBadDataLengthError;
861 }
862 return ProcessStream(src, input_stream, output_stream, dest);
863}
864
865int AudioProcessingImpl::ProcessStream(const float* const* src,
866 const StreamConfig& input_config,
867 const StreamConfig& output_config,
868 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800869 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800870 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700871 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800872 {
873 // Acquire the capture lock in order to safely call the function
874 // that retrieves the render side data. This function accesses apm
875 // getters that need the capture lock held when being called.
876 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700877 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800878
879 if (!src || !dest) {
880 return kNullPointerError;
881 }
882
883 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700884 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000885 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000886
Michael Graczyk86c6d332015-07-23 11:41:39 -0700887 processing_config.input_stream() = input_config;
888 processing_config.output_stream() = output_config;
889
peahdf3efa82015-11-28 12:35:15 -0800890 {
891 // Do conditional reinitialization.
892 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -0700893 RETURN_ON_ERR(
894 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -0800895 }
896 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700897 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
898 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000899
aleloi868f32f2017-05-23 07:20:05 -0700900 if (aec_dump_) {
901 RecordUnprocessedCaptureStream(src);
902 }
903
peahdf3efa82015-11-28 12:35:15 -0800904 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700905 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800906 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000907
aleloi868f32f2017-05-23 07:20:05 -0700908 if (aec_dump_) {
909 RecordProcessedCaptureStream(dest);
910 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000911 return kNoError;
912}
913
Alex Loiko73ec0192018-05-15 10:52:28 +0200914void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200915 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200916 while (capture_runtime_settings_.Remove(&setting)) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200917 switch (setting.type()) {
918 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200919 if (config_.pre_amplifier.enabled) {
920 float value;
921 setting.GetFloat(&value);
922 private_submodules_->pre_amplifier->SetGainFactor(value);
923 }
924 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200925 break;
Alex Loiko73ec0192018-05-15 10:52:28 +0200926 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
927 RTC_NOTREACHED();
928 break;
929 case RuntimeSetting::Type::kNotSpecified:
930 RTC_NOTREACHED();
931 break;
932 }
933 }
934}
935
936void AudioProcessingImpl::HandleRenderRuntimeSettings() {
937 RuntimeSetting setting;
938 while (render_runtime_settings_.Remove(&setting)) {
939 switch (setting.type()) {
940 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
941 if (private_submodules_->render_pre_processor) {
942 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
943 }
944 break;
945 case RuntimeSetting::Type::kCapturePreGain:
946 RTC_NOTREACHED();
947 break;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200948 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +0200949 RTC_NOTREACHED();
950 break;
951 }
952 }
953}
954
peah9e6a2902017-05-15 07:19:21 -0700955void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
peah764e3642016-10-22 05:04:30 -0700956 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
957 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700958 &aec_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700959
kwibergaf476c72016-11-28 15:21:39 -0800960 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -0700961
962 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700963 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700964 // The data queue is full and needs to be emptied.
965 EmptyQueuedRenderAudio();
966
967 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700968 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700969 RTC_DCHECK(result);
970 }
971
972 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
973 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700974 &aecm_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700975
976 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700977 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700978 // The data queue is full and needs to be emptied.
979 EmptyQueuedRenderAudio();
980
981 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700982 bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700983 RTC_DCHECK(result);
984 }
peah701d6282016-10-25 05:42:20 -0700985
986 if (!constants_.use_experimental_agc) {
987 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
988 // Insert the samples into the queue.
989 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
990 // The data queue is full and needs to be emptied.
991 EmptyQueuedRenderAudio();
992
993 // Retry the insert (should always work).
994 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
995 RTC_DCHECK(result);
996 }
997 }
peah9e6a2902017-05-15 07:19:21 -0700998}
ivoc9f4a4a02016-10-28 05:39:16 -0700999
peah9e6a2902017-05-15 07:19:21 -07001000void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001001 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1002
1003 // Insert the samples into the queue.
1004 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1005 // The data queue is full and needs to be emptied.
1006 EmptyQueuedRenderAudio();
1007
1008 // Retry the insert (should always work).
1009 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1010 RTC_DCHECK(result);
1011 }
peah764e3642016-10-22 05:04:30 -07001012}
1013
1014void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001015 const size_t new_aec_render_queue_element_max_size =
peah764e3642016-10-22 05:04:30 -07001016 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -07001017 kMaxAllowedValuesOfSamplesPerBand *
peah764e3642016-10-22 05:04:30 -07001018 EchoCancellationImpl::NumCancellersRequired(
1019 num_output_channels(), num_reverse_channels()));
1020
peah701d6282016-10-25 05:42:20 -07001021 const size_t new_aecm_render_queue_element_max_size =
peaha0624602016-10-25 04:45:24 -07001022 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -07001023 kMaxAllowedValuesOfSamplesPerBand *
peaha0624602016-10-25 04:45:24 -07001024 EchoControlMobileImpl::NumCancellersRequired(
1025 num_output_channels(), num_reverse_channels()));
peah764e3642016-10-22 05:04:30 -07001026
peah701d6282016-10-25 05:42:20 -07001027 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001028 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001029
ivoc9f4a4a02016-10-28 05:39:16 -07001030 const size_t new_red_render_queue_element_max_size =
1031 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1032
peaha0624602016-10-25 04:45:24 -07001033 // Reallocate the queues if the queue item sizes are too small to fit the
1034 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001035 if (aec_render_queue_element_max_size_ <
1036 new_aec_render_queue_element_max_size) {
1037 aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
peah764e3642016-10-22 05:04:30 -07001038
peaha0624602016-10-25 04:45:24 -07001039 std::vector<float> template_queue_element(
peah701d6282016-10-25 05:42:20 -07001040 aec_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001041
peah701d6282016-10-25 05:42:20 -07001042 aec_render_signal_queue_.reset(
peah764e3642016-10-22 05:04:30 -07001043 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1044 kMaxNumFramesToBuffer, template_queue_element,
peaha0624602016-10-25 04:45:24 -07001045 RenderQueueItemVerifier<float>(
peah701d6282016-10-25 05:42:20 -07001046 aec_render_queue_element_max_size_)));
peah764e3642016-10-22 05:04:30 -07001047
peah701d6282016-10-25 05:42:20 -07001048 aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
1049 aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
peah764e3642016-10-22 05:04:30 -07001050 } else {
peah701d6282016-10-25 05:42:20 -07001051 aec_render_signal_queue_->Clear();
peaha0624602016-10-25 04:45:24 -07001052 }
1053
peah701d6282016-10-25 05:42:20 -07001054 if (aecm_render_queue_element_max_size_ <
1055 new_aecm_render_queue_element_max_size) {
1056 aecm_render_queue_element_max_size_ =
1057 new_aecm_render_queue_element_max_size;
peaha0624602016-10-25 04:45:24 -07001058
1059 std::vector<int16_t> template_queue_element(
peah701d6282016-10-25 05:42:20 -07001060 aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001061
peah701d6282016-10-25 05:42:20 -07001062 aecm_render_signal_queue_.reset(
peaha0624602016-10-25 04:45:24 -07001063 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1064 kMaxNumFramesToBuffer, template_queue_element,
1065 RenderQueueItemVerifier<int16_t>(
peah701d6282016-10-25 05:42:20 -07001066 aecm_render_queue_element_max_size_)));
peaha0624602016-10-25 04:45:24 -07001067
peah701d6282016-10-25 05:42:20 -07001068 aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
1069 aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001070 } else {
peah701d6282016-10-25 05:42:20 -07001071 aecm_render_signal_queue_->Clear();
1072 }
1073
1074 if (agc_render_queue_element_max_size_ <
1075 new_agc_render_queue_element_max_size) {
1076 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1077
1078 std::vector<int16_t> template_queue_element(
1079 agc_render_queue_element_max_size_);
1080
1081 agc_render_signal_queue_.reset(
1082 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1083 kMaxNumFramesToBuffer, template_queue_element,
1084 RenderQueueItemVerifier<int16_t>(
1085 agc_render_queue_element_max_size_)));
1086
1087 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1088 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1089 } else {
1090 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001091 }
ivoc9f4a4a02016-10-28 05:39:16 -07001092
1093 if (red_render_queue_element_max_size_ <
1094 new_red_render_queue_element_max_size) {
1095 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1096
1097 std::vector<float> template_queue_element(
1098 red_render_queue_element_max_size_);
1099
1100 red_render_signal_queue_.reset(
1101 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1102 kMaxNumFramesToBuffer, template_queue_element,
1103 RenderQueueItemVerifier<float>(
1104 red_render_queue_element_max_size_)));
1105
1106 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1107 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1108 } else {
1109 red_render_signal_queue_->Clear();
1110 }
peah764e3642016-10-22 05:04:30 -07001111}
1112
1113void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1114 rtc::CritScope cs_capture(&crit_capture_);
peah701d6282016-10-25 05:42:20 -07001115 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -07001116 public_submodules_->echo_cancellation->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001117 aec_capture_queue_buffer_);
peaha0624602016-10-25 04:45:24 -07001118 }
1119
peah701d6282016-10-25 05:42:20 -07001120 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -07001121 public_submodules_->echo_control_mobile->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001122 aecm_capture_queue_buffer_);
1123 }
1124
1125 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1126 public_submodules_->gain_control->ProcessRenderAudio(
1127 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001128 }
ivoc9f4a4a02016-10-28 05:39:16 -07001129
1130 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001131 RTC_DCHECK(private_submodules_->echo_detector);
1132 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001133 red_capture_queue_buffer_);
1134 }
peah764e3642016-10-22 05:04:30 -07001135}
1136
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001137int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001138 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001139 {
1140 // Acquire the capture lock in order to safely call the function
1141 // that retrieves the render side data. This function accesses apm
1142 // getters that need the capture lock held when being called.
1143 // The lock needs to be released as
1144 // public_submodules_->echo_control_mobile->is_enabled() aquires this lock
1145 // as well.
1146 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001147 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001148 }
peahfa6228e2015-11-16 16:27:42 -08001149
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001150 if (!frame) {
1151 return kNullPointerError;
1152 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001153 // Must be a native rate.
1154 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1155 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001156 frame->sample_rate_hz_ != kSampleRate32kHz &&
1157 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001158 return kBadSampleRateError;
1159 }
peah192164e2015-11-17 02:16:45 -08001160
peahdf3efa82015-11-28 12:35:15 -08001161 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001162 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001163 {
1164 // Aquire lock for the access of api_format.
1165 // The lock is released immediately due to the conditional
1166 // reinitialization.
1167 rtc::CritScope cs_capture(&crit_capture_);
1168 // TODO(ajm): The input and output rates and channels are currently
1169 // constrained to be identical in the int16 interface.
1170 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001171
1172 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001173 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001174 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1175 processing_config.input_stream().set_num_channels(frame->num_channels_);
1176 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1177 processing_config.output_stream().set_num_channels(frame->num_channels_);
1178
peahdf3efa82015-11-28 12:35:15 -08001179 {
1180 // Do conditional reinitialization.
1181 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -07001182 RETURN_ON_ERR(
1183 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -08001184 }
1185 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001186 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001187 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001188 return kBadDataLengthError;
1189 }
1190
aleloi868f32f2017-05-23 07:20:05 -07001191 if (aec_dump_) {
1192 RecordUnprocessedCaptureStream(*frame);
1193 }
1194
peahdf3efa82015-11-28 12:35:15 -08001195 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001196 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001197 capture_.capture_audio->InterleaveTo(
peah23ac8b42017-05-23 05:33:56 -07001198 frame, submodule_states_.CaptureMultiBandProcessingActive() ||
1199 submodule_states_.CaptureFullBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001200
aleloi868f32f2017-05-23 07:20:05 -07001201 if (aec_dump_) {
1202 RecordProcessedCaptureStream(*frame);
1203 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001204
1205 return kNoError;
1206}
1207
peahde65ddc2016-09-16 15:02:15 -07001208int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001209 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001210
peahb58a1582016-03-15 09:34:24 -07001211 // Ensure that not both the AEC and AECM are active at the same time.
1212 // TODO(peah): Simplify once the public API Enable functions for these
1213 // are moved to APM.
1214 RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&
1215 public_submodules_->echo_control_mobile->is_enabled()));
1216
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001217 MaybeUpdateHistograms();
1218
peahde65ddc2016-09-16 15:02:15 -07001219 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001220
Alex Loikob5c9a792018-04-16 16:31:22 +02001221 if (private_submodules_->pre_amplifier) {
1222 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
1223 capture_buffer->channels_f(), capture_buffer->num_channels(),
1224 capture_buffer->num_frames()));
1225 }
1226
peah1b08dc32016-12-20 13:45:58 -08001227 capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>(
henrik.lundin290d43a2016-11-29 08:09:09 -08001228 capture_buffer->channels_const()[0],
1229 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001230 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1231 if (log_rms) {
1232 capture_rms_interval_counter_ = 0;
1233 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001234 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1235 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1236 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1237 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001238 }
1239
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001240 if (private_submodules_->echo_controller) {
Per Åhgren9aed31c2017-06-29 20:23:27 +02001241 // TODO(peah): Reactivate analogue AGC gain detection once the analogue AGC
1242 // issues have been addressed.
1243 capture_.echo_path_gain_change = false;
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001244 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001245 }
1246
peahbe615622016-02-13 16:40:47 -08001247 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001248 public_submodules_->gain_control->is_enabled()) {
1249 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001250 capture_buffer->channels()[0], capture_buffer->num_channels(),
1251 capture_nonlocked_.capture_processing_format.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001252 }
1253
peah2ace3f92016-09-10 04:42:27 -07001254 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1255 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001256 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1257 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001258 }
1259
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001260 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001261 // Force down-mixing of the number of channels after the detection of
1262 // capture signal saturation.
1263 // TODO(peah): Look into ensuring that this kind of tampering with the
1264 // AudioBuffer functionality should not be needed.
1265 capture_buffer->set_num_channels(1);
1266 }
1267
aluebsb2328d12016-01-11 20:32:29 -08001268 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001269 private_submodules_->beamformer->AnalyzeChunk(
1270 *capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001271 // Discards all channels by the leftmost one.
peahde65ddc2016-09-16 15:02:15 -07001272 capture_buffer->set_num_channels(1);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001273 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001274
peahe0eae3c2016-12-14 01:16:23 -08001275 // TODO(peah): Move the AEC3 low-cut filter to this place.
1276 if (private_submodules_->low_cut_filter &&
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001277 !private_submodules_->echo_controller) {
peah8271d042016-11-22 07:24:52 -08001278 private_submodules_->low_cut_filter->Process(capture_buffer);
1279 }
peahde65ddc2016-09-16 15:02:15 -07001280 RETURN_ON_ERR(
1281 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1282 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001283
1284 // Ensure that the stream delay was set before the call to the
1285 // AEC ProcessCaptureAudio function.
1286 if (public_submodules_->echo_cancellation->is_enabled() &&
Per Åhgren0dfd3722018-05-06 18:16:01 +02001287 !private_submodules_->echo_controller && !was_stream_delay_set()) {
peahb58a1582016-03-15 09:34:24 -07001288 return AudioProcessing::kStreamParameterNotSetError;
1289 }
1290
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001291 if (private_submodules_->echo_controller) {
Per Åhgren13735822018-02-12 21:42:56 +01001292 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1293
Per Åhgrend0fa8202018-04-18 09:35:13 +02001294 if (was_stream_delay_set()) {
1295 private_submodules_->echo_controller->SetAudioBufferDelay(
1296 stream_delay_ms());
1297 }
1298
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001299 private_submodules_->echo_controller->ProcessCapture(
peah67995532017-04-10 14:12:41 -07001300 capture_buffer, capture_.echo_path_gain_change);
peah61202ac2017-02-06 03:39:42 -08001301 } else {
1302 RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(
1303 capture_buffer, stream_delay_ms()));
peahe0eae3c2016-12-14 01:16:23 -08001304 }
1305
peahdf3efa82015-11-28 12:35:15 -08001306 if (public_submodules_->echo_control_mobile->is_enabled() &&
1307 public_submodules_->noise_suppression->is_enabled()) {
peahde65ddc2016-09-16 15:02:15 -07001308 capture_buffer->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +00001309 }
peahde65ddc2016-09-16 15:02:15 -07001310 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah1bcfce52016-08-26 07:16:04 -07001311#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001312 if (capture_nonlocked_.intelligibility_enabled) {
aluebsc466bad2016-02-10 12:03:00 -08001313 RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());
Sam Zackrissonab1aee02018-03-05 15:59:06 +01001314 const int gain_db =
1315 public_submodules_->gain_control->is_enabled()
1316 ? public_submodules_->gain_control->compression_gain_db()
1317 : 0;
1318 const float gain = DbToRatio(gain_db);
aluebsc466bad2016-02-10 12:03:00 -08001319 public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -07001320 public_submodules_->noise_suppression->NoiseEstimate(), gain);
aluebsc466bad2016-02-10 12:03:00 -08001321 }
peah1bcfce52016-08-26 07:16:04 -07001322#endif
peah253534d2016-03-15 04:32:28 -07001323
1324 // Ensure that the stream delay was set before the call to the
1325 // AECM ProcessCaptureAudio function.
1326 if (public_submodules_->echo_control_mobile->is_enabled() &&
1327 !was_stream_delay_set()) {
1328 return AudioProcessing::kStreamParameterNotSetError;
1329 }
1330
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001331 if (!(private_submodules_->echo_controller ||
Per Åhgren46537a32017-06-07 10:08:10 +02001332 public_submodules_->echo_cancellation->is_enabled())) {
1333 RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(
1334 capture_buffer, stream_delay_ms()));
1335 }
ivoc9f4a4a02016-10-28 05:39:16 -07001336
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001337 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001338 private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001339 }
1340
peahde65ddc2016-09-16 15:02:15 -07001341 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001342
peahbe615622016-02-13 16:40:47 -08001343 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001344 public_submodules_->gain_control->is_enabled() &&
aluebsb2328d12016-01-11 20:32:29 -08001345 (!capture_nonlocked_.beamformer_enabled ||
peahdf3efa82015-11-28 12:35:15 -08001346 private_submodules_->beamformer->is_target_present())) {
1347 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001348 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1349 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001350 }
peahb8fbb542016-03-15 02:28:08 -07001351 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001352 capture_buffer, echo_cancellation()->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001353
peah2ace3f92016-09-10 04:42:27 -07001354 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1355 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001356 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1357 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001358 }
1359
peah9e6a2902017-05-15 07:19:21 -07001360 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001361 RTC_DCHECK(private_submodules_->echo_detector);
1362 private_submodules_->echo_detector->AnalyzeCaptureAudio(
peah9e6a2902017-05-15 07:19:21 -07001363 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
1364 capture_buffer->num_frames()));
1365 }
1366
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001367 // TODO(aluebs): Investigate if the transient suppression placement should be
1368 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001369 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001370 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001371 private_submodules_->agc_manager.get()
1372 ? private_submodules_->agc_manager->voice_probability()
1373 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001374
peahdf3efa82015-11-28 12:35:15 -08001375 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001376 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1377 capture_buffer->num_channels(),
1378 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1379 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1380 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001381 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001382 }
1383
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001384 if (config_.gain_controller2.enabled) {
alessiob3ec96df2017-05-22 06:57:06 -07001385 private_submodules_->gain_controller2->Process(capture_buffer);
1386 }
1387
Sam Zackrisson0beac582017-09-25 12:04:02 +02001388 if (private_submodules_->capture_post_processor) {
1389 private_submodules_->capture_post_processor->Process(capture_buffer);
1390 }
1391
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001392 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001393 public_submodules_->level_estimator->ProcessStream(capture_buffer);
ajm@google.com808e0e02011-08-03 21:08:51 +00001394
peah1b08dc32016-12-20 13:45:58 -08001395 capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>(
1396 capture_buffer->channels_const()[0],
1397 capture_nonlocked_.capture_processing_format.num_frames()));
1398 if (log_rms) {
1399 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1400 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1401 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1402 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1403 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1404 }
1405
peahdf3efa82015-11-28 12:35:15 -08001406 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001407 return kNoError;
1408}
1409
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001410int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001411 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001412 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001413 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001414 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001415 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001416 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001417 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001418 };
1419 if (samples_per_channel != reverse_config.num_frames()) {
1420 return kBadDataLengthError;
1421 }
peahdf3efa82015-11-28 12:35:15 -08001422 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001423}
1424
peahde65ddc2016-09-16 15:02:15 -07001425int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1426 const StreamConfig& input_config,
1427 const StreamConfig& output_config,
1428 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001429 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001430 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001431 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001432 if (submodule_states_.RenderMultiBandProcessingActive() ||
1433 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001434 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1435 dest);
peah2ace3f92016-09-10 04:42:27 -07001436 } else if (formats_.api_format.reverse_input_stream() !=
1437 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001438 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1439 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001440 } else {
peahde65ddc2016-09-16 15:02:15 -07001441 CopyAudioIfNeeded(src, input_config.num_frames(),
1442 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001443 }
1444
1445 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001446}
1447
peahdf3efa82015-11-28 12:35:15 -08001448int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001449 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001450 const StreamConfig& input_config,
1451 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001452 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001453 return kNullPointerError;
1454 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001455
peahde65ddc2016-09-16 15:02:15 -07001456 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001457 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001458 }
1459
peahdf3efa82015-11-28 12:35:15 -08001460 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001461 processing_config.reverse_input_stream() = input_config;
1462 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001463
peahdf3efa82015-11-28 12:35:15 -08001464 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001465 RTC_DCHECK_EQ(input_config.num_frames(),
1466 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001467
aleloi868f32f2017-05-23 07:20:05 -07001468 if (aec_dump_) {
1469 const size_t channel_size =
1470 formats_.api_format.reverse_input_stream().num_frames();
1471 const size_t num_channels =
1472 formats_.api_format.reverse_input_stream().num_channels();
1473 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001474 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001475 }
peahdf3efa82015-11-28 12:35:15 -08001476 render_.render_audio->CopyFrom(src,
1477 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001478 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001479}
1480
1481int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001482 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001483 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001484 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001485 return kNullPointerError;
1486 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001487 // Must be a native rate.
1488 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1489 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001490 frame->sample_rate_hz_ != kSampleRate32kHz &&
1491 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001492 return kBadSampleRateError;
1493 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001494
Michael Graczyk86c6d332015-07-23 11:41:39 -07001495 if (frame->num_channels_ <= 0) {
1496 return kBadNumberChannelsError;
1497 }
1498
peahdf3efa82015-11-28 12:35:15 -08001499 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001500 processing_config.reverse_input_stream().set_sample_rate_hz(
1501 frame->sample_rate_hz_);
1502 processing_config.reverse_input_stream().set_num_channels(
1503 frame->num_channels_);
1504 processing_config.reverse_output_stream().set_sample_rate_hz(
1505 frame->sample_rate_hz_);
1506 processing_config.reverse_output_stream().set_num_channels(
1507 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001508
peahdf3efa82015-11-28 12:35:15 -08001509 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001510 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001511 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001512 return kBadDataLengthError;
1513 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001514
aleloi868f32f2017-05-23 07:20:05 -07001515 if (aec_dump_) {
1516 aec_dump_->WriteRenderStreamMessage(*frame);
1517 }
1518
peahdf3efa82015-11-28 12:35:15 -08001519 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001520 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001521 render_.render_audio->InterleaveTo(
Alex Loiko5825aa62017-12-18 16:02:40 +01001522 frame, submodule_states_.RenderMultiBandProcessingActive() ||
1523 submodule_states_.RenderFullBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001524 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001525}
niklase@google.com470e71d2011-07-07 08:21:25 +00001526
peahde65ddc2016-09-16 15:02:15 -07001527int AudioProcessingImpl::ProcessRenderStreamLocked() {
1528 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001529
1530 QueueNonbandedRenderAudio(render_buffer);
1531
Alex Loiko73ec0192018-05-15 10:52:28 +02001532 HandleRenderRuntimeSettings();
1533
Alex Loiko5825aa62017-12-18 16:02:40 +01001534 if (private_submodules_->render_pre_processor) {
1535 private_submodules_->render_pre_processor->Process(render_buffer);
1536 }
1537
peah2ace3f92016-09-10 04:42:27 -07001538 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001539 SampleRateSupportsMultiBand(
1540 formats_.render_processing_format.sample_rate_hz())) {
1541 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001542 }
1543
peah1bcfce52016-08-26 07:16:04 -07001544#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001545 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001546 public_submodules_->intelligibility_enhancer->ProcessRenderAudio(
Alejandro Luebsef009252016-09-20 14:51:56 -07001547 render_buffer);
ekmeyerson60d9b332015-08-14 10:35:55 -07001548 }
peah1bcfce52016-08-26 07:16:04 -07001549#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001550
peahce4d9152017-05-19 01:28:05 -07001551 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1552 QueueBandedRenderAudio(render_buffer);
1553 }
1554
peahe0eae3c2016-12-14 01:16:23 -08001555 // TODO(peah): Perform the queueing ínside QueueRenderAudiuo().
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001556 if (private_submodules_->echo_controller) {
1557 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001558 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001559
peah2ace3f92016-09-10 04:42:27 -07001560 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001561 SampleRateSupportsMultiBand(
1562 formats_.render_processing_format.sample_rate_hz())) {
1563 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001564 }
1565
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001566 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001567}
1568
1569int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001570 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001571 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001572 capture_.was_stream_delay_set = true;
1573 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001574
niklase@google.com470e71d2011-07-07 08:21:25 +00001575 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001576 delay = 0;
1577 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001578 }
1579
1580 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1581 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001582 delay = 500;
1583 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001584 }
1585
peahdf3efa82015-11-28 12:35:15 -08001586 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001587 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001588}
1589
1590int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001591 // Used as callback from submodules, hence locking is not allowed.
1592 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001593}
1594
1595bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001596 // Used as callback from submodules, hence locking is not allowed.
1597 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001598}
1599
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001600void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001601 rtc::CritScope cs(&crit_capture_);
1602 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001603}
1604
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001605void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001606 rtc::CritScope cs(&crit_capture_);
1607 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001608}
1609
1610int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001611 rtc::CritScope cs(&crit_capture_);
1612 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001613}
1614
aleloi868f32f2017-05-23 07:20:05 -07001615void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1616 RTC_DCHECK(aec_dump);
1617 rtc::CritScope cs_render(&crit_render_);
1618 rtc::CritScope cs_capture(&crit_capture_);
1619
1620 // The previously attached AecDump will be destroyed with the
1621 // 'aec_dump' parameter, which is after locks are released.
1622 aec_dump_.swap(aec_dump);
1623 WriteAecDumpConfigMessage(true);
Alex Loiko1aec5942018-05-15 13:13:22 +02001624 aec_dump_->WriteInitMessage(formats_.api_format);
aleloi868f32f2017-05-23 07:20:05 -07001625}
1626
1627void AudioProcessingImpl::DetachAecDump() {
1628 // The d-tor of a task-queue based AecDump blocks until all pending
1629 // tasks are done. This construction avoids blocking while holding
1630 // the render and capture locks.
1631 std::unique_ptr<AecDump> aec_dump = nullptr;
1632 {
1633 rtc::CritScope cs_render(&crit_render_);
1634 rtc::CritScope cs_capture(&crit_capture_);
1635 aec_dump = std::move(aec_dump_);
1636 }
1637}
1638
Sam Zackrisson4d364492018-03-02 16:03:21 +01001639void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1640 std::unique_ptr<AudioGenerator> audio_generator) {
1641 // TODO(bugs.webrtc.org/8882) Stub.
1642 // Reset internal audio generator with audio_generator.
1643}
1644
1645void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1646 // TODO(bugs.webrtc.org/8882) Stub.
1647 // Delete audio generator, if one is attached.
1648}
1649
ivoc4e477a12017-01-15 08:29:46 -08001650AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics() {
1651 residual_echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1652 echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1653 echo_return_loss_enhancement.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1654 a_nlp.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1655}
1656
1657AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics(
1658 const AudioProcessingStatistics& other) = default;
1659
1660AudioProcessing::AudioProcessingStatistics::~AudioProcessingStatistics() =
1661 default;
1662
ivoc3e9a5372016-10-28 07:55:33 -07001663// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
1664AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics()
1665 const {
1666 return AudioProcessingStatistics();
1667}
1668
Ivo Creusenae026092017-11-20 13:07:16 +01001669// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
Ivo Creusen56d46092017-11-24 17:29:59 +01001670AudioProcessingStats AudioProcessing::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001671 bool has_remote_tracks) const {
1672 return AudioProcessingStats();
1673}
1674
ivoc3e9a5372016-10-28 07:55:33 -07001675AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics()
1676 const {
1677 AudioProcessingStatistics stats;
1678 EchoCancellation::Metrics metrics;
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001679 if (private_submodules_->echo_controller) {
1680 rtc::CritScope cs_capture(&crit_capture_);
1681 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1682 float erl = static_cast<float>(ec_metrics.echo_return_loss);
1683 float erle = static_cast<float>(ec_metrics.echo_return_loss_enhancement);
1684 // Instant value will also be used for min, max and average.
1685 stats.echo_return_loss.Set(erl, erl, erl, erl);
1686 stats.echo_return_loss_enhancement.Set(erle, erle, erle, erle);
1687 } else if (public_submodules_->echo_cancellation->GetMetrics(&metrics) ==
1688 Error::kNoError) {
ivocd0a151c2016-11-02 09:14:37 -07001689 stats.a_nlp.Set(metrics.a_nlp);
1690 stats.divergent_filter_fraction = metrics.divergent_filter_fraction;
1691 stats.echo_return_loss.Set(metrics.echo_return_loss);
1692 stats.echo_return_loss_enhancement.Set(
1693 metrics.echo_return_loss_enhancement);
1694 stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss);
1695 }
ivoc9c192b22017-03-16 04:22:14 -07001696 {
1697 rtc::CritScope cs_capture(&crit_capture_);
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001698 RTC_DCHECK(private_submodules_->echo_detector);
1699 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1700 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
ivoc9c192b22017-03-16 04:22:14 -07001701 stats.residual_echo_likelihood_recent_max =
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001702 ed_metrics.echo_likelihood_recent_max;
ivoc9c192b22017-03-16 04:22:14 -07001703 }
ivoc3e9a5372016-10-28 07:55:33 -07001704 public_submodules_->echo_cancellation->GetDelayMetrics(
1705 &stats.delay_median, &stats.delay_standard_deviation,
1706 &stats.fraction_poor_delays);
1707 return stats;
1708}
1709
Ivo Creusen56d46092017-11-24 17:29:59 +01001710AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001711 bool has_remote_tracks) const {
1712 AudioProcessingStats stats;
1713 if (has_remote_tracks) {
1714 EchoCancellation::Metrics metrics;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001715 if (private_submodules_->echo_controller) {
1716 rtc::CritScope cs_capture(&crit_capture_);
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001717 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1718 stats.echo_return_loss = ec_metrics.echo_return_loss;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001719 stats.echo_return_loss_enhancement =
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001720 ec_metrics.echo_return_loss_enhancement;
Per Åhgren83c4a022017-11-27 12:07:09 +01001721 stats.delay_ms = ec_metrics.delay_ms;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001722 } else if (public_submodules_->echo_cancellation->GetMetrics(&metrics) ==
1723 Error::kNoError) {
Ivo Creusenae026092017-11-20 13:07:16 +01001724 if (metrics.divergent_filter_fraction != -1.0f) {
1725 stats.divergent_filter_fraction =
1726 rtc::Optional<double>(metrics.divergent_filter_fraction);
1727 }
1728 if (metrics.echo_return_loss.instant != -100) {
1729 stats.echo_return_loss =
1730 rtc::Optional<double>(metrics.echo_return_loss.instant);
1731 }
1732 if (metrics.echo_return_loss_enhancement.instant != -100) {
1733 stats.echo_return_loss_enhancement =
1734 rtc::Optional<double>(metrics.echo_return_loss_enhancement.instant);
1735 }
1736 }
1737 if (config_.residual_echo_detector.enabled) {
1738 rtc::CritScope cs_capture(&crit_capture_);
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001739 RTC_DCHECK(private_submodules_->echo_detector);
1740 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1741 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
Ivo Creusenae026092017-11-20 13:07:16 +01001742 stats.residual_echo_likelihood_recent_max =
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001743 ed_metrics.echo_likelihood_recent_max;
Ivo Creusenae026092017-11-20 13:07:16 +01001744 }
1745 int delay_median, delay_std;
1746 float fraction_poor_delays;
1747 if (public_submodules_->echo_cancellation->GetDelayMetrics(
1748 &delay_median, &delay_std, &fraction_poor_delays) ==
1749 Error::kNoError) {
1750 if (delay_median >= 0) {
1751 stats.delay_median_ms = rtc::Optional<int32_t>(delay_median);
1752 }
1753 if (delay_std >= 0) {
1754 stats.delay_standard_deviation_ms = rtc::Optional<int32_t>(delay_std);
1755 }
1756 }
1757 }
1758 return stats;
1759}
1760
niklase@google.com470e71d2011-07-07 08:21:25 +00001761EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
peahb624d8c2016-03-05 03:01:14 -08001762 return public_submodules_->echo_cancellation.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001763}
1764
1765EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
peahbb9edbd2016-03-10 12:54:25 -08001766 return public_submodules_->echo_control_mobile.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001767}
1768
1769GainControl* AudioProcessingImpl::gain_control() const {
peahbe615622016-02-13 16:40:47 -08001770 if (constants_.use_experimental_agc) {
1771 return public_submodules_->gain_control_for_experimental_agc.get();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001772 }
peahbfa97112016-03-10 21:09:04 -08001773 return public_submodules_->gain_control.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001774}
1775
1776HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
peah8271d042016-11-22 07:24:52 -08001777 return high_pass_filter_impl_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001778}
1779
1780LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001781 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001782}
1783
1784NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
solenberg5e465c32015-12-08 13:22:33 -08001785 return public_submodules_->noise_suppression.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001786}
1787
1788VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001789 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001790}
1791
peah8271d042016-11-22 07:24:52 -08001792void AudioProcessingImpl::MutateConfig(
1793 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1794 rtc::CritScope cs_render(&crit_render_);
1795 rtc::CritScope cs_capture(&crit_capture_);
1796 mutator(&config_);
1797 ApplyConfig(config_);
1798}
1799
1800AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1801 rtc::CritScope cs_render(&crit_render_);
1802 rtc::CritScope cs_capture(&crit_capture_);
1803 return config_;
1804}
1805
peah2ace3f92016-09-10 04:42:27 -07001806bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1807 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001808 config_.high_pass_filter.enabled,
peah2ace3f92016-09-10 04:42:27 -07001809 public_submodules_->echo_cancellation->is_enabled(),
1810 public_submodules_->echo_control_mobile->is_enabled(),
ivoc9f4a4a02016-10-28 05:39:16 -07001811 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001812 public_submodules_->noise_suppression->is_enabled(),
1813 capture_nonlocked_.intelligibility_enabled,
1814 capture_nonlocked_.beamformer_enabled,
1815 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001816 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001817 capture_nonlocked_.echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -07001818 public_submodules_->voice_detection->is_enabled(),
1819 public_submodules_->level_estimator->is_enabled(),
1820 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001821}
1822
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001823
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001824void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001825 if (capture_.transient_suppressor_enabled) {
1826 if (!public_submodules_->transient_suppressor.get()) {
1827 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001828 }
peahdf3efa82015-11-28 12:35:15 -08001829 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001830 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1831 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001832 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001833}
1834
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001835void AudioProcessingImpl::InitializeBeamformer() {
aluebsb2328d12016-01-11 20:32:29 -08001836 if (capture_nonlocked_.beamformer_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001837 if (!private_submodules_->beamformer) {
1838 private_submodules_->beamformer.reset(new NonlinearBeamformer(
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001839 capture_.array_geometry, 1u, capture_.target_direction));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001840 }
peahdf3efa82015-11-28 12:35:15 -08001841 private_submodules_->beamformer->Initialize(kChunkSizeMs,
1842 capture_nonlocked_.split_rate);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001843 }
1844}
1845
ekmeyerson60d9b332015-08-14 10:35:55 -07001846void AudioProcessingImpl::InitializeIntelligibility() {
peah1bcfce52016-08-26 07:16:04 -07001847#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001848 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001849 public_submodules_->intelligibility_enhancer.reset(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -08001850 new IntelligibilityEnhancer(capture_nonlocked_.split_rate,
Alex Luebs57ae8292016-03-09 16:24:34 +01001851 render_.render_audio->num_channels(),
Alejandro Luebsef009252016-09-20 14:51:56 -07001852 render_.render_audio->num_bands(),
Alex Luebs57ae8292016-03-09 16:24:34 +01001853 NoiseSuppressionImpl::num_noise_bins()));
ekmeyerson60d9b332015-08-14 10:35:55 -07001854 }
peah1bcfce52016-08-26 07:16:04 -07001855#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001856}
1857
peah8271d042016-11-22 07:24:52 -08001858void AudioProcessingImpl::InitializeLowCutFilter() {
1859 if (config_.high_pass_filter.enabled) {
1860 private_submodules_->low_cut_filter.reset(
1861 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1862 } else {
1863 private_submodules_->low_cut_filter.reset();
1864 }
1865}
alessiob3ec96df2017-05-22 06:57:06 -07001866
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001867void AudioProcessingImpl::InitializeEchoController() {
Gustaf Ullberg002ef282017-10-12 15:13:17 +02001868 if (echo_control_factory_) {
1869 private_submodules_->echo_controller =
1870 echo_control_factory_->Create(proc_sample_rate_hz());
peahe0eae3c2016-12-14 01:16:23 -08001871 } else {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001872 private_submodules_->echo_controller.reset();
peahe0eae3c2016-12-14 01:16:23 -08001873 }
1874}
peah8271d042016-11-22 07:24:52 -08001875
alessiob3ec96df2017-05-22 06:57:06 -07001876void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001877 if (config_.gain_controller2.enabled) {
1878 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001879 }
1880}
1881
Alex Loikob5c9a792018-04-16 16:31:22 +02001882void AudioProcessingImpl::InitializePreAmplifier() {
1883 if (config_.pre_amplifier.enabled) {
1884 private_submodules_->pre_amplifier.reset(
1885 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1886 } else {
1887 private_submodules_->pre_amplifier.reset();
1888 }
1889}
1890
ivoc9f4a4a02016-10-28 05:39:16 -07001891void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001892 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001893 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001894 proc_sample_rate_hz(), 1,
1895 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001896}
1897
Sam Zackrisson0beac582017-09-25 12:04:02 +02001898void AudioProcessingImpl::InitializePostProcessor() {
1899 if (private_submodules_->capture_post_processor) {
1900 private_submodules_->capture_post_processor->Initialize(
1901 proc_sample_rate_hz(), num_proc_channels());
1902 }
1903}
1904
Alex Loiko5825aa62017-12-18 16:02:40 +01001905void AudioProcessingImpl::InitializePreProcessor() {
1906 if (private_submodules_->render_pre_processor) {
1907 private_submodules_->render_pre_processor->Initialize(
1908 formats_.render_processing_format.sample_rate_hz(),
1909 formats_.render_processing_format.num_channels());
1910 }
1911}
1912
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001913void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001914 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001915
1916 if (echo_cancellation()->is_enabled()) {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001917 // Activate delay_jumps_ counters if we know echo_cancellation is running.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001918 // If a stream has echo we know that the echo_cancellation is in process.
peahdf3efa82015-11-28 12:35:15 -08001919 if (capture_.stream_delay_jumps == -1 &&
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001920 echo_cancellation()->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001921 capture_.stream_delay_jumps = 0;
1922 }
1923 if (capture_.aec_system_delay_jumps == -1 &&
1924 echo_cancellation()->stream_has_echo()) {
1925 capture_.aec_system_delay_jumps = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001926 }
1927
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001928 // Detect a jump in platform reported system delay and log the difference.
peahdf3efa82015-11-28 12:35:15 -08001929 const int diff_stream_delay_ms =
1930 capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
1931 if (diff_stream_delay_ms > kMinDiffDelayMs &&
1932 capture_.last_stream_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001933 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1934 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
peahdf3efa82015-11-28 12:35:15 -08001935 if (capture_.stream_delay_jumps == -1) {
1936 capture_.stream_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001937 }
peahdf3efa82015-11-28 12:35:15 -08001938 capture_.stream_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001939 }
peahdf3efa82015-11-28 12:35:15 -08001940 capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001941
1942 // Detect a jump in AEC system delay and log the difference.
peah20028c42016-03-04 11:50:54 -08001943 const int samples_per_ms =
peahdf3efa82015-11-28 12:35:15 -08001944 rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
peah20028c42016-03-04 11:50:54 -08001945 RTC_DCHECK_LT(0, samples_per_ms);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001946 const int aec_system_delay_ms =
peah20028c42016-03-04 11:50:54 -08001947 public_submodules_->echo_cancellation->GetSystemDelayInSamples() /
1948 samples_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001949 const int diff_aec_system_delay_ms =
peahdf3efa82015-11-28 12:35:15 -08001950 aec_system_delay_ms - capture_.last_aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001951 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
peahdf3efa82015-11-28 12:35:15 -08001952 capture_.last_aec_system_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001953 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1954 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1955 100);
peahdf3efa82015-11-28 12:35:15 -08001956 if (capture_.aec_system_delay_jumps == -1) {
1957 capture_.aec_system_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001958 }
peahdf3efa82015-11-28 12:35:15 -08001959 capture_.aec_system_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001960 }
peahdf3efa82015-11-28 12:35:15 -08001961 capture_.last_aec_system_delay_ms = aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001962 }
1963}
1964
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001965void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
peahdf3efa82015-11-28 12:35:15 -08001966 // Run in a single-threaded manner.
1967 rtc::CritScope cs_render(&crit_render_);
1968 rtc::CritScope cs_capture(&crit_capture_);
1969
1970 if (capture_.stream_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001971 RTC_HISTOGRAM_ENUMERATION(
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001972 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
peahdf3efa82015-11-28 12:35:15 -08001973 capture_.stream_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001974 }
peahdf3efa82015-11-28 12:35:15 -08001975 capture_.stream_delay_jumps = -1;
1976 capture_.last_stream_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001977
peahdf3efa82015-11-28 12:35:15 -08001978 if (capture_.aec_system_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001979 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1980 capture_.aec_system_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001981 }
peahdf3efa82015-11-28 12:35:15 -08001982 capture_.aec_system_delay_jumps = -1;
1983 capture_.last_aec_system_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001984}
1985
aleloi868f32f2017-05-23 07:20:05 -07001986void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1987 if (!aec_dump_) {
1988 return;
1989 }
1990 std::string experiments_description =
1991 public_submodules_->echo_cancellation->GetExperimentsDescription();
1992 // TODO(peah): Add semicolon-separated concatenations of experiment
1993 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001994 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1995 experiments_description += "AgcClippingLevelExperiment;";
1996 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001997 if (capture_nonlocked_.echo_controller_enabled) {
1998 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001999 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02002000 if (config_.gain_controller2.enabled) {
2001 experiments_description += "GainController2;";
2002 }
aleloi868f32f2017-05-23 07:20:05 -07002003
2004 InternalAPMConfig apm_config;
2005
2006 apm_config.aec_enabled = public_submodules_->echo_cancellation->is_enabled();
2007 apm_config.aec_delay_agnostic_enabled =
2008 public_submodules_->echo_cancellation->is_delay_agnostic_enabled();
2009 apm_config.aec_drift_compensation_enabled =
2010 public_submodules_->echo_cancellation->is_drift_compensation_enabled();
2011 apm_config.aec_extended_filter_enabled =
2012 public_submodules_->echo_cancellation->is_extended_filter_enabled();
2013 apm_config.aec_suppression_level = static_cast<int>(
2014 public_submodules_->echo_cancellation->suppression_level());
2015
2016 apm_config.aecm_enabled =
2017 public_submodules_->echo_control_mobile->is_enabled();
2018 apm_config.aecm_comfort_noise_enabled =
2019 public_submodules_->echo_control_mobile->is_comfort_noise_enabled();
2020 apm_config.aecm_routing_mode =
2021 static_cast<int>(public_submodules_->echo_control_mobile->routing_mode());
2022
2023 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2024 apm_config.agc_mode =
2025 static_cast<int>(public_submodules_->gain_control->mode());
2026 apm_config.agc_limiter_enabled =
2027 public_submodules_->gain_control->is_limiter_enabled();
2028 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2029
2030 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2031
2032 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2033 apm_config.ns_level =
2034 static_cast<int>(public_submodules_->noise_suppression->level());
2035
2036 apm_config.transient_suppression_enabled =
2037 capture_.transient_suppressor_enabled;
2038 apm_config.intelligibility_enhancer_enabled =
2039 capture_nonlocked_.intelligibility_enabled;
2040 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002041 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2042 apm_config.pre_amplifier_fixed_gain_factor =
2043 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002044
2045 if (!forced && apm_config == apm_config_for_aec_dump_) {
2046 return;
2047 }
2048 aec_dump_->WriteConfig(apm_config);
2049 apm_config_for_aec_dump_ = apm_config;
2050}
2051
2052void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2053 const float* const* src) {
2054 RTC_DCHECK(aec_dump_);
2055 WriteAecDumpConfigMessage(false);
2056
2057 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2058 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2059 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002060 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002061 RecordAudioProcessingState();
2062}
2063
2064void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2065 const AudioFrame& capture_frame) {
2066 RTC_DCHECK(aec_dump_);
2067 WriteAecDumpConfigMessage(false);
2068
2069 aec_dump_->AddCaptureStreamInput(capture_frame);
2070 RecordAudioProcessingState();
2071}
2072
2073void AudioProcessingImpl::RecordProcessedCaptureStream(
2074 const float* const* processed_capture_stream) {
2075 RTC_DCHECK(aec_dump_);
2076
2077 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2078 const size_t num_channels =
2079 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002080 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2081 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002082 aec_dump_->WriteCaptureStreamMessage();
2083}
2084
2085void AudioProcessingImpl::RecordProcessedCaptureStream(
2086 const AudioFrame& processed_capture_frame) {
2087 RTC_DCHECK(aec_dump_);
2088
2089 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2090 aec_dump_->WriteCaptureStreamMessage();
2091}
2092
2093void AudioProcessingImpl::RecordAudioProcessingState() {
2094 RTC_DCHECK(aec_dump_);
2095 AecDump::AudioProcessingState audio_proc_state;
2096 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2097 audio_proc_state.drift =
2098 public_submodules_->echo_cancellation->stream_drift_samples();
2099 audio_proc_state.level = gain_control()->stream_analog_level();
2100 audio_proc_state.keypress = capture_.key_pressed;
2101 aec_dump_->AddAudioProcessingState(audio_proc_state);
2102}
2103
kwiberg83ffe452016-08-29 14:46:07 -07002104AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
2105 bool transient_suppressor_enabled,
2106 const std::vector<Point>& array_geometry,
2107 SphericalPointf target_direction)
2108 : aec_system_delay_jumps(-1),
2109 delay_offset_ms(0),
2110 was_stream_delay_set(false),
2111 last_stream_delay_ms(0),
2112 last_aec_system_delay_ms(0),
2113 stream_delay_jumps(-1),
2114 output_will_be_muted(false),
2115 key_pressed(false),
2116 transient_suppressor_enabled(transient_suppressor_enabled),
2117 array_geometry(array_geometry),
2118 target_direction(target_direction),
peahde65ddc2016-09-16 15:02:15 -07002119 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002120 split_rate(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002121 echo_path_gain_change(false) {}
kwiberg83ffe452016-08-29 14:46:07 -07002122
2123AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2124
2125AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2126
2127AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2128
niklase@google.com470e71d2011-07-07 08:21:25 +00002129} // namespace webrtc