blob: fc463c1dbb5410e4587731dab2108095815800d1 [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
Per Åhgrena1351272019-08-15 12:15:46 +0200952 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800953 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700954 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800955 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000956
aleloi868f32f2017-05-23 07:20:05 -0700957 if (aec_dump_) {
958 RecordProcessedCaptureStream(dest);
959 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000960 return kNoError;
961}
962
Alex Loiko73ec0192018-05-15 10:52:28 +0200963void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200964 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200965 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200966 if (aec_dump_) {
967 aec_dump_->WriteRuntimeSetting(setting);
968 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200969 switch (setting.type()) {
970 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200971 if (config_.pre_amplifier.enabled) {
972 float value;
973 setting.GetFloat(&value);
974 private_submodules_->pre_amplifier->SetGainFactor(value);
975 }
976 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200977 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100978 case RuntimeSetting::Type::kCaptureCompressionGain: {
979 float value;
980 setting.GetFloat(&value);
981 int int_value = static_cast<int>(value + .5f);
982 config_.gain_controller1.compression_gain_db = int_value;
983 int error = agc1()->set_compression_gain_db(int_value);
984 RTC_DCHECK_EQ(kNoError, error);
985 break;
986 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200987 case RuntimeSetting::Type::kCaptureFixedPostGain: {
988 if (config_.gain_controller2.enabled) {
989 float value;
990 setting.GetFloat(&value);
991 config_.gain_controller2.fixed_digital.gain_db = value;
992 private_submodules_->gain_controller2->ApplyConfig(
993 config_.gain_controller2);
994 }
995 break;
996 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200997 case RuntimeSetting::Type::kPlayoutVolumeChange: {
998 int value;
999 setting.GetInt(&value);
1000 capture_.playout_volume = value;
1001 break;
1002 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001003 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1004 RTC_NOTREACHED();
1005 break;
1006 case RuntimeSetting::Type::kNotSpecified:
1007 RTC_NOTREACHED();
1008 break;
1009 }
1010 }
1011}
1012
1013void AudioProcessingImpl::HandleRenderRuntimeSettings() {
1014 RuntimeSetting setting;
1015 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +02001016 if (aec_dump_) {
1017 aec_dump_->WriteRuntimeSetting(setting);
1018 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001019 switch (setting.type()) {
1020 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1021 if (private_submodules_->render_pre_processor) {
1022 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
1023 }
1024 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001025 case RuntimeSetting::Type::kCapturePreGain: // fall-through
1026 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001027 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001028 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001029 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001030 RTC_NOTREACHED();
1031 break;
1032 }
1033 }
1034}
1035
peah9e6a2902017-05-15 07:19:21 -07001036void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001037 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001038
1039 // Insert the samples into the queue.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001040 if (private_submodules_->echo_cancellation) {
1041 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001042 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1043 num_reverse_channels(),
1044 &aec_render_queue_buffer_);
1045
Per Åhgrenf204faf2019-04-25 15:18:06 +02001046 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1047 // The data queue is full and needs to be emptied.
1048 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001049
Per Åhgrenf204faf2019-04-25 15:18:06 +02001050 // Retry the insert (should always work).
1051 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1052 RTC_DCHECK(result);
1053 }
peaha0624602016-10-25 04:45:24 -07001054 }
1055
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001056 if (private_submodules_->echo_control_mobile) {
1057 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1058 num_reverse_channels(),
1059 &aecm_render_queue_buffer_);
1060 RTC_DCHECK(aecm_render_signal_queue_);
1061 // Insert the samples into the queue.
1062 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1063 // The data queue is full and needs to be emptied.
1064 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001065
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001066 // Retry the insert (should always work).
1067 bool result =
1068 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1069 RTC_DCHECK(result);
1070 }
peah764e3642016-10-22 05:04:30 -07001071 }
peah701d6282016-10-25 05:42:20 -07001072
1073 if (!constants_.use_experimental_agc) {
1074 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1075 // Insert the samples into the queue.
1076 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1077 // The data queue is full and needs to be emptied.
1078 EmptyQueuedRenderAudio();
1079
1080 // Retry the insert (should always work).
1081 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1082 RTC_DCHECK(result);
1083 }
1084 }
peah9e6a2902017-05-15 07:19:21 -07001085}
ivoc9f4a4a02016-10-28 05:39:16 -07001086
peah9e6a2902017-05-15 07:19:21 -07001087void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001088 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1089
1090 // Insert the samples into the queue.
1091 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1092 // The data queue is full and needs to be emptied.
1093 EmptyQueuedRenderAudio();
1094
1095 // Retry the insert (should always work).
1096 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1097 RTC_DCHECK(result);
1098 }
peah764e3642016-10-22 05:04:30 -07001099}
1100
1101void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001102 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001103 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001104
ivoc9f4a4a02016-10-28 05:39:16 -07001105 const size_t new_red_render_queue_element_max_size =
1106 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1107
peaha0624602016-10-25 04:45:24 -07001108 // Reallocate the queues if the queue item sizes are too small to fit the
1109 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001110
1111 if (agc_render_queue_element_max_size_ <
1112 new_agc_render_queue_element_max_size) {
1113 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1114
1115 std::vector<int16_t> template_queue_element(
1116 agc_render_queue_element_max_size_);
1117
1118 agc_render_signal_queue_.reset(
1119 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1120 kMaxNumFramesToBuffer, template_queue_element,
1121 RenderQueueItemVerifier<int16_t>(
1122 agc_render_queue_element_max_size_)));
1123
1124 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1125 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1126 } else {
1127 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001128 }
ivoc9f4a4a02016-10-28 05:39:16 -07001129
1130 if (red_render_queue_element_max_size_ <
1131 new_red_render_queue_element_max_size) {
1132 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1133
1134 std::vector<float> template_queue_element(
1135 red_render_queue_element_max_size_);
1136
1137 red_render_signal_queue_.reset(
1138 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1139 kMaxNumFramesToBuffer, template_queue_element,
1140 RenderQueueItemVerifier<float>(
1141 red_render_queue_element_max_size_)));
1142
1143 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1144 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1145 } else {
1146 red_render_signal_queue_->Clear();
1147 }
peah764e3642016-10-22 05:04:30 -07001148}
1149
1150void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1151 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001152 if (private_submodules_->echo_cancellation) {
1153 RTC_DCHECK(aec_render_signal_queue_);
1154 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
1155 private_submodules_->echo_cancellation->ProcessRenderAudio(
1156 aec_capture_queue_buffer_);
1157 }
peaha0624602016-10-25 04:45:24 -07001158 }
1159
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001160 if (private_submodules_->echo_control_mobile) {
1161 RTC_DCHECK(aecm_render_signal_queue_);
1162 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
1163 private_submodules_->echo_control_mobile->ProcessRenderAudio(
1164 aecm_capture_queue_buffer_);
1165 }
peah701d6282016-10-25 05:42:20 -07001166 }
1167
1168 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1169 public_submodules_->gain_control->ProcessRenderAudio(
1170 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001171 }
ivoc9f4a4a02016-10-28 05:39:16 -07001172
1173 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001174 RTC_DCHECK(private_submodules_->echo_detector);
1175 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001176 red_capture_queue_buffer_);
1177 }
peah764e3642016-10-22 05:04:30 -07001178}
1179
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001180int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001181 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001182 {
1183 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001184 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001185 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001186 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001187 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001188 }
peahfa6228e2015-11-16 16:27:42 -08001189
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001190 if (!frame) {
1191 return kNullPointerError;
1192 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001193 // Must be a native rate.
1194 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1195 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001196 frame->sample_rate_hz_ != kSampleRate32kHz &&
1197 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001198 return kBadSampleRateError;
1199 }
peah192164e2015-11-17 02:16:45 -08001200
peahdf3efa82015-11-28 12:35:15 -08001201 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001202 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001203 {
1204 // Aquire lock for the access of api_format.
1205 // The lock is released immediately due to the conditional
1206 // reinitialization.
1207 rtc::CritScope cs_capture(&crit_capture_);
1208 // TODO(ajm): The input and output rates and channels are currently
1209 // constrained to be identical in the int16 interface.
1210 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001211
1212 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001213 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001214
Oskar Sundbom4b276482019-05-23 14:28:00 +02001215 reinitialization_required =
1216 reinitialization_required ||
1217 processing_config.input_stream().sample_rate_hz() !=
1218 frame->sample_rate_hz_ ||
1219 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1220 processing_config.output_stream().sample_rate_hz() !=
1221 frame->sample_rate_hz_ ||
1222 processing_config.output_stream().num_channels() != frame->num_channels_;
1223
1224 if (reinitialization_required) {
1225 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1226 processing_config.input_stream().set_num_channels(frame->num_channels_);
1227 processing_config.output_stream().set_sample_rate_hz(
1228 frame->sample_rate_hz_);
1229 processing_config.output_stream().set_num_channels(frame->num_channels_);
1230
1231 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001232 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001233 rtc::CritScope cs_capture(&crit_capture_);
1234 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001235 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001236
peahdf3efa82015-11-28 12:35:15 -08001237 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001238 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001239 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001240 return kBadDataLengthError;
1241 }
1242
aleloi868f32f2017-05-23 07:20:05 -07001243 if (aec_dump_) {
1244 RecordUnprocessedCaptureStream(*frame);
1245 }
1246
Per Åhgrena1351272019-08-15 12:15:46 +02001247 capture_.vad_activity = frame->vad_activity_;
peahdf3efa82015-11-28 12:35:15 -08001248 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001249 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001250 if (submodule_states_.CaptureMultiBandProcessingActive() ||
1251 submodule_states_.CaptureFullBandProcessingActive()) {
1252 capture_.capture_audio->InterleaveTo(frame);
1253 }
1254 frame->vad_activity_ = capture_.vad_activity;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001255
aleloi868f32f2017-05-23 07:20:05 -07001256 if (aec_dump_) {
1257 RecordProcessedCaptureStream(*frame);
1258 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001259
1260 return kNoError;
1261}
1262
peahde65ddc2016-09-16 15:02:15 -07001263int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001264 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001265
peahb58a1582016-03-15 09:34:24 -07001266 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001267 // TODO(peah): Simplify once the public API Enable functions for these
1268 // are moved to APM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001269 RTC_DCHECK_LE(!!private_submodules_->echo_controller +
1270 !!private_submodules_->echo_cancellation +
1271 !!private_submodules_->echo_control_mobile,
1272 1);
peahb58a1582016-03-15 09:34:24 -07001273
peahde65ddc2016-09-16 15:02:15 -07001274 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001275
Alex Loikob5c9a792018-04-16 16:31:22 +02001276 if (private_submodules_->pre_amplifier) {
1277 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
1278 capture_buffer->channels_f(), capture_buffer->num_channels(),
1279 capture_buffer->num_frames()));
1280 }
1281
Per Åhgren928146f2019-08-20 09:19:21 +02001282 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
1283 capture_buffer->channels_const_f()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001284 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001285 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1286 if (log_rms) {
1287 capture_rms_interval_counter_ = 0;
1288 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001289 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1290 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1291 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1292 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001293 }
1294
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001295 if (private_submodules_->echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001296 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001297 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001298 capture_.echo_path_gain_change =
1299 capture_.prev_analog_mic_level != analog_mic_level &&
1300 capture_.prev_analog_mic_level != -1;
1301 capture_.prev_analog_mic_level = analog_mic_level;
1302
Per Åhgrend2650d12018-10-02 17:00:59 +02001303 // Detect and flag any change in the pre-amplifier gain.
1304 if (private_submodules_->pre_amplifier) {
1305 float pre_amp_gain = private_submodules_->pre_amplifier->GetGainFactor();
1306 capture_.echo_path_gain_change =
1307 capture_.echo_path_gain_change ||
1308 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001309 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001310 capture_.prev_pre_amp_gain = pre_amp_gain;
1311 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001312
1313 // Detect volume change.
1314 capture_.echo_path_gain_change =
1315 capture_.echo_path_gain_change ||
1316 (capture_.prev_playout_volume != capture_.playout_volume &&
1317 capture_.prev_playout_volume >= 0);
1318 capture_.prev_playout_volume = capture_.playout_volume;
1319
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001320 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001321 }
1322
peahbe615622016-02-13 16:40:47 -08001323 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001324 public_submodules_->gain_control->is_enabled()) {
1325 private_submodules_->agc_manager->AnalyzePreProcess(
Per Åhgren928146f2019-08-20 09:19:21 +02001326 capture_buffer->channels_f()[0], capture_buffer->num_channels(),
peahde65ddc2016-09-16 15:02:15 -07001327 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001328
1329 if (constants_.use_experimental_agc_process_before_aec) {
1330 private_submodules_->agc_manager->Process(
Per Åhgren928146f2019-08-20 09:19:21 +02001331 capture_buffer->channels_const_f()[0],
Alex Loikod9342442018-09-10 13:59:41 +02001332 capture_nonlocked_.capture_processing_format.num_frames(),
1333 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1334 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001335 }
1336
peah2ace3f92016-09-10 04:42:27 -07001337 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1338 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001339 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1340 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001341 }
1342
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001343 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001344 // Force down-mixing of the number of channels after the detection of
1345 // capture signal saturation.
1346 // TODO(peah): Look into ensuring that this kind of tampering with the
1347 // AudioBuffer functionality should not be needed.
1348 capture_buffer->set_num_channels(1);
1349 }
1350
peahe0eae3c2016-12-14 01:16:23 -08001351 // TODO(peah): Move the AEC3 low-cut filter to this place.
1352 if (private_submodules_->low_cut_filter &&
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001353 !private_submodules_->echo_controller) {
peah8271d042016-11-22 07:24:52 -08001354 private_submodules_->low_cut_filter->Process(capture_buffer);
1355 }
peahde65ddc2016-09-16 15:02:15 -07001356 RETURN_ON_ERR(
1357 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1358 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001359
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001360 if (private_submodules_->echo_control_mobile) {
1361 // Ensure that the stream delay was set before the call to the
1362 // AECM ProcessCaptureAudio function.
1363 if (!was_stream_delay_set()) {
1364 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001365 }
1366
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001367 if (public_submodules_->noise_suppression->is_enabled()) {
Per Åhgrena1351272019-08-15 12:15:46 +02001368 private_submodules_->echo_control_mobile->CopyLowPassReference(
1369 capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001370 }
peahe0eae3c2016-12-14 01:16:23 -08001371
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001372 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah253534d2016-03-15 04:32:28 -07001373
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001374 RETURN_ON_ERR(private_submodules_->echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001375 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001376 } else {
1377 if (private_submodules_->echo_controller) {
1378 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1379
1380 if (was_stream_delay_set()) {
1381 private_submodules_->echo_controller->SetAudioBufferDelay(
1382 stream_delay_ms());
1383 }
1384
1385 private_submodules_->echo_controller->ProcessCapture(
1386 capture_buffer, capture_.echo_path_gain_change);
1387 } else if (private_submodules_->echo_cancellation) {
1388 // Ensure that the stream delay was set before the call to the
1389 // AEC ProcessCaptureAudio function.
1390 if (!was_stream_delay_set()) {
1391 return AudioProcessing::kStreamParameterNotSetError;
1392 }
1393
1394 RETURN_ON_ERR(private_submodules_->echo_cancellation->ProcessCaptureAudio(
1395 capture_buffer, stream_delay_ms()));
1396 }
1397
1398 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
Per Åhgren46537a32017-06-07 10:08:10 +02001399 }
ivoc9f4a4a02016-10-28 05:39:16 -07001400
Per Åhgrena1351272019-08-15 12:15:46 +02001401 if (public_submodules_->voice_detection->is_enabled() &&
1402 !public_submodules_->voice_detection->using_external_vad()) {
1403 bool voice_active =
1404 public_submodules_->voice_detection->ProcessCaptureAudio(
1405 capture_buffer);
1406 capture_.vad_activity =
1407 voice_active ? AudioFrame::kVadActive : AudioFrame::kVadPassive;
1408 }
1409
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001410 if (config_.voice_detection.enabled) {
1411 private_submodules_->voice_detector->ProcessCaptureAudio(capture_buffer);
1412 capture_.stats.voice_detected =
1413 private_submodules_->voice_detector->stream_has_voice();
1414 } else {
1415 capture_.stats.voice_detected = absl::nullopt;
1416 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001417
peahbe615622016-02-13 16:40:47 -08001418 if (constants_.use_experimental_agc &&
Alex Loikod9342442018-09-10 13:59:41 +02001419 public_submodules_->gain_control->is_enabled() &&
1420 !constants_.use_experimental_agc_process_before_aec) {
peahdf3efa82015-11-28 12:35:15 -08001421 private_submodules_->agc_manager->Process(
Per Åhgren928146f2019-08-20 09:19:21 +02001422 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
peahde65ddc2016-09-16 15:02:15 -07001423 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001424 }
Per Åhgren200feba2019-03-06 04:16:46 +01001425 // TODO(peah): Add reporting from AEC3 whether there is echo.
peahb8fbb542016-03-15 02:28:08 -07001426 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001427 capture_buffer,
Per Åhgrenf204faf2019-04-25 15:18:06 +02001428 private_submodules_->echo_cancellation &&
1429 private_submodules_->echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001430
peah2ace3f92016-09-10 04:42:27 -07001431 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1432 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001433 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1434 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001435 }
1436
peah9e6a2902017-05-15 07:19:21 -07001437 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001438 RTC_DCHECK(private_submodules_->echo_detector);
1439 private_submodules_->echo_detector->AnalyzeCaptureAudio(
peah9e6a2902017-05-15 07:19:21 -07001440 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
1441 capture_buffer->num_frames()));
1442 }
1443
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001444 // TODO(aluebs): Investigate if the transient suppression placement should be
1445 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001446 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001447 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001448 private_submodules_->agc_manager.get()
1449 ? private_submodules_->agc_manager->voice_probability()
1450 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001451
peahdf3efa82015-11-28 12:35:15 -08001452 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001453 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1454 capture_buffer->num_channels(),
1455 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001456 capture_buffer->num_frames_per_band(),
1457 capture_.keyboard_info.keyboard_data,
1458 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001459 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001460 }
1461
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001462 // Experimental APM sub-module that analyzes |capture_buffer|.
1463 if (private_submodules_->capture_analyzer) {
1464 private_submodules_->capture_analyzer->Analyze(capture_buffer);
1465 }
1466
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001467 if (config_.gain_controller2.enabled) {
Alex Loikoa837dd72018-08-06 16:32:12 +02001468 private_submodules_->gain_controller2->NotifyAnalogLevel(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001469 agc1()->stream_analog_level());
alessiob3ec96df2017-05-22 06:57:06 -07001470 private_submodules_->gain_controller2->Process(capture_buffer);
1471 }
1472
Sam Zackrisson0beac582017-09-25 12:04:02 +02001473 if (private_submodules_->capture_post_processor) {
1474 private_submodules_->capture_post_processor->Process(capture_buffer);
1475 }
1476
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001477 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001478 public_submodules_->level_estimator->ProcessStream(capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001479 if (config_.level_estimation.enabled) {
1480 private_submodules_->output_level_estimator->ProcessStream(capture_buffer);
1481 capture_.stats.output_rms_dbfs =
1482 private_submodules_->output_level_estimator->RMS();
1483 } else {
1484 capture_.stats.output_rms_dbfs = absl::nullopt;
1485 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001486
Per Åhgren928146f2019-08-20 09:19:21 +02001487 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
1488 capture_buffer->channels_const_f()[0],
peah1b08dc32016-12-20 13:45:58 -08001489 capture_nonlocked_.capture_processing_format.num_frames()));
1490 if (log_rms) {
1491 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1492 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1493 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1494 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1495 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1496 }
1497
peahdf3efa82015-11-28 12:35:15 -08001498 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001499 return kNoError;
1500}
1501
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001502int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001503 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001504 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001505 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001506 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001507 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001508 const StreamConfig reverse_config = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001509 sample_rate_hz,
1510 ChannelsFromLayout(layout),
1511 LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001512 };
1513 if (samples_per_channel != reverse_config.num_frames()) {
1514 return kBadDataLengthError;
1515 }
peahdf3efa82015-11-28 12:35:15 -08001516 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001517}
1518
peahde65ddc2016-09-16 15:02:15 -07001519int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1520 const StreamConfig& input_config,
1521 const StreamConfig& output_config,
1522 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001523 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001524 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001525 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001526 if (submodule_states_.RenderMultiBandProcessingActive() ||
1527 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001528 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1529 dest);
peah2ace3f92016-09-10 04:42:27 -07001530 } else if (formats_.api_format.reverse_input_stream() !=
1531 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001532 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1533 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001534 } else {
peahde65ddc2016-09-16 15:02:15 -07001535 CopyAudioIfNeeded(src, input_config.num_frames(),
1536 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001537 }
1538
1539 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001540}
1541
peahdf3efa82015-11-28 12:35:15 -08001542int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001543 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001544 const StreamConfig& input_config,
1545 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001546 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001547 return kNullPointerError;
1548 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001549
peahde65ddc2016-09-16 15:02:15 -07001550 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001551 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001552 }
1553
peahdf3efa82015-11-28 12:35:15 -08001554 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001555 processing_config.reverse_input_stream() = input_config;
1556 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001557
peahdf3efa82015-11-28 12:35:15 -08001558 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001559 RTC_DCHECK_EQ(input_config.num_frames(),
1560 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001561
aleloi868f32f2017-05-23 07:20:05 -07001562 if (aec_dump_) {
1563 const size_t channel_size =
1564 formats_.api_format.reverse_input_stream().num_frames();
1565 const size_t num_channels =
1566 formats_.api_format.reverse_input_stream().num_channels();
1567 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001568 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001569 }
peahdf3efa82015-11-28 12:35:15 -08001570 render_.render_audio->CopyFrom(src,
1571 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001572 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001573}
1574
1575int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001576 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001577 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001578 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001579 return kNullPointerError;
1580 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001581 // Must be a native rate.
1582 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1583 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001584 frame->sample_rate_hz_ != kSampleRate32kHz &&
1585 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001586 return kBadSampleRateError;
1587 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001588
Michael Graczyk86c6d332015-07-23 11:41:39 -07001589 if (frame->num_channels_ <= 0) {
1590 return kBadNumberChannelsError;
1591 }
1592
peahdf3efa82015-11-28 12:35:15 -08001593 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001594 processing_config.reverse_input_stream().set_sample_rate_hz(
1595 frame->sample_rate_hz_);
1596 processing_config.reverse_input_stream().set_num_channels(
1597 frame->num_channels_);
1598 processing_config.reverse_output_stream().set_sample_rate_hz(
1599 frame->sample_rate_hz_);
1600 processing_config.reverse_output_stream().set_num_channels(
1601 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001602
peahdf3efa82015-11-28 12:35:15 -08001603 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001604 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001605 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001606 return kBadDataLengthError;
1607 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001608
aleloi868f32f2017-05-23 07:20:05 -07001609 if (aec_dump_) {
1610 aec_dump_->WriteRenderStreamMessage(*frame);
1611 }
1612
peahdf3efa82015-11-28 12:35:15 -08001613 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001614 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001615 if (submodule_states_.RenderMultiBandProcessingActive() ||
1616 submodule_states_.RenderFullBandProcessingActive()) {
1617 render_.render_audio->InterleaveTo(frame);
1618 }
aluebsb0319552016-03-17 20:39:53 -07001619 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001620}
niklase@google.com470e71d2011-07-07 08:21:25 +00001621
peahde65ddc2016-09-16 15:02:15 -07001622int AudioProcessingImpl::ProcessRenderStreamLocked() {
1623 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001624
Alex Loiko73ec0192018-05-15 10:52:28 +02001625 HandleRenderRuntimeSettings();
1626
Alex Loiko5825aa62017-12-18 16:02:40 +01001627 if (private_submodules_->render_pre_processor) {
1628 private_submodules_->render_pre_processor->Process(render_buffer);
1629 }
1630
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001631 QueueNonbandedRenderAudio(render_buffer);
1632
peah2ace3f92016-09-10 04:42:27 -07001633 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001634 SampleRateSupportsMultiBand(
1635 formats_.render_processing_format.sample_rate_hz())) {
1636 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001637 }
1638
peahce4d9152017-05-19 01:28:05 -07001639 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1640 QueueBandedRenderAudio(render_buffer);
1641 }
1642
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001643 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001644 if (private_submodules_->echo_controller) {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001645 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001646 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001647
peah2ace3f92016-09-10 04:42:27 -07001648 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001649 SampleRateSupportsMultiBand(
1650 formats_.render_processing_format.sample_rate_hz())) {
1651 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001652 }
1653
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001654 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001655}
1656
1657int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001658 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001659 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001660 capture_.was_stream_delay_set = true;
1661 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001662
niklase@google.com470e71d2011-07-07 08:21:25 +00001663 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001664 delay = 0;
1665 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001666 }
1667
1668 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1669 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001670 delay = 500;
1671 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001672 }
1673
peahdf3efa82015-11-28 12:35:15 -08001674 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001675 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001676}
1677
1678int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001679 // Used as callback from submodules, hence locking is not allowed.
1680 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001681}
1682
1683bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001684 // Used as callback from submodules, hence locking is not allowed.
1685 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001686}
1687
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001688void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001689 rtc::CritScope cs(&crit_capture_);
1690 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001691}
1692
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001693void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001694 rtc::CritScope cs(&crit_capture_);
1695 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001696}
1697
1698int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001699 rtc::CritScope cs(&crit_capture_);
1700 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001701}
1702
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001703void AudioProcessingImpl::set_stream_analog_level(int level) {
1704 rtc::CritScope cs_capture(&crit_capture_);
1705 int error = agc1()->set_stream_analog_level(level);
1706 RTC_DCHECK_EQ(kNoError, error);
1707}
1708
1709int AudioProcessingImpl::recommended_stream_analog_level() const {
1710 rtc::CritScope cs_capture(&crit_capture_);
1711 return agc1()->stream_analog_level();
1712}
1713
aleloi868f32f2017-05-23 07:20:05 -07001714void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1715 RTC_DCHECK(aec_dump);
1716 rtc::CritScope cs_render(&crit_render_);
1717 rtc::CritScope cs_capture(&crit_capture_);
1718
1719 // The previously attached AecDump will be destroyed with the
1720 // 'aec_dump' parameter, which is after locks are released.
1721 aec_dump_.swap(aec_dump);
1722 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001723 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001724}
1725
1726void AudioProcessingImpl::DetachAecDump() {
1727 // The d-tor of a task-queue based AecDump blocks until all pending
1728 // tasks are done. This construction avoids blocking while holding
1729 // the render and capture locks.
1730 std::unique_ptr<AecDump> aec_dump = nullptr;
1731 {
1732 rtc::CritScope cs_render(&crit_render_);
1733 rtc::CritScope cs_capture(&crit_capture_);
1734 aec_dump = std::move(aec_dump_);
1735 }
1736}
1737
Sam Zackrisson4d364492018-03-02 16:03:21 +01001738void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1739 std::unique_ptr<AudioGenerator> audio_generator) {
1740 // TODO(bugs.webrtc.org/8882) Stub.
1741 // Reset internal audio generator with audio_generator.
1742}
1743
1744void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1745 // TODO(bugs.webrtc.org/8882) Stub.
1746 // Delete audio generator, if one is attached.
1747}
1748
Ivo Creusen56d46092017-11-24 17:29:59 +01001749AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001750 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001751 rtc::CritScope cs_capture(&crit_capture_);
1752 if (!has_remote_tracks) {
1753 return capture_.stats;
1754 }
1755 AudioProcessingStats stats = capture_.stats;
1756 EchoCancellationImpl::Metrics metrics;
1757 if (private_submodules_->echo_controller) {
1758 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1759 stats.echo_return_loss = ec_metrics.echo_return_loss;
1760 stats.echo_return_loss_enhancement =
1761 ec_metrics.echo_return_loss_enhancement;
1762 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001763 }
1764 if (config_.residual_echo_detector.enabled) {
1765 RTC_DCHECK(private_submodules_->echo_detector);
1766 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1767 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1768 stats.residual_echo_likelihood_recent_max =
1769 ed_metrics.echo_likelihood_recent_max;
1770 }
Ivo Creusenae026092017-11-20 13:07:16 +01001771 return stats;
1772}
1773
niklase@google.com470e71d2011-07-07 08:21:25 +00001774GainControl* AudioProcessingImpl::gain_control() const {
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001775 return public_submodules_->gain_control_config_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001776}
1777
niklase@google.com470e71d2011-07-07 08:21:25 +00001778LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001779 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001780}
1781
1782NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
Sam Zackrisson23513132019-01-11 15:10:32 +01001783 return public_submodules_->noise_suppression_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001784}
1785
1786VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001787 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001788}
1789
peah8271d042016-11-22 07:24:52 -08001790void AudioProcessingImpl::MutateConfig(
1791 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1792 rtc::CritScope cs_render(&crit_render_);
1793 rtc::CritScope cs_capture(&crit_capture_);
1794 mutator(&config_);
1795 ApplyConfig(config_);
1796}
1797
1798AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1799 rtc::CritScope cs_render(&crit_render_);
1800 rtc::CritScope cs_capture(&crit_capture_);
1801 return config_;
1802}
1803
peah2ace3f92016-09-10 04:42:27 -07001804bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1805 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001806 config_.high_pass_filter.enabled,
Per Åhgrend547d862019-05-03 15:48:47 +02001807 !!private_submodules_->echo_cancellation,
1808 !!private_submodules_->echo_control_mobile,
ivoc9f4a4a02016-10-28 05:39:16 -07001809 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001810 public_submodules_->noise_suppression->is_enabled(),
peah2ace3f92016-09-10 04:42:27 -07001811 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001812 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001813 capture_nonlocked_.echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -07001814 public_submodules_->voice_detection->is_enabled(),
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001815 config_.voice_detection.enabled,
peah2ace3f92016-09-10 04:42:27 -07001816 public_submodules_->level_estimator->is_enabled(),
1817 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001818}
1819
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001820void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001821 if (capture_.transient_suppressor_enabled) {
1822 if (!public_submodules_->transient_suppressor.get()) {
1823 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001824 }
peahdf3efa82015-11-28 12:35:15 -08001825 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001826 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1827 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001828 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001829}
1830
peah8271d042016-11-22 07:24:52 -08001831void AudioProcessingImpl::InitializeLowCutFilter() {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001832 if (submodule_states_.LowCutFilteringRequired()) {
peah8271d042016-11-22 07:24:52 -08001833 private_submodules_->low_cut_filter.reset(
1834 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1835 } else {
1836 private_submodules_->low_cut_filter.reset();
1837 }
1838}
alessiob3ec96df2017-05-22 06:57:06 -07001839
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001840void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001841 bool use_echo_controller =
1842 echo_control_factory_ ||
Per Åhgren200feba2019-03-06 04:16:46 +01001843 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode &&
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001844 !config_.echo_canceller.use_legacy_aec);
1845
1846 if (use_echo_controller) {
1847 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001848 if (echo_control_factory_) {
1849 private_submodules_->echo_controller =
1850 echo_control_factory_->Create(proc_sample_rate_hz());
1851 } else {
1852 private_submodules_->echo_controller = absl::make_unique<EchoCanceller3>(
1853 EchoCanceller3Config(), proc_sample_rate_hz(), true);
1854 }
1855
1856 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001857
Per Åhgrenf204faf2019-04-25 15:18:06 +02001858 private_submodules_->echo_cancellation.reset();
1859 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001860 private_submodules_->echo_control_mobile.reset();
1861 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001862 return;
peahe0eae3c2016-12-14 01:16:23 -08001863 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001864
1865 private_submodules_->echo_controller.reset();
1866 capture_nonlocked_.echo_controller_enabled = false;
1867
1868 if (!config_.echo_canceller.enabled) {
1869 private_submodules_->echo_cancellation.reset();
1870 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001871 private_submodules_->echo_control_mobile.reset();
1872 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001873 return;
1874 }
1875
1876 if (config_.echo_canceller.mobile_mode) {
1877 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001878 size_t max_element_size =
1879 std::max(static_cast<size_t>(1),
1880 kMaxAllowedValuesOfSamplesPerBand *
1881 EchoControlMobileImpl::NumCancellersRequired(
1882 num_output_channels(), num_reverse_channels()));
1883
1884 std::vector<int16_t> template_queue_element(max_element_size);
1885
1886 aecm_render_signal_queue_.reset(
1887 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1888 kMaxNumFramesToBuffer, template_queue_element,
1889 RenderQueueItemVerifier<int16_t>(max_element_size)));
1890
1891 aecm_render_queue_buffer_.resize(max_element_size);
1892 aecm_capture_queue_buffer_.resize(max_element_size);
1893
1894 private_submodules_->echo_control_mobile.reset(new EchoControlMobileImpl());
1895
1896 private_submodules_->echo_control_mobile->Initialize(
1897 proc_split_sample_rate_hz(), num_reverse_channels(),
1898 num_output_channels());
1899
Per Åhgrenf204faf2019-04-25 15:18:06 +02001900 private_submodules_->echo_cancellation.reset();
1901 aec_render_signal_queue_.reset();
1902 return;
1903 }
1904
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001905 private_submodules_->echo_control_mobile.reset();
1906 aecm_render_signal_queue_.reset();
1907
Per Åhgrenf204faf2019-04-25 15:18:06 +02001908 // Create and activate AEC2.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001909 private_submodules_->echo_cancellation.reset(new EchoCancellationImpl());
1910 private_submodules_->echo_cancellation->SetExtraOptions(
1911 capture_nonlocked_.use_aec2_extended_filter,
1912 capture_nonlocked_.use_aec2_delay_agnostic,
1913 capture_nonlocked_.use_aec2_refined_adaptive_filter);
1914
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001915 size_t element_max_size =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001916 std::max(static_cast<size_t>(1),
1917 kMaxAllowedValuesOfSamplesPerBand *
1918 EchoCancellationImpl::NumCancellersRequired(
1919 num_output_channels(), num_reverse_channels()));
1920
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001921 std::vector<float> template_queue_element(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001922
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001923 aec_render_signal_queue_.reset(
1924 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1925 kMaxNumFramesToBuffer, template_queue_element,
1926 RenderQueueItemVerifier<float>(element_max_size)));
Per Åhgrenf204faf2019-04-25 15:18:06 +02001927
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001928 aec_render_queue_buffer_.resize(element_max_size);
1929 aec_capture_queue_buffer_.resize(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001930
1931 private_submodules_->echo_cancellation->Initialize(
1932 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
1933 num_proc_channels());
1934
Per Åhgrenf204faf2019-04-25 15:18:06 +02001935 private_submodules_->echo_cancellation->set_suppression_level(
1936 config_.echo_canceller.legacy_moderate_suppression_level
1937 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
1938 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
peahe0eae3c2016-12-14 01:16:23 -08001939}
peah8271d042016-11-22 07:24:52 -08001940
alessiob3ec96df2017-05-22 06:57:06 -07001941void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001942 if (config_.gain_controller2.enabled) {
1943 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001944 }
1945}
1946
Alex Loikob5c9a792018-04-16 16:31:22 +02001947void AudioProcessingImpl::InitializePreAmplifier() {
1948 if (config_.pre_amplifier.enabled) {
1949 private_submodules_->pre_amplifier.reset(
1950 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1951 } else {
1952 private_submodules_->pre_amplifier.reset();
1953 }
1954}
1955
ivoc9f4a4a02016-10-28 05:39:16 -07001956void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001957 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001958 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001959 proc_sample_rate_hz(), 1,
1960 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001961}
1962
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001963void AudioProcessingImpl::InitializeAnalyzer() {
1964 if (private_submodules_->capture_analyzer) {
1965 private_submodules_->capture_analyzer->Initialize(proc_sample_rate_hz(),
1966 num_proc_channels());
1967 }
1968}
1969
Sam Zackrisson0beac582017-09-25 12:04:02 +02001970void AudioProcessingImpl::InitializePostProcessor() {
1971 if (private_submodules_->capture_post_processor) {
1972 private_submodules_->capture_post_processor->Initialize(
1973 proc_sample_rate_hz(), num_proc_channels());
1974 }
1975}
1976
Alex Loiko5825aa62017-12-18 16:02:40 +01001977void AudioProcessingImpl::InitializePreProcessor() {
1978 if (private_submodules_->render_pre_processor) {
1979 private_submodules_->render_pre_processor->Initialize(
1980 formats_.render_processing_format.sample_rate_hz(),
1981 formats_.render_processing_format.num_channels());
1982 }
1983}
1984
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001985void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001986
aleloi868f32f2017-05-23 07:20:05 -07001987void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1988 if (!aec_dump_) {
1989 return;
1990 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001991
1992 std::string experiments_description = "";
1993 if (private_submodules_->echo_cancellation) {
1994 experiments_description +=
1995 private_submodules_->echo_cancellation->GetExperimentsDescription();
1996 }
aleloi868f32f2017-05-23 07:20:05 -07001997 // TODO(peah): Add semicolon-separated concatenations of experiment
1998 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001999 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
2000 experiments_description += "AgcClippingLevelExperiment;";
2001 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02002002 if (capture_nonlocked_.echo_controller_enabled) {
2003 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07002004 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02002005 if (config_.gain_controller2.enabled) {
2006 experiments_description += "GainController2;";
2007 }
aleloi868f32f2017-05-23 07:20:05 -07002008
2009 InternalAPMConfig apm_config;
2010
Per Åhgren200feba2019-03-06 04:16:46 +01002011 apm_config.aec_enabled = config_.echo_canceller.enabled;
aleloi868f32f2017-05-23 07:20:05 -07002012 apm_config.aec_delay_agnostic_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002013 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002014 private_submodules_->echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002015 apm_config.aec_drift_compensation_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002016 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002017 private_submodules_->echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002018 apm_config.aec_extended_filter_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002019 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002020 private_submodules_->echo_cancellation->is_extended_filter_enabled();
Per Åhgrenf204faf2019-04-25 15:18:06 +02002021 apm_config.aec_suppression_level =
2022 private_submodules_->echo_cancellation
2023 ? static_cast<int>(
2024 private_submodules_->echo_cancellation->suppression_level())
2025 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002026
Per Åhgrend547d862019-05-03 15:48:47 +02002027 apm_config.aecm_enabled = !!private_submodules_->echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002028 apm_config.aecm_comfort_noise_enabled =
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002029 private_submodules_->echo_control_mobile &&
Sam Zackrissonc22f5512018-11-05 16:10:00 +01002030 private_submodules_->echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002031 apm_config.aecm_routing_mode =
2032 private_submodules_->echo_control_mobile
2033 ? static_cast<int>(
2034 private_submodules_->echo_control_mobile->routing_mode())
2035 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002036
2037 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2038 apm_config.agc_mode =
2039 static_cast<int>(public_submodules_->gain_control->mode());
2040 apm_config.agc_limiter_enabled =
2041 public_submodules_->gain_control->is_limiter_enabled();
2042 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2043
2044 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2045
2046 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2047 apm_config.ns_level =
2048 static_cast<int>(public_submodules_->noise_suppression->level());
2049
2050 apm_config.transient_suppression_enabled =
2051 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002052 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002053 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2054 apm_config.pre_amplifier_fixed_gain_factor =
2055 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002056
2057 if (!forced && apm_config == apm_config_for_aec_dump_) {
2058 return;
2059 }
2060 aec_dump_->WriteConfig(apm_config);
2061 apm_config_for_aec_dump_ = apm_config;
2062}
2063
2064void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2065 const float* const* src) {
2066 RTC_DCHECK(aec_dump_);
2067 WriteAecDumpConfigMessage(false);
2068
2069 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2070 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2071 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002072 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002073 RecordAudioProcessingState();
2074}
2075
2076void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2077 const AudioFrame& capture_frame) {
2078 RTC_DCHECK(aec_dump_);
2079 WriteAecDumpConfigMessage(false);
2080
2081 aec_dump_->AddCaptureStreamInput(capture_frame);
2082 RecordAudioProcessingState();
2083}
2084
2085void AudioProcessingImpl::RecordProcessedCaptureStream(
2086 const float* const* processed_capture_stream) {
2087 RTC_DCHECK(aec_dump_);
2088
2089 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2090 const size_t num_channels =
2091 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002092 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2093 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002094 aec_dump_->WriteCaptureStreamMessage();
2095}
2096
2097void AudioProcessingImpl::RecordProcessedCaptureStream(
2098 const AudioFrame& processed_capture_frame) {
2099 RTC_DCHECK(aec_dump_);
2100
2101 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2102 aec_dump_->WriteCaptureStreamMessage();
2103}
2104
2105void AudioProcessingImpl::RecordAudioProcessingState() {
2106 RTC_DCHECK(aec_dump_);
2107 AecDump::AudioProcessingState audio_proc_state;
2108 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2109 audio_proc_state.drift =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002110 private_submodules_->echo_cancellation
2111 ? private_submodules_->echo_cancellation->stream_drift_samples()
2112 : 0;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01002113 audio_proc_state.level = agc1()->stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002114 audio_proc_state.keypress = capture_.key_pressed;
2115 aec_dump_->AddAudioProcessingState(audio_proc_state);
2116}
2117
kwiberg83ffe452016-08-29 14:46:07 -07002118AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002119 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002120 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002121 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002122 output_will_be_muted(false),
2123 key_pressed(false),
2124 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002125 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002126 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002127 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002128 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002129 prev_pre_amp_gain(-1.f),
2130 playout_volume(-1),
2131 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002132
2133AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2134
Per Åhgrena1351272019-08-15 12:15:46 +02002135void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2136 const float* const* data,
2137 const StreamConfig& stream_config) {
2138 if (stream_config.has_keyboard()) {
2139 keyboard_data = data[stream_config.num_channels()];
2140 } else {
2141 keyboard_data = NULL;
2142 }
2143 num_keyboard_frames = stream_config.num_frames();
2144}
2145
kwiberg83ffe452016-08-29 14:46:07 -07002146AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2147
2148AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2149
niklase@google.com470e71d2011-07-07 08:21:25 +00002150} // namespace webrtc