blob: c0058c73a8ad9ffbaccedf0bb0ee5ef3ade5115c [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
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "absl/types/optional.h"
20#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "common_audio/audio_converter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_audio/include/audio_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_processing/agc/agc_manager_direct.h"
Alex Loikob5c9a792018-04-16 16:31:22 +020024#include "modules/audio_processing/agc2/gain_applier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/common.h"
27#include "modules/audio_processing/echo_cancellation_impl.h"
28#include "modules/audio_processing/echo_control_mobile_impl.h"
29#include "modules/audio_processing/gain_control_for_experimental_agc.h"
30#include "modules/audio_processing/gain_control_impl.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010031#include "modules/audio_processing/gain_controller2.h"
Yves Gerey988cc082018-10-23 12:03:01 +020032#include "modules/audio_processing/include/audio_frame_view.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020033#include "modules/audio_processing/level_estimator_impl.h"
Per Åhgren13735822018-02-12 21:42:56 +010034#include "modules/audio_processing/logging/apm_data_dumper.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020035#include "modules/audio_processing/low_cut_filter.h"
36#include "modules/audio_processing/noise_suppression_impl.h"
37#include "modules/audio_processing/residual_echo_detector.h"
38#include "modules/audio_processing/transient/transient_suppressor.h"
39#include "modules/audio_processing/voice_detection_impl.h"
40#include "rtc_base/atomicops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "rtc_base/checks.h"
Yves Gerey988cc082018-10-23 12:03:01 +020042#include "rtc_base/constructormagic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020043#include "rtc_base/logging.h"
Niels Möller84255bb2017-10-06 13:43:23 +020044#include "rtc_base/refcountedobject.h"
Minyue Li656d6092018-08-10 15:38:52 +020045#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "rtc_base/trace_event.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000048
Michael Graczyk86c6d332015-07-23 11:41:39 -070049#define RETURN_ON_ERR(expr) \
50 do { \
51 int err = (expr); \
52 if (err != kNoError) { \
53 return err; \
54 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000055 } while (0)
56
niklase@google.com470e71d2011-07-07 08:21:25 +000057namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070058
kwibergd59d3bb2016-09-13 07:49:33 -070059constexpr int AudioProcessing::kNativeSampleRatesHz[];
Alex Loiko73ec0192018-05-15 10:52:28 +020060constexpr int kRuntimeSettingQueueSize = 100;
aluebsdf6416a2016-03-16 18:26:35 -070061
Michael Graczyk86c6d332015-07-23 11:41:39 -070062namespace {
63
64static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
65 switch (layout) {
66 case AudioProcessing::kMono:
67 case AudioProcessing::kStereo:
68 return false;
69 case AudioProcessing::kMonoAndKeyboard:
70 case AudioProcessing::kStereoAndKeyboard:
71 return true;
72 }
73
kwiberg9e2be5f2016-09-14 05:23:22 -070074 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070075 return false;
76}
aluebsdf6416a2016-03-16 18:26:35 -070077
peah2ace3f92016-09-10 04:42:27 -070078bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070079 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
80 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
81}
82
peah2ace3f92016-09-10 04:42:27 -070083int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) {
84#ifdef WEBRTC_ARCH_ARM_FAMILY
kwibergd59d3bb2016-09-13 07:49:33 -070085 constexpr int kMaxSplittingNativeProcessRate =
86 AudioProcessing::kSampleRate32kHz;
peah2ace3f92016-09-10 04:42:27 -070087#else
kwibergd59d3bb2016-09-13 07:49:33 -070088 constexpr int kMaxSplittingNativeProcessRate =
89 AudioProcessing::kSampleRate48kHz;
peah2ace3f92016-09-10 04:42:27 -070090#endif
kwibergd59d3bb2016-09-13 07:49:33 -070091 static_assert(
92 kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz,
93 "");
peah2ace3f92016-09-10 04:42:27 -070094 const int uppermost_native_rate = band_splitting_required
95 ? kMaxSplittingNativeProcessRate
96 : AudioProcessing::kSampleRate48kHz;
97
98 for (auto rate : AudioProcessing::kNativeSampleRatesHz) {
99 if (rate >= uppermost_native_rate) {
100 return uppermost_native_rate;
101 }
102 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700103 return rate;
104 }
105 }
peah2ace3f92016-09-10 04:42:27 -0700106 RTC_NOTREACHED();
107 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700108}
109
peah9e6a2902017-05-15 07:19:21 -0700110// Maximum lengths that frame of samples being passed from the render side to
111// the capture side can have (does not apply to AEC3).
112static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
113static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
114
peah764e3642016-10-22 05:04:30 -0700115// Maximum number of frames to buffer in the render queue.
116// TODO(peah): Decrease this once we properly handle hugely unbalanced
117// reverse and forward call numbers.
118static const size_t kMaxNumFramesToBuffer = 100;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700119} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000120
121// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000122static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000123
Sam Zackrisson0beac582017-09-25 12:04:02 +0200124AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100125 bool capture_post_processor_enabled,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200126 bool render_pre_processor_enabled,
127 bool capture_analyzer_enabled)
Alex Loiko5825aa62017-12-18 16:02:40 +0100128 : capture_post_processor_enabled_(capture_post_processor_enabled),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200129 render_pre_processor_enabled_(render_pre_processor_enabled),
130 capture_analyzer_enabled_(capture_analyzer_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700131
132bool AudioProcessingImpl::ApmSubmoduleStates::Update(
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200133 bool high_pass_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700134 bool echo_canceller_enabled,
135 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700136 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700137 bool noise_suppressor_enabled,
peah2ace3f92016-09-10 04:42:27 -0700138 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700139 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200140 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200141 bool echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -0700142 bool voice_activity_detector_enabled,
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100143 bool private_voice_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700144 bool level_estimator_enabled,
145 bool transient_suppressor_enabled) {
146 bool changed = false;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200147 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700148 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
149 changed |=
150 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700151 changed |=
152 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700153 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
154 changed |=
peah2ace3f92016-09-10 04:42:27 -0700155 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
alessiob3ec96df2017-05-22 06:57:06 -0700156 changed |=
157 (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200158 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200159 changed |= (echo_controller_enabled != echo_controller_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700160 changed |= (level_estimator_enabled != level_estimator_enabled_);
161 changed |=
162 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100163 changed |=
164 (private_voice_detector_enabled != private_voice_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700165 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
166 if (changed) {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200167 high_pass_filter_enabled_ = high_pass_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700168 echo_canceller_enabled_ = echo_canceller_enabled;
169 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700170 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700171 noise_suppressor_enabled_ = noise_suppressor_enabled;
peah2ace3f92016-09-10 04:42:27 -0700172 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700173 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200174 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200175 echo_controller_enabled_ = echo_controller_enabled;
peah2ace3f92016-09-10 04:42:27 -0700176 level_estimator_enabled_ = level_estimator_enabled;
177 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100178 private_voice_detector_enabled_ = private_voice_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700179 transient_suppressor_enabled_ = transient_suppressor_enabled;
180 }
181
182 changed |= first_update_;
183 first_update_ = false;
184 return changed;
185}
186
187bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
188 const {
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100189 return CaptureMultiBandProcessingActive() ||
190 voice_activity_detector_enabled_ || private_voice_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700191}
192
193bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
194 const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200195 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700196 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200197 adaptive_gain_controller_enabled_ || echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700198}
199
peah23ac8b42017-05-23 05:33:56 -0700200bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
201 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200202 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
203 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700204}
205
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200206bool AudioProcessingImpl::ApmSubmoduleStates::CaptureAnalyzerActive() const {
207 return capture_analyzer_enabled_;
208}
209
peah2ace3f92016-09-10 04:42:27 -0700210bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
211 const {
212 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800213 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200214 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700215}
216
Alex Loiko5825aa62017-12-18 16:02:40 +0100217bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive()
218 const {
219 return render_pre_processor_enabled_;
220}
221
peah2ace3f92016-09-10 04:42:27 -0700222bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
223 const {
peah2ace3f92016-09-10 04:42:27 -0700224 return false;
peah2ace3f92016-09-10 04:42:27 -0700225}
226
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200227bool AudioProcessingImpl::ApmSubmoduleStates::LowCutFilteringRequired() const {
228 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
229 mobile_echo_controller_enabled_ || noise_suppressor_enabled_;
230}
231
solenberg5e465c32015-12-08 13:22:33 -0800232struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800233 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800234 // Accessed externally of APM without any lock acquired.
peahbfa97112016-03-10 21:09:04 -0800235 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800236 std::unique_ptr<LevelEstimatorImpl> level_estimator;
237 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
238 std::unique_ptr<VoiceDetectionImpl> voice_detection;
239 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800240 gain_control_for_experimental_agc;
solenberg5e465c32015-12-08 13:22:33 -0800241
242 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800243 std::unique_ptr<TransientSuppressor> transient_suppressor;
solenberg5e465c32015-12-08 13:22:33 -0800244};
245
246struct AudioProcessingImpl::ApmPrivateSubmodules {
Sam Zackrissondb389722018-06-21 10:12:24 +0200247 ApmPrivateSubmodules(std::unique_ptr<CustomProcessing> capture_post_processor,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100248 std::unique_ptr<CustomProcessing> render_pre_processor,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200249 rtc::scoped_refptr<EchoDetector> echo_detector,
250 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Sam Zackrissondb389722018-06-21 10:12:24 +0200251 : echo_detector(std::move(echo_detector)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100252 capture_post_processor(std::move(capture_post_processor)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200253 render_pre_processor(std::move(render_pre_processor)),
254 capture_analyzer(std::move(capture_analyzer)) {}
solenberg5e465c32015-12-08 13:22:33 -0800255 // Accessed internally from capture or during initialization
kwiberg88788ad2016-02-19 07:04:49 -0800256 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700257 std::unique_ptr<GainController2> gain_controller2;
peah8271d042016-11-22 07:24:52 -0800258 std::unique_ptr<LowCutFilter> low_cut_filter;
Ivo Creusend1f970d2018-06-14 11:02:03 +0200259 rtc::scoped_refptr<EchoDetector> echo_detector;
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100260 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
Sam Zackrissonc22f5512018-11-05 16:10:00 +0100261 std::unique_ptr<EchoControl> echo_controller;
262 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
Alex Loiko5825aa62017-12-18 16:02:40 +0100263 std::unique_ptr<CustomProcessing> capture_post_processor;
264 std::unique_ptr<CustomProcessing> render_pre_processor;
Alex Loikob5c9a792018-04-16 16:31:22 +0200265 std::unique_ptr<GainApplier> pre_amplifier;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200266 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100267 std::unique_ptr<LevelEstimatorImpl> output_level_estimator;
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100268 std::unique_ptr<VoiceDetectionImpl> voice_detector;
solenberg5e465c32015-12-08 13:22:33 -0800269};
270
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100271AudioProcessingBuilder::AudioProcessingBuilder() = default;
272AudioProcessingBuilder::~AudioProcessingBuilder() = default;
273
274AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
275 std::unique_ptr<CustomProcessing> capture_post_processing) {
276 capture_post_processing_ = std::move(capture_post_processing);
277 return *this;
278}
279
280AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
281 std::unique_ptr<CustomProcessing> render_pre_processing) {
282 render_pre_processing_ = std::move(render_pre_processing);
283 return *this;
284}
285
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200286AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
287 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
288 capture_analyzer_ = std::move(capture_analyzer);
289 return *this;
290}
291
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100292AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
293 std::unique_ptr<EchoControlFactory> echo_control_factory) {
294 echo_control_factory_ = std::move(echo_control_factory);
295 return *this;
296}
297
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100298AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200299 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100300 echo_detector_ = std::move(echo_detector);
301 return *this;
302}
303
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100304AudioProcessing* AudioProcessingBuilder::Create() {
305 webrtc::Config config;
306 return Create(config);
307}
308
309AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100310 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
311 config, std::move(capture_post_processing_),
312 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200313 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100314 if (apm->Initialize() != AudioProcessing::kNoError) {
315 delete apm;
316 apm = nullptr;
317 }
318 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100319}
320
peah88ac8532016-09-12 16:47:25 -0700321AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200322 : AudioProcessingImpl(config, nullptr, nullptr, nullptr, nullptr, nullptr) {
323}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000324
Per Åhgren13735822018-02-12 21:42:56 +0100325int AudioProcessingImpl::instance_count_ = 0;
326
Sam Zackrisson0beac582017-09-25 12:04:02 +0200327AudioProcessingImpl::AudioProcessingImpl(
328 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100329 std::unique_ptr<CustomProcessing> capture_post_processor,
330 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200331 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200332 rtc::scoped_refptr<EchoDetector> echo_detector,
333 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100334 : data_dumper_(
335 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200336 capture_runtime_settings_(kRuntimeSettingQueueSize),
337 render_runtime_settings_(kRuntimeSettingQueueSize),
338 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
339 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200340 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200341 submodule_states_(!!capture_post_processor,
342 !!render_pre_processor,
343 !!capture_analyzer),
peah8271d042016-11-22 07:24:52 -0800344 public_submodules_(new ApmPublicSubmodules()),
Sam Zackrisson0beac582017-09-25 12:04:02 +0200345 private_submodules_(
Sam Zackrissondb389722018-06-21 10:12:24 +0200346 new ApmPrivateSubmodules(std::move(capture_post_processor),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100347 std::move(render_pre_processor),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200348 std::move(echo_detector),
349 std::move(capture_analyzer))),
peahdf3efa82015-11-28 12:35:15 -0800350 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800351 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000352#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alex Loikod9342442018-09-10 13:59:41 +0200353 /* enabled= */ false,
354 /* enabled_agc2_level_estimator= */ false,
355 /* digital_adaptive_disabled= */ false,
356 /* analyze_before_aec= */ false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000357#else
Alex Loiko64cb83b2018-07-02 13:38:19 +0200358 config.Get<ExperimentalAgc>().enabled,
359 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
Alex Loikod9342442018-09-10 13:59:41 +0200360 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
361 config.Get<ExperimentalAgc>().analyze_before_aec),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000362#endif
andrew1c7075f2015-06-24 18:14:14 -0700363#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200364 capture_(false),
andrew1c7075f2015-06-24 18:14:14 -0700365#else
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200366 capture_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700367#endif
Alessio Bazzicacc22f512018-08-30 13:01:34 +0200368 capture_nonlocked_() {
peahdf3efa82015-11-28 12:35:15 -0800369 {
370 rtc::CritScope cs_render(&crit_render_);
371 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000372
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200373 // Mark Echo Controller enabled if a factory is injected.
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000374 capture_nonlocked_.echo_controller_enabled =
375 static_cast<bool>(echo_control_factory_);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200376
peahbfa97112016-03-10 21:09:04 -0800377 public_submodules_->gain_control.reset(
Alex Loiko80c0f062018-06-19 17:09:43 +0200378 new GainControlImpl(&crit_render_, &crit_capture_));
solenberg949028f2015-12-15 11:39:38 -0800379 public_submodules_->level_estimator.reset(
380 new LevelEstimatorImpl(&crit_capture_));
solenberg5e465c32015-12-08 13:22:33 -0800381 public_submodules_->noise_suppression.reset(
382 new NoiseSuppressionImpl(&crit_capture_));
solenberga29386c2015-12-16 03:31:12 -0800383 public_submodules_->voice_detection.reset(
384 new VoiceDetectionImpl(&crit_capture_));
peahbe615622016-02-13 16:40:47 -0800385 public_submodules_->gain_control_for_experimental_agc.reset(
peahbfa97112016-03-10 21:09:04 -0800386 new GainControlForExperimentalAgc(
387 public_submodules_->gain_control.get(), &crit_capture_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100388
389 // If no echo detector is injected, use the ResidualEchoDetector.
390 if (!private_submodules_->echo_detector) {
Ivo Creusend1f970d2018-06-14 11:02:03 +0200391 private_submodules_->echo_detector =
392 new rtc::RefCountedObject<ResidualEchoDetector>();
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100393 }
peahca4cac72016-06-29 15:26:12 -0700394
Sam Zackrissonc22f5512018-11-05 16:10:00 +0100395 private_submodules_->echo_cancellation.reset(new EchoCancellationImpl());
396 private_submodules_->echo_control_mobile.reset(new EchoControlMobileImpl());
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200397 // TODO(alessiob): Move the injected gain controller once injection is
398 // implemented.
399 private_submodules_->gain_controller2.reset(new GainController2());
400
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200401 RTC_LOG(LS_INFO) << "Capture analyzer activated: "
402 << !!private_submodules_->capture_analyzer
403 << "\nCapture post processor activated: "
Jonas Olsson645b0272018-02-15 15:16:27 +0100404 << !!private_submodules_->capture_post_processor
405 << "\nRender pre processor activated: "
Alex Loiko5825aa62017-12-18 16:02:40 +0100406 << !!private_submodules_->render_pre_processor;
peahdf3efa82015-11-28 12:35:15 -0800407 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000408
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000409 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000410}
411
412AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800413 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800414 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800415 private_submodules_->agc_manager.reset();
416 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800417 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000418}
419
niklase@google.com470e71d2011-07-07 08:21:25 +0000420int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800421 // Run in a single-threaded manner during initialization.
422 rtc::CritScope cs_render(&crit_render_);
423 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000424 return InitializeLocked();
425}
426
peahde65ddc2016-09-16 15:02:15 -0700427int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
428 int capture_output_sample_rate_hz,
429 int render_input_sample_rate_hz,
430 ChannelLayout capture_input_layout,
431 ChannelLayout capture_output_layout,
432 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700433 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700434 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
435 LayoutHasKeyboard(capture_input_layout)},
436 {capture_output_sample_rate_hz,
437 ChannelsFromLayout(capture_output_layout),
438 LayoutHasKeyboard(capture_output_layout)},
439 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
440 LayoutHasKeyboard(render_input_layout)},
441 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
442 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700443
444 return Initialize(processing_config);
445}
446
447int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800448 // Run in a single-threaded manner during initialization.
449 rtc::CritScope cs_render(&crit_render_);
450 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700451 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000452}
453
peahdf3efa82015-11-28 12:35:15 -0800454int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800455 const ProcessingConfig& processing_config) {
peah2ace3f92016-09-10 04:42:27 -0700456 return MaybeInitialize(processing_config, false);
peah81b9bfe2015-11-27 02:47:28 -0800457}
458
peahdf3efa82015-11-28 12:35:15 -0800459int AudioProcessingImpl::MaybeInitializeCapture(
peah2ace3f92016-09-10 04:42:27 -0700460 const ProcessingConfig& processing_config,
461 bool force_initialization) {
462 return MaybeInitialize(processing_config, force_initialization);
peah81b9bfe2015-11-27 02:47:28 -0800463}
464
peah192164e2015-11-17 02:16:45 -0800465// Calls InitializeLocked() if any of the audio parameters have changed from
peahdf3efa82015-11-28 12:35:15 -0800466// their current values (needs to be called while holding the crit_render_lock).
467int AudioProcessingImpl::MaybeInitialize(
peah2ace3f92016-09-10 04:42:27 -0700468 const ProcessingConfig& processing_config,
469 bool force_initialization) {
peahdf3efa82015-11-28 12:35:15 -0800470 // Called from both threads. Thread check is therefore not possible.
peah2ace3f92016-09-10 04:42:27 -0700471 if (processing_config == formats_.api_format && !force_initialization) {
peah192164e2015-11-17 02:16:45 -0800472 return kNoError;
473 }
peahdf3efa82015-11-28 12:35:15 -0800474
475 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800476 return InitializeLocked(processing_config);
477}
478
niklase@google.com470e71d2011-07-07 08:21:25 +0000479int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200480 UpdateActiveSubmoduleStates();
481
peahde65ddc2016-09-16 15:02:15 -0700482 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800483 formats_.api_format.reverse_output_stream().num_frames() == 0
peahde65ddc2016-09-16 15:02:15 -0700484 ? formats_.render_processing_format.num_frames()
peahdf3efa82015-11-28 12:35:15 -0800485 : formats_.api_format.reverse_output_stream().num_frames();
486 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
487 render_.render_audio.reset(new AudioBuffer(
488 formats_.api_format.reverse_input_stream().num_frames(),
489 formats_.api_format.reverse_input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700490 formats_.render_processing_format.num_frames(),
491 formats_.render_processing_format.num_channels(),
492 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700493 if (formats_.api_format.reverse_input_stream() !=
494 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800495 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800496 formats_.api_format.reverse_input_stream().num_channels(),
497 formats_.api_format.reverse_input_stream().num_frames(),
498 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800499 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700500 } else {
peahdf3efa82015-11-28 12:35:15 -0800501 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700502 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700503 } else {
peahdf3efa82015-11-28 12:35:15 -0800504 render_.render_audio.reset(nullptr);
505 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700506 }
peahce4d9152017-05-19 01:28:05 -0700507
peahdf3efa82015-11-28 12:35:15 -0800508 capture_.capture_audio.reset(
509 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
510 formats_.api_format.input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700511 capture_nonlocked_.capture_processing_format.num_frames(),
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200512 formats_.api_format.output_stream().num_channels(),
peahdf3efa82015-11-28 12:35:15 -0800513 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000514
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100515 private_submodules_->echo_cancellation->Initialize(
peahde65ddc2016-09-16 15:02:15 -0700516 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
517 num_proc_channels());
peah764e3642016-10-22 05:04:30 -0700518 AllocateRenderQueue();
519
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100520 int success = private_submodules_->echo_cancellation->enable_metrics(true);
ivoc3e9a5372016-10-28 07:55:33 -0700521 RTC_DCHECK_EQ(0, success);
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100522 success = private_submodules_->echo_cancellation->enable_delay_logging(true);
ivoc3e9a5372016-10-28 07:55:33 -0700523 RTC_DCHECK_EQ(0, success);
Sam Zackrissonc22f5512018-11-05 16:10:00 +0100524 private_submodules_->echo_control_mobile->Initialize(
peahde65ddc2016-09-16 15:02:15 -0700525 proc_split_sample_rate_hz(), num_reverse_channels(),
526 num_output_channels());
peah135259a2016-10-28 03:12:11 -0700527
528 public_submodules_->gain_control->Initialize(num_proc_channels(),
529 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700530 if (constants_.use_experimental_agc) {
531 if (!private_submodules_->agc_manager.get()) {
532 private_submodules_->agc_manager.reset(new AgcManagerDirect(
533 public_submodules_->gain_control.get(),
534 public_submodules_->gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200535 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
536 constants_.use_experimental_agc_agc2_level_estimation,
537 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700538 }
539 private_submodules_->agc_manager->Initialize();
540 private_submodules_->agc_manager->SetCaptureMuted(
541 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700542 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700543 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200544 InitializeTransient();
peah8271d042016-11-22 07:24:52 -0800545 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700546 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
547 proc_sample_rate_hz());
548 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100549 if (private_submodules_->voice_detector) {
550 private_submodules_->voice_detector->Initialize(
551 proc_split_sample_rate_hz());
552 }
peahde65ddc2016-09-16 15:02:15 -0700553 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700554 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200555 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700556 InitializeGainController2();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200557 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200558 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100559 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800560
aleloi868f32f2017-05-23 07:20:05 -0700561 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200562 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700563 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000564 return kNoError;
565}
566
Michael Graczyk86c6d332015-07-23 11:41:39 -0700567int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200568 UpdateActiveSubmoduleStates();
569
Michael Graczyk86c6d332015-07-23 11:41:39 -0700570 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700571 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
572 return kBadSampleRateError;
573 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000574 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700575
Peter Kasting69558702016-01-12 16:26:35 -0800576 const size_t num_in_channels = config.input_stream().num_channels();
577 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700578
579 // Need at least one input channel.
580 // Need either one output channel or as many outputs as there are inputs.
581 if (num_in_channels == 0 ||
582 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700583 return kBadNumberChannelsError;
584 }
585
peahdf3efa82015-11-28 12:35:15 -0800586 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000587
peahde65ddc2016-09-16 15:02:15 -0700588 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700589 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700590 formats_.api_format.output_stream().sample_rate_hz()),
591 submodule_states_.CaptureMultiBandSubModulesActive() ||
592 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000593
peahde65ddc2016-09-16 15:02:15 -0700594 capture_nonlocked_.capture_processing_format =
595 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700596
peah2ce640f2017-04-07 03:57:48 -0700597 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200598 if (!capture_nonlocked_.echo_controller_enabled) {
peah2ce640f2017-04-07 03:57:48 -0700599 render_processing_rate = FindNativeProcessRateToUse(
600 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
601 formats_.api_format.reverse_output_stream().sample_rate_hz()),
602 submodule_states_.CaptureMultiBandSubModulesActive() ||
603 submodule_states_.RenderMultiBandSubModulesActive());
604 } else {
605 render_processing_rate = capture_processing_rate;
606 }
607
aluebseb3603b2016-04-20 15:27:58 -0700608 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
609 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700610 if (render_processing_rate > kSampleRate32kHz &&
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200611 !capture_nonlocked_.echo_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -0700612 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
613 ? kSampleRate32kHz
614 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700615 }
peah2ce640f2017-04-07 03:57:48 -0700616
peahde65ddc2016-09-16 15:02:15 -0700617 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700618 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700619 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
620 kSampleRate8kHz) {
621 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000622 } else {
peahde65ddc2016-09-16 15:02:15 -0700623 render_processing_rate =
624 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000625 }
626
peahde65ddc2016-09-16 15:02:15 -0700627 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000628 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700629 if (submodule_states_.RenderMultiBandSubModulesActive()) {
630 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
631 } else {
632 formats_.render_processing_format = StreamConfig(
633 formats_.api_format.reverse_input_stream().sample_rate_hz(),
634 formats_.api_format.reverse_input_stream().num_channels());
635 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000636
peahde65ddc2016-09-16 15:02:15 -0700637 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
638 kSampleRate32kHz ||
639 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
640 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800641 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000642 } else {
peahdf3efa82015-11-28 12:35:15 -0800643 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700644 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000645 }
646
647 return InitializeLocked();
648}
649
peah88ac8532016-09-12 16:47:25 -0700650void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peah88ac8532016-09-12 16:47:25 -0700651 // Run in a single-threaded manner when applying the settings.
652 rtc::CritScope cs_render(&crit_render_);
653 rtc::CritScope cs_capture(&crit_capture_);
654
Yves Gerey499bc6c2018-10-10 18:29:07 +0200655 config_ = config;
656
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100657 private_submodules_->echo_cancellation->Enable(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200658 config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode);
Sam Zackrissonc22f5512018-11-05 16:10:00 +0100659 private_submodules_->echo_control_mobile->Enable(
Sam Zackrisson8c147b62018-09-28 12:40:47 +0200660 config_.echo_canceller.enabled && config_.echo_canceller.mobile_mode);
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200661
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100662 private_submodules_->echo_cancellation->set_suppression_level(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200663 config.echo_canceller.legacy_moderate_suppression_level
sazabe490b22018-10-03 17:03:13 +0200664 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
665 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200666
peah8271d042016-11-22 07:24:52 -0800667 InitializeLowCutFilter();
668
Mirko Bonadei675513b2017-11-09 11:09:25 +0100669 RTC_LOG(LS_INFO) << "Highpass filter activated: "
670 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800671
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100672 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700673 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100674 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
675 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100676 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100677 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700678 config_.gain_controller2 = AudioProcessing::Config::GainController2();
679 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200680 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200681 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200682 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100683 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
684 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200685 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
686 << config_.pre_amplifier.enabled;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100687
688 if (config_.level_estimation.enabled &&
689 !private_submodules_->output_level_estimator) {
690 private_submodules_->output_level_estimator.reset(
691 new LevelEstimatorImpl(&crit_capture_));
692 private_submodules_->output_level_estimator->Enable(true);
693 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100694
695 if (config_.voice_detection.enabled && !private_submodules_->voice_detector) {
696 private_submodules_->voice_detector.reset(
697 new VoiceDetectionImpl(&crit_capture_));
698 private_submodules_->voice_detector->Enable(true);
699 private_submodules_->voice_detector->set_likelihood(
700 VoiceDetection::kVeryLowLikelihood);
701 private_submodules_->voice_detector->Initialize(
702 proc_split_sample_rate_hz());
703 }
peah88ac8532016-09-12 16:47:25 -0700704}
705
706void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800707 // Run in a single-threaded manner when setting the extra options.
708 rtc::CritScope cs_render(&crit_render_);
709 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000710
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100711 private_submodules_->echo_cancellation->SetExtraOptions(config);
peahb624d8c2016-03-05 03:01:14 -0800712
peahdf3efa82015-11-28 12:35:15 -0800713 if (capture_.transient_suppressor_enabled !=
714 config.Get<ExperimentalNs>().enabled) {
715 capture_.transient_suppressor_enabled =
716 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000717 InitializeTransient();
718 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000719}
720
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000721int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800722 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700723 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000724}
725
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000726int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800727 // Used as callback from submodules, hence locking is not allowed.
728 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000729}
730
Peter Kasting69558702016-01-12 16:26:35 -0800731size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800732 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700733 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000734}
735
Peter Kasting69558702016-01-12 16:26:35 -0800736size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800737 // Used as callback from submodules, hence locking is not allowed.
738 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000739}
740
Peter Kasting69558702016-01-12 16:26:35 -0800741size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800742 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200743 return capture_nonlocked_.echo_controller_enabled ? 1 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800744}
745
Peter Kasting69558702016-01-12 16:26:35 -0800746size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800747 // Used as callback from submodules, hence locking is not allowed.
748 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000749}
750
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000751void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800752 rtc::CritScope cs(&crit_capture_);
753 capture_.output_will_be_muted = muted;
754 if (private_submodules_->agc_manager.get()) {
755 private_submodules_->agc_manager->SetCaptureMuted(
756 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000757 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000758}
759
Alessio Bazzicac054e782018-04-16 12:10:09 +0200760void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200761 switch (setting.type()) {
762 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
763 render_runtime_settings_enqueuer_.Enqueue(setting);
764 return;
765 case RuntimeSetting::Type::kNotSpecified:
766 RTC_NOTREACHED();
767 return;
768 case RuntimeSetting::Type::kCapturePreGain:
769 capture_runtime_settings_enqueuer_.Enqueue(setting);
770 return;
771 }
772 // The language allows the enum to have a non-enumerator
773 // value. Check that this doesn't happen.
774 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200775}
776
777AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
778 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200779 : runtime_settings_(*runtime_settings) {
780 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200781}
782
783AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
784 default;
785
786void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
787 RuntimeSetting setting) {
788 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200789 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200790 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200791 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200792 RTC_LOG(LS_ERROR)
793 << "The runtime settings queue is full. Oldest setting discarded.";
794 }
795 if (remaining_attempts == 0)
796 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
797}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000798
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000799int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700800 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000801 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000802 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000803 int output_sample_rate_hz,
804 ChannelLayout output_layout,
805 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800806 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800807 StreamConfig input_stream;
808 StreamConfig output_stream;
809 {
810 // Access the formats_.api_format.input_stream beneath the capture lock.
811 // The lock must be released as it is later required in the call
812 // to ProcessStream(,,,);
813 rtc::CritScope cs(&crit_capture_);
814 input_stream = formats_.api_format.input_stream();
815 output_stream = formats_.api_format.output_stream();
816 }
817
Michael Graczyk86c6d332015-07-23 11:41:39 -0700818 input_stream.set_sample_rate_hz(input_sample_rate_hz);
819 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
820 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700821 output_stream.set_sample_rate_hz(output_sample_rate_hz);
822 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
823 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
824
825 if (samples_per_channel != input_stream.num_frames()) {
826 return kBadDataLengthError;
827 }
828 return ProcessStream(src, input_stream, output_stream, dest);
829}
830
831int AudioProcessingImpl::ProcessStream(const float* const* src,
832 const StreamConfig& input_config,
833 const StreamConfig& output_config,
834 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800835 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800836 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700837 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800838 {
839 // Acquire the capture lock in order to safely call the function
840 // that retrieves the render side data. This function accesses apm
841 // getters that need the capture lock held when being called.
842 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700843 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800844
845 if (!src || !dest) {
846 return kNullPointerError;
847 }
848
849 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700850 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000851 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000852
Michael Graczyk86c6d332015-07-23 11:41:39 -0700853 processing_config.input_stream() = input_config;
854 processing_config.output_stream() = output_config;
855
peahdf3efa82015-11-28 12:35:15 -0800856 {
857 // Do conditional reinitialization.
858 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -0700859 RETURN_ON_ERR(
860 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -0800861 }
862 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700863 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
864 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000865
aleloi868f32f2017-05-23 07:20:05 -0700866 if (aec_dump_) {
867 RecordUnprocessedCaptureStream(src);
868 }
869
peahdf3efa82015-11-28 12:35:15 -0800870 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700871 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800872 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000873
aleloi868f32f2017-05-23 07:20:05 -0700874 if (aec_dump_) {
875 RecordProcessedCaptureStream(dest);
876 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000877 return kNoError;
878}
879
Alex Loiko73ec0192018-05-15 10:52:28 +0200880void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200881 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200882 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200883 if (aec_dump_) {
884 aec_dump_->WriteRuntimeSetting(setting);
885 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200886 switch (setting.type()) {
887 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200888 if (config_.pre_amplifier.enabled) {
889 float value;
890 setting.GetFloat(&value);
891 private_submodules_->pre_amplifier->SetGainFactor(value);
892 }
893 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200894 break;
Alex Loiko73ec0192018-05-15 10:52:28 +0200895 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
896 RTC_NOTREACHED();
897 break;
898 case RuntimeSetting::Type::kNotSpecified:
899 RTC_NOTREACHED();
900 break;
901 }
902 }
903}
904
905void AudioProcessingImpl::HandleRenderRuntimeSettings() {
906 RuntimeSetting setting;
907 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200908 if (aec_dump_) {
909 aec_dump_->WriteRuntimeSetting(setting);
910 }
Alex Loiko73ec0192018-05-15 10:52:28 +0200911 switch (setting.type()) {
912 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
913 if (private_submodules_->render_pre_processor) {
914 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
915 }
916 break;
917 case RuntimeSetting::Type::kCapturePreGain:
918 RTC_NOTREACHED();
919 break;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200920 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +0200921 RTC_NOTREACHED();
922 break;
923 }
924 }
925}
926
peah9e6a2902017-05-15 07:19:21 -0700927void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
peah764e3642016-10-22 05:04:30 -0700928 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
929 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700930 &aec_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700931
kwibergaf476c72016-11-28 15:21:39 -0800932 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -0700933
934 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700935 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700936 // The data queue is full and needs to be emptied.
937 EmptyQueuedRenderAudio();
938
939 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700940 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700941 RTC_DCHECK(result);
942 }
943
944 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
945 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700946 &aecm_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700947
948 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700949 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700950 // The data queue is full and needs to be emptied.
951 EmptyQueuedRenderAudio();
952
953 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700954 bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700955 RTC_DCHECK(result);
956 }
peah701d6282016-10-25 05:42:20 -0700957
958 if (!constants_.use_experimental_agc) {
959 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
960 // Insert the samples into the queue.
961 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
962 // The data queue is full and needs to be emptied.
963 EmptyQueuedRenderAudio();
964
965 // Retry the insert (should always work).
966 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
967 RTC_DCHECK(result);
968 }
969 }
peah9e6a2902017-05-15 07:19:21 -0700970}
ivoc9f4a4a02016-10-28 05:39:16 -0700971
peah9e6a2902017-05-15 07:19:21 -0700972void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -0700973 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
974
975 // Insert the samples into the queue.
976 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
977 // The data queue is full and needs to be emptied.
978 EmptyQueuedRenderAudio();
979
980 // Retry the insert (should always work).
981 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
982 RTC_DCHECK(result);
983 }
peah764e3642016-10-22 05:04:30 -0700984}
985
986void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -0700987 const size_t new_aec_render_queue_element_max_size =
peah764e3642016-10-22 05:04:30 -0700988 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -0700989 kMaxAllowedValuesOfSamplesPerBand *
peah764e3642016-10-22 05:04:30 -0700990 EchoCancellationImpl::NumCancellersRequired(
991 num_output_channels(), num_reverse_channels()));
992
peah701d6282016-10-25 05:42:20 -0700993 const size_t new_aecm_render_queue_element_max_size =
peaha0624602016-10-25 04:45:24 -0700994 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -0700995 kMaxAllowedValuesOfSamplesPerBand *
peaha0624602016-10-25 04:45:24 -0700996 EchoControlMobileImpl::NumCancellersRequired(
997 num_output_channels(), num_reverse_channels()));
peah764e3642016-10-22 05:04:30 -0700998
peah701d6282016-10-25 05:42:20 -0700999 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001000 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001001
ivoc9f4a4a02016-10-28 05:39:16 -07001002 const size_t new_red_render_queue_element_max_size =
1003 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1004
peaha0624602016-10-25 04:45:24 -07001005 // Reallocate the queues if the queue item sizes are too small to fit the
1006 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001007 if (aec_render_queue_element_max_size_ <
1008 new_aec_render_queue_element_max_size) {
1009 aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
peah764e3642016-10-22 05:04:30 -07001010
peaha0624602016-10-25 04:45:24 -07001011 std::vector<float> template_queue_element(
peah701d6282016-10-25 05:42:20 -07001012 aec_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001013
peah701d6282016-10-25 05:42:20 -07001014 aec_render_signal_queue_.reset(
peah764e3642016-10-22 05:04:30 -07001015 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1016 kMaxNumFramesToBuffer, template_queue_element,
peaha0624602016-10-25 04:45:24 -07001017 RenderQueueItemVerifier<float>(
peah701d6282016-10-25 05:42:20 -07001018 aec_render_queue_element_max_size_)));
peah764e3642016-10-22 05:04:30 -07001019
peah701d6282016-10-25 05:42:20 -07001020 aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
1021 aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
peah764e3642016-10-22 05:04:30 -07001022 } else {
peah701d6282016-10-25 05:42:20 -07001023 aec_render_signal_queue_->Clear();
peaha0624602016-10-25 04:45:24 -07001024 }
1025
peah701d6282016-10-25 05:42:20 -07001026 if (aecm_render_queue_element_max_size_ <
1027 new_aecm_render_queue_element_max_size) {
1028 aecm_render_queue_element_max_size_ =
1029 new_aecm_render_queue_element_max_size;
peaha0624602016-10-25 04:45:24 -07001030
1031 std::vector<int16_t> template_queue_element(
peah701d6282016-10-25 05:42:20 -07001032 aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001033
peah701d6282016-10-25 05:42:20 -07001034 aecm_render_signal_queue_.reset(
peaha0624602016-10-25 04:45:24 -07001035 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1036 kMaxNumFramesToBuffer, template_queue_element,
1037 RenderQueueItemVerifier<int16_t>(
peah701d6282016-10-25 05:42:20 -07001038 aecm_render_queue_element_max_size_)));
peaha0624602016-10-25 04:45:24 -07001039
peah701d6282016-10-25 05:42:20 -07001040 aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
1041 aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001042 } else {
peah701d6282016-10-25 05:42:20 -07001043 aecm_render_signal_queue_->Clear();
1044 }
1045
1046 if (agc_render_queue_element_max_size_ <
1047 new_agc_render_queue_element_max_size) {
1048 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1049
1050 std::vector<int16_t> template_queue_element(
1051 agc_render_queue_element_max_size_);
1052
1053 agc_render_signal_queue_.reset(
1054 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1055 kMaxNumFramesToBuffer, template_queue_element,
1056 RenderQueueItemVerifier<int16_t>(
1057 agc_render_queue_element_max_size_)));
1058
1059 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1060 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1061 } else {
1062 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001063 }
ivoc9f4a4a02016-10-28 05:39:16 -07001064
1065 if (red_render_queue_element_max_size_ <
1066 new_red_render_queue_element_max_size) {
1067 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1068
1069 std::vector<float> template_queue_element(
1070 red_render_queue_element_max_size_);
1071
1072 red_render_signal_queue_.reset(
1073 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1074 kMaxNumFramesToBuffer, template_queue_element,
1075 RenderQueueItemVerifier<float>(
1076 red_render_queue_element_max_size_)));
1077
1078 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1079 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1080 } else {
1081 red_render_signal_queue_->Clear();
1082 }
peah764e3642016-10-22 05:04:30 -07001083}
1084
1085void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1086 rtc::CritScope cs_capture(&crit_capture_);
peah701d6282016-10-25 05:42:20 -07001087 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001088 private_submodules_->echo_cancellation->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001089 aec_capture_queue_buffer_);
peaha0624602016-10-25 04:45:24 -07001090 }
1091
peah701d6282016-10-25 05:42:20 -07001092 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001093 private_submodules_->echo_control_mobile->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001094 aecm_capture_queue_buffer_);
1095 }
1096
1097 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1098 public_submodules_->gain_control->ProcessRenderAudio(
1099 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001100 }
ivoc9f4a4a02016-10-28 05:39:16 -07001101
1102 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001103 RTC_DCHECK(private_submodules_->echo_detector);
1104 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001105 red_capture_queue_buffer_);
1106 }
peah764e3642016-10-22 05:04:30 -07001107}
1108
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001109int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001110 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001111 {
1112 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001113 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001114 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001115 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001116 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001117 }
peahfa6228e2015-11-16 16:27:42 -08001118
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001119 if (!frame) {
1120 return kNullPointerError;
1121 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001122 // Must be a native rate.
1123 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1124 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001125 frame->sample_rate_hz_ != kSampleRate32kHz &&
1126 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001127 return kBadSampleRateError;
1128 }
peah192164e2015-11-17 02:16:45 -08001129
peahdf3efa82015-11-28 12:35:15 -08001130 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001131 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001132 {
1133 // Aquire lock for the access of api_format.
1134 // The lock is released immediately due to the conditional
1135 // reinitialization.
1136 rtc::CritScope cs_capture(&crit_capture_);
1137 // TODO(ajm): The input and output rates and channels are currently
1138 // constrained to be identical in the int16 interface.
1139 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001140
1141 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001142 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001143 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1144 processing_config.input_stream().set_num_channels(frame->num_channels_);
1145 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1146 processing_config.output_stream().set_num_channels(frame->num_channels_);
1147
peahdf3efa82015-11-28 12:35:15 -08001148 {
1149 // Do conditional reinitialization.
1150 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -07001151 RETURN_ON_ERR(
1152 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -08001153 }
1154 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001155 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001156 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001157 return kBadDataLengthError;
1158 }
1159
aleloi868f32f2017-05-23 07:20:05 -07001160 if (aec_dump_) {
1161 RecordUnprocessedCaptureStream(*frame);
1162 }
1163
peahdf3efa82015-11-28 12:35:15 -08001164 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001165 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001166 capture_.capture_audio->InterleaveTo(
peah23ac8b42017-05-23 05:33:56 -07001167 frame, submodule_states_.CaptureMultiBandProcessingActive() ||
1168 submodule_states_.CaptureFullBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001169
aleloi868f32f2017-05-23 07:20:05 -07001170 if (aec_dump_) {
1171 RecordProcessedCaptureStream(*frame);
1172 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001173
1174 return kNoError;
1175}
1176
peahde65ddc2016-09-16 15:02:15 -07001177int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001178 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001179
peahb58a1582016-03-15 09:34:24 -07001180 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001181 // TODO(peah): Simplify once the public API Enable functions for these
1182 // are moved to APM.
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001183 RTC_DCHECK(!(private_submodules_->echo_cancellation->is_enabled() &&
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001184 private_submodules_->echo_control_mobile->is_enabled()));
peahb58a1582016-03-15 09:34:24 -07001185
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001186 MaybeUpdateHistograms();
1187
peahde65ddc2016-09-16 15:02:15 -07001188 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001189
Alex Loikob5c9a792018-04-16 16:31:22 +02001190 if (private_submodules_->pre_amplifier) {
1191 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
1192 capture_buffer->channels_f(), capture_buffer->num_channels(),
1193 capture_buffer->num_frames()));
1194 }
1195
peah1b08dc32016-12-20 13:45:58 -08001196 capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>(
henrik.lundin290d43a2016-11-29 08:09:09 -08001197 capture_buffer->channels_const()[0],
1198 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001199 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1200 if (log_rms) {
1201 capture_rms_interval_counter_ = 0;
1202 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001203 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1204 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1205 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1206 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001207 }
1208
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001209 if (private_submodules_->echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001210 // Detect and flag any change in the analog gain.
1211 int analog_mic_level = gain_control()->stream_analog_level();
1212 capture_.echo_path_gain_change =
1213 capture_.prev_analog_mic_level != analog_mic_level &&
1214 capture_.prev_analog_mic_level != -1;
1215 capture_.prev_analog_mic_level = analog_mic_level;
1216
Per Åhgrend2650d12018-10-02 17:00:59 +02001217 // Detect and flag any change in the pre-amplifier gain.
1218 if (private_submodules_->pre_amplifier) {
1219 float pre_amp_gain = private_submodules_->pre_amplifier->GetGainFactor();
1220 capture_.echo_path_gain_change =
1221 capture_.echo_path_gain_change ||
1222 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001223 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001224 capture_.prev_pre_amp_gain = pre_amp_gain;
1225 }
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001226 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001227 }
1228
peahbe615622016-02-13 16:40:47 -08001229 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001230 public_submodules_->gain_control->is_enabled()) {
1231 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001232 capture_buffer->channels()[0], capture_buffer->num_channels(),
1233 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001234
1235 if (constants_.use_experimental_agc_process_before_aec) {
1236 private_submodules_->agc_manager->Process(
1237 capture_buffer->channels()[0],
1238 capture_nonlocked_.capture_processing_format.num_frames(),
1239 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1240 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001241 }
1242
peah2ace3f92016-09-10 04:42:27 -07001243 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1244 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001245 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1246 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001247 }
1248
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001249 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001250 // Force down-mixing of the number of channels after the detection of
1251 // capture signal saturation.
1252 // TODO(peah): Look into ensuring that this kind of tampering with the
1253 // AudioBuffer functionality should not be needed.
1254 capture_buffer->set_num_channels(1);
1255 }
1256
peahe0eae3c2016-12-14 01:16:23 -08001257 // TODO(peah): Move the AEC3 low-cut filter to this place.
1258 if (private_submodules_->low_cut_filter &&
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001259 !private_submodules_->echo_controller) {
peah8271d042016-11-22 07:24:52 -08001260 private_submodules_->low_cut_filter->Process(capture_buffer);
1261 }
peahde65ddc2016-09-16 15:02:15 -07001262 RETURN_ON_ERR(
1263 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1264 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001265
1266 // Ensure that the stream delay was set before the call to the
1267 // AEC ProcessCaptureAudio function.
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001268 if (private_submodules_->echo_cancellation->is_enabled() &&
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001269 !private_submodules_->echo_controller && !was_stream_delay_set()) {
peahb58a1582016-03-15 09:34:24 -07001270 return AudioProcessing::kStreamParameterNotSetError;
1271 }
1272
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001273 if (private_submodules_->echo_controller) {
Per Åhgren13735822018-02-12 21:42:56 +01001274 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1275
Per Åhgrend0fa8202018-04-18 09:35:13 +02001276 if (was_stream_delay_set()) {
1277 private_submodules_->echo_controller->SetAudioBufferDelay(
1278 stream_delay_ms());
1279 }
1280
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001281 private_submodules_->echo_controller->ProcessCapture(
peah67995532017-04-10 14:12:41 -07001282 capture_buffer, capture_.echo_path_gain_change);
peah61202ac2017-02-06 03:39:42 -08001283 } else {
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001284 RETURN_ON_ERR(private_submodules_->echo_cancellation->ProcessCaptureAudio(
peah61202ac2017-02-06 03:39:42 -08001285 capture_buffer, stream_delay_ms()));
peahe0eae3c2016-12-14 01:16:23 -08001286 }
1287
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001288 if (private_submodules_->echo_control_mobile->is_enabled() &&
peahdf3efa82015-11-28 12:35:15 -08001289 public_submodules_->noise_suppression->is_enabled()) {
peahde65ddc2016-09-16 15:02:15 -07001290 capture_buffer->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +00001291 }
peahde65ddc2016-09-16 15:02:15 -07001292 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah253534d2016-03-15 04:32:28 -07001293
1294 // Ensure that the stream delay was set before the call to the
1295 // AECM ProcessCaptureAudio function.
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001296 if (private_submodules_->echo_control_mobile->is_enabled() &&
peah253534d2016-03-15 04:32:28 -07001297 !was_stream_delay_set()) {
1298 return AudioProcessing::kStreamParameterNotSetError;
1299 }
1300
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001301 if (!(private_submodules_->echo_controller ||
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001302 private_submodules_->echo_cancellation->is_enabled())) {
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001303 RETURN_ON_ERR(private_submodules_->echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001304 capture_buffer, stream_delay_ms()));
1305 }
ivoc9f4a4a02016-10-28 05:39:16 -07001306
peahde65ddc2016-09-16 15:02:15 -07001307 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001308 if (config_.voice_detection.enabled) {
1309 private_submodules_->voice_detector->ProcessCaptureAudio(capture_buffer);
1310 capture_.stats.voice_detected =
1311 private_submodules_->voice_detector->stream_has_voice();
1312 } else {
1313 capture_.stats.voice_detected = absl::nullopt;
1314 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001315
peahbe615622016-02-13 16:40:47 -08001316 if (constants_.use_experimental_agc &&
Alex Loikod9342442018-09-10 13:59:41 +02001317 public_submodules_->gain_control->is_enabled() &&
1318 !constants_.use_experimental_agc_process_before_aec) {
peahdf3efa82015-11-28 12:35:15 -08001319 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001320 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1321 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001322 }
peahb8fbb542016-03-15 02:28:08 -07001323 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001324 capture_buffer,
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001325 private_submodules_->echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001326
peah2ace3f92016-09-10 04:42:27 -07001327 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1328 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001329 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1330 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001331 }
1332
peah9e6a2902017-05-15 07:19:21 -07001333 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001334 RTC_DCHECK(private_submodules_->echo_detector);
1335 private_submodules_->echo_detector->AnalyzeCaptureAudio(
peah9e6a2902017-05-15 07:19:21 -07001336 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
1337 capture_buffer->num_frames()));
1338 }
1339
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001340 // TODO(aluebs): Investigate if the transient suppression placement should be
1341 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001342 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001343 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001344 private_submodules_->agc_manager.get()
1345 ? private_submodules_->agc_manager->voice_probability()
1346 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001347
peahdf3efa82015-11-28 12:35:15 -08001348 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001349 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1350 capture_buffer->num_channels(),
1351 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1352 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1353 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001354 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001355 }
1356
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001357 // Experimental APM sub-module that analyzes |capture_buffer|.
1358 if (private_submodules_->capture_analyzer) {
1359 private_submodules_->capture_analyzer->Analyze(capture_buffer);
1360 }
1361
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001362 if (config_.gain_controller2.enabled) {
Alex Loikoa837dd72018-08-06 16:32:12 +02001363 private_submodules_->gain_controller2->NotifyAnalogLevel(
1364 gain_control()->stream_analog_level());
alessiob3ec96df2017-05-22 06:57:06 -07001365 private_submodules_->gain_controller2->Process(capture_buffer);
1366 }
1367
Sam Zackrisson0beac582017-09-25 12:04:02 +02001368 if (private_submodules_->capture_post_processor) {
1369 private_submodules_->capture_post_processor->Process(capture_buffer);
1370 }
1371
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001372 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001373 public_submodules_->level_estimator->ProcessStream(capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001374 if (config_.level_estimation.enabled) {
1375 private_submodules_->output_level_estimator->ProcessStream(capture_buffer);
1376 capture_.stats.output_rms_dbfs =
1377 private_submodules_->output_level_estimator->RMS();
1378 } else {
1379 capture_.stats.output_rms_dbfs = absl::nullopt;
1380 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001381
peah1b08dc32016-12-20 13:45:58 -08001382 capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>(
1383 capture_buffer->channels_const()[0],
1384 capture_nonlocked_.capture_processing_format.num_frames()));
1385 if (log_rms) {
1386 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1387 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1388 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1389 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1390 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1391 }
1392
peahdf3efa82015-11-28 12:35:15 -08001393 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001394 return kNoError;
1395}
1396
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001397int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001398 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001399 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001400 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001401 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001402 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001403 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001404 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001405 };
1406 if (samples_per_channel != reverse_config.num_frames()) {
1407 return kBadDataLengthError;
1408 }
peahdf3efa82015-11-28 12:35:15 -08001409 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001410}
1411
peahde65ddc2016-09-16 15:02:15 -07001412int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1413 const StreamConfig& input_config,
1414 const StreamConfig& output_config,
1415 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001416 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001417 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001418 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001419 if (submodule_states_.RenderMultiBandProcessingActive() ||
1420 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001421 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1422 dest);
peah2ace3f92016-09-10 04:42:27 -07001423 } else if (formats_.api_format.reverse_input_stream() !=
1424 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001425 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1426 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001427 } else {
peahde65ddc2016-09-16 15:02:15 -07001428 CopyAudioIfNeeded(src, input_config.num_frames(),
1429 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001430 }
1431
1432 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001433}
1434
peahdf3efa82015-11-28 12:35:15 -08001435int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001436 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001437 const StreamConfig& input_config,
1438 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001439 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001440 return kNullPointerError;
1441 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001442
peahde65ddc2016-09-16 15:02:15 -07001443 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001444 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001445 }
1446
peahdf3efa82015-11-28 12:35:15 -08001447 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001448 processing_config.reverse_input_stream() = input_config;
1449 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001450
peahdf3efa82015-11-28 12:35:15 -08001451 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001452 RTC_DCHECK_EQ(input_config.num_frames(),
1453 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001454
aleloi868f32f2017-05-23 07:20:05 -07001455 if (aec_dump_) {
1456 const size_t channel_size =
1457 formats_.api_format.reverse_input_stream().num_frames();
1458 const size_t num_channels =
1459 formats_.api_format.reverse_input_stream().num_channels();
1460 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001461 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001462 }
peahdf3efa82015-11-28 12:35:15 -08001463 render_.render_audio->CopyFrom(src,
1464 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001465 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001466}
1467
1468int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001469 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001470 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001471 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001472 return kNullPointerError;
1473 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001474 // Must be a native rate.
1475 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1476 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001477 frame->sample_rate_hz_ != kSampleRate32kHz &&
1478 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001479 return kBadSampleRateError;
1480 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001481
Michael Graczyk86c6d332015-07-23 11:41:39 -07001482 if (frame->num_channels_ <= 0) {
1483 return kBadNumberChannelsError;
1484 }
1485
peahdf3efa82015-11-28 12:35:15 -08001486 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001487 processing_config.reverse_input_stream().set_sample_rate_hz(
1488 frame->sample_rate_hz_);
1489 processing_config.reverse_input_stream().set_num_channels(
1490 frame->num_channels_);
1491 processing_config.reverse_output_stream().set_sample_rate_hz(
1492 frame->sample_rate_hz_);
1493 processing_config.reverse_output_stream().set_num_channels(
1494 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001495
peahdf3efa82015-11-28 12:35:15 -08001496 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001497 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001498 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001499 return kBadDataLengthError;
1500 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001501
aleloi868f32f2017-05-23 07:20:05 -07001502 if (aec_dump_) {
1503 aec_dump_->WriteRenderStreamMessage(*frame);
1504 }
1505
peahdf3efa82015-11-28 12:35:15 -08001506 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001507 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001508 render_.render_audio->InterleaveTo(
Alex Loiko5825aa62017-12-18 16:02:40 +01001509 frame, submodule_states_.RenderMultiBandProcessingActive() ||
1510 submodule_states_.RenderFullBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001511 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001512}
niklase@google.com470e71d2011-07-07 08:21:25 +00001513
peahde65ddc2016-09-16 15:02:15 -07001514int AudioProcessingImpl::ProcessRenderStreamLocked() {
1515 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001516
Alex Loiko73ec0192018-05-15 10:52:28 +02001517 HandleRenderRuntimeSettings();
1518
Alex Loiko5825aa62017-12-18 16:02:40 +01001519 if (private_submodules_->render_pre_processor) {
1520 private_submodules_->render_pre_processor->Process(render_buffer);
1521 }
1522
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001523 QueueNonbandedRenderAudio(render_buffer);
1524
peah2ace3f92016-09-10 04:42:27 -07001525 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001526 SampleRateSupportsMultiBand(
1527 formats_.render_processing_format.sample_rate_hz())) {
1528 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001529 }
1530
peahce4d9152017-05-19 01:28:05 -07001531 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1532 QueueBandedRenderAudio(render_buffer);
1533 }
1534
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001535 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001536 if (private_submodules_->echo_controller) {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001537 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001538 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001539
peah2ace3f92016-09-10 04:42:27 -07001540 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001541 SampleRateSupportsMultiBand(
1542 formats_.render_processing_format.sample_rate_hz())) {
1543 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001544 }
1545
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001546 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001547}
1548
1549int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001550 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001551 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001552 capture_.was_stream_delay_set = true;
1553 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001554
niklase@google.com470e71d2011-07-07 08:21:25 +00001555 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001556 delay = 0;
1557 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001558 }
1559
1560 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1561 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001562 delay = 500;
1563 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001564 }
1565
peahdf3efa82015-11-28 12:35:15 -08001566 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001567 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001568}
1569
1570int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001571 // Used as callback from submodules, hence locking is not allowed.
1572 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001573}
1574
1575bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001576 // Used as callback from submodules, hence locking is not allowed.
1577 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001578}
1579
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001580void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001581 rtc::CritScope cs(&crit_capture_);
1582 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001583}
1584
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001585void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001586 rtc::CritScope cs(&crit_capture_);
1587 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001588}
1589
1590int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001591 rtc::CritScope cs(&crit_capture_);
1592 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001593}
1594
aleloi868f32f2017-05-23 07:20:05 -07001595void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1596 RTC_DCHECK(aec_dump);
1597 rtc::CritScope cs_render(&crit_render_);
1598 rtc::CritScope cs_capture(&crit_capture_);
1599
1600 // The previously attached AecDump will be destroyed with the
1601 // 'aec_dump' parameter, which is after locks are released.
1602 aec_dump_.swap(aec_dump);
1603 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001604 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001605}
1606
1607void AudioProcessingImpl::DetachAecDump() {
1608 // The d-tor of a task-queue based AecDump blocks until all pending
1609 // tasks are done. This construction avoids blocking while holding
1610 // the render and capture locks.
1611 std::unique_ptr<AecDump> aec_dump = nullptr;
1612 {
1613 rtc::CritScope cs_render(&crit_render_);
1614 rtc::CritScope cs_capture(&crit_capture_);
1615 aec_dump = std::move(aec_dump_);
1616 }
1617}
1618
Sam Zackrisson4d364492018-03-02 16:03:21 +01001619void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1620 std::unique_ptr<AudioGenerator> audio_generator) {
1621 // TODO(bugs.webrtc.org/8882) Stub.
1622 // Reset internal audio generator with audio_generator.
1623}
1624
1625void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1626 // TODO(bugs.webrtc.org/8882) Stub.
1627 // Delete audio generator, if one is attached.
1628}
1629
Ivo Creusen56d46092017-11-24 17:29:59 +01001630AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001631 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001632 rtc::CritScope cs_capture(&crit_capture_);
1633 if (!has_remote_tracks) {
1634 return capture_.stats;
1635 }
1636 AudioProcessingStats stats = capture_.stats;
1637 EchoCancellationImpl::Metrics metrics;
1638 if (private_submodules_->echo_controller) {
1639 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1640 stats.echo_return_loss = ec_metrics.echo_return_loss;
1641 stats.echo_return_loss_enhancement =
1642 ec_metrics.echo_return_loss_enhancement;
1643 stats.delay_ms = ec_metrics.delay_ms;
1644 } else if (private_submodules_->echo_cancellation->GetMetrics(&metrics) ==
1645 Error::kNoError) {
1646 if (metrics.divergent_filter_fraction != -1.0f) {
1647 stats.divergent_filter_fraction =
1648 absl::optional<double>(metrics.divergent_filter_fraction);
1649 }
1650 if (metrics.echo_return_loss.instant != -100) {
1651 stats.echo_return_loss =
1652 absl::optional<double>(metrics.echo_return_loss.instant);
1653 }
1654 if (metrics.echo_return_loss_enhancement.instant != -100) {
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001655 stats.echo_return_loss_enhancement =
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001656 absl::optional<double>(metrics.echo_return_loss_enhancement.instant);
Ivo Creusenae026092017-11-20 13:07:16 +01001657 }
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001658 }
1659 if (config_.residual_echo_detector.enabled) {
1660 RTC_DCHECK(private_submodules_->echo_detector);
1661 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1662 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1663 stats.residual_echo_likelihood_recent_max =
1664 ed_metrics.echo_likelihood_recent_max;
1665 }
1666 int delay_median, delay_std;
1667 float fraction_poor_delays;
1668 if (private_submodules_->echo_cancellation->GetDelayMetrics(
1669 &delay_median, &delay_std, &fraction_poor_delays) ==
1670 Error::kNoError) {
1671 if (delay_median >= 0) {
1672 stats.delay_median_ms = absl::optional<int32_t>(delay_median);
Ivo Creusenae026092017-11-20 13:07:16 +01001673 }
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001674 if (delay_std >= 0) {
1675 stats.delay_standard_deviation_ms = absl::optional<int32_t>(delay_std);
Ivo Creusenae026092017-11-20 13:07:16 +01001676 }
1677 }
1678 return stats;
1679}
1680
niklase@google.com470e71d2011-07-07 08:21:25 +00001681GainControl* AudioProcessingImpl::gain_control() const {
peahbe615622016-02-13 16:40:47 -08001682 if (constants_.use_experimental_agc) {
1683 return public_submodules_->gain_control_for_experimental_agc.get();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001684 }
peahbfa97112016-03-10 21:09:04 -08001685 return public_submodules_->gain_control.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001686}
1687
niklase@google.com470e71d2011-07-07 08:21:25 +00001688LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001689 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001690}
1691
1692NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
solenberg5e465c32015-12-08 13:22:33 -08001693 return public_submodules_->noise_suppression.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001694}
1695
1696VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001697 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001698}
1699
peah8271d042016-11-22 07:24:52 -08001700void AudioProcessingImpl::MutateConfig(
1701 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1702 rtc::CritScope cs_render(&crit_render_);
1703 rtc::CritScope cs_capture(&crit_capture_);
1704 mutator(&config_);
1705 ApplyConfig(config_);
1706}
1707
1708AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1709 rtc::CritScope cs_render(&crit_render_);
1710 rtc::CritScope cs_capture(&crit_capture_);
1711 return config_;
1712}
1713
peah2ace3f92016-09-10 04:42:27 -07001714bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1715 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001716 config_.high_pass_filter.enabled,
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001717 private_submodules_->echo_cancellation->is_enabled(),
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001718 private_submodules_->echo_control_mobile->is_enabled(),
ivoc9f4a4a02016-10-28 05:39:16 -07001719 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001720 public_submodules_->noise_suppression->is_enabled(),
peah2ace3f92016-09-10 04:42:27 -07001721 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001722 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001723 capture_nonlocked_.echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -07001724 public_submodules_->voice_detection->is_enabled(),
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001725 config_.voice_detection.enabled,
peah2ace3f92016-09-10 04:42:27 -07001726 public_submodules_->level_estimator->is_enabled(),
1727 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001728}
1729
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001730void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001731 if (capture_.transient_suppressor_enabled) {
1732 if (!public_submodules_->transient_suppressor.get()) {
1733 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001734 }
peahdf3efa82015-11-28 12:35:15 -08001735 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001736 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1737 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001738 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001739}
1740
peah8271d042016-11-22 07:24:52 -08001741void AudioProcessingImpl::InitializeLowCutFilter() {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001742 if (submodule_states_.LowCutFilteringRequired()) {
peah8271d042016-11-22 07:24:52 -08001743 private_submodules_->low_cut_filter.reset(
1744 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1745 } else {
1746 private_submodules_->low_cut_filter.reset();
1747 }
1748}
alessiob3ec96df2017-05-22 06:57:06 -07001749
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001750void AudioProcessingImpl::InitializeEchoController() {
Gustaf Ullberg002ef282017-10-12 15:13:17 +02001751 if (echo_control_factory_) {
1752 private_submodules_->echo_controller =
1753 echo_control_factory_->Create(proc_sample_rate_hz());
peahe0eae3c2016-12-14 01:16:23 -08001754 } else {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001755 private_submodules_->echo_controller.reset();
peahe0eae3c2016-12-14 01:16:23 -08001756 }
1757}
peah8271d042016-11-22 07:24:52 -08001758
alessiob3ec96df2017-05-22 06:57:06 -07001759void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001760 if (config_.gain_controller2.enabled) {
1761 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001762 }
1763}
1764
Alex Loikob5c9a792018-04-16 16:31:22 +02001765void AudioProcessingImpl::InitializePreAmplifier() {
1766 if (config_.pre_amplifier.enabled) {
1767 private_submodules_->pre_amplifier.reset(
1768 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1769 } else {
1770 private_submodules_->pre_amplifier.reset();
1771 }
1772}
1773
ivoc9f4a4a02016-10-28 05:39:16 -07001774void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001775 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001776 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001777 proc_sample_rate_hz(), 1,
1778 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001779}
1780
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001781void AudioProcessingImpl::InitializeAnalyzer() {
1782 if (private_submodules_->capture_analyzer) {
1783 private_submodules_->capture_analyzer->Initialize(proc_sample_rate_hz(),
1784 num_proc_channels());
1785 }
1786}
1787
Sam Zackrisson0beac582017-09-25 12:04:02 +02001788void AudioProcessingImpl::InitializePostProcessor() {
1789 if (private_submodules_->capture_post_processor) {
1790 private_submodules_->capture_post_processor->Initialize(
1791 proc_sample_rate_hz(), num_proc_channels());
1792 }
1793}
1794
Alex Loiko5825aa62017-12-18 16:02:40 +01001795void AudioProcessingImpl::InitializePreProcessor() {
1796 if (private_submodules_->render_pre_processor) {
1797 private_submodules_->render_pre_processor->Initialize(
1798 formats_.render_processing_format.sample_rate_hz(),
1799 formats_.render_processing_format.num_channels());
1800 }
1801}
1802
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001803void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001804 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001805
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001806 if (private_submodules_->echo_cancellation->is_enabled()) {
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001807 // Activate delay_jumps_ counters if we know echo_cancellation is running.
1808 // If a stream has echo we know that the echo_cancellation is in process.
peahdf3efa82015-11-28 12:35:15 -08001809 if (capture_.stream_delay_jumps == -1 &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001810 private_submodules_->echo_cancellation->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001811 capture_.stream_delay_jumps = 0;
1812 }
1813 if (capture_.aec_system_delay_jumps == -1 &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001814 private_submodules_->echo_cancellation->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001815 capture_.aec_system_delay_jumps = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001816 }
1817
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001818 // Detect a jump in platform reported system delay and log the difference.
peahdf3efa82015-11-28 12:35:15 -08001819 const int diff_stream_delay_ms =
1820 capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
1821 if (diff_stream_delay_ms > kMinDiffDelayMs &&
1822 capture_.last_stream_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001823 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1824 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
peahdf3efa82015-11-28 12:35:15 -08001825 if (capture_.stream_delay_jumps == -1) {
1826 capture_.stream_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001827 }
peahdf3efa82015-11-28 12:35:15 -08001828 capture_.stream_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001829 }
peahdf3efa82015-11-28 12:35:15 -08001830 capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001831
1832 // Detect a jump in AEC system delay and log the difference.
peah20028c42016-03-04 11:50:54 -08001833 const int samples_per_ms =
peahdf3efa82015-11-28 12:35:15 -08001834 rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
peah20028c42016-03-04 11:50:54 -08001835 RTC_DCHECK_LT(0, samples_per_ms);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001836 const int aec_system_delay_ms =
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001837 private_submodules_->echo_cancellation->GetSystemDelayInSamples() /
peah20028c42016-03-04 11:50:54 -08001838 samples_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001839 const int diff_aec_system_delay_ms =
peahdf3efa82015-11-28 12:35:15 -08001840 aec_system_delay_ms - capture_.last_aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001841 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
peahdf3efa82015-11-28 12:35:15 -08001842 capture_.last_aec_system_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001843 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1844 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1845 100);
peahdf3efa82015-11-28 12:35:15 -08001846 if (capture_.aec_system_delay_jumps == -1) {
1847 capture_.aec_system_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001848 }
peahdf3efa82015-11-28 12:35:15 -08001849 capture_.aec_system_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001850 }
peahdf3efa82015-11-28 12:35:15 -08001851 capture_.last_aec_system_delay_ms = aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001852 }
1853}
1854
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001855void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
peahdf3efa82015-11-28 12:35:15 -08001856 // Run in a single-threaded manner.
1857 rtc::CritScope cs_render(&crit_render_);
1858 rtc::CritScope cs_capture(&crit_capture_);
1859
1860 if (capture_.stream_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001861 RTC_HISTOGRAM_ENUMERATION(
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001862 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
peahdf3efa82015-11-28 12:35:15 -08001863 capture_.stream_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001864 }
peahdf3efa82015-11-28 12:35:15 -08001865 capture_.stream_delay_jumps = -1;
1866 capture_.last_stream_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001867
peahdf3efa82015-11-28 12:35:15 -08001868 if (capture_.aec_system_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001869 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1870 capture_.aec_system_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001871 }
peahdf3efa82015-11-28 12:35:15 -08001872 capture_.aec_system_delay_jumps = -1;
1873 capture_.last_aec_system_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001874}
1875
aleloi868f32f2017-05-23 07:20:05 -07001876void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1877 if (!aec_dump_) {
1878 return;
1879 }
1880 std::string experiments_description =
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001881 private_submodules_->echo_cancellation->GetExperimentsDescription();
aleloi868f32f2017-05-23 07:20:05 -07001882 // TODO(peah): Add semicolon-separated concatenations of experiment
1883 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001884 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1885 experiments_description += "AgcClippingLevelExperiment;";
1886 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001887 if (capture_nonlocked_.echo_controller_enabled) {
1888 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001889 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001890 if (config_.gain_controller2.enabled) {
1891 experiments_description += "GainController2;";
1892 }
aleloi868f32f2017-05-23 07:20:05 -07001893
1894 InternalAPMConfig apm_config;
1895
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001896 apm_config.aec_enabled = private_submodules_->echo_cancellation->is_enabled();
aleloi868f32f2017-05-23 07:20:05 -07001897 apm_config.aec_delay_agnostic_enabled =
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001898 private_submodules_->echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07001899 apm_config.aec_drift_compensation_enabled =
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001900 private_submodules_->echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07001901 apm_config.aec_extended_filter_enabled =
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001902 private_submodules_->echo_cancellation->is_extended_filter_enabled();
aleloi868f32f2017-05-23 07:20:05 -07001903 apm_config.aec_suppression_level = static_cast<int>(
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001904 private_submodules_->echo_cancellation->suppression_level());
aleloi868f32f2017-05-23 07:20:05 -07001905
1906 apm_config.aecm_enabled =
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001907 private_submodules_->echo_control_mobile->is_enabled();
aleloi868f32f2017-05-23 07:20:05 -07001908 apm_config.aecm_comfort_noise_enabled =
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001909 private_submodules_->echo_control_mobile->is_comfort_noise_enabled();
1910 apm_config.aecm_routing_mode = static_cast<int>(
1911 private_submodules_->echo_control_mobile->routing_mode());
aleloi868f32f2017-05-23 07:20:05 -07001912
1913 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
1914 apm_config.agc_mode =
1915 static_cast<int>(public_submodules_->gain_control->mode());
1916 apm_config.agc_limiter_enabled =
1917 public_submodules_->gain_control->is_limiter_enabled();
1918 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
1919
1920 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
1921
1922 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
1923 apm_config.ns_level =
1924 static_cast<int>(public_submodules_->noise_suppression->level());
1925
1926 apm_config.transient_suppression_enabled =
1927 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07001928 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02001929 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
1930 apm_config.pre_amplifier_fixed_gain_factor =
1931 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07001932
1933 if (!forced && apm_config == apm_config_for_aec_dump_) {
1934 return;
1935 }
1936 aec_dump_->WriteConfig(apm_config);
1937 apm_config_for_aec_dump_ = apm_config;
1938}
1939
1940void AudioProcessingImpl::RecordUnprocessedCaptureStream(
1941 const float* const* src) {
1942 RTC_DCHECK(aec_dump_);
1943 WriteAecDumpConfigMessage(false);
1944
1945 const size_t channel_size = formats_.api_format.input_stream().num_frames();
1946 const size_t num_channels = formats_.api_format.input_stream().num_channels();
1947 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001948 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001949 RecordAudioProcessingState();
1950}
1951
1952void AudioProcessingImpl::RecordUnprocessedCaptureStream(
1953 const AudioFrame& capture_frame) {
1954 RTC_DCHECK(aec_dump_);
1955 WriteAecDumpConfigMessage(false);
1956
1957 aec_dump_->AddCaptureStreamInput(capture_frame);
1958 RecordAudioProcessingState();
1959}
1960
1961void AudioProcessingImpl::RecordProcessedCaptureStream(
1962 const float* const* processed_capture_stream) {
1963 RTC_DCHECK(aec_dump_);
1964
1965 const size_t channel_size = formats_.api_format.output_stream().num_frames();
1966 const size_t num_channels =
1967 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001968 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
1969 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001970 aec_dump_->WriteCaptureStreamMessage();
1971}
1972
1973void AudioProcessingImpl::RecordProcessedCaptureStream(
1974 const AudioFrame& processed_capture_frame) {
1975 RTC_DCHECK(aec_dump_);
1976
1977 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
1978 aec_dump_->WriteCaptureStreamMessage();
1979}
1980
1981void AudioProcessingImpl::RecordAudioProcessingState() {
1982 RTC_DCHECK(aec_dump_);
1983 AecDump::AudioProcessingState audio_proc_state;
1984 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
1985 audio_proc_state.drift =
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001986 private_submodules_->echo_cancellation->stream_drift_samples();
aleloi868f32f2017-05-23 07:20:05 -07001987 audio_proc_state.level = gain_control()->stream_analog_level();
1988 audio_proc_state.keypress = capture_.key_pressed;
1989 aec_dump_->AddAudioProcessingState(audio_proc_state);
1990}
1991
kwiberg83ffe452016-08-29 14:46:07 -07001992AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02001993 bool transient_suppressor_enabled)
kwiberg83ffe452016-08-29 14:46:07 -07001994 : aec_system_delay_jumps(-1),
1995 delay_offset_ms(0),
1996 was_stream_delay_set(false),
1997 last_stream_delay_ms(0),
1998 last_aec_system_delay_ms(0),
1999 stream_delay_jumps(-1),
2000 output_will_be_muted(false),
2001 key_pressed(false),
2002 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002003 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002004 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002005 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002006 prev_analog_mic_level(-1),
2007 prev_pre_amp_gain(-1.f) {}
kwiberg83ffe452016-08-29 14:46:07 -07002008
2009AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2010
2011AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2012
2013AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2014
niklase@google.com470e71d2011-07-07 08:21:25 +00002015} // namespace webrtc