blob: cdc37c698a892c573c432c1bbbf7f21d8338d036 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org40654032012-01-30 20:51:15 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/audio_processing_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Michael Graczyk86c6d332015-07-23 11:41:39 -070013#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <cstdint>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020015#include <memory>
alessiob3ec96df2017-05-22 06:57:06 -070016#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020017#include <type_traits>
18#include <utility>
niklase@google.com470e71d2011-07-07 08:21:25 +000019
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "absl/types/optional.h"
21#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_audio/audio_converter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_audio/include/audio_util.h"
Per Åhgren200feba2019-03-06 04:16:46 +010024#include "modules/audio_processing/aec3/echo_canceller3.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/agc/agc_manager_direct.h"
Alex Loikob5c9a792018-04-16 16:31:22 +020026#include "modules/audio_processing/agc2/gain_applier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/common.h"
29#include "modules/audio_processing/echo_cancellation_impl.h"
30#include "modules/audio_processing/echo_control_mobile_impl.h"
Sam Zackrissonf0d1c032019-03-27 13:28:08 +010031#include "modules/audio_processing/gain_control_config_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_processing/gain_control_for_experimental_agc.h"
33#include "modules/audio_processing/gain_control_impl.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010034#include "modules/audio_processing/gain_controller2.h"
Per Åhgren0aefbf02019-08-23 21:29:17 +020035#include "modules/audio_processing/high_pass_filter.h"
Yves Gerey988cc082018-10-23 12:03:01 +020036#include "modules/audio_processing/include/audio_frame_view.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020037#include "modules/audio_processing/level_estimator_impl.h"
Per Åhgren13735822018-02-12 21:42:56 +010038#include "modules/audio_processing/logging/apm_data_dumper.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020039#include "modules/audio_processing/noise_suppression_impl.h"
Sam Zackrisson23513132019-01-11 15:10:32 +010040#include "modules/audio_processing/noise_suppression_proxy.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020041#include "modules/audio_processing/residual_echo_detector.h"
42#include "modules/audio_processing/transient/transient_suppressor.h"
43#include "modules/audio_processing/voice_detection_impl.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080048#include "rtc_base/ref_counted_object.h"
49#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "rtc_base/trace_event.h"
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,
peah2ace3f92016-09-10 04:42:27 -0700167 bool voice_activity_detector_enabled,
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100168 bool private_voice_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700169 bool level_estimator_enabled,
170 bool transient_suppressor_enabled) {
171 bool changed = false;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200172 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700173 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
174 changed |=
175 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700176 changed |=
177 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700178 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
179 changed |=
peah2ace3f92016-09-10 04:42:27 -0700180 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200181 changed |= (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200182 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200183 changed |= (echo_controller_enabled != echo_controller_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700184 changed |= (level_estimator_enabled != level_estimator_enabled_);
185 changed |=
186 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100187 changed |=
188 (private_voice_detector_enabled != private_voice_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700189 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
190 if (changed) {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200191 high_pass_filter_enabled_ = high_pass_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700192 echo_canceller_enabled_ = echo_canceller_enabled;
193 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700194 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700195 noise_suppressor_enabled_ = noise_suppressor_enabled;
peah2ace3f92016-09-10 04:42:27 -0700196 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700197 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200198 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200199 echo_controller_enabled_ = echo_controller_enabled;
peah2ace3f92016-09-10 04:42:27 -0700200 level_estimator_enabled_ = level_estimator_enabled;
201 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100202 private_voice_detector_enabled_ = private_voice_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700203 transient_suppressor_enabled_ = transient_suppressor_enabled;
204 }
205
206 changed |= first_update_;
207 first_update_ = false;
208 return changed;
209}
210
211bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
212 const {
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100213 return CaptureMultiBandProcessingActive() ||
214 voice_activity_detector_enabled_ || private_voice_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700215}
216
217bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
218 const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200219 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700220 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200221 adaptive_gain_controller_enabled_ || echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700222}
223
peah23ac8b42017-05-23 05:33:56 -0700224bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
225 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200226 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
227 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700228}
229
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200230bool AudioProcessingImpl::ApmSubmoduleStates::CaptureAnalyzerActive() const {
231 return capture_analyzer_enabled_;
232}
233
peah2ace3f92016-09-10 04:42:27 -0700234bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
235 const {
236 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800237 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200238 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700239}
240
Alex Loiko5825aa62017-12-18 16:02:40 +0100241bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive()
242 const {
243 return render_pre_processor_enabled_;
244}
245
peah2ace3f92016-09-10 04:42:27 -0700246bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
247 const {
peah2ace3f92016-09-10 04:42:27 -0700248 return false;
peah2ace3f92016-09-10 04:42:27 -0700249}
250
Per Åhgren0aefbf02019-08-23 21:29:17 +0200251bool AudioProcessingImpl::ApmSubmoduleStates::HighPassFilteringRequired()
252 const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200253 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
254 mobile_echo_controller_enabled_ || noise_suppressor_enabled_;
255}
256
solenberg5e465c32015-12-08 13:22:33 -0800257struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800258 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800259 // Accessed externally of APM without any lock acquired.
Sam Zackrisson23513132019-01-11 15:10:32 +0100260 // TODO(bugs.webrtc.org/9947): Move these submodules into private_submodules_
261 // when their pointer-to-submodule API functions are gone.
kwiberg88788ad2016-02-19 07:04:49 -0800262 std::unique_ptr<LevelEstimatorImpl> level_estimator;
263 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
Sam Zackrisson23513132019-01-11 15:10:32 +0100264 std::unique_ptr<NoiseSuppressionProxy> noise_suppression_proxy;
kwiberg88788ad2016-02-19 07:04:49 -0800265 std::unique_ptr<VoiceDetectionImpl> voice_detection;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100266 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800267 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800268 gain_control_for_experimental_agc;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100269 std::unique_ptr<GainControlConfigProxy> gain_control_config_proxy;
solenberg5e465c32015-12-08 13:22:33 -0800270
271 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800272 std::unique_ptr<TransientSuppressor> transient_suppressor;
solenberg5e465c32015-12-08 13:22:33 -0800273};
274
275struct AudioProcessingImpl::ApmPrivateSubmodules {
Sam Zackrissondb389722018-06-21 10:12:24 +0200276 ApmPrivateSubmodules(std::unique_ptr<CustomProcessing> capture_post_processor,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100277 std::unique_ptr<CustomProcessing> render_pre_processor,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200278 rtc::scoped_refptr<EchoDetector> echo_detector,
279 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Sam Zackrissondb389722018-06-21 10:12:24 +0200280 : echo_detector(std::move(echo_detector)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100281 capture_post_processor(std::move(capture_post_processor)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200282 render_pre_processor(std::move(render_pre_processor)),
283 capture_analyzer(std::move(capture_analyzer)) {}
solenberg5e465c32015-12-08 13:22:33 -0800284 // Accessed internally from capture or during initialization
kwiberg88788ad2016-02-19 07:04:49 -0800285 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700286 std::unique_ptr<GainController2> gain_controller2;
Per Åhgren0aefbf02019-08-23 21:29:17 +0200287 std::unique_ptr<HighPassFilter> high_pass_filter;
Ivo Creusend1f970d2018-06-14 11:02:03 +0200288 rtc::scoped_refptr<EchoDetector> echo_detector;
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100289 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
Sam Zackrissonc22f5512018-11-05 16:10:00 +0100290 std::unique_ptr<EchoControl> echo_controller;
291 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
Alex Loiko5825aa62017-12-18 16:02:40 +0100292 std::unique_ptr<CustomProcessing> capture_post_processor;
293 std::unique_ptr<CustomProcessing> render_pre_processor;
Alex Loikob5c9a792018-04-16 16:31:22 +0200294 std::unique_ptr<GainApplier> pre_amplifier;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200295 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100296 std::unique_ptr<LevelEstimatorImpl> output_level_estimator;
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100297 std::unique_ptr<VoiceDetectionImpl> voice_detector;
solenberg5e465c32015-12-08 13:22:33 -0800298};
299
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100300AudioProcessingBuilder::AudioProcessingBuilder() = default;
301AudioProcessingBuilder::~AudioProcessingBuilder() = default;
302
303AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
304 std::unique_ptr<CustomProcessing> capture_post_processing) {
305 capture_post_processing_ = std::move(capture_post_processing);
306 return *this;
307}
308
309AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
310 std::unique_ptr<CustomProcessing> render_pre_processing) {
311 render_pre_processing_ = std::move(render_pre_processing);
312 return *this;
313}
314
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200315AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
316 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
317 capture_analyzer_ = std::move(capture_analyzer);
318 return *this;
319}
320
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100321AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
322 std::unique_ptr<EchoControlFactory> echo_control_factory) {
323 echo_control_factory_ = std::move(echo_control_factory);
324 return *this;
325}
326
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100327AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200328 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100329 echo_detector_ = std::move(echo_detector);
330 return *this;
331}
332
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100333AudioProcessing* AudioProcessingBuilder::Create() {
334 webrtc::Config config;
335 return Create(config);
336}
337
338AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100339 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
340 config, std::move(capture_post_processing_),
341 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200342 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100343 if (apm->Initialize() != AudioProcessing::kNoError) {
344 delete apm;
345 apm = nullptr;
346 }
347 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100348}
349
peah88ac8532016-09-12 16:47:25 -0700350AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200351 : AudioProcessingImpl(config, nullptr, nullptr, nullptr, nullptr, nullptr) {
352}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000353
Per Åhgren13735822018-02-12 21:42:56 +0100354int AudioProcessingImpl::instance_count_ = 0;
355
Sam Zackrisson0beac582017-09-25 12:04:02 +0200356AudioProcessingImpl::AudioProcessingImpl(
357 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100358 std::unique_ptr<CustomProcessing> capture_post_processor,
359 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200360 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200361 rtc::scoped_refptr<EchoDetector> echo_detector,
362 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100363 : data_dumper_(
364 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200365 capture_runtime_settings_(kRuntimeSettingQueueSize),
366 render_runtime_settings_(kRuntimeSettingQueueSize),
367 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
368 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200369 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200370 submodule_states_(!!capture_post_processor,
371 !!render_pre_processor,
372 !!capture_analyzer),
peah8271d042016-11-22 07:24:52 -0800373 public_submodules_(new ApmPublicSubmodules()),
Sam Zackrisson0beac582017-09-25 12:04:02 +0200374 private_submodules_(
Sam Zackrissondb389722018-06-21 10:12:24 +0200375 new ApmPrivateSubmodules(std::move(capture_post_processor),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100376 std::move(render_pre_processor),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200377 std::move(echo_detector),
378 std::move(capture_analyzer))),
peahdf3efa82015-11-28 12:35:15 -0800379 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800380 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000381#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alex Loikod9342442018-09-10 13:59:41 +0200382 /* enabled= */ false,
383 /* enabled_agc2_level_estimator= */ false,
384 /* digital_adaptive_disabled= */ false,
385 /* analyze_before_aec= */ false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000386#else
Alex Loiko64cb83b2018-07-02 13:38:19 +0200387 config.Get<ExperimentalAgc>().enabled,
388 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
Alex Loikod9342442018-09-10 13:59:41 +0200389 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
390 config.Get<ExperimentalAgc>().analyze_before_aec),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000391#endif
andrew1c7075f2015-06-24 18:14:14 -0700392#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200393 capture_(false),
andrew1c7075f2015-06-24 18:14:14 -0700394#else
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200395 capture_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700396#endif
Alessio Bazzicacc22f512018-08-30 13:01:34 +0200397 capture_nonlocked_() {
Sam Zackrisson421c8592019-02-11 13:39:46 +0100398 // Mark Echo Controller enabled if a factory is injected.
399 capture_nonlocked_.echo_controller_enabled =
400 static_cast<bool>(echo_control_factory_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000401
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100402 public_submodules_->gain_control.reset(new GainControlImpl());
Sam Zackrisson421c8592019-02-11 13:39:46 +0100403 public_submodules_->level_estimator.reset(
404 new LevelEstimatorImpl(&crit_capture_));
405 public_submodules_->noise_suppression.reset(
406 new NoiseSuppressionImpl(&crit_capture_));
407 public_submodules_->noise_suppression_proxy.reset(new NoiseSuppressionProxy(
408 this, public_submodules_->noise_suppression.get()));
409 public_submodules_->voice_detection.reset(
410 new VoiceDetectionImpl(&crit_capture_));
411 public_submodules_->gain_control_for_experimental_agc.reset(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100412 new GainControlForExperimentalAgc(
413 public_submodules_->gain_control.get()));
414 public_submodules_->gain_control_config_proxy.reset(
415 new GainControlConfigProxy(&crit_capture_, this, agc1()));
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200416
Sam Zackrisson421c8592019-02-11 13:39:46 +0100417 // If no echo detector is injected, use the ResidualEchoDetector.
418 if (!private_submodules_->echo_detector) {
419 private_submodules_->echo_detector =
420 new rtc::RefCountedObject<ResidualEchoDetector>();
peahdf3efa82015-11-28 12:35:15 -0800421 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000422
Sam Zackrisson421c8592019-02-11 13:39:46 +0100423 // TODO(alessiob): Move the injected gain controller once injection is
424 // implemented.
425 private_submodules_->gain_controller2.reset(new GainController2());
426
427 RTC_LOG(LS_INFO) << "Capture analyzer activated: "
428 << !!private_submodules_->capture_analyzer
429 << "\nCapture post processor activated: "
430 << !!private_submodules_->capture_post_processor
431 << "\nRender pre processor activated: "
432 << !!private_submodules_->render_pre_processor;
433
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000434 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000435}
436
437AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800438 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800439 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800440 private_submodules_->agc_manager.reset();
441 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800442 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000443}
444
niklase@google.com470e71d2011-07-07 08:21:25 +0000445int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800446 // Run in a single-threaded manner during initialization.
447 rtc::CritScope cs_render(&crit_render_);
448 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000449 return InitializeLocked();
450}
451
peahde65ddc2016-09-16 15:02:15 -0700452int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
453 int capture_output_sample_rate_hz,
454 int render_input_sample_rate_hz,
455 ChannelLayout capture_input_layout,
456 ChannelLayout capture_output_layout,
457 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700458 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700459 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
460 LayoutHasKeyboard(capture_input_layout)},
461 {capture_output_sample_rate_hz,
462 ChannelsFromLayout(capture_output_layout),
463 LayoutHasKeyboard(capture_output_layout)},
464 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
465 LayoutHasKeyboard(render_input_layout)},
466 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
467 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700468
469 return Initialize(processing_config);
470}
471
472int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800473 // Run in a single-threaded manner during initialization.
474 rtc::CritScope cs_render(&crit_render_);
475 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700476 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000477}
478
peahdf3efa82015-11-28 12:35:15 -0800479int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800480 const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800481 // Called from both threads. Thread check is therefore not possible.
Oskar Sundbom4b276482019-05-23 14:28:00 +0200482 if (processing_config == formats_.api_format) {
peah192164e2015-11-17 02:16:45 -0800483 return kNoError;
484 }
peahdf3efa82015-11-28 12:35:15 -0800485
486 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800487 return InitializeLocked(processing_config);
488}
489
niklase@google.com470e71d2011-07-07 08:21:25 +0000490int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200491 UpdateActiveSubmoduleStates();
492
Per Åhgrend47941e2019-08-22 11:51:13 +0200493 const int render_audiobuffer_sample_rate_hz =
peahdf3efa82015-11-28 12:35:15 -0800494 formats_.api_format.reverse_output_stream().num_frames() == 0
Per Åhgrend47941e2019-08-22 11:51:13 +0200495 ? formats_.render_processing_format.sample_rate_hz()
496 : formats_.api_format.reverse_output_stream().sample_rate_hz();
peahdf3efa82015-11-28 12:35:15 -0800497 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
498 render_.render_audio.reset(new AudioBuffer(
Per Åhgrend47941e2019-08-22 11:51:13 +0200499 formats_.api_format.reverse_input_stream().sample_rate_hz(),
peahdf3efa82015-11-28 12:35:15 -0800500 formats_.api_format.reverse_input_stream().num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200501 formats_.render_processing_format.sample_rate_hz(),
peahde65ddc2016-09-16 15:02:15 -0700502 formats_.render_processing_format.num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200503 render_audiobuffer_sample_rate_hz,
504 formats_.render_processing_format.num_channels()));
peah2ace3f92016-09-10 04:42:27 -0700505 if (formats_.api_format.reverse_input_stream() !=
506 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800507 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800508 formats_.api_format.reverse_input_stream().num_channels(),
509 formats_.api_format.reverse_input_stream().num_frames(),
510 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800511 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700512 } else {
peahdf3efa82015-11-28 12:35:15 -0800513 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700514 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700515 } else {
peahdf3efa82015-11-28 12:35:15 -0800516 render_.render_audio.reset(nullptr);
517 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700518 }
peahce4d9152017-05-19 01:28:05 -0700519
Per Åhgrend47941e2019-08-22 11:51:13 +0200520 capture_.capture_audio.reset(new AudioBuffer(
521 formats_.api_format.input_stream().sample_rate_hz(),
522 formats_.api_format.input_stream().num_channels(),
523 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
524 formats_.api_format.output_stream().num_channels(),
525 formats_.api_format.output_stream().sample_rate_hz(),
526 formats_.api_format.output_stream().num_channels()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000527
peah764e3642016-10-22 05:04:30 -0700528 AllocateRenderQueue();
529
peah135259a2016-10-28 03:12:11 -0700530 public_submodules_->gain_control->Initialize(num_proc_channels(),
531 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700532 if (constants_.use_experimental_agc) {
533 if (!private_submodules_->agc_manager.get()) {
534 private_submodules_->agc_manager.reset(new AgcManagerDirect(
535 public_submodules_->gain_control.get(),
536 public_submodules_->gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200537 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
538 constants_.use_experimental_agc_agc2_level_estimation,
539 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700540 }
541 private_submodules_->agc_manager->Initialize();
542 private_submodules_->agc_manager->SetCaptureMuted(
543 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700544 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700545 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200546 InitializeTransient();
Per Åhgren0aefbf02019-08-23 21:29:17 +0200547 InitializeHighPassFilter();
peahde65ddc2016-09-16 15:02:15 -0700548 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
549 proc_sample_rate_hz());
550 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100551 if (private_submodules_->voice_detector) {
552 private_submodules_->voice_detector->Initialize(
553 proc_split_sample_rate_hz());
554 }
peahde65ddc2016-09-16 15:02:15 -0700555 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700556 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200557 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700558 InitializeGainController2();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200559 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200560 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100561 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800562
aleloi868f32f2017-05-23 07:20:05 -0700563 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200564 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700565 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000566 return kNoError;
567}
568
Michael Graczyk86c6d332015-07-23 11:41:39 -0700569int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200570 UpdateActiveSubmoduleStates();
571
Michael Graczyk86c6d332015-07-23 11:41:39 -0700572 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700573 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
574 return kBadSampleRateError;
575 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000576 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700577
Peter Kasting69558702016-01-12 16:26:35 -0800578 const size_t num_in_channels = config.input_stream().num_channels();
579 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700580
581 // Need at least one input channel.
582 // Need either one output channel or as many outputs as there are inputs.
583 if (num_in_channels == 0 ||
584 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700585 return kBadNumberChannelsError;
586 }
587
peahdf3efa82015-11-28 12:35:15 -0800588 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000589
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200590 // Choose maximum rate to use for the split filtering.
591 RTC_DCHECK(config_.pipeline.maximum_internal_processing_rate == 48000 ||
592 config_.pipeline.maximum_internal_processing_rate == 32000);
593 int max_splitting_rate = 48000;
594 if (config_.pipeline.maximum_internal_processing_rate == 32000) {
595 max_splitting_rate = config_.pipeline.maximum_internal_processing_rate;
596 }
597
Per Åhgrenc8626b62019-08-23 15:49:51 +0200598 int capture_processing_rate = SuitableProcessRate(
peah423d2362016-04-09 16:06:52 -0700599 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700600 formats_.api_format.output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200601 max_splitting_rate,
peah2ace3f92016-09-10 04:42:27 -0700602 submodule_states_.CaptureMultiBandSubModulesActive() ||
603 submodule_states_.RenderMultiBandSubModulesActive());
Per Åhgrenc8626b62019-08-23 15:49:51 +0200604 RTC_DCHECK_NE(8000, capture_processing_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000605
peahde65ddc2016-09-16 15:02:15 -0700606 capture_nonlocked_.capture_processing_format =
607 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700608
peah2ce640f2017-04-07 03:57:48 -0700609 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200610 if (!capture_nonlocked_.echo_controller_enabled) {
Per Åhgrenc8626b62019-08-23 15:49:51 +0200611 render_processing_rate = SuitableProcessRate(
peah2ce640f2017-04-07 03:57:48 -0700612 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
613 formats_.api_format.reverse_output_stream().sample_rate_hz()),
Per Åhgrenfcbe4072019-09-15 00:27:58 +0200614 max_splitting_rate,
peah2ce640f2017-04-07 03:57:48 -0700615 submodule_states_.CaptureMultiBandSubModulesActive() ||
616 submodule_states_.RenderMultiBandSubModulesActive());
617 } else {
618 render_processing_rate = capture_processing_rate;
619 }
620
peahde65ddc2016-09-16 15:02:15 -0700621 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700622 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700623 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
624 kSampleRate8kHz) {
625 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000626 } else {
peahde65ddc2016-09-16 15:02:15 -0700627 render_processing_rate =
628 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000629 }
630
Per Åhgrenc8626b62019-08-23 15:49:51 +0200631 RTC_DCHECK_NE(8000, render_processing_rate);
632
peahde65ddc2016-09-16 15:02:15 -0700633 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000634 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700635 if (submodule_states_.RenderMultiBandSubModulesActive()) {
636 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
637 } else {
638 formats_.render_processing_format = StreamConfig(
639 formats_.api_format.reverse_input_stream().sample_rate_hz(),
640 formats_.api_format.reverse_input_stream().num_channels());
641 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000642
peahde65ddc2016-09-16 15:02:15 -0700643 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
644 kSampleRate32kHz ||
645 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
646 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800647 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000648 } else {
peahdf3efa82015-11-28 12:35:15 -0800649 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700650 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000651 }
652
653 return InitializeLocked();
654}
655
peah88ac8532016-09-12 16:47:25 -0700656void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peah88ac8532016-09-12 16:47:25 -0700657 // Run in a single-threaded manner when applying the settings.
658 rtc::CritScope cs_render(&crit_render_);
659 rtc::CritScope cs_capture(&crit_capture_);
660
Per Åhgren200feba2019-03-06 04:16:46 +0100661 const bool aec_config_changed =
662 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
663 config_.echo_canceller.use_legacy_aec !=
664 config.echo_canceller.use_legacy_aec ||
665 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode ||
666 (config_.echo_canceller.enabled && config.echo_canceller.use_legacy_aec &&
667 config_.echo_canceller.legacy_moderate_suppression_level !=
668 config.echo_canceller.legacy_moderate_suppression_level);
669
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100670 const bool agc1_config_changed =
671 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
672 config_.gain_controller1.mode != config.gain_controller1.mode ||
673 config_.gain_controller1.target_level_dbfs !=
674 config.gain_controller1.target_level_dbfs ||
675 config_.gain_controller1.compression_gain_db !=
676 config.gain_controller1.compression_gain_db ||
677 config_.gain_controller1.enable_limiter !=
678 config.gain_controller1.enable_limiter ||
679 config_.gain_controller1.analog_level_minimum !=
680 config.gain_controller1.analog_level_minimum ||
681 config_.gain_controller1.analog_level_maximum !=
682 config.gain_controller1.analog_level_maximum;
683
Yves Gerey499bc6c2018-10-10 18:29:07 +0200684 config_ = config;
685
Per Åhgren200feba2019-03-06 04:16:46 +0100686 if (aec_config_changed) {
687 InitializeEchoController();
688 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200689
Sam Zackrisson23513132019-01-11 15:10:32 +0100690 public_submodules_->noise_suppression->Enable(
691 config.noise_suppression.enabled);
692 public_submodules_->noise_suppression->set_level(
693 NsConfigLevelToInterfaceLevel(config.noise_suppression.level));
694
Per Åhgren0aefbf02019-08-23 21:29:17 +0200695 InitializeHighPassFilter();
peah8271d042016-11-22 07:24:52 -0800696
Mirko Bonadei675513b2017-11-09 11:09:25 +0100697 RTC_LOG(LS_INFO) << "Highpass filter activated: "
698 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800699
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100700 if (agc1_config_changed) {
701 ApplyAgc1Config(config_.gain_controller1);
702 }
703
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100704 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700705 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100706 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
707 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100708 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100709 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700710 config_.gain_controller2 = AudioProcessing::Config::GainController2();
711 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200712 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200713 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200714 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100715 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
716 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200717 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
718 << config_.pre_amplifier.enabled;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100719
720 if (config_.level_estimation.enabled &&
721 !private_submodules_->output_level_estimator) {
722 private_submodules_->output_level_estimator.reset(
723 new LevelEstimatorImpl(&crit_capture_));
724 private_submodules_->output_level_estimator->Enable(true);
725 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100726
727 if (config_.voice_detection.enabled && !private_submodules_->voice_detector) {
728 private_submodules_->voice_detector.reset(
729 new VoiceDetectionImpl(&crit_capture_));
730 private_submodules_->voice_detector->Enable(true);
731 private_submodules_->voice_detector->set_likelihood(
732 VoiceDetection::kVeryLowLikelihood);
733 private_submodules_->voice_detector->Initialize(
734 proc_split_sample_rate_hz());
735 }
peah88ac8532016-09-12 16:47:25 -0700736}
737
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100738void AudioProcessingImpl::ApplyAgc1Config(
739 const Config::GainController1& config) {
740 GainControl* agc = agc1();
741 int error = agc->Enable(config.enabled);
742 RTC_DCHECK_EQ(kNoError, error);
743 error = agc->set_mode(Agc1ConfigModeToInterfaceMode(config.mode));
744 RTC_DCHECK_EQ(kNoError, error);
745 error = agc->set_target_level_dbfs(config.target_level_dbfs);
746 RTC_DCHECK_EQ(kNoError, error);
747 error = agc->set_compression_gain_db(config.compression_gain_db);
748 RTC_DCHECK_EQ(kNoError, error);
749 error = agc->enable_limiter(config.enable_limiter);
750 RTC_DCHECK_EQ(kNoError, error);
751 error = agc->set_analog_level_limits(config.analog_level_minimum,
752 config.analog_level_maximum);
753 RTC_DCHECK_EQ(kNoError, error);
754}
755
756GainControl* AudioProcessingImpl::agc1() {
757 if (constants_.use_experimental_agc) {
758 return public_submodules_->gain_control_for_experimental_agc.get();
759 }
760 return public_submodules_->gain_control.get();
761}
762
763const GainControl* AudioProcessingImpl::agc1() const {
764 if (constants_.use_experimental_agc) {
765 return public_submodules_->gain_control_for_experimental_agc.get();
766 }
767 return public_submodules_->gain_control.get();
768}
769
peah88ac8532016-09-12 16:47:25 -0700770void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800771 // Run in a single-threaded manner when setting the extra options.
772 rtc::CritScope cs_render(&crit_render_);
773 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000774
Per Åhgrenf204faf2019-04-25 15:18:06 +0200775 capture_nonlocked_.use_aec2_extended_filter =
776 config.Get<ExtendedFilter>().enabled;
777 capture_nonlocked_.use_aec2_delay_agnostic =
778 config.Get<DelayAgnostic>().enabled;
779 capture_nonlocked_.use_aec2_refined_adaptive_filter =
780 config.Get<RefinedAdaptiveFilter>().enabled;
peahb624d8c2016-03-05 03:01:14 -0800781
peahdf3efa82015-11-28 12:35:15 -0800782 if (capture_.transient_suppressor_enabled !=
783 config.Get<ExperimentalNs>().enabled) {
784 capture_.transient_suppressor_enabled =
785 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000786 InitializeTransient();
787 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000788}
789
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000790int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800791 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700792 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000793}
794
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000795int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800796 // Used as callback from submodules, hence locking is not allowed.
797 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000798}
799
Peter Kasting69558702016-01-12 16:26:35 -0800800size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800801 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700802 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000803}
804
Peter Kasting69558702016-01-12 16:26:35 -0800805size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800806 // Used as callback from submodules, hence locking is not allowed.
807 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000808}
809
Peter Kasting69558702016-01-12 16:26:35 -0800810size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800811 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200812 return capture_nonlocked_.echo_controller_enabled ? 1 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800813}
814
Peter Kasting69558702016-01-12 16:26:35 -0800815size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800816 // Used as callback from submodules, hence locking is not allowed.
817 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000818}
819
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000820void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800821 rtc::CritScope cs(&crit_capture_);
822 capture_.output_will_be_muted = muted;
823 if (private_submodules_->agc_manager.get()) {
824 private_submodules_->agc_manager->SetCaptureMuted(
825 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000826 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000827}
828
Alessio Bazzicac054e782018-04-16 12:10:09 +0200829void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200830 switch (setting.type()) {
831 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
832 render_runtime_settings_enqueuer_.Enqueue(setting);
833 return;
834 case RuntimeSetting::Type::kNotSpecified:
835 RTC_NOTREACHED();
836 return;
837 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100838 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200839 case RuntimeSetting::Type::kCaptureFixedPostGain:
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200840 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200841 capture_runtime_settings_enqueuer_.Enqueue(setting);
842 return;
843 }
844 // The language allows the enum to have a non-enumerator
845 // value. Check that this doesn't happen.
846 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200847}
848
849AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
850 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200851 : runtime_settings_(*runtime_settings) {
852 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200853}
854
855AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
856 default;
857
858void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
859 RuntimeSetting setting) {
860 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200861 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200862 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200863 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200864 RTC_LOG(LS_ERROR)
865 << "The runtime settings queue is full. Oldest setting discarded.";
866 }
867 if (remaining_attempts == 0)
868 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
869}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000870
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000871int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700872 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000873 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000874 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000875 int output_sample_rate_hz,
876 ChannelLayout output_layout,
877 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800878 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800879 StreamConfig input_stream;
880 StreamConfig output_stream;
881 {
882 // Access the formats_.api_format.input_stream beneath the capture lock.
883 // The lock must be released as it is later required in the call
884 // to ProcessStream(,,,);
885 rtc::CritScope cs(&crit_capture_);
886 input_stream = formats_.api_format.input_stream();
887 output_stream = formats_.api_format.output_stream();
888 }
889
Michael Graczyk86c6d332015-07-23 11:41:39 -0700890 input_stream.set_sample_rate_hz(input_sample_rate_hz);
891 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
892 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700893 output_stream.set_sample_rate_hz(output_sample_rate_hz);
894 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
895 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
896
897 if (samples_per_channel != input_stream.num_frames()) {
898 return kBadDataLengthError;
899 }
900 return ProcessStream(src, input_stream, output_stream, dest);
901}
902
903int AudioProcessingImpl::ProcessStream(const float* const* src,
904 const StreamConfig& input_config,
905 const StreamConfig& output_config,
906 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800907 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800908 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700909 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800910 {
911 // Acquire the capture lock in order to safely call the function
912 // that retrieves the render side data. This function accesses apm
913 // getters that need the capture lock held when being called.
914 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700915 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800916
917 if (!src || !dest) {
918 return kNullPointerError;
919 }
920
921 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700922 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000923 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000924
Oskar Sundbom4b276482019-05-23 14:28:00 +0200925 if (processing_config.input_stream() != input_config) {
926 processing_config.input_stream() = input_config;
927 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800928 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200929
930 if (processing_config.output_stream() != output_config) {
931 processing_config.output_stream() = output_config;
932 reinitialization_required = true;
933 }
934
935 if (reinitialization_required) {
936 // Reinitialize.
937 rtc::CritScope cs_render(&crit_render_);
938 rtc::CritScope cs_capture(&crit_capture_);
939 RETURN_ON_ERR(InitializeLocked(processing_config));
940 }
941
peahdf3efa82015-11-28 12:35:15 -0800942 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700943 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
944 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000945
aleloi868f32f2017-05-23 07:20:05 -0700946 if (aec_dump_) {
947 RecordUnprocessedCaptureStream(src);
948 }
949
Per Åhgrena1351272019-08-15 12:15:46 +0200950 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800951 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700952 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800953 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000954
aleloi868f32f2017-05-23 07:20:05 -0700955 if (aec_dump_) {
956 RecordProcessedCaptureStream(dest);
957 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000958 return kNoError;
959}
960
Alex Loiko73ec0192018-05-15 10:52:28 +0200961void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200962 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200963 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200964 if (aec_dump_) {
965 aec_dump_->WriteRuntimeSetting(setting);
966 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200967 switch (setting.type()) {
968 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200969 if (config_.pre_amplifier.enabled) {
970 float value;
971 setting.GetFloat(&value);
972 private_submodules_->pre_amplifier->SetGainFactor(value);
973 }
974 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200975 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100976 case RuntimeSetting::Type::kCaptureCompressionGain: {
977 float value;
978 setting.GetFloat(&value);
979 int int_value = static_cast<int>(value + .5f);
980 config_.gain_controller1.compression_gain_db = int_value;
981 int error = agc1()->set_compression_gain_db(int_value);
982 RTC_DCHECK_EQ(kNoError, error);
983 break;
984 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200985 case RuntimeSetting::Type::kCaptureFixedPostGain: {
986 if (config_.gain_controller2.enabled) {
987 float value;
988 setting.GetFloat(&value);
989 config_.gain_controller2.fixed_digital.gain_db = value;
990 private_submodules_->gain_controller2->ApplyConfig(
991 config_.gain_controller2);
992 }
993 break;
994 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200995 case RuntimeSetting::Type::kPlayoutVolumeChange: {
996 int value;
997 setting.GetInt(&value);
998 capture_.playout_volume = value;
999 break;
1000 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001001 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1002 RTC_NOTREACHED();
1003 break;
1004 case RuntimeSetting::Type::kNotSpecified:
1005 RTC_NOTREACHED();
1006 break;
1007 }
1008 }
1009}
1010
1011void AudioProcessingImpl::HandleRenderRuntimeSettings() {
1012 RuntimeSetting setting;
1013 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +02001014 if (aec_dump_) {
1015 aec_dump_->WriteRuntimeSetting(setting);
1016 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001017 switch (setting.type()) {
1018 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1019 if (private_submodules_->render_pre_processor) {
1020 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
1021 }
1022 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001023 case RuntimeSetting::Type::kCapturePreGain: // fall-through
1024 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001025 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001026 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001027 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001028 RTC_NOTREACHED();
1029 break;
1030 }
1031 }
1032}
1033
peah9e6a2902017-05-15 07:19:21 -07001034void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001035 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001036
1037 // Insert the samples into the queue.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001038 if (private_submodules_->echo_cancellation) {
1039 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001040 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1041 num_reverse_channels(),
1042 &aec_render_queue_buffer_);
1043
Per Åhgrenf204faf2019-04-25 15:18:06 +02001044 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1045 // The data queue is full and needs to be emptied.
1046 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001047
Per Åhgrenf204faf2019-04-25 15:18:06 +02001048 // Retry the insert (should always work).
1049 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1050 RTC_DCHECK(result);
1051 }
peaha0624602016-10-25 04:45:24 -07001052 }
1053
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001054 if (private_submodules_->echo_control_mobile) {
1055 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1056 num_reverse_channels(),
1057 &aecm_render_queue_buffer_);
1058 RTC_DCHECK(aecm_render_signal_queue_);
1059 // Insert the samples into the queue.
1060 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1061 // The data queue is full and needs to be emptied.
1062 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001063
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001064 // Retry the insert (should always work).
1065 bool result =
1066 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1067 RTC_DCHECK(result);
1068 }
peah764e3642016-10-22 05:04:30 -07001069 }
peah701d6282016-10-25 05:42:20 -07001070
1071 if (!constants_.use_experimental_agc) {
1072 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1073 // Insert the samples into the queue.
1074 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1075 // The data queue is full and needs to be emptied.
1076 EmptyQueuedRenderAudio();
1077
1078 // Retry the insert (should always work).
1079 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1080 RTC_DCHECK(result);
1081 }
1082 }
peah9e6a2902017-05-15 07:19:21 -07001083}
ivoc9f4a4a02016-10-28 05:39:16 -07001084
peah9e6a2902017-05-15 07:19:21 -07001085void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001086 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1087
1088 // Insert the samples into the queue.
1089 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1090 // The data queue is full and needs to be emptied.
1091 EmptyQueuedRenderAudio();
1092
1093 // Retry the insert (should always work).
1094 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1095 RTC_DCHECK(result);
1096 }
peah764e3642016-10-22 05:04:30 -07001097}
1098
1099void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001100 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001101 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001102
ivoc9f4a4a02016-10-28 05:39:16 -07001103 const size_t new_red_render_queue_element_max_size =
1104 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1105
peaha0624602016-10-25 04:45:24 -07001106 // Reallocate the queues if the queue item sizes are too small to fit the
1107 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001108
1109 if (agc_render_queue_element_max_size_ <
1110 new_agc_render_queue_element_max_size) {
1111 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1112
1113 std::vector<int16_t> template_queue_element(
1114 agc_render_queue_element_max_size_);
1115
1116 agc_render_signal_queue_.reset(
1117 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1118 kMaxNumFramesToBuffer, template_queue_element,
1119 RenderQueueItemVerifier<int16_t>(
1120 agc_render_queue_element_max_size_)));
1121
1122 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1123 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1124 } else {
1125 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001126 }
ivoc9f4a4a02016-10-28 05:39:16 -07001127
1128 if (red_render_queue_element_max_size_ <
1129 new_red_render_queue_element_max_size) {
1130 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1131
1132 std::vector<float> template_queue_element(
1133 red_render_queue_element_max_size_);
1134
1135 red_render_signal_queue_.reset(
1136 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1137 kMaxNumFramesToBuffer, template_queue_element,
1138 RenderQueueItemVerifier<float>(
1139 red_render_queue_element_max_size_)));
1140
1141 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1142 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1143 } else {
1144 red_render_signal_queue_->Clear();
1145 }
peah764e3642016-10-22 05:04:30 -07001146}
1147
1148void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1149 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001150 if (private_submodules_->echo_cancellation) {
1151 RTC_DCHECK(aec_render_signal_queue_);
1152 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
1153 private_submodules_->echo_cancellation->ProcessRenderAudio(
1154 aec_capture_queue_buffer_);
1155 }
peaha0624602016-10-25 04:45:24 -07001156 }
1157
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001158 if (private_submodules_->echo_control_mobile) {
1159 RTC_DCHECK(aecm_render_signal_queue_);
1160 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
1161 private_submodules_->echo_control_mobile->ProcessRenderAudio(
1162 aecm_capture_queue_buffer_);
1163 }
peah701d6282016-10-25 05:42:20 -07001164 }
1165
1166 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1167 public_submodules_->gain_control->ProcessRenderAudio(
1168 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001169 }
ivoc9f4a4a02016-10-28 05:39:16 -07001170
1171 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001172 RTC_DCHECK(private_submodules_->echo_detector);
1173 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001174 red_capture_queue_buffer_);
1175 }
peah764e3642016-10-22 05:04:30 -07001176}
1177
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001178int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001179 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001180 {
1181 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001182 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001183 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001184 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001185 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001186 }
peahfa6228e2015-11-16 16:27:42 -08001187
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001188 if (!frame) {
1189 return kNullPointerError;
1190 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001191 // Must be a native rate.
1192 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1193 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001194 frame->sample_rate_hz_ != kSampleRate32kHz &&
1195 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001196 return kBadSampleRateError;
1197 }
peah192164e2015-11-17 02:16:45 -08001198
peahdf3efa82015-11-28 12:35:15 -08001199 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001200 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001201 {
1202 // Aquire lock for the access of api_format.
1203 // The lock is released immediately due to the conditional
1204 // reinitialization.
1205 rtc::CritScope cs_capture(&crit_capture_);
1206 // TODO(ajm): The input and output rates and channels are currently
1207 // constrained to be identical in the int16 interface.
1208 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001209
1210 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001211 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001212
Oskar Sundbom4b276482019-05-23 14:28:00 +02001213 reinitialization_required =
1214 reinitialization_required ||
1215 processing_config.input_stream().sample_rate_hz() !=
1216 frame->sample_rate_hz_ ||
1217 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1218 processing_config.output_stream().sample_rate_hz() !=
1219 frame->sample_rate_hz_ ||
1220 processing_config.output_stream().num_channels() != frame->num_channels_;
1221
1222 if (reinitialization_required) {
1223 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1224 processing_config.input_stream().set_num_channels(frame->num_channels_);
1225 processing_config.output_stream().set_sample_rate_hz(
1226 frame->sample_rate_hz_);
1227 processing_config.output_stream().set_num_channels(frame->num_channels_);
1228
1229 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001230 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001231 rtc::CritScope cs_capture(&crit_capture_);
1232 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001233 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001234
peahdf3efa82015-11-28 12:35:15 -08001235 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001236 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001237 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001238 return kBadDataLengthError;
1239 }
1240
aleloi868f32f2017-05-23 07:20:05 -07001241 if (aec_dump_) {
1242 RecordUnprocessedCaptureStream(*frame);
1243 }
1244
Per Åhgrena1351272019-08-15 12:15:46 +02001245 capture_.vad_activity = frame->vad_activity_;
Per Åhgrend47941e2019-08-22 11:51:13 +02001246 capture_.capture_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001247 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001248 if (submodule_states_.CaptureMultiBandProcessingActive() ||
1249 submodule_states_.CaptureFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001250 capture_.capture_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001251 }
1252 frame->vad_activity_ = capture_.vad_activity;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001253
aleloi868f32f2017-05-23 07:20:05 -07001254 if (aec_dump_) {
1255 RecordProcessedCaptureStream(*frame);
1256 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001257
1258 return kNoError;
1259}
1260
peahde65ddc2016-09-16 15:02:15 -07001261int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001262 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001263
peahb58a1582016-03-15 09:34:24 -07001264 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001265 // TODO(peah): Simplify once the public API Enable functions for these
1266 // are moved to APM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001267 RTC_DCHECK_LE(!!private_submodules_->echo_controller +
1268 !!private_submodules_->echo_cancellation +
1269 !!private_submodules_->echo_control_mobile,
1270 1);
peahb58a1582016-03-15 09:34:24 -07001271
peahde65ddc2016-09-16 15:02:15 -07001272 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001273
Alex Loikob5c9a792018-04-16 16:31:22 +02001274 if (private_submodules_->pre_amplifier) {
1275 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001276 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001277 capture_buffer->num_frames()));
1278 }
1279
Per Åhgren928146f2019-08-20 09:19:21 +02001280 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001281 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001282 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001283 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1284 if (log_rms) {
1285 capture_rms_interval_counter_ = 0;
1286 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001287 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1288 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1289 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1290 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001291 }
1292
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001293 if (private_submodules_->echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001294 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001295 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001296 capture_.echo_path_gain_change =
1297 capture_.prev_analog_mic_level != analog_mic_level &&
1298 capture_.prev_analog_mic_level != -1;
1299 capture_.prev_analog_mic_level = analog_mic_level;
1300
Per Åhgrend2650d12018-10-02 17:00:59 +02001301 // Detect and flag any change in the pre-amplifier gain.
1302 if (private_submodules_->pre_amplifier) {
1303 float pre_amp_gain = private_submodules_->pre_amplifier->GetGainFactor();
1304 capture_.echo_path_gain_change =
1305 capture_.echo_path_gain_change ||
1306 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001307 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001308 capture_.prev_pre_amp_gain = pre_amp_gain;
1309 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001310
1311 // Detect volume change.
1312 capture_.echo_path_gain_change =
1313 capture_.echo_path_gain_change ||
1314 (capture_.prev_playout_volume != capture_.playout_volume &&
1315 capture_.prev_playout_volume >= 0);
1316 capture_.prev_playout_volume = capture_.playout_volume;
1317
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001318 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001319 }
1320
peahbe615622016-02-13 16:40:47 -08001321 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001322 public_submodules_->gain_control->is_enabled()) {
1323 private_submodules_->agc_manager->AnalyzePreProcess(
Per Åhgren928146f2019-08-20 09:19:21 +02001324 capture_buffer->channels_f()[0], capture_buffer->num_channels(),
peahde65ddc2016-09-16 15:02:15 -07001325 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001326
1327 if (constants_.use_experimental_agc_process_before_aec) {
1328 private_submodules_->agc_manager->Process(
Per Åhgrend47941e2019-08-22 11:51:13 +02001329 capture_buffer->channels_const()[0],
Alex Loikod9342442018-09-10 13:59:41 +02001330 capture_nonlocked_.capture_processing_format.num_frames(),
1331 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1332 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001333 }
1334
peah2ace3f92016-09-10 04:42:27 -07001335 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1336 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001337 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1338 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001339 }
1340
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001341 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001342 // Force down-mixing of the number of channels after the detection of
1343 // capture signal saturation.
1344 // TODO(peah): Look into ensuring that this kind of tampering with the
1345 // AudioBuffer functionality should not be needed.
1346 capture_buffer->set_num_channels(1);
1347 }
1348
Per Åhgren0aefbf02019-08-23 21:29:17 +02001349 if (private_submodules_->high_pass_filter) {
1350 private_submodules_->high_pass_filter->Process(capture_buffer);
peah8271d042016-11-22 07:24:52 -08001351 }
peahde65ddc2016-09-16 15:02:15 -07001352 RETURN_ON_ERR(
1353 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1354 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001355
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001356 if (private_submodules_->echo_control_mobile) {
1357 // Ensure that the stream delay was set before the call to the
1358 // AECM ProcessCaptureAudio function.
1359 if (!was_stream_delay_set()) {
1360 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001361 }
1362
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001363 if (public_submodules_->noise_suppression->is_enabled()) {
Per Åhgrena1351272019-08-15 12:15:46 +02001364 private_submodules_->echo_control_mobile->CopyLowPassReference(
1365 capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001366 }
peahe0eae3c2016-12-14 01:16:23 -08001367
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001368 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah253534d2016-03-15 04:32:28 -07001369
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001370 RETURN_ON_ERR(private_submodules_->echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001371 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001372 } else {
1373 if (private_submodules_->echo_controller) {
1374 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1375
1376 if (was_stream_delay_set()) {
1377 private_submodules_->echo_controller->SetAudioBufferDelay(
1378 stream_delay_ms());
1379 }
1380
1381 private_submodules_->echo_controller->ProcessCapture(
1382 capture_buffer, capture_.echo_path_gain_change);
1383 } else if (private_submodules_->echo_cancellation) {
1384 // Ensure that the stream delay was set before the call to the
1385 // AEC ProcessCaptureAudio function.
1386 if (!was_stream_delay_set()) {
1387 return AudioProcessing::kStreamParameterNotSetError;
1388 }
1389
1390 RETURN_ON_ERR(private_submodules_->echo_cancellation->ProcessCaptureAudio(
1391 capture_buffer, stream_delay_ms()));
1392 }
1393
1394 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
Per Åhgren46537a32017-06-07 10:08:10 +02001395 }
ivoc9f4a4a02016-10-28 05:39:16 -07001396
Per Åhgrena1351272019-08-15 12:15:46 +02001397 if (public_submodules_->voice_detection->is_enabled() &&
1398 !public_submodules_->voice_detection->using_external_vad()) {
1399 bool voice_active =
1400 public_submodules_->voice_detection->ProcessCaptureAudio(
1401 capture_buffer);
1402 capture_.vad_activity =
1403 voice_active ? AudioFrame::kVadActive : AudioFrame::kVadPassive;
1404 }
1405
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001406 if (config_.voice_detection.enabled) {
1407 private_submodules_->voice_detector->ProcessCaptureAudio(capture_buffer);
1408 capture_.stats.voice_detected =
1409 private_submodules_->voice_detector->stream_has_voice();
1410 } else {
1411 capture_.stats.voice_detected = absl::nullopt;
1412 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001413
peahbe615622016-02-13 16:40:47 -08001414 if (constants_.use_experimental_agc &&
Alex Loikod9342442018-09-10 13:59:41 +02001415 public_submodules_->gain_control->is_enabled() &&
1416 !constants_.use_experimental_agc_process_before_aec) {
peahdf3efa82015-11-28 12:35:15 -08001417 private_submodules_->agc_manager->Process(
Per Åhgren928146f2019-08-20 09:19:21 +02001418 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
peahde65ddc2016-09-16 15:02:15 -07001419 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001420 }
Per Åhgren200feba2019-03-06 04:16:46 +01001421 // TODO(peah): Add reporting from AEC3 whether there is echo.
peahb8fbb542016-03-15 02:28:08 -07001422 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001423 capture_buffer,
Per Åhgrenf204faf2019-04-25 15:18:06 +02001424 private_submodules_->echo_cancellation &&
1425 private_submodules_->echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001426
peah2ace3f92016-09-10 04:42:27 -07001427 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1428 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001429 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1430 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001431 }
1432
peah9e6a2902017-05-15 07:19:21 -07001433 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001434 RTC_DCHECK(private_submodules_->echo_detector);
1435 private_submodules_->echo_detector->AnalyzeCaptureAudio(
Per Åhgrend47941e2019-08-22 11:51:13 +02001436 rtc::ArrayView<const float>(capture_buffer->channels()[0],
peah9e6a2902017-05-15 07:19:21 -07001437 capture_buffer->num_frames()));
1438 }
1439
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001440 // TODO(aluebs): Investigate if the transient suppression placement should be
1441 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001442 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001443 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001444 private_submodules_->agc_manager.get()
1445 ? private_submodules_->agc_manager->voice_probability()
1446 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001447
peahdf3efa82015-11-28 12:35:15 -08001448 public_submodules_->transient_suppressor->Suppress(
Per Åhgrend47941e2019-08-22 11:51:13 +02001449 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001450 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001451 capture_buffer->split_bands_const(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001452 capture_buffer->num_frames_per_band(),
1453 capture_.keyboard_info.keyboard_data,
1454 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001455 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001456 }
1457
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001458 // Experimental APM sub-module that analyzes |capture_buffer|.
1459 if (private_submodules_->capture_analyzer) {
1460 private_submodules_->capture_analyzer->Analyze(capture_buffer);
1461 }
1462
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001463 if (config_.gain_controller2.enabled) {
Alex Loikoa837dd72018-08-06 16:32:12 +02001464 private_submodules_->gain_controller2->NotifyAnalogLevel(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001465 agc1()->stream_analog_level());
alessiob3ec96df2017-05-22 06:57:06 -07001466 private_submodules_->gain_controller2->Process(capture_buffer);
1467 }
1468
Sam Zackrisson0beac582017-09-25 12:04:02 +02001469 if (private_submodules_->capture_post_processor) {
1470 private_submodules_->capture_post_processor->Process(capture_buffer);
1471 }
1472
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001473 // The level estimator operates on the recombined data.
Per Åhgrend47941e2019-08-22 11:51:13 +02001474 public_submodules_->level_estimator->ProcessStream(*capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001475 if (config_.level_estimation.enabled) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001476 private_submodules_->output_level_estimator->ProcessStream(*capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001477 capture_.stats.output_rms_dbfs =
1478 private_submodules_->output_level_estimator->RMS();
1479 } else {
1480 capture_.stats.output_rms_dbfs = absl::nullopt;
1481 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001482
Per Åhgren928146f2019-08-20 09:19:21 +02001483 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001484 capture_buffer->channels_const()[0],
peah1b08dc32016-12-20 13:45:58 -08001485 capture_nonlocked_.capture_processing_format.num_frames()));
1486 if (log_rms) {
1487 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1488 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1489 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1490 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1491 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1492 }
1493
peahdf3efa82015-11-28 12:35:15 -08001494 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001495 return kNoError;
1496}
1497
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001498int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001499 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001500 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001501 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001502 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001503 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001504 const StreamConfig reverse_config = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001505 sample_rate_hz,
1506 ChannelsFromLayout(layout),
1507 LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001508 };
1509 if (samples_per_channel != reverse_config.num_frames()) {
1510 return kBadDataLengthError;
1511 }
peahdf3efa82015-11-28 12:35:15 -08001512 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001513}
1514
peahde65ddc2016-09-16 15:02:15 -07001515int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1516 const StreamConfig& input_config,
1517 const StreamConfig& output_config,
1518 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001519 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001520 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001521 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001522 if (submodule_states_.RenderMultiBandProcessingActive() ||
1523 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001524 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1525 dest);
peah2ace3f92016-09-10 04:42:27 -07001526 } else if (formats_.api_format.reverse_input_stream() !=
1527 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001528 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1529 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001530 } else {
peahde65ddc2016-09-16 15:02:15 -07001531 CopyAudioIfNeeded(src, input_config.num_frames(),
1532 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001533 }
1534
1535 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001536}
1537
peahdf3efa82015-11-28 12:35:15 -08001538int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001539 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001540 const StreamConfig& input_config,
1541 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001542 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001543 return kNullPointerError;
1544 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001545
peahde65ddc2016-09-16 15:02:15 -07001546 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001547 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001548 }
1549
peahdf3efa82015-11-28 12:35:15 -08001550 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001551 processing_config.reverse_input_stream() = input_config;
1552 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001553
peahdf3efa82015-11-28 12:35:15 -08001554 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001555 RTC_DCHECK_EQ(input_config.num_frames(),
1556 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001557
aleloi868f32f2017-05-23 07:20:05 -07001558 if (aec_dump_) {
1559 const size_t channel_size =
1560 formats_.api_format.reverse_input_stream().num_frames();
1561 const size_t num_channels =
1562 formats_.api_format.reverse_input_stream().num_channels();
1563 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001564 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001565 }
peahdf3efa82015-11-28 12:35:15 -08001566 render_.render_audio->CopyFrom(src,
1567 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001568 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001569}
1570
1571int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001572 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001573 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001574 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001575 return kNullPointerError;
1576 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001577 // Must be a native rate.
1578 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1579 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001580 frame->sample_rate_hz_ != kSampleRate32kHz &&
1581 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001582 return kBadSampleRateError;
1583 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001584
Michael Graczyk86c6d332015-07-23 11:41:39 -07001585 if (frame->num_channels_ <= 0) {
1586 return kBadNumberChannelsError;
1587 }
1588
peahdf3efa82015-11-28 12:35:15 -08001589 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001590 processing_config.reverse_input_stream().set_sample_rate_hz(
1591 frame->sample_rate_hz_);
1592 processing_config.reverse_input_stream().set_num_channels(
1593 frame->num_channels_);
1594 processing_config.reverse_output_stream().set_sample_rate_hz(
1595 frame->sample_rate_hz_);
1596 processing_config.reverse_output_stream().set_num_channels(
1597 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001598
peahdf3efa82015-11-28 12:35:15 -08001599 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001600 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001601 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001602 return kBadDataLengthError;
1603 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001604
aleloi868f32f2017-05-23 07:20:05 -07001605 if (aec_dump_) {
1606 aec_dump_->WriteRenderStreamMessage(*frame);
1607 }
1608
Per Åhgrend47941e2019-08-22 11:51:13 +02001609 render_.render_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001610 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001611 if (submodule_states_.RenderMultiBandProcessingActive() ||
1612 submodule_states_.RenderFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001613 render_.render_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001614 }
aluebsb0319552016-03-17 20:39:53 -07001615 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001616}
niklase@google.com470e71d2011-07-07 08:21:25 +00001617
peahde65ddc2016-09-16 15:02:15 -07001618int AudioProcessingImpl::ProcessRenderStreamLocked() {
1619 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001620
Alex Loiko73ec0192018-05-15 10:52:28 +02001621 HandleRenderRuntimeSettings();
1622
Alex Loiko5825aa62017-12-18 16:02:40 +01001623 if (private_submodules_->render_pre_processor) {
1624 private_submodules_->render_pre_processor->Process(render_buffer);
1625 }
1626
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001627 QueueNonbandedRenderAudio(render_buffer);
1628
peah2ace3f92016-09-10 04:42:27 -07001629 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001630 SampleRateSupportsMultiBand(
1631 formats_.render_processing_format.sample_rate_hz())) {
1632 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001633 }
1634
peahce4d9152017-05-19 01:28:05 -07001635 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1636 QueueBandedRenderAudio(render_buffer);
1637 }
1638
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001639 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001640 if (private_submodules_->echo_controller) {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001641 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001642 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001643
peah2ace3f92016-09-10 04:42:27 -07001644 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001645 SampleRateSupportsMultiBand(
1646 formats_.render_processing_format.sample_rate_hz())) {
1647 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001648 }
1649
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001650 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001651}
1652
1653int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001654 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001655 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001656 capture_.was_stream_delay_set = true;
1657 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001658
niklase@google.com470e71d2011-07-07 08:21:25 +00001659 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001660 delay = 0;
1661 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001662 }
1663
1664 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1665 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001666 delay = 500;
1667 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001668 }
1669
peahdf3efa82015-11-28 12:35:15 -08001670 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001671 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001672}
1673
1674int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001675 // Used as callback from submodules, hence locking is not allowed.
1676 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001677}
1678
1679bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001680 // Used as callback from submodules, hence locking is not allowed.
1681 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001682}
1683
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001684void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001685 rtc::CritScope cs(&crit_capture_);
1686 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001687}
1688
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001689void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001690 rtc::CritScope cs(&crit_capture_);
1691 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001692}
1693
1694int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001695 rtc::CritScope cs(&crit_capture_);
1696 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001697}
1698
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001699void AudioProcessingImpl::set_stream_analog_level(int level) {
1700 rtc::CritScope cs_capture(&crit_capture_);
1701 int error = agc1()->set_stream_analog_level(level);
1702 RTC_DCHECK_EQ(kNoError, error);
1703}
1704
1705int AudioProcessingImpl::recommended_stream_analog_level() const {
1706 rtc::CritScope cs_capture(&crit_capture_);
1707 return agc1()->stream_analog_level();
1708}
1709
aleloi868f32f2017-05-23 07:20:05 -07001710void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1711 RTC_DCHECK(aec_dump);
1712 rtc::CritScope cs_render(&crit_render_);
1713 rtc::CritScope cs_capture(&crit_capture_);
1714
1715 // The previously attached AecDump will be destroyed with the
1716 // 'aec_dump' parameter, which is after locks are released.
1717 aec_dump_.swap(aec_dump);
1718 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001719 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001720}
1721
1722void AudioProcessingImpl::DetachAecDump() {
1723 // The d-tor of a task-queue based AecDump blocks until all pending
1724 // tasks are done. This construction avoids blocking while holding
1725 // the render and capture locks.
1726 std::unique_ptr<AecDump> aec_dump = nullptr;
1727 {
1728 rtc::CritScope cs_render(&crit_render_);
1729 rtc::CritScope cs_capture(&crit_capture_);
1730 aec_dump = std::move(aec_dump_);
1731 }
1732}
1733
Sam Zackrisson4d364492018-03-02 16:03:21 +01001734void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1735 std::unique_ptr<AudioGenerator> audio_generator) {
1736 // TODO(bugs.webrtc.org/8882) Stub.
1737 // Reset internal audio generator with audio_generator.
1738}
1739
1740void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1741 // TODO(bugs.webrtc.org/8882) Stub.
1742 // Delete audio generator, if one is attached.
1743}
1744
Ivo Creusen56d46092017-11-24 17:29:59 +01001745AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001746 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001747 rtc::CritScope cs_capture(&crit_capture_);
1748 if (!has_remote_tracks) {
1749 return capture_.stats;
1750 }
1751 AudioProcessingStats stats = capture_.stats;
1752 EchoCancellationImpl::Metrics metrics;
1753 if (private_submodules_->echo_controller) {
1754 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1755 stats.echo_return_loss = ec_metrics.echo_return_loss;
1756 stats.echo_return_loss_enhancement =
1757 ec_metrics.echo_return_loss_enhancement;
1758 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001759 }
1760 if (config_.residual_echo_detector.enabled) {
1761 RTC_DCHECK(private_submodules_->echo_detector);
1762 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1763 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1764 stats.residual_echo_likelihood_recent_max =
1765 ed_metrics.echo_likelihood_recent_max;
1766 }
Ivo Creusenae026092017-11-20 13:07:16 +01001767 return stats;
1768}
1769
niklase@google.com470e71d2011-07-07 08:21:25 +00001770GainControl* AudioProcessingImpl::gain_control() const {
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001771 return public_submodules_->gain_control_config_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001772}
1773
niklase@google.com470e71d2011-07-07 08:21:25 +00001774LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001775 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001776}
1777
1778NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
Sam Zackrisson23513132019-01-11 15:10:32 +01001779 return public_submodules_->noise_suppression_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001780}
1781
1782VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001783 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001784}
1785
peah8271d042016-11-22 07:24:52 -08001786void AudioProcessingImpl::MutateConfig(
1787 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1788 rtc::CritScope cs_render(&crit_render_);
1789 rtc::CritScope cs_capture(&crit_capture_);
1790 mutator(&config_);
1791 ApplyConfig(config_);
1792}
1793
1794AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1795 rtc::CritScope cs_render(&crit_render_);
1796 rtc::CritScope cs_capture(&crit_capture_);
1797 return config_;
1798}
1799
peah2ace3f92016-09-10 04:42:27 -07001800bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1801 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001802 config_.high_pass_filter.enabled,
Per Åhgrend547d862019-05-03 15:48:47 +02001803 !!private_submodules_->echo_cancellation,
1804 !!private_submodules_->echo_control_mobile,
ivoc9f4a4a02016-10-28 05:39:16 -07001805 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001806 public_submodules_->noise_suppression->is_enabled(),
peah2ace3f92016-09-10 04:42:27 -07001807 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001808 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001809 capture_nonlocked_.echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -07001810 public_submodules_->voice_detection->is_enabled(),
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001811 config_.voice_detection.enabled,
peah2ace3f92016-09-10 04:42:27 -07001812 public_submodules_->level_estimator->is_enabled(),
1813 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001814}
1815
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001816void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001817 if (capture_.transient_suppressor_enabled) {
1818 if (!public_submodules_->transient_suppressor.get()) {
1819 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001820 }
peahdf3efa82015-11-28 12:35:15 -08001821 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001822 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1823 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001824 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001825}
1826
Per Åhgren0aefbf02019-08-23 21:29:17 +02001827void AudioProcessingImpl::InitializeHighPassFilter() {
1828 if (submodule_states_.HighPassFilteringRequired()) {
1829 private_submodules_->high_pass_filter.reset(
1830 new HighPassFilter(num_proc_channels()));
peah8271d042016-11-22 07:24:52 -08001831 } else {
Per Åhgren0aefbf02019-08-23 21:29:17 +02001832 private_submodules_->high_pass_filter.reset();
peah8271d042016-11-22 07:24:52 -08001833 }
1834}
alessiob3ec96df2017-05-22 06:57:06 -07001835
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001836void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001837 bool use_echo_controller =
1838 echo_control_factory_ ||
Per Åhgren200feba2019-03-06 04:16:46 +01001839 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode &&
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001840 !config_.echo_canceller.use_legacy_aec);
1841
1842 if (use_echo_controller) {
1843 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001844 if (echo_control_factory_) {
1845 private_submodules_->echo_controller =
1846 echo_control_factory_->Create(proc_sample_rate_hz());
1847 } else {
Mirko Bonadei317a1f02019-09-17 17:06:18 +02001848 private_submodules_->echo_controller = std::make_unique<EchoCanceller3>(
Per Åhgrence202a02019-09-02 17:01:19 +02001849 EchoCanceller3Config(), proc_sample_rate_hz(),
1850 /*num_render_channels=*/1, /*num_capture_channels=*/1);
Per Åhgren200feba2019-03-06 04:16:46 +01001851 }
1852
1853 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001854
Per Åhgrenf204faf2019-04-25 15:18:06 +02001855 private_submodules_->echo_cancellation.reset();
1856 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001857 private_submodules_->echo_control_mobile.reset();
1858 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001859 return;
peahe0eae3c2016-12-14 01:16:23 -08001860 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001861
1862 private_submodules_->echo_controller.reset();
1863 capture_nonlocked_.echo_controller_enabled = false;
1864
1865 if (!config_.echo_canceller.enabled) {
1866 private_submodules_->echo_cancellation.reset();
1867 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001868 private_submodules_->echo_control_mobile.reset();
1869 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001870 return;
1871 }
1872
1873 if (config_.echo_canceller.mobile_mode) {
1874 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001875 size_t max_element_size =
1876 std::max(static_cast<size_t>(1),
1877 kMaxAllowedValuesOfSamplesPerBand *
1878 EchoControlMobileImpl::NumCancellersRequired(
1879 num_output_channels(), num_reverse_channels()));
1880
1881 std::vector<int16_t> template_queue_element(max_element_size);
1882
1883 aecm_render_signal_queue_.reset(
1884 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1885 kMaxNumFramesToBuffer, template_queue_element,
1886 RenderQueueItemVerifier<int16_t>(max_element_size)));
1887
1888 aecm_render_queue_buffer_.resize(max_element_size);
1889 aecm_capture_queue_buffer_.resize(max_element_size);
1890
1891 private_submodules_->echo_control_mobile.reset(new EchoControlMobileImpl());
1892
1893 private_submodules_->echo_control_mobile->Initialize(
1894 proc_split_sample_rate_hz(), num_reverse_channels(),
1895 num_output_channels());
1896
Per Åhgrenf204faf2019-04-25 15:18:06 +02001897 private_submodules_->echo_cancellation.reset();
1898 aec_render_signal_queue_.reset();
1899 return;
1900 }
1901
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001902 private_submodules_->echo_control_mobile.reset();
1903 aecm_render_signal_queue_.reset();
1904
Per Åhgrenf204faf2019-04-25 15:18:06 +02001905 // Create and activate AEC2.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001906 private_submodules_->echo_cancellation.reset(new EchoCancellationImpl());
1907 private_submodules_->echo_cancellation->SetExtraOptions(
1908 capture_nonlocked_.use_aec2_extended_filter,
1909 capture_nonlocked_.use_aec2_delay_agnostic,
1910 capture_nonlocked_.use_aec2_refined_adaptive_filter);
1911
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001912 size_t element_max_size =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001913 std::max(static_cast<size_t>(1),
1914 kMaxAllowedValuesOfSamplesPerBand *
1915 EchoCancellationImpl::NumCancellersRequired(
1916 num_output_channels(), num_reverse_channels()));
1917
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001918 std::vector<float> template_queue_element(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001919
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001920 aec_render_signal_queue_.reset(
1921 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1922 kMaxNumFramesToBuffer, template_queue_element,
1923 RenderQueueItemVerifier<float>(element_max_size)));
Per Åhgrenf204faf2019-04-25 15:18:06 +02001924
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001925 aec_render_queue_buffer_.resize(element_max_size);
1926 aec_capture_queue_buffer_.resize(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001927
1928 private_submodules_->echo_cancellation->Initialize(
1929 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
1930 num_proc_channels());
1931
Per Åhgrenf204faf2019-04-25 15:18:06 +02001932 private_submodules_->echo_cancellation->set_suppression_level(
1933 config_.echo_canceller.legacy_moderate_suppression_level
1934 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
1935 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
peahe0eae3c2016-12-14 01:16:23 -08001936}
peah8271d042016-11-22 07:24:52 -08001937
alessiob3ec96df2017-05-22 06:57:06 -07001938void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001939 if (config_.gain_controller2.enabled) {
1940 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001941 }
1942}
1943
Alex Loikob5c9a792018-04-16 16:31:22 +02001944void AudioProcessingImpl::InitializePreAmplifier() {
1945 if (config_.pre_amplifier.enabled) {
1946 private_submodules_->pre_amplifier.reset(
1947 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1948 } else {
1949 private_submodules_->pre_amplifier.reset();
1950 }
1951}
1952
ivoc9f4a4a02016-10-28 05:39:16 -07001953void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001954 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001955 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001956 proc_sample_rate_hz(), 1,
1957 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001958}
1959
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001960void AudioProcessingImpl::InitializeAnalyzer() {
1961 if (private_submodules_->capture_analyzer) {
1962 private_submodules_->capture_analyzer->Initialize(proc_sample_rate_hz(),
1963 num_proc_channels());
1964 }
1965}
1966
Sam Zackrisson0beac582017-09-25 12:04:02 +02001967void AudioProcessingImpl::InitializePostProcessor() {
1968 if (private_submodules_->capture_post_processor) {
1969 private_submodules_->capture_post_processor->Initialize(
1970 proc_sample_rate_hz(), num_proc_channels());
1971 }
1972}
1973
Alex Loiko5825aa62017-12-18 16:02:40 +01001974void AudioProcessingImpl::InitializePreProcessor() {
1975 if (private_submodules_->render_pre_processor) {
1976 private_submodules_->render_pre_processor->Initialize(
1977 formats_.render_processing_format.sample_rate_hz(),
1978 formats_.render_processing_format.num_channels());
1979 }
1980}
1981
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001982void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001983
aleloi868f32f2017-05-23 07:20:05 -07001984void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1985 if (!aec_dump_) {
1986 return;
1987 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001988
1989 std::string experiments_description = "";
1990 if (private_submodules_->echo_cancellation) {
1991 experiments_description +=
1992 private_submodules_->echo_cancellation->GetExperimentsDescription();
1993 }
aleloi868f32f2017-05-23 07:20:05 -07001994 // TODO(peah): Add semicolon-separated concatenations of experiment
1995 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001996 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1997 experiments_description += "AgcClippingLevelExperiment;";
1998 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001999 if (capture_nonlocked_.echo_controller_enabled) {
2000 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07002001 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02002002 if (config_.gain_controller2.enabled) {
2003 experiments_description += "GainController2;";
2004 }
aleloi868f32f2017-05-23 07:20:05 -07002005
2006 InternalAPMConfig apm_config;
2007
Per Åhgren200feba2019-03-06 04:16:46 +01002008 apm_config.aec_enabled = config_.echo_canceller.enabled;
aleloi868f32f2017-05-23 07:20:05 -07002009 apm_config.aec_delay_agnostic_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002010 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002011 private_submodules_->echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002012 apm_config.aec_drift_compensation_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002013 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002014 private_submodules_->echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002015 apm_config.aec_extended_filter_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002016 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002017 private_submodules_->echo_cancellation->is_extended_filter_enabled();
Per Åhgrenf204faf2019-04-25 15:18:06 +02002018 apm_config.aec_suppression_level =
2019 private_submodules_->echo_cancellation
2020 ? static_cast<int>(
2021 private_submodules_->echo_cancellation->suppression_level())
2022 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002023
Per Åhgrend547d862019-05-03 15:48:47 +02002024 apm_config.aecm_enabled = !!private_submodules_->echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002025 apm_config.aecm_comfort_noise_enabled =
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002026 private_submodules_->echo_control_mobile &&
Sam Zackrissonc22f5512018-11-05 16:10:00 +01002027 private_submodules_->echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002028 apm_config.aecm_routing_mode =
2029 private_submodules_->echo_control_mobile
2030 ? static_cast<int>(
2031 private_submodules_->echo_control_mobile->routing_mode())
2032 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002033
2034 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2035 apm_config.agc_mode =
2036 static_cast<int>(public_submodules_->gain_control->mode());
2037 apm_config.agc_limiter_enabled =
2038 public_submodules_->gain_control->is_limiter_enabled();
2039 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2040
2041 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2042
2043 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2044 apm_config.ns_level =
2045 static_cast<int>(public_submodules_->noise_suppression->level());
2046
2047 apm_config.transient_suppression_enabled =
2048 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002049 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002050 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2051 apm_config.pre_amplifier_fixed_gain_factor =
2052 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002053
2054 if (!forced && apm_config == apm_config_for_aec_dump_) {
2055 return;
2056 }
2057 aec_dump_->WriteConfig(apm_config);
2058 apm_config_for_aec_dump_ = apm_config;
2059}
2060
2061void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2062 const float* const* src) {
2063 RTC_DCHECK(aec_dump_);
2064 WriteAecDumpConfigMessage(false);
2065
2066 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2067 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2068 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002069 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002070 RecordAudioProcessingState();
2071}
2072
2073void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2074 const AudioFrame& capture_frame) {
2075 RTC_DCHECK(aec_dump_);
2076 WriteAecDumpConfigMessage(false);
2077
2078 aec_dump_->AddCaptureStreamInput(capture_frame);
2079 RecordAudioProcessingState();
2080}
2081
2082void AudioProcessingImpl::RecordProcessedCaptureStream(
2083 const float* const* processed_capture_stream) {
2084 RTC_DCHECK(aec_dump_);
2085
2086 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2087 const size_t num_channels =
2088 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002089 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2090 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002091 aec_dump_->WriteCaptureStreamMessage();
2092}
2093
2094void AudioProcessingImpl::RecordProcessedCaptureStream(
2095 const AudioFrame& processed_capture_frame) {
2096 RTC_DCHECK(aec_dump_);
2097
2098 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2099 aec_dump_->WriteCaptureStreamMessage();
2100}
2101
2102void AudioProcessingImpl::RecordAudioProcessingState() {
2103 RTC_DCHECK(aec_dump_);
2104 AecDump::AudioProcessingState audio_proc_state;
2105 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2106 audio_proc_state.drift =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002107 private_submodules_->echo_cancellation
2108 ? private_submodules_->echo_cancellation->stream_drift_samples()
2109 : 0;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01002110 audio_proc_state.level = agc1()->stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002111 audio_proc_state.keypress = capture_.key_pressed;
2112 aec_dump_->AddAudioProcessingState(audio_proc_state);
2113}
2114
kwiberg83ffe452016-08-29 14:46:07 -07002115AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002116 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002117 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002118 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002119 output_will_be_muted(false),
2120 key_pressed(false),
2121 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002122 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002123 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002124 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002125 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002126 prev_pre_amp_gain(-1.f),
2127 playout_volume(-1),
2128 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002129
2130AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2131
Per Åhgrena1351272019-08-15 12:15:46 +02002132void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2133 const float* const* data,
2134 const StreamConfig& stream_config) {
2135 if (stream_config.has_keyboard()) {
2136 keyboard_data = data[stream_config.num_channels()];
2137 } else {
2138 keyboard_data = NULL;
2139 }
2140 num_keyboard_frames = stream_config.num_frames();
2141}
2142
kwiberg83ffe452016-08-29 14:46:07 -07002143AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2144
2145AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2146
niklase@google.com470e71d2011-07-07 08:21:25 +00002147} // namespace webrtc