blob: c4ef3b2f59f2d5425041a0b9d739b91c261dba4f [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 Zackrisson72cc71c2019-10-21 12:54:02 +0200344 RTC_LOG(LS_INFO) << "Injected APM submodules:"
345 << "\nEcho control factory: " << !!echo_control_factory_
346 << "\nEcho detector: " << !!submodules_.echo_detector
347 << "\nCapture analyzer: " << !!submodules_.capture_analyzer
348 << "\nCapture post processor: "
349 << !!submodules_.capture_post_processor
350 << "\nRender pre processor: "
351 << !!submodules_.render_pre_processor;
352
Sam Zackrisson421c8592019-02-11 13:39:46 +0100353 // Mark Echo Controller enabled if a factory is injected.
354 capture_nonlocked_.echo_controller_enabled =
355 static_cast<bool>(echo_control_factory_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000356
saza1d600522019-10-18 13:29:43 +0200357 submodules_.gain_control.reset(new GainControlImpl());
358 submodules_.gain_control_for_experimental_agc.reset(
359 new GainControlForExperimentalAgc(submodules_.gain_control.get()));
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200360
Sam Zackrisson421c8592019-02-11 13:39:46 +0100361 // If no echo detector is injected, use the ResidualEchoDetector.
saza1d600522019-10-18 13:29:43 +0200362 if (!submodules_.echo_detector) {
363 submodules_.echo_detector =
Sam Zackrisson421c8592019-02-11 13:39:46 +0100364 new rtc::RefCountedObject<ResidualEchoDetector>();
peahdf3efa82015-11-28 12:35:15 -0800365 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000366
Sam Zackrisson421c8592019-02-11 13:39:46 +0100367 // TODO(alessiob): Move the injected gain controller once injection is
368 // implemented.
saza1d600522019-10-18 13:29:43 +0200369 submodules_.gain_controller2.reset(new GainController2());
Sam Zackrisson421c8592019-02-11 13:39:46 +0100370
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000371 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000372}
373
374AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800375 // Depends on gain_control_ and
saza1d600522019-10-18 13:29:43 +0200376 // submodules_.gain_control_for_experimental_agc.
377 submodules_.agc_manager.reset();
peahdf3efa82015-11-28 12:35:15 -0800378 // Depends on gain_control_.
saza1d600522019-10-18 13:29:43 +0200379 submodules_.gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000380}
381
niklase@google.com470e71d2011-07-07 08:21:25 +0000382int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800383 // Run in a single-threaded manner during initialization.
384 rtc::CritScope cs_render(&crit_render_);
385 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000386 return InitializeLocked();
387}
388
peahde65ddc2016-09-16 15:02:15 -0700389int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
390 int capture_output_sample_rate_hz,
391 int render_input_sample_rate_hz,
392 ChannelLayout capture_input_layout,
393 ChannelLayout capture_output_layout,
394 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700395 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700396 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
397 LayoutHasKeyboard(capture_input_layout)},
398 {capture_output_sample_rate_hz,
399 ChannelsFromLayout(capture_output_layout),
400 LayoutHasKeyboard(capture_output_layout)},
401 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
402 LayoutHasKeyboard(render_input_layout)},
403 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
404 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700405
406 return Initialize(processing_config);
407}
408
409int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800410 // Run in a single-threaded manner during initialization.
411 rtc::CritScope cs_render(&crit_render_);
412 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700413 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000414}
415
peahdf3efa82015-11-28 12:35:15 -0800416int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800417 const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800418 // Called from both threads. Thread check is therefore not possible.
Oskar Sundbom4b276482019-05-23 14:28:00 +0200419 if (processing_config == formats_.api_format) {
peah192164e2015-11-17 02:16:45 -0800420 return kNoError;
421 }
peahdf3efa82015-11-28 12:35:15 -0800422
423 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800424 return InitializeLocked(processing_config);
425}
426
niklase@google.com470e71d2011-07-07 08:21:25 +0000427int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200428 UpdateActiveSubmoduleStates();
429
Per Åhgrend47941e2019-08-22 11:51:13 +0200430 const int render_audiobuffer_sample_rate_hz =
peahdf3efa82015-11-28 12:35:15 -0800431 formats_.api_format.reverse_output_stream().num_frames() == 0
Per Åhgrend47941e2019-08-22 11:51:13 +0200432 ? formats_.render_processing_format.sample_rate_hz()
433 : formats_.api_format.reverse_output_stream().sample_rate_hz();
peahdf3efa82015-11-28 12:35:15 -0800434 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
435 render_.render_audio.reset(new AudioBuffer(
Per Åhgrend47941e2019-08-22 11:51:13 +0200436 formats_.api_format.reverse_input_stream().sample_rate_hz(),
peahdf3efa82015-11-28 12:35:15 -0800437 formats_.api_format.reverse_input_stream().num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200438 formats_.render_processing_format.sample_rate_hz(),
peahde65ddc2016-09-16 15:02:15 -0700439 formats_.render_processing_format.num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200440 render_audiobuffer_sample_rate_hz,
441 formats_.render_processing_format.num_channels()));
peah2ace3f92016-09-10 04:42:27 -0700442 if (formats_.api_format.reverse_input_stream() !=
443 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800444 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800445 formats_.api_format.reverse_input_stream().num_channels(),
446 formats_.api_format.reverse_input_stream().num_frames(),
447 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800448 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700449 } else {
peahdf3efa82015-11-28 12:35:15 -0800450 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700451 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700452 } else {
peahdf3efa82015-11-28 12:35:15 -0800453 render_.render_audio.reset(nullptr);
454 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700455 }
peahce4d9152017-05-19 01:28:05 -0700456
Per Åhgrend47941e2019-08-22 11:51:13 +0200457 capture_.capture_audio.reset(new AudioBuffer(
458 formats_.api_format.input_stream().sample_rate_hz(),
459 formats_.api_format.input_stream().num_channels(),
460 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
461 formats_.api_format.output_stream().num_channels(),
462 formats_.api_format.output_stream().sample_rate_hz(),
463 formats_.api_format.output_stream().num_channels()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000464
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200465 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() <
466 formats_.api_format.output_stream().sample_rate_hz() &&
467 formats_.api_format.output_stream().sample_rate_hz() == 48000) {
468 capture_.capture_fullband_audio.reset(
469 new AudioBuffer(formats_.api_format.input_stream().sample_rate_hz(),
470 formats_.api_format.input_stream().num_channels(),
471 formats_.api_format.output_stream().sample_rate_hz(),
472 formats_.api_format.output_stream().num_channels(),
473 formats_.api_format.output_stream().sample_rate_hz(),
474 formats_.api_format.output_stream().num_channels()));
475 } else {
476 capture_.capture_fullband_audio.reset();
477 }
478
peah764e3642016-10-22 05:04:30 -0700479 AllocateRenderQueue();
480
saza1d600522019-10-18 13:29:43 +0200481 submodules_.gain_control->Initialize(num_proc_channels(),
482 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700483 if (constants_.use_experimental_agc) {
saza1d600522019-10-18 13:29:43 +0200484 if (!submodules_.agc_manager.get()) {
485 submodules_.agc_manager.reset(new AgcManagerDirect(
486 submodules_.gain_control.get(),
487 submodules_.gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200488 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
489 constants_.use_experimental_agc_agc2_level_estimation,
490 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700491 }
saza1d600522019-10-18 13:29:43 +0200492 submodules_.agc_manager->Initialize();
493 submodules_.agc_manager->SetCaptureMuted(capture_.output_will_be_muted);
494 submodules_.gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700495 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200496 InitializeTransient();
Per Åhgren0aefbf02019-08-23 21:29:17 +0200497 InitializeHighPassFilter();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200498 InitializeVoiceDetector();
ivoc9f4a4a02016-10-28 05:39:16 -0700499 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200500 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700501 InitializeGainController2();
saza0bad15f2019-10-16 11:46:11 +0200502 InitializeNoiseSuppressor();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200503 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200504 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100505 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800506
aleloi868f32f2017-05-23 07:20:05 -0700507 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200508 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700509 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000510 return kNoError;
511}
512
Michael Graczyk86c6d332015-07-23 11:41:39 -0700513int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200514 UpdateActiveSubmoduleStates();
515
Michael Graczyk86c6d332015-07-23 11:41:39 -0700516 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700517 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
518 return kBadSampleRateError;
519 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000520 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700521
Peter Kasting69558702016-01-12 16:26:35 -0800522 const size_t num_in_channels = config.input_stream().num_channels();
523 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700524
525 // Need at least one input channel.
526 // Need either one output channel or as many outputs as there are inputs.
527 if (num_in_channels == 0 ||
528 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700529 return kBadNumberChannelsError;
530 }
531
peahdf3efa82015-11-28 12:35:15 -0800532 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000533
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200534 // Choose maximum rate to use for the split filtering.
535 RTC_DCHECK(config_.pipeline.maximum_internal_processing_rate == 48000 ||
536 config_.pipeline.maximum_internal_processing_rate == 32000);
537 int max_splitting_rate = 48000;
538 if (config_.pipeline.maximum_internal_processing_rate == 32000) {
539 max_splitting_rate = config_.pipeline.maximum_internal_processing_rate;
540 }
541
Per Åhgrenc8626b62019-08-23 15:49:51 +0200542 int capture_processing_rate = SuitableProcessRate(
peah423d2362016-04-09 16:06:52 -0700543 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700544 formats_.api_format.output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200545 max_splitting_rate,
peah2ace3f92016-09-10 04:42:27 -0700546 submodule_states_.CaptureMultiBandSubModulesActive() ||
547 submodule_states_.RenderMultiBandSubModulesActive());
Per Åhgrenc8626b62019-08-23 15:49:51 +0200548 RTC_DCHECK_NE(8000, capture_processing_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000549
peahde65ddc2016-09-16 15:02:15 -0700550 capture_nonlocked_.capture_processing_format =
551 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700552
peah2ce640f2017-04-07 03:57:48 -0700553 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200554 if (!capture_nonlocked_.echo_controller_enabled) {
Per Åhgrenc8626b62019-08-23 15:49:51 +0200555 render_processing_rate = SuitableProcessRate(
peah2ce640f2017-04-07 03:57:48 -0700556 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
557 formats_.api_format.reverse_output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200558 max_splitting_rate,
peah2ce640f2017-04-07 03:57:48 -0700559 submodule_states_.CaptureMultiBandSubModulesActive() ||
560 submodule_states_.RenderMultiBandSubModulesActive());
561 } else {
562 render_processing_rate = capture_processing_rate;
563 }
564
peahde65ddc2016-09-16 15:02:15 -0700565 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700566 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700567 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
568 kSampleRate8kHz) {
569 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000570 } else {
peahde65ddc2016-09-16 15:02:15 -0700571 render_processing_rate =
572 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000573 }
574
Per Åhgrenc8626b62019-08-23 15:49:51 +0200575 RTC_DCHECK_NE(8000, render_processing_rate);
576
peahce4d9152017-05-19 01:28:05 -0700577 if (submodule_states_.RenderMultiBandSubModulesActive()) {
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200578 // By default, downmix the render stream to mono for analysis. This has been
579 // demonstrated to work well for AEC in most practical scenarios.
580 const bool experimental_multi_channel_render =
581 config_.pipeline.experimental_multi_channel &&
582 constants_.experimental_multi_channel_render_support;
583 int render_processing_num_channels =
584 experimental_multi_channel_render
585 ? formats_.api_format.reverse_input_stream().num_channels()
586 : 1;
587 formats_.render_processing_format =
588 StreamConfig(render_processing_rate, render_processing_num_channels);
peahce4d9152017-05-19 01:28:05 -0700589 } else {
590 formats_.render_processing_format = StreamConfig(
591 formats_.api_format.reverse_input_stream().sample_rate_hz(),
592 formats_.api_format.reverse_input_stream().num_channels());
593 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000594
peahde65ddc2016-09-16 15:02:15 -0700595 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
596 kSampleRate32kHz ||
597 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
598 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800599 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000600 } else {
peahdf3efa82015-11-28 12:35:15 -0800601 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700602 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000603 }
604
605 return InitializeLocked();
606}
607
peah88ac8532016-09-12 16:47:25 -0700608void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
Sam Zackrisson72cc71c2019-10-21 12:54:02 +0200609 RTC_LOG(LS_INFO) << "AudioProcessing::ApplyConfig: " << config.ToString();
610
peah88ac8532016-09-12 16:47:25 -0700611 // Run in a single-threaded manner when applying the settings.
612 rtc::CritScope cs_render(&crit_render_);
613 rtc::CritScope cs_capture(&crit_capture_);
614
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200615 const bool pipeline_config_changed =
616 config_.pipeline.experimental_multi_channel !=
617 config.pipeline.experimental_multi_channel;
618
Per Åhgren200feba2019-03-06 04:16:46 +0100619 const bool aec_config_changed =
620 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
621 config_.echo_canceller.use_legacy_aec !=
622 config.echo_canceller.use_legacy_aec ||
623 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode ||
624 (config_.echo_canceller.enabled && config.echo_canceller.use_legacy_aec &&
625 config_.echo_canceller.legacy_moderate_suppression_level !=
626 config.echo_canceller.legacy_moderate_suppression_level);
627
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100628 const bool agc1_config_changed =
629 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
630 config_.gain_controller1.mode != config.gain_controller1.mode ||
631 config_.gain_controller1.target_level_dbfs !=
632 config.gain_controller1.target_level_dbfs ||
633 config_.gain_controller1.compression_gain_db !=
634 config.gain_controller1.compression_gain_db ||
635 config_.gain_controller1.enable_limiter !=
636 config.gain_controller1.enable_limiter ||
637 config_.gain_controller1.analog_level_minimum !=
638 config.gain_controller1.analog_level_minimum ||
639 config_.gain_controller1.analog_level_maximum !=
640 config.gain_controller1.analog_level_maximum;
641
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200642 const bool voice_detection_config_changed =
643 config_.voice_detection.enabled != config.voice_detection.enabled;
644
saza0bad15f2019-10-16 11:46:11 +0200645 const bool ns_config_changed =
646 config_.noise_suppression.enabled != config.noise_suppression.enabled ||
647 config_.noise_suppression.level != config.noise_suppression.level;
648
Yves Gerey499bc6c2018-10-10 18:29:07 +0200649 config_ = config;
650
Per Åhgren200feba2019-03-06 04:16:46 +0100651 if (aec_config_changed) {
652 InitializeEchoController();
653 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200654
saza0bad15f2019-10-16 11:46:11 +0200655 if (ns_config_changed) {
656 InitializeNoiseSuppressor();
657 }
Sam Zackrisson23513132019-01-11 15:10:32 +0100658
Per Åhgren0aefbf02019-08-23 21:29:17 +0200659 InitializeHighPassFilter();
peah8271d042016-11-22 07:24:52 -0800660
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100661 if (agc1_config_changed) {
662 ApplyAgc1Config(config_.gain_controller1);
663 }
664
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100665 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700666 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100667 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
668 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100669 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100670 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700671 config_.gain_controller2 = AudioProcessing::Config::GainController2();
672 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200673 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200674 InitializePreAmplifier();
saza1d600522019-10-18 13:29:43 +0200675 submodules_.gain_controller2->ApplyConfig(config_.gain_controller2);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100676
saza1d600522019-10-18 13:29:43 +0200677 if (config_.level_estimation.enabled && !submodules_.output_level_estimator) {
678 submodules_.output_level_estimator = std::make_unique<LevelEstimator>();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100679 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100680
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200681 if (voice_detection_config_changed) {
682 InitializeVoiceDetector();
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100683 }
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200684
685 // Reinitialization must happen after all submodule configuration to avoid
686 // additional reinitializations on the next capture / render processing call.
687 if (pipeline_config_changed) {
688 InitializeLocked(formats_.api_format);
689 }
peah88ac8532016-09-12 16:47:25 -0700690}
691
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100692void AudioProcessingImpl::ApplyAgc1Config(
693 const Config::GainController1& config) {
694 GainControl* agc = agc1();
695 int error = agc->Enable(config.enabled);
696 RTC_DCHECK_EQ(kNoError, error);
697 error = agc->set_mode(Agc1ConfigModeToInterfaceMode(config.mode));
698 RTC_DCHECK_EQ(kNoError, error);
699 error = agc->set_target_level_dbfs(config.target_level_dbfs);
700 RTC_DCHECK_EQ(kNoError, error);
701 error = agc->set_compression_gain_db(config.compression_gain_db);
702 RTC_DCHECK_EQ(kNoError, error);
703 error = agc->enable_limiter(config.enable_limiter);
704 RTC_DCHECK_EQ(kNoError, error);
705 error = agc->set_analog_level_limits(config.analog_level_minimum,
706 config.analog_level_maximum);
707 RTC_DCHECK_EQ(kNoError, error);
708}
709
710GainControl* AudioProcessingImpl::agc1() {
711 if (constants_.use_experimental_agc) {
saza1d600522019-10-18 13:29:43 +0200712 return submodules_.gain_control_for_experimental_agc.get();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100713 }
saza1d600522019-10-18 13:29:43 +0200714 return submodules_.gain_control.get();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100715}
716
717const GainControl* AudioProcessingImpl::agc1() const {
718 if (constants_.use_experimental_agc) {
saza1d600522019-10-18 13:29:43 +0200719 return submodules_.gain_control_for_experimental_agc.get();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100720 }
saza1d600522019-10-18 13:29:43 +0200721 return submodules_.gain_control.get();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100722}
723
peah88ac8532016-09-12 16:47:25 -0700724void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800725 // Run in a single-threaded manner when setting the extra options.
726 rtc::CritScope cs_render(&crit_render_);
727 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000728
Per Åhgrenf204faf2019-04-25 15:18:06 +0200729 capture_nonlocked_.use_aec2_extended_filter =
730 config.Get<ExtendedFilter>().enabled;
731 capture_nonlocked_.use_aec2_delay_agnostic =
732 config.Get<DelayAgnostic>().enabled;
733 capture_nonlocked_.use_aec2_refined_adaptive_filter =
734 config.Get<RefinedAdaptiveFilter>().enabled;
peahb624d8c2016-03-05 03:01:14 -0800735
peahdf3efa82015-11-28 12:35:15 -0800736 if (capture_.transient_suppressor_enabled !=
737 config.Get<ExperimentalNs>().enabled) {
738 capture_.transient_suppressor_enabled =
739 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000740 InitializeTransient();
741 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000742}
743
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000744int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800745 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700746 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000747}
748
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200749int AudioProcessingImpl::proc_fullband_sample_rate_hz() const {
750 return capture_.capture_fullband_audio
751 ? capture_.capture_fullband_audio->num_frames() * 100
752 : capture_nonlocked_.capture_processing_format.sample_rate_hz();
753}
754
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000755int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800756 // Used as callback from submodules, hence locking is not allowed.
757 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000758}
759
Peter Kasting69558702016-01-12 16:26:35 -0800760size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800761 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700762 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000763}
764
Peter Kasting69558702016-01-12 16:26:35 -0800765size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800766 // Used as callback from submodules, hence locking is not allowed.
767 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000768}
769
Peter Kasting69558702016-01-12 16:26:35 -0800770size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800771 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200772 const bool experimental_multi_channel_capture =
773 config_.pipeline.experimental_multi_channel &&
774 constants_.experimental_multi_channel_capture_support;
775 if (capture_nonlocked_.echo_controller_enabled &&
776 !experimental_multi_channel_capture) {
777 return 1;
778 }
779 return num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800780}
781
Peter Kasting69558702016-01-12 16:26:35 -0800782size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800783 // Used as callback from submodules, hence locking is not allowed.
784 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000785}
786
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000787void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800788 rtc::CritScope cs(&crit_capture_);
789 capture_.output_will_be_muted = muted;
saza1d600522019-10-18 13:29:43 +0200790 if (submodules_.agc_manager.get()) {
791 submodules_.agc_manager->SetCaptureMuted(capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000792 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000793}
794
Alessio Bazzicac054e782018-04-16 12:10:09 +0200795void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200796 switch (setting.type()) {
797 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
798 render_runtime_settings_enqueuer_.Enqueue(setting);
799 return;
800 case RuntimeSetting::Type::kNotSpecified:
801 RTC_NOTREACHED();
802 return;
803 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100804 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200805 case RuntimeSetting::Type::kCaptureFixedPostGain:
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200806 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200807 capture_runtime_settings_enqueuer_.Enqueue(setting);
808 return;
809 }
810 // The language allows the enum to have a non-enumerator
811 // value. Check that this doesn't happen.
812 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200813}
814
815AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
816 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200817 : runtime_settings_(*runtime_settings) {
818 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200819}
820
821AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
822 default;
823
824void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
825 RuntimeSetting setting) {
826 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200827 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200828 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200829 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200830 RTC_LOG(LS_ERROR)
831 << "The runtime settings queue is full. Oldest setting discarded.";
832 }
833 if (remaining_attempts == 0)
834 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
835}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000836
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000837int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700838 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000839 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000840 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000841 int output_sample_rate_hz,
842 ChannelLayout output_layout,
843 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800844 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800845 StreamConfig input_stream;
846 StreamConfig output_stream;
847 {
848 // Access the formats_.api_format.input_stream beneath the capture lock.
849 // The lock must be released as it is later required in the call
850 // to ProcessStream(,,,);
851 rtc::CritScope cs(&crit_capture_);
852 input_stream = formats_.api_format.input_stream();
853 output_stream = formats_.api_format.output_stream();
854 }
855
Michael Graczyk86c6d332015-07-23 11:41:39 -0700856 input_stream.set_sample_rate_hz(input_sample_rate_hz);
857 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
858 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700859 output_stream.set_sample_rate_hz(output_sample_rate_hz);
860 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
861 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
862
863 if (samples_per_channel != input_stream.num_frames()) {
864 return kBadDataLengthError;
865 }
866 return ProcessStream(src, input_stream, output_stream, dest);
867}
868
869int AudioProcessingImpl::ProcessStream(const float* const* src,
870 const StreamConfig& input_config,
871 const StreamConfig& output_config,
872 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800873 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800874 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700875 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800876 {
877 // Acquire the capture lock in order to safely call the function
878 // that retrieves the render side data. This function accesses apm
879 // getters that need the capture lock held when being called.
880 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700881 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800882
883 if (!src || !dest) {
884 return kNullPointerError;
885 }
886
887 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700888 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000889 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000890
Oskar Sundbom4b276482019-05-23 14:28:00 +0200891 if (processing_config.input_stream() != input_config) {
892 processing_config.input_stream() = input_config;
893 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800894 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200895
896 if (processing_config.output_stream() != output_config) {
897 processing_config.output_stream() = output_config;
898 reinitialization_required = true;
899 }
900
901 if (reinitialization_required) {
902 // Reinitialize.
903 rtc::CritScope cs_render(&crit_render_);
904 rtc::CritScope cs_capture(&crit_capture_);
905 RETURN_ON_ERR(InitializeLocked(processing_config));
906 }
907
peahdf3efa82015-11-28 12:35:15 -0800908 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700909 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
910 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000911
aleloi868f32f2017-05-23 07:20:05 -0700912 if (aec_dump_) {
913 RecordUnprocessedCaptureStream(src);
914 }
915
Per Åhgrena1351272019-08-15 12:15:46 +0200916 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800917 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200918 if (capture_.capture_fullband_audio) {
919 capture_.capture_fullband_audio->CopyFrom(
920 src, formats_.api_format.input_stream());
921 }
peahde65ddc2016-09-16 15:02:15 -0700922 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200923 if (capture_.capture_fullband_audio) {
924 capture_.capture_fullband_audio->CopyTo(formats_.api_format.output_stream(),
925 dest);
926 } else {
927 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
928 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000929
aleloi868f32f2017-05-23 07:20:05 -0700930 if (aec_dump_) {
931 RecordProcessedCaptureStream(dest);
932 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000933 return kNoError;
934}
935
Alex Loiko73ec0192018-05-15 10:52:28 +0200936void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200937 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200938 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200939 if (aec_dump_) {
940 aec_dump_->WriteRuntimeSetting(setting);
941 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200942 switch (setting.type()) {
943 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200944 if (config_.pre_amplifier.enabled) {
945 float value;
946 setting.GetFloat(&value);
saza1d600522019-10-18 13:29:43 +0200947 submodules_.pre_amplifier->SetGainFactor(value);
Alex Loikob5c9a792018-04-16 16:31:22 +0200948 }
949 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200950 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100951 case RuntimeSetting::Type::kCaptureCompressionGain: {
952 float value;
953 setting.GetFloat(&value);
954 int int_value = static_cast<int>(value + .5f);
955 config_.gain_controller1.compression_gain_db = int_value;
956 int error = agc1()->set_compression_gain_db(int_value);
957 RTC_DCHECK_EQ(kNoError, error);
958 break;
959 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200960 case RuntimeSetting::Type::kCaptureFixedPostGain: {
961 if (config_.gain_controller2.enabled) {
962 float value;
963 setting.GetFloat(&value);
964 config_.gain_controller2.fixed_digital.gain_db = value;
saza1d600522019-10-18 13:29:43 +0200965 submodules_.gain_controller2->ApplyConfig(config_.gain_controller2);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200966 }
967 break;
968 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200969 case RuntimeSetting::Type::kPlayoutVolumeChange: {
970 int value;
971 setting.GetInt(&value);
972 capture_.playout_volume = value;
973 break;
974 }
Alex Loiko73ec0192018-05-15 10:52:28 +0200975 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
976 RTC_NOTREACHED();
977 break;
978 case RuntimeSetting::Type::kNotSpecified:
979 RTC_NOTREACHED();
980 break;
981 }
982 }
983}
984
985void AudioProcessingImpl::HandleRenderRuntimeSettings() {
986 RuntimeSetting setting;
987 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200988 if (aec_dump_) {
989 aec_dump_->WriteRuntimeSetting(setting);
990 }
Alex Loiko73ec0192018-05-15 10:52:28 +0200991 switch (setting.type()) {
992 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
saza1d600522019-10-18 13:29:43 +0200993 if (submodules_.render_pre_processor) {
994 submodules_.render_pre_processor->SetRuntimeSetting(setting);
Alex Loiko73ec0192018-05-15 10:52:28 +0200995 }
996 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100997 case RuntimeSetting::Type::kCapturePreGain: // fall-through
998 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200999 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001000 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001001 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001002 RTC_NOTREACHED();
1003 break;
1004 }
1005 }
1006}
1007
peah9e6a2902017-05-15 07:19:21 -07001008void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001009 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001010
1011 // Insert the samples into the queue.
saza1d600522019-10-18 13:29:43 +02001012 if (submodules_.echo_cancellation) {
Per Åhgrenf204faf2019-04-25 15:18:06 +02001013 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001014 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1015 num_reverse_channels(),
1016 &aec_render_queue_buffer_);
1017
Per Åhgrenf204faf2019-04-25 15:18:06 +02001018 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1019 // The data queue is full and needs to be emptied.
1020 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001021
Per Åhgrenf204faf2019-04-25 15:18:06 +02001022 // Retry the insert (should always work).
1023 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1024 RTC_DCHECK(result);
1025 }
peaha0624602016-10-25 04:45:24 -07001026 }
1027
saza1d600522019-10-18 13:29:43 +02001028 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001029 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1030 num_reverse_channels(),
1031 &aecm_render_queue_buffer_);
1032 RTC_DCHECK(aecm_render_signal_queue_);
1033 // Insert the samples into the queue.
1034 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1035 // The data queue is full and needs to be emptied.
1036 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001037
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001038 // Retry the insert (should always work).
1039 bool result =
1040 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1041 RTC_DCHECK(result);
1042 }
peah764e3642016-10-22 05:04:30 -07001043 }
peah701d6282016-10-25 05:42:20 -07001044
1045 if (!constants_.use_experimental_agc) {
1046 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1047 // Insert the samples into the queue.
1048 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1049 // The data queue is full and needs to be emptied.
1050 EmptyQueuedRenderAudio();
1051
1052 // Retry the insert (should always work).
1053 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1054 RTC_DCHECK(result);
1055 }
1056 }
peah9e6a2902017-05-15 07:19:21 -07001057}
ivoc9f4a4a02016-10-28 05:39:16 -07001058
peah9e6a2902017-05-15 07:19:21 -07001059void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001060 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1061
1062 // Insert the samples into the queue.
1063 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1064 // The data queue is full and needs to be emptied.
1065 EmptyQueuedRenderAudio();
1066
1067 // Retry the insert (should always work).
1068 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1069 RTC_DCHECK(result);
1070 }
peah764e3642016-10-22 05:04:30 -07001071}
1072
1073void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001074 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001075 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001076
ivoc9f4a4a02016-10-28 05:39:16 -07001077 const size_t new_red_render_queue_element_max_size =
1078 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1079
peaha0624602016-10-25 04:45:24 -07001080 // Reallocate the queues if the queue item sizes are too small to fit the
1081 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001082
1083 if (agc_render_queue_element_max_size_ <
1084 new_agc_render_queue_element_max_size) {
1085 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1086
1087 std::vector<int16_t> template_queue_element(
1088 agc_render_queue_element_max_size_);
1089
1090 agc_render_signal_queue_.reset(
1091 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1092 kMaxNumFramesToBuffer, template_queue_element,
1093 RenderQueueItemVerifier<int16_t>(
1094 agc_render_queue_element_max_size_)));
1095
1096 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1097 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1098 } else {
1099 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001100 }
ivoc9f4a4a02016-10-28 05:39:16 -07001101
1102 if (red_render_queue_element_max_size_ <
1103 new_red_render_queue_element_max_size) {
1104 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1105
1106 std::vector<float> template_queue_element(
1107 red_render_queue_element_max_size_);
1108
1109 red_render_signal_queue_.reset(
1110 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1111 kMaxNumFramesToBuffer, template_queue_element,
1112 RenderQueueItemVerifier<float>(
1113 red_render_queue_element_max_size_)));
1114
1115 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1116 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1117 } else {
1118 red_render_signal_queue_->Clear();
1119 }
peah764e3642016-10-22 05:04:30 -07001120}
1121
1122void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1123 rtc::CritScope cs_capture(&crit_capture_);
saza1d600522019-10-18 13:29:43 +02001124 if (submodules_.echo_cancellation) {
Per Åhgrenf204faf2019-04-25 15:18:06 +02001125 RTC_DCHECK(aec_render_signal_queue_);
1126 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001127 submodules_.echo_cancellation->ProcessRenderAudio(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001128 aec_capture_queue_buffer_);
1129 }
peaha0624602016-10-25 04:45:24 -07001130 }
1131
saza1d600522019-10-18 13:29:43 +02001132 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001133 RTC_DCHECK(aecm_render_signal_queue_);
1134 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001135 submodules_.echo_control_mobile->ProcessRenderAudio(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001136 aecm_capture_queue_buffer_);
1137 }
peah701d6282016-10-25 05:42:20 -07001138 }
1139
1140 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001141 submodules_.gain_control->ProcessRenderAudio(agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001142 }
ivoc9f4a4a02016-10-28 05:39:16 -07001143
1144 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001145 RTC_DCHECK(submodules_.echo_detector);
1146 submodules_.echo_detector->AnalyzeRenderAudio(red_capture_queue_buffer_);
ivoc9f4a4a02016-10-28 05:39:16 -07001147 }
peah764e3642016-10-22 05:04:30 -07001148}
1149
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001150int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001151 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001152 {
1153 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001154 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001155 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001156 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001157 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001158 }
peahfa6228e2015-11-16 16:27:42 -08001159
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001160 if (!frame) {
1161 return kNullPointerError;
1162 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001163 // Must be a native rate.
1164 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1165 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001166 frame->sample_rate_hz_ != kSampleRate32kHz &&
1167 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001168 return kBadSampleRateError;
1169 }
peah192164e2015-11-17 02:16:45 -08001170
peahdf3efa82015-11-28 12:35:15 -08001171 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001172 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001173 {
1174 // Aquire lock for the access of api_format.
1175 // The lock is released immediately due to the conditional
1176 // reinitialization.
1177 rtc::CritScope cs_capture(&crit_capture_);
1178 // TODO(ajm): The input and output rates and channels are currently
1179 // constrained to be identical in the int16 interface.
1180 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001181
1182 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001183 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001184
Oskar Sundbom4b276482019-05-23 14:28:00 +02001185 reinitialization_required =
1186 reinitialization_required ||
1187 processing_config.input_stream().sample_rate_hz() !=
1188 frame->sample_rate_hz_ ||
1189 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1190 processing_config.output_stream().sample_rate_hz() !=
1191 frame->sample_rate_hz_ ||
1192 processing_config.output_stream().num_channels() != frame->num_channels_;
1193
1194 if (reinitialization_required) {
1195 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1196 processing_config.input_stream().set_num_channels(frame->num_channels_);
1197 processing_config.output_stream().set_sample_rate_hz(
1198 frame->sample_rate_hz_);
1199 processing_config.output_stream().set_num_channels(frame->num_channels_);
1200
1201 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001202 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001203 rtc::CritScope cs_capture(&crit_capture_);
1204 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001205 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001206
peahdf3efa82015-11-28 12:35:15 -08001207 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001208 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001209 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001210 return kBadDataLengthError;
1211 }
1212
aleloi868f32f2017-05-23 07:20:05 -07001213 if (aec_dump_) {
1214 RecordUnprocessedCaptureStream(*frame);
1215 }
1216
Per Åhgrend47941e2019-08-22 11:51:13 +02001217 capture_.capture_audio->CopyFrom(frame);
Gustaf Ullberg3c918b12019-10-11 13:14:44 +02001218 if (capture_.capture_fullband_audio) {
1219 capture_.capture_fullband_audio->CopyFrom(frame);
1220 }
peahde65ddc2016-09-16 15:02:15 -07001221 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001222 if (submodule_states_.CaptureMultiBandProcessingPresent() ||
Per Åhgrena1351272019-08-15 12:15:46 +02001223 submodule_states_.CaptureFullBandProcessingActive()) {
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001224 if (capture_.capture_fullband_audio) {
1225 capture_.capture_fullband_audio->CopyTo(frame);
1226 } else {
1227 capture_.capture_audio->CopyTo(frame);
1228 }
Per Åhgrena1351272019-08-15 12:15:46 +02001229 }
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001230 if (capture_.stats.voice_detected) {
1231 frame->vad_activity_ = *capture_.stats.voice_detected
1232 ? AudioFrame::kVadActive
1233 : AudioFrame::kVadPassive;
1234 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001235
aleloi868f32f2017-05-23 07:20:05 -07001236 if (aec_dump_) {
1237 RecordProcessedCaptureStream(*frame);
1238 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001239
1240 return kNoError;
1241}
1242
peahde65ddc2016-09-16 15:02:15 -07001243int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001244 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001245
peahb58a1582016-03-15 09:34:24 -07001246 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001247 // TODO(peah): Simplify once the public API Enable functions for these
1248 // are moved to APM.
saza1d600522019-10-18 13:29:43 +02001249 RTC_DCHECK_LE(!!submodules_.echo_controller +
1250 !!submodules_.echo_cancellation +
1251 !!submodules_.echo_control_mobile,
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001252 1);
peahb58a1582016-03-15 09:34:24 -07001253
peahde65ddc2016-09-16 15:02:15 -07001254 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001255
saza1d600522019-10-18 13:29:43 +02001256 if (submodules_.pre_amplifier) {
1257 submodules_.pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001258 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001259 capture_buffer->num_frames()));
1260 }
1261
Per Åhgren928146f2019-08-20 09:19:21 +02001262 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001263 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001264 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001265 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1266 if (log_rms) {
1267 capture_rms_interval_counter_ = 0;
1268 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001269 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1270 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1271 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1272 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001273 }
1274
saza1d600522019-10-18 13:29:43 +02001275 if (submodules_.echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001276 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001277 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001278 capture_.echo_path_gain_change =
1279 capture_.prev_analog_mic_level != analog_mic_level &&
1280 capture_.prev_analog_mic_level != -1;
1281 capture_.prev_analog_mic_level = analog_mic_level;
1282
Per Åhgrend2650d12018-10-02 17:00:59 +02001283 // Detect and flag any change in the pre-amplifier gain.
saza1d600522019-10-18 13:29:43 +02001284 if (submodules_.pre_amplifier) {
1285 float pre_amp_gain = submodules_.pre_amplifier->GetGainFactor();
Per Åhgrend2650d12018-10-02 17:00:59 +02001286 capture_.echo_path_gain_change =
1287 capture_.echo_path_gain_change ||
1288 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001289 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001290 capture_.prev_pre_amp_gain = pre_amp_gain;
1291 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001292
1293 // Detect volume change.
1294 capture_.echo_path_gain_change =
1295 capture_.echo_path_gain_change ||
1296 (capture_.prev_playout_volume != capture_.playout_volume &&
1297 capture_.prev_playout_volume >= 0);
1298 capture_.prev_playout_volume = capture_.playout_volume;
1299
saza1d600522019-10-18 13:29:43 +02001300 submodules_.echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001301 }
1302
peahbe615622016-02-13 16:40:47 -08001303 if (constants_.use_experimental_agc &&
saza1d600522019-10-18 13:29:43 +02001304 submodules_.gain_control->is_enabled()) {
1305 submodules_.agc_manager->AnalyzePreProcess(
Per Åhgren928146f2019-08-20 09:19:21 +02001306 capture_buffer->channels_f()[0], capture_buffer->num_channels(),
peahde65ddc2016-09-16 15:02:15 -07001307 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001308
1309 if (constants_.use_experimental_agc_process_before_aec) {
saza1d600522019-10-18 13:29:43 +02001310 submodules_.agc_manager->Process(
Per Åhgrend47941e2019-08-22 11:51:13 +02001311 capture_buffer->channels_const()[0],
Alex Loikod9342442018-09-10 13:59:41 +02001312 capture_nonlocked_.capture_processing_format.num_frames(),
1313 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1314 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001315 }
1316
peah2ace3f92016-09-10 04:42:27 -07001317 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1318 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001319 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1320 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001321 }
1322
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001323 const bool experimental_multi_channel_capture =
1324 config_.pipeline.experimental_multi_channel &&
1325 constants_.experimental_multi_channel_capture_support;
saza1d600522019-10-18 13:29:43 +02001326 if (submodules_.echo_controller && !experimental_multi_channel_capture) {
peah522d71b2017-02-23 05:16:26 -08001327 // Force down-mixing of the number of channels after the detection of
1328 // capture signal saturation.
1329 // TODO(peah): Look into ensuring that this kind of tampering with the
1330 // AudioBuffer functionality should not be needed.
1331 capture_buffer->set_num_channels(1);
1332 }
1333
saza1d600522019-10-18 13:29:43 +02001334 if (submodules_.high_pass_filter) {
1335 submodules_.high_pass_filter->Process(capture_buffer);
peah8271d042016-11-22 07:24:52 -08001336 }
saza1d600522019-10-18 13:29:43 +02001337 RETURN_ON_ERR(submodules_.gain_control->AnalyzeCaptureAudio(capture_buffer));
1338 if (submodules_.noise_suppressor) {
1339 submodules_.noise_suppressor->AnalyzeCaptureAudio(capture_buffer);
saza0bad15f2019-10-16 11:46:11 +02001340 }
peahb58a1582016-03-15 09:34:24 -07001341
saza1d600522019-10-18 13:29:43 +02001342 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001343 // Ensure that the stream delay was set before the call to the
1344 // AECM ProcessCaptureAudio function.
1345 if (!was_stream_delay_set()) {
1346 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001347 }
1348
saza1d600522019-10-18 13:29:43 +02001349 if (submodules_.noise_suppressor) {
1350 submodules_.echo_control_mobile->CopyLowPassReference(capture_buffer);
1351 submodules_.noise_suppressor->ProcessCaptureAudio(capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001352 }
peahe0eae3c2016-12-14 01:16:23 -08001353
saza1d600522019-10-18 13:29:43 +02001354 RETURN_ON_ERR(submodules_.echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001355 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001356 } else {
saza1d600522019-10-18 13:29:43 +02001357 if (submodules_.echo_controller) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001358 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1359
1360 if (was_stream_delay_set()) {
saza1d600522019-10-18 13:29:43 +02001361 submodules_.echo_controller->SetAudioBufferDelay(stream_delay_ms());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001362 }
1363
saza1d600522019-10-18 13:29:43 +02001364 submodules_.echo_controller->ProcessCapture(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001365 capture_buffer, capture_.echo_path_gain_change);
saza1d600522019-10-18 13:29:43 +02001366 } else if (submodules_.echo_cancellation) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001367 // Ensure that the stream delay was set before the call to the
1368 // AEC ProcessCaptureAudio function.
1369 if (!was_stream_delay_set()) {
1370 return AudioProcessing::kStreamParameterNotSetError;
1371 }
1372
saza1d600522019-10-18 13:29:43 +02001373 RETURN_ON_ERR(submodules_.echo_cancellation->ProcessCaptureAudio(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001374 capture_buffer, stream_delay_ms()));
1375 }
1376
saza1d600522019-10-18 13:29:43 +02001377 if (submodules_.noise_suppressor) {
1378 submodules_.noise_suppressor->ProcessCaptureAudio(capture_buffer);
saza0bad15f2019-10-16 11:46:11 +02001379 }
Per Åhgren46537a32017-06-07 10:08:10 +02001380 }
ivoc9f4a4a02016-10-28 05:39:16 -07001381
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001382 if (config_.voice_detection.enabled) {
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001383 capture_.stats.voice_detected =
saza1d600522019-10-18 13:29:43 +02001384 submodules_.voice_detector->ProcessCaptureAudio(capture_buffer);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001385 } else {
1386 capture_.stats.voice_detected = absl::nullopt;
1387 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001388
peahbe615622016-02-13 16:40:47 -08001389 if (constants_.use_experimental_agc &&
saza1d600522019-10-18 13:29:43 +02001390 submodules_.gain_control->is_enabled() &&
Alex Loikod9342442018-09-10 13:59:41 +02001391 !constants_.use_experimental_agc_process_before_aec) {
saza1d600522019-10-18 13:29:43 +02001392 submodules_.agc_manager->Process(
Per Åhgren928146f2019-08-20 09:19:21 +02001393 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
peahde65ddc2016-09-16 15:02:15 -07001394 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001395 }
Per Åhgren200feba2019-03-06 04:16:46 +01001396 // TODO(peah): Add reporting from AEC3 whether there is echo.
saza1d600522019-10-18 13:29:43 +02001397 RETURN_ON_ERR(submodules_.gain_control->ProcessCaptureAudio(
1398 capture_buffer, submodules_.echo_cancellation &&
1399 submodules_.echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001400
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001401 if (submodule_states_.CaptureMultiBandProcessingPresent() &&
peah2ace3f92016-09-10 04:42:27 -07001402 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001403 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1404 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001405 }
1406
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001407 if (capture_.capture_fullband_audio) {
saza1d600522019-10-18 13:29:43 +02001408 const auto& ec = submodules_.echo_controller;
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001409 bool ec_active = ec ? ec->ActiveProcessing() : false;
1410 // Only update the fullband buffer if the multiband processing has changed
1411 // the signal. Keep the original signal otherwise.
1412 if (submodule_states_.CaptureMultiBandProcessingActive(ec_active)) {
1413 capture_buffer->CopyTo(capture_.capture_fullband_audio.get());
1414 }
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001415 capture_buffer = capture_.capture_fullband_audio.get();
1416 }
1417
peah9e6a2902017-05-15 07:19:21 -07001418 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001419 RTC_DCHECK(submodules_.echo_detector);
1420 submodules_.echo_detector->AnalyzeCaptureAudio(rtc::ArrayView<const float>(
1421 capture_buffer->channels()[0], capture_buffer->num_frames()));
peah9e6a2902017-05-15 07:19:21 -07001422 }
1423
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001424 // TODO(aluebs): Investigate if the transient suppression placement should be
1425 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001426 if (capture_.transient_suppressor_enabled) {
saza1d600522019-10-18 13:29:43 +02001427 float voice_probability = submodules_.agc_manager.get()
1428 ? submodules_.agc_manager->voice_probability()
1429 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001430
saza1d600522019-10-18 13:29:43 +02001431 submodules_.transient_suppressor->Suppress(
Per Åhgrend47941e2019-08-22 11:51:13 +02001432 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001433 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001434 capture_buffer->split_bands_const(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001435 capture_buffer->num_frames_per_band(),
1436 capture_.keyboard_info.keyboard_data,
1437 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001438 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001439 }
1440
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001441 // Experimental APM sub-module that analyzes |capture_buffer|.
saza1d600522019-10-18 13:29:43 +02001442 if (submodules_.capture_analyzer) {
1443 submodules_.capture_analyzer->Analyze(capture_buffer);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001444 }
1445
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001446 if (config_.gain_controller2.enabled) {
saza1d600522019-10-18 13:29:43 +02001447 submodules_.gain_controller2->NotifyAnalogLevel(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001448 agc1()->stream_analog_level());
saza1d600522019-10-18 13:29:43 +02001449 submodules_.gain_controller2->Process(capture_buffer);
alessiob3ec96df2017-05-22 06:57:06 -07001450 }
1451
saza1d600522019-10-18 13:29:43 +02001452 if (submodules_.capture_post_processor) {
1453 submodules_.capture_post_processor->Process(capture_buffer);
Sam Zackrisson0beac582017-09-25 12:04:02 +02001454 }
1455
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001456 // The level estimator operates on the recombined data.
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001457 if (config_.level_estimation.enabled) {
saza1d600522019-10-18 13:29:43 +02001458 submodules_.output_level_estimator->ProcessStream(*capture_buffer);
1459 capture_.stats.output_rms_dbfs = submodules_.output_level_estimator->RMS();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001460 } else {
1461 capture_.stats.output_rms_dbfs = absl::nullopt;
1462 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001463
Per Åhgren928146f2019-08-20 09:19:21 +02001464 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001465 capture_buffer->channels_const()[0],
peah1b08dc32016-12-20 13:45:58 -08001466 capture_nonlocked_.capture_processing_format.num_frames()));
1467 if (log_rms) {
1468 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1469 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1470 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1471 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1472 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1473 }
1474
peahdf3efa82015-11-28 12:35:15 -08001475 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001476 return kNoError;
1477}
1478
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001479int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001480 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001481 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001482 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001483 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001484 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001485 const StreamConfig reverse_config = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001486 sample_rate_hz,
1487 ChannelsFromLayout(layout),
1488 LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001489 };
1490 if (samples_per_channel != reverse_config.num_frames()) {
1491 return kBadDataLengthError;
1492 }
peahdf3efa82015-11-28 12:35:15 -08001493 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001494}
1495
Gustaf Ullberg8c51f2e2019-10-22 15:21:31 +02001496int AudioProcessingImpl::AnalyzeReverseStream(
1497 const float* const* data,
1498 const StreamConfig& reverse_config) {
1499 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_StreamConfig");
1500 rtc::CritScope cs(&crit_render_);
1501 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
1502}
1503
peahde65ddc2016-09-16 15:02:15 -07001504int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1505 const StreamConfig& input_config,
1506 const StreamConfig& output_config,
1507 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001508 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001509 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001510 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001511 if (submodule_states_.RenderMultiBandProcessingActive() ||
1512 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001513 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1514 dest);
peah2ace3f92016-09-10 04:42:27 -07001515 } else if (formats_.api_format.reverse_input_stream() !=
1516 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001517 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1518 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001519 } else {
peahde65ddc2016-09-16 15:02:15 -07001520 CopyAudioIfNeeded(src, input_config.num_frames(),
1521 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001522 }
1523
1524 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001525}
1526
peahdf3efa82015-11-28 12:35:15 -08001527int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001528 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001529 const StreamConfig& input_config,
1530 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001531 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001532 return kNullPointerError;
1533 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001534
peahde65ddc2016-09-16 15:02:15 -07001535 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001536 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001537 }
1538
peahdf3efa82015-11-28 12:35:15 -08001539 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001540 processing_config.reverse_input_stream() = input_config;
1541 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001542
peahdf3efa82015-11-28 12:35:15 -08001543 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001544 RTC_DCHECK_EQ(input_config.num_frames(),
1545 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001546
aleloi868f32f2017-05-23 07:20:05 -07001547 if (aec_dump_) {
1548 const size_t channel_size =
1549 formats_.api_format.reverse_input_stream().num_frames();
1550 const size_t num_channels =
1551 formats_.api_format.reverse_input_stream().num_channels();
1552 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001553 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001554 }
peahdf3efa82015-11-28 12:35:15 -08001555 render_.render_audio->CopyFrom(src,
1556 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001557 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001558}
1559
1560int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001561 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001562 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001563 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001564 return kNullPointerError;
1565 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001566 // Must be a native rate.
1567 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1568 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001569 frame->sample_rate_hz_ != kSampleRate32kHz &&
1570 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001571 return kBadSampleRateError;
1572 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001573
Michael Graczyk86c6d332015-07-23 11:41:39 -07001574 if (frame->num_channels_ <= 0) {
1575 return kBadNumberChannelsError;
1576 }
1577
peahdf3efa82015-11-28 12:35:15 -08001578 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001579 processing_config.reverse_input_stream().set_sample_rate_hz(
1580 frame->sample_rate_hz_);
1581 processing_config.reverse_input_stream().set_num_channels(
1582 frame->num_channels_);
1583 processing_config.reverse_output_stream().set_sample_rate_hz(
1584 frame->sample_rate_hz_);
1585 processing_config.reverse_output_stream().set_num_channels(
1586 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001587
peahdf3efa82015-11-28 12:35:15 -08001588 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001589 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001590 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001591 return kBadDataLengthError;
1592 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001593
aleloi868f32f2017-05-23 07:20:05 -07001594 if (aec_dump_) {
1595 aec_dump_->WriteRenderStreamMessage(*frame);
1596 }
1597
Per Åhgrend47941e2019-08-22 11:51:13 +02001598 render_.render_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001599 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001600 if (submodule_states_.RenderMultiBandProcessingActive() ||
1601 submodule_states_.RenderFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001602 render_.render_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001603 }
aluebsb0319552016-03-17 20:39:53 -07001604 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001605}
niklase@google.com470e71d2011-07-07 08:21:25 +00001606
peahde65ddc2016-09-16 15:02:15 -07001607int AudioProcessingImpl::ProcessRenderStreamLocked() {
1608 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001609
Alex Loiko73ec0192018-05-15 10:52:28 +02001610 HandleRenderRuntimeSettings();
1611
saza1d600522019-10-18 13:29:43 +02001612 if (submodules_.render_pre_processor) {
1613 submodules_.render_pre_processor->Process(render_buffer);
Alex Loiko5825aa62017-12-18 16:02:40 +01001614 }
1615
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001616 QueueNonbandedRenderAudio(render_buffer);
1617
peah2ace3f92016-09-10 04:42:27 -07001618 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001619 SampleRateSupportsMultiBand(
1620 formats_.render_processing_format.sample_rate_hz())) {
1621 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001622 }
1623
peahce4d9152017-05-19 01:28:05 -07001624 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1625 QueueBandedRenderAudio(render_buffer);
1626 }
1627
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001628 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
saza1d600522019-10-18 13:29:43 +02001629 if (submodules_.echo_controller) {
1630 submodules_.echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001631 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001632
peah2ace3f92016-09-10 04:42:27 -07001633 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001634 SampleRateSupportsMultiBand(
1635 formats_.render_processing_format.sample_rate_hz())) {
1636 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001637 }
1638
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001639 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001640}
1641
1642int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001643 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001644 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001645 capture_.was_stream_delay_set = true;
1646 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001647
niklase@google.com470e71d2011-07-07 08:21:25 +00001648 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001649 delay = 0;
1650 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001651 }
1652
1653 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1654 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001655 delay = 500;
1656 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001657 }
1658
peahdf3efa82015-11-28 12:35:15 -08001659 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001660 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001661}
1662
1663int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001664 // Used as callback from submodules, hence locking is not allowed.
1665 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001666}
1667
1668bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001669 // Used as callback from submodules, hence locking is not allowed.
1670 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001671}
1672
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001673void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001674 rtc::CritScope cs(&crit_capture_);
1675 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001676}
1677
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001678void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001679 rtc::CritScope cs(&crit_capture_);
1680 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001681}
1682
1683int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001684 rtc::CritScope cs(&crit_capture_);
1685 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001686}
1687
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001688void AudioProcessingImpl::set_stream_analog_level(int level) {
1689 rtc::CritScope cs_capture(&crit_capture_);
1690 int error = agc1()->set_stream_analog_level(level);
1691 RTC_DCHECK_EQ(kNoError, error);
1692}
1693
1694int AudioProcessingImpl::recommended_stream_analog_level() const {
1695 rtc::CritScope cs_capture(&crit_capture_);
1696 return agc1()->stream_analog_level();
1697}
1698
aleloi868f32f2017-05-23 07:20:05 -07001699void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1700 RTC_DCHECK(aec_dump);
1701 rtc::CritScope cs_render(&crit_render_);
1702 rtc::CritScope cs_capture(&crit_capture_);
1703
1704 // The previously attached AecDump will be destroyed with the
1705 // 'aec_dump' parameter, which is after locks are released.
1706 aec_dump_.swap(aec_dump);
1707 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001708 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001709}
1710
1711void AudioProcessingImpl::DetachAecDump() {
1712 // The d-tor of a task-queue based AecDump blocks until all pending
1713 // tasks are done. This construction avoids blocking while holding
1714 // the render and capture locks.
1715 std::unique_ptr<AecDump> aec_dump = nullptr;
1716 {
1717 rtc::CritScope cs_render(&crit_render_);
1718 rtc::CritScope cs_capture(&crit_capture_);
1719 aec_dump = std::move(aec_dump_);
1720 }
1721}
1722
Sam Zackrisson4d364492018-03-02 16:03:21 +01001723void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1724 std::unique_ptr<AudioGenerator> audio_generator) {
1725 // TODO(bugs.webrtc.org/8882) Stub.
1726 // Reset internal audio generator with audio_generator.
1727}
1728
1729void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1730 // TODO(bugs.webrtc.org/8882) Stub.
1731 // Delete audio generator, if one is attached.
1732}
1733
Ivo Creusen56d46092017-11-24 17:29:59 +01001734AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001735 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001736 rtc::CritScope cs_capture(&crit_capture_);
1737 if (!has_remote_tracks) {
1738 return capture_.stats;
1739 }
1740 AudioProcessingStats stats = capture_.stats;
1741 EchoCancellationImpl::Metrics metrics;
saza1d600522019-10-18 13:29:43 +02001742 if (submodules_.echo_controller) {
1743 auto ec_metrics = submodules_.echo_controller->GetMetrics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001744 stats.echo_return_loss = ec_metrics.echo_return_loss;
1745 stats.echo_return_loss_enhancement =
1746 ec_metrics.echo_return_loss_enhancement;
1747 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001748 }
1749 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001750 RTC_DCHECK(submodules_.echo_detector);
1751 auto ed_metrics = submodules_.echo_detector->GetMetrics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001752 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1753 stats.residual_echo_likelihood_recent_max =
1754 ed_metrics.echo_likelihood_recent_max;
1755 }
Ivo Creusenae026092017-11-20 13:07:16 +01001756 return stats;
1757}
1758
peah8271d042016-11-22 07:24:52 -08001759void AudioProcessingImpl::MutateConfig(
1760 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1761 rtc::CritScope cs_render(&crit_render_);
1762 rtc::CritScope cs_capture(&crit_capture_);
1763 mutator(&config_);
1764 ApplyConfig(config_);
1765}
1766
1767AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1768 rtc::CritScope cs_render(&crit_render_);
1769 rtc::CritScope cs_capture(&crit_capture_);
1770 return config_;
1771}
1772
peah2ace3f92016-09-10 04:42:27 -07001773bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1774 return submodule_states_.Update(
saza1d600522019-10-18 13:29:43 +02001775 config_.high_pass_filter.enabled, !!submodules_.echo_cancellation,
1776 !!submodules_.echo_control_mobile, config_.residual_echo_detector.enabled,
1777 !!submodules_.noise_suppressor, submodules_.gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001778 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001779 capture_nonlocked_.echo_controller_enabled,
saza0bad15f2019-10-16 11:46:11 +02001780 config_.voice_detection.enabled, capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001781}
1782
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001783void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001784 if (capture_.transient_suppressor_enabled) {
saza1d600522019-10-18 13:29:43 +02001785 if (!submodules_.transient_suppressor.get()) {
1786 submodules_.transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001787 }
saza1d600522019-10-18 13:29:43 +02001788 submodules_.transient_suppressor->Initialize(proc_fullband_sample_rate_hz(),
1789 capture_nonlocked_.split_rate,
1790 num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001791 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001792}
1793
Per Åhgren0aefbf02019-08-23 21:29:17 +02001794void AudioProcessingImpl::InitializeHighPassFilter() {
1795 if (submodule_states_.HighPassFilteringRequired()) {
saza1d600522019-10-18 13:29:43 +02001796 submodules_.high_pass_filter.reset(new HighPassFilter(num_proc_channels()));
peah8271d042016-11-22 07:24:52 -08001797 } else {
saza1d600522019-10-18 13:29:43 +02001798 submodules_.high_pass_filter.reset();
peah8271d042016-11-22 07:24:52 -08001799 }
1800}
alessiob3ec96df2017-05-22 06:57:06 -07001801
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001802void AudioProcessingImpl::InitializeVoiceDetector() {
1803 if (config_.voice_detection.enabled) {
saza1d600522019-10-18 13:29:43 +02001804 submodules_.voice_detector = std::make_unique<VoiceDetection>(
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001805 proc_split_sample_rate_hz(), VoiceDetection::kVeryLowLikelihood);
1806 } else {
saza1d600522019-10-18 13:29:43 +02001807 submodules_.voice_detector.reset();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001808 }
1809}
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001810void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001811 bool use_echo_controller =
1812 echo_control_factory_ ||
Per Åhgren200feba2019-03-06 04:16:46 +01001813 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode &&
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001814 !config_.echo_canceller.use_legacy_aec);
1815
1816 if (use_echo_controller) {
1817 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001818 if (echo_control_factory_) {
saza1d600522019-10-18 13:29:43 +02001819 submodules_.echo_controller =
Per Åhgren200feba2019-03-06 04:16:46 +01001820 echo_control_factory_->Create(proc_sample_rate_hz());
1821 } else {
saza1d600522019-10-18 13:29:43 +02001822 submodules_.echo_controller = std::make_unique<EchoCanceller3>(
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001823 EchoCanceller3Config(), proc_sample_rate_hz(), num_reverse_channels(),
1824 num_proc_channels());
Per Åhgren200feba2019-03-06 04:16:46 +01001825 }
1826
1827 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001828
saza1d600522019-10-18 13:29:43 +02001829 submodules_.echo_cancellation.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001830 aec_render_signal_queue_.reset();
saza1d600522019-10-18 13:29:43 +02001831 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001832 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001833 return;
peahe0eae3c2016-12-14 01:16:23 -08001834 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001835
saza1d600522019-10-18 13:29:43 +02001836 submodules_.echo_controller.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001837 capture_nonlocked_.echo_controller_enabled = false;
1838
1839 if (!config_.echo_canceller.enabled) {
saza1d600522019-10-18 13:29:43 +02001840 submodules_.echo_cancellation.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001841 aec_render_signal_queue_.reset();
saza1d600522019-10-18 13:29:43 +02001842 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001843 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001844 return;
1845 }
1846
1847 if (config_.echo_canceller.mobile_mode) {
1848 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001849 size_t max_element_size =
1850 std::max(static_cast<size_t>(1),
1851 kMaxAllowedValuesOfSamplesPerBand *
1852 EchoControlMobileImpl::NumCancellersRequired(
1853 num_output_channels(), num_reverse_channels()));
1854
1855 std::vector<int16_t> template_queue_element(max_element_size);
1856
1857 aecm_render_signal_queue_.reset(
1858 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1859 kMaxNumFramesToBuffer, template_queue_element,
1860 RenderQueueItemVerifier<int16_t>(max_element_size)));
1861
1862 aecm_render_queue_buffer_.resize(max_element_size);
1863 aecm_capture_queue_buffer_.resize(max_element_size);
1864
saza1d600522019-10-18 13:29:43 +02001865 submodules_.echo_control_mobile.reset(new EchoControlMobileImpl());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001866
saza1d600522019-10-18 13:29:43 +02001867 submodules_.echo_control_mobile->Initialize(proc_split_sample_rate_hz(),
1868 num_reverse_channels(),
1869 num_output_channels());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001870
saza1d600522019-10-18 13:29:43 +02001871 submodules_.echo_cancellation.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001872 aec_render_signal_queue_.reset();
1873 return;
1874 }
1875
saza1d600522019-10-18 13:29:43 +02001876 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001877 aecm_render_signal_queue_.reset();
1878
Per Åhgrenf204faf2019-04-25 15:18:06 +02001879 // Create and activate AEC2.
saza1d600522019-10-18 13:29:43 +02001880 submodules_.echo_cancellation.reset(new EchoCancellationImpl());
1881 submodules_.echo_cancellation->SetExtraOptions(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001882 capture_nonlocked_.use_aec2_extended_filter,
1883 capture_nonlocked_.use_aec2_delay_agnostic,
1884 capture_nonlocked_.use_aec2_refined_adaptive_filter);
1885
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001886 size_t element_max_size =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001887 std::max(static_cast<size_t>(1),
1888 kMaxAllowedValuesOfSamplesPerBand *
1889 EchoCancellationImpl::NumCancellersRequired(
1890 num_output_channels(), num_reverse_channels()));
1891
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001892 std::vector<float> template_queue_element(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001893
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001894 aec_render_signal_queue_.reset(
1895 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1896 kMaxNumFramesToBuffer, template_queue_element,
1897 RenderQueueItemVerifier<float>(element_max_size)));
Per Åhgrenf204faf2019-04-25 15:18:06 +02001898
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001899 aec_render_queue_buffer_.resize(element_max_size);
1900 aec_capture_queue_buffer_.resize(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001901
saza1d600522019-10-18 13:29:43 +02001902 submodules_.echo_cancellation->Initialize(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001903 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
1904 num_proc_channels());
1905
saza1d600522019-10-18 13:29:43 +02001906 submodules_.echo_cancellation->set_suppression_level(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001907 config_.echo_canceller.legacy_moderate_suppression_level
1908 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
1909 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
peahe0eae3c2016-12-14 01:16:23 -08001910}
peah8271d042016-11-22 07:24:52 -08001911
alessiob3ec96df2017-05-22 06:57:06 -07001912void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001913 if (config_.gain_controller2.enabled) {
saza1d600522019-10-18 13:29:43 +02001914 submodules_.gain_controller2->Initialize(proc_fullband_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001915 }
1916}
1917
saza0bad15f2019-10-16 11:46:11 +02001918void AudioProcessingImpl::InitializeNoiseSuppressor() {
1919 if (config_.noise_suppression.enabled) {
1920 auto ns_level =
1921 NsConfigLevelToInterfaceLevel(config_.noise_suppression.level);
saza1d600522019-10-18 13:29:43 +02001922 submodules_.noise_suppressor = std::make_unique<NoiseSuppression>(
saza0bad15f2019-10-16 11:46:11 +02001923 num_proc_channels(), proc_sample_rate_hz(), ns_level);
1924 } else {
saza1d600522019-10-18 13:29:43 +02001925 submodules_.noise_suppressor.reset();
saza0bad15f2019-10-16 11:46:11 +02001926 }
1927}
1928
Alex Loikob5c9a792018-04-16 16:31:22 +02001929void AudioProcessingImpl::InitializePreAmplifier() {
1930 if (config_.pre_amplifier.enabled) {
saza1d600522019-10-18 13:29:43 +02001931 submodules_.pre_amplifier.reset(
Alex Loikob5c9a792018-04-16 16:31:22 +02001932 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1933 } else {
saza1d600522019-10-18 13:29:43 +02001934 submodules_.pre_amplifier.reset();
Alex Loikob5c9a792018-04-16 16:31:22 +02001935 }
1936}
1937
ivoc9f4a4a02016-10-28 05:39:16 -07001938void AudioProcessingImpl::InitializeResidualEchoDetector() {
saza1d600522019-10-18 13:29:43 +02001939 RTC_DCHECK(submodules_.echo_detector);
1940 submodules_.echo_detector->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001941 proc_fullband_sample_rate_hz(), 1,
Ivo Creusenb1facc12018-04-12 16:15:58 +02001942 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001943}
1944
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001945void AudioProcessingImpl::InitializeAnalyzer() {
saza1d600522019-10-18 13:29:43 +02001946 if (submodules_.capture_analyzer) {
1947 submodules_.capture_analyzer->Initialize(proc_fullband_sample_rate_hz(),
1948 num_proc_channels());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001949 }
1950}
1951
Sam Zackrisson0beac582017-09-25 12:04:02 +02001952void AudioProcessingImpl::InitializePostProcessor() {
saza1d600522019-10-18 13:29:43 +02001953 if (submodules_.capture_post_processor) {
1954 submodules_.capture_post_processor->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001955 proc_fullband_sample_rate_hz(), num_proc_channels());
Sam Zackrisson0beac582017-09-25 12:04:02 +02001956 }
1957}
1958
Alex Loiko5825aa62017-12-18 16:02:40 +01001959void AudioProcessingImpl::InitializePreProcessor() {
saza1d600522019-10-18 13:29:43 +02001960 if (submodules_.render_pre_processor) {
1961 submodules_.render_pre_processor->Initialize(
Alex Loiko5825aa62017-12-18 16:02:40 +01001962 formats_.render_processing_format.sample_rate_hz(),
1963 formats_.render_processing_format.num_channels());
1964 }
1965}
1966
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001967void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001968
aleloi868f32f2017-05-23 07:20:05 -07001969void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1970 if (!aec_dump_) {
1971 return;
1972 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001973
1974 std::string experiments_description = "";
saza1d600522019-10-18 13:29:43 +02001975 if (submodules_.echo_cancellation) {
Per Åhgrenf204faf2019-04-25 15:18:06 +02001976 experiments_description +=
saza1d600522019-10-18 13:29:43 +02001977 submodules_.echo_cancellation->GetExperimentsDescription();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001978 }
aleloi868f32f2017-05-23 07:20:05 -07001979 // TODO(peah): Add semicolon-separated concatenations of experiment
1980 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001981 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1982 experiments_description += "AgcClippingLevelExperiment;";
1983 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001984 if (capture_nonlocked_.echo_controller_enabled) {
1985 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001986 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001987 if (config_.gain_controller2.enabled) {
1988 experiments_description += "GainController2;";
1989 }
aleloi868f32f2017-05-23 07:20:05 -07001990
1991 InternalAPMConfig apm_config;
1992
Per Åhgren200feba2019-03-06 04:16:46 +01001993 apm_config.aec_enabled = config_.echo_canceller.enabled;
aleloi868f32f2017-05-23 07:20:05 -07001994 apm_config.aec_delay_agnostic_enabled =
saza1d600522019-10-18 13:29:43 +02001995 submodules_.echo_cancellation &&
1996 submodules_.echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07001997 apm_config.aec_drift_compensation_enabled =
saza1d600522019-10-18 13:29:43 +02001998 submodules_.echo_cancellation &&
1999 submodules_.echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002000 apm_config.aec_extended_filter_enabled =
saza1d600522019-10-18 13:29:43 +02002001 submodules_.echo_cancellation &&
2002 submodules_.echo_cancellation->is_extended_filter_enabled();
Per Åhgrenf204faf2019-04-25 15:18:06 +02002003 apm_config.aec_suppression_level =
saza1d600522019-10-18 13:29:43 +02002004 submodules_.echo_cancellation
2005 ? static_cast<int>(submodules_.echo_cancellation->suppression_level())
Per Åhgrenf204faf2019-04-25 15:18:06 +02002006 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002007
saza1d600522019-10-18 13:29:43 +02002008 apm_config.aecm_enabled = !!submodules_.echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002009 apm_config.aecm_comfort_noise_enabled =
saza1d600522019-10-18 13:29:43 +02002010 submodules_.echo_control_mobile &&
2011 submodules_.echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002012 apm_config.aecm_routing_mode =
saza1d600522019-10-18 13:29:43 +02002013 submodules_.echo_control_mobile
2014 ? static_cast<int>(submodules_.echo_control_mobile->routing_mode())
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002015 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002016
saza1d600522019-10-18 13:29:43 +02002017 apm_config.agc_enabled = submodules_.gain_control->is_enabled();
2018 apm_config.agc_mode = static_cast<int>(submodules_.gain_control->mode());
aleloi868f32f2017-05-23 07:20:05 -07002019 apm_config.agc_limiter_enabled =
saza1d600522019-10-18 13:29:43 +02002020 submodules_.gain_control->is_limiter_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002021 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2022
2023 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2024
saza0bad15f2019-10-16 11:46:11 +02002025 apm_config.ns_enabled = config_.noise_suppression.enabled;
2026 apm_config.ns_level = static_cast<int>(config_.noise_suppression.level);
aleloi868f32f2017-05-23 07:20:05 -07002027
2028 apm_config.transient_suppression_enabled =
2029 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002030 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002031 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2032 apm_config.pre_amplifier_fixed_gain_factor =
2033 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002034
2035 if (!forced && apm_config == apm_config_for_aec_dump_) {
2036 return;
2037 }
2038 aec_dump_->WriteConfig(apm_config);
2039 apm_config_for_aec_dump_ = apm_config;
2040}
2041
2042void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2043 const float* const* src) {
2044 RTC_DCHECK(aec_dump_);
2045 WriteAecDumpConfigMessage(false);
2046
2047 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2048 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2049 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002050 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002051 RecordAudioProcessingState();
2052}
2053
2054void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2055 const AudioFrame& capture_frame) {
2056 RTC_DCHECK(aec_dump_);
2057 WriteAecDumpConfigMessage(false);
2058
2059 aec_dump_->AddCaptureStreamInput(capture_frame);
2060 RecordAudioProcessingState();
2061}
2062
2063void AudioProcessingImpl::RecordProcessedCaptureStream(
2064 const float* const* processed_capture_stream) {
2065 RTC_DCHECK(aec_dump_);
2066
2067 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2068 const size_t num_channels =
2069 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002070 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2071 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002072 aec_dump_->WriteCaptureStreamMessage();
2073}
2074
2075void AudioProcessingImpl::RecordProcessedCaptureStream(
2076 const AudioFrame& processed_capture_frame) {
2077 RTC_DCHECK(aec_dump_);
2078
2079 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2080 aec_dump_->WriteCaptureStreamMessage();
2081}
2082
2083void AudioProcessingImpl::RecordAudioProcessingState() {
2084 RTC_DCHECK(aec_dump_);
2085 AecDump::AudioProcessingState audio_proc_state;
2086 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2087 audio_proc_state.drift =
saza1d600522019-10-18 13:29:43 +02002088 submodules_.echo_cancellation
2089 ? submodules_.echo_cancellation->stream_drift_samples()
Per Åhgrenf204faf2019-04-25 15:18:06 +02002090 : 0;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01002091 audio_proc_state.level = agc1()->stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002092 audio_proc_state.keypress = capture_.key_pressed;
2093 aec_dump_->AddAudioProcessingState(audio_proc_state);
2094}
2095
kwiberg83ffe452016-08-29 14:46:07 -07002096AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002097 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002098 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002099 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002100 output_will_be_muted(false),
2101 key_pressed(false),
2102 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002103 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002104 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002105 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002106 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002107 prev_pre_amp_gain(-1.f),
2108 playout_volume(-1),
2109 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002110
2111AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2112
Per Åhgrena1351272019-08-15 12:15:46 +02002113void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2114 const float* const* data,
2115 const StreamConfig& stream_config) {
2116 if (stream_config.has_keyboard()) {
2117 keyboard_data = data[stream_config.num_channels()];
2118 } else {
2119 keyboard_data = NULL;
2120 }
2121 num_keyboard_frames = stream_config.num_frames();
2122}
2123
kwiberg83ffe452016-08-29 14:46:07 -07002124AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2125
2126AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2127
niklase@google.com470e71d2011-07-07 08:21:25 +00002128} // namespace webrtc