blob: ca6edfe06666aad94033ec257f08da9d868d5d9c [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);
Sam Zackrisson21bfa402019-10-23 09:43:01 +0200947 config_.pre_amplifier.fixed_gain_factor = value;
saza1d600522019-10-18 13:29:43 +0200948 submodules_.pre_amplifier->SetGainFactor(value);
Alex Loikob5c9a792018-04-16 16:31:22 +0200949 }
950 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200951 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100952 case RuntimeSetting::Type::kCaptureCompressionGain: {
953 float value;
954 setting.GetFloat(&value);
955 int int_value = static_cast<int>(value + .5f);
956 config_.gain_controller1.compression_gain_db = int_value;
957 int error = agc1()->set_compression_gain_db(int_value);
958 RTC_DCHECK_EQ(kNoError, error);
959 break;
960 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200961 case RuntimeSetting::Type::kCaptureFixedPostGain: {
962 if (config_.gain_controller2.enabled) {
963 float value;
964 setting.GetFloat(&value);
965 config_.gain_controller2.fixed_digital.gain_db = value;
saza1d600522019-10-18 13:29:43 +0200966 submodules_.gain_controller2->ApplyConfig(config_.gain_controller2);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200967 }
968 break;
969 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200970 case RuntimeSetting::Type::kPlayoutVolumeChange: {
971 int value;
972 setting.GetInt(&value);
973 capture_.playout_volume = value;
974 break;
975 }
Alex Loiko73ec0192018-05-15 10:52:28 +0200976 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
977 RTC_NOTREACHED();
978 break;
979 case RuntimeSetting::Type::kNotSpecified:
980 RTC_NOTREACHED();
981 break;
982 }
983 }
984}
985
986void AudioProcessingImpl::HandleRenderRuntimeSettings() {
987 RuntimeSetting setting;
988 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200989 if (aec_dump_) {
990 aec_dump_->WriteRuntimeSetting(setting);
991 }
Alex Loiko73ec0192018-05-15 10:52:28 +0200992 switch (setting.type()) {
993 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
saza1d600522019-10-18 13:29:43 +0200994 if (submodules_.render_pre_processor) {
995 submodules_.render_pre_processor->SetRuntimeSetting(setting);
Alex Loiko73ec0192018-05-15 10:52:28 +0200996 }
997 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100998 case RuntimeSetting::Type::kCapturePreGain: // fall-through
999 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001000 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001001 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001002 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001003 RTC_NOTREACHED();
1004 break;
1005 }
1006 }
1007}
1008
peah9e6a2902017-05-15 07:19:21 -07001009void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001010 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001011
1012 // Insert the samples into the queue.
saza1d600522019-10-18 13:29:43 +02001013 if (submodules_.echo_cancellation) {
Per Åhgrenf204faf2019-04-25 15:18:06 +02001014 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001015 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1016 num_reverse_channels(),
1017 &aec_render_queue_buffer_);
1018
Per Åhgrenf204faf2019-04-25 15:18:06 +02001019 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1020 // The data queue is full and needs to be emptied.
1021 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001022
Per Åhgrenf204faf2019-04-25 15:18:06 +02001023 // Retry the insert (should always work).
1024 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1025 RTC_DCHECK(result);
1026 }
peaha0624602016-10-25 04:45:24 -07001027 }
1028
saza1d600522019-10-18 13:29:43 +02001029 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001030 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1031 num_reverse_channels(),
1032 &aecm_render_queue_buffer_);
1033 RTC_DCHECK(aecm_render_signal_queue_);
1034 // Insert the samples into the queue.
1035 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1036 // The data queue is full and needs to be emptied.
1037 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001038
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001039 // Retry the insert (should always work).
1040 bool result =
1041 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1042 RTC_DCHECK(result);
1043 }
peah764e3642016-10-22 05:04:30 -07001044 }
peah701d6282016-10-25 05:42:20 -07001045
1046 if (!constants_.use_experimental_agc) {
1047 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1048 // Insert the samples into the queue.
1049 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1050 // The data queue is full and needs to be emptied.
1051 EmptyQueuedRenderAudio();
1052
1053 // Retry the insert (should always work).
1054 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1055 RTC_DCHECK(result);
1056 }
1057 }
peah9e6a2902017-05-15 07:19:21 -07001058}
ivoc9f4a4a02016-10-28 05:39:16 -07001059
peah9e6a2902017-05-15 07:19:21 -07001060void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001061 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1062
1063 // Insert the samples into the queue.
1064 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1065 // The data queue is full and needs to be emptied.
1066 EmptyQueuedRenderAudio();
1067
1068 // Retry the insert (should always work).
1069 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1070 RTC_DCHECK(result);
1071 }
peah764e3642016-10-22 05:04:30 -07001072}
1073
1074void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001075 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001076 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001077
ivoc9f4a4a02016-10-28 05:39:16 -07001078 const size_t new_red_render_queue_element_max_size =
1079 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1080
peaha0624602016-10-25 04:45:24 -07001081 // Reallocate the queues if the queue item sizes are too small to fit the
1082 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001083
1084 if (agc_render_queue_element_max_size_ <
1085 new_agc_render_queue_element_max_size) {
1086 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1087
1088 std::vector<int16_t> template_queue_element(
1089 agc_render_queue_element_max_size_);
1090
1091 agc_render_signal_queue_.reset(
1092 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1093 kMaxNumFramesToBuffer, template_queue_element,
1094 RenderQueueItemVerifier<int16_t>(
1095 agc_render_queue_element_max_size_)));
1096
1097 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1098 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1099 } else {
1100 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001101 }
ivoc9f4a4a02016-10-28 05:39:16 -07001102
1103 if (red_render_queue_element_max_size_ <
1104 new_red_render_queue_element_max_size) {
1105 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1106
1107 std::vector<float> template_queue_element(
1108 red_render_queue_element_max_size_);
1109
1110 red_render_signal_queue_.reset(
1111 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1112 kMaxNumFramesToBuffer, template_queue_element,
1113 RenderQueueItemVerifier<float>(
1114 red_render_queue_element_max_size_)));
1115
1116 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1117 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1118 } else {
1119 red_render_signal_queue_->Clear();
1120 }
peah764e3642016-10-22 05:04:30 -07001121}
1122
1123void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1124 rtc::CritScope cs_capture(&crit_capture_);
saza1d600522019-10-18 13:29:43 +02001125 if (submodules_.echo_cancellation) {
Per Åhgrenf204faf2019-04-25 15:18:06 +02001126 RTC_DCHECK(aec_render_signal_queue_);
1127 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001128 submodules_.echo_cancellation->ProcessRenderAudio(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001129 aec_capture_queue_buffer_);
1130 }
peaha0624602016-10-25 04:45:24 -07001131 }
1132
saza1d600522019-10-18 13:29:43 +02001133 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001134 RTC_DCHECK(aecm_render_signal_queue_);
1135 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001136 submodules_.echo_control_mobile->ProcessRenderAudio(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001137 aecm_capture_queue_buffer_);
1138 }
peah701d6282016-10-25 05:42:20 -07001139 }
1140
1141 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001142 submodules_.gain_control->ProcessRenderAudio(agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001143 }
ivoc9f4a4a02016-10-28 05:39:16 -07001144
1145 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001146 RTC_DCHECK(submodules_.echo_detector);
1147 submodules_.echo_detector->AnalyzeRenderAudio(red_capture_queue_buffer_);
ivoc9f4a4a02016-10-28 05:39:16 -07001148 }
peah764e3642016-10-22 05:04:30 -07001149}
1150
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001151int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001152 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001153 {
1154 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001155 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001156 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001157 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001158 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001159 }
peahfa6228e2015-11-16 16:27:42 -08001160
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001161 if (!frame) {
1162 return kNullPointerError;
1163 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001164 // Must be a native rate.
1165 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1166 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001167 frame->sample_rate_hz_ != kSampleRate32kHz &&
1168 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001169 return kBadSampleRateError;
1170 }
peah192164e2015-11-17 02:16:45 -08001171
peahdf3efa82015-11-28 12:35:15 -08001172 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001173 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001174 {
1175 // Aquire lock for the access of api_format.
1176 // The lock is released immediately due to the conditional
1177 // reinitialization.
1178 rtc::CritScope cs_capture(&crit_capture_);
1179 // TODO(ajm): The input and output rates and channels are currently
1180 // constrained to be identical in the int16 interface.
1181 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001182
1183 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001184 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001185
Oskar Sundbom4b276482019-05-23 14:28:00 +02001186 reinitialization_required =
1187 reinitialization_required ||
1188 processing_config.input_stream().sample_rate_hz() !=
1189 frame->sample_rate_hz_ ||
1190 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1191 processing_config.output_stream().sample_rate_hz() !=
1192 frame->sample_rate_hz_ ||
1193 processing_config.output_stream().num_channels() != frame->num_channels_;
1194
1195 if (reinitialization_required) {
1196 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1197 processing_config.input_stream().set_num_channels(frame->num_channels_);
1198 processing_config.output_stream().set_sample_rate_hz(
1199 frame->sample_rate_hz_);
1200 processing_config.output_stream().set_num_channels(frame->num_channels_);
1201
1202 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001203 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001204 rtc::CritScope cs_capture(&crit_capture_);
1205 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001206 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001207
peahdf3efa82015-11-28 12:35:15 -08001208 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001209 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001210 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001211 return kBadDataLengthError;
1212 }
1213
aleloi868f32f2017-05-23 07:20:05 -07001214 if (aec_dump_) {
1215 RecordUnprocessedCaptureStream(*frame);
1216 }
1217
Per Åhgrend47941e2019-08-22 11:51:13 +02001218 capture_.capture_audio->CopyFrom(frame);
Gustaf Ullberg3c918b12019-10-11 13:14:44 +02001219 if (capture_.capture_fullband_audio) {
1220 capture_.capture_fullband_audio->CopyFrom(frame);
1221 }
peahde65ddc2016-09-16 15:02:15 -07001222 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001223 if (submodule_states_.CaptureMultiBandProcessingPresent() ||
Per Åhgrena1351272019-08-15 12:15:46 +02001224 submodule_states_.CaptureFullBandProcessingActive()) {
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001225 if (capture_.capture_fullband_audio) {
1226 capture_.capture_fullband_audio->CopyTo(frame);
1227 } else {
1228 capture_.capture_audio->CopyTo(frame);
1229 }
Per Åhgrena1351272019-08-15 12:15:46 +02001230 }
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001231 if (capture_.stats.voice_detected) {
1232 frame->vad_activity_ = *capture_.stats.voice_detected
1233 ? AudioFrame::kVadActive
1234 : AudioFrame::kVadPassive;
1235 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001236
aleloi868f32f2017-05-23 07:20:05 -07001237 if (aec_dump_) {
1238 RecordProcessedCaptureStream(*frame);
1239 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001240
1241 return kNoError;
1242}
1243
peahde65ddc2016-09-16 15:02:15 -07001244int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001245 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001246
peahb58a1582016-03-15 09:34:24 -07001247 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001248 // TODO(peah): Simplify once the public API Enable functions for these
1249 // are moved to APM.
saza1d600522019-10-18 13:29:43 +02001250 RTC_DCHECK_LE(!!submodules_.echo_controller +
1251 !!submodules_.echo_cancellation +
1252 !!submodules_.echo_control_mobile,
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001253 1);
peahb58a1582016-03-15 09:34:24 -07001254
peahde65ddc2016-09-16 15:02:15 -07001255 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001256
saza1d600522019-10-18 13:29:43 +02001257 if (submodules_.pre_amplifier) {
1258 submodules_.pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001259 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001260 capture_buffer->num_frames()));
1261 }
1262
Per Åhgren928146f2019-08-20 09:19:21 +02001263 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001264 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001265 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001266 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1267 if (log_rms) {
1268 capture_rms_interval_counter_ = 0;
1269 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001270 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1271 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1272 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1273 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001274 }
1275
saza1d600522019-10-18 13:29:43 +02001276 if (submodules_.echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001277 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001278 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001279 capture_.echo_path_gain_change =
1280 capture_.prev_analog_mic_level != analog_mic_level &&
1281 capture_.prev_analog_mic_level != -1;
1282 capture_.prev_analog_mic_level = analog_mic_level;
1283
Per Åhgrend2650d12018-10-02 17:00:59 +02001284 // Detect and flag any change in the pre-amplifier gain.
saza1d600522019-10-18 13:29:43 +02001285 if (submodules_.pre_amplifier) {
1286 float pre_amp_gain = submodules_.pre_amplifier->GetGainFactor();
Per Åhgrend2650d12018-10-02 17:00:59 +02001287 capture_.echo_path_gain_change =
1288 capture_.echo_path_gain_change ||
1289 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001290 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001291 capture_.prev_pre_amp_gain = pre_amp_gain;
1292 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001293
1294 // Detect volume change.
1295 capture_.echo_path_gain_change =
1296 capture_.echo_path_gain_change ||
1297 (capture_.prev_playout_volume != capture_.playout_volume &&
1298 capture_.prev_playout_volume >= 0);
1299 capture_.prev_playout_volume = capture_.playout_volume;
1300
saza1d600522019-10-18 13:29:43 +02001301 submodules_.echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001302 }
1303
peahbe615622016-02-13 16:40:47 -08001304 if (constants_.use_experimental_agc &&
saza1d600522019-10-18 13:29:43 +02001305 submodules_.gain_control->is_enabled()) {
1306 submodules_.agc_manager->AnalyzePreProcess(
Per Åhgren928146f2019-08-20 09:19:21 +02001307 capture_buffer->channels_f()[0], capture_buffer->num_channels(),
peahde65ddc2016-09-16 15:02:15 -07001308 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001309
1310 if (constants_.use_experimental_agc_process_before_aec) {
saza1d600522019-10-18 13:29:43 +02001311 submodules_.agc_manager->Process(
Per Åhgrend47941e2019-08-22 11:51:13 +02001312 capture_buffer->channels_const()[0],
Alex Loikod9342442018-09-10 13:59:41 +02001313 capture_nonlocked_.capture_processing_format.num_frames(),
1314 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1315 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001316 }
1317
peah2ace3f92016-09-10 04:42:27 -07001318 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1319 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001320 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1321 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001322 }
1323
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001324 const bool experimental_multi_channel_capture =
1325 config_.pipeline.experimental_multi_channel &&
1326 constants_.experimental_multi_channel_capture_support;
saza1d600522019-10-18 13:29:43 +02001327 if (submodules_.echo_controller && !experimental_multi_channel_capture) {
peah522d71b2017-02-23 05:16:26 -08001328 // Force down-mixing of the number of channels after the detection of
1329 // capture signal saturation.
1330 // TODO(peah): Look into ensuring that this kind of tampering with the
1331 // AudioBuffer functionality should not be needed.
1332 capture_buffer->set_num_channels(1);
1333 }
1334
saza1d600522019-10-18 13:29:43 +02001335 if (submodules_.high_pass_filter) {
1336 submodules_.high_pass_filter->Process(capture_buffer);
peah8271d042016-11-22 07:24:52 -08001337 }
saza1d600522019-10-18 13:29:43 +02001338 RETURN_ON_ERR(submodules_.gain_control->AnalyzeCaptureAudio(capture_buffer));
1339 if (submodules_.noise_suppressor) {
1340 submodules_.noise_suppressor->AnalyzeCaptureAudio(capture_buffer);
saza0bad15f2019-10-16 11:46:11 +02001341 }
peahb58a1582016-03-15 09:34:24 -07001342
saza1d600522019-10-18 13:29:43 +02001343 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001344 // Ensure that the stream delay was set before the call to the
1345 // AECM ProcessCaptureAudio function.
1346 if (!was_stream_delay_set()) {
1347 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001348 }
1349
saza1d600522019-10-18 13:29:43 +02001350 if (submodules_.noise_suppressor) {
1351 submodules_.echo_control_mobile->CopyLowPassReference(capture_buffer);
1352 submodules_.noise_suppressor->ProcessCaptureAudio(capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001353 }
peahe0eae3c2016-12-14 01:16:23 -08001354
saza1d600522019-10-18 13:29:43 +02001355 RETURN_ON_ERR(submodules_.echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001356 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001357 } else {
saza1d600522019-10-18 13:29:43 +02001358 if (submodules_.echo_controller) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001359 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1360
1361 if (was_stream_delay_set()) {
saza1d600522019-10-18 13:29:43 +02001362 submodules_.echo_controller->SetAudioBufferDelay(stream_delay_ms());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001363 }
1364
saza1d600522019-10-18 13:29:43 +02001365 submodules_.echo_controller->ProcessCapture(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001366 capture_buffer, capture_.echo_path_gain_change);
saza1d600522019-10-18 13:29:43 +02001367 } else if (submodules_.echo_cancellation) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001368 // Ensure that the stream delay was set before the call to the
1369 // AEC ProcessCaptureAudio function.
1370 if (!was_stream_delay_set()) {
1371 return AudioProcessing::kStreamParameterNotSetError;
1372 }
1373
saza1d600522019-10-18 13:29:43 +02001374 RETURN_ON_ERR(submodules_.echo_cancellation->ProcessCaptureAudio(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001375 capture_buffer, stream_delay_ms()));
1376 }
1377
saza1d600522019-10-18 13:29:43 +02001378 if (submodules_.noise_suppressor) {
1379 submodules_.noise_suppressor->ProcessCaptureAudio(capture_buffer);
saza0bad15f2019-10-16 11:46:11 +02001380 }
Per Åhgren46537a32017-06-07 10:08:10 +02001381 }
ivoc9f4a4a02016-10-28 05:39:16 -07001382
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001383 if (config_.voice_detection.enabled) {
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001384 capture_.stats.voice_detected =
saza1d600522019-10-18 13:29:43 +02001385 submodules_.voice_detector->ProcessCaptureAudio(capture_buffer);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001386 } else {
1387 capture_.stats.voice_detected = absl::nullopt;
1388 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001389
peahbe615622016-02-13 16:40:47 -08001390 if (constants_.use_experimental_agc &&
saza1d600522019-10-18 13:29:43 +02001391 submodules_.gain_control->is_enabled() &&
Alex Loikod9342442018-09-10 13:59:41 +02001392 !constants_.use_experimental_agc_process_before_aec) {
saza1d600522019-10-18 13:29:43 +02001393 submodules_.agc_manager->Process(
Per Åhgren928146f2019-08-20 09:19:21 +02001394 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
peahde65ddc2016-09-16 15:02:15 -07001395 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001396 }
Per Åhgren200feba2019-03-06 04:16:46 +01001397 // TODO(peah): Add reporting from AEC3 whether there is echo.
saza1d600522019-10-18 13:29:43 +02001398 RETURN_ON_ERR(submodules_.gain_control->ProcessCaptureAudio(
1399 capture_buffer, submodules_.echo_cancellation &&
1400 submodules_.echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001401
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001402 if (submodule_states_.CaptureMultiBandProcessingPresent() &&
peah2ace3f92016-09-10 04:42:27 -07001403 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001404 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1405 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001406 }
1407
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001408 if (capture_.capture_fullband_audio) {
saza1d600522019-10-18 13:29:43 +02001409 const auto& ec = submodules_.echo_controller;
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001410 bool ec_active = ec ? ec->ActiveProcessing() : false;
1411 // Only update the fullband buffer if the multiband processing has changed
1412 // the signal. Keep the original signal otherwise.
1413 if (submodule_states_.CaptureMultiBandProcessingActive(ec_active)) {
1414 capture_buffer->CopyTo(capture_.capture_fullband_audio.get());
1415 }
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001416 capture_buffer = capture_.capture_fullband_audio.get();
1417 }
1418
peah9e6a2902017-05-15 07:19:21 -07001419 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001420 RTC_DCHECK(submodules_.echo_detector);
1421 submodules_.echo_detector->AnalyzeCaptureAudio(rtc::ArrayView<const float>(
1422 capture_buffer->channels()[0], capture_buffer->num_frames()));
peah9e6a2902017-05-15 07:19:21 -07001423 }
1424
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001425 // TODO(aluebs): Investigate if the transient suppression placement should be
1426 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001427 if (capture_.transient_suppressor_enabled) {
saza1d600522019-10-18 13:29:43 +02001428 float voice_probability = submodules_.agc_manager.get()
1429 ? submodules_.agc_manager->voice_probability()
1430 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001431
saza1d600522019-10-18 13:29:43 +02001432 submodules_.transient_suppressor->Suppress(
Per Åhgrend47941e2019-08-22 11:51:13 +02001433 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001434 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001435 capture_buffer->split_bands_const(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001436 capture_buffer->num_frames_per_band(),
1437 capture_.keyboard_info.keyboard_data,
1438 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001439 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001440 }
1441
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001442 // Experimental APM sub-module that analyzes |capture_buffer|.
saza1d600522019-10-18 13:29:43 +02001443 if (submodules_.capture_analyzer) {
1444 submodules_.capture_analyzer->Analyze(capture_buffer);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001445 }
1446
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001447 if (config_.gain_controller2.enabled) {
saza1d600522019-10-18 13:29:43 +02001448 submodules_.gain_controller2->NotifyAnalogLevel(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001449 agc1()->stream_analog_level());
saza1d600522019-10-18 13:29:43 +02001450 submodules_.gain_controller2->Process(capture_buffer);
alessiob3ec96df2017-05-22 06:57:06 -07001451 }
1452
saza1d600522019-10-18 13:29:43 +02001453 if (submodules_.capture_post_processor) {
1454 submodules_.capture_post_processor->Process(capture_buffer);
Sam Zackrisson0beac582017-09-25 12:04:02 +02001455 }
1456
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001457 // The level estimator operates on the recombined data.
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001458 if (config_.level_estimation.enabled) {
saza1d600522019-10-18 13:29:43 +02001459 submodules_.output_level_estimator->ProcessStream(*capture_buffer);
1460 capture_.stats.output_rms_dbfs = submodules_.output_level_estimator->RMS();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001461 } else {
1462 capture_.stats.output_rms_dbfs = absl::nullopt;
1463 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001464
Per Åhgren928146f2019-08-20 09:19:21 +02001465 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001466 capture_buffer->channels_const()[0],
peah1b08dc32016-12-20 13:45:58 -08001467 capture_nonlocked_.capture_processing_format.num_frames()));
1468 if (log_rms) {
1469 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1470 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1471 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1472 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1473 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1474 }
1475
peahdf3efa82015-11-28 12:35:15 -08001476 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001477 return kNoError;
1478}
1479
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001480int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001481 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001482 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001483 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001484 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001485 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001486 const StreamConfig reverse_config = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001487 sample_rate_hz,
1488 ChannelsFromLayout(layout),
1489 LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001490 };
1491 if (samples_per_channel != reverse_config.num_frames()) {
1492 return kBadDataLengthError;
1493 }
peahdf3efa82015-11-28 12:35:15 -08001494 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001495}
1496
Gustaf Ullberg8c51f2e2019-10-22 15:21:31 +02001497int AudioProcessingImpl::AnalyzeReverseStream(
1498 const float* const* data,
1499 const StreamConfig& reverse_config) {
1500 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_StreamConfig");
1501 rtc::CritScope cs(&crit_render_);
1502 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
1503}
1504
peahde65ddc2016-09-16 15:02:15 -07001505int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1506 const StreamConfig& input_config,
1507 const StreamConfig& output_config,
1508 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001509 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001510 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001511 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001512 if (submodule_states_.RenderMultiBandProcessingActive() ||
1513 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001514 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1515 dest);
peah2ace3f92016-09-10 04:42:27 -07001516 } else if (formats_.api_format.reverse_input_stream() !=
1517 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001518 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1519 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001520 } else {
peahde65ddc2016-09-16 15:02:15 -07001521 CopyAudioIfNeeded(src, input_config.num_frames(),
1522 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001523 }
1524
1525 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001526}
1527
peahdf3efa82015-11-28 12:35:15 -08001528int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001529 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001530 const StreamConfig& input_config,
1531 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001532 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001533 return kNullPointerError;
1534 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001535
peahde65ddc2016-09-16 15:02:15 -07001536 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001537 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001538 }
1539
peahdf3efa82015-11-28 12:35:15 -08001540 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001541 processing_config.reverse_input_stream() = input_config;
1542 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001543
peahdf3efa82015-11-28 12:35:15 -08001544 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001545 RTC_DCHECK_EQ(input_config.num_frames(),
1546 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001547
aleloi868f32f2017-05-23 07:20:05 -07001548 if (aec_dump_) {
1549 const size_t channel_size =
1550 formats_.api_format.reverse_input_stream().num_frames();
1551 const size_t num_channels =
1552 formats_.api_format.reverse_input_stream().num_channels();
1553 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001554 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001555 }
peahdf3efa82015-11-28 12:35:15 -08001556 render_.render_audio->CopyFrom(src,
1557 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001558 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001559}
1560
1561int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001562 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001563 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001564 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001565 return kNullPointerError;
1566 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001567 // Must be a native rate.
1568 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1569 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001570 frame->sample_rate_hz_ != kSampleRate32kHz &&
1571 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001572 return kBadSampleRateError;
1573 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001574
Michael Graczyk86c6d332015-07-23 11:41:39 -07001575 if (frame->num_channels_ <= 0) {
1576 return kBadNumberChannelsError;
1577 }
1578
peahdf3efa82015-11-28 12:35:15 -08001579 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001580 processing_config.reverse_input_stream().set_sample_rate_hz(
1581 frame->sample_rate_hz_);
1582 processing_config.reverse_input_stream().set_num_channels(
1583 frame->num_channels_);
1584 processing_config.reverse_output_stream().set_sample_rate_hz(
1585 frame->sample_rate_hz_);
1586 processing_config.reverse_output_stream().set_num_channels(
1587 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001588
peahdf3efa82015-11-28 12:35:15 -08001589 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001590 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001591 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001592 return kBadDataLengthError;
1593 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001594
aleloi868f32f2017-05-23 07:20:05 -07001595 if (aec_dump_) {
1596 aec_dump_->WriteRenderStreamMessage(*frame);
1597 }
1598
Per Åhgrend47941e2019-08-22 11:51:13 +02001599 render_.render_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001600 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001601 if (submodule_states_.RenderMultiBandProcessingActive() ||
1602 submodule_states_.RenderFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001603 render_.render_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001604 }
aluebsb0319552016-03-17 20:39:53 -07001605 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001606}
niklase@google.com470e71d2011-07-07 08:21:25 +00001607
peahde65ddc2016-09-16 15:02:15 -07001608int AudioProcessingImpl::ProcessRenderStreamLocked() {
1609 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001610
Alex Loiko73ec0192018-05-15 10:52:28 +02001611 HandleRenderRuntimeSettings();
1612
saza1d600522019-10-18 13:29:43 +02001613 if (submodules_.render_pre_processor) {
1614 submodules_.render_pre_processor->Process(render_buffer);
Alex Loiko5825aa62017-12-18 16:02:40 +01001615 }
1616
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001617 QueueNonbandedRenderAudio(render_buffer);
1618
peah2ace3f92016-09-10 04:42:27 -07001619 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001620 SampleRateSupportsMultiBand(
1621 formats_.render_processing_format.sample_rate_hz())) {
1622 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001623 }
1624
peahce4d9152017-05-19 01:28:05 -07001625 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1626 QueueBandedRenderAudio(render_buffer);
1627 }
1628
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001629 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
saza1d600522019-10-18 13:29:43 +02001630 if (submodules_.echo_controller) {
1631 submodules_.echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001632 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001633
peah2ace3f92016-09-10 04:42:27 -07001634 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001635 SampleRateSupportsMultiBand(
1636 formats_.render_processing_format.sample_rate_hz())) {
1637 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001638 }
1639
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001640 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001641}
1642
1643int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001644 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001645 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001646 capture_.was_stream_delay_set = true;
1647 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001648
niklase@google.com470e71d2011-07-07 08:21:25 +00001649 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001650 delay = 0;
1651 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001652 }
1653
1654 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1655 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001656 delay = 500;
1657 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001658 }
1659
peahdf3efa82015-11-28 12:35:15 -08001660 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001661 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001662}
1663
1664int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001665 // Used as callback from submodules, hence locking is not allowed.
1666 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001667}
1668
1669bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001670 // Used as callback from submodules, hence locking is not allowed.
1671 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001672}
1673
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001674void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001675 rtc::CritScope cs(&crit_capture_);
1676 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001677}
1678
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001679void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001680 rtc::CritScope cs(&crit_capture_);
1681 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001682}
1683
1684int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001685 rtc::CritScope cs(&crit_capture_);
1686 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001687}
1688
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001689void AudioProcessingImpl::set_stream_analog_level(int level) {
1690 rtc::CritScope cs_capture(&crit_capture_);
1691 int error = agc1()->set_stream_analog_level(level);
1692 RTC_DCHECK_EQ(kNoError, error);
1693}
1694
1695int AudioProcessingImpl::recommended_stream_analog_level() const {
1696 rtc::CritScope cs_capture(&crit_capture_);
1697 return agc1()->stream_analog_level();
1698}
1699
aleloi868f32f2017-05-23 07:20:05 -07001700void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1701 RTC_DCHECK(aec_dump);
1702 rtc::CritScope cs_render(&crit_render_);
1703 rtc::CritScope cs_capture(&crit_capture_);
1704
1705 // The previously attached AecDump will be destroyed with the
1706 // 'aec_dump' parameter, which is after locks are released.
1707 aec_dump_.swap(aec_dump);
1708 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001709 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001710}
1711
1712void AudioProcessingImpl::DetachAecDump() {
1713 // The d-tor of a task-queue based AecDump blocks until all pending
1714 // tasks are done. This construction avoids blocking while holding
1715 // the render and capture locks.
1716 std::unique_ptr<AecDump> aec_dump = nullptr;
1717 {
1718 rtc::CritScope cs_render(&crit_render_);
1719 rtc::CritScope cs_capture(&crit_capture_);
1720 aec_dump = std::move(aec_dump_);
1721 }
1722}
1723
Sam Zackrisson4d364492018-03-02 16:03:21 +01001724void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1725 std::unique_ptr<AudioGenerator> audio_generator) {
1726 // TODO(bugs.webrtc.org/8882) Stub.
1727 // Reset internal audio generator with audio_generator.
1728}
1729
1730void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1731 // TODO(bugs.webrtc.org/8882) Stub.
1732 // Delete audio generator, if one is attached.
1733}
1734
Ivo Creusen56d46092017-11-24 17:29:59 +01001735AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001736 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001737 rtc::CritScope cs_capture(&crit_capture_);
1738 if (!has_remote_tracks) {
1739 return capture_.stats;
1740 }
1741 AudioProcessingStats stats = capture_.stats;
1742 EchoCancellationImpl::Metrics metrics;
saza1d600522019-10-18 13:29:43 +02001743 if (submodules_.echo_controller) {
1744 auto ec_metrics = submodules_.echo_controller->GetMetrics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001745 stats.echo_return_loss = ec_metrics.echo_return_loss;
1746 stats.echo_return_loss_enhancement =
1747 ec_metrics.echo_return_loss_enhancement;
1748 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001749 }
1750 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001751 RTC_DCHECK(submodules_.echo_detector);
1752 auto ed_metrics = submodules_.echo_detector->GetMetrics();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001753 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1754 stats.residual_echo_likelihood_recent_max =
1755 ed_metrics.echo_likelihood_recent_max;
1756 }
Ivo Creusenae026092017-11-20 13:07:16 +01001757 return stats;
1758}
1759
peah8271d042016-11-22 07:24:52 -08001760void AudioProcessingImpl::MutateConfig(
1761 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1762 rtc::CritScope cs_render(&crit_render_);
1763 rtc::CritScope cs_capture(&crit_capture_);
1764 mutator(&config_);
1765 ApplyConfig(config_);
1766}
1767
1768AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1769 rtc::CritScope cs_render(&crit_render_);
1770 rtc::CritScope cs_capture(&crit_capture_);
1771 return config_;
1772}
1773
peah2ace3f92016-09-10 04:42:27 -07001774bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1775 return submodule_states_.Update(
saza1d600522019-10-18 13:29:43 +02001776 config_.high_pass_filter.enabled, !!submodules_.echo_cancellation,
1777 !!submodules_.echo_control_mobile, config_.residual_echo_detector.enabled,
1778 !!submodules_.noise_suppressor, submodules_.gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001779 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001780 capture_nonlocked_.echo_controller_enabled,
saza0bad15f2019-10-16 11:46:11 +02001781 config_.voice_detection.enabled, capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001782}
1783
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001784void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001785 if (capture_.transient_suppressor_enabled) {
saza1d600522019-10-18 13:29:43 +02001786 if (!submodules_.transient_suppressor.get()) {
1787 submodules_.transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001788 }
saza1d600522019-10-18 13:29:43 +02001789 submodules_.transient_suppressor->Initialize(proc_fullband_sample_rate_hz(),
1790 capture_nonlocked_.split_rate,
1791 num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001792 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001793}
1794
Per Åhgren0aefbf02019-08-23 21:29:17 +02001795void AudioProcessingImpl::InitializeHighPassFilter() {
1796 if (submodule_states_.HighPassFilteringRequired()) {
saza1d600522019-10-18 13:29:43 +02001797 submodules_.high_pass_filter.reset(new HighPassFilter(num_proc_channels()));
peah8271d042016-11-22 07:24:52 -08001798 } else {
saza1d600522019-10-18 13:29:43 +02001799 submodules_.high_pass_filter.reset();
peah8271d042016-11-22 07:24:52 -08001800 }
1801}
alessiob3ec96df2017-05-22 06:57:06 -07001802
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001803void AudioProcessingImpl::InitializeVoiceDetector() {
1804 if (config_.voice_detection.enabled) {
saza1d600522019-10-18 13:29:43 +02001805 submodules_.voice_detector = std::make_unique<VoiceDetection>(
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001806 proc_split_sample_rate_hz(), VoiceDetection::kVeryLowLikelihood);
1807 } else {
saza1d600522019-10-18 13:29:43 +02001808 submodules_.voice_detector.reset();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001809 }
1810}
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001811void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001812 bool use_echo_controller =
1813 echo_control_factory_ ||
Per Åhgren200feba2019-03-06 04:16:46 +01001814 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode &&
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001815 !config_.echo_canceller.use_legacy_aec);
1816
1817 if (use_echo_controller) {
1818 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001819 if (echo_control_factory_) {
saza1d600522019-10-18 13:29:43 +02001820 submodules_.echo_controller =
Per Åhgren200feba2019-03-06 04:16:46 +01001821 echo_control_factory_->Create(proc_sample_rate_hz());
1822 } else {
saza1d600522019-10-18 13:29:43 +02001823 submodules_.echo_controller = std::make_unique<EchoCanceller3>(
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001824 EchoCanceller3Config(), proc_sample_rate_hz(), num_reverse_channels(),
1825 num_proc_channels());
Per Åhgren200feba2019-03-06 04:16:46 +01001826 }
1827
1828 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001829
saza1d600522019-10-18 13:29:43 +02001830 submodules_.echo_cancellation.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001831 aec_render_signal_queue_.reset();
saza1d600522019-10-18 13:29:43 +02001832 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001833 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001834 return;
peahe0eae3c2016-12-14 01:16:23 -08001835 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001836
saza1d600522019-10-18 13:29:43 +02001837 submodules_.echo_controller.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001838 capture_nonlocked_.echo_controller_enabled = false;
1839
1840 if (!config_.echo_canceller.enabled) {
saza1d600522019-10-18 13:29:43 +02001841 submodules_.echo_cancellation.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001842 aec_render_signal_queue_.reset();
saza1d600522019-10-18 13:29:43 +02001843 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001844 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001845 return;
1846 }
1847
1848 if (config_.echo_canceller.mobile_mode) {
1849 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001850 size_t max_element_size =
1851 std::max(static_cast<size_t>(1),
1852 kMaxAllowedValuesOfSamplesPerBand *
1853 EchoControlMobileImpl::NumCancellersRequired(
1854 num_output_channels(), num_reverse_channels()));
1855
1856 std::vector<int16_t> template_queue_element(max_element_size);
1857
1858 aecm_render_signal_queue_.reset(
1859 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1860 kMaxNumFramesToBuffer, template_queue_element,
1861 RenderQueueItemVerifier<int16_t>(max_element_size)));
1862
1863 aecm_render_queue_buffer_.resize(max_element_size);
1864 aecm_capture_queue_buffer_.resize(max_element_size);
1865
saza1d600522019-10-18 13:29:43 +02001866 submodules_.echo_control_mobile.reset(new EchoControlMobileImpl());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001867
saza1d600522019-10-18 13:29:43 +02001868 submodules_.echo_control_mobile->Initialize(proc_split_sample_rate_hz(),
1869 num_reverse_channels(),
1870 num_output_channels());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001871
saza1d600522019-10-18 13:29:43 +02001872 submodules_.echo_cancellation.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001873 aec_render_signal_queue_.reset();
1874 return;
1875 }
1876
saza1d600522019-10-18 13:29:43 +02001877 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001878 aecm_render_signal_queue_.reset();
1879
Per Åhgrenf204faf2019-04-25 15:18:06 +02001880 // Create and activate AEC2.
saza1d600522019-10-18 13:29:43 +02001881 submodules_.echo_cancellation.reset(new EchoCancellationImpl());
1882 submodules_.echo_cancellation->SetExtraOptions(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001883 capture_nonlocked_.use_aec2_extended_filter,
1884 capture_nonlocked_.use_aec2_delay_agnostic,
1885 capture_nonlocked_.use_aec2_refined_adaptive_filter);
1886
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001887 size_t element_max_size =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001888 std::max(static_cast<size_t>(1),
1889 kMaxAllowedValuesOfSamplesPerBand *
1890 EchoCancellationImpl::NumCancellersRequired(
1891 num_output_channels(), num_reverse_channels()));
1892
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001893 std::vector<float> template_queue_element(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001894
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001895 aec_render_signal_queue_.reset(
1896 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1897 kMaxNumFramesToBuffer, template_queue_element,
1898 RenderQueueItemVerifier<float>(element_max_size)));
Per Åhgrenf204faf2019-04-25 15:18:06 +02001899
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001900 aec_render_queue_buffer_.resize(element_max_size);
1901 aec_capture_queue_buffer_.resize(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001902
saza1d600522019-10-18 13:29:43 +02001903 submodules_.echo_cancellation->Initialize(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001904 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
1905 num_proc_channels());
1906
saza1d600522019-10-18 13:29:43 +02001907 submodules_.echo_cancellation->set_suppression_level(
Per Åhgrenf204faf2019-04-25 15:18:06 +02001908 config_.echo_canceller.legacy_moderate_suppression_level
1909 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
1910 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
peahe0eae3c2016-12-14 01:16:23 -08001911}
peah8271d042016-11-22 07:24:52 -08001912
alessiob3ec96df2017-05-22 06:57:06 -07001913void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001914 if (config_.gain_controller2.enabled) {
saza1d600522019-10-18 13:29:43 +02001915 submodules_.gain_controller2->Initialize(proc_fullband_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001916 }
1917}
1918
saza0bad15f2019-10-16 11:46:11 +02001919void AudioProcessingImpl::InitializeNoiseSuppressor() {
1920 if (config_.noise_suppression.enabled) {
1921 auto ns_level =
1922 NsConfigLevelToInterfaceLevel(config_.noise_suppression.level);
saza1d600522019-10-18 13:29:43 +02001923 submodules_.noise_suppressor = std::make_unique<NoiseSuppression>(
saza0bad15f2019-10-16 11:46:11 +02001924 num_proc_channels(), proc_sample_rate_hz(), ns_level);
1925 } else {
saza1d600522019-10-18 13:29:43 +02001926 submodules_.noise_suppressor.reset();
saza0bad15f2019-10-16 11:46:11 +02001927 }
1928}
1929
Alex Loikob5c9a792018-04-16 16:31:22 +02001930void AudioProcessingImpl::InitializePreAmplifier() {
1931 if (config_.pre_amplifier.enabled) {
saza1d600522019-10-18 13:29:43 +02001932 submodules_.pre_amplifier.reset(
Alex Loikob5c9a792018-04-16 16:31:22 +02001933 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1934 } else {
saza1d600522019-10-18 13:29:43 +02001935 submodules_.pre_amplifier.reset();
Alex Loikob5c9a792018-04-16 16:31:22 +02001936 }
1937}
1938
ivoc9f4a4a02016-10-28 05:39:16 -07001939void AudioProcessingImpl::InitializeResidualEchoDetector() {
saza1d600522019-10-18 13:29:43 +02001940 RTC_DCHECK(submodules_.echo_detector);
1941 submodules_.echo_detector->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001942 proc_fullband_sample_rate_hz(), 1,
Ivo Creusenb1facc12018-04-12 16:15:58 +02001943 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001944}
1945
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001946void AudioProcessingImpl::InitializeAnalyzer() {
saza1d600522019-10-18 13:29:43 +02001947 if (submodules_.capture_analyzer) {
1948 submodules_.capture_analyzer->Initialize(proc_fullband_sample_rate_hz(),
1949 num_proc_channels());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001950 }
1951}
1952
Sam Zackrisson0beac582017-09-25 12:04:02 +02001953void AudioProcessingImpl::InitializePostProcessor() {
saza1d600522019-10-18 13:29:43 +02001954 if (submodules_.capture_post_processor) {
1955 submodules_.capture_post_processor->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001956 proc_fullband_sample_rate_hz(), num_proc_channels());
Sam Zackrisson0beac582017-09-25 12:04:02 +02001957 }
1958}
1959
Alex Loiko5825aa62017-12-18 16:02:40 +01001960void AudioProcessingImpl::InitializePreProcessor() {
saza1d600522019-10-18 13:29:43 +02001961 if (submodules_.render_pre_processor) {
1962 submodules_.render_pre_processor->Initialize(
Alex Loiko5825aa62017-12-18 16:02:40 +01001963 formats_.render_processing_format.sample_rate_hz(),
1964 formats_.render_processing_format.num_channels());
1965 }
1966}
1967
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001968void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001969
aleloi868f32f2017-05-23 07:20:05 -07001970void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1971 if (!aec_dump_) {
1972 return;
1973 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001974
1975 std::string experiments_description = "";
saza1d600522019-10-18 13:29:43 +02001976 if (submodules_.echo_cancellation) {
Per Åhgrenf204faf2019-04-25 15:18:06 +02001977 experiments_description +=
saza1d600522019-10-18 13:29:43 +02001978 submodules_.echo_cancellation->GetExperimentsDescription();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001979 }
aleloi868f32f2017-05-23 07:20:05 -07001980 // TODO(peah): Add semicolon-separated concatenations of experiment
1981 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001982 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1983 experiments_description += "AgcClippingLevelExperiment;";
1984 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001985 if (capture_nonlocked_.echo_controller_enabled) {
1986 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001987 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001988 if (config_.gain_controller2.enabled) {
1989 experiments_description += "GainController2;";
1990 }
aleloi868f32f2017-05-23 07:20:05 -07001991
1992 InternalAPMConfig apm_config;
1993
Per Åhgren200feba2019-03-06 04:16:46 +01001994 apm_config.aec_enabled = config_.echo_canceller.enabled;
aleloi868f32f2017-05-23 07:20:05 -07001995 apm_config.aec_delay_agnostic_enabled =
saza1d600522019-10-18 13:29:43 +02001996 submodules_.echo_cancellation &&
1997 submodules_.echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07001998 apm_config.aec_drift_compensation_enabled =
saza1d600522019-10-18 13:29:43 +02001999 submodules_.echo_cancellation &&
2000 submodules_.echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002001 apm_config.aec_extended_filter_enabled =
saza1d600522019-10-18 13:29:43 +02002002 submodules_.echo_cancellation &&
2003 submodules_.echo_cancellation->is_extended_filter_enabled();
Per Åhgrenf204faf2019-04-25 15:18:06 +02002004 apm_config.aec_suppression_level =
saza1d600522019-10-18 13:29:43 +02002005 submodules_.echo_cancellation
2006 ? static_cast<int>(submodules_.echo_cancellation->suppression_level())
Per Åhgrenf204faf2019-04-25 15:18:06 +02002007 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002008
saza1d600522019-10-18 13:29:43 +02002009 apm_config.aecm_enabled = !!submodules_.echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002010 apm_config.aecm_comfort_noise_enabled =
saza1d600522019-10-18 13:29:43 +02002011 submodules_.echo_control_mobile &&
2012 submodules_.echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002013 apm_config.aecm_routing_mode =
saza1d600522019-10-18 13:29:43 +02002014 submodules_.echo_control_mobile
2015 ? static_cast<int>(submodules_.echo_control_mobile->routing_mode())
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002016 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002017
saza1d600522019-10-18 13:29:43 +02002018 apm_config.agc_enabled = submodules_.gain_control->is_enabled();
2019 apm_config.agc_mode = static_cast<int>(submodules_.gain_control->mode());
aleloi868f32f2017-05-23 07:20:05 -07002020 apm_config.agc_limiter_enabled =
saza1d600522019-10-18 13:29:43 +02002021 submodules_.gain_control->is_limiter_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002022 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2023
2024 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2025
saza0bad15f2019-10-16 11:46:11 +02002026 apm_config.ns_enabled = config_.noise_suppression.enabled;
2027 apm_config.ns_level = static_cast<int>(config_.noise_suppression.level);
aleloi868f32f2017-05-23 07:20:05 -07002028
2029 apm_config.transient_suppression_enabled =
2030 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002031 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002032 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2033 apm_config.pre_amplifier_fixed_gain_factor =
2034 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002035
2036 if (!forced && apm_config == apm_config_for_aec_dump_) {
2037 return;
2038 }
2039 aec_dump_->WriteConfig(apm_config);
2040 apm_config_for_aec_dump_ = apm_config;
2041}
2042
2043void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2044 const float* const* src) {
2045 RTC_DCHECK(aec_dump_);
2046 WriteAecDumpConfigMessage(false);
2047
2048 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2049 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2050 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002051 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002052 RecordAudioProcessingState();
2053}
2054
2055void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2056 const AudioFrame& capture_frame) {
2057 RTC_DCHECK(aec_dump_);
2058 WriteAecDumpConfigMessage(false);
2059
2060 aec_dump_->AddCaptureStreamInput(capture_frame);
2061 RecordAudioProcessingState();
2062}
2063
2064void AudioProcessingImpl::RecordProcessedCaptureStream(
2065 const float* const* processed_capture_stream) {
2066 RTC_DCHECK(aec_dump_);
2067
2068 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2069 const size_t num_channels =
2070 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002071 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2072 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002073 aec_dump_->WriteCaptureStreamMessage();
2074}
2075
2076void AudioProcessingImpl::RecordProcessedCaptureStream(
2077 const AudioFrame& processed_capture_frame) {
2078 RTC_DCHECK(aec_dump_);
2079
2080 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2081 aec_dump_->WriteCaptureStreamMessage();
2082}
2083
2084void AudioProcessingImpl::RecordAudioProcessingState() {
2085 RTC_DCHECK(aec_dump_);
2086 AecDump::AudioProcessingState audio_proc_state;
2087 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2088 audio_proc_state.drift =
saza1d600522019-10-18 13:29:43 +02002089 submodules_.echo_cancellation
2090 ? submodules_.echo_cancellation->stream_drift_samples()
Per Åhgrenf204faf2019-04-25 15:18:06 +02002091 : 0;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01002092 audio_proc_state.level = agc1()->stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002093 audio_proc_state.keypress = capture_.key_pressed;
2094 aec_dump_->AddAudioProcessingState(audio_proc_state);
2095}
2096
kwiberg83ffe452016-08-29 14:46:07 -07002097AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002098 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002099 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002100 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002101 output_will_be_muted(false),
2102 key_pressed(false),
2103 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002104 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002105 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002106 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002107 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002108 prev_pre_amp_gain(-1.f),
2109 playout_volume(-1),
2110 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002111
2112AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2113
Per Åhgrena1351272019-08-15 12:15:46 +02002114void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2115 const float* const* data,
2116 const StreamConfig& stream_config) {
2117 if (stream_config.has_keyboard()) {
2118 keyboard_data = data[stream_config.num_channels()];
2119 } else {
2120 keyboard_data = NULL;
2121 }
2122 num_keyboard_frames = stream_config.num_frames();
2123}
2124
kwiberg83ffe452016-08-29 14:46:07 -07002125AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2126
2127AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2128
niklase@google.com470e71d2011-07-07 08:21:25 +00002129} // namespace webrtc