blob: bc61b523b95fa94978e29b5024acd9ae86d81dcd [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"
Per Åhgren0aefbf02019-08-23 21:29:17 +020035#include "modules/audio_processing/high_pass_filter.h"
Yves Gerey988cc082018-10-23 12:03:01 +020036#include "modules/audio_processing/include/audio_frame_view.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020037#include "modules/audio_processing/level_estimator_impl.h"
Per Åhgren13735822018-02-12 21:42:56 +010038#include "modules/audio_processing/logging/apm_data_dumper.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020039#include "modules/audio_processing/noise_suppression_impl.h"
Sam Zackrisson23513132019-01-11 15:10:32 +010040#include "modules/audio_processing/noise_suppression_proxy.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020041#include "modules/audio_processing/residual_echo_detector.h"
42#include "modules/audio_processing/transient/transient_suppressor.h"
43#include "modules/audio_processing/voice_detection_impl.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080048#include "rtc_base/ref_counted_object.h"
49#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "rtc_base/trace_event.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000052
Michael Graczyk86c6d332015-07-23 11:41:39 -070053#define RETURN_ON_ERR(expr) \
54 do { \
55 int err = (expr); \
56 if (err != kNoError) { \
57 return err; \
58 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000059 } while (0)
60
niklase@google.com470e71d2011-07-07 08:21:25 +000061namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070062
kwibergd59d3bb2016-09-13 07:49:33 -070063constexpr int AudioProcessing::kNativeSampleRatesHz[];
Alex Loiko73ec0192018-05-15 10:52:28 +020064constexpr int kRuntimeSettingQueueSize = 100;
aluebsdf6416a2016-03-16 18:26:35 -070065
Michael Graczyk86c6d332015-07-23 11:41:39 -070066namespace {
67
68static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
69 switch (layout) {
70 case AudioProcessing::kMono:
71 case AudioProcessing::kStereo:
72 return false;
73 case AudioProcessing::kMonoAndKeyboard:
74 case AudioProcessing::kStereoAndKeyboard:
75 return true;
76 }
77
kwiberg9e2be5f2016-09-14 05:23:22 -070078 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070079 return false;
80}
aluebsdf6416a2016-03-16 18:26:35 -070081
peah2ace3f92016-09-10 04:42:27 -070082bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070083 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
84 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
85}
86
Per Åhgrenc8626b62019-08-23 15:49:51 +020087// Identify the native processing rate that best handles a sample rate.
88int SuitableProcessRate(int minimum_rate, bool band_splitting_required) {
peah2ace3f92016-09-10 04:42:27 -070089#ifdef WEBRTC_ARCH_ARM_FAMILY
Per Åhgrenc8626b62019-08-23 15:49:51 +020090 constexpr int kMaxSplittingRate = 32000;
peah2ace3f92016-09-10 04:42:27 -070091#else
Per Åhgrenc8626b62019-08-23 15:49:51 +020092 constexpr int kMaxSplittingRate = 48000;
peah2ace3f92016-09-10 04:42:27 -070093#endif
Per Åhgrenc8626b62019-08-23 15:49:51 +020094 static_assert(kMaxSplittingRate <= 48000, "");
95 const int uppermost_native_rate =
96 band_splitting_required ? kMaxSplittingRate : 48000;
97 for (auto rate : {16000, 32000, 48000}) {
peah2ace3f92016-09-10 04:42:27 -070098 if (rate >= uppermost_native_rate) {
99 return uppermost_native_rate;
100 }
101 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700102 return rate;
103 }
104 }
peah2ace3f92016-09-10 04:42:27 -0700105 RTC_NOTREACHED();
106 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700107}
108
Sam Zackrisson23513132019-01-11 15:10:32 +0100109NoiseSuppression::Level NsConfigLevelToInterfaceLevel(
110 AudioProcessing::Config::NoiseSuppression::Level level) {
111 using NsConfig = AudioProcessing::Config::NoiseSuppression;
112 switch (level) {
113 case NsConfig::kLow:
114 return NoiseSuppression::kLow;
115 case NsConfig::kModerate:
116 return NoiseSuppression::kModerate;
117 case NsConfig::kHigh:
118 return NoiseSuppression::kHigh;
119 case NsConfig::kVeryHigh:
120 return NoiseSuppression::kVeryHigh;
121 default:
122 RTC_NOTREACHED();
123 }
124}
125
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100126GainControl::Mode Agc1ConfigModeToInterfaceMode(
127 AudioProcessing::Config::GainController1::Mode mode) {
128 using Agc1Config = AudioProcessing::Config::GainController1;
129 switch (mode) {
130 case Agc1Config::kAdaptiveAnalog:
131 return GainControl::kAdaptiveAnalog;
132 case Agc1Config::kAdaptiveDigital:
133 return GainControl::kAdaptiveDigital;
134 case Agc1Config::kFixedDigital:
135 return GainControl::kFixedDigital;
136 }
137}
138
peah9e6a2902017-05-15 07:19:21 -0700139// Maximum lengths that frame of samples being passed from the render side to
140// the capture side can have (does not apply to AEC3).
141static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
142static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
143
peah764e3642016-10-22 05:04:30 -0700144// Maximum number of frames to buffer in the render queue.
145// TODO(peah): Decrease this once we properly handle hugely unbalanced
146// reverse and forward call numbers.
147static const size_t kMaxNumFramesToBuffer = 100;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700148} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000149
150// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000151static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000152
Sam Zackrisson0beac582017-09-25 12:04:02 +0200153AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100154 bool capture_post_processor_enabled,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200155 bool render_pre_processor_enabled,
156 bool capture_analyzer_enabled)
Alex Loiko5825aa62017-12-18 16:02:40 +0100157 : capture_post_processor_enabled_(capture_post_processor_enabled),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200158 render_pre_processor_enabled_(render_pre_processor_enabled),
159 capture_analyzer_enabled_(capture_analyzer_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700160
161bool AudioProcessingImpl::ApmSubmoduleStates::Update(
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200162 bool high_pass_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700163 bool echo_canceller_enabled,
164 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700165 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700166 bool noise_suppressor_enabled,
peah2ace3f92016-09-10 04:42:27 -0700167 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700168 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200169 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200170 bool echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -0700171 bool voice_activity_detector_enabled,
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100172 bool private_voice_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700173 bool level_estimator_enabled,
174 bool transient_suppressor_enabled) {
175 bool changed = false;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200176 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700177 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
178 changed |=
179 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700180 changed |=
181 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700182 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
183 changed |=
peah2ace3f92016-09-10 04:42:27 -0700184 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200185 changed |= (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200186 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200187 changed |= (echo_controller_enabled != echo_controller_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700188 changed |= (level_estimator_enabled != level_estimator_enabled_);
189 changed |=
190 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100191 changed |=
192 (private_voice_detector_enabled != private_voice_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700193 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
194 if (changed) {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200195 high_pass_filter_enabled_ = high_pass_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700196 echo_canceller_enabled_ = echo_canceller_enabled;
197 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700198 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700199 noise_suppressor_enabled_ = noise_suppressor_enabled;
peah2ace3f92016-09-10 04:42:27 -0700200 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700201 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200202 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200203 echo_controller_enabled_ = echo_controller_enabled;
peah2ace3f92016-09-10 04:42:27 -0700204 level_estimator_enabled_ = level_estimator_enabled;
205 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100206 private_voice_detector_enabled_ = private_voice_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700207 transient_suppressor_enabled_ = transient_suppressor_enabled;
208 }
209
210 changed |= first_update_;
211 first_update_ = false;
212 return changed;
213}
214
215bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
216 const {
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100217 return CaptureMultiBandProcessingActive() ||
218 voice_activity_detector_enabled_ || private_voice_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700219}
220
221bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
222 const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200223 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700224 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200225 adaptive_gain_controller_enabled_ || echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700226}
227
peah23ac8b42017-05-23 05:33:56 -0700228bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
229 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200230 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
231 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700232}
233
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200234bool AudioProcessingImpl::ApmSubmoduleStates::CaptureAnalyzerActive() const {
235 return capture_analyzer_enabled_;
236}
237
peah2ace3f92016-09-10 04:42:27 -0700238bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
239 const {
240 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800241 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200242 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700243}
244
Alex Loiko5825aa62017-12-18 16:02:40 +0100245bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive()
246 const {
247 return render_pre_processor_enabled_;
248}
249
peah2ace3f92016-09-10 04:42:27 -0700250bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
251 const {
peah2ace3f92016-09-10 04:42:27 -0700252 return false;
peah2ace3f92016-09-10 04:42:27 -0700253}
254
Per Åhgren0aefbf02019-08-23 21:29:17 +0200255bool AudioProcessingImpl::ApmSubmoduleStates::HighPassFilteringRequired()
256 const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200257 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
258 mobile_echo_controller_enabled_ || noise_suppressor_enabled_;
259}
260
solenberg5e465c32015-12-08 13:22:33 -0800261struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800262 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800263 // Accessed externally of APM without any lock acquired.
Sam Zackrisson23513132019-01-11 15:10:32 +0100264 // TODO(bugs.webrtc.org/9947): Move these submodules into private_submodules_
265 // when their pointer-to-submodule API functions are gone.
kwiberg88788ad2016-02-19 07:04:49 -0800266 std::unique_ptr<LevelEstimatorImpl> level_estimator;
267 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
Sam Zackrisson23513132019-01-11 15:10:32 +0100268 std::unique_ptr<NoiseSuppressionProxy> noise_suppression_proxy;
kwiberg88788ad2016-02-19 07:04:49 -0800269 std::unique_ptr<VoiceDetectionImpl> voice_detection;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100270 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800271 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800272 gain_control_for_experimental_agc;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100273 std::unique_ptr<GainControlConfigProxy> gain_control_config_proxy;
solenberg5e465c32015-12-08 13:22:33 -0800274
275 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800276 std::unique_ptr<TransientSuppressor> transient_suppressor;
solenberg5e465c32015-12-08 13:22:33 -0800277};
278
279struct AudioProcessingImpl::ApmPrivateSubmodules {
Sam Zackrissondb389722018-06-21 10:12:24 +0200280 ApmPrivateSubmodules(std::unique_ptr<CustomProcessing> capture_post_processor,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100281 std::unique_ptr<CustomProcessing> render_pre_processor,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200282 rtc::scoped_refptr<EchoDetector> echo_detector,
283 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Sam Zackrissondb389722018-06-21 10:12:24 +0200284 : echo_detector(std::move(echo_detector)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100285 capture_post_processor(std::move(capture_post_processor)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200286 render_pre_processor(std::move(render_pre_processor)),
287 capture_analyzer(std::move(capture_analyzer)) {}
solenberg5e465c32015-12-08 13:22:33 -0800288 // Accessed internally from capture or during initialization
kwiberg88788ad2016-02-19 07:04:49 -0800289 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700290 std::unique_ptr<GainController2> gain_controller2;
Per Åhgren0aefbf02019-08-23 21:29:17 +0200291 std::unique_ptr<HighPassFilter> high_pass_filter;
Ivo Creusend1f970d2018-06-14 11:02:03 +0200292 rtc::scoped_refptr<EchoDetector> echo_detector;
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100293 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
Sam Zackrissonc22f5512018-11-05 16:10:00 +0100294 std::unique_ptr<EchoControl> echo_controller;
295 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
Alex Loiko5825aa62017-12-18 16:02:40 +0100296 std::unique_ptr<CustomProcessing> capture_post_processor;
297 std::unique_ptr<CustomProcessing> render_pre_processor;
Alex Loikob5c9a792018-04-16 16:31:22 +0200298 std::unique_ptr<GainApplier> pre_amplifier;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200299 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100300 std::unique_ptr<LevelEstimatorImpl> output_level_estimator;
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100301 std::unique_ptr<VoiceDetectionImpl> voice_detector;
solenberg5e465c32015-12-08 13:22:33 -0800302};
303
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100304AudioProcessingBuilder::AudioProcessingBuilder() = default;
305AudioProcessingBuilder::~AudioProcessingBuilder() = default;
306
307AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
308 std::unique_ptr<CustomProcessing> capture_post_processing) {
309 capture_post_processing_ = std::move(capture_post_processing);
310 return *this;
311}
312
313AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
314 std::unique_ptr<CustomProcessing> render_pre_processing) {
315 render_pre_processing_ = std::move(render_pre_processing);
316 return *this;
317}
318
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200319AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
320 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
321 capture_analyzer_ = std::move(capture_analyzer);
322 return *this;
323}
324
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100325AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
326 std::unique_ptr<EchoControlFactory> echo_control_factory) {
327 echo_control_factory_ = std::move(echo_control_factory);
328 return *this;
329}
330
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100331AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200332 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100333 echo_detector_ = std::move(echo_detector);
334 return *this;
335}
336
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100337AudioProcessing* AudioProcessingBuilder::Create() {
338 webrtc::Config config;
339 return Create(config);
340}
341
342AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100343 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
344 config, std::move(capture_post_processing_),
345 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200346 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100347 if (apm->Initialize() != AudioProcessing::kNoError) {
348 delete apm;
349 apm = nullptr;
350 }
351 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100352}
353
peah88ac8532016-09-12 16:47:25 -0700354AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200355 : AudioProcessingImpl(config, nullptr, nullptr, nullptr, nullptr, nullptr) {
356}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000357
Per Åhgren13735822018-02-12 21:42:56 +0100358int AudioProcessingImpl::instance_count_ = 0;
359
Sam Zackrisson0beac582017-09-25 12:04:02 +0200360AudioProcessingImpl::AudioProcessingImpl(
361 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100362 std::unique_ptr<CustomProcessing> capture_post_processor,
363 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200364 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200365 rtc::scoped_refptr<EchoDetector> echo_detector,
366 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100367 : data_dumper_(
368 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200369 capture_runtime_settings_(kRuntimeSettingQueueSize),
370 render_runtime_settings_(kRuntimeSettingQueueSize),
371 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
372 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200373 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200374 submodule_states_(!!capture_post_processor,
375 !!render_pre_processor,
376 !!capture_analyzer),
peah8271d042016-11-22 07:24:52 -0800377 public_submodules_(new ApmPublicSubmodules()),
Sam Zackrisson0beac582017-09-25 12:04:02 +0200378 private_submodules_(
Sam Zackrissondb389722018-06-21 10:12:24 +0200379 new ApmPrivateSubmodules(std::move(capture_post_processor),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100380 std::move(render_pre_processor),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200381 std::move(echo_detector),
382 std::move(capture_analyzer))),
peahdf3efa82015-11-28 12:35:15 -0800383 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800384 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000385#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alex Loikod9342442018-09-10 13:59:41 +0200386 /* enabled= */ false,
387 /* enabled_agc2_level_estimator= */ false,
388 /* digital_adaptive_disabled= */ false,
389 /* analyze_before_aec= */ false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000390#else
Alex Loiko64cb83b2018-07-02 13:38:19 +0200391 config.Get<ExperimentalAgc>().enabled,
392 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
Alex Loikod9342442018-09-10 13:59:41 +0200393 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
394 config.Get<ExperimentalAgc>().analyze_before_aec),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000395#endif
andrew1c7075f2015-06-24 18:14:14 -0700396#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200397 capture_(false),
andrew1c7075f2015-06-24 18:14:14 -0700398#else
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200399 capture_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700400#endif
Alessio Bazzicacc22f512018-08-30 13:01:34 +0200401 capture_nonlocked_() {
Sam Zackrisson421c8592019-02-11 13:39:46 +0100402 // Mark Echo Controller enabled if a factory is injected.
403 capture_nonlocked_.echo_controller_enabled =
404 static_cast<bool>(echo_control_factory_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000405
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100406 public_submodules_->gain_control.reset(new GainControlImpl());
Sam Zackrisson421c8592019-02-11 13:39:46 +0100407 public_submodules_->level_estimator.reset(
408 new LevelEstimatorImpl(&crit_capture_));
409 public_submodules_->noise_suppression.reset(
410 new NoiseSuppressionImpl(&crit_capture_));
411 public_submodules_->noise_suppression_proxy.reset(new NoiseSuppressionProxy(
412 this, public_submodules_->noise_suppression.get()));
413 public_submodules_->voice_detection.reset(
414 new VoiceDetectionImpl(&crit_capture_));
415 public_submodules_->gain_control_for_experimental_agc.reset(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100416 new GainControlForExperimentalAgc(
417 public_submodules_->gain_control.get()));
418 public_submodules_->gain_control_config_proxy.reset(
419 new GainControlConfigProxy(&crit_capture_, this, agc1()));
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200420
Sam Zackrisson421c8592019-02-11 13:39:46 +0100421 // If no echo detector is injected, use the ResidualEchoDetector.
422 if (!private_submodules_->echo_detector) {
423 private_submodules_->echo_detector =
424 new rtc::RefCountedObject<ResidualEchoDetector>();
peahdf3efa82015-11-28 12:35:15 -0800425 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000426
Sam Zackrisson421c8592019-02-11 13:39:46 +0100427 // TODO(alessiob): Move the injected gain controller once injection is
428 // implemented.
429 private_submodules_->gain_controller2.reset(new GainController2());
430
431 RTC_LOG(LS_INFO) << "Capture analyzer activated: "
432 << !!private_submodules_->capture_analyzer
433 << "\nCapture post processor activated: "
434 << !!private_submodules_->capture_post_processor
435 << "\nRender pre processor activated: "
436 << !!private_submodules_->render_pre_processor;
437
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000438 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
441AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800442 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800443 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800444 private_submodules_->agc_manager.reset();
445 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800446 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
niklase@google.com470e71d2011-07-07 08:21:25 +0000449int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800450 // Run in a single-threaded manner during initialization.
451 rtc::CritScope cs_render(&crit_render_);
452 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000453 return InitializeLocked();
454}
455
peahde65ddc2016-09-16 15:02:15 -0700456int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
457 int capture_output_sample_rate_hz,
458 int render_input_sample_rate_hz,
459 ChannelLayout capture_input_layout,
460 ChannelLayout capture_output_layout,
461 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700462 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700463 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
464 LayoutHasKeyboard(capture_input_layout)},
465 {capture_output_sample_rate_hz,
466 ChannelsFromLayout(capture_output_layout),
467 LayoutHasKeyboard(capture_output_layout)},
468 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
469 LayoutHasKeyboard(render_input_layout)},
470 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
471 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700472
473 return Initialize(processing_config);
474}
475
476int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800477 // Run in a single-threaded manner during initialization.
478 rtc::CritScope cs_render(&crit_render_);
479 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700480 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000481}
482
peahdf3efa82015-11-28 12:35:15 -0800483int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800484 const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800485 // Called from both threads. Thread check is therefore not possible.
Oskar Sundbom4b276482019-05-23 14:28:00 +0200486 if (processing_config == formats_.api_format) {
peah192164e2015-11-17 02:16:45 -0800487 return kNoError;
488 }
peahdf3efa82015-11-28 12:35:15 -0800489
490 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800491 return InitializeLocked(processing_config);
492}
493
niklase@google.com470e71d2011-07-07 08:21:25 +0000494int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200495 UpdateActiveSubmoduleStates();
496
Per Åhgrend47941e2019-08-22 11:51:13 +0200497 const int render_audiobuffer_sample_rate_hz =
peahdf3efa82015-11-28 12:35:15 -0800498 formats_.api_format.reverse_output_stream().num_frames() == 0
Per Åhgrend47941e2019-08-22 11:51:13 +0200499 ? formats_.render_processing_format.sample_rate_hz()
500 : formats_.api_format.reverse_output_stream().sample_rate_hz();
peahdf3efa82015-11-28 12:35:15 -0800501 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
502 render_.render_audio.reset(new AudioBuffer(
Per Åhgrend47941e2019-08-22 11:51:13 +0200503 formats_.api_format.reverse_input_stream().sample_rate_hz(),
peahdf3efa82015-11-28 12:35:15 -0800504 formats_.api_format.reverse_input_stream().num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200505 formats_.render_processing_format.sample_rate_hz(),
peahde65ddc2016-09-16 15:02:15 -0700506 formats_.render_processing_format.num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200507 render_audiobuffer_sample_rate_hz,
508 formats_.render_processing_format.num_channels()));
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
Per Åhgrend47941e2019-08-22 11:51:13 +0200524 capture_.capture_audio.reset(new AudioBuffer(
525 formats_.api_format.input_stream().sample_rate_hz(),
526 formats_.api_format.input_stream().num_channels(),
527 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
528 formats_.api_format.output_stream().num_channels(),
529 formats_.api_format.output_stream().sample_rate_hz(),
530 formats_.api_format.output_stream().num_channels()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000531
peah764e3642016-10-22 05:04:30 -0700532 AllocateRenderQueue();
533
peah135259a2016-10-28 03:12:11 -0700534 public_submodules_->gain_control->Initialize(num_proc_channels(),
535 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700536 if (constants_.use_experimental_agc) {
537 if (!private_submodules_->agc_manager.get()) {
538 private_submodules_->agc_manager.reset(new AgcManagerDirect(
539 public_submodules_->gain_control.get(),
540 public_submodules_->gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200541 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
542 constants_.use_experimental_agc_agc2_level_estimation,
543 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700544 }
545 private_submodules_->agc_manager->Initialize();
546 private_submodules_->agc_manager->SetCaptureMuted(
547 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700548 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700549 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200550 InitializeTransient();
Per Åhgren0aefbf02019-08-23 21:29:17 +0200551 InitializeHighPassFilter();
peahde65ddc2016-09-16 15:02:15 -0700552 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
553 proc_sample_rate_hz());
554 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100555 if (private_submodules_->voice_detector) {
556 private_submodules_->voice_detector->Initialize(
557 proc_split_sample_rate_hz());
558 }
peahde65ddc2016-09-16 15:02:15 -0700559 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700560 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200561 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700562 InitializeGainController2();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200563 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200564 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100565 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800566
aleloi868f32f2017-05-23 07:20:05 -0700567 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200568 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700569 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000570 return kNoError;
571}
572
Michael Graczyk86c6d332015-07-23 11:41:39 -0700573int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200574 UpdateActiveSubmoduleStates();
575
Michael Graczyk86c6d332015-07-23 11:41:39 -0700576 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700577 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
578 return kBadSampleRateError;
579 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000580 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700581
Peter Kasting69558702016-01-12 16:26:35 -0800582 const size_t num_in_channels = config.input_stream().num_channels();
583 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700584
585 // Need at least one input channel.
586 // Need either one output channel or as many outputs as there are inputs.
587 if (num_in_channels == 0 ||
588 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700589 return kBadNumberChannelsError;
590 }
591
peahdf3efa82015-11-28 12:35:15 -0800592 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000593
Per Åhgrenc8626b62019-08-23 15:49:51 +0200594 int capture_processing_rate = SuitableProcessRate(
peah423d2362016-04-09 16:06:52 -0700595 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700596 formats_.api_format.output_stream().sample_rate_hz()),
597 submodule_states_.CaptureMultiBandSubModulesActive() ||
598 submodule_states_.RenderMultiBandSubModulesActive());
Per Åhgrenc8626b62019-08-23 15:49:51 +0200599 RTC_DCHECK_NE(8000, capture_processing_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000600
peahde65ddc2016-09-16 15:02:15 -0700601 capture_nonlocked_.capture_processing_format =
602 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700603
peah2ce640f2017-04-07 03:57:48 -0700604 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200605 if (!capture_nonlocked_.echo_controller_enabled) {
Per Åhgrenc8626b62019-08-23 15:49:51 +0200606 render_processing_rate = SuitableProcessRate(
peah2ce640f2017-04-07 03:57:48 -0700607 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
608 formats_.api_format.reverse_output_stream().sample_rate_hz()),
609 submodule_states_.CaptureMultiBandSubModulesActive() ||
610 submodule_states_.RenderMultiBandSubModulesActive());
611 } else {
612 render_processing_rate = capture_processing_rate;
613 }
614
aluebseb3603b2016-04-20 15:27:58 -0700615 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
616 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700617 if (render_processing_rate > kSampleRate32kHz &&
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200618 !capture_nonlocked_.echo_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -0700619 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
620 ? kSampleRate32kHz
621 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700622 }
peah2ce640f2017-04-07 03:57:48 -0700623
peahde65ddc2016-09-16 15:02:15 -0700624 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700625 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700626 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
627 kSampleRate8kHz) {
628 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000629 } else {
peahde65ddc2016-09-16 15:02:15 -0700630 render_processing_rate =
631 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000632 }
633
Per Åhgrenc8626b62019-08-23 15:49:51 +0200634 RTC_DCHECK_NE(8000, render_processing_rate);
635
peahde65ddc2016-09-16 15:02:15 -0700636 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000637 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700638 if (submodule_states_.RenderMultiBandSubModulesActive()) {
639 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
640 } else {
641 formats_.render_processing_format = StreamConfig(
642 formats_.api_format.reverse_input_stream().sample_rate_hz(),
643 formats_.api_format.reverse_input_stream().num_channels());
644 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000645
peahde65ddc2016-09-16 15:02:15 -0700646 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
647 kSampleRate32kHz ||
648 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
649 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800650 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000651 } else {
peahdf3efa82015-11-28 12:35:15 -0800652 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700653 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000654 }
655
656 return InitializeLocked();
657}
658
peah88ac8532016-09-12 16:47:25 -0700659void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peah88ac8532016-09-12 16:47:25 -0700660 // Run in a single-threaded manner when applying the settings.
661 rtc::CritScope cs_render(&crit_render_);
662 rtc::CritScope cs_capture(&crit_capture_);
663
Per Åhgren200feba2019-03-06 04:16:46 +0100664 const bool aec_config_changed =
665 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
666 config_.echo_canceller.use_legacy_aec !=
667 config.echo_canceller.use_legacy_aec ||
668 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode ||
669 (config_.echo_canceller.enabled && config.echo_canceller.use_legacy_aec &&
670 config_.echo_canceller.legacy_moderate_suppression_level !=
671 config.echo_canceller.legacy_moderate_suppression_level);
672
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100673 const bool agc1_config_changed =
674 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
675 config_.gain_controller1.mode != config.gain_controller1.mode ||
676 config_.gain_controller1.target_level_dbfs !=
677 config.gain_controller1.target_level_dbfs ||
678 config_.gain_controller1.compression_gain_db !=
679 config.gain_controller1.compression_gain_db ||
680 config_.gain_controller1.enable_limiter !=
681 config.gain_controller1.enable_limiter ||
682 config_.gain_controller1.analog_level_minimum !=
683 config.gain_controller1.analog_level_minimum ||
684 config_.gain_controller1.analog_level_maximum !=
685 config.gain_controller1.analog_level_maximum;
686
Yves Gerey499bc6c2018-10-10 18:29:07 +0200687 config_ = config;
688
Per Åhgren200feba2019-03-06 04:16:46 +0100689 if (aec_config_changed) {
690 InitializeEchoController();
691 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200692
Sam Zackrisson23513132019-01-11 15:10:32 +0100693 public_submodules_->noise_suppression->Enable(
694 config.noise_suppression.enabled);
695 public_submodules_->noise_suppression->set_level(
696 NsConfigLevelToInterfaceLevel(config.noise_suppression.level));
697
Per Åhgren0aefbf02019-08-23 21:29:17 +0200698 InitializeHighPassFilter();
peah8271d042016-11-22 07:24:52 -0800699
Mirko Bonadei675513b2017-11-09 11:09:25 +0100700 RTC_LOG(LS_INFO) << "Highpass filter activated: "
701 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800702
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100703 if (agc1_config_changed) {
704 ApplyAgc1Config(config_.gain_controller1);
705 }
706
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100707 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700708 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100709 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
710 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100711 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100712 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700713 config_.gain_controller2 = AudioProcessing::Config::GainController2();
714 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200715 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200716 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200717 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100718 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
719 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200720 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
721 << config_.pre_amplifier.enabled;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100722
723 if (config_.level_estimation.enabled &&
724 !private_submodules_->output_level_estimator) {
725 private_submodules_->output_level_estimator.reset(
726 new LevelEstimatorImpl(&crit_capture_));
727 private_submodules_->output_level_estimator->Enable(true);
728 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100729
730 if (config_.voice_detection.enabled && !private_submodules_->voice_detector) {
731 private_submodules_->voice_detector.reset(
732 new VoiceDetectionImpl(&crit_capture_));
733 private_submodules_->voice_detector->Enable(true);
734 private_submodules_->voice_detector->set_likelihood(
735 VoiceDetection::kVeryLowLikelihood);
736 private_submodules_->voice_detector->Initialize(
737 proc_split_sample_rate_hz());
738 }
peah88ac8532016-09-12 16:47:25 -0700739}
740
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100741void AudioProcessingImpl::ApplyAgc1Config(
742 const Config::GainController1& config) {
743 GainControl* agc = agc1();
744 int error = agc->Enable(config.enabled);
745 RTC_DCHECK_EQ(kNoError, error);
746 error = agc->set_mode(Agc1ConfigModeToInterfaceMode(config.mode));
747 RTC_DCHECK_EQ(kNoError, error);
748 error = agc->set_target_level_dbfs(config.target_level_dbfs);
749 RTC_DCHECK_EQ(kNoError, error);
750 error = agc->set_compression_gain_db(config.compression_gain_db);
751 RTC_DCHECK_EQ(kNoError, error);
752 error = agc->enable_limiter(config.enable_limiter);
753 RTC_DCHECK_EQ(kNoError, error);
754 error = agc->set_analog_level_limits(config.analog_level_minimum,
755 config.analog_level_maximum);
756 RTC_DCHECK_EQ(kNoError, error);
757}
758
759GainControl* AudioProcessingImpl::agc1() {
760 if (constants_.use_experimental_agc) {
761 return public_submodules_->gain_control_for_experimental_agc.get();
762 }
763 return public_submodules_->gain_control.get();
764}
765
766const GainControl* AudioProcessingImpl::agc1() const {
767 if (constants_.use_experimental_agc) {
768 return public_submodules_->gain_control_for_experimental_agc.get();
769 }
770 return public_submodules_->gain_control.get();
771}
772
peah88ac8532016-09-12 16:47:25 -0700773void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800774 // Run in a single-threaded manner when setting the extra options.
775 rtc::CritScope cs_render(&crit_render_);
776 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000777
Per Åhgrenf204faf2019-04-25 15:18:06 +0200778 capture_nonlocked_.use_aec2_extended_filter =
779 config.Get<ExtendedFilter>().enabled;
780 capture_nonlocked_.use_aec2_delay_agnostic =
781 config.Get<DelayAgnostic>().enabled;
782 capture_nonlocked_.use_aec2_refined_adaptive_filter =
783 config.Get<RefinedAdaptiveFilter>().enabled;
peahb624d8c2016-03-05 03:01:14 -0800784
peahdf3efa82015-11-28 12:35:15 -0800785 if (capture_.transient_suppressor_enabled !=
786 config.Get<ExperimentalNs>().enabled) {
787 capture_.transient_suppressor_enabled =
788 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000789 InitializeTransient();
790 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000791}
792
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000793int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800794 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700795 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000796}
797
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000798int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800799 // Used as callback from submodules, hence locking is not allowed.
800 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000801}
802
Peter Kasting69558702016-01-12 16:26:35 -0800803size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800804 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700805 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000806}
807
Peter Kasting69558702016-01-12 16:26:35 -0800808size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800809 // Used as callback from submodules, hence locking is not allowed.
810 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000811}
812
Peter Kasting69558702016-01-12 16:26:35 -0800813size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800814 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200815 return capture_nonlocked_.echo_controller_enabled ? 1 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800816}
817
Peter Kasting69558702016-01-12 16:26:35 -0800818size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800819 // Used as callback from submodules, hence locking is not allowed.
820 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000821}
822
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000823void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800824 rtc::CritScope cs(&crit_capture_);
825 capture_.output_will_be_muted = muted;
826 if (private_submodules_->agc_manager.get()) {
827 private_submodules_->agc_manager->SetCaptureMuted(
828 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000829 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000830}
831
Alessio Bazzicac054e782018-04-16 12:10:09 +0200832void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200833 switch (setting.type()) {
834 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
835 render_runtime_settings_enqueuer_.Enqueue(setting);
836 return;
837 case RuntimeSetting::Type::kNotSpecified:
838 RTC_NOTREACHED();
839 return;
840 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100841 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200842 case RuntimeSetting::Type::kCaptureFixedPostGain:
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200843 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200844 capture_runtime_settings_enqueuer_.Enqueue(setting);
845 return;
846 }
847 // The language allows the enum to have a non-enumerator
848 // value. Check that this doesn't happen.
849 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200850}
851
852AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
853 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200854 : runtime_settings_(*runtime_settings) {
855 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200856}
857
858AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
859 default;
860
861void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
862 RuntimeSetting setting) {
863 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200864 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200865 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200866 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200867 RTC_LOG(LS_ERROR)
868 << "The runtime settings queue is full. Oldest setting discarded.";
869 }
870 if (remaining_attempts == 0)
871 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
872}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000873
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000874int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700875 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000876 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000877 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000878 int output_sample_rate_hz,
879 ChannelLayout output_layout,
880 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800881 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800882 StreamConfig input_stream;
883 StreamConfig output_stream;
884 {
885 // Access the formats_.api_format.input_stream beneath the capture lock.
886 // The lock must be released as it is later required in the call
887 // to ProcessStream(,,,);
888 rtc::CritScope cs(&crit_capture_);
889 input_stream = formats_.api_format.input_stream();
890 output_stream = formats_.api_format.output_stream();
891 }
892
Michael Graczyk86c6d332015-07-23 11:41:39 -0700893 input_stream.set_sample_rate_hz(input_sample_rate_hz);
894 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
895 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700896 output_stream.set_sample_rate_hz(output_sample_rate_hz);
897 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
898 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
899
900 if (samples_per_channel != input_stream.num_frames()) {
901 return kBadDataLengthError;
902 }
903 return ProcessStream(src, input_stream, output_stream, dest);
904}
905
906int AudioProcessingImpl::ProcessStream(const float* const* src,
907 const StreamConfig& input_config,
908 const StreamConfig& output_config,
909 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800910 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800911 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700912 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800913 {
914 // Acquire the capture lock in order to safely call the function
915 // that retrieves the render side data. This function accesses apm
916 // getters that need the capture lock held when being called.
917 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700918 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800919
920 if (!src || !dest) {
921 return kNullPointerError;
922 }
923
924 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700925 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000926 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000927
Oskar Sundbom4b276482019-05-23 14:28:00 +0200928 if (processing_config.input_stream() != input_config) {
929 processing_config.input_stream() = input_config;
930 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800931 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200932
933 if (processing_config.output_stream() != output_config) {
934 processing_config.output_stream() = output_config;
935 reinitialization_required = true;
936 }
937
938 if (reinitialization_required) {
939 // Reinitialize.
940 rtc::CritScope cs_render(&crit_render_);
941 rtc::CritScope cs_capture(&crit_capture_);
942 RETURN_ON_ERR(InitializeLocked(processing_config));
943 }
944
peahdf3efa82015-11-28 12:35:15 -0800945 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700946 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
947 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000948
aleloi868f32f2017-05-23 07:20:05 -0700949 if (aec_dump_) {
950 RecordUnprocessedCaptureStream(src);
951 }
952
Per Åhgrena1351272019-08-15 12:15:46 +0200953 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800954 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700955 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800956 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000957
aleloi868f32f2017-05-23 07:20:05 -0700958 if (aec_dump_) {
959 RecordProcessedCaptureStream(dest);
960 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000961 return kNoError;
962}
963
Alex Loiko73ec0192018-05-15 10:52:28 +0200964void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200965 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200966 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200967 if (aec_dump_) {
968 aec_dump_->WriteRuntimeSetting(setting);
969 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200970 switch (setting.type()) {
971 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200972 if (config_.pre_amplifier.enabled) {
973 float value;
974 setting.GetFloat(&value);
975 private_submodules_->pre_amplifier->SetGainFactor(value);
976 }
977 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200978 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100979 case RuntimeSetting::Type::kCaptureCompressionGain: {
980 float value;
981 setting.GetFloat(&value);
982 int int_value = static_cast<int>(value + .5f);
983 config_.gain_controller1.compression_gain_db = int_value;
984 int error = agc1()->set_compression_gain_db(int_value);
985 RTC_DCHECK_EQ(kNoError, error);
986 break;
987 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200988 case RuntimeSetting::Type::kCaptureFixedPostGain: {
989 if (config_.gain_controller2.enabled) {
990 float value;
991 setting.GetFloat(&value);
992 config_.gain_controller2.fixed_digital.gain_db = value;
993 private_submodules_->gain_controller2->ApplyConfig(
994 config_.gain_controller2);
995 }
996 break;
997 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200998 case RuntimeSetting::Type::kPlayoutVolumeChange: {
999 int value;
1000 setting.GetInt(&value);
1001 capture_.playout_volume = value;
1002 break;
1003 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001004 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1005 RTC_NOTREACHED();
1006 break;
1007 case RuntimeSetting::Type::kNotSpecified:
1008 RTC_NOTREACHED();
1009 break;
1010 }
1011 }
1012}
1013
1014void AudioProcessingImpl::HandleRenderRuntimeSettings() {
1015 RuntimeSetting setting;
1016 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +02001017 if (aec_dump_) {
1018 aec_dump_->WriteRuntimeSetting(setting);
1019 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001020 switch (setting.type()) {
1021 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1022 if (private_submodules_->render_pre_processor) {
1023 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
1024 }
1025 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001026 case RuntimeSetting::Type::kCapturePreGain: // fall-through
1027 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001028 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001029 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001030 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001031 RTC_NOTREACHED();
1032 break;
1033 }
1034 }
1035}
1036
peah9e6a2902017-05-15 07:19:21 -07001037void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001038 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001039
1040 // Insert the samples into the queue.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001041 if (private_submodules_->echo_cancellation) {
1042 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001043 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1044 num_reverse_channels(),
1045 &aec_render_queue_buffer_);
1046
Per Åhgrenf204faf2019-04-25 15:18:06 +02001047 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1048 // The data queue is full and needs to be emptied.
1049 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001050
Per Åhgrenf204faf2019-04-25 15:18:06 +02001051 // Retry the insert (should always work).
1052 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1053 RTC_DCHECK(result);
1054 }
peaha0624602016-10-25 04:45:24 -07001055 }
1056
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001057 if (private_submodules_->echo_control_mobile) {
1058 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1059 num_reverse_channels(),
1060 &aecm_render_queue_buffer_);
1061 RTC_DCHECK(aecm_render_signal_queue_);
1062 // Insert the samples into the queue.
1063 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1064 // The data queue is full and needs to be emptied.
1065 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001066
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001067 // Retry the insert (should always work).
1068 bool result =
1069 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1070 RTC_DCHECK(result);
1071 }
peah764e3642016-10-22 05:04:30 -07001072 }
peah701d6282016-10-25 05:42:20 -07001073
1074 if (!constants_.use_experimental_agc) {
1075 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1076 // Insert the samples into the queue.
1077 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1078 // The data queue is full and needs to be emptied.
1079 EmptyQueuedRenderAudio();
1080
1081 // Retry the insert (should always work).
1082 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1083 RTC_DCHECK(result);
1084 }
1085 }
peah9e6a2902017-05-15 07:19:21 -07001086}
ivoc9f4a4a02016-10-28 05:39:16 -07001087
peah9e6a2902017-05-15 07:19:21 -07001088void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001089 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1090
1091 // Insert the samples into the queue.
1092 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1093 // The data queue is full and needs to be emptied.
1094 EmptyQueuedRenderAudio();
1095
1096 // Retry the insert (should always work).
1097 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1098 RTC_DCHECK(result);
1099 }
peah764e3642016-10-22 05:04:30 -07001100}
1101
1102void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001103 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001104 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001105
ivoc9f4a4a02016-10-28 05:39:16 -07001106 const size_t new_red_render_queue_element_max_size =
1107 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1108
peaha0624602016-10-25 04:45:24 -07001109 // Reallocate the queues if the queue item sizes are too small to fit the
1110 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001111
1112 if (agc_render_queue_element_max_size_ <
1113 new_agc_render_queue_element_max_size) {
1114 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1115
1116 std::vector<int16_t> template_queue_element(
1117 agc_render_queue_element_max_size_);
1118
1119 agc_render_signal_queue_.reset(
1120 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1121 kMaxNumFramesToBuffer, template_queue_element,
1122 RenderQueueItemVerifier<int16_t>(
1123 agc_render_queue_element_max_size_)));
1124
1125 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1126 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1127 } else {
1128 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001129 }
ivoc9f4a4a02016-10-28 05:39:16 -07001130
1131 if (red_render_queue_element_max_size_ <
1132 new_red_render_queue_element_max_size) {
1133 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1134
1135 std::vector<float> template_queue_element(
1136 red_render_queue_element_max_size_);
1137
1138 red_render_signal_queue_.reset(
1139 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1140 kMaxNumFramesToBuffer, template_queue_element,
1141 RenderQueueItemVerifier<float>(
1142 red_render_queue_element_max_size_)));
1143
1144 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1145 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1146 } else {
1147 red_render_signal_queue_->Clear();
1148 }
peah764e3642016-10-22 05:04:30 -07001149}
1150
1151void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1152 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001153 if (private_submodules_->echo_cancellation) {
1154 RTC_DCHECK(aec_render_signal_queue_);
1155 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
1156 private_submodules_->echo_cancellation->ProcessRenderAudio(
1157 aec_capture_queue_buffer_);
1158 }
peaha0624602016-10-25 04:45:24 -07001159 }
1160
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001161 if (private_submodules_->echo_control_mobile) {
1162 RTC_DCHECK(aecm_render_signal_queue_);
1163 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
1164 private_submodules_->echo_control_mobile->ProcessRenderAudio(
1165 aecm_capture_queue_buffer_);
1166 }
peah701d6282016-10-25 05:42:20 -07001167 }
1168
1169 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1170 public_submodules_->gain_control->ProcessRenderAudio(
1171 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001172 }
ivoc9f4a4a02016-10-28 05:39:16 -07001173
1174 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001175 RTC_DCHECK(private_submodules_->echo_detector);
1176 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001177 red_capture_queue_buffer_);
1178 }
peah764e3642016-10-22 05:04:30 -07001179}
1180
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001181int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001182 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001183 {
1184 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001185 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001186 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001187 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001188 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001189 }
peahfa6228e2015-11-16 16:27:42 -08001190
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001191 if (!frame) {
1192 return kNullPointerError;
1193 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001194 // Must be a native rate.
1195 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1196 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001197 frame->sample_rate_hz_ != kSampleRate32kHz &&
1198 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001199 return kBadSampleRateError;
1200 }
peah192164e2015-11-17 02:16:45 -08001201
peahdf3efa82015-11-28 12:35:15 -08001202 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001203 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001204 {
1205 // Aquire lock for the access of api_format.
1206 // The lock is released immediately due to the conditional
1207 // reinitialization.
1208 rtc::CritScope cs_capture(&crit_capture_);
1209 // TODO(ajm): The input and output rates and channels are currently
1210 // constrained to be identical in the int16 interface.
1211 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001212
1213 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001214 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001215
Oskar Sundbom4b276482019-05-23 14:28:00 +02001216 reinitialization_required =
1217 reinitialization_required ||
1218 processing_config.input_stream().sample_rate_hz() !=
1219 frame->sample_rate_hz_ ||
1220 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1221 processing_config.output_stream().sample_rate_hz() !=
1222 frame->sample_rate_hz_ ||
1223 processing_config.output_stream().num_channels() != frame->num_channels_;
1224
1225 if (reinitialization_required) {
1226 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1227 processing_config.input_stream().set_num_channels(frame->num_channels_);
1228 processing_config.output_stream().set_sample_rate_hz(
1229 frame->sample_rate_hz_);
1230 processing_config.output_stream().set_num_channels(frame->num_channels_);
1231
1232 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001233 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001234 rtc::CritScope cs_capture(&crit_capture_);
1235 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001236 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001237
peahdf3efa82015-11-28 12:35:15 -08001238 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001239 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001240 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001241 return kBadDataLengthError;
1242 }
1243
aleloi868f32f2017-05-23 07:20:05 -07001244 if (aec_dump_) {
1245 RecordUnprocessedCaptureStream(*frame);
1246 }
1247
Per Åhgrena1351272019-08-15 12:15:46 +02001248 capture_.vad_activity = frame->vad_activity_;
Per Åhgrend47941e2019-08-22 11:51:13 +02001249 capture_.capture_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001250 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001251 if (submodule_states_.CaptureMultiBandProcessingActive() ||
1252 submodule_states_.CaptureFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001253 capture_.capture_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001254 }
1255 frame->vad_activity_ = capture_.vad_activity;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001256
aleloi868f32f2017-05-23 07:20:05 -07001257 if (aec_dump_) {
1258 RecordProcessedCaptureStream(*frame);
1259 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001260
1261 return kNoError;
1262}
1263
peahde65ddc2016-09-16 15:02:15 -07001264int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001265 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001266
peahb58a1582016-03-15 09:34:24 -07001267 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001268 // TODO(peah): Simplify once the public API Enable functions for these
1269 // are moved to APM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001270 RTC_DCHECK_LE(!!private_submodules_->echo_controller +
1271 !!private_submodules_->echo_cancellation +
1272 !!private_submodules_->echo_control_mobile,
1273 1);
peahb58a1582016-03-15 09:34:24 -07001274
peahde65ddc2016-09-16 15:02:15 -07001275 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001276
Alex Loikob5c9a792018-04-16 16:31:22 +02001277 if (private_submodules_->pre_amplifier) {
1278 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001279 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001280 capture_buffer->num_frames()));
1281 }
1282
Per Åhgren928146f2019-08-20 09:19:21 +02001283 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001284 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001285 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001286 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1287 if (log_rms) {
1288 capture_rms_interval_counter_ = 0;
1289 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001290 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1291 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1292 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1293 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001294 }
1295
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001296 if (private_submodules_->echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001297 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001298 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001299 capture_.echo_path_gain_change =
1300 capture_.prev_analog_mic_level != analog_mic_level &&
1301 capture_.prev_analog_mic_level != -1;
1302 capture_.prev_analog_mic_level = analog_mic_level;
1303
Per Åhgrend2650d12018-10-02 17:00:59 +02001304 // Detect and flag any change in the pre-amplifier gain.
1305 if (private_submodules_->pre_amplifier) {
1306 float pre_amp_gain = private_submodules_->pre_amplifier->GetGainFactor();
1307 capture_.echo_path_gain_change =
1308 capture_.echo_path_gain_change ||
1309 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001310 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001311 capture_.prev_pre_amp_gain = pre_amp_gain;
1312 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001313
1314 // Detect volume change.
1315 capture_.echo_path_gain_change =
1316 capture_.echo_path_gain_change ||
1317 (capture_.prev_playout_volume != capture_.playout_volume &&
1318 capture_.prev_playout_volume >= 0);
1319 capture_.prev_playout_volume = capture_.playout_volume;
1320
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001321 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001322 }
1323
peahbe615622016-02-13 16:40:47 -08001324 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001325 public_submodules_->gain_control->is_enabled()) {
1326 private_submodules_->agc_manager->AnalyzePreProcess(
Per Åhgren928146f2019-08-20 09:19:21 +02001327 capture_buffer->channels_f()[0], capture_buffer->num_channels(),
peahde65ddc2016-09-16 15:02:15 -07001328 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001329
1330 if (constants_.use_experimental_agc_process_before_aec) {
1331 private_submodules_->agc_manager->Process(
Per Åhgrend47941e2019-08-22 11:51:13 +02001332 capture_buffer->channels_const()[0],
Alex Loikod9342442018-09-10 13:59:41 +02001333 capture_nonlocked_.capture_processing_format.num_frames(),
1334 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1335 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001336 }
1337
peah2ace3f92016-09-10 04:42:27 -07001338 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1339 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001340 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1341 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001342 }
1343
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001344 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001345 // Force down-mixing of the number of channels after the detection of
1346 // capture signal saturation.
1347 // TODO(peah): Look into ensuring that this kind of tampering with the
1348 // AudioBuffer functionality should not be needed.
1349 capture_buffer->set_num_channels(1);
1350 }
1351
Per Åhgren0aefbf02019-08-23 21:29:17 +02001352 if (private_submodules_->high_pass_filter) {
1353 private_submodules_->high_pass_filter->Process(capture_buffer);
peah8271d042016-11-22 07:24:52 -08001354 }
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(
Per Åhgrend47941e2019-08-22 11:51:13 +02001439 rtc::ArrayView<const float>(capture_buffer->channels()[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(
Per Åhgrend47941e2019-08-22 11:51:13 +02001452 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001453 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001454 capture_buffer->split_bands_const(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.
Per Åhgrend47941e2019-08-22 11:51:13 +02001477 public_submodules_->level_estimator->ProcessStream(*capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001478 if (config_.level_estimation.enabled) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001479 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>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001487 capture_buffer->channels_const()[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
Per Åhgrend47941e2019-08-22 11:51:13 +02001612 render_.render_audio->CopyFrom(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()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001616 render_.render_audio->CopyTo(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
Per Åhgren0aefbf02019-08-23 21:29:17 +02001830void AudioProcessingImpl::InitializeHighPassFilter() {
1831 if (submodule_states_.HighPassFilteringRequired()) {
1832 private_submodules_->high_pass_filter.reset(
1833 new HighPassFilter(num_proc_channels()));
peah8271d042016-11-22 07:24:52 -08001834 } else {
Per Åhgren0aefbf02019-08-23 21:29:17 +02001835 private_submodules_->high_pass_filter.reset();
peah8271d042016-11-22 07:24:52 -08001836 }
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>(
Per Åhgren0aefbf02019-08-23 21:29:17 +02001852 EchoCanceller3Config(), proc_sample_rate_hz());
Per Åhgren200feba2019-03-06 04:16:46 +01001853 }
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