blob: a7000382886a9f579442fc2490b5d3d6750c5226 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org40654032012-01-30 20:51:15 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/audio_processing_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Michael Graczyk86c6d332015-07-23 11:41:39 -070013#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <cstdint>
alessiob3ec96df2017-05-22 06:57:06 -070015#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <type_traits>
17#include <utility>
niklase@google.com470e71d2011-07-07 08:21:25 +000018
Per Åhgren200feba2019-03-06 04:16:46 +010019#include "absl/memory/memory.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "absl/types/optional.h"
21#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_audio/audio_converter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_audio/include/audio_util.h"
Per Åhgren200feba2019-03-06 04:16:46 +010024#include "modules/audio_processing/aec3/echo_canceller3.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/agc/agc_manager_direct.h"
Alex Loikob5c9a792018-04-16 16:31:22 +020026#include "modules/audio_processing/agc2/gain_applier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/common.h"
29#include "modules/audio_processing/echo_cancellation_impl.h"
30#include "modules/audio_processing/echo_control_mobile_impl.h"
Sam Zackrissonf0d1c032019-03-27 13:28:08 +010031#include "modules/audio_processing/gain_control_config_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_processing/gain_control_for_experimental_agc.h"
33#include "modules/audio_processing/gain_control_impl.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010034#include "modules/audio_processing/gain_controller2.h"
Yves Gerey988cc082018-10-23 12:03:01 +020035#include "modules/audio_processing/include/audio_frame_view.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020036#include "modules/audio_processing/level_estimator_impl.h"
Per Åhgren13735822018-02-12 21:42:56 +010037#include "modules/audio_processing/logging/apm_data_dumper.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020038#include "modules/audio_processing/low_cut_filter.h"
39#include "modules/audio_processing/noise_suppression_impl.h"
Sam Zackrisson23513132019-01-11 15:10:32 +010040#include "modules/audio_processing/noise_suppression_proxy.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020041#include "modules/audio_processing/residual_echo_detector.h"
42#include "modules/audio_processing/transient/transient_suppressor.h"
43#include "modules/audio_processing/voice_detection_impl.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080048#include "rtc_base/ref_counted_object.h"
49#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "rtc_base/trace_event.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000052
Michael Graczyk86c6d332015-07-23 11:41:39 -070053#define RETURN_ON_ERR(expr) \
54 do { \
55 int err = (expr); \
56 if (err != kNoError) { \
57 return err; \
58 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000059 } while (0)
60
niklase@google.com470e71d2011-07-07 08:21:25 +000061namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070062
kwibergd59d3bb2016-09-13 07:49:33 -070063constexpr int AudioProcessing::kNativeSampleRatesHz[];
Alex Loiko73ec0192018-05-15 10:52:28 +020064constexpr int kRuntimeSettingQueueSize = 100;
aluebsdf6416a2016-03-16 18:26:35 -070065
Michael Graczyk86c6d332015-07-23 11:41:39 -070066namespace {
67
68static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
69 switch (layout) {
70 case AudioProcessing::kMono:
71 case AudioProcessing::kStereo:
72 return false;
73 case AudioProcessing::kMonoAndKeyboard:
74 case AudioProcessing::kStereoAndKeyboard:
75 return true;
76 }
77
kwiberg9e2be5f2016-09-14 05:23:22 -070078 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070079 return false;
80}
aluebsdf6416a2016-03-16 18:26:35 -070081
peah2ace3f92016-09-10 04:42:27 -070082bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070083 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
84 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
85}
86
peah2ace3f92016-09-10 04:42:27 -070087int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) {
88#ifdef WEBRTC_ARCH_ARM_FAMILY
kwibergd59d3bb2016-09-13 07:49:33 -070089 constexpr int kMaxSplittingNativeProcessRate =
90 AudioProcessing::kSampleRate32kHz;
peah2ace3f92016-09-10 04:42:27 -070091#else
kwibergd59d3bb2016-09-13 07:49:33 -070092 constexpr int kMaxSplittingNativeProcessRate =
93 AudioProcessing::kSampleRate48kHz;
peah2ace3f92016-09-10 04:42:27 -070094#endif
kwibergd59d3bb2016-09-13 07:49:33 -070095 static_assert(
96 kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz,
97 "");
peah2ace3f92016-09-10 04:42:27 -070098 const int uppermost_native_rate = band_splitting_required
99 ? kMaxSplittingNativeProcessRate
100 : AudioProcessing::kSampleRate48kHz;
101
102 for (auto rate : AudioProcessing::kNativeSampleRatesHz) {
103 if (rate >= uppermost_native_rate) {
104 return uppermost_native_rate;
105 }
106 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700107 return rate;
108 }
109 }
peah2ace3f92016-09-10 04:42:27 -0700110 RTC_NOTREACHED();
111 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700112}
113
Sam Zackrisson23513132019-01-11 15:10:32 +0100114NoiseSuppression::Level NsConfigLevelToInterfaceLevel(
115 AudioProcessing::Config::NoiseSuppression::Level level) {
116 using NsConfig = AudioProcessing::Config::NoiseSuppression;
117 switch (level) {
118 case NsConfig::kLow:
119 return NoiseSuppression::kLow;
120 case NsConfig::kModerate:
121 return NoiseSuppression::kModerate;
122 case NsConfig::kHigh:
123 return NoiseSuppression::kHigh;
124 case NsConfig::kVeryHigh:
125 return NoiseSuppression::kVeryHigh;
126 default:
127 RTC_NOTREACHED();
128 }
129}
130
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100131GainControl::Mode Agc1ConfigModeToInterfaceMode(
132 AudioProcessing::Config::GainController1::Mode mode) {
133 using Agc1Config = AudioProcessing::Config::GainController1;
134 switch (mode) {
135 case Agc1Config::kAdaptiveAnalog:
136 return GainControl::kAdaptiveAnalog;
137 case Agc1Config::kAdaptiveDigital:
138 return GainControl::kAdaptiveDigital;
139 case Agc1Config::kFixedDigital:
140 return GainControl::kFixedDigital;
141 }
142}
143
peah9e6a2902017-05-15 07:19:21 -0700144// Maximum lengths that frame of samples being passed from the render side to
145// the capture side can have (does not apply to AEC3).
146static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
147static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
148
peah764e3642016-10-22 05:04:30 -0700149// Maximum number of frames to buffer in the render queue.
150// TODO(peah): Decrease this once we properly handle hugely unbalanced
151// reverse and forward call numbers.
152static const size_t kMaxNumFramesToBuffer = 100;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700153} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000154
155// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000156static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000157
Sam Zackrisson0beac582017-09-25 12:04:02 +0200158AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100159 bool capture_post_processor_enabled,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200160 bool render_pre_processor_enabled,
161 bool capture_analyzer_enabled)
Alex Loiko5825aa62017-12-18 16:02:40 +0100162 : capture_post_processor_enabled_(capture_post_processor_enabled),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200163 render_pre_processor_enabled_(render_pre_processor_enabled),
164 capture_analyzer_enabled_(capture_analyzer_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700165
166bool AudioProcessingImpl::ApmSubmoduleStates::Update(
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200167 bool high_pass_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700168 bool echo_canceller_enabled,
169 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700170 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700171 bool noise_suppressor_enabled,
peah2ace3f92016-09-10 04:42:27 -0700172 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700173 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200174 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200175 bool echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -0700176 bool voice_activity_detector_enabled,
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100177 bool private_voice_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700178 bool level_estimator_enabled,
179 bool transient_suppressor_enabled) {
180 bool changed = false;
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200181 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700182 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
183 changed |=
184 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700185 changed |=
186 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700187 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
188 changed |=
peah2ace3f92016-09-10 04:42:27 -0700189 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200190 changed |= (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200191 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200192 changed |= (echo_controller_enabled != echo_controller_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700193 changed |= (level_estimator_enabled != level_estimator_enabled_);
194 changed |=
195 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100196 changed |=
197 (private_voice_detector_enabled != private_voice_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700198 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
199 if (changed) {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200200 high_pass_filter_enabled_ = high_pass_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700201 echo_canceller_enabled_ = echo_canceller_enabled;
202 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700203 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700204 noise_suppressor_enabled_ = noise_suppressor_enabled;
peah2ace3f92016-09-10 04:42:27 -0700205 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700206 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200207 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200208 echo_controller_enabled_ = echo_controller_enabled;
peah2ace3f92016-09-10 04:42:27 -0700209 level_estimator_enabled_ = level_estimator_enabled;
210 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100211 private_voice_detector_enabled_ = private_voice_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700212 transient_suppressor_enabled_ = transient_suppressor_enabled;
213 }
214
215 changed |= first_update_;
216 first_update_ = false;
217 return changed;
218}
219
220bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
221 const {
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100222 return CaptureMultiBandProcessingActive() ||
223 voice_activity_detector_enabled_ || private_voice_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700224}
225
226bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
227 const {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200228 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700229 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200230 adaptive_gain_controller_enabled_ || echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700231}
232
peah23ac8b42017-05-23 05:33:56 -0700233bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
234 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200235 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
236 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700237}
238
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200239bool AudioProcessingImpl::ApmSubmoduleStates::CaptureAnalyzerActive() const {
240 return capture_analyzer_enabled_;
241}
242
peah2ace3f92016-09-10 04:42:27 -0700243bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
244 const {
245 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800246 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200247 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700248}
249
Alex Loiko5825aa62017-12-18 16:02:40 +0100250bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive()
251 const {
252 return render_pre_processor_enabled_;
253}
254
peah2ace3f92016-09-10 04:42:27 -0700255bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
256 const {
peah2ace3f92016-09-10 04:42:27 -0700257 return false;
peah2ace3f92016-09-10 04:42:27 -0700258}
259
Sam Zackrissoncb1b5562018-09-28 14:15:09 +0200260bool AudioProcessingImpl::ApmSubmoduleStates::LowCutFilteringRequired() const {
261 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
262 mobile_echo_controller_enabled_ || noise_suppressor_enabled_;
263}
264
solenberg5e465c32015-12-08 13:22:33 -0800265struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800266 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800267 // Accessed externally of APM without any lock acquired.
Sam Zackrisson23513132019-01-11 15:10:32 +0100268 // TODO(bugs.webrtc.org/9947): Move these submodules into private_submodules_
269 // when their pointer-to-submodule API functions are gone.
kwiberg88788ad2016-02-19 07:04:49 -0800270 std::unique_ptr<LevelEstimatorImpl> level_estimator;
271 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
Sam Zackrisson23513132019-01-11 15:10:32 +0100272 std::unique_ptr<NoiseSuppressionProxy> noise_suppression_proxy;
kwiberg88788ad2016-02-19 07:04:49 -0800273 std::unique_ptr<VoiceDetectionImpl> voice_detection;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100274 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800275 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800276 gain_control_for_experimental_agc;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100277 std::unique_ptr<GainControlConfigProxy> gain_control_config_proxy;
solenberg5e465c32015-12-08 13:22:33 -0800278
279 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800280 std::unique_ptr<TransientSuppressor> transient_suppressor;
solenberg5e465c32015-12-08 13:22:33 -0800281};
282
283struct AudioProcessingImpl::ApmPrivateSubmodules {
Sam Zackrissondb389722018-06-21 10:12:24 +0200284 ApmPrivateSubmodules(std::unique_ptr<CustomProcessing> capture_post_processor,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100285 std::unique_ptr<CustomProcessing> render_pre_processor,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200286 rtc::scoped_refptr<EchoDetector> echo_detector,
287 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Sam Zackrissondb389722018-06-21 10:12:24 +0200288 : echo_detector(std::move(echo_detector)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100289 capture_post_processor(std::move(capture_post_processor)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200290 render_pre_processor(std::move(render_pre_processor)),
291 capture_analyzer(std::move(capture_analyzer)) {}
solenberg5e465c32015-12-08 13:22:33 -0800292 // Accessed internally from capture or during initialization
kwiberg88788ad2016-02-19 07:04:49 -0800293 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700294 std::unique_ptr<GainController2> gain_controller2;
peah8271d042016-11-22 07:24:52 -0800295 std::unique_ptr<LowCutFilter> low_cut_filter;
Ivo Creusend1f970d2018-06-14 11:02:03 +0200296 rtc::scoped_refptr<EchoDetector> echo_detector;
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +0100297 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
Sam Zackrissonc22f5512018-11-05 16:10:00 +0100298 std::unique_ptr<EchoControl> echo_controller;
299 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
Alex Loiko5825aa62017-12-18 16:02:40 +0100300 std::unique_ptr<CustomProcessing> capture_post_processor;
301 std::unique_ptr<CustomProcessing> render_pre_processor;
Alex Loikob5c9a792018-04-16 16:31:22 +0200302 std::unique_ptr<GainApplier> pre_amplifier;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200303 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100304 std::unique_ptr<LevelEstimatorImpl> output_level_estimator;
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100305 std::unique_ptr<VoiceDetectionImpl> voice_detector;
solenberg5e465c32015-12-08 13:22:33 -0800306};
307
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100308AudioProcessingBuilder::AudioProcessingBuilder() = default;
309AudioProcessingBuilder::~AudioProcessingBuilder() = default;
310
311AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
312 std::unique_ptr<CustomProcessing> capture_post_processing) {
313 capture_post_processing_ = std::move(capture_post_processing);
314 return *this;
315}
316
317AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
318 std::unique_ptr<CustomProcessing> render_pre_processing) {
319 render_pre_processing_ = std::move(render_pre_processing);
320 return *this;
321}
322
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200323AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
324 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
325 capture_analyzer_ = std::move(capture_analyzer);
326 return *this;
327}
328
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100329AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
330 std::unique_ptr<EchoControlFactory> echo_control_factory) {
331 echo_control_factory_ = std::move(echo_control_factory);
332 return *this;
333}
334
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100335AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200336 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100337 echo_detector_ = std::move(echo_detector);
338 return *this;
339}
340
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100341AudioProcessing* AudioProcessingBuilder::Create() {
342 webrtc::Config config;
343 return Create(config);
344}
345
346AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100347 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
348 config, std::move(capture_post_processing_),
349 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200350 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100351 if (apm->Initialize() != AudioProcessing::kNoError) {
352 delete apm;
353 apm = nullptr;
354 }
355 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100356}
357
peah88ac8532016-09-12 16:47:25 -0700358AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200359 : AudioProcessingImpl(config, nullptr, nullptr, nullptr, nullptr, nullptr) {
360}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000361
Per Åhgren13735822018-02-12 21:42:56 +0100362int AudioProcessingImpl::instance_count_ = 0;
363
Sam Zackrisson0beac582017-09-25 12:04:02 +0200364AudioProcessingImpl::AudioProcessingImpl(
365 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100366 std::unique_ptr<CustomProcessing> capture_post_processor,
367 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200368 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200369 rtc::scoped_refptr<EchoDetector> echo_detector,
370 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100371 : data_dumper_(
372 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200373 capture_runtime_settings_(kRuntimeSettingQueueSize),
374 render_runtime_settings_(kRuntimeSettingQueueSize),
375 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
376 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200377 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200378 submodule_states_(!!capture_post_processor,
379 !!render_pre_processor,
380 !!capture_analyzer),
peah8271d042016-11-22 07:24:52 -0800381 public_submodules_(new ApmPublicSubmodules()),
Sam Zackrisson0beac582017-09-25 12:04:02 +0200382 private_submodules_(
Sam Zackrissondb389722018-06-21 10:12:24 +0200383 new ApmPrivateSubmodules(std::move(capture_post_processor),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100384 std::move(render_pre_processor),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200385 std::move(echo_detector),
386 std::move(capture_analyzer))),
peahdf3efa82015-11-28 12:35:15 -0800387 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800388 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000389#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alex Loikod9342442018-09-10 13:59:41 +0200390 /* enabled= */ false,
391 /* enabled_agc2_level_estimator= */ false,
392 /* digital_adaptive_disabled= */ false,
393 /* analyze_before_aec= */ false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000394#else
Alex Loiko64cb83b2018-07-02 13:38:19 +0200395 config.Get<ExperimentalAgc>().enabled,
396 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
Alex Loikod9342442018-09-10 13:59:41 +0200397 config.Get<ExperimentalAgc>().digital_adaptive_disabled,
398 config.Get<ExperimentalAgc>().analyze_before_aec),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000399#endif
andrew1c7075f2015-06-24 18:14:14 -0700400#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200401 capture_(false),
andrew1c7075f2015-06-24 18:14:14 -0700402#else
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200403 capture_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700404#endif
Alessio Bazzicacc22f512018-08-30 13:01:34 +0200405 capture_nonlocked_() {
Sam Zackrisson421c8592019-02-11 13:39:46 +0100406 // Mark Echo Controller enabled if a factory is injected.
407 capture_nonlocked_.echo_controller_enabled =
408 static_cast<bool>(echo_control_factory_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000409
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100410 public_submodules_->gain_control.reset(new GainControlImpl());
Sam Zackrisson421c8592019-02-11 13:39:46 +0100411 public_submodules_->level_estimator.reset(
412 new LevelEstimatorImpl(&crit_capture_));
413 public_submodules_->noise_suppression.reset(
414 new NoiseSuppressionImpl(&crit_capture_));
415 public_submodules_->noise_suppression_proxy.reset(new NoiseSuppressionProxy(
416 this, public_submodules_->noise_suppression.get()));
417 public_submodules_->voice_detection.reset(
418 new VoiceDetectionImpl(&crit_capture_));
419 public_submodules_->gain_control_for_experimental_agc.reset(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100420 new GainControlForExperimentalAgc(
421 public_submodules_->gain_control.get()));
422 public_submodules_->gain_control_config_proxy.reset(
423 new GainControlConfigProxy(&crit_capture_, this, agc1()));
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200424
Sam Zackrisson421c8592019-02-11 13:39:46 +0100425 // If no echo detector is injected, use the ResidualEchoDetector.
426 if (!private_submodules_->echo_detector) {
427 private_submodules_->echo_detector =
428 new rtc::RefCountedObject<ResidualEchoDetector>();
peahdf3efa82015-11-28 12:35:15 -0800429 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000430
Sam Zackrisson421c8592019-02-11 13:39:46 +0100431 // TODO(alessiob): Move the injected gain controller once injection is
432 // implemented.
433 private_submodules_->gain_controller2.reset(new GainController2());
434
435 RTC_LOG(LS_INFO) << "Capture analyzer activated: "
436 << !!private_submodules_->capture_analyzer
437 << "\nCapture post processor activated: "
438 << !!private_submodules_->capture_post_processor
439 << "\nRender pre processor activated: "
440 << !!private_submodules_->render_pre_processor;
441
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000442 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000443}
444
445AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800446 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800447 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800448 private_submodules_->agc_manager.reset();
449 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800450 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
452
niklase@google.com470e71d2011-07-07 08:21:25 +0000453int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800454 // Run in a single-threaded manner during initialization.
455 rtc::CritScope cs_render(&crit_render_);
456 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000457 return InitializeLocked();
458}
459
peahde65ddc2016-09-16 15:02:15 -0700460int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
461 int capture_output_sample_rate_hz,
462 int render_input_sample_rate_hz,
463 ChannelLayout capture_input_layout,
464 ChannelLayout capture_output_layout,
465 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700466 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700467 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
468 LayoutHasKeyboard(capture_input_layout)},
469 {capture_output_sample_rate_hz,
470 ChannelsFromLayout(capture_output_layout),
471 LayoutHasKeyboard(capture_output_layout)},
472 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
473 LayoutHasKeyboard(render_input_layout)},
474 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
475 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700476
477 return Initialize(processing_config);
478}
479
480int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800481 // Run in a single-threaded manner during initialization.
482 rtc::CritScope cs_render(&crit_render_);
483 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700484 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000485}
486
peahdf3efa82015-11-28 12:35:15 -0800487int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800488 const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800489 // Called from both threads. Thread check is therefore not possible.
Oskar Sundbom4b276482019-05-23 14:28:00 +0200490 if (processing_config == formats_.api_format) {
peah192164e2015-11-17 02:16:45 -0800491 return kNoError;
492 }
peahdf3efa82015-11-28 12:35:15 -0800493
494 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800495 return InitializeLocked(processing_config);
496}
497
niklase@google.com470e71d2011-07-07 08:21:25 +0000498int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200499 UpdateActiveSubmoduleStates();
500
peahde65ddc2016-09-16 15:02:15 -0700501 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800502 formats_.api_format.reverse_output_stream().num_frames() == 0
peahde65ddc2016-09-16 15:02:15 -0700503 ? formats_.render_processing_format.num_frames()
peahdf3efa82015-11-28 12:35:15 -0800504 : formats_.api_format.reverse_output_stream().num_frames();
505 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
506 render_.render_audio.reset(new AudioBuffer(
507 formats_.api_format.reverse_input_stream().num_frames(),
508 formats_.api_format.reverse_input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700509 formats_.render_processing_format.num_frames(),
510 formats_.render_processing_format.num_channels(),
511 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700512 if (formats_.api_format.reverse_input_stream() !=
513 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800514 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800515 formats_.api_format.reverse_input_stream().num_channels(),
516 formats_.api_format.reverse_input_stream().num_frames(),
517 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800518 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700519 } else {
peahdf3efa82015-11-28 12:35:15 -0800520 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700521 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700522 } else {
peahdf3efa82015-11-28 12:35:15 -0800523 render_.render_audio.reset(nullptr);
524 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700525 }
peahce4d9152017-05-19 01:28:05 -0700526
peahdf3efa82015-11-28 12:35:15 -0800527 capture_.capture_audio.reset(
528 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
529 formats_.api_format.input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700530 capture_nonlocked_.capture_processing_format.num_frames(),
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200531 formats_.api_format.output_stream().num_channels(),
peahdf3efa82015-11-28 12:35:15 -0800532 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000533
peah764e3642016-10-22 05:04:30 -0700534 AllocateRenderQueue();
535
peah135259a2016-10-28 03:12:11 -0700536 public_submodules_->gain_control->Initialize(num_proc_channels(),
537 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700538 if (constants_.use_experimental_agc) {
539 if (!private_submodules_->agc_manager.get()) {
540 private_submodules_->agc_manager.reset(new AgcManagerDirect(
541 public_submodules_->gain_control.get(),
542 public_submodules_->gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200543 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
544 constants_.use_experimental_agc_agc2_level_estimation,
545 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700546 }
547 private_submodules_->agc_manager->Initialize();
548 private_submodules_->agc_manager->SetCaptureMuted(
549 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700550 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700551 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200552 InitializeTransient();
peah8271d042016-11-22 07:24:52 -0800553 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700554 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
555 proc_sample_rate_hz());
556 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100557 if (private_submodules_->voice_detector) {
558 private_submodules_->voice_detector->Initialize(
559 proc_split_sample_rate_hz());
560 }
peahde65ddc2016-09-16 15:02:15 -0700561 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700562 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200563 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700564 InitializeGainController2();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200565 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200566 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100567 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800568
aleloi868f32f2017-05-23 07:20:05 -0700569 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200570 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700571 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000572 return kNoError;
573}
574
Michael Graczyk86c6d332015-07-23 11:41:39 -0700575int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200576 UpdateActiveSubmoduleStates();
577
Michael Graczyk86c6d332015-07-23 11:41:39 -0700578 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700579 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
580 return kBadSampleRateError;
581 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000582 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700583
Peter Kasting69558702016-01-12 16:26:35 -0800584 const size_t num_in_channels = config.input_stream().num_channels();
585 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700586
587 // Need at least one input channel.
588 // Need either one output channel or as many outputs as there are inputs.
589 if (num_in_channels == 0 ||
590 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700591 return kBadNumberChannelsError;
592 }
593
peahdf3efa82015-11-28 12:35:15 -0800594 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000595
peahde65ddc2016-09-16 15:02:15 -0700596 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700597 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700598 formats_.api_format.output_stream().sample_rate_hz()),
599 submodule_states_.CaptureMultiBandSubModulesActive() ||
600 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000601
peahde65ddc2016-09-16 15:02:15 -0700602 capture_nonlocked_.capture_processing_format =
603 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700604
peah2ce640f2017-04-07 03:57:48 -0700605 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200606 if (!capture_nonlocked_.echo_controller_enabled) {
peah2ce640f2017-04-07 03:57:48 -0700607 render_processing_rate = FindNativeProcessRateToUse(
608 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
609 formats_.api_format.reverse_output_stream().sample_rate_hz()),
610 submodule_states_.CaptureMultiBandSubModulesActive() ||
611 submodule_states_.RenderMultiBandSubModulesActive());
612 } else {
613 render_processing_rate = capture_processing_rate;
614 }
615
aluebseb3603b2016-04-20 15:27:58 -0700616 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
617 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700618 if (render_processing_rate > kSampleRate32kHz &&
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200619 !capture_nonlocked_.echo_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -0700620 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
621 ? kSampleRate32kHz
622 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700623 }
peah2ce640f2017-04-07 03:57:48 -0700624
peahde65ddc2016-09-16 15:02:15 -0700625 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700626 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700627 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
628 kSampleRate8kHz) {
629 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000630 } else {
peahde65ddc2016-09-16 15:02:15 -0700631 render_processing_rate =
632 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000633 }
634
peahde65ddc2016-09-16 15:02:15 -0700635 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000636 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700637 if (submodule_states_.RenderMultiBandSubModulesActive()) {
638 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
639 } else {
640 formats_.render_processing_format = StreamConfig(
641 formats_.api_format.reverse_input_stream().sample_rate_hz(),
642 formats_.api_format.reverse_input_stream().num_channels());
643 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000644
peahde65ddc2016-09-16 15:02:15 -0700645 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
646 kSampleRate32kHz ||
647 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
648 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800649 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000650 } else {
peahdf3efa82015-11-28 12:35:15 -0800651 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700652 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000653 }
654
655 return InitializeLocked();
656}
657
peah88ac8532016-09-12 16:47:25 -0700658void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peah88ac8532016-09-12 16:47:25 -0700659 // Run in a single-threaded manner when applying the settings.
660 rtc::CritScope cs_render(&crit_render_);
661 rtc::CritScope cs_capture(&crit_capture_);
662
Per Åhgren200feba2019-03-06 04:16:46 +0100663 const bool aec_config_changed =
664 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
665 config_.echo_canceller.use_legacy_aec !=
666 config.echo_canceller.use_legacy_aec ||
667 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode ||
668 (config_.echo_canceller.enabled && config.echo_canceller.use_legacy_aec &&
669 config_.echo_canceller.legacy_moderate_suppression_level !=
670 config.echo_canceller.legacy_moderate_suppression_level);
671
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100672 const bool agc1_config_changed =
673 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
674 config_.gain_controller1.mode != config.gain_controller1.mode ||
675 config_.gain_controller1.target_level_dbfs !=
676 config.gain_controller1.target_level_dbfs ||
677 config_.gain_controller1.compression_gain_db !=
678 config.gain_controller1.compression_gain_db ||
679 config_.gain_controller1.enable_limiter !=
680 config.gain_controller1.enable_limiter ||
681 config_.gain_controller1.analog_level_minimum !=
682 config.gain_controller1.analog_level_minimum ||
683 config_.gain_controller1.analog_level_maximum !=
684 config.gain_controller1.analog_level_maximum;
685
Yves Gerey499bc6c2018-10-10 18:29:07 +0200686 config_ = config;
687
Per Åhgren200feba2019-03-06 04:16:46 +0100688 if (aec_config_changed) {
689 InitializeEchoController();
690 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200691
Sam Zackrisson23513132019-01-11 15:10:32 +0100692 public_submodules_->noise_suppression->Enable(
693 config.noise_suppression.enabled);
694 public_submodules_->noise_suppression->set_level(
695 NsConfigLevelToInterfaceLevel(config.noise_suppression.level));
696
peah8271d042016-11-22 07:24:52 -0800697 InitializeLowCutFilter();
698
Mirko Bonadei675513b2017-11-09 11:09:25 +0100699 RTC_LOG(LS_INFO) << "Highpass filter activated: "
700 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800701
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100702 if (agc1_config_changed) {
703 ApplyAgc1Config(config_.gain_controller1);
704 }
705
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100706 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700707 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100708 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
709 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100710 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100711 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700712 config_.gain_controller2 = AudioProcessing::Config::GainController2();
713 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200714 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200715 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200716 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100717 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
718 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200719 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
720 << config_.pre_amplifier.enabled;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100721
722 if (config_.level_estimation.enabled &&
723 !private_submodules_->output_level_estimator) {
724 private_submodules_->output_level_estimator.reset(
725 new LevelEstimatorImpl(&crit_capture_));
726 private_submodules_->output_level_estimator->Enable(true);
727 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100728
729 if (config_.voice_detection.enabled && !private_submodules_->voice_detector) {
730 private_submodules_->voice_detector.reset(
731 new VoiceDetectionImpl(&crit_capture_));
732 private_submodules_->voice_detector->Enable(true);
733 private_submodules_->voice_detector->set_likelihood(
734 VoiceDetection::kVeryLowLikelihood);
735 private_submodules_->voice_detector->Initialize(
736 proc_split_sample_rate_hz());
737 }
peah88ac8532016-09-12 16:47:25 -0700738}
739
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100740void AudioProcessingImpl::ApplyAgc1Config(
741 const Config::GainController1& config) {
742 GainControl* agc = agc1();
743 int error = agc->Enable(config.enabled);
744 RTC_DCHECK_EQ(kNoError, error);
745 error = agc->set_mode(Agc1ConfigModeToInterfaceMode(config.mode));
746 RTC_DCHECK_EQ(kNoError, error);
747 error = agc->set_target_level_dbfs(config.target_level_dbfs);
748 RTC_DCHECK_EQ(kNoError, error);
749 error = agc->set_compression_gain_db(config.compression_gain_db);
750 RTC_DCHECK_EQ(kNoError, error);
751 error = agc->enable_limiter(config.enable_limiter);
752 RTC_DCHECK_EQ(kNoError, error);
753 error = agc->set_analog_level_limits(config.analog_level_minimum,
754 config.analog_level_maximum);
755 RTC_DCHECK_EQ(kNoError, error);
756}
757
758GainControl* AudioProcessingImpl::agc1() {
759 if (constants_.use_experimental_agc) {
760 return public_submodules_->gain_control_for_experimental_agc.get();
761 }
762 return public_submodules_->gain_control.get();
763}
764
765const GainControl* AudioProcessingImpl::agc1() const {
766 if (constants_.use_experimental_agc) {
767 return public_submodules_->gain_control_for_experimental_agc.get();
768 }
769 return public_submodules_->gain_control.get();
770}
771
peah88ac8532016-09-12 16:47:25 -0700772void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800773 // Run in a single-threaded manner when setting the extra options.
774 rtc::CritScope cs_render(&crit_render_);
775 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000776
Per Åhgrenf204faf2019-04-25 15:18:06 +0200777 capture_nonlocked_.use_aec2_extended_filter =
778 config.Get<ExtendedFilter>().enabled;
779 capture_nonlocked_.use_aec2_delay_agnostic =
780 config.Get<DelayAgnostic>().enabled;
781 capture_nonlocked_.use_aec2_refined_adaptive_filter =
782 config.Get<RefinedAdaptiveFilter>().enabled;
peahb624d8c2016-03-05 03:01:14 -0800783
peahdf3efa82015-11-28 12:35:15 -0800784 if (capture_.transient_suppressor_enabled !=
785 config.Get<ExperimentalNs>().enabled) {
786 capture_.transient_suppressor_enabled =
787 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000788 InitializeTransient();
789 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000790}
791
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000792int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800793 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700794 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000795}
796
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000797int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800798 // Used as callback from submodules, hence locking is not allowed.
799 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000800}
801
Peter Kasting69558702016-01-12 16:26:35 -0800802size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800803 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700804 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000805}
806
Peter Kasting69558702016-01-12 16:26:35 -0800807size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800808 // Used as callback from submodules, hence locking is not allowed.
809 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000810}
811
Peter Kasting69558702016-01-12 16:26:35 -0800812size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800813 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200814 return capture_nonlocked_.echo_controller_enabled ? 1 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800815}
816
Peter Kasting69558702016-01-12 16:26:35 -0800817size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800818 // Used as callback from submodules, hence locking is not allowed.
819 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000820}
821
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000822void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800823 rtc::CritScope cs(&crit_capture_);
824 capture_.output_will_be_muted = muted;
825 if (private_submodules_->agc_manager.get()) {
826 private_submodules_->agc_manager->SetCaptureMuted(
827 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000828 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000829}
830
Alessio Bazzicac054e782018-04-16 12:10:09 +0200831void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200832 switch (setting.type()) {
833 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
834 render_runtime_settings_enqueuer_.Enqueue(setting);
835 return;
836 case RuntimeSetting::Type::kNotSpecified:
837 RTC_NOTREACHED();
838 return;
839 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100840 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200841 case RuntimeSetting::Type::kCaptureFixedPostGain:
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200842 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200843 capture_runtime_settings_enqueuer_.Enqueue(setting);
844 return;
845 }
846 // The language allows the enum to have a non-enumerator
847 // value. Check that this doesn't happen.
848 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200849}
850
851AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
852 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200853 : runtime_settings_(*runtime_settings) {
854 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200855}
856
857AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
858 default;
859
860void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
861 RuntimeSetting setting) {
862 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200863 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200864 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200865 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200866 RTC_LOG(LS_ERROR)
867 << "The runtime settings queue is full. Oldest setting discarded.";
868 }
869 if (remaining_attempts == 0)
870 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
871}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000872
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000873int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700874 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000875 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000876 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000877 int output_sample_rate_hz,
878 ChannelLayout output_layout,
879 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800880 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800881 StreamConfig input_stream;
882 StreamConfig output_stream;
883 {
884 // Access the formats_.api_format.input_stream beneath the capture lock.
885 // The lock must be released as it is later required in the call
886 // to ProcessStream(,,,);
887 rtc::CritScope cs(&crit_capture_);
888 input_stream = formats_.api_format.input_stream();
889 output_stream = formats_.api_format.output_stream();
890 }
891
Michael Graczyk86c6d332015-07-23 11:41:39 -0700892 input_stream.set_sample_rate_hz(input_sample_rate_hz);
893 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
894 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700895 output_stream.set_sample_rate_hz(output_sample_rate_hz);
896 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
897 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
898
899 if (samples_per_channel != input_stream.num_frames()) {
900 return kBadDataLengthError;
901 }
902 return ProcessStream(src, input_stream, output_stream, dest);
903}
904
905int AudioProcessingImpl::ProcessStream(const float* const* src,
906 const StreamConfig& input_config,
907 const StreamConfig& output_config,
908 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800909 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800910 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700911 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800912 {
913 // Acquire the capture lock in order to safely call the function
914 // that retrieves the render side data. This function accesses apm
915 // getters that need the capture lock held when being called.
916 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700917 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800918
919 if (!src || !dest) {
920 return kNullPointerError;
921 }
922
923 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700924 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000925 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000926
Oskar Sundbom4b276482019-05-23 14:28:00 +0200927 if (processing_config.input_stream() != input_config) {
928 processing_config.input_stream() = input_config;
929 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800930 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200931
932 if (processing_config.output_stream() != output_config) {
933 processing_config.output_stream() = output_config;
934 reinitialization_required = true;
935 }
936
937 if (reinitialization_required) {
938 // Reinitialize.
939 rtc::CritScope cs_render(&crit_render_);
940 rtc::CritScope cs_capture(&crit_capture_);
941 RETURN_ON_ERR(InitializeLocked(processing_config));
942 }
943
peahdf3efa82015-11-28 12:35:15 -0800944 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700945 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
946 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000947
aleloi868f32f2017-05-23 07:20:05 -0700948 if (aec_dump_) {
949 RecordUnprocessedCaptureStream(src);
950 }
951
peahdf3efa82015-11-28 12:35:15 -0800952 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700953 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800954 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000955
aleloi868f32f2017-05-23 07:20:05 -0700956 if (aec_dump_) {
957 RecordProcessedCaptureStream(dest);
958 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000959 return kNoError;
960}
961
Alex Loiko73ec0192018-05-15 10:52:28 +0200962void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200963 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200964 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200965 if (aec_dump_) {
966 aec_dump_->WriteRuntimeSetting(setting);
967 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200968 switch (setting.type()) {
969 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200970 if (config_.pre_amplifier.enabled) {
971 float value;
972 setting.GetFloat(&value);
973 private_submodules_->pre_amplifier->SetGainFactor(value);
974 }
975 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200976 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100977 case RuntimeSetting::Type::kCaptureCompressionGain: {
978 float value;
979 setting.GetFloat(&value);
980 int int_value = static_cast<int>(value + .5f);
981 config_.gain_controller1.compression_gain_db = int_value;
982 int error = agc1()->set_compression_gain_db(int_value);
983 RTC_DCHECK_EQ(kNoError, error);
984 break;
985 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200986 case RuntimeSetting::Type::kCaptureFixedPostGain: {
987 if (config_.gain_controller2.enabled) {
988 float value;
989 setting.GetFloat(&value);
990 config_.gain_controller2.fixed_digital.gain_db = value;
991 private_submodules_->gain_controller2->ApplyConfig(
992 config_.gain_controller2);
993 }
994 break;
995 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200996 case RuntimeSetting::Type::kPlayoutVolumeChange: {
997 int value;
998 setting.GetInt(&value);
999 capture_.playout_volume = value;
1000 break;
1001 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001002 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1003 RTC_NOTREACHED();
1004 break;
1005 case RuntimeSetting::Type::kNotSpecified:
1006 RTC_NOTREACHED();
1007 break;
1008 }
1009 }
1010}
1011
1012void AudioProcessingImpl::HandleRenderRuntimeSettings() {
1013 RuntimeSetting setting;
1014 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +02001015 if (aec_dump_) {
1016 aec_dump_->WriteRuntimeSetting(setting);
1017 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001018 switch (setting.type()) {
1019 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1020 if (private_submodules_->render_pre_processor) {
1021 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
1022 }
1023 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001024 case RuntimeSetting::Type::kCapturePreGain: // fall-through
1025 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001026 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001027 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001028 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001029 RTC_NOTREACHED();
1030 break;
1031 }
1032 }
1033}
1034
peah9e6a2902017-05-15 07:19:21 -07001035void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001036 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001037
1038 // Insert the samples into the queue.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001039 if (private_submodules_->echo_cancellation) {
1040 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001041 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1042 num_reverse_channels(),
1043 &aec_render_queue_buffer_);
1044
Per Åhgrenf204faf2019-04-25 15:18:06 +02001045 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1046 // The data queue is full and needs to be emptied.
1047 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001048
Per Åhgrenf204faf2019-04-25 15:18:06 +02001049 // Retry the insert (should always work).
1050 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1051 RTC_DCHECK(result);
1052 }
peaha0624602016-10-25 04:45:24 -07001053 }
1054
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001055 if (private_submodules_->echo_control_mobile) {
1056 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1057 num_reverse_channels(),
1058 &aecm_render_queue_buffer_);
1059 RTC_DCHECK(aecm_render_signal_queue_);
1060 // Insert the samples into the queue.
1061 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1062 // The data queue is full and needs to be emptied.
1063 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001064
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001065 // Retry the insert (should always work).
1066 bool result =
1067 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1068 RTC_DCHECK(result);
1069 }
peah764e3642016-10-22 05:04:30 -07001070 }
peah701d6282016-10-25 05:42:20 -07001071
1072 if (!constants_.use_experimental_agc) {
1073 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1074 // Insert the samples into the queue.
1075 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1076 // The data queue is full and needs to be emptied.
1077 EmptyQueuedRenderAudio();
1078
1079 // Retry the insert (should always work).
1080 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1081 RTC_DCHECK(result);
1082 }
1083 }
peah9e6a2902017-05-15 07:19:21 -07001084}
ivoc9f4a4a02016-10-28 05:39:16 -07001085
peah9e6a2902017-05-15 07:19:21 -07001086void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001087 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1088
1089 // Insert the samples into the queue.
1090 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1091 // The data queue is full and needs to be emptied.
1092 EmptyQueuedRenderAudio();
1093
1094 // Retry the insert (should always work).
1095 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1096 RTC_DCHECK(result);
1097 }
peah764e3642016-10-22 05:04:30 -07001098}
1099
1100void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001101 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001102 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001103
ivoc9f4a4a02016-10-28 05:39:16 -07001104 const size_t new_red_render_queue_element_max_size =
1105 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1106
peaha0624602016-10-25 04:45:24 -07001107 // Reallocate the queues if the queue item sizes are too small to fit the
1108 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001109
1110 if (agc_render_queue_element_max_size_ <
1111 new_agc_render_queue_element_max_size) {
1112 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1113
1114 std::vector<int16_t> template_queue_element(
1115 agc_render_queue_element_max_size_);
1116
1117 agc_render_signal_queue_.reset(
1118 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1119 kMaxNumFramesToBuffer, template_queue_element,
1120 RenderQueueItemVerifier<int16_t>(
1121 agc_render_queue_element_max_size_)));
1122
1123 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1124 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1125 } else {
1126 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001127 }
ivoc9f4a4a02016-10-28 05:39:16 -07001128
1129 if (red_render_queue_element_max_size_ <
1130 new_red_render_queue_element_max_size) {
1131 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1132
1133 std::vector<float> template_queue_element(
1134 red_render_queue_element_max_size_);
1135
1136 red_render_signal_queue_.reset(
1137 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1138 kMaxNumFramesToBuffer, template_queue_element,
1139 RenderQueueItemVerifier<float>(
1140 red_render_queue_element_max_size_)));
1141
1142 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1143 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1144 } else {
1145 red_render_signal_queue_->Clear();
1146 }
peah764e3642016-10-22 05:04:30 -07001147}
1148
1149void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1150 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001151 if (private_submodules_->echo_cancellation) {
1152 RTC_DCHECK(aec_render_signal_queue_);
1153 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
1154 private_submodules_->echo_cancellation->ProcessRenderAudio(
1155 aec_capture_queue_buffer_);
1156 }
peaha0624602016-10-25 04:45:24 -07001157 }
1158
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001159 if (private_submodules_->echo_control_mobile) {
1160 RTC_DCHECK(aecm_render_signal_queue_);
1161 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
1162 private_submodules_->echo_control_mobile->ProcessRenderAudio(
1163 aecm_capture_queue_buffer_);
1164 }
peah701d6282016-10-25 05:42:20 -07001165 }
1166
1167 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1168 public_submodules_->gain_control->ProcessRenderAudio(
1169 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001170 }
ivoc9f4a4a02016-10-28 05:39:16 -07001171
1172 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001173 RTC_DCHECK(private_submodules_->echo_detector);
1174 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001175 red_capture_queue_buffer_);
1176 }
peah764e3642016-10-22 05:04:30 -07001177}
1178
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001179int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001180 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001181 {
1182 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001183 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001184 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001185 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001186 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001187 }
peahfa6228e2015-11-16 16:27:42 -08001188
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001189 if (!frame) {
1190 return kNullPointerError;
1191 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001192 // Must be a native rate.
1193 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1194 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001195 frame->sample_rate_hz_ != kSampleRate32kHz &&
1196 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001197 return kBadSampleRateError;
1198 }
peah192164e2015-11-17 02:16:45 -08001199
peahdf3efa82015-11-28 12:35:15 -08001200 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001201 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001202 {
1203 // Aquire lock for the access of api_format.
1204 // The lock is released immediately due to the conditional
1205 // reinitialization.
1206 rtc::CritScope cs_capture(&crit_capture_);
1207 // TODO(ajm): The input and output rates and channels are currently
1208 // constrained to be identical in the int16 interface.
1209 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001210
1211 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001212 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001213
Oskar Sundbom4b276482019-05-23 14:28:00 +02001214 reinitialization_required =
1215 reinitialization_required ||
1216 processing_config.input_stream().sample_rate_hz() !=
1217 frame->sample_rate_hz_ ||
1218 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1219 processing_config.output_stream().sample_rate_hz() !=
1220 frame->sample_rate_hz_ ||
1221 processing_config.output_stream().num_channels() != frame->num_channels_;
1222
1223 if (reinitialization_required) {
1224 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1225 processing_config.input_stream().set_num_channels(frame->num_channels_);
1226 processing_config.output_stream().set_sample_rate_hz(
1227 frame->sample_rate_hz_);
1228 processing_config.output_stream().set_num_channels(frame->num_channels_);
1229
1230 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001231 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001232 rtc::CritScope cs_capture(&crit_capture_);
1233 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001234 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001235
peahdf3efa82015-11-28 12:35:15 -08001236 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001237 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001238 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001239 return kBadDataLengthError;
1240 }
1241
aleloi868f32f2017-05-23 07:20:05 -07001242 if (aec_dump_) {
1243 RecordUnprocessedCaptureStream(*frame);
1244 }
1245
peahdf3efa82015-11-28 12:35:15 -08001246 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001247 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001248 capture_.capture_audio->InterleaveTo(
peah23ac8b42017-05-23 05:33:56 -07001249 frame, submodule_states_.CaptureMultiBandProcessingActive() ||
1250 submodule_states_.CaptureFullBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001251
aleloi868f32f2017-05-23 07:20:05 -07001252 if (aec_dump_) {
1253 RecordProcessedCaptureStream(*frame);
1254 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001255
1256 return kNoError;
1257}
1258
peahde65ddc2016-09-16 15:02:15 -07001259int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001260 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001261
peahb58a1582016-03-15 09:34:24 -07001262 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001263 // TODO(peah): Simplify once the public API Enable functions for these
1264 // are moved to APM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001265 RTC_DCHECK_LE(!!private_submodules_->echo_controller +
1266 !!private_submodules_->echo_cancellation +
1267 !!private_submodules_->echo_control_mobile,
1268 1);
peahb58a1582016-03-15 09:34:24 -07001269
peahde65ddc2016-09-16 15:02:15 -07001270 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001271
Alex Loikob5c9a792018-04-16 16:31:22 +02001272 if (private_submodules_->pre_amplifier) {
1273 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
1274 capture_buffer->channels_f(), capture_buffer->num_channels(),
1275 capture_buffer->num_frames()));
1276 }
1277
peah1b08dc32016-12-20 13:45:58 -08001278 capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>(
henrik.lundin290d43a2016-11-29 08:09:09 -08001279 capture_buffer->channels_const()[0],
1280 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001281 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1282 if (log_rms) {
1283 capture_rms_interval_counter_ = 0;
1284 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001285 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1286 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1287 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1288 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001289 }
1290
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001291 if (private_submodules_->echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001292 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001293 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001294 capture_.echo_path_gain_change =
1295 capture_.prev_analog_mic_level != analog_mic_level &&
1296 capture_.prev_analog_mic_level != -1;
1297 capture_.prev_analog_mic_level = analog_mic_level;
1298
Per Åhgrend2650d12018-10-02 17:00:59 +02001299 // Detect and flag any change in the pre-amplifier gain.
1300 if (private_submodules_->pre_amplifier) {
1301 float pre_amp_gain = private_submodules_->pre_amplifier->GetGainFactor();
1302 capture_.echo_path_gain_change =
1303 capture_.echo_path_gain_change ||
1304 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001305 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001306 capture_.prev_pre_amp_gain = pre_amp_gain;
1307 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001308
1309 // Detect volume change.
1310 capture_.echo_path_gain_change =
1311 capture_.echo_path_gain_change ||
1312 (capture_.prev_playout_volume != capture_.playout_volume &&
1313 capture_.prev_playout_volume >= 0);
1314 capture_.prev_playout_volume = capture_.playout_volume;
1315
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001316 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001317 }
1318
peahbe615622016-02-13 16:40:47 -08001319 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001320 public_submodules_->gain_control->is_enabled()) {
1321 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001322 capture_buffer->channels()[0], capture_buffer->num_channels(),
1323 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001324
1325 if (constants_.use_experimental_agc_process_before_aec) {
1326 private_submodules_->agc_manager->Process(
1327 capture_buffer->channels()[0],
1328 capture_nonlocked_.capture_processing_format.num_frames(),
1329 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1330 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001331 }
1332
peah2ace3f92016-09-10 04:42:27 -07001333 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1334 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001335 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1336 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001337 }
1338
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001339 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001340 // Force down-mixing of the number of channels after the detection of
1341 // capture signal saturation.
1342 // TODO(peah): Look into ensuring that this kind of tampering with the
1343 // AudioBuffer functionality should not be needed.
1344 capture_buffer->set_num_channels(1);
1345 }
1346
peahe0eae3c2016-12-14 01:16:23 -08001347 // TODO(peah): Move the AEC3 low-cut filter to this place.
1348 if (private_submodules_->low_cut_filter &&
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001349 !private_submodules_->echo_controller) {
peah8271d042016-11-22 07:24:52 -08001350 private_submodules_->low_cut_filter->Process(capture_buffer);
1351 }
peahde65ddc2016-09-16 15:02:15 -07001352 RETURN_ON_ERR(
1353 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1354 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001355
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001356 if (private_submodules_->echo_control_mobile) {
1357 // Ensure that the stream delay was set before the call to the
1358 // AECM ProcessCaptureAudio function.
1359 if (!was_stream_delay_set()) {
1360 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001361 }
1362
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001363 if (public_submodules_->noise_suppression->is_enabled()) {
1364 capture_buffer->CopyLowPassToReference();
1365 }
peahe0eae3c2016-12-14 01:16:23 -08001366
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001367 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah253534d2016-03-15 04:32:28 -07001368
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001369 RETURN_ON_ERR(private_submodules_->echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001370 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001371 } else {
1372 if (private_submodules_->echo_controller) {
1373 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1374
1375 if (was_stream_delay_set()) {
1376 private_submodules_->echo_controller->SetAudioBufferDelay(
1377 stream_delay_ms());
1378 }
1379
1380 private_submodules_->echo_controller->ProcessCapture(
1381 capture_buffer, capture_.echo_path_gain_change);
1382 } else if (private_submodules_->echo_cancellation) {
1383 // Ensure that the stream delay was set before the call to the
1384 // AEC ProcessCaptureAudio function.
1385 if (!was_stream_delay_set()) {
1386 return AudioProcessing::kStreamParameterNotSetError;
1387 }
1388
1389 RETURN_ON_ERR(private_submodules_->echo_cancellation->ProcessCaptureAudio(
1390 capture_buffer, stream_delay_ms()));
1391 }
1392
1393 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
Per Åhgren46537a32017-06-07 10:08:10 +02001394 }
ivoc9f4a4a02016-10-28 05:39:16 -07001395
peahde65ddc2016-09-16 15:02:15 -07001396 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001397 if (config_.voice_detection.enabled) {
1398 private_submodules_->voice_detector->ProcessCaptureAudio(capture_buffer);
1399 capture_.stats.voice_detected =
1400 private_submodules_->voice_detector->stream_has_voice();
1401 } else {
1402 capture_.stats.voice_detected = absl::nullopt;
1403 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001404
peahbe615622016-02-13 16:40:47 -08001405 if (constants_.use_experimental_agc &&
Alex Loikod9342442018-09-10 13:59:41 +02001406 public_submodules_->gain_control->is_enabled() &&
1407 !constants_.use_experimental_agc_process_before_aec) {
peahdf3efa82015-11-28 12:35:15 -08001408 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001409 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1410 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001411 }
Per Åhgren200feba2019-03-06 04:16:46 +01001412 // TODO(peah): Add reporting from AEC3 whether there is echo.
peahb8fbb542016-03-15 02:28:08 -07001413 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001414 capture_buffer,
Per Åhgrenf204faf2019-04-25 15:18:06 +02001415 private_submodules_->echo_cancellation &&
1416 private_submodules_->echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001417
peah2ace3f92016-09-10 04:42:27 -07001418 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1419 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001420 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1421 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001422 }
1423
peah9e6a2902017-05-15 07:19:21 -07001424 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001425 RTC_DCHECK(private_submodules_->echo_detector);
1426 private_submodules_->echo_detector->AnalyzeCaptureAudio(
peah9e6a2902017-05-15 07:19:21 -07001427 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
1428 capture_buffer->num_frames()));
1429 }
1430
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001431 // TODO(aluebs): Investigate if the transient suppression placement should be
1432 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001433 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001434 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001435 private_submodules_->agc_manager.get()
1436 ? private_submodules_->agc_manager->voice_probability()
1437 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001438
peahdf3efa82015-11-28 12:35:15 -08001439 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001440 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1441 capture_buffer->num_channels(),
1442 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1443 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1444 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001445 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001446 }
1447
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001448 // Experimental APM sub-module that analyzes |capture_buffer|.
1449 if (private_submodules_->capture_analyzer) {
1450 private_submodules_->capture_analyzer->Analyze(capture_buffer);
1451 }
1452
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001453 if (config_.gain_controller2.enabled) {
Alex Loikoa837dd72018-08-06 16:32:12 +02001454 private_submodules_->gain_controller2->NotifyAnalogLevel(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001455 agc1()->stream_analog_level());
alessiob3ec96df2017-05-22 06:57:06 -07001456 private_submodules_->gain_controller2->Process(capture_buffer);
1457 }
1458
Sam Zackrisson0beac582017-09-25 12:04:02 +02001459 if (private_submodules_->capture_post_processor) {
1460 private_submodules_->capture_post_processor->Process(capture_buffer);
1461 }
1462
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001463 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001464 public_submodules_->level_estimator->ProcessStream(capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001465 if (config_.level_estimation.enabled) {
1466 private_submodules_->output_level_estimator->ProcessStream(capture_buffer);
1467 capture_.stats.output_rms_dbfs =
1468 private_submodules_->output_level_estimator->RMS();
1469 } else {
1470 capture_.stats.output_rms_dbfs = absl::nullopt;
1471 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001472
peah1b08dc32016-12-20 13:45:58 -08001473 capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>(
1474 capture_buffer->channels_const()[0],
1475 capture_nonlocked_.capture_processing_format.num_frames()));
1476 if (log_rms) {
1477 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1478 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1479 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1480 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1481 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1482 }
1483
peahdf3efa82015-11-28 12:35:15 -08001484 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001485 return kNoError;
1486}
1487
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001488int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001489 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001490 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001491 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001492 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001493 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001494 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001495 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001496 };
1497 if (samples_per_channel != reverse_config.num_frames()) {
1498 return kBadDataLengthError;
1499 }
peahdf3efa82015-11-28 12:35:15 -08001500 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001501}
1502
peahde65ddc2016-09-16 15:02:15 -07001503int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1504 const StreamConfig& input_config,
1505 const StreamConfig& output_config,
1506 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001507 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001508 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001509 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001510 if (submodule_states_.RenderMultiBandProcessingActive() ||
1511 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001512 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1513 dest);
peah2ace3f92016-09-10 04:42:27 -07001514 } else if (formats_.api_format.reverse_input_stream() !=
1515 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001516 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1517 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001518 } else {
peahde65ddc2016-09-16 15:02:15 -07001519 CopyAudioIfNeeded(src, input_config.num_frames(),
1520 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001521 }
1522
1523 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001524}
1525
peahdf3efa82015-11-28 12:35:15 -08001526int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001527 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001528 const StreamConfig& input_config,
1529 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001530 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001531 return kNullPointerError;
1532 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001533
peahde65ddc2016-09-16 15:02:15 -07001534 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001535 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001536 }
1537
peahdf3efa82015-11-28 12:35:15 -08001538 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001539 processing_config.reverse_input_stream() = input_config;
1540 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001541
peahdf3efa82015-11-28 12:35:15 -08001542 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001543 RTC_DCHECK_EQ(input_config.num_frames(),
1544 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001545
aleloi868f32f2017-05-23 07:20:05 -07001546 if (aec_dump_) {
1547 const size_t channel_size =
1548 formats_.api_format.reverse_input_stream().num_frames();
1549 const size_t num_channels =
1550 formats_.api_format.reverse_input_stream().num_channels();
1551 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001552 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001553 }
peahdf3efa82015-11-28 12:35:15 -08001554 render_.render_audio->CopyFrom(src,
1555 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001556 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001557}
1558
1559int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001560 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001561 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001562 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001563 return kNullPointerError;
1564 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001565 // Must be a native rate.
1566 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1567 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001568 frame->sample_rate_hz_ != kSampleRate32kHz &&
1569 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001570 return kBadSampleRateError;
1571 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001572
Michael Graczyk86c6d332015-07-23 11:41:39 -07001573 if (frame->num_channels_ <= 0) {
1574 return kBadNumberChannelsError;
1575 }
1576
peahdf3efa82015-11-28 12:35:15 -08001577 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001578 processing_config.reverse_input_stream().set_sample_rate_hz(
1579 frame->sample_rate_hz_);
1580 processing_config.reverse_input_stream().set_num_channels(
1581 frame->num_channels_);
1582 processing_config.reverse_output_stream().set_sample_rate_hz(
1583 frame->sample_rate_hz_);
1584 processing_config.reverse_output_stream().set_num_channels(
1585 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001586
peahdf3efa82015-11-28 12:35:15 -08001587 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001588 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001589 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001590 return kBadDataLengthError;
1591 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001592
aleloi868f32f2017-05-23 07:20:05 -07001593 if (aec_dump_) {
1594 aec_dump_->WriteRenderStreamMessage(*frame);
1595 }
1596
peahdf3efa82015-11-28 12:35:15 -08001597 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001598 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001599 render_.render_audio->InterleaveTo(
Alex Loiko5825aa62017-12-18 16:02:40 +01001600 frame, submodule_states_.RenderMultiBandProcessingActive() ||
1601 submodule_states_.RenderFullBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001602 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001603}
niklase@google.com470e71d2011-07-07 08:21:25 +00001604
peahde65ddc2016-09-16 15:02:15 -07001605int AudioProcessingImpl::ProcessRenderStreamLocked() {
1606 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001607
Alex Loiko73ec0192018-05-15 10:52:28 +02001608 HandleRenderRuntimeSettings();
1609
Alex Loiko5825aa62017-12-18 16:02:40 +01001610 if (private_submodules_->render_pre_processor) {
1611 private_submodules_->render_pre_processor->Process(render_buffer);
1612 }
1613
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001614 QueueNonbandedRenderAudio(render_buffer);
1615
peah2ace3f92016-09-10 04:42:27 -07001616 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001617 SampleRateSupportsMultiBand(
1618 formats_.render_processing_format.sample_rate_hz())) {
1619 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001620 }
1621
peahce4d9152017-05-19 01:28:05 -07001622 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1623 QueueBandedRenderAudio(render_buffer);
1624 }
1625
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001626 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001627 if (private_submodules_->echo_controller) {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001628 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001629 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001630
peah2ace3f92016-09-10 04:42:27 -07001631 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001632 SampleRateSupportsMultiBand(
1633 formats_.render_processing_format.sample_rate_hz())) {
1634 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001635 }
1636
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001637 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001638}
1639
1640int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001641 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001642 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001643 capture_.was_stream_delay_set = true;
1644 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001645
niklase@google.com470e71d2011-07-07 08:21:25 +00001646 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001647 delay = 0;
1648 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001649 }
1650
1651 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1652 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001653 delay = 500;
1654 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001655 }
1656
peahdf3efa82015-11-28 12:35:15 -08001657 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001658 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001659}
1660
1661int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001662 // Used as callback from submodules, hence locking is not allowed.
1663 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001664}
1665
1666bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001667 // Used as callback from submodules, hence locking is not allowed.
1668 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001669}
1670
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001671void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001672 rtc::CritScope cs(&crit_capture_);
1673 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001674}
1675
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001676void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001677 rtc::CritScope cs(&crit_capture_);
1678 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001679}
1680
1681int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001682 rtc::CritScope cs(&crit_capture_);
1683 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001684}
1685
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001686void AudioProcessingImpl::set_stream_analog_level(int level) {
1687 rtc::CritScope cs_capture(&crit_capture_);
1688 int error = agc1()->set_stream_analog_level(level);
1689 RTC_DCHECK_EQ(kNoError, error);
1690}
1691
1692int AudioProcessingImpl::recommended_stream_analog_level() const {
1693 rtc::CritScope cs_capture(&crit_capture_);
1694 return agc1()->stream_analog_level();
1695}
1696
aleloi868f32f2017-05-23 07:20:05 -07001697void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1698 RTC_DCHECK(aec_dump);
1699 rtc::CritScope cs_render(&crit_render_);
1700 rtc::CritScope cs_capture(&crit_capture_);
1701
1702 // The previously attached AecDump will be destroyed with the
1703 // 'aec_dump' parameter, which is after locks are released.
1704 aec_dump_.swap(aec_dump);
1705 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001706 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001707}
1708
1709void AudioProcessingImpl::DetachAecDump() {
1710 // The d-tor of a task-queue based AecDump blocks until all pending
1711 // tasks are done. This construction avoids blocking while holding
1712 // the render and capture locks.
1713 std::unique_ptr<AecDump> aec_dump = nullptr;
1714 {
1715 rtc::CritScope cs_render(&crit_render_);
1716 rtc::CritScope cs_capture(&crit_capture_);
1717 aec_dump = std::move(aec_dump_);
1718 }
1719}
1720
Sam Zackrisson4d364492018-03-02 16:03:21 +01001721void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1722 std::unique_ptr<AudioGenerator> audio_generator) {
1723 // TODO(bugs.webrtc.org/8882) Stub.
1724 // Reset internal audio generator with audio_generator.
1725}
1726
1727void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1728 // TODO(bugs.webrtc.org/8882) Stub.
1729 // Delete audio generator, if one is attached.
1730}
1731
Ivo Creusen56d46092017-11-24 17:29:59 +01001732AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001733 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001734 rtc::CritScope cs_capture(&crit_capture_);
1735 if (!has_remote_tracks) {
1736 return capture_.stats;
1737 }
1738 AudioProcessingStats stats = capture_.stats;
1739 EchoCancellationImpl::Metrics metrics;
1740 if (private_submodules_->echo_controller) {
1741 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1742 stats.echo_return_loss = ec_metrics.echo_return_loss;
1743 stats.echo_return_loss_enhancement =
1744 ec_metrics.echo_return_loss_enhancement;
1745 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001746 }
1747 if (config_.residual_echo_detector.enabled) {
1748 RTC_DCHECK(private_submodules_->echo_detector);
1749 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1750 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1751 stats.residual_echo_likelihood_recent_max =
1752 ed_metrics.echo_likelihood_recent_max;
1753 }
Ivo Creusenae026092017-11-20 13:07:16 +01001754 return stats;
1755}
1756
niklase@google.com470e71d2011-07-07 08:21:25 +00001757GainControl* AudioProcessingImpl::gain_control() const {
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001758 return public_submodules_->gain_control_config_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001759}
1760
niklase@google.com470e71d2011-07-07 08:21:25 +00001761LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001762 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001763}
1764
1765NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
Sam Zackrisson23513132019-01-11 15:10:32 +01001766 return public_submodules_->noise_suppression_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001767}
1768
1769VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001770 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001771}
1772
peah8271d042016-11-22 07:24:52 -08001773void AudioProcessingImpl::MutateConfig(
1774 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1775 rtc::CritScope cs_render(&crit_render_);
1776 rtc::CritScope cs_capture(&crit_capture_);
1777 mutator(&config_);
1778 ApplyConfig(config_);
1779}
1780
1781AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1782 rtc::CritScope cs_render(&crit_render_);
1783 rtc::CritScope cs_capture(&crit_capture_);
1784 return config_;
1785}
1786
peah2ace3f92016-09-10 04:42:27 -07001787bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1788 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001789 config_.high_pass_filter.enabled,
Per Åhgrend547d862019-05-03 15:48:47 +02001790 !!private_submodules_->echo_cancellation,
1791 !!private_submodules_->echo_control_mobile,
ivoc9f4a4a02016-10-28 05:39:16 -07001792 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001793 public_submodules_->noise_suppression->is_enabled(),
peah2ace3f92016-09-10 04:42:27 -07001794 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001795 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001796 capture_nonlocked_.echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -07001797 public_submodules_->voice_detection->is_enabled(),
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001798 config_.voice_detection.enabled,
peah2ace3f92016-09-10 04:42:27 -07001799 public_submodules_->level_estimator->is_enabled(),
1800 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001801}
1802
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001803void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001804 if (capture_.transient_suppressor_enabled) {
1805 if (!public_submodules_->transient_suppressor.get()) {
1806 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001807 }
peahdf3efa82015-11-28 12:35:15 -08001808 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001809 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1810 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001811 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001812}
1813
peah8271d042016-11-22 07:24:52 -08001814void AudioProcessingImpl::InitializeLowCutFilter() {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001815 if (submodule_states_.LowCutFilteringRequired()) {
peah8271d042016-11-22 07:24:52 -08001816 private_submodules_->low_cut_filter.reset(
1817 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1818 } else {
1819 private_submodules_->low_cut_filter.reset();
1820 }
1821}
alessiob3ec96df2017-05-22 06:57:06 -07001822
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001823void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001824 bool use_echo_controller =
1825 echo_control_factory_ ||
Per Åhgren200feba2019-03-06 04:16:46 +01001826 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode &&
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001827 !config_.echo_canceller.use_legacy_aec);
1828
1829 if (use_echo_controller) {
1830 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001831 if (echo_control_factory_) {
1832 private_submodules_->echo_controller =
1833 echo_control_factory_->Create(proc_sample_rate_hz());
1834 } else {
1835 private_submodules_->echo_controller = absl::make_unique<EchoCanceller3>(
1836 EchoCanceller3Config(), proc_sample_rate_hz(), true);
1837 }
1838
1839 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001840
Per Åhgrenf204faf2019-04-25 15:18:06 +02001841 private_submodules_->echo_cancellation.reset();
1842 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001843 private_submodules_->echo_control_mobile.reset();
1844 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001845 return;
peahe0eae3c2016-12-14 01:16:23 -08001846 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001847
1848 private_submodules_->echo_controller.reset();
1849 capture_nonlocked_.echo_controller_enabled = false;
1850
1851 if (!config_.echo_canceller.enabled) {
1852 private_submodules_->echo_cancellation.reset();
1853 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001854 private_submodules_->echo_control_mobile.reset();
1855 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001856 return;
1857 }
1858
1859 if (config_.echo_canceller.mobile_mode) {
1860 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001861 size_t max_element_size =
1862 std::max(static_cast<size_t>(1),
1863 kMaxAllowedValuesOfSamplesPerBand *
1864 EchoControlMobileImpl::NumCancellersRequired(
1865 num_output_channels(), num_reverse_channels()));
1866
1867 std::vector<int16_t> template_queue_element(max_element_size);
1868
1869 aecm_render_signal_queue_.reset(
1870 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1871 kMaxNumFramesToBuffer, template_queue_element,
1872 RenderQueueItemVerifier<int16_t>(max_element_size)));
1873
1874 aecm_render_queue_buffer_.resize(max_element_size);
1875 aecm_capture_queue_buffer_.resize(max_element_size);
1876
1877 private_submodules_->echo_control_mobile.reset(new EchoControlMobileImpl());
1878
1879 private_submodules_->echo_control_mobile->Initialize(
1880 proc_split_sample_rate_hz(), num_reverse_channels(),
1881 num_output_channels());
1882
Per Åhgrenf204faf2019-04-25 15:18:06 +02001883 private_submodules_->echo_cancellation.reset();
1884 aec_render_signal_queue_.reset();
1885 return;
1886 }
1887
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001888 private_submodules_->echo_control_mobile.reset();
1889 aecm_render_signal_queue_.reset();
1890
Per Åhgrenf204faf2019-04-25 15:18:06 +02001891 // Create and activate AEC2.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001892 private_submodules_->echo_cancellation.reset(new EchoCancellationImpl());
1893 private_submodules_->echo_cancellation->SetExtraOptions(
1894 capture_nonlocked_.use_aec2_extended_filter,
1895 capture_nonlocked_.use_aec2_delay_agnostic,
1896 capture_nonlocked_.use_aec2_refined_adaptive_filter);
1897
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001898 size_t element_max_size =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001899 std::max(static_cast<size_t>(1),
1900 kMaxAllowedValuesOfSamplesPerBand *
1901 EchoCancellationImpl::NumCancellersRequired(
1902 num_output_channels(), num_reverse_channels()));
1903
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001904 std::vector<float> template_queue_element(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001905
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001906 aec_render_signal_queue_.reset(
1907 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1908 kMaxNumFramesToBuffer, template_queue_element,
1909 RenderQueueItemVerifier<float>(element_max_size)));
Per Åhgrenf204faf2019-04-25 15:18:06 +02001910
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001911 aec_render_queue_buffer_.resize(element_max_size);
1912 aec_capture_queue_buffer_.resize(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001913
1914 private_submodules_->echo_cancellation->Initialize(
1915 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
1916 num_proc_channels());
1917
Per Åhgrenf204faf2019-04-25 15:18:06 +02001918 private_submodules_->echo_cancellation->set_suppression_level(
1919 config_.echo_canceller.legacy_moderate_suppression_level
1920 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
1921 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
peahe0eae3c2016-12-14 01:16:23 -08001922}
peah8271d042016-11-22 07:24:52 -08001923
alessiob3ec96df2017-05-22 06:57:06 -07001924void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001925 if (config_.gain_controller2.enabled) {
1926 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001927 }
1928}
1929
Alex Loikob5c9a792018-04-16 16:31:22 +02001930void AudioProcessingImpl::InitializePreAmplifier() {
1931 if (config_.pre_amplifier.enabled) {
1932 private_submodules_->pre_amplifier.reset(
1933 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1934 } else {
1935 private_submodules_->pre_amplifier.reset();
1936 }
1937}
1938
ivoc9f4a4a02016-10-28 05:39:16 -07001939void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001940 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001941 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001942 proc_sample_rate_hz(), 1,
1943 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001944}
1945
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001946void AudioProcessingImpl::InitializeAnalyzer() {
1947 if (private_submodules_->capture_analyzer) {
1948 private_submodules_->capture_analyzer->Initialize(proc_sample_rate_hz(),
1949 num_proc_channels());
1950 }
1951}
1952
Sam Zackrisson0beac582017-09-25 12:04:02 +02001953void AudioProcessingImpl::InitializePostProcessor() {
1954 if (private_submodules_->capture_post_processor) {
1955 private_submodules_->capture_post_processor->Initialize(
1956 proc_sample_rate_hz(), num_proc_channels());
1957 }
1958}
1959
Alex Loiko5825aa62017-12-18 16:02:40 +01001960void AudioProcessingImpl::InitializePreProcessor() {
1961 if (private_submodules_->render_pre_processor) {
1962 private_submodules_->render_pre_processor->Initialize(
1963 formats_.render_processing_format.sample_rate_hz(),
1964 formats_.render_processing_format.num_channels());
1965 }
1966}
1967
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001968void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001969
aleloi868f32f2017-05-23 07:20:05 -07001970void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1971 if (!aec_dump_) {
1972 return;
1973 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001974
1975 std::string experiments_description = "";
1976 if (private_submodules_->echo_cancellation) {
1977 experiments_description +=
1978 private_submodules_->echo_cancellation->GetExperimentsDescription();
1979 }
aleloi868f32f2017-05-23 07:20:05 -07001980 // TODO(peah): Add semicolon-separated concatenations of experiment
1981 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001982 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1983 experiments_description += "AgcClippingLevelExperiment;";
1984 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001985 if (capture_nonlocked_.echo_controller_enabled) {
1986 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001987 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001988 if (config_.gain_controller2.enabled) {
1989 experiments_description += "GainController2;";
1990 }
aleloi868f32f2017-05-23 07:20:05 -07001991
1992 InternalAPMConfig apm_config;
1993
Per Åhgren200feba2019-03-06 04:16:46 +01001994 apm_config.aec_enabled = config_.echo_canceller.enabled;
aleloi868f32f2017-05-23 07:20:05 -07001995 apm_config.aec_delay_agnostic_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001996 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01001997 private_submodules_->echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07001998 apm_config.aec_drift_compensation_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001999 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002000 private_submodules_->echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002001 apm_config.aec_extended_filter_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002002 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002003 private_submodules_->echo_cancellation->is_extended_filter_enabled();
Per Åhgrenf204faf2019-04-25 15:18:06 +02002004 apm_config.aec_suppression_level =
2005 private_submodules_->echo_cancellation
2006 ? static_cast<int>(
2007 private_submodules_->echo_cancellation->suppression_level())
2008 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002009
Per Åhgrend547d862019-05-03 15:48:47 +02002010 apm_config.aecm_enabled = !!private_submodules_->echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002011 apm_config.aecm_comfort_noise_enabled =
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002012 private_submodules_->echo_control_mobile &&
Sam Zackrissonc22f5512018-11-05 16:10:00 +01002013 private_submodules_->echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002014 apm_config.aecm_routing_mode =
2015 private_submodules_->echo_control_mobile
2016 ? static_cast<int>(
2017 private_submodules_->echo_control_mobile->routing_mode())
2018 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002019
2020 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2021 apm_config.agc_mode =
2022 static_cast<int>(public_submodules_->gain_control->mode());
2023 apm_config.agc_limiter_enabled =
2024 public_submodules_->gain_control->is_limiter_enabled();
2025 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2026
2027 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2028
2029 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2030 apm_config.ns_level =
2031 static_cast<int>(public_submodules_->noise_suppression->level());
2032
2033 apm_config.transient_suppression_enabled =
2034 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002035 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002036 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2037 apm_config.pre_amplifier_fixed_gain_factor =
2038 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002039
2040 if (!forced && apm_config == apm_config_for_aec_dump_) {
2041 return;
2042 }
2043 aec_dump_->WriteConfig(apm_config);
2044 apm_config_for_aec_dump_ = apm_config;
2045}
2046
2047void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2048 const float* const* src) {
2049 RTC_DCHECK(aec_dump_);
2050 WriteAecDumpConfigMessage(false);
2051
2052 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2053 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2054 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002055 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002056 RecordAudioProcessingState();
2057}
2058
2059void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2060 const AudioFrame& capture_frame) {
2061 RTC_DCHECK(aec_dump_);
2062 WriteAecDumpConfigMessage(false);
2063
2064 aec_dump_->AddCaptureStreamInput(capture_frame);
2065 RecordAudioProcessingState();
2066}
2067
2068void AudioProcessingImpl::RecordProcessedCaptureStream(
2069 const float* const* processed_capture_stream) {
2070 RTC_DCHECK(aec_dump_);
2071
2072 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2073 const size_t num_channels =
2074 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002075 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2076 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002077 aec_dump_->WriteCaptureStreamMessage();
2078}
2079
2080void AudioProcessingImpl::RecordProcessedCaptureStream(
2081 const AudioFrame& processed_capture_frame) {
2082 RTC_DCHECK(aec_dump_);
2083
2084 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2085 aec_dump_->WriteCaptureStreamMessage();
2086}
2087
2088void AudioProcessingImpl::RecordAudioProcessingState() {
2089 RTC_DCHECK(aec_dump_);
2090 AecDump::AudioProcessingState audio_proc_state;
2091 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2092 audio_proc_state.drift =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002093 private_submodules_->echo_cancellation
2094 ? private_submodules_->echo_cancellation->stream_drift_samples()
2095 : 0;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01002096 audio_proc_state.level = agc1()->stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002097 audio_proc_state.keypress = capture_.key_pressed;
2098 aec_dump_->AddAudioProcessingState(audio_proc_state);
2099}
2100
kwiberg83ffe452016-08-29 14:46:07 -07002101AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002102 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002103 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002104 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002105 output_will_be_muted(false),
2106 key_pressed(false),
2107 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002108 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002109 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002110 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002111 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002112 prev_pre_amp_gain(-1.f),
2113 playout_volume(-1),
2114 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002115
2116AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2117
2118AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2119
2120AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2121
niklase@google.com470e71d2011-07-07 08:21:25 +00002122} // namespace webrtc