blob: 81959127a17614cdee0802d3addf411d9288fc37 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org40654032012-01-30 20:51:15 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/audio_processing_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Michael Graczyk86c6d332015-07-23 11:41:39 -070013#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <cstdint>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
alessiob3ec96df2017-05-22 06:57:06 -070016#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <type_traits>
18#include <utility>
niklase@google.com470e71d2011-07-07 08:21:25 +000019
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "absl/types/optional.h"
21#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_audio/audio_converter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_audio/include/audio_util.h"
Alex Loikob5c9a792018-04-16 16:31:22 +020024#include "modules/audio_processing/agc2/gain_applier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/common.h"
Yves Gerey988cc082018-10-23 12:03:01 +020027#include "modules/audio_processing/include/audio_frame_view.h"
Per Åhgren13735822018-02-12 21:42:56 +010028#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080029#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/ref_counted_object.h"
34#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/trace_event.h"
Sam Zackrissonfeee1e42019-09-20 07:50:35 +020036#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000038
Michael Graczyk86c6d332015-07-23 11:41:39 -070039#define RETURN_ON_ERR(expr) \
40 do { \
41 int err = (expr); \
42 if (err != kNoError) { \
43 return err; \
44 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000045 } while (0)
46
niklase@google.com470e71d2011-07-07 08:21:25 +000047namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070048
kwibergd59d3bb2016-09-13 07:49:33 -070049constexpr int AudioProcessing::kNativeSampleRatesHz[];
Alex Loiko73ec0192018-05-15 10:52:28 +020050constexpr int kRuntimeSettingQueueSize = 100;
aluebsdf6416a2016-03-16 18:26:35 -070051
Michael Graczyk86c6d332015-07-23 11:41:39 -070052namespace {
53
54static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
55 switch (layout) {
56 case AudioProcessing::kMono:
57 case AudioProcessing::kStereo:
58 return false;
59 case AudioProcessing::kMonoAndKeyboard:
60 case AudioProcessing::kStereoAndKeyboard:
61 return true;
62 }
63
kwiberg9e2be5f2016-09-14 05:23:22 -070064 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070065 return false;
66}
aluebsdf6416a2016-03-16 18:26:35 -070067
peah2ace3f92016-09-10 04:42:27 -070068bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070069 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
70 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
71}
72
Per Åhgren0cbb58e2019-10-29 22:59:44 +010073// Checks whether the legacy ns functionality should be enforced.
74bool DetectLegacyNsEnforcement() {
75 return field_trial::IsEnabled("WebRTC-NewNoiseSuppressionKillSwitch");
76}
77
Per Åhgrenc0424252019-12-10 13:04:15 +010078// Checks whether the high-pass filter should be done in the full-band.
79bool EnforceSplitBandHpf() {
80 return field_trial::IsEnabled("WebRTC-FullBandHpfKillSwitch");
81}
82
Per Åhgrenb2b58d82019-12-02 14:59:40 +010083// Checks whether AEC3 should be allowed to decide what the default
84// configuration should be based on the render and capture channel configuration
85// at hand.
86bool UseSetupSpecificDefaultAec3Congfig() {
87 return !field_trial::IsEnabled(
88 "WebRTC-Aec3SetupSpecificDefaultConfigDefaultsKillSwitch");
89}
90
Per Åhgrenc8626b62019-08-23 15:49:51 +020091// Identify the native processing rate that best handles a sample rate.
Per Åhgrenfcbe4072019-09-15 00:27:58 +020092int SuitableProcessRate(int minimum_rate,
93 int max_splitting_rate,
94 bool band_splitting_required) {
Per Åhgrenc8626b62019-08-23 15:49:51 +020095 const int uppermost_native_rate =
Per Åhgrenfcbe4072019-09-15 00:27:58 +020096 band_splitting_required ? max_splitting_rate : 48000;
Per Åhgrenc8626b62019-08-23 15:49:51 +020097 for (auto rate : {16000, 32000, 48000}) {
peah2ace3f92016-09-10 04:42:27 -070098 if (rate >= uppermost_native_rate) {
99 return uppermost_native_rate;
100 }
101 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700102 return rate;
103 }
104 }
peah2ace3f92016-09-10 04:42:27 -0700105 RTC_NOTREACHED();
106 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700107}
108
Sam Zackrisson23513132019-01-11 15:10:32 +0100109NoiseSuppression::Level NsConfigLevelToInterfaceLevel(
110 AudioProcessing::Config::NoiseSuppression::Level level) {
111 using NsConfig = AudioProcessing::Config::NoiseSuppression;
112 switch (level) {
113 case NsConfig::kLow:
saza0bad15f2019-10-16 11:46:11 +0200114 return NoiseSuppression::Level::kLow;
Sam Zackrisson23513132019-01-11 15:10:32 +0100115 case NsConfig::kModerate:
saza0bad15f2019-10-16 11:46:11 +0200116 return NoiseSuppression::Level::kModerate;
Sam Zackrisson23513132019-01-11 15:10:32 +0100117 case NsConfig::kHigh:
saza0bad15f2019-10-16 11:46:11 +0200118 return NoiseSuppression::Level::kHigh;
Sam Zackrisson23513132019-01-11 15:10:32 +0100119 case NsConfig::kVeryHigh:
saza0bad15f2019-10-16 11:46:11 +0200120 return NoiseSuppression::Level::kVeryHigh;
Sam Zackrisson23513132019-01-11 15:10:32 +0100121 default:
122 RTC_NOTREACHED();
123 }
124}
125
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100126GainControl::Mode Agc1ConfigModeToInterfaceMode(
127 AudioProcessing::Config::GainController1::Mode mode) {
128 using Agc1Config = AudioProcessing::Config::GainController1;
129 switch (mode) {
130 case Agc1Config::kAdaptiveAnalog:
131 return GainControl::kAdaptiveAnalog;
132 case Agc1Config::kAdaptiveDigital:
133 return GainControl::kAdaptiveDigital;
134 case Agc1Config::kFixedDigital:
135 return GainControl::kFixedDigital;
136 }
137}
138
peah9e6a2902017-05-15 07:19:21 -0700139// Maximum lengths that frame of samples being passed from the render side to
140// the capture side can have (does not apply to AEC3).
141static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
142static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
143
peah764e3642016-10-22 05:04:30 -0700144// Maximum number of frames to buffer in the render queue.
145// TODO(peah): Decrease this once we properly handle hugely unbalanced
146// reverse and forward call numbers.
147static const size_t kMaxNumFramesToBuffer = 100;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700148} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000149
150// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000151static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000152
saza1d600522019-10-18 13:29:43 +0200153AudioProcessingImpl::SubmoduleStates::SubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100154 bool capture_post_processor_enabled,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200155 bool render_pre_processor_enabled,
156 bool capture_analyzer_enabled)
Alex Loiko5825aa62017-12-18 16:02:40 +0100157 : capture_post_processor_enabled_(capture_post_processor_enabled),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200158 render_pre_processor_enabled_(render_pre_processor_enabled),
159 capture_analyzer_enabled_(capture_analyzer_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700160
saza1d600522019-10-18 13:29:43 +0200161bool AudioProcessingImpl::SubmoduleStates::Update(
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200162 bool high_pass_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700163 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700164 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700165 bool noise_suppressor_enabled,
peah2ace3f92016-09-10 04:42:27 -0700166 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700167 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200168 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200169 bool echo_controller_enabled,
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200170 bool voice_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700171 bool transient_suppressor_enabled) {
172 bool changed = false;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200173 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700174 changed |=
175 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700176 changed |=
177 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700178 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
179 changed |=
peah2ace3f92016-09-10 04:42:27 -0700180 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200181 changed |= (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200182 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200183 changed |= (echo_controller_enabled != echo_controller_enabled_);
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200184 changed |= (voice_detector_enabled != voice_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700185 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
186 if (changed) {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200187 high_pass_filter_enabled_ = high_pass_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700188 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700189 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700190 noise_suppressor_enabled_ = noise_suppressor_enabled;
peah2ace3f92016-09-10 04:42:27 -0700191 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700192 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200193 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200194 echo_controller_enabled_ = echo_controller_enabled;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200195 voice_detector_enabled_ = voice_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700196 transient_suppressor_enabled_ = transient_suppressor_enabled;
197 }
198
199 changed |= first_update_;
200 first_update_ = false;
201 return changed;
202}
203
saza1d600522019-10-18 13:29:43 +0200204bool AudioProcessingImpl::SubmoduleStates::CaptureMultiBandSubModulesActive()
peah2ace3f92016-09-10 04:42:27 -0700205 const {
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200206 return CaptureMultiBandProcessingPresent() || voice_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700207}
208
saza1d600522019-10-18 13:29:43 +0200209bool AudioProcessingImpl::SubmoduleStates::CaptureMultiBandProcessingPresent()
210 const {
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200211 // If echo controller is present, assume it performs active processing.
212 return CaptureMultiBandProcessingActive(/*ec_processing_active=*/true);
213}
214
saza1d600522019-10-18 13:29:43 +0200215bool AudioProcessingImpl::SubmoduleStates::CaptureMultiBandProcessingActive(
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200216 bool ec_processing_active) const {
Per Åhgren62ea0aa2019-12-09 10:18:44 +0100217 return high_pass_filter_enabled_ || mobile_echo_controller_enabled_ ||
218 noise_suppressor_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200219 (echo_controller_enabled_ && ec_processing_active);
peah2ace3f92016-09-10 04:42:27 -0700220}
221
saza1d600522019-10-18 13:29:43 +0200222bool AudioProcessingImpl::SubmoduleStates::CaptureFullBandProcessingActive()
peah23ac8b42017-05-23 05:33:56 -0700223 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200224 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
225 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700226}
227
saza1d600522019-10-18 13:29:43 +0200228bool AudioProcessingImpl::SubmoduleStates::CaptureAnalyzerActive() const {
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200229 return capture_analyzer_enabled_;
230}
231
saza1d600522019-10-18 13:29:43 +0200232bool AudioProcessingImpl::SubmoduleStates::RenderMultiBandSubModulesActive()
peah2ace3f92016-09-10 04:42:27 -0700233 const {
Per Åhgren62ea0aa2019-12-09 10:18:44 +0100234 return RenderMultiBandProcessingActive() || mobile_echo_controller_enabled_ ||
235 adaptive_gain_controller_enabled_ || echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700236}
237
saza1d600522019-10-18 13:29:43 +0200238bool AudioProcessingImpl::SubmoduleStates::RenderFullBandProcessingActive()
Alex Loiko5825aa62017-12-18 16:02:40 +0100239 const {
240 return render_pre_processor_enabled_;
241}
242
saza1d600522019-10-18 13:29:43 +0200243bool AudioProcessingImpl::SubmoduleStates::RenderMultiBandProcessingActive()
peah2ace3f92016-09-10 04:42:27 -0700244 const {
peah2ace3f92016-09-10 04:42:27 -0700245 return false;
peah2ace3f92016-09-10 04:42:27 -0700246}
247
saza1d600522019-10-18 13:29:43 +0200248bool AudioProcessingImpl::SubmoduleStates::HighPassFilteringRequired() const {
Per Åhgren62ea0aa2019-12-09 10:18:44 +0100249 return high_pass_filter_enabled_ || mobile_echo_controller_enabled_ ||
250 noise_suppressor_enabled_;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200251}
252
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100253AudioProcessingBuilder::AudioProcessingBuilder() = default;
254AudioProcessingBuilder::~AudioProcessingBuilder() = default;
255
256AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
257 std::unique_ptr<CustomProcessing> capture_post_processing) {
258 capture_post_processing_ = std::move(capture_post_processing);
259 return *this;
260}
261
262AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
263 std::unique_ptr<CustomProcessing> render_pre_processing) {
264 render_pre_processing_ = std::move(render_pre_processing);
265 return *this;
266}
267
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200268AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
269 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
270 capture_analyzer_ = std::move(capture_analyzer);
271 return *this;
272}
273
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100274AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
275 std::unique_ptr<EchoControlFactory> echo_control_factory) {
276 echo_control_factory_ = std::move(echo_control_factory);
277 return *this;
278}
279
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100280AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200281 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100282 echo_detector_ = std::move(echo_detector);
283 return *this;
284}
285
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100286AudioProcessing* AudioProcessingBuilder::Create() {
287 webrtc::Config config;
288 return Create(config);
289}
290
291AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100292 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
293 config, std::move(capture_post_processing_),
294 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200295 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100296 if (apm->Initialize() != AudioProcessing::kNoError) {
297 delete apm;
298 apm = nullptr;
299 }
300 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100301}
302
peah88ac8532016-09-12 16:47:25 -0700303AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200304 : AudioProcessingImpl(config,
305 /*capture_post_processor=*/nullptr,
306 /*render_pre_processor=*/nullptr,
307 /*echo_control_factory=*/nullptr,
308 /*echo_detector=*/nullptr,
309 /*capture_analyzer=*/nullptr) {}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000310
Per Åhgren13735822018-02-12 21:42:56 +0100311int AudioProcessingImpl::instance_count_ = 0;
312
Sam Zackrisson0beac582017-09-25 12:04:02 +0200313AudioProcessingImpl::AudioProcessingImpl(
314 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100315 std::unique_ptr<CustomProcessing> capture_post_processor,
316 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200317 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200318 rtc::scoped_refptr<EchoDetector> echo_detector,
319 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100320 : data_dumper_(
321 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Per Åhgren0cbb58e2019-10-29 22:59:44 +0100322 enforced_usage_of_legacy_ns_(DetectLegacyNsEnforcement()),
Per Åhgrenb2b58d82019-12-02 14:59:40 +0100323 use_setup_specific_default_aec3_config_(
324 UseSetupSpecificDefaultAec3Congfig()),
Alex Loiko73ec0192018-05-15 10:52:28 +0200325 capture_runtime_settings_(kRuntimeSettingQueueSize),
326 render_runtime_settings_(kRuntimeSettingQueueSize),
327 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
328 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200329 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200330 submodule_states_(!!capture_post_processor,
331 !!render_pre_processor,
332 !!capture_analyzer),
saza1d600522019-10-18 13:29:43 +0200333 submodules_(std::move(capture_post_processor),
334 std::move(render_pre_processor),
335 std::move(echo_detector),
Per Åhgren3daedb62019-11-22 12:11:40 +0100336 std::move(capture_analyzer)),
337 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
338 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000339#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Per Åhgren3daedb62019-11-22 12:11:40 +0100340 /* enabled= */ false,
341 /* enabled_agc2_level_estimator= */ false,
342 /* digital_adaptive_disabled= */ false,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000343#else
Per Åhgren3daedb62019-11-22 12:11:40 +0100344 config.Get<ExperimentalAgc>().enabled,
345 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
346 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000347#endif
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200348 !field_trial::IsEnabled(
349 "WebRTC-ApmExperimentalMultiChannelRenderKillSwitch"),
350 !field_trial::IsEnabled(
Per Åhgrenc0424252019-12-10 13:04:15 +0100351 "WebRTC-ApmExperimentalMultiChannelCaptureKillSwitch"),
352 EnforceSplitBandHpf()),
Alessio Bazzicacc22f512018-08-30 13:01:34 +0200353 capture_nonlocked_() {
Sam Zackrisson72cc71c2019-10-21 12:54:02 +0200354 RTC_LOG(LS_INFO) << "Injected APM submodules:"
355 << "\nEcho control factory: " << !!echo_control_factory_
356 << "\nEcho detector: " << !!submodules_.echo_detector
357 << "\nCapture analyzer: " << !!submodules_.capture_analyzer
358 << "\nCapture post processor: "
359 << !!submodules_.capture_post_processor
360 << "\nRender pre processor: "
361 << !!submodules_.render_pre_processor;
362
Sam Zackrisson421c8592019-02-11 13:39:46 +0100363 // Mark Echo Controller enabled if a factory is injected.
364 capture_nonlocked_.echo_controller_enabled =
365 static_cast<bool>(echo_control_factory_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000366
saza1d600522019-10-18 13:29:43 +0200367 submodules_.gain_control.reset(new GainControlImpl());
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200368
Sam Zackrisson421c8592019-02-11 13:39:46 +0100369 // If no echo detector is injected, use the ResidualEchoDetector.
saza1d600522019-10-18 13:29:43 +0200370 if (!submodules_.echo_detector) {
371 submodules_.echo_detector =
Sam Zackrisson421c8592019-02-11 13:39:46 +0100372 new rtc::RefCountedObject<ResidualEchoDetector>();
peahdf3efa82015-11-28 12:35:15 -0800373 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000374
Per Åhgrenc0734712020-01-02 15:15:36 +0100375 // TODO(webrtc:5298): Remove once the use of ExperimentalNs has been
376 // deprecated.
377#if !(defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS))
378 config_.transient_suppression.enabled = config.Get<ExperimentalNs>().enabled;
379#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000380}
381
Per Åhgren0e3198e2019-11-18 08:52:22 +0100382AudioProcessingImpl::~AudioProcessingImpl() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +0000383
niklase@google.com470e71d2011-07-07 08:21:25 +0000384int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800385 // Run in a single-threaded manner during initialization.
386 rtc::CritScope cs_render(&crit_render_);
387 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000388 return InitializeLocked();
389}
390
peahde65ddc2016-09-16 15:02:15 -0700391int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
392 int capture_output_sample_rate_hz,
393 int render_input_sample_rate_hz,
394 ChannelLayout capture_input_layout,
395 ChannelLayout capture_output_layout,
396 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700397 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700398 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
399 LayoutHasKeyboard(capture_input_layout)},
400 {capture_output_sample_rate_hz,
401 ChannelsFromLayout(capture_output_layout),
402 LayoutHasKeyboard(capture_output_layout)},
403 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
404 LayoutHasKeyboard(render_input_layout)},
405 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
406 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700407
408 return Initialize(processing_config);
409}
410
411int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800412 // Run in a single-threaded manner during initialization.
413 rtc::CritScope cs_render(&crit_render_);
414 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700415 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000416}
417
peahdf3efa82015-11-28 12:35:15 -0800418int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800419 const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800420 // Called from both threads. Thread check is therefore not possible.
Oskar Sundbom4b276482019-05-23 14:28:00 +0200421 if (processing_config == formats_.api_format) {
peah192164e2015-11-17 02:16:45 -0800422 return kNoError;
423 }
peahdf3efa82015-11-28 12:35:15 -0800424
425 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800426 return InitializeLocked(processing_config);
427}
428
niklase@google.com470e71d2011-07-07 08:21:25 +0000429int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200430 UpdateActiveSubmoduleStates();
431
Per Åhgrend47941e2019-08-22 11:51:13 +0200432 const int render_audiobuffer_sample_rate_hz =
peahdf3efa82015-11-28 12:35:15 -0800433 formats_.api_format.reverse_output_stream().num_frames() == 0
Per Åhgrend47941e2019-08-22 11:51:13 +0200434 ? formats_.render_processing_format.sample_rate_hz()
435 : formats_.api_format.reverse_output_stream().sample_rate_hz();
peahdf3efa82015-11-28 12:35:15 -0800436 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
437 render_.render_audio.reset(new AudioBuffer(
Per Åhgrend47941e2019-08-22 11:51:13 +0200438 formats_.api_format.reverse_input_stream().sample_rate_hz(),
peahdf3efa82015-11-28 12:35:15 -0800439 formats_.api_format.reverse_input_stream().num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200440 formats_.render_processing_format.sample_rate_hz(),
peahde65ddc2016-09-16 15:02:15 -0700441 formats_.render_processing_format.num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200442 render_audiobuffer_sample_rate_hz,
443 formats_.render_processing_format.num_channels()));
peah2ace3f92016-09-10 04:42:27 -0700444 if (formats_.api_format.reverse_input_stream() !=
445 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800446 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800447 formats_.api_format.reverse_input_stream().num_channels(),
448 formats_.api_format.reverse_input_stream().num_frames(),
449 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800450 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700451 } else {
peahdf3efa82015-11-28 12:35:15 -0800452 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700453 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700454 } else {
peahdf3efa82015-11-28 12:35:15 -0800455 render_.render_audio.reset(nullptr);
456 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700457 }
peahce4d9152017-05-19 01:28:05 -0700458
Per Åhgrend47941e2019-08-22 11:51:13 +0200459 capture_.capture_audio.reset(new AudioBuffer(
460 formats_.api_format.input_stream().sample_rate_hz(),
461 formats_.api_format.input_stream().num_channels(),
462 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
463 formats_.api_format.output_stream().num_channels(),
464 formats_.api_format.output_stream().sample_rate_hz(),
465 formats_.api_format.output_stream().num_channels()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000466
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200467 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() <
468 formats_.api_format.output_stream().sample_rate_hz() &&
469 formats_.api_format.output_stream().sample_rate_hz() == 48000) {
470 capture_.capture_fullband_audio.reset(
471 new AudioBuffer(formats_.api_format.input_stream().sample_rate_hz(),
472 formats_.api_format.input_stream().num_channels(),
473 formats_.api_format.output_stream().sample_rate_hz(),
474 formats_.api_format.output_stream().num_channels(),
475 formats_.api_format.output_stream().sample_rate_hz(),
476 formats_.api_format.output_stream().num_channels()));
477 } else {
478 capture_.capture_fullband_audio.reset();
479 }
480
peah764e3642016-10-22 05:04:30 -0700481 AllocateRenderQueue();
482
saza1d600522019-10-18 13:29:43 +0200483 submodules_.gain_control->Initialize(num_proc_channels(),
484 proc_sample_rate_hz());
Per Åhgren3daedb62019-11-22 12:11:40 +0100485 if (constants_.use_experimental_agc) {
486 if (!submodules_.agc_manager.get() ||
487 submodules_.agc_manager->num_channels() !=
488 static_cast<int>(num_proc_channels()) ||
489 submodules_.agc_manager->sample_rate_hz() !=
490 capture_nonlocked_.split_rate) {
Per Åhgren2a6b3b12019-11-26 19:34:26 +0100491 int stream_analog_level = -1;
492 const bool re_creation = !!submodules_.agc_manager;
493 if (re_creation) {
494 stream_analog_level = submodules_.agc_manager->stream_analog_level();
495 }
Per Åhgren3daedb62019-11-22 12:11:40 +0100496 submodules_.agc_manager.reset(new AgcManagerDirect(
497 num_proc_channels(), constants_.agc_startup_min_volume,
498 constants_.agc_clipped_level_min,
499 constants_.use_experimental_agc_agc2_level_estimation,
500 constants_.use_experimental_agc_agc2_digital_adaptive,
501 capture_nonlocked_.split_rate));
Per Åhgren2a6b3b12019-11-26 19:34:26 +0100502 if (re_creation) {
503 submodules_.agc_manager->set_stream_analog_level(stream_analog_level);
504 }
Per Åhgren3daedb62019-11-22 12:11:40 +0100505 }
saza1d600522019-10-18 13:29:43 +0200506 submodules_.agc_manager->Initialize();
Per Åhgren3daedb62019-11-22 12:11:40 +0100507 submodules_.agc_manager->SetupDigitalGainControl(
Per Åhgren0e3198e2019-11-18 08:52:22 +0100508 submodules_.gain_control.get());
saza1d600522019-10-18 13:29:43 +0200509 submodules_.agc_manager->SetCaptureMuted(capture_.output_will_be_muted);
peahde65ddc2016-09-16 15:02:15 -0700510 }
Per Åhgrenc0734712020-01-02 15:15:36 +0100511 InitializeTransientSuppressor();
Per Åhgren4011de02019-12-03 11:48:48 +0000512 InitializeHighPassFilter();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200513 InitializeVoiceDetector();
ivoc9f4a4a02016-10-28 05:39:16 -0700514 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200515 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700516 InitializeGainController2();
saza0bad15f2019-10-16 11:46:11 +0200517 InitializeNoiseSuppressor();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200518 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200519 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100520 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800521
aleloi868f32f2017-05-23 07:20:05 -0700522 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200523 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700524 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000525 return kNoError;
526}
527
Michael Graczyk86c6d332015-07-23 11:41:39 -0700528int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200529 UpdateActiveSubmoduleStates();
530
Michael Graczyk86c6d332015-07-23 11:41:39 -0700531 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700532 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
533 return kBadSampleRateError;
534 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000535 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700536
Peter Kasting69558702016-01-12 16:26:35 -0800537 const size_t num_in_channels = config.input_stream().num_channels();
538 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700539
540 // Need at least one input channel.
541 // Need either one output channel or as many outputs as there are inputs.
542 if (num_in_channels == 0 ||
543 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700544 return kBadNumberChannelsError;
545 }
546
peahdf3efa82015-11-28 12:35:15 -0800547 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000548
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200549 // Choose maximum rate to use for the split filtering.
550 RTC_DCHECK(config_.pipeline.maximum_internal_processing_rate == 48000 ||
551 config_.pipeline.maximum_internal_processing_rate == 32000);
552 int max_splitting_rate = 48000;
553 if (config_.pipeline.maximum_internal_processing_rate == 32000) {
554 max_splitting_rate = config_.pipeline.maximum_internal_processing_rate;
555 }
556
Per Åhgrenc8626b62019-08-23 15:49:51 +0200557 int capture_processing_rate = SuitableProcessRate(
peah423d2362016-04-09 16:06:52 -0700558 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700559 formats_.api_format.output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200560 max_splitting_rate,
peah2ace3f92016-09-10 04:42:27 -0700561 submodule_states_.CaptureMultiBandSubModulesActive() ||
562 submodule_states_.RenderMultiBandSubModulesActive());
Per Åhgrenc8626b62019-08-23 15:49:51 +0200563 RTC_DCHECK_NE(8000, capture_processing_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000564
peahde65ddc2016-09-16 15:02:15 -0700565 capture_nonlocked_.capture_processing_format =
566 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700567
peah2ce640f2017-04-07 03:57:48 -0700568 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200569 if (!capture_nonlocked_.echo_controller_enabled) {
Per Åhgrenc8626b62019-08-23 15:49:51 +0200570 render_processing_rate = SuitableProcessRate(
peah2ce640f2017-04-07 03:57:48 -0700571 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
572 formats_.api_format.reverse_output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200573 max_splitting_rate,
peah2ce640f2017-04-07 03:57:48 -0700574 submodule_states_.CaptureMultiBandSubModulesActive() ||
575 submodule_states_.RenderMultiBandSubModulesActive());
576 } else {
577 render_processing_rate = capture_processing_rate;
578 }
579
peahde65ddc2016-09-16 15:02:15 -0700580 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700581 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700582 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
583 kSampleRate8kHz) {
584 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000585 } else {
peahde65ddc2016-09-16 15:02:15 -0700586 render_processing_rate =
587 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000588 }
589
Per Åhgrenc8626b62019-08-23 15:49:51 +0200590 RTC_DCHECK_NE(8000, render_processing_rate);
591
peahce4d9152017-05-19 01:28:05 -0700592 if (submodule_states_.RenderMultiBandSubModulesActive()) {
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200593 // By default, downmix the render stream to mono for analysis. This has been
594 // demonstrated to work well for AEC in most practical scenarios.
Per Åhgrene14cb992019-11-27 09:34:22 +0100595 const bool multi_channel_render = config_.pipeline.multi_channel_render &&
596 constants_.multi_channel_render_support;
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200597 int render_processing_num_channels =
Per Åhgrene14cb992019-11-27 09:34:22 +0100598 multi_channel_render
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200599 ? formats_.api_format.reverse_input_stream().num_channels()
600 : 1;
601 formats_.render_processing_format =
602 StreamConfig(render_processing_rate, render_processing_num_channels);
peahce4d9152017-05-19 01:28:05 -0700603 } else {
604 formats_.render_processing_format = StreamConfig(
605 formats_.api_format.reverse_input_stream().sample_rate_hz(),
606 formats_.api_format.reverse_input_stream().num_channels());
607 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000608
peahde65ddc2016-09-16 15:02:15 -0700609 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
610 kSampleRate32kHz ||
611 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
612 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800613 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000614 } else {
peahdf3efa82015-11-28 12:35:15 -0800615 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700616 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000617 }
618
619 return InitializeLocked();
620}
621
peah88ac8532016-09-12 16:47:25 -0700622void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
Sam Zackrisson72cc71c2019-10-21 12:54:02 +0200623 RTC_LOG(LS_INFO) << "AudioProcessing::ApplyConfig: " << config.ToString();
624
peah88ac8532016-09-12 16:47:25 -0700625 // Run in a single-threaded manner when applying the settings.
626 rtc::CritScope cs_render(&crit_render_);
627 rtc::CritScope cs_capture(&crit_capture_);
628
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200629 const bool pipeline_config_changed =
Per Åhgrene14cb992019-11-27 09:34:22 +0100630 config_.pipeline.multi_channel_render !=
631 config.pipeline.multi_channel_render ||
632 config_.pipeline.multi_channel_capture !=
Per Åhgrenc0424252019-12-10 13:04:15 +0100633 config.pipeline.multi_channel_capture ||
634 config_.pipeline.maximum_internal_processing_rate !=
635 config.pipeline.maximum_internal_processing_rate;
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200636
Per Åhgren200feba2019-03-06 04:16:46 +0100637 const bool aec_config_changed =
638 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
Per Åhgren62ea0aa2019-12-09 10:18:44 +0100639 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode;
Per Åhgren200feba2019-03-06 04:16:46 +0100640
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100641 const bool agc1_config_changed =
642 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
643 config_.gain_controller1.mode != config.gain_controller1.mode ||
644 config_.gain_controller1.target_level_dbfs !=
645 config.gain_controller1.target_level_dbfs ||
646 config_.gain_controller1.compression_gain_db !=
647 config.gain_controller1.compression_gain_db ||
648 config_.gain_controller1.enable_limiter !=
649 config.gain_controller1.enable_limiter ||
650 config_.gain_controller1.analog_level_minimum !=
651 config.gain_controller1.analog_level_minimum ||
652 config_.gain_controller1.analog_level_maximum !=
653 config.gain_controller1.analog_level_maximum;
654
Per Åhgren2bd85ab2020-01-03 10:36:34 +0100655 const bool agc2_config_changed =
656 config_.gain_controller2.enabled != config.gain_controller2.enabled;
657
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200658 const bool voice_detection_config_changed =
659 config_.voice_detection.enabled != config.voice_detection.enabled;
660
saza0bad15f2019-10-16 11:46:11 +0200661 const bool ns_config_changed =
662 config_.noise_suppression.enabled != config.noise_suppression.enabled ||
663 config_.noise_suppression.level != config.noise_suppression.level;
664
Per Åhgrenc0734712020-01-02 15:15:36 +0100665 const bool ts_config_changed = config_.transient_suppression.enabled !=
666 config.transient_suppression.enabled;
667
Yves Gerey499bc6c2018-10-10 18:29:07 +0200668 config_ = config;
669
Per Åhgren200feba2019-03-06 04:16:46 +0100670 if (aec_config_changed) {
671 InitializeEchoController();
672 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200673
saza0bad15f2019-10-16 11:46:11 +0200674 if (ns_config_changed) {
675 InitializeNoiseSuppressor();
676 }
Sam Zackrisson23513132019-01-11 15:10:32 +0100677
Per Åhgrenc0734712020-01-02 15:15:36 +0100678 if (ts_config_changed) {
679 InitializeTransientSuppressor();
680 }
681
Per Åhgren4011de02019-12-03 11:48:48 +0000682 InitializeHighPassFilter();
peah8271d042016-11-22 07:24:52 -0800683
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100684 if (agc1_config_changed) {
685 ApplyAgc1Config(config_.gain_controller1);
686 }
687
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100688 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700689 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100690 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
691 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100692 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100693 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700694 config_.gain_controller2 = AudioProcessing::Config::GainController2();
695 }
Per Åhgren2bd85ab2020-01-03 10:36:34 +0100696 if (agc2_config_changed) {
697 InitializeGainController2();
698 }
Alex Loikob5c9a792018-04-16 16:31:22 +0200699 InitializePreAmplifier();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100700
saza1d600522019-10-18 13:29:43 +0200701 if (config_.level_estimation.enabled && !submodules_.output_level_estimator) {
702 submodules_.output_level_estimator = std::make_unique<LevelEstimator>();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100703 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100704
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200705 if (voice_detection_config_changed) {
706 InitializeVoiceDetector();
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100707 }
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200708
709 // Reinitialization must happen after all submodule configuration to avoid
710 // additional reinitializations on the next capture / render processing call.
711 if (pipeline_config_changed) {
712 InitializeLocked(formats_.api_format);
713 }
peah88ac8532016-09-12 16:47:25 -0700714}
715
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100716void AudioProcessingImpl::ApplyAgc1Config(
717 const Config::GainController1& config) {
Per Åhgren0e3198e2019-11-18 08:52:22 +0100718 int error = submodules_.gain_control->Enable(config.enabled);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100719 RTC_DCHECK_EQ(kNoError, error);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100720
Per Åhgren0e3198e2019-11-18 08:52:22 +0100721 if (!submodules_.agc_manager) {
722 error = submodules_.gain_control->set_mode(
723 Agc1ConfigModeToInterfaceMode(config.mode));
724 RTC_DCHECK_EQ(kNoError, error);
725 error = submodules_.gain_control->set_target_level_dbfs(
726 config.target_level_dbfs);
727 RTC_DCHECK_EQ(kNoError, error);
728 error = submodules_.gain_control->set_compression_gain_db(
729 config.compression_gain_db);
730 RTC_DCHECK_EQ(kNoError, error);
731 error = submodules_.gain_control->enable_limiter(config.enable_limiter);
732 RTC_DCHECK_EQ(kNoError, error);
733 error = submodules_.gain_control->set_analog_level_limits(
734 config.analog_level_minimum, config.analog_level_maximum);
735 RTC_DCHECK_EQ(kNoError, error);
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100736 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100737}
738
Per Åhgrenc0734712020-01-02 15:15:36 +0100739// TODO(webrtc:5298): Remove.
peah88ac8532016-09-12 16:47:25 -0700740void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000741}
742
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000743int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800744 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700745 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000746}
747
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200748int AudioProcessingImpl::proc_fullband_sample_rate_hz() const {
749 return capture_.capture_fullband_audio
750 ? capture_.capture_fullband_audio->num_frames() * 100
751 : capture_nonlocked_.capture_processing_format.sample_rate_hz();
752}
753
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000754int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800755 // Used as callback from submodules, hence locking is not allowed.
756 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000757}
758
Peter Kasting69558702016-01-12 16:26:35 -0800759size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800760 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700761 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000762}
763
Peter Kasting69558702016-01-12 16:26:35 -0800764size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800765 // Used as callback from submodules, hence locking is not allowed.
766 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000767}
768
Peter Kasting69558702016-01-12 16:26:35 -0800769size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800770 // Used as callback from submodules, hence locking is not allowed.
Per Åhgrene14cb992019-11-27 09:34:22 +0100771 const bool multi_channel_capture = config_.pipeline.multi_channel_capture &&
772 constants_.multi_channel_capture_support;
773 if (capture_nonlocked_.echo_controller_enabled && !multi_channel_capture) {
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200774 return 1;
775 }
776 return num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800777}
778
Peter Kasting69558702016-01-12 16:26:35 -0800779size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800780 // Used as callback from submodules, hence locking is not allowed.
781 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000782}
783
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000784void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800785 rtc::CritScope cs(&crit_capture_);
786 capture_.output_will_be_muted = muted;
saza1d600522019-10-18 13:29:43 +0200787 if (submodules_.agc_manager.get()) {
788 submodules_.agc_manager->SetCaptureMuted(capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000789 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000790}
791
Alessio Bazzicac054e782018-04-16 12:10:09 +0200792void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200793 switch (setting.type()) {
794 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
Alessio Bazzica7c19a702019-11-07 13:22:00 +0100795 case RuntimeSetting::Type::kPlayoutAudioDeviceChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200796 render_runtime_settings_enqueuer_.Enqueue(setting);
797 return;
Alex Loiko73ec0192018-05-15 10:52:28 +0200798 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100799 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200800 case RuntimeSetting::Type::kCaptureFixedPostGain:
Alessio Bazzica7c19a702019-11-07 13:22:00 +0100801 capture_runtime_settings_enqueuer_.Enqueue(setting);
802 return;
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200803 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200804 capture_runtime_settings_enqueuer_.Enqueue(setting);
Alessio Bazzica7c19a702019-11-07 13:22:00 +0100805 render_runtime_settings_enqueuer_.Enqueue(setting);
806 return;
807 case RuntimeSetting::Type::kNotSpecified:
808 RTC_NOTREACHED();
Alex Loiko73ec0192018-05-15 10:52:28 +0200809 return;
810 }
811 // The language allows the enum to have a non-enumerator
812 // value. Check that this doesn't happen.
813 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200814}
815
816AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
817 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200818 : runtime_settings_(*runtime_settings) {
819 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200820}
821
822AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
823 default;
824
825void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
826 RuntimeSetting setting) {
827 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200828 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200829 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200830 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200831 RTC_LOG(LS_ERROR)
832 << "The runtime settings queue is full. Oldest setting discarded.";
833 }
834 if (remaining_attempts == 0)
835 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
836}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000837
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000838int AudioProcessingImpl::ProcessStream(const float* const* src,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700839 const StreamConfig& input_config,
840 const StreamConfig& output_config,
841 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800842 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800843 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700844 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800845 {
846 // Acquire the capture lock in order to safely call the function
847 // that retrieves the render side data. This function accesses apm
848 // getters that need the capture lock held when being called.
849 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700850 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800851
852 if (!src || !dest) {
853 return kNullPointerError;
854 }
855
856 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700857 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000858 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000859
Oskar Sundbom4b276482019-05-23 14:28:00 +0200860 if (processing_config.input_stream() != input_config) {
861 processing_config.input_stream() = input_config;
862 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800863 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200864
865 if (processing_config.output_stream() != output_config) {
866 processing_config.output_stream() = output_config;
867 reinitialization_required = true;
868 }
869
870 if (reinitialization_required) {
871 // Reinitialize.
872 rtc::CritScope cs_render(&crit_render_);
873 rtc::CritScope cs_capture(&crit_capture_);
874 RETURN_ON_ERR(InitializeLocked(processing_config));
875 }
876
peahdf3efa82015-11-28 12:35:15 -0800877 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700878 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
879 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000880
aleloi868f32f2017-05-23 07:20:05 -0700881 if (aec_dump_) {
882 RecordUnprocessedCaptureStream(src);
883 }
884
Per Åhgrena1351272019-08-15 12:15:46 +0200885 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800886 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200887 if (capture_.capture_fullband_audio) {
888 capture_.capture_fullband_audio->CopyFrom(
889 src, formats_.api_format.input_stream());
890 }
peahde65ddc2016-09-16 15:02:15 -0700891 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200892 if (capture_.capture_fullband_audio) {
893 capture_.capture_fullband_audio->CopyTo(formats_.api_format.output_stream(),
894 dest);
895 } else {
896 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
897 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000898
aleloi868f32f2017-05-23 07:20:05 -0700899 if (aec_dump_) {
900 RecordProcessedCaptureStream(dest);
901 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000902 return kNoError;
903}
904
Alex Loiko73ec0192018-05-15 10:52:28 +0200905void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200906 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200907 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200908 if (aec_dump_) {
909 aec_dump_->WriteRuntimeSetting(setting);
910 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200911 switch (setting.type()) {
912 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200913 if (config_.pre_amplifier.enabled) {
914 float value;
915 setting.GetFloat(&value);
Sam Zackrisson21bfa402019-10-23 09:43:01 +0200916 config_.pre_amplifier.fixed_gain_factor = value;
saza1d600522019-10-18 13:29:43 +0200917 submodules_.pre_amplifier->SetGainFactor(value);
Alex Loikob5c9a792018-04-16 16:31:22 +0200918 }
919 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200920 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100921 case RuntimeSetting::Type::kCaptureCompressionGain: {
Per Åhgren0e3198e2019-11-18 08:52:22 +0100922 if (!submodules_.agc_manager) {
923 float value;
924 setting.GetFloat(&value);
925 int int_value = static_cast<int>(value + .5f);
926 config_.gain_controller1.compression_gain_db = int_value;
927 int error =
928 submodules_.gain_control->set_compression_gain_db(int_value);
929 RTC_DCHECK_EQ(kNoError, error);
930 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100931 break;
932 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200933 case RuntimeSetting::Type::kCaptureFixedPostGain: {
Per Åhgren2bd85ab2020-01-03 10:36:34 +0100934 if (submodules_.gain_controller2) {
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200935 float value;
936 setting.GetFloat(&value);
937 config_.gain_controller2.fixed_digital.gain_db = value;
saza1d600522019-10-18 13:29:43 +0200938 submodules_.gain_controller2->ApplyConfig(config_.gain_controller2);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200939 }
940 break;
941 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200942 case RuntimeSetting::Type::kPlayoutVolumeChange: {
943 int value;
944 setting.GetInt(&value);
945 capture_.playout_volume = value;
946 break;
947 }
Alessio Bazzica7c19a702019-11-07 13:22:00 +0100948 case RuntimeSetting::Type::kPlayoutAudioDeviceChange:
949 RTC_NOTREACHED();
950 break;
Alex Loiko73ec0192018-05-15 10:52:28 +0200951 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
952 RTC_NOTREACHED();
953 break;
954 case RuntimeSetting::Type::kNotSpecified:
955 RTC_NOTREACHED();
956 break;
957 }
958 }
959}
960
961void AudioProcessingImpl::HandleRenderRuntimeSettings() {
962 RuntimeSetting setting;
963 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200964 if (aec_dump_) {
965 aec_dump_->WriteRuntimeSetting(setting);
966 }
Alex Loiko73ec0192018-05-15 10:52:28 +0200967 switch (setting.type()) {
Alessio Bazzica7c19a702019-11-07 13:22:00 +0100968 case RuntimeSetting::Type::kPlayoutAudioDeviceChange: // fall-through
Alessio Bazzica7587de42019-11-11 13:32:20 +0100969 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alex Loiko73ec0192018-05-15 10:52:28 +0200970 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
saza1d600522019-10-18 13:29:43 +0200971 if (submodules_.render_pre_processor) {
972 submodules_.render_pre_processor->SetRuntimeSetting(setting);
Alex Loiko73ec0192018-05-15 10:52:28 +0200973 }
974 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100975 case RuntimeSetting::Type::kCapturePreGain: // fall-through
976 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200977 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200978 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +0200979 RTC_NOTREACHED();
980 break;
981 }
982 }
983}
984
peah9e6a2902017-05-15 07:19:21 -0700985void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -0800986 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -0700987
saza1d600522019-10-18 13:29:43 +0200988 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +0200989 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
990 num_reverse_channels(),
991 &aecm_render_queue_buffer_);
992 RTC_DCHECK(aecm_render_signal_queue_);
993 // Insert the samples into the queue.
994 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
995 // The data queue is full and needs to be emptied.
996 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -0700997
Per Åhgrenb6e24d72019-04-29 12:14:50 +0200998 // Retry the insert (should always work).
999 bool result =
1000 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1001 RTC_DCHECK(result);
1002 }
peah764e3642016-10-22 05:04:30 -07001003 }
peah701d6282016-10-25 05:42:20 -07001004
Per Åhgren0e3198e2019-11-18 08:52:22 +01001005 if (!submodules_.agc_manager) {
Per Åhgrene35b32c2019-11-22 18:22:04 +01001006 GainControlImpl::PackRenderAudioBuffer(*audio, &agc_render_queue_buffer_);
peah701d6282016-10-25 05:42:20 -07001007 // Insert the samples into the queue.
1008 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1009 // The data queue is full and needs to be emptied.
1010 EmptyQueuedRenderAudio();
1011
1012 // Retry the insert (should always work).
1013 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1014 RTC_DCHECK(result);
1015 }
1016 }
peah9e6a2902017-05-15 07:19:21 -07001017}
ivoc9f4a4a02016-10-28 05:39:16 -07001018
peah9e6a2902017-05-15 07:19:21 -07001019void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001020 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1021
1022 // Insert the samples into the queue.
1023 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1024 // The data queue is full and needs to be emptied.
1025 EmptyQueuedRenderAudio();
1026
1027 // Retry the insert (should always work).
1028 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1029 RTC_DCHECK(result);
1030 }
peah764e3642016-10-22 05:04:30 -07001031}
1032
1033void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001034 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001035 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001036
ivoc9f4a4a02016-10-28 05:39:16 -07001037 const size_t new_red_render_queue_element_max_size =
1038 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1039
peaha0624602016-10-25 04:45:24 -07001040 // Reallocate the queues if the queue item sizes are too small to fit the
1041 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001042
1043 if (agc_render_queue_element_max_size_ <
1044 new_agc_render_queue_element_max_size) {
1045 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1046
1047 std::vector<int16_t> template_queue_element(
1048 agc_render_queue_element_max_size_);
1049
1050 agc_render_signal_queue_.reset(
1051 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1052 kMaxNumFramesToBuffer, template_queue_element,
1053 RenderQueueItemVerifier<int16_t>(
1054 agc_render_queue_element_max_size_)));
1055
1056 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1057 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1058 } else {
1059 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001060 }
ivoc9f4a4a02016-10-28 05:39:16 -07001061
1062 if (red_render_queue_element_max_size_ <
1063 new_red_render_queue_element_max_size) {
1064 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1065
1066 std::vector<float> template_queue_element(
1067 red_render_queue_element_max_size_);
1068
1069 red_render_signal_queue_.reset(
1070 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1071 kMaxNumFramesToBuffer, template_queue_element,
1072 RenderQueueItemVerifier<float>(
1073 red_render_queue_element_max_size_)));
1074
1075 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1076 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1077 } else {
1078 red_render_signal_queue_->Clear();
1079 }
peah764e3642016-10-22 05:04:30 -07001080}
1081
1082void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1083 rtc::CritScope cs_capture(&crit_capture_);
saza1d600522019-10-18 13:29:43 +02001084 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001085 RTC_DCHECK(aecm_render_signal_queue_);
1086 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001087 submodules_.echo_control_mobile->ProcessRenderAudio(
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001088 aecm_capture_queue_buffer_);
1089 }
peah701d6282016-10-25 05:42:20 -07001090 }
1091
1092 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001093 submodules_.gain_control->ProcessRenderAudio(agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001094 }
ivoc9f4a4a02016-10-28 05:39:16 -07001095
1096 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
saza1d600522019-10-18 13:29:43 +02001097 RTC_DCHECK(submodules_.echo_detector);
1098 submodules_.echo_detector->AnalyzeRenderAudio(red_capture_queue_buffer_);
ivoc9f4a4a02016-10-28 05:39:16 -07001099 }
peah764e3642016-10-22 05:04:30 -07001100}
1101
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001102int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001103 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001104 {
1105 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001106 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001107 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001108 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001109 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001110 }
peahfa6228e2015-11-16 16:27:42 -08001111
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001112 if (!frame) {
1113 return kNullPointerError;
1114 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001115 // Must be a native rate.
1116 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1117 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001118 frame->sample_rate_hz_ != kSampleRate32kHz &&
1119 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001120 return kBadSampleRateError;
1121 }
peah192164e2015-11-17 02:16:45 -08001122
peahdf3efa82015-11-28 12:35:15 -08001123 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001124 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001125 {
1126 // Aquire lock for the access of api_format.
1127 // The lock is released immediately due to the conditional
1128 // reinitialization.
1129 rtc::CritScope cs_capture(&crit_capture_);
1130 // TODO(ajm): The input and output rates and channels are currently
1131 // constrained to be identical in the int16 interface.
1132 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001133
1134 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001135 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001136
Oskar Sundbom4b276482019-05-23 14:28:00 +02001137 reinitialization_required =
1138 reinitialization_required ||
1139 processing_config.input_stream().sample_rate_hz() !=
1140 frame->sample_rate_hz_ ||
1141 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1142 processing_config.output_stream().sample_rate_hz() !=
1143 frame->sample_rate_hz_ ||
1144 processing_config.output_stream().num_channels() != frame->num_channels_;
1145
1146 if (reinitialization_required) {
1147 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1148 processing_config.input_stream().set_num_channels(frame->num_channels_);
1149 processing_config.output_stream().set_sample_rate_hz(
1150 frame->sample_rate_hz_);
1151 processing_config.output_stream().set_num_channels(frame->num_channels_);
1152
1153 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001154 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001155 rtc::CritScope cs_capture(&crit_capture_);
1156 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001157 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001158
peahdf3efa82015-11-28 12:35:15 -08001159 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001160 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001161 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001162 return kBadDataLengthError;
1163 }
1164
aleloi868f32f2017-05-23 07:20:05 -07001165 if (aec_dump_) {
1166 RecordUnprocessedCaptureStream(*frame);
1167 }
1168
Per Åhgrend47941e2019-08-22 11:51:13 +02001169 capture_.capture_audio->CopyFrom(frame);
Gustaf Ullberg3c918b12019-10-11 13:14:44 +02001170 if (capture_.capture_fullband_audio) {
1171 capture_.capture_fullband_audio->CopyFrom(frame);
1172 }
peahde65ddc2016-09-16 15:02:15 -07001173 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001174 if (submodule_states_.CaptureMultiBandProcessingPresent() ||
Per Åhgrena1351272019-08-15 12:15:46 +02001175 submodule_states_.CaptureFullBandProcessingActive()) {
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001176 if (capture_.capture_fullband_audio) {
1177 capture_.capture_fullband_audio->CopyTo(frame);
1178 } else {
1179 capture_.capture_audio->CopyTo(frame);
1180 }
Per Åhgrena1351272019-08-15 12:15:46 +02001181 }
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001182 if (capture_.stats.voice_detected) {
1183 frame->vad_activity_ = *capture_.stats.voice_detected
1184 ? AudioFrame::kVadActive
1185 : AudioFrame::kVadPassive;
1186 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001187
aleloi868f32f2017-05-23 07:20:05 -07001188 if (aec_dump_) {
1189 RecordProcessedCaptureStream(*frame);
1190 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001191
1192 return kNoError;
1193}
1194
peahde65ddc2016-09-16 15:02:15 -07001195int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001196 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001197
peahb58a1582016-03-15 09:34:24 -07001198 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001199 // TODO(peah): Simplify once the public API Enable functions for these
1200 // are moved to APM.
saza1d600522019-10-18 13:29:43 +02001201 RTC_DCHECK_LE(!!submodules_.echo_controller +
saza1d600522019-10-18 13:29:43 +02001202 !!submodules_.echo_control_mobile,
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001203 1);
peahb58a1582016-03-15 09:34:24 -07001204
peahde65ddc2016-09-16 15:02:15 -07001205 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
Per Åhgren2e8e1c62019-12-20 00:42:22 +01001206 AudioBuffer* linear_aec_buffer = capture_.linear_aec_output.get();
ekmeyerson60d9b332015-08-14 10:35:55 -07001207
Per Åhgrenc0424252019-12-10 13:04:15 +01001208 if (submodules_.high_pass_filter &&
1209 config_.high_pass_filter.apply_in_full_band &&
1210 !constants_.enforce_split_band_hpf) {
1211 submodules_.high_pass_filter->Process(capture_buffer,
1212 /*use_split_band_data=*/false);
1213 }
1214
saza1d600522019-10-18 13:29:43 +02001215 if (submodules_.pre_amplifier) {
1216 submodules_.pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001217 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001218 capture_buffer->num_frames()));
1219 }
1220
Per Åhgren928146f2019-08-20 09:19:21 +02001221 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001222 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001223 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001224 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1225 if (log_rms) {
1226 capture_rms_interval_counter_ = 0;
1227 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001228 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1229 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1230 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1231 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001232 }
1233
saza1d600522019-10-18 13:29:43 +02001234 if (submodules_.echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001235 // Detect and flag any change in the analog gain.
Per Åhgren0e3198e2019-11-18 08:52:22 +01001236 int analog_mic_level = recommended_stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001237 capture_.echo_path_gain_change =
1238 capture_.prev_analog_mic_level != analog_mic_level &&
1239 capture_.prev_analog_mic_level != -1;
1240 capture_.prev_analog_mic_level = analog_mic_level;
1241
Per Åhgrend2650d12018-10-02 17:00:59 +02001242 // Detect and flag any change in the pre-amplifier gain.
saza1d600522019-10-18 13:29:43 +02001243 if (submodules_.pre_amplifier) {
1244 float pre_amp_gain = submodules_.pre_amplifier->GetGainFactor();
Per Åhgrend2650d12018-10-02 17:00:59 +02001245 capture_.echo_path_gain_change =
1246 capture_.echo_path_gain_change ||
1247 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001248 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001249 capture_.prev_pre_amp_gain = pre_amp_gain;
1250 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001251
1252 // Detect volume change.
1253 capture_.echo_path_gain_change =
1254 capture_.echo_path_gain_change ||
1255 (capture_.prev_playout_volume != capture_.playout_volume &&
1256 capture_.prev_playout_volume >= 0);
1257 capture_.prev_playout_volume = capture_.playout_volume;
1258
saza1d600522019-10-18 13:29:43 +02001259 submodules_.echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001260 }
1261
Per Åhgren3daedb62019-11-22 12:11:40 +01001262 if (constants_.use_experimental_agc &&
1263 submodules_.gain_control->is_enabled()) {
1264 submodules_.agc_manager->AnalyzePreProcess(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001265 }
1266
peah2ace3f92016-09-10 04:42:27 -07001267 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1268 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001269 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1270 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001271 }
1272
Per Åhgrene14cb992019-11-27 09:34:22 +01001273 const bool multi_channel_capture = config_.pipeline.multi_channel_capture &&
1274 constants_.multi_channel_capture_support;
1275 if (submodules_.echo_controller && !multi_channel_capture) {
peah522d71b2017-02-23 05:16:26 -08001276 // Force down-mixing of the number of channels after the detection of
1277 // capture signal saturation.
1278 // TODO(peah): Look into ensuring that this kind of tampering with the
1279 // AudioBuffer functionality should not be needed.
1280 capture_buffer->set_num_channels(1);
1281 }
1282
Per Åhgrenc0424252019-12-10 13:04:15 +01001283 if (submodules_.high_pass_filter &&
1284 (!config_.high_pass_filter.apply_in_full_band ||
1285 constants_.enforce_split_band_hpf)) {
1286 submodules_.high_pass_filter->Process(capture_buffer,
1287 /*use_split_band_data=*/true);
peah8271d042016-11-22 07:24:52 -08001288 }
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001289
Per Åhgrene35b32c2019-11-22 18:22:04 +01001290 RETURN_ON_ERR(submodules_.gain_control->AnalyzeCaptureAudio(*capture_buffer));
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001291 RTC_DCHECK(
1292 !(submodules_.legacy_noise_suppressor && submodules_.noise_suppressor));
Per Åhgren2e8e1c62019-12-20 00:42:22 +01001293
1294 if (!config_.noise_suppression.analyze_linear_aec_output_when_available ||
1295 !linear_aec_buffer || submodules_.echo_control_mobile) {
1296 if (submodules_.noise_suppressor) {
1297 submodules_.noise_suppressor->Analyze(*capture_buffer);
1298 } else if (submodules_.legacy_noise_suppressor) {
1299 submodules_.legacy_noise_suppressor->AnalyzeCaptureAudio(capture_buffer);
1300 }
saza0bad15f2019-10-16 11:46:11 +02001301 }
peahb58a1582016-03-15 09:34:24 -07001302
saza1d600522019-10-18 13:29:43 +02001303 if (submodules_.echo_control_mobile) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001304 // Ensure that the stream delay was set before the call to the
1305 // AECM ProcessCaptureAudio function.
1306 if (!was_stream_delay_set()) {
1307 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001308 }
1309
saza1d600522019-10-18 13:29:43 +02001310 if (submodules_.noise_suppressor) {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001311 submodules_.noise_suppressor->Process(capture_buffer);
1312 } else if (submodules_.legacy_noise_suppressor) {
saza1d600522019-10-18 13:29:43 +02001313 submodules_.echo_control_mobile->CopyLowPassReference(capture_buffer);
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001314 submodules_.legacy_noise_suppressor->ProcessCaptureAudio(capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001315 }
peahe0eae3c2016-12-14 01:16:23 -08001316
saza1d600522019-10-18 13:29:43 +02001317 RETURN_ON_ERR(submodules_.echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001318 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001319 } else {
saza1d600522019-10-18 13:29:43 +02001320 if (submodules_.echo_controller) {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001321 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1322
1323 if (was_stream_delay_set()) {
saza1d600522019-10-18 13:29:43 +02001324 submodules_.echo_controller->SetAudioBufferDelay(stream_delay_ms());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001325 }
1326
saza1d600522019-10-18 13:29:43 +02001327 submodules_.echo_controller->ProcessCapture(
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001328 capture_buffer, linear_aec_buffer, capture_.echo_path_gain_change);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001329 }
1330
Per Åhgren2e8e1c62019-12-20 00:42:22 +01001331 if (config_.noise_suppression.analyze_linear_aec_output_when_available &&
1332 linear_aec_buffer) {
1333 if (submodules_.noise_suppressor) {
1334 submodules_.noise_suppressor->Analyze(*linear_aec_buffer);
1335 } else if (submodules_.legacy_noise_suppressor) {
1336 submodules_.legacy_noise_suppressor->AnalyzeCaptureAudio(
1337 linear_aec_buffer);
1338 }
1339 }
1340
saza1d600522019-10-18 13:29:43 +02001341 if (submodules_.noise_suppressor) {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001342 submodules_.noise_suppressor->Process(capture_buffer);
1343 } else if (submodules_.legacy_noise_suppressor) {
1344 submodules_.legacy_noise_suppressor->ProcessCaptureAudio(capture_buffer);
saza0bad15f2019-10-16 11:46:11 +02001345 }
Per Åhgren46537a32017-06-07 10:08:10 +02001346 }
ivoc9f4a4a02016-10-28 05:39:16 -07001347
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001348 if (config_.voice_detection.enabled) {
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001349 capture_.stats.voice_detected =
saza1d600522019-10-18 13:29:43 +02001350 submodules_.voice_detector->ProcessCaptureAudio(capture_buffer);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001351 } else {
1352 capture_.stats.voice_detected = absl::nullopt;
1353 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001354
Per Åhgren3daedb62019-11-22 12:11:40 +01001355 if (constants_.use_experimental_agc &&
1356 submodules_.gain_control->is_enabled()) {
1357 submodules_.agc_manager->Process(capture_buffer);
1358
1359 absl::optional<int> new_digital_gain =
1360 submodules_.agc_manager->GetDigitalComressionGain();
1361 if (new_digital_gain) {
1362 submodules_.gain_control->set_compression_gain_db(*new_digital_gain);
1363 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001364 }
Per Åhgren200feba2019-03-06 04:16:46 +01001365 // TODO(peah): Add reporting from AEC3 whether there is echo.
saza1d600522019-10-18 13:29:43 +02001366 RETURN_ON_ERR(submodules_.gain_control->ProcessCaptureAudio(
Per Åhgren62ea0aa2019-12-09 10:18:44 +01001367 capture_buffer, /*stream_has_echo*/ false));
niklase@google.com470e71d2011-07-07 08:21:25 +00001368
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001369 if (submodule_states_.CaptureMultiBandProcessingPresent() &&
peah2ace3f92016-09-10 04:42:27 -07001370 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001371 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1372 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001373 }
1374
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001375 if (capture_.capture_fullband_audio) {
saza1d600522019-10-18 13:29:43 +02001376 const auto& ec = submodules_.echo_controller;
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001377 bool ec_active = ec ? ec->ActiveProcessing() : false;
1378 // Only update the fullband buffer if the multiband processing has changed
1379 // the signal. Keep the original signal otherwise.
1380 if (submodule_states_.CaptureMultiBandProcessingActive(ec_active)) {
1381 capture_buffer->CopyTo(capture_.capture_fullband_audio.get());
1382 }
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001383 capture_buffer = capture_.capture_fullband_audio.get();
1384 }
1385
peah9e6a2902017-05-15 07:19:21 -07001386 if (config_.residual_echo_detector.enabled) {
saza1d600522019-10-18 13:29:43 +02001387 RTC_DCHECK(submodules_.echo_detector);
1388 submodules_.echo_detector->AnalyzeCaptureAudio(rtc::ArrayView<const float>(
1389 capture_buffer->channels()[0], capture_buffer->num_frames()));
peah9e6a2902017-05-15 07:19:21 -07001390 }
1391
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001392 // TODO(aluebs): Investigate if the transient suppression placement should be
1393 // before or after the AGC.
Per Åhgrenc0734712020-01-02 15:15:36 +01001394 if (submodules_.transient_suppressor) {
saza1d600522019-10-18 13:29:43 +02001395 float voice_probability = submodules_.agc_manager.get()
1396 ? submodules_.agc_manager->voice_probability()
1397 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001398
saza1d600522019-10-18 13:29:43 +02001399 submodules_.transient_suppressor->Suppress(
Per Åhgrend47941e2019-08-22 11:51:13 +02001400 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001401 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001402 capture_buffer->split_bands_const(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001403 capture_buffer->num_frames_per_band(),
1404 capture_.keyboard_info.keyboard_data,
1405 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001406 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001407 }
1408
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001409 // Experimental APM sub-module that analyzes |capture_buffer|.
saza1d600522019-10-18 13:29:43 +02001410 if (submodules_.capture_analyzer) {
1411 submodules_.capture_analyzer->Analyze(capture_buffer);
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001412 }
1413
Per Åhgren2bd85ab2020-01-03 10:36:34 +01001414 if (submodules_.gain_controller2) {
saza1d600522019-10-18 13:29:43 +02001415 submodules_.gain_controller2->NotifyAnalogLevel(
Per Åhgren0e3198e2019-11-18 08:52:22 +01001416 recommended_stream_analog_level());
saza1d600522019-10-18 13:29:43 +02001417 submodules_.gain_controller2->Process(capture_buffer);
alessiob3ec96df2017-05-22 06:57:06 -07001418 }
1419
saza1d600522019-10-18 13:29:43 +02001420 if (submodules_.capture_post_processor) {
1421 submodules_.capture_post_processor->Process(capture_buffer);
Sam Zackrisson0beac582017-09-25 12:04:02 +02001422 }
1423
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001424 // The level estimator operates on the recombined data.
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001425 if (config_.level_estimation.enabled) {
saza1d600522019-10-18 13:29:43 +02001426 submodules_.output_level_estimator->ProcessStream(*capture_buffer);
1427 capture_.stats.output_rms_dbfs = submodules_.output_level_estimator->RMS();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001428 } else {
1429 capture_.stats.output_rms_dbfs = absl::nullopt;
1430 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001431
Per Åhgren928146f2019-08-20 09:19:21 +02001432 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001433 capture_buffer->channels_const()[0],
peah1b08dc32016-12-20 13:45:58 -08001434 capture_nonlocked_.capture_processing_format.num_frames()));
1435 if (log_rms) {
1436 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1437 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1438 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1439 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1440 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1441 }
1442
Per Åhgren0e3198e2019-11-18 08:52:22 +01001443 if (submodules_.agc_manager) {
1444 int level = recommended_stream_analog_level();
1445 data_dumper_->DumpRaw("experimental_gain_control_stream_analog_level", 1,
1446 &level);
1447 }
1448
Per Åhgrencf4c8722019-12-30 14:32:14 +01001449 // Compute echo-related stats.
1450 if (submodules_.echo_controller) {
1451 auto ec_metrics = submodules_.echo_controller->GetMetrics();
1452 capture_.stats.echo_return_loss = ec_metrics.echo_return_loss;
1453 capture_.stats.echo_return_loss_enhancement =
1454 ec_metrics.echo_return_loss_enhancement;
1455 capture_.stats.delay_ms = ec_metrics.delay_ms;
1456 }
1457 if (config_.residual_echo_detector.enabled) {
1458 RTC_DCHECK(submodules_.echo_detector);
1459 auto ed_metrics = submodules_.echo_detector->GetMetrics();
1460 capture_.stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1461 capture_.stats.residual_echo_likelihood_recent_max =
1462 ed_metrics.echo_likelihood_recent_max;
1463 }
1464
1465 // Pass stats for reporting.
1466 stats_reporter_.UpdateStatistics(capture_.stats);
1467
peahdf3efa82015-11-28 12:35:15 -08001468 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001469 return kNoError;
1470}
1471
Gustaf Ullberg8c51f2e2019-10-22 15:21:31 +02001472int AudioProcessingImpl::AnalyzeReverseStream(
1473 const float* const* data,
1474 const StreamConfig& reverse_config) {
1475 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_StreamConfig");
1476 rtc::CritScope cs(&crit_render_);
1477 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
1478}
1479
peahde65ddc2016-09-16 15:02:15 -07001480int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1481 const StreamConfig& input_config,
1482 const StreamConfig& output_config,
1483 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001484 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001485 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001486 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001487 if (submodule_states_.RenderMultiBandProcessingActive() ||
1488 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001489 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1490 dest);
peah2ace3f92016-09-10 04:42:27 -07001491 } else if (formats_.api_format.reverse_input_stream() !=
1492 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001493 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1494 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001495 } else {
peahde65ddc2016-09-16 15:02:15 -07001496 CopyAudioIfNeeded(src, input_config.num_frames(),
1497 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001498 }
1499
1500 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001501}
1502
peahdf3efa82015-11-28 12:35:15 -08001503int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001504 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001505 const StreamConfig& input_config,
1506 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001507 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001508 return kNullPointerError;
1509 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001510
peahde65ddc2016-09-16 15:02:15 -07001511 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001512 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001513 }
1514
peahdf3efa82015-11-28 12:35:15 -08001515 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001516 processing_config.reverse_input_stream() = input_config;
1517 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001518
peahdf3efa82015-11-28 12:35:15 -08001519 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001520 RTC_DCHECK_EQ(input_config.num_frames(),
1521 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001522
aleloi868f32f2017-05-23 07:20:05 -07001523 if (aec_dump_) {
1524 const size_t channel_size =
1525 formats_.api_format.reverse_input_stream().num_frames();
1526 const size_t num_channels =
1527 formats_.api_format.reverse_input_stream().num_channels();
1528 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001529 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001530 }
peahdf3efa82015-11-28 12:35:15 -08001531 render_.render_audio->CopyFrom(src,
1532 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001533 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001534}
1535
1536int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001537 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001538 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001539 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001540 return kNullPointerError;
1541 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001542 // Must be a native rate.
1543 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1544 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001545 frame->sample_rate_hz_ != kSampleRate32kHz &&
1546 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001547 return kBadSampleRateError;
1548 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001549
Michael Graczyk86c6d332015-07-23 11:41:39 -07001550 if (frame->num_channels_ <= 0) {
1551 return kBadNumberChannelsError;
1552 }
1553
peahdf3efa82015-11-28 12:35:15 -08001554 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001555 processing_config.reverse_input_stream().set_sample_rate_hz(
1556 frame->sample_rate_hz_);
1557 processing_config.reverse_input_stream().set_num_channels(
1558 frame->num_channels_);
1559 processing_config.reverse_output_stream().set_sample_rate_hz(
1560 frame->sample_rate_hz_);
1561 processing_config.reverse_output_stream().set_num_channels(
1562 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001563
peahdf3efa82015-11-28 12:35:15 -08001564 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001565 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001566 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001567 return kBadDataLengthError;
1568 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001569
aleloi868f32f2017-05-23 07:20:05 -07001570 if (aec_dump_) {
1571 aec_dump_->WriteRenderStreamMessage(*frame);
1572 }
1573
Per Åhgrend47941e2019-08-22 11:51:13 +02001574 render_.render_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001575 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001576 if (submodule_states_.RenderMultiBandProcessingActive() ||
1577 submodule_states_.RenderFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001578 render_.render_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001579 }
aluebsb0319552016-03-17 20:39:53 -07001580 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001581}
niklase@google.com470e71d2011-07-07 08:21:25 +00001582
peahde65ddc2016-09-16 15:02:15 -07001583int AudioProcessingImpl::ProcessRenderStreamLocked() {
1584 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001585
Alex Loiko73ec0192018-05-15 10:52:28 +02001586 HandleRenderRuntimeSettings();
1587
saza1d600522019-10-18 13:29:43 +02001588 if (submodules_.render_pre_processor) {
1589 submodules_.render_pre_processor->Process(render_buffer);
Alex Loiko5825aa62017-12-18 16:02:40 +01001590 }
1591
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001592 QueueNonbandedRenderAudio(render_buffer);
1593
peah2ace3f92016-09-10 04:42:27 -07001594 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001595 SampleRateSupportsMultiBand(
1596 formats_.render_processing_format.sample_rate_hz())) {
1597 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001598 }
1599
peahce4d9152017-05-19 01:28:05 -07001600 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1601 QueueBandedRenderAudio(render_buffer);
1602 }
1603
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001604 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
saza1d600522019-10-18 13:29:43 +02001605 if (submodules_.echo_controller) {
1606 submodules_.echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001607 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001608
peah2ace3f92016-09-10 04:42:27 -07001609 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001610 SampleRateSupportsMultiBand(
1611 formats_.render_processing_format.sample_rate_hz())) {
1612 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001613 }
1614
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001615 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001616}
1617
1618int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001619 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001620 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001621 capture_.was_stream_delay_set = true;
1622 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001623
niklase@google.com470e71d2011-07-07 08:21:25 +00001624 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001625 delay = 0;
1626 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001627 }
1628
1629 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1630 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001631 delay = 500;
1632 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001633 }
1634
peahdf3efa82015-11-28 12:35:15 -08001635 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001636 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001637}
1638
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001639bool AudioProcessingImpl::GetLinearAecOutput(
1640 rtc::ArrayView<std::array<float, 160>> linear_output) const {
1641 rtc::CritScope cs(&crit_capture_);
1642 AudioBuffer* linear_aec_buffer = capture_.linear_aec_output.get();
1643
1644 RTC_DCHECK(linear_aec_buffer);
1645 if (linear_aec_buffer) {
1646 RTC_DCHECK_EQ(1, linear_aec_buffer->num_bands());
1647 RTC_DCHECK_EQ(linear_output.size(), linear_aec_buffer->num_channels());
1648
1649 for (size_t ch = 0; ch < linear_aec_buffer->num_channels(); ++ch) {
1650 RTC_DCHECK_EQ(linear_output[ch].size(), linear_aec_buffer->num_frames());
1651 rtc::ArrayView<const float> channel_view =
1652 rtc::ArrayView<const float>(linear_aec_buffer->channels_const()[ch],
1653 linear_aec_buffer->num_frames());
1654 std::copy(channel_view.begin(), channel_view.end(),
1655 linear_output[ch].begin());
1656 }
1657 return true;
1658 }
1659 RTC_LOG(LS_ERROR) << "No linear AEC output available";
1660 RTC_NOTREACHED();
1661 return false;
1662}
1663
niklase@google.com470e71d2011-07-07 08:21:25 +00001664int 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_);
Per Åhgren0e3198e2019-11-18 08:52:22 +01001691
1692 if (submodules_.agc_manager) {
1693 submodules_.agc_manager->set_stream_analog_level(level);
1694 data_dumper_->DumpRaw("experimental_gain_control_set_stream_analog_level",
1695 1, &level);
1696 } else {
1697 int error = submodules_.gain_control->set_stream_analog_level(level);
1698 RTC_DCHECK_EQ(kNoError, error);
1699 }
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001700}
1701
1702int AudioProcessingImpl::recommended_stream_analog_level() const {
1703 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgren0e3198e2019-11-18 08:52:22 +01001704 if (submodules_.agc_manager) {
1705 return submodules_.agc_manager->stream_analog_level();
1706 }
1707 return submodules_.gain_control->stream_analog_level();
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001708}
1709
aleloi868f32f2017-05-23 07:20:05 -07001710void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1711 RTC_DCHECK(aec_dump);
1712 rtc::CritScope cs_render(&crit_render_);
1713 rtc::CritScope cs_capture(&crit_capture_);
1714
1715 // The previously attached AecDump will be destroyed with the
1716 // 'aec_dump' parameter, which is after locks are released.
1717 aec_dump_.swap(aec_dump);
1718 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001719 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001720}
1721
1722void AudioProcessingImpl::DetachAecDump() {
1723 // The d-tor of a task-queue based AecDump blocks until all pending
1724 // tasks are done. This construction avoids blocking while holding
1725 // the render and capture locks.
1726 std::unique_ptr<AecDump> aec_dump = nullptr;
1727 {
1728 rtc::CritScope cs_render(&crit_render_);
1729 rtc::CritScope cs_capture(&crit_capture_);
1730 aec_dump = std::move(aec_dump_);
1731 }
1732}
1733
Sam Zackrisson4d364492018-03-02 16:03:21 +01001734void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1735 std::unique_ptr<AudioGenerator> audio_generator) {
1736 // TODO(bugs.webrtc.org/8882) Stub.
1737 // Reset internal audio generator with audio_generator.
1738}
1739
1740void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1741 // TODO(bugs.webrtc.org/8882) Stub.
1742 // Delete audio generator, if one is attached.
1743}
1744
peah8271d042016-11-22 07:24:52 -08001745void AudioProcessingImpl::MutateConfig(
1746 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1747 rtc::CritScope cs_render(&crit_render_);
1748 rtc::CritScope cs_capture(&crit_capture_);
1749 mutator(&config_);
1750 ApplyConfig(config_);
1751}
1752
1753AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1754 rtc::CritScope cs_render(&crit_render_);
1755 rtc::CritScope cs_capture(&crit_capture_);
1756 return config_;
1757}
1758
peah2ace3f92016-09-10 04:42:27 -07001759bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1760 return submodule_states_.Update(
Per Åhgren62ea0aa2019-12-09 10:18:44 +01001761 config_.high_pass_filter.enabled, !!submodules_.echo_control_mobile,
1762 config_.residual_echo_detector.enabled,
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001763 !!submodules_.legacy_noise_suppressor || !!submodules_.noise_suppressor,
Per Åhgren2bd85ab2020-01-03 10:36:34 +01001764 submodules_.gain_control->is_enabled(), !!submodules_.gain_controller2,
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001765 config_.pre_amplifier.enabled, capture_nonlocked_.echo_controller_enabled,
Per Åhgrenc0734712020-01-02 15:15:36 +01001766 config_.voice_detection.enabled, !!submodules_.transient_suppressor);
ekmeyerson60d9b332015-08-14 10:35:55 -07001767}
1768
Per Åhgrenc0734712020-01-02 15:15:36 +01001769void AudioProcessingImpl::InitializeTransientSuppressor() {
1770 if (config_.transient_suppression.enabled) {
1771 if (!submodules_.transient_suppressor) {
saza1d600522019-10-18 13:29:43 +02001772 submodules_.transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001773 }
saza1d600522019-10-18 13:29:43 +02001774 submodules_.transient_suppressor->Initialize(proc_fullband_sample_rate_hz(),
1775 capture_nonlocked_.split_rate,
1776 num_proc_channels());
Per Åhgrenc0734712020-01-02 15:15:36 +01001777 } else {
1778 submodules_.transient_suppressor.reset();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001779 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001780}
1781
Per Åhgren4011de02019-12-03 11:48:48 +00001782void AudioProcessingImpl::InitializeHighPassFilter() {
Per Åhgrenb8106462019-12-04 08:34:12 +01001783 bool high_pass_filter_needed_by_aec =
1784 config_.echo_canceller.enabled &&
1785 config_.echo_canceller.enforce_high_pass_filtering &&
1786 !config_.echo_canceller.mobile_mode;
1787 if (submodule_states_.HighPassFilteringRequired() ||
1788 high_pass_filter_needed_by_aec) {
Per Åhgrenc0424252019-12-10 13:04:15 +01001789 bool use_full_band = config_.high_pass_filter.apply_in_full_band &&
1790 !constants_.enforce_split_band_hpf;
1791 int rate = use_full_band ? proc_fullband_sample_rate_hz()
1792 : proc_split_sample_rate_hz();
1793 size_t num_channels =
1794 use_full_band ? num_output_channels() : num_proc_channels();
1795
1796 submodules_.high_pass_filter.reset(new HighPassFilter(rate, num_channels));
peah8271d042016-11-22 07:24:52 -08001797 } else {
saza1d600522019-10-18 13:29:43 +02001798 submodules_.high_pass_filter.reset();
peah8271d042016-11-22 07:24:52 -08001799 }
1800}
alessiob3ec96df2017-05-22 06:57:06 -07001801
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001802void AudioProcessingImpl::InitializeVoiceDetector() {
1803 if (config_.voice_detection.enabled) {
saza1d600522019-10-18 13:29:43 +02001804 submodules_.voice_detector = std::make_unique<VoiceDetection>(
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001805 proc_split_sample_rate_hz(), VoiceDetection::kVeryLowLikelihood);
1806 } else {
saza1d600522019-10-18 13:29:43 +02001807 submodules_.voice_detector.reset();
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001808 }
1809}
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001810void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001811 bool use_echo_controller =
1812 echo_control_factory_ ||
Per Åhgren62ea0aa2019-12-09 10:18:44 +01001813 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001814
1815 if (use_echo_controller) {
1816 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001817 if (echo_control_factory_) {
Per Åhgren4e5c7092019-11-01 20:44:11 +01001818 submodules_.echo_controller = echo_control_factory_->Create(
1819 proc_sample_rate_hz(), num_reverse_channels(), num_proc_channels());
Gustaf Ullberg2c6f3732019-11-07 17:15:12 +01001820 RTC_DCHECK(submodules_.echo_controller);
Per Åhgren200feba2019-03-06 04:16:46 +01001821 } else {
Per Åhgrenb2b58d82019-12-02 14:59:40 +01001822 EchoCanceller3Config config =
1823 use_setup_specific_default_aec3_config_
1824 ? EchoCanceller3::CreateDefaultConfig(num_reverse_channels(),
1825 num_proc_channels())
1826 : EchoCanceller3Config();
saza1d600522019-10-18 13:29:43 +02001827 submodules_.echo_controller = std::make_unique<EchoCanceller3>(
Per Åhgrenb2b58d82019-12-02 14:59:40 +01001828 config, proc_sample_rate_hz(), num_reverse_channels(),
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001829 num_proc_channels());
Per Åhgren200feba2019-03-06 04:16:46 +01001830 }
1831
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001832 // Setup the storage for returning the linear AEC output.
1833 if (config_.echo_canceller.export_linear_aec_output) {
1834 constexpr int kLinearOutputRateHz = 16000;
1835 capture_.linear_aec_output = std::make_unique<AudioBuffer>(
1836 kLinearOutputRateHz, num_proc_channels(), kLinearOutputRateHz,
1837 num_proc_channels(), kLinearOutputRateHz, num_proc_channels());
1838 } else {
1839 capture_.linear_aec_output.reset();
1840 }
1841
Per Åhgren200feba2019-03-06 04:16:46 +01001842 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001843
saza1d600522019-10-18 13:29:43 +02001844 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001845 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001846 return;
peahe0eae3c2016-12-14 01:16:23 -08001847 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001848
saza1d600522019-10-18 13:29:43 +02001849 submodules_.echo_controller.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001850 capture_nonlocked_.echo_controller_enabled = false;
Per Åhgrenc20a19c2019-11-13 11:12:29 +01001851 capture_.linear_aec_output.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001852
1853 if (!config_.echo_canceller.enabled) {
saza1d600522019-10-18 13:29:43 +02001854 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001855 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001856 return;
1857 }
1858
1859 if (config_.echo_canceller.mobile_mode) {
1860 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001861 size_t max_element_size =
1862 std::max(static_cast<size_t>(1),
1863 kMaxAllowedValuesOfSamplesPerBand *
1864 EchoControlMobileImpl::NumCancellersRequired(
1865 num_output_channels(), num_reverse_channels()));
1866
1867 std::vector<int16_t> template_queue_element(max_element_size);
1868
1869 aecm_render_signal_queue_.reset(
1870 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1871 kMaxNumFramesToBuffer, template_queue_element,
1872 RenderQueueItemVerifier<int16_t>(max_element_size)));
1873
1874 aecm_render_queue_buffer_.resize(max_element_size);
1875 aecm_capture_queue_buffer_.resize(max_element_size);
1876
saza1d600522019-10-18 13:29:43 +02001877 submodules_.echo_control_mobile.reset(new EchoControlMobileImpl());
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001878
saza1d600522019-10-18 13:29:43 +02001879 submodules_.echo_control_mobile->Initialize(proc_split_sample_rate_hz(),
1880 num_reverse_channels(),
1881 num_output_channels());
Per Åhgrenf204faf2019-04-25 15:18:06 +02001882 return;
1883 }
1884
saza1d600522019-10-18 13:29:43 +02001885 submodules_.echo_control_mobile.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001886 aecm_render_signal_queue_.reset();
peahe0eae3c2016-12-14 01:16:23 -08001887}
peah8271d042016-11-22 07:24:52 -08001888
alessiob3ec96df2017-05-22 06:57:06 -07001889void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001890 if (config_.gain_controller2.enabled) {
Per Åhgren2bd85ab2020-01-03 10:36:34 +01001891 if (!submodules_.gain_controller2) {
1892 // TODO(alessiob): Move the injected gain controller once injection is
1893 // implemented.
1894 submodules_.gain_controller2.reset(new GainController2());
1895 }
1896
saza1d600522019-10-18 13:29:43 +02001897 submodules_.gain_controller2->Initialize(proc_fullband_sample_rate_hz());
Per Åhgren2bd85ab2020-01-03 10:36:34 +01001898 submodules_.gain_controller2->ApplyConfig(config_.gain_controller2);
1899 } else {
1900 submodules_.gain_controller2.reset();
alessiob3ec96df2017-05-22 06:57:06 -07001901 }
1902}
1903
saza0bad15f2019-10-16 11:46:11 +02001904void AudioProcessingImpl::InitializeNoiseSuppressor() {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001905 submodules_.legacy_noise_suppressor.reset();
1906 submodules_.noise_suppressor.reset();
1907
saza0bad15f2019-10-16 11:46:11 +02001908 if (config_.noise_suppression.enabled) {
Per Åhgren0cbb58e2019-10-29 22:59:44 +01001909 const bool use_legacy_ns =
1910 config_.noise_suppression.use_legacy_ns || enforced_usage_of_legacy_ns_;
1911
1912 if (!use_legacy_ns) {
1913 auto map_level =
1914 [](AudioProcessing::Config::NoiseSuppression::Level level) {
1915 using NoiseSuppresionConfig =
1916 AudioProcessing::Config::NoiseSuppression;
1917 switch (level) {
1918 case NoiseSuppresionConfig::kLow:
1919 return NsConfig::SuppressionLevel::k6dB;
1920 case NoiseSuppresionConfig::kModerate:
1921 return NsConfig::SuppressionLevel::k12dB;
1922 case NoiseSuppresionConfig::kHigh:
1923 return NsConfig::SuppressionLevel::k18dB;
1924 case NoiseSuppresionConfig::kVeryHigh:
1925 return NsConfig::SuppressionLevel::k21dB;
1926 default:
1927 RTC_NOTREACHED();
1928 }
1929 };
1930
1931 NsConfig cfg;
1932 cfg.target_level = map_level(config_.noise_suppression.level);
1933 submodules_.noise_suppressor = std::make_unique<NoiseSuppressor>(
1934 cfg, proc_sample_rate_hz(), num_proc_channels());
1935 } else {
1936 auto ns_level =
1937 NsConfigLevelToInterfaceLevel(config_.noise_suppression.level);
1938 submodules_.legacy_noise_suppressor = std::make_unique<NoiseSuppression>(
1939 num_proc_channels(), proc_sample_rate_hz(), ns_level);
1940 }
saza0bad15f2019-10-16 11:46:11 +02001941 }
1942}
1943
Alex Loikob5c9a792018-04-16 16:31:22 +02001944void AudioProcessingImpl::InitializePreAmplifier() {
1945 if (config_.pre_amplifier.enabled) {
saza1d600522019-10-18 13:29:43 +02001946 submodules_.pre_amplifier.reset(
Alex Loikob5c9a792018-04-16 16:31:22 +02001947 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1948 } else {
saza1d600522019-10-18 13:29:43 +02001949 submodules_.pre_amplifier.reset();
Alex Loikob5c9a792018-04-16 16:31:22 +02001950 }
1951}
1952
ivoc9f4a4a02016-10-28 05:39:16 -07001953void AudioProcessingImpl::InitializeResidualEchoDetector() {
saza1d600522019-10-18 13:29:43 +02001954 RTC_DCHECK(submodules_.echo_detector);
1955 submodules_.echo_detector->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001956 proc_fullband_sample_rate_hz(), 1,
Ivo Creusenb1facc12018-04-12 16:15:58 +02001957 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001958}
1959
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001960void AudioProcessingImpl::InitializeAnalyzer() {
saza1d600522019-10-18 13:29:43 +02001961 if (submodules_.capture_analyzer) {
1962 submodules_.capture_analyzer->Initialize(proc_fullband_sample_rate_hz(),
1963 num_proc_channels());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001964 }
1965}
1966
Sam Zackrisson0beac582017-09-25 12:04:02 +02001967void AudioProcessingImpl::InitializePostProcessor() {
saza1d600522019-10-18 13:29:43 +02001968 if (submodules_.capture_post_processor) {
1969 submodules_.capture_post_processor->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001970 proc_fullband_sample_rate_hz(), num_proc_channels());
Sam Zackrisson0beac582017-09-25 12:04:02 +02001971 }
1972}
1973
Alex Loiko5825aa62017-12-18 16:02:40 +01001974void AudioProcessingImpl::InitializePreProcessor() {
saza1d600522019-10-18 13:29:43 +02001975 if (submodules_.render_pre_processor) {
1976 submodules_.render_pre_processor->Initialize(
Alex Loiko5825aa62017-12-18 16:02:40 +01001977 formats_.render_processing_format.sample_rate_hz(),
1978 formats_.render_processing_format.num_channels());
1979 }
1980}
1981
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001982void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001983
aleloi868f32f2017-05-23 07:20:05 -07001984void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1985 if (!aec_dump_) {
1986 return;
1987 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001988
1989 std::string experiments_description = "";
aleloi868f32f2017-05-23 07:20:05 -07001990 // TODO(peah): Add semicolon-separated concatenations of experiment
1991 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001992 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1993 experiments_description += "AgcClippingLevelExperiment;";
1994 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001995 if (capture_nonlocked_.echo_controller_enabled) {
1996 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001997 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001998 if (config_.gain_controller2.enabled) {
1999 experiments_description += "GainController2;";
2000 }
aleloi868f32f2017-05-23 07:20:05 -07002001
2002 InternalAPMConfig apm_config;
2003
Per Åhgren200feba2019-03-06 04:16:46 +01002004 apm_config.aec_enabled = config_.echo_canceller.enabled;
Per Åhgren62ea0aa2019-12-09 10:18:44 +01002005 apm_config.aec_delay_agnostic_enabled = false;
2006 apm_config.aec_extended_filter_enabled = false;
2007 apm_config.aec_suppression_level = 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();
Per Åhgren0e3198e2019-11-18 08:52:22 +01002022 apm_config.noise_robust_agc_enabled = !!submodules_.agc_manager;
aleloi868f32f2017-05-23 07:20:05 -07002023
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 =
Per Åhgrenc0734712020-01-02 15:15:36 +01002030 config_.transient_suppression.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;
Per Åhgren62ea0aa2019-12-09 10:18:44 +01002088 audio_proc_state.drift = 0;
Per Åhgren0e3198e2019-11-18 08:52:22 +01002089 audio_proc_state.level = recommended_stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002090 audio_proc_state.keypress = capture_.key_pressed;
2091 aec_dump_->AddAudioProcessingState(audio_proc_state);
2092}
2093
Per Åhgrenc0734712020-01-02 15:15:36 +01002094AudioProcessingImpl::ApmCaptureState::ApmCaptureState()
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002095 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002096 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002097 output_will_be_muted(false),
2098 key_pressed(false),
peahde65ddc2016-09-16 15:02:15 -07002099 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002100 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002101 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002102 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002103 prev_pre_amp_gain(-1.f),
2104 playout_volume(-1),
2105 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002106
2107AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2108
Per Åhgrena1351272019-08-15 12:15:46 +02002109void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2110 const float* const* data,
2111 const StreamConfig& stream_config) {
2112 if (stream_config.has_keyboard()) {
2113 keyboard_data = data[stream_config.num_channels()];
2114 } else {
2115 keyboard_data = NULL;
2116 }
2117 num_keyboard_frames = stream_config.num_frames();
2118}
2119
kwiberg83ffe452016-08-29 14:46:07 -07002120AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2121
2122AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2123
Per Åhgrencf4c8722019-12-30 14:32:14 +01002124AudioProcessingImpl::ApmStatsReporter::ApmStatsReporter()
2125 : stats_message_queue_(1) {}
2126
2127AudioProcessingImpl::ApmStatsReporter::~ApmStatsReporter() = default;
2128
2129AudioProcessingStats AudioProcessingImpl::ApmStatsReporter::GetStatistics() {
2130 rtc::CritScope cs_stats(&crit_stats_);
2131 bool new_stats_available = stats_message_queue_.Remove(&cached_stats_);
2132 // If the message queue is full, return the cached stats.
2133 static_cast<void>(new_stats_available);
2134
2135 return cached_stats_;
2136}
2137
2138void AudioProcessingImpl::ApmStatsReporter::UpdateStatistics(
2139 const AudioProcessingStats& new_stats) {
2140 AudioProcessingStats stats_to_queue = new_stats;
2141 bool stats_message_passed = stats_message_queue_.Insert(&stats_to_queue);
2142 // If the message queue is full, discard the new stats.
2143 static_cast<void>(stats_message_passed);
2144}
2145
niklase@google.com470e71d2011-07-07 08:21:25 +00002146} // namespace webrtc