blob: 4a1a86ce86aea1fb1144b74e7eb5de5958aa5ddc [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
peah103ac7e2017-04-12 05:40:55 -070013#include <math.h>
Michael Graczyk86c6d332015-07-23 11:41:39 -070014#include <algorithm>
alessiob3ec96df2017-05-22 06:57:06 -070015#include <string>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "common_audio/audio_converter.h"
18#include "common_audio/channel_buffer.h"
19#include "common_audio/include/audio_util.h"
20#include "common_audio/signal_processing/include/signal_processing_library.h"
21#include "modules/audio_processing/aec/aec_core.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_processing/agc/agc_manager_direct.h"
Alex Loikob5c9a792018-04-16 16:31:22 +020023#include "modules/audio_processing/agc2/gain_applier.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_processing/common.h"
26#include "modules/audio_processing/echo_cancellation_impl.h"
Sam Zackrisson74ed7342018-08-16 10:54:07 +020027#include "modules/audio_processing/echo_cancellation_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/echo_control_mobile_impl.h"
Sam Zackrisson74ed7342018-08-16 10:54:07 +020029#include "modules/audio_processing/echo_control_mobile_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "modules/audio_processing/gain_control_for_experimental_agc.h"
31#include "modules/audio_processing/gain_control_impl.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010032#include "modules/audio_processing/gain_controller2.h"
Per Åhgren13735822018-02-12 21:42:56 +010033#include "modules/audio_processing/logging/apm_data_dumper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "rtc_base/checks.h"
35#include "rtc_base/logging.h"
36#include "rtc_base/platform_file.h"
Niels Möller84255bb2017-10-06 13:43:23 +020037#include "rtc_base/refcountedobject.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020038#include "rtc_base/system/arch.h"
Minyue Li656d6092018-08-10 15:38:52 +020039#include "rtc_base/timeutils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020040#include "rtc_base/trace_event.h"
peah1bcfce52016-08-26 07:16:04 -070041#if WEBRTC_INTELLIGIBILITY_ENHANCER
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020042#include "modules/audio_processing/intelligibility/intelligibility_enhancer.h"
peah1bcfce52016-08-26 07:16:04 -070043#endif
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "modules/audio_processing/level_estimator_impl.h"
45#include "modules/audio_processing/low_cut_filter.h"
46#include "modules/audio_processing/noise_suppression_impl.h"
47#include "modules/audio_processing/residual_echo_detector.h"
48#include "modules/audio_processing/transient/transient_suppressor.h"
49#include "modules/audio_processing/voice_detection_impl.h"
Per Åhgren13735822018-02-12 21:42:56 +010050#include "rtc_base/atomicops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020051#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000052
peah1bcfce52016-08-26 07:16:04 -070053// Check to verify that the define for the intelligibility enhancer is properly
54// set.
55#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
56 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
57 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
58#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
59#endif
60
Michael Graczyk86c6d332015-07-23 11:41:39 -070061#define RETURN_ON_ERR(expr) \
62 do { \
63 int err = (expr); \
64 if (err != kNoError) { \
65 return err; \
66 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000067 } while (0)
68
niklase@google.com470e71d2011-07-07 08:21:25 +000069namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070070
kwibergd59d3bb2016-09-13 07:49:33 -070071constexpr int AudioProcessing::kNativeSampleRatesHz[];
Alex Loiko73ec0192018-05-15 10:52:28 +020072constexpr int kRuntimeSettingQueueSize = 100;
aluebsdf6416a2016-03-16 18:26:35 -070073
Michael Graczyk86c6d332015-07-23 11:41:39 -070074namespace {
75
76static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
77 switch (layout) {
78 case AudioProcessing::kMono:
79 case AudioProcessing::kStereo:
80 return false;
81 case AudioProcessing::kMonoAndKeyboard:
82 case AudioProcessing::kStereoAndKeyboard:
83 return true;
84 }
85
kwiberg9e2be5f2016-09-14 05:23:22 -070086 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070087 return false;
88}
aluebsdf6416a2016-03-16 18:26:35 -070089
peah2ace3f92016-09-10 04:42:27 -070090bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070091 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
92 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
93}
94
peah2ace3f92016-09-10 04:42:27 -070095int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) {
96#ifdef WEBRTC_ARCH_ARM_FAMILY
kwibergd59d3bb2016-09-13 07:49:33 -070097 constexpr int kMaxSplittingNativeProcessRate =
98 AudioProcessing::kSampleRate32kHz;
peah2ace3f92016-09-10 04:42:27 -070099#else
kwibergd59d3bb2016-09-13 07:49:33 -0700100 constexpr int kMaxSplittingNativeProcessRate =
101 AudioProcessing::kSampleRate48kHz;
peah2ace3f92016-09-10 04:42:27 -0700102#endif
kwibergd59d3bb2016-09-13 07:49:33 -0700103 static_assert(
104 kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz,
105 "");
peah2ace3f92016-09-10 04:42:27 -0700106 const int uppermost_native_rate = band_splitting_required
107 ? kMaxSplittingNativeProcessRate
108 : AudioProcessing::kSampleRate48kHz;
109
110 for (auto rate : AudioProcessing::kNativeSampleRatesHz) {
111 if (rate >= uppermost_native_rate) {
112 return uppermost_native_rate;
113 }
114 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700115 return rate;
116 }
117 }
peah2ace3f92016-09-10 04:42:27 -0700118 RTC_NOTREACHED();
119 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700120}
121
peah9e6a2902017-05-15 07:19:21 -0700122// Maximum lengths that frame of samples being passed from the render side to
123// the capture side can have (does not apply to AEC3).
124static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
125static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
126
peah764e3642016-10-22 05:04:30 -0700127// Maximum number of frames to buffer in the render queue.
128// TODO(peah): Decrease this once we properly handle hugely unbalanced
129// reverse and forward call numbers.
130static const size_t kMaxNumFramesToBuffer = 100;
131
peah8271d042016-11-22 07:24:52 -0800132class HighPassFilterImpl : public HighPassFilter {
133 public:
134 explicit HighPassFilterImpl(AudioProcessingImpl* apm) : apm_(apm) {}
135 ~HighPassFilterImpl() override = default;
136
137 // HighPassFilter implementation.
138 int Enable(bool enable) override {
139 apm_->MutateConfig([enable](AudioProcessing::Config* config) {
140 config->high_pass_filter.enabled = enable;
141 });
142
143 return AudioProcessing::kNoError;
144 }
145
146 bool is_enabled() const override {
147 return apm_->GetConfig().high_pass_filter.enabled;
148 }
149
150 private:
151 AudioProcessingImpl* apm_;
152 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(HighPassFilterImpl);
153};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700154} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000155
156// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000157static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000158
Sam Zackrisson0beac582017-09-25 12:04:02 +0200159AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates(
Alex Loiko5825aa62017-12-18 16:02:40 +0100160 bool capture_post_processor_enabled,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200161 bool render_pre_processor_enabled,
162 bool capture_analyzer_enabled)
Alex Loiko5825aa62017-12-18 16:02:40 +0100163 : capture_post_processor_enabled_(capture_post_processor_enabled),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200164 render_pre_processor_enabled_(render_pre_processor_enabled),
165 capture_analyzer_enabled_(capture_analyzer_enabled) {}
peah2ace3f92016-09-10 04:42:27 -0700166
167bool AudioProcessingImpl::ApmSubmoduleStates::Update(
peah8271d042016-11-22 07:24:52 -0800168 bool low_cut_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700169 bool echo_canceller_enabled,
170 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700171 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700172 bool noise_suppressor_enabled,
173 bool intelligibility_enhancer_enabled,
peah2ace3f92016-09-10 04:42:27 -0700174 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700175 bool gain_controller2_enabled,
Alex Loikob5c9a792018-04-16 16:31:22 +0200176 bool pre_amplifier_enabled,
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200177 bool echo_controller_enabled,
peah2ace3f92016-09-10 04:42:27 -0700178 bool voice_activity_detector_enabled,
179 bool level_estimator_enabled,
180 bool transient_suppressor_enabled) {
181 bool changed = false;
peah8271d042016-11-22 07:24:52 -0800182 changed |= (low_cut_filter_enabled != low_cut_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700183 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
184 changed |=
185 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700186 changed |=
187 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700188 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
189 changed |=
190 (intelligibility_enhancer_enabled != intelligibility_enhancer_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700191 changed |=
192 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
alessiob3ec96df2017-05-22 06:57:06 -0700193 changed |=
194 (gain_controller2_enabled != gain_controller2_enabled_);
Alex Loikob5c9a792018-04-16 16:31:22 +0200195 changed |= (pre_amplifier_enabled_ != pre_amplifier_enabled);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200196 changed |= (echo_controller_enabled != echo_controller_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700197 changed |= (level_estimator_enabled != level_estimator_enabled_);
198 changed |=
199 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
200 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
201 if (changed) {
peah8271d042016-11-22 07:24:52 -0800202 low_cut_filter_enabled_ = low_cut_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700203 echo_canceller_enabled_ = echo_canceller_enabled;
204 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700205 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700206 noise_suppressor_enabled_ = noise_suppressor_enabled;
207 intelligibility_enhancer_enabled_ = intelligibility_enhancer_enabled;
peah2ace3f92016-09-10 04:42:27 -0700208 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700209 gain_controller2_enabled_ = gain_controller2_enabled;
Alex Loikob5c9a792018-04-16 16:31:22 +0200210 pre_amplifier_enabled_ = pre_amplifier_enabled;
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200211 echo_controller_enabled_ = echo_controller_enabled;
peah2ace3f92016-09-10 04:42:27 -0700212 level_estimator_enabled_ = level_estimator_enabled;
213 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
214 transient_suppressor_enabled_ = transient_suppressor_enabled;
215 }
216
217 changed |= first_update_;
218 first_update_ = false;
219 return changed;
220}
221
222bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
223 const {
224#if WEBRTC_INTELLIGIBILITY_ENHANCER
225 return CaptureMultiBandProcessingActive() ||
peah52775842017-05-16 06:14:09 -0700226 intelligibility_enhancer_enabled_ || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700227#else
peah52775842017-05-16 06:14:09 -0700228 return CaptureMultiBandProcessingActive() || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700229#endif
230}
231
232bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
233 const {
peah8271d042016-11-22 07:24:52 -0800234 return low_cut_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700235 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200236 adaptive_gain_controller_enabled_ || echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700237}
238
peah23ac8b42017-05-23 05:33:56 -0700239bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
240 const {
Alex Loikob5c9a792018-04-16 16:31:22 +0200241 return gain_controller2_enabled_ || capture_post_processor_enabled_ ||
242 pre_amplifier_enabled_;
peah23ac8b42017-05-23 05:33:56 -0700243}
244
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200245bool AudioProcessingImpl::ApmSubmoduleStates::CaptureAnalyzerActive() const {
246 return capture_analyzer_enabled_;
247}
248
peah2ace3f92016-09-10 04:42:27 -0700249bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
250 const {
251 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800252 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200253 echo_controller_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700254}
255
Alex Loiko5825aa62017-12-18 16:02:40 +0100256bool AudioProcessingImpl::ApmSubmoduleStates::RenderFullBandProcessingActive()
257 const {
258 return render_pre_processor_enabled_;
259}
260
peah2ace3f92016-09-10 04:42:27 -0700261bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
262 const {
263#if WEBRTC_INTELLIGIBILITY_ENHANCER
264 return intelligibility_enhancer_enabled_;
265#else
266 return false;
267#endif
268}
269
solenberg5e465c32015-12-08 13:22:33 -0800270struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800271 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800272 // Accessed externally of APM without any lock acquired.
peahb624d8c2016-03-05 03:01:14 -0800273 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
peahbb9edbd2016-03-10 12:54:25 -0800274 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
Sam Zackrisson74ed7342018-08-16 10:54:07 +0200275 std::unique_ptr<EchoCancellationProxy> echo_cancellation_proxy;
276 std::unique_ptr<EchoControlMobileProxy> echo_control_mobile_proxy;
peahbfa97112016-03-10 21:09:04 -0800277 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800278 std::unique_ptr<LevelEstimatorImpl> level_estimator;
279 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
280 std::unique_ptr<VoiceDetectionImpl> voice_detection;
281 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800282 gain_control_for_experimental_agc;
solenberg5e465c32015-12-08 13:22:33 -0800283
284 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800285 std::unique_ptr<TransientSuppressor> transient_suppressor;
peah1bcfce52016-08-26 07:16:04 -0700286#if WEBRTC_INTELLIGIBILITY_ENHANCER
kwiberg88788ad2016-02-19 07:04:49 -0800287 std::unique_ptr<IntelligibilityEnhancer> intelligibility_enhancer;
peah1bcfce52016-08-26 07:16:04 -0700288#endif
solenberg5e465c32015-12-08 13:22:33 -0800289};
290
291struct AudioProcessingImpl::ApmPrivateSubmodules {
Sam Zackrissondb389722018-06-21 10:12:24 +0200292 ApmPrivateSubmodules(std::unique_ptr<CustomProcessing> capture_post_processor,
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100293 std::unique_ptr<CustomProcessing> render_pre_processor,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200294 rtc::scoped_refptr<EchoDetector> echo_detector,
295 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Sam Zackrissondb389722018-06-21 10:12:24 +0200296 : echo_detector(std::move(echo_detector)),
Alex Loiko5825aa62017-12-18 16:02:40 +0100297 capture_post_processor(std::move(capture_post_processor)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200298 render_pre_processor(std::move(render_pre_processor)),
299 capture_analyzer(std::move(capture_analyzer)) {}
solenberg5e465c32015-12-08 13:22:33 -0800300 // Accessed internally from capture or during initialization
kwiberg88788ad2016-02-19 07:04:49 -0800301 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700302 std::unique_ptr<GainController2> gain_controller2;
peah8271d042016-11-22 07:24:52 -0800303 std::unique_ptr<LowCutFilter> low_cut_filter;
Ivo Creusend1f970d2018-06-14 11:02:03 +0200304 rtc::scoped_refptr<EchoDetector> echo_detector;
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +0200305 std::unique_ptr<EchoControl> echo_controller;
Alex Loiko5825aa62017-12-18 16:02:40 +0100306 std::unique_ptr<CustomProcessing> capture_post_processor;
307 std::unique_ptr<CustomProcessing> render_pre_processor;
Alex Loikob5c9a792018-04-16 16:31:22 +0200308 std::unique_ptr<GainApplier> pre_amplifier;
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200309 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer;
solenberg5e465c32015-12-08 13:22:33 -0800310};
311
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100312AudioProcessingBuilder::AudioProcessingBuilder() = default;
313AudioProcessingBuilder::~AudioProcessingBuilder() = default;
314
315AudioProcessingBuilder& AudioProcessingBuilder::SetCapturePostProcessing(
316 std::unique_ptr<CustomProcessing> capture_post_processing) {
317 capture_post_processing_ = std::move(capture_post_processing);
318 return *this;
319}
320
321AudioProcessingBuilder& AudioProcessingBuilder::SetRenderPreProcessing(
322 std::unique_ptr<CustomProcessing> render_pre_processing) {
323 render_pre_processing_ = std::move(render_pre_processing);
324 return *this;
325}
326
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200327AudioProcessingBuilder& AudioProcessingBuilder::SetCaptureAnalyzer(
328 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) {
329 capture_analyzer_ = std::move(capture_analyzer);
330 return *this;
331}
332
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100333AudioProcessingBuilder& AudioProcessingBuilder::SetEchoControlFactory(
334 std::unique_ptr<EchoControlFactory> echo_control_factory) {
335 echo_control_factory_ = std::move(echo_control_factory);
336 return *this;
337}
338
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100339AudioProcessingBuilder& AudioProcessingBuilder::SetEchoDetector(
Ivo Creusend1f970d2018-06-14 11:02:03 +0200340 rtc::scoped_refptr<EchoDetector> echo_detector) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100341 echo_detector_ = std::move(echo_detector);
342 return *this;
343}
344
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100345AudioProcessing* AudioProcessingBuilder::Create() {
346 webrtc::Config config;
347 return Create(config);
348}
349
350AudioProcessing* AudioProcessingBuilder::Create(const webrtc::Config& config) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100351 AudioProcessingImpl* apm = new rtc::RefCountedObject<AudioProcessingImpl>(
352 config, std::move(capture_post_processing_),
353 std::move(render_pre_processing_), std::move(echo_control_factory_),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200354 std::move(echo_detector_), std::move(capture_analyzer_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100355 if (apm->Initialize() != AudioProcessing::kNoError) {
356 delete apm;
357 apm = nullptr;
358 }
359 return apm;
Ivo Creusen5ec7e122017-12-22 11:35:59 +0100360}
361
peah88ac8532016-09-12 16:47:25 -0700362AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200363 : AudioProcessingImpl(config, nullptr, nullptr, nullptr, nullptr, nullptr) {
364}
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000365
Per Åhgren13735822018-02-12 21:42:56 +0100366int AudioProcessingImpl::instance_count_ = 0;
367
Sam Zackrisson0beac582017-09-25 12:04:02 +0200368AudioProcessingImpl::AudioProcessingImpl(
369 const webrtc::Config& config,
Alex Loiko5825aa62017-12-18 16:02:40 +0100370 std::unique_ptr<CustomProcessing> capture_post_processor,
371 std::unique_ptr<CustomProcessing> render_pre_processor,
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200372 std::unique_ptr<EchoControlFactory> echo_control_factory,
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200373 rtc::scoped_refptr<EchoDetector> echo_detector,
374 std::unique_ptr<CustomAudioAnalyzer> capture_analyzer)
Per Åhgren13735822018-02-12 21:42:56 +0100375 : data_dumper_(
376 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko73ec0192018-05-15 10:52:28 +0200377 capture_runtime_settings_(kRuntimeSettingQueueSize),
378 render_runtime_settings_(kRuntimeSettingQueueSize),
379 capture_runtime_settings_enqueuer_(&capture_runtime_settings_),
380 render_runtime_settings_enqueuer_(&render_runtime_settings_),
Per Åhgren13735822018-02-12 21:42:56 +0100381 high_pass_filter_impl_(new HighPassFilterImpl(this)),
Gustaf Ullberg002ef282017-10-12 15:13:17 +0200382 echo_control_factory_(std::move(echo_control_factory)),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200383 submodule_states_(!!capture_post_processor,
384 !!render_pre_processor,
385 !!capture_analyzer),
peah8271d042016-11-22 07:24:52 -0800386 public_submodules_(new ApmPublicSubmodules()),
Sam Zackrisson0beac582017-09-25 12:04:02 +0200387 private_submodules_(
Sam Zackrissondb389722018-06-21 10:12:24 +0200388 new ApmPrivateSubmodules(std::move(capture_post_processor),
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100389 std::move(render_pre_processor),
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200390 std::move(echo_detector),
391 std::move(capture_analyzer))),
peahdf3efa82015-11-28 12:35:15 -0800392 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800393 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000394#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alex Loiko64cb83b2018-07-02 13:38:19 +0200395 false,
396 false,
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700397 false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000398#else
Alex Loiko64cb83b2018-07-02 13:38:19 +0200399 config.Get<ExperimentalAgc>().enabled,
400 config.Get<ExperimentalAgc>().enabled_agc2_level_estimator,
Alex Loiko9489c3a2018-08-09 15:04:24 +0200401 config.Get<ExperimentalAgc>().digital_adaptive_disabled),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000402#endif
andrew1c7075f2015-06-24 18:14:14 -0700403#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200404 capture_(false),
andrew1c7075f2015-06-24 18:14:14 -0700405#else
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200406 capture_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700407#endif
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200408 capture_nonlocked_(config.Get<Intelligibility>().enabled) {
peahdf3efa82015-11-28 12:35:15 -0800409 {
410 rtc::CritScope cs_render(&crit_render_);
411 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000412
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200413 // Mark Echo Controller enabled if a factory is injected.
Sam Zackrisson2a959d92018-07-23 14:48:07 +0000414 capture_nonlocked_.echo_controller_enabled =
415 static_cast<bool>(echo_control_factory_);
Gustaf Ullbergce045ac2017-10-16 13:49:04 +0200416
peahb624d8c2016-03-05 03:01:14 -0800417 public_submodules_->echo_cancellation.reset(
peahb58a1582016-03-15 09:34:24 -0700418 new EchoCancellationImpl(&crit_render_, &crit_capture_));
peahbb9edbd2016-03-10 12:54:25 -0800419 public_submodules_->echo_control_mobile.reset(
peah253534d2016-03-15 04:32:28 -0700420 new EchoControlMobileImpl(&crit_render_, &crit_capture_));
Sam Zackrisson74ed7342018-08-16 10:54:07 +0200421 public_submodules_->echo_cancellation_proxy.reset(new EchoCancellationProxy(
422 this, public_submodules_->echo_cancellation.get()));
423 public_submodules_->echo_control_mobile_proxy.reset(
424 new EchoControlMobileProxy(
425 this, public_submodules_->echo_control_mobile.get()));
peahbfa97112016-03-10 21:09:04 -0800426 public_submodules_->gain_control.reset(
Alex Loiko80c0f062018-06-19 17:09:43 +0200427 new GainControlImpl(&crit_render_, &crit_capture_));
solenberg949028f2015-12-15 11:39:38 -0800428 public_submodules_->level_estimator.reset(
429 new LevelEstimatorImpl(&crit_capture_));
solenberg5e465c32015-12-08 13:22:33 -0800430 public_submodules_->noise_suppression.reset(
431 new NoiseSuppressionImpl(&crit_capture_));
solenberga29386c2015-12-16 03:31:12 -0800432 public_submodules_->voice_detection.reset(
433 new VoiceDetectionImpl(&crit_capture_));
peahbe615622016-02-13 16:40:47 -0800434 public_submodules_->gain_control_for_experimental_agc.reset(
peahbfa97112016-03-10 21:09:04 -0800435 new GainControlForExperimentalAgc(
436 public_submodules_->gain_control.get(), &crit_capture_));
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100437
438 // If no echo detector is injected, use the ResidualEchoDetector.
439 if (!private_submodules_->echo_detector) {
Ivo Creusend1f970d2018-06-14 11:02:03 +0200440 private_submodules_->echo_detector =
441 new rtc::RefCountedObject<ResidualEchoDetector>();
Ivo Creusen09fa4b02018-01-11 16:08:54 +0100442 }
peahca4cac72016-06-29 15:26:12 -0700443
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200444 // TODO(alessiob): Move the injected gain controller once injection is
445 // implemented.
446 private_submodules_->gain_controller2.reset(new GainController2());
447
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200448 RTC_LOG(LS_INFO) << "Capture analyzer activated: "
449 << !!private_submodules_->capture_analyzer
450 << "\nCapture post processor activated: "
Jonas Olsson645b0272018-02-15 15:16:27 +0100451 << !!private_submodules_->capture_post_processor
452 << "\nRender pre processor activated: "
Alex Loiko5825aa62017-12-18 16:02:40 +0100453 << !!private_submodules_->render_pre_processor;
peahdf3efa82015-11-28 12:35:15 -0800454 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000455
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000456 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000457}
458
459AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800460 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800461 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800462 private_submodules_->agc_manager.reset();
463 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800464 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000465}
466
niklase@google.com470e71d2011-07-07 08:21:25 +0000467int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800468 // Run in a single-threaded manner during initialization.
469 rtc::CritScope cs_render(&crit_render_);
470 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000471 return InitializeLocked();
472}
473
peahde65ddc2016-09-16 15:02:15 -0700474int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
475 int capture_output_sample_rate_hz,
476 int render_input_sample_rate_hz,
477 ChannelLayout capture_input_layout,
478 ChannelLayout capture_output_layout,
479 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700480 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700481 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
482 LayoutHasKeyboard(capture_input_layout)},
483 {capture_output_sample_rate_hz,
484 ChannelsFromLayout(capture_output_layout),
485 LayoutHasKeyboard(capture_output_layout)},
486 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
487 LayoutHasKeyboard(render_input_layout)},
488 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
489 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700490
491 return Initialize(processing_config);
492}
493
494int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800495 // Run in a single-threaded manner during initialization.
496 rtc::CritScope cs_render(&crit_render_);
497 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700498 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000499}
500
peahdf3efa82015-11-28 12:35:15 -0800501int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800502 const ProcessingConfig& processing_config) {
peah2ace3f92016-09-10 04:42:27 -0700503 return MaybeInitialize(processing_config, false);
peah81b9bfe2015-11-27 02:47:28 -0800504}
505
peahdf3efa82015-11-28 12:35:15 -0800506int AudioProcessingImpl::MaybeInitializeCapture(
peah2ace3f92016-09-10 04:42:27 -0700507 const ProcessingConfig& processing_config,
508 bool force_initialization) {
509 return MaybeInitialize(processing_config, force_initialization);
peah81b9bfe2015-11-27 02:47:28 -0800510}
511
peah192164e2015-11-17 02:16:45 -0800512// Calls InitializeLocked() if any of the audio parameters have changed from
peahdf3efa82015-11-28 12:35:15 -0800513// their current values (needs to be called while holding the crit_render_lock).
514int AudioProcessingImpl::MaybeInitialize(
peah2ace3f92016-09-10 04:42:27 -0700515 const ProcessingConfig& processing_config,
516 bool force_initialization) {
peahdf3efa82015-11-28 12:35:15 -0800517 // Called from both threads. Thread check is therefore not possible.
peah2ace3f92016-09-10 04:42:27 -0700518 if (processing_config == formats_.api_format && !force_initialization) {
peah192164e2015-11-17 02:16:45 -0800519 return kNoError;
520 }
peahdf3efa82015-11-28 12:35:15 -0800521
522 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800523 return InitializeLocked(processing_config);
524}
525
niklase@google.com470e71d2011-07-07 08:21:25 +0000526int AudioProcessingImpl::InitializeLocked() {
Per Åhgren4bdced52017-06-27 16:00:38 +0200527 UpdateActiveSubmoduleStates();
528
peahde65ddc2016-09-16 15:02:15 -0700529 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800530 formats_.api_format.reverse_output_stream().num_frames() == 0
peahde65ddc2016-09-16 15:02:15 -0700531 ? formats_.render_processing_format.num_frames()
peahdf3efa82015-11-28 12:35:15 -0800532 : formats_.api_format.reverse_output_stream().num_frames();
533 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
534 render_.render_audio.reset(new AudioBuffer(
535 formats_.api_format.reverse_input_stream().num_frames(),
536 formats_.api_format.reverse_input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700537 formats_.render_processing_format.num_frames(),
538 formats_.render_processing_format.num_channels(),
539 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700540 if (formats_.api_format.reverse_input_stream() !=
541 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800542 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800543 formats_.api_format.reverse_input_stream().num_channels(),
544 formats_.api_format.reverse_input_stream().num_frames(),
545 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800546 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700547 } else {
peahdf3efa82015-11-28 12:35:15 -0800548 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700549 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700550 } else {
peahdf3efa82015-11-28 12:35:15 -0800551 render_.render_audio.reset(nullptr);
552 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700553 }
peahce4d9152017-05-19 01:28:05 -0700554
peahdf3efa82015-11-28 12:35:15 -0800555 capture_.capture_audio.reset(
556 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
557 formats_.api_format.input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700558 capture_nonlocked_.capture_processing_format.num_frames(),
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200559 formats_.api_format.output_stream().num_channels(),
peahdf3efa82015-11-28 12:35:15 -0800560 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000561
peahde65ddc2016-09-16 15:02:15 -0700562 public_submodules_->echo_cancellation->Initialize(
563 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
564 num_proc_channels());
peah764e3642016-10-22 05:04:30 -0700565 AllocateRenderQueue();
566
ivoc3e9a5372016-10-28 07:55:33 -0700567 int success = public_submodules_->echo_cancellation->enable_metrics(true);
568 RTC_DCHECK_EQ(0, success);
569 success = public_submodules_->echo_cancellation->enable_delay_logging(true);
570 RTC_DCHECK_EQ(0, success);
peahde65ddc2016-09-16 15:02:15 -0700571 public_submodules_->echo_control_mobile->Initialize(
572 proc_split_sample_rate_hz(), num_reverse_channels(),
573 num_output_channels());
peah135259a2016-10-28 03:12:11 -0700574
575 public_submodules_->gain_control->Initialize(num_proc_channels(),
576 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700577 if (constants_.use_experimental_agc) {
578 if (!private_submodules_->agc_manager.get()) {
579 private_submodules_->agc_manager.reset(new AgcManagerDirect(
580 public_submodules_->gain_control.get(),
581 public_submodules_->gain_control_for_experimental_agc.get(),
Alex Loiko64cb83b2018-07-02 13:38:19 +0200582 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min,
583 constants_.use_experimental_agc_agc2_level_estimation,
584 constants_.use_experimental_agc_agc2_digital_adaptive));
peahde65ddc2016-09-16 15:02:15 -0700585 }
586 private_submodules_->agc_manager->Initialize();
587 private_submodules_->agc_manager->SetCaptureMuted(
588 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700589 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700590 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200591 InitializeTransient();
peah1bcfce52016-08-26 07:16:04 -0700592#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -0700593 InitializeIntelligibility();
peah1bcfce52016-08-26 07:16:04 -0700594#endif
peah8271d042016-11-22 07:24:52 -0800595 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700596 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
597 proc_sample_rate_hz());
598 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
599 public_submodules_->level_estimator->Initialize();
ivoc9f4a4a02016-10-28 05:39:16 -0700600 InitializeResidualEchoDetector();
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +0200601 InitializeEchoController();
alessiob3ec96df2017-05-22 06:57:06 -0700602 InitializeGainController2();
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +0200603 InitializeAnalyzer();
Sam Zackrisson0beac582017-09-25 12:04:02 +0200604 InitializePostProcessor();
Alex Loiko5825aa62017-12-18 16:02:40 +0100605 InitializePreProcessor();
solenberg70f99032015-12-08 11:07:32 -0800606
aleloi868f32f2017-05-23 07:20:05 -0700607 if (aec_dump_) {
Minyue Li656d6092018-08-10 15:38:52 +0200608 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -0700609 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000610 return kNoError;
611}
612
Michael Graczyk86c6d332015-07-23 11:41:39 -0700613int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Åhgren4bdced52017-06-27 16:00:38 +0200614 UpdateActiveSubmoduleStates();
615
Michael Graczyk86c6d332015-07-23 11:41:39 -0700616 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700617 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
618 return kBadSampleRateError;
619 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000620 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700621
Peter Kasting69558702016-01-12 16:26:35 -0800622 const size_t num_in_channels = config.input_stream().num_channels();
623 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700624
625 // Need at least one input channel.
626 // Need either one output channel or as many outputs as there are inputs.
627 if (num_in_channels == 0 ||
628 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700629 return kBadNumberChannelsError;
630 }
631
peahdf3efa82015-11-28 12:35:15 -0800632 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000633
peahde65ddc2016-09-16 15:02:15 -0700634 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700635 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700636 formats_.api_format.output_stream().sample_rate_hz()),
637 submodule_states_.CaptureMultiBandSubModulesActive() ||
638 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000639
peahde65ddc2016-09-16 15:02:15 -0700640 capture_nonlocked_.capture_processing_format =
641 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700642
peah2ce640f2017-04-07 03:57:48 -0700643 int render_processing_rate;
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200644 if (!capture_nonlocked_.echo_controller_enabled) {
peah2ce640f2017-04-07 03:57:48 -0700645 render_processing_rate = FindNativeProcessRateToUse(
646 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
647 formats_.api_format.reverse_output_stream().sample_rate_hz()),
648 submodule_states_.CaptureMultiBandSubModulesActive() ||
649 submodule_states_.RenderMultiBandSubModulesActive());
650 } else {
651 render_processing_rate = capture_processing_rate;
652 }
653
aluebseb3603b2016-04-20 15:27:58 -0700654 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
655 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700656 if (render_processing_rate > kSampleRate32kHz &&
Gustaf Ullbergbd83b912017-10-18 12:32:42 +0200657 !capture_nonlocked_.echo_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -0700658 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
659 ? kSampleRate32kHz
660 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700661 }
peah2ce640f2017-04-07 03:57:48 -0700662
peahde65ddc2016-09-16 15:02:15 -0700663 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700664 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700665 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
666 kSampleRate8kHz) {
667 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000668 } else {
peahde65ddc2016-09-16 15:02:15 -0700669 render_processing_rate =
670 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000671 }
672
peahde65ddc2016-09-16 15:02:15 -0700673 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000674 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700675 if (submodule_states_.RenderMultiBandSubModulesActive()) {
676 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
677 } else {
678 formats_.render_processing_format = StreamConfig(
679 formats_.api_format.reverse_input_stream().sample_rate_hz(),
680 formats_.api_format.reverse_input_stream().num_channels());
681 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000682
peahde65ddc2016-09-16 15:02:15 -0700683 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
684 kSampleRate32kHz ||
685 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
686 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800687 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000688 } else {
peahdf3efa82015-11-28 12:35:15 -0800689 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700690 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000691 }
692
693 return InitializeLocked();
694}
695
peah88ac8532016-09-12 16:47:25 -0700696void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peahc19f3122016-10-07 14:54:10 -0700697 config_ = config;
peah88ac8532016-09-12 16:47:25 -0700698
peah88ac8532016-09-12 16:47:25 -0700699 // Run in a single-threaded manner when applying the settings.
700 rtc::CritScope cs_render(&crit_render_);
701 rtc::CritScope cs_capture(&crit_capture_);
702
Sam Zackrissonb3b47ad2018-08-17 16:26:14 +0200703 static_cast<EchoCancellation*>(public_submodules_->echo_cancellation.get())
704 ->Enable(config_.echo_canceller.enabled &&
705 !config_.echo_canceller.mobile_mode);
706 static_cast<EchoControlMobile*>(public_submodules_->echo_control_mobile.get())
707 ->Enable(config_.echo_canceller.enabled &&
708 config_.echo_canceller.mobile_mode);
709
peah8271d042016-11-22 07:24:52 -0800710 InitializeLowCutFilter();
711
Mirko Bonadei675513b2017-11-09 11:09:25 +0100712 RTC_LOG(LS_INFO) << "Highpass filter activated: "
713 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800714
Sam Zackrissonab1aee02018-03-05 15:59:06 +0100715 const bool config_ok = GainController2::Validate(config_.gain_controller2);
alessiob3ec96df2017-05-22 06:57:06 -0700716 if (!config_ok) {
Jonas Olsson645b0272018-02-15 15:16:27 +0100717 RTC_LOG(LS_ERROR) << "AudioProcessing module config error\n"
718 "Gain Controller 2: "
Mirko Bonadei675513b2017-11-09 11:09:25 +0100719 << GainController2::ToString(config_.gain_controller2)
Jonas Olsson645b0272018-02-15 15:16:27 +0100720 << "\nReverting to default parameter set";
alessiob3ec96df2017-05-22 06:57:06 -0700721 config_.gain_controller2 = AudioProcessing::Config::GainController2();
722 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200723 InitializeGainController2();
Alex Loikob5c9a792018-04-16 16:31:22 +0200724 InitializePreAmplifier();
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200725 private_submodules_->gain_controller2->ApplyConfig(config_.gain_controller2);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100726 RTC_LOG(LS_INFO) << "Gain Controller 2 activated: "
727 << config_.gain_controller2.enabled;
Alex Loiko5feb30e2018-04-16 13:52:32 +0200728 RTC_LOG(LS_INFO) << "Pre-amplifier activated: "
729 << config_.pre_amplifier.enabled;
peah88ac8532016-09-12 16:47:25 -0700730}
731
732void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800733 // Run in a single-threaded manner when setting the extra options.
734 rtc::CritScope cs_render(&crit_render_);
735 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000736
peahb624d8c2016-03-05 03:01:14 -0800737 public_submodules_->echo_cancellation->SetExtraOptions(config);
738
peahdf3efa82015-11-28 12:35:15 -0800739 if (capture_.transient_suppressor_enabled !=
740 config.Get<ExperimentalNs>().enabled) {
741 capture_.transient_suppressor_enabled =
742 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000743 InitializeTransient();
744 }
aluebs2a346882016-01-11 18:04:30 -0800745
peah1bcfce52016-08-26 07:16:04 -0700746#if WEBRTC_INTELLIGIBILITY_ENHANCER
alessiob3ec96df2017-05-22 06:57:06 -0700747 if (capture_nonlocked_.intelligibility_enabled !=
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700748 config.Get<Intelligibility>().enabled) {
749 capture_nonlocked_.intelligibility_enabled =
750 config.Get<Intelligibility>().enabled;
751 InitializeIntelligibility();
752 }
peah1bcfce52016-08-26 07:16:04 -0700753#endif
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000754}
755
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000756int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800757 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700758 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000759}
760
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000761int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800762 // Used as callback from submodules, hence locking is not allowed.
763 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000764}
765
Peter Kasting69558702016-01-12 16:26:35 -0800766size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800767 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700768 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000769}
770
Peter Kasting69558702016-01-12 16:26:35 -0800771size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800772 // Used as callback from submodules, hence locking is not allowed.
773 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000774}
775
Peter Kasting69558702016-01-12 16:26:35 -0800776size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800777 // Used as callback from submodules, hence locking is not allowed.
Sam Zackrisson9394f6f2018-06-14 10:11:35 +0200778 return capture_nonlocked_.echo_controller_enabled ? 1 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800779}
780
Peter Kasting69558702016-01-12 16:26:35 -0800781size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800782 // Used as callback from submodules, hence locking is not allowed.
783 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000784}
785
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000786void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800787 rtc::CritScope cs(&crit_capture_);
788 capture_.output_will_be_muted = muted;
789 if (private_submodules_->agc_manager.get()) {
790 private_submodules_->agc_manager->SetCaptureMuted(
791 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000792 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000793}
794
Alessio Bazzicac054e782018-04-16 12:10:09 +0200795void AudioProcessingImpl::SetRuntimeSetting(RuntimeSetting setting) {
Alex Loiko73ec0192018-05-15 10:52:28 +0200796 switch (setting.type()) {
797 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
798 render_runtime_settings_enqueuer_.Enqueue(setting);
799 return;
800 case RuntimeSetting::Type::kNotSpecified:
801 RTC_NOTREACHED();
802 return;
803 case RuntimeSetting::Type::kCapturePreGain:
804 capture_runtime_settings_enqueuer_.Enqueue(setting);
805 return;
806 }
807 // The language allows the enum to have a non-enumerator
808 // value. Check that this doesn't happen.
809 RTC_NOTREACHED();
Alessio Bazzicac054e782018-04-16 12:10:09 +0200810}
811
812AudioProcessingImpl::RuntimeSettingEnqueuer::RuntimeSettingEnqueuer(
813 SwapQueue<RuntimeSetting>* runtime_settings)
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200814 : runtime_settings_(*runtime_settings) {
815 RTC_DCHECK(runtime_settings);
Alessio Bazzicac054e782018-04-16 12:10:09 +0200816}
817
818AudioProcessingImpl::RuntimeSettingEnqueuer::~RuntimeSettingEnqueuer() =
819 default;
820
821void AudioProcessingImpl::RuntimeSettingEnqueuer::Enqueue(
822 RuntimeSetting setting) {
823 size_t remaining_attempts = 10;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200824 while (!runtime_settings_.Insert(&setting) && remaining_attempts-- > 0) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200825 RuntimeSetting setting_to_discard;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200826 if (runtime_settings_.Remove(&setting_to_discard))
Alessio Bazzicac054e782018-04-16 12:10:09 +0200827 RTC_LOG(LS_ERROR)
828 << "The runtime settings queue is full. Oldest setting discarded.";
829 }
830 if (remaining_attempts == 0)
831 RTC_LOG(LS_ERROR) << "Cannot enqueue a new runtime setting.";
832}
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000833
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000834int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700835 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000836 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000837 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000838 int output_sample_rate_hz,
839 ChannelLayout output_layout,
840 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800841 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800842 StreamConfig input_stream;
843 StreamConfig output_stream;
844 {
845 // Access the formats_.api_format.input_stream beneath the capture lock.
846 // The lock must be released as it is later required in the call
847 // to ProcessStream(,,,);
848 rtc::CritScope cs(&crit_capture_);
849 input_stream = formats_.api_format.input_stream();
850 output_stream = formats_.api_format.output_stream();
851 }
852
Michael Graczyk86c6d332015-07-23 11:41:39 -0700853 input_stream.set_sample_rate_hz(input_sample_rate_hz);
854 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
855 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700856 output_stream.set_sample_rate_hz(output_sample_rate_hz);
857 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
858 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
859
860 if (samples_per_channel != input_stream.num_frames()) {
861 return kBadDataLengthError;
862 }
863 return ProcessStream(src, input_stream, output_stream, dest);
864}
865
866int AudioProcessingImpl::ProcessStream(const float* const* src,
867 const StreamConfig& input_config,
868 const StreamConfig& output_config,
869 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800870 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800871 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700872 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800873 {
874 // Acquire the capture lock in order to safely call the function
875 // that retrieves the render side data. This function accesses apm
876 // getters that need the capture lock held when being called.
877 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700878 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800879
880 if (!src || !dest) {
881 return kNullPointerError;
882 }
883
884 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700885 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000886 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000887
Michael Graczyk86c6d332015-07-23 11:41:39 -0700888 processing_config.input_stream() = input_config;
889 processing_config.output_stream() = output_config;
890
peahdf3efa82015-11-28 12:35:15 -0800891 {
892 // Do conditional reinitialization.
893 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -0700894 RETURN_ON_ERR(
895 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -0800896 }
897 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700898 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
899 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000900
aleloi868f32f2017-05-23 07:20:05 -0700901 if (aec_dump_) {
902 RecordUnprocessedCaptureStream(src);
903 }
904
peahdf3efa82015-11-28 12:35:15 -0800905 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700906 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800907 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000908
aleloi868f32f2017-05-23 07:20:05 -0700909 if (aec_dump_) {
910 RecordProcessedCaptureStream(dest);
911 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000912 return kNoError;
913}
914
Alex Loiko73ec0192018-05-15 10:52:28 +0200915void AudioProcessingImpl::HandleCaptureRuntimeSettings() {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200916 RuntimeSetting setting;
Alex Loiko73ec0192018-05-15 10:52:28 +0200917 while (capture_runtime_settings_.Remove(&setting)) {
Alessio Bazzicac054e782018-04-16 12:10:09 +0200918 switch (setting.type()) {
919 case RuntimeSetting::Type::kCapturePreGain:
Alex Loikob5c9a792018-04-16 16:31:22 +0200920 if (config_.pre_amplifier.enabled) {
921 float value;
922 setting.GetFloat(&value);
923 private_submodules_->pre_amplifier->SetGainFactor(value);
924 }
925 // TODO(bugs.chromium.org/9138): Log setting handling by Aec Dump.
Alessio Bazzicac054e782018-04-16 12:10:09 +0200926 break;
Alex Loiko73ec0192018-05-15 10:52:28 +0200927 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
928 RTC_NOTREACHED();
929 break;
930 case RuntimeSetting::Type::kNotSpecified:
931 RTC_NOTREACHED();
932 break;
933 }
934 }
935}
936
937void AudioProcessingImpl::HandleRenderRuntimeSettings() {
938 RuntimeSetting setting;
939 while (render_runtime_settings_.Remove(&setting)) {
940 switch (setting.type()) {
941 case RuntimeSetting::Type::kCustomRenderProcessingRuntimeSetting:
942 if (private_submodules_->render_pre_processor) {
943 private_submodules_->render_pre_processor->SetRuntimeSetting(setting);
944 }
945 break;
946 case RuntimeSetting::Type::kCapturePreGain:
947 RTC_NOTREACHED();
948 break;
Alessio Bazzica33444dc2018-04-20 13:16:55 +0200949 case RuntimeSetting::Type::kNotSpecified:
Alessio Bazzicac054e782018-04-16 12:10:09 +0200950 RTC_NOTREACHED();
951 break;
952 }
953 }
954}
955
peah9e6a2902017-05-15 07:19:21 -0700956void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
peah764e3642016-10-22 05:04:30 -0700957 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
958 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700959 &aec_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700960
kwibergaf476c72016-11-28 15:21:39 -0800961 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -0700962
963 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700964 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700965 // The data queue is full and needs to be emptied.
966 EmptyQueuedRenderAudio();
967
968 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700969 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700970 RTC_DCHECK(result);
971 }
972
973 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
974 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700975 &aecm_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700976
977 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700978 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700979 // The data queue is full and needs to be emptied.
980 EmptyQueuedRenderAudio();
981
982 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700983 bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700984 RTC_DCHECK(result);
985 }
peah701d6282016-10-25 05:42:20 -0700986
987 if (!constants_.use_experimental_agc) {
988 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
989 // Insert the samples into the queue.
990 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
991 // The data queue is full and needs to be emptied.
992 EmptyQueuedRenderAudio();
993
994 // Retry the insert (should always work).
995 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
996 RTC_DCHECK(result);
997 }
998 }
peah9e6a2902017-05-15 07:19:21 -0700999}
ivoc9f4a4a02016-10-28 05:39:16 -07001000
peah9e6a2902017-05-15 07:19:21 -07001001void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -07001002 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
1003
1004 // Insert the samples into the queue.
1005 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
1006 // The data queue is full and needs to be emptied.
1007 EmptyQueuedRenderAudio();
1008
1009 // Retry the insert (should always work).
1010 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
1011 RTC_DCHECK(result);
1012 }
peah764e3642016-10-22 05:04:30 -07001013}
1014
1015void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -07001016 const size_t new_aec_render_queue_element_max_size =
peah764e3642016-10-22 05:04:30 -07001017 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -07001018 kMaxAllowedValuesOfSamplesPerBand *
peah764e3642016-10-22 05:04:30 -07001019 EchoCancellationImpl::NumCancellersRequired(
1020 num_output_channels(), num_reverse_channels()));
1021
peah701d6282016-10-25 05:42:20 -07001022 const size_t new_aecm_render_queue_element_max_size =
peaha0624602016-10-25 04:45:24 -07001023 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -07001024 kMaxAllowedValuesOfSamplesPerBand *
peaha0624602016-10-25 04:45:24 -07001025 EchoControlMobileImpl::NumCancellersRequired(
1026 num_output_channels(), num_reverse_channels()));
peah764e3642016-10-22 05:04:30 -07001027
peah701d6282016-10-25 05:42:20 -07001028 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -07001029 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -07001030
ivoc9f4a4a02016-10-28 05:39:16 -07001031 const size_t new_red_render_queue_element_max_size =
1032 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
1033
peaha0624602016-10-25 04:45:24 -07001034 // Reallocate the queues if the queue item sizes are too small to fit the
1035 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -07001036 if (aec_render_queue_element_max_size_ <
1037 new_aec_render_queue_element_max_size) {
1038 aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
peah764e3642016-10-22 05:04:30 -07001039
peaha0624602016-10-25 04:45:24 -07001040 std::vector<float> template_queue_element(
peah701d6282016-10-25 05:42:20 -07001041 aec_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001042
peah701d6282016-10-25 05:42:20 -07001043 aec_render_signal_queue_.reset(
peah764e3642016-10-22 05:04:30 -07001044 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1045 kMaxNumFramesToBuffer, template_queue_element,
peaha0624602016-10-25 04:45:24 -07001046 RenderQueueItemVerifier<float>(
peah701d6282016-10-25 05:42:20 -07001047 aec_render_queue_element_max_size_)));
peah764e3642016-10-22 05:04:30 -07001048
peah701d6282016-10-25 05:42:20 -07001049 aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
1050 aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
peah764e3642016-10-22 05:04:30 -07001051 } else {
peah701d6282016-10-25 05:42:20 -07001052 aec_render_signal_queue_->Clear();
peaha0624602016-10-25 04:45:24 -07001053 }
1054
peah701d6282016-10-25 05:42:20 -07001055 if (aecm_render_queue_element_max_size_ <
1056 new_aecm_render_queue_element_max_size) {
1057 aecm_render_queue_element_max_size_ =
1058 new_aecm_render_queue_element_max_size;
peaha0624602016-10-25 04:45:24 -07001059
1060 std::vector<int16_t> template_queue_element(
peah701d6282016-10-25 05:42:20 -07001061 aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001062
peah701d6282016-10-25 05:42:20 -07001063 aecm_render_signal_queue_.reset(
peaha0624602016-10-25 04:45:24 -07001064 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1065 kMaxNumFramesToBuffer, template_queue_element,
1066 RenderQueueItemVerifier<int16_t>(
peah701d6282016-10-25 05:42:20 -07001067 aecm_render_queue_element_max_size_)));
peaha0624602016-10-25 04:45:24 -07001068
peah701d6282016-10-25 05:42:20 -07001069 aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
1070 aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001071 } else {
peah701d6282016-10-25 05:42:20 -07001072 aecm_render_signal_queue_->Clear();
1073 }
1074
1075 if (agc_render_queue_element_max_size_ <
1076 new_agc_render_queue_element_max_size) {
1077 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1078
1079 std::vector<int16_t> template_queue_element(
1080 agc_render_queue_element_max_size_);
1081
1082 agc_render_signal_queue_.reset(
1083 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1084 kMaxNumFramesToBuffer, template_queue_element,
1085 RenderQueueItemVerifier<int16_t>(
1086 agc_render_queue_element_max_size_)));
1087
1088 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1089 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1090 } else {
1091 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001092 }
ivoc9f4a4a02016-10-28 05:39:16 -07001093
1094 if (red_render_queue_element_max_size_ <
1095 new_red_render_queue_element_max_size) {
1096 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1097
1098 std::vector<float> template_queue_element(
1099 red_render_queue_element_max_size_);
1100
1101 red_render_signal_queue_.reset(
1102 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1103 kMaxNumFramesToBuffer, template_queue_element,
1104 RenderQueueItemVerifier<float>(
1105 red_render_queue_element_max_size_)));
1106
1107 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1108 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1109 } else {
1110 red_render_signal_queue_->Clear();
1111 }
peah764e3642016-10-22 05:04:30 -07001112}
1113
1114void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1115 rtc::CritScope cs_capture(&crit_capture_);
peah701d6282016-10-25 05:42:20 -07001116 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -07001117 public_submodules_->echo_cancellation->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001118 aec_capture_queue_buffer_);
peaha0624602016-10-25 04:45:24 -07001119 }
1120
peah701d6282016-10-25 05:42:20 -07001121 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -07001122 public_submodules_->echo_control_mobile->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001123 aecm_capture_queue_buffer_);
1124 }
1125
1126 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1127 public_submodules_->gain_control->ProcessRenderAudio(
1128 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001129 }
ivoc9f4a4a02016-10-28 05:39:16 -07001130
1131 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001132 RTC_DCHECK(private_submodules_->echo_detector);
1133 private_submodules_->echo_detector->AnalyzeRenderAudio(
ivoc9f4a4a02016-10-28 05:39:16 -07001134 red_capture_queue_buffer_);
1135 }
peah764e3642016-10-22 05:04:30 -07001136}
1137
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001138int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001139 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001140 {
1141 // Acquire the capture lock in order to safely call the function
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001142 // that retrieves the render side data. This function accesses APM
peahdf3efa82015-11-28 12:35:15 -08001143 // getters that need the capture lock held when being called.
1144 // The lock needs to be released as
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001145 // public_submodules_->echo_control_mobile->is_enabled() acquires this lock
peahdf3efa82015-11-28 12:35:15 -08001146 // as well.
1147 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001148 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001149 }
peahfa6228e2015-11-16 16:27:42 -08001150
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001151 if (!frame) {
1152 return kNullPointerError;
1153 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001154 // Must be a native rate.
1155 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1156 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001157 frame->sample_rate_hz_ != kSampleRate32kHz &&
1158 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001159 return kBadSampleRateError;
1160 }
peah192164e2015-11-17 02:16:45 -08001161
peahdf3efa82015-11-28 12:35:15 -08001162 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001163 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001164 {
1165 // Aquire lock for the access of api_format.
1166 // The lock is released immediately due to the conditional
1167 // reinitialization.
1168 rtc::CritScope cs_capture(&crit_capture_);
1169 // TODO(ajm): The input and output rates and channels are currently
1170 // constrained to be identical in the int16 interface.
1171 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001172
1173 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001174 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001175 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1176 processing_config.input_stream().set_num_channels(frame->num_channels_);
1177 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1178 processing_config.output_stream().set_num_channels(frame->num_channels_);
1179
peahdf3efa82015-11-28 12:35:15 -08001180 {
1181 // Do conditional reinitialization.
1182 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -07001183 RETURN_ON_ERR(
1184 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -08001185 }
1186 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001187 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001188 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 return kBadDataLengthError;
1190 }
1191
aleloi868f32f2017-05-23 07:20:05 -07001192 if (aec_dump_) {
1193 RecordUnprocessedCaptureStream(*frame);
1194 }
1195
peahdf3efa82015-11-28 12:35:15 -08001196 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001197 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001198 capture_.capture_audio->InterleaveTo(
peah23ac8b42017-05-23 05:33:56 -07001199 frame, submodule_states_.CaptureMultiBandProcessingActive() ||
1200 submodule_states_.CaptureFullBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001201
aleloi868f32f2017-05-23 07:20:05 -07001202 if (aec_dump_) {
1203 RecordProcessedCaptureStream(*frame);
1204 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001205
1206 return kNoError;
1207}
1208
peahde65ddc2016-09-16 15:02:15 -07001209int AudioProcessingImpl::ProcessCaptureStreamLocked() {
Alex Loiko73ec0192018-05-15 10:52:28 +02001210 HandleCaptureRuntimeSettings();
Alessio Bazzicac054e782018-04-16 12:10:09 +02001211
peahb58a1582016-03-15 09:34:24 -07001212 // Ensure that not both the AEC and AECM are active at the same time.
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001213 // TODO(peah): Simplify once the public API Enable functions for these
1214 // are moved to APM.
peahb58a1582016-03-15 09:34:24 -07001215 RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&
1216 public_submodules_->echo_control_mobile->is_enabled()));
1217
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001218 MaybeUpdateHistograms();
1219
peahde65ddc2016-09-16 15:02:15 -07001220 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001221
Alex Loikob5c9a792018-04-16 16:31:22 +02001222 if (private_submodules_->pre_amplifier) {
1223 private_submodules_->pre_amplifier->ApplyGain(AudioFrameView<float>(
1224 capture_buffer->channels_f(), capture_buffer->num_channels(),
1225 capture_buffer->num_frames()));
1226 }
1227
peah1b08dc32016-12-20 13:45:58 -08001228 capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>(
henrik.lundin290d43a2016-11-29 08:09:09 -08001229 capture_buffer->channels_const()[0],
1230 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001231 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1232 if (log_rms) {
1233 capture_rms_interval_counter_ = 0;
1234 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001235 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1236 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1237 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1238 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001239 }
1240
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001241 if (private_submodules_->echo_controller) {
Per Åhgren88cf0502018-07-16 17:08:41 +02001242 // Detect and flag any change in the analog gain.
1243 int analog_mic_level = gain_control()->stream_analog_level();
1244 capture_.echo_path_gain_change =
1245 capture_.prev_analog_mic_level != analog_mic_level &&
1246 capture_.prev_analog_mic_level != -1;
1247 capture_.prev_analog_mic_level = analog_mic_level;
1248
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001249 private_submodules_->echo_controller->AnalyzeCapture(capture_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001250 }
1251
peahbe615622016-02-13 16:40:47 -08001252 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001253 public_submodules_->gain_control->is_enabled()) {
1254 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001255 capture_buffer->channels()[0], capture_buffer->num_channels(),
1256 capture_nonlocked_.capture_processing_format.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001257 }
1258
peah2ace3f92016-09-10 04:42:27 -07001259 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1260 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001261 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1262 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001263 }
1264
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001265 if (private_submodules_->echo_controller) {
peah522d71b2017-02-23 05:16:26 -08001266 // Force down-mixing of the number of channels after the detection of
1267 // capture signal saturation.
1268 // TODO(peah): Look into ensuring that this kind of tampering with the
1269 // AudioBuffer functionality should not be needed.
1270 capture_buffer->set_num_channels(1);
1271 }
1272
peahe0eae3c2016-12-14 01:16:23 -08001273 // TODO(peah): Move the AEC3 low-cut filter to this place.
1274 if (private_submodules_->low_cut_filter &&
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001275 !private_submodules_->echo_controller) {
peah8271d042016-11-22 07:24:52 -08001276 private_submodules_->low_cut_filter->Process(capture_buffer);
1277 }
peahde65ddc2016-09-16 15:02:15 -07001278 RETURN_ON_ERR(
1279 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1280 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001281
1282 // Ensure that the stream delay was set before the call to the
1283 // AEC ProcessCaptureAudio function.
1284 if (public_submodules_->echo_cancellation->is_enabled() &&
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001285 !private_submodules_->echo_controller && !was_stream_delay_set()) {
peahb58a1582016-03-15 09:34:24 -07001286 return AudioProcessing::kStreamParameterNotSetError;
1287 }
1288
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001289 if (private_submodules_->echo_controller) {
Per Åhgren13735822018-02-12 21:42:56 +01001290 data_dumper_->DumpRaw("stream_delay", stream_delay_ms());
1291
Per Åhgrend0fa8202018-04-18 09:35:13 +02001292 if (was_stream_delay_set()) {
1293 private_submodules_->echo_controller->SetAudioBufferDelay(
1294 stream_delay_ms());
1295 }
1296
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001297 private_submodules_->echo_controller->ProcessCapture(
peah67995532017-04-10 14:12:41 -07001298 capture_buffer, capture_.echo_path_gain_change);
peah61202ac2017-02-06 03:39:42 -08001299 } else {
1300 RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(
1301 capture_buffer, stream_delay_ms()));
peahe0eae3c2016-12-14 01:16:23 -08001302 }
1303
peahdf3efa82015-11-28 12:35:15 -08001304 if (public_submodules_->echo_control_mobile->is_enabled() &&
1305 public_submodules_->noise_suppression->is_enabled()) {
peahde65ddc2016-09-16 15:02:15 -07001306 capture_buffer->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +00001307 }
peahde65ddc2016-09-16 15:02:15 -07001308 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah1bcfce52016-08-26 07:16:04 -07001309#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001310 if (capture_nonlocked_.intelligibility_enabled) {
aluebsc466bad2016-02-10 12:03:00 -08001311 RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());
Sam Zackrissonab1aee02018-03-05 15:59:06 +01001312 const int gain_db =
1313 public_submodules_->gain_control->is_enabled()
1314 ? public_submodules_->gain_control->compression_gain_db()
1315 : 0;
1316 const float gain = DbToRatio(gain_db);
aluebsc466bad2016-02-10 12:03:00 -08001317 public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -07001318 public_submodules_->noise_suppression->NoiseEstimate(), gain);
aluebsc466bad2016-02-10 12:03:00 -08001319 }
peah1bcfce52016-08-26 07:16:04 -07001320#endif
peah253534d2016-03-15 04:32:28 -07001321
1322 // Ensure that the stream delay was set before the call to the
1323 // AECM ProcessCaptureAudio function.
1324 if (public_submodules_->echo_control_mobile->is_enabled() &&
1325 !was_stream_delay_set()) {
1326 return AudioProcessing::kStreamParameterNotSetError;
1327 }
1328
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001329 if (!(private_submodules_->echo_controller ||
1330 public_submodules_->echo_cancellation->is_enabled())) {
Per Åhgren46537a32017-06-07 10:08:10 +02001331 RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(
1332 capture_buffer, stream_delay_ms()));
1333 }
ivoc9f4a4a02016-10-28 05:39:16 -07001334
peahde65ddc2016-09-16 15:02:15 -07001335 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001336
peahbe615622016-02-13 16:40:47 -08001337 if (constants_.use_experimental_agc &&
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02001338 public_submodules_->gain_control->is_enabled()) {
peahdf3efa82015-11-28 12:35:15 -08001339 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001340 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1341 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001342 }
peahb8fbb542016-03-15 02:28:08 -07001343 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001344 capture_buffer, echo_cancellation()->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001345
peah2ace3f92016-09-10 04:42:27 -07001346 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1347 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001348 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1349 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001350 }
1351
peah9e6a2902017-05-15 07:19:21 -07001352 if (config_.residual_echo_detector.enabled) {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001353 RTC_DCHECK(private_submodules_->echo_detector);
1354 private_submodules_->echo_detector->AnalyzeCaptureAudio(
peah9e6a2902017-05-15 07:19:21 -07001355 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
1356 capture_buffer->num_frames()));
1357 }
1358
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001359 // TODO(aluebs): Investigate if the transient suppression placement should be
1360 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001361 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001362 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001363 private_submodules_->agc_manager.get()
1364 ? private_submodules_->agc_manager->voice_probability()
1365 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001366
peahdf3efa82015-11-28 12:35:15 -08001367 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001368 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1369 capture_buffer->num_channels(),
1370 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1371 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1372 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001373 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001374 }
1375
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001376 // Experimental APM sub-module that analyzes |capture_buffer|.
1377 if (private_submodules_->capture_analyzer) {
1378 private_submodules_->capture_analyzer->Analyze(capture_buffer);
1379 }
1380
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001381 if (config_.gain_controller2.enabled) {
Alex Loikoa837dd72018-08-06 16:32:12 +02001382 private_submodules_->gain_controller2->NotifyAnalogLevel(
1383 gain_control()->stream_analog_level());
alessiob3ec96df2017-05-22 06:57:06 -07001384 private_submodules_->gain_controller2->Process(capture_buffer);
1385 }
1386
Sam Zackrisson0beac582017-09-25 12:04:02 +02001387 if (private_submodules_->capture_post_processor) {
1388 private_submodules_->capture_post_processor->Process(capture_buffer);
1389 }
1390
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001391 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001392 public_submodules_->level_estimator->ProcessStream(capture_buffer);
ajm@google.com808e0e02011-08-03 21:08:51 +00001393
peah1b08dc32016-12-20 13:45:58 -08001394 capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>(
1395 capture_buffer->channels_const()[0],
1396 capture_nonlocked_.capture_processing_format.num_frames()));
1397 if (log_rms) {
1398 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1399 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1400 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1401 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1402 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1403 }
1404
peahdf3efa82015-11-28 12:35:15 -08001405 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001406 return kNoError;
1407}
1408
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001409int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001410 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001411 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001412 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001413 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001414 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001415 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001416 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001417 };
1418 if (samples_per_channel != reverse_config.num_frames()) {
1419 return kBadDataLengthError;
1420 }
peahdf3efa82015-11-28 12:35:15 -08001421 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001422}
1423
peahde65ddc2016-09-16 15:02:15 -07001424int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1425 const StreamConfig& input_config,
1426 const StreamConfig& output_config,
1427 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001428 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001429 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001430 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
Alex Loiko5825aa62017-12-18 16:02:40 +01001431 if (submodule_states_.RenderMultiBandProcessingActive() ||
1432 submodule_states_.RenderFullBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001433 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1434 dest);
peah2ace3f92016-09-10 04:42:27 -07001435 } else if (formats_.api_format.reverse_input_stream() !=
1436 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001437 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1438 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001439 } else {
peahde65ddc2016-09-16 15:02:15 -07001440 CopyAudioIfNeeded(src, input_config.num_frames(),
1441 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001442 }
1443
1444 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001445}
1446
peahdf3efa82015-11-28 12:35:15 -08001447int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001448 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001449 const StreamConfig& input_config,
1450 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001451 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001452 return kNullPointerError;
1453 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001454
peahde65ddc2016-09-16 15:02:15 -07001455 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001456 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001457 }
1458
peahdf3efa82015-11-28 12:35:15 -08001459 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001460 processing_config.reverse_input_stream() = input_config;
1461 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001462
peahdf3efa82015-11-28 12:35:15 -08001463 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +02001464 RTC_DCHECK_EQ(input_config.num_frames(),
1465 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001466
aleloi868f32f2017-05-23 07:20:05 -07001467 if (aec_dump_) {
1468 const size_t channel_size =
1469 formats_.api_format.reverse_input_stream().num_frames();
1470 const size_t num_channels =
1471 formats_.api_format.reverse_input_stream().num_channels();
1472 aec_dump_->WriteRenderStreamMessage(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001473 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07001474 }
peahdf3efa82015-11-28 12:35:15 -08001475 render_.render_audio->CopyFrom(src,
1476 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001477 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001478}
1479
1480int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001481 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001482 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001483 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001484 return kNullPointerError;
1485 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001486 // Must be a native rate.
1487 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1488 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001489 frame->sample_rate_hz_ != kSampleRate32kHz &&
1490 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001491 return kBadSampleRateError;
1492 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001493
Michael Graczyk86c6d332015-07-23 11:41:39 -07001494 if (frame->num_channels_ <= 0) {
1495 return kBadNumberChannelsError;
1496 }
1497
peahdf3efa82015-11-28 12:35:15 -08001498 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001499 processing_config.reverse_input_stream().set_sample_rate_hz(
1500 frame->sample_rate_hz_);
1501 processing_config.reverse_input_stream().set_num_channels(
1502 frame->num_channels_);
1503 processing_config.reverse_output_stream().set_sample_rate_hz(
1504 frame->sample_rate_hz_);
1505 processing_config.reverse_output_stream().set_num_channels(
1506 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001507
peahdf3efa82015-11-28 12:35:15 -08001508 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001509 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001510 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001511 return kBadDataLengthError;
1512 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001513
aleloi868f32f2017-05-23 07:20:05 -07001514 if (aec_dump_) {
1515 aec_dump_->WriteRenderStreamMessage(*frame);
1516 }
1517
peahdf3efa82015-11-28 12:35:15 -08001518 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001519 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001520 render_.render_audio->InterleaveTo(
Alex Loiko5825aa62017-12-18 16:02:40 +01001521 frame, submodule_states_.RenderMultiBandProcessingActive() ||
1522 submodule_states_.RenderFullBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001523 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001524}
niklase@google.com470e71d2011-07-07 08:21:25 +00001525
peahde65ddc2016-09-16 15:02:15 -07001526int AudioProcessingImpl::ProcessRenderStreamLocked() {
1527 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001528
Alex Loiko73ec0192018-05-15 10:52:28 +02001529 HandleRenderRuntimeSettings();
1530
Alex Loiko5825aa62017-12-18 16:02:40 +01001531 if (private_submodules_->render_pre_processor) {
1532 private_submodules_->render_pre_processor->Process(render_buffer);
1533 }
1534
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001535 QueueNonbandedRenderAudio(render_buffer);
1536
peah2ace3f92016-09-10 04:42:27 -07001537 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001538 SampleRateSupportsMultiBand(
1539 formats_.render_processing_format.sample_rate_hz())) {
1540 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001541 }
1542
peah1bcfce52016-08-26 07:16:04 -07001543#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001544 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001545 public_submodules_->intelligibility_enhancer->ProcessRenderAudio(
Alejandro Luebsef009252016-09-20 14:51:56 -07001546 render_buffer);
ekmeyerson60d9b332015-08-14 10:35:55 -07001547 }
peah1bcfce52016-08-26 07:16:04 -07001548#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001549
peahce4d9152017-05-19 01:28:05 -07001550 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1551 QueueBandedRenderAudio(render_buffer);
1552 }
1553
Alessio Bazzicad2b97402018-08-09 14:23:11 +02001554 // TODO(peah): Perform the queuing inside QueueRenderAudiuo().
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001555 if (private_submodules_->echo_controller) {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001556 private_submodules_->echo_controller->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001557 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001558
peah2ace3f92016-09-10 04:42:27 -07001559 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001560 SampleRateSupportsMultiBand(
1561 formats_.render_processing_format.sample_rate_hz())) {
1562 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001563 }
1564
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001565 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001566}
1567
1568int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001569 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001570 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001571 capture_.was_stream_delay_set = true;
1572 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001573
niklase@google.com470e71d2011-07-07 08:21:25 +00001574 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001575 delay = 0;
1576 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001577 }
1578
1579 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1580 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001581 delay = 500;
1582 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001583 }
1584
peahdf3efa82015-11-28 12:35:15 -08001585 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001586 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001587}
1588
1589int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001590 // Used as callback from submodules, hence locking is not allowed.
1591 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001592}
1593
1594bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001595 // Used as callback from submodules, hence locking is not allowed.
1596 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001597}
1598
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001599void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001600 rtc::CritScope cs(&crit_capture_);
1601 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001602}
1603
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001604void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001605 rtc::CritScope cs(&crit_capture_);
1606 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001607}
1608
1609int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001610 rtc::CritScope cs(&crit_capture_);
1611 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001612}
1613
aleloi868f32f2017-05-23 07:20:05 -07001614void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1615 RTC_DCHECK(aec_dump);
1616 rtc::CritScope cs_render(&crit_render_);
1617 rtc::CritScope cs_capture(&crit_capture_);
1618
1619 // The previously attached AecDump will be destroyed with the
1620 // 'aec_dump' parameter, which is after locks are released.
1621 aec_dump_.swap(aec_dump);
1622 WriteAecDumpConfigMessage(true);
Minyue Li656d6092018-08-10 15:38:52 +02001623 aec_dump_->WriteInitMessage(formats_.api_format, rtc::TimeUTCMillis());
aleloi868f32f2017-05-23 07:20:05 -07001624}
1625
1626void AudioProcessingImpl::DetachAecDump() {
1627 // The d-tor of a task-queue based AecDump blocks until all pending
1628 // tasks are done. This construction avoids blocking while holding
1629 // the render and capture locks.
1630 std::unique_ptr<AecDump> aec_dump = nullptr;
1631 {
1632 rtc::CritScope cs_render(&crit_render_);
1633 rtc::CritScope cs_capture(&crit_capture_);
1634 aec_dump = std::move(aec_dump_);
1635 }
1636}
1637
Sam Zackrisson4d364492018-03-02 16:03:21 +01001638void AudioProcessingImpl::AttachPlayoutAudioGenerator(
1639 std::unique_ptr<AudioGenerator> audio_generator) {
1640 // TODO(bugs.webrtc.org/8882) Stub.
1641 // Reset internal audio generator with audio_generator.
1642}
1643
1644void AudioProcessingImpl::DetachPlayoutAudioGenerator() {
1645 // TODO(bugs.webrtc.org/8882) Stub.
1646 // Delete audio generator, if one is attached.
1647}
1648
ivoc4e477a12017-01-15 08:29:46 -08001649AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics() {
1650 residual_echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1651 echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1652 echo_return_loss_enhancement.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1653 a_nlp.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1654}
1655
1656AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics(
1657 const AudioProcessingStatistics& other) = default;
1658
1659AudioProcessing::AudioProcessingStatistics::~AudioProcessingStatistics() =
1660 default;
1661
ivoc3e9a5372016-10-28 07:55:33 -07001662// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
1663AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics()
1664 const {
1665 return AudioProcessingStatistics();
1666}
1667
Ivo Creusenae026092017-11-20 13:07:16 +01001668// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
Ivo Creusen56d46092017-11-24 17:29:59 +01001669AudioProcessingStats AudioProcessing::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001670 bool has_remote_tracks) const {
1671 return AudioProcessingStats();
1672}
1673
ivoc3e9a5372016-10-28 07:55:33 -07001674AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics()
1675 const {
1676 AudioProcessingStatistics stats;
1677 EchoCancellation::Metrics metrics;
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001678 if (private_submodules_->echo_controller) {
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001679 rtc::CritScope cs_capture(&crit_capture_);
1680 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1681 float erl = static_cast<float>(ec_metrics.echo_return_loss);
1682 float erle = static_cast<float>(ec_metrics.echo_return_loss_enhancement);
1683 // Instant value will also be used for min, max and average.
1684 stats.echo_return_loss.Set(erl, erl, erl, erl);
1685 stats.echo_return_loss_enhancement.Set(erle, erle, erle, erle);
1686 } else if (public_submodules_->echo_cancellation->GetMetrics(&metrics) ==
1687 Error::kNoError) {
ivocd0a151c2016-11-02 09:14:37 -07001688 stats.a_nlp.Set(metrics.a_nlp);
1689 stats.divergent_filter_fraction = metrics.divergent_filter_fraction;
1690 stats.echo_return_loss.Set(metrics.echo_return_loss);
1691 stats.echo_return_loss_enhancement.Set(
1692 metrics.echo_return_loss_enhancement);
1693 stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss);
1694 }
ivoc9c192b22017-03-16 04:22:14 -07001695 {
1696 rtc::CritScope cs_capture(&crit_capture_);
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001697 RTC_DCHECK(private_submodules_->echo_detector);
1698 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1699 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
ivoc9c192b22017-03-16 04:22:14 -07001700 stats.residual_echo_likelihood_recent_max =
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001701 ed_metrics.echo_likelihood_recent_max;
ivoc9c192b22017-03-16 04:22:14 -07001702 }
ivoc3e9a5372016-10-28 07:55:33 -07001703 public_submodules_->echo_cancellation->GetDelayMetrics(
1704 &stats.delay_median, &stats.delay_standard_deviation,
1705 &stats.fraction_poor_delays);
1706 return stats;
1707}
1708
Ivo Creusen56d46092017-11-24 17:29:59 +01001709AudioProcessingStats AudioProcessingImpl::GetStatistics(
Ivo Creusenae026092017-11-20 13:07:16 +01001710 bool has_remote_tracks) const {
1711 AudioProcessingStats stats;
1712 if (has_remote_tracks) {
1713 EchoCancellation::Metrics metrics;
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001714 if (private_submodules_->echo_controller) {
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001715 rtc::CritScope cs_capture(&crit_capture_);
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001716 auto ec_metrics = private_submodules_->echo_controller->GetMetrics();
1717 stats.echo_return_loss = ec_metrics.echo_return_loss;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001718 stats.echo_return_loss_enhancement =
Gustaf Ullberg09b9fae2017-11-24 13:42:29 +01001719 ec_metrics.echo_return_loss_enhancement;
Per Åhgren83c4a022017-11-27 12:07:09 +01001720 stats.delay_ms = ec_metrics.delay_ms;
Gustaf Ullberg332150d2017-11-22 14:17:39 +01001721 } else if (public_submodules_->echo_cancellation->GetMetrics(&metrics) ==
1722 Error::kNoError) {
Ivo Creusenae026092017-11-20 13:07:16 +01001723 if (metrics.divergent_filter_fraction != -1.0f) {
1724 stats.divergent_filter_fraction =
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +02001725 absl::optional<double>(metrics.divergent_filter_fraction);
Ivo Creusenae026092017-11-20 13:07:16 +01001726 }
1727 if (metrics.echo_return_loss.instant != -100) {
1728 stats.echo_return_loss =
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +02001729 absl::optional<double>(metrics.echo_return_loss.instant);
Ivo Creusenae026092017-11-20 13:07:16 +01001730 }
1731 if (metrics.echo_return_loss_enhancement.instant != -100) {
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +02001732 stats.echo_return_loss_enhancement = absl::optional<double>(
1733 metrics.echo_return_loss_enhancement.instant);
Ivo Creusenae026092017-11-20 13:07:16 +01001734 }
1735 }
1736 if (config_.residual_echo_detector.enabled) {
1737 rtc::CritScope cs_capture(&crit_capture_);
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001738 RTC_DCHECK(private_submodules_->echo_detector);
1739 auto ed_metrics = private_submodules_->echo_detector->GetMetrics();
1740 stats.residual_echo_likelihood = ed_metrics.echo_likelihood;
Ivo Creusenae026092017-11-20 13:07:16 +01001741 stats.residual_echo_likelihood_recent_max =
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001742 ed_metrics.echo_likelihood_recent_max;
Ivo Creusenae026092017-11-20 13:07:16 +01001743 }
1744 int delay_median, delay_std;
1745 float fraction_poor_delays;
1746 if (public_submodules_->echo_cancellation->GetDelayMetrics(
1747 &delay_median, &delay_std, &fraction_poor_delays) ==
1748 Error::kNoError) {
1749 if (delay_median >= 0) {
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +02001750 stats.delay_median_ms = absl::optional<int32_t>(delay_median);
Ivo Creusenae026092017-11-20 13:07:16 +01001751 }
1752 if (delay_std >= 0) {
Danil Chapovalovdb9f7ab2018-06-19 10:50:11 +02001753 stats.delay_standard_deviation_ms = absl::optional<int32_t>(delay_std);
Ivo Creusenae026092017-11-20 13:07:16 +01001754 }
1755 }
1756 }
1757 return stats;
1758}
1759
niklase@google.com470e71d2011-07-07 08:21:25 +00001760EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
Sam Zackrisson74ed7342018-08-16 10:54:07 +02001761 return public_submodules_->echo_cancellation_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001762}
1763
1764EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
Sam Zackrisson74ed7342018-08-16 10:54:07 +02001765 return public_submodules_->echo_control_mobile_proxy.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001766}
1767
1768GainControl* AudioProcessingImpl::gain_control() const {
peahbe615622016-02-13 16:40:47 -08001769 if (constants_.use_experimental_agc) {
1770 return public_submodules_->gain_control_for_experimental_agc.get();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001771 }
peahbfa97112016-03-10 21:09:04 -08001772 return public_submodules_->gain_control.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001773}
1774
1775HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
peah8271d042016-11-22 07:24:52 -08001776 return high_pass_filter_impl_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001777}
1778
1779LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001780 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001781}
1782
1783NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
solenberg5e465c32015-12-08 13:22:33 -08001784 return public_submodules_->noise_suppression.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001785}
1786
1787VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001788 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001789}
1790
peah8271d042016-11-22 07:24:52 -08001791void AudioProcessingImpl::MutateConfig(
1792 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1793 rtc::CritScope cs_render(&crit_render_);
1794 rtc::CritScope cs_capture(&crit_capture_);
1795 mutator(&config_);
1796 ApplyConfig(config_);
1797}
1798
1799AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1800 rtc::CritScope cs_render(&crit_render_);
1801 rtc::CritScope cs_capture(&crit_capture_);
1802 return config_;
1803}
1804
peah2ace3f92016-09-10 04:42:27 -07001805bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1806 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001807 config_.high_pass_filter.enabled,
peah2ace3f92016-09-10 04:42:27 -07001808 public_submodules_->echo_cancellation->is_enabled(),
1809 public_submodules_->echo_control_mobile->is_enabled(),
ivoc9f4a4a02016-10-28 05:39:16 -07001810 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001811 public_submodules_->noise_suppression->is_enabled(),
1812 capture_nonlocked_.intelligibility_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(),
1817 public_submodules_->level_estimator->is_enabled(),
1818 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001819}
1820
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001821
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
ekmeyerson60d9b332015-08-14 10:35:55 -07001833void AudioProcessingImpl::InitializeIntelligibility() {
peah1bcfce52016-08-26 07:16:04 -07001834#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001835 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001836 public_submodules_->intelligibility_enhancer.reset(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -08001837 new IntelligibilityEnhancer(capture_nonlocked_.split_rate,
Alex Luebs57ae8292016-03-09 16:24:34 +01001838 render_.render_audio->num_channels(),
Alejandro Luebsef009252016-09-20 14:51:56 -07001839 render_.render_audio->num_bands(),
Alex Luebs57ae8292016-03-09 16:24:34 +01001840 NoiseSuppressionImpl::num_noise_bins()));
ekmeyerson60d9b332015-08-14 10:35:55 -07001841 }
peah1bcfce52016-08-26 07:16:04 -07001842#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001843}
1844
peah8271d042016-11-22 07:24:52 -08001845void AudioProcessingImpl::InitializeLowCutFilter() {
1846 if (config_.high_pass_filter.enabled) {
1847 private_submodules_->low_cut_filter.reset(
1848 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1849 } else {
1850 private_submodules_->low_cut_filter.reset();
1851 }
1852}
alessiob3ec96df2017-05-22 06:57:06 -07001853
Gustaf Ullberg8eb9c7d2017-10-14 08:28:46 +02001854void AudioProcessingImpl::InitializeEchoController() {
Gustaf Ullberg002ef282017-10-12 15:13:17 +02001855 if (echo_control_factory_) {
1856 private_submodules_->echo_controller =
1857 echo_control_factory_->Create(proc_sample_rate_hz());
peahe0eae3c2016-12-14 01:16:23 -08001858 } else {
Gustaf Ullberg59ff0e22017-10-09 10:20:34 +02001859 private_submodules_->echo_controller.reset();
peahe0eae3c2016-12-14 01:16:23 -08001860 }
1861}
peah8271d042016-11-22 07:24:52 -08001862
alessiob3ec96df2017-05-22 06:57:06 -07001863void AudioProcessingImpl::InitializeGainController2() {
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001864 if (config_.gain_controller2.enabled) {
1865 private_submodules_->gain_controller2->Initialize(proc_sample_rate_hz());
alessiob3ec96df2017-05-22 06:57:06 -07001866 }
1867}
1868
Alex Loikob5c9a792018-04-16 16:31:22 +02001869void AudioProcessingImpl::InitializePreAmplifier() {
1870 if (config_.pre_amplifier.enabled) {
1871 private_submodules_->pre_amplifier.reset(
1872 new GainApplier(true, config_.pre_amplifier.fixed_gain_factor));
1873 } else {
1874 private_submodules_->pre_amplifier.reset();
1875 }
1876}
1877
ivoc9f4a4a02016-10-28 05:39:16 -07001878void AudioProcessingImpl::InitializeResidualEchoDetector() {
Ivo Creusen09fa4b02018-01-11 16:08:54 +01001879 RTC_DCHECK(private_submodules_->echo_detector);
Ivo Creusen647ef092018-03-14 17:13:48 +01001880 private_submodules_->echo_detector->Initialize(
Ivo Creusenb1facc12018-04-12 16:15:58 +02001881 proc_sample_rate_hz(), 1,
1882 formats_.render_processing_format.sample_rate_hz(), 1);
ivoc9f4a4a02016-10-28 05:39:16 -07001883}
1884
Valeriia Nemychnikovaf06eb572018-08-29 10:37:09 +02001885void AudioProcessingImpl::InitializeAnalyzer() {
1886 if (private_submodules_->capture_analyzer) {
1887 private_submodules_->capture_analyzer->Initialize(proc_sample_rate_hz(),
1888 num_proc_channels());
1889 }
1890}
1891
Sam Zackrisson0beac582017-09-25 12:04:02 +02001892void AudioProcessingImpl::InitializePostProcessor() {
1893 if (private_submodules_->capture_post_processor) {
1894 private_submodules_->capture_post_processor->Initialize(
1895 proc_sample_rate_hz(), num_proc_channels());
1896 }
1897}
1898
Alex Loiko5825aa62017-12-18 16:02:40 +01001899void AudioProcessingImpl::InitializePreProcessor() {
1900 if (private_submodules_->render_pre_processor) {
1901 private_submodules_->render_pre_processor->Initialize(
1902 formats_.render_processing_format.sample_rate_hz(),
1903 formats_.render_processing_format.num_channels());
1904 }
1905}
1906
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001907void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001908 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001909
1910 if (echo_cancellation()->is_enabled()) {
Sam Zackrisson2a959d92018-07-23 14:48:07 +00001911 // Activate delay_jumps_ counters if we know echo_cancellation is running.
1912 // If a stream has echo we know that the echo_cancellation is in process.
peahdf3efa82015-11-28 12:35:15 -08001913 if (capture_.stream_delay_jumps == -1 &&
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001914 echo_cancellation()->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001915 capture_.stream_delay_jumps = 0;
1916 }
1917 if (capture_.aec_system_delay_jumps == -1 &&
1918 echo_cancellation()->stream_has_echo()) {
1919 capture_.aec_system_delay_jumps = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001920 }
1921
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001922 // Detect a jump in platform reported system delay and log the difference.
peahdf3efa82015-11-28 12:35:15 -08001923 const int diff_stream_delay_ms =
1924 capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
1925 if (diff_stream_delay_ms > kMinDiffDelayMs &&
1926 capture_.last_stream_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001927 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1928 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
peahdf3efa82015-11-28 12:35:15 -08001929 if (capture_.stream_delay_jumps == -1) {
1930 capture_.stream_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001931 }
peahdf3efa82015-11-28 12:35:15 -08001932 capture_.stream_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001933 }
peahdf3efa82015-11-28 12:35:15 -08001934 capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001935
1936 // Detect a jump in AEC system delay and log the difference.
peah20028c42016-03-04 11:50:54 -08001937 const int samples_per_ms =
peahdf3efa82015-11-28 12:35:15 -08001938 rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
peah20028c42016-03-04 11:50:54 -08001939 RTC_DCHECK_LT(0, samples_per_ms);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001940 const int aec_system_delay_ms =
peah20028c42016-03-04 11:50:54 -08001941 public_submodules_->echo_cancellation->GetSystemDelayInSamples() /
1942 samples_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001943 const int diff_aec_system_delay_ms =
peahdf3efa82015-11-28 12:35:15 -08001944 aec_system_delay_ms - capture_.last_aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001945 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
peahdf3efa82015-11-28 12:35:15 -08001946 capture_.last_aec_system_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001947 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1948 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1949 100);
peahdf3efa82015-11-28 12:35:15 -08001950 if (capture_.aec_system_delay_jumps == -1) {
1951 capture_.aec_system_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001952 }
peahdf3efa82015-11-28 12:35:15 -08001953 capture_.aec_system_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001954 }
peahdf3efa82015-11-28 12:35:15 -08001955 capture_.last_aec_system_delay_ms = aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001956 }
1957}
1958
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001959void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
peahdf3efa82015-11-28 12:35:15 -08001960 // Run in a single-threaded manner.
1961 rtc::CritScope cs_render(&crit_render_);
1962 rtc::CritScope cs_capture(&crit_capture_);
1963
1964 if (capture_.stream_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001965 RTC_HISTOGRAM_ENUMERATION(
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001966 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
peahdf3efa82015-11-28 12:35:15 -08001967 capture_.stream_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001968 }
peahdf3efa82015-11-28 12:35:15 -08001969 capture_.stream_delay_jumps = -1;
1970 capture_.last_stream_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001971
peahdf3efa82015-11-28 12:35:15 -08001972 if (capture_.aec_system_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001973 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1974 capture_.aec_system_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001975 }
peahdf3efa82015-11-28 12:35:15 -08001976 capture_.aec_system_delay_jumps = -1;
1977 capture_.last_aec_system_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001978}
1979
aleloi868f32f2017-05-23 07:20:05 -07001980void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1981 if (!aec_dump_) {
1982 return;
1983 }
1984 std::string experiments_description =
1985 public_submodules_->echo_cancellation->GetExperimentsDescription();
1986 // TODO(peah): Add semicolon-separated concatenations of experiment
1987 // descriptions for other submodules.
aleloi868f32f2017-05-23 07:20:05 -07001988 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1989 experiments_description += "AgcClippingLevelExperiment;";
1990 }
Gustaf Ullbergce045ac2017-10-16 13:49:04 +02001991 if (capture_nonlocked_.echo_controller_enabled) {
1992 experiments_description += "EchoController;";
aleloi868f32f2017-05-23 07:20:05 -07001993 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +02001994 if (config_.gain_controller2.enabled) {
1995 experiments_description += "GainController2;";
1996 }
aleloi868f32f2017-05-23 07:20:05 -07001997
1998 InternalAPMConfig apm_config;
1999
2000 apm_config.aec_enabled = public_submodules_->echo_cancellation->is_enabled();
2001 apm_config.aec_delay_agnostic_enabled =
2002 public_submodules_->echo_cancellation->is_delay_agnostic_enabled();
2003 apm_config.aec_drift_compensation_enabled =
2004 public_submodules_->echo_cancellation->is_drift_compensation_enabled();
2005 apm_config.aec_extended_filter_enabled =
2006 public_submodules_->echo_cancellation->is_extended_filter_enabled();
2007 apm_config.aec_suppression_level = static_cast<int>(
2008 public_submodules_->echo_cancellation->suppression_level());
2009
2010 apm_config.aecm_enabled =
2011 public_submodules_->echo_control_mobile->is_enabled();
2012 apm_config.aecm_comfort_noise_enabled =
2013 public_submodules_->echo_control_mobile->is_comfort_noise_enabled();
2014 apm_config.aecm_routing_mode =
2015 static_cast<int>(public_submodules_->echo_control_mobile->routing_mode());
2016
2017 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
2018 apm_config.agc_mode =
2019 static_cast<int>(public_submodules_->gain_control->mode());
2020 apm_config.agc_limiter_enabled =
2021 public_submodules_->gain_control->is_limiter_enabled();
2022 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
2023
2024 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
2025
2026 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
2027 apm_config.ns_level =
2028 static_cast<int>(public_submodules_->noise_suppression->level());
2029
2030 apm_config.transient_suppression_enabled =
2031 capture_.transient_suppressor_enabled;
2032 apm_config.intelligibility_enhancer_enabled =
2033 capture_nonlocked_.intelligibility_enabled;
2034 apm_config.experiments_description = experiments_description;
Alex Loiko5feb30e2018-04-16 13:52:32 +02002035 apm_config.pre_amplifier_enabled = config_.pre_amplifier.enabled;
2036 apm_config.pre_amplifier_fixed_gain_factor =
2037 config_.pre_amplifier.fixed_gain_factor;
aleloi868f32f2017-05-23 07:20:05 -07002038
2039 if (!forced && apm_config == apm_config_for_aec_dump_) {
2040 return;
2041 }
2042 aec_dump_->WriteConfig(apm_config);
2043 apm_config_for_aec_dump_ = apm_config;
2044}
2045
2046void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2047 const float* const* src) {
2048 RTC_DCHECK(aec_dump_);
2049 WriteAecDumpConfigMessage(false);
2050
2051 const size_t channel_size = formats_.api_format.input_stream().num_frames();
2052 const size_t num_channels = formats_.api_format.input_stream().num_channels();
2053 aec_dump_->AddCaptureStreamInput(
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002054 AudioFrameView<const float>(src, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002055 RecordAudioProcessingState();
2056}
2057
2058void AudioProcessingImpl::RecordUnprocessedCaptureStream(
2059 const AudioFrame& capture_frame) {
2060 RTC_DCHECK(aec_dump_);
2061 WriteAecDumpConfigMessage(false);
2062
2063 aec_dump_->AddCaptureStreamInput(capture_frame);
2064 RecordAudioProcessingState();
2065}
2066
2067void AudioProcessingImpl::RecordProcessedCaptureStream(
2068 const float* const* processed_capture_stream) {
2069 RTC_DCHECK(aec_dump_);
2070
2071 const size_t channel_size = formats_.api_format.output_stream().num_frames();
2072 const size_t num_channels =
2073 formats_.api_format.output_stream().num_channels();
Alex Loikoe36e8bb2018-02-16 11:54:07 +01002074 aec_dump_->AddCaptureStreamOutput(AudioFrameView<const float>(
2075 processed_capture_stream, num_channels, channel_size));
aleloi868f32f2017-05-23 07:20:05 -07002076 aec_dump_->WriteCaptureStreamMessage();
2077}
2078
2079void AudioProcessingImpl::RecordProcessedCaptureStream(
2080 const AudioFrame& processed_capture_frame) {
2081 RTC_DCHECK(aec_dump_);
2082
2083 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
2084 aec_dump_->WriteCaptureStreamMessage();
2085}
2086
2087void AudioProcessingImpl::RecordAudioProcessingState() {
2088 RTC_DCHECK(aec_dump_);
2089 AecDump::AudioProcessingState audio_proc_state;
2090 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
2091 audio_proc_state.drift =
2092 public_submodules_->echo_cancellation->stream_drift_samples();
2093 audio_proc_state.level = gain_control()->stream_analog_level();
2094 audio_proc_state.keypress = capture_.key_pressed;
2095 aec_dump_->AddAudioProcessingState(audio_proc_state);
2096}
2097
kwiberg83ffe452016-08-29 14:46:07 -07002098AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
Sam Zackrisson9394f6f2018-06-14 10:11:35 +02002099 bool transient_suppressor_enabled)
kwiberg83ffe452016-08-29 14:46:07 -07002100 : aec_system_delay_jumps(-1),
2101 delay_offset_ms(0),
2102 was_stream_delay_set(false),
2103 last_stream_delay_ms(0),
2104 last_aec_system_delay_ms(0),
2105 stream_delay_jumps(-1),
2106 output_will_be_muted(false),
2107 key_pressed(false),
2108 transient_suppressor_enabled(transient_suppressor_enabled),
peahde65ddc2016-09-16 15:02:15 -07002109 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002110 split_rate(kSampleRate16kHz),
Per Åhgren88cf0502018-07-16 17:08:41 +02002111 echo_path_gain_change(false),
2112 prev_analog_mic_level(-1) {}
kwiberg83ffe452016-08-29 14:46:07 -07002113
2114AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2115
2116AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2117
2118AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2119
niklase@google.com470e71d2011-07-07 08:21:25 +00002120} // namespace webrtc