blob: c742c1042210696171bdc3387f06566bda6e180d [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 Åhgrenc8626b62019-08-23 15:49:51 +020073// Identify the native processing rate that best handles a sample rate.
Per Åhgrenfcbe4072019-09-15 00:27:58 +020074int SuitableProcessRate(int minimum_rate,
75 int max_splitting_rate,
76 bool band_splitting_required) {
Per Åhgrenc8626b62019-08-23 15:49:51 +020077 const int uppermost_native_rate =
Per Åhgrenfcbe4072019-09-15 00:27:58 +020078 band_splitting_required ? max_splitting_rate : 48000;
Per Åhgrenc8626b62019-08-23 15:49:51 +020079 for (auto rate : {16000, 32000, 48000}) {
peah2ace3f92016-09-10 04:42:27 -070080 if (rate >= uppermost_native_rate) {
81 return uppermost_native_rate;
82 }
83 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -070084 return rate;
85 }
86 }
peah2ace3f92016-09-10 04:42:27 -070087 RTC_NOTREACHED();
88 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -070089}
90
Sam Zackrisson23513132019-01-11 15:10:32 +010091NoiseSuppression::Level NsConfigLevelToInterfaceLevel(
92 AudioProcessing::Config::NoiseSuppression::Level level) {
93 using NsConfig = AudioProcessing::Config::NoiseSuppression;
94 switch (level) {
95 case NsConfig::kLow:
saza0bad15f2019-10-16 11:46:11 +020096 return NoiseSuppression::Level::kLow;
Sam Zackrisson23513132019-01-11 15:10:32 +010097 case NsConfig::kModerate:
saza0bad15f2019-10-16 11:46:11 +020098 return NoiseSuppression::Level::kModerate;
Sam Zackrisson23513132019-01-11 15:10:32 +010099 case NsConfig::kHigh:
saza0bad15f2019-10-16 11:46:11 +0200100 return NoiseSuppression::Level::kHigh;
Sam Zackrisson23513132019-01-11 15:10:32 +0100101 case NsConfig::kVeryHigh:
saza0bad15f2019-10-16 11:46:11 +0200102 return NoiseSuppression::Level::kVeryHigh;
Sam Zackrisson23513132019-01-11 15:10:32 +0100103 default:
104 RTC_NOTREACHED();
105 }
106}
107
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100108GainControl::Mode Agc1ConfigModeToInterfaceMode(
109 AudioProcessing::Config::GainController1::Mode mode) {
110 using Agc1Config = AudioProcessing::Config::GainController1;
111 switch (mode) {
112 case Agc1Config::kAdaptiveAnalog:
113 return GainControl::kAdaptiveAnalog;
114 case Agc1Config::kAdaptiveDigital:
115 return GainControl::kAdaptiveDigital;
116 case Agc1Config::kFixedDigital:
117 return GainControl::kFixedDigital;
118 }
119}
120
peah9e6a2902017-05-15 07:19:21 -0700121// Maximum lengths that frame of samples being passed from the render side to
122// the capture side can have (does not apply to AEC3).
123static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
124static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
125
peah764e3642016-10-22 05:04:30 -0700126// Maximum number of frames to buffer in the render queue.
127// TODO(peah): Decrease this once we properly handle hugely unbalanced
128// reverse and forward call numbers.
129static const size_t kMaxNumFramesToBuffer = 100;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700130} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000131
132// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000133static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000134
saza1d600522019-10-18 13:29:43 +0200135AudioProcessingImpl::SubmoduleStates::SubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100136 bool capture_post_processor_enabled,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200137 bool render_pre_processor_enabled,
138 bool capture_analyzer_enabled)
Alex Loiko5825aa62017-12-18 16:02:40 +0100139 : capture_post_processor_enabled_(capture_post_processor_enabled),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200140 render_pre_processor_enabled_(render_pre_processor_enabled),
141 capture_analyzer_enabled_(capture_analyzer_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700142
saza1d600522019-10-18 13:29:43 +0200143bool AudioProcessingImpl::SubmoduleStates::Update(
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200144 bool high_pass_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700145 bool echo_canceller_enabled,
146 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700147 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700148 bool noise_suppressor_enabled,
peah2ace3f92016-09-10 04:42:27 -0700149 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700150 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200151 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200152 bool echo_controller_enabled,
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200153 bool voice_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700154 bool transient_suppressor_enabled) {
155 bool changed = false;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200156 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700157 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
158 changed |=
159 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700160 changed |=
161 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700162 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
163 changed |=
peah2ace3f92016-09-10 04:42:27 -0700164 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200165 changed |= (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200166 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200167 changed |= (echo_controller_enabled != echo_controller_enabled_);
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200168 changed |= (voice_detector_enabled != voice_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700169 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
170 if (changed) {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200171 high_pass_filter_enabled_ = high_pass_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700172 echo_canceller_enabled_ = echo_canceller_enabled;
173 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700174 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700175 noise_suppressor_enabled_ = noise_suppressor_enabled;
peah2ace3f92016-09-10 04:42:27 -0700176 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700177 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200178 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200179 echo_controller_enabled_ = echo_controller_enabled;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200180 voice_detector_enabled_ = voice_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700181 transient_suppressor_enabled_ = transient_suppressor_enabled;
182 }
183
184 changed |= first_update_;
185 first_update_ = false;
186 return changed;
187}
188
saza1d600522019-10-18 13:29:43 +0200189bool AudioProcessingImpl::SubmoduleStates::CaptureMultiBandSubModulesActive()
peah2ace3f92016-09-10 04:42:27 -0700190 const {
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200191 return CaptureMultiBandProcessingPresent() || voice_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700192}
193
saza1d600522019-10-18 13:29:43 +0200194bool AudioProcessingImpl::SubmoduleStates::CaptureMultiBandProcessingPresent()
195 const {
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200196 // If echo controller is present, assume it performs active processing.
197 return CaptureMultiBandProcessingActive(/*ec_processing_active=*/true);
198}
199
saza1d600522019-10-18 13:29:43 +0200200bool AudioProcessingImpl::SubmoduleStates::CaptureMultiBandProcessingActive(
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200201 bool ec_processing_active) const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200202 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700203 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200204 adaptive_gain_controller_enabled_ ||
205 (echo_controller_enabled_ && ec_processing_active);
peah2ace3f92016-09-10 04:42:27 -0700206}
207
saza1d600522019-10-18 13:29:43 +0200208bool AudioProcessingImpl::SubmoduleStates::CaptureFullBandProcessingActive()
peah23ac8b42017-05-23 05:33:56 -0700209 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200210 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
211 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700212}
213
saza1d600522019-10-18 13:29:43 +0200214bool AudioProcessingImpl::SubmoduleStates::CaptureAnalyzerActive() const {
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200215 return capture_analyzer_enabled_;
216}
217
saza1d600522019-10-18 13:29:43 +0200218bool AudioProcessingImpl::SubmoduleStates::RenderMultiBandSubModulesActive()
peah2ace3f92016-09-10 04:42:27 -0700219 const {
220 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800221 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200222 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700223}
224
saza1d600522019-10-18 13:29:43 +0200225bool AudioProcessingImpl::SubmoduleStates::RenderFullBandProcessingActive()
Alex Loiko5825aa62017-12-18 16:02:40 +0100226 const {
227 return render_pre_processor_enabled_;
228}
229
saza1d600522019-10-18 13:29:43 +0200230bool AudioProcessingImpl::SubmoduleStates::RenderMultiBandProcessingActive()
peah2ace3f92016-09-10 04:42:27 -0700231 const {
peah2ace3f92016-09-10 04:42:27 -0700232 return false;
peah2ace3f92016-09-10 04:42:27 -0700233}
234
saza1d600522019-10-18 13:29:43 +0200235bool AudioProcessingImpl::SubmoduleStates::HighPassFilteringRequired() const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200236 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
237 mobile_echo_controller_enabled_ || noise_suppressor_enabled_;
238}
239
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100240AudioProcessingBuilder::AudioProcessingBuilder() = default;
241AudioProcessingBuilder::~AudioProcessingBuilder() = default;
242
243AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
244 std::unique_ptr<CustomProcessing> capture_post_processing) {
245 capture_post_processing_ = std::move(capture_post_processing);
246 return *this;
247}
248
249AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
250 std::unique_ptr<CustomProcessing> render_pre_processing) {
251 render_pre_processing_ = std::move(render_pre_processing);
252 return *this;
253}
254
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200255AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
256 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
257 capture_analyzer_ = std::move(capture_analyzer);
258 return *this;
259}
260
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100261AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
262 std::unique_ptr<EchoControlFactory> echo_control_factory) {
263 echo_control_factory_ = std::move(echo_control_factory);
264 return *this;
265}
266
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100267AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200268 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100269 echo_detector_ = std::move(echo_detector);
270 return *this;
271}
272
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100273AudioProcessing* AudioProcessingBuilder::Create() {
274 webrtc::Config config;
275 return Create(config);
276}
277
278AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100279 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
280 config, std::move(capture_post_processing_),
281 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200282 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100283 if (apm->Initialize() != AudioProcessing::kNoError) {
284 delete apm;
285 apm = nullptr;
286 }
287 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100288}
289
peah88ac8532016-09-12 16:47:25 -0700290AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200291 : AudioProcessingImpl(config,
292 /*capture_post_processor=*/nullptr,
293 /*render_pre_processor=*/nullptr,
294 /*echo_control_factory=*/nullptr,
295 /*echo_detector=*/nullptr,
296 /*capture_analyzer=*/nullptr) {}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000297
Per Åhgren13735822018-02-12 21:42:56 +0100298int AudioProcessingImpl::instance_count_ = 0;
299
Sam Zackrisson0beac582017-09-25 12:04:02 +0200300AudioProcessingImpl::AudioProcessingImpl(
301 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100302 std::unique_ptr<CustomProcessing> capture_post_processor,
303 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200304 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200305 rtc::scoped_refptr<EchoDetector> echo_detector,
306 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100307 : data_dumper_(
308 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200309 capture_runtime_settings_(kRuntimeSettingQueueSize),
310 render_runtime_settings_(kRuntimeSettingQueueSize),
311 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
312 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200313 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200314 submodule_states_(!!capture_post_processor,
315 !!render_pre_processor,
316 !!capture_analyzer),
saza1d600522019-10-18 13:29:43 +0200317 submodules_(std::move(capture_post_processor),
318 std::move(render_pre_processor),
319 std::move(echo_detector),
320 std::move(capture_analyzer)),
peahdf3efa82015-11-28 12:35:15 -0800321 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800322 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000323#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alex Loikod9342442018-09-10 13:59:41 +0200324 /* enabled= */ false,
325 /* enabled_agc2_level_estimator= */ false,
326 /* digital_adaptive_disabled= */ false,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200327 /* analyze_before_aec= */ false,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000328#else
Alex Loiko64cb83b2018-07-02 13:38:19 +0200329 config.Get<ExperimentalAgc>().enabled,
330 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
Alex Loikod9342442018-09-10 13:59:41 +0200331 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200332 config.Get<ExperimentalAgc>().analyze_before_aec,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000333#endif
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200334 !field_trial::IsEnabled(
335 "WebRTC-ApmExperimentalMultiChannelRenderKillSwitch"),
336 !field_trial::IsEnabled(
337 "WebRTC-ApmExperimentalMultiChannelCaptureKillSwitch")),
andrew1c7075f2015-06-24 18:14:14 -0700338#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200339 capture_(false),
andrew1c7075f2015-06-24 18:14:14 -0700340#else
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200341 capture_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700342#endif
Alessio Bazzicacc22f512018-08-30 13:01:34 +0200343 capture_nonlocked_() {
Sam Zackrisson421c8592019-02-11 13:39:46 +0100344 // Mark Echo Controller enabled if a factory is injected.
345 capture_nonlocked_.echo_controller_enabled =
346 static_cast<bool>(echo_control_factory_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
saza1d600522019-10-18 13:29:43 +0200348 submodules_.gain_control.reset(new GainControlImpl());
349 submodules_.gain_control_for_experimental_agc.reset(
350 new GainControlForExperimentalAgc(submodules_.gain_control.get()));
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200351
Sam Zackrisson421c8592019-02-11 13:39:46 +0100352 // If no echo detector is injected, use the ResidualEchoDetector.
saza1d600522019-10-18 13:29:43 +0200353 if (!submodules_.echo_detector) {
354 submodules_.echo_detector =
Sam Zackrisson421c8592019-02-11 13:39:46 +0100355 new rtc::RefCountedObject<ResidualEchoDetector>();
peahdf3efa82015-11-28 12:35:15 -0800356 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000357
Sam Zackrisson421c8592019-02-11 13:39:46 +0100358 // TODO(alessiob): Move the injected gain controller once injection is
359 // implemented.
saza1d600522019-10-18 13:29:43 +0200360 submodules_.gain_controller2.reset(new GainController2());
Sam Zackrisson421c8592019-02-11 13:39:46 +0100361
362 RTC_LOG(LS_INFO) << "Capture analyzer activated: "
saza1d600522019-10-18 13:29:43 +0200363 << !!submodules_.capture_analyzer
Sam Zackrisson421c8592019-02-11 13:39:46 +0100364 << "\nCapture post processor activated: "
saza1d600522019-10-18 13:29:43 +0200365 << !!submodules_.capture_post_processor
Sam Zackrisson421c8592019-02-11 13:39:46 +0100366 << "\nRender pre processor activated: "
saza1d600522019-10-18 13:29:43 +0200367 << !!submodules_.render_pre_processor;
Sam Zackrisson421c8592019-02-11 13:39:46 +0100368
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000369 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000370}
371
372AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800373 // Depends on gain_control_ and
saza1d600522019-10-18 13:29:43 +0200374 // submodules_.gain_control_for_experimental_agc.
375 submodules_.agc_manager.reset();
peahdf3efa82015-11-28 12:35:15 -0800376 // Depends on gain_control_.
saza1d600522019-10-18 13:29:43 +0200377 submodules_.gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000378}
379
niklase@google.com470e71d2011-07-07 08:21:25 +0000380int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800381 // Run in a single-threaded manner during initialization.
382 rtc::CritScope cs_render(&crit_render_);
383 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000384 return InitializeLocked();
385}
386
peahde65ddc2016-09-16 15:02:15 -0700387int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
388 int capture_output_sample_rate_hz,
389 int render_input_sample_rate_hz,
390 ChannelLayout capture_input_layout,
391 ChannelLayout capture_output_layout,
392 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700393 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700394 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
395 LayoutHasKeyboard(capture_input_layout)},
396 {capture_output_sample_rate_hz,
397 ChannelsFromLayout(capture_output_layout),
398 LayoutHasKeyboard(capture_output_layout)},
399 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
400 LayoutHasKeyboard(render_input_layout)},
401 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
402 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700403
404 return Initialize(processing_config);
405}
406
407int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800408 // Run in a single-threaded manner during initialization.
409 rtc::CritScope cs_render(&crit_render_);
410 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700411 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000412}
413
peahdf3efa82015-11-28 12:35:15 -0800414int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800415 const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800416 // Called from both threads. Thread check is therefore not possible.
Oskar Sundbom4b276482019-05-23 14:28:00 +0200417 if (processing_config == formats_.api_format) {
peah192164e2015-11-17 02:16:45 -0800418 return kNoError;
419 }
peahdf3efa82015-11-28 12:35:15 -0800420
421 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800422 return InitializeLocked(processing_config);
423}
424
niklase@google.com470e71d2011-07-07 08:21:25 +0000425int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200426 UpdateActiveSubmoduleStates();
427
Per Åhgrend47941e2019-08-22 11:51:13 +0200428 const int render_audiobuffer_sample_rate_hz =
peahdf3efa82015-11-28 12:35:15 -0800429 formats_.api_format.reverse_output_stream().num_frames() == 0
Per Åhgrend47941e2019-08-22 11:51:13 +0200430 ? formats_.render_processing_format.sample_rate_hz()
431 : formats_.api_format.reverse_output_stream().sample_rate_hz();
peahdf3efa82015-11-28 12:35:15 -0800432 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
433 render_.render_audio.reset(new AudioBuffer(
Per Åhgrend47941e2019-08-22 11:51:13 +0200434 formats_.api_format.reverse_input_stream().sample_rate_hz(),
peahdf3efa82015-11-28 12:35:15 -0800435 formats_.api_format.reverse_input_stream().num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200436 formats_.render_processing_format.sample_rate_hz(),
peahde65ddc2016-09-16 15:02:15 -0700437 formats_.render_processing_format.num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200438 render_audiobuffer_sample_rate_hz,
439 formats_.render_processing_format.num_channels()));
peah2ace3f92016-09-10 04:42:27 -0700440 if (formats_.api_format.reverse_input_stream() !=
441 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800442 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800443 formats_.api_format.reverse_input_stream().num_channels(),
444 formats_.api_format.reverse_input_stream().num_frames(),
445 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800446 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700447 } else {
peahdf3efa82015-11-28 12:35:15 -0800448 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700449 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700450 } else {
peahdf3efa82015-11-28 12:35:15 -0800451 render_.render_audio.reset(nullptr);
452 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700453 }
peahce4d9152017-05-19 01:28:05 -0700454
Per Åhgrend47941e2019-08-22 11:51:13 +0200455 capture_.capture_audio.reset(new AudioBuffer(
456 formats_.api_format.input_stream().sample_rate_hz(),
457 formats_.api_format.input_stream().num_channels(),
458 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
459 formats_.api_format.output_stream().num_channels(),
460 formats_.api_format.output_stream().sample_rate_hz(),
461 formats_.api_format.output_stream().num_channels()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000462
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200463 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() <
464 formats_.api_format.output_stream().sample_rate_hz() &&
465 formats_.api_format.output_stream().sample_rate_hz() == 48000) {
466 capture_.capture_fullband_audio.reset(
467 new AudioBuffer(formats_.api_format.input_stream().sample_rate_hz(),
468 formats_.api_format.input_stream().num_channels(),
469 formats_.api_format.output_stream().sample_rate_hz(),
470 formats_.api_format.output_stream().num_channels(),
471 formats_.api_format.output_stream().sample_rate_hz(),
472 formats_.api_format.output_stream().num_channels()));
473 } else {
474 capture_.capture_fullband_audio.reset();
475 }
476
peah764e3642016-10-22 05:04:30 -0700477 AllocateRenderQueue();
478
saza1d600522019-10-18 13:29:43 +0200479 submodules_.gain_control->Initialize(num_proc_channels(),
480 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700481 if (constants_.use_experimental_agc) {
saza1d600522019-10-18 13:29:43 +0200482 if (!submodules_.agc_manager.get()) {
483 submodules_.agc_manager.reset(new AgcManagerDirect(
484 submodules_.gain_control.get(),
485 submodules_.gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200486 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
487 constants_.use_experimental_agc_agc2_level_estimation,
488 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700489 }
saza1d600522019-10-18 13:29:43 +0200490 submodules_.agc_manager->Initialize();
491 submodules_.agc_manager->SetCaptureMuted(capture_.output_will_be_muted);
492 submodules_.gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700493 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200494 InitializeTransient();
Per Åhgren0aefbf02019-08-23 21:29:17 +0200495 InitializeHighPassFilter();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200496 InitializeVoiceDetector();
ivoc9f4a4a02016-10-28 05:39:16 -0700497 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200498 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700499 InitializeGainController2();
saza0bad15f2019-10-16 11:46:11 +0200500 InitializeNoiseSuppressor();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200501 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200502 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100503 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800504
aleloi868f32f2017-05-23 07:20:05 -0700505 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200506 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700507 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000508 return kNoError;
509}
510
Michael Graczyk86c6d332015-07-23 11:41:39 -0700511int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200512 UpdateActiveSubmoduleStates();
513
Michael Graczyk86c6d332015-07-23 11:41:39 -0700514 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700515 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
516 return kBadSampleRateError;
517 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000518 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700519
Peter Kasting69558702016-01-12 16:26:35 -0800520 const size_t num_in_channels = config.input_stream().num_channels();
521 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700522
523 // Need at least one input channel.
524 // Need either one output channel or as many outputs as there are inputs.
525 if (num_in_channels == 0 ||
526 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700527 return kBadNumberChannelsError;
528 }
529
peahdf3efa82015-11-28 12:35:15 -0800530 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000531
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200532 // Choose maximum rate to use for the split filtering.
533 RTC_DCHECK(config_.pipeline.maximum_internal_processing_rate == 48000 ||
534 config_.pipeline.maximum_internal_processing_rate == 32000);
535 int max_splitting_rate = 48000;
536 if (config_.pipeline.maximum_internal_processing_rate == 32000) {
537 max_splitting_rate = config_.pipeline.maximum_internal_processing_rate;
538 }
539
Per Åhgrenc8626b62019-08-23 15:49:51 +0200540 int capture_processing_rate = SuitableProcessRate(
peah423d2362016-04-09 16:06:52 -0700541 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700542 formats_.api_format.output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200543 max_splitting_rate,
peah2ace3f92016-09-10 04:42:27 -0700544 submodule_states_.CaptureMultiBandSubModulesActive() ||
545 submodule_states_.RenderMultiBandSubModulesActive());
Per Åhgrenc8626b62019-08-23 15:49:51 +0200546 RTC_DCHECK_NE(8000, capture_processing_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000547
peahde65ddc2016-09-16 15:02:15 -0700548 capture_nonlocked_.capture_processing_format =
549 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700550
peah2ce640f2017-04-07 03:57:48 -0700551 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200552 if (!capture_nonlocked_.echo_controller_enabled) {
Per Åhgrenc8626b62019-08-23 15:49:51 +0200553 render_processing_rate = SuitableProcessRate(
peah2ce640f2017-04-07 03:57:48 -0700554 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
555 formats_.api_format.reverse_output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200556 max_splitting_rate,
peah2ce640f2017-04-07 03:57:48 -0700557 submodule_states_.CaptureMultiBandSubModulesActive() ||
558 submodule_states_.RenderMultiBandSubModulesActive());
559 } else {
560 render_processing_rate = capture_processing_rate;
561 }
562
peahde65ddc2016-09-16 15:02:15 -0700563 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700564 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700565 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
566 kSampleRate8kHz) {
567 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000568 } else {
peahde65ddc2016-09-16 15:02:15 -0700569 render_processing_rate =
570 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000571 }
572
Per Åhgrenc8626b62019-08-23 15:49:51 +0200573 RTC_DCHECK_NE(8000, render_processing_rate);
574
peahce4d9152017-05-19 01:28:05 -0700575 if (submodule_states_.RenderMultiBandSubModulesActive()) {
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200576 // By default, downmix the render stream to mono for analysis. This has been
577 // demonstrated to work well for AEC in most practical scenarios.
578 const bool experimental_multi_channel_render =
579 config_.pipeline.experimental_multi_channel &&
580 constants_.experimental_multi_channel_render_support;
581 int render_processing_num_channels =
582 experimental_multi_channel_render
583 ? formats_.api_format.reverse_input_stream().num_channels()
584 : 1;
585 formats_.render_processing_format =
586 StreamConfig(render_processing_rate, render_processing_num_channels);
peahce4d9152017-05-19 01:28:05 -0700587 } else {
588 formats_.render_processing_format = StreamConfig(
589 formats_.api_format.reverse_input_stream().sample_rate_hz(),
590 formats_.api_format.reverse_input_stream().num_channels());
591 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000592
peahde65ddc2016-09-16 15:02:15 -0700593 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
594 kSampleRate32kHz ||
595 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
596 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800597 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000598 } else {
peahdf3efa82015-11-28 12:35:15 -0800599 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700600 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000601 }
602
603 return InitializeLocked();
604}
605
peah88ac8532016-09-12 16:47:25 -0700606void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peah88ac8532016-09-12 16:47:25 -0700607 // Run in a single-threaded manner when applying the settings.
608 rtc::CritScope cs_render(&crit_render_);
609 rtc::CritScope cs_capture(&crit_capture_);
610
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200611 const bool pipeline_config_changed =
612 config_.pipeline.experimental_multi_channel !=
613 config.pipeline.experimental_multi_channel;
614
Per Åhgren200feba2019-03-06 04:16:46 +0100615 const bool aec_config_changed =
616 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
617 config_.echo_canceller.use_legacy_aec !=
618 config.echo_canceller.use_legacy_aec ||
619 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode ||
620 (config_.echo_canceller.enabled && config.echo_canceller.use_legacy_aec &&
621 config_.echo_canceller.legacy_moderate_suppression_level !=
622 config.echo_canceller.legacy_moderate_suppression_level);
623
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100624 const bool agc1_config_changed =
625 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
626 config_.gain_controller1.mode != config.gain_controller1.mode ||
627 config_.gain_controller1.target_level_dbfs !=
628 config.gain_controller1.target_level_dbfs ||
629 config_.gain_controller1.compression_gain_db !=
630 config.gain_controller1.compression_gain_db ||
631 config_.gain_controller1.enable_limiter !=
632 config.gain_controller1.enable_limiter ||
633 config_.gain_controller1.analog_level_minimum !=
634 config.gain_controller1.analog_level_minimum ||
635 config_.gain_controller1.analog_level_maximum !=
636 config.gain_controller1.analog_level_maximum;
637
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200638 const bool voice_detection_config_changed =
639 config_.voice_detection.enabled != config.voice_detection.enabled;
640
saza0bad15f2019-10-16 11:46:11 +0200641 const bool ns_config_changed =
642 config_.noise_suppression.enabled != config.noise_suppression.enabled ||
643 config_.noise_suppression.level != config.noise_suppression.level;
644
Yves Gerey499bc6c2018-10-10 18:29:07 +0200645 config_ = config;
646
Per Åhgren200feba2019-03-06 04:16:46 +0100647 if (aec_config_changed) {
648 InitializeEchoController();
649 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200650
saza0bad15f2019-10-16 11:46:11 +0200651 if (ns_config_changed) {
652 InitializeNoiseSuppressor();
653 }
Sam Zackrisson23513132019-01-11 15:10:32 +0100654
Per Åhgren0aefbf02019-08-23 21:29:17 +0200655 InitializeHighPassFilter();
peah8271d042016-11-22 07:24:52 -0800656
Mirko Bonadei675513b2017-11-09 11:09:25 +0100657 RTC_LOG(LS_INFO) << "Highpass filter activated: "
658 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800659
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100660 if (agc1_config_changed) {
661 ApplyAgc1Config(config_.gain_controller1);
662 }
663
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100664 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700665 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100666 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
667 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100668 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100669 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700670 config_.gain_controller2 = AudioProcessing::Config::GainController2();
671 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200672 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200673 InitializePreAmplifier();
saza1d600522019-10-18 13:29:43 +0200674 submodules_.gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100675 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
676 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200677 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
678 << config_.pre_amplifier.enabled;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100679
saza1d600522019-10-18 13:29:43 +0200680 if (config_.level_estimation.enabled && !submodules_.output_level_estimator) {
681 submodules_.output_level_estimator = std::make_unique<LevelEstimator>();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100682 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100683
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200684 if (voice_detection_config_changed) {
685 InitializeVoiceDetector();
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100686 }
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200687
688 // Reinitialization must happen after all submodule configuration to avoid
689 // additional reinitializations on the next capture / render processing call.
690 if (pipeline_config_changed) {
691 InitializeLocked(formats_.api_format);
692 }
peah88ac8532016-09-12 16:47:25 -0700693}
694
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100695void AudioProcessingImpl::ApplyAgc1Config(
696 const Config::GainController1& config) {
697 GainControl* agc = agc1();
698 int error = agc->Enable(config.enabled);
699 RTC_DCHECK_EQ(kNoError, error);
700 error = agc->set_mode(Agc1ConfigModeToInterfaceMode(config.mode));
701 RTC_DCHECK_EQ(kNoError, error);
702 error = agc->set_target_level_dbfs(config.target_level_dbfs);
703 RTC_DCHECK_EQ(kNoError, error);
704 error = agc->set_compression_gain_db(config.compression_gain_db);
705 RTC_DCHECK_EQ(kNoError, error);
706 error = agc->enable_limiter(config.enable_limiter);
707 RTC_DCHECK_EQ(kNoError, error);
708 error = agc->set_analog_level_limits(config.analog_level_minimum,
709 config.analog_level_maximum);
710 RTC_DCHECK_EQ(kNoError, error);
711}
712
713GainControl* AudioProcessingImpl::agc1() {
714 if (constants_.use_experimental_agc) {
saza1d600522019-10-18 13:29:43 +0200715 return submodules_.gain_control_for_experimental_agc.get();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100716 }
saza1d600522019-10-18 13:29:43 +0200717 return submodules_.gain_control.get();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100718}
719
720const GainControl* AudioProcessingImpl::agc1() const {
721 if (constants_.use_experimental_agc) {
saza1d600522019-10-18 13:29:43 +0200722 return submodules_.gain_control_for_experimental_agc.get();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100723 }
saza1d600522019-10-18 13:29:43 +0200724 return submodules_.gain_control.get();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100725}
726
peah88ac8532016-09-12 16:47:25 -0700727void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800728 // Run in a single-threaded manner when setting the extra options.
729 rtc::CritScope cs_render(&crit_render_);
730 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000731
Per Åhgrenf204faf2019-04-25 15:18:06 +0200732 capture_nonlocked_.use_aec2_extended_filter =
733 config.Get<ExtendedFilter>().enabled;
734 capture_nonlocked_.use_aec2_delay_agnostic =
735 config.Get<DelayAgnostic>().enabled;
736 capture_nonlocked_.use_aec2_refined_adaptive_filter =
737 config.Get<RefinedAdaptiveFilter>().enabled;
peahb624d8c2016-03-05 03:01:14 -0800738
peahdf3efa82015-11-28 12:35:15 -0800739 if (capture_.transient_suppressor_enabled !=
740 config.Get<ExperimentalNs>().enabled) {
741 capture_.transient_suppressor_enabled =
742 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000743 InitializeTransient();
744 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000745}
746
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000747int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800748 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700749 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000750}
751
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200752int AudioProcessingImpl::proc_fullband_sample_rate_hz() const {
753 return capture_.capture_fullband_audio
754 ? capture_.capture_fullband_audio->num_frames() * 100
755 : capture_nonlocked_.capture_processing_format.sample_rate_hz();
756}
757
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000758int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800759 // Used as callback from submodules, hence locking is not allowed.
760 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000761}
762
Peter Kasting69558702016-01-12 16:26:35 -0800763size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800764 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700765 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000766}
767
Peter Kasting69558702016-01-12 16:26:35 -0800768size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800769 // Used as callback from submodules, hence locking is not allowed.
770 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000771}
772
Peter Kasting69558702016-01-12 16:26:35 -0800773size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800774 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200775 const bool experimental_multi_channel_capture =
776 config_.pipeline.experimental_multi_channel &&
777 constants_.experimental_multi_channel_capture_support;
778 if (capture_nonlocked_.echo_controller_enabled &&
779 !experimental_multi_channel_capture) {
780 return 1;
781 }
782 return num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800783}
784
Peter Kasting69558702016-01-12 16:26:35 -0800785size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800786 // Used as callback from submodules, hence locking is not allowed.
787 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000788}
789
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000790void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800791 rtc::CritScope cs(&crit_capture_);
792 capture_.output_will_be_muted = muted;
saza1d600522019-10-18 13:29:43 +0200793 if (submodules_.agc_manager.get()) {
794 submodules_.agc_manager->SetCaptureMuted(capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000795 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000796}
797
Alessio Bazzicac054e782018-04-16 12:10:09 +0200798void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200799 switch (setting.type()) {
800 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
801 render_runtime_settings_enqueuer_.Enqueue(setting);
802 return;
803 case RuntimeSetting::Type::kNotSpecified:
804 RTC_NOTREACHED();
805 return;
806 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100807 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200808 case RuntimeSetting::Type::kCaptureFixedPostGain:
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200809 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200810 capture_runtime_settings_enqueuer_.Enqueue(setting);
811 return;
812 }
813 // The language allows the enum to have a non-enumerator
814 // value. Check that this doesn't happen.
815 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200816}
817
818AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
819 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200820 : runtime_settings_(*runtime_settings) {
821 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200822}
823
824AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
825 default;
826
827void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
828 RuntimeSetting setting) {
829 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200830 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200831 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200832 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200833 RTC_LOG(LS_ERROR)
834 << "The runtime settings queue is full. Oldest setting discarded.";
835 }
836 if (remaining_attempts == 0)
837 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
838}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000839
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000840int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700841 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000842 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000843 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000844 int output_sample_rate_hz,
845 ChannelLayout output_layout,
846 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800847 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800848 StreamConfig input_stream;
849 StreamConfig output_stream;
850 {
851 // Access the formats_.api_format.input_stream beneath the capture lock.
852 // The lock must be released as it is later required in the call
853 // to ProcessStream(,,,);
854 rtc::CritScope cs(&crit_capture_);
855 input_stream = formats_.api_format.input_stream();
856 output_stream = formats_.api_format.output_stream();
857 }
858
Michael Graczyk86c6d332015-07-23 11:41:39 -0700859 input_stream.set_sample_rate_hz(input_sample_rate_hz);
860 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
861 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700862 output_stream.set_sample_rate_hz(output_sample_rate_hz);
863 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
864 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
865
866 if (samples_per_channel != input_stream.num_frames()) {
867 return kBadDataLengthError;
868 }
869 return ProcessStream(src, input_stream, output_stream, dest);
870}
871
872int AudioProcessingImpl::ProcessStream(const float* const* src,
873 const StreamConfig& input_config,
874 const StreamConfig& output_config,
875 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800876 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800877 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700878 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800879 {
880 // Acquire the capture lock in order to safely call the function
881 // that retrieves the render side data. This function accesses apm
882 // getters that need the capture lock held when being called.
883 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700884 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800885
886 if (!src || !dest) {
887 return kNullPointerError;
888 }
889
890 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700891 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000892 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000893
Oskar Sundbom4b276482019-05-23 14:28:00 +0200894 if (processing_config.input_stream() != input_config) {
895 processing_config.input_stream() = input_config;
896 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800897 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200898
899 if (processing_config.output_stream() != output_config) {
900 processing_config.output_stream() = output_config;
901 reinitialization_required = true;
902 }
903
904 if (reinitialization_required) {
905 // Reinitialize.
906 rtc::CritScope cs_render(&crit_render_);
907 rtc::CritScope cs_capture(&crit_capture_);
908 RETURN_ON_ERR(InitializeLocked(processing_config));
909 }
910
peahdf3efa82015-11-28 12:35:15 -0800911 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700912 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
913 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000914
aleloi868f32f2017-05-23 07:20:05 -0700915 if (aec_dump_) {
916 RecordUnprocessedCaptureStream(src);
917 }
918
Per Åhgrena1351272019-08-15 12:15:46 +0200919 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800920 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200921 if (capture_.capture_fullband_audio) {
922 capture_.capture_fullband_audio->CopyFrom(
923 src, formats_.api_format.input_stream());
924 }
peahde65ddc2016-09-16 15:02:15 -0700925 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200926 if (capture_.capture_fullband_audio) {
927 capture_.capture_fullband_audio->CopyTo(formats_.api_format.output_stream(),
928 dest);
929 } else {
930 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
931 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000932
aleloi868f32f2017-05-23 07:20:05 -0700933 if (aec_dump_) {
934 RecordProcessedCaptureStream(dest);
935 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000936 return kNoError;
937}
938
Alex Loiko73ec0192018-05-15 10:52:28 +0200939void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200940 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200941 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200942 if (aec_dump_) {
943 aec_dump_->WriteRuntimeSetting(setting);
944 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200945 switch (setting.type()) {
946 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200947 if (config_.pre_amplifier.enabled) {
948 float value;
949 setting.GetFloat(&value);
saza1d600522019-10-18 13:29:43 +0200950 submodules_.pre_amplifier->SetGainFactor(value);
Alex Loikob5c9a792018-04-16 16:31:22 +0200951 }
952 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200953 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100954 case RuntimeSetting::Type::kCaptureCompressionGain: {
955 float value;
956 setting.GetFloat(&value);
957 int int_value = static_cast<int>(value + .5f);
958 config_.gain_controller1.compression_gain_db = int_value;
959 int error = agc1()->set_compression_gain_db(int_value);
960 RTC_DCHECK_EQ(kNoError, error);
961 break;
962 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200963 case RuntimeSetting::Type::kCaptureFixedPostGain: {
964 if (config_.gain_controller2.enabled) {
965 float value;
966 setting.GetFloat(&value);
967 config_.gain_controller2.fixed_digital.gain_db = value;
saza1d600522019-10-18 13:29:43 +0200968 submodules_.gain_controller2->ApplyConfig(config_.gain_controller2);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200969 }
970 break;
971 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200972 case RuntimeSetting::Type::kPlayoutVolumeChange: {
973 int value;
974 setting.GetInt(&value);
975 capture_.playout_volume = value;
976 break;
977 }
Alex Loiko73ec0192018-05-15 10:52:28 +0200978 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
979 RTC_NOTREACHED();
980 break;
981 case RuntimeSetting::Type::kNotSpecified:
982 RTC_NOTREACHED();
983 break;
984 }
985 }
986}
987
988void AudioProcessingImpl::HandleRenderRuntimeSettings() {
989 RuntimeSetting setting;
990 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200991 if (aec_dump_) {
992 aec_dump_->WriteRuntimeSetting(setting);
993 }
Alex Loiko73ec0192018-05-15 10:52:28 +0200994 switch (setting.type()) {
995 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
saza1d600522019-10-18 13:29:43 +0200996 if (submodules_.render_pre_processor) {
997 submodules_.render_pre_processor->SetRuntimeSetting(setting);
Alex Loiko73ec0192018-05-15 10:52:28 +0200998 }
999 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001000 case RuntimeSetting::Type::kCapturePreGain: // fall-through
1001 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001002 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001003 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001004 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001005 RTC_NOTREACHED();
1006 break;
1007 }
1008 }
1009}
1010
peah9e6a2902017-05-15 07:19:21 -07001011void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001012 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001013
1014 // Insert the samples into the queue.
saza1d600522019-10-18 13:29:43 +02001015 if (submodules_.echo_cancellation) {
Per Åhgrenf204faf2019-04-25 15:18:06 +02001016 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001017 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1018 num_reverse_channels(),
1019 &aec_render_queue_buffer_);
1020
Per Åhgrenf204faf2019-04-25 15:18:06 +02001021 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1022 // The data queue is full and needs to be emptied.
1023 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001024
Per Åhgrenf204faf2019-04-25 15:18:06 +02001025 // Retry the insert (should always work).
1026 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1027 RTC_DCHECK(result);
1028 }
peaha0624602016-10-25 04:45:24 -07001029 }
1030
saza1d600522019-10-18 13:29:43 +02001031 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001032 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1033 num_reverse_channels(),
1034 &aecm_render_queue_buffer_);
1035 RTC_DCHECK(aecm_render_signal_queue_);
1036 // Insert the samples into the queue.
1037 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1038 // The data queue is full and needs to be emptied.
1039 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001040
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001041 // Retry the insert (should always work).
1042 bool result =
1043 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1044 RTC_DCHECK(result);
1045 }
peah764e3642016-10-22 05:04:30 -07001046 }
peah701d6282016-10-25 05:42:20 -07001047
1048 if (!constants_.use_experimental_agc) {
1049 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1050 // Insert the samples into the queue.
1051 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1052 // The data queue is full and needs to be emptied.
1053 EmptyQueuedRenderAudio();
1054
1055 // Retry the insert (should always work).
1056 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1057 RTC_DCHECK(result);
1058 }
1059 }
peah9e6a2902017-05-15 07:19:21 -07001060}
ivoc9f4a4a02016-10-28 05:39:16 -07001061
peah9e6a2902017-05-15 07:19:21 -07001062void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001063 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1064
1065 // Insert the samples into the queue.
1066 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1067 // The data queue is full and needs to be emptied.
1068 EmptyQueuedRenderAudio();
1069
1070 // Retry the insert (should always work).
1071 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1072 RTC_DCHECK(result);
1073 }
peah764e3642016-10-22 05:04:30 -07001074}
1075
1076void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001077 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001078 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001079
ivoc9f4a4a02016-10-28 05:39:16 -07001080 const size_t new_red_render_queue_element_max_size =
1081 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1082
peaha0624602016-10-25 04:45:24 -07001083 // Reallocate the queues if the queue item sizes are too small to fit the
1084 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001085
1086 if (agc_render_queue_element_max_size_ <
1087 new_agc_render_queue_element_max_size) {
1088 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1089
1090 std::vector<int16_t> template_queue_element(
1091 agc_render_queue_element_max_size_);
1092
1093 agc_render_signal_queue_.reset(
1094 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1095 kMaxNumFramesToBuffer, template_queue_element,
1096 RenderQueueItemVerifier<int16_t>(
1097 agc_render_queue_element_max_size_)));
1098
1099 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1100 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1101 } else {
1102 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001103 }
ivoc9f4a4a02016-10-28 05:39:16 -07001104
1105 if (red_render_queue_element_max_size_ <
1106 new_red_render_queue_element_max_size) {
1107 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1108
1109 std::vector<float> template_queue_element(
1110 red_render_queue_element_max_size_);
1111
1112 red_render_signal_queue_.reset(
1113 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1114 kMaxNumFramesToBuffer, template_queue_element,
1115 RenderQueueItemVerifier<float>(
1116 red_render_queue_element_max_size_)));
1117
1118 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1119 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1120 } else {
1121 red_render_signal_queue_->Clear();
1122 }
peah764e3642016-10-22 05:04:30 -07001123}
1124
1125void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1126 rtc::CritScope cs_capture(&crit_capture_);
saza1d600522019-10-18 13:29:43 +02001127 if (submodules_.echo_cancellation) {
Per Åhgrenf204faf2019-04-25 15:18:06 +02001128 RTC_DCHECK(aec_render_signal_queue_);
1129 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001130 submodules_.echo_cancellation->ProcessRenderAudio(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001131 aec_capture_queue_buffer_);
1132 }
peaha0624602016-10-25 04:45:24 -07001133 }
1134
saza1d600522019-10-18 13:29:43 +02001135 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001136 RTC_DCHECK(aecm_render_signal_queue_);
1137 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001138 submodules_.echo_control_mobile->ProcessRenderAudio(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001139 aecm_capture_queue_buffer_);
1140 }
peah701d6282016-10-25 05:42:20 -07001141 }
1142
1143 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001144 submodules_.gain_control->ProcessRenderAudio(agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001145 }
ivoc9f4a4a02016-10-28 05:39:16 -07001146
1147 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001148 RTC_DCHECK(submodules_.echo_detector);
1149 submodules_.echo_detector->AnalyzeRenderAudio(red_capture_queue_buffer_);
ivoc9f4a4a02016-10-28 05:39:16 -07001150 }
peah764e3642016-10-22 05:04:30 -07001151}
1152
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001153int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001154 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001155 {
1156 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001157 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001158 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001159 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001160 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001161 }
peahfa6228e2015-11-16 16:27:42 -08001162
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001163 if (!frame) {
1164 return kNullPointerError;
1165 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001166 // Must be a native rate.
1167 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1168 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001169 frame->sample_rate_hz_ != kSampleRate32kHz &&
1170 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001171 return kBadSampleRateError;
1172 }
peah192164e2015-11-17 02:16:45 -08001173
peahdf3efa82015-11-28 12:35:15 -08001174 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001175 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001176 {
1177 // Aquire lock for the access of api_format.
1178 // The lock is released immediately due to the conditional
1179 // reinitialization.
1180 rtc::CritScope cs_capture(&crit_capture_);
1181 // TODO(ajm): The input and output rates and channels are currently
1182 // constrained to be identical in the int16 interface.
1183 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001184
1185 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001186 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001187
Oskar Sundbom4b276482019-05-23 14:28:00 +02001188 reinitialization_required =
1189 reinitialization_required ||
1190 processing_config.input_stream().sample_rate_hz() !=
1191 frame->sample_rate_hz_ ||
1192 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1193 processing_config.output_stream().sample_rate_hz() !=
1194 frame->sample_rate_hz_ ||
1195 processing_config.output_stream().num_channels() != frame->num_channels_;
1196
1197 if (reinitialization_required) {
1198 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1199 processing_config.input_stream().set_num_channels(frame->num_channels_);
1200 processing_config.output_stream().set_sample_rate_hz(
1201 frame->sample_rate_hz_);
1202 processing_config.output_stream().set_num_channels(frame->num_channels_);
1203
1204 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001205 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001206 rtc::CritScope cs_capture(&crit_capture_);
1207 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001208 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001209
peahdf3efa82015-11-28 12:35:15 -08001210 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001211 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001212 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001213 return kBadDataLengthError;
1214 }
1215
aleloi868f32f2017-05-23 07:20:05 -07001216 if (aec_dump_) {
1217 RecordUnprocessedCaptureStream(*frame);
1218 }
1219
Per Åhgrend47941e2019-08-22 11:51:13 +02001220 capture_.capture_audio->CopyFrom(frame);
Gustaf Ullberg3c918b12019-10-11 13:14:44 +02001221 if (capture_.capture_fullband_audio) {
1222 capture_.capture_fullband_audio->CopyFrom(frame);
1223 }
peahde65ddc2016-09-16 15:02:15 -07001224 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001225 if (submodule_states_.CaptureMultiBandProcessingPresent() ||
Per Åhgrena1351272019-08-15 12:15:46 +02001226 submodule_states_.CaptureFullBandProcessingActive()) {
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001227 if (capture_.capture_fullband_audio) {
1228 capture_.capture_fullband_audio->CopyTo(frame);
1229 } else {
1230 capture_.capture_audio->CopyTo(frame);
1231 }
Per Åhgrena1351272019-08-15 12:15:46 +02001232 }
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001233 if (capture_.stats.voice_detected) {
1234 frame->vad_activity_ = *capture_.stats.voice_detected
1235 ? AudioFrame::kVadActive
1236 : AudioFrame::kVadPassive;
1237 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001238
aleloi868f32f2017-05-23 07:20:05 -07001239 if (aec_dump_) {
1240 RecordProcessedCaptureStream(*frame);
1241 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001242
1243 return kNoError;
1244}
1245
peahde65ddc2016-09-16 15:02:15 -07001246int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001247 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001248
peahb58a1582016-03-15 09:34:24 -07001249 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001250 // TODO(peah): Simplify once the public API Enable functions for these
1251 // are moved to APM.
saza1d600522019-10-18 13:29:43 +02001252 RTC_DCHECK_LE(!!submodules_.echo_controller +
1253 !!submodules_.echo_cancellation +
1254 !!submodules_.echo_control_mobile,
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001255 1);
peahb58a1582016-03-15 09:34:24 -07001256
peahde65ddc2016-09-16 15:02:15 -07001257 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001258
saza1d600522019-10-18 13:29:43 +02001259 if (submodules_.pre_amplifier) {
1260 submodules_.pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001261 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001262 capture_buffer->num_frames()));
1263 }
1264
Per Åhgren928146f2019-08-20 09:19:21 +02001265 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001266 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001267 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001268 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1269 if (log_rms) {
1270 capture_rms_interval_counter_ = 0;
1271 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001272 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1273 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1274 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1275 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001276 }
1277
saza1d600522019-10-18 13:29:43 +02001278 if (submodules_.echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001279 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001280 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001281 capture_.echo_path_gain_change =
1282 capture_.prev_analog_mic_level != analog_mic_level &&
1283 capture_.prev_analog_mic_level != -1;
1284 capture_.prev_analog_mic_level = analog_mic_level;
1285
Per Åhgrend2650d12018-10-02 17:00:59 +02001286 // Detect and flag any change in the pre-amplifier gain.
saza1d600522019-10-18 13:29:43 +02001287 if (submodules_.pre_amplifier) {
1288 float pre_amp_gain = submodules_.pre_amplifier->GetGainFactor();
Per Åhgrend2650d12018-10-02 17:00:59 +02001289 capture_.echo_path_gain_change =
1290 capture_.echo_path_gain_change ||
1291 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001292 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001293 capture_.prev_pre_amp_gain = pre_amp_gain;
1294 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001295
1296 // Detect volume change.
1297 capture_.echo_path_gain_change =
1298 capture_.echo_path_gain_change ||
1299 (capture_.prev_playout_volume != capture_.playout_volume &&
1300 capture_.prev_playout_volume >= 0);
1301 capture_.prev_playout_volume = capture_.playout_volume;
1302
saza1d600522019-10-18 13:29:43 +02001303 submodules_.echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001304 }
1305
peahbe615622016-02-13 16:40:47 -08001306 if (constants_.use_experimental_agc &&
saza1d600522019-10-18 13:29:43 +02001307 submodules_.gain_control->is_enabled()) {
1308 submodules_.agc_manager->AnalyzePreProcess(
Per Åhgren928146f2019-08-20 09:19:21 +02001309 capture_buffer->channels_f()[0], capture_buffer->num_channels(),
peahde65ddc2016-09-16 15:02:15 -07001310 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001311
1312 if (constants_.use_experimental_agc_process_before_aec) {
saza1d600522019-10-18 13:29:43 +02001313 submodules_.agc_manager->Process(
Per Åhgrend47941e2019-08-22 11:51:13 +02001314 capture_buffer->channels_const()[0],
Alex Loikod9342442018-09-10 13:59:41 +02001315 capture_nonlocked_.capture_processing_format.num_frames(),
1316 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1317 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001318 }
1319
peah2ace3f92016-09-10 04:42:27 -07001320 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1321 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001322 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1323 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001324 }
1325
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001326 const bool experimental_multi_channel_capture =
1327 config_.pipeline.experimental_multi_channel &&
1328 constants_.experimental_multi_channel_capture_support;
saza1d600522019-10-18 13:29:43 +02001329 if (submodules_.echo_controller && !experimental_multi_channel_capture) {
peah522d71b2017-02-23 05:16:26 -08001330 // Force down-mixing of the number of channels after the detection of
1331 // capture signal saturation.
1332 // TODO(peah): Look into ensuring that this kind of tampering with the
1333 // AudioBuffer functionality should not be needed.
1334 capture_buffer->set_num_channels(1);
1335 }
1336
saza1d600522019-10-18 13:29:43 +02001337 if (submodules_.high_pass_filter) {
1338 submodules_.high_pass_filter->Process(capture_buffer);
peah8271d042016-11-22 07:24:52 -08001339 }
saza1d600522019-10-18 13:29:43 +02001340 RETURN_ON_ERR(submodules_.gain_control->AnalyzeCaptureAudio(capture_buffer));
1341 if (submodules_.noise_suppressor) {
1342 submodules_.noise_suppressor->AnalyzeCaptureAudio(capture_buffer);
saza0bad15f2019-10-16 11:46:11 +02001343 }
peahb58a1582016-03-15 09:34:24 -07001344
saza1d600522019-10-18 13:29:43 +02001345 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001346 // Ensure that the stream delay was set before the call to the
1347 // AECM ProcessCaptureAudio function.
1348 if (!was_stream_delay_set()) {
1349 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001350 }
1351
saza1d600522019-10-18 13:29:43 +02001352 if (submodules_.noise_suppressor) {
1353 submodules_.echo_control_mobile->CopyLowPassReference(capture_buffer);
1354 submodules_.noise_suppressor->ProcessCaptureAudio(capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001355 }
peahe0eae3c2016-12-14 01:16:23 -08001356
saza1d600522019-10-18 13:29:43 +02001357 RETURN_ON_ERR(submodules_.echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001358 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001359 } else {
saza1d600522019-10-18 13:29:43 +02001360 if (submodules_.echo_controller) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001361 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1362
1363 if (was_stream_delay_set()) {
saza1d600522019-10-18 13:29:43 +02001364 submodules_.echo_controller->SetAudioBufferDelay(stream_delay_ms());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001365 }
1366
saza1d600522019-10-18 13:29:43 +02001367 submodules_.echo_controller->ProcessCapture(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001368 capture_buffer, capture_.echo_path_gain_change);
saza1d600522019-10-18 13:29:43 +02001369 } else if (submodules_.echo_cancellation) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001370 // Ensure that the stream delay was set before the call to the
1371 // AEC ProcessCaptureAudio function.
1372 if (!was_stream_delay_set()) {
1373 return AudioProcessing::kStreamParameterNotSetError;
1374 }
1375
saza1d600522019-10-18 13:29:43 +02001376 RETURN_ON_ERR(submodules_.echo_cancellation->ProcessCaptureAudio(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001377 capture_buffer, stream_delay_ms()));
1378 }
1379
saza1d600522019-10-18 13:29:43 +02001380 if (submodules_.noise_suppressor) {
1381 submodules_.noise_suppressor->ProcessCaptureAudio(capture_buffer);
saza0bad15f2019-10-16 11:46:11 +02001382 }
Per Åhgren46537a32017-06-07 10:08:10 +02001383 }
ivoc9f4a4a02016-10-28 05:39:16 -07001384
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001385 if (config_.voice_detection.enabled) {
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001386 capture_.stats.voice_detected =
saza1d600522019-10-18 13:29:43 +02001387 submodules_.voice_detector->ProcessCaptureAudio(capture_buffer);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001388 } else {
1389 capture_.stats.voice_detected = absl::nullopt;
1390 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001391
peahbe615622016-02-13 16:40:47 -08001392 if (constants_.use_experimental_agc &&
saza1d600522019-10-18 13:29:43 +02001393 submodules_.gain_control->is_enabled() &&
Alex Loikod9342442018-09-10 13:59:41 +02001394 !constants_.use_experimental_agc_process_before_aec) {
saza1d600522019-10-18 13:29:43 +02001395 submodules_.agc_manager->Process(
Per Åhgren928146f2019-08-20 09:19:21 +02001396 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
peahde65ddc2016-09-16 15:02:15 -07001397 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001398 }
Per Åhgren200feba2019-03-06 04:16:46 +01001399 // TODO(peah): Add reporting from AEC3 whether there is echo.
saza1d600522019-10-18 13:29:43 +02001400 RETURN_ON_ERR(submodules_.gain_control->ProcessCaptureAudio(
1401 capture_buffer, submodules_.echo_cancellation &&
1402 submodules_.echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001403
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001404 if (submodule_states_.CaptureMultiBandProcessingPresent() &&
peah2ace3f92016-09-10 04:42:27 -07001405 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001406 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1407 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001408 }
1409
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001410 if (capture_.capture_fullband_audio) {
saza1d600522019-10-18 13:29:43 +02001411 const auto& ec = submodules_.echo_controller;
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001412 bool ec_active = ec ? ec->ActiveProcessing() : false;
1413 // Only update the fullband buffer if the multiband processing has changed
1414 // the signal. Keep the original signal otherwise.
1415 if (submodule_states_.CaptureMultiBandProcessingActive(ec_active)) {
1416 capture_buffer->CopyTo(capture_.capture_fullband_audio.get());
1417 }
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001418 capture_buffer = capture_.capture_fullband_audio.get();
1419 }
1420
peah9e6a2902017-05-15 07:19:21 -07001421 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001422 RTC_DCHECK(submodules_.echo_detector);
1423 submodules_.echo_detector->AnalyzeCaptureAudio(rtc::ArrayView<const float>(
1424 capture_buffer->channels()[0], capture_buffer->num_frames()));
peah9e6a2902017-05-15 07:19:21 -07001425 }
1426
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001427 // TODO(aluebs): Investigate if the transient suppression placement should be
1428 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001429 if (capture_.transient_suppressor_enabled) {
saza1d600522019-10-18 13:29:43 +02001430 float voice_probability = submodules_.agc_manager.get()
1431 ? submodules_.agc_manager->voice_probability()
1432 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001433
saza1d600522019-10-18 13:29:43 +02001434 submodules_.transient_suppressor->Suppress(
Per Åhgrend47941e2019-08-22 11:51:13 +02001435 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001436 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001437 capture_buffer->split_bands_const(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001438 capture_buffer->num_frames_per_band(),
1439 capture_.keyboard_info.keyboard_data,
1440 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001441 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001442 }
1443
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001444 // Experimental APM sub-module that analyzes |capture_buffer|.
saza1d600522019-10-18 13:29:43 +02001445 if (submodules_.capture_analyzer) {
1446 submodules_.capture_analyzer->Analyze(capture_buffer);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001447 }
1448
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001449 if (config_.gain_controller2.enabled) {
saza1d600522019-10-18 13:29:43 +02001450 submodules_.gain_controller2->NotifyAnalogLevel(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001451 agc1()->stream_analog_level());
saza1d600522019-10-18 13:29:43 +02001452 submodules_.gain_controller2->Process(capture_buffer);
alessiob3ec96df2017-05-22 06:57:06 -07001453 }
1454
saza1d600522019-10-18 13:29:43 +02001455 if (submodules_.capture_post_processor) {
1456 submodules_.capture_post_processor->Process(capture_buffer);
Sam Zackrisson0beac582017-09-25 12:04:02 +02001457 }
1458
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001459 // The level estimator operates on the recombined data.
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001460 if (config_.level_estimation.enabled) {
saza1d600522019-10-18 13:29:43 +02001461 submodules_.output_level_estimator->ProcessStream(*capture_buffer);
1462 capture_.stats.output_rms_dbfs = submodules_.output_level_estimator->RMS();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001463 } else {
1464 capture_.stats.output_rms_dbfs = absl::nullopt;
1465 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001466
Per Åhgren928146f2019-08-20 09:19:21 +02001467 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001468 capture_buffer->channels_const()[0],
peah1b08dc32016-12-20 13:45:58 -08001469 capture_nonlocked_.capture_processing_format.num_frames()));
1470 if (log_rms) {
1471 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1472 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1473 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1474 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1475 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1476 }
1477
peahdf3efa82015-11-28 12:35:15 -08001478 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001479 return kNoError;
1480}
1481
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001482int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001483 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001484 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001485 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001486 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001487 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001488 const StreamConfig reverse_config = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001489 sample_rate_hz,
1490 ChannelsFromLayout(layout),
1491 LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001492 };
1493 if (samples_per_channel != reverse_config.num_frames()) {
1494 return kBadDataLengthError;
1495 }
peahdf3efa82015-11-28 12:35:15 -08001496 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001497}
1498
peahde65ddc2016-09-16 15:02:15 -07001499int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1500 const StreamConfig& input_config,
1501 const StreamConfig& output_config,
1502 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001503 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001504 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001505 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001506 if (submodule_states_.RenderMultiBandProcessingActive() ||
1507 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001508 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1509 dest);
peah2ace3f92016-09-10 04:42:27 -07001510 } else if (formats_.api_format.reverse_input_stream() !=
1511 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001512 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1513 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001514 } else {
peahde65ddc2016-09-16 15:02:15 -07001515 CopyAudioIfNeeded(src, input_config.num_frames(),
1516 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001517 }
1518
1519 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001520}
1521
peahdf3efa82015-11-28 12:35:15 -08001522int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001523 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001524 const StreamConfig& input_config,
1525 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001526 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001527 return kNullPointerError;
1528 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001529
peahde65ddc2016-09-16 15:02:15 -07001530 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001531 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001532 }
1533
peahdf3efa82015-11-28 12:35:15 -08001534 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001535 processing_config.reverse_input_stream() = input_config;
1536 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001537
peahdf3efa82015-11-28 12:35:15 -08001538 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001539 RTC_DCHECK_EQ(input_config.num_frames(),
1540 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001541
aleloi868f32f2017-05-23 07:20:05 -07001542 if (aec_dump_) {
1543 const size_t channel_size =
1544 formats_.api_format.reverse_input_stream().num_frames();
1545 const size_t num_channels =
1546 formats_.api_format.reverse_input_stream().num_channels();
1547 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001548 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001549 }
peahdf3efa82015-11-28 12:35:15 -08001550 render_.render_audio->CopyFrom(src,
1551 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001552 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001553}
1554
1555int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001556 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001557 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001558 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001559 return kNullPointerError;
1560 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001561 // Must be a native rate.
1562 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1563 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001564 frame->sample_rate_hz_ != kSampleRate32kHz &&
1565 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001566 return kBadSampleRateError;
1567 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001568
Michael Graczyk86c6d332015-07-23 11:41:39 -07001569 if (frame->num_channels_ <= 0) {
1570 return kBadNumberChannelsError;
1571 }
1572
peahdf3efa82015-11-28 12:35:15 -08001573 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001574 processing_config.reverse_input_stream().set_sample_rate_hz(
1575 frame->sample_rate_hz_);
1576 processing_config.reverse_input_stream().set_num_channels(
1577 frame->num_channels_);
1578 processing_config.reverse_output_stream().set_sample_rate_hz(
1579 frame->sample_rate_hz_);
1580 processing_config.reverse_output_stream().set_num_channels(
1581 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001582
peahdf3efa82015-11-28 12:35:15 -08001583 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001584 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001585 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001586 return kBadDataLengthError;
1587 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001588
aleloi868f32f2017-05-23 07:20:05 -07001589 if (aec_dump_) {
1590 aec_dump_->WriteRenderStreamMessage(*frame);
1591 }
1592
Per Åhgrend47941e2019-08-22 11:51:13 +02001593 render_.render_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001594 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001595 if (submodule_states_.RenderMultiBandProcessingActive() ||
1596 submodule_states_.RenderFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001597 render_.render_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001598 }
aluebsb0319552016-03-17 20:39:53 -07001599 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001600}
niklase@google.com470e71d2011-07-07 08:21:25 +00001601
peahde65ddc2016-09-16 15:02:15 -07001602int AudioProcessingImpl::ProcessRenderStreamLocked() {
1603 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001604
Alex Loiko73ec0192018-05-15 10:52:28 +02001605 HandleRenderRuntimeSettings();
1606
saza1d600522019-10-18 13:29:43 +02001607 if (submodules_.render_pre_processor) {
1608 submodules_.render_pre_processor->Process(render_buffer);
Alex Loiko5825aa62017-12-18 16:02:40 +01001609 }
1610
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001611 QueueNonbandedRenderAudio(render_buffer);
1612
peah2ace3f92016-09-10 04:42:27 -07001613 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001614 SampleRateSupportsMultiBand(
1615 formats_.render_processing_format.sample_rate_hz())) {
1616 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001617 }
1618
peahce4d9152017-05-19 01:28:05 -07001619 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1620 QueueBandedRenderAudio(render_buffer);
1621 }
1622
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001623 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
saza1d600522019-10-18 13:29:43 +02001624 if (submodules_.echo_controller) {
1625 submodules_.echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001626 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001627
peah2ace3f92016-09-10 04:42:27 -07001628 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001629 SampleRateSupportsMultiBand(
1630 formats_.render_processing_format.sample_rate_hz())) {
1631 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001632 }
1633
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001634 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001635}
1636
1637int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001638 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001639 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001640 capture_.was_stream_delay_set = true;
1641 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001642
niklase@google.com470e71d2011-07-07 08:21:25 +00001643 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001644 delay = 0;
1645 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001646 }
1647
1648 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1649 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001650 delay = 500;
1651 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001652 }
1653
peahdf3efa82015-11-28 12:35:15 -08001654 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001655 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001656}
1657
1658int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001659 // Used as callback from submodules, hence locking is not allowed.
1660 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001661}
1662
1663bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001664 // Used as callback from submodules, hence locking is not allowed.
1665 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001666}
1667
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001668void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001669 rtc::CritScope cs(&crit_capture_);
1670 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001671}
1672
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001673void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001674 rtc::CritScope cs(&crit_capture_);
1675 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001676}
1677
1678int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001679 rtc::CritScope cs(&crit_capture_);
1680 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001681}
1682
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001683void AudioProcessingImpl::set_stream_analog_level(int level) {
1684 rtc::CritScope cs_capture(&crit_capture_);
1685 int error = agc1()->set_stream_analog_level(level);
1686 RTC_DCHECK_EQ(kNoError, error);
1687}
1688
1689int AudioProcessingImpl::recommended_stream_analog_level() const {
1690 rtc::CritScope cs_capture(&crit_capture_);
1691 return agc1()->stream_analog_level();
1692}
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;
1736 EchoCancellationImpl::Metrics metrics;
saza1d600522019-10-18 13:29:43 +02001737 if (submodules_.echo_controller) {
1738 auto ec_metrics = submodules_.echo_controller->GetMetrics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001739 stats.echo_return_loss = ec_metrics.echo_return_loss;
1740 stats.echo_return_loss_enhancement =
1741 ec_metrics.echo_return_loss_enhancement;
1742 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001743 }
1744 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001745 RTC_DCHECK(submodules_.echo_detector);
1746 auto ed_metrics = submodules_.echo_detector->GetMetrics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001747 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1748 stats.residual_echo_likelihood_recent_max =
1749 ed_metrics.echo_likelihood_recent_max;
1750 }
Ivo Creusenae026092017-11-20 13:07:16 +01001751 return stats;
1752}
1753
peah8271d042016-11-22 07:24:52 -08001754void AudioProcessingImpl::MutateConfig(
1755 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1756 rtc::CritScope cs_render(&crit_render_);
1757 rtc::CritScope cs_capture(&crit_capture_);
1758 mutator(&config_);
1759 ApplyConfig(config_);
1760}
1761
1762AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1763 rtc::CritScope cs_render(&crit_render_);
1764 rtc::CritScope cs_capture(&crit_capture_);
1765 return config_;
1766}
1767
peah2ace3f92016-09-10 04:42:27 -07001768bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1769 return submodule_states_.Update(
saza1d600522019-10-18 13:29:43 +02001770 config_.high_pass_filter.enabled, !!submodules_.echo_cancellation,
1771 !!submodules_.echo_control_mobile, config_.residual_echo_detector.enabled,
1772 !!submodules_.noise_suppressor, submodules_.gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001773 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001774 capture_nonlocked_.echo_controller_enabled,
saza0bad15f2019-10-16 11:46:11 +02001775 config_.voice_detection.enabled, capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001776}
1777
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001778void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001779 if (capture_.transient_suppressor_enabled) {
saza1d600522019-10-18 13:29:43 +02001780 if (!submodules_.transient_suppressor.get()) {
1781 submodules_.transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001782 }
saza1d600522019-10-18 13:29:43 +02001783 submodules_.transient_suppressor->Initialize(proc_fullband_sample_rate_hz(),
1784 capture_nonlocked_.split_rate,
1785 num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001786 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001787}
1788
Per Åhgren0aefbf02019-08-23 21:29:17 +02001789void AudioProcessingImpl::InitializeHighPassFilter() {
1790 if (submodule_states_.HighPassFilteringRequired()) {
saza1d600522019-10-18 13:29:43 +02001791 submodules_.high_pass_filter.reset(new HighPassFilter(num_proc_channels()));
peah8271d042016-11-22 07:24:52 -08001792 } else {
saza1d600522019-10-18 13:29:43 +02001793 submodules_.high_pass_filter.reset();
peah8271d042016-11-22 07:24:52 -08001794 }
1795}
alessiob3ec96df2017-05-22 06:57:06 -07001796
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001797void AudioProcessingImpl::InitializeVoiceDetector() {
1798 if (config_.voice_detection.enabled) {
saza1d600522019-10-18 13:29:43 +02001799 submodules_.voice_detector = std::make_unique<VoiceDetection>(
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001800 proc_split_sample_rate_hz(), VoiceDetection::kVeryLowLikelihood);
1801 } else {
saza1d600522019-10-18 13:29:43 +02001802 submodules_.voice_detector.reset();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001803 }
1804}
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001805void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001806 bool use_echo_controller =
1807 echo_control_factory_ ||
Per Åhgren200feba2019-03-06 04:16:46 +01001808 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode &&
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001809 !config_.echo_canceller.use_legacy_aec);
1810
1811 if (use_echo_controller) {
1812 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001813 if (echo_control_factory_) {
saza1d600522019-10-18 13:29:43 +02001814 submodules_.echo_controller =
Per Åhgren200feba2019-03-06 04:16:46 +01001815 echo_control_factory_->Create(proc_sample_rate_hz());
1816 } else {
saza1d600522019-10-18 13:29:43 +02001817 submodules_.echo_controller = std::make_unique<EchoCanceller3>(
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001818 EchoCanceller3Config(), proc_sample_rate_hz(), num_reverse_channels(),
1819 num_proc_channels());
Per Åhgren200feba2019-03-06 04:16:46 +01001820 }
1821
1822 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001823
saza1d600522019-10-18 13:29:43 +02001824 submodules_.echo_cancellation.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001825 aec_render_signal_queue_.reset();
saza1d600522019-10-18 13:29:43 +02001826 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001827 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001828 return;
peahe0eae3c2016-12-14 01:16:23 -08001829 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001830
saza1d600522019-10-18 13:29:43 +02001831 submodules_.echo_controller.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001832 capture_nonlocked_.echo_controller_enabled = false;
1833
1834 if (!config_.echo_canceller.enabled) {
saza1d600522019-10-18 13:29:43 +02001835 submodules_.echo_cancellation.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001836 aec_render_signal_queue_.reset();
saza1d600522019-10-18 13:29:43 +02001837 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001838 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001839 return;
1840 }
1841
1842 if (config_.echo_canceller.mobile_mode) {
1843 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001844 size_t max_element_size =
1845 std::max(static_cast<size_t>(1),
1846 kMaxAllowedValuesOfSamplesPerBand *
1847 EchoControlMobileImpl::NumCancellersRequired(
1848 num_output_channels(), num_reverse_channels()));
1849
1850 std::vector<int16_t> template_queue_element(max_element_size);
1851
1852 aecm_render_signal_queue_.reset(
1853 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1854 kMaxNumFramesToBuffer, template_queue_element,
1855 RenderQueueItemVerifier<int16_t>(max_element_size)));
1856
1857 aecm_render_queue_buffer_.resize(max_element_size);
1858 aecm_capture_queue_buffer_.resize(max_element_size);
1859
saza1d600522019-10-18 13:29:43 +02001860 submodules_.echo_control_mobile.reset(new EchoControlMobileImpl());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001861
saza1d600522019-10-18 13:29:43 +02001862 submodules_.echo_control_mobile->Initialize(proc_split_sample_rate_hz(),
1863 num_reverse_channels(),
1864 num_output_channels());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001865
saza1d600522019-10-18 13:29:43 +02001866 submodules_.echo_cancellation.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001867 aec_render_signal_queue_.reset();
1868 return;
1869 }
1870
saza1d600522019-10-18 13:29:43 +02001871 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001872 aecm_render_signal_queue_.reset();
1873
Per Åhgrenf204faf2019-04-25 15:18:06 +02001874 // Create and activate AEC2.
saza1d600522019-10-18 13:29:43 +02001875 submodules_.echo_cancellation.reset(new EchoCancellationImpl());
1876 submodules_.echo_cancellation->SetExtraOptions(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001877 capture_nonlocked_.use_aec2_extended_filter,
1878 capture_nonlocked_.use_aec2_delay_agnostic,
1879 capture_nonlocked_.use_aec2_refined_adaptive_filter);
1880
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001881 size_t element_max_size =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001882 std::max(static_cast<size_t>(1),
1883 kMaxAllowedValuesOfSamplesPerBand *
1884 EchoCancellationImpl::NumCancellersRequired(
1885 num_output_channels(), num_reverse_channels()));
1886
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001887 std::vector<float> template_queue_element(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001888
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001889 aec_render_signal_queue_.reset(
1890 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1891 kMaxNumFramesToBuffer, template_queue_element,
1892 RenderQueueItemVerifier<float>(element_max_size)));
Per Åhgrenf204faf2019-04-25 15:18:06 +02001893
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001894 aec_render_queue_buffer_.resize(element_max_size);
1895 aec_capture_queue_buffer_.resize(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001896
saza1d600522019-10-18 13:29:43 +02001897 submodules_.echo_cancellation->Initialize(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001898 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
1899 num_proc_channels());
1900
saza1d600522019-10-18 13:29:43 +02001901 submodules_.echo_cancellation->set_suppression_level(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001902 config_.echo_canceller.legacy_moderate_suppression_level
1903 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
1904 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
peahe0eae3c2016-12-14 01:16:23 -08001905}
peah8271d042016-11-22 07:24:52 -08001906
alessiob3ec96df2017-05-22 06:57:06 -07001907void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001908 if (config_.gain_controller2.enabled) {
saza1d600522019-10-18 13:29:43 +02001909 submodules_.gain_controller2->Initialize(proc_fullband_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001910 }
1911}
1912
saza0bad15f2019-10-16 11:46:11 +02001913void AudioProcessingImpl::InitializeNoiseSuppressor() {
1914 if (config_.noise_suppression.enabled) {
1915 auto ns_level =
1916 NsConfigLevelToInterfaceLevel(config_.noise_suppression.level);
saza1d600522019-10-18 13:29:43 +02001917 submodules_.noise_suppressor = std::make_unique<NoiseSuppression>(
saza0bad15f2019-10-16 11:46:11 +02001918 num_proc_channels(), proc_sample_rate_hz(), ns_level);
1919 } else {
saza1d600522019-10-18 13:29:43 +02001920 submodules_.noise_suppressor.reset();
saza0bad15f2019-10-16 11:46:11 +02001921 }
1922}
1923
Alex Loikob5c9a792018-04-16 16:31:22 +02001924void AudioProcessingImpl::InitializePreAmplifier() {
1925 if (config_.pre_amplifier.enabled) {
saza1d600522019-10-18 13:29:43 +02001926 submodules_.pre_amplifier.reset(
Alex Loikob5c9a792018-04-16 16:31:22 +02001927 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1928 } else {
saza1d600522019-10-18 13:29:43 +02001929 submodules_.pre_amplifier.reset();
Alex Loikob5c9a792018-04-16 16:31:22 +02001930 }
1931}
1932
ivoc9f4a4a02016-10-28 05:39:16 -07001933void AudioProcessingImpl::InitializeResidualEchoDetector() {
saza1d600522019-10-18 13:29:43 +02001934 RTC_DCHECK(submodules_.echo_detector);
1935 submodules_.echo_detector->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001936 proc_fullband_sample_rate_hz(), 1,
Ivo Creusenb1facc12018-04-12 16:15:58 +02001937 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001938}
1939
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001940void AudioProcessingImpl::InitializeAnalyzer() {
saza1d600522019-10-18 13:29:43 +02001941 if (submodules_.capture_analyzer) {
1942 submodules_.capture_analyzer->Initialize(proc_fullband_sample_rate_hz(),
1943 num_proc_channels());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001944 }
1945}
1946
Sam Zackrisson0beac582017-09-25 12:04:02 +02001947void AudioProcessingImpl::InitializePostProcessor() {
saza1d600522019-10-18 13:29:43 +02001948 if (submodules_.capture_post_processor) {
1949 submodules_.capture_post_processor->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001950 proc_fullband_sample_rate_hz(), num_proc_channels());
Sam Zackrisson0beac582017-09-25 12:04:02 +02001951 }
1952}
1953
Alex Loiko5825aa62017-12-18 16:02:40 +01001954void AudioProcessingImpl::InitializePreProcessor() {
saza1d600522019-10-18 13:29:43 +02001955 if (submodules_.render_pre_processor) {
1956 submodules_.render_pre_processor->Initialize(
Alex Loiko5825aa62017-12-18 16:02:40 +01001957 formats_.render_processing_format.sample_rate_hz(),
1958 formats_.render_processing_format.num_channels());
1959 }
1960}
1961
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001962void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001963
aleloi868f32f2017-05-23 07:20:05 -07001964void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1965 if (!aec_dump_) {
1966 return;
1967 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001968
1969 std::string experiments_description = "";
saza1d600522019-10-18 13:29:43 +02001970 if (submodules_.echo_cancellation) {
Per Åhgrenf204faf2019-04-25 15:18:06 +02001971 experiments_description +=
saza1d600522019-10-18 13:29:43 +02001972 submodules_.echo_cancellation->GetExperimentsDescription();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001973 }
aleloi868f32f2017-05-23 07:20:05 -07001974 // TODO(peah): Add semicolon-separated concatenations of experiment
1975 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001976 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1977 experiments_description += "AgcClippingLevelExperiment;";
1978 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001979 if (capture_nonlocked_.echo_controller_enabled) {
1980 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001981 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001982 if (config_.gain_controller2.enabled) {
1983 experiments_description += "GainController2;";
1984 }
aleloi868f32f2017-05-23 07:20:05 -07001985
1986 InternalAPMConfig apm_config;
1987
Per Åhgren200feba2019-03-06 04:16:46 +01001988 apm_config.aec_enabled = config_.echo_canceller.enabled;
aleloi868f32f2017-05-23 07:20:05 -07001989 apm_config.aec_delay_agnostic_enabled =
saza1d600522019-10-18 13:29:43 +02001990 submodules_.echo_cancellation &&
1991 submodules_.echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07001992 apm_config.aec_drift_compensation_enabled =
saza1d600522019-10-18 13:29:43 +02001993 submodules_.echo_cancellation &&
1994 submodules_.echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07001995 apm_config.aec_extended_filter_enabled =
saza1d600522019-10-18 13:29:43 +02001996 submodules_.echo_cancellation &&
1997 submodules_.echo_cancellation->is_extended_filter_enabled();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001998 apm_config.aec_suppression_level =
saza1d600522019-10-18 13:29:43 +02001999 submodules_.echo_cancellation
2000 ? static_cast<int>(submodules_.echo_cancellation->suppression_level())
Per Åhgrenf204faf2019-04-25 15:18:06 +02002001 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002002
saza1d600522019-10-18 13:29:43 +02002003 apm_config.aecm_enabled = !!submodules_.echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002004 apm_config.aecm_comfort_noise_enabled =
saza1d600522019-10-18 13:29:43 +02002005 submodules_.echo_control_mobile &&
2006 submodules_.echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002007 apm_config.aecm_routing_mode =
saza1d600522019-10-18 13:29:43 +02002008 submodules_.echo_control_mobile
2009 ? static_cast<int>(submodules_.echo_control_mobile->routing_mode())
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002010 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002011
saza1d600522019-10-18 13:29:43 +02002012 apm_config.agc_enabled = submodules_.gain_control->is_enabled();
2013 apm_config.agc_mode = static_cast<int>(submodules_.gain_control->mode());
aleloi868f32f2017-05-23 07:20:05 -07002014 apm_config.agc_limiter_enabled =
saza1d600522019-10-18 13:29:43 +02002015 submodules_.gain_control->is_limiter_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002016 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2017
2018 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2019
saza0bad15f2019-10-16 11:46:11 +02002020 apm_config.ns_enabled = config_.noise_suppression.enabled;
2021 apm_config.ns_level = static_cast<int>(config_.noise_suppression.level);
aleloi868f32f2017-05-23 07:20:05 -07002022
2023 apm_config.transient_suppression_enabled =
2024 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002025 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002026 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2027 apm_config.pre_amplifier_fixed_gain_factor =
2028 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002029
2030 if (!forced && apm_config == apm_config_for_aec_dump_) {
2031 return;
2032 }
2033 aec_dump_->WriteConfig(apm_config);
2034 apm_config_for_aec_dump_ = apm_config;
2035}
2036
2037void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2038 const float* const* src) {
2039 RTC_DCHECK(aec_dump_);
2040 WriteAecDumpConfigMessage(false);
2041
2042 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2043 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2044 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002045 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002046 RecordAudioProcessingState();
2047}
2048
2049void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2050 const AudioFrame& capture_frame) {
2051 RTC_DCHECK(aec_dump_);
2052 WriteAecDumpConfigMessage(false);
2053
2054 aec_dump_->AddCaptureStreamInput(capture_frame);
2055 RecordAudioProcessingState();
2056}
2057
2058void AudioProcessingImpl::RecordProcessedCaptureStream(
2059 const float* const* processed_capture_stream) {
2060 RTC_DCHECK(aec_dump_);
2061
2062 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2063 const size_t num_channels =
2064 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002065 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2066 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002067 aec_dump_->WriteCaptureStreamMessage();
2068}
2069
2070void AudioProcessingImpl::RecordProcessedCaptureStream(
2071 const AudioFrame& processed_capture_frame) {
2072 RTC_DCHECK(aec_dump_);
2073
2074 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2075 aec_dump_->WriteCaptureStreamMessage();
2076}
2077
2078void AudioProcessingImpl::RecordAudioProcessingState() {
2079 RTC_DCHECK(aec_dump_);
2080 AecDump::AudioProcessingState audio_proc_state;
2081 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2082 audio_proc_state.drift =
saza1d600522019-10-18 13:29:43 +02002083 submodules_.echo_cancellation
2084 ? submodules_.echo_cancellation->stream_drift_samples()
Per Åhgrenf204faf2019-04-25 15:18:06 +02002085 : 0;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01002086 audio_proc_state.level = agc1()->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