blob: beabd9dcdcfb1e67266f5d9d0e29a18a453ba28e [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>
alessiob3ec96df2017-05-22 06:57:06 -070015#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <type_traits>
17#include <utility>
niklase@google.com470e71d2011-07-07 08:21:25 +000018
Per Åhgren200feba2019-03-06 04:16:46 +010019#include "absl/memory/memory.h"
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"
Yves Gerey988cc082018-10-23 12:03:01 +020035#include "modules/audio_processing/include/audio_frame_view.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020036#include "modules/audio_processing/level_estimator_impl.h"
Per Åhgren13735822018-02-12 21:42:56 +010037#include "modules/audio_processing/logging/apm_data_dumper.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020038#include "modules/audio_processing/low_cut_filter.h"
39#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 Åhgren1c602e32019-08-21 15:45:07 +020087// Identify the native processing rate that best handles a sample rate.
88int SuitableProcessRate(int minimum_rate, bool band_splitting_required) {
peah2ace3f92016-09-10 04:42:27 -070089#ifdef WEBRTC_ARCH_ARM_FAMILY
Per Åhgren1c602e32019-08-21 15:45:07 +020090 constexpr int kMaxSplittingRate = 32000;
peah2ace3f92016-09-10 04:42:27 -070091#else
Per Åhgren1c602e32019-08-21 15:45:07 +020092 constexpr int kMaxSplittingRate = 48000;
peah2ace3f92016-09-10 04:42:27 -070093#endif
Per Åhgren1c602e32019-08-21 15:45:07 +020094 static_assert(kMaxSplittingRate <= 48000, "");
peah2ace3f92016-09-10 04:42:27 -070095
Per Åhgren1c602e32019-08-21 15:45:07 +020096 const int uppermost_native_rate =
97 band_splitting_required ? kMaxSplittingRate : 48000;
98
99 for (auto rate : {16000, 32000, 48000}) {
peah2ace3f92016-09-10 04:42:27 -0700100 if (rate >= uppermost_native_rate) {
101 return uppermost_native_rate;
102 }
103 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700104 return rate;
105 }
106 }
peah2ace3f92016-09-10 04:42:27 -0700107 RTC_NOTREACHED();
108 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700109}
110
Sam Zackrisson23513132019-01-11 15:10:32 +0100111NoiseSuppression::Level NsConfigLevelToInterfaceLevel(
112 AudioProcessing::Config::NoiseSuppression::Level level) {
113 using NsConfig = AudioProcessing::Config::NoiseSuppression;
114 switch (level) {
115 case NsConfig::kLow:
116 return NoiseSuppression::kLow;
117 case NsConfig::kModerate:
118 return NoiseSuppression::kModerate;
119 case NsConfig::kHigh:
120 return NoiseSuppression::kHigh;
121 case NsConfig::kVeryHigh:
122 return NoiseSuppression::kVeryHigh;
123 default:
124 RTC_NOTREACHED();
125 }
126}
127
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100128GainControl::Mode Agc1ConfigModeToInterfaceMode(
129 AudioProcessing::Config::GainController1::Mode mode) {
130 using Agc1Config = AudioProcessing::Config::GainController1;
131 switch (mode) {
132 case Agc1Config::kAdaptiveAnalog:
133 return GainControl::kAdaptiveAnalog;
134 case Agc1Config::kAdaptiveDigital:
135 return GainControl::kAdaptiveDigital;
136 case Agc1Config::kFixedDigital:
137 return GainControl::kFixedDigital;
138 }
139}
140
peah9e6a2902017-05-15 07:19:21 -0700141// Maximum lengths that frame of samples being passed from the render side to
142// the capture side can have (does not apply to AEC3).
143static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
144static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
145
peah764e3642016-10-22 05:04:30 -0700146// Maximum number of frames to buffer in the render queue.
147// TODO(peah): Decrease this once we properly handle hugely unbalanced
148// reverse and forward call numbers.
149static const size_t kMaxNumFramesToBuffer = 100;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700150} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000151
152// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000153static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000154
Sam Zackrisson0beac582017-09-25 12:04:02 +0200155AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100156 bool capture_post_processor_enabled,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200157 bool render_pre_processor_enabled,
158 bool capture_analyzer_enabled)
Alex Loiko5825aa62017-12-18 16:02:40 +0100159 : capture_post_processor_enabled_(capture_post_processor_enabled),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200160 render_pre_processor_enabled_(render_pre_processor_enabled),
161 capture_analyzer_enabled_(capture_analyzer_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700162
163bool AudioProcessingImpl::ApmSubmoduleStates::Update(
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200164 bool high_pass_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700165 bool echo_canceller_enabled,
166 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700167 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700168 bool noise_suppressor_enabled,
peah2ace3f92016-09-10 04:42:27 -0700169 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700170 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200171 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200172 bool echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -0700173 bool voice_activity_detector_enabled,
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100174 bool private_voice_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700175 bool level_estimator_enabled,
176 bool transient_suppressor_enabled) {
177 bool changed = false;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200178 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700179 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
180 changed |=
181 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700182 changed |=
183 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700184 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
185 changed |=
peah2ace3f92016-09-10 04:42:27 -0700186 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200187 changed |= (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200188 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200189 changed |= (echo_controller_enabled != echo_controller_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700190 changed |= (level_estimator_enabled != level_estimator_enabled_);
191 changed |=
192 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100193 changed |=
194 (private_voice_detector_enabled != private_voice_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700195 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
196 if (changed) {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200197 high_pass_filter_enabled_ = high_pass_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700198 echo_canceller_enabled_ = echo_canceller_enabled;
199 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700200 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700201 noise_suppressor_enabled_ = noise_suppressor_enabled;
peah2ace3f92016-09-10 04:42:27 -0700202 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700203 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200204 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200205 echo_controller_enabled_ = echo_controller_enabled;
peah2ace3f92016-09-10 04:42:27 -0700206 level_estimator_enabled_ = level_estimator_enabled;
207 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100208 private_voice_detector_enabled_ = private_voice_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700209 transient_suppressor_enabled_ = transient_suppressor_enabled;
210 }
211
212 changed |= first_update_;
213 first_update_ = false;
214 return changed;
215}
216
217bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
218 const {
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100219 return CaptureMultiBandProcessingActive() ||
220 voice_activity_detector_enabled_ || private_voice_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700221}
222
223bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
224 const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200225 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700226 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200227 adaptive_gain_controller_enabled_ || echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700228}
229
peah23ac8b42017-05-23 05:33:56 -0700230bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
231 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200232 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
233 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700234}
235
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200236bool AudioProcessingImpl::ApmSubmoduleStates::CaptureAnalyzerActive() const {
237 return capture_analyzer_enabled_;
238}
239
peah2ace3f92016-09-10 04:42:27 -0700240bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
241 const {
242 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800243 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200244 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700245}
246
Alex Loiko5825aa62017-12-18 16:02:40 +0100247bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive()
248 const {
249 return render_pre_processor_enabled_;
250}
251
peah2ace3f92016-09-10 04:42:27 -0700252bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
253 const {
peah2ace3f92016-09-10 04:42:27 -0700254 return false;
peah2ace3f92016-09-10 04:42:27 -0700255}
256
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200257bool AudioProcessingImpl::ApmSubmoduleStates::LowCutFilteringRequired() const {
258 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
259 mobile_echo_controller_enabled_ || noise_suppressor_enabled_;
260}
261
solenberg5e465c32015-12-08 13:22:33 -0800262struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800263 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800264 // Accessed externally of APM without any lock acquired.
Sam Zackrisson23513132019-01-11 15:10:32 +0100265 // TODO(bugs.webrtc.org/9947): Move these submodules into private_submodules_
266 // when their pointer-to-submodule API functions are gone.
kwiberg88788ad2016-02-19 07:04:49 -0800267 std::unique_ptr<LevelEstimatorImpl> level_estimator;
268 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
Sam Zackrisson23513132019-01-11 15:10:32 +0100269 std::unique_ptr<NoiseSuppressionProxy> noise_suppression_proxy;
kwiberg88788ad2016-02-19 07:04:49 -0800270 std::unique_ptr<VoiceDetectionImpl> voice_detection;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100271 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800272 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800273 gain_control_for_experimental_agc;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100274 std::unique_ptr<GainControlConfigProxy> gain_control_config_proxy;
solenberg5e465c32015-12-08 13:22:33 -0800275
276 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800277 std::unique_ptr<TransientSuppressor> transient_suppressor;
solenberg5e465c32015-12-08 13:22:33 -0800278};
279
280struct AudioProcessingImpl::ApmPrivateSubmodules {
Sam Zackrissondb389722018-06-21 10:12:24 +0200281 ApmPrivateSubmodules(std::unique_ptr<CustomProcessing> capture_post_processor,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100282 std::unique_ptr<CustomProcessing> render_pre_processor,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200283 rtc::scoped_refptr<EchoDetector> echo_detector,
284 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Sam Zackrissondb389722018-06-21 10:12:24 +0200285 : echo_detector(std::move(echo_detector)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100286 capture_post_processor(std::move(capture_post_processor)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200287 render_pre_processor(std::move(render_pre_processor)),
288 capture_analyzer(std::move(capture_analyzer)) {}
solenberg5e465c32015-12-08 13:22:33 -0800289 // Accessed internally from capture or during initialization
kwiberg88788ad2016-02-19 07:04:49 -0800290 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700291 std::unique_ptr<GainController2> gain_controller2;
peah8271d042016-11-22 07:24:52 -0800292 std::unique_ptr<LowCutFilter> low_cut_filter;
Ivo Creusend1f970d2018-06-14 11:02:03 +0200293 rtc::scoped_refptr<EchoDetector> echo_detector;
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100294 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
Sam Zackrissonc22f5512018-11-05 16:10:00 +0100295 std::unique_ptr<EchoControl> echo_controller;
296 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
Alex Loiko5825aa62017-12-18 16:02:40 +0100297 std::unique_ptr<CustomProcessing> capture_post_processor;
298 std::unique_ptr<CustomProcessing> render_pre_processor;
Alex Loikob5c9a792018-04-16 16:31:22 +0200299 std::unique_ptr<GainApplier> pre_amplifier;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200300 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100301 std::unique_ptr<LevelEstimatorImpl> output_level_estimator;
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100302 std::unique_ptr<VoiceDetectionImpl> voice_detector;
solenberg5e465c32015-12-08 13:22:33 -0800303};
304
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100305AudioProcessingBuilder::AudioProcessingBuilder() = default;
306AudioProcessingBuilder::~AudioProcessingBuilder() = default;
307
308AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
309 std::unique_ptr<CustomProcessing> capture_post_processing) {
310 capture_post_processing_ = std::move(capture_post_processing);
311 return *this;
312}
313
314AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
315 std::unique_ptr<CustomProcessing> render_pre_processing) {
316 render_pre_processing_ = std::move(render_pre_processing);
317 return *this;
318}
319
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200320AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
321 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
322 capture_analyzer_ = std::move(capture_analyzer);
323 return *this;
324}
325
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100326AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
327 std::unique_ptr<EchoControlFactory> echo_control_factory) {
328 echo_control_factory_ = std::move(echo_control_factory);
329 return *this;
330}
331
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100332AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200333 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100334 echo_detector_ = std::move(echo_detector);
335 return *this;
336}
337
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100338AudioProcessing* AudioProcessingBuilder::Create() {
339 webrtc::Config config;
340 return Create(config);
341}
342
343AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100344 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
345 config, std::move(capture_post_processing_),
346 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200347 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100348 if (apm->Initialize() != AudioProcessing::kNoError) {
349 delete apm;
350 apm = nullptr;
351 }
352 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100353}
354
peah88ac8532016-09-12 16:47:25 -0700355AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200356 : AudioProcessingImpl(config, nullptr, nullptr, nullptr, nullptr, nullptr) {
357}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000358
Per Åhgren13735822018-02-12 21:42:56 +0100359int AudioProcessingImpl::instance_count_ = 0;
360
Sam Zackrisson0beac582017-09-25 12:04:02 +0200361AudioProcessingImpl::AudioProcessingImpl(
362 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100363 std::unique_ptr<CustomProcessing> capture_post_processor,
364 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200365 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200366 rtc::scoped_refptr<EchoDetector> echo_detector,
367 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100368 : data_dumper_(
369 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200370 capture_runtime_settings_(kRuntimeSettingQueueSize),
371 render_runtime_settings_(kRuntimeSettingQueueSize),
372 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
373 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200374 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200375 submodule_states_(!!capture_post_processor,
376 !!render_pre_processor,
377 !!capture_analyzer),
peah8271d042016-11-22 07:24:52 -0800378 public_submodules_(new ApmPublicSubmodules()),
Sam Zackrisson0beac582017-09-25 12:04:02 +0200379 private_submodules_(
Sam Zackrissondb389722018-06-21 10:12:24 +0200380 new ApmPrivateSubmodules(std::move(capture_post_processor),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100381 std::move(render_pre_processor),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200382 std::move(echo_detector),
383 std::move(capture_analyzer))),
peahdf3efa82015-11-28 12:35:15 -0800384 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800385 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000386#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alex Loikod9342442018-09-10 13:59:41 +0200387 /* enabled= */ false,
388 /* enabled_agc2_level_estimator= */ false,
389 /* digital_adaptive_disabled= */ false,
390 /* analyze_before_aec= */ false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000391#else
Alex Loiko64cb83b2018-07-02 13:38:19 +0200392 config.Get<ExperimentalAgc>().enabled,
393 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
Alex Loikod9342442018-09-10 13:59:41 +0200394 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
395 config.Get<ExperimentalAgc>().analyze_before_aec),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000396#endif
andrew1c7075f2015-06-24 18:14:14 -0700397#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200398 capture_(false),
andrew1c7075f2015-06-24 18:14:14 -0700399#else
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200400 capture_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700401#endif
Alessio Bazzicacc22f512018-08-30 13:01:34 +0200402 capture_nonlocked_() {
Sam Zackrisson421c8592019-02-11 13:39:46 +0100403 // Mark Echo Controller enabled if a factory is injected.
404 capture_nonlocked_.echo_controller_enabled =
405 static_cast<bool>(echo_control_factory_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000406
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100407 public_submodules_->gain_control.reset(new GainControlImpl());
Sam Zackrisson421c8592019-02-11 13:39:46 +0100408 public_submodules_->level_estimator.reset(
409 new LevelEstimatorImpl(&crit_capture_));
410 public_submodules_->noise_suppression.reset(
411 new NoiseSuppressionImpl(&crit_capture_));
412 public_submodules_->noise_suppression_proxy.reset(new NoiseSuppressionProxy(
413 this, public_submodules_->noise_suppression.get()));
414 public_submodules_->voice_detection.reset(
415 new VoiceDetectionImpl(&crit_capture_));
416 public_submodules_->gain_control_for_experimental_agc.reset(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100417 new GainControlForExperimentalAgc(
418 public_submodules_->gain_control.get()));
419 public_submodules_->gain_control_config_proxy.reset(
420 new GainControlConfigProxy(&crit_capture_, this, agc1()));
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200421
Sam Zackrisson421c8592019-02-11 13:39:46 +0100422 // If no echo detector is injected, use the ResidualEchoDetector.
423 if (!private_submodules_->echo_detector) {
424 private_submodules_->echo_detector =
425 new rtc::RefCountedObject<ResidualEchoDetector>();
peahdf3efa82015-11-28 12:35:15 -0800426 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000427
Sam Zackrisson421c8592019-02-11 13:39:46 +0100428 // TODO(alessiob): Move the injected gain controller once injection is
429 // implemented.
430 private_submodules_->gain_controller2.reset(new GainController2());
431
432 RTC_LOG(LS_INFO) << "Capture analyzer activated: "
433 << !!private_submodules_->capture_analyzer
434 << "\nCapture post processor activated: "
435 << !!private_submodules_->capture_post_processor
436 << "\nRender pre processor activated: "
437 << !!private_submodules_->render_pre_processor;
438
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000439 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000440}
441
442AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800443 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800444 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800445 private_submodules_->agc_manager.reset();
446 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800447 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000448}
449
niklase@google.com470e71d2011-07-07 08:21:25 +0000450int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800451 // Run in a single-threaded manner during initialization.
452 rtc::CritScope cs_render(&crit_render_);
453 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000454 return InitializeLocked();
455}
456
peahde65ddc2016-09-16 15:02:15 -0700457int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
458 int capture_output_sample_rate_hz,
459 int render_input_sample_rate_hz,
460 ChannelLayout capture_input_layout,
461 ChannelLayout capture_output_layout,
462 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700463 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700464 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
465 LayoutHasKeyboard(capture_input_layout)},
466 {capture_output_sample_rate_hz,
467 ChannelsFromLayout(capture_output_layout),
468 LayoutHasKeyboard(capture_output_layout)},
469 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
470 LayoutHasKeyboard(render_input_layout)},
471 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
472 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700473
474 return Initialize(processing_config);
475}
476
477int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800478 // Run in a single-threaded manner during initialization.
479 rtc::CritScope cs_render(&crit_render_);
480 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700481 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000482}
483
peahdf3efa82015-11-28 12:35:15 -0800484int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800485 const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800486 // Called from both threads. Thread check is therefore not possible.
Oskar Sundbom4b276482019-05-23 14:28:00 +0200487 if (processing_config == formats_.api_format) {
peah192164e2015-11-17 02:16:45 -0800488 return kNoError;
489 }
peahdf3efa82015-11-28 12:35:15 -0800490
491 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800492 return InitializeLocked(processing_config);
493}
494
niklase@google.com470e71d2011-07-07 08:21:25 +0000495int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200496 UpdateActiveSubmoduleStates();
497
Steve Antonf254e9e2019-08-21 17:52:28 +0000498 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800499 formats_.api_format.reverse_output_stream().num_frames() == 0
Steve Antonf254e9e2019-08-21 17:52:28 +0000500 ? formats_.render_processing_format.num_frames()
501 : formats_.api_format.reverse_output_stream().num_frames();
peahdf3efa82015-11-28 12:35:15 -0800502 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
503 render_.render_audio.reset(new AudioBuffer(
Steve Antonf254e9e2019-08-21 17:52:28 +0000504 formats_.api_format.reverse_input_stream().num_frames(),
peahdf3efa82015-11-28 12:35:15 -0800505 formats_.api_format.reverse_input_stream().num_channels(),
Steve Antonf254e9e2019-08-21 17:52:28 +0000506 formats_.render_processing_format.num_frames(),
peahde65ddc2016-09-16 15:02:15 -0700507 formats_.render_processing_format.num_channels(),
Steve Antonf254e9e2019-08-21 17:52:28 +0000508 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700509 if (formats_.api_format.reverse_input_stream() !=
510 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800511 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800512 formats_.api_format.reverse_input_stream().num_channels(),
513 formats_.api_format.reverse_input_stream().num_frames(),
514 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800515 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700516 } else {
peahdf3efa82015-11-28 12:35:15 -0800517 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700518 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700519 } else {
peahdf3efa82015-11-28 12:35:15 -0800520 render_.render_audio.reset(nullptr);
521 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700522 }
peahce4d9152017-05-19 01:28:05 -0700523
Steve Antonf254e9e2019-08-21 17:52:28 +0000524 capture_.capture_audio.reset(
525 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
526 formats_.api_format.input_stream().num_channels(),
527 capture_nonlocked_.capture_processing_format.num_frames(),
528 formats_.api_format.output_stream().num_channels(),
529 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000530
peah764e3642016-10-22 05:04:30 -0700531 AllocateRenderQueue();
532
peah135259a2016-10-28 03:12:11 -0700533 public_submodules_->gain_control->Initialize(num_proc_channels(),
534 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700535 if (constants_.use_experimental_agc) {
536 if (!private_submodules_->agc_manager.get()) {
537 private_submodules_->agc_manager.reset(new AgcManagerDirect(
538 public_submodules_->gain_control.get(),
539 public_submodules_->gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200540 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
541 constants_.use_experimental_agc_agc2_level_estimation,
542 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700543 }
544 private_submodules_->agc_manager->Initialize();
545 private_submodules_->agc_manager->SetCaptureMuted(
546 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700547 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700548 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200549 InitializeTransient();
peah8271d042016-11-22 07:24:52 -0800550 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700551 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
552 proc_sample_rate_hz());
553 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100554 if (private_submodules_->voice_detector) {
555 private_submodules_->voice_detector->Initialize(
556 proc_split_sample_rate_hz());
557 }
peahde65ddc2016-09-16 15:02:15 -0700558 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700559 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200560 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700561 InitializeGainController2();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200562 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200563 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100564 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800565
aleloi868f32f2017-05-23 07:20:05 -0700566 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200567 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700568 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000569 return kNoError;
570}
571
Michael Graczyk86c6d332015-07-23 11:41:39 -0700572int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200573 UpdateActiveSubmoduleStates();
574
Michael Graczyk86c6d332015-07-23 11:41:39 -0700575 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700576 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
577 return kBadSampleRateError;
578 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000579 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700580
Peter Kasting69558702016-01-12 16:26:35 -0800581 const size_t num_in_channels = config.input_stream().num_channels();
582 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700583
584 // Need at least one input channel.
585 // Need either one output channel or as many outputs as there are inputs.
586 if (num_in_channels == 0 ||
587 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700588 return kBadNumberChannelsError;
589 }
590
peahdf3efa82015-11-28 12:35:15 -0800591 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000592
Per Åhgren1c602e32019-08-21 15:45:07 +0200593 int capture_processing_rate = SuitableProcessRate(
peah423d2362016-04-09 16:06:52 -0700594 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700595 formats_.api_format.output_stream().sample_rate_hz()),
596 submodule_states_.CaptureMultiBandSubModulesActive() ||
597 submodule_states_.RenderMultiBandSubModulesActive());
Per Åhgren1c602e32019-08-21 15:45:07 +0200598 RTC_DCHECK_NE(8000, capture_processing_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000599
peahde65ddc2016-09-16 15:02:15 -0700600 capture_nonlocked_.capture_processing_format =
601 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700602
peah2ce640f2017-04-07 03:57:48 -0700603 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200604 if (!capture_nonlocked_.echo_controller_enabled) {
Per Åhgren1c602e32019-08-21 15:45:07 +0200605 render_processing_rate = SuitableProcessRate(
peah2ce640f2017-04-07 03:57:48 -0700606 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
607 formats_.api_format.reverse_output_stream().sample_rate_hz()),
608 submodule_states_.CaptureMultiBandSubModulesActive() ||
609 submodule_states_.RenderMultiBandSubModulesActive());
610 } else {
611 render_processing_rate = capture_processing_rate;
612 }
613
aluebseb3603b2016-04-20 15:27:58 -0700614 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
615 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700616 if (render_processing_rate > kSampleRate32kHz &&
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200617 !capture_nonlocked_.echo_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -0700618 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
619 ? kSampleRate32kHz
620 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700621 }
peah2ce640f2017-04-07 03:57:48 -0700622
peahde65ddc2016-09-16 15:02:15 -0700623 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700624 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700625 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
626 kSampleRate8kHz) {
627 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000628 } else {
peahde65ddc2016-09-16 15:02:15 -0700629 render_processing_rate =
630 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000631 }
Per Åhgren1c602e32019-08-21 15:45:07 +0200632 RTC_DCHECK_NE(8000, render_processing_rate);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000633
peahde65ddc2016-09-16 15:02:15 -0700634 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000635 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700636 if (submodule_states_.RenderMultiBandSubModulesActive()) {
637 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
638 } else {
639 formats_.render_processing_format = StreamConfig(
640 formats_.api_format.reverse_input_stream().sample_rate_hz(),
641 formats_.api_format.reverse_input_stream().num_channels());
642 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000643
peahde65ddc2016-09-16 15:02:15 -0700644 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
645 kSampleRate32kHz ||
646 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
647 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800648 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000649 } else {
peahdf3efa82015-11-28 12:35:15 -0800650 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700651 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000652 }
653
654 return InitializeLocked();
655}
656
peah88ac8532016-09-12 16:47:25 -0700657void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peah88ac8532016-09-12 16:47:25 -0700658 // Run in a single-threaded manner when applying the settings.
659 rtc::CritScope cs_render(&crit_render_);
660 rtc::CritScope cs_capture(&crit_capture_);
661
Per Åhgren200feba2019-03-06 04:16:46 +0100662 const bool aec_config_changed =
663 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
664 config_.echo_canceller.use_legacy_aec !=
665 config.echo_canceller.use_legacy_aec ||
666 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode ||
667 (config_.echo_canceller.enabled && config.echo_canceller.use_legacy_aec &&
668 config_.echo_canceller.legacy_moderate_suppression_level !=
669 config.echo_canceller.legacy_moderate_suppression_level);
670
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100671 const bool agc1_config_changed =
672 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
673 config_.gain_controller1.mode != config.gain_controller1.mode ||
674 config_.gain_controller1.target_level_dbfs !=
675 config.gain_controller1.target_level_dbfs ||
676 config_.gain_controller1.compression_gain_db !=
677 config.gain_controller1.compression_gain_db ||
678 config_.gain_controller1.enable_limiter !=
679 config.gain_controller1.enable_limiter ||
680 config_.gain_controller1.analog_level_minimum !=
681 config.gain_controller1.analog_level_minimum ||
682 config_.gain_controller1.analog_level_maximum !=
683 config.gain_controller1.analog_level_maximum;
684
Yves Gerey499bc6c2018-10-10 18:29:07 +0200685 config_ = config;
686
Per Åhgren200feba2019-03-06 04:16:46 +0100687 if (aec_config_changed) {
688 InitializeEchoController();
689 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200690
Sam Zackrisson23513132019-01-11 15:10:32 +0100691 public_submodules_->noise_suppression->Enable(
692 config.noise_suppression.enabled);
693 public_submodules_->noise_suppression->set_level(
694 NsConfigLevelToInterfaceLevel(config.noise_suppression.level));
695
peah8271d042016-11-22 07:24:52 -0800696 InitializeLowCutFilter();
697
Mirko Bonadei675513b2017-11-09 11:09:25 +0100698 RTC_LOG(LS_INFO) << "Highpass filter activated: "
699 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800700
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100701 if (agc1_config_changed) {
702 ApplyAgc1Config(config_.gain_controller1);
703 }
704
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100705 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700706 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100707 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
708 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100709 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100710 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700711 config_.gain_controller2 = AudioProcessing::Config::GainController2();
712 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200713 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200714 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200715 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100716 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
717 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200718 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
719 << config_.pre_amplifier.enabled;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100720
721 if (config_.level_estimation.enabled &&
722 !private_submodules_->output_level_estimator) {
723 private_submodules_->output_level_estimator.reset(
724 new LevelEstimatorImpl(&crit_capture_));
725 private_submodules_->output_level_estimator->Enable(true);
726 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100727
728 if (config_.voice_detection.enabled && !private_submodules_->voice_detector) {
729 private_submodules_->voice_detector.reset(
730 new VoiceDetectionImpl(&crit_capture_));
731 private_submodules_->voice_detector->Enable(true);
732 private_submodules_->voice_detector->set_likelihood(
733 VoiceDetection::kVeryLowLikelihood);
734 private_submodules_->voice_detector->Initialize(
735 proc_split_sample_rate_hz());
736 }
peah88ac8532016-09-12 16:47:25 -0700737}
738
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100739void AudioProcessingImpl::ApplyAgc1Config(
740 const Config::GainController1& config) {
741 GainControl* agc = agc1();
742 int error = agc->Enable(config.enabled);
743 RTC_DCHECK_EQ(kNoError, error);
744 error = agc->set_mode(Agc1ConfigModeToInterfaceMode(config.mode));
745 RTC_DCHECK_EQ(kNoError, error);
746 error = agc->set_target_level_dbfs(config.target_level_dbfs);
747 RTC_DCHECK_EQ(kNoError, error);
748 error = agc->set_compression_gain_db(config.compression_gain_db);
749 RTC_DCHECK_EQ(kNoError, error);
750 error = agc->enable_limiter(config.enable_limiter);
751 RTC_DCHECK_EQ(kNoError, error);
752 error = agc->set_analog_level_limits(config.analog_level_minimum,
753 config.analog_level_maximum);
754 RTC_DCHECK_EQ(kNoError, error);
755}
756
757GainControl* AudioProcessingImpl::agc1() {
758 if (constants_.use_experimental_agc) {
759 return public_submodules_->gain_control_for_experimental_agc.get();
760 }
761 return public_submodules_->gain_control.get();
762}
763
764const GainControl* AudioProcessingImpl::agc1() const {
765 if (constants_.use_experimental_agc) {
766 return public_submodules_->gain_control_for_experimental_agc.get();
767 }
768 return public_submodules_->gain_control.get();
769}
770
peah88ac8532016-09-12 16:47:25 -0700771void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800772 // Run in a single-threaded manner when setting the extra options.
773 rtc::CritScope cs_render(&crit_render_);
774 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000775
Per Åhgrenf204faf2019-04-25 15:18:06 +0200776 capture_nonlocked_.use_aec2_extended_filter =
777 config.Get<ExtendedFilter>().enabled;
778 capture_nonlocked_.use_aec2_delay_agnostic =
779 config.Get<DelayAgnostic>().enabled;
780 capture_nonlocked_.use_aec2_refined_adaptive_filter =
781 config.Get<RefinedAdaptiveFilter>().enabled;
peahb624d8c2016-03-05 03:01:14 -0800782
peahdf3efa82015-11-28 12:35:15 -0800783 if (capture_.transient_suppressor_enabled !=
784 config.Get<ExperimentalNs>().enabled) {
785 capture_.transient_suppressor_enabled =
786 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000787 InitializeTransient();
788 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000789}
790
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000791int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800792 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700793 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000794}
795
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000796int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800797 // Used as callback from submodules, hence locking is not allowed.
798 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000799}
800
Peter Kasting69558702016-01-12 16:26:35 -0800801size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800802 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700803 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000804}
805
Peter Kasting69558702016-01-12 16:26:35 -0800806size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800807 // Used as callback from submodules, hence locking is not allowed.
808 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000809}
810
Peter Kasting69558702016-01-12 16:26:35 -0800811size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800812 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200813 return capture_nonlocked_.echo_controller_enabled ? 1 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800814}
815
Peter Kasting69558702016-01-12 16:26:35 -0800816size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800817 // Used as callback from submodules, hence locking is not allowed.
818 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000819}
820
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000821void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800822 rtc::CritScope cs(&crit_capture_);
823 capture_.output_will_be_muted = muted;
824 if (private_submodules_->agc_manager.get()) {
825 private_submodules_->agc_manager->SetCaptureMuted(
826 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000827 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000828}
829
Alessio Bazzicac054e782018-04-16 12:10:09 +0200830void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200831 switch (setting.type()) {
832 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
833 render_runtime_settings_enqueuer_.Enqueue(setting);
834 return;
835 case RuntimeSetting::Type::kNotSpecified:
836 RTC_NOTREACHED();
837 return;
838 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100839 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200840 case RuntimeSetting::Type::kCaptureFixedPostGain:
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200841 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200842 capture_runtime_settings_enqueuer_.Enqueue(setting);
843 return;
844 }
845 // The language allows the enum to have a non-enumerator
846 // value. Check that this doesn't happen.
847 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200848}
849
850AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
851 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200852 : runtime_settings_(*runtime_settings) {
853 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200854}
855
856AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
857 default;
858
859void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
860 RuntimeSetting setting) {
861 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200862 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200863 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200864 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200865 RTC_LOG(LS_ERROR)
866 << "The runtime settings queue is full. Oldest setting discarded.";
867 }
868 if (remaining_attempts == 0)
869 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
870}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000871
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000872int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700873 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000874 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000875 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000876 int output_sample_rate_hz,
877 ChannelLayout output_layout,
878 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800879 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800880 StreamConfig input_stream;
881 StreamConfig output_stream;
882 {
883 // Access the formats_.api_format.input_stream beneath the capture lock.
884 // The lock must be released as it is later required in the call
885 // to ProcessStream(,,,);
886 rtc::CritScope cs(&crit_capture_);
887 input_stream = formats_.api_format.input_stream();
888 output_stream = formats_.api_format.output_stream();
889 }
890
Michael Graczyk86c6d332015-07-23 11:41:39 -0700891 input_stream.set_sample_rate_hz(input_sample_rate_hz);
892 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
893 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700894 output_stream.set_sample_rate_hz(output_sample_rate_hz);
895 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
896 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
897
898 if (samples_per_channel != input_stream.num_frames()) {
899 return kBadDataLengthError;
900 }
901 return ProcessStream(src, input_stream, output_stream, dest);
902}
903
904int AudioProcessingImpl::ProcessStream(const float* const* src,
905 const StreamConfig& input_config,
906 const StreamConfig& output_config,
907 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800908 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800909 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700910 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800911 {
912 // Acquire the capture lock in order to safely call the function
913 // that retrieves the render side data. This function accesses apm
914 // getters that need the capture lock held when being called.
915 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700916 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800917
918 if (!src || !dest) {
919 return kNullPointerError;
920 }
921
922 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700923 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000924 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000925
Oskar Sundbom4b276482019-05-23 14:28:00 +0200926 if (processing_config.input_stream() != input_config) {
927 processing_config.input_stream() = input_config;
928 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800929 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200930
931 if (processing_config.output_stream() != output_config) {
932 processing_config.output_stream() = output_config;
933 reinitialization_required = true;
934 }
935
936 if (reinitialization_required) {
937 // Reinitialize.
938 rtc::CritScope cs_render(&crit_render_);
939 rtc::CritScope cs_capture(&crit_capture_);
940 RETURN_ON_ERR(InitializeLocked(processing_config));
941 }
942
peahdf3efa82015-11-28 12:35:15 -0800943 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700944 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
945 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000946
aleloi868f32f2017-05-23 07:20:05 -0700947 if (aec_dump_) {
948 RecordUnprocessedCaptureStream(src);
949 }
950
Per Åhgrena1351272019-08-15 12:15:46 +0200951 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800952 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700953 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800954 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000955
aleloi868f32f2017-05-23 07:20:05 -0700956 if (aec_dump_) {
957 RecordProcessedCaptureStream(dest);
958 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000959 return kNoError;
960}
961
Alex Loiko73ec0192018-05-15 10:52:28 +0200962void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200963 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200964 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200965 if (aec_dump_) {
966 aec_dump_->WriteRuntimeSetting(setting);
967 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200968 switch (setting.type()) {
969 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200970 if (config_.pre_amplifier.enabled) {
971 float value;
972 setting.GetFloat(&value);
973 private_submodules_->pre_amplifier->SetGainFactor(value);
974 }
975 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200976 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100977 case RuntimeSetting::Type::kCaptureCompressionGain: {
978 float value;
979 setting.GetFloat(&value);
980 int int_value = static_cast<int>(value + .5f);
981 config_.gain_controller1.compression_gain_db = int_value;
982 int error = agc1()->set_compression_gain_db(int_value);
983 RTC_DCHECK_EQ(kNoError, error);
984 break;
985 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200986 case RuntimeSetting::Type::kCaptureFixedPostGain: {
987 if (config_.gain_controller2.enabled) {
988 float value;
989 setting.GetFloat(&value);
990 config_.gain_controller2.fixed_digital.gain_db = value;
991 private_submodules_->gain_controller2->ApplyConfig(
992 config_.gain_controller2);
993 }
994 break;
995 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200996 case RuntimeSetting::Type::kPlayoutVolumeChange: {
997 int value;
998 setting.GetInt(&value);
999 capture_.playout_volume = value;
1000 break;
1001 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001002 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1003 RTC_NOTREACHED();
1004 break;
1005 case RuntimeSetting::Type::kNotSpecified:
1006 RTC_NOTREACHED();
1007 break;
1008 }
1009 }
1010}
1011
1012void AudioProcessingImpl::HandleRenderRuntimeSettings() {
1013 RuntimeSetting setting;
1014 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +02001015 if (aec_dump_) {
1016 aec_dump_->WriteRuntimeSetting(setting);
1017 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001018 switch (setting.type()) {
1019 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1020 if (private_submodules_->render_pre_processor) {
1021 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
1022 }
1023 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001024 case RuntimeSetting::Type::kCapturePreGain: // fall-through
1025 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001026 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001027 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001028 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001029 RTC_NOTREACHED();
1030 break;
1031 }
1032 }
1033}
1034
peah9e6a2902017-05-15 07:19:21 -07001035void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001036 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001037
1038 // Insert the samples into the queue.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001039 if (private_submodules_->echo_cancellation) {
1040 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001041 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1042 num_reverse_channels(),
1043 &aec_render_queue_buffer_);
1044
Per Åhgrenf204faf2019-04-25 15:18:06 +02001045 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1046 // The data queue is full and needs to be emptied.
1047 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001048
Per Åhgrenf204faf2019-04-25 15:18:06 +02001049 // Retry the insert (should always work).
1050 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1051 RTC_DCHECK(result);
1052 }
peaha0624602016-10-25 04:45:24 -07001053 }
1054
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001055 if (private_submodules_->echo_control_mobile) {
1056 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1057 num_reverse_channels(),
1058 &aecm_render_queue_buffer_);
1059 RTC_DCHECK(aecm_render_signal_queue_);
1060 // Insert the samples into the queue.
1061 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1062 // The data queue is full and needs to be emptied.
1063 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001064
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001065 // Retry the insert (should always work).
1066 bool result =
1067 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1068 RTC_DCHECK(result);
1069 }
peah764e3642016-10-22 05:04:30 -07001070 }
peah701d6282016-10-25 05:42:20 -07001071
1072 if (!constants_.use_experimental_agc) {
1073 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1074 // Insert the samples into the queue.
1075 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1076 // The data queue is full and needs to be emptied.
1077 EmptyQueuedRenderAudio();
1078
1079 // Retry the insert (should always work).
1080 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1081 RTC_DCHECK(result);
1082 }
1083 }
peah9e6a2902017-05-15 07:19:21 -07001084}
ivoc9f4a4a02016-10-28 05:39:16 -07001085
peah9e6a2902017-05-15 07:19:21 -07001086void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001087 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1088
1089 // Insert the samples into the queue.
1090 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1091 // The data queue is full and needs to be emptied.
1092 EmptyQueuedRenderAudio();
1093
1094 // Retry the insert (should always work).
1095 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1096 RTC_DCHECK(result);
1097 }
peah764e3642016-10-22 05:04:30 -07001098}
1099
1100void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001101 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001102 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001103
ivoc9f4a4a02016-10-28 05:39:16 -07001104 const size_t new_red_render_queue_element_max_size =
1105 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1106
peaha0624602016-10-25 04:45:24 -07001107 // Reallocate the queues if the queue item sizes are too small to fit the
1108 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001109
1110 if (agc_render_queue_element_max_size_ <
1111 new_agc_render_queue_element_max_size) {
1112 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1113
1114 std::vector<int16_t> template_queue_element(
1115 agc_render_queue_element_max_size_);
1116
1117 agc_render_signal_queue_.reset(
1118 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1119 kMaxNumFramesToBuffer, template_queue_element,
1120 RenderQueueItemVerifier<int16_t>(
1121 agc_render_queue_element_max_size_)));
1122
1123 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1124 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1125 } else {
1126 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001127 }
ivoc9f4a4a02016-10-28 05:39:16 -07001128
1129 if (red_render_queue_element_max_size_ <
1130 new_red_render_queue_element_max_size) {
1131 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1132
1133 std::vector<float> template_queue_element(
1134 red_render_queue_element_max_size_);
1135
1136 red_render_signal_queue_.reset(
1137 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1138 kMaxNumFramesToBuffer, template_queue_element,
1139 RenderQueueItemVerifier<float>(
1140 red_render_queue_element_max_size_)));
1141
1142 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1143 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1144 } else {
1145 red_render_signal_queue_->Clear();
1146 }
peah764e3642016-10-22 05:04:30 -07001147}
1148
1149void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1150 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001151 if (private_submodules_->echo_cancellation) {
1152 RTC_DCHECK(aec_render_signal_queue_);
1153 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
1154 private_submodules_->echo_cancellation->ProcessRenderAudio(
1155 aec_capture_queue_buffer_);
1156 }
peaha0624602016-10-25 04:45:24 -07001157 }
1158
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001159 if (private_submodules_->echo_control_mobile) {
1160 RTC_DCHECK(aecm_render_signal_queue_);
1161 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
1162 private_submodules_->echo_control_mobile->ProcessRenderAudio(
1163 aecm_capture_queue_buffer_);
1164 }
peah701d6282016-10-25 05:42:20 -07001165 }
1166
1167 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1168 public_submodules_->gain_control->ProcessRenderAudio(
1169 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001170 }
ivoc9f4a4a02016-10-28 05:39:16 -07001171
1172 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001173 RTC_DCHECK(private_submodules_->echo_detector);
1174 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001175 red_capture_queue_buffer_);
1176 }
peah764e3642016-10-22 05:04:30 -07001177}
1178
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001179int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001180 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001181 {
1182 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001183 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001184 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001185 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001186 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001187 }
peahfa6228e2015-11-16 16:27:42 -08001188
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001189 if (!frame) {
1190 return kNullPointerError;
1191 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001192 // Must be a native rate.
1193 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1194 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001195 frame->sample_rate_hz_ != kSampleRate32kHz &&
1196 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001197 return kBadSampleRateError;
1198 }
peah192164e2015-11-17 02:16:45 -08001199
peahdf3efa82015-11-28 12:35:15 -08001200 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001201 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001202 {
1203 // Aquire lock for the access of api_format.
1204 // The lock is released immediately due to the conditional
1205 // reinitialization.
1206 rtc::CritScope cs_capture(&crit_capture_);
1207 // TODO(ajm): The input and output rates and channels are currently
1208 // constrained to be identical in the int16 interface.
1209 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001210
1211 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001212 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001213
Oskar Sundbom4b276482019-05-23 14:28:00 +02001214 reinitialization_required =
1215 reinitialization_required ||
1216 processing_config.input_stream().sample_rate_hz() !=
1217 frame->sample_rate_hz_ ||
1218 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1219 processing_config.output_stream().sample_rate_hz() !=
1220 frame->sample_rate_hz_ ||
1221 processing_config.output_stream().num_channels() != frame->num_channels_;
1222
1223 if (reinitialization_required) {
1224 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1225 processing_config.input_stream().set_num_channels(frame->num_channels_);
1226 processing_config.output_stream().set_sample_rate_hz(
1227 frame->sample_rate_hz_);
1228 processing_config.output_stream().set_num_channels(frame->num_channels_);
1229
1230 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001231 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001232 rtc::CritScope cs_capture(&crit_capture_);
1233 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001234 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001235
peahdf3efa82015-11-28 12:35:15 -08001236 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001237 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001238 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001239 return kBadDataLengthError;
1240 }
1241
aleloi868f32f2017-05-23 07:20:05 -07001242 if (aec_dump_) {
1243 RecordUnprocessedCaptureStream(*frame);
1244 }
1245
Per Åhgrena1351272019-08-15 12:15:46 +02001246 capture_.vad_activity = frame->vad_activity_;
Steve Antonf254e9e2019-08-21 17:52:28 +00001247 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001248 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001249 if (submodule_states_.CaptureMultiBandProcessingActive() ||
1250 submodule_states_.CaptureFullBandProcessingActive()) {
Steve Antonf254e9e2019-08-21 17:52:28 +00001251 capture_.capture_audio->InterleaveTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001252 }
1253 frame->vad_activity_ = capture_.vad_activity;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001254
aleloi868f32f2017-05-23 07:20:05 -07001255 if (aec_dump_) {
1256 RecordProcessedCaptureStream(*frame);
1257 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001258
1259 return kNoError;
1260}
1261
peahde65ddc2016-09-16 15:02:15 -07001262int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001263 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001264
peahb58a1582016-03-15 09:34:24 -07001265 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001266 // TODO(peah): Simplify once the public API Enable functions for these
1267 // are moved to APM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001268 RTC_DCHECK_LE(!!private_submodules_->echo_controller +
1269 !!private_submodules_->echo_cancellation +
1270 !!private_submodules_->echo_control_mobile,
1271 1);
peahb58a1582016-03-15 09:34:24 -07001272
peahde65ddc2016-09-16 15:02:15 -07001273 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001274
Alex Loikob5c9a792018-04-16 16:31:22 +02001275 if (private_submodules_->pre_amplifier) {
1276 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
Steve Antonf254e9e2019-08-21 17:52:28 +00001277 capture_buffer->channels_f(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001278 capture_buffer->num_frames()));
1279 }
1280
Per Åhgren928146f2019-08-20 09:19:21 +02001281 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Steve Antonf254e9e2019-08-21 17:52:28 +00001282 capture_buffer->channels_const_f()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001283 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001284 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1285 if (log_rms) {
1286 capture_rms_interval_counter_ = 0;
1287 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001288 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1289 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1290 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1291 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001292 }
1293
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001294 if (private_submodules_->echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001295 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001296 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001297 capture_.echo_path_gain_change =
1298 capture_.prev_analog_mic_level != analog_mic_level &&
1299 capture_.prev_analog_mic_level != -1;
1300 capture_.prev_analog_mic_level = analog_mic_level;
1301
Per Åhgrend2650d12018-10-02 17:00:59 +02001302 // Detect and flag any change in the pre-amplifier gain.
1303 if (private_submodules_->pre_amplifier) {
1304 float pre_amp_gain = private_submodules_->pre_amplifier->GetGainFactor();
1305 capture_.echo_path_gain_change =
1306 capture_.echo_path_gain_change ||
1307 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001308 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001309 capture_.prev_pre_amp_gain = pre_amp_gain;
1310 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001311
1312 // Detect volume change.
1313 capture_.echo_path_gain_change =
1314 capture_.echo_path_gain_change ||
1315 (capture_.prev_playout_volume != capture_.playout_volume &&
1316 capture_.prev_playout_volume >= 0);
1317 capture_.prev_playout_volume = capture_.playout_volume;
1318
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001319 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001320 }
1321
peahbe615622016-02-13 16:40:47 -08001322 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001323 public_submodules_->gain_control->is_enabled()) {
1324 private_submodules_->agc_manager->AnalyzePreProcess(
Per Åhgren928146f2019-08-20 09:19:21 +02001325 capture_buffer->channels_f()[0], capture_buffer->num_channels(),
peahde65ddc2016-09-16 15:02:15 -07001326 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001327
1328 if (constants_.use_experimental_agc_process_before_aec) {
1329 private_submodules_->agc_manager->Process(
Steve Antonf254e9e2019-08-21 17:52:28 +00001330 capture_buffer->channels_const_f()[0],
Alex Loikod9342442018-09-10 13:59:41 +02001331 capture_nonlocked_.capture_processing_format.num_frames(),
1332 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1333 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001334 }
1335
peah2ace3f92016-09-10 04:42:27 -07001336 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1337 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001338 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1339 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001340 }
1341
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001342 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001343 // Force down-mixing of the number of channels after the detection of
1344 // capture signal saturation.
1345 // TODO(peah): Look into ensuring that this kind of tampering with the
1346 // AudioBuffer functionality should not be needed.
1347 capture_buffer->set_num_channels(1);
1348 }
1349
peahe0eae3c2016-12-14 01:16:23 -08001350 // TODO(peah): Move the AEC3 low-cut filter to this place.
1351 if (private_submodules_->low_cut_filter &&
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001352 !private_submodules_->echo_controller) {
peah8271d042016-11-22 07:24:52 -08001353 private_submodules_->low_cut_filter->Process(capture_buffer);
1354 }
peahde65ddc2016-09-16 15:02:15 -07001355 RETURN_ON_ERR(
1356 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1357 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001358
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001359 if (private_submodules_->echo_control_mobile) {
1360 // Ensure that the stream delay was set before the call to the
1361 // AECM ProcessCaptureAudio function.
1362 if (!was_stream_delay_set()) {
1363 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001364 }
1365
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001366 if (public_submodules_->noise_suppression->is_enabled()) {
Per Åhgrena1351272019-08-15 12:15:46 +02001367 private_submodules_->echo_control_mobile->CopyLowPassReference(
1368 capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001369 }
peahe0eae3c2016-12-14 01:16:23 -08001370
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001371 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah253534d2016-03-15 04:32:28 -07001372
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001373 RETURN_ON_ERR(private_submodules_->echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001374 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001375 } else {
1376 if (private_submodules_->echo_controller) {
1377 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1378
1379 if (was_stream_delay_set()) {
1380 private_submodules_->echo_controller->SetAudioBufferDelay(
1381 stream_delay_ms());
1382 }
1383
1384 private_submodules_->echo_controller->ProcessCapture(
1385 capture_buffer, capture_.echo_path_gain_change);
1386 } else if (private_submodules_->echo_cancellation) {
1387 // Ensure that the stream delay was set before the call to the
1388 // AEC ProcessCaptureAudio function.
1389 if (!was_stream_delay_set()) {
1390 return AudioProcessing::kStreamParameterNotSetError;
1391 }
1392
1393 RETURN_ON_ERR(private_submodules_->echo_cancellation->ProcessCaptureAudio(
1394 capture_buffer, stream_delay_ms()));
1395 }
1396
1397 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
Per Åhgren46537a32017-06-07 10:08:10 +02001398 }
ivoc9f4a4a02016-10-28 05:39:16 -07001399
Per Åhgrena1351272019-08-15 12:15:46 +02001400 if (public_submodules_->voice_detection->is_enabled() &&
1401 !public_submodules_->voice_detection->using_external_vad()) {
1402 bool voice_active =
1403 public_submodules_->voice_detection->ProcessCaptureAudio(
1404 capture_buffer);
1405 capture_.vad_activity =
1406 voice_active ? AudioFrame::kVadActive : AudioFrame::kVadPassive;
1407 }
1408
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001409 if (config_.voice_detection.enabled) {
1410 private_submodules_->voice_detector->ProcessCaptureAudio(capture_buffer);
1411 capture_.stats.voice_detected =
1412 private_submodules_->voice_detector->stream_has_voice();
1413 } else {
1414 capture_.stats.voice_detected = absl::nullopt;
1415 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001416
peahbe615622016-02-13 16:40:47 -08001417 if (constants_.use_experimental_agc &&
Alex Loikod9342442018-09-10 13:59:41 +02001418 public_submodules_->gain_control->is_enabled() &&
1419 !constants_.use_experimental_agc_process_before_aec) {
peahdf3efa82015-11-28 12:35:15 -08001420 private_submodules_->agc_manager->Process(
Per Åhgren928146f2019-08-20 09:19:21 +02001421 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
peahde65ddc2016-09-16 15:02:15 -07001422 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001423 }
Per Åhgren200feba2019-03-06 04:16:46 +01001424 // TODO(peah): Add reporting from AEC3 whether there is echo.
peahb8fbb542016-03-15 02:28:08 -07001425 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001426 capture_buffer,
Per Åhgrenf204faf2019-04-25 15:18:06 +02001427 private_submodules_->echo_cancellation &&
1428 private_submodules_->echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001429
peah2ace3f92016-09-10 04:42:27 -07001430 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1431 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001432 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1433 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001434 }
1435
peah9e6a2902017-05-15 07:19:21 -07001436 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001437 RTC_DCHECK(private_submodules_->echo_detector);
1438 private_submodules_->echo_detector->AnalyzeCaptureAudio(
Steve Antonf254e9e2019-08-21 17:52:28 +00001439 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
peah9e6a2902017-05-15 07:19:21 -07001440 capture_buffer->num_frames()));
1441 }
1442
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001443 // TODO(aluebs): Investigate if the transient suppression placement should be
1444 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001445 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001446 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001447 private_submodules_->agc_manager.get()
1448 ? private_submodules_->agc_manager->voice_probability()
1449 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001450
peahdf3efa82015-11-28 12:35:15 -08001451 public_submodules_->transient_suppressor->Suppress(
Steve Antonf254e9e2019-08-21 17:52:28 +00001452 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001453 capture_buffer->num_channels(),
Steve Antonf254e9e2019-08-21 17:52:28 +00001454 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001455 capture_buffer->num_frames_per_band(),
1456 capture_.keyboard_info.keyboard_data,
1457 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001458 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001459 }
1460
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001461 // Experimental APM sub-module that analyzes |capture_buffer|.
1462 if (private_submodules_->capture_analyzer) {
1463 private_submodules_->capture_analyzer->Analyze(capture_buffer);
1464 }
1465
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001466 if (config_.gain_controller2.enabled) {
Alex Loikoa837dd72018-08-06 16:32:12 +02001467 private_submodules_->gain_controller2->NotifyAnalogLevel(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001468 agc1()->stream_analog_level());
alessiob3ec96df2017-05-22 06:57:06 -07001469 private_submodules_->gain_controller2->Process(capture_buffer);
1470 }
1471
Sam Zackrisson0beac582017-09-25 12:04:02 +02001472 if (private_submodules_->capture_post_processor) {
1473 private_submodules_->capture_post_processor->Process(capture_buffer);
1474 }
1475
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001476 // The level estimator operates on the recombined data.
Steve Antonf254e9e2019-08-21 17:52:28 +00001477 public_submodules_->level_estimator->ProcessStream(capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001478 if (config_.level_estimation.enabled) {
Steve Antonf254e9e2019-08-21 17:52:28 +00001479 private_submodules_->output_level_estimator->ProcessStream(capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001480 capture_.stats.output_rms_dbfs =
1481 private_submodules_->output_level_estimator->RMS();
1482 } else {
1483 capture_.stats.output_rms_dbfs = absl::nullopt;
1484 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001485
Per Åhgren928146f2019-08-20 09:19:21 +02001486 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Steve Antonf254e9e2019-08-21 17:52:28 +00001487 capture_buffer->channels_const_f()[0],
peah1b08dc32016-12-20 13:45:58 -08001488 capture_nonlocked_.capture_processing_format.num_frames()));
1489 if (log_rms) {
1490 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1491 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1492 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1493 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1494 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1495 }
1496
peahdf3efa82015-11-28 12:35:15 -08001497 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001498 return kNoError;
1499}
1500
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001501int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001502 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001503 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001504 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001505 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001506 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001507 const StreamConfig reverse_config = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001508 sample_rate_hz,
1509 ChannelsFromLayout(layout),
1510 LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001511 };
1512 if (samples_per_channel != reverse_config.num_frames()) {
1513 return kBadDataLengthError;
1514 }
peahdf3efa82015-11-28 12:35:15 -08001515 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001516}
1517
peahde65ddc2016-09-16 15:02:15 -07001518int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1519 const StreamConfig& input_config,
1520 const StreamConfig& output_config,
1521 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001522 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001523 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001524 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001525 if (submodule_states_.RenderMultiBandProcessingActive() ||
1526 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001527 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1528 dest);
peah2ace3f92016-09-10 04:42:27 -07001529 } else if (formats_.api_format.reverse_input_stream() !=
1530 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001531 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1532 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001533 } else {
peahde65ddc2016-09-16 15:02:15 -07001534 CopyAudioIfNeeded(src, input_config.num_frames(),
1535 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001536 }
1537
1538 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001539}
1540
peahdf3efa82015-11-28 12:35:15 -08001541int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001542 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001543 const StreamConfig& input_config,
1544 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001545 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001546 return kNullPointerError;
1547 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001548
peahde65ddc2016-09-16 15:02:15 -07001549 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001550 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001551 }
1552
peahdf3efa82015-11-28 12:35:15 -08001553 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001554 processing_config.reverse_input_stream() = input_config;
1555 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001556
peahdf3efa82015-11-28 12:35:15 -08001557 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001558 RTC_DCHECK_EQ(input_config.num_frames(),
1559 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001560
aleloi868f32f2017-05-23 07:20:05 -07001561 if (aec_dump_) {
1562 const size_t channel_size =
1563 formats_.api_format.reverse_input_stream().num_frames();
1564 const size_t num_channels =
1565 formats_.api_format.reverse_input_stream().num_channels();
1566 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001567 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001568 }
peahdf3efa82015-11-28 12:35:15 -08001569 render_.render_audio->CopyFrom(src,
1570 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001571 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001572}
1573
1574int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001575 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001576 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001577 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001578 return kNullPointerError;
1579 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001580 // Must be a native rate.
1581 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1582 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001583 frame->sample_rate_hz_ != kSampleRate32kHz &&
1584 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001585 return kBadSampleRateError;
1586 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001587
Michael Graczyk86c6d332015-07-23 11:41:39 -07001588 if (frame->num_channels_ <= 0) {
1589 return kBadNumberChannelsError;
1590 }
1591
peahdf3efa82015-11-28 12:35:15 -08001592 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001593 processing_config.reverse_input_stream().set_sample_rate_hz(
1594 frame->sample_rate_hz_);
1595 processing_config.reverse_input_stream().set_num_channels(
1596 frame->num_channels_);
1597 processing_config.reverse_output_stream().set_sample_rate_hz(
1598 frame->sample_rate_hz_);
1599 processing_config.reverse_output_stream().set_num_channels(
1600 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001601
peahdf3efa82015-11-28 12:35:15 -08001602 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001603 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001604 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001605 return kBadDataLengthError;
1606 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001607
aleloi868f32f2017-05-23 07:20:05 -07001608 if (aec_dump_) {
1609 aec_dump_->WriteRenderStreamMessage(*frame);
1610 }
1611
Steve Antonf254e9e2019-08-21 17:52:28 +00001612 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001613 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001614 if (submodule_states_.RenderMultiBandProcessingActive() ||
1615 submodule_states_.RenderFullBandProcessingActive()) {
Steve Antonf254e9e2019-08-21 17:52:28 +00001616 render_.render_audio->InterleaveTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001617 }
aluebsb0319552016-03-17 20:39:53 -07001618 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001619}
niklase@google.com470e71d2011-07-07 08:21:25 +00001620
peahde65ddc2016-09-16 15:02:15 -07001621int AudioProcessingImpl::ProcessRenderStreamLocked() {
1622 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001623
Alex Loiko73ec0192018-05-15 10:52:28 +02001624 HandleRenderRuntimeSettings();
1625
Alex Loiko5825aa62017-12-18 16:02:40 +01001626 if (private_submodules_->render_pre_processor) {
1627 private_submodules_->render_pre_processor->Process(render_buffer);
1628 }
1629
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001630 QueueNonbandedRenderAudio(render_buffer);
1631
peah2ace3f92016-09-10 04:42:27 -07001632 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001633 SampleRateSupportsMultiBand(
1634 formats_.render_processing_format.sample_rate_hz())) {
1635 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001636 }
1637
peahce4d9152017-05-19 01:28:05 -07001638 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1639 QueueBandedRenderAudio(render_buffer);
1640 }
1641
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001642 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001643 if (private_submodules_->echo_controller) {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001644 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001645 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001646
peah2ace3f92016-09-10 04:42:27 -07001647 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001648 SampleRateSupportsMultiBand(
1649 formats_.render_processing_format.sample_rate_hz())) {
1650 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001651 }
1652
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001653 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001654}
1655
1656int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001657 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001658 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001659 capture_.was_stream_delay_set = true;
1660 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001661
niklase@google.com470e71d2011-07-07 08:21:25 +00001662 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001663 delay = 0;
1664 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001665 }
1666
1667 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1668 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001669 delay = 500;
1670 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001671 }
1672
peahdf3efa82015-11-28 12:35:15 -08001673 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001674 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001675}
1676
1677int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001678 // Used as callback from submodules, hence locking is not allowed.
1679 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001680}
1681
1682bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001683 // Used as callback from submodules, hence locking is not allowed.
1684 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001685}
1686
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001687void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001688 rtc::CritScope cs(&crit_capture_);
1689 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001690}
1691
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001692void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001693 rtc::CritScope cs(&crit_capture_);
1694 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001695}
1696
1697int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001698 rtc::CritScope cs(&crit_capture_);
1699 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001700}
1701
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001702void AudioProcessingImpl::set_stream_analog_level(int level) {
1703 rtc::CritScope cs_capture(&crit_capture_);
1704 int error = agc1()->set_stream_analog_level(level);
1705 RTC_DCHECK_EQ(kNoError, error);
1706}
1707
1708int AudioProcessingImpl::recommended_stream_analog_level() const {
1709 rtc::CritScope cs_capture(&crit_capture_);
1710 return agc1()->stream_analog_level();
1711}
1712
aleloi868f32f2017-05-23 07:20:05 -07001713void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1714 RTC_DCHECK(aec_dump);
1715 rtc::CritScope cs_render(&crit_render_);
1716 rtc::CritScope cs_capture(&crit_capture_);
1717
1718 // The previously attached AecDump will be destroyed with the
1719 // 'aec_dump' parameter, which is after locks are released.
1720 aec_dump_.swap(aec_dump);
1721 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001722 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001723}
1724
1725void AudioProcessingImpl::DetachAecDump() {
1726 // The d-tor of a task-queue based AecDump blocks until all pending
1727 // tasks are done. This construction avoids blocking while holding
1728 // the render and capture locks.
1729 std::unique_ptr<AecDump> aec_dump = nullptr;
1730 {
1731 rtc::CritScope cs_render(&crit_render_);
1732 rtc::CritScope cs_capture(&crit_capture_);
1733 aec_dump = std::move(aec_dump_);
1734 }
1735}
1736
Sam Zackrisson4d364492018-03-02 16:03:21 +01001737void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1738 std::unique_ptr<AudioGenerator> audio_generator) {
1739 // TODO(bugs.webrtc.org/8882) Stub.
1740 // Reset internal audio generator with audio_generator.
1741}
1742
1743void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1744 // TODO(bugs.webrtc.org/8882) Stub.
1745 // Delete audio generator, if one is attached.
1746}
1747
Ivo Creusen56d46092017-11-24 17:29:59 +01001748AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001749 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001750 rtc::CritScope cs_capture(&crit_capture_);
1751 if (!has_remote_tracks) {
1752 return capture_.stats;
1753 }
1754 AudioProcessingStats stats = capture_.stats;
1755 EchoCancellationImpl::Metrics metrics;
1756 if (private_submodules_->echo_controller) {
1757 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1758 stats.echo_return_loss = ec_metrics.echo_return_loss;
1759 stats.echo_return_loss_enhancement =
1760 ec_metrics.echo_return_loss_enhancement;
1761 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001762 }
1763 if (config_.residual_echo_detector.enabled) {
1764 RTC_DCHECK(private_submodules_->echo_detector);
1765 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1766 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1767 stats.residual_echo_likelihood_recent_max =
1768 ed_metrics.echo_likelihood_recent_max;
1769 }
Ivo Creusenae026092017-11-20 13:07:16 +01001770 return stats;
1771}
1772
niklase@google.com470e71d2011-07-07 08:21:25 +00001773GainControl* AudioProcessingImpl::gain_control() const {
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001774 return public_submodules_->gain_control_config_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001775}
1776
niklase@google.com470e71d2011-07-07 08:21:25 +00001777LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001778 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001779}
1780
1781NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
Sam Zackrisson23513132019-01-11 15:10:32 +01001782 return public_submodules_->noise_suppression_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001783}
1784
1785VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001786 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001787}
1788
peah8271d042016-11-22 07:24:52 -08001789void AudioProcessingImpl::MutateConfig(
1790 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1791 rtc::CritScope cs_render(&crit_render_);
1792 rtc::CritScope cs_capture(&crit_capture_);
1793 mutator(&config_);
1794 ApplyConfig(config_);
1795}
1796
1797AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1798 rtc::CritScope cs_render(&crit_render_);
1799 rtc::CritScope cs_capture(&crit_capture_);
1800 return config_;
1801}
1802
peah2ace3f92016-09-10 04:42:27 -07001803bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1804 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001805 config_.high_pass_filter.enabled,
Per Åhgrend547d862019-05-03 15:48:47 +02001806 !!private_submodules_->echo_cancellation,
1807 !!private_submodules_->echo_control_mobile,
ivoc9f4a4a02016-10-28 05:39:16 -07001808 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001809 public_submodules_->noise_suppression->is_enabled(),
peah2ace3f92016-09-10 04:42:27 -07001810 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001811 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001812 capture_nonlocked_.echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -07001813 public_submodules_->voice_detection->is_enabled(),
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001814 config_.voice_detection.enabled,
peah2ace3f92016-09-10 04:42:27 -07001815 public_submodules_->level_estimator->is_enabled(),
1816 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001817}
1818
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001819void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001820 if (capture_.transient_suppressor_enabled) {
1821 if (!public_submodules_->transient_suppressor.get()) {
1822 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001823 }
peahdf3efa82015-11-28 12:35:15 -08001824 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001825 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1826 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001827 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001828}
1829
peah8271d042016-11-22 07:24:52 -08001830void AudioProcessingImpl::InitializeLowCutFilter() {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001831 if (submodule_states_.LowCutFilteringRequired()) {
peah8271d042016-11-22 07:24:52 -08001832 private_submodules_->low_cut_filter.reset(
1833 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1834 } else {
1835 private_submodules_->low_cut_filter.reset();
1836 }
1837}
alessiob3ec96df2017-05-22 06:57:06 -07001838
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001839void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001840 bool use_echo_controller =
1841 echo_control_factory_ ||
Per Åhgren200feba2019-03-06 04:16:46 +01001842 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode &&
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001843 !config_.echo_canceller.use_legacy_aec);
1844
1845 if (use_echo_controller) {
1846 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001847 if (echo_control_factory_) {
1848 private_submodules_->echo_controller =
1849 echo_control_factory_->Create(proc_sample_rate_hz());
1850 } else {
1851 private_submodules_->echo_controller = absl::make_unique<EchoCanceller3>(
1852 EchoCanceller3Config(), proc_sample_rate_hz(), true);
1853 }
1854
1855 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001856
Per Åhgrenf204faf2019-04-25 15:18:06 +02001857 private_submodules_->echo_cancellation.reset();
1858 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001859 private_submodules_->echo_control_mobile.reset();
1860 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001861 return;
peahe0eae3c2016-12-14 01:16:23 -08001862 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001863
1864 private_submodules_->echo_controller.reset();
1865 capture_nonlocked_.echo_controller_enabled = false;
1866
1867 if (!config_.echo_canceller.enabled) {
1868 private_submodules_->echo_cancellation.reset();
1869 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001870 private_submodules_->echo_control_mobile.reset();
1871 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001872 return;
1873 }
1874
1875 if (config_.echo_canceller.mobile_mode) {
1876 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001877 size_t max_element_size =
1878 std::max(static_cast<size_t>(1),
1879 kMaxAllowedValuesOfSamplesPerBand *
1880 EchoControlMobileImpl::NumCancellersRequired(
1881 num_output_channels(), num_reverse_channels()));
1882
1883 std::vector<int16_t> template_queue_element(max_element_size);
1884
1885 aecm_render_signal_queue_.reset(
1886 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1887 kMaxNumFramesToBuffer, template_queue_element,
1888 RenderQueueItemVerifier<int16_t>(max_element_size)));
1889
1890 aecm_render_queue_buffer_.resize(max_element_size);
1891 aecm_capture_queue_buffer_.resize(max_element_size);
1892
1893 private_submodules_->echo_control_mobile.reset(new EchoControlMobileImpl());
1894
1895 private_submodules_->echo_control_mobile->Initialize(
1896 proc_split_sample_rate_hz(), num_reverse_channels(),
1897 num_output_channels());
1898
Per Åhgrenf204faf2019-04-25 15:18:06 +02001899 private_submodules_->echo_cancellation.reset();
1900 aec_render_signal_queue_.reset();
1901 return;
1902 }
1903
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001904 private_submodules_->echo_control_mobile.reset();
1905 aecm_render_signal_queue_.reset();
1906
Per Åhgrenf204faf2019-04-25 15:18:06 +02001907 // Create and activate AEC2.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001908 private_submodules_->echo_cancellation.reset(new EchoCancellationImpl());
1909 private_submodules_->echo_cancellation->SetExtraOptions(
1910 capture_nonlocked_.use_aec2_extended_filter,
1911 capture_nonlocked_.use_aec2_delay_agnostic,
1912 capture_nonlocked_.use_aec2_refined_adaptive_filter);
1913
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001914 size_t element_max_size =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001915 std::max(static_cast<size_t>(1),
1916 kMaxAllowedValuesOfSamplesPerBand *
1917 EchoCancellationImpl::NumCancellersRequired(
1918 num_output_channels(), num_reverse_channels()));
1919
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001920 std::vector<float> template_queue_element(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001921
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001922 aec_render_signal_queue_.reset(
1923 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1924 kMaxNumFramesToBuffer, template_queue_element,
1925 RenderQueueItemVerifier<float>(element_max_size)));
Per Åhgrenf204faf2019-04-25 15:18:06 +02001926
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001927 aec_render_queue_buffer_.resize(element_max_size);
1928 aec_capture_queue_buffer_.resize(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001929
1930 private_submodules_->echo_cancellation->Initialize(
1931 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
1932 num_proc_channels());
1933
Per Åhgrenf204faf2019-04-25 15:18:06 +02001934 private_submodules_->echo_cancellation->set_suppression_level(
1935 config_.echo_canceller.legacy_moderate_suppression_level
1936 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
1937 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
peahe0eae3c2016-12-14 01:16:23 -08001938}
peah8271d042016-11-22 07:24:52 -08001939
alessiob3ec96df2017-05-22 06:57:06 -07001940void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001941 if (config_.gain_controller2.enabled) {
1942 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001943 }
1944}
1945
Alex Loikob5c9a792018-04-16 16:31:22 +02001946void AudioProcessingImpl::InitializePreAmplifier() {
1947 if (config_.pre_amplifier.enabled) {
1948 private_submodules_->pre_amplifier.reset(
1949 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1950 } else {
1951 private_submodules_->pre_amplifier.reset();
1952 }
1953}
1954
ivoc9f4a4a02016-10-28 05:39:16 -07001955void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001956 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001957 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001958 proc_sample_rate_hz(), 1,
1959 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001960}
1961
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001962void AudioProcessingImpl::InitializeAnalyzer() {
1963 if (private_submodules_->capture_analyzer) {
1964 private_submodules_->capture_analyzer->Initialize(proc_sample_rate_hz(),
1965 num_proc_channels());
1966 }
1967}
1968
Sam Zackrisson0beac582017-09-25 12:04:02 +02001969void AudioProcessingImpl::InitializePostProcessor() {
1970 if (private_submodules_->capture_post_processor) {
1971 private_submodules_->capture_post_processor->Initialize(
1972 proc_sample_rate_hz(), num_proc_channels());
1973 }
1974}
1975
Alex Loiko5825aa62017-12-18 16:02:40 +01001976void AudioProcessingImpl::InitializePreProcessor() {
1977 if (private_submodules_->render_pre_processor) {
1978 private_submodules_->render_pre_processor->Initialize(
1979 formats_.render_processing_format.sample_rate_hz(),
1980 formats_.render_processing_format.num_channels());
1981 }
1982}
1983
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001984void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001985
aleloi868f32f2017-05-23 07:20:05 -07001986void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1987 if (!aec_dump_) {
1988 return;
1989 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001990
1991 std::string experiments_description = "";
1992 if (private_submodules_->echo_cancellation) {
1993 experiments_description +=
1994 private_submodules_->echo_cancellation->GetExperimentsDescription();
1995 }
aleloi868f32f2017-05-23 07:20:05 -07001996 // TODO(peah): Add semicolon-separated concatenations of experiment
1997 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001998 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1999 experiments_description += "AgcClippingLevelExperiment;";
2000 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02002001 if (capture_nonlocked_.echo_controller_enabled) {
2002 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07002003 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02002004 if (config_.gain_controller2.enabled) {
2005 experiments_description += "GainController2;";
2006 }
aleloi868f32f2017-05-23 07:20:05 -07002007
2008 InternalAPMConfig apm_config;
2009
Per Åhgren200feba2019-03-06 04:16:46 +01002010 apm_config.aec_enabled = config_.echo_canceller.enabled;
aleloi868f32f2017-05-23 07:20:05 -07002011 apm_config.aec_delay_agnostic_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002012 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002013 private_submodules_->echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002014 apm_config.aec_drift_compensation_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002015 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002016 private_submodules_->echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002017 apm_config.aec_extended_filter_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002018 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002019 private_submodules_->echo_cancellation->is_extended_filter_enabled();
Per Åhgrenf204faf2019-04-25 15:18:06 +02002020 apm_config.aec_suppression_level =
2021 private_submodules_->echo_cancellation
2022 ? static_cast<int>(
2023 private_submodules_->echo_cancellation->suppression_level())
2024 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002025
Per Åhgrend547d862019-05-03 15:48:47 +02002026 apm_config.aecm_enabled = !!private_submodules_->echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002027 apm_config.aecm_comfort_noise_enabled =
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002028 private_submodules_->echo_control_mobile &&
Sam Zackrissonc22f5512018-11-05 16:10:00 +01002029 private_submodules_->echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002030 apm_config.aecm_routing_mode =
2031 private_submodules_->echo_control_mobile
2032 ? static_cast<int>(
2033 private_submodules_->echo_control_mobile->routing_mode())
2034 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002035
2036 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2037 apm_config.agc_mode =
2038 static_cast<int>(public_submodules_->gain_control->mode());
2039 apm_config.agc_limiter_enabled =
2040 public_submodules_->gain_control->is_limiter_enabled();
2041 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2042
2043 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2044
2045 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2046 apm_config.ns_level =
2047 static_cast<int>(public_submodules_->noise_suppression->level());
2048
2049 apm_config.transient_suppression_enabled =
2050 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002051 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002052 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2053 apm_config.pre_amplifier_fixed_gain_factor =
2054 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002055
2056 if (!forced && apm_config == apm_config_for_aec_dump_) {
2057 return;
2058 }
2059 aec_dump_->WriteConfig(apm_config);
2060 apm_config_for_aec_dump_ = apm_config;
2061}
2062
2063void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2064 const float* const* src) {
2065 RTC_DCHECK(aec_dump_);
2066 WriteAecDumpConfigMessage(false);
2067
2068 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2069 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2070 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002071 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002072 RecordAudioProcessingState();
2073}
2074
2075void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2076 const AudioFrame& capture_frame) {
2077 RTC_DCHECK(aec_dump_);
2078 WriteAecDumpConfigMessage(false);
2079
2080 aec_dump_->AddCaptureStreamInput(capture_frame);
2081 RecordAudioProcessingState();
2082}
2083
2084void AudioProcessingImpl::RecordProcessedCaptureStream(
2085 const float* const* processed_capture_stream) {
2086 RTC_DCHECK(aec_dump_);
2087
2088 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2089 const size_t num_channels =
2090 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002091 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2092 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002093 aec_dump_->WriteCaptureStreamMessage();
2094}
2095
2096void AudioProcessingImpl::RecordProcessedCaptureStream(
2097 const AudioFrame& processed_capture_frame) {
2098 RTC_DCHECK(aec_dump_);
2099
2100 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2101 aec_dump_->WriteCaptureStreamMessage();
2102}
2103
2104void AudioProcessingImpl::RecordAudioProcessingState() {
2105 RTC_DCHECK(aec_dump_);
2106 AecDump::AudioProcessingState audio_proc_state;
2107 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2108 audio_proc_state.drift =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002109 private_submodules_->echo_cancellation
2110 ? private_submodules_->echo_cancellation->stream_drift_samples()
2111 : 0;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01002112 audio_proc_state.level = agc1()->stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002113 audio_proc_state.keypress = capture_.key_pressed;
2114 aec_dump_->AddAudioProcessingState(audio_proc_state);
2115}
2116
kwiberg83ffe452016-08-29 14:46:07 -07002117AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002118 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002119 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002120 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002121 output_will_be_muted(false),
2122 key_pressed(false),
2123 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002124 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002125 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002126 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002127 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002128 prev_pre_amp_gain(-1.f),
2129 playout_volume(-1),
2130 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002131
2132AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2133
Per Åhgrena1351272019-08-15 12:15:46 +02002134void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2135 const float* const* data,
2136 const StreamConfig& stream_config) {
2137 if (stream_config.has_keyboard()) {
2138 keyboard_data = data[stream_config.num_channels()];
2139 } else {
2140 keyboard_data = NULL;
2141 }
2142 num_keyboard_frames = stream_config.num_frames();
2143}
2144
kwiberg83ffe452016-08-29 14:46:07 -07002145AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2146
2147AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2148
niklase@google.com470e71d2011-07-07 08:21:25 +00002149} // namespace webrtc