blob: 39de770e488fbc922f14e9140a05721d5cdf590e [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"
31#include "modules/audio_processing/gain_control_for_experimental_agc.h"
32#include "modules/audio_processing/gain_control_impl.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010033#include "modules/audio_processing/gain_controller2.h"
Per Åhgren0aefbf02019-08-23 21:29:17 +020034#include "modules/audio_processing/high_pass_filter.h"
Yves Gerey988cc082018-10-23 12:03:01 +020035#include "modules/audio_processing/include/audio_frame_view.h"
saza6787f232019-10-11 19:31:07 +020036#include "modules/audio_processing/level_estimator.h"
Per Åhgren13735822018-02-12 21:42:56 +010037#include "modules/audio_processing/logging/apm_data_dumper.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020038#include "modules/audio_processing/noise_suppression_impl.h"
Sam Zackrisson23513132019-01-11 15:10:32 +010039#include "modules/audio_processing/noise_suppression_proxy.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020040#include "modules/audio_processing/residual_echo_detector.h"
41#include "modules/audio_processing/transient/transient_suppressor.h"
Sam Zackrisson0824c6f2019-10-07 14:03:56 +020042#include "modules/audio_processing/voice_detection.h"
Steve Anton10542f22019-01-11 09:11:00 -080043#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080045#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080047#include "rtc_base/ref_counted_object.h"
48#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020049#include "rtc_base/trace_event.h"
Sam Zackrissonfeee1e42019-09-20 07:50:35 +020050#include "system_wrappers/include/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000052
Michael Graczyk86c6d332015-07-23 11:41:39 -070053#define RETURN_ON_ERR(expr) \
54 do { \
55 int err = (expr); \
56 if (err != kNoError) { \
57 return err; \
58 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000059 } while (0)
60
niklase@google.com470e71d2011-07-07 08:21:25 +000061namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070062
kwibergd59d3bb2016-09-13 07:49:33 -070063constexpr int AudioProcessing::kNativeSampleRatesHz[];
Alex Loiko73ec0192018-05-15 10:52:28 +020064constexpr int kRuntimeSettingQueueSize = 100;
aluebsdf6416a2016-03-16 18:26:35 -070065
Michael Graczyk86c6d332015-07-23 11:41:39 -070066namespace {
67
68static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
69 switch (layout) {
70 case AudioProcessing::kMono:
71 case AudioProcessing::kStereo:
72 return false;
73 case AudioProcessing::kMonoAndKeyboard:
74 case AudioProcessing::kStereoAndKeyboard:
75 return true;
76 }
77
kwiberg9e2be5f2016-09-14 05:23:22 -070078 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070079 return false;
80}
aluebsdf6416a2016-03-16 18:26:35 -070081
peah2ace3f92016-09-10 04:42:27 -070082bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070083 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
84 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
85}
86
Per Åhgrenc8626b62019-08-23 15:49:51 +020087// Identify the native processing rate that best handles a sample rate.
Per Åhgrenfcbe4072019-09-15 00:27:58 +020088int SuitableProcessRate(int minimum_rate,
89 int max_splitting_rate,
90 bool band_splitting_required) {
Per Åhgrenc8626b62019-08-23 15:49:51 +020091 const int uppermost_native_rate =
Per Åhgrenfcbe4072019-09-15 00:27:58 +020092 band_splitting_required ? max_splitting_rate : 48000;
Per Åhgrenc8626b62019-08-23 15:49:51 +020093 for (auto rate : {16000, 32000, 48000}) {
peah2ace3f92016-09-10 04:42:27 -070094 if (rate >= uppermost_native_rate) {
95 return uppermost_native_rate;
96 }
97 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -070098 return rate;
99 }
100 }
peah2ace3f92016-09-10 04:42:27 -0700101 RTC_NOTREACHED();
102 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700103}
104
Sam Zackrisson23513132019-01-11 15:10:32 +0100105NoiseSuppression::Level NsConfigLevelToInterfaceLevel(
106 AudioProcessing::Config::NoiseSuppression::Level level) {
107 using NsConfig = AudioProcessing::Config::NoiseSuppression;
108 switch (level) {
109 case NsConfig::kLow:
110 return NoiseSuppression::kLow;
111 case NsConfig::kModerate:
112 return NoiseSuppression::kModerate;
113 case NsConfig::kHigh:
114 return NoiseSuppression::kHigh;
115 case NsConfig::kVeryHigh:
116 return NoiseSuppression::kVeryHigh;
117 default:
118 RTC_NOTREACHED();
119 }
120}
121
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100122GainControl::Mode Agc1ConfigModeToInterfaceMode(
123 AudioProcessing::Config::GainController1::Mode mode) {
124 using Agc1Config = AudioProcessing::Config::GainController1;
125 switch (mode) {
126 case Agc1Config::kAdaptiveAnalog:
127 return GainControl::kAdaptiveAnalog;
128 case Agc1Config::kAdaptiveDigital:
129 return GainControl::kAdaptiveDigital;
130 case Agc1Config::kFixedDigital:
131 return GainControl::kFixedDigital;
132 }
133}
134
peah9e6a2902017-05-15 07:19:21 -0700135// Maximum lengths that frame of samples being passed from the render side to
136// the capture side can have (does not apply to AEC3).
137static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
138static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
139
peah764e3642016-10-22 05:04:30 -0700140// Maximum number of frames to buffer in the render queue.
141// TODO(peah): Decrease this once we properly handle hugely unbalanced
142// reverse and forward call numbers.
143static const size_t kMaxNumFramesToBuffer = 100;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700144} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000145
146// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000147static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000148
Sam Zackrisson0beac582017-09-25 12:04:02 +0200149AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100150 bool capture_post_processor_enabled,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200151 bool render_pre_processor_enabled,
152 bool capture_analyzer_enabled)
Alex Loiko5825aa62017-12-18 16:02:40 +0100153 : capture_post_processor_enabled_(capture_post_processor_enabled),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200154 render_pre_processor_enabled_(render_pre_processor_enabled),
155 capture_analyzer_enabled_(capture_analyzer_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700156
157bool AudioProcessingImpl::ApmSubmoduleStates::Update(
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200158 bool high_pass_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700159 bool echo_canceller_enabled,
160 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700161 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700162 bool noise_suppressor_enabled,
peah2ace3f92016-09-10 04:42:27 -0700163 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700164 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200165 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200166 bool echo_controller_enabled,
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200167 bool voice_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700168 bool transient_suppressor_enabled) {
169 bool changed = false;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200170 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700171 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
172 changed |=
173 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700174 changed |=
175 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700176 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
177 changed |=
peah2ace3f92016-09-10 04:42:27 -0700178 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200179 changed |= (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200180 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200181 changed |= (echo_controller_enabled != echo_controller_enabled_);
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200182 changed |= (voice_detector_enabled != voice_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700183 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
184 if (changed) {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200185 high_pass_filter_enabled_ = high_pass_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700186 echo_canceller_enabled_ = echo_canceller_enabled;
187 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700188 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700189 noise_suppressor_enabled_ = noise_suppressor_enabled;
peah2ace3f92016-09-10 04:42:27 -0700190 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700191 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200192 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200193 echo_controller_enabled_ = echo_controller_enabled;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200194 voice_detector_enabled_ = voice_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700195 transient_suppressor_enabled_ = transient_suppressor_enabled;
196 }
197
198 changed |= first_update_;
199 first_update_ = false;
200 return changed;
201}
202
203bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
204 const {
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200205 return CaptureMultiBandProcessingPresent() || voice_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700206}
207
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200208bool AudioProcessingImpl::ApmSubmoduleStates::
209 CaptureMultiBandProcessingPresent() const {
210 // If echo controller is present, assume it performs active processing.
211 return CaptureMultiBandProcessingActive(/*ec_processing_active=*/true);
212}
213
214bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive(
215 bool ec_processing_active) const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200216 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700217 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200218 adaptive_gain_controller_enabled_ ||
219 (echo_controller_enabled_ && ec_processing_active);
peah2ace3f92016-09-10 04:42:27 -0700220}
221
peah23ac8b42017-05-23 05:33:56 -0700222bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
223 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200224 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
225 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700226}
227
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200228bool AudioProcessingImpl::ApmSubmoduleStates::CaptureAnalyzerActive() const {
229 return capture_analyzer_enabled_;
230}
231
peah2ace3f92016-09-10 04:42:27 -0700232bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
233 const {
234 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800235 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200236 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700237}
238
Alex Loiko5825aa62017-12-18 16:02:40 +0100239bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive()
240 const {
241 return render_pre_processor_enabled_;
242}
243
peah2ace3f92016-09-10 04:42:27 -0700244bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
245 const {
peah2ace3f92016-09-10 04:42:27 -0700246 return false;
peah2ace3f92016-09-10 04:42:27 -0700247}
248
Per Åhgren0aefbf02019-08-23 21:29:17 +0200249bool AudioProcessingImpl::ApmSubmoduleStates::HighPassFilteringRequired()
250 const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200251 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
252 mobile_echo_controller_enabled_ || noise_suppressor_enabled_;
253}
254
solenberg5e465c32015-12-08 13:22:33 -0800255struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800256 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800257 // Accessed externally of APM without any lock acquired.
Sam Zackrisson23513132019-01-11 15:10:32 +0100258 // TODO(bugs.webrtc.org/9947): Move these submodules into private_submodules_
259 // when their pointer-to-submodule API functions are gone.
kwiberg88788ad2016-02-19 07:04:49 -0800260 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
Sam Zackrisson23513132019-01-11 15:10:32 +0100261 std::unique_ptr<NoiseSuppressionProxy> noise_suppression_proxy;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100262 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800263 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800264 gain_control_for_experimental_agc;
solenberg5e465c32015-12-08 13:22:33 -0800265
266 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800267 std::unique_ptr<TransientSuppressor> transient_suppressor;
solenberg5e465c32015-12-08 13:22:33 -0800268};
269
270struct AudioProcessingImpl::ApmPrivateSubmodules {
Sam Zackrissondb389722018-06-21 10:12:24 +0200271 ApmPrivateSubmodules(std::unique_ptr<CustomProcessing> capture_post_processor,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100272 std::unique_ptr<CustomProcessing> render_pre_processor,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200273 rtc::scoped_refptr<EchoDetector> echo_detector,
274 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Sam Zackrissondb389722018-06-21 10:12:24 +0200275 : echo_detector(std::move(echo_detector)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100276 capture_post_processor(std::move(capture_post_processor)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200277 render_pre_processor(std::move(render_pre_processor)),
278 capture_analyzer(std::move(capture_analyzer)) {}
solenberg5e465c32015-12-08 13:22:33 -0800279 // Accessed internally from capture or during initialization
kwiberg88788ad2016-02-19 07:04:49 -0800280 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700281 std::unique_ptr<GainController2> gain_controller2;
Per Åhgren0aefbf02019-08-23 21:29:17 +0200282 std::unique_ptr<HighPassFilter> high_pass_filter;
Ivo Creusend1f970d2018-06-14 11:02:03 +0200283 rtc::scoped_refptr<EchoDetector> echo_detector;
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100284 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
Sam Zackrissonc22f5512018-11-05 16:10:00 +0100285 std::unique_ptr<EchoControl> echo_controller;
286 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
Alex Loiko5825aa62017-12-18 16:02:40 +0100287 std::unique_ptr<CustomProcessing> capture_post_processor;
288 std::unique_ptr<CustomProcessing> render_pre_processor;
Alex Loikob5c9a792018-04-16 16:31:22 +0200289 std::unique_ptr<GainApplier> pre_amplifier;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200290 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer;
saza6787f232019-10-11 19:31:07 +0200291 std::unique_ptr<LevelEstimator> output_level_estimator;
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200292 std::unique_ptr<VoiceDetection> voice_detector;
solenberg5e465c32015-12-08 13:22:33 -0800293};
294
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100295AudioProcessingBuilder::AudioProcessingBuilder() = default;
296AudioProcessingBuilder::~AudioProcessingBuilder() = default;
297
298AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
299 std::unique_ptr<CustomProcessing> capture_post_processing) {
300 capture_post_processing_ = std::move(capture_post_processing);
301 return *this;
302}
303
304AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
305 std::unique_ptr<CustomProcessing> render_pre_processing) {
306 render_pre_processing_ = std::move(render_pre_processing);
307 return *this;
308}
309
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200310AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
311 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
312 capture_analyzer_ = std::move(capture_analyzer);
313 return *this;
314}
315
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100316AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
317 std::unique_ptr<EchoControlFactory> echo_control_factory) {
318 echo_control_factory_ = std::move(echo_control_factory);
319 return *this;
320}
321
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100322AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200323 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100324 echo_detector_ = std::move(echo_detector);
325 return *this;
326}
327
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100328AudioProcessing* AudioProcessingBuilder::Create() {
329 webrtc::Config config;
330 return Create(config);
331}
332
333AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100334 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
335 config, std::move(capture_post_processing_),
336 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200337 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100338 if (apm->Initialize() != AudioProcessing::kNoError) {
339 delete apm;
340 apm = nullptr;
341 }
342 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100343}
344
peah88ac8532016-09-12 16:47:25 -0700345AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200346 : AudioProcessingImpl(config,
347 /*capture_post_processor=*/nullptr,
348 /*render_pre_processor=*/nullptr,
349 /*echo_control_factory=*/nullptr,
350 /*echo_detector=*/nullptr,
351 /*capture_analyzer=*/nullptr) {}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000352
Per Åhgren13735822018-02-12 21:42:56 +0100353int AudioProcessingImpl::instance_count_ = 0;
354
Sam Zackrisson0beac582017-09-25 12:04:02 +0200355AudioProcessingImpl::AudioProcessingImpl(
356 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100357 std::unique_ptr<CustomProcessing> capture_post_processor,
358 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200359 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200360 rtc::scoped_refptr<EchoDetector> echo_detector,
361 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100362 : data_dumper_(
363 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200364 capture_runtime_settings_(kRuntimeSettingQueueSize),
365 render_runtime_settings_(kRuntimeSettingQueueSize),
366 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
367 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200368 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200369 submodule_states_(!!capture_post_processor,
370 !!render_pre_processor,
371 !!capture_analyzer),
peah8271d042016-11-22 07:24:52 -0800372 public_submodules_(new ApmPublicSubmodules()),
Sam Zackrisson0beac582017-09-25 12:04:02 +0200373 private_submodules_(
Sam Zackrissondb389722018-06-21 10:12:24 +0200374 new ApmPrivateSubmodules(std::move(capture_post_processor),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100375 std::move(render_pre_processor),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200376 std::move(echo_detector),
377 std::move(capture_analyzer))),
peahdf3efa82015-11-28 12:35:15 -0800378 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800379 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000380#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alex Loikod9342442018-09-10 13:59:41 +0200381 /* enabled= */ false,
382 /* enabled_agc2_level_estimator= */ false,
383 /* digital_adaptive_disabled= */ false,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200384 /* analyze_before_aec= */ false,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000385#else
Alex Loiko64cb83b2018-07-02 13:38:19 +0200386 config.Get<ExperimentalAgc>().enabled,
387 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
Alex Loikod9342442018-09-10 13:59:41 +0200388 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200389 config.Get<ExperimentalAgc>().analyze_before_aec,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000390#endif
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200391 !field_trial::IsEnabled(
392 "WebRTC-ApmExperimentalMultiChannelRenderKillSwitch"),
393 !field_trial::IsEnabled(
394 "WebRTC-ApmExperimentalMultiChannelCaptureKillSwitch")),
andrew1c7075f2015-06-24 18:14:14 -0700395#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200396 capture_(false),
andrew1c7075f2015-06-24 18:14:14 -0700397#else
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200398 capture_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700399#endif
Alessio Bazzicacc22f512018-08-30 13:01:34 +0200400 capture_nonlocked_() {
Sam Zackrisson421c8592019-02-11 13:39:46 +0100401 // Mark Echo Controller enabled if a factory is injected.
402 capture_nonlocked_.echo_controller_enabled =
403 static_cast<bool>(echo_control_factory_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000404
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100405 public_submodules_->gain_control.reset(new GainControlImpl());
Sam Zackrisson421c8592019-02-11 13:39:46 +0100406 public_submodules_->noise_suppression.reset(
407 new NoiseSuppressionImpl(&crit_capture_));
408 public_submodules_->noise_suppression_proxy.reset(new NoiseSuppressionProxy(
409 this, public_submodules_->noise_suppression.get()));
Sam Zackrisson421c8592019-02-11 13:39:46 +0100410 public_submodules_->gain_control_for_experimental_agc.reset(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100411 new GainControlForExperimentalAgc(
412 public_submodules_->gain_control.get()));
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200413
Sam Zackrisson421c8592019-02-11 13:39:46 +0100414 // If no echo detector is injected, use the ResidualEchoDetector.
415 if (!private_submodules_->echo_detector) {
416 private_submodules_->echo_detector =
417 new rtc::RefCountedObject<ResidualEchoDetector>();
peahdf3efa82015-11-28 12:35:15 -0800418 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000419
Sam Zackrisson421c8592019-02-11 13:39:46 +0100420 // TODO(alessiob): Move the injected gain controller once injection is
421 // implemented.
422 private_submodules_->gain_controller2.reset(new GainController2());
423
424 RTC_LOG(LS_INFO) << "Capture analyzer activated: "
425 << !!private_submodules_->capture_analyzer
426 << "\nCapture post processor activated: "
427 << !!private_submodules_->capture_post_processor
428 << "\nRender pre processor activated: "
429 << !!private_submodules_->render_pre_processor;
430
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000431 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000432}
433
434AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800435 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800436 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800437 private_submodules_->agc_manager.reset();
438 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800439 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000440}
441
niklase@google.com470e71d2011-07-07 08:21:25 +0000442int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800443 // Run in a single-threaded manner during initialization.
444 rtc::CritScope cs_render(&crit_render_);
445 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000446 return InitializeLocked();
447}
448
peahde65ddc2016-09-16 15:02:15 -0700449int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
450 int capture_output_sample_rate_hz,
451 int render_input_sample_rate_hz,
452 ChannelLayout capture_input_layout,
453 ChannelLayout capture_output_layout,
454 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700455 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700456 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
457 LayoutHasKeyboard(capture_input_layout)},
458 {capture_output_sample_rate_hz,
459 ChannelsFromLayout(capture_output_layout),
460 LayoutHasKeyboard(capture_output_layout)},
461 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
462 LayoutHasKeyboard(render_input_layout)},
463 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
464 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700465
466 return Initialize(processing_config);
467}
468
469int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800470 // Run in a single-threaded manner during initialization.
471 rtc::CritScope cs_render(&crit_render_);
472 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700473 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000474}
475
peahdf3efa82015-11-28 12:35:15 -0800476int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800477 const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800478 // Called from both threads. Thread check is therefore not possible.
Oskar Sundbom4b276482019-05-23 14:28:00 +0200479 if (processing_config == formats_.api_format) {
peah192164e2015-11-17 02:16:45 -0800480 return kNoError;
481 }
peahdf3efa82015-11-28 12:35:15 -0800482
483 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800484 return InitializeLocked(processing_config);
485}
486
niklase@google.com470e71d2011-07-07 08:21:25 +0000487int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200488 UpdateActiveSubmoduleStates();
489
Per Åhgrend47941e2019-08-22 11:51:13 +0200490 const int render_audiobuffer_sample_rate_hz =
peahdf3efa82015-11-28 12:35:15 -0800491 formats_.api_format.reverse_output_stream().num_frames() == 0
Per Åhgrend47941e2019-08-22 11:51:13 +0200492 ? formats_.render_processing_format.sample_rate_hz()
493 : formats_.api_format.reverse_output_stream().sample_rate_hz();
peahdf3efa82015-11-28 12:35:15 -0800494 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
495 render_.render_audio.reset(new AudioBuffer(
Per Åhgrend47941e2019-08-22 11:51:13 +0200496 formats_.api_format.reverse_input_stream().sample_rate_hz(),
peahdf3efa82015-11-28 12:35:15 -0800497 formats_.api_format.reverse_input_stream().num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200498 formats_.render_processing_format.sample_rate_hz(),
peahde65ddc2016-09-16 15:02:15 -0700499 formats_.render_processing_format.num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200500 render_audiobuffer_sample_rate_hz,
501 formats_.render_processing_format.num_channels()));
peah2ace3f92016-09-10 04:42:27 -0700502 if (formats_.api_format.reverse_input_stream() !=
503 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800504 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800505 formats_.api_format.reverse_input_stream().num_channels(),
506 formats_.api_format.reverse_input_stream().num_frames(),
507 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800508 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700509 } else {
peahdf3efa82015-11-28 12:35:15 -0800510 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700511 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700512 } else {
peahdf3efa82015-11-28 12:35:15 -0800513 render_.render_audio.reset(nullptr);
514 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700515 }
peahce4d9152017-05-19 01:28:05 -0700516
Per Åhgrend47941e2019-08-22 11:51:13 +0200517 capture_.capture_audio.reset(new AudioBuffer(
518 formats_.api_format.input_stream().sample_rate_hz(),
519 formats_.api_format.input_stream().num_channels(),
520 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
521 formats_.api_format.output_stream().num_channels(),
522 formats_.api_format.output_stream().sample_rate_hz(),
523 formats_.api_format.output_stream().num_channels()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000524
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200525 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() <
526 formats_.api_format.output_stream().sample_rate_hz() &&
527 formats_.api_format.output_stream().sample_rate_hz() == 48000) {
528 capture_.capture_fullband_audio.reset(
529 new AudioBuffer(formats_.api_format.input_stream().sample_rate_hz(),
530 formats_.api_format.input_stream().num_channels(),
531 formats_.api_format.output_stream().sample_rate_hz(),
532 formats_.api_format.output_stream().num_channels(),
533 formats_.api_format.output_stream().sample_rate_hz(),
534 formats_.api_format.output_stream().num_channels()));
535 } else {
536 capture_.capture_fullband_audio.reset();
537 }
538
peah764e3642016-10-22 05:04:30 -0700539 AllocateRenderQueue();
540
peah135259a2016-10-28 03:12:11 -0700541 public_submodules_->gain_control->Initialize(num_proc_channels(),
542 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700543 if (constants_.use_experimental_agc) {
544 if (!private_submodules_->agc_manager.get()) {
545 private_submodules_->agc_manager.reset(new AgcManagerDirect(
546 public_submodules_->gain_control.get(),
547 public_submodules_->gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200548 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
549 constants_.use_experimental_agc_agc2_level_estimation,
550 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700551 }
552 private_submodules_->agc_manager->Initialize();
553 private_submodules_->agc_manager->SetCaptureMuted(
554 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700555 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700556 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200557 InitializeTransient();
Per Åhgren0aefbf02019-08-23 21:29:17 +0200558 InitializeHighPassFilter();
peahde65ddc2016-09-16 15:02:15 -0700559 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
560 proc_sample_rate_hz());
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200561 InitializeVoiceDetector();
ivoc9f4a4a02016-10-28 05:39:16 -0700562 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200563 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700564 InitializeGainController2();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200565 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200566 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100567 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800568
aleloi868f32f2017-05-23 07:20:05 -0700569 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200570 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700571 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000572 return kNoError;
573}
574
Michael Graczyk86c6d332015-07-23 11:41:39 -0700575int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200576 UpdateActiveSubmoduleStates();
577
Michael Graczyk86c6d332015-07-23 11:41:39 -0700578 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700579 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
580 return kBadSampleRateError;
581 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000582 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700583
Peter Kasting69558702016-01-12 16:26:35 -0800584 const size_t num_in_channels = config.input_stream().num_channels();
585 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700586
587 // Need at least one input channel.
588 // Need either one output channel or as many outputs as there are inputs.
589 if (num_in_channels == 0 ||
590 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700591 return kBadNumberChannelsError;
592 }
593
peahdf3efa82015-11-28 12:35:15 -0800594 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000595
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200596 // Choose maximum rate to use for the split filtering.
597 RTC_DCHECK(config_.pipeline.maximum_internal_processing_rate == 48000 ||
598 config_.pipeline.maximum_internal_processing_rate == 32000);
599 int max_splitting_rate = 48000;
600 if (config_.pipeline.maximum_internal_processing_rate == 32000) {
601 max_splitting_rate = config_.pipeline.maximum_internal_processing_rate;
602 }
603
Per Åhgrenc8626b62019-08-23 15:49:51 +0200604 int capture_processing_rate = SuitableProcessRate(
peah423d2362016-04-09 16:06:52 -0700605 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700606 formats_.api_format.output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200607 max_splitting_rate,
peah2ace3f92016-09-10 04:42:27 -0700608 submodule_states_.CaptureMultiBandSubModulesActive() ||
609 submodule_states_.RenderMultiBandSubModulesActive());
Per Åhgrenc8626b62019-08-23 15:49:51 +0200610 RTC_DCHECK_NE(8000, capture_processing_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000611
peahde65ddc2016-09-16 15:02:15 -0700612 capture_nonlocked_.capture_processing_format =
613 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700614
peah2ce640f2017-04-07 03:57:48 -0700615 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200616 if (!capture_nonlocked_.echo_controller_enabled) {
Per Åhgrenc8626b62019-08-23 15:49:51 +0200617 render_processing_rate = SuitableProcessRate(
peah2ce640f2017-04-07 03:57:48 -0700618 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
619 formats_.api_format.reverse_output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200620 max_splitting_rate,
peah2ce640f2017-04-07 03:57:48 -0700621 submodule_states_.CaptureMultiBandSubModulesActive() ||
622 submodule_states_.RenderMultiBandSubModulesActive());
623 } else {
624 render_processing_rate = capture_processing_rate;
625 }
626
peahde65ddc2016-09-16 15:02:15 -0700627 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700628 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700629 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
630 kSampleRate8kHz) {
631 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000632 } else {
peahde65ddc2016-09-16 15:02:15 -0700633 render_processing_rate =
634 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000635 }
636
Per Åhgrenc8626b62019-08-23 15:49:51 +0200637 RTC_DCHECK_NE(8000, render_processing_rate);
638
peahce4d9152017-05-19 01:28:05 -0700639 if (submodule_states_.RenderMultiBandSubModulesActive()) {
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200640 // By default, downmix the render stream to mono for analysis. This has been
641 // demonstrated to work well for AEC in most practical scenarios.
642 const bool experimental_multi_channel_render =
643 config_.pipeline.experimental_multi_channel &&
644 constants_.experimental_multi_channel_render_support;
645 int render_processing_num_channels =
646 experimental_multi_channel_render
647 ? formats_.api_format.reverse_input_stream().num_channels()
648 : 1;
649 formats_.render_processing_format =
650 StreamConfig(render_processing_rate, render_processing_num_channels);
peahce4d9152017-05-19 01:28:05 -0700651 } else {
652 formats_.render_processing_format = StreamConfig(
653 formats_.api_format.reverse_input_stream().sample_rate_hz(),
654 formats_.api_format.reverse_input_stream().num_channels());
655 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000656
peahde65ddc2016-09-16 15:02:15 -0700657 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
658 kSampleRate32kHz ||
659 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
660 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800661 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000662 } else {
peahdf3efa82015-11-28 12:35:15 -0800663 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700664 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000665 }
666
667 return InitializeLocked();
668}
669
peah88ac8532016-09-12 16:47:25 -0700670void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peah88ac8532016-09-12 16:47:25 -0700671 // Run in a single-threaded manner when applying the settings.
672 rtc::CritScope cs_render(&crit_render_);
673 rtc::CritScope cs_capture(&crit_capture_);
674
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200675 const bool pipeline_config_changed =
676 config_.pipeline.experimental_multi_channel !=
677 config.pipeline.experimental_multi_channel;
678
Per Åhgren200feba2019-03-06 04:16:46 +0100679 const bool aec_config_changed =
680 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
681 config_.echo_canceller.use_legacy_aec !=
682 config.echo_canceller.use_legacy_aec ||
683 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode ||
684 (config_.echo_canceller.enabled && config.echo_canceller.use_legacy_aec &&
685 config_.echo_canceller.legacy_moderate_suppression_level !=
686 config.echo_canceller.legacy_moderate_suppression_level);
687
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100688 const bool agc1_config_changed =
689 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
690 config_.gain_controller1.mode != config.gain_controller1.mode ||
691 config_.gain_controller1.target_level_dbfs !=
692 config.gain_controller1.target_level_dbfs ||
693 config_.gain_controller1.compression_gain_db !=
694 config.gain_controller1.compression_gain_db ||
695 config_.gain_controller1.enable_limiter !=
696 config.gain_controller1.enable_limiter ||
697 config_.gain_controller1.analog_level_minimum !=
698 config.gain_controller1.analog_level_minimum ||
699 config_.gain_controller1.analog_level_maximum !=
700 config.gain_controller1.analog_level_maximum;
701
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200702 const bool voice_detection_config_changed =
703 config_.voice_detection.enabled != config.voice_detection.enabled;
704
Yves Gerey499bc6c2018-10-10 18:29:07 +0200705 config_ = config;
706
Per Åhgren200feba2019-03-06 04:16:46 +0100707 if (aec_config_changed) {
708 InitializeEchoController();
709 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200710
Sam Zackrisson23513132019-01-11 15:10:32 +0100711 public_submodules_->noise_suppression->Enable(
712 config.noise_suppression.enabled);
713 public_submodules_->noise_suppression->set_level(
714 NsConfigLevelToInterfaceLevel(config.noise_suppression.level));
715
Per Åhgren0aefbf02019-08-23 21:29:17 +0200716 InitializeHighPassFilter();
peah8271d042016-11-22 07:24:52 -0800717
Mirko Bonadei675513b2017-11-09 11:09:25 +0100718 RTC_LOG(LS_INFO) << "Highpass filter activated: "
719 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800720
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100721 if (agc1_config_changed) {
722 ApplyAgc1Config(config_.gain_controller1);
723 }
724
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100725 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700726 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100727 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
728 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100729 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100730 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700731 config_.gain_controller2 = AudioProcessing::Config::GainController2();
732 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200733 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200734 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200735 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100736 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
737 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200738 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
739 << config_.pre_amplifier.enabled;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100740
741 if (config_.level_estimation.enabled &&
742 !private_submodules_->output_level_estimator) {
saza6787f232019-10-11 19:31:07 +0200743 private_submodules_->output_level_estimator =
744 std::make_unique<LevelEstimator>();
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100745 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100746
Sam Zackrisson0824c6f2019-10-07 14:03:56 +0200747 if (voice_detection_config_changed) {
748 InitializeVoiceDetector();
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100749 }
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200750
751 // Reinitialization must happen after all submodule configuration to avoid
752 // additional reinitializations on the next capture / render processing call.
753 if (pipeline_config_changed) {
754 InitializeLocked(formats_.api_format);
755 }
peah88ac8532016-09-12 16:47:25 -0700756}
757
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100758void AudioProcessingImpl::ApplyAgc1Config(
759 const Config::GainController1& config) {
760 GainControl* agc = agc1();
761 int error = agc->Enable(config.enabled);
762 RTC_DCHECK_EQ(kNoError, error);
763 error = agc->set_mode(Agc1ConfigModeToInterfaceMode(config.mode));
764 RTC_DCHECK_EQ(kNoError, error);
765 error = agc->set_target_level_dbfs(config.target_level_dbfs);
766 RTC_DCHECK_EQ(kNoError, error);
767 error = agc->set_compression_gain_db(config.compression_gain_db);
768 RTC_DCHECK_EQ(kNoError, error);
769 error = agc->enable_limiter(config.enable_limiter);
770 RTC_DCHECK_EQ(kNoError, error);
771 error = agc->set_analog_level_limits(config.analog_level_minimum,
772 config.analog_level_maximum);
773 RTC_DCHECK_EQ(kNoError, error);
774}
775
776GainControl* AudioProcessingImpl::agc1() {
777 if (constants_.use_experimental_agc) {
778 return public_submodules_->gain_control_for_experimental_agc.get();
779 }
780 return public_submodules_->gain_control.get();
781}
782
783const GainControl* AudioProcessingImpl::agc1() const {
784 if (constants_.use_experimental_agc) {
785 return public_submodules_->gain_control_for_experimental_agc.get();
786 }
787 return public_submodules_->gain_control.get();
788}
789
peah88ac8532016-09-12 16:47:25 -0700790void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800791 // Run in a single-threaded manner when setting the extra options.
792 rtc::CritScope cs_render(&crit_render_);
793 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000794
Per Åhgrenf204faf2019-04-25 15:18:06 +0200795 capture_nonlocked_.use_aec2_extended_filter =
796 config.Get<ExtendedFilter>().enabled;
797 capture_nonlocked_.use_aec2_delay_agnostic =
798 config.Get<DelayAgnostic>().enabled;
799 capture_nonlocked_.use_aec2_refined_adaptive_filter =
800 config.Get<RefinedAdaptiveFilter>().enabled;
peahb624d8c2016-03-05 03:01:14 -0800801
peahdf3efa82015-11-28 12:35:15 -0800802 if (capture_.transient_suppressor_enabled !=
803 config.Get<ExperimentalNs>().enabled) {
804 capture_.transient_suppressor_enabled =
805 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000806 InitializeTransient();
807 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000808}
809
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000810int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800811 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700812 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000813}
814
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200815int AudioProcessingImpl::proc_fullband_sample_rate_hz() const {
816 return capture_.capture_fullband_audio
817 ? capture_.capture_fullband_audio->num_frames() * 100
818 : capture_nonlocked_.capture_processing_format.sample_rate_hz();
819}
820
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000821int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800822 // Used as callback from submodules, hence locking is not allowed.
823 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000824}
825
Peter Kasting69558702016-01-12 16:26:35 -0800826size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800827 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700828 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000829}
830
Peter Kasting69558702016-01-12 16:26:35 -0800831size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800832 // Used as callback from submodules, hence locking is not allowed.
833 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000834}
835
Peter Kasting69558702016-01-12 16:26:35 -0800836size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800837 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrissonfeee1e42019-09-20 07:50:35 +0200838 const bool experimental_multi_channel_capture =
839 config_.pipeline.experimental_multi_channel &&
840 constants_.experimental_multi_channel_capture_support;
841 if (capture_nonlocked_.echo_controller_enabled &&
842 !experimental_multi_channel_capture) {
843 return 1;
844 }
845 return num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800846}
847
Peter Kasting69558702016-01-12 16:26:35 -0800848size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800849 // Used as callback from submodules, hence locking is not allowed.
850 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000851}
852
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000853void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800854 rtc::CritScope cs(&crit_capture_);
855 capture_.output_will_be_muted = muted;
856 if (private_submodules_->agc_manager.get()) {
857 private_submodules_->agc_manager->SetCaptureMuted(
858 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000859 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000860}
861
Alessio Bazzicac054e782018-04-16 12:10:09 +0200862void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200863 switch (setting.type()) {
864 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
865 render_runtime_settings_enqueuer_.Enqueue(setting);
866 return;
867 case RuntimeSetting::Type::kNotSpecified:
868 RTC_NOTREACHED();
869 return;
870 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100871 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200872 case RuntimeSetting::Type::kCaptureFixedPostGain:
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200873 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200874 capture_runtime_settings_enqueuer_.Enqueue(setting);
875 return;
876 }
877 // The language allows the enum to have a non-enumerator
878 // value. Check that this doesn't happen.
879 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200880}
881
882AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
883 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200884 : runtime_settings_(*runtime_settings) {
885 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200886}
887
888AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
889 default;
890
891void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
892 RuntimeSetting setting) {
893 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200894 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200895 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200896 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200897 RTC_LOG(LS_ERROR)
898 << "The runtime settings queue is full. Oldest setting discarded.";
899 }
900 if (remaining_attempts == 0)
901 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
902}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000903
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000904int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700905 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000906 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000907 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000908 int output_sample_rate_hz,
909 ChannelLayout output_layout,
910 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800911 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800912 StreamConfig input_stream;
913 StreamConfig output_stream;
914 {
915 // Access the formats_.api_format.input_stream beneath the capture lock.
916 // The lock must be released as it is later required in the call
917 // to ProcessStream(,,,);
918 rtc::CritScope cs(&crit_capture_);
919 input_stream = formats_.api_format.input_stream();
920 output_stream = formats_.api_format.output_stream();
921 }
922
Michael Graczyk86c6d332015-07-23 11:41:39 -0700923 input_stream.set_sample_rate_hz(input_sample_rate_hz);
924 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
925 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700926 output_stream.set_sample_rate_hz(output_sample_rate_hz);
927 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
928 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
929
930 if (samples_per_channel != input_stream.num_frames()) {
931 return kBadDataLengthError;
932 }
933 return ProcessStream(src, input_stream, output_stream, dest);
934}
935
936int AudioProcessingImpl::ProcessStream(const float* const* src,
937 const StreamConfig& input_config,
938 const StreamConfig& output_config,
939 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800940 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800941 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700942 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800943 {
944 // Acquire the capture lock in order to safely call the function
945 // that retrieves the render side data. This function accesses apm
946 // getters that need the capture lock held when being called.
947 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700948 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800949
950 if (!src || !dest) {
951 return kNullPointerError;
952 }
953
954 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700955 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000956 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000957
Oskar Sundbom4b276482019-05-23 14:28:00 +0200958 if (processing_config.input_stream() != input_config) {
959 processing_config.input_stream() = input_config;
960 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800961 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200962
963 if (processing_config.output_stream() != output_config) {
964 processing_config.output_stream() = output_config;
965 reinitialization_required = true;
966 }
967
968 if (reinitialization_required) {
969 // Reinitialize.
970 rtc::CritScope cs_render(&crit_render_);
971 rtc::CritScope cs_capture(&crit_capture_);
972 RETURN_ON_ERR(InitializeLocked(processing_config));
973 }
974
peahdf3efa82015-11-28 12:35:15 -0800975 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700976 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
977 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000978
aleloi868f32f2017-05-23 07:20:05 -0700979 if (aec_dump_) {
980 RecordUnprocessedCaptureStream(src);
981 }
982
Per Åhgrena1351272019-08-15 12:15:46 +0200983 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800984 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200985 if (capture_.capture_fullband_audio) {
986 capture_.capture_fullband_audio->CopyFrom(
987 src, formats_.api_format.input_stream());
988 }
peahde65ddc2016-09-16 15:02:15 -0700989 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg422b9e02019-10-09 13:02:14 +0200990 if (capture_.capture_fullband_audio) {
991 capture_.capture_fullband_audio->CopyTo(formats_.api_format.output_stream(),
992 dest);
993 } else {
994 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
995 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000996
aleloi868f32f2017-05-23 07:20:05 -0700997 if (aec_dump_) {
998 RecordProcessedCaptureStream(dest);
999 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001000 return kNoError;
1001}
1002
Alex Loiko73ec0192018-05-15 10:52:28 +02001003void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +02001004 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +02001005 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +02001006 if (aec_dump_) {
1007 aec_dump_->WriteRuntimeSetting(setting);
1008 }
Alessio Bazzicac054e782018-04-16 12:10:09 +02001009 switch (setting.type()) {
1010 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +02001011 if (config_.pre_amplifier.enabled) {
1012 float value;
1013 setting.GetFloat(&value);
1014 private_submodules_->pre_amplifier->SetGainFactor(value);
1015 }
1016 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +02001017 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001018 case RuntimeSetting::Type::kCaptureCompressionGain: {
1019 float value;
1020 setting.GetFloat(&value);
1021 int int_value = static_cast<int>(value + .5f);
1022 config_.gain_controller1.compression_gain_db = int_value;
1023 int error = agc1()->set_compression_gain_db(int_value);
1024 RTC_DCHECK_EQ(kNoError, error);
1025 break;
1026 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001027 case RuntimeSetting::Type::kCaptureFixedPostGain: {
1028 if (config_.gain_controller2.enabled) {
1029 float value;
1030 setting.GetFloat(&value);
1031 config_.gain_controller2.fixed_digital.gain_db = value;
1032 private_submodules_->gain_controller2->ApplyConfig(
1033 config_.gain_controller2);
1034 }
1035 break;
1036 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001037 case RuntimeSetting::Type::kPlayoutVolumeChange: {
1038 int value;
1039 setting.GetInt(&value);
1040 capture_.playout_volume = value;
1041 break;
1042 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001043 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1044 RTC_NOTREACHED();
1045 break;
1046 case RuntimeSetting::Type::kNotSpecified:
1047 RTC_NOTREACHED();
1048 break;
1049 }
1050 }
1051}
1052
1053void AudioProcessingImpl::HandleRenderRuntimeSettings() {
1054 RuntimeSetting setting;
1055 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +02001056 if (aec_dump_) {
1057 aec_dump_->WriteRuntimeSetting(setting);
1058 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001059 switch (setting.type()) {
1060 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1061 if (private_submodules_->render_pre_processor) {
1062 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
1063 }
1064 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001065 case RuntimeSetting::Type::kCapturePreGain: // fall-through
1066 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001067 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001068 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001069 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001070 RTC_NOTREACHED();
1071 break;
1072 }
1073 }
1074}
1075
peah9e6a2902017-05-15 07:19:21 -07001076void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001077 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001078
1079 // Insert the samples into the queue.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001080 if (private_submodules_->echo_cancellation) {
1081 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001082 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1083 num_reverse_channels(),
1084 &aec_render_queue_buffer_);
1085
Per Åhgrenf204faf2019-04-25 15:18:06 +02001086 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1087 // The data queue is full and needs to be emptied.
1088 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001089
Per Åhgrenf204faf2019-04-25 15:18:06 +02001090 // Retry the insert (should always work).
1091 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1092 RTC_DCHECK(result);
1093 }
peaha0624602016-10-25 04:45:24 -07001094 }
1095
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001096 if (private_submodules_->echo_control_mobile) {
1097 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1098 num_reverse_channels(),
1099 &aecm_render_queue_buffer_);
1100 RTC_DCHECK(aecm_render_signal_queue_);
1101 // Insert the samples into the queue.
1102 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1103 // The data queue is full and needs to be emptied.
1104 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001105
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001106 // Retry the insert (should always work).
1107 bool result =
1108 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1109 RTC_DCHECK(result);
1110 }
peah764e3642016-10-22 05:04:30 -07001111 }
peah701d6282016-10-25 05:42:20 -07001112
1113 if (!constants_.use_experimental_agc) {
1114 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1115 // Insert the samples into the queue.
1116 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1117 // The data queue is full and needs to be emptied.
1118 EmptyQueuedRenderAudio();
1119
1120 // Retry the insert (should always work).
1121 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1122 RTC_DCHECK(result);
1123 }
1124 }
peah9e6a2902017-05-15 07:19:21 -07001125}
ivoc9f4a4a02016-10-28 05:39:16 -07001126
peah9e6a2902017-05-15 07:19:21 -07001127void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001128 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1129
1130 // Insert the samples into the queue.
1131 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1132 // The data queue is full and needs to be emptied.
1133 EmptyQueuedRenderAudio();
1134
1135 // Retry the insert (should always work).
1136 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1137 RTC_DCHECK(result);
1138 }
peah764e3642016-10-22 05:04:30 -07001139}
1140
1141void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001142 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001143 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001144
ivoc9f4a4a02016-10-28 05:39:16 -07001145 const size_t new_red_render_queue_element_max_size =
1146 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1147
peaha0624602016-10-25 04:45:24 -07001148 // Reallocate the queues if the queue item sizes are too small to fit the
1149 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001150
1151 if (agc_render_queue_element_max_size_ <
1152 new_agc_render_queue_element_max_size) {
1153 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1154
1155 std::vector<int16_t> template_queue_element(
1156 agc_render_queue_element_max_size_);
1157
1158 agc_render_signal_queue_.reset(
1159 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1160 kMaxNumFramesToBuffer, template_queue_element,
1161 RenderQueueItemVerifier<int16_t>(
1162 agc_render_queue_element_max_size_)));
1163
1164 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1165 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1166 } else {
1167 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001168 }
ivoc9f4a4a02016-10-28 05:39:16 -07001169
1170 if (red_render_queue_element_max_size_ <
1171 new_red_render_queue_element_max_size) {
1172 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1173
1174 std::vector<float> template_queue_element(
1175 red_render_queue_element_max_size_);
1176
1177 red_render_signal_queue_.reset(
1178 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1179 kMaxNumFramesToBuffer, template_queue_element,
1180 RenderQueueItemVerifier<float>(
1181 red_render_queue_element_max_size_)));
1182
1183 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1184 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1185 } else {
1186 red_render_signal_queue_->Clear();
1187 }
peah764e3642016-10-22 05:04:30 -07001188}
1189
1190void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1191 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001192 if (private_submodules_->echo_cancellation) {
1193 RTC_DCHECK(aec_render_signal_queue_);
1194 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
1195 private_submodules_->echo_cancellation->ProcessRenderAudio(
1196 aec_capture_queue_buffer_);
1197 }
peaha0624602016-10-25 04:45:24 -07001198 }
1199
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001200 if (private_submodules_->echo_control_mobile) {
1201 RTC_DCHECK(aecm_render_signal_queue_);
1202 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
1203 private_submodules_->echo_control_mobile->ProcessRenderAudio(
1204 aecm_capture_queue_buffer_);
1205 }
peah701d6282016-10-25 05:42:20 -07001206 }
1207
1208 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1209 public_submodules_->gain_control->ProcessRenderAudio(
1210 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001211 }
ivoc9f4a4a02016-10-28 05:39:16 -07001212
1213 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001214 RTC_DCHECK(private_submodules_->echo_detector);
1215 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001216 red_capture_queue_buffer_);
1217 }
peah764e3642016-10-22 05:04:30 -07001218}
1219
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001220int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001221 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001222 {
1223 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001224 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001225 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001226 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001227 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001228 }
peahfa6228e2015-11-16 16:27:42 -08001229
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001230 if (!frame) {
1231 return kNullPointerError;
1232 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001233 // Must be a native rate.
1234 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1235 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001236 frame->sample_rate_hz_ != kSampleRate32kHz &&
1237 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001238 return kBadSampleRateError;
1239 }
peah192164e2015-11-17 02:16:45 -08001240
peahdf3efa82015-11-28 12:35:15 -08001241 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001242 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001243 {
1244 // Aquire lock for the access of api_format.
1245 // The lock is released immediately due to the conditional
1246 // reinitialization.
1247 rtc::CritScope cs_capture(&crit_capture_);
1248 // TODO(ajm): The input and output rates and channels are currently
1249 // constrained to be identical in the int16 interface.
1250 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001251
1252 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001253 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001254
Oskar Sundbom4b276482019-05-23 14:28:00 +02001255 reinitialization_required =
1256 reinitialization_required ||
1257 processing_config.input_stream().sample_rate_hz() !=
1258 frame->sample_rate_hz_ ||
1259 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1260 processing_config.output_stream().sample_rate_hz() !=
1261 frame->sample_rate_hz_ ||
1262 processing_config.output_stream().num_channels() != frame->num_channels_;
1263
1264 if (reinitialization_required) {
1265 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1266 processing_config.input_stream().set_num_channels(frame->num_channels_);
1267 processing_config.output_stream().set_sample_rate_hz(
1268 frame->sample_rate_hz_);
1269 processing_config.output_stream().set_num_channels(frame->num_channels_);
1270
1271 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001272 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001273 rtc::CritScope cs_capture(&crit_capture_);
1274 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001275 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001276
peahdf3efa82015-11-28 12:35:15 -08001277 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001278 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001279 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001280 return kBadDataLengthError;
1281 }
1282
aleloi868f32f2017-05-23 07:20:05 -07001283 if (aec_dump_) {
1284 RecordUnprocessedCaptureStream(*frame);
1285 }
1286
Per Åhgrend47941e2019-08-22 11:51:13 +02001287 capture_.capture_audio->CopyFrom(frame);
Gustaf Ullberg3c918b12019-10-11 13:14:44 +02001288 if (capture_.capture_fullband_audio) {
1289 capture_.capture_fullband_audio->CopyFrom(frame);
1290 }
peahde65ddc2016-09-16 15:02:15 -07001291 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001292 if (submodule_states_.CaptureMultiBandProcessingPresent() ||
Per Åhgrena1351272019-08-15 12:15:46 +02001293 submodule_states_.CaptureFullBandProcessingActive()) {
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001294 if (capture_.capture_fullband_audio) {
1295 capture_.capture_fullband_audio->CopyTo(frame);
1296 } else {
1297 capture_.capture_audio->CopyTo(frame);
1298 }
Per Åhgrena1351272019-08-15 12:15:46 +02001299 }
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001300 if (capture_.stats.voice_detected) {
1301 frame->vad_activity_ = *capture_.stats.voice_detected
1302 ? AudioFrame::kVadActive
1303 : AudioFrame::kVadPassive;
1304 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001305
aleloi868f32f2017-05-23 07:20:05 -07001306 if (aec_dump_) {
1307 RecordProcessedCaptureStream(*frame);
1308 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001309
1310 return kNoError;
1311}
1312
peahde65ddc2016-09-16 15:02:15 -07001313int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001314 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001315
peahb58a1582016-03-15 09:34:24 -07001316 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001317 // TODO(peah): Simplify once the public API Enable functions for these
1318 // are moved to APM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001319 RTC_DCHECK_LE(!!private_submodules_->echo_controller +
1320 !!private_submodules_->echo_cancellation +
1321 !!private_submodules_->echo_control_mobile,
1322 1);
peahb58a1582016-03-15 09:34:24 -07001323
peahde65ddc2016-09-16 15:02:15 -07001324 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001325
Alex Loikob5c9a792018-04-16 16:31:22 +02001326 if (private_submodules_->pre_amplifier) {
1327 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001328 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001329 capture_buffer->num_frames()));
1330 }
1331
Per Åhgren928146f2019-08-20 09:19:21 +02001332 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001333 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001334 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001335 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1336 if (log_rms) {
1337 capture_rms_interval_counter_ = 0;
1338 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001339 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1340 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1341 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1342 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001343 }
1344
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001345 if (private_submodules_->echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001346 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001347 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001348 capture_.echo_path_gain_change =
1349 capture_.prev_analog_mic_level != analog_mic_level &&
1350 capture_.prev_analog_mic_level != -1;
1351 capture_.prev_analog_mic_level = analog_mic_level;
1352
Per Åhgrend2650d12018-10-02 17:00:59 +02001353 // Detect and flag any change in the pre-amplifier gain.
1354 if (private_submodules_->pre_amplifier) {
1355 float pre_amp_gain = private_submodules_->pre_amplifier->GetGainFactor();
1356 capture_.echo_path_gain_change =
1357 capture_.echo_path_gain_change ||
1358 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001359 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001360 capture_.prev_pre_amp_gain = pre_amp_gain;
1361 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001362
1363 // Detect volume change.
1364 capture_.echo_path_gain_change =
1365 capture_.echo_path_gain_change ||
1366 (capture_.prev_playout_volume != capture_.playout_volume &&
1367 capture_.prev_playout_volume >= 0);
1368 capture_.prev_playout_volume = capture_.playout_volume;
1369
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001370 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001371 }
1372
peahbe615622016-02-13 16:40:47 -08001373 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001374 public_submodules_->gain_control->is_enabled()) {
1375 private_submodules_->agc_manager->AnalyzePreProcess(
Per Åhgren928146f2019-08-20 09:19:21 +02001376 capture_buffer->channels_f()[0], capture_buffer->num_channels(),
peahde65ddc2016-09-16 15:02:15 -07001377 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001378
1379 if (constants_.use_experimental_agc_process_before_aec) {
1380 private_submodules_->agc_manager->Process(
Per Åhgrend47941e2019-08-22 11:51:13 +02001381 capture_buffer->channels_const()[0],
Alex Loikod9342442018-09-10 13:59:41 +02001382 capture_nonlocked_.capture_processing_format.num_frames(),
1383 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1384 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001385 }
1386
peah2ace3f92016-09-10 04:42:27 -07001387 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1388 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001389 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1390 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001391 }
1392
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001393 const bool experimental_multi_channel_capture =
1394 config_.pipeline.experimental_multi_channel &&
1395 constants_.experimental_multi_channel_capture_support;
1396 if (private_submodules_->echo_controller &&
1397 !experimental_multi_channel_capture) {
peah522d71b2017-02-23 05:16:26 -08001398 // Force down-mixing of the number of channels after the detection of
1399 // capture signal saturation.
1400 // TODO(peah): Look into ensuring that this kind of tampering with the
1401 // AudioBuffer functionality should not be needed.
1402 capture_buffer->set_num_channels(1);
1403 }
1404
Per Åhgren0aefbf02019-08-23 21:29:17 +02001405 if (private_submodules_->high_pass_filter) {
1406 private_submodules_->high_pass_filter->Process(capture_buffer);
peah8271d042016-11-22 07:24:52 -08001407 }
peahde65ddc2016-09-16 15:02:15 -07001408 RETURN_ON_ERR(
1409 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1410 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001411
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001412 if (private_submodules_->echo_control_mobile) {
1413 // Ensure that the stream delay was set before the call to the
1414 // AECM ProcessCaptureAudio function.
1415 if (!was_stream_delay_set()) {
1416 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001417 }
1418
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001419 if (public_submodules_->noise_suppression->is_enabled()) {
Per Åhgrena1351272019-08-15 12:15:46 +02001420 private_submodules_->echo_control_mobile->CopyLowPassReference(
1421 capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001422 }
peahe0eae3c2016-12-14 01:16:23 -08001423
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001424 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah253534d2016-03-15 04:32:28 -07001425
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001426 RETURN_ON_ERR(private_submodules_->echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001427 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001428 } else {
1429 if (private_submodules_->echo_controller) {
1430 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1431
1432 if (was_stream_delay_set()) {
1433 private_submodules_->echo_controller->SetAudioBufferDelay(
1434 stream_delay_ms());
1435 }
1436
1437 private_submodules_->echo_controller->ProcessCapture(
1438 capture_buffer, capture_.echo_path_gain_change);
1439 } else if (private_submodules_->echo_cancellation) {
1440 // Ensure that the stream delay was set before the call to the
1441 // AEC ProcessCaptureAudio function.
1442 if (!was_stream_delay_set()) {
1443 return AudioProcessing::kStreamParameterNotSetError;
1444 }
1445
1446 RETURN_ON_ERR(private_submodules_->echo_cancellation->ProcessCaptureAudio(
1447 capture_buffer, stream_delay_ms()));
1448 }
1449
1450 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
Per Åhgren46537a32017-06-07 10:08:10 +02001451 }
ivoc9f4a4a02016-10-28 05:39:16 -07001452
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001453 if (config_.voice_detection.enabled) {
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001454 capture_.stats.voice_detected =
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001455 private_submodules_->voice_detector->ProcessCaptureAudio(
1456 capture_buffer);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001457 } else {
1458 capture_.stats.voice_detected = absl::nullopt;
1459 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001460
peahbe615622016-02-13 16:40:47 -08001461 if (constants_.use_experimental_agc &&
Alex Loikod9342442018-09-10 13:59:41 +02001462 public_submodules_->gain_control->is_enabled() &&
1463 !constants_.use_experimental_agc_process_before_aec) {
peahdf3efa82015-11-28 12:35:15 -08001464 private_submodules_->agc_manager->Process(
Per Åhgren928146f2019-08-20 09:19:21 +02001465 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
peahde65ddc2016-09-16 15:02:15 -07001466 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001467 }
Per Åhgren200feba2019-03-06 04:16:46 +01001468 // TODO(peah): Add reporting from AEC3 whether there is echo.
peahb8fbb542016-03-15 02:28:08 -07001469 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001470 capture_buffer,
Per Åhgrenf204faf2019-04-25 15:18:06 +02001471 private_submodules_->echo_cancellation &&
1472 private_submodules_->echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001473
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001474 if (submodule_states_.CaptureMultiBandProcessingPresent() &&
peah2ace3f92016-09-10 04:42:27 -07001475 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001476 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1477 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001478 }
1479
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001480 if (capture_.capture_fullband_audio) {
Gustaf Ullberg8675eee2019-10-09 13:34:36 +02001481 const auto& ec = private_submodules_->echo_controller;
1482 bool ec_active = ec ? ec->ActiveProcessing() : false;
1483 // Only update the fullband buffer if the multiband processing has changed
1484 // the signal. Keep the original signal otherwise.
1485 if (submodule_states_.CaptureMultiBandProcessingActive(ec_active)) {
1486 capture_buffer->CopyTo(capture_.capture_fullband_audio.get());
1487 }
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001488 capture_buffer = capture_.capture_fullband_audio.get();
1489 }
1490
peah9e6a2902017-05-15 07:19:21 -07001491 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001492 RTC_DCHECK(private_submodules_->echo_detector);
1493 private_submodules_->echo_detector->AnalyzeCaptureAudio(
Per Åhgrend47941e2019-08-22 11:51:13 +02001494 rtc::ArrayView<const float>(capture_buffer->channels()[0],
peah9e6a2902017-05-15 07:19:21 -07001495 capture_buffer->num_frames()));
1496 }
1497
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001498 // TODO(aluebs): Investigate if the transient suppression placement should be
1499 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001500 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001501 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001502 private_submodules_->agc_manager.get()
1503 ? private_submodules_->agc_manager->voice_probability()
1504 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001505
peahdf3efa82015-11-28 12:35:15 -08001506 public_submodules_->transient_suppressor->Suppress(
Per Åhgrend47941e2019-08-22 11:51:13 +02001507 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001508 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001509 capture_buffer->split_bands_const(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001510 capture_buffer->num_frames_per_band(),
1511 capture_.keyboard_info.keyboard_data,
1512 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001513 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001514 }
1515
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001516 // Experimental APM sub-module that analyzes |capture_buffer|.
1517 if (private_submodules_->capture_analyzer) {
1518 private_submodules_->capture_analyzer->Analyze(capture_buffer);
1519 }
1520
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001521 if (config_.gain_controller2.enabled) {
Alex Loikoa837dd72018-08-06 16:32:12 +02001522 private_submodules_->gain_controller2->NotifyAnalogLevel(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001523 agc1()->stream_analog_level());
alessiob3ec96df2017-05-22 06:57:06 -07001524 private_submodules_->gain_controller2->Process(capture_buffer);
1525 }
1526
Sam Zackrisson0beac582017-09-25 12:04:02 +02001527 if (private_submodules_->capture_post_processor) {
1528 private_submodules_->capture_post_processor->Process(capture_buffer);
1529 }
1530
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001531 // The level estimator operates on the recombined data.
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001532 if (config_.level_estimation.enabled) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001533 private_submodules_->output_level_estimator->ProcessStream(*capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001534 capture_.stats.output_rms_dbfs =
1535 private_submodules_->output_level_estimator->RMS();
1536 } else {
1537 capture_.stats.output_rms_dbfs = absl::nullopt;
1538 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001539
Per Åhgren928146f2019-08-20 09:19:21 +02001540 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001541 capture_buffer->channels_const()[0],
peah1b08dc32016-12-20 13:45:58 -08001542 capture_nonlocked_.capture_processing_format.num_frames()));
1543 if (log_rms) {
1544 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1545 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1546 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1547 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1548 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1549 }
1550
peahdf3efa82015-11-28 12:35:15 -08001551 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001552 return kNoError;
1553}
1554
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001555int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001556 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001557 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001558 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001559 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001560 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001561 const StreamConfig reverse_config = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001562 sample_rate_hz,
1563 ChannelsFromLayout(layout),
1564 LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001565 };
1566 if (samples_per_channel != reverse_config.num_frames()) {
1567 return kBadDataLengthError;
1568 }
peahdf3efa82015-11-28 12:35:15 -08001569 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001570}
1571
peahde65ddc2016-09-16 15:02:15 -07001572int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1573 const StreamConfig& input_config,
1574 const StreamConfig& output_config,
1575 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001576 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001577 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001578 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001579 if (submodule_states_.RenderMultiBandProcessingActive() ||
1580 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001581 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1582 dest);
peah2ace3f92016-09-10 04:42:27 -07001583 } else if (formats_.api_format.reverse_input_stream() !=
1584 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001585 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1586 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001587 } else {
peahde65ddc2016-09-16 15:02:15 -07001588 CopyAudioIfNeeded(src, input_config.num_frames(),
1589 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001590 }
1591
1592 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001593}
1594
peahdf3efa82015-11-28 12:35:15 -08001595int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001596 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001597 const StreamConfig& input_config,
1598 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001599 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001600 return kNullPointerError;
1601 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001602
peahde65ddc2016-09-16 15:02:15 -07001603 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001604 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001605 }
1606
peahdf3efa82015-11-28 12:35:15 -08001607 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001608 processing_config.reverse_input_stream() = input_config;
1609 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001610
peahdf3efa82015-11-28 12:35:15 -08001611 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001612 RTC_DCHECK_EQ(input_config.num_frames(),
1613 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001614
aleloi868f32f2017-05-23 07:20:05 -07001615 if (aec_dump_) {
1616 const size_t channel_size =
1617 formats_.api_format.reverse_input_stream().num_frames();
1618 const size_t num_channels =
1619 formats_.api_format.reverse_input_stream().num_channels();
1620 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001621 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001622 }
peahdf3efa82015-11-28 12:35:15 -08001623 render_.render_audio->CopyFrom(src,
1624 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001625 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001626}
1627
1628int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001629 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001630 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001631 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001632 return kNullPointerError;
1633 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001634 // Must be a native rate.
1635 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1636 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001637 frame->sample_rate_hz_ != kSampleRate32kHz &&
1638 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001639 return kBadSampleRateError;
1640 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001641
Michael Graczyk86c6d332015-07-23 11:41:39 -07001642 if (frame->num_channels_ <= 0) {
1643 return kBadNumberChannelsError;
1644 }
1645
peahdf3efa82015-11-28 12:35:15 -08001646 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001647 processing_config.reverse_input_stream().set_sample_rate_hz(
1648 frame->sample_rate_hz_);
1649 processing_config.reverse_input_stream().set_num_channels(
1650 frame->num_channels_);
1651 processing_config.reverse_output_stream().set_sample_rate_hz(
1652 frame->sample_rate_hz_);
1653 processing_config.reverse_output_stream().set_num_channels(
1654 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001655
peahdf3efa82015-11-28 12:35:15 -08001656 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001657 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001658 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001659 return kBadDataLengthError;
1660 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001661
aleloi868f32f2017-05-23 07:20:05 -07001662 if (aec_dump_) {
1663 aec_dump_->WriteRenderStreamMessage(*frame);
1664 }
1665
Per Åhgrend47941e2019-08-22 11:51:13 +02001666 render_.render_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001667 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001668 if (submodule_states_.RenderMultiBandProcessingActive() ||
1669 submodule_states_.RenderFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001670 render_.render_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001671 }
aluebsb0319552016-03-17 20:39:53 -07001672 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001673}
niklase@google.com470e71d2011-07-07 08:21:25 +00001674
peahde65ddc2016-09-16 15:02:15 -07001675int AudioProcessingImpl::ProcessRenderStreamLocked() {
1676 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001677
Alex Loiko73ec0192018-05-15 10:52:28 +02001678 HandleRenderRuntimeSettings();
1679
Alex Loiko5825aa62017-12-18 16:02:40 +01001680 if (private_submodules_->render_pre_processor) {
1681 private_submodules_->render_pre_processor->Process(render_buffer);
1682 }
1683
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001684 QueueNonbandedRenderAudio(render_buffer);
1685
peah2ace3f92016-09-10 04:42:27 -07001686 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001687 SampleRateSupportsMultiBand(
1688 formats_.render_processing_format.sample_rate_hz())) {
1689 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001690 }
1691
peahce4d9152017-05-19 01:28:05 -07001692 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1693 QueueBandedRenderAudio(render_buffer);
1694 }
1695
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001696 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001697 if (private_submodules_->echo_controller) {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001698 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001699 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001700
peah2ace3f92016-09-10 04:42:27 -07001701 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001702 SampleRateSupportsMultiBand(
1703 formats_.render_processing_format.sample_rate_hz())) {
1704 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001705 }
1706
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001707 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001708}
1709
1710int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001711 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001712 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001713 capture_.was_stream_delay_set = true;
1714 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001715
niklase@google.com470e71d2011-07-07 08:21:25 +00001716 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001717 delay = 0;
1718 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001719 }
1720
1721 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1722 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001723 delay = 500;
1724 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001725 }
1726
peahdf3efa82015-11-28 12:35:15 -08001727 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001728 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001729}
1730
1731int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001732 // Used as callback from submodules, hence locking is not allowed.
1733 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001734}
1735
1736bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001737 // Used as callback from submodules, hence locking is not allowed.
1738 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001739}
1740
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001741void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001742 rtc::CritScope cs(&crit_capture_);
1743 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001744}
1745
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001746void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001747 rtc::CritScope cs(&crit_capture_);
1748 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001749}
1750
1751int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001752 rtc::CritScope cs(&crit_capture_);
1753 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001754}
1755
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001756void AudioProcessingImpl::set_stream_analog_level(int level) {
1757 rtc::CritScope cs_capture(&crit_capture_);
1758 int error = agc1()->set_stream_analog_level(level);
1759 RTC_DCHECK_EQ(kNoError, error);
1760}
1761
1762int AudioProcessingImpl::recommended_stream_analog_level() const {
1763 rtc::CritScope cs_capture(&crit_capture_);
1764 return agc1()->stream_analog_level();
1765}
1766
aleloi868f32f2017-05-23 07:20:05 -07001767void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1768 RTC_DCHECK(aec_dump);
1769 rtc::CritScope cs_render(&crit_render_);
1770 rtc::CritScope cs_capture(&crit_capture_);
1771
1772 // The previously attached AecDump will be destroyed with the
1773 // 'aec_dump' parameter, which is after locks are released.
1774 aec_dump_.swap(aec_dump);
1775 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001776 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001777}
1778
1779void AudioProcessingImpl::DetachAecDump() {
1780 // The d-tor of a task-queue based AecDump blocks until all pending
1781 // tasks are done. This construction avoids blocking while holding
1782 // the render and capture locks.
1783 std::unique_ptr<AecDump> aec_dump = nullptr;
1784 {
1785 rtc::CritScope cs_render(&crit_render_);
1786 rtc::CritScope cs_capture(&crit_capture_);
1787 aec_dump = std::move(aec_dump_);
1788 }
1789}
1790
Sam Zackrisson4d364492018-03-02 16:03:21 +01001791void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1792 std::unique_ptr<AudioGenerator> audio_generator) {
1793 // TODO(bugs.webrtc.org/8882) Stub.
1794 // Reset internal audio generator with audio_generator.
1795}
1796
1797void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1798 // TODO(bugs.webrtc.org/8882) Stub.
1799 // Delete audio generator, if one is attached.
1800}
1801
Ivo Creusen56d46092017-11-24 17:29:59 +01001802AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001803 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001804 rtc::CritScope cs_capture(&crit_capture_);
1805 if (!has_remote_tracks) {
1806 return capture_.stats;
1807 }
1808 AudioProcessingStats stats = capture_.stats;
1809 EchoCancellationImpl::Metrics metrics;
1810 if (private_submodules_->echo_controller) {
1811 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1812 stats.echo_return_loss = ec_metrics.echo_return_loss;
1813 stats.echo_return_loss_enhancement =
1814 ec_metrics.echo_return_loss_enhancement;
1815 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001816 }
1817 if (config_.residual_echo_detector.enabled) {
1818 RTC_DCHECK(private_submodules_->echo_detector);
1819 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1820 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1821 stats.residual_echo_likelihood_recent_max =
1822 ed_metrics.echo_likelihood_recent_max;
1823 }
Ivo Creusenae026092017-11-20 13:07:16 +01001824 return stats;
1825}
1826
niklase@google.com470e71d2011-07-07 08:21:25 +00001827NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
Sam Zackrisson23513132019-01-11 15:10:32 +01001828 return public_submodules_->noise_suppression_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001829}
1830
peah8271d042016-11-22 07:24:52 -08001831void AudioProcessingImpl::MutateConfig(
1832 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1833 rtc::CritScope cs_render(&crit_render_);
1834 rtc::CritScope cs_capture(&crit_capture_);
1835 mutator(&config_);
1836 ApplyConfig(config_);
1837}
1838
1839AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1840 rtc::CritScope cs_render(&crit_render_);
1841 rtc::CritScope cs_capture(&crit_capture_);
1842 return config_;
1843}
1844
peah2ace3f92016-09-10 04:42:27 -07001845bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1846 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001847 config_.high_pass_filter.enabled,
Per Åhgrend547d862019-05-03 15:48:47 +02001848 !!private_submodules_->echo_cancellation,
1849 !!private_submodules_->echo_control_mobile,
ivoc9f4a4a02016-10-28 05:39:16 -07001850 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001851 public_submodules_->noise_suppression->is_enabled(),
peah2ace3f92016-09-10 04:42:27 -07001852 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001853 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001854 capture_nonlocked_.echo_controller_enabled,
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001855 config_.voice_detection.enabled,
peah2ace3f92016-09-10 04:42:27 -07001856 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001857}
1858
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001859void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001860 if (capture_.transient_suppressor_enabled) {
1861 if (!public_submodules_->transient_suppressor.get()) {
1862 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001863 }
peahdf3efa82015-11-28 12:35:15 -08001864 public_submodules_->transient_suppressor->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001865 proc_fullband_sample_rate_hz(), capture_nonlocked_.split_rate,
1866 num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001867 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001868}
1869
Per Åhgren0aefbf02019-08-23 21:29:17 +02001870void AudioProcessingImpl::InitializeHighPassFilter() {
1871 if (submodule_states_.HighPassFilteringRequired()) {
1872 private_submodules_->high_pass_filter.reset(
1873 new HighPassFilter(num_proc_channels()));
peah8271d042016-11-22 07:24:52 -08001874 } else {
Per Åhgren0aefbf02019-08-23 21:29:17 +02001875 private_submodules_->high_pass_filter.reset();
peah8271d042016-11-22 07:24:52 -08001876 }
1877}
alessiob3ec96df2017-05-22 06:57:06 -07001878
Sam Zackrisson0824c6f2019-10-07 14:03:56 +02001879void AudioProcessingImpl::InitializeVoiceDetector() {
1880 if (config_.voice_detection.enabled) {
1881 private_submodules_->voice_detector = std::make_unique<VoiceDetection>(
1882 proc_split_sample_rate_hz(), VoiceDetection::kVeryLowLikelihood);
1883 } else {
1884 private_submodules_->voice_detector.reset();
1885 }
1886}
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001887void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001888 bool use_echo_controller =
1889 echo_control_factory_ ||
Per Åhgren200feba2019-03-06 04:16:46 +01001890 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode &&
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001891 !config_.echo_canceller.use_legacy_aec);
1892
1893 if (use_echo_controller) {
1894 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001895 if (echo_control_factory_) {
1896 private_submodules_->echo_controller =
1897 echo_control_factory_->Create(proc_sample_rate_hz());
1898 } else {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001899 private_submodules_->echo_controller = std::make_unique<EchoCanceller3>(
Sam Zackrissonfeee1e42019-09-20 07:50:35 +02001900 EchoCanceller3Config(), proc_sample_rate_hz(), num_reverse_channels(),
1901 num_proc_channels());
Per Åhgren200feba2019-03-06 04:16:46 +01001902 }
1903
1904 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001905
Per Åhgrenf204faf2019-04-25 15:18:06 +02001906 private_submodules_->echo_cancellation.reset();
1907 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001908 private_submodules_->echo_control_mobile.reset();
1909 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001910 return;
peahe0eae3c2016-12-14 01:16:23 -08001911 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001912
1913 private_submodules_->echo_controller.reset();
1914 capture_nonlocked_.echo_controller_enabled = false;
1915
1916 if (!config_.echo_canceller.enabled) {
1917 private_submodules_->echo_cancellation.reset();
1918 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001919 private_submodules_->echo_control_mobile.reset();
1920 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001921 return;
1922 }
1923
1924 if (config_.echo_canceller.mobile_mode) {
1925 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001926 size_t max_element_size =
1927 std::max(static_cast<size_t>(1),
1928 kMaxAllowedValuesOfSamplesPerBand *
1929 EchoControlMobileImpl::NumCancellersRequired(
1930 num_output_channels(), num_reverse_channels()));
1931
1932 std::vector<int16_t> template_queue_element(max_element_size);
1933
1934 aecm_render_signal_queue_.reset(
1935 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1936 kMaxNumFramesToBuffer, template_queue_element,
1937 RenderQueueItemVerifier<int16_t>(max_element_size)));
1938
1939 aecm_render_queue_buffer_.resize(max_element_size);
1940 aecm_capture_queue_buffer_.resize(max_element_size);
1941
1942 private_submodules_->echo_control_mobile.reset(new EchoControlMobileImpl());
1943
1944 private_submodules_->echo_control_mobile->Initialize(
1945 proc_split_sample_rate_hz(), num_reverse_channels(),
1946 num_output_channels());
1947
Per Åhgrenf204faf2019-04-25 15:18:06 +02001948 private_submodules_->echo_cancellation.reset();
1949 aec_render_signal_queue_.reset();
1950 return;
1951 }
1952
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001953 private_submodules_->echo_control_mobile.reset();
1954 aecm_render_signal_queue_.reset();
1955
Per Åhgrenf204faf2019-04-25 15:18:06 +02001956 // Create and activate AEC2.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001957 private_submodules_->echo_cancellation.reset(new EchoCancellationImpl());
1958 private_submodules_->echo_cancellation->SetExtraOptions(
1959 capture_nonlocked_.use_aec2_extended_filter,
1960 capture_nonlocked_.use_aec2_delay_agnostic,
1961 capture_nonlocked_.use_aec2_refined_adaptive_filter);
1962
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001963 size_t element_max_size =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001964 std::max(static_cast<size_t>(1),
1965 kMaxAllowedValuesOfSamplesPerBand *
1966 EchoCancellationImpl::NumCancellersRequired(
1967 num_output_channels(), num_reverse_channels()));
1968
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001969 std::vector<float> template_queue_element(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001970
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001971 aec_render_signal_queue_.reset(
1972 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1973 kMaxNumFramesToBuffer, template_queue_element,
1974 RenderQueueItemVerifier<float>(element_max_size)));
Per Åhgrenf204faf2019-04-25 15:18:06 +02001975
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001976 aec_render_queue_buffer_.resize(element_max_size);
1977 aec_capture_queue_buffer_.resize(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001978
1979 private_submodules_->echo_cancellation->Initialize(
1980 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
1981 num_proc_channels());
1982
Per Åhgrenf204faf2019-04-25 15:18:06 +02001983 private_submodules_->echo_cancellation->set_suppression_level(
1984 config_.echo_canceller.legacy_moderate_suppression_level
1985 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
1986 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
peahe0eae3c2016-12-14 01:16:23 -08001987}
peah8271d042016-11-22 07:24:52 -08001988
alessiob3ec96df2017-05-22 06:57:06 -07001989void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001990 if (config_.gain_controller2.enabled) {
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02001991 private_submodules_->gain_controller2->Initialize(
1992 proc_fullband_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001993 }
1994}
1995
Alex Loikob5c9a792018-04-16 16:31:22 +02001996void AudioProcessingImpl::InitializePreAmplifier() {
1997 if (config_.pre_amplifier.enabled) {
1998 private_submodules_->pre_amplifier.reset(
1999 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
2000 } else {
2001 private_submodules_->pre_amplifier.reset();
2002 }
2003}
2004
ivoc9f4a4a02016-10-28 05:39:16 -07002005void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01002006 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01002007 private_submodules_->echo_detector->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02002008 proc_fullband_sample_rate_hz(), 1,
Ivo Creusenb1facc12018-04-12 16:15:58 +02002009 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07002010}
2011
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002012void AudioProcessingImpl::InitializeAnalyzer() {
2013 if (private_submodules_->capture_analyzer) {
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02002014 private_submodules_->capture_analyzer->Initialize(
2015 proc_fullband_sample_rate_hz(), num_proc_channels());
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02002016 }
2017}
2018
Sam Zackrisson0beac582017-09-25 12:04:02 +02002019void AudioProcessingImpl::InitializePostProcessor() {
2020 if (private_submodules_->capture_post_processor) {
2021 private_submodules_->capture_post_processor->Initialize(
Gustaf Ullberg422b9e02019-10-09 13:02:14 +02002022 proc_fullband_sample_rate_hz(), num_proc_channels());
Sam Zackrisson0beac582017-09-25 12:04:02 +02002023 }
2024}
2025
Alex Loiko5825aa62017-12-18 16:02:40 +01002026void AudioProcessingImpl::InitializePreProcessor() {
2027 if (private_submodules_->render_pre_processor) {
2028 private_submodules_->render_pre_processor->Initialize(
2029 formats_.render_processing_format.sample_rate_hz(),
2030 formats_.render_processing_format.num_channels());
2031 }
2032}
2033
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002034void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02002035
aleloi868f32f2017-05-23 07:20:05 -07002036void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
2037 if (!aec_dump_) {
2038 return;
2039 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02002040
2041 std::string experiments_description = "";
2042 if (private_submodules_->echo_cancellation) {
2043 experiments_description +=
2044 private_submodules_->echo_cancellation->GetExperimentsDescription();
2045 }
aleloi868f32f2017-05-23 07:20:05 -07002046 // TODO(peah): Add semicolon-separated concatenations of experiment
2047 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07002048 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
2049 experiments_description += "AgcClippingLevelExperiment;";
2050 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02002051 if (capture_nonlocked_.echo_controller_enabled) {
2052 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07002053 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02002054 if (config_.gain_controller2.enabled) {
2055 experiments_description += "GainController2;";
2056 }
aleloi868f32f2017-05-23 07:20:05 -07002057
2058 InternalAPMConfig apm_config;
2059
Per Åhgren200feba2019-03-06 04:16:46 +01002060 apm_config.aec_enabled = config_.echo_canceller.enabled;
aleloi868f32f2017-05-23 07:20:05 -07002061 apm_config.aec_delay_agnostic_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002062 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002063 private_submodules_->echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002064 apm_config.aec_drift_compensation_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002065 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002066 private_submodules_->echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002067 apm_config.aec_extended_filter_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002068 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002069 private_submodules_->echo_cancellation->is_extended_filter_enabled();
Per Åhgrenf204faf2019-04-25 15:18:06 +02002070 apm_config.aec_suppression_level =
2071 private_submodules_->echo_cancellation
2072 ? static_cast<int>(
2073 private_submodules_->echo_cancellation->suppression_level())
2074 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002075
Per Åhgrend547d862019-05-03 15:48:47 +02002076 apm_config.aecm_enabled = !!private_submodules_->echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002077 apm_config.aecm_comfort_noise_enabled =
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002078 private_submodules_->echo_control_mobile &&
Sam Zackrissonc22f5512018-11-05 16:10:00 +01002079 private_submodules_->echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002080 apm_config.aecm_routing_mode =
2081 private_submodules_->echo_control_mobile
2082 ? static_cast<int>(
2083 private_submodules_->echo_control_mobile->routing_mode())
2084 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002085
2086 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2087 apm_config.agc_mode =
2088 static_cast<int>(public_submodules_->gain_control->mode());
2089 apm_config.agc_limiter_enabled =
2090 public_submodules_->gain_control->is_limiter_enabled();
2091 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2092
2093 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2094
2095 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2096 apm_config.ns_level =
2097 static_cast<int>(public_submodules_->noise_suppression->level());
2098
2099 apm_config.transient_suppression_enabled =
2100 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002101 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002102 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2103 apm_config.pre_amplifier_fixed_gain_factor =
2104 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002105
2106 if (!forced && apm_config == apm_config_for_aec_dump_) {
2107 return;
2108 }
2109 aec_dump_->WriteConfig(apm_config);
2110 apm_config_for_aec_dump_ = apm_config;
2111}
2112
2113void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2114 const float* const* src) {
2115 RTC_DCHECK(aec_dump_);
2116 WriteAecDumpConfigMessage(false);
2117
2118 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2119 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2120 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002121 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002122 RecordAudioProcessingState();
2123}
2124
2125void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2126 const AudioFrame& capture_frame) {
2127 RTC_DCHECK(aec_dump_);
2128 WriteAecDumpConfigMessage(false);
2129
2130 aec_dump_->AddCaptureStreamInput(capture_frame);
2131 RecordAudioProcessingState();
2132}
2133
2134void AudioProcessingImpl::RecordProcessedCaptureStream(
2135 const float* const* processed_capture_stream) {
2136 RTC_DCHECK(aec_dump_);
2137
2138 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2139 const size_t num_channels =
2140 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002141 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2142 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002143 aec_dump_->WriteCaptureStreamMessage();
2144}
2145
2146void AudioProcessingImpl::RecordProcessedCaptureStream(
2147 const AudioFrame& processed_capture_frame) {
2148 RTC_DCHECK(aec_dump_);
2149
2150 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2151 aec_dump_->WriteCaptureStreamMessage();
2152}
2153
2154void AudioProcessingImpl::RecordAudioProcessingState() {
2155 RTC_DCHECK(aec_dump_);
2156 AecDump::AudioProcessingState audio_proc_state;
2157 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2158 audio_proc_state.drift =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002159 private_submodules_->echo_cancellation
2160 ? private_submodules_->echo_cancellation->stream_drift_samples()
2161 : 0;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01002162 audio_proc_state.level = agc1()->stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002163 audio_proc_state.keypress = capture_.key_pressed;
2164 aec_dump_->AddAudioProcessingState(audio_proc_state);
2165}
2166
kwiberg83ffe452016-08-29 14:46:07 -07002167AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002168 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002169 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002170 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002171 output_will_be_muted(false),
2172 key_pressed(false),
2173 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002174 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002175 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002176 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002177 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002178 prev_pre_amp_gain(-1.f),
2179 playout_volume(-1),
2180 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002181
2182AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2183
Per Åhgrena1351272019-08-15 12:15:46 +02002184void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2185 const float* const* data,
2186 const StreamConfig& stream_config) {
2187 if (stream_config.has_keyboard()) {
2188 keyboard_data = data[stream_config.num_channels()];
2189 } else {
2190 keyboard_data = NULL;
2191 }
2192 num_keyboard_frames = stream_config.num_frames();
2193}
2194
kwiberg83ffe452016-08-29 14:46:07 -07002195AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2196
2197AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2198
niklase@google.com470e71d2011-07-07 08:21:25 +00002199} // namespace webrtc