blob: 6d8b209740a5b305c45f5845cd331e1541b08f76 [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
Michael Graczyk86c6d332015-07-23 11:41:39 -070013#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <cstdint>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
alessiob3ec96df2017-05-22 06:57:06 -070016#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <type_traits>
18#include <utility>
niklase@google.com470e71d2011-07-07 08:21:25 +000019
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "absl/types/optional.h"
21#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_audio/audio_converter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_audio/include/audio_util.h"
Alex Loikob5c9a792018-04-16 16:31:22 +020024#include "modules/audio_processing/agc2/gain_applier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/common.h"
Yves Gerey988cc082018-10-23 12:03:01 +020027#include "modules/audio_processing/include/audio_frame_view.h"
Per Åhgren13735822018-02-12 21:42:56 +010028#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/ref_counted_object.h"
34#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/trace_event.h"
Sam Zackrissonfeee1e42019-09-20 07:50:35 +020036#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000038
Michael Graczyk86c6d332015-07-23 11:41:39 -070039#define RETURN_ON_ERR(expr) \
40 do { \
41 int err = (expr); \
42 if (err != kNoError) { \
43 return err; \
44 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000045 } while (0)
46
niklase@google.com470e71d2011-07-07 08:21:25 +000047namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070048
kwibergd59d3bb2016-09-13 07:49:33 -070049constexpr int AudioProcessing::kNativeSampleRatesHz[];
Alex Loiko73ec0192018-05-15 10:52:28 +020050constexpr int kRuntimeSettingQueueSize = 100;
aluebsdf6416a2016-03-16 18:26:35 -070051
Michael Graczyk86c6d332015-07-23 11:41:39 -070052namespace {
53
54static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
55 switch (layout) {
56 case AudioProcessing::kMono:
57 case AudioProcessing::kStereo:
58 return false;
59 case AudioProcessing::kMonoAndKeyboard:
60 case AudioProcessing::kStereoAndKeyboard:
61 return true;
62 }
63
kwiberg9e2be5f2016-09-14 05:23:22 -070064 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070065 return false;
66}
aluebsdf6416a2016-03-16 18:26:35 -070067
peah2ace3f92016-09-10 04:42:27 -070068bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070069 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
70 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
71}
72
Per Åhgren0cbb58e2019-10-29 22:59:44 +010073// Checks whether the legacy ns functionality should be enforced.
74bool DetectLegacyNsEnforcement() {
75 return field_trial::IsEnabled("WebRTC-NewNoiseSuppressionKillSwitch");
76}
77
Per Åhgrenc0424252019-12-10 13:04:15 +010078// Checks whether the high-pass filter should be done in the full-band.
79bool EnforceSplitBandHpf() {
80 return field_trial::IsEnabled("WebRTC-FullBandHpfKillSwitch");
81}
82
Per Åhgrenb2b58d82019-12-02 14:59:40 +010083// Checks whether AEC3 should be allowed to decide what the default
84// configuration should be based on the render and capture channel configuration
85// at hand.
86bool UseSetupSpecificDefaultAec3Congfig() {
87 return !field_trial::IsEnabled(
88 "WebRTC-Aec3SetupSpecificDefaultConfigDefaultsKillSwitch");
89}
90
Per Åhgrenc8626b62019-08-23 15:49:51 +020091// Identify the native processing rate that best handles a sample rate.
Per Åhgrenfcbe4072019-09-15 00:27:58 +020092int SuitableProcessRate(int minimum_rate,
93 int max_splitting_rate,
94 bool band_splitting_required) {
Per Åhgrenc8626b62019-08-23 15:49:51 +020095 const int uppermost_native_rate =
Per Åhgrenfcbe4072019-09-15 00:27:58 +020096 band_splitting_required ? max_splitting_rate : 48000;
Per Åhgrenc8626b62019-08-23 15:49:51 +020097 for (auto rate : {16000, 32000, 48000}) {
peah2ace3f92016-09-10 04:42:27 -070098 if (rate >= uppermost_native_rate) {
99 return uppermost_native_rate;
100 }
101 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700102 return rate;
103 }
104 }
peah2ace3f92016-09-10 04:42:27 -0700105 RTC_NOTREACHED();
106 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700107}
108
Sam Zackrisson23513132019-01-11 15:10:32 +0100109NoiseSuppression::Level NsConfigLevelToInterfaceLevel(
110 AudioProcessing::Config::NoiseSuppression::Level level) {
111 using NsConfig = AudioProcessing::Config::NoiseSuppression;
112 switch (level) {
113 case NsConfig::kLow:
saza0bad15f2019-10-16 11:46:11 +0200114 return NoiseSuppression::Level::kLow;
Sam Zackrisson23513132019-01-11 15:10:32 +0100115 case NsConfig::kModerate:
saza0bad15f2019-10-16 11:46:11 +0200116 return NoiseSuppression::Level::kModerate;
Sam Zackrisson23513132019-01-11 15:10:32 +0100117 case NsConfig::kHigh:
saza0bad15f2019-10-16 11:46:11 +0200118 return NoiseSuppression::Level::kHigh;
Sam Zackrisson23513132019-01-11 15:10:32 +0100119 case NsConfig::kVeryHigh:
saza0bad15f2019-10-16 11:46:11 +0200120 return NoiseSuppression::Level::kVeryHigh;
Sam Zackrisson23513132019-01-11 15:10:32 +0100121 default:
122 RTC_NOTREACHED();
123 }
124}
125
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100126GainControl::Mode Agc1ConfigModeToInterfaceMode(
127 AudioProcessing::Config::GainController1::Mode mode) {
128 using Agc1Config = AudioProcessing::Config::GainController1;
129 switch (mode) {
130 case Agc1Config::kAdaptiveAnalog:
131 return GainControl::kAdaptiveAnalog;
132 case Agc1Config::kAdaptiveDigital:
133 return GainControl::kAdaptiveDigital;
134 case Agc1Config::kFixedDigital:
135 return GainControl::kFixedDigital;
136 }
137}
138
peah9e6a2902017-05-15 07:19:21 -0700139// Maximum lengths that frame of samples being passed from the render side to
140// the capture side can have (does not apply to AEC3).
141static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
142static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
143
peah764e3642016-10-22 05:04:30 -0700144// Maximum number of frames to buffer in the render queue.
145// TODO(peah): Decrease this once we properly handle hugely unbalanced
146// reverse and forward call numbers.
147static const size_t kMaxNumFramesToBuffer = 100;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700148} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000149
150// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000151static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000152
saza1d600522019-10-18 13:29:43 +0200153AudioProcessingImpl::SubmoduleStates::SubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100154 bool capture_post_processor_enabled,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200155 bool render_pre_processor_enabled,
156 bool capture_analyzer_enabled)
Alex Loiko5825aa62017-12-18 16:02:40 +0100157 : capture_post_processor_enabled_(capture_post_processor_enabled),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200158 render_pre_processor_enabled_(render_pre_processor_enabled),
159 capture_analyzer_enabled_(capture_analyzer_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700160
saza1d600522019-10-18 13:29:43 +0200161bool AudioProcessingImpl::SubmoduleStates::Update(
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200162 bool high_pass_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700163 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700164 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700165 bool noise_suppressor_enabled,
peah2ace3f92016-09-10 04:42:27 -0700166 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700167 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200168 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200169 bool echo_controller_enabled,
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200170 bool voice_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700171 bool transient_suppressor_enabled) {
172 bool changed = false;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200173 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700174 changed |=
175 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700176 changed |=
177 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700178 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
179 changed |=
peah2ace3f92016-09-10 04:42:27 -0700180 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200181 changed |= (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200182 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200183 changed |= (echo_controller_enabled != echo_controller_enabled_);
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200184 changed |= (voice_detector_enabled != voice_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700185 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
186 if (changed) {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200187 high_pass_filter_enabled_ = high_pass_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700188 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700189 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700190 noise_suppressor_enabled_ = noise_suppressor_enabled;
peah2ace3f92016-09-10 04:42:27 -0700191 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700192 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200193 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200194 echo_controller_enabled_ = echo_controller_enabled;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200195 voice_detector_enabled_ = voice_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700196 transient_suppressor_enabled_ = transient_suppressor_enabled;
197 }
198
199 changed |= first_update_;
200 first_update_ = false;
201 return changed;
202}
203
saza1d600522019-10-18 13:29:43 +0200204bool AudioProcessingImpl::SubmoduleStates::CaptureMultiBandSubModulesActive()
peah2ace3f92016-09-10 04:42:27 -0700205 const {
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200206 return CaptureMultiBandProcessingPresent() || voice_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700207}
208
saza1d600522019-10-18 13:29:43 +0200209bool AudioProcessingImpl::SubmoduleStates::CaptureMultiBandProcessingPresent()
210 const {
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200211 // If echo controller is present, assume it performs active processing.
212 return CaptureMultiBandProcessingActive(/*ec_processing_active=*/true);
213}
214
saza1d600522019-10-18 13:29:43 +0200215bool AudioProcessingImpl::SubmoduleStates::CaptureMultiBandProcessingActive(
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200216 bool ec_processing_active) const {
Per Åhgren62ea0aa2019-12-09 10:18:44 +0100217 return high_pass_filter_enabled_ || mobile_echo_controller_enabled_ ||
218 noise_suppressor_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200219 (echo_controller_enabled_ && ec_processing_active);
peah2ace3f92016-09-10 04:42:27 -0700220}
221
saza1d600522019-10-18 13:29:43 +0200222bool AudioProcessingImpl::SubmoduleStates::CaptureFullBandProcessingActive()
peah23ac8b42017-05-23 05:33:56 -0700223 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200224 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
225 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700226}
227
saza1d600522019-10-18 13:29:43 +0200228bool AudioProcessingImpl::SubmoduleStates::CaptureAnalyzerActive() const {
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200229 return capture_analyzer_enabled_;
230}
231
saza1d600522019-10-18 13:29:43 +0200232bool AudioProcessingImpl::SubmoduleStates::RenderMultiBandSubModulesActive()
peah2ace3f92016-09-10 04:42:27 -0700233 const {
Per Åhgren62ea0aa2019-12-09 10:18:44 +0100234 return RenderMultiBandProcessingActive() || mobile_echo_controller_enabled_ ||
235 adaptive_gain_controller_enabled_ || echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700236}
237
saza1d600522019-10-18 13:29:43 +0200238bool AudioProcessingImpl::SubmoduleStates::RenderFullBandProcessingActive()
Alex Loiko5825aa62017-12-18 16:02:40 +0100239 const {
240 return render_pre_processor_enabled_;
241}
242
saza1d600522019-10-18 13:29:43 +0200243bool AudioProcessingImpl::SubmoduleStates::RenderMultiBandProcessingActive()
peah2ace3f92016-09-10 04:42:27 -0700244 const {
peah2ace3f92016-09-10 04:42:27 -0700245 return false;
peah2ace3f92016-09-10 04:42:27 -0700246}
247
saza1d600522019-10-18 13:29:43 +0200248bool AudioProcessingImpl::SubmoduleStates::HighPassFilteringRequired() const {
Per Åhgren62ea0aa2019-12-09 10:18:44 +0100249 return high_pass_filter_enabled_ || mobile_echo_controller_enabled_ ||
250 noise_suppressor_enabled_;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200251}
252
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100253AudioProcessingBuilder::AudioProcessingBuilder() = default;
254AudioProcessingBuilder::~AudioProcessingBuilder() = default;
255
256AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
257 std::unique_ptr<CustomProcessing> capture_post_processing) {
258 capture_post_processing_ = std::move(capture_post_processing);
259 return *this;
260}
261
262AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
263 std::unique_ptr<CustomProcessing> render_pre_processing) {
264 render_pre_processing_ = std::move(render_pre_processing);
265 return *this;
266}
267
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200268AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
269 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
270 capture_analyzer_ = std::move(capture_analyzer);
271 return *this;
272}
273
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100274AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
275 std::unique_ptr<EchoControlFactory> echo_control_factory) {
276 echo_control_factory_ = std::move(echo_control_factory);
277 return *this;
278}
279
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100280AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200281 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100282 echo_detector_ = std::move(echo_detector);
283 return *this;
284}
285
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100286AudioProcessing* AudioProcessingBuilder::Create() {
287 webrtc::Config config;
288 return Create(config);
289}
290
291AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100292 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
293 config, std::move(capture_post_processing_),
294 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200295 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100296 if (apm->Initialize() != AudioProcessing::kNoError) {
297 delete apm;
298 apm = nullptr;
299 }
300 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100301}
302
peah88ac8532016-09-12 16:47:25 -0700303AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200304 : AudioProcessingImpl(config,
305 /*capture_post_processor=*/nullptr,
306 /*render_pre_processor=*/nullptr,
307 /*echo_control_factory=*/nullptr,
308 /*echo_detector=*/nullptr,
309 /*capture_analyzer=*/nullptr) {}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000310
Per Åhgren13735822018-02-12 21:42:56 +0100311int AudioProcessingImpl::instance_count_ = 0;
312
Sam Zackrisson0beac582017-09-25 12:04:02 +0200313AudioProcessingImpl::AudioProcessingImpl(
314 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100315 std::unique_ptr<CustomProcessing> capture_post_processor,
316 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200317 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200318 rtc::scoped_refptr<EchoDetector> echo_detector,
319 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100320 : data_dumper_(
321 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren0cbb58e2019-10-29 22:59:44 +0100322 enforced_usage_of_legacy_ns_(DetectLegacyNsEnforcement()),
Per Åhgrenb2b58d82019-12-02 14:59:40 +0100323 use_setup_specific_default_aec3_config_(
324 UseSetupSpecificDefaultAec3Congfig()),
Alex Loiko73ec0192018-05-15 10:52:28 +0200325 capture_runtime_settings_(kRuntimeSettingQueueSize),
326 render_runtime_settings_(kRuntimeSettingQueueSize),
327 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
328 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200329 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200330 submodule_states_(!!capture_post_processor,
331 !!render_pre_processor,
332 !!capture_analyzer),
saza1d600522019-10-18 13:29:43 +0200333 submodules_(std::move(capture_post_processor),
334 std::move(render_pre_processor),
335 std::move(echo_detector),
Per Åhgren3daedb62019-11-22 12:11:40 +0100336 std::move(capture_analyzer)),
337 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
338 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000339#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Per Åhgren3daedb62019-11-22 12:11:40 +0100340 /* enabled= */ false,
341 /* enabled_agc2_level_estimator= */ false,
342 /* digital_adaptive_disabled= */ false,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000343#else
Per Åhgren3daedb62019-11-22 12:11:40 +0100344 config.Get<ExperimentalAgc>().enabled,
345 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
346 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000347#endif
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200348 !field_trial::IsEnabled(
349 "WebRTC-ApmExperimentalMultiChannelRenderKillSwitch"),
350 !field_trial::IsEnabled(
Per Åhgrenc0424252019-12-10 13:04:15 +0100351 "WebRTC-ApmExperimentalMultiChannelCaptureKillSwitch"),
352 EnforceSplitBandHpf()),
andrew1c7075f2015-06-24 18:14:14 -0700353#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200354 capture_(false),
andrew1c7075f2015-06-24 18:14:14 -0700355#else
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200356 capture_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700357#endif
Alessio Bazzicacc22f512018-08-30 13:01:34 +0200358 capture_nonlocked_() {
Sam Zackrisson72cc71c2019-10-21 12:54:02 +0200359 RTC_LOG(LS_INFO) << "Injected APM submodules:"
360 << "\nEcho control factory: " << !!echo_control_factory_
361 << "\nEcho detector: " << !!submodules_.echo_detector
362 << "\nCapture analyzer: " << !!submodules_.capture_analyzer
363 << "\nCapture post processor: "
364 << !!submodules_.capture_post_processor
365 << "\nRender pre processor: "
366 << !!submodules_.render_pre_processor;
367
Sam Zackrisson421c8592019-02-11 13:39:46 +0100368 // Mark Echo Controller enabled if a factory is injected.
369 capture_nonlocked_.echo_controller_enabled =
370 static_cast<bool>(echo_control_factory_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000371
saza1d600522019-10-18 13:29:43 +0200372 submodules_.gain_control.reset(new GainControlImpl());
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200373
Sam Zackrisson421c8592019-02-11 13:39:46 +0100374 // If no echo detector is injected, use the ResidualEchoDetector.
saza1d600522019-10-18 13:29:43 +0200375 if (!submodules_.echo_detector) {
376 submodules_.echo_detector =
Sam Zackrisson421c8592019-02-11 13:39:46 +0100377 new rtc::RefCountedObject<ResidualEchoDetector>();
peahdf3efa82015-11-28 12:35:15 -0800378 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000379
Sam Zackrisson421c8592019-02-11 13:39:46 +0100380 // TODO(alessiob): Move the injected gain controller once injection is
381 // implemented.
saza1d600522019-10-18 13:29:43 +0200382 submodules_.gain_controller2.reset(new GainController2());
Sam Zackrisson421c8592019-02-11 13:39:46 +0100383
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000384 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000385}
386
Per Åhgren0e3198e2019-11-18 08:52:22 +0100387AudioProcessingImpl::~AudioProcessingImpl() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +0000388
niklase@google.com470e71d2011-07-07 08:21:25 +0000389int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800390 // Run in a single-threaded manner during initialization.
391 rtc::CritScope cs_render(&crit_render_);
392 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000393 return InitializeLocked();
394}
395
peahde65ddc2016-09-16 15:02:15 -0700396int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
397 int capture_output_sample_rate_hz,
398 int render_input_sample_rate_hz,
399 ChannelLayout capture_input_layout,
400 ChannelLayout capture_output_layout,
401 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700402 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700403 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
404 LayoutHasKeyboard(capture_input_layout)},
405 {capture_output_sample_rate_hz,
406 ChannelsFromLayout(capture_output_layout),
407 LayoutHasKeyboard(capture_output_layout)},
408 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
409 LayoutHasKeyboard(render_input_layout)},
410 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
411 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700412
413 return Initialize(processing_config);
414}
415
416int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800417 // Run in a single-threaded manner during initialization.
418 rtc::CritScope cs_render(&crit_render_);
419 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700420 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000421}
422
peahdf3efa82015-11-28 12:35:15 -0800423int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800424 const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800425 // Called from both threads. Thread check is therefore not possible.
Oskar Sundbom4b276482019-05-23 14:28:00 +0200426 if (processing_config == formats_.api_format) {
peah192164e2015-11-17 02:16:45 -0800427 return kNoError;
428 }
peahdf3efa82015-11-28 12:35:15 -0800429
430 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800431 return InitializeLocked(processing_config);
432}
433
niklase@google.com470e71d2011-07-07 08:21:25 +0000434int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200435 UpdateActiveSubmoduleStates();
436
Per Åhgrend47941e2019-08-22 11:51:13 +0200437 const int render_audiobuffer_sample_rate_hz =
peahdf3efa82015-11-28 12:35:15 -0800438 formats_.api_format.reverse_output_stream().num_frames() == 0
Per Åhgrend47941e2019-08-22 11:51:13 +0200439 ? formats_.render_processing_format.sample_rate_hz()
440 : formats_.api_format.reverse_output_stream().sample_rate_hz();
peahdf3efa82015-11-28 12:35:15 -0800441 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
442 render_.render_audio.reset(new AudioBuffer(
Per Åhgrend47941e2019-08-22 11:51:13 +0200443 formats_.api_format.reverse_input_stream().sample_rate_hz(),
peahdf3efa82015-11-28 12:35:15 -0800444 formats_.api_format.reverse_input_stream().num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200445 formats_.render_processing_format.sample_rate_hz(),
peahde65ddc2016-09-16 15:02:15 -0700446 formats_.render_processing_format.num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200447 render_audiobuffer_sample_rate_hz,
448 formats_.render_processing_format.num_channels()));
peah2ace3f92016-09-10 04:42:27 -0700449 if (formats_.api_format.reverse_input_stream() !=
450 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800451 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800452 formats_.api_format.reverse_input_stream().num_channels(),
453 formats_.api_format.reverse_input_stream().num_frames(),
454 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800455 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700456 } else {
peahdf3efa82015-11-28 12:35:15 -0800457 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700458 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700459 } else {
peahdf3efa82015-11-28 12:35:15 -0800460 render_.render_audio.reset(nullptr);
461 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700462 }
peahce4d9152017-05-19 01:28:05 -0700463
Per Åhgrend47941e2019-08-22 11:51:13 +0200464 capture_.capture_audio.reset(new AudioBuffer(
465 formats_.api_format.input_stream().sample_rate_hz(),
466 formats_.api_format.input_stream().num_channels(),
467 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
468 formats_.api_format.output_stream().num_channels(),
469 formats_.api_format.output_stream().sample_rate_hz(),
470 formats_.api_format.output_stream().num_channels()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000471
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200472 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() <
473 formats_.api_format.output_stream().sample_rate_hz() &&
474 formats_.api_format.output_stream().sample_rate_hz() == 48000) {
475 capture_.capture_fullband_audio.reset(
476 new AudioBuffer(formats_.api_format.input_stream().sample_rate_hz(),
477 formats_.api_format.input_stream().num_channels(),
478 formats_.api_format.output_stream().sample_rate_hz(),
479 formats_.api_format.output_stream().num_channels(),
480 formats_.api_format.output_stream().sample_rate_hz(),
481 formats_.api_format.output_stream().num_channels()));
482 } else {
483 capture_.capture_fullband_audio.reset();
484 }
485
peah764e3642016-10-22 05:04:30 -0700486 AllocateRenderQueue();
487
saza1d600522019-10-18 13:29:43 +0200488 submodules_.gain_control->Initialize(num_proc_channels(),
489 proc_sample_rate_hz());
Per Åhgren3daedb62019-11-22 12:11:40 +0100490 if (constants_.use_experimental_agc) {
491 if (!submodules_.agc_manager.get() ||
492 submodules_.agc_manager->num_channels() !=
493 static_cast<int>(num_proc_channels()) ||
494 submodules_.agc_manager->sample_rate_hz() !=
495 capture_nonlocked_.split_rate) {
Per Åhgren2a6b3b12019-11-26 19:34:26 +0100496 int stream_analog_level = -1;
497 const bool re_creation = !!submodules_.agc_manager;
498 if (re_creation) {
499 stream_analog_level = submodules_.agc_manager->stream_analog_level();
500 }
Per Åhgren3daedb62019-11-22 12:11:40 +0100501 submodules_.agc_manager.reset(new AgcManagerDirect(
502 num_proc_channels(), constants_.agc_startup_min_volume,
503 constants_.agc_clipped_level_min,
504 constants_.use_experimental_agc_agc2_level_estimation,
505 constants_.use_experimental_agc_agc2_digital_adaptive,
506 capture_nonlocked_.split_rate));
Per Åhgren2a6b3b12019-11-26 19:34:26 +0100507 if (re_creation) {
508 submodules_.agc_manager->set_stream_analog_level(stream_analog_level);
509 }
Per Åhgren3daedb62019-11-22 12:11:40 +0100510 }
saza1d600522019-10-18 13:29:43 +0200511 submodules_.agc_manager->Initialize();
Per Åhgren3daedb62019-11-22 12:11:40 +0100512 submodules_.agc_manager->SetupDigitalGainControl(
Per Åhgren0e3198e2019-11-18 08:52:22 +0100513 submodules_.gain_control.get());
saza1d600522019-10-18 13:29:43 +0200514 submodules_.agc_manager->SetCaptureMuted(capture_.output_will_be_muted);
peahde65ddc2016-09-16 15:02:15 -0700515 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200516 InitializeTransient();
Per Åhgren4011de02019-12-03 11:48:48 +0000517 InitializeHighPassFilter();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200518 InitializeVoiceDetector();
ivoc9f4a4a02016-10-28 05:39:16 -0700519 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200520 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700521 InitializeGainController2();
saza0bad15f2019-10-16 11:46:11 +0200522 InitializeNoiseSuppressor();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200523 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200524 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100525 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800526
aleloi868f32f2017-05-23 07:20:05 -0700527 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200528 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700529 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000530 return kNoError;
531}
532
Michael Graczyk86c6d332015-07-23 11:41:39 -0700533int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200534 UpdateActiveSubmoduleStates();
535
Michael Graczyk86c6d332015-07-23 11:41:39 -0700536 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700537 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
538 return kBadSampleRateError;
539 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000540 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700541
Peter Kasting69558702016-01-12 16:26:35 -0800542 const size_t num_in_channels = config.input_stream().num_channels();
543 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700544
545 // Need at least one input channel.
546 // Need either one output channel or as many outputs as there are inputs.
547 if (num_in_channels == 0 ||
548 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700549 return kBadNumberChannelsError;
550 }
551
peahdf3efa82015-11-28 12:35:15 -0800552 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000553
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200554 // Choose maximum rate to use for the split filtering.
555 RTC_DCHECK(config_.pipeline.maximum_internal_processing_rate == 48000 ||
556 config_.pipeline.maximum_internal_processing_rate == 32000);
557 int max_splitting_rate = 48000;
558 if (config_.pipeline.maximum_internal_processing_rate == 32000) {
559 max_splitting_rate = config_.pipeline.maximum_internal_processing_rate;
560 }
561
Per Åhgrenc8626b62019-08-23 15:49:51 +0200562 int capture_processing_rate = SuitableProcessRate(
peah423d2362016-04-09 16:06:52 -0700563 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700564 formats_.api_format.output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200565 max_splitting_rate,
peah2ace3f92016-09-10 04:42:27 -0700566 submodule_states_.CaptureMultiBandSubModulesActive() ||
567 submodule_states_.RenderMultiBandSubModulesActive());
Per Åhgrenc8626b62019-08-23 15:49:51 +0200568 RTC_DCHECK_NE(8000, capture_processing_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000569
peahde65ddc2016-09-16 15:02:15 -0700570 capture_nonlocked_.capture_processing_format =
571 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700572
peah2ce640f2017-04-07 03:57:48 -0700573 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200574 if (!capture_nonlocked_.echo_controller_enabled) {
Per Åhgrenc8626b62019-08-23 15:49:51 +0200575 render_processing_rate = SuitableProcessRate(
peah2ce640f2017-04-07 03:57:48 -0700576 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
577 formats_.api_format.reverse_output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200578 max_splitting_rate,
peah2ce640f2017-04-07 03:57:48 -0700579 submodule_states_.CaptureMultiBandSubModulesActive() ||
580 submodule_states_.RenderMultiBandSubModulesActive());
581 } else {
582 render_processing_rate = capture_processing_rate;
583 }
584
peahde65ddc2016-09-16 15:02:15 -0700585 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700586 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700587 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
588 kSampleRate8kHz) {
589 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000590 } else {
peahde65ddc2016-09-16 15:02:15 -0700591 render_processing_rate =
592 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000593 }
594
Per Åhgrenc8626b62019-08-23 15:49:51 +0200595 RTC_DCHECK_NE(8000, render_processing_rate);
596
peahce4d9152017-05-19 01:28:05 -0700597 if (submodule_states_.RenderMultiBandSubModulesActive()) {
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200598 // By default, downmix the render stream to mono for analysis. This has been
599 // demonstrated to work well for AEC in most practical scenarios.
Per Åhgrene14cb992019-11-27 09:34:22 +0100600 const bool multi_channel_render = config_.pipeline.multi_channel_render &&
601 constants_.multi_channel_render_support;
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200602 int render_processing_num_channels =
Per Åhgrene14cb992019-11-27 09:34:22 +0100603 multi_channel_render
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200604 ? formats_.api_format.reverse_input_stream().num_channels()
605 : 1;
606 formats_.render_processing_format =
607 StreamConfig(render_processing_rate, render_processing_num_channels);
peahce4d9152017-05-19 01:28:05 -0700608 } else {
609 formats_.render_processing_format = StreamConfig(
610 formats_.api_format.reverse_input_stream().sample_rate_hz(),
611 formats_.api_format.reverse_input_stream().num_channels());
612 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000613
peahde65ddc2016-09-16 15:02:15 -0700614 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
615 kSampleRate32kHz ||
616 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
617 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800618 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000619 } else {
peahdf3efa82015-11-28 12:35:15 -0800620 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700621 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000622 }
623
624 return InitializeLocked();
625}
626
peah88ac8532016-09-12 16:47:25 -0700627void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
Sam Zackrisson72cc71c2019-10-21 12:54:02 +0200628 RTC_LOG(LS_INFO) << "AudioProcessing::ApplyConfig: " << config.ToString();
629
peah88ac8532016-09-12 16:47:25 -0700630 // Run in a single-threaded manner when applying the settings.
631 rtc::CritScope cs_render(&crit_render_);
632 rtc::CritScope cs_capture(&crit_capture_);
633
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200634 const bool pipeline_config_changed =
Per Åhgrene14cb992019-11-27 09:34:22 +0100635 config_.pipeline.multi_channel_render !=
636 config.pipeline.multi_channel_render ||
637 config_.pipeline.multi_channel_capture !=
Per Åhgrenc0424252019-12-10 13:04:15 +0100638 config.pipeline.multi_channel_capture ||
639 config_.pipeline.maximum_internal_processing_rate !=
640 config.pipeline.maximum_internal_processing_rate;
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200641
Per Åhgren200feba2019-03-06 04:16:46 +0100642 const bool aec_config_changed =
643 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
Per Åhgren62ea0aa2019-12-09 10:18:44 +0100644 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode;
Per Åhgren200feba2019-03-06 04:16:46 +0100645
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100646 const bool agc1_config_changed =
647 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
648 config_.gain_controller1.mode != config.gain_controller1.mode ||
649 config_.gain_controller1.target_level_dbfs !=
650 config.gain_controller1.target_level_dbfs ||
651 config_.gain_controller1.compression_gain_db !=
652 config.gain_controller1.compression_gain_db ||
653 config_.gain_controller1.enable_limiter !=
654 config.gain_controller1.enable_limiter ||
655 config_.gain_controller1.analog_level_minimum !=
656 config.gain_controller1.analog_level_minimum ||
657 config_.gain_controller1.analog_level_maximum !=
658 config.gain_controller1.analog_level_maximum;
659
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200660 const bool voice_detection_config_changed =
661 config_.voice_detection.enabled != config.voice_detection.enabled;
662
saza0bad15f2019-10-16 11:46:11 +0200663 const bool ns_config_changed =
664 config_.noise_suppression.enabled != config.noise_suppression.enabled ||
665 config_.noise_suppression.level != config.noise_suppression.level;
666
Yves Gerey499bc6c2018-10-10 18:29:07 +0200667 config_ = config;
668
Per Åhgren200feba2019-03-06 04:16:46 +0100669 if (aec_config_changed) {
670 InitializeEchoController();
671 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200672
saza0bad15f2019-10-16 11:46:11 +0200673 if (ns_config_changed) {
674 InitializeNoiseSuppressor();
675 }
Sam Zackrisson23513132019-01-11 15:10:32 +0100676
Per Åhgren4011de02019-12-03 11:48:48 +0000677 InitializeHighPassFilter();
peah8271d042016-11-22 07:24:52 -0800678
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100679 if (agc1_config_changed) {
680 ApplyAgc1Config(config_.gain_controller1);
681 }
682
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100683 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700684 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100685 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
686 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100687 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100688 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700689 config_.gain_controller2 = AudioProcessing::Config::GainController2();
690 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200691 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200692 InitializePreAmplifier();
saza1d600522019-10-18 13:29:43 +0200693 submodules_.gain_controller2->ApplyConfig(config_.gain_controller2);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100694
saza1d600522019-10-18 13:29:43 +0200695 if (config_.level_estimation.enabled && !submodules_.output_level_estimator) {
696 submodules_.output_level_estimator = std::make_unique<LevelEstimator>();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100697 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100698
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200699 if (voice_detection_config_changed) {
700 InitializeVoiceDetector();
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100701 }
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200702
703 // Reinitialization must happen after all submodule configuration to avoid
704 // additional reinitializations on the next capture / render processing call.
705 if (pipeline_config_changed) {
706 InitializeLocked(formats_.api_format);
707 }
peah88ac8532016-09-12 16:47:25 -0700708}
709
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100710void AudioProcessingImpl::ApplyAgc1Config(
711 const Config::GainController1& config) {
Per Åhgren0e3198e2019-11-18 08:52:22 +0100712 int error = submodules_.gain_control->Enable(config.enabled);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100713 RTC_DCHECK_EQ(kNoError, error);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100714
Per Åhgren0e3198e2019-11-18 08:52:22 +0100715 if (!submodules_.agc_manager) {
716 error = submodules_.gain_control->set_mode(
717 Agc1ConfigModeToInterfaceMode(config.mode));
718 RTC_DCHECK_EQ(kNoError, error);
719 error = submodules_.gain_control->set_target_level_dbfs(
720 config.target_level_dbfs);
721 RTC_DCHECK_EQ(kNoError, error);
722 error = submodules_.gain_control->set_compression_gain_db(
723 config.compression_gain_db);
724 RTC_DCHECK_EQ(kNoError, error);
725 error = submodules_.gain_control->enable_limiter(config.enable_limiter);
726 RTC_DCHECK_EQ(kNoError, error);
727 error = submodules_.gain_control->set_analog_level_limits(
728 config.analog_level_minimum, config.analog_level_maximum);
729 RTC_DCHECK_EQ(kNoError, error);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100730 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100731}
732
peah88ac8532016-09-12 16:47:25 -0700733void 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
peahdf3efa82015-11-28 12:35:15 -0800738 if (capture_.transient_suppressor_enabled !=
739 config.Get<ExperimentalNs>().enabled) {
740 capture_.transient_suppressor_enabled =
741 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000742 InitializeTransient();
743 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000744}
745
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000746int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800747 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700748 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000749}
750
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200751int AudioProcessingImpl::proc_fullband_sample_rate_hz() const {
752 return capture_.capture_fullband_audio
753 ? capture_.capture_fullband_audio->num_frames() * 100
754 : capture_nonlocked_.capture_processing_format.sample_rate_hz();
755}
756
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000757int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800758 // Used as callback from submodules, hence locking is not allowed.
759 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000760}
761
Peter Kasting69558702016-01-12 16:26:35 -0800762size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800763 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700764 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000765}
766
Peter Kasting69558702016-01-12 16:26:35 -0800767size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800768 // Used as callback from submodules, hence locking is not allowed.
769 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000770}
771
Peter Kasting69558702016-01-12 16:26:35 -0800772size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800773 // Used as callback from submodules, hence locking is not allowed.
Per Åhgrene14cb992019-11-27 09:34:22 +0100774 const bool multi_channel_capture = config_.pipeline.multi_channel_capture &&
775 constants_.multi_channel_capture_support;
776 if (capture_nonlocked_.echo_controller_enabled && !multi_channel_capture) {
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200777 return 1;
778 }
779 return num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800780}
781
Peter Kasting69558702016-01-12 16:26:35 -0800782size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800783 // Used as callback from submodules, hence locking is not allowed.
784 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000785}
786
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000787void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800788 rtc::CritScope cs(&crit_capture_);
789 capture_.output_will_be_muted = muted;
saza1d600522019-10-18 13:29:43 +0200790 if (submodules_.agc_manager.get()) {
791 submodules_.agc_manager->SetCaptureMuted(capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000792 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000793}
794
Alessio Bazzicac054e782018-04-16 12:10:09 +0200795void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200796 switch (setting.type()) {
797 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
Alessio Bazzica7c19a702019-11-07 13:22:00 +0100798 case RuntimeSetting::Type::kPlayoutAudioDeviceChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200799 render_runtime_settings_enqueuer_.Enqueue(setting);
800 return;
Alex Loiko73ec0192018-05-15 10:52:28 +0200801 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100802 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200803 case RuntimeSetting::Type::kCaptureFixedPostGain:
Alessio Bazzica7c19a702019-11-07 13:22:00 +0100804 capture_runtime_settings_enqueuer_.Enqueue(setting);
805 return;
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200806 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200807 capture_runtime_settings_enqueuer_.Enqueue(setting);
Alessio Bazzica7c19a702019-11-07 13:22:00 +0100808 render_runtime_settings_enqueuer_.Enqueue(setting);
809 return;
810 case RuntimeSetting::Type::kNotSpecified:
811 RTC_NOTREACHED();
Alex Loiko73ec0192018-05-15 10:52:28 +0200812 return;
813 }
814 // The language allows the enum to have a non-enumerator
815 // value. Check that this doesn't happen.
816 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200817}
818
819AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
820 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200821 : runtime_settings_(*runtime_settings) {
822 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200823}
824
825AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
826 default;
827
828void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
829 RuntimeSetting setting) {
830 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200831 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200832 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200833 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200834 RTC_LOG(LS_ERROR)
835 << "The runtime settings queue is full. Oldest setting discarded.";
836 }
837 if (remaining_attempts == 0)
838 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
839}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000840
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000841int AudioProcessingImpl::ProcessStream(const float* const* src,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700842 const StreamConfig& input_config,
843 const StreamConfig& output_config,
844 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800845 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800846 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700847 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800848 {
849 // Acquire the capture lock in order to safely call the function
850 // that retrieves the render side data. This function accesses apm
851 // getters that need the capture lock held when being called.
852 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700853 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800854
855 if (!src || !dest) {
856 return kNullPointerError;
857 }
858
859 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700860 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000861 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000862
Oskar Sundbom4b276482019-05-23 14:28:00 +0200863 if (processing_config.input_stream() != input_config) {
864 processing_config.input_stream() = input_config;
865 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800866 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200867
868 if (processing_config.output_stream() != output_config) {
869 processing_config.output_stream() = output_config;
870 reinitialization_required = true;
871 }
872
873 if (reinitialization_required) {
874 // Reinitialize.
875 rtc::CritScope cs_render(&crit_render_);
876 rtc::CritScope cs_capture(&crit_capture_);
877 RETURN_ON_ERR(InitializeLocked(processing_config));
878 }
879
peahdf3efa82015-11-28 12:35:15 -0800880 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700881 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
882 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000883
aleloi868f32f2017-05-23 07:20:05 -0700884 if (aec_dump_) {
885 RecordUnprocessedCaptureStream(src);
886 }
887
Per Åhgrena1351272019-08-15 12:15:46 +0200888 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800889 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200890 if (capture_.capture_fullband_audio) {
891 capture_.capture_fullband_audio->CopyFrom(
892 src, formats_.api_format.input_stream());
893 }
peahde65ddc2016-09-16 15:02:15 -0700894 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200895 if (capture_.capture_fullband_audio) {
896 capture_.capture_fullband_audio->CopyTo(formats_.api_format.output_stream(),
897 dest);
898 } else {
899 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
900 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000901
aleloi868f32f2017-05-23 07:20:05 -0700902 if (aec_dump_) {
903 RecordProcessedCaptureStream(dest);
904 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000905 return kNoError;
906}
907
Alex Loiko73ec0192018-05-15 10:52:28 +0200908void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200909 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200910 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200911 if (aec_dump_) {
912 aec_dump_->WriteRuntimeSetting(setting);
913 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200914 switch (setting.type()) {
915 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200916 if (config_.pre_amplifier.enabled) {
917 float value;
918 setting.GetFloat(&value);
Sam Zackrisson21bfa402019-10-23 09:43:01 +0200919 config_.pre_amplifier.fixed_gain_factor = value;
saza1d600522019-10-18 13:29:43 +0200920 submodules_.pre_amplifier->SetGainFactor(value);
Alex Loikob5c9a792018-04-16 16:31:22 +0200921 }
922 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200923 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100924 case RuntimeSetting::Type::kCaptureCompressionGain: {
Per Åhgren0e3198e2019-11-18 08:52:22 +0100925 if (!submodules_.agc_manager) {
926 float value;
927 setting.GetFloat(&value);
928 int int_value = static_cast<int>(value + .5f);
929 config_.gain_controller1.compression_gain_db = int_value;
930 int error =
931 submodules_.gain_control->set_compression_gain_db(int_value);
932 RTC_DCHECK_EQ(kNoError, error);
933 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100934 break;
935 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200936 case RuntimeSetting::Type::kCaptureFixedPostGain: {
937 if (config_.gain_controller2.enabled) {
938 float value;
939 setting.GetFloat(&value);
940 config_.gain_controller2.fixed_digital.gain_db = value;
saza1d600522019-10-18 13:29:43 +0200941 submodules_.gain_controller2->ApplyConfig(config_.gain_controller2);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200942 }
943 break;
944 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200945 case RuntimeSetting::Type::kPlayoutVolumeChange: {
946 int value;
947 setting.GetInt(&value);
948 capture_.playout_volume = value;
949 break;
950 }
Alessio Bazzica7c19a702019-11-07 13:22:00 +0100951 case RuntimeSetting::Type::kPlayoutAudioDeviceChange:
952 RTC_NOTREACHED();
953 break;
Alex Loiko73ec0192018-05-15 10:52:28 +0200954 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
955 RTC_NOTREACHED();
956 break;
957 case RuntimeSetting::Type::kNotSpecified:
958 RTC_NOTREACHED();
959 break;
960 }
961 }
962}
963
964void AudioProcessingImpl::HandleRenderRuntimeSettings() {
965 RuntimeSetting setting;
966 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200967 if (aec_dump_) {
968 aec_dump_->WriteRuntimeSetting(setting);
969 }
Alex Loiko73ec0192018-05-15 10:52:28 +0200970 switch (setting.type()) {
Alessio Bazzica7c19a702019-11-07 13:22:00 +0100971 case RuntimeSetting::Type::kPlayoutAudioDeviceChange: // fall-through
Alessio Bazzica7587de42019-11-11 13:32:20 +0100972 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alex Loiko73ec0192018-05-15 10:52:28 +0200973 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
saza1d600522019-10-18 13:29:43 +0200974 if (submodules_.render_pre_processor) {
975 submodules_.render_pre_processor->SetRuntimeSetting(setting);
Alex Loiko73ec0192018-05-15 10:52:28 +0200976 }
977 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100978 case RuntimeSetting::Type::kCapturePreGain: // fall-through
979 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200980 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200981 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +0200982 RTC_NOTREACHED();
983 break;
984 }
985 }
986}
987
peah9e6a2902017-05-15 07:19:21 -0700988void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -0800989 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -0700990
saza1d600522019-10-18 13:29:43 +0200991 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +0200992 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
993 num_reverse_channels(),
994 &aecm_render_queue_buffer_);
995 RTC_DCHECK(aecm_render_signal_queue_);
996 // Insert the samples into the queue.
997 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
998 // The data queue is full and needs to be emptied.
999 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001000
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001001 // Retry the insert (should always work).
1002 bool result =
1003 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1004 RTC_DCHECK(result);
1005 }
peah764e3642016-10-22 05:04:30 -07001006 }
peah701d6282016-10-25 05:42:20 -07001007
Per Åhgren0e3198e2019-11-18 08:52:22 +01001008 if (!submodules_.agc_manager) {
Per Åhgrene35b32c2019-11-22 18:22:04 +01001009 GainControlImpl::PackRenderAudioBuffer(*audio, &agc_render_queue_buffer_);
peah701d6282016-10-25 05:42:20 -07001010 // Insert the samples into the queue.
1011 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1012 // The data queue is full and needs to be emptied.
1013 EmptyQueuedRenderAudio();
1014
1015 // Retry the insert (should always work).
1016 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1017 RTC_DCHECK(result);
1018 }
1019 }
peah9e6a2902017-05-15 07:19:21 -07001020}
ivoc9f4a4a02016-10-28 05:39:16 -07001021
peah9e6a2902017-05-15 07:19:21 -07001022void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001023 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1024
1025 // Insert the samples into the queue.
1026 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1027 // The data queue is full and needs to be emptied.
1028 EmptyQueuedRenderAudio();
1029
1030 // Retry the insert (should always work).
1031 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1032 RTC_DCHECK(result);
1033 }
peah764e3642016-10-22 05:04:30 -07001034}
1035
1036void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001037 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001038 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001039
ivoc9f4a4a02016-10-28 05:39:16 -07001040 const size_t new_red_render_queue_element_max_size =
1041 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1042
peaha0624602016-10-25 04:45:24 -07001043 // Reallocate the queues if the queue item sizes are too small to fit the
1044 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001045
1046 if (agc_render_queue_element_max_size_ <
1047 new_agc_render_queue_element_max_size) {
1048 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1049
1050 std::vector<int16_t> template_queue_element(
1051 agc_render_queue_element_max_size_);
1052
1053 agc_render_signal_queue_.reset(
1054 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1055 kMaxNumFramesToBuffer, template_queue_element,
1056 RenderQueueItemVerifier<int16_t>(
1057 agc_render_queue_element_max_size_)));
1058
1059 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1060 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1061 } else {
1062 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001063 }
ivoc9f4a4a02016-10-28 05:39:16 -07001064
1065 if (red_render_queue_element_max_size_ <
1066 new_red_render_queue_element_max_size) {
1067 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1068
1069 std::vector<float> template_queue_element(
1070 red_render_queue_element_max_size_);
1071
1072 red_render_signal_queue_.reset(
1073 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1074 kMaxNumFramesToBuffer, template_queue_element,
1075 RenderQueueItemVerifier<float>(
1076 red_render_queue_element_max_size_)));
1077
1078 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1079 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1080 } else {
1081 red_render_signal_queue_->Clear();
1082 }
peah764e3642016-10-22 05:04:30 -07001083}
1084
1085void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1086 rtc::CritScope cs_capture(&crit_capture_);
saza1d600522019-10-18 13:29:43 +02001087 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001088 RTC_DCHECK(aecm_render_signal_queue_);
1089 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001090 submodules_.echo_control_mobile->ProcessRenderAudio(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001091 aecm_capture_queue_buffer_);
1092 }
peah701d6282016-10-25 05:42:20 -07001093 }
1094
1095 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001096 submodules_.gain_control->ProcessRenderAudio(agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001097 }
ivoc9f4a4a02016-10-28 05:39:16 -07001098
1099 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001100 RTC_DCHECK(submodules_.echo_detector);
1101 submodules_.echo_detector->AnalyzeRenderAudio(red_capture_queue_buffer_);
ivoc9f4a4a02016-10-28 05:39:16 -07001102 }
peah764e3642016-10-22 05:04:30 -07001103}
1104
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001105int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001106 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001107 {
1108 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001109 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001110 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001111 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001112 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001113 }
peahfa6228e2015-11-16 16:27:42 -08001114
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001115 if (!frame) {
1116 return kNullPointerError;
1117 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001118 // Must be a native rate.
1119 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1120 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001121 frame->sample_rate_hz_ != kSampleRate32kHz &&
1122 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001123 return kBadSampleRateError;
1124 }
peah192164e2015-11-17 02:16:45 -08001125
peahdf3efa82015-11-28 12:35:15 -08001126 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001127 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001128 {
1129 // Aquire lock for the access of api_format.
1130 // The lock is released immediately due to the conditional
1131 // reinitialization.
1132 rtc::CritScope cs_capture(&crit_capture_);
1133 // TODO(ajm): The input and output rates and channels are currently
1134 // constrained to be identical in the int16 interface.
1135 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001136
1137 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001138 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001139
Oskar Sundbom4b276482019-05-23 14:28:00 +02001140 reinitialization_required =
1141 reinitialization_required ||
1142 processing_config.input_stream().sample_rate_hz() !=
1143 frame->sample_rate_hz_ ||
1144 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1145 processing_config.output_stream().sample_rate_hz() !=
1146 frame->sample_rate_hz_ ||
1147 processing_config.output_stream().num_channels() != frame->num_channels_;
1148
1149 if (reinitialization_required) {
1150 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1151 processing_config.input_stream().set_num_channels(frame->num_channels_);
1152 processing_config.output_stream().set_sample_rate_hz(
1153 frame->sample_rate_hz_);
1154 processing_config.output_stream().set_num_channels(frame->num_channels_);
1155
1156 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001157 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001158 rtc::CritScope cs_capture(&crit_capture_);
1159 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001160 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001161
peahdf3efa82015-11-28 12:35:15 -08001162 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001163 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001164 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001165 return kBadDataLengthError;
1166 }
1167
aleloi868f32f2017-05-23 07:20:05 -07001168 if (aec_dump_) {
1169 RecordUnprocessedCaptureStream(*frame);
1170 }
1171
Per Åhgrend47941e2019-08-22 11:51:13 +02001172 capture_.capture_audio->CopyFrom(frame);
Gustaf Ullberg3c918b12019-10-11 13:14:44 +02001173 if (capture_.capture_fullband_audio) {
1174 capture_.capture_fullband_audio->CopyFrom(frame);
1175 }
peahde65ddc2016-09-16 15:02:15 -07001176 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001177 if (submodule_states_.CaptureMultiBandProcessingPresent() ||
Per Åhgrena1351272019-08-15 12:15:46 +02001178 submodule_states_.CaptureFullBandProcessingActive()) {
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001179 if (capture_.capture_fullband_audio) {
1180 capture_.capture_fullband_audio->CopyTo(frame);
1181 } else {
1182 capture_.capture_audio->CopyTo(frame);
1183 }
Per Åhgrena1351272019-08-15 12:15:46 +02001184 }
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001185 if (capture_.stats.voice_detected) {
1186 frame->vad_activity_ = *capture_.stats.voice_detected
1187 ? AudioFrame::kVadActive
1188 : AudioFrame::kVadPassive;
1189 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001190
aleloi868f32f2017-05-23 07:20:05 -07001191 if (aec_dump_) {
1192 RecordProcessedCaptureStream(*frame);
1193 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001194
1195 return kNoError;
1196}
1197
peahde65ddc2016-09-16 15:02:15 -07001198int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001199 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001200
peahb58a1582016-03-15 09:34:24 -07001201 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001202 // TODO(peah): Simplify once the public API Enable functions for these
1203 // are moved to APM.
saza1d600522019-10-18 13:29:43 +02001204 RTC_DCHECK_LE(!!submodules_.echo_controller +
saza1d600522019-10-18 13:29:43 +02001205 !!submodules_.echo_control_mobile,
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001206 1);
peahb58a1582016-03-15 09:34:24 -07001207
peahde65ddc2016-09-16 15:02:15 -07001208 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001209
Per Åhgrenc0424252019-12-10 13:04:15 +01001210 if (submodules_.high_pass_filter &&
1211 config_.high_pass_filter.apply_in_full_band &&
1212 !constants_.enforce_split_band_hpf) {
1213 submodules_.high_pass_filter->Process(capture_buffer,
1214 /*use_split_band_data=*/false);
1215 }
1216
saza1d600522019-10-18 13:29:43 +02001217 if (submodules_.pre_amplifier) {
1218 submodules_.pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001219 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001220 capture_buffer->num_frames()));
1221 }
1222
Per Åhgren928146f2019-08-20 09:19:21 +02001223 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001224 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001225 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001226 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1227 if (log_rms) {
1228 capture_rms_interval_counter_ = 0;
1229 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001230 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1231 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1232 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1233 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001234 }
1235
saza1d600522019-10-18 13:29:43 +02001236 if (submodules_.echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001237 // Detect and flag any change in the analog gain.
Per Åhgren0e3198e2019-11-18 08:52:22 +01001238 int analog_mic_level = recommended_stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001239 capture_.echo_path_gain_change =
1240 capture_.prev_analog_mic_level != analog_mic_level &&
1241 capture_.prev_analog_mic_level != -1;
1242 capture_.prev_analog_mic_level = analog_mic_level;
1243
Per Åhgrend2650d12018-10-02 17:00:59 +02001244 // Detect and flag any change in the pre-amplifier gain.
saza1d600522019-10-18 13:29:43 +02001245 if (submodules_.pre_amplifier) {
1246 float pre_amp_gain = submodules_.pre_amplifier->GetGainFactor();
Per Åhgrend2650d12018-10-02 17:00:59 +02001247 capture_.echo_path_gain_change =
1248 capture_.echo_path_gain_change ||
1249 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001250 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001251 capture_.prev_pre_amp_gain = pre_amp_gain;
1252 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001253
1254 // Detect volume change.
1255 capture_.echo_path_gain_change =
1256 capture_.echo_path_gain_change ||
1257 (capture_.prev_playout_volume != capture_.playout_volume &&
1258 capture_.prev_playout_volume >= 0);
1259 capture_.prev_playout_volume = capture_.playout_volume;
1260
saza1d600522019-10-18 13:29:43 +02001261 submodules_.echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001262 }
1263
Per Åhgren3daedb62019-11-22 12:11:40 +01001264 if (constants_.use_experimental_agc &&
1265 submodules_.gain_control->is_enabled()) {
1266 submodules_.agc_manager->AnalyzePreProcess(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001267 }
1268
peah2ace3f92016-09-10 04:42:27 -07001269 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1270 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001271 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1272 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001273 }
1274
Per Åhgrene14cb992019-11-27 09:34:22 +01001275 const bool multi_channel_capture = config_.pipeline.multi_channel_capture &&
1276 constants_.multi_channel_capture_support;
1277 if (submodules_.echo_controller && !multi_channel_capture) {
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
Per Åhgrenc0424252019-12-10 13:04:15 +01001285 if (submodules_.high_pass_filter &&
1286 (!config_.high_pass_filter.apply_in_full_band ||
1287 constants_.enforce_split_band_hpf)) {
1288 submodules_.high_pass_filter->Process(capture_buffer,
1289 /*use_split_band_data=*/true);
peah8271d042016-11-22 07:24:52 -08001290 }
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001291
Per Åhgrene35b32c2019-11-22 18:22:04 +01001292 RETURN_ON_ERR(submodules_.gain_control->AnalyzeCaptureAudio(*capture_buffer));
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001293 RTC_DCHECK(
1294 !(submodules_.legacy_noise_suppressor && submodules_.noise_suppressor));
saza1d600522019-10-18 13:29:43 +02001295 if (submodules_.noise_suppressor) {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001296 submodules_.noise_suppressor->Analyze(*capture_buffer);
1297 } else if (submodules_.legacy_noise_suppressor) {
1298 submodules_.legacy_noise_suppressor->AnalyzeCaptureAudio(capture_buffer);
saza0bad15f2019-10-16 11:46:11 +02001299 }
peahb58a1582016-03-15 09:34:24 -07001300
saza1d600522019-10-18 13:29:43 +02001301 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001302 // Ensure that the stream delay was set before the call to the
1303 // AECM ProcessCaptureAudio function.
1304 if (!was_stream_delay_set()) {
1305 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001306 }
1307
saza1d600522019-10-18 13:29:43 +02001308 if (submodules_.noise_suppressor) {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001309 submodules_.noise_suppressor->Process(capture_buffer);
1310 } else if (submodules_.legacy_noise_suppressor) {
saza1d600522019-10-18 13:29:43 +02001311 submodules_.echo_control_mobile->CopyLowPassReference(capture_buffer);
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001312 submodules_.legacy_noise_suppressor->ProcessCaptureAudio(capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001313 }
peahe0eae3c2016-12-14 01:16:23 -08001314
saza1d600522019-10-18 13:29:43 +02001315 RETURN_ON_ERR(submodules_.echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001316 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001317 } else {
saza1d600522019-10-18 13:29:43 +02001318 if (submodules_.echo_controller) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001319 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1320
1321 if (was_stream_delay_set()) {
saza1d600522019-10-18 13:29:43 +02001322 submodules_.echo_controller->SetAudioBufferDelay(stream_delay_ms());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001323 }
1324
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001325 AudioBuffer* linear_aec_buffer = capture_.linear_aec_output.get();
saza1d600522019-10-18 13:29:43 +02001326 submodules_.echo_controller->ProcessCapture(
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001327 capture_buffer, linear_aec_buffer, capture_.echo_path_gain_change);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001328 }
1329
saza1d600522019-10-18 13:29:43 +02001330 if (submodules_.noise_suppressor) {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001331 submodules_.noise_suppressor->Process(capture_buffer);
1332 } else if (submodules_.legacy_noise_suppressor) {
1333 submodules_.legacy_noise_suppressor->ProcessCaptureAudio(capture_buffer);
saza0bad15f2019-10-16 11:46:11 +02001334 }
Per Åhgren46537a32017-06-07 10:08:10 +02001335 }
ivoc9f4a4a02016-10-28 05:39:16 -07001336
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001337 if (config_.voice_detection.enabled) {
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001338 capture_.stats.voice_detected =
saza1d600522019-10-18 13:29:43 +02001339 submodules_.voice_detector->ProcessCaptureAudio(capture_buffer);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001340 } else {
1341 capture_.stats.voice_detected = absl::nullopt;
1342 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001343
Per Åhgren3daedb62019-11-22 12:11:40 +01001344 if (constants_.use_experimental_agc &&
1345 submodules_.gain_control->is_enabled()) {
1346 submodules_.agc_manager->Process(capture_buffer);
1347
1348 absl::optional<int> new_digital_gain =
1349 submodules_.agc_manager->GetDigitalComressionGain();
1350 if (new_digital_gain) {
1351 submodules_.gain_control->set_compression_gain_db(*new_digital_gain);
1352 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001353 }
Per Åhgren200feba2019-03-06 04:16:46 +01001354 // TODO(peah): Add reporting from AEC3 whether there is echo.
saza1d600522019-10-18 13:29:43 +02001355 RETURN_ON_ERR(submodules_.gain_control->ProcessCaptureAudio(
Per Åhgren62ea0aa2019-12-09 10:18:44 +01001356 capture_buffer, /*stream_has_echo*/ false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001357
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001358 if (submodule_states_.CaptureMultiBandProcessingPresent() &&
peah2ace3f92016-09-10 04:42:27 -07001359 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001360 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1361 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001362 }
1363
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001364 if (capture_.capture_fullband_audio) {
saza1d600522019-10-18 13:29:43 +02001365 const auto& ec = submodules_.echo_controller;
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001366 bool ec_active = ec ? ec->ActiveProcessing() : false;
1367 // Only update the fullband buffer if the multiband processing has changed
1368 // the signal. Keep the original signal otherwise.
1369 if (submodule_states_.CaptureMultiBandProcessingActive(ec_active)) {
1370 capture_buffer->CopyTo(capture_.capture_fullband_audio.get());
1371 }
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001372 capture_buffer = capture_.capture_fullband_audio.get();
1373 }
1374
peah9e6a2902017-05-15 07:19:21 -07001375 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001376 RTC_DCHECK(submodules_.echo_detector);
1377 submodules_.echo_detector->AnalyzeCaptureAudio(rtc::ArrayView<const float>(
1378 capture_buffer->channels()[0], capture_buffer->num_frames()));
peah9e6a2902017-05-15 07:19:21 -07001379 }
1380
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001381 // TODO(aluebs): Investigate if the transient suppression placement should be
1382 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001383 if (capture_.transient_suppressor_enabled) {
saza1d600522019-10-18 13:29:43 +02001384 float voice_probability = submodules_.agc_manager.get()
1385 ? submodules_.agc_manager->voice_probability()
1386 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001387
saza1d600522019-10-18 13:29:43 +02001388 submodules_.transient_suppressor->Suppress(
Per Åhgrend47941e2019-08-22 11:51:13 +02001389 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001390 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001391 capture_buffer->split_bands_const(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001392 capture_buffer->num_frames_per_band(),
1393 capture_.keyboard_info.keyboard_data,
1394 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001395 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001396 }
1397
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001398 // Experimental APM sub-module that analyzes |capture_buffer|.
saza1d600522019-10-18 13:29:43 +02001399 if (submodules_.capture_analyzer) {
1400 submodules_.capture_analyzer->Analyze(capture_buffer);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001401 }
1402
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001403 if (config_.gain_controller2.enabled) {
saza1d600522019-10-18 13:29:43 +02001404 submodules_.gain_controller2->NotifyAnalogLevel(
Per Åhgren0e3198e2019-11-18 08:52:22 +01001405 recommended_stream_analog_level());
saza1d600522019-10-18 13:29:43 +02001406 submodules_.gain_controller2->Process(capture_buffer);
alessiob3ec96df2017-05-22 06:57:06 -07001407 }
1408
saza1d600522019-10-18 13:29:43 +02001409 if (submodules_.capture_post_processor) {
1410 submodules_.capture_post_processor->Process(capture_buffer);
Sam Zackrisson0beac582017-09-25 12:04:02 +02001411 }
1412
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001413 // The level estimator operates on the recombined data.
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001414 if (config_.level_estimation.enabled) {
saza1d600522019-10-18 13:29:43 +02001415 submodules_.output_level_estimator->ProcessStream(*capture_buffer);
1416 capture_.stats.output_rms_dbfs = submodules_.output_level_estimator->RMS();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001417 } else {
1418 capture_.stats.output_rms_dbfs = absl::nullopt;
1419 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001420
Per Åhgren928146f2019-08-20 09:19:21 +02001421 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001422 capture_buffer->channels_const()[0],
peah1b08dc32016-12-20 13:45:58 -08001423 capture_nonlocked_.capture_processing_format.num_frames()));
1424 if (log_rms) {
1425 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1426 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1427 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1428 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1429 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1430 }
1431
Per Åhgren0e3198e2019-11-18 08:52:22 +01001432 if (submodules_.agc_manager) {
1433 int level = recommended_stream_analog_level();
1434 data_dumper_->DumpRaw("experimental_gain_control_stream_analog_level", 1,
1435 &level);
1436 }
1437
peahdf3efa82015-11-28 12:35:15 -08001438 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001439 return kNoError;
1440}
1441
Gustaf Ullberg8c51f2e2019-10-22 15:21:31 +02001442int AudioProcessingImpl::AnalyzeReverseStream(
1443 const float* const* data,
1444 const StreamConfig& reverse_config) {
1445 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_StreamConfig");
1446 rtc::CritScope cs(&crit_render_);
1447 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
1448}
1449
peahde65ddc2016-09-16 15:02:15 -07001450int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1451 const StreamConfig& input_config,
1452 const StreamConfig& output_config,
1453 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001454 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001455 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001456 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001457 if (submodule_states_.RenderMultiBandProcessingActive() ||
1458 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001459 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1460 dest);
peah2ace3f92016-09-10 04:42:27 -07001461 } else if (formats_.api_format.reverse_input_stream() !=
1462 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001463 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1464 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001465 } else {
peahde65ddc2016-09-16 15:02:15 -07001466 CopyAudioIfNeeded(src, input_config.num_frames(),
1467 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001468 }
1469
1470 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001471}
1472
peahdf3efa82015-11-28 12:35:15 -08001473int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001474 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001475 const StreamConfig& input_config,
1476 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001477 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001478 return kNullPointerError;
1479 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001480
peahde65ddc2016-09-16 15:02:15 -07001481 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001482 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001483 }
1484
peahdf3efa82015-11-28 12:35:15 -08001485 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001486 processing_config.reverse_input_stream() = input_config;
1487 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001488
peahdf3efa82015-11-28 12:35:15 -08001489 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001490 RTC_DCHECK_EQ(input_config.num_frames(),
1491 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001492
aleloi868f32f2017-05-23 07:20:05 -07001493 if (aec_dump_) {
1494 const size_t channel_size =
1495 formats_.api_format.reverse_input_stream().num_frames();
1496 const size_t num_channels =
1497 formats_.api_format.reverse_input_stream().num_channels();
1498 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001499 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001500 }
peahdf3efa82015-11-28 12:35:15 -08001501 render_.render_audio->CopyFrom(src,
1502 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001503 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001504}
1505
1506int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001507 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001508 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001509 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001510 return kNullPointerError;
1511 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001512 // Must be a native rate.
1513 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1514 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001515 frame->sample_rate_hz_ != kSampleRate32kHz &&
1516 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001517 return kBadSampleRateError;
1518 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001519
Michael Graczyk86c6d332015-07-23 11:41:39 -07001520 if (frame->num_channels_ <= 0) {
1521 return kBadNumberChannelsError;
1522 }
1523
peahdf3efa82015-11-28 12:35:15 -08001524 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001525 processing_config.reverse_input_stream().set_sample_rate_hz(
1526 frame->sample_rate_hz_);
1527 processing_config.reverse_input_stream().set_num_channels(
1528 frame->num_channels_);
1529 processing_config.reverse_output_stream().set_sample_rate_hz(
1530 frame->sample_rate_hz_);
1531 processing_config.reverse_output_stream().set_num_channels(
1532 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001533
peahdf3efa82015-11-28 12:35:15 -08001534 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001535 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001536 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001537 return kBadDataLengthError;
1538 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001539
aleloi868f32f2017-05-23 07:20:05 -07001540 if (aec_dump_) {
1541 aec_dump_->WriteRenderStreamMessage(*frame);
1542 }
1543
Per Åhgrend47941e2019-08-22 11:51:13 +02001544 render_.render_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001545 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001546 if (submodule_states_.RenderMultiBandProcessingActive() ||
1547 submodule_states_.RenderFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001548 render_.render_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001549 }
aluebsb0319552016-03-17 20:39:53 -07001550 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001551}
niklase@google.com470e71d2011-07-07 08:21:25 +00001552
peahde65ddc2016-09-16 15:02:15 -07001553int AudioProcessingImpl::ProcessRenderStreamLocked() {
1554 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001555
Alex Loiko73ec0192018-05-15 10:52:28 +02001556 HandleRenderRuntimeSettings();
1557
saza1d600522019-10-18 13:29:43 +02001558 if (submodules_.render_pre_processor) {
1559 submodules_.render_pre_processor->Process(render_buffer);
Alex Loiko5825aa62017-12-18 16:02:40 +01001560 }
1561
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001562 QueueNonbandedRenderAudio(render_buffer);
1563
peah2ace3f92016-09-10 04:42:27 -07001564 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001565 SampleRateSupportsMultiBand(
1566 formats_.render_processing_format.sample_rate_hz())) {
1567 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001568 }
1569
peahce4d9152017-05-19 01:28:05 -07001570 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1571 QueueBandedRenderAudio(render_buffer);
1572 }
1573
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001574 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
saza1d600522019-10-18 13:29:43 +02001575 if (submodules_.echo_controller) {
1576 submodules_.echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001577 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001578
peah2ace3f92016-09-10 04:42:27 -07001579 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001580 SampleRateSupportsMultiBand(
1581 formats_.render_processing_format.sample_rate_hz())) {
1582 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001583 }
1584
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001585 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001586}
1587
1588int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001589 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001590 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001591 capture_.was_stream_delay_set = true;
1592 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001593
niklase@google.com470e71d2011-07-07 08:21:25 +00001594 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001595 delay = 0;
1596 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001597 }
1598
1599 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1600 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001601 delay = 500;
1602 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001603 }
1604
peahdf3efa82015-11-28 12:35:15 -08001605 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001606 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001607}
1608
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001609bool AudioProcessingImpl::GetLinearAecOutput(
1610 rtc::ArrayView<std::array<float, 160>> linear_output) const {
1611 rtc::CritScope cs(&crit_capture_);
1612 AudioBuffer* linear_aec_buffer = capture_.linear_aec_output.get();
1613
1614 RTC_DCHECK(linear_aec_buffer);
1615 if (linear_aec_buffer) {
1616 RTC_DCHECK_EQ(1, linear_aec_buffer->num_bands());
1617 RTC_DCHECK_EQ(linear_output.size(), linear_aec_buffer->num_channels());
1618
1619 for (size_t ch = 0; ch < linear_aec_buffer->num_channels(); ++ch) {
1620 RTC_DCHECK_EQ(linear_output[ch].size(), linear_aec_buffer->num_frames());
1621 rtc::ArrayView<const float> channel_view =
1622 rtc::ArrayView<const float>(linear_aec_buffer->channels_const()[ch],
1623 linear_aec_buffer->num_frames());
1624 std::copy(channel_view.begin(), channel_view.end(),
1625 linear_output[ch].begin());
1626 }
1627 return true;
1628 }
1629 RTC_LOG(LS_ERROR) << "No linear AEC output available";
1630 RTC_NOTREACHED();
1631 return false;
1632}
1633
niklase@google.com470e71d2011-07-07 08:21:25 +00001634int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001635 // Used as callback from submodules, hence locking is not allowed.
1636 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001637}
1638
1639bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001640 // Used as callback from submodules, hence locking is not allowed.
1641 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001642}
1643
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001644void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001645 rtc::CritScope cs(&crit_capture_);
1646 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001647}
1648
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001649void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001650 rtc::CritScope cs(&crit_capture_);
1651 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001652}
1653
1654int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001655 rtc::CritScope cs(&crit_capture_);
1656 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001657}
1658
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001659void AudioProcessingImpl::set_stream_analog_level(int level) {
1660 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgren0e3198e2019-11-18 08:52:22 +01001661
1662 if (submodules_.agc_manager) {
1663 submodules_.agc_manager->set_stream_analog_level(level);
1664 data_dumper_->DumpRaw("experimental_gain_control_set_stream_analog_level",
1665 1, &level);
1666 } else {
1667 int error = submodules_.gain_control->set_stream_analog_level(level);
1668 RTC_DCHECK_EQ(kNoError, error);
1669 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001670}
1671
1672int AudioProcessingImpl::recommended_stream_analog_level() const {
1673 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgren0e3198e2019-11-18 08:52:22 +01001674 if (submodules_.agc_manager) {
1675 return submodules_.agc_manager->stream_analog_level();
1676 }
1677 return submodules_.gain_control->stream_analog_level();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001678}
1679
aleloi868f32f2017-05-23 07:20:05 -07001680void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1681 RTC_DCHECK(aec_dump);
1682 rtc::CritScope cs_render(&crit_render_);
1683 rtc::CritScope cs_capture(&crit_capture_);
1684
1685 // The previously attached AecDump will be destroyed with the
1686 // 'aec_dump' parameter, which is after locks are released.
1687 aec_dump_.swap(aec_dump);
1688 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001689 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001690}
1691
1692void AudioProcessingImpl::DetachAecDump() {
1693 // The d-tor of a task-queue based AecDump blocks until all pending
1694 // tasks are done. This construction avoids blocking while holding
1695 // the render and capture locks.
1696 std::unique_ptr<AecDump> aec_dump = nullptr;
1697 {
1698 rtc::CritScope cs_render(&crit_render_);
1699 rtc::CritScope cs_capture(&crit_capture_);
1700 aec_dump = std::move(aec_dump_);
1701 }
1702}
1703
Sam Zackrisson4d364492018-03-02 16:03:21 +01001704void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1705 std::unique_ptr<AudioGenerator> audio_generator) {
1706 // TODO(bugs.webrtc.org/8882) Stub.
1707 // Reset internal audio generator with audio_generator.
1708}
1709
1710void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1711 // TODO(bugs.webrtc.org/8882) Stub.
1712 // Delete audio generator, if one is attached.
1713}
1714
Ivo Creusen56d46092017-11-24 17:29:59 +01001715AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001716 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001717 rtc::CritScope cs_capture(&crit_capture_);
1718 if (!has_remote_tracks) {
1719 return capture_.stats;
1720 }
1721 AudioProcessingStats stats = capture_.stats;
saza1d600522019-10-18 13:29:43 +02001722 if (submodules_.echo_controller) {
1723 auto ec_metrics = submodules_.echo_controller->GetMetrics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001724 stats.echo_return_loss = ec_metrics.echo_return_loss;
1725 stats.echo_return_loss_enhancement =
1726 ec_metrics.echo_return_loss_enhancement;
1727 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001728 }
1729 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001730 RTC_DCHECK(submodules_.echo_detector);
1731 auto ed_metrics = submodules_.echo_detector->GetMetrics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001732 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1733 stats.residual_echo_likelihood_recent_max =
1734 ed_metrics.echo_likelihood_recent_max;
1735 }
Ivo Creusenae026092017-11-20 13:07:16 +01001736 return stats;
1737}
1738
peah8271d042016-11-22 07:24:52 -08001739void AudioProcessingImpl::MutateConfig(
1740 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1741 rtc::CritScope cs_render(&crit_render_);
1742 rtc::CritScope cs_capture(&crit_capture_);
1743 mutator(&config_);
1744 ApplyConfig(config_);
1745}
1746
1747AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1748 rtc::CritScope cs_render(&crit_render_);
1749 rtc::CritScope cs_capture(&crit_capture_);
1750 return config_;
1751}
1752
peah2ace3f92016-09-10 04:42:27 -07001753bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1754 return submodule_states_.Update(
Per Åhgren62ea0aa2019-12-09 10:18:44 +01001755 config_.high_pass_filter.enabled, !!submodules_.echo_control_mobile,
1756 config_.residual_echo_detector.enabled,
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001757 !!submodules_.legacy_noise_suppressor || !!submodules_.noise_suppressor,
1758 submodules_.gain_control->is_enabled(), config_.gain_controller2.enabled,
1759 config_.pre_amplifier.enabled, capture_nonlocked_.echo_controller_enabled,
saza0bad15f2019-10-16 11:46:11 +02001760 config_.voice_detection.enabled, capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001761}
1762
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001763void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001764 if (capture_.transient_suppressor_enabled) {
saza1d600522019-10-18 13:29:43 +02001765 if (!submodules_.transient_suppressor.get()) {
1766 submodules_.transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001767 }
saza1d600522019-10-18 13:29:43 +02001768 submodules_.transient_suppressor->Initialize(proc_fullband_sample_rate_hz(),
1769 capture_nonlocked_.split_rate,
1770 num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001771 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001772}
1773
Per Åhgren4011de02019-12-03 11:48:48 +00001774void AudioProcessingImpl::InitializeHighPassFilter() {
Per Åhgrenb8106462019-12-04 08:34:12 +01001775 bool high_pass_filter_needed_by_aec =
1776 config_.echo_canceller.enabled &&
1777 config_.echo_canceller.enforce_high_pass_filtering &&
1778 !config_.echo_canceller.mobile_mode;
1779 if (submodule_states_.HighPassFilteringRequired() ||
1780 high_pass_filter_needed_by_aec) {
Per Åhgrenc0424252019-12-10 13:04:15 +01001781 bool use_full_band = config_.high_pass_filter.apply_in_full_band &&
1782 !constants_.enforce_split_band_hpf;
1783 int rate = use_full_band ? proc_fullband_sample_rate_hz()
1784 : proc_split_sample_rate_hz();
1785 size_t num_channels =
1786 use_full_band ? num_output_channels() : num_proc_channels();
1787
1788 submodules_.high_pass_filter.reset(new HighPassFilter(rate, num_channels));
peah8271d042016-11-22 07:24:52 -08001789 } else {
saza1d600522019-10-18 13:29:43 +02001790 submodules_.high_pass_filter.reset();
peah8271d042016-11-22 07:24:52 -08001791 }
1792}
alessiob3ec96df2017-05-22 06:57:06 -07001793
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001794void AudioProcessingImpl::InitializeVoiceDetector() {
1795 if (config_.voice_detection.enabled) {
saza1d600522019-10-18 13:29:43 +02001796 submodules_.voice_detector = std::make_unique<VoiceDetection>(
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001797 proc_split_sample_rate_hz(), VoiceDetection::kVeryLowLikelihood);
1798 } else {
saza1d600522019-10-18 13:29:43 +02001799 submodules_.voice_detector.reset();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001800 }
1801}
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001802void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001803 bool use_echo_controller =
1804 echo_control_factory_ ||
Per Åhgren62ea0aa2019-12-09 10:18:44 +01001805 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001806
1807 if (use_echo_controller) {
1808 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001809 if (echo_control_factory_) {
Per Åhgren4e5c7092019-11-01 20:44:11 +01001810 submodules_.echo_controller = echo_control_factory_->Create(
1811 proc_sample_rate_hz(), num_reverse_channels(), num_proc_channels());
Gustaf Ullberg2c6f3732019-11-07 17:15:12 +01001812 RTC_DCHECK(submodules_.echo_controller);
Per Åhgren200feba2019-03-06 04:16:46 +01001813 } else {
Per Åhgrenb2b58d82019-12-02 14:59:40 +01001814 EchoCanceller3Config config =
1815 use_setup_specific_default_aec3_config_
1816 ? EchoCanceller3::CreateDefaultConfig(num_reverse_channels(),
1817 num_proc_channels())
1818 : EchoCanceller3Config();
saza1d600522019-10-18 13:29:43 +02001819 submodules_.echo_controller = std::make_unique<EchoCanceller3>(
Per Åhgrenb2b58d82019-12-02 14:59:40 +01001820 config, proc_sample_rate_hz(), num_reverse_channels(),
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001821 num_proc_channels());
Per Åhgren200feba2019-03-06 04:16:46 +01001822 }
1823
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001824 // Setup the storage for returning the linear AEC output.
1825 if (config_.echo_canceller.export_linear_aec_output) {
1826 constexpr int kLinearOutputRateHz = 16000;
1827 capture_.linear_aec_output = std::make_unique<AudioBuffer>(
1828 kLinearOutputRateHz, num_proc_channels(), kLinearOutputRateHz,
1829 num_proc_channels(), kLinearOutputRateHz, num_proc_channels());
1830 } else {
1831 capture_.linear_aec_output.reset();
1832 }
1833
Per Åhgren200feba2019-03-06 04:16:46 +01001834 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001835
saza1d600522019-10-18 13:29:43 +02001836 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001837 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001838 return;
peahe0eae3c2016-12-14 01:16:23 -08001839 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001840
saza1d600522019-10-18 13:29:43 +02001841 submodules_.echo_controller.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001842 capture_nonlocked_.echo_controller_enabled = false;
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001843 capture_.linear_aec_output.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001844
1845 if (!config_.echo_canceller.enabled) {
saza1d600522019-10-18 13:29:43 +02001846 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001847 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001848 return;
1849 }
1850
1851 if (config_.echo_canceller.mobile_mode) {
1852 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001853 size_t max_element_size =
1854 std::max(static_cast<size_t>(1),
1855 kMaxAllowedValuesOfSamplesPerBand *
1856 EchoControlMobileImpl::NumCancellersRequired(
1857 num_output_channels(), num_reverse_channels()));
1858
1859 std::vector<int16_t> template_queue_element(max_element_size);
1860
1861 aecm_render_signal_queue_.reset(
1862 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1863 kMaxNumFramesToBuffer, template_queue_element,
1864 RenderQueueItemVerifier<int16_t>(max_element_size)));
1865
1866 aecm_render_queue_buffer_.resize(max_element_size);
1867 aecm_capture_queue_buffer_.resize(max_element_size);
1868
saza1d600522019-10-18 13:29:43 +02001869 submodules_.echo_control_mobile.reset(new EchoControlMobileImpl());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001870
saza1d600522019-10-18 13:29:43 +02001871 submodules_.echo_control_mobile->Initialize(proc_split_sample_rate_hz(),
1872 num_reverse_channels(),
1873 num_output_channels());
Per Åhgrenf204faf2019-04-25 15:18:06 +02001874 return;
1875 }
1876
saza1d600522019-10-18 13:29:43 +02001877 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001878 aecm_render_signal_queue_.reset();
peahe0eae3c2016-12-14 01:16:23 -08001879}
peah8271d042016-11-22 07:24:52 -08001880
alessiob3ec96df2017-05-22 06:57:06 -07001881void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001882 if (config_.gain_controller2.enabled) {
saza1d600522019-10-18 13:29:43 +02001883 submodules_.gain_controller2->Initialize(proc_fullband_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001884 }
1885}
1886
saza0bad15f2019-10-16 11:46:11 +02001887void AudioProcessingImpl::InitializeNoiseSuppressor() {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001888 submodules_.legacy_noise_suppressor.reset();
1889 submodules_.noise_suppressor.reset();
1890
saza0bad15f2019-10-16 11:46:11 +02001891 if (config_.noise_suppression.enabled) {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001892 const bool use_legacy_ns =
1893 config_.noise_suppression.use_legacy_ns || enforced_usage_of_legacy_ns_;
1894
1895 if (!use_legacy_ns) {
1896 auto map_level =
1897 [](AudioProcessing::Config::NoiseSuppression::Level level) {
1898 using NoiseSuppresionConfig =
1899 AudioProcessing::Config::NoiseSuppression;
1900 switch (level) {
1901 case NoiseSuppresionConfig::kLow:
1902 return NsConfig::SuppressionLevel::k6dB;
1903 case NoiseSuppresionConfig::kModerate:
1904 return NsConfig::SuppressionLevel::k12dB;
1905 case NoiseSuppresionConfig::kHigh:
1906 return NsConfig::SuppressionLevel::k18dB;
1907 case NoiseSuppresionConfig::kVeryHigh:
1908 return NsConfig::SuppressionLevel::k21dB;
1909 default:
1910 RTC_NOTREACHED();
1911 }
1912 };
1913
1914 NsConfig cfg;
1915 cfg.target_level = map_level(config_.noise_suppression.level);
1916 submodules_.noise_suppressor = std::make_unique<NoiseSuppressor>(
1917 cfg, proc_sample_rate_hz(), num_proc_channels());
1918 } else {
1919 auto ns_level =
1920 NsConfigLevelToInterfaceLevel(config_.noise_suppression.level);
1921 submodules_.legacy_noise_suppressor = std::make_unique<NoiseSuppression>(
1922 num_proc_channels(), proc_sample_rate_hz(), ns_level);
1923 }
saza0bad15f2019-10-16 11:46:11 +02001924 }
1925}
1926
Alex Loikob5c9a792018-04-16 16:31:22 +02001927void AudioProcessingImpl::InitializePreAmplifier() {
1928 if (config_.pre_amplifier.enabled) {
saza1d600522019-10-18 13:29:43 +02001929 submodules_.pre_amplifier.reset(
Alex Loikob5c9a792018-04-16 16:31:22 +02001930 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1931 } else {
saza1d600522019-10-18 13:29:43 +02001932 submodules_.pre_amplifier.reset();
Alex Loikob5c9a792018-04-16 16:31:22 +02001933 }
1934}
1935
ivoc9f4a4a02016-10-28 05:39:16 -07001936void AudioProcessingImpl::InitializeResidualEchoDetector() {
saza1d600522019-10-18 13:29:43 +02001937 RTC_DCHECK(submodules_.echo_detector);
1938 submodules_.echo_detector->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001939 proc_fullband_sample_rate_hz(), 1,
Ivo Creusenb1facc12018-04-12 16:15:58 +02001940 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001941}
1942
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001943void AudioProcessingImpl::InitializeAnalyzer() {
saza1d600522019-10-18 13:29:43 +02001944 if (submodules_.capture_analyzer) {
1945 submodules_.capture_analyzer->Initialize(proc_fullband_sample_rate_hz(),
1946 num_proc_channels());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001947 }
1948}
1949
Sam Zackrisson0beac582017-09-25 12:04:02 +02001950void AudioProcessingImpl::InitializePostProcessor() {
saza1d600522019-10-18 13:29:43 +02001951 if (submodules_.capture_post_processor) {
1952 submodules_.capture_post_processor->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001953 proc_fullband_sample_rate_hz(), num_proc_channels());
Sam Zackrisson0beac582017-09-25 12:04:02 +02001954 }
1955}
1956
Alex Loiko5825aa62017-12-18 16:02:40 +01001957void AudioProcessingImpl::InitializePreProcessor() {
saza1d600522019-10-18 13:29:43 +02001958 if (submodules_.render_pre_processor) {
1959 submodules_.render_pre_processor->Initialize(
Alex Loiko5825aa62017-12-18 16:02:40 +01001960 formats_.render_processing_format.sample_rate_hz(),
1961 formats_.render_processing_format.num_channels());
1962 }
1963}
1964
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001965void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001966
aleloi868f32f2017-05-23 07:20:05 -07001967void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1968 if (!aec_dump_) {
1969 return;
1970 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001971
1972 std::string experiments_description = "";
aleloi868f32f2017-05-23 07:20:05 -07001973 // TODO(peah): Add semicolon-separated concatenations of experiment
1974 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001975 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1976 experiments_description += "AgcClippingLevelExperiment;";
1977 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001978 if (capture_nonlocked_.echo_controller_enabled) {
1979 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001980 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001981 if (config_.gain_controller2.enabled) {
1982 experiments_description += "GainController2;";
1983 }
aleloi868f32f2017-05-23 07:20:05 -07001984
1985 InternalAPMConfig apm_config;
1986
Per Åhgren200feba2019-03-06 04:16:46 +01001987 apm_config.aec_enabled = config_.echo_canceller.enabled;
Per Åhgren62ea0aa2019-12-09 10:18:44 +01001988 apm_config.aec_delay_agnostic_enabled = false;
1989 apm_config.aec_extended_filter_enabled = false;
1990 apm_config.aec_suppression_level = 0;
aleloi868f32f2017-05-23 07:20:05 -07001991
saza1d600522019-10-18 13:29:43 +02001992 apm_config.aecm_enabled = !!submodules_.echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07001993 apm_config.aecm_comfort_noise_enabled =
saza1d600522019-10-18 13:29:43 +02001994 submodules_.echo_control_mobile &&
1995 submodules_.echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001996 apm_config.aecm_routing_mode =
saza1d600522019-10-18 13:29:43 +02001997 submodules_.echo_control_mobile
1998 ? static_cast<int>(submodules_.echo_control_mobile->routing_mode())
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001999 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002000
saza1d600522019-10-18 13:29:43 +02002001 apm_config.agc_enabled = submodules_.gain_control->is_enabled();
2002 apm_config.agc_mode = static_cast<int>(submodules_.gain_control->mode());
aleloi868f32f2017-05-23 07:20:05 -07002003 apm_config.agc_limiter_enabled =
saza1d600522019-10-18 13:29:43 +02002004 submodules_.gain_control->is_limiter_enabled();
Per Åhgren0e3198e2019-11-18 08:52:22 +01002005 apm_config.noise_robust_agc_enabled = !!submodules_.agc_manager;
aleloi868f32f2017-05-23 07:20:05 -07002006
2007 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2008
saza0bad15f2019-10-16 11:46:11 +02002009 apm_config.ns_enabled = config_.noise_suppression.enabled;
2010 apm_config.ns_level = static_cast<int>(config_.noise_suppression.level);
aleloi868f32f2017-05-23 07:20:05 -07002011
2012 apm_config.transient_suppression_enabled =
2013 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002014 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002015 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2016 apm_config.pre_amplifier_fixed_gain_factor =
2017 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002018
2019 if (!forced && apm_config == apm_config_for_aec_dump_) {
2020 return;
2021 }
2022 aec_dump_->WriteConfig(apm_config);
2023 apm_config_for_aec_dump_ = apm_config;
2024}
2025
2026void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2027 const float* const* src) {
2028 RTC_DCHECK(aec_dump_);
2029 WriteAecDumpConfigMessage(false);
2030
2031 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2032 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2033 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002034 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002035 RecordAudioProcessingState();
2036}
2037
2038void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2039 const AudioFrame& capture_frame) {
2040 RTC_DCHECK(aec_dump_);
2041 WriteAecDumpConfigMessage(false);
2042
2043 aec_dump_->AddCaptureStreamInput(capture_frame);
2044 RecordAudioProcessingState();
2045}
2046
2047void AudioProcessingImpl::RecordProcessedCaptureStream(
2048 const float* const* processed_capture_stream) {
2049 RTC_DCHECK(aec_dump_);
2050
2051 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2052 const size_t num_channels =
2053 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002054 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2055 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002056 aec_dump_->WriteCaptureStreamMessage();
2057}
2058
2059void AudioProcessingImpl::RecordProcessedCaptureStream(
2060 const AudioFrame& processed_capture_frame) {
2061 RTC_DCHECK(aec_dump_);
2062
2063 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2064 aec_dump_->WriteCaptureStreamMessage();
2065}
2066
2067void AudioProcessingImpl::RecordAudioProcessingState() {
2068 RTC_DCHECK(aec_dump_);
2069 AecDump::AudioProcessingState audio_proc_state;
2070 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
Per Åhgren62ea0aa2019-12-09 10:18:44 +01002071 audio_proc_state.drift = 0;
Per Åhgren0e3198e2019-11-18 08:52:22 +01002072 audio_proc_state.level = recommended_stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002073 audio_proc_state.keypress = capture_.key_pressed;
2074 aec_dump_->AddAudioProcessingState(audio_proc_state);
2075}
2076
kwiberg83ffe452016-08-29 14:46:07 -07002077AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002078 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002079 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002080 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002081 output_will_be_muted(false),
2082 key_pressed(false),
2083 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002084 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002085 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002086 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002087 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002088 prev_pre_amp_gain(-1.f),
2089 playout_volume(-1),
2090 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002091
2092AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2093
Per Åhgrena1351272019-08-15 12:15:46 +02002094void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2095 const float* const* data,
2096 const StreamConfig& stream_config) {
2097 if (stream_config.has_keyboard()) {
2098 keyboard_data = data[stream_config.num_channels()];
2099 } else {
2100 keyboard_data = NULL;
2101 }
2102 num_keyboard_frames = stream_config.num_frames();
2103}
2104
kwiberg83ffe452016-08-29 14:46:07 -07002105AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2106
2107AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2108
niklase@google.com470e71d2011-07-07 08:21:25 +00002109} // namespace webrtc