blob: e3b385729cc645f46ae1344eabaec51266a60b1e [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};
151
aleloi868f32f2017-05-23 07:20:05 -0700152webrtc::InternalAPMStreamsConfig ToStreamsConfig(
153 const ProcessingConfig& api_format) {
154 webrtc::InternalAPMStreamsConfig result;
155 result.input_sample_rate = api_format.input_stream().sample_rate_hz();
156 result.input_num_channels = api_format.input_stream().num_channels();
157 result.output_num_channels = api_format.output_stream().num_channels();
158 result.render_input_num_channels =
159 api_format.reverse_input_stream().num_channels();
160 result.render_input_sample_rate =
161 api_format.reverse_input_stream().sample_rate_hz();
162 result.output_sample_rate = api_format.output_stream().sample_rate_hz();
163 result.render_output_sample_rate =
164 api_format.reverse_output_stream().sample_rate_hz();
165 result.render_output_num_channels =
166 api_format.reverse_output_stream().num_channels();
167 return result;
168}
Michael Graczyk86c6d332015-07-23 11:41:39 -0700169} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000170
171// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000172static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000173
Sam Zackrisson0beac582017-09-25 12:04:02 +0200174AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100175 bool capture_post_processor_enabled,
176 bool render_pre_processor_enabled)
177 : capture_post_processor_enabled_(capture_post_processor_enabled),
178 render_pre_processor_enabled_(render_pre_processor_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700179
180bool AudioProcessingImpl::ApmSubmoduleStates::Update(
peah8271d042016-11-22 07:24:52 -0800181 bool low_cut_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700182 bool echo_canceller_enabled,
183 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700184 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700185 bool noise_suppressor_enabled,
186 bool intelligibility_enhancer_enabled,
187 bool beamformer_enabled,
188 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700189 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200190 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200191 bool echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -0700192 bool voice_activity_detector_enabled,
193 bool level_estimator_enabled,
194 bool transient_suppressor_enabled) {
195 bool changed = false;
peah8271d042016-11-22 07:24:52 -0800196 changed |= (low_cut_filter_enabled != low_cut_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700197 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
198 changed |=
199 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700200 changed |=
201 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700202 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
203 changed |=
204 (intelligibility_enhancer_enabled != intelligibility_enhancer_enabled_);
205 changed |= (beamformer_enabled != beamformer_enabled_);
206 changed |=
207 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
alessiob3ec96df2017-05-22 06:57:06 -0700208 changed |=
209 (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200210 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200211 changed |= (echo_controller_enabled != echo_controller_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700212 changed |= (level_estimator_enabled != level_estimator_enabled_);
213 changed |=
214 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
215 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
216 if (changed) {
peah8271d042016-11-22 07:24:52 -0800217 low_cut_filter_enabled_ = low_cut_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700218 echo_canceller_enabled_ = echo_canceller_enabled;
219 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700220 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700221 noise_suppressor_enabled_ = noise_suppressor_enabled;
222 intelligibility_enhancer_enabled_ = intelligibility_enhancer_enabled;
223 beamformer_enabled_ = beamformer_enabled;
224 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700225 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200226 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200227 echo_controller_enabled_ = echo_controller_enabled;
peah2ace3f92016-09-10 04:42:27 -0700228 level_estimator_enabled_ = level_estimator_enabled;
229 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
230 transient_suppressor_enabled_ = transient_suppressor_enabled;
231 }
232
233 changed |= first_update_;
234 first_update_ = false;
235 return changed;
236}
237
238bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
239 const {
240#if WEBRTC_INTELLIGIBILITY_ENHANCER
241 return CaptureMultiBandProcessingActive() ||
peah52775842017-05-16 06:14:09 -0700242 intelligibility_enhancer_enabled_ || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700243#else
peah52775842017-05-16 06:14:09 -0700244 return CaptureMultiBandProcessingActive() || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700245#endif
246}
247
248bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
249 const {
peah8271d042016-11-22 07:24:52 -0800250 return low_cut_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700251 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
peahe0eae3c2016-12-14 01:16:23 -0800252 beamformer_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200253 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700254}
255
peah23ac8b42017-05-23 05:33:56 -0700256bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
257 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200258 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
259 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700260}
261
peah2ace3f92016-09-10 04:42:27 -0700262bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
263 const {
264 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800265 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200266 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700267}
268
Alex Loiko5825aa62017-12-18 16:02:40 +0100269bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive()
270 const {
271 return render_pre_processor_enabled_;
272}
273
peah2ace3f92016-09-10 04:42:27 -0700274bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
275 const {
276#if WEBRTC_INTELLIGIBILITY_ENHANCER
277 return intelligibility_enhancer_enabled_;
278#else
279 return false;
280#endif
281}
282
solenberg5e465c32015-12-08 13:22:33 -0800283struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800284 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800285 // Accessed externally of APM without any lock acquired.
peahb624d8c2016-03-05 03:01:14 -0800286 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
peahbb9edbd2016-03-10 12:54:25 -0800287 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
peahbfa97112016-03-10 21:09:04 -0800288 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800289 std::unique_ptr<LevelEstimatorImpl> level_estimator;
290 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
291 std::unique_ptr<VoiceDetectionImpl> voice_detection;
292 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800293 gain_control_for_experimental_agc;
solenberg5e465c32015-12-08 13:22:33 -0800294
295 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800296 std::unique_ptr<TransientSuppressor> transient_suppressor;
peah1bcfce52016-08-26 07:16:04 -0700297#if WEBRTC_INTELLIGIBILITY_ENHANCER
kwiberg88788ad2016-02-19 07:04:49 -0800298 std::unique_ptr<IntelligibilityEnhancer> intelligibility_enhancer;
peah1bcfce52016-08-26 07:16:04 -0700299#endif
solenberg5e465c32015-12-08 13:22:33 -0800300};
301
302struct AudioProcessingImpl::ApmPrivateSubmodules {
Sam Zackrisson0beac582017-09-25 12:04:02 +0200303 ApmPrivateSubmodules(NonlinearBeamformer* beamformer,
Alex Loiko5825aa62017-12-18 16:02:40 +0100304 std::unique_ptr<CustomProcessing> capture_post_processor,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100305 std::unique_ptr<CustomProcessing> render_pre_processor,
306 std::unique_ptr<EchoDetector> echo_detector)
Sam Zackrisson0beac582017-09-25 12:04:02 +0200307 : beamformer(beamformer),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100308 echo_detector(std::move(echo_detector)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100309 capture_post_processor(std::move(capture_post_processor)),
310 render_pre_processor(std::move(render_pre_processor)) {}
solenberg5e465c32015-12-08 13:22:33 -0800311 // Accessed internally from capture or during initialization
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700312 std::unique_ptr<NonlinearBeamformer> beamformer;
kwiberg88788ad2016-02-19 07:04:49 -0800313 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700314 std::unique_ptr<GainController2> gain_controller2;
peah8271d042016-11-22 07:24:52 -0800315 std::unique_ptr<LowCutFilter> low_cut_filter;
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100316 std::unique_ptr<EchoDetector> echo_detector;
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +0200317 std::unique_ptr<EchoControl> echo_controller;
Alex Loiko5825aa62017-12-18 16:02:40 +0100318 std::unique_ptr<CustomProcessing> capture_post_processor;
319 std::unique_ptr<CustomProcessing> render_pre_processor;
Alex Loikob5c9a792018-04-16 16:31:22 +0200320 std::unique_ptr<GainApplier> pre_amplifier;
solenberg5e465c32015-12-08 13:22:33 -0800321};
322
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100323AudioProcessingBuilder::AudioProcessingBuilder() = default;
324AudioProcessingBuilder::~AudioProcessingBuilder() = default;
325
326AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
327 std::unique_ptr<CustomProcessing> capture_post_processing) {
328 capture_post_processing_ = std::move(capture_post_processing);
329 return *this;
330}
331
332AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
333 std::unique_ptr<CustomProcessing> render_pre_processing) {
334 render_pre_processing_ = std::move(render_pre_processing);
335 return *this;
336}
337
338AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
339 std::unique_ptr<EchoControlFactory> echo_control_factory) {
340 echo_control_factory_ = std::move(echo_control_factory);
341 return *this;
342}
343
344AudioProcessingBuilder& AudioProcessingBuilder::SetNonlinearBeamformer(
345 std::unique_ptr<NonlinearBeamformer> nonlinear_beamformer) {
346 nonlinear_beamformer_ = std::move(nonlinear_beamformer);
347 return *this;
348}
349
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100350AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
351 std::unique_ptr<EchoDetector> echo_detector) {
352 echo_detector_ = std::move(echo_detector);
353 return *this;
354}
355
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100356AudioProcessing* AudioProcessingBuilder::Create() {
357 webrtc::Config config;
358 return Create(config);
359}
360
361AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100362 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
363 config, std::move(capture_post_processing_),
364 std::move(render_pre_processing_), std::move(echo_control_factory_),
365 std::move(echo_detector_), nonlinear_beamformer_.release());
366 if (apm->Initialize() != AudioProcessing::kNoError) {
367 delete apm;
368 apm = nullptr;
369 }
370 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100371}
372
peah88ac8532016-09-12 16:47:25 -0700373AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100374 : AudioProcessingImpl(config, nullptr, nullptr, nullptr, nullptr, nullptr) {
375}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000376
Per Åhgren13735822018-02-12 21:42:56 +0100377int AudioProcessingImpl::instance_count_ = 0;
378
Sam Zackrisson0beac582017-09-25 12:04:02 +0200379AudioProcessingImpl::AudioProcessingImpl(
380 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100381 std::unique_ptr<CustomProcessing> capture_post_processor,
382 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200383 std::unique_ptr<EchoControlFactory> echo_control_factory,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100384 std::unique_ptr<EchoDetector> echo_detector,
Sam Zackrisson0beac582017-09-25 12:04:02 +0200385 NonlinearBeamformer* beamformer)
Per Åhgren13735822018-02-12 21:42:56 +0100386 : data_dumper_(
387 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200388 capture_runtime_settings_(kRuntimeSettingQueueSize),
389 render_runtime_settings_(kRuntimeSettingQueueSize),
390 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
391 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Per Åhgren13735822018-02-12 21:42:56 +0100392 high_pass_filter_impl_(new HighPassFilterImpl(this)),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200393 echo_control_factory_(std::move(echo_control_factory)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100394 submodule_states_(!!capture_post_processor, !!render_pre_processor),
peah8271d042016-11-22 07:24:52 -0800395 public_submodules_(new ApmPublicSubmodules()),
Sam Zackrisson0beac582017-09-25 12:04:02 +0200396 private_submodules_(
397 new ApmPrivateSubmodules(beamformer,
Alex Loiko5825aa62017-12-18 16:02:40 +0100398 std::move(capture_post_processor),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100399 std::move(render_pre_processor),
400 std::move(echo_detector))),
peahdf3efa82015-11-28 12:35:15 -0800401 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800402 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000403#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700404 false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000405#else
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700406 config.Get<ExperimentalAgc>().enabled),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000407#endif
andrew1c7075f2015-06-24 18:14:14 -0700408#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
aluebs2a346882016-01-11 18:04:30 -0800409 capture_(false,
andrew1c7075f2015-06-24 18:14:14 -0700410#else
aluebs2a346882016-01-11 18:04:30 -0800411 capture_(config.Get<ExperimentalNs>().enabled,
andrew1c7075f2015-06-24 18:14:14 -0700412#endif
aluebs2a346882016-01-11 18:04:30 -0800413 config.Get<Beamforming>().array_geometry,
aluebsb2328d12016-01-11 20:32:29 -0800414 config.Get<Beamforming>().target_direction),
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700415 capture_nonlocked_(config.Get<Beamforming>().enabled,
peah88ac8532016-09-12 16:47:25 -0700416 config.Get<Intelligibility>().enabled) {
peahdf3efa82015-11-28 12:35:15 -0800417 {
418 rtc::CritScope cs_render(&crit_render_);
419 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000420
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200421 // Mark Echo Controller enabled if a factory is injected.
422 capture_nonlocked_.echo_controller_enabled =
423 static_cast<bool>(echo_control_factory_);
424
peahb624d8c2016-03-05 03:01:14 -0800425 public_submodules_->echo_cancellation.reset(
peahb58a1582016-03-15 09:34:24 -0700426 new EchoCancellationImpl(&crit_render_, &crit_capture_));
peahbb9edbd2016-03-10 12:54:25 -0800427 public_submodules_->echo_control_mobile.reset(
peah253534d2016-03-15 04:32:28 -0700428 new EchoControlMobileImpl(&crit_render_, &crit_capture_));
peahbfa97112016-03-10 21:09:04 -0800429 public_submodules_->gain_control.reset(
peahb8fbb542016-03-15 02:28:08 -0700430 new GainControlImpl(&crit_capture_, &crit_capture_));
solenberg949028f2015-12-15 11:39:38 -0800431 public_submodules_->level_estimator.reset(
432 new LevelEstimatorImpl(&crit_capture_));
solenberg5e465c32015-12-08 13:22:33 -0800433 public_submodules_->noise_suppression.reset(
434 new NoiseSuppressionImpl(&crit_capture_));
solenberga29386c2015-12-16 03:31:12 -0800435 public_submodules_->voice_detection.reset(
436 new VoiceDetectionImpl(&crit_capture_));
peahbe615622016-02-13 16:40:47 -0800437 public_submodules_->gain_control_for_experimental_agc.reset(
peahbfa97112016-03-10 21:09:04 -0800438 new GainControlForExperimentalAgc(
439 public_submodules_->gain_control.get(), &crit_capture_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100440
441 // If no echo detector is injected, use the ResidualEchoDetector.
442 if (!private_submodules_->echo_detector) {
443 private_submodules_->echo_detector.reset(new ResidualEchoDetector());
444 }
peahca4cac72016-06-29 15:26:12 -0700445
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200446 // TODO(alessiob): Move the injected gain controller once injection is
447 // implemented.
448 private_submodules_->gain_controller2.reset(new GainController2());
449
Mirko Bonadei675513b2017-11-09 11:09:25 +0100450 RTC_LOG(LS_INFO) << "Capture post processor activated: "
Jonas Olsson645b0272018-02-15 15:16:27 +0100451 << !!private_submodules_->capture_post_processor
452 << "\nRender pre processor activated: "
Alex Loiko5825aa62017-12-18 16:02:40 +0100453 << !!private_submodules_->render_pre_processor;
peahdf3efa82015-11-28 12:35:15 -0800454 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000455
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000456 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000457}
458
459AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800460 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800461 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800462 private_submodules_->agc_manager.reset();
463 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800464 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
466
niklase@google.com470e71d2011-07-07 08:21:25 +0000467int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800468 // Run in a single-threaded manner during initialization.
469 rtc::CritScope cs_render(&crit_render_);
470 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000471 return InitializeLocked();
472}
473
peahde65ddc2016-09-16 15:02:15 -0700474int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
475 int capture_output_sample_rate_hz,
476 int render_input_sample_rate_hz,
477 ChannelLayout capture_input_layout,
478 ChannelLayout capture_output_layout,
479 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700480 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700481 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
482 LayoutHasKeyboard(capture_input_layout)},
483 {capture_output_sample_rate_hz,
484 ChannelsFromLayout(capture_output_layout),
485 LayoutHasKeyboard(capture_output_layout)},
486 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
487 LayoutHasKeyboard(render_input_layout)},
488 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
489 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700490
491 return Initialize(processing_config);
492}
493
494int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800495 // Run in a single-threaded manner during initialization.
496 rtc::CritScope cs_render(&crit_render_);
497 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700498 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000499}
500
peahdf3efa82015-11-28 12:35:15 -0800501int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800502 const ProcessingConfig& processing_config) {
peah2ace3f92016-09-10 04:42:27 -0700503 return MaybeInitialize(processing_config, false);
peah81b9bfe2015-11-27 02:47:28 -0800504}
505
peahdf3efa82015-11-28 12:35:15 -0800506int AudioProcessingImpl::MaybeInitializeCapture(
peah2ace3f92016-09-10 04:42:27 -0700507 const ProcessingConfig& processing_config,
508 bool force_initialization) {
509 return MaybeInitialize(processing_config, force_initialization);
peah81b9bfe2015-11-27 02:47:28 -0800510}
511
peah192164e2015-11-17 02:16:45 -0800512// Calls InitializeLocked() if any of the audio parameters have changed from
peahdf3efa82015-11-28 12:35:15 -0800513// their current values (needs to be called while holding the crit_render_lock).
514int AudioProcessingImpl::MaybeInitialize(
peah2ace3f92016-09-10 04:42:27 -0700515 const ProcessingConfig& processing_config,
516 bool force_initialization) {
peahdf3efa82015-11-28 12:35:15 -0800517 // Called from both threads. Thread check is therefore not possible.
peah2ace3f92016-09-10 04:42:27 -0700518 if (processing_config == formats_.api_format && !force_initialization) {
peah192164e2015-11-17 02:16:45 -0800519 return kNoError;
520 }
peahdf3efa82015-11-28 12:35:15 -0800521
522 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800523 return InitializeLocked(processing_config);
524}
525
niklase@google.com470e71d2011-07-07 08:21:25 +0000526int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200527 UpdateActiveSubmoduleStates();
528
peah522d71b2017-02-23 05:16:26 -0800529 const int capture_audiobuffer_num_channels =
530 capture_nonlocked_.beamformer_enabled
531 ? formats_.api_format.input_stream().num_channels()
532 : formats_.api_format.output_stream().num_channels();
533
peahde65ddc2016-09-16 15:02:15 -0700534 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800535 formats_.api_format.reverse_output_stream().num_frames() == 0
peahde65ddc2016-09-16 15:02:15 -0700536 ? formats_.render_processing_format.num_frames()
peahdf3efa82015-11-28 12:35:15 -0800537 : formats_.api_format.reverse_output_stream().num_frames();
538 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
539 render_.render_audio.reset(new AudioBuffer(
540 formats_.api_format.reverse_input_stream().num_frames(),
541 formats_.api_format.reverse_input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700542 formats_.render_processing_format.num_frames(),
543 formats_.render_processing_format.num_channels(),
544 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700545 if (formats_.api_format.reverse_input_stream() !=
546 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800547 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800548 formats_.api_format.reverse_input_stream().num_channels(),
549 formats_.api_format.reverse_input_stream().num_frames(),
550 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800551 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700552 } else {
peahdf3efa82015-11-28 12:35:15 -0800553 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700554 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700555 } else {
peahdf3efa82015-11-28 12:35:15 -0800556 render_.render_audio.reset(nullptr);
557 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700558 }
peahce4d9152017-05-19 01:28:05 -0700559
peahdf3efa82015-11-28 12:35:15 -0800560 capture_.capture_audio.reset(
561 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
562 formats_.api_format.input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700563 capture_nonlocked_.capture_processing_format.num_frames(),
564 capture_audiobuffer_num_channels,
peahdf3efa82015-11-28 12:35:15 -0800565 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000566
peahde65ddc2016-09-16 15:02:15 -0700567 public_submodules_->echo_cancellation->Initialize(
568 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
569 num_proc_channels());
peah764e3642016-10-22 05:04:30 -0700570 AllocateRenderQueue();
571
ivoc3e9a5372016-10-28 07:55:33 -0700572 int success = public_submodules_->echo_cancellation->enable_metrics(true);
573 RTC_DCHECK_EQ(0, success);
574 success = public_submodules_->echo_cancellation->enable_delay_logging(true);
575 RTC_DCHECK_EQ(0, success);
peahde65ddc2016-09-16 15:02:15 -0700576 public_submodules_->echo_control_mobile->Initialize(
577 proc_split_sample_rate_hz(), num_reverse_channels(),
578 num_output_channels());
peah135259a2016-10-28 03:12:11 -0700579
580 public_submodules_->gain_control->Initialize(num_proc_channels(),
581 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700582 if (constants_.use_experimental_agc) {
583 if (!private_submodules_->agc_manager.get()) {
584 private_submodules_->agc_manager.reset(new AgcManagerDirect(
585 public_submodules_->gain_control.get(),
586 public_submodules_->gain_control_for_experimental_agc.get(),
henrik.lundinbd681b92016-12-05 09:08:42 -0800587 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min));
peahde65ddc2016-09-16 15:02:15 -0700588 }
589 private_submodules_->agc_manager->Initialize();
590 private_submodules_->agc_manager->SetCaptureMuted(
591 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700592 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700593 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200594 InitializeTransient();
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000595 InitializeBeamformer();
peah1bcfce52016-08-26 07:16:04 -0700596#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -0700597 InitializeIntelligibility();
peah1bcfce52016-08-26 07:16:04 -0700598#endif
peah8271d042016-11-22 07:24:52 -0800599 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700600 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
601 proc_sample_rate_hz());
602 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
603 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700604 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200605 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700606 InitializeGainController2();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200607 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100608 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800609
aleloi868f32f2017-05-23 07:20:05 -0700610 if (aec_dump_) {
611 aec_dump_->WriteInitMessage(ToStreamsConfig(formats_.api_format));
612 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000613 return kNoError;
614}
615
Michael Graczyk86c6d332015-07-23 11:41:39 -0700616int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200617 UpdateActiveSubmoduleStates();
618
Michael Graczyk86c6d332015-07-23 11:41:39 -0700619 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700620 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
621 return kBadSampleRateError;
622 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000623 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700624
Peter Kasting69558702016-01-12 16:26:35 -0800625 const size_t num_in_channels = config.input_stream().num_channels();
626 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700627
628 // Need at least one input channel.
629 // Need either one output channel or as many outputs as there are inputs.
630 if (num_in_channels == 0 ||
631 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700632 return kBadNumberChannelsError;
633 }
634
aluebsb2328d12016-01-11 20:32:29 -0800635 if (capture_nonlocked_.beamformer_enabled &&
Peter Kasting69558702016-01-12 16:26:35 -0800636 num_in_channels != capture_.array_geometry.size()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700637 return kBadNumberChannelsError;
638 }
639
peahdf3efa82015-11-28 12:35:15 -0800640 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000641
peahde65ddc2016-09-16 15:02:15 -0700642 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700643 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700644 formats_.api_format.output_stream().sample_rate_hz()),
645 submodule_states_.CaptureMultiBandSubModulesActive() ||
646 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000647
peahde65ddc2016-09-16 15:02:15 -0700648 capture_nonlocked_.capture_processing_format =
649 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700650
peah2ce640f2017-04-07 03:57:48 -0700651 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200652 if (!capture_nonlocked_.echo_controller_enabled) {
peah2ce640f2017-04-07 03:57:48 -0700653 render_processing_rate = FindNativeProcessRateToUse(
654 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
655 formats_.api_format.reverse_output_stream().sample_rate_hz()),
656 submodule_states_.CaptureMultiBandSubModulesActive() ||
657 submodule_states_.RenderMultiBandSubModulesActive());
658 } else {
659 render_processing_rate = capture_processing_rate;
660 }
661
aluebseb3603b2016-04-20 15:27:58 -0700662 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
663 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700664 if (render_processing_rate > kSampleRate32kHz &&
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200665 !capture_nonlocked_.echo_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -0700666 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
667 ? kSampleRate32kHz
668 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700669 }
peah2ce640f2017-04-07 03:57:48 -0700670
peahde65ddc2016-09-16 15:02:15 -0700671 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700672 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700673 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
674 kSampleRate8kHz) {
675 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000676 } else {
peahde65ddc2016-09-16 15:02:15 -0700677 render_processing_rate =
678 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000679 }
680
peahde65ddc2016-09-16 15:02:15 -0700681 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000682 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700683 if (submodule_states_.RenderMultiBandSubModulesActive()) {
684 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
685 } else {
686 formats_.render_processing_format = StreamConfig(
687 formats_.api_format.reverse_input_stream().sample_rate_hz(),
688 formats_.api_format.reverse_input_stream().num_channels());
689 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000690
peahde65ddc2016-09-16 15:02:15 -0700691 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
692 kSampleRate32kHz ||
693 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
694 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800695 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000696 } else {
peahdf3efa82015-11-28 12:35:15 -0800697 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700698 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000699 }
700
701 return InitializeLocked();
702}
703
peah88ac8532016-09-12 16:47:25 -0700704void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peahc19f3122016-10-07 14:54:10 -0700705 config_ = config;
peah88ac8532016-09-12 16:47:25 -0700706
peah88ac8532016-09-12 16:47:25 -0700707 // Run in a single-threaded manner when applying the settings.
708 rtc::CritScope cs_render(&crit_render_);
709 rtc::CritScope cs_capture(&crit_capture_);
710
peah8271d042016-11-22 07:24:52 -0800711 InitializeLowCutFilter();
712
Mirko Bonadei675513b2017-11-09 11:09:25 +0100713 RTC_LOG(LS_INFO) << "Highpass filter activated: "
714 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800715
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100716 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700717 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100718 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
719 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100720 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100721 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700722 config_.gain_controller2 = AudioProcessing::Config::GainController2();
723 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200724 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200725 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200726 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100727 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
728 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200729 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
730 << config_.pre_amplifier.enabled;
peah88ac8532016-09-12 16:47:25 -0700731}
732
733void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800734 // Run in a single-threaded manner when setting the extra options.
735 rtc::CritScope cs_render(&crit_render_);
736 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000737
peahb624d8c2016-03-05 03:01:14 -0800738 public_submodules_->echo_cancellation->SetExtraOptions(config);
739
peahdf3efa82015-11-28 12:35:15 -0800740 if (capture_.transient_suppressor_enabled !=
741 config.Get<ExperimentalNs>().enabled) {
742 capture_.transient_suppressor_enabled =
743 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000744 InitializeTransient();
745 }
aluebs2a346882016-01-11 18:04:30 -0800746
peah1bcfce52016-08-26 07:16:04 -0700747#if WEBRTC_INTELLIGIBILITY_ENHANCER
alessiob3ec96df2017-05-22 06:57:06 -0700748 if (capture_nonlocked_.intelligibility_enabled !=
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700749 config.Get<Intelligibility>().enabled) {
750 capture_nonlocked_.intelligibility_enabled =
751 config.Get<Intelligibility>().enabled;
752 InitializeIntelligibility();
753 }
peah1bcfce52016-08-26 07:16:04 -0700754#endif
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700755
aluebs2a346882016-01-11 18:04:30 -0800756#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
aluebsb2328d12016-01-11 20:32:29 -0800757 if (capture_nonlocked_.beamformer_enabled !=
758 config.Get<Beamforming>().enabled) {
759 capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled;
aluebs2a346882016-01-11 18:04:30 -0800760 if (config.Get<Beamforming>().array_geometry.size() > 1) {
761 capture_.array_geometry = config.Get<Beamforming>().array_geometry;
762 }
763 capture_.target_direction = config.Get<Beamforming>().target_direction;
764 InitializeBeamformer();
765 }
766#endif // WEBRTC_ANDROID_PLATFORM_BUILD
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000767}
768
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000769int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800770 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700771 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000772}
773
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000774int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800775 // Used as callback from submodules, hence locking is not allowed.
776 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000777}
778
Peter Kasting69558702016-01-12 16:26:35 -0800779size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800780 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700781 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000782}
783
Peter Kasting69558702016-01-12 16:26:35 -0800784size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800785 // Used as callback from submodules, hence locking is not allowed.
786 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000787}
788
Peter Kasting69558702016-01-12 16:26:35 -0800789size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800790 // Used as callback from submodules, hence locking is not allowed.
peahedddac52017-05-16 01:08:58 -0700791 return (capture_nonlocked_.beamformer_enabled ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200792 capture_nonlocked_.echo_controller_enabled)
peahedddac52017-05-16 01:08:58 -0700793 ? 1
794 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800795}
796
Peter Kasting69558702016-01-12 16:26:35 -0800797size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800798 // Used as callback from submodules, hence locking is not allowed.
799 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000800}
801
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000802void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800803 rtc::CritScope cs(&crit_capture_);
804 capture_.output_will_be_muted = muted;
805 if (private_submodules_->agc_manager.get()) {
806 private_submodules_->agc_manager->SetCaptureMuted(
807 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000808 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000809}
810
Alessio Bazzicac054e782018-04-16 12:10:09 +0200811void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200812 switch (setting.type()) {
813 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
814 render_runtime_settings_enqueuer_.Enqueue(setting);
815 return;
816 case RuntimeSetting::Type::kNotSpecified:
817 RTC_NOTREACHED();
818 return;
819 case RuntimeSetting::Type::kCapturePreGain:
820 capture_runtime_settings_enqueuer_.Enqueue(setting);
821 return;
822 }
823 // The language allows the enum to have a non-enumerator
824 // value. Check that this doesn't happen.
825 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200826}
827
828AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
829 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200830 : runtime_settings_(*runtime_settings) {
831 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200832}
833
834AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
835 default;
836
837void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
838 RuntimeSetting setting) {
839 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200840 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200841 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200842 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200843 RTC_LOG(LS_ERROR)
844 << "The runtime settings queue is full. Oldest setting discarded.";
845 }
846 if (remaining_attempts == 0)
847 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
848}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000849
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000850int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700851 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000852 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000853 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000854 int output_sample_rate_hz,
855 ChannelLayout output_layout,
856 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800857 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800858 StreamConfig input_stream;
859 StreamConfig output_stream;
860 {
861 // Access the formats_.api_format.input_stream beneath the capture lock.
862 // The lock must be released as it is later required in the call
863 // to ProcessStream(,,,);
864 rtc::CritScope cs(&crit_capture_);
865 input_stream = formats_.api_format.input_stream();
866 output_stream = formats_.api_format.output_stream();
867 }
868
Michael Graczyk86c6d332015-07-23 11:41:39 -0700869 input_stream.set_sample_rate_hz(input_sample_rate_hz);
870 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
871 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700872 output_stream.set_sample_rate_hz(output_sample_rate_hz);
873 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
874 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
875
876 if (samples_per_channel != input_stream.num_frames()) {
877 return kBadDataLengthError;
878 }
879 return ProcessStream(src, input_stream, output_stream, dest);
880}
881
882int AudioProcessingImpl::ProcessStream(const float* const* src,
883 const StreamConfig& input_config,
884 const StreamConfig& output_config,
885 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800886 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800887 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700888 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800889 {
890 // Acquire the capture lock in order to safely call the function
891 // that retrieves the render side data. This function accesses apm
892 // getters that need the capture lock held when being called.
893 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700894 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800895
896 if (!src || !dest) {
897 return kNullPointerError;
898 }
899
900 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700901 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000902 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000903
Michael Graczyk86c6d332015-07-23 11:41:39 -0700904 processing_config.input_stream() = input_config;
905 processing_config.output_stream() = output_config;
906
peahdf3efa82015-11-28 12:35:15 -0800907 {
908 // Do conditional reinitialization.
909 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -0700910 RETURN_ON_ERR(
911 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -0800912 }
913 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700914 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
915 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000916
aleloi868f32f2017-05-23 07:20:05 -0700917 if (aec_dump_) {
918 RecordUnprocessedCaptureStream(src);
919 }
920
peahdf3efa82015-11-28 12:35:15 -0800921 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700922 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800923 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000924
aleloi868f32f2017-05-23 07:20:05 -0700925 if (aec_dump_) {
926 RecordProcessedCaptureStream(dest);
927 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000928 return kNoError;
929}
930
Alex Loiko73ec0192018-05-15 10:52:28 +0200931void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200932 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200933 while (capture_runtime_settings_.Remove(&setting)) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200934 switch (setting.type()) {
935 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200936 if (config_.pre_amplifier.enabled) {
937 float value;
938 setting.GetFloat(&value);
939 private_submodules_->pre_amplifier->SetGainFactor(value);
940 }
941 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200942 break;
Alex Loiko73ec0192018-05-15 10:52:28 +0200943 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
944 RTC_NOTREACHED();
945 break;
946 case RuntimeSetting::Type::kNotSpecified:
947 RTC_NOTREACHED();
948 break;
949 }
950 }
951}
952
953void AudioProcessingImpl::HandleRenderRuntimeSettings() {
954 RuntimeSetting setting;
955 while (render_runtime_settings_.Remove(&setting)) {
956 switch (setting.type()) {
957 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
958 if (private_submodules_->render_pre_processor) {
959 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
960 }
961 break;
962 case RuntimeSetting::Type::kCapturePreGain:
963 RTC_NOTREACHED();
964 break;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200965 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +0200966 RTC_NOTREACHED();
967 break;
968 }
969 }
970}
971
peah9e6a2902017-05-15 07:19:21 -0700972void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
peah764e3642016-10-22 05:04:30 -0700973 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
974 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700975 &aec_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700976
kwibergaf476c72016-11-28 15:21:39 -0800977 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -0700978
979 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700980 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700981 // The data queue is full and needs to be emptied.
982 EmptyQueuedRenderAudio();
983
984 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700985 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700986 RTC_DCHECK(result);
987 }
988
989 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
990 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700991 &aecm_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700992
993 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700994 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700995 // The data queue is full and needs to be emptied.
996 EmptyQueuedRenderAudio();
997
998 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700999 bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001000 RTC_DCHECK(result);
1001 }
peah701d6282016-10-25 05:42:20 -07001002
1003 if (!constants_.use_experimental_agc) {
1004 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1005 // Insert the samples into the queue.
1006 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1007 // The data queue is full and needs to be emptied.
1008 EmptyQueuedRenderAudio();
1009
1010 // Retry the insert (should always work).
1011 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1012 RTC_DCHECK(result);
1013 }
1014 }
peah9e6a2902017-05-15 07:19:21 -07001015}
ivoc9f4a4a02016-10-28 05:39:16 -07001016
peah9e6a2902017-05-15 07:19:21 -07001017void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001018 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1019
1020 // Insert the samples into the queue.
1021 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1022 // The data queue is full and needs to be emptied.
1023 EmptyQueuedRenderAudio();
1024
1025 // Retry the insert (should always work).
1026 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1027 RTC_DCHECK(result);
1028 }
peah764e3642016-10-22 05:04:30 -07001029}
1030
1031void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001032 const size_t new_aec_render_queue_element_max_size =
peah764e3642016-10-22 05:04:30 -07001033 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -07001034 kMaxAllowedValuesOfSamplesPerBand *
peah764e3642016-10-22 05:04:30 -07001035 EchoCancellationImpl::NumCancellersRequired(
1036 num_output_channels(), num_reverse_channels()));
1037
peah701d6282016-10-25 05:42:20 -07001038 const size_t new_aecm_render_queue_element_max_size =
peaha0624602016-10-25 04:45:24 -07001039 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -07001040 kMaxAllowedValuesOfSamplesPerBand *
peaha0624602016-10-25 04:45:24 -07001041 EchoControlMobileImpl::NumCancellersRequired(
1042 num_output_channels(), num_reverse_channels()));
peah764e3642016-10-22 05:04:30 -07001043
peah701d6282016-10-25 05:42:20 -07001044 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001045 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001046
ivoc9f4a4a02016-10-28 05:39:16 -07001047 const size_t new_red_render_queue_element_max_size =
1048 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1049
peaha0624602016-10-25 04:45:24 -07001050 // Reallocate the queues if the queue item sizes are too small to fit the
1051 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001052 if (aec_render_queue_element_max_size_ <
1053 new_aec_render_queue_element_max_size) {
1054 aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
peah764e3642016-10-22 05:04:30 -07001055
peaha0624602016-10-25 04:45:24 -07001056 std::vector<float> template_queue_element(
peah701d6282016-10-25 05:42:20 -07001057 aec_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001058
peah701d6282016-10-25 05:42:20 -07001059 aec_render_signal_queue_.reset(
peah764e3642016-10-22 05:04:30 -07001060 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1061 kMaxNumFramesToBuffer, template_queue_element,
peaha0624602016-10-25 04:45:24 -07001062 RenderQueueItemVerifier<float>(
peah701d6282016-10-25 05:42:20 -07001063 aec_render_queue_element_max_size_)));
peah764e3642016-10-22 05:04:30 -07001064
peah701d6282016-10-25 05:42:20 -07001065 aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
1066 aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
peah764e3642016-10-22 05:04:30 -07001067 } else {
peah701d6282016-10-25 05:42:20 -07001068 aec_render_signal_queue_->Clear();
peaha0624602016-10-25 04:45:24 -07001069 }
1070
peah701d6282016-10-25 05:42:20 -07001071 if (aecm_render_queue_element_max_size_ <
1072 new_aecm_render_queue_element_max_size) {
1073 aecm_render_queue_element_max_size_ =
1074 new_aecm_render_queue_element_max_size;
peaha0624602016-10-25 04:45:24 -07001075
1076 std::vector<int16_t> template_queue_element(
peah701d6282016-10-25 05:42:20 -07001077 aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001078
peah701d6282016-10-25 05:42:20 -07001079 aecm_render_signal_queue_.reset(
peaha0624602016-10-25 04:45:24 -07001080 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1081 kMaxNumFramesToBuffer, template_queue_element,
1082 RenderQueueItemVerifier<int16_t>(
peah701d6282016-10-25 05:42:20 -07001083 aecm_render_queue_element_max_size_)));
peaha0624602016-10-25 04:45:24 -07001084
peah701d6282016-10-25 05:42:20 -07001085 aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
1086 aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001087 } else {
peah701d6282016-10-25 05:42:20 -07001088 aecm_render_signal_queue_->Clear();
1089 }
1090
1091 if (agc_render_queue_element_max_size_ <
1092 new_agc_render_queue_element_max_size) {
1093 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1094
1095 std::vector<int16_t> template_queue_element(
1096 agc_render_queue_element_max_size_);
1097
1098 agc_render_signal_queue_.reset(
1099 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1100 kMaxNumFramesToBuffer, template_queue_element,
1101 RenderQueueItemVerifier<int16_t>(
1102 agc_render_queue_element_max_size_)));
1103
1104 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1105 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1106 } else {
1107 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001108 }
ivoc9f4a4a02016-10-28 05:39:16 -07001109
1110 if (red_render_queue_element_max_size_ <
1111 new_red_render_queue_element_max_size) {
1112 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1113
1114 std::vector<float> template_queue_element(
1115 red_render_queue_element_max_size_);
1116
1117 red_render_signal_queue_.reset(
1118 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1119 kMaxNumFramesToBuffer, template_queue_element,
1120 RenderQueueItemVerifier<float>(
1121 red_render_queue_element_max_size_)));
1122
1123 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1124 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1125 } else {
1126 red_render_signal_queue_->Clear();
1127 }
peah764e3642016-10-22 05:04:30 -07001128}
1129
1130void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1131 rtc::CritScope cs_capture(&crit_capture_);
peah701d6282016-10-25 05:42:20 -07001132 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -07001133 public_submodules_->echo_cancellation->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001134 aec_capture_queue_buffer_);
peaha0624602016-10-25 04:45:24 -07001135 }
1136
peah701d6282016-10-25 05:42:20 -07001137 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -07001138 public_submodules_->echo_control_mobile->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001139 aecm_capture_queue_buffer_);
1140 }
1141
1142 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1143 public_submodules_->gain_control->ProcessRenderAudio(
1144 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001145 }
ivoc9f4a4a02016-10-28 05:39:16 -07001146
1147 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001148 RTC_DCHECK(private_submodules_->echo_detector);
1149 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001150 red_capture_queue_buffer_);
1151 }
peah764e3642016-10-22 05:04:30 -07001152}
1153
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001154int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001155 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001156 {
1157 // Acquire the capture lock in order to safely call the function
1158 // that retrieves the render side data. This function accesses apm
1159 // getters that need the capture lock held when being called.
1160 // The lock needs to be released as
1161 // public_submodules_->echo_control_mobile->is_enabled() aquires this lock
1162 // as well.
1163 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001164 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001165 }
peahfa6228e2015-11-16 16:27:42 -08001166
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001167 if (!frame) {
1168 return kNullPointerError;
1169 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001170 // Must be a native rate.
1171 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1172 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001173 frame->sample_rate_hz_ != kSampleRate32kHz &&
1174 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001175 return kBadSampleRateError;
1176 }
peah192164e2015-11-17 02:16:45 -08001177
peahdf3efa82015-11-28 12:35:15 -08001178 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001179 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001180 {
1181 // Aquire lock for the access of api_format.
1182 // The lock is released immediately due to the conditional
1183 // reinitialization.
1184 rtc::CritScope cs_capture(&crit_capture_);
1185 // TODO(ajm): The input and output rates and channels are currently
1186 // constrained to be identical in the int16 interface.
1187 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001188
1189 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001190 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001191 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1192 processing_config.input_stream().set_num_channels(frame->num_channels_);
1193 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1194 processing_config.output_stream().set_num_channels(frame->num_channels_);
1195
peahdf3efa82015-11-28 12:35:15 -08001196 {
1197 // Do conditional reinitialization.
1198 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -07001199 RETURN_ON_ERR(
1200 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -08001201 }
1202 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001203 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001204 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001205 return kBadDataLengthError;
1206 }
1207
aleloi868f32f2017-05-23 07:20:05 -07001208 if (aec_dump_) {
1209 RecordUnprocessedCaptureStream(*frame);
1210 }
1211
peahdf3efa82015-11-28 12:35:15 -08001212 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001213 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001214 capture_.capture_audio->InterleaveTo(
peah23ac8b42017-05-23 05:33:56 -07001215 frame, submodule_states_.CaptureMultiBandProcessingActive() ||
1216 submodule_states_.CaptureFullBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001217
aleloi868f32f2017-05-23 07:20:05 -07001218 if (aec_dump_) {
1219 RecordProcessedCaptureStream(*frame);
1220 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001221
1222 return kNoError;
1223}
1224
peahde65ddc2016-09-16 15:02:15 -07001225int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001226 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001227
peahb58a1582016-03-15 09:34:24 -07001228 // Ensure that not both the AEC and AECM are active at the same time.
1229 // TODO(peah): Simplify once the public API Enable functions for these
1230 // are moved to APM.
1231 RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&
1232 public_submodules_->echo_control_mobile->is_enabled()));
1233
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001234 MaybeUpdateHistograms();
1235
peahde65ddc2016-09-16 15:02:15 -07001236 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001237
Alex Loikob5c9a792018-04-16 16:31:22 +02001238 if (private_submodules_->pre_amplifier) {
1239 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
1240 capture_buffer->channels_f(), capture_buffer->num_channels(),
1241 capture_buffer->num_frames()));
1242 }
1243
peah1b08dc32016-12-20 13:45:58 -08001244 capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>(
henrik.lundin290d43a2016-11-29 08:09:09 -08001245 capture_buffer->channels_const()[0],
1246 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001247 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1248 if (log_rms) {
1249 capture_rms_interval_counter_ = 0;
1250 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001251 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1252 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1253 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1254 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001255 }
1256
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001257 if (private_submodules_->echo_controller) {
Per Åhgren9aed31c2017-06-29 20:23:27 +02001258 // TODO(peah): Reactivate analogue AGC gain detection once the analogue AGC
1259 // issues have been addressed.
1260 capture_.echo_path_gain_change = false;
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001261 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001262 }
1263
peahbe615622016-02-13 16:40:47 -08001264 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001265 public_submodules_->gain_control->is_enabled()) {
1266 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001267 capture_buffer->channels()[0], capture_buffer->num_channels(),
1268 capture_nonlocked_.capture_processing_format.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001269 }
1270
peah2ace3f92016-09-10 04:42:27 -07001271 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1272 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001273 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1274 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001275 }
1276
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001277 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001278 // Force down-mixing of the number of channels after the detection of
1279 // capture signal saturation.
1280 // TODO(peah): Look into ensuring that this kind of tampering with the
1281 // AudioBuffer functionality should not be needed.
1282 capture_buffer->set_num_channels(1);
1283 }
1284
aluebsb2328d12016-01-11 20:32:29 -08001285 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001286 private_submodules_->beamformer->AnalyzeChunk(
1287 *capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001288 // Discards all channels by the leftmost one.
peahde65ddc2016-09-16 15:02:15 -07001289 capture_buffer->set_num_channels(1);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001290 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001291
peahe0eae3c2016-12-14 01:16:23 -08001292 // TODO(peah): Move the AEC3 low-cut filter to this place.
1293 if (private_submodules_->low_cut_filter &&
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001294 !private_submodules_->echo_controller) {
peah8271d042016-11-22 07:24:52 -08001295 private_submodules_->low_cut_filter->Process(capture_buffer);
1296 }
peahde65ddc2016-09-16 15:02:15 -07001297 RETURN_ON_ERR(
1298 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1299 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001300
1301 // Ensure that the stream delay was set before the call to the
1302 // AEC ProcessCaptureAudio function.
1303 if (public_submodules_->echo_cancellation->is_enabled() &&
Per Åhgren0dfd3722018-05-06 18:16:01 +02001304 !private_submodules_->echo_controller && !was_stream_delay_set()) {
peahb58a1582016-03-15 09:34:24 -07001305 return AudioProcessing::kStreamParameterNotSetError;
1306 }
1307
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001308 if (private_submodules_->echo_controller) {
Per Åhgren13735822018-02-12 21:42:56 +01001309 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1310
Per Åhgrend0fa8202018-04-18 09:35:13 +02001311 if (was_stream_delay_set()) {
1312 private_submodules_->echo_controller->SetAudioBufferDelay(
1313 stream_delay_ms());
1314 }
1315
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001316 private_submodules_->echo_controller->ProcessCapture(
peah67995532017-04-10 14:12:41 -07001317 capture_buffer, capture_.echo_path_gain_change);
peah61202ac2017-02-06 03:39:42 -08001318 } else {
1319 RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(
1320 capture_buffer, stream_delay_ms()));
peahe0eae3c2016-12-14 01:16:23 -08001321 }
1322
peahdf3efa82015-11-28 12:35:15 -08001323 if (public_submodules_->echo_control_mobile->is_enabled() &&
1324 public_submodules_->noise_suppression->is_enabled()) {
peahde65ddc2016-09-16 15:02:15 -07001325 capture_buffer->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +00001326 }
peahde65ddc2016-09-16 15:02:15 -07001327 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah1bcfce52016-08-26 07:16:04 -07001328#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001329 if (capture_nonlocked_.intelligibility_enabled) {
aluebsc466bad2016-02-10 12:03:00 -08001330 RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());
Sam Zackrissonab1aee02018-03-05 15:59:06 +01001331 const int gain_db =
1332 public_submodules_->gain_control->is_enabled()
1333 ? public_submodules_->gain_control->compression_gain_db()
1334 : 0;
1335 const float gain = DbToRatio(gain_db);
aluebsc466bad2016-02-10 12:03:00 -08001336 public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -07001337 public_submodules_->noise_suppression->NoiseEstimate(), gain);
aluebsc466bad2016-02-10 12:03:00 -08001338 }
peah1bcfce52016-08-26 07:16:04 -07001339#endif
peah253534d2016-03-15 04:32:28 -07001340
1341 // Ensure that the stream delay was set before the call to the
1342 // AECM ProcessCaptureAudio function.
1343 if (public_submodules_->echo_control_mobile->is_enabled() &&
1344 !was_stream_delay_set()) {
1345 return AudioProcessing::kStreamParameterNotSetError;
1346 }
1347
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001348 if (!(private_submodules_->echo_controller ||
Per Åhgren46537a32017-06-07 10:08:10 +02001349 public_submodules_->echo_cancellation->is_enabled())) {
1350 RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(
1351 capture_buffer, stream_delay_ms()));
1352 }
ivoc9f4a4a02016-10-28 05:39:16 -07001353
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001354 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001355 private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001356 }
1357
peahde65ddc2016-09-16 15:02:15 -07001358 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001359
peahbe615622016-02-13 16:40:47 -08001360 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001361 public_submodules_->gain_control->is_enabled() &&
aluebsb2328d12016-01-11 20:32:29 -08001362 (!capture_nonlocked_.beamformer_enabled ||
peahdf3efa82015-11-28 12:35:15 -08001363 private_submodules_->beamformer->is_target_present())) {
1364 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001365 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1366 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001367 }
peahb8fbb542016-03-15 02:28:08 -07001368 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001369 capture_buffer, echo_cancellation()->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001370
peah2ace3f92016-09-10 04:42:27 -07001371 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1372 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001373 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1374 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001375 }
1376
peah9e6a2902017-05-15 07:19:21 -07001377 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001378 RTC_DCHECK(private_submodules_->echo_detector);
1379 private_submodules_->echo_detector->AnalyzeCaptureAudio(
peah9e6a2902017-05-15 07:19:21 -07001380 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
1381 capture_buffer->num_frames()));
1382 }
1383
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001384 // TODO(aluebs): Investigate if the transient suppression placement should be
1385 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001386 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001387 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001388 private_submodules_->agc_manager.get()
1389 ? private_submodules_->agc_manager->voice_probability()
1390 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001391
peahdf3efa82015-11-28 12:35:15 -08001392 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001393 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1394 capture_buffer->num_channels(),
1395 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1396 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1397 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001398 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001399 }
1400
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001401 if (config_.gain_controller2.enabled) {
alessiob3ec96df2017-05-22 06:57:06 -07001402 private_submodules_->gain_controller2->Process(capture_buffer);
1403 }
1404
Sam Zackrisson0beac582017-09-25 12:04:02 +02001405 if (private_submodules_->capture_post_processor) {
1406 private_submodules_->capture_post_processor->Process(capture_buffer);
1407 }
1408
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001409 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001410 public_submodules_->level_estimator->ProcessStream(capture_buffer);
ajm@google.com808e0e02011-08-03 21:08:51 +00001411
peah1b08dc32016-12-20 13:45:58 -08001412 capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>(
1413 capture_buffer->channels_const()[0],
1414 capture_nonlocked_.capture_processing_format.num_frames()));
1415 if (log_rms) {
1416 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1417 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1418 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1419 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1420 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1421 }
1422
peahdf3efa82015-11-28 12:35:15 -08001423 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001424 return kNoError;
1425}
1426
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001427int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001428 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001429 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001430 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001431 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001432 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001433 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001434 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001435 };
1436 if (samples_per_channel != reverse_config.num_frames()) {
1437 return kBadDataLengthError;
1438 }
peahdf3efa82015-11-28 12:35:15 -08001439 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001440}
1441
peahde65ddc2016-09-16 15:02:15 -07001442int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1443 const StreamConfig& input_config,
1444 const StreamConfig& output_config,
1445 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001446 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001447 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001448 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001449 if (submodule_states_.RenderMultiBandProcessingActive() ||
1450 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001451 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1452 dest);
peah2ace3f92016-09-10 04:42:27 -07001453 } else if (formats_.api_format.reverse_input_stream() !=
1454 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001455 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1456 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001457 } else {
peahde65ddc2016-09-16 15:02:15 -07001458 CopyAudioIfNeeded(src, input_config.num_frames(),
1459 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001460 }
1461
1462 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001463}
1464
peahdf3efa82015-11-28 12:35:15 -08001465int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001466 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001467 const StreamConfig& input_config,
1468 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001469 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001470 return kNullPointerError;
1471 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001472
peahde65ddc2016-09-16 15:02:15 -07001473 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001474 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001475 }
1476
peahdf3efa82015-11-28 12:35:15 -08001477 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001478 processing_config.reverse_input_stream() = input_config;
1479 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001480
peahdf3efa82015-11-28 12:35:15 -08001481 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001482 RTC_DCHECK_EQ(input_config.num_frames(),
1483 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001484
aleloi868f32f2017-05-23 07:20:05 -07001485 if (aec_dump_) {
1486 const size_t channel_size =
1487 formats_.api_format.reverse_input_stream().num_frames();
1488 const size_t num_channels =
1489 formats_.api_format.reverse_input_stream().num_channels();
1490 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001491 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001492 }
peahdf3efa82015-11-28 12:35:15 -08001493 render_.render_audio->CopyFrom(src,
1494 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001495 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001496}
1497
1498int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001499 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001500 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001501 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001502 return kNullPointerError;
1503 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001504 // Must be a native rate.
1505 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1506 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001507 frame->sample_rate_hz_ != kSampleRate32kHz &&
1508 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001509 return kBadSampleRateError;
1510 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001511
Michael Graczyk86c6d332015-07-23 11:41:39 -07001512 if (frame->num_channels_ <= 0) {
1513 return kBadNumberChannelsError;
1514 }
1515
peahdf3efa82015-11-28 12:35:15 -08001516 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001517 processing_config.reverse_input_stream().set_sample_rate_hz(
1518 frame->sample_rate_hz_);
1519 processing_config.reverse_input_stream().set_num_channels(
1520 frame->num_channels_);
1521 processing_config.reverse_output_stream().set_sample_rate_hz(
1522 frame->sample_rate_hz_);
1523 processing_config.reverse_output_stream().set_num_channels(
1524 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001525
peahdf3efa82015-11-28 12:35:15 -08001526 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001527 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001528 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001529 return kBadDataLengthError;
1530 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001531
aleloi868f32f2017-05-23 07:20:05 -07001532 if (aec_dump_) {
1533 aec_dump_->WriteRenderStreamMessage(*frame);
1534 }
1535
peahdf3efa82015-11-28 12:35:15 -08001536 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001537 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001538 render_.render_audio->InterleaveTo(
Alex Loiko5825aa62017-12-18 16:02:40 +01001539 frame, submodule_states_.RenderMultiBandProcessingActive() ||
1540 submodule_states_.RenderFullBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001541 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001542}
niklase@google.com470e71d2011-07-07 08:21:25 +00001543
peahde65ddc2016-09-16 15:02:15 -07001544int AudioProcessingImpl::ProcessRenderStreamLocked() {
1545 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001546
1547 QueueNonbandedRenderAudio(render_buffer);
1548
Alex Loiko73ec0192018-05-15 10:52:28 +02001549 HandleRenderRuntimeSettings();
1550
Alex Loiko5825aa62017-12-18 16:02:40 +01001551 if (private_submodules_->render_pre_processor) {
1552 private_submodules_->render_pre_processor->Process(render_buffer);
1553 }
1554
peah2ace3f92016-09-10 04:42:27 -07001555 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001556 SampleRateSupportsMultiBand(
1557 formats_.render_processing_format.sample_rate_hz())) {
1558 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001559 }
1560
peah1bcfce52016-08-26 07:16:04 -07001561#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001562 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001563 public_submodules_->intelligibility_enhancer->ProcessRenderAudio(
Alejandro Luebsef009252016-09-20 14:51:56 -07001564 render_buffer);
ekmeyerson60d9b332015-08-14 10:35:55 -07001565 }
peah1bcfce52016-08-26 07:16:04 -07001566#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001567
peahce4d9152017-05-19 01:28:05 -07001568 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1569 QueueBandedRenderAudio(render_buffer);
1570 }
1571
peahe0eae3c2016-12-14 01:16:23 -08001572 // TODO(peah): Perform the queueing ínside QueueRenderAudiuo().
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001573 if (private_submodules_->echo_controller) {
1574 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001575 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001576
peah2ace3f92016-09-10 04:42:27 -07001577 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001578 SampleRateSupportsMultiBand(
1579 formats_.render_processing_format.sample_rate_hz())) {
1580 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001581 }
1582
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001583 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001584}
1585
1586int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001587 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001588 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001589 capture_.was_stream_delay_set = true;
1590 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001591
niklase@google.com470e71d2011-07-07 08:21:25 +00001592 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001593 delay = 0;
1594 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001595 }
1596
1597 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1598 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001599 delay = 500;
1600 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001601 }
1602
peahdf3efa82015-11-28 12:35:15 -08001603 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001604 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001605}
1606
1607int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001608 // Used as callback from submodules, hence locking is not allowed.
1609 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001610}
1611
1612bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001613 // Used as callback from submodules, hence locking is not allowed.
1614 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001615}
1616
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001617void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001618 rtc::CritScope cs(&crit_capture_);
1619 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001620}
1621
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001622void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001623 rtc::CritScope cs(&crit_capture_);
1624 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001625}
1626
1627int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001628 rtc::CritScope cs(&crit_capture_);
1629 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001630}
1631
aleloi868f32f2017-05-23 07:20:05 -07001632void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1633 RTC_DCHECK(aec_dump);
1634 rtc::CritScope cs_render(&crit_render_);
1635 rtc::CritScope cs_capture(&crit_capture_);
1636
1637 // The previously attached AecDump will be destroyed with the
1638 // 'aec_dump' parameter, which is after locks are released.
1639 aec_dump_.swap(aec_dump);
1640 WriteAecDumpConfigMessage(true);
1641 aec_dump_->WriteInitMessage(ToStreamsConfig(formats_.api_format));
1642}
1643
1644void AudioProcessingImpl::DetachAecDump() {
1645 // The d-tor of a task-queue based AecDump blocks until all pending
1646 // tasks are done. This construction avoids blocking while holding
1647 // the render and capture locks.
1648 std::unique_ptr<AecDump> aec_dump = nullptr;
1649 {
1650 rtc::CritScope cs_render(&crit_render_);
1651 rtc::CritScope cs_capture(&crit_capture_);
1652 aec_dump = std::move(aec_dump_);
1653 }
1654}
1655
Sam Zackrisson4d364492018-03-02 16:03:21 +01001656void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1657 std::unique_ptr<AudioGenerator> audio_generator) {
1658 // TODO(bugs.webrtc.org/8882) Stub.
1659 // Reset internal audio generator with audio_generator.
1660}
1661
1662void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1663 // TODO(bugs.webrtc.org/8882) Stub.
1664 // Delete audio generator, if one is attached.
1665}
1666
ivoc4e477a12017-01-15 08:29:46 -08001667AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics() {
1668 residual_echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1669 echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1670 echo_return_loss_enhancement.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1671 a_nlp.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1672}
1673
1674AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics(
1675 const AudioProcessingStatistics& other) = default;
1676
1677AudioProcessing::AudioProcessingStatistics::~AudioProcessingStatistics() =
1678 default;
1679
ivoc3e9a5372016-10-28 07:55:33 -07001680// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
1681AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics()
1682 const {
1683 return AudioProcessingStatistics();
1684}
1685
Ivo Creusenae026092017-11-20 13:07:16 +01001686// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
Ivo Creusen56d46092017-11-24 17:29:59 +01001687AudioProcessingStats AudioProcessing::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001688 bool has_remote_tracks) const {
1689 return AudioProcessingStats();
1690}
1691
ivoc3e9a5372016-10-28 07:55:33 -07001692AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics()
1693 const {
1694 AudioProcessingStatistics stats;
1695 EchoCancellation::Metrics metrics;
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001696 if (private_submodules_->echo_controller) {
1697 rtc::CritScope cs_capture(&crit_capture_);
1698 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1699 float erl = static_cast<float>(ec_metrics.echo_return_loss);
1700 float erle = static_cast<float>(ec_metrics.echo_return_loss_enhancement);
1701 // Instant value will also be used for min, max and average.
1702 stats.echo_return_loss.Set(erl, erl, erl, erl);
1703 stats.echo_return_loss_enhancement.Set(erle, erle, erle, erle);
1704 } else if (public_submodules_->echo_cancellation->GetMetrics(&metrics) ==
1705 Error::kNoError) {
ivocd0a151c2016-11-02 09:14:37 -07001706 stats.a_nlp.Set(metrics.a_nlp);
1707 stats.divergent_filter_fraction = metrics.divergent_filter_fraction;
1708 stats.echo_return_loss.Set(metrics.echo_return_loss);
1709 stats.echo_return_loss_enhancement.Set(
1710 metrics.echo_return_loss_enhancement);
1711 stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss);
1712 }
ivoc9c192b22017-03-16 04:22:14 -07001713 {
1714 rtc::CritScope cs_capture(&crit_capture_);
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001715 RTC_DCHECK(private_submodules_->echo_detector);
1716 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1717 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
ivoc9c192b22017-03-16 04:22:14 -07001718 stats.residual_echo_likelihood_recent_max =
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001719 ed_metrics.echo_likelihood_recent_max;
ivoc9c192b22017-03-16 04:22:14 -07001720 }
ivoc3e9a5372016-10-28 07:55:33 -07001721 public_submodules_->echo_cancellation->GetDelayMetrics(
1722 &stats.delay_median, &stats.delay_standard_deviation,
1723 &stats.fraction_poor_delays);
1724 return stats;
1725}
1726
Ivo Creusen56d46092017-11-24 17:29:59 +01001727AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001728 bool has_remote_tracks) const {
1729 AudioProcessingStats stats;
1730 if (has_remote_tracks) {
1731 EchoCancellation::Metrics metrics;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001732 if (private_submodules_->echo_controller) {
1733 rtc::CritScope cs_capture(&crit_capture_);
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001734 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1735 stats.echo_return_loss = ec_metrics.echo_return_loss;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001736 stats.echo_return_loss_enhancement =
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001737 ec_metrics.echo_return_loss_enhancement;
Per Åhgren83c4a022017-11-27 12:07:09 +01001738 stats.delay_ms = ec_metrics.delay_ms;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001739 } else if (public_submodules_->echo_cancellation->GetMetrics(&metrics) ==
1740 Error::kNoError) {
Ivo Creusenae026092017-11-20 13:07:16 +01001741 if (metrics.divergent_filter_fraction != -1.0f) {
1742 stats.divergent_filter_fraction =
1743 rtc::Optional<double>(metrics.divergent_filter_fraction);
1744 }
1745 if (metrics.echo_return_loss.instant != -100) {
1746 stats.echo_return_loss =
1747 rtc::Optional<double>(metrics.echo_return_loss.instant);
1748 }
1749 if (metrics.echo_return_loss_enhancement.instant != -100) {
1750 stats.echo_return_loss_enhancement =
1751 rtc::Optional<double>(metrics.echo_return_loss_enhancement.instant);
1752 }
1753 }
1754 if (config_.residual_echo_detector.enabled) {
1755 rtc::CritScope cs_capture(&crit_capture_);
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001756 RTC_DCHECK(private_submodules_->echo_detector);
1757 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1758 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
Ivo Creusenae026092017-11-20 13:07:16 +01001759 stats.residual_echo_likelihood_recent_max =
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001760 ed_metrics.echo_likelihood_recent_max;
Ivo Creusenae026092017-11-20 13:07:16 +01001761 }
1762 int delay_median, delay_std;
1763 float fraction_poor_delays;
1764 if (public_submodules_->echo_cancellation->GetDelayMetrics(
1765 &delay_median, &delay_std, &fraction_poor_delays) ==
1766 Error::kNoError) {
1767 if (delay_median >= 0) {
1768 stats.delay_median_ms = rtc::Optional<int32_t>(delay_median);
1769 }
1770 if (delay_std >= 0) {
1771 stats.delay_standard_deviation_ms = rtc::Optional<int32_t>(delay_std);
1772 }
1773 }
1774 }
1775 return stats;
1776}
1777
niklase@google.com470e71d2011-07-07 08:21:25 +00001778EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
peahb624d8c2016-03-05 03:01:14 -08001779 return public_submodules_->echo_cancellation.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001780}
1781
1782EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
peahbb9edbd2016-03-10 12:54:25 -08001783 return public_submodules_->echo_control_mobile.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001784}
1785
1786GainControl* AudioProcessingImpl::gain_control() const {
peahbe615622016-02-13 16:40:47 -08001787 if (constants_.use_experimental_agc) {
1788 return public_submodules_->gain_control_for_experimental_agc.get();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001789 }
peahbfa97112016-03-10 21:09:04 -08001790 return public_submodules_->gain_control.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001791}
1792
1793HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
peah8271d042016-11-22 07:24:52 -08001794 return high_pass_filter_impl_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001795}
1796
1797LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001798 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001799}
1800
1801NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
solenberg5e465c32015-12-08 13:22:33 -08001802 return public_submodules_->noise_suppression.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001803}
1804
1805VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001806 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001807}
1808
peah8271d042016-11-22 07:24:52 -08001809void AudioProcessingImpl::MutateConfig(
1810 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1811 rtc::CritScope cs_render(&crit_render_);
1812 rtc::CritScope cs_capture(&crit_capture_);
1813 mutator(&config_);
1814 ApplyConfig(config_);
1815}
1816
1817AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1818 rtc::CritScope cs_render(&crit_render_);
1819 rtc::CritScope cs_capture(&crit_capture_);
1820 return config_;
1821}
1822
peah2ace3f92016-09-10 04:42:27 -07001823bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1824 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001825 config_.high_pass_filter.enabled,
peah2ace3f92016-09-10 04:42:27 -07001826 public_submodules_->echo_cancellation->is_enabled(),
1827 public_submodules_->echo_control_mobile->is_enabled(),
ivoc9f4a4a02016-10-28 05:39:16 -07001828 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001829 public_submodules_->noise_suppression->is_enabled(),
1830 capture_nonlocked_.intelligibility_enabled,
1831 capture_nonlocked_.beamformer_enabled,
1832 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001833 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001834 capture_nonlocked_.echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -07001835 public_submodules_->voice_detection->is_enabled(),
1836 public_submodules_->level_estimator->is_enabled(),
1837 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001838}
1839
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001840
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001841void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001842 if (capture_.transient_suppressor_enabled) {
1843 if (!public_submodules_->transient_suppressor.get()) {
1844 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001845 }
peahdf3efa82015-11-28 12:35:15 -08001846 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001847 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1848 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001849 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001850}
1851
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001852void AudioProcessingImpl::InitializeBeamformer() {
aluebsb2328d12016-01-11 20:32:29 -08001853 if (capture_nonlocked_.beamformer_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001854 if (!private_submodules_->beamformer) {
1855 private_submodules_->beamformer.reset(new NonlinearBeamformer(
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001856 capture_.array_geometry, 1u, capture_.target_direction));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001857 }
peahdf3efa82015-11-28 12:35:15 -08001858 private_submodules_->beamformer->Initialize(kChunkSizeMs,
1859 capture_nonlocked_.split_rate);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001860 }
1861}
1862
ekmeyerson60d9b332015-08-14 10:35:55 -07001863void AudioProcessingImpl::InitializeIntelligibility() {
peah1bcfce52016-08-26 07:16:04 -07001864#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001865 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001866 public_submodules_->intelligibility_enhancer.reset(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -08001867 new IntelligibilityEnhancer(capture_nonlocked_.split_rate,
Alex Luebs57ae8292016-03-09 16:24:34 +01001868 render_.render_audio->num_channels(),
Alejandro Luebsef009252016-09-20 14:51:56 -07001869 render_.render_audio->num_bands(),
Alex Luebs57ae8292016-03-09 16:24:34 +01001870 NoiseSuppressionImpl::num_noise_bins()));
ekmeyerson60d9b332015-08-14 10:35:55 -07001871 }
peah1bcfce52016-08-26 07:16:04 -07001872#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001873}
1874
peah8271d042016-11-22 07:24:52 -08001875void AudioProcessingImpl::InitializeLowCutFilter() {
1876 if (config_.high_pass_filter.enabled) {
1877 private_submodules_->low_cut_filter.reset(
1878 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1879 } else {
1880 private_submodules_->low_cut_filter.reset();
1881 }
1882}
alessiob3ec96df2017-05-22 06:57:06 -07001883
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001884void AudioProcessingImpl::InitializeEchoController() {
Gustaf Ullberg002ef282017-10-12 15:13:17 +02001885 if (echo_control_factory_) {
1886 private_submodules_->echo_controller =
1887 echo_control_factory_->Create(proc_sample_rate_hz());
peahe0eae3c2016-12-14 01:16:23 -08001888 } else {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001889 private_submodules_->echo_controller.reset();
peahe0eae3c2016-12-14 01:16:23 -08001890 }
1891}
peah8271d042016-11-22 07:24:52 -08001892
alessiob3ec96df2017-05-22 06:57:06 -07001893void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001894 if (config_.gain_controller2.enabled) {
1895 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001896 }
1897}
1898
Alex Loikob5c9a792018-04-16 16:31:22 +02001899void AudioProcessingImpl::InitializePreAmplifier() {
1900 if (config_.pre_amplifier.enabled) {
1901 private_submodules_->pre_amplifier.reset(
1902 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1903 } else {
1904 private_submodules_->pre_amplifier.reset();
1905 }
1906}
1907
ivoc9f4a4a02016-10-28 05:39:16 -07001908void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001909 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001910 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001911 proc_sample_rate_hz(), 1,
1912 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001913}
1914
Sam Zackrisson0beac582017-09-25 12:04:02 +02001915void AudioProcessingImpl::InitializePostProcessor() {
1916 if (private_submodules_->capture_post_processor) {
1917 private_submodules_->capture_post_processor->Initialize(
1918 proc_sample_rate_hz(), num_proc_channels());
1919 }
1920}
1921
Alex Loiko5825aa62017-12-18 16:02:40 +01001922void AudioProcessingImpl::InitializePreProcessor() {
1923 if (private_submodules_->render_pre_processor) {
1924 private_submodules_->render_pre_processor->Initialize(
1925 formats_.render_processing_format.sample_rate_hz(),
1926 formats_.render_processing_format.num_channels());
1927 }
1928}
1929
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001930void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001931 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001932
1933 if (echo_cancellation()->is_enabled()) {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001934 // Activate delay_jumps_ counters if we know echo_cancellation is running.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001935 // If a stream has echo we know that the echo_cancellation is in process.
peahdf3efa82015-11-28 12:35:15 -08001936 if (capture_.stream_delay_jumps == -1 &&
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001937 echo_cancellation()->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001938 capture_.stream_delay_jumps = 0;
1939 }
1940 if (capture_.aec_system_delay_jumps == -1 &&
1941 echo_cancellation()->stream_has_echo()) {
1942 capture_.aec_system_delay_jumps = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001943 }
1944
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001945 // Detect a jump in platform reported system delay and log the difference.
peahdf3efa82015-11-28 12:35:15 -08001946 const int diff_stream_delay_ms =
1947 capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
1948 if (diff_stream_delay_ms > kMinDiffDelayMs &&
1949 capture_.last_stream_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001950 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1951 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
peahdf3efa82015-11-28 12:35:15 -08001952 if (capture_.stream_delay_jumps == -1) {
1953 capture_.stream_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001954 }
peahdf3efa82015-11-28 12:35:15 -08001955 capture_.stream_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001956 }
peahdf3efa82015-11-28 12:35:15 -08001957 capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001958
1959 // Detect a jump in AEC system delay and log the difference.
peah20028c42016-03-04 11:50:54 -08001960 const int samples_per_ms =
peahdf3efa82015-11-28 12:35:15 -08001961 rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
peah20028c42016-03-04 11:50:54 -08001962 RTC_DCHECK_LT(0, samples_per_ms);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001963 const int aec_system_delay_ms =
peah20028c42016-03-04 11:50:54 -08001964 public_submodules_->echo_cancellation->GetSystemDelayInSamples() /
1965 samples_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001966 const int diff_aec_system_delay_ms =
peahdf3efa82015-11-28 12:35:15 -08001967 aec_system_delay_ms - capture_.last_aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001968 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
peahdf3efa82015-11-28 12:35:15 -08001969 capture_.last_aec_system_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001970 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1971 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1972 100);
peahdf3efa82015-11-28 12:35:15 -08001973 if (capture_.aec_system_delay_jumps == -1) {
1974 capture_.aec_system_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001975 }
peahdf3efa82015-11-28 12:35:15 -08001976 capture_.aec_system_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001977 }
peahdf3efa82015-11-28 12:35:15 -08001978 capture_.last_aec_system_delay_ms = aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001979 }
1980}
1981
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001982void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
peahdf3efa82015-11-28 12:35:15 -08001983 // Run in a single-threaded manner.
1984 rtc::CritScope cs_render(&crit_render_);
1985 rtc::CritScope cs_capture(&crit_capture_);
1986
1987 if (capture_.stream_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001988 RTC_HISTOGRAM_ENUMERATION(
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001989 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
peahdf3efa82015-11-28 12:35:15 -08001990 capture_.stream_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001991 }
peahdf3efa82015-11-28 12:35:15 -08001992 capture_.stream_delay_jumps = -1;
1993 capture_.last_stream_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001994
peahdf3efa82015-11-28 12:35:15 -08001995 if (capture_.aec_system_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001996 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1997 capture_.aec_system_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001998 }
peahdf3efa82015-11-28 12:35:15 -08001999 capture_.aec_system_delay_jumps = -1;
2000 capture_.last_aec_system_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02002001}
2002
aleloi868f32f2017-05-23 07:20:05 -07002003void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
2004 if (!aec_dump_) {
2005 return;
2006 }
2007 std::string experiments_description =
2008 public_submodules_->echo_cancellation->GetExperimentsDescription();
2009 // TODO(peah): Add semicolon-separated concatenations of experiment
2010 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07002011 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
2012 experiments_description += "AgcClippingLevelExperiment;";
2013 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02002014 if (capture_nonlocked_.echo_controller_enabled) {
2015 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07002016 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02002017 if (config_.gain_controller2.enabled) {
2018 experiments_description += "GainController2;";
2019 }
aleloi868f32f2017-05-23 07:20:05 -07002020
2021 InternalAPMConfig apm_config;
2022
2023 apm_config.aec_enabled = public_submodules_->echo_cancellation->is_enabled();
2024 apm_config.aec_delay_agnostic_enabled =
2025 public_submodules_->echo_cancellation->is_delay_agnostic_enabled();
2026 apm_config.aec_drift_compensation_enabled =
2027 public_submodules_->echo_cancellation->is_drift_compensation_enabled();
2028 apm_config.aec_extended_filter_enabled =
2029 public_submodules_->echo_cancellation->is_extended_filter_enabled();
2030 apm_config.aec_suppression_level = static_cast<int>(
2031 public_submodules_->echo_cancellation->suppression_level());
2032
2033 apm_config.aecm_enabled =
2034 public_submodules_->echo_control_mobile->is_enabled();
2035 apm_config.aecm_comfort_noise_enabled =
2036 public_submodules_->echo_control_mobile->is_comfort_noise_enabled();
2037 apm_config.aecm_routing_mode =
2038 static_cast<int>(public_submodules_->echo_control_mobile->routing_mode());
2039
2040 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2041 apm_config.agc_mode =
2042 static_cast<int>(public_submodules_->gain_control->mode());
2043 apm_config.agc_limiter_enabled =
2044 public_submodules_->gain_control->is_limiter_enabled();
2045 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2046
2047 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2048
2049 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2050 apm_config.ns_level =
2051 static_cast<int>(public_submodules_->noise_suppression->level());
2052
2053 apm_config.transient_suppression_enabled =
2054 capture_.transient_suppressor_enabled;
2055 apm_config.intelligibility_enhancer_enabled =
2056 capture_nonlocked_.intelligibility_enabled;
2057 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002058 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2059 apm_config.pre_amplifier_fixed_gain_factor =
2060 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002061
2062 if (!forced && apm_config == apm_config_for_aec_dump_) {
2063 return;
2064 }
2065 aec_dump_->WriteConfig(apm_config);
2066 apm_config_for_aec_dump_ = apm_config;
2067}
2068
2069void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2070 const float* const* src) {
2071 RTC_DCHECK(aec_dump_);
2072 WriteAecDumpConfigMessage(false);
2073
2074 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2075 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2076 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002077 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002078 RecordAudioProcessingState();
2079}
2080
2081void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2082 const AudioFrame& capture_frame) {
2083 RTC_DCHECK(aec_dump_);
2084 WriteAecDumpConfigMessage(false);
2085
2086 aec_dump_->AddCaptureStreamInput(capture_frame);
2087 RecordAudioProcessingState();
2088}
2089
2090void AudioProcessingImpl::RecordProcessedCaptureStream(
2091 const float* const* processed_capture_stream) {
2092 RTC_DCHECK(aec_dump_);
2093
2094 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2095 const size_t num_channels =
2096 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002097 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2098 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002099 aec_dump_->WriteCaptureStreamMessage();
2100}
2101
2102void AudioProcessingImpl::RecordProcessedCaptureStream(
2103 const AudioFrame& processed_capture_frame) {
2104 RTC_DCHECK(aec_dump_);
2105
2106 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2107 aec_dump_->WriteCaptureStreamMessage();
2108}
2109
2110void AudioProcessingImpl::RecordAudioProcessingState() {
2111 RTC_DCHECK(aec_dump_);
2112 AecDump::AudioProcessingState audio_proc_state;
2113 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2114 audio_proc_state.drift =
2115 public_submodules_->echo_cancellation->stream_drift_samples();
2116 audio_proc_state.level = gain_control()->stream_analog_level();
2117 audio_proc_state.keypress = capture_.key_pressed;
2118 aec_dump_->AddAudioProcessingState(audio_proc_state);
2119}
2120
kwiberg83ffe452016-08-29 14:46:07 -07002121AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
2122 bool transient_suppressor_enabled,
2123 const std::vector<Point>& array_geometry,
2124 SphericalPointf target_direction)
2125 : aec_system_delay_jumps(-1),
2126 delay_offset_ms(0),
2127 was_stream_delay_set(false),
2128 last_stream_delay_ms(0),
2129 last_aec_system_delay_ms(0),
2130 stream_delay_jumps(-1),
2131 output_will_be_muted(false),
2132 key_pressed(false),
2133 transient_suppressor_enabled(transient_suppressor_enabled),
2134 array_geometry(array_geometry),
2135 target_direction(target_direction),
peahde65ddc2016-09-16 15:02:15 -07002136 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002137 split_rate(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002138 echo_path_gain_change(false) {}
kwiberg83ffe452016-08-29 14:46:07 -07002139
2140AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2141
2142AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2143
2144AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2145
niklase@google.com470e71d2011-07-07 08:21:25 +00002146} // namespace webrtc