blob: cffbfbede3efb63b296e1c4688d60d186b3a0614 [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.
Per Åhgren2e8e1c62019-12-20 00:42:22 +01001209 AudioBuffer* linear_aec_buffer = capture_.linear_aec_output.get();
ekmeyerson60d9b332015-08-14 10:35:55 -07001210
Per Åhgrenc0424252019-12-10 13:04:15 +01001211 if (submodules_.high_pass_filter &&
1212 config_.high_pass_filter.apply_in_full_band &&
1213 !constants_.enforce_split_band_hpf) {
1214 submodules_.high_pass_filter->Process(capture_buffer,
1215 /*use_split_band_data=*/false);
1216 }
1217
saza1d600522019-10-18 13:29:43 +02001218 if (submodules_.pre_amplifier) {
1219 submodules_.pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001220 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001221 capture_buffer->num_frames()));
1222 }
1223
Per Åhgren928146f2019-08-20 09:19:21 +02001224 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001225 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001226 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001227 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1228 if (log_rms) {
1229 capture_rms_interval_counter_ = 0;
1230 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001231 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1232 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1233 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1234 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001235 }
1236
saza1d600522019-10-18 13:29:43 +02001237 if (submodules_.echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001238 // Detect and flag any change in the analog gain.
Per Åhgren0e3198e2019-11-18 08:52:22 +01001239 int analog_mic_level = recommended_stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001240 capture_.echo_path_gain_change =
1241 capture_.prev_analog_mic_level != analog_mic_level &&
1242 capture_.prev_analog_mic_level != -1;
1243 capture_.prev_analog_mic_level = analog_mic_level;
1244
Per Åhgrend2650d12018-10-02 17:00:59 +02001245 // Detect and flag any change in the pre-amplifier gain.
saza1d600522019-10-18 13:29:43 +02001246 if (submodules_.pre_amplifier) {
1247 float pre_amp_gain = submodules_.pre_amplifier->GetGainFactor();
Per Åhgrend2650d12018-10-02 17:00:59 +02001248 capture_.echo_path_gain_change =
1249 capture_.echo_path_gain_change ||
1250 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001251 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001252 capture_.prev_pre_amp_gain = pre_amp_gain;
1253 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001254
1255 // Detect volume change.
1256 capture_.echo_path_gain_change =
1257 capture_.echo_path_gain_change ||
1258 (capture_.prev_playout_volume != capture_.playout_volume &&
1259 capture_.prev_playout_volume >= 0);
1260 capture_.prev_playout_volume = capture_.playout_volume;
1261
saza1d600522019-10-18 13:29:43 +02001262 submodules_.echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001263 }
1264
Per Åhgren3daedb62019-11-22 12:11:40 +01001265 if (constants_.use_experimental_agc &&
1266 submodules_.gain_control->is_enabled()) {
1267 submodules_.agc_manager->AnalyzePreProcess(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001268 }
1269
peah2ace3f92016-09-10 04:42:27 -07001270 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1271 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001272 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1273 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001274 }
1275
Per Åhgrene14cb992019-11-27 09:34:22 +01001276 const bool multi_channel_capture = config_.pipeline.multi_channel_capture &&
1277 constants_.multi_channel_capture_support;
1278 if (submodules_.echo_controller && !multi_channel_capture) {
peah522d71b2017-02-23 05:16:26 -08001279 // Force down-mixing of the number of channels after the detection of
1280 // capture signal saturation.
1281 // TODO(peah): Look into ensuring that this kind of tampering with the
1282 // AudioBuffer functionality should not be needed.
1283 capture_buffer->set_num_channels(1);
1284 }
1285
Per Åhgrenc0424252019-12-10 13:04:15 +01001286 if (submodules_.high_pass_filter &&
1287 (!config_.high_pass_filter.apply_in_full_band ||
1288 constants_.enforce_split_band_hpf)) {
1289 submodules_.high_pass_filter->Process(capture_buffer,
1290 /*use_split_band_data=*/true);
peah8271d042016-11-22 07:24:52 -08001291 }
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001292
Per Åhgrene35b32c2019-11-22 18:22:04 +01001293 RETURN_ON_ERR(submodules_.gain_control->AnalyzeCaptureAudio(*capture_buffer));
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001294 RTC_DCHECK(
1295 !(submodules_.legacy_noise_suppressor && submodules_.noise_suppressor));
Per Åhgren2e8e1c62019-12-20 00:42:22 +01001296
1297 if (!config_.noise_suppression.analyze_linear_aec_output_when_available ||
1298 !linear_aec_buffer || submodules_.echo_control_mobile) {
1299 if (submodules_.noise_suppressor) {
1300 submodules_.noise_suppressor->Analyze(*capture_buffer);
1301 } else if (submodules_.legacy_noise_suppressor) {
1302 submodules_.legacy_noise_suppressor->AnalyzeCaptureAudio(capture_buffer);
1303 }
saza0bad15f2019-10-16 11:46:11 +02001304 }
peahb58a1582016-03-15 09:34:24 -07001305
saza1d600522019-10-18 13:29:43 +02001306 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001307 // Ensure that the stream delay was set before the call to the
1308 // AECM ProcessCaptureAudio function.
1309 if (!was_stream_delay_set()) {
1310 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001311 }
1312
saza1d600522019-10-18 13:29:43 +02001313 if (submodules_.noise_suppressor) {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001314 submodules_.noise_suppressor->Process(capture_buffer);
1315 } else if (submodules_.legacy_noise_suppressor) {
saza1d600522019-10-18 13:29:43 +02001316 submodules_.echo_control_mobile->CopyLowPassReference(capture_buffer);
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001317 submodules_.legacy_noise_suppressor->ProcessCaptureAudio(capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001318 }
peahe0eae3c2016-12-14 01:16:23 -08001319
saza1d600522019-10-18 13:29:43 +02001320 RETURN_ON_ERR(submodules_.echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001321 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001322 } else {
saza1d600522019-10-18 13:29:43 +02001323 if (submodules_.echo_controller) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001324 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1325
1326 if (was_stream_delay_set()) {
saza1d600522019-10-18 13:29:43 +02001327 submodules_.echo_controller->SetAudioBufferDelay(stream_delay_ms());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001328 }
1329
saza1d600522019-10-18 13:29:43 +02001330 submodules_.echo_controller->ProcessCapture(
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001331 capture_buffer, linear_aec_buffer, capture_.echo_path_gain_change);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001332 }
1333
Per Åhgren2e8e1c62019-12-20 00:42:22 +01001334 if (config_.noise_suppression.analyze_linear_aec_output_when_available &&
1335 linear_aec_buffer) {
1336 if (submodules_.noise_suppressor) {
1337 submodules_.noise_suppressor->Analyze(*linear_aec_buffer);
1338 } else if (submodules_.legacy_noise_suppressor) {
1339 submodules_.legacy_noise_suppressor->AnalyzeCaptureAudio(
1340 linear_aec_buffer);
1341 }
1342 }
1343
saza1d600522019-10-18 13:29:43 +02001344 if (submodules_.noise_suppressor) {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001345 submodules_.noise_suppressor->Process(capture_buffer);
1346 } else if (submodules_.legacy_noise_suppressor) {
1347 submodules_.legacy_noise_suppressor->ProcessCaptureAudio(capture_buffer);
saza0bad15f2019-10-16 11:46:11 +02001348 }
Per Åhgren46537a32017-06-07 10:08:10 +02001349 }
ivoc9f4a4a02016-10-28 05:39:16 -07001350
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001351 if (config_.voice_detection.enabled) {
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001352 capture_.stats.voice_detected =
saza1d600522019-10-18 13:29:43 +02001353 submodules_.voice_detector->ProcessCaptureAudio(capture_buffer);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001354 } else {
1355 capture_.stats.voice_detected = absl::nullopt;
1356 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001357
Per Åhgren3daedb62019-11-22 12:11:40 +01001358 if (constants_.use_experimental_agc &&
1359 submodules_.gain_control->is_enabled()) {
1360 submodules_.agc_manager->Process(capture_buffer);
1361
1362 absl::optional<int> new_digital_gain =
1363 submodules_.agc_manager->GetDigitalComressionGain();
1364 if (new_digital_gain) {
1365 submodules_.gain_control->set_compression_gain_db(*new_digital_gain);
1366 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001367 }
Per Åhgren200feba2019-03-06 04:16:46 +01001368 // TODO(peah): Add reporting from AEC3 whether there is echo.
saza1d600522019-10-18 13:29:43 +02001369 RETURN_ON_ERR(submodules_.gain_control->ProcessCaptureAudio(
Per Åhgren62ea0aa2019-12-09 10:18:44 +01001370 capture_buffer, /*stream_has_echo*/ false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001371
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001372 if (submodule_states_.CaptureMultiBandProcessingPresent() &&
peah2ace3f92016-09-10 04:42:27 -07001373 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001374 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1375 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001376 }
1377
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001378 if (capture_.capture_fullband_audio) {
saza1d600522019-10-18 13:29:43 +02001379 const auto& ec = submodules_.echo_controller;
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001380 bool ec_active = ec ? ec->ActiveProcessing() : false;
1381 // Only update the fullband buffer if the multiband processing has changed
1382 // the signal. Keep the original signal otherwise.
1383 if (submodule_states_.CaptureMultiBandProcessingActive(ec_active)) {
1384 capture_buffer->CopyTo(capture_.capture_fullband_audio.get());
1385 }
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001386 capture_buffer = capture_.capture_fullband_audio.get();
1387 }
1388
peah9e6a2902017-05-15 07:19:21 -07001389 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001390 RTC_DCHECK(submodules_.echo_detector);
1391 submodules_.echo_detector->AnalyzeCaptureAudio(rtc::ArrayView<const float>(
1392 capture_buffer->channels()[0], capture_buffer->num_frames()));
peah9e6a2902017-05-15 07:19:21 -07001393 }
1394
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001395 // TODO(aluebs): Investigate if the transient suppression placement should be
1396 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001397 if (capture_.transient_suppressor_enabled) {
saza1d600522019-10-18 13:29:43 +02001398 float voice_probability = submodules_.agc_manager.get()
1399 ? submodules_.agc_manager->voice_probability()
1400 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001401
saza1d600522019-10-18 13:29:43 +02001402 submodules_.transient_suppressor->Suppress(
Per Åhgrend47941e2019-08-22 11:51:13 +02001403 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001404 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001405 capture_buffer->split_bands_const(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001406 capture_buffer->num_frames_per_band(),
1407 capture_.keyboard_info.keyboard_data,
1408 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001409 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001410 }
1411
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001412 // Experimental APM sub-module that analyzes |capture_buffer|.
saza1d600522019-10-18 13:29:43 +02001413 if (submodules_.capture_analyzer) {
1414 submodules_.capture_analyzer->Analyze(capture_buffer);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001415 }
1416
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001417 if (config_.gain_controller2.enabled) {
saza1d600522019-10-18 13:29:43 +02001418 submodules_.gain_controller2->NotifyAnalogLevel(
Per Åhgren0e3198e2019-11-18 08:52:22 +01001419 recommended_stream_analog_level());
saza1d600522019-10-18 13:29:43 +02001420 submodules_.gain_controller2->Process(capture_buffer);
alessiob3ec96df2017-05-22 06:57:06 -07001421 }
1422
saza1d600522019-10-18 13:29:43 +02001423 if (submodules_.capture_post_processor) {
1424 submodules_.capture_post_processor->Process(capture_buffer);
Sam Zackrisson0beac582017-09-25 12:04:02 +02001425 }
1426
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001427 // The level estimator operates on the recombined data.
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001428 if (config_.level_estimation.enabled) {
saza1d600522019-10-18 13:29:43 +02001429 submodules_.output_level_estimator->ProcessStream(*capture_buffer);
1430 capture_.stats.output_rms_dbfs = submodules_.output_level_estimator->RMS();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001431 } else {
1432 capture_.stats.output_rms_dbfs = absl::nullopt;
1433 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001434
Per Åhgren928146f2019-08-20 09:19:21 +02001435 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001436 capture_buffer->channels_const()[0],
peah1b08dc32016-12-20 13:45:58 -08001437 capture_nonlocked_.capture_processing_format.num_frames()));
1438 if (log_rms) {
1439 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1440 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1441 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1442 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1443 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1444 }
1445
Per Åhgren0e3198e2019-11-18 08:52:22 +01001446 if (submodules_.agc_manager) {
1447 int level = recommended_stream_analog_level();
1448 data_dumper_->DumpRaw("experimental_gain_control_stream_analog_level", 1,
1449 &level);
1450 }
1451
peahdf3efa82015-11-28 12:35:15 -08001452 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001453 return kNoError;
1454}
1455
Gustaf Ullberg8c51f2e2019-10-22 15:21:31 +02001456int AudioProcessingImpl::AnalyzeReverseStream(
1457 const float* const* data,
1458 const StreamConfig& reverse_config) {
1459 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_StreamConfig");
1460 rtc::CritScope cs(&crit_render_);
1461 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
1462}
1463
peahde65ddc2016-09-16 15:02:15 -07001464int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1465 const StreamConfig& input_config,
1466 const StreamConfig& output_config,
1467 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001468 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001469 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001470 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001471 if (submodule_states_.RenderMultiBandProcessingActive() ||
1472 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001473 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1474 dest);
peah2ace3f92016-09-10 04:42:27 -07001475 } else if (formats_.api_format.reverse_input_stream() !=
1476 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001477 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1478 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001479 } else {
peahde65ddc2016-09-16 15:02:15 -07001480 CopyAudioIfNeeded(src, input_config.num_frames(),
1481 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001482 }
1483
1484 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001485}
1486
peahdf3efa82015-11-28 12:35:15 -08001487int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001488 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001489 const StreamConfig& input_config,
1490 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001491 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001492 return kNullPointerError;
1493 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001494
peahde65ddc2016-09-16 15:02:15 -07001495 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001496 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001497 }
1498
peahdf3efa82015-11-28 12:35:15 -08001499 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001500 processing_config.reverse_input_stream() = input_config;
1501 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001502
peahdf3efa82015-11-28 12:35:15 -08001503 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001504 RTC_DCHECK_EQ(input_config.num_frames(),
1505 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001506
aleloi868f32f2017-05-23 07:20:05 -07001507 if (aec_dump_) {
1508 const size_t channel_size =
1509 formats_.api_format.reverse_input_stream().num_frames();
1510 const size_t num_channels =
1511 formats_.api_format.reverse_input_stream().num_channels();
1512 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001513 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001514 }
peahdf3efa82015-11-28 12:35:15 -08001515 render_.render_audio->CopyFrom(src,
1516 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001517 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001518}
1519
1520int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001521 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001522 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001523 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001524 return kNullPointerError;
1525 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001526 // Must be a native rate.
1527 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1528 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001529 frame->sample_rate_hz_ != kSampleRate32kHz &&
1530 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001531 return kBadSampleRateError;
1532 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001533
Michael Graczyk86c6d332015-07-23 11:41:39 -07001534 if (frame->num_channels_ <= 0) {
1535 return kBadNumberChannelsError;
1536 }
1537
peahdf3efa82015-11-28 12:35:15 -08001538 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001539 processing_config.reverse_input_stream().set_sample_rate_hz(
1540 frame->sample_rate_hz_);
1541 processing_config.reverse_input_stream().set_num_channels(
1542 frame->num_channels_);
1543 processing_config.reverse_output_stream().set_sample_rate_hz(
1544 frame->sample_rate_hz_);
1545 processing_config.reverse_output_stream().set_num_channels(
1546 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001547
peahdf3efa82015-11-28 12:35:15 -08001548 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001549 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001550 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001551 return kBadDataLengthError;
1552 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001553
aleloi868f32f2017-05-23 07:20:05 -07001554 if (aec_dump_) {
1555 aec_dump_->WriteRenderStreamMessage(*frame);
1556 }
1557
Per Åhgrend47941e2019-08-22 11:51:13 +02001558 render_.render_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001559 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001560 if (submodule_states_.RenderMultiBandProcessingActive() ||
1561 submodule_states_.RenderFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001562 render_.render_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001563 }
aluebsb0319552016-03-17 20:39:53 -07001564 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001565}
niklase@google.com470e71d2011-07-07 08:21:25 +00001566
peahde65ddc2016-09-16 15:02:15 -07001567int AudioProcessingImpl::ProcessRenderStreamLocked() {
1568 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001569
Alex Loiko73ec0192018-05-15 10:52:28 +02001570 HandleRenderRuntimeSettings();
1571
saza1d600522019-10-18 13:29:43 +02001572 if (submodules_.render_pre_processor) {
1573 submodules_.render_pre_processor->Process(render_buffer);
Alex Loiko5825aa62017-12-18 16:02:40 +01001574 }
1575
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001576 QueueNonbandedRenderAudio(render_buffer);
1577
peah2ace3f92016-09-10 04:42:27 -07001578 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001579 SampleRateSupportsMultiBand(
1580 formats_.render_processing_format.sample_rate_hz())) {
1581 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001582 }
1583
peahce4d9152017-05-19 01:28:05 -07001584 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1585 QueueBandedRenderAudio(render_buffer);
1586 }
1587
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001588 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
saza1d600522019-10-18 13:29:43 +02001589 if (submodules_.echo_controller) {
1590 submodules_.echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001591 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001592
peah2ace3f92016-09-10 04:42:27 -07001593 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001594 SampleRateSupportsMultiBand(
1595 formats_.render_processing_format.sample_rate_hz())) {
1596 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001597 }
1598
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001599 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001600}
1601
1602int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001603 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001604 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001605 capture_.was_stream_delay_set = true;
1606 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001607
niklase@google.com470e71d2011-07-07 08:21:25 +00001608 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001609 delay = 0;
1610 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001611 }
1612
1613 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1614 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001615 delay = 500;
1616 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001617 }
1618
peahdf3efa82015-11-28 12:35:15 -08001619 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001620 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001621}
1622
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001623bool AudioProcessingImpl::GetLinearAecOutput(
1624 rtc::ArrayView<std::array<float, 160>> linear_output) const {
1625 rtc::CritScope cs(&crit_capture_);
1626 AudioBuffer* linear_aec_buffer = capture_.linear_aec_output.get();
1627
1628 RTC_DCHECK(linear_aec_buffer);
1629 if (linear_aec_buffer) {
1630 RTC_DCHECK_EQ(1, linear_aec_buffer->num_bands());
1631 RTC_DCHECK_EQ(linear_output.size(), linear_aec_buffer->num_channels());
1632
1633 for (size_t ch = 0; ch < linear_aec_buffer->num_channels(); ++ch) {
1634 RTC_DCHECK_EQ(linear_output[ch].size(), linear_aec_buffer->num_frames());
1635 rtc::ArrayView<const float> channel_view =
1636 rtc::ArrayView<const float>(linear_aec_buffer->channels_const()[ch],
1637 linear_aec_buffer->num_frames());
1638 std::copy(channel_view.begin(), channel_view.end(),
1639 linear_output[ch].begin());
1640 }
1641 return true;
1642 }
1643 RTC_LOG(LS_ERROR) << "No linear AEC output available";
1644 RTC_NOTREACHED();
1645 return false;
1646}
1647
niklase@google.com470e71d2011-07-07 08:21:25 +00001648int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001649 // Used as callback from submodules, hence locking is not allowed.
1650 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001651}
1652
1653bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001654 // Used as callback from submodules, hence locking is not allowed.
1655 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001656}
1657
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001658void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001659 rtc::CritScope cs(&crit_capture_);
1660 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001661}
1662
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001663void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001664 rtc::CritScope cs(&crit_capture_);
1665 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001666}
1667
1668int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001669 rtc::CritScope cs(&crit_capture_);
1670 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001671}
1672
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001673void AudioProcessingImpl::set_stream_analog_level(int level) {
1674 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgren0e3198e2019-11-18 08:52:22 +01001675
1676 if (submodules_.agc_manager) {
1677 submodules_.agc_manager->set_stream_analog_level(level);
1678 data_dumper_->DumpRaw("experimental_gain_control_set_stream_analog_level",
1679 1, &level);
1680 } else {
1681 int error = submodules_.gain_control->set_stream_analog_level(level);
1682 RTC_DCHECK_EQ(kNoError, error);
1683 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001684}
1685
1686int AudioProcessingImpl::recommended_stream_analog_level() const {
1687 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgren0e3198e2019-11-18 08:52:22 +01001688 if (submodules_.agc_manager) {
1689 return submodules_.agc_manager->stream_analog_level();
1690 }
1691 return submodules_.gain_control->stream_analog_level();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001692}
1693
aleloi868f32f2017-05-23 07:20:05 -07001694void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1695 RTC_DCHECK(aec_dump);
1696 rtc::CritScope cs_render(&crit_render_);
1697 rtc::CritScope cs_capture(&crit_capture_);
1698
1699 // The previously attached AecDump will be destroyed with the
1700 // 'aec_dump' parameter, which is after locks are released.
1701 aec_dump_.swap(aec_dump);
1702 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001703 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001704}
1705
1706void AudioProcessingImpl::DetachAecDump() {
1707 // The d-tor of a task-queue based AecDump blocks until all pending
1708 // tasks are done. This construction avoids blocking while holding
1709 // the render and capture locks.
1710 std::unique_ptr<AecDump> aec_dump = nullptr;
1711 {
1712 rtc::CritScope cs_render(&crit_render_);
1713 rtc::CritScope cs_capture(&crit_capture_);
1714 aec_dump = std::move(aec_dump_);
1715 }
1716}
1717
Sam Zackrisson4d364492018-03-02 16:03:21 +01001718void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1719 std::unique_ptr<AudioGenerator> audio_generator) {
1720 // TODO(bugs.webrtc.org/8882) Stub.
1721 // Reset internal audio generator with audio_generator.
1722}
1723
1724void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1725 // TODO(bugs.webrtc.org/8882) Stub.
1726 // Delete audio generator, if one is attached.
1727}
1728
Ivo Creusen56d46092017-11-24 17:29:59 +01001729AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001730 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001731 rtc::CritScope cs_capture(&crit_capture_);
1732 if (!has_remote_tracks) {
1733 return capture_.stats;
1734 }
1735 AudioProcessingStats stats = capture_.stats;
saza1d600522019-10-18 13:29:43 +02001736 if (submodules_.echo_controller) {
1737 auto ec_metrics = submodules_.echo_controller->GetMetrics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001738 stats.echo_return_loss = ec_metrics.echo_return_loss;
1739 stats.echo_return_loss_enhancement =
1740 ec_metrics.echo_return_loss_enhancement;
1741 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001742 }
1743 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001744 RTC_DCHECK(submodules_.echo_detector);
1745 auto ed_metrics = submodules_.echo_detector->GetMetrics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001746 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1747 stats.residual_echo_likelihood_recent_max =
1748 ed_metrics.echo_likelihood_recent_max;
1749 }
Ivo Creusenae026092017-11-20 13:07:16 +01001750 return stats;
1751}
1752
peah8271d042016-11-22 07:24:52 -08001753void AudioProcessingImpl::MutateConfig(
1754 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1755 rtc::CritScope cs_render(&crit_render_);
1756 rtc::CritScope cs_capture(&crit_capture_);
1757 mutator(&config_);
1758 ApplyConfig(config_);
1759}
1760
1761AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1762 rtc::CritScope cs_render(&crit_render_);
1763 rtc::CritScope cs_capture(&crit_capture_);
1764 return config_;
1765}
1766
peah2ace3f92016-09-10 04:42:27 -07001767bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1768 return submodule_states_.Update(
Per Åhgren62ea0aa2019-12-09 10:18:44 +01001769 config_.high_pass_filter.enabled, !!submodules_.echo_control_mobile,
1770 config_.residual_echo_detector.enabled,
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001771 !!submodules_.legacy_noise_suppressor || !!submodules_.noise_suppressor,
1772 submodules_.gain_control->is_enabled(), config_.gain_controller2.enabled,
1773 config_.pre_amplifier.enabled, capture_nonlocked_.echo_controller_enabled,
saza0bad15f2019-10-16 11:46:11 +02001774 config_.voice_detection.enabled, capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001775}
1776
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001777void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001778 if (capture_.transient_suppressor_enabled) {
saza1d600522019-10-18 13:29:43 +02001779 if (!submodules_.transient_suppressor.get()) {
1780 submodules_.transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001781 }
saza1d600522019-10-18 13:29:43 +02001782 submodules_.transient_suppressor->Initialize(proc_fullband_sample_rate_hz(),
1783 capture_nonlocked_.split_rate,
1784 num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001785 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001786}
1787
Per Åhgren4011de02019-12-03 11:48:48 +00001788void AudioProcessingImpl::InitializeHighPassFilter() {
Per Åhgrenb8106462019-12-04 08:34:12 +01001789 bool high_pass_filter_needed_by_aec =
1790 config_.echo_canceller.enabled &&
1791 config_.echo_canceller.enforce_high_pass_filtering &&
1792 !config_.echo_canceller.mobile_mode;
1793 if (submodule_states_.HighPassFilteringRequired() ||
1794 high_pass_filter_needed_by_aec) {
Per Åhgrenc0424252019-12-10 13:04:15 +01001795 bool use_full_band = config_.high_pass_filter.apply_in_full_band &&
1796 !constants_.enforce_split_band_hpf;
1797 int rate = use_full_band ? proc_fullband_sample_rate_hz()
1798 : proc_split_sample_rate_hz();
1799 size_t num_channels =
1800 use_full_band ? num_output_channels() : num_proc_channels();
1801
1802 submodules_.high_pass_filter.reset(new HighPassFilter(rate, num_channels));
peah8271d042016-11-22 07:24:52 -08001803 } else {
saza1d600522019-10-18 13:29:43 +02001804 submodules_.high_pass_filter.reset();
peah8271d042016-11-22 07:24:52 -08001805 }
1806}
alessiob3ec96df2017-05-22 06:57:06 -07001807
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001808void AudioProcessingImpl::InitializeVoiceDetector() {
1809 if (config_.voice_detection.enabled) {
saza1d600522019-10-18 13:29:43 +02001810 submodules_.voice_detector = std::make_unique<VoiceDetection>(
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001811 proc_split_sample_rate_hz(), VoiceDetection::kVeryLowLikelihood);
1812 } else {
saza1d600522019-10-18 13:29:43 +02001813 submodules_.voice_detector.reset();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001814 }
1815}
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001816void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001817 bool use_echo_controller =
1818 echo_control_factory_ ||
Per Åhgren62ea0aa2019-12-09 10:18:44 +01001819 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001820
1821 if (use_echo_controller) {
1822 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001823 if (echo_control_factory_) {
Per Åhgren4e5c7092019-11-01 20:44:11 +01001824 submodules_.echo_controller = echo_control_factory_->Create(
1825 proc_sample_rate_hz(), num_reverse_channels(), num_proc_channels());
Gustaf Ullberg2c6f3732019-11-07 17:15:12 +01001826 RTC_DCHECK(submodules_.echo_controller);
Per Åhgren200feba2019-03-06 04:16:46 +01001827 } else {
Per Åhgrenb2b58d82019-12-02 14:59:40 +01001828 EchoCanceller3Config config =
1829 use_setup_specific_default_aec3_config_
1830 ? EchoCanceller3::CreateDefaultConfig(num_reverse_channels(),
1831 num_proc_channels())
1832 : EchoCanceller3Config();
saza1d600522019-10-18 13:29:43 +02001833 submodules_.echo_controller = std::make_unique<EchoCanceller3>(
Per Åhgrenb2b58d82019-12-02 14:59:40 +01001834 config, proc_sample_rate_hz(), num_reverse_channels(),
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001835 num_proc_channels());
Per Åhgren200feba2019-03-06 04:16:46 +01001836 }
1837
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001838 // Setup the storage for returning the linear AEC output.
1839 if (config_.echo_canceller.export_linear_aec_output) {
1840 constexpr int kLinearOutputRateHz = 16000;
1841 capture_.linear_aec_output = std::make_unique<AudioBuffer>(
1842 kLinearOutputRateHz, num_proc_channels(), kLinearOutputRateHz,
1843 num_proc_channels(), kLinearOutputRateHz, num_proc_channels());
1844 } else {
1845 capture_.linear_aec_output.reset();
1846 }
1847
Per Åhgren200feba2019-03-06 04:16:46 +01001848 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001849
saza1d600522019-10-18 13:29:43 +02001850 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001851 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001852 return;
peahe0eae3c2016-12-14 01:16:23 -08001853 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001854
saza1d600522019-10-18 13:29:43 +02001855 submodules_.echo_controller.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001856 capture_nonlocked_.echo_controller_enabled = false;
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001857 capture_.linear_aec_output.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001858
1859 if (!config_.echo_canceller.enabled) {
saza1d600522019-10-18 13:29:43 +02001860 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001861 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001862 return;
1863 }
1864
1865 if (config_.echo_canceller.mobile_mode) {
1866 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001867 size_t max_element_size =
1868 std::max(static_cast<size_t>(1),
1869 kMaxAllowedValuesOfSamplesPerBand *
1870 EchoControlMobileImpl::NumCancellersRequired(
1871 num_output_channels(), num_reverse_channels()));
1872
1873 std::vector<int16_t> template_queue_element(max_element_size);
1874
1875 aecm_render_signal_queue_.reset(
1876 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1877 kMaxNumFramesToBuffer, template_queue_element,
1878 RenderQueueItemVerifier<int16_t>(max_element_size)));
1879
1880 aecm_render_queue_buffer_.resize(max_element_size);
1881 aecm_capture_queue_buffer_.resize(max_element_size);
1882
saza1d600522019-10-18 13:29:43 +02001883 submodules_.echo_control_mobile.reset(new EchoControlMobileImpl());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001884
saza1d600522019-10-18 13:29:43 +02001885 submodules_.echo_control_mobile->Initialize(proc_split_sample_rate_hz(),
1886 num_reverse_channels(),
1887 num_output_channels());
Per Åhgrenf204faf2019-04-25 15:18:06 +02001888 return;
1889 }
1890
saza1d600522019-10-18 13:29:43 +02001891 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001892 aecm_render_signal_queue_.reset();
peahe0eae3c2016-12-14 01:16:23 -08001893}
peah8271d042016-11-22 07:24:52 -08001894
alessiob3ec96df2017-05-22 06:57:06 -07001895void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001896 if (config_.gain_controller2.enabled) {
saza1d600522019-10-18 13:29:43 +02001897 submodules_.gain_controller2->Initialize(proc_fullband_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001898 }
1899}
1900
saza0bad15f2019-10-16 11:46:11 +02001901void AudioProcessingImpl::InitializeNoiseSuppressor() {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001902 submodules_.legacy_noise_suppressor.reset();
1903 submodules_.noise_suppressor.reset();
1904
saza0bad15f2019-10-16 11:46:11 +02001905 if (config_.noise_suppression.enabled) {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001906 const bool use_legacy_ns =
1907 config_.noise_suppression.use_legacy_ns || enforced_usage_of_legacy_ns_;
1908
1909 if (!use_legacy_ns) {
1910 auto map_level =
1911 [](AudioProcessing::Config::NoiseSuppression::Level level) {
1912 using NoiseSuppresionConfig =
1913 AudioProcessing::Config::NoiseSuppression;
1914 switch (level) {
1915 case NoiseSuppresionConfig::kLow:
1916 return NsConfig::SuppressionLevel::k6dB;
1917 case NoiseSuppresionConfig::kModerate:
1918 return NsConfig::SuppressionLevel::k12dB;
1919 case NoiseSuppresionConfig::kHigh:
1920 return NsConfig::SuppressionLevel::k18dB;
1921 case NoiseSuppresionConfig::kVeryHigh:
1922 return NsConfig::SuppressionLevel::k21dB;
1923 default:
1924 RTC_NOTREACHED();
1925 }
1926 };
1927
1928 NsConfig cfg;
1929 cfg.target_level = map_level(config_.noise_suppression.level);
1930 submodules_.noise_suppressor = std::make_unique<NoiseSuppressor>(
1931 cfg, proc_sample_rate_hz(), num_proc_channels());
1932 } else {
1933 auto ns_level =
1934 NsConfigLevelToInterfaceLevel(config_.noise_suppression.level);
1935 submodules_.legacy_noise_suppressor = std::make_unique<NoiseSuppression>(
1936 num_proc_channels(), proc_sample_rate_hz(), ns_level);
1937 }
saza0bad15f2019-10-16 11:46:11 +02001938 }
1939}
1940
Alex Loikob5c9a792018-04-16 16:31:22 +02001941void AudioProcessingImpl::InitializePreAmplifier() {
1942 if (config_.pre_amplifier.enabled) {
saza1d600522019-10-18 13:29:43 +02001943 submodules_.pre_amplifier.reset(
Alex Loikob5c9a792018-04-16 16:31:22 +02001944 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1945 } else {
saza1d600522019-10-18 13:29:43 +02001946 submodules_.pre_amplifier.reset();
Alex Loikob5c9a792018-04-16 16:31:22 +02001947 }
1948}
1949
ivoc9f4a4a02016-10-28 05:39:16 -07001950void AudioProcessingImpl::InitializeResidualEchoDetector() {
saza1d600522019-10-18 13:29:43 +02001951 RTC_DCHECK(submodules_.echo_detector);
1952 submodules_.echo_detector->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001953 proc_fullband_sample_rate_hz(), 1,
Ivo Creusenb1facc12018-04-12 16:15:58 +02001954 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001955}
1956
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001957void AudioProcessingImpl::InitializeAnalyzer() {
saza1d600522019-10-18 13:29:43 +02001958 if (submodules_.capture_analyzer) {
1959 submodules_.capture_analyzer->Initialize(proc_fullband_sample_rate_hz(),
1960 num_proc_channels());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001961 }
1962}
1963
Sam Zackrisson0beac582017-09-25 12:04:02 +02001964void AudioProcessingImpl::InitializePostProcessor() {
saza1d600522019-10-18 13:29:43 +02001965 if (submodules_.capture_post_processor) {
1966 submodules_.capture_post_processor->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001967 proc_fullband_sample_rate_hz(), num_proc_channels());
Sam Zackrisson0beac582017-09-25 12:04:02 +02001968 }
1969}
1970
Alex Loiko5825aa62017-12-18 16:02:40 +01001971void AudioProcessingImpl::InitializePreProcessor() {
saza1d600522019-10-18 13:29:43 +02001972 if (submodules_.render_pre_processor) {
1973 submodules_.render_pre_processor->Initialize(
Alex Loiko5825aa62017-12-18 16:02:40 +01001974 formats_.render_processing_format.sample_rate_hz(),
1975 formats_.render_processing_format.num_channels());
1976 }
1977}
1978
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001979void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001980
aleloi868f32f2017-05-23 07:20:05 -07001981void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1982 if (!aec_dump_) {
1983 return;
1984 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001985
1986 std::string experiments_description = "";
aleloi868f32f2017-05-23 07:20:05 -07001987 // TODO(peah): Add semicolon-separated concatenations of experiment
1988 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001989 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1990 experiments_description += "AgcClippingLevelExperiment;";
1991 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001992 if (capture_nonlocked_.echo_controller_enabled) {
1993 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001994 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001995 if (config_.gain_controller2.enabled) {
1996 experiments_description += "GainController2;";
1997 }
aleloi868f32f2017-05-23 07:20:05 -07001998
1999 InternalAPMConfig apm_config;
2000
Per Åhgren200feba2019-03-06 04:16:46 +01002001 apm_config.aec_enabled = config_.echo_canceller.enabled;
Per Åhgren62ea0aa2019-12-09 10:18:44 +01002002 apm_config.aec_delay_agnostic_enabled = false;
2003 apm_config.aec_extended_filter_enabled = false;
2004 apm_config.aec_suppression_level = 0;
aleloi868f32f2017-05-23 07:20:05 -07002005
saza1d600522019-10-18 13:29:43 +02002006 apm_config.aecm_enabled = !!submodules_.echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002007 apm_config.aecm_comfort_noise_enabled =
saza1d600522019-10-18 13:29:43 +02002008 submodules_.echo_control_mobile &&
2009 submodules_.echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002010 apm_config.aecm_routing_mode =
saza1d600522019-10-18 13:29:43 +02002011 submodules_.echo_control_mobile
2012 ? static_cast<int>(submodules_.echo_control_mobile->routing_mode())
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002013 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002014
saza1d600522019-10-18 13:29:43 +02002015 apm_config.agc_enabled = submodules_.gain_control->is_enabled();
2016 apm_config.agc_mode = static_cast<int>(submodules_.gain_control->mode());
aleloi868f32f2017-05-23 07:20:05 -07002017 apm_config.agc_limiter_enabled =
saza1d600522019-10-18 13:29:43 +02002018 submodules_.gain_control->is_limiter_enabled();
Per Åhgren0e3198e2019-11-18 08:52:22 +01002019 apm_config.noise_robust_agc_enabled = !!submodules_.agc_manager;
aleloi868f32f2017-05-23 07:20:05 -07002020
2021 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2022
saza0bad15f2019-10-16 11:46:11 +02002023 apm_config.ns_enabled = config_.noise_suppression.enabled;
2024 apm_config.ns_level = static_cast<int>(config_.noise_suppression.level);
aleloi868f32f2017-05-23 07:20:05 -07002025
2026 apm_config.transient_suppression_enabled =
2027 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002028 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002029 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2030 apm_config.pre_amplifier_fixed_gain_factor =
2031 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002032
2033 if (!forced && apm_config == apm_config_for_aec_dump_) {
2034 return;
2035 }
2036 aec_dump_->WriteConfig(apm_config);
2037 apm_config_for_aec_dump_ = apm_config;
2038}
2039
2040void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2041 const float* const* src) {
2042 RTC_DCHECK(aec_dump_);
2043 WriteAecDumpConfigMessage(false);
2044
2045 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2046 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2047 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002048 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002049 RecordAudioProcessingState();
2050}
2051
2052void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2053 const AudioFrame& capture_frame) {
2054 RTC_DCHECK(aec_dump_);
2055 WriteAecDumpConfigMessage(false);
2056
2057 aec_dump_->AddCaptureStreamInput(capture_frame);
2058 RecordAudioProcessingState();
2059}
2060
2061void AudioProcessingImpl::RecordProcessedCaptureStream(
2062 const float* const* processed_capture_stream) {
2063 RTC_DCHECK(aec_dump_);
2064
2065 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2066 const size_t num_channels =
2067 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002068 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2069 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002070 aec_dump_->WriteCaptureStreamMessage();
2071}
2072
2073void AudioProcessingImpl::RecordProcessedCaptureStream(
2074 const AudioFrame& processed_capture_frame) {
2075 RTC_DCHECK(aec_dump_);
2076
2077 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2078 aec_dump_->WriteCaptureStreamMessage();
2079}
2080
2081void AudioProcessingImpl::RecordAudioProcessingState() {
2082 RTC_DCHECK(aec_dump_);
2083 AecDump::AudioProcessingState audio_proc_state;
2084 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
Per Åhgren62ea0aa2019-12-09 10:18:44 +01002085 audio_proc_state.drift = 0;
Per Åhgren0e3198e2019-11-18 08:52:22 +01002086 audio_proc_state.level = recommended_stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002087 audio_proc_state.keypress = capture_.key_pressed;
2088 aec_dump_->AddAudioProcessingState(audio_proc_state);
2089}
2090
kwiberg83ffe452016-08-29 14:46:07 -07002091AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002092 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002093 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002094 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002095 output_will_be_muted(false),
2096 key_pressed(false),
2097 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002098 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002099 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002100 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002101 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002102 prev_pre_amp_gain(-1.f),
2103 playout_volume(-1),
2104 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002105
2106AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2107
Per Åhgrena1351272019-08-15 12:15:46 +02002108void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2109 const float* const* data,
2110 const StreamConfig& stream_config) {
2111 if (stream_config.has_keyboard()) {
2112 keyboard_data = data[stream_config.num_channels()];
2113 } else {
2114 keyboard_data = NULL;
2115 }
2116 num_keyboard_frames = stream_config.num_frames();
2117}
2118
kwiberg83ffe452016-08-29 14:46:07 -07002119AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2120
2121AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2122
niklase@google.com470e71d2011-07-07 08:21:25 +00002123} // namespace webrtc