blob: 464c61b848da4ea89319becfea79d927e67a2250 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org40654032012-01-30 20:51:15 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/audio_processing_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Michael Graczyk86c6d332015-07-23 11:41:39 -070013#include <algorithm>
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <cstdint>
alessiob3ec96df2017-05-22 06:57:06 -070015#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <type_traits>
17#include <utility>
niklase@google.com470e71d2011-07-07 08:21:25 +000018
Per Åhgren200feba2019-03-06 04:16:46 +010019#include "absl/memory/memory.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "absl/types/optional.h"
21#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_audio/audio_converter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_audio/include/audio_util.h"
Per Åhgren200feba2019-03-06 04:16:46 +010024#include "modules/audio_processing/aec3/echo_canceller3.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/agc/agc_manager_direct.h"
Alex Loikob5c9a792018-04-16 16:31:22 +020026#include "modules/audio_processing/agc2/gain_applier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/common.h"
29#include "modules/audio_processing/echo_cancellation_impl.h"
30#include "modules/audio_processing/echo_control_mobile_impl.h"
Sam Zackrissonf0d1c032019-03-27 13:28:08 +010031#include "modules/audio_processing/gain_control_config_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_processing/gain_control_for_experimental_agc.h"
33#include "modules/audio_processing/gain_control_impl.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010034#include "modules/audio_processing/gain_controller2.h"
Yves Gerey988cc082018-10-23 12:03:01 +020035#include "modules/audio_processing/include/audio_frame_view.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020036#include "modules/audio_processing/level_estimator_impl.h"
Per Åhgren13735822018-02-12 21:42:56 +010037#include "modules/audio_processing/logging/apm_data_dumper.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020038#include "modules/audio_processing/low_cut_filter.h"
39#include "modules/audio_processing/noise_suppression_impl.h"
Sam Zackrisson23513132019-01-11 15:10:32 +010040#include "modules/audio_processing/noise_suppression_proxy.h"
Per Åhgrend2650d12018-10-02 17:00:59 +020041#include "modules/audio_processing/residual_echo_detector.h"
42#include "modules/audio_processing/transient/transient_suppressor.h"
43#include "modules/audio_processing/voice_detection_impl.h"
Steve Anton10542f22019-01-11 09:11:00 -080044#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080048#include "rtc_base/ref_counted_object.h"
49#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020050#include "rtc_base/trace_event.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000052
Michael Graczyk86c6d332015-07-23 11:41:39 -070053#define RETURN_ON_ERR(expr) \
54 do { \
55 int err = (expr); \
56 if (err != kNoError) { \
57 return err; \
58 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000059 } while (0)
60
niklase@google.com470e71d2011-07-07 08:21:25 +000061namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070062
kwibergd59d3bb2016-09-13 07:49:33 -070063constexpr int AudioProcessing::kNativeSampleRatesHz[];
Alex Loiko73ec0192018-05-15 10:52:28 +020064constexpr int kRuntimeSettingQueueSize = 100;
aluebsdf6416a2016-03-16 18:26:35 -070065
Michael Graczyk86c6d332015-07-23 11:41:39 -070066namespace {
67
68static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
69 switch (layout) {
70 case AudioProcessing::kMono:
71 case AudioProcessing::kStereo:
72 return false;
73 case AudioProcessing::kMonoAndKeyboard:
74 case AudioProcessing::kStereoAndKeyboard:
75 return true;
76 }
77
kwiberg9e2be5f2016-09-14 05:23:22 -070078 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070079 return false;
80}
aluebsdf6416a2016-03-16 18:26:35 -070081
peah2ace3f92016-09-10 04:42:27 -070082bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070083 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
84 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
85}
86
Per Åhgrend47941e2019-08-22 11:51:13 +020087int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) {
peah2ace3f92016-09-10 04:42:27 -070088#ifdef WEBRTC_ARCH_ARM_FAMILY
Per Åhgrend47941e2019-08-22 11:51:13 +020089 constexpr int kMaxSplittingNativeProcessRate =
90 AudioProcessing::kSampleRate32kHz;
peah2ace3f92016-09-10 04:42:27 -070091#else
Per Åhgrend47941e2019-08-22 11:51:13 +020092 constexpr int kMaxSplittingNativeProcessRate =
93 AudioProcessing::kSampleRate48kHz;
peah2ace3f92016-09-10 04:42:27 -070094#endif
Per Åhgrend47941e2019-08-22 11:51:13 +020095 static_assert(
96 kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz,
97 "");
98 const int uppermost_native_rate = band_splitting_required
99 ? kMaxSplittingNativeProcessRate
100 : AudioProcessing::kSampleRate48kHz;
peah2ace3f92016-09-10 04:42:27 -0700101
Per Åhgrend47941e2019-08-22 11:51:13 +0200102 for (auto rate : AudioProcessing::kNativeSampleRatesHz) {
peah2ace3f92016-09-10 04:42:27 -0700103 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
Per Åhgrend47941e2019-08-22 11:51:13 +0200501 const int render_audiobuffer_sample_rate_hz =
peahdf3efa82015-11-28 12:35:15 -0800502 formats_.api_format.reverse_output_stream().num_frames() == 0
Per Åhgrend47941e2019-08-22 11:51:13 +0200503 ? formats_.render_processing_format.sample_rate_hz()
504 : formats_.api_format.reverse_output_stream().sample_rate_hz();
peahdf3efa82015-11-28 12:35:15 -0800505 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
506 render_.render_audio.reset(new AudioBuffer(
Per Åhgrend47941e2019-08-22 11:51:13 +0200507 formats_.api_format.reverse_input_stream().sample_rate_hz(),
peahdf3efa82015-11-28 12:35:15 -0800508 formats_.api_format.reverse_input_stream().num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200509 formats_.render_processing_format.sample_rate_hz(),
peahde65ddc2016-09-16 15:02:15 -0700510 formats_.render_processing_format.num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +0200511 render_audiobuffer_sample_rate_hz,
512 formats_.render_processing_format.num_channels()));
peah2ace3f92016-09-10 04:42:27 -0700513 if (formats_.api_format.reverse_input_stream() !=
514 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800515 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800516 formats_.api_format.reverse_input_stream().num_channels(),
517 formats_.api_format.reverse_input_stream().num_frames(),
518 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800519 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700520 } else {
peahdf3efa82015-11-28 12:35:15 -0800521 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700522 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700523 } else {
peahdf3efa82015-11-28 12:35:15 -0800524 render_.render_audio.reset(nullptr);
525 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700526 }
peahce4d9152017-05-19 01:28:05 -0700527
Per Åhgrend47941e2019-08-22 11:51:13 +0200528 capture_.capture_audio.reset(new AudioBuffer(
529 formats_.api_format.input_stream().sample_rate_hz(),
530 formats_.api_format.input_stream().num_channels(),
531 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
532 formats_.api_format.output_stream().num_channels(),
533 formats_.api_format.output_stream().sample_rate_hz(),
534 formats_.api_format.output_stream().num_channels()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000535
peah764e3642016-10-22 05:04:30 -0700536 AllocateRenderQueue();
537
peah135259a2016-10-28 03:12:11 -0700538 public_submodules_->gain_control->Initialize(num_proc_channels(),
539 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700540 if (constants_.use_experimental_agc) {
541 if (!private_submodules_->agc_manager.get()) {
542 private_submodules_->agc_manager.reset(new AgcManagerDirect(
543 public_submodules_->gain_control.get(),
544 public_submodules_->gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200545 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
546 constants_.use_experimental_agc_agc2_level_estimation,
547 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700548 }
549 private_submodules_->agc_manager->Initialize();
550 private_submodules_->agc_manager->SetCaptureMuted(
551 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700552 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700553 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200554 InitializeTransient();
peah8271d042016-11-22 07:24:52 -0800555 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700556 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
557 proc_sample_rate_hz());
558 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100559 if (private_submodules_->voice_detector) {
560 private_submodules_->voice_detector->Initialize(
561 proc_split_sample_rate_hz());
562 }
peahde65ddc2016-09-16 15:02:15 -0700563 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700564 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200565 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700566 InitializeGainController2();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200567 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200568 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100569 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800570
aleloi868f32f2017-05-23 07:20:05 -0700571 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200572 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700573 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000574 return kNoError;
575}
576
Michael Graczyk86c6d332015-07-23 11:41:39 -0700577int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200578 UpdateActiveSubmoduleStates();
579
Michael Graczyk86c6d332015-07-23 11:41:39 -0700580 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700581 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
582 return kBadSampleRateError;
583 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000584 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700585
Peter Kasting69558702016-01-12 16:26:35 -0800586 const size_t num_in_channels = config.input_stream().num_channels();
587 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700588
589 // Need at least one input channel.
590 // Need either one output channel or as many outputs as there are inputs.
591 if (num_in_channels == 0 ||
592 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700593 return kBadNumberChannelsError;
594 }
595
peahdf3efa82015-11-28 12:35:15 -0800596 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000597
Per Åhgrend47941e2019-08-22 11:51:13 +0200598 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700599 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700600 formats_.api_format.output_stream().sample_rate_hz()),
601 submodule_states_.CaptureMultiBandSubModulesActive() ||
602 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000603
peahde65ddc2016-09-16 15:02:15 -0700604 capture_nonlocked_.capture_processing_format =
605 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700606
peah2ce640f2017-04-07 03:57:48 -0700607 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200608 if (!capture_nonlocked_.echo_controller_enabled) {
Per Åhgrend47941e2019-08-22 11:51:13 +0200609 render_processing_rate = FindNativeProcessRateToUse(
peah2ce640f2017-04-07 03:57:48 -0700610 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
611 formats_.api_format.reverse_output_stream().sample_rate_hz()),
612 submodule_states_.CaptureMultiBandSubModulesActive() ||
613 submodule_states_.RenderMultiBandSubModulesActive());
614 } else {
615 render_processing_rate = capture_processing_rate;
616 }
617
aluebseb3603b2016-04-20 15:27:58 -0700618 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
619 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700620 if (render_processing_rate > kSampleRate32kHz &&
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200621 !capture_nonlocked_.echo_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -0700622 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
623 ? kSampleRate32kHz
624 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700625 }
peah2ce640f2017-04-07 03:57:48 -0700626
peahde65ddc2016-09-16 15:02:15 -0700627 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700628 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700629 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
630 kSampleRate8kHz) {
631 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000632 } else {
peahde65ddc2016-09-16 15:02:15 -0700633 render_processing_rate =
634 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000635 }
636
peahde65ddc2016-09-16 15:02:15 -0700637 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000638 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700639 if (submodule_states_.RenderMultiBandSubModulesActive()) {
640 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
641 } else {
642 formats_.render_processing_format = StreamConfig(
643 formats_.api_format.reverse_input_stream().sample_rate_hz(),
644 formats_.api_format.reverse_input_stream().num_channels());
645 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000646
peahde65ddc2016-09-16 15:02:15 -0700647 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
648 kSampleRate32kHz ||
649 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
650 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800651 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000652 } else {
peahdf3efa82015-11-28 12:35:15 -0800653 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700654 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000655 }
656
657 return InitializeLocked();
658}
659
peah88ac8532016-09-12 16:47:25 -0700660void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peah88ac8532016-09-12 16:47:25 -0700661 // Run in a single-threaded manner when applying the settings.
662 rtc::CritScope cs_render(&crit_render_);
663 rtc::CritScope cs_capture(&crit_capture_);
664
Per Åhgren200feba2019-03-06 04:16:46 +0100665 const bool aec_config_changed =
666 config_.echo_canceller.enabled != config.echo_canceller.enabled ||
667 config_.echo_canceller.use_legacy_aec !=
668 config.echo_canceller.use_legacy_aec ||
669 config_.echo_canceller.mobile_mode != config.echo_canceller.mobile_mode ||
670 (config_.echo_canceller.enabled && config.echo_canceller.use_legacy_aec &&
671 config_.echo_canceller.legacy_moderate_suppression_level !=
672 config.echo_canceller.legacy_moderate_suppression_level);
673
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100674 const bool agc1_config_changed =
675 config_.gain_controller1.enabled != config.gain_controller1.enabled ||
676 config_.gain_controller1.mode != config.gain_controller1.mode ||
677 config_.gain_controller1.target_level_dbfs !=
678 config.gain_controller1.target_level_dbfs ||
679 config_.gain_controller1.compression_gain_db !=
680 config.gain_controller1.compression_gain_db ||
681 config_.gain_controller1.enable_limiter !=
682 config.gain_controller1.enable_limiter ||
683 config_.gain_controller1.analog_level_minimum !=
684 config.gain_controller1.analog_level_minimum ||
685 config_.gain_controller1.analog_level_maximum !=
686 config.gain_controller1.analog_level_maximum;
687
Yves Gerey499bc6c2018-10-10 18:29:07 +0200688 config_ = config;
689
Per Åhgren200feba2019-03-06 04:16:46 +0100690 if (aec_config_changed) {
691 InitializeEchoController();
692 }
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +0200693
Sam Zackrisson23513132019-01-11 15:10:32 +0100694 public_submodules_->noise_suppression->Enable(
695 config.noise_suppression.enabled);
696 public_submodules_->noise_suppression->set_level(
697 NsConfigLevelToInterfaceLevel(config.noise_suppression.level));
698
peah8271d042016-11-22 07:24:52 -0800699 InitializeLowCutFilter();
700
Mirko Bonadei675513b2017-11-09 11:09:25 +0100701 RTC_LOG(LS_INFO) << "Highpass filter activated: "
702 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800703
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100704 if (agc1_config_changed) {
705 ApplyAgc1Config(config_.gain_controller1);
706 }
707
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100708 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700709 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100710 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
711 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100712 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100713 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700714 config_.gain_controller2 = AudioProcessing::Config::GainController2();
715 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200716 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200717 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200718 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100719 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
720 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200721 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
722 << config_.pre_amplifier.enabled;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +0100723
724 if (config_.level_estimation.enabled &&
725 !private_submodules_->output_level_estimator) {
726 private_submodules_->output_level_estimator.reset(
727 new LevelEstimatorImpl(&crit_capture_));
728 private_submodules_->output_level_estimator->Enable(true);
729 }
Sam Zackrisson4db667b2018-12-21 16:29:27 +0100730
731 if (config_.voice_detection.enabled && !private_submodules_->voice_detector) {
732 private_submodules_->voice_detector.reset(
733 new VoiceDetectionImpl(&crit_capture_));
734 private_submodules_->voice_detector->Enable(true);
735 private_submodules_->voice_detector->set_likelihood(
736 VoiceDetection::kVeryLowLikelihood);
737 private_submodules_->voice_detector->Initialize(
738 proc_split_sample_rate_hz());
739 }
peah88ac8532016-09-12 16:47:25 -0700740}
741
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100742void AudioProcessingImpl::ApplyAgc1Config(
743 const Config::GainController1& config) {
744 GainControl* agc = agc1();
745 int error = agc->Enable(config.enabled);
746 RTC_DCHECK_EQ(kNoError, error);
747 error = agc->set_mode(Agc1ConfigModeToInterfaceMode(config.mode));
748 RTC_DCHECK_EQ(kNoError, error);
749 error = agc->set_target_level_dbfs(config.target_level_dbfs);
750 RTC_DCHECK_EQ(kNoError, error);
751 error = agc->set_compression_gain_db(config.compression_gain_db);
752 RTC_DCHECK_EQ(kNoError, error);
753 error = agc->enable_limiter(config.enable_limiter);
754 RTC_DCHECK_EQ(kNoError, error);
755 error = agc->set_analog_level_limits(config.analog_level_minimum,
756 config.analog_level_maximum);
757 RTC_DCHECK_EQ(kNoError, error);
758}
759
760GainControl* AudioProcessingImpl::agc1() {
761 if (constants_.use_experimental_agc) {
762 return public_submodules_->gain_control_for_experimental_agc.get();
763 }
764 return public_submodules_->gain_control.get();
765}
766
767const GainControl* AudioProcessingImpl::agc1() const {
768 if (constants_.use_experimental_agc) {
769 return public_submodules_->gain_control_for_experimental_agc.get();
770 }
771 return public_submodules_->gain_control.get();
772}
773
peah88ac8532016-09-12 16:47:25 -0700774void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800775 // Run in a single-threaded manner when setting the extra options.
776 rtc::CritScope cs_render(&crit_render_);
777 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000778
Per Åhgrenf204faf2019-04-25 15:18:06 +0200779 capture_nonlocked_.use_aec2_extended_filter =
780 config.Get<ExtendedFilter>().enabled;
781 capture_nonlocked_.use_aec2_delay_agnostic =
782 config.Get<DelayAgnostic>().enabled;
783 capture_nonlocked_.use_aec2_refined_adaptive_filter =
784 config.Get<RefinedAdaptiveFilter>().enabled;
peahb624d8c2016-03-05 03:01:14 -0800785
peahdf3efa82015-11-28 12:35:15 -0800786 if (capture_.transient_suppressor_enabled !=
787 config.Get<ExperimentalNs>().enabled) {
788 capture_.transient_suppressor_enabled =
789 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000790 InitializeTransient();
791 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000792}
793
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000794int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800795 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700796 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000797}
798
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000799int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800800 // Used as callback from submodules, hence locking is not allowed.
801 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000802}
803
Peter Kasting69558702016-01-12 16:26:35 -0800804size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800805 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700806 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000807}
808
Peter Kasting69558702016-01-12 16:26:35 -0800809size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800810 // Used as callback from submodules, hence locking is not allowed.
811 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000812}
813
Peter Kasting69558702016-01-12 16:26:35 -0800814size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800815 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200816 return capture_nonlocked_.echo_controller_enabled ? 1 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800817}
818
Peter Kasting69558702016-01-12 16:26:35 -0800819size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800820 // Used as callback from submodules, hence locking is not allowed.
821 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000822}
823
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000824void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800825 rtc::CritScope cs(&crit_capture_);
826 capture_.output_will_be_muted = muted;
827 if (private_submodules_->agc_manager.get()) {
828 private_submodules_->agc_manager->SetCaptureMuted(
829 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000830 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000831}
832
Alessio Bazzicac054e782018-04-16 12:10:09 +0200833void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200834 switch (setting.type()) {
835 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
836 render_runtime_settings_enqueuer_.Enqueue(setting);
837 return;
838 case RuntimeSetting::Type::kNotSpecified:
839 RTC_NOTREACHED();
840 return;
841 case RuntimeSetting::Type::kCapturePreGain:
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100842 case RuntimeSetting::Type::kCaptureCompressionGain:
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200843 case RuntimeSetting::Type::kCaptureFixedPostGain:
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200844 case RuntimeSetting::Type::kPlayoutVolumeChange:
Alex Loiko73ec0192018-05-15 10:52:28 +0200845 capture_runtime_settings_enqueuer_.Enqueue(setting);
846 return;
847 }
848 // The language allows the enum to have a non-enumerator
849 // value. Check that this doesn't happen.
850 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200851}
852
853AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
854 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200855 : runtime_settings_(*runtime_settings) {
856 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200857}
858
859AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
860 default;
861
862void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
863 RuntimeSetting setting) {
864 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200865 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200866 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200867 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200868 RTC_LOG(LS_ERROR)
869 << "The runtime settings queue is full. Oldest setting discarded.";
870 }
871 if (remaining_attempts == 0)
872 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
873}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000874
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000875int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700876 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000877 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000878 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000879 int output_sample_rate_hz,
880 ChannelLayout output_layout,
881 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800882 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800883 StreamConfig input_stream;
884 StreamConfig output_stream;
885 {
886 // Access the formats_.api_format.input_stream beneath the capture lock.
887 // The lock must be released as it is later required in the call
888 // to ProcessStream(,,,);
889 rtc::CritScope cs(&crit_capture_);
890 input_stream = formats_.api_format.input_stream();
891 output_stream = formats_.api_format.output_stream();
892 }
893
Michael Graczyk86c6d332015-07-23 11:41:39 -0700894 input_stream.set_sample_rate_hz(input_sample_rate_hz);
895 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
896 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700897 output_stream.set_sample_rate_hz(output_sample_rate_hz);
898 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
899 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
900
901 if (samples_per_channel != input_stream.num_frames()) {
902 return kBadDataLengthError;
903 }
904 return ProcessStream(src, input_stream, output_stream, dest);
905}
906
907int AudioProcessingImpl::ProcessStream(const float* const* src,
908 const StreamConfig& input_config,
909 const StreamConfig& output_config,
910 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800911 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800912 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700913 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800914 {
915 // Acquire the capture lock in order to safely call the function
916 // that retrieves the render side data. This function accesses apm
917 // getters that need the capture lock held when being called.
918 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700919 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800920
921 if (!src || !dest) {
922 return kNullPointerError;
923 }
924
925 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700926 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000927 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000928
Oskar Sundbom4b276482019-05-23 14:28:00 +0200929 if (processing_config.input_stream() != input_config) {
930 processing_config.input_stream() = input_config;
931 reinitialization_required = true;
peahdf3efa82015-11-28 12:35:15 -0800932 }
Oskar Sundbom4b276482019-05-23 14:28:00 +0200933
934 if (processing_config.output_stream() != output_config) {
935 processing_config.output_stream() = output_config;
936 reinitialization_required = true;
937 }
938
939 if (reinitialization_required) {
940 // Reinitialize.
941 rtc::CritScope cs_render(&crit_render_);
942 rtc::CritScope cs_capture(&crit_capture_);
943 RETURN_ON_ERR(InitializeLocked(processing_config));
944 }
945
peahdf3efa82015-11-28 12:35:15 -0800946 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700947 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
948 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000949
aleloi868f32f2017-05-23 07:20:05 -0700950 if (aec_dump_) {
951 RecordUnprocessedCaptureStream(src);
952 }
953
Per Åhgrena1351272019-08-15 12:15:46 +0200954 capture_.keyboard_info.Extract(src, formats_.api_format.input_stream());
peahdf3efa82015-11-28 12:35:15 -0800955 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700956 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800957 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000958
aleloi868f32f2017-05-23 07:20:05 -0700959 if (aec_dump_) {
960 RecordProcessedCaptureStream(dest);
961 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000962 return kNoError;
963}
964
Alex Loiko73ec0192018-05-15 10:52:28 +0200965void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200966 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200967 while (capture_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +0200968 if (aec_dump_) {
969 aec_dump_->WriteRuntimeSetting(setting);
970 }
Alessio Bazzicac054e782018-04-16 12:10:09 +0200971 switch (setting.type()) {
972 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200973 if (config_.pre_amplifier.enabled) {
974 float value;
975 setting.GetFloat(&value);
976 private_submodules_->pre_amplifier->SetGainFactor(value);
977 }
978 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200979 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +0100980 case RuntimeSetting::Type::kCaptureCompressionGain: {
981 float value;
982 setting.GetFloat(&value);
983 int int_value = static_cast<int>(value + .5f);
984 config_.gain_controller1.compression_gain_db = int_value;
985 int error = agc1()->set_compression_gain_db(int_value);
986 RTC_DCHECK_EQ(kNoError, error);
987 break;
988 }
Per Åhgren6ee75fd2019-04-26 11:33:37 +0200989 case RuntimeSetting::Type::kCaptureFixedPostGain: {
990 if (config_.gain_controller2.enabled) {
991 float value;
992 setting.GetFloat(&value);
993 config_.gain_controller2.fixed_digital.gain_db = value;
994 private_submodules_->gain_controller2->ApplyConfig(
995 config_.gain_controller2);
996 }
997 break;
998 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +0200999 case RuntimeSetting::Type::kPlayoutVolumeChange: {
1000 int value;
1001 setting.GetInt(&value);
1002 capture_.playout_volume = value;
1003 break;
1004 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001005 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1006 RTC_NOTREACHED();
1007 break;
1008 case RuntimeSetting::Type::kNotSpecified:
1009 RTC_NOTREACHED();
1010 break;
1011 }
1012 }
1013}
1014
1015void AudioProcessingImpl::HandleRenderRuntimeSettings() {
1016 RuntimeSetting setting;
1017 while (render_runtime_settings_.Remove(&setting)) {
Alex Loiko62347222018-09-10 10:18:07 +02001018 if (aec_dump_) {
1019 aec_dump_->WriteRuntimeSetting(setting);
1020 }
Alex Loiko73ec0192018-05-15 10:52:28 +02001021 switch (setting.type()) {
1022 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
1023 if (private_submodules_->render_pre_processor) {
1024 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
1025 }
1026 break;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001027 case RuntimeSetting::Type::kCapturePreGain: // fall-through
1028 case RuntimeSetting::Type::kCaptureCompressionGain: // fall-through
Per Åhgren6ee75fd2019-04-26 11:33:37 +02001029 case RuntimeSetting::Type::kCaptureFixedPostGain: // fall-through
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001030 case RuntimeSetting::Type::kPlayoutVolumeChange: // fall-through
Alessio Bazzica33444dc2018-04-20 13:16:55 +02001031 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +02001032 RTC_NOTREACHED();
1033 break;
1034 }
1035 }
1036}
1037
peah9e6a2902017-05-15 07:19:21 -07001038void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
kwibergaf476c72016-11-28 15:21:39 -08001039 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -07001040
1041 // Insert the samples into the queue.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001042 if (private_submodules_->echo_cancellation) {
1043 RTC_DCHECK(aec_render_signal_queue_);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001044 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1045 num_reverse_channels(),
1046 &aec_render_queue_buffer_);
1047
Per Åhgrenf204faf2019-04-25 15:18:06 +02001048 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
1049 // The data queue is full and needs to be emptied.
1050 EmptyQueuedRenderAudio();
peah764e3642016-10-22 05:04:30 -07001051
Per Åhgrenf204faf2019-04-25 15:18:06 +02001052 // Retry the insert (should always work).
1053 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
1054 RTC_DCHECK(result);
1055 }
peaha0624602016-10-25 04:45:24 -07001056 }
1057
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001058 if (private_submodules_->echo_control_mobile) {
1059 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
1060 num_reverse_channels(),
1061 &aecm_render_queue_buffer_);
1062 RTC_DCHECK(aecm_render_signal_queue_);
1063 // Insert the samples into the queue.
1064 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
1065 // The data queue is full and needs to be emptied.
1066 EmptyQueuedRenderAudio();
peaha0624602016-10-25 04:45:24 -07001067
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001068 // Retry the insert (should always work).
1069 bool result =
1070 aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
1071 RTC_DCHECK(result);
1072 }
peah764e3642016-10-22 05:04:30 -07001073 }
peah701d6282016-10-25 05:42:20 -07001074
1075 if (!constants_.use_experimental_agc) {
1076 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
1077 // Insert the samples into the queue.
1078 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
1079 // The data queue is full and needs to be emptied.
1080 EmptyQueuedRenderAudio();
1081
1082 // Retry the insert (should always work).
1083 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
1084 RTC_DCHECK(result);
1085 }
1086 }
peah9e6a2902017-05-15 07:19:21 -07001087}
ivoc9f4a4a02016-10-28 05:39:16 -07001088
peah9e6a2902017-05-15 07:19:21 -07001089void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001090 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1091
1092 // Insert the samples into the queue.
1093 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1094 // The data queue is full and needs to be emptied.
1095 EmptyQueuedRenderAudio();
1096
1097 // Retry the insert (should always work).
1098 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1099 RTC_DCHECK(result);
1100 }
peah764e3642016-10-22 05:04:30 -07001101}
1102
1103void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001104 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001105 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001106
ivoc9f4a4a02016-10-28 05:39:16 -07001107 const size_t new_red_render_queue_element_max_size =
1108 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1109
peaha0624602016-10-25 04:45:24 -07001110 // Reallocate the queues if the queue item sizes are too small to fit the
1111 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001112
1113 if (agc_render_queue_element_max_size_ <
1114 new_agc_render_queue_element_max_size) {
1115 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1116
1117 std::vector<int16_t> template_queue_element(
1118 agc_render_queue_element_max_size_);
1119
1120 agc_render_signal_queue_.reset(
1121 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1122 kMaxNumFramesToBuffer, template_queue_element,
1123 RenderQueueItemVerifier<int16_t>(
1124 agc_render_queue_element_max_size_)));
1125
1126 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1127 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1128 } else {
1129 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001130 }
ivoc9f4a4a02016-10-28 05:39:16 -07001131
1132 if (red_render_queue_element_max_size_ <
1133 new_red_render_queue_element_max_size) {
1134 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1135
1136 std::vector<float> template_queue_element(
1137 red_render_queue_element_max_size_);
1138
1139 red_render_signal_queue_.reset(
1140 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1141 kMaxNumFramesToBuffer, template_queue_element,
1142 RenderQueueItemVerifier<float>(
1143 red_render_queue_element_max_size_)));
1144
1145 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1146 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1147 } else {
1148 red_render_signal_queue_->Clear();
1149 }
peah764e3642016-10-22 05:04:30 -07001150}
1151
1152void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1153 rtc::CritScope cs_capture(&crit_capture_);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001154 if (private_submodules_->echo_cancellation) {
1155 RTC_DCHECK(aec_render_signal_queue_);
1156 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
1157 private_submodules_->echo_cancellation->ProcessRenderAudio(
1158 aec_capture_queue_buffer_);
1159 }
peaha0624602016-10-25 04:45:24 -07001160 }
1161
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001162 if (private_submodules_->echo_control_mobile) {
1163 RTC_DCHECK(aecm_render_signal_queue_);
1164 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
1165 private_submodules_->echo_control_mobile->ProcessRenderAudio(
1166 aecm_capture_queue_buffer_);
1167 }
peah701d6282016-10-25 05:42:20 -07001168 }
1169
1170 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1171 public_submodules_->gain_control->ProcessRenderAudio(
1172 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001173 }
ivoc9f4a4a02016-10-28 05:39:16 -07001174
1175 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001176 RTC_DCHECK(private_submodules_->echo_detector);
1177 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001178 red_capture_queue_buffer_);
1179 }
peah764e3642016-10-22 05:04:30 -07001180}
1181
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001182int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001183 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001184 {
1185 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001186 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001187 // getters that need the capture lock held when being called.
peahdf3efa82015-11-28 12:35:15 -08001188 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001189 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001190 }
peahfa6228e2015-11-16 16:27:42 -08001191
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001192 if (!frame) {
1193 return kNullPointerError;
1194 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001195 // Must be a native rate.
1196 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1197 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001198 frame->sample_rate_hz_ != kSampleRate32kHz &&
1199 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001200 return kBadSampleRateError;
1201 }
peah192164e2015-11-17 02:16:45 -08001202
peahdf3efa82015-11-28 12:35:15 -08001203 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001204 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001205 {
1206 // Aquire lock for the access of api_format.
1207 // The lock is released immediately due to the conditional
1208 // reinitialization.
1209 rtc::CritScope cs_capture(&crit_capture_);
1210 // TODO(ajm): The input and output rates and channels are currently
1211 // constrained to be identical in the int16 interface.
1212 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001213
1214 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001215 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001216
Oskar Sundbom4b276482019-05-23 14:28:00 +02001217 reinitialization_required =
1218 reinitialization_required ||
1219 processing_config.input_stream().sample_rate_hz() !=
1220 frame->sample_rate_hz_ ||
1221 processing_config.input_stream().num_channels() != frame->num_channels_ ||
1222 processing_config.output_stream().sample_rate_hz() !=
1223 frame->sample_rate_hz_ ||
1224 processing_config.output_stream().num_channels() != frame->num_channels_;
1225
1226 if (reinitialization_required) {
1227 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1228 processing_config.input_stream().set_num_channels(frame->num_channels_);
1229 processing_config.output_stream().set_sample_rate_hz(
1230 frame->sample_rate_hz_);
1231 processing_config.output_stream().set_num_channels(frame->num_channels_);
1232
1233 // Reinitialize.
peahdf3efa82015-11-28 12:35:15 -08001234 rtc::CritScope cs_render(&crit_render_);
Oskar Sundbom4b276482019-05-23 14:28:00 +02001235 rtc::CritScope cs_capture(&crit_capture_);
1236 RETURN_ON_ERR(InitializeLocked(processing_config));
peahdf3efa82015-11-28 12:35:15 -08001237 }
Oskar Sundbom4b276482019-05-23 14:28:00 +02001238
peahdf3efa82015-11-28 12:35:15 -08001239 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001240 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001241 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001242 return kBadDataLengthError;
1243 }
1244
aleloi868f32f2017-05-23 07:20:05 -07001245 if (aec_dump_) {
1246 RecordUnprocessedCaptureStream(*frame);
1247 }
1248
Per Åhgrena1351272019-08-15 12:15:46 +02001249 capture_.vad_activity = frame->vad_activity_;
Per Åhgrend47941e2019-08-22 11:51:13 +02001250 capture_.capture_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001251 RETURN_ON_ERR(ProcessCaptureStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001252 if (submodule_states_.CaptureMultiBandProcessingActive() ||
1253 submodule_states_.CaptureFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001254 capture_.capture_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001255 }
1256 frame->vad_activity_ = capture_.vad_activity;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001257
aleloi868f32f2017-05-23 07:20:05 -07001258 if (aec_dump_) {
1259 RecordProcessedCaptureStream(*frame);
1260 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001261
1262 return kNoError;
1263}
1264
peahde65ddc2016-09-16 15:02:15 -07001265int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001266 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001267
peahb58a1582016-03-15 09:34:24 -07001268 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001269 // TODO(peah): Simplify once the public API Enable functions for these
1270 // are moved to APM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001271 RTC_DCHECK_LE(!!private_submodules_->echo_controller +
1272 !!private_submodules_->echo_cancellation +
1273 !!private_submodules_->echo_control_mobile,
1274 1);
peahb58a1582016-03-15 09:34:24 -07001275
peahde65ddc2016-09-16 15:02:15 -07001276 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001277
Alex Loikob5c9a792018-04-16 16:31:22 +02001278 if (private_submodules_->pre_amplifier) {
1279 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001280 capture_buffer->channels(), capture_buffer->num_channels(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001281 capture_buffer->num_frames()));
1282 }
1283
Per Åhgren928146f2019-08-20 09:19:21 +02001284 capture_input_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001285 capture_buffer->channels_const()[0],
henrik.lundin290d43a2016-11-29 08:09:09 -08001286 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001287 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1288 if (log_rms) {
1289 capture_rms_interval_counter_ = 0;
1290 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001291 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1292 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1293 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1294 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001295 }
1296
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001297 if (private_submodules_->echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001298 // Detect and flag any change in the analog gain.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001299 int analog_mic_level = agc1()->stream_analog_level();
Per Åhgren88cf0502018-07-16 17:08:41 +02001300 capture_.echo_path_gain_change =
1301 capture_.prev_analog_mic_level != analog_mic_level &&
1302 capture_.prev_analog_mic_level != -1;
1303 capture_.prev_analog_mic_level = analog_mic_level;
1304
Per Åhgrend2650d12018-10-02 17:00:59 +02001305 // Detect and flag any change in the pre-amplifier gain.
1306 if (private_submodules_->pre_amplifier) {
1307 float pre_amp_gain = private_submodules_->pre_amplifier->GetGainFactor();
1308 capture_.echo_path_gain_change =
1309 capture_.echo_path_gain_change ||
1310 (capture_.prev_pre_amp_gain != pre_amp_gain &&
Per Åhgrene8a55692018-10-02 23:10:38 +02001311 capture_.prev_pre_amp_gain >= 0.f);
Per Åhgrend2650d12018-10-02 17:00:59 +02001312 capture_.prev_pre_amp_gain = pre_amp_gain;
1313 }
Fredrik Hernqvistca362852019-05-10 15:50:02 +02001314
1315 // Detect volume change.
1316 capture_.echo_path_gain_change =
1317 capture_.echo_path_gain_change ||
1318 (capture_.prev_playout_volume != capture_.playout_volume &&
1319 capture_.prev_playout_volume >= 0);
1320 capture_.prev_playout_volume = capture_.playout_volume;
1321
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001322 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001323 }
1324
peahbe615622016-02-13 16:40:47 -08001325 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001326 public_submodules_->gain_control->is_enabled()) {
1327 private_submodules_->agc_manager->AnalyzePreProcess(
Per Åhgren928146f2019-08-20 09:19:21 +02001328 capture_buffer->channels_f()[0], capture_buffer->num_channels(),
peahde65ddc2016-09-16 15:02:15 -07001329 capture_nonlocked_.capture_processing_format.num_frames());
Alex Loikod9342442018-09-10 13:59:41 +02001330
1331 if (constants_.use_experimental_agc_process_before_aec) {
1332 private_submodules_->agc_manager->Process(
Per Åhgrend47941e2019-08-22 11:51:13 +02001333 capture_buffer->channels_const()[0],
Alex Loikod9342442018-09-10 13:59:41 +02001334 capture_nonlocked_.capture_processing_format.num_frames(),
1335 capture_nonlocked_.capture_processing_format.sample_rate_hz());
1336 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001337 }
1338
peah2ace3f92016-09-10 04:42:27 -07001339 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1340 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001341 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1342 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001343 }
1344
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001345 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001346 // Force down-mixing of the number of channels after the detection of
1347 // capture signal saturation.
1348 // TODO(peah): Look into ensuring that this kind of tampering with the
1349 // AudioBuffer functionality should not be needed.
1350 capture_buffer->set_num_channels(1);
1351 }
1352
peahe0eae3c2016-12-14 01:16:23 -08001353 // TODO(peah): Move the AEC3 low-cut filter to this place.
1354 if (private_submodules_->low_cut_filter &&
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001355 !private_submodules_->echo_controller) {
peah8271d042016-11-22 07:24:52 -08001356 private_submodules_->low_cut_filter->Process(capture_buffer);
1357 }
peahde65ddc2016-09-16 15:02:15 -07001358 RETURN_ON_ERR(
1359 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1360 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001361
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001362 if (private_submodules_->echo_control_mobile) {
1363 // Ensure that the stream delay was set before the call to the
1364 // AECM ProcessCaptureAudio function.
1365 if (!was_stream_delay_set()) {
1366 return AudioProcessing::kStreamParameterNotSetError;
Per Åhgrend0fa8202018-04-18 09:35:13 +02001367 }
1368
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001369 if (public_submodules_->noise_suppression->is_enabled()) {
Per Åhgrena1351272019-08-15 12:15:46 +02001370 private_submodules_->echo_control_mobile->CopyLowPassReference(
1371 capture_buffer);
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001372 }
peahe0eae3c2016-12-14 01:16:23 -08001373
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001374 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah253534d2016-03-15 04:32:28 -07001375
Sam Zackrissonc22f5512018-11-05 16:10:00 +01001376 RETURN_ON_ERR(private_submodules_->echo_control_mobile->ProcessCaptureAudio(
Per Åhgren46537a32017-06-07 10:08:10 +02001377 capture_buffer, stream_delay_ms()));
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001378 } else {
1379 if (private_submodules_->echo_controller) {
1380 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1381
1382 if (was_stream_delay_set()) {
1383 private_submodules_->echo_controller->SetAudioBufferDelay(
1384 stream_delay_ms());
1385 }
1386
1387 private_submodules_->echo_controller->ProcessCapture(
1388 capture_buffer, capture_.echo_path_gain_change);
1389 } else if (private_submodules_->echo_cancellation) {
1390 // Ensure that the stream delay was set before the call to the
1391 // AEC ProcessCaptureAudio function.
1392 if (!was_stream_delay_set()) {
1393 return AudioProcessing::kStreamParameterNotSetError;
1394 }
1395
1396 RETURN_ON_ERR(private_submodules_->echo_cancellation->ProcessCaptureAudio(
1397 capture_buffer, stream_delay_ms()));
1398 }
1399
1400 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
Per Åhgren46537a32017-06-07 10:08:10 +02001401 }
ivoc9f4a4a02016-10-28 05:39:16 -07001402
Per Åhgrena1351272019-08-15 12:15:46 +02001403 if (public_submodules_->voice_detection->is_enabled() &&
1404 !public_submodules_->voice_detection->using_external_vad()) {
1405 bool voice_active =
1406 public_submodules_->voice_detection->ProcessCaptureAudio(
1407 capture_buffer);
1408 capture_.vad_activity =
1409 voice_active ? AudioFrame::kVadActive : AudioFrame::kVadPassive;
1410 }
1411
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001412 if (config_.voice_detection.enabled) {
1413 private_submodules_->voice_detector->ProcessCaptureAudio(capture_buffer);
1414 capture_.stats.voice_detected =
1415 private_submodules_->voice_detector->stream_has_voice();
1416 } else {
1417 capture_.stats.voice_detected = absl::nullopt;
1418 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001419
peahbe615622016-02-13 16:40:47 -08001420 if (constants_.use_experimental_agc &&
Alex Loikod9342442018-09-10 13:59:41 +02001421 public_submodules_->gain_control->is_enabled() &&
1422 !constants_.use_experimental_agc_process_before_aec) {
peahdf3efa82015-11-28 12:35:15 -08001423 private_submodules_->agc_manager->Process(
Per Åhgren928146f2019-08-20 09:19:21 +02001424 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
peahde65ddc2016-09-16 15:02:15 -07001425 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001426 }
Per Åhgren200feba2019-03-06 04:16:46 +01001427 // TODO(peah): Add reporting from AEC3 whether there is echo.
peahb8fbb542016-03-15 02:28:08 -07001428 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
Sam Zackrissoncdf0e6d2018-09-17 11:05:17 +02001429 capture_buffer,
Per Åhgrenf204faf2019-04-25 15:18:06 +02001430 private_submodules_->echo_cancellation &&
1431 private_submodules_->echo_cancellation->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001432
peah2ace3f92016-09-10 04:42:27 -07001433 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1434 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001435 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1436 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001437 }
1438
peah9e6a2902017-05-15 07:19:21 -07001439 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001440 RTC_DCHECK(private_submodules_->echo_detector);
1441 private_submodules_->echo_detector->AnalyzeCaptureAudio(
Per Åhgrend47941e2019-08-22 11:51:13 +02001442 rtc::ArrayView<const float>(capture_buffer->channels()[0],
peah9e6a2902017-05-15 07:19:21 -07001443 capture_buffer->num_frames()));
1444 }
1445
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001446 // TODO(aluebs): Investigate if the transient suppression placement should be
1447 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001448 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001449 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001450 private_submodules_->agc_manager.get()
1451 ? private_submodules_->agc_manager->voice_probability()
1452 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001453
peahdf3efa82015-11-28 12:35:15 -08001454 public_submodules_->transient_suppressor->Suppress(
Per Åhgrend47941e2019-08-22 11:51:13 +02001455 capture_buffer->channels()[0], capture_buffer->num_frames(),
peahde65ddc2016-09-16 15:02:15 -07001456 capture_buffer->num_channels(),
Per Åhgrend47941e2019-08-22 11:51:13 +02001457 capture_buffer->split_bands_const(0)[kBand0To8kHz],
Per Åhgrena1351272019-08-15 12:15:46 +02001458 capture_buffer->num_frames_per_band(),
1459 capture_.keyboard_info.keyboard_data,
1460 capture_.keyboard_info.num_keyboard_frames, voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001461 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001462 }
1463
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001464 // Experimental APM sub-module that analyzes |capture_buffer|.
1465 if (private_submodules_->capture_analyzer) {
1466 private_submodules_->capture_analyzer->Analyze(capture_buffer);
1467 }
1468
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001469 if (config_.gain_controller2.enabled) {
Alex Loikoa837dd72018-08-06 16:32:12 +02001470 private_submodules_->gain_controller2->NotifyAnalogLevel(
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001471 agc1()->stream_analog_level());
alessiob3ec96df2017-05-22 06:57:06 -07001472 private_submodules_->gain_controller2->Process(capture_buffer);
1473 }
1474
Sam Zackrisson0beac582017-09-25 12:04:02 +02001475 if (private_submodules_->capture_post_processor) {
1476 private_submodules_->capture_post_processor->Process(capture_buffer);
1477 }
1478
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001479 // The level estimator operates on the recombined data.
Per Åhgrend47941e2019-08-22 11:51:13 +02001480 public_submodules_->level_estimator->ProcessStream(*capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001481 if (config_.level_estimation.enabled) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001482 private_submodules_->output_level_estimator->ProcessStream(*capture_buffer);
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001483 capture_.stats.output_rms_dbfs =
1484 private_submodules_->output_level_estimator->RMS();
1485 } else {
1486 capture_.stats.output_rms_dbfs = absl::nullopt;
1487 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001488
Per Åhgren928146f2019-08-20 09:19:21 +02001489 capture_output_rms_.Analyze(rtc::ArrayView<const float>(
Per Åhgrend47941e2019-08-22 11:51:13 +02001490 capture_buffer->channels_const()[0],
peah1b08dc32016-12-20 13:45:58 -08001491 capture_nonlocked_.capture_processing_format.num_frames()));
1492 if (log_rms) {
1493 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1494 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1495 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1496 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1497 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1498 }
1499
peahdf3efa82015-11-28 12:35:15 -08001500 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001501 return kNoError;
1502}
1503
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001504int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001505 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001506 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001507 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001508 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001509 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001510 const StreamConfig reverse_config = {
Jonas Olssona4d87372019-07-05 19:08:33 +02001511 sample_rate_hz,
1512 ChannelsFromLayout(layout),
1513 LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001514 };
1515 if (samples_per_channel != reverse_config.num_frames()) {
1516 return kBadDataLengthError;
1517 }
peahdf3efa82015-11-28 12:35:15 -08001518 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001519}
1520
peahde65ddc2016-09-16 15:02:15 -07001521int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1522 const StreamConfig& input_config,
1523 const StreamConfig& output_config,
1524 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001525 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001526 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001527 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001528 if (submodule_states_.RenderMultiBandProcessingActive() ||
1529 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001530 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1531 dest);
peah2ace3f92016-09-10 04:42:27 -07001532 } else if (formats_.api_format.reverse_input_stream() !=
1533 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001534 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1535 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001536 } else {
peahde65ddc2016-09-16 15:02:15 -07001537 CopyAudioIfNeeded(src, input_config.num_frames(),
1538 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001539 }
1540
1541 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001542}
1543
peahdf3efa82015-11-28 12:35:15 -08001544int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001545 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001546 const StreamConfig& input_config,
1547 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001548 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001549 return kNullPointerError;
1550 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001551
peahde65ddc2016-09-16 15:02:15 -07001552 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001553 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001554 }
1555
peahdf3efa82015-11-28 12:35:15 -08001556 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001557 processing_config.reverse_input_stream() = input_config;
1558 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001559
peahdf3efa82015-11-28 12:35:15 -08001560 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001561 RTC_DCHECK_EQ(input_config.num_frames(),
1562 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001563
aleloi868f32f2017-05-23 07:20:05 -07001564 if (aec_dump_) {
1565 const size_t channel_size =
1566 formats_.api_format.reverse_input_stream().num_frames();
1567 const size_t num_channels =
1568 formats_.api_format.reverse_input_stream().num_channels();
1569 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001570 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001571 }
peahdf3efa82015-11-28 12:35:15 -08001572 render_.render_audio->CopyFrom(src,
1573 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001574 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001575}
1576
1577int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001578 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001579 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001580 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001581 return kNullPointerError;
1582 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001583 // Must be a native rate.
1584 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1585 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001586 frame->sample_rate_hz_ != kSampleRate32kHz &&
1587 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001588 return kBadSampleRateError;
1589 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001590
Michael Graczyk86c6d332015-07-23 11:41:39 -07001591 if (frame->num_channels_ <= 0) {
1592 return kBadNumberChannelsError;
1593 }
1594
peahdf3efa82015-11-28 12:35:15 -08001595 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001596 processing_config.reverse_input_stream().set_sample_rate_hz(
1597 frame->sample_rate_hz_);
1598 processing_config.reverse_input_stream().set_num_channels(
1599 frame->num_channels_);
1600 processing_config.reverse_output_stream().set_sample_rate_hz(
1601 frame->sample_rate_hz_);
1602 processing_config.reverse_output_stream().set_num_channels(
1603 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001604
peahdf3efa82015-11-28 12:35:15 -08001605 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001606 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001607 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001608 return kBadDataLengthError;
1609 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001610
aleloi868f32f2017-05-23 07:20:05 -07001611 if (aec_dump_) {
1612 aec_dump_->WriteRenderStreamMessage(*frame);
1613 }
1614
Per Åhgrend47941e2019-08-22 11:51:13 +02001615 render_.render_audio->CopyFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001616 RETURN_ON_ERR(ProcessRenderStreamLocked());
Per Åhgrena1351272019-08-15 12:15:46 +02001617 if (submodule_states_.RenderMultiBandProcessingActive() ||
1618 submodule_states_.RenderFullBandProcessingActive()) {
Per Åhgrend47941e2019-08-22 11:51:13 +02001619 render_.render_audio->CopyTo(frame);
Per Åhgrena1351272019-08-15 12:15:46 +02001620 }
aluebsb0319552016-03-17 20:39:53 -07001621 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001622}
niklase@google.com470e71d2011-07-07 08:21:25 +00001623
peahde65ddc2016-09-16 15:02:15 -07001624int AudioProcessingImpl::ProcessRenderStreamLocked() {
1625 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001626
Alex Loiko73ec0192018-05-15 10:52:28 +02001627 HandleRenderRuntimeSettings();
1628
Alex Loiko5825aa62017-12-18 16:02:40 +01001629 if (private_submodules_->render_pre_processor) {
1630 private_submodules_->render_pre_processor->Process(render_buffer);
1631 }
1632
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001633 QueueNonbandedRenderAudio(render_buffer);
1634
peah2ace3f92016-09-10 04:42:27 -07001635 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001636 SampleRateSupportsMultiBand(
1637 formats_.render_processing_format.sample_rate_hz())) {
1638 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001639 }
1640
peahce4d9152017-05-19 01:28:05 -07001641 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1642 QueueBandedRenderAudio(render_buffer);
1643 }
1644
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001645 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001646 if (private_submodules_->echo_controller) {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001647 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001648 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001649
peah2ace3f92016-09-10 04:42:27 -07001650 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001651 SampleRateSupportsMultiBand(
1652 formats_.render_processing_format.sample_rate_hz())) {
1653 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001654 }
1655
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001656 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001657}
1658
1659int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001660 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001661 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001662 capture_.was_stream_delay_set = true;
1663 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001664
niklase@google.com470e71d2011-07-07 08:21:25 +00001665 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001666 delay = 0;
1667 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001668 }
1669
1670 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1671 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001672 delay = 500;
1673 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001674 }
1675
peahdf3efa82015-11-28 12:35:15 -08001676 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001677 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001678}
1679
1680int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001681 // Used as callback from submodules, hence locking is not allowed.
1682 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001683}
1684
1685bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001686 // Used as callback from submodules, hence locking is not allowed.
1687 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001688}
1689
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001690void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001691 rtc::CritScope cs(&crit_capture_);
1692 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001693}
1694
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001695void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001696 rtc::CritScope cs(&crit_capture_);
1697 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001698}
1699
1700int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001701 rtc::CritScope cs(&crit_capture_);
1702 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001703}
1704
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001705void AudioProcessingImpl::set_stream_analog_level(int level) {
1706 rtc::CritScope cs_capture(&crit_capture_);
1707 int error = agc1()->set_stream_analog_level(level);
1708 RTC_DCHECK_EQ(kNoError, error);
1709}
1710
1711int AudioProcessingImpl::recommended_stream_analog_level() const {
1712 rtc::CritScope cs_capture(&crit_capture_);
1713 return agc1()->stream_analog_level();
1714}
1715
aleloi868f32f2017-05-23 07:20:05 -07001716void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1717 RTC_DCHECK(aec_dump);
1718 rtc::CritScope cs_render(&crit_render_);
1719 rtc::CritScope cs_capture(&crit_capture_);
1720
1721 // The previously attached AecDump will be destroyed with the
1722 // 'aec_dump' parameter, which is after locks are released.
1723 aec_dump_.swap(aec_dump);
1724 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001725 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001726}
1727
1728void AudioProcessingImpl::DetachAecDump() {
1729 // The d-tor of a task-queue based AecDump blocks until all pending
1730 // tasks are done. This construction avoids blocking while holding
1731 // the render and capture locks.
1732 std::unique_ptr<AecDump> aec_dump = nullptr;
1733 {
1734 rtc::CritScope cs_render(&crit_render_);
1735 rtc::CritScope cs_capture(&crit_capture_);
1736 aec_dump = std::move(aec_dump_);
1737 }
1738}
1739
Sam Zackrisson4d364492018-03-02 16:03:21 +01001740void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1741 std::unique_ptr<AudioGenerator> audio_generator) {
1742 // TODO(bugs.webrtc.org/8882) Stub.
1743 // Reset internal audio generator with audio_generator.
1744}
1745
1746void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1747 // TODO(bugs.webrtc.org/8882) Stub.
1748 // Delete audio generator, if one is attached.
1749}
1750
Ivo Creusen56d46092017-11-24 17:29:59 +01001751AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001752 bool has_remote_tracks) const {
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001753 rtc::CritScope cs_capture(&crit_capture_);
1754 if (!has_remote_tracks) {
1755 return capture_.stats;
1756 }
1757 AudioProcessingStats stats = capture_.stats;
1758 EchoCancellationImpl::Metrics metrics;
1759 if (private_submodules_->echo_controller) {
1760 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1761 stats.echo_return_loss = ec_metrics.echo_return_loss;
1762 stats.echo_return_loss_enhancement =
1763 ec_metrics.echo_return_loss_enhancement;
1764 stats.delay_ms = ec_metrics.delay_ms;
Sam Zackrissonb24c00f2018-11-26 16:18:25 +01001765 }
1766 if (config_.residual_echo_detector.enabled) {
1767 RTC_DCHECK(private_submodules_->echo_detector);
1768 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1769 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
1770 stats.residual_echo_likelihood_recent_max =
1771 ed_metrics.echo_likelihood_recent_max;
1772 }
Ivo Creusenae026092017-11-20 13:07:16 +01001773 return stats;
1774}
1775
niklase@google.com470e71d2011-07-07 08:21:25 +00001776GainControl* AudioProcessingImpl::gain_control() const {
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01001777 return public_submodules_->gain_control_config_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001778}
1779
niklase@google.com470e71d2011-07-07 08:21:25 +00001780LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001781 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001782}
1783
1784NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
Sam Zackrisson23513132019-01-11 15:10:32 +01001785 return public_submodules_->noise_suppression_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001786}
1787
1788VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001789 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001790}
1791
peah8271d042016-11-22 07:24:52 -08001792void AudioProcessingImpl::MutateConfig(
1793 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1794 rtc::CritScope cs_render(&crit_render_);
1795 rtc::CritScope cs_capture(&crit_capture_);
1796 mutator(&config_);
1797 ApplyConfig(config_);
1798}
1799
1800AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1801 rtc::CritScope cs_render(&crit_render_);
1802 rtc::CritScope cs_capture(&crit_capture_);
1803 return config_;
1804}
1805
peah2ace3f92016-09-10 04:42:27 -07001806bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1807 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001808 config_.high_pass_filter.enabled,
Per Åhgrend547d862019-05-03 15:48:47 +02001809 !!private_submodules_->echo_cancellation,
1810 !!private_submodules_->echo_control_mobile,
ivoc9f4a4a02016-10-28 05:39:16 -07001811 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001812 public_submodules_->noise_suppression->is_enabled(),
peah2ace3f92016-09-10 04:42:27 -07001813 public_submodules_->gain_control->is_enabled(),
Alex Loikob5c9a792018-04-16 16:31:22 +02001814 config_.gain_controller2.enabled, config_.pre_amplifier.enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001815 capture_nonlocked_.echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -07001816 public_submodules_->voice_detection->is_enabled(),
Sam Zackrisson4db667b2018-12-21 16:29:27 +01001817 config_.voice_detection.enabled,
peah2ace3f92016-09-10 04:42:27 -07001818 public_submodules_->level_estimator->is_enabled(),
1819 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001820}
1821
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001822void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001823 if (capture_.transient_suppressor_enabled) {
1824 if (!public_submodules_->transient_suppressor.get()) {
1825 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001826 }
peahdf3efa82015-11-28 12:35:15 -08001827 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001828 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1829 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001830 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001831}
1832
peah8271d042016-11-22 07:24:52 -08001833void AudioProcessingImpl::InitializeLowCutFilter() {
Sam Zackrissoncb1b5562018-09-28 14:15:09 +02001834 if (submodule_states_.LowCutFilteringRequired()) {
peah8271d042016-11-22 07:24:52 -08001835 private_submodules_->low_cut_filter.reset(
1836 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1837 } else {
1838 private_submodules_->low_cut_filter.reset();
1839 }
1840}
alessiob3ec96df2017-05-22 06:57:06 -07001841
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001842void AudioProcessingImpl::InitializeEchoController() {
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001843 bool use_echo_controller =
1844 echo_control_factory_ ||
Per Åhgren200feba2019-03-06 04:16:46 +01001845 (config_.echo_canceller.enabled && !config_.echo_canceller.mobile_mode &&
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001846 !config_.echo_canceller.use_legacy_aec);
1847
1848 if (use_echo_controller) {
1849 // Create and activate the echo controller.
Per Åhgren200feba2019-03-06 04:16:46 +01001850 if (echo_control_factory_) {
1851 private_submodules_->echo_controller =
1852 echo_control_factory_->Create(proc_sample_rate_hz());
1853 } else {
1854 private_submodules_->echo_controller = absl::make_unique<EchoCanceller3>(
1855 EchoCanceller3Config(), proc_sample_rate_hz(), true);
1856 }
1857
1858 capture_nonlocked_.echo_controller_enabled = true;
Per Åhgren200feba2019-03-06 04:16:46 +01001859
Per Åhgrenf204faf2019-04-25 15:18:06 +02001860 private_submodules_->echo_cancellation.reset();
1861 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001862 private_submodules_->echo_control_mobile.reset();
1863 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001864 return;
peahe0eae3c2016-12-14 01:16:23 -08001865 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001866
1867 private_submodules_->echo_controller.reset();
1868 capture_nonlocked_.echo_controller_enabled = false;
1869
1870 if (!config_.echo_canceller.enabled) {
1871 private_submodules_->echo_cancellation.reset();
1872 aec_render_signal_queue_.reset();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001873 private_submodules_->echo_control_mobile.reset();
1874 aecm_render_signal_queue_.reset();
Per Åhgrenf204faf2019-04-25 15:18:06 +02001875 return;
1876 }
1877
1878 if (config_.echo_canceller.mobile_mode) {
1879 // Create and activate AECM.
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001880 size_t max_element_size =
1881 std::max(static_cast<size_t>(1),
1882 kMaxAllowedValuesOfSamplesPerBand *
1883 EchoControlMobileImpl::NumCancellersRequired(
1884 num_output_channels(), num_reverse_channels()));
1885
1886 std::vector<int16_t> template_queue_element(max_element_size);
1887
1888 aecm_render_signal_queue_.reset(
1889 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1890 kMaxNumFramesToBuffer, template_queue_element,
1891 RenderQueueItemVerifier<int16_t>(max_element_size)));
1892
1893 aecm_render_queue_buffer_.resize(max_element_size);
1894 aecm_capture_queue_buffer_.resize(max_element_size);
1895
1896 private_submodules_->echo_control_mobile.reset(new EchoControlMobileImpl());
1897
1898 private_submodules_->echo_control_mobile->Initialize(
1899 proc_split_sample_rate_hz(), num_reverse_channels(),
1900 num_output_channels());
1901
Per Åhgrenf204faf2019-04-25 15:18:06 +02001902 private_submodules_->echo_cancellation.reset();
1903 aec_render_signal_queue_.reset();
1904 return;
1905 }
1906
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001907 private_submodules_->echo_control_mobile.reset();
1908 aecm_render_signal_queue_.reset();
1909
Per Åhgrenf204faf2019-04-25 15:18:06 +02001910 // Create and activate AEC2.
Per Åhgrenf204faf2019-04-25 15:18:06 +02001911 private_submodules_->echo_cancellation.reset(new EchoCancellationImpl());
1912 private_submodules_->echo_cancellation->SetExtraOptions(
1913 capture_nonlocked_.use_aec2_extended_filter,
1914 capture_nonlocked_.use_aec2_delay_agnostic,
1915 capture_nonlocked_.use_aec2_refined_adaptive_filter);
1916
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001917 size_t element_max_size =
Per Åhgrenf204faf2019-04-25 15:18:06 +02001918 std::max(static_cast<size_t>(1),
1919 kMaxAllowedValuesOfSamplesPerBand *
1920 EchoCancellationImpl::NumCancellersRequired(
1921 num_output_channels(), num_reverse_channels()));
1922
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001923 std::vector<float> template_queue_element(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001924
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001925 aec_render_signal_queue_.reset(
1926 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1927 kMaxNumFramesToBuffer, template_queue_element,
1928 RenderQueueItemVerifier<float>(element_max_size)));
Per Åhgrenf204faf2019-04-25 15:18:06 +02001929
Per Åhgrenb6e24d72019-04-29 12:14:50 +02001930 aec_render_queue_buffer_.resize(element_max_size);
1931 aec_capture_queue_buffer_.resize(element_max_size);
Per Åhgrenf204faf2019-04-25 15:18:06 +02001932
1933 private_submodules_->echo_cancellation->Initialize(
1934 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
1935 num_proc_channels());
1936
Per Åhgrenf204faf2019-04-25 15:18:06 +02001937 private_submodules_->echo_cancellation->set_suppression_level(
1938 config_.echo_canceller.legacy_moderate_suppression_level
1939 ? EchoCancellationImpl::SuppressionLevel::kModerateSuppression
1940 : EchoCancellationImpl::SuppressionLevel::kHighSuppression);
peahe0eae3c2016-12-14 01:16:23 -08001941}
peah8271d042016-11-22 07:24:52 -08001942
alessiob3ec96df2017-05-22 06:57:06 -07001943void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001944 if (config_.gain_controller2.enabled) {
1945 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001946 }
1947}
1948
Alex Loikob5c9a792018-04-16 16:31:22 +02001949void AudioProcessingImpl::InitializePreAmplifier() {
1950 if (config_.pre_amplifier.enabled) {
1951 private_submodules_->pre_amplifier.reset(
1952 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1953 } else {
1954 private_submodules_->pre_amplifier.reset();
1955 }
1956}
1957
ivoc9f4a4a02016-10-28 05:39:16 -07001958void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001959 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001960 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001961 proc_sample_rate_hz(), 1,
1962 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001963}
1964
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001965void AudioProcessingImpl::InitializeAnalyzer() {
1966 if (private_submodules_->capture_analyzer) {
1967 private_submodules_->capture_analyzer->Initialize(proc_sample_rate_hz(),
1968 num_proc_channels());
1969 }
1970}
1971
Sam Zackrisson0beac582017-09-25 12:04:02 +02001972void AudioProcessingImpl::InitializePostProcessor() {
1973 if (private_submodules_->capture_post_processor) {
1974 private_submodules_->capture_post_processor->Initialize(
1975 proc_sample_rate_hz(), num_proc_channels());
1976 }
1977}
1978
Alex Loiko5825aa62017-12-18 16:02:40 +01001979void AudioProcessingImpl::InitializePreProcessor() {
1980 if (private_submodules_->render_pre_processor) {
1981 private_submodules_->render_pre_processor->Initialize(
1982 formats_.render_processing_format.sample_rate_hz(),
1983 formats_.render_processing_format.num_channels());
1984 }
1985}
1986
Per Åhgrenea4c5df2019-05-03 09:00:08 +02001987void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {}
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001988
aleloi868f32f2017-05-23 07:20:05 -07001989void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1990 if (!aec_dump_) {
1991 return;
1992 }
Per Åhgrenf204faf2019-04-25 15:18:06 +02001993
1994 std::string experiments_description = "";
1995 if (private_submodules_->echo_cancellation) {
1996 experiments_description +=
1997 private_submodules_->echo_cancellation->GetExperimentsDescription();
1998 }
aleloi868f32f2017-05-23 07:20:05 -07001999 // TODO(peah): Add semicolon-separated concatenations of experiment
2000 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07002001 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
2002 experiments_description += "AgcClippingLevelExperiment;";
2003 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02002004 if (capture_nonlocked_.echo_controller_enabled) {
2005 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07002006 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02002007 if (config_.gain_controller2.enabled) {
2008 experiments_description += "GainController2;";
2009 }
aleloi868f32f2017-05-23 07:20:05 -07002010
2011 InternalAPMConfig apm_config;
2012
Per Åhgren200feba2019-03-06 04:16:46 +01002013 apm_config.aec_enabled = config_.echo_canceller.enabled;
aleloi868f32f2017-05-23 07:20:05 -07002014 apm_config.aec_delay_agnostic_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002015 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002016 private_submodules_->echo_cancellation->is_delay_agnostic_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002017 apm_config.aec_drift_compensation_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002018 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002019 private_submodules_->echo_cancellation->is_drift_compensation_enabled();
aleloi868f32f2017-05-23 07:20:05 -07002020 apm_config.aec_extended_filter_enabled =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002021 private_submodules_->echo_cancellation &&
Sam Zackrisson7f4dfa42018-11-01 08:59:29 +01002022 private_submodules_->echo_cancellation->is_extended_filter_enabled();
Per Åhgrenf204faf2019-04-25 15:18:06 +02002023 apm_config.aec_suppression_level =
2024 private_submodules_->echo_cancellation
2025 ? static_cast<int>(
2026 private_submodules_->echo_cancellation->suppression_level())
2027 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002028
Per Åhgrend547d862019-05-03 15:48:47 +02002029 apm_config.aecm_enabled = !!private_submodules_->echo_control_mobile;
aleloi868f32f2017-05-23 07:20:05 -07002030 apm_config.aecm_comfort_noise_enabled =
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002031 private_submodules_->echo_control_mobile &&
Sam Zackrissonc22f5512018-11-05 16:10:00 +01002032 private_submodules_->echo_control_mobile->is_comfort_noise_enabled();
Per Åhgrenb6e24d72019-04-29 12:14:50 +02002033 apm_config.aecm_routing_mode =
2034 private_submodules_->echo_control_mobile
2035 ? static_cast<int>(
2036 private_submodules_->echo_control_mobile->routing_mode())
2037 : 0;
aleloi868f32f2017-05-23 07:20:05 -07002038
2039 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2040 apm_config.agc_mode =
2041 static_cast<int>(public_submodules_->gain_control->mode());
2042 apm_config.agc_limiter_enabled =
2043 public_submodules_->gain_control->is_limiter_enabled();
2044 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2045
2046 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2047
2048 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2049 apm_config.ns_level =
2050 static_cast<int>(public_submodules_->noise_suppression->level());
2051
2052 apm_config.transient_suppression_enabled =
2053 capture_.transient_suppressor_enabled;
aleloi868f32f2017-05-23 07:20:05 -07002054 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002055 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2056 apm_config.pre_amplifier_fixed_gain_factor =
2057 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002058
2059 if (!forced && apm_config == apm_config_for_aec_dump_) {
2060 return;
2061 }
2062 aec_dump_->WriteConfig(apm_config);
2063 apm_config_for_aec_dump_ = apm_config;
2064}
2065
2066void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2067 const float* const* src) {
2068 RTC_DCHECK(aec_dump_);
2069 WriteAecDumpConfigMessage(false);
2070
2071 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2072 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2073 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002074 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002075 RecordAudioProcessingState();
2076}
2077
2078void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2079 const AudioFrame& capture_frame) {
2080 RTC_DCHECK(aec_dump_);
2081 WriteAecDumpConfigMessage(false);
2082
2083 aec_dump_->AddCaptureStreamInput(capture_frame);
2084 RecordAudioProcessingState();
2085}
2086
2087void AudioProcessingImpl::RecordProcessedCaptureStream(
2088 const float* const* processed_capture_stream) {
2089 RTC_DCHECK(aec_dump_);
2090
2091 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2092 const size_t num_channels =
2093 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002094 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2095 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002096 aec_dump_->WriteCaptureStreamMessage();
2097}
2098
2099void AudioProcessingImpl::RecordProcessedCaptureStream(
2100 const AudioFrame& processed_capture_frame) {
2101 RTC_DCHECK(aec_dump_);
2102
2103 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2104 aec_dump_->WriteCaptureStreamMessage();
2105}
2106
2107void AudioProcessingImpl::RecordAudioProcessingState() {
2108 RTC_DCHECK(aec_dump_);
2109 AecDump::AudioProcessingState audio_proc_state;
2110 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2111 audio_proc_state.drift =
Per Åhgrenf204faf2019-04-25 15:18:06 +02002112 private_submodules_->echo_cancellation
2113 ? private_submodules_->echo_cancellation->stream_drift_samples()
2114 : 0;
Sam Zackrissonf0d1c032019-03-27 13:28:08 +01002115 audio_proc_state.level = agc1()->stream_analog_level();
aleloi868f32f2017-05-23 07:20:05 -07002116 audio_proc_state.keypress = capture_.key_pressed;
2117 aec_dump_->AddAudioProcessingState(audio_proc_state);
2118}
2119
kwiberg83ffe452016-08-29 14:46:07 -07002120AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002121 bool transient_suppressor_enabled)
Per Åhgrenea4c5df2019-05-03 09:00:08 +02002122 : delay_offset_ms(0),
kwiberg83ffe452016-08-29 14:46:07 -07002123 was_stream_delay_set(false),
kwiberg83ffe452016-08-29 14:46:07 -07002124 output_will_be_muted(false),
2125 key_pressed(false),
2126 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002127 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002128 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002129 echo_path_gain_change(false),
Per Åhgrend2650d12018-10-02 17:00:59 +02002130 prev_analog_mic_level(-1),
Fredrik Hernqvistca362852019-05-10 15:50:02 +02002131 prev_pre_amp_gain(-1.f),
2132 playout_volume(-1),
2133 prev_playout_volume(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002134
2135AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2136
Per Åhgrena1351272019-08-15 12:15:46 +02002137void AudioProcessingImpl::ApmCaptureState::KeyboardInfo::Extract(
2138 const float* const* data,
2139 const StreamConfig& stream_config) {
2140 if (stream_config.has_keyboard()) {
2141 keyboard_data = data[stream_config.num_channels()];
2142 } else {
2143 keyboard_data = NULL;
2144 }
2145 num_keyboard_frames = stream_config.num_frames();
2146}
2147
kwiberg83ffe452016-08-29 14:46:07 -07002148AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2149
2150AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2151
niklase@google.com470e71d2011-07-07 08:21:25 +00002152} // namespace webrtc