blob: c661848d34a9d86eb04a01d1954da6cfefb5d852 [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"
Per Åhgren200feba2019-03-06 04:16:46 +010024#include "modules/audio_processing/aec3/echo_canceller3.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/agc/agc_manager_direct.h"
Alex Loikob5c9a792018-04-16 16:31:22 +020026#include "modules/audio_processing/agc2/gain_applier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/common.h"
29#include "modules/audio_processing/echo_cancellation_impl.h"
30#include "modules/audio_processing/echo_control_mobile_impl.h"
Sam Zackrissonf0d1c032019-03-27 13:28:08 +010031#include "modules/audio_processing/gain_control_config_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_processing/gain_control_for_experimental_agc.h"
33#include "modules/audio_processing/gain_control_impl.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010034#include "modules/audio_processing/gain_controller2.h"
Per Åhgren0aefbf02019-08-23 21:29:17 +020035#include "modules/audio_processing/high_pass_filter.h"
Yves Gerey988cc082018-10-23 12:03:01 +020036#include "modules/audio_processing/include/audio_frame_view.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020037#include "modules/audio_processing/level_estimator_impl.h"
Per Åhgren13735822018-02-12 21:42:56 +010038#include "modules/audio_processing/logging/apm_data_dumper.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020039#include "modules/audio_processing/noise_suppression_impl.h"
Sam Zackrisson23513132019-01-11 15:10:32 +010040#include "modules/audio_processing/noise_suppression_proxy.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020041#include "modules/audio_processing/residual_echo_detector.h"
42#include "modules/audio_processing/transient/transient_suppressor.h"
Sam Zackrisson0824c6f2019-10-07 14:03:56 +020043#include "modules/audio_processing/voice_detection.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080048#include "rtc_base/ref_counted_object.h"
49#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "rtc_base/trace_event.h"
Sam Zackrissonfeee1e42019-09-20 07:50:35 +020051#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020052#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000053
Michael Graczyk86c6d332015-07-23 11:41:39 -070054#define RETURN_ON_ERR(expr) \
55 do { \
56 int err = (expr); \
57 if (err != kNoError) { \
58 return err; \
59 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000060 } while (0)
61
niklase@google.com470e71d2011-07-07 08:21:25 +000062namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070063
kwibergd59d3bb2016-09-13 07:49:33 -070064constexpr int AudioProcessing::kNativeSampleRatesHz[];
Alex Loiko73ec0192018-05-15 10:52:28 +020065constexpr int kRuntimeSettingQueueSize = 100;
aluebsdf6416a2016-03-16 18:26:35 -070066
Michael Graczyk86c6d332015-07-23 11:41:39 -070067namespace {
68
69static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
70 switch (layout) {
71 case AudioProcessing::kMono:
72 case AudioProcessing::kStereo:
73 return false;
74 case AudioProcessing::kMonoAndKeyboard:
75 case AudioProcessing::kStereoAndKeyboard:
76 return true;
77 }
78
kwiberg9e2be5f2016-09-14 05:23:22 -070079 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070080 return false;
81}
aluebsdf6416a2016-03-16 18:26:35 -070082
peah2ace3f92016-09-10 04:42:27 -070083bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070084 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
85 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
86}
87
Per Åhgrenc8626b62019-08-23 15:49:51 +020088// Identify the native processing rate that best handles a sample rate.
Per Åhgrenfcbe4072019-09-15 00:27:58 +020089int SuitableProcessRate(int minimum_rate,
90 int max_splitting_rate,
91 bool band_splitting_required) {
Per Åhgrenc8626b62019-08-23 15:49:51 +020092 const int uppermost_native_rate =
Per Åhgrenfcbe4072019-09-15 00:27:58 +020093 band_splitting_required ? max_splitting_rate : 48000;
Per Åhgrenc8626b62019-08-23 15:49:51 +020094 for (auto rate : {16000, 32000, 48000}) {
peah2ace3f92016-09-10 04:42:27 -070095 if (rate >= uppermost_native_rate) {
96 return uppermost_native_rate;
97 }
98 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -070099 return rate;
100 }
101 }
peah2ace3f92016-09-10 04:42:27 -0700102 RTC_NOTREACHED();
103 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700104}
105
Sam Zackrisson23513132019-01-11 15:10:32 +0100106NoiseSuppression::Level NsConfigLevelToInterfaceLevel(
107 AudioProcessing::Config::NoiseSuppression::Level level) {
108 using NsConfig = AudioProcessing::Config::NoiseSuppression;
109 switch (level) {
110 case NsConfig::kLow:
111 return NoiseSuppression::kLow;
112 case NsConfig::kModerate:
113 return NoiseSuppression::kModerate;
114 case NsConfig::kHigh:
115 return NoiseSuppression::kHigh;
116 case NsConfig::kVeryHigh:
117 return NoiseSuppression::kVeryHigh;
118 default:
119 RTC_NOTREACHED();
120 }
121}
122
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100123GainControl::Mode Agc1ConfigModeToInterfaceMode(
124 AudioProcessing::Config::GainController1::Mode mode) {
125 using Agc1Config = AudioProcessing::Config::GainController1;
126 switch (mode) {
127 case Agc1Config::kAdaptiveAnalog:
128 return GainControl::kAdaptiveAnalog;
129 case Agc1Config::kAdaptiveDigital:
130 return GainControl::kAdaptiveDigital;
131 case Agc1Config::kFixedDigital:
132 return GainControl::kFixedDigital;
133 }
134}
135
peah9e6a2902017-05-15 07:19:21 -0700136// Maximum lengths that frame of samples being passed from the render side to
137// the capture side can have (does not apply to AEC3).
138static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
139static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
140
peah764e3642016-10-22 05:04:30 -0700141// Maximum number of frames to buffer in the render queue.
142// TODO(peah): Decrease this once we properly handle hugely unbalanced
143// reverse and forward call numbers.
144static const size_t kMaxNumFramesToBuffer = 100;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700145} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000146
147// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000148static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000149
Sam Zackrisson0beac582017-09-25 12:04:02 +0200150AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100151 bool capture_post_processor_enabled,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200152 bool render_pre_processor_enabled,
153 bool capture_analyzer_enabled)
Alex Loiko5825aa62017-12-18 16:02:40 +0100154 : capture_post_processor_enabled_(capture_post_processor_enabled),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200155 render_pre_processor_enabled_(render_pre_processor_enabled),
156 capture_analyzer_enabled_(capture_analyzer_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700157
158bool AudioProcessingImpl::ApmSubmoduleStates::Update(
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200159 bool high_pass_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700160 bool echo_canceller_enabled,
161 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700162 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700163 bool noise_suppressor_enabled,
peah2ace3f92016-09-10 04:42:27 -0700164 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700165 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200166 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200167 bool echo_controller_enabled,
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200168 bool voice_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700169 bool level_estimator_enabled,
170 bool transient_suppressor_enabled) {
171 bool changed = false;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200172 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700173 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
174 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_);
peah2ace3f92016-09-10 04:42:27 -0700184 changed |= (level_estimator_enabled != level_estimator_enabled_);
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200185 changed |= (voice_detector_enabled != voice_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700186 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
187 if (changed) {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200188 high_pass_filter_enabled_ = high_pass_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700189 echo_canceller_enabled_ = echo_canceller_enabled;
190 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700191 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700192 noise_suppressor_enabled_ = noise_suppressor_enabled;
peah2ace3f92016-09-10 04:42:27 -0700193 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700194 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200195 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200196 echo_controller_enabled_ = echo_controller_enabled;
peah2ace3f92016-09-10 04:42:27 -0700197 level_estimator_enabled_ = level_estimator_enabled;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200198 voice_detector_enabled_ = voice_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700199 transient_suppressor_enabled_ = transient_suppressor_enabled;
200 }
201
202 changed |= first_update_;
203 first_update_ = false;
204 return changed;
205}
206
207bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
208 const {
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200209 return CaptureMultiBandProcessingActive() || voice_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700210}
211
212bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
213 const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200214 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700215 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200216 adaptive_gain_controller_enabled_ || echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700217}
218
peah23ac8b42017-05-23 05:33:56 -0700219bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
220 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200221 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
222 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700223}
224
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200225bool AudioProcessingImpl::ApmSubmoduleStates::CaptureAnalyzerActive() const {
226 return capture_analyzer_enabled_;
227}
228
peah2ace3f92016-09-10 04:42:27 -0700229bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
230 const {
231 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800232 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200233 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700234}
235
Alex Loiko5825aa62017-12-18 16:02:40 +0100236bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive()
237 const {
238 return render_pre_processor_enabled_;
239}
240
peah2ace3f92016-09-10 04:42:27 -0700241bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
242 const {
peah2ace3f92016-09-10 04:42:27 -0700243 return false;
peah2ace3f92016-09-10 04:42:27 -0700244}
245
Per Åhgren0aefbf02019-08-23 21:29:17 +0200246bool AudioProcessingImpl::ApmSubmoduleStates::HighPassFilteringRequired()
247 const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200248 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
249 mobile_echo_controller_enabled_ || noise_suppressor_enabled_;
250}
251
solenberg5e465c32015-12-08 13:22:33 -0800252struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800253 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800254 // Accessed externally of APM without any lock acquired.
Sam Zackrisson23513132019-01-11 15:10:32 +0100255 // TODO(bugs.webrtc.org/9947): Move these submodules into private_submodules_
256 // when their pointer-to-submodule API functions are gone.
kwiberg88788ad2016-02-19 07:04:49 -0800257 std::unique_ptr<LevelEstimatorImpl> level_estimator;
258 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
Sam Zackrisson23513132019-01-11 15:10:32 +0100259 std::unique_ptr<NoiseSuppressionProxy> noise_suppression_proxy;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100260 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800261 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800262 gain_control_for_experimental_agc;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100263 std::unique_ptr<GainControlConfigProxy> gain_control_config_proxy;
solenberg5e465c32015-12-08 13:22:33 -0800264
265 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800266 std::unique_ptr<TransientSuppressor> transient_suppressor;
solenberg5e465c32015-12-08 13:22:33 -0800267};
268
269struct AudioProcessingImpl::ApmPrivateSubmodules {
Sam Zackrissondb389722018-06-21 10:12:24 +0200270 ApmPrivateSubmodules(std::unique_ptr<CustomProcessing> capture_post_processor,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100271 std::unique_ptr<CustomProcessing> render_pre_processor,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200272 rtc::scoped_refptr<EchoDetector> echo_detector,
273 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Sam Zackrissondb389722018-06-21 10:12:24 +0200274 : echo_detector(std::move(echo_detector)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100275 capture_post_processor(std::move(capture_post_processor)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200276 render_pre_processor(std::move(render_pre_processor)),
277 capture_analyzer(std::move(capture_analyzer)) {}
solenberg5e465c32015-12-08 13:22:33 -0800278 // Accessed internally from capture or during initialization
kwiberg88788ad2016-02-19 07:04:49 -0800279 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700280 std::unique_ptr<GainController2> gain_controller2;
Per Åhgren0aefbf02019-08-23 21:29:17 +0200281 std::unique_ptr<HighPassFilter> high_pass_filter;
Ivo Creusend1f970d2018-06-14 11:02:03 +0200282 rtc::scoped_refptr<EchoDetector> echo_detector;
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100283 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
Sam Zackrissonc22f5512018-11-05 16:10:00 +0100284 std::unique_ptr<EchoControl> echo_controller;
285 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
Alex Loiko5825aa62017-12-18 16:02:40 +0100286 std::unique_ptr<CustomProcessing> capture_post_processor;
287 std::unique_ptr<CustomProcessing> render_pre_processor;
Alex Loikob5c9a792018-04-16 16:31:22 +0200288 std::unique_ptr<GainApplier> pre_amplifier;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200289 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100290 std::unique_ptr<LevelEstimatorImpl> output_level_estimator;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200291 std::unique_ptr<VoiceDetection> voice_detector;
solenberg5e465c32015-12-08 13:22:33 -0800292};
293
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100294AudioProcessingBuilder::AudioProcessingBuilder() = default;
295AudioProcessingBuilder::~AudioProcessingBuilder() = default;
296
297AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
298 std::unique_ptr<CustomProcessing> capture_post_processing) {
299 capture_post_processing_ = std::move(capture_post_processing);
300 return *this;
301}
302
303AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
304 std::unique_ptr<CustomProcessing> render_pre_processing) {
305 render_pre_processing_ = std::move(render_pre_processing);
306 return *this;
307}
308
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200309AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
310 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
311 capture_analyzer_ = std::move(capture_analyzer);
312 return *this;
313}
314
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100315AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
316 std::unique_ptr<EchoControlFactory> echo_control_factory) {
317 echo_control_factory_ = std::move(echo_control_factory);
318 return *this;
319}
320
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100321AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200322 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100323 echo_detector_ = std::move(echo_detector);
324 return *this;
325}
326
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100327AudioProcessing* AudioProcessingBuilder::Create() {
328 webrtc::Config config;
329 return Create(config);
330}
331
332AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100333 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
334 config, std::move(capture_post_processing_),
335 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200336 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100337 if (apm->Initialize() != AudioProcessing::kNoError) {
338 delete apm;
339 apm = nullptr;
340 }
341 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100342}
343
peah88ac8532016-09-12 16:47:25 -0700344AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200345 : AudioProcessingImpl(config,
346 /*capture_post_processor=*/nullptr,
347 /*render_pre_processor=*/nullptr,
348 /*echo_control_factory=*/nullptr,
349 /*echo_detector=*/nullptr,
350 /*capture_analyzer=*/nullptr) {}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000351
Per Åhgren13735822018-02-12 21:42:56 +0100352int AudioProcessingImpl::instance_count_ = 0;
353
Sam Zackrisson0beac582017-09-25 12:04:02 +0200354AudioProcessingImpl::AudioProcessingImpl(
355 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100356 std::unique_ptr<CustomProcessing> capture_post_processor,
357 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200358 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200359 rtc::scoped_refptr<EchoDetector> echo_detector,
360 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100361 : data_dumper_(
362 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200363 capture_runtime_settings_(kRuntimeSettingQueueSize),
364 render_runtime_settings_(kRuntimeSettingQueueSize),
365 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
366 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200367 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200368 submodule_states_(!!capture_post_processor,
369 !!render_pre_processor,
370 !!capture_analyzer),
peah8271d042016-11-22 07:24:52 -0800371 public_submodules_(new ApmPublicSubmodules()),
Sam Zackrisson0beac582017-09-25 12:04:02 +0200372 private_submodules_(
Sam Zackrissondb389722018-06-21 10:12:24 +0200373 new ApmPrivateSubmodules(std::move(capture_post_processor),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100374 std::move(render_pre_processor),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200375 std::move(echo_detector),
376 std::move(capture_analyzer))),
peahdf3efa82015-11-28 12:35:15 -0800377 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800378 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000379#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alex Loikod9342442018-09-10 13:59:41 +0200380 /* enabled= */ false,
381 /* enabled_agc2_level_estimator= */ false,
382 /* digital_adaptive_disabled= */ false,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200383 /* analyze_before_aec= */ false,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000384#else
Alex Loiko64cb83b2018-07-02 13:38:19 +0200385 config.Get<ExperimentalAgc>().enabled,
386 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
Alex Loikod9342442018-09-10 13:59:41 +0200387 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200388 config.Get<ExperimentalAgc>().analyze_before_aec,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000389#endif
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200390 !field_trial::IsEnabled(
391 "WebRTC-ApmExperimentalMultiChannelRenderKillSwitch"),
392 !field_trial::IsEnabled(
393 "WebRTC-ApmExperimentalMultiChannelCaptureKillSwitch")),
andrew1c7075f2015-06-24 18:14:14 -0700394#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200395 capture_(false),
andrew1c7075f2015-06-24 18:14:14 -0700396#else
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200397 capture_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700398#endif
Alessio Bazzicacc22f512018-08-30 13:01:34 +0200399 capture_nonlocked_() {
Sam Zackrisson421c8592019-02-11 13:39:46 +0100400 // Mark Echo Controller enabled if a factory is injected.
401 capture_nonlocked_.echo_controller_enabled =
402 static_cast<bool>(echo_control_factory_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000403
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100404 public_submodules_->gain_control.reset(new GainControlImpl());
Sam Zackrisson421c8592019-02-11 13:39:46 +0100405 public_submodules_->level_estimator.reset(
406 new LevelEstimatorImpl(&crit_capture_));
407 public_submodules_->noise_suppression.reset(
408 new NoiseSuppressionImpl(&crit_capture_));
409 public_submodules_->noise_suppression_proxy.reset(new NoiseSuppressionProxy(
410 this, public_submodules_->noise_suppression.get()));
Sam Zackrisson421c8592019-02-11 13:39:46 +0100411 public_submodules_->gain_control_for_experimental_agc.reset(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100412 new GainControlForExperimentalAgc(
413 public_submodules_->gain_control.get()));
414 public_submodules_->gain_control_config_proxy.reset(
415 new GainControlConfigProxy(&crit_capture_, this, agc1()));
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200416
Sam Zackrisson421c8592019-02-11 13:39:46 +0100417 // If no echo detector is injected, use the ResidualEchoDetector.
418 if (!private_submodules_->echo_detector) {
419 private_submodules_->echo_detector =
420 new rtc::RefCountedObject<ResidualEchoDetector>();
peahdf3efa82015-11-28 12:35:15 -0800421 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000422
Sam Zackrisson421c8592019-02-11 13:39:46 +0100423 // TODO(alessiob): Move the injected gain controller once injection is
424 // implemented.
425 private_submodules_->gain_controller2.reset(new GainController2());
426
427 RTC_LOG(LS_INFO) << "Capture analyzer activated: "
428 << !!private_submodules_->capture_analyzer
429 << "\nCapture post processor activated: "
430 << !!private_submodules_->capture_post_processor
431 << "\nRender pre processor activated: "
432 << !!private_submodules_->render_pre_processor;
433
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000434 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000435}
436
437AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800438 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800439 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800440 private_submodules_->agc_manager.reset();
441 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800442 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000443}
444
niklase@google.com470e71d2011-07-07 08:21:25 +0000445int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800446 // Run in a single-threaded manner during initialization.
447 rtc::CritScope cs_render(&crit_render_);
448 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000449 return InitializeLocked();
450}
451
peahde65ddc2016-09-16 15:02:15 -0700452int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
453 int capture_output_sample_rate_hz,
454 int render_input_sample_rate_hz,
455 ChannelLayout capture_input_layout,
456 ChannelLayout capture_output_layout,
457 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700458 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700459 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
460 LayoutHasKeyboard(capture_input_layout)},
461 {capture_output_sample_rate_hz,
462 ChannelsFromLayout(capture_output_layout),
463 LayoutHasKeyboard(capture_output_layout)},
464 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
465 LayoutHasKeyboard(render_input_layout)},
466 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
467 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700468
469 return Initialize(processing_config);
470}
471
472int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800473 // Run in a single-threaded manner during initialization.
474 rtc::CritScope cs_render(&crit_render_);
475 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700476 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000477}
478
peahdf3efa82015-11-28 12:35:15 -0800479int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800480 const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800481 // Called from both threads. Thread check is therefore not possible.
Oskar Sundbom4b276482019-05-23 14:28:00 +0200482 if (processing_config == formats_.api_format) {
peah192164e2015-11-17 02:16:45 -0800483 return kNoError;
484 }
peahdf3efa82015-11-28 12:35:15 -0800485
486 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800487 return InitializeLocked(processing_config);
488}
489
niklase@google.com470e71d2011-07-07 08:21:25 +0000490int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200491 UpdateActiveSubmoduleStates();
492
Per Åhgrend47941e2019-08-22 11:51:13 +0200493 const int render_audiobuffer_sample_rate_hz =
peahdf3efa82015-11-28 12:35:15 -0800494 formats_.api_format.reverse_output_stream().num_frames() == 0
Per Åhgrend47941e2019-08-22 11:51:13 +0200495 ? formats_.render_processing_format.sample_rate_hz()
496 : formats_.api_format.reverse_output_stream().sample_rate_hz();
peahdf3efa82015-11-28 12:35:15 -0800497 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
498 render_.render_audio.reset(new AudioBuffer(
Per Åhgrend47941e2019-08-22 11:51:13 +0200499 formats_.api_format.reverse_input_stream().sample_rate_hz(),
peahdf3efa82015-11-28 12:35:15 -0800500 formats_.api_format.reverse_input_stream().num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200501 formats_.render_processing_format.sample_rate_hz(),
peahde65ddc2016-09-16 15:02:15 -0700502 formats_.render_processing_format.num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200503 render_audiobuffer_sample_rate_hz,
504 formats_.render_processing_format.num_channels()));
peah2ace3f92016-09-10 04:42:27 -0700505 if (formats_.api_format.reverse_input_stream() !=
506 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800507 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800508 formats_.api_format.reverse_input_stream().num_channels(),
509 formats_.api_format.reverse_input_stream().num_frames(),
510 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800511 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700512 } else {
peahdf3efa82015-11-28 12:35:15 -0800513 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700514 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700515 } else {
peahdf3efa82015-11-28 12:35:15 -0800516 render_.render_audio.reset(nullptr);
517 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700518 }
peahce4d9152017-05-19 01:28:05 -0700519
Per Åhgrend47941e2019-08-22 11:51:13 +0200520 capture_.capture_audio.reset(new AudioBuffer(
521 formats_.api_format.input_stream().sample_rate_hz(),
522 formats_.api_format.input_stream().num_channels(),
523 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
524 formats_.api_format.output_stream().num_channels(),
525 formats_.api_format.output_stream().sample_rate_hz(),
526 formats_.api_format.output_stream().num_channels()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000527
peah764e3642016-10-22 05:04:30 -0700528 AllocateRenderQueue();
529
peah135259a2016-10-28 03:12:11 -0700530 public_submodules_->gain_control->Initialize(num_proc_channels(),
531 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700532 if (constants_.use_experimental_agc) {
533 if (!private_submodules_->agc_manager.get()) {
534 private_submodules_->agc_manager.reset(new AgcManagerDirect(
535 public_submodules_->gain_control.get(),
536 public_submodules_->gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200537 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
538 constants_.use_experimental_agc_agc2_level_estimation,
539 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700540 }
541 private_submodules_->agc_manager->Initialize();
542 private_submodules_->agc_manager->SetCaptureMuted(
543 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700544 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700545 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200546 InitializeTransient();
Per Åhgren0aefbf02019-08-23 21:29:17 +0200547 InitializeHighPassFilter();
peahde65ddc2016-09-16 15:02:15 -0700548 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
549 proc_sample_rate_hz());
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200550 InitializeVoiceDetector();
peahde65ddc2016-09-16 15:02:15 -0700551 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700552 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200553 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700554 InitializeGainController2();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200555 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200556 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100557 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800558
aleloi868f32f2017-05-23 07:20:05 -0700559 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200560 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700561 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000562 return kNoError;
563}
564
Michael Graczyk86c6d332015-07-23 11:41:39 -0700565int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200566 UpdateActiveSubmoduleStates();
567
Michael Graczyk86c6d332015-07-23 11:41:39 -0700568 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700569 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
570 return kBadSampleRateError;
571 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000572 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700573
Peter Kasting69558702016-01-12 16:26:35 -0800574 const size_t num_in_channels = config.input_stream().num_channels();
575 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700576
577 // Need at least one input channel.
578 // Need either one output channel or as many outputs as there are inputs.
579 if (num_in_channels == 0 ||
580 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700581 return kBadNumberChannelsError;
582 }
583
peahdf3efa82015-11-28 12:35:15 -0800584 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000585
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200586 // Choose maximum rate to use for the split filtering.
587 RTC_DCHECK(config_.pipeline.maximum_internal_processing_rate == 48000 ||
588 config_.pipeline.maximum_internal_processing_rate == 32000);
589 int max_splitting_rate = 48000;
590 if (config_.pipeline.maximum_internal_processing_rate == 32000) {
591 max_splitting_rate = config_.pipeline.maximum_internal_processing_rate;
592 }
593
Per Åhgrenc8626b62019-08-23 15:49:51 +0200594 int capture_processing_rate = SuitableProcessRate(
peah423d2362016-04-09 16:06:52 -0700595 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700596 formats_.api_format.output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200597 max_splitting_rate,
peah2ace3f92016-09-10 04:42:27 -0700598 submodule_states_.CaptureMultiBandSubModulesActive() ||
599 submodule_states_.RenderMultiBandSubModulesActive());
Per Åhgrenc8626b62019-08-23 15:49:51 +0200600 RTC_DCHECK_NE(8000, capture_processing_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000601
peahde65ddc2016-09-16 15:02:15 -0700602 capture_nonlocked_.capture_processing_format =
603 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700604
peah2ce640f2017-04-07 03:57:48 -0700605 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200606 if (!capture_nonlocked_.echo_controller_enabled) {
Per Åhgrenc8626b62019-08-23 15:49:51 +0200607 render_processing_rate = SuitableProcessRate(
peah2ce640f2017-04-07 03:57:48 -0700608 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
609 formats_.api_format.reverse_output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200610 max_splitting_rate,
peah2ce640f2017-04-07 03:57:48 -0700611 submodule_states_.CaptureMultiBandSubModulesActive() ||
612 submodule_states_.RenderMultiBandSubModulesActive());
613 } else {
614 render_processing_rate = capture_processing_rate;
615 }
616
peahde65ddc2016-09-16 15:02:15 -0700617 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700618 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700619 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
620 kSampleRate8kHz) {
621 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000622 } else {
peahde65ddc2016-09-16 15:02:15 -0700623 render_processing_rate =
624 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000625 }
626
Per Åhgrenc8626b62019-08-23 15:49:51 +0200627 RTC_DCHECK_NE(8000, render_processing_rate);
628
peahce4d9152017-05-19 01:28:05 -0700629 if (submodule_states_.RenderMultiBandSubModulesActive()) {
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200630 // By default, downmix the render stream to mono for analysis. This has been
631 // demonstrated to work well for AEC in most practical scenarios.
632 const bool experimental_multi_channel_render =
633 config_.pipeline.experimental_multi_channel &&
634 constants_.experimental_multi_channel_render_support;
635 int render_processing_num_channels =
636 experimental_multi_channel_render
637 ? formats_.api_format.reverse_input_stream().num_channels()
638 : 1;
639 formats_.render_processing_format =
640 StreamConfig(render_processing_rate, render_processing_num_channels);
peahce4d9152017-05-19 01:28:05 -0700641 } else {
642 formats_.render_processing_format = StreamConfig(
643 formats_.api_format.reverse_input_stream().sample_rate_hz(),
644 formats_.api_format.reverse_input_stream().num_channels());
645 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000646
peahde65ddc2016-09-16 15:02:15 -0700647 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
648 kSampleRate32kHz ||
649 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
650 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800651 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000652 } else {
peahdf3efa82015-11-28 12:35:15 -0800653 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700654 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000655 }
656
657 return InitializeLocked();
658}
659
peah88ac8532016-09-12 16:47:25 -0700660void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peah88ac8532016-09-12 16:47:25 -0700661 // Run in a single-threaded manner when applying the settings.
662 rtc::CritScope cs_render(&crit_render_);
663 rtc::CritScope cs_capture(&crit_capture_);
664
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200665 const bool pipeline_config_changed =
666 config_.pipeline.experimental_multi_channel !=
667 config.pipeline.experimental_multi_channel;
668
Per Åhgren200feba2019-03-06 04:16:46 +0100669 const bool aec_config_changed =
670 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
671 config_.echo_canceller.use_legacy_aec !=
672 config.echo_canceller.use_legacy_aec ||
673 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode ||
674 (config_.echo_canceller.enabled && config.echo_canceller.use_legacy_aec &&
675 config_.echo_canceller.legacy_moderate_suppression_level !=
676 config.echo_canceller.legacy_moderate_suppression_level);
677
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100678 const bool agc1_config_changed =
679 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
680 config_.gain_controller1.mode != config.gain_controller1.mode ||
681 config_.gain_controller1.target_level_dbfs !=
682 config.gain_controller1.target_level_dbfs ||
683 config_.gain_controller1.compression_gain_db !=
684 config.gain_controller1.compression_gain_db ||
685 config_.gain_controller1.enable_limiter !=
686 config.gain_controller1.enable_limiter ||
687 config_.gain_controller1.analog_level_minimum !=
688 config.gain_controller1.analog_level_minimum ||
689 config_.gain_controller1.analog_level_maximum !=
690 config.gain_controller1.analog_level_maximum;
691
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200692 const bool voice_detection_config_changed =
693 config_.voice_detection.enabled != config.voice_detection.enabled;
694
Yves Gerey499bc6c2018-10-10 18:29:07 +0200695 config_ = config;
696
Per Åhgren200feba2019-03-06 04:16:46 +0100697 if (aec_config_changed) {
698 InitializeEchoController();
699 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200700
Sam Zackrisson23513132019-01-11 15:10:32 +0100701 public_submodules_->noise_suppression->Enable(
702 config.noise_suppression.enabled);
703 public_submodules_->noise_suppression->set_level(
704 NsConfigLevelToInterfaceLevel(config.noise_suppression.level));
705
Per Åhgren0aefbf02019-08-23 21:29:17 +0200706 InitializeHighPassFilter();
peah8271d042016-11-22 07:24:52 -0800707
Mirko Bonadei675513b2017-11-09 11:09:25 +0100708 RTC_LOG(LS_INFO) << "Highpass filter activated: "
709 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800710
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100711 if (agc1_config_changed) {
712 ApplyAgc1Config(config_.gain_controller1);
713 }
714
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100715 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700716 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100717 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
718 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100719 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100720 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700721 config_.gain_controller2 = AudioProcessing::Config::GainController2();
722 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200723 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200724 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200725 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100726 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
727 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200728 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
729 << config_.pre_amplifier.enabled;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100730
731 if (config_.level_estimation.enabled &&
732 !private_submodules_->output_level_estimator) {
733 private_submodules_->output_level_estimator.reset(
734 new LevelEstimatorImpl(&crit_capture_));
735 private_submodules_->output_level_estimator->Enable(true);
736 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100737
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200738 if (voice_detection_config_changed) {
739 InitializeVoiceDetector();
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100740 }
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200741
742 // Reinitialization must happen after all submodule configuration to avoid
743 // additional reinitializations on the next capture / render processing call.
744 if (pipeline_config_changed) {
745 InitializeLocked(formats_.api_format);
746 }
peah88ac8532016-09-12 16:47:25 -0700747}
748
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100749void AudioProcessingImpl::ApplyAgc1Config(
750 const Config::GainController1& config) {
751 GainControl* agc = agc1();
752 int error = agc->Enable(config.enabled);
753 RTC_DCHECK_EQ(kNoError, error);
754 error = agc->set_mode(Agc1ConfigModeToInterfaceMode(config.mode));
755 RTC_DCHECK_EQ(kNoError, error);
756 error = agc->set_target_level_dbfs(config.target_level_dbfs);
757 RTC_DCHECK_EQ(kNoError, error);
758 error = agc->set_compression_gain_db(config.compression_gain_db);
759 RTC_DCHECK_EQ(kNoError, error);
760 error = agc->enable_limiter(config.enable_limiter);
761 RTC_DCHECK_EQ(kNoError, error);
762 error = agc->set_analog_level_limits(config.analog_level_minimum,
763 config.analog_level_maximum);
764 RTC_DCHECK_EQ(kNoError, error);
765}
766
767GainControl* AudioProcessingImpl::agc1() {
768 if (constants_.use_experimental_agc) {
769 return public_submodules_->gain_control_for_experimental_agc.get();
770 }
771 return public_submodules_->gain_control.get();
772}
773
774const GainControl* AudioProcessingImpl::agc1() const {
775 if (constants_.use_experimental_agc) {
776 return public_submodules_->gain_control_for_experimental_agc.get();
777 }
778 return public_submodules_->gain_control.get();
779}
780
peah88ac8532016-09-12 16:47:25 -0700781void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800782 // Run in a single-threaded manner when setting the extra options.
783 rtc::CritScope cs_render(&crit_render_);
784 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000785
Per Åhgrenf204faf2019-04-25 15:18:06 +0200786 capture_nonlocked_.use_aec2_extended_filter =
787 config.Get<ExtendedFilter>().enabled;
788 capture_nonlocked_.use_aec2_delay_agnostic =
789 config.Get<DelayAgnostic>().enabled;
790 capture_nonlocked_.use_aec2_refined_adaptive_filter =
791 config.Get<RefinedAdaptiveFilter>().enabled;
peahb624d8c2016-03-05 03:01:14 -0800792
peahdf3efa82015-11-28 12:35:15 -0800793 if (capture_.transient_suppressor_enabled !=
794 config.Get<ExperimentalNs>().enabled) {
795 capture_.transient_suppressor_enabled =
796 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000797 InitializeTransient();
798 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000799}
800
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000801int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800802 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700803 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000804}
805
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000806int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800807 // Used as callback from submodules, hence locking is not allowed.
808 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000809}
810
Peter Kasting69558702016-01-12 16:26:35 -0800811size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800812 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700813 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000814}
815
Peter Kasting69558702016-01-12 16:26:35 -0800816size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800817 // Used as callback from submodules, hence locking is not allowed.
818 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000819}
820
Peter Kasting69558702016-01-12 16:26:35 -0800821size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800822 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200823 const bool experimental_multi_channel_capture =
824 config_.pipeline.experimental_multi_channel &&
825 constants_.experimental_multi_channel_capture_support;
826 if (capture_nonlocked_.echo_controller_enabled &&
827 !experimental_multi_channel_capture) {
828 return 1;
829 }
830 return num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800831}
832
Peter Kasting69558702016-01-12 16:26:35 -0800833size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800834 // Used as callback from submodules, hence locking is not allowed.
835 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000836}
837
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000838void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800839 rtc::CritScope cs(&crit_capture_);
840 capture_.output_will_be_muted = muted;
841 if (private_submodules_->agc_manager.get()) {
842 private_submodules_->agc_manager->SetCaptureMuted(
843 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000844 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000845}
846
Alessio Bazzicac054e782018-04-16 12:10:09 +0200847void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200848 switch (setting.type()) {
849 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
850 render_runtime_settings_enqueuer_.Enqueue(setting);
851 return;
852 case RuntimeSetting::Type::kNotSpecified:
853 RTC_NOTREACHED();
854 return;
855 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100856 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200857 case RuntimeSetting::Type::kCaptureFixedPostGain:
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200858 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200859 capture_runtime_settings_enqueuer_.Enqueue(setting);
860 return;
861 }
862 // The language allows the enum to have a non-enumerator
863 // value. Check that this doesn't happen.
864 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200865}
866
867AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
868 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200869 : runtime_settings_(*runtime_settings) {
870 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200871}
872
873AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
874 default;
875
876void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
877 RuntimeSetting setting) {
878 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200879 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200880 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200881 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200882 RTC_LOG(LS_ERROR)
883 << "The runtime settings queue is full. Oldest setting discarded.";
884 }
885 if (remaining_attempts == 0)
886 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
887}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000888
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000889int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700890 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000891 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000892 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000893 int output_sample_rate_hz,
894 ChannelLayout output_layout,
895 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800896 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800897 StreamConfig input_stream;
898 StreamConfig output_stream;
899 {
900 // Access the formats_.api_format.input_stream beneath the capture lock.
901 // The lock must be released as it is later required in the call
902 // to ProcessStream(,,,);
903 rtc::CritScope cs(&crit_capture_);
904 input_stream = formats_.api_format.input_stream();
905 output_stream = formats_.api_format.output_stream();
906 }
907
Michael Graczyk86c6d332015-07-23 11:41:39 -0700908 input_stream.set_sample_rate_hz(input_sample_rate_hz);
909 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
910 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700911 output_stream.set_sample_rate_hz(output_sample_rate_hz);
912 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
913 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
914
915 if (samples_per_channel != input_stream.num_frames()) {
916 return kBadDataLengthError;
917 }
918 return ProcessStream(src, input_stream, output_stream, dest);
919}
920
921int AudioProcessingImpl::ProcessStream(const float* const* src,
922 const StreamConfig& input_config,
923 const StreamConfig& output_config,
924 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800925 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800926 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700927 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800928 {
929 // Acquire the capture lock in order to safely call the function
930 // that retrieves the render side data. This function accesses apm
931 // getters that need the capture lock held when being called.
932 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700933 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800934
935 if (!src || !dest) {
936 return kNullPointerError;
937 }
938
939 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700940 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000941 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000942
Oskar Sundbom4b276482019-05-23 14:28:00 +0200943 if (processing_config.input_stream() != input_config) {
944 processing_config.input_stream() = input_config;
945 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800946 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200947
948 if (processing_config.output_stream() != output_config) {
949 processing_config.output_stream() = output_config;
950 reinitialization_required = true;
951 }
952
953 if (reinitialization_required) {
954 // Reinitialize.
955 rtc::CritScope cs_render(&crit_render_);
956 rtc::CritScope cs_capture(&crit_capture_);
957 RETURN_ON_ERR(InitializeLocked(processing_config));
958 }
959
peahdf3efa82015-11-28 12:35:15 -0800960 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700961 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
962 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000963
aleloi868f32f2017-05-23 07:20:05 -0700964 if (aec_dump_) {
965 RecordUnprocessedCaptureStream(src);
966 }
967
Per Åhgrena1351272019-08-15 12:15:46 +0200968 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800969 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700970 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800971 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000972
aleloi868f32f2017-05-23 07:20:05 -0700973 if (aec_dump_) {
974 RecordProcessedCaptureStream(dest);
975 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000976 return kNoError;
977}
978
Alex Loiko73ec0192018-05-15 10:52:28 +0200979void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200980 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200981 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200982 if (aec_dump_) {
983 aec_dump_->WriteRuntimeSetting(setting);
984 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200985 switch (setting.type()) {
986 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200987 if (config_.pre_amplifier.enabled) {
988 float value;
989 setting.GetFloat(&value);
990 private_submodules_->pre_amplifier->SetGainFactor(value);
991 }
992 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200993 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100994 case RuntimeSetting::Type::kCaptureCompressionGain: {
995 float value;
996 setting.GetFloat(&value);
997 int int_value = static_cast<int>(value + .5f);
998 config_.gain_controller1.compression_gain_db = int_value;
999 int error = agc1()->set_compression_gain_db(int_value);
1000 RTC_DCHECK_EQ(kNoError, error);
1001 break;
1002 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001003 case RuntimeSetting::Type::kCaptureFixedPostGain: {
1004 if (config_.gain_controller2.enabled) {
1005 float value;
1006 setting.GetFloat(&value);
1007 config_.gain_controller2.fixed_digital.gain_db = value;
1008 private_submodules_->gain_controller2->ApplyConfig(
1009 config_.gain_controller2);
1010 }
1011 break;
1012 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001013 case RuntimeSetting::Type::kPlayoutVolumeChange: {
1014 int value;
1015 setting.GetInt(&value);
1016 capture_.playout_volume = value;
1017 break;
1018 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001019 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1020 RTC_NOTREACHED();
1021 break;
1022 case RuntimeSetting::Type::kNotSpecified:
1023 RTC_NOTREACHED();
1024 break;
1025 }
1026 }
1027}
1028
1029void AudioProcessingImpl::HandleRenderRuntimeSettings() {
1030 RuntimeSetting setting;
1031 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +02001032 if (aec_dump_) {
1033 aec_dump_->WriteRuntimeSetting(setting);
1034 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001035 switch (setting.type()) {
1036 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1037 if (private_submodules_->render_pre_processor) {
1038 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
1039 }
1040 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001041 case RuntimeSetting::Type::kCapturePreGain: // fall-through
1042 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001043 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001044 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001045 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001046 RTC_NOTREACHED();
1047 break;
1048 }
1049 }
1050}
1051
peah9e6a2902017-05-15 07:19:21 -07001052void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001053 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001054
1055 // Insert the samples into the queue.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001056 if (private_submodules_->echo_cancellation) {
1057 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001058 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1059 num_reverse_channels(),
1060 &aec_render_queue_buffer_);
1061
Per Åhgrenf204faf2019-04-25 15:18:06 +02001062 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1063 // The data queue is full and needs to be emptied.
1064 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001065
Per Åhgrenf204faf2019-04-25 15:18:06 +02001066 // Retry the insert (should always work).
1067 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1068 RTC_DCHECK(result);
1069 }
peaha0624602016-10-25 04:45:24 -07001070 }
1071
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001072 if (private_submodules_->echo_control_mobile) {
1073 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1074 num_reverse_channels(),
1075 &aecm_render_queue_buffer_);
1076 RTC_DCHECK(aecm_render_signal_queue_);
1077 // Insert the samples into the queue.
1078 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1079 // The data queue is full and needs to be emptied.
1080 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001081
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001082 // Retry the insert (should always work).
1083 bool result =
1084 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1085 RTC_DCHECK(result);
1086 }
peah764e3642016-10-22 05:04:30 -07001087 }
peah701d6282016-10-25 05:42:20 -07001088
1089 if (!constants_.use_experimental_agc) {
1090 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1091 // Insert the samples into the queue.
1092 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1093 // The data queue is full and needs to be emptied.
1094 EmptyQueuedRenderAudio();
1095
1096 // Retry the insert (should always work).
1097 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1098 RTC_DCHECK(result);
1099 }
1100 }
peah9e6a2902017-05-15 07:19:21 -07001101}
ivoc9f4a4a02016-10-28 05:39:16 -07001102
peah9e6a2902017-05-15 07:19:21 -07001103void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001104 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1105
1106 // Insert the samples into the queue.
1107 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1108 // The data queue is full and needs to be emptied.
1109 EmptyQueuedRenderAudio();
1110
1111 // Retry the insert (should always work).
1112 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1113 RTC_DCHECK(result);
1114 }
peah764e3642016-10-22 05:04:30 -07001115}
1116
1117void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001118 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001119 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001120
ivoc9f4a4a02016-10-28 05:39:16 -07001121 const size_t new_red_render_queue_element_max_size =
1122 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1123
peaha0624602016-10-25 04:45:24 -07001124 // Reallocate the queues if the queue item sizes are too small to fit the
1125 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001126
1127 if (agc_render_queue_element_max_size_ <
1128 new_agc_render_queue_element_max_size) {
1129 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1130
1131 std::vector<int16_t> template_queue_element(
1132 agc_render_queue_element_max_size_);
1133
1134 agc_render_signal_queue_.reset(
1135 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1136 kMaxNumFramesToBuffer, template_queue_element,
1137 RenderQueueItemVerifier<int16_t>(
1138 agc_render_queue_element_max_size_)));
1139
1140 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1141 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1142 } else {
1143 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001144 }
ivoc9f4a4a02016-10-28 05:39:16 -07001145
1146 if (red_render_queue_element_max_size_ <
1147 new_red_render_queue_element_max_size) {
1148 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1149
1150 std::vector<float> template_queue_element(
1151 red_render_queue_element_max_size_);
1152
1153 red_render_signal_queue_.reset(
1154 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1155 kMaxNumFramesToBuffer, template_queue_element,
1156 RenderQueueItemVerifier<float>(
1157 red_render_queue_element_max_size_)));
1158
1159 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1160 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1161 } else {
1162 red_render_signal_queue_->Clear();
1163 }
peah764e3642016-10-22 05:04:30 -07001164}
1165
1166void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1167 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001168 if (private_submodules_->echo_cancellation) {
1169 RTC_DCHECK(aec_render_signal_queue_);
1170 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
1171 private_submodules_->echo_cancellation->ProcessRenderAudio(
1172 aec_capture_queue_buffer_);
1173 }
peaha0624602016-10-25 04:45:24 -07001174 }
1175
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001176 if (private_submodules_->echo_control_mobile) {
1177 RTC_DCHECK(aecm_render_signal_queue_);
1178 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
1179 private_submodules_->echo_control_mobile->ProcessRenderAudio(
1180 aecm_capture_queue_buffer_);
1181 }
peah701d6282016-10-25 05:42:20 -07001182 }
1183
1184 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1185 public_submodules_->gain_control->ProcessRenderAudio(
1186 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001187 }
ivoc9f4a4a02016-10-28 05:39:16 -07001188
1189 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001190 RTC_DCHECK(private_submodules_->echo_detector);
1191 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001192 red_capture_queue_buffer_);
1193 }
peah764e3642016-10-22 05:04:30 -07001194}
1195
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001196int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001197 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001198 {
1199 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001200 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001201 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001202 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001203 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001204 }
peahfa6228e2015-11-16 16:27:42 -08001205
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001206 if (!frame) {
1207 return kNullPointerError;
1208 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001209 // Must be a native rate.
1210 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1211 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001212 frame->sample_rate_hz_ != kSampleRate32kHz &&
1213 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001214 return kBadSampleRateError;
1215 }
peah192164e2015-11-17 02:16:45 -08001216
peahdf3efa82015-11-28 12:35:15 -08001217 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001218 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001219 {
1220 // Aquire lock for the access of api_format.
1221 // The lock is released immediately due to the conditional
1222 // reinitialization.
1223 rtc::CritScope cs_capture(&crit_capture_);
1224 // TODO(ajm): The input and output rates and channels are currently
1225 // constrained to be identical in the int16 interface.
1226 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001227
1228 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001229 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001230
Oskar Sundbom4b276482019-05-23 14:28:00 +02001231 reinitialization_required =
1232 reinitialization_required ||
1233 processing_config.input_stream().sample_rate_hz() !=
1234 frame->sample_rate_hz_ ||
1235 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1236 processing_config.output_stream().sample_rate_hz() !=
1237 frame->sample_rate_hz_ ||
1238 processing_config.output_stream().num_channels() != frame->num_channels_;
1239
1240 if (reinitialization_required) {
1241 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1242 processing_config.input_stream().set_num_channels(frame->num_channels_);
1243 processing_config.output_stream().set_sample_rate_hz(
1244 frame->sample_rate_hz_);
1245 processing_config.output_stream().set_num_channels(frame->num_channels_);
1246
1247 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001248 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001249 rtc::CritScope cs_capture(&crit_capture_);
1250 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001251 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001252
peahdf3efa82015-11-28 12:35:15 -08001253 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001254 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001255 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001256 return kBadDataLengthError;
1257 }
1258
aleloi868f32f2017-05-23 07:20:05 -07001259 if (aec_dump_) {
1260 RecordUnprocessedCaptureStream(*frame);
1261 }
1262
Per Åhgrend47941e2019-08-22 11:51:13 +02001263 capture_.capture_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001264 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001265 if (submodule_states_.CaptureMultiBandProcessingActive() ||
1266 submodule_states_.CaptureFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001267 capture_.capture_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001268 }
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001269 if (capture_.stats.voice_detected) {
1270 frame->vad_activity_ = *capture_.stats.voice_detected
1271 ? AudioFrame::kVadActive
1272 : AudioFrame::kVadPassive;
1273 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001274
aleloi868f32f2017-05-23 07:20:05 -07001275 if (aec_dump_) {
1276 RecordProcessedCaptureStream(*frame);
1277 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001278
1279 return kNoError;
1280}
1281
peahde65ddc2016-09-16 15:02:15 -07001282int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001283 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001284
peahb58a1582016-03-15 09:34:24 -07001285 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001286 // TODO(peah): Simplify once the public API Enable functions for these
1287 // are moved to APM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001288 RTC_DCHECK_LE(!!private_submodules_->echo_controller +
1289 !!private_submodules_->echo_cancellation +
1290 !!private_submodules_->echo_control_mobile,
1291 1);
peahb58a1582016-03-15 09:34:24 -07001292
peahde65ddc2016-09-16 15:02:15 -07001293 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001294
Alex Loikob5c9a792018-04-16 16:31:22 +02001295 if (private_submodules_->pre_amplifier) {
1296 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001297 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001298 capture_buffer->num_frames()));
1299 }
1300
Per Åhgren928146f2019-08-20 09:19:21 +02001301 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001302 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001303 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001304 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1305 if (log_rms) {
1306 capture_rms_interval_counter_ = 0;
1307 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001308 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1309 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1310 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1311 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001312 }
1313
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001314 if (private_submodules_->echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001315 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001316 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001317 capture_.echo_path_gain_change =
1318 capture_.prev_analog_mic_level != analog_mic_level &&
1319 capture_.prev_analog_mic_level != -1;
1320 capture_.prev_analog_mic_level = analog_mic_level;
1321
Per Åhgrend2650d12018-10-02 17:00:59 +02001322 // Detect and flag any change in the pre-amplifier gain.
1323 if (private_submodules_->pre_amplifier) {
1324 float pre_amp_gain = private_submodules_->pre_amplifier->GetGainFactor();
1325 capture_.echo_path_gain_change =
1326 capture_.echo_path_gain_change ||
1327 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001328 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001329 capture_.prev_pre_amp_gain = pre_amp_gain;
1330 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001331
1332 // Detect volume change.
1333 capture_.echo_path_gain_change =
1334 capture_.echo_path_gain_change ||
1335 (capture_.prev_playout_volume != capture_.playout_volume &&
1336 capture_.prev_playout_volume >= 0);
1337 capture_.prev_playout_volume = capture_.playout_volume;
1338
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001339 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001340 }
1341
peahbe615622016-02-13 16:40:47 -08001342 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001343 public_submodules_->gain_control->is_enabled()) {
1344 private_submodules_->agc_manager->AnalyzePreProcess(
Per Åhgren928146f2019-08-20 09:19:21 +02001345 capture_buffer->channels_f()[0], capture_buffer->num_channels(),
peahde65ddc2016-09-16 15:02:15 -07001346 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001347
1348 if (constants_.use_experimental_agc_process_before_aec) {
1349 private_submodules_->agc_manager->Process(
Per Åhgrend47941e2019-08-22 11:51:13 +02001350 capture_buffer->channels_const()[0],
Alex Loikod9342442018-09-10 13:59:41 +02001351 capture_nonlocked_.capture_processing_format.num_frames(),
1352 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1353 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001354 }
1355
peah2ace3f92016-09-10 04:42:27 -07001356 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1357 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001358 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1359 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001360 }
1361
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001362 const bool experimental_multi_channel_capture =
1363 config_.pipeline.experimental_multi_channel &&
1364 constants_.experimental_multi_channel_capture_support;
1365 if (private_submodules_->echo_controller &&
1366 !experimental_multi_channel_capture) {
peah522d71b2017-02-23 05:16:26 -08001367 // Force down-mixing of the number of channels after the detection of
1368 // capture signal saturation.
1369 // TODO(peah): Look into ensuring that this kind of tampering with the
1370 // AudioBuffer functionality should not be needed.
1371 capture_buffer->set_num_channels(1);
1372 }
1373
Per Åhgren0aefbf02019-08-23 21:29:17 +02001374 if (private_submodules_->high_pass_filter) {
1375 private_submodules_->high_pass_filter->Process(capture_buffer);
peah8271d042016-11-22 07:24:52 -08001376 }
peahde65ddc2016-09-16 15:02:15 -07001377 RETURN_ON_ERR(
1378 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1379 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001380
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001381 if (private_submodules_->echo_control_mobile) {
1382 // Ensure that the stream delay was set before the call to the
1383 // AECM ProcessCaptureAudio function.
1384 if (!was_stream_delay_set()) {
1385 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001386 }
1387
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001388 if (public_submodules_->noise_suppression->is_enabled()) {
Per Åhgrena1351272019-08-15 12:15:46 +02001389 private_submodules_->echo_control_mobile->CopyLowPassReference(
1390 capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001391 }
peahe0eae3c2016-12-14 01:16:23 -08001392
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001393 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah253534d2016-03-15 04:32:28 -07001394
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001395 RETURN_ON_ERR(private_submodules_->echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001396 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001397 } else {
1398 if (private_submodules_->echo_controller) {
1399 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1400
1401 if (was_stream_delay_set()) {
1402 private_submodules_->echo_controller->SetAudioBufferDelay(
1403 stream_delay_ms());
1404 }
1405
1406 private_submodules_->echo_controller->ProcessCapture(
1407 capture_buffer, capture_.echo_path_gain_change);
1408 } else if (private_submodules_->echo_cancellation) {
1409 // Ensure that the stream delay was set before the call to the
1410 // AEC ProcessCaptureAudio function.
1411 if (!was_stream_delay_set()) {
1412 return AudioProcessing::kStreamParameterNotSetError;
1413 }
1414
1415 RETURN_ON_ERR(private_submodules_->echo_cancellation->ProcessCaptureAudio(
1416 capture_buffer, stream_delay_ms()));
1417 }
1418
1419 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
Per Åhgren46537a32017-06-07 10:08:10 +02001420 }
ivoc9f4a4a02016-10-28 05:39:16 -07001421
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001422 if (config_.voice_detection.enabled) {
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001423 capture_.stats.voice_detected =
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001424 private_submodules_->voice_detector->ProcessCaptureAudio(
1425 capture_buffer);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001426 } else {
1427 capture_.stats.voice_detected = absl::nullopt;
1428 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001429
peahbe615622016-02-13 16:40:47 -08001430 if (constants_.use_experimental_agc &&
Alex Loikod9342442018-09-10 13:59:41 +02001431 public_submodules_->gain_control->is_enabled() &&
1432 !constants_.use_experimental_agc_process_before_aec) {
peahdf3efa82015-11-28 12:35:15 -08001433 private_submodules_->agc_manager->Process(
Per Åhgren928146f2019-08-20 09:19:21 +02001434 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
peahde65ddc2016-09-16 15:02:15 -07001435 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001436 }
Per Åhgren200feba2019-03-06 04:16:46 +01001437 // TODO(peah): Add reporting from AEC3 whether there is echo.
peahb8fbb542016-03-15 02:28:08 -07001438 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001439 capture_buffer,
Per Åhgrenf204faf2019-04-25 15:18:06 +02001440 private_submodules_->echo_cancellation &&
1441 private_submodules_->echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001442
peah2ace3f92016-09-10 04:42:27 -07001443 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1444 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001445 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1446 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001447 }
1448
peah9e6a2902017-05-15 07:19:21 -07001449 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001450 RTC_DCHECK(private_submodules_->echo_detector);
1451 private_submodules_->echo_detector->AnalyzeCaptureAudio(
Per Åhgrend47941e2019-08-22 11:51:13 +02001452 rtc::ArrayView<const float>(capture_buffer->channels()[0],
peah9e6a2902017-05-15 07:19:21 -07001453 capture_buffer->num_frames()));
1454 }
1455
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001456 // TODO(aluebs): Investigate if the transient suppression placement should be
1457 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001458 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001459 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001460 private_submodules_->agc_manager.get()
1461 ? private_submodules_->agc_manager->voice_probability()
1462 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001463
peahdf3efa82015-11-28 12:35:15 -08001464 public_submodules_->transient_suppressor->Suppress(
Per Åhgrend47941e2019-08-22 11:51:13 +02001465 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001466 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001467 capture_buffer->split_bands_const(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001468 capture_buffer->num_frames_per_band(),
1469 capture_.keyboard_info.keyboard_data,
1470 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001471 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001472 }
1473
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001474 // Experimental APM sub-module that analyzes |capture_buffer|.
1475 if (private_submodules_->capture_analyzer) {
1476 private_submodules_->capture_analyzer->Analyze(capture_buffer);
1477 }
1478
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001479 if (config_.gain_controller2.enabled) {
Alex Loikoa837dd72018-08-06 16:32:12 +02001480 private_submodules_->gain_controller2->NotifyAnalogLevel(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001481 agc1()->stream_analog_level());
alessiob3ec96df2017-05-22 06:57:06 -07001482 private_submodules_->gain_controller2->Process(capture_buffer);
1483 }
1484
Sam Zackrisson0beac582017-09-25 12:04:02 +02001485 if (private_submodules_->capture_post_processor) {
1486 private_submodules_->capture_post_processor->Process(capture_buffer);
1487 }
1488
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001489 // The level estimator operates on the recombined data.
Per Åhgrend47941e2019-08-22 11:51:13 +02001490 public_submodules_->level_estimator->ProcessStream(*capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001491 if (config_.level_estimation.enabled) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001492 private_submodules_->output_level_estimator->ProcessStream(*capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001493 capture_.stats.output_rms_dbfs =
1494 private_submodules_->output_level_estimator->RMS();
1495 } else {
1496 capture_.stats.output_rms_dbfs = absl::nullopt;
1497 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001498
Per Åhgren928146f2019-08-20 09:19:21 +02001499 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001500 capture_buffer->channels_const()[0],
peah1b08dc32016-12-20 13:45:58 -08001501 capture_nonlocked_.capture_processing_format.num_frames()));
1502 if (log_rms) {
1503 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1504 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1505 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1506 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1507 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1508 }
1509
peahdf3efa82015-11-28 12:35:15 -08001510 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001511 return kNoError;
1512}
1513
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001514int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001515 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001516 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001517 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001518 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001519 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001520 const StreamConfig reverse_config = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001521 sample_rate_hz,
1522 ChannelsFromLayout(layout),
1523 LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001524 };
1525 if (samples_per_channel != reverse_config.num_frames()) {
1526 return kBadDataLengthError;
1527 }
peahdf3efa82015-11-28 12:35:15 -08001528 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001529}
1530
peahde65ddc2016-09-16 15:02:15 -07001531int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1532 const StreamConfig& input_config,
1533 const StreamConfig& output_config,
1534 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001535 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001536 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001537 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001538 if (submodule_states_.RenderMultiBandProcessingActive() ||
1539 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001540 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1541 dest);
peah2ace3f92016-09-10 04:42:27 -07001542 } else if (formats_.api_format.reverse_input_stream() !=
1543 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001544 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1545 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001546 } else {
peahde65ddc2016-09-16 15:02:15 -07001547 CopyAudioIfNeeded(src, input_config.num_frames(),
1548 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001549 }
1550
1551 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001552}
1553
peahdf3efa82015-11-28 12:35:15 -08001554int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001555 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001556 const StreamConfig& input_config,
1557 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001558 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001559 return kNullPointerError;
1560 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001561
peahde65ddc2016-09-16 15:02:15 -07001562 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001563 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001564 }
1565
peahdf3efa82015-11-28 12:35:15 -08001566 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001567 processing_config.reverse_input_stream() = input_config;
1568 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001569
peahdf3efa82015-11-28 12:35:15 -08001570 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001571 RTC_DCHECK_EQ(input_config.num_frames(),
1572 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001573
aleloi868f32f2017-05-23 07:20:05 -07001574 if (aec_dump_) {
1575 const size_t channel_size =
1576 formats_.api_format.reverse_input_stream().num_frames();
1577 const size_t num_channels =
1578 formats_.api_format.reverse_input_stream().num_channels();
1579 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001580 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001581 }
peahdf3efa82015-11-28 12:35:15 -08001582 render_.render_audio->CopyFrom(src,
1583 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001584 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001585}
1586
1587int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001588 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001589 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001590 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001591 return kNullPointerError;
1592 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001593 // Must be a native rate.
1594 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1595 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001596 frame->sample_rate_hz_ != kSampleRate32kHz &&
1597 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001598 return kBadSampleRateError;
1599 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001600
Michael Graczyk86c6d332015-07-23 11:41:39 -07001601 if (frame->num_channels_ <= 0) {
1602 return kBadNumberChannelsError;
1603 }
1604
peahdf3efa82015-11-28 12:35:15 -08001605 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001606 processing_config.reverse_input_stream().set_sample_rate_hz(
1607 frame->sample_rate_hz_);
1608 processing_config.reverse_input_stream().set_num_channels(
1609 frame->num_channels_);
1610 processing_config.reverse_output_stream().set_sample_rate_hz(
1611 frame->sample_rate_hz_);
1612 processing_config.reverse_output_stream().set_num_channels(
1613 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001614
peahdf3efa82015-11-28 12:35:15 -08001615 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001616 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001617 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001618 return kBadDataLengthError;
1619 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001620
aleloi868f32f2017-05-23 07:20:05 -07001621 if (aec_dump_) {
1622 aec_dump_->WriteRenderStreamMessage(*frame);
1623 }
1624
Per Åhgrend47941e2019-08-22 11:51:13 +02001625 render_.render_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001626 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001627 if (submodule_states_.RenderMultiBandProcessingActive() ||
1628 submodule_states_.RenderFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001629 render_.render_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001630 }
aluebsb0319552016-03-17 20:39:53 -07001631 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001632}
niklase@google.com470e71d2011-07-07 08:21:25 +00001633
peahde65ddc2016-09-16 15:02:15 -07001634int AudioProcessingImpl::ProcessRenderStreamLocked() {
1635 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001636
Alex Loiko73ec0192018-05-15 10:52:28 +02001637 HandleRenderRuntimeSettings();
1638
Alex Loiko5825aa62017-12-18 16:02:40 +01001639 if (private_submodules_->render_pre_processor) {
1640 private_submodules_->render_pre_processor->Process(render_buffer);
1641 }
1642
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001643 QueueNonbandedRenderAudio(render_buffer);
1644
peah2ace3f92016-09-10 04:42:27 -07001645 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001646 SampleRateSupportsMultiBand(
1647 formats_.render_processing_format.sample_rate_hz())) {
1648 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001649 }
1650
peahce4d9152017-05-19 01:28:05 -07001651 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1652 QueueBandedRenderAudio(render_buffer);
1653 }
1654
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001655 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001656 if (private_submodules_->echo_controller) {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001657 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001658 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001659
peah2ace3f92016-09-10 04:42:27 -07001660 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001661 SampleRateSupportsMultiBand(
1662 formats_.render_processing_format.sample_rate_hz())) {
1663 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001664 }
1665
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001666 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001667}
1668
1669int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001670 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001671 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001672 capture_.was_stream_delay_set = true;
1673 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001674
niklase@google.com470e71d2011-07-07 08:21:25 +00001675 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001676 delay = 0;
1677 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001678 }
1679
1680 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1681 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001682 delay = 500;
1683 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001684 }
1685
peahdf3efa82015-11-28 12:35:15 -08001686 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001687 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001688}
1689
1690int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001691 // Used as callback from submodules, hence locking is not allowed.
1692 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001693}
1694
1695bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001696 // Used as callback from submodules, hence locking is not allowed.
1697 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001698}
1699
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001700void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001701 rtc::CritScope cs(&crit_capture_);
1702 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001703}
1704
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001705void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001706 rtc::CritScope cs(&crit_capture_);
1707 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001708}
1709
1710int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001711 rtc::CritScope cs(&crit_capture_);
1712 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001713}
1714
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001715void AudioProcessingImpl::set_stream_analog_level(int level) {
1716 rtc::CritScope cs_capture(&crit_capture_);
1717 int error = agc1()->set_stream_analog_level(level);
1718 RTC_DCHECK_EQ(kNoError, error);
1719}
1720
1721int AudioProcessingImpl::recommended_stream_analog_level() const {
1722 rtc::CritScope cs_capture(&crit_capture_);
1723 return agc1()->stream_analog_level();
1724}
1725
aleloi868f32f2017-05-23 07:20:05 -07001726void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1727 RTC_DCHECK(aec_dump);
1728 rtc::CritScope cs_render(&crit_render_);
1729 rtc::CritScope cs_capture(&crit_capture_);
1730
1731 // The previously attached AecDump will be destroyed with the
1732 // 'aec_dump' parameter, which is after locks are released.
1733 aec_dump_.swap(aec_dump);
1734 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001735 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001736}
1737
1738void AudioProcessingImpl::DetachAecDump() {
1739 // The d-tor of a task-queue based AecDump blocks until all pending
1740 // tasks are done. This construction avoids blocking while holding
1741 // the render and capture locks.
1742 std::unique_ptr<AecDump> aec_dump = nullptr;
1743 {
1744 rtc::CritScope cs_render(&crit_render_);
1745 rtc::CritScope cs_capture(&crit_capture_);
1746 aec_dump = std::move(aec_dump_);
1747 }
1748}
1749
Sam Zackrisson4d364492018-03-02 16:03:21 +01001750void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1751 std::unique_ptr<AudioGenerator> audio_generator) {
1752 // TODO(bugs.webrtc.org/8882) Stub.
1753 // Reset internal audio generator with audio_generator.
1754}
1755
1756void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1757 // TODO(bugs.webrtc.org/8882) Stub.
1758 // Delete audio generator, if one is attached.
1759}
1760
Ivo Creusen56d46092017-11-24 17:29:59 +01001761AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001762 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001763 rtc::CritScope cs_capture(&crit_capture_);
1764 if (!has_remote_tracks) {
1765 return capture_.stats;
1766 }
1767 AudioProcessingStats stats = capture_.stats;
1768 EchoCancellationImpl::Metrics metrics;
1769 if (private_submodules_->echo_controller) {
1770 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1771 stats.echo_return_loss = ec_metrics.echo_return_loss;
1772 stats.echo_return_loss_enhancement =
1773 ec_metrics.echo_return_loss_enhancement;
1774 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001775 }
1776 if (config_.residual_echo_detector.enabled) {
1777 RTC_DCHECK(private_submodules_->echo_detector);
1778 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1779 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1780 stats.residual_echo_likelihood_recent_max =
1781 ed_metrics.echo_likelihood_recent_max;
1782 }
Ivo Creusenae026092017-11-20 13:07:16 +01001783 return stats;
1784}
1785
niklase@google.com470e71d2011-07-07 08:21:25 +00001786GainControl* AudioProcessingImpl::gain_control() const {
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001787 return public_submodules_->gain_control_config_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001788}
1789
niklase@google.com470e71d2011-07-07 08:21:25 +00001790LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001791 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001792}
1793
1794NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
Sam Zackrisson23513132019-01-11 15:10:32 +01001795 return public_submodules_->noise_suppression_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001796}
1797
peah8271d042016-11-22 07:24:52 -08001798void AudioProcessingImpl::MutateConfig(
1799 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1800 rtc::CritScope cs_render(&crit_render_);
1801 rtc::CritScope cs_capture(&crit_capture_);
1802 mutator(&config_);
1803 ApplyConfig(config_);
1804}
1805
1806AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1807 rtc::CritScope cs_render(&crit_render_);
1808 rtc::CritScope cs_capture(&crit_capture_);
1809 return config_;
1810}
1811
peah2ace3f92016-09-10 04:42:27 -07001812bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1813 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001814 config_.high_pass_filter.enabled,
Per Åhgrend547d862019-05-03 15:48:47 +02001815 !!private_submodules_->echo_cancellation,
1816 !!private_submodules_->echo_control_mobile,
ivoc9f4a4a02016-10-28 05:39:16 -07001817 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001818 public_submodules_->noise_suppression->is_enabled(),
peah2ace3f92016-09-10 04:42:27 -07001819 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001820 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001821 capture_nonlocked_.echo_controller_enabled,
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001822 config_.voice_detection.enabled,
peah2ace3f92016-09-10 04:42:27 -07001823 public_submodules_->level_estimator->is_enabled(),
1824 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001825}
1826
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001827void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001828 if (capture_.transient_suppressor_enabled) {
1829 if (!public_submodules_->transient_suppressor.get()) {
1830 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001831 }
peahdf3efa82015-11-28 12:35:15 -08001832 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001833 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1834 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001835 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001836}
1837
Per Åhgren0aefbf02019-08-23 21:29:17 +02001838void AudioProcessingImpl::InitializeHighPassFilter() {
1839 if (submodule_states_.HighPassFilteringRequired()) {
1840 private_submodules_->high_pass_filter.reset(
1841 new HighPassFilter(num_proc_channels()));
peah8271d042016-11-22 07:24:52 -08001842 } else {
Per Åhgren0aefbf02019-08-23 21:29:17 +02001843 private_submodules_->high_pass_filter.reset();
peah8271d042016-11-22 07:24:52 -08001844 }
1845}
alessiob3ec96df2017-05-22 06:57:06 -07001846
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001847void AudioProcessingImpl::InitializeVoiceDetector() {
1848 if (config_.voice_detection.enabled) {
1849 private_submodules_->voice_detector = std::make_unique<VoiceDetection>(
1850 proc_split_sample_rate_hz(), VoiceDetection::kVeryLowLikelihood);
1851 } else {
1852 private_submodules_->voice_detector.reset();
1853 }
1854}
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001855void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001856 bool use_echo_controller =
1857 echo_control_factory_ ||
Per Åhgren200feba2019-03-06 04:16:46 +01001858 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode &&
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001859 !config_.echo_canceller.use_legacy_aec);
1860
1861 if (use_echo_controller) {
1862 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001863 if (echo_control_factory_) {
1864 private_submodules_->echo_controller =
1865 echo_control_factory_->Create(proc_sample_rate_hz());
1866 } else {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001867 private_submodules_->echo_controller = std::make_unique<EchoCanceller3>(
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001868 EchoCanceller3Config(), proc_sample_rate_hz(), num_reverse_channels(),
1869 num_proc_channels());
Per Åhgren200feba2019-03-06 04:16:46 +01001870 }
1871
1872 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001873
Per Åhgrenf204faf2019-04-25 15:18:06 +02001874 private_submodules_->echo_cancellation.reset();
1875 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001876 private_submodules_->echo_control_mobile.reset();
1877 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001878 return;
peahe0eae3c2016-12-14 01:16:23 -08001879 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001880
1881 private_submodules_->echo_controller.reset();
1882 capture_nonlocked_.echo_controller_enabled = false;
1883
1884 if (!config_.echo_canceller.enabled) {
1885 private_submodules_->echo_cancellation.reset();
1886 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001887 private_submodules_->echo_control_mobile.reset();
1888 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001889 return;
1890 }
1891
1892 if (config_.echo_canceller.mobile_mode) {
1893 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001894 size_t max_element_size =
1895 std::max(static_cast<size_t>(1),
1896 kMaxAllowedValuesOfSamplesPerBand *
1897 EchoControlMobileImpl::NumCancellersRequired(
1898 num_output_channels(), num_reverse_channels()));
1899
1900 std::vector<int16_t> template_queue_element(max_element_size);
1901
1902 aecm_render_signal_queue_.reset(
1903 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1904 kMaxNumFramesToBuffer, template_queue_element,
1905 RenderQueueItemVerifier<int16_t>(max_element_size)));
1906
1907 aecm_render_queue_buffer_.resize(max_element_size);
1908 aecm_capture_queue_buffer_.resize(max_element_size);
1909
1910 private_submodules_->echo_control_mobile.reset(new EchoControlMobileImpl());
1911
1912 private_submodules_->echo_control_mobile->Initialize(
1913 proc_split_sample_rate_hz(), num_reverse_channels(),
1914 num_output_channels());
1915
Per Åhgrenf204faf2019-04-25 15:18:06 +02001916 private_submodules_->echo_cancellation.reset();
1917 aec_render_signal_queue_.reset();
1918 return;
1919 }
1920
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001921 private_submodules_->echo_control_mobile.reset();
1922 aecm_render_signal_queue_.reset();
1923
Per Åhgrenf204faf2019-04-25 15:18:06 +02001924 // Create and activate AEC2.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001925 private_submodules_->echo_cancellation.reset(new EchoCancellationImpl());
1926 private_submodules_->echo_cancellation->SetExtraOptions(
1927 capture_nonlocked_.use_aec2_extended_filter,
1928 capture_nonlocked_.use_aec2_delay_agnostic,
1929 capture_nonlocked_.use_aec2_refined_adaptive_filter);
1930
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001931 size_t element_max_size =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001932 std::max(static_cast<size_t>(1),
1933 kMaxAllowedValuesOfSamplesPerBand *
1934 EchoCancellationImpl::NumCancellersRequired(
1935 num_output_channels(), num_reverse_channels()));
1936
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001937 std::vector<float> template_queue_element(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001938
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001939 aec_render_signal_queue_.reset(
1940 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1941 kMaxNumFramesToBuffer, template_queue_element,
1942 RenderQueueItemVerifier<float>(element_max_size)));
Per Åhgrenf204faf2019-04-25 15:18:06 +02001943
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001944 aec_render_queue_buffer_.resize(element_max_size);
1945 aec_capture_queue_buffer_.resize(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001946
1947 private_submodules_->echo_cancellation->Initialize(
1948 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
1949 num_proc_channels());
1950
Per Åhgrenf204faf2019-04-25 15:18:06 +02001951 private_submodules_->echo_cancellation->set_suppression_level(
1952 config_.echo_canceller.legacy_moderate_suppression_level
1953 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
1954 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
peahe0eae3c2016-12-14 01:16:23 -08001955}
peah8271d042016-11-22 07:24:52 -08001956
alessiob3ec96df2017-05-22 06:57:06 -07001957void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001958 if (config_.gain_controller2.enabled) {
1959 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001960 }
1961}
1962
Alex Loikob5c9a792018-04-16 16:31:22 +02001963void AudioProcessingImpl::InitializePreAmplifier() {
1964 if (config_.pre_amplifier.enabled) {
1965 private_submodules_->pre_amplifier.reset(
1966 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1967 } else {
1968 private_submodules_->pre_amplifier.reset();
1969 }
1970}
1971
ivoc9f4a4a02016-10-28 05:39:16 -07001972void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001973 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001974 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001975 proc_sample_rate_hz(), 1,
1976 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001977}
1978
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001979void AudioProcessingImpl::InitializeAnalyzer() {
1980 if (private_submodules_->capture_analyzer) {
1981 private_submodules_->capture_analyzer->Initialize(proc_sample_rate_hz(),
1982 num_proc_channels());
1983 }
1984}
1985
Sam Zackrisson0beac582017-09-25 12:04:02 +02001986void AudioProcessingImpl::InitializePostProcessor() {
1987 if (private_submodules_->capture_post_processor) {
1988 private_submodules_->capture_post_processor->Initialize(
1989 proc_sample_rate_hz(), num_proc_channels());
1990 }
1991}
1992
Alex Loiko5825aa62017-12-18 16:02:40 +01001993void AudioProcessingImpl::InitializePreProcessor() {
1994 if (private_submodules_->render_pre_processor) {
1995 private_submodules_->render_pre_processor->Initialize(
1996 formats_.render_processing_format.sample_rate_hz(),
1997 formats_.render_processing_format.num_channels());
1998 }
1999}
2000
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002001void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02002002
aleloi868f32f2017-05-23 07:20:05 -07002003void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
2004 if (!aec_dump_) {
2005 return;
2006 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02002007
2008 std::string experiments_description = "";
2009 if (private_submodules_->echo_cancellation) {
2010 experiments_description +=
2011 private_submodules_->echo_cancellation->GetExperimentsDescription();
2012 }
aleloi868f32f2017-05-23 07:20:05 -07002013 // TODO(peah): Add semicolon-separated concatenations of experiment
2014 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07002015 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
2016 experiments_description += "AgcClippingLevelExperiment;";
2017 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02002018 if (capture_nonlocked_.echo_controller_enabled) {
2019 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07002020 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02002021 if (config_.gain_controller2.enabled) {
2022 experiments_description += "GainController2;";
2023 }
aleloi868f32f2017-05-23 07:20:05 -07002024
2025 InternalAPMConfig apm_config;
2026
Per Åhgren200feba2019-03-06 04:16:46 +01002027 apm_config.aec_enabled = config_.echo_canceller.enabled;
aleloi868f32f2017-05-23 07:20:05 -07002028 apm_config.aec_delay_agnostic_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002029 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002030 private_submodules_->echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002031 apm_config.aec_drift_compensation_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002032 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002033 private_submodules_->echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002034 apm_config.aec_extended_filter_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002035 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002036 private_submodules_->echo_cancellation->is_extended_filter_enabled();
Per Åhgrenf204faf2019-04-25 15:18:06 +02002037 apm_config.aec_suppression_level =
2038 private_submodules_->echo_cancellation
2039 ? static_cast<int>(
2040 private_submodules_->echo_cancellation->suppression_level())
2041 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002042
Per Åhgrend547d862019-05-03 15:48:47 +02002043 apm_config.aecm_enabled = !!private_submodules_->echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002044 apm_config.aecm_comfort_noise_enabled =
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002045 private_submodules_->echo_control_mobile &&
Sam Zackrissonc22f5512018-11-05 16:10:00 +01002046 private_submodules_->echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002047 apm_config.aecm_routing_mode =
2048 private_submodules_->echo_control_mobile
2049 ? static_cast<int>(
2050 private_submodules_->echo_control_mobile->routing_mode())
2051 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002052
2053 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2054 apm_config.agc_mode =
2055 static_cast<int>(public_submodules_->gain_control->mode());
2056 apm_config.agc_limiter_enabled =
2057 public_submodules_->gain_control->is_limiter_enabled();
2058 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2059
2060 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2061
2062 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2063 apm_config.ns_level =
2064 static_cast<int>(public_submodules_->noise_suppression->level());
2065
2066 apm_config.transient_suppression_enabled =
2067 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002068 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002069 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2070 apm_config.pre_amplifier_fixed_gain_factor =
2071 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002072
2073 if (!forced && apm_config == apm_config_for_aec_dump_) {
2074 return;
2075 }
2076 aec_dump_->WriteConfig(apm_config);
2077 apm_config_for_aec_dump_ = apm_config;
2078}
2079
2080void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2081 const float* const* src) {
2082 RTC_DCHECK(aec_dump_);
2083 WriteAecDumpConfigMessage(false);
2084
2085 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2086 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2087 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002088 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002089 RecordAudioProcessingState();
2090}
2091
2092void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2093 const AudioFrame& capture_frame) {
2094 RTC_DCHECK(aec_dump_);
2095 WriteAecDumpConfigMessage(false);
2096
2097 aec_dump_->AddCaptureStreamInput(capture_frame);
2098 RecordAudioProcessingState();
2099}
2100
2101void AudioProcessingImpl::RecordProcessedCaptureStream(
2102 const float* const* processed_capture_stream) {
2103 RTC_DCHECK(aec_dump_);
2104
2105 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2106 const size_t num_channels =
2107 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002108 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2109 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002110 aec_dump_->WriteCaptureStreamMessage();
2111}
2112
2113void AudioProcessingImpl::RecordProcessedCaptureStream(
2114 const AudioFrame& processed_capture_frame) {
2115 RTC_DCHECK(aec_dump_);
2116
2117 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2118 aec_dump_->WriteCaptureStreamMessage();
2119}
2120
2121void AudioProcessingImpl::RecordAudioProcessingState() {
2122 RTC_DCHECK(aec_dump_);
2123 AecDump::AudioProcessingState audio_proc_state;
2124 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2125 audio_proc_state.drift =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002126 private_submodules_->echo_cancellation
2127 ? private_submodules_->echo_cancellation->stream_drift_samples()
2128 : 0;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01002129 audio_proc_state.level = agc1()->stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002130 audio_proc_state.keypress = capture_.key_pressed;
2131 aec_dump_->AddAudioProcessingState(audio_proc_state);
2132}
2133
kwiberg83ffe452016-08-29 14:46:07 -07002134AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002135 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002136 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002137 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002138 output_will_be_muted(false),
2139 key_pressed(false),
2140 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002141 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002142 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002143 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002144 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002145 prev_pre_amp_gain(-1.f),
2146 playout_volume(-1),
2147 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002148
2149AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2150
Per Åhgrena1351272019-08-15 12:15:46 +02002151void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2152 const float* const* data,
2153 const StreamConfig& stream_config) {
2154 if (stream_config.has_keyboard()) {
2155 keyboard_data = data[stream_config.num_channels()];
2156 } else {
2157 keyboard_data = NULL;
2158 }
2159 num_keyboard_frames = stream_config.num_frames();
2160}
2161
kwiberg83ffe452016-08-29 14:46:07 -07002162AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2163
2164AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2165
niklase@google.com470e71d2011-07-07 08:21:25 +00002166} // namespace webrtc