blob: feea33d1797545c315d4b5439a16e2079b0b786e [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"
22#include "modules/audio_processing/aec3/echo_canceller3.h"
23#include "modules/audio_processing/agc/agc_manager_direct.h"
24#include "modules/audio_processing/agc2/gain_controller2.h"
25#include "modules/audio_processing/audio_buffer.h"
26#include "modules/audio_processing/beamformer/nonlinear_beamformer.h"
27#include "modules/audio_processing/common.h"
28#include "modules/audio_processing/echo_cancellation_impl.h"
29#include "modules/audio_processing/echo_control_mobile_impl.h"
30#include "modules/audio_processing/gain_control_for_experimental_agc.h"
31#include "modules/audio_processing/gain_control_impl.h"
32#include "rtc_base/checks.h"
33#include "rtc_base/logging.h"
34#include "rtc_base/platform_file.h"
35#include "rtc_base/trace_event.h"
peah1bcfce52016-08-26 07:16:04 -070036#if WEBRTC_INTELLIGIBILITY_ENHANCER
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "modules/audio_processing/intelligibility/intelligibility_enhancer.h"
peah1bcfce52016-08-26 07:16:04 -070038#endif
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020039#include "modules/audio_processing/level_controller/level_controller.h"
40#include "modules/audio_processing/level_estimator_impl.h"
41#include "modules/audio_processing/low_cut_filter.h"
42#include "modules/audio_processing/noise_suppression_impl.h"
43#include "modules/audio_processing/residual_echo_detector.h"
44#include "modules/audio_processing/transient/transient_suppressor.h"
45#include "modules/audio_processing/voice_detection_impl.h"
46#include "modules/include/module_common_types.h"
47#include "system_wrappers/include/file_wrapper.h"
48#include "system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000049
peah1bcfce52016-08-26 07:16:04 -070050// Check to verify that the define for the intelligibility enhancer is properly
51// set.
52#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
53 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
54 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
55#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
56#endif
57
Michael Graczyk86c6d332015-07-23 11:41:39 -070058#define RETURN_ON_ERR(expr) \
59 do { \
60 int err = (expr); \
61 if (err != kNoError) { \
62 return err; \
63 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000064 } while (0)
65
niklase@google.com470e71d2011-07-07 08:21:25 +000066namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070067
kwibergd59d3bb2016-09-13 07:49:33 -070068constexpr int AudioProcessing::kNativeSampleRatesHz[];
aluebsdf6416a2016-03-16 18:26:35 -070069
Michael Graczyk86c6d332015-07-23 11:41:39 -070070namespace {
71
72static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
73 switch (layout) {
74 case AudioProcessing::kMono:
75 case AudioProcessing::kStereo:
76 return false;
77 case AudioProcessing::kMonoAndKeyboard:
78 case AudioProcessing::kStereoAndKeyboard:
79 return true;
80 }
81
kwiberg9e2be5f2016-09-14 05:23:22 -070082 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070083 return false;
84}
aluebsdf6416a2016-03-16 18:26:35 -070085
peah2ace3f92016-09-10 04:42:27 -070086bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070087 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
88 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
89}
90
peah2ace3f92016-09-10 04:42:27 -070091int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) {
92#ifdef WEBRTC_ARCH_ARM_FAMILY
kwibergd59d3bb2016-09-13 07:49:33 -070093 constexpr int kMaxSplittingNativeProcessRate =
94 AudioProcessing::kSampleRate32kHz;
peah2ace3f92016-09-10 04:42:27 -070095#else
kwibergd59d3bb2016-09-13 07:49:33 -070096 constexpr int kMaxSplittingNativeProcessRate =
97 AudioProcessing::kSampleRate48kHz;
peah2ace3f92016-09-10 04:42:27 -070098#endif
kwibergd59d3bb2016-09-13 07:49:33 -070099 static_assert(
100 kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz,
101 "");
peah2ace3f92016-09-10 04:42:27 -0700102 const int uppermost_native_rate = band_splitting_required
103 ? kMaxSplittingNativeProcessRate
104 : AudioProcessing::kSampleRate48kHz;
105
106 for (auto rate : AudioProcessing::kNativeSampleRatesHz) {
107 if (rate >= uppermost_native_rate) {
108 return uppermost_native_rate;
109 }
110 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700111 return rate;
112 }
113 }
peah2ace3f92016-09-10 04:42:27 -0700114 RTC_NOTREACHED();
115 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700116}
117
peah9e6a2902017-05-15 07:19:21 -0700118// Maximum lengths that frame of samples being passed from the render side to
119// the capture side can have (does not apply to AEC3).
120static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
121static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
122
peah764e3642016-10-22 05:04:30 -0700123// Maximum number of frames to buffer in the render queue.
124// TODO(peah): Decrease this once we properly handle hugely unbalanced
125// reverse and forward call numbers.
126static const size_t kMaxNumFramesToBuffer = 100;
127
peah8271d042016-11-22 07:24:52 -0800128class HighPassFilterImpl : public HighPassFilter {
129 public:
130 explicit HighPassFilterImpl(AudioProcessingImpl* apm) : apm_(apm) {}
131 ~HighPassFilterImpl() override = default;
132
133 // HighPassFilter implementation.
134 int Enable(bool enable) override {
135 apm_->MutateConfig([enable](AudioProcessing::Config* config) {
136 config->high_pass_filter.enabled = enable;
137 });
138
139 return AudioProcessing::kNoError;
140 }
141
142 bool is_enabled() const override {
143 return apm_->GetConfig().high_pass_filter.enabled;
144 }
145
146 private:
147 AudioProcessingImpl* apm_;
148 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(HighPassFilterImpl);
149};
150
aleloi868f32f2017-05-23 07:20:05 -0700151webrtc::InternalAPMStreamsConfig ToStreamsConfig(
152 const ProcessingConfig& api_format) {
153 webrtc::InternalAPMStreamsConfig result;
154 result.input_sample_rate = api_format.input_stream().sample_rate_hz();
155 result.input_num_channels = api_format.input_stream().num_channels();
156 result.output_num_channels = api_format.output_stream().num_channels();
157 result.render_input_num_channels =
158 api_format.reverse_input_stream().num_channels();
159 result.render_input_sample_rate =
160 api_format.reverse_input_stream().sample_rate_hz();
161 result.output_sample_rate = api_format.output_stream().sample_rate_hz();
162 result.render_output_sample_rate =
163 api_format.reverse_output_stream().sample_rate_hz();
164 result.render_output_num_channels =
165 api_format.reverse_output_stream().num_channels();
166 return result;
167}
Michael Graczyk86c6d332015-07-23 11:41:39 -0700168} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000169
170// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000171static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000172
peah2ace3f92016-09-10 04:42:27 -0700173AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates() {}
174
175bool AudioProcessingImpl::ApmSubmoduleStates::Update(
peah8271d042016-11-22 07:24:52 -0800176 bool low_cut_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700177 bool echo_canceller_enabled,
178 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700179 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700180 bool noise_suppressor_enabled,
181 bool intelligibility_enhancer_enabled,
182 bool beamformer_enabled,
183 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700184 bool gain_controller2_enabled,
peah2ace3f92016-09-10 04:42:27 -0700185 bool level_controller_enabled,
peahe0eae3c2016-12-14 01:16:23 -0800186 bool echo_canceller3_enabled,
peah2ace3f92016-09-10 04:42:27 -0700187 bool voice_activity_detector_enabled,
188 bool level_estimator_enabled,
189 bool transient_suppressor_enabled) {
190 bool changed = false;
peah8271d042016-11-22 07:24:52 -0800191 changed |= (low_cut_filter_enabled != low_cut_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700192 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
193 changed |=
194 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700195 changed |=
196 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700197 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
198 changed |=
199 (intelligibility_enhancer_enabled != intelligibility_enhancer_enabled_);
200 changed |= (beamformer_enabled != beamformer_enabled_);
201 changed |=
202 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
alessiob3ec96df2017-05-22 06:57:06 -0700203 changed |=
204 (gain_controller2_enabled != gain_controller2_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700205 changed |= (level_controller_enabled != level_controller_enabled_);
peahe0eae3c2016-12-14 01:16:23 -0800206 changed |= (echo_canceller3_enabled != echo_canceller3_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700207 changed |= (level_estimator_enabled != level_estimator_enabled_);
208 changed |=
209 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
210 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
211 if (changed) {
peah8271d042016-11-22 07:24:52 -0800212 low_cut_filter_enabled_ = low_cut_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700213 echo_canceller_enabled_ = echo_canceller_enabled;
214 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700215 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700216 noise_suppressor_enabled_ = noise_suppressor_enabled;
217 intelligibility_enhancer_enabled_ = intelligibility_enhancer_enabled;
218 beamformer_enabled_ = beamformer_enabled;
219 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700220 gain_controller2_enabled_ = gain_controller2_enabled;
peah2ace3f92016-09-10 04:42:27 -0700221 level_controller_enabled_ = level_controller_enabled;
peahe0eae3c2016-12-14 01:16:23 -0800222 echo_canceller3_enabled_ = echo_canceller3_enabled;
peah2ace3f92016-09-10 04:42:27 -0700223 level_estimator_enabled_ = level_estimator_enabled;
224 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
225 transient_suppressor_enabled_ = transient_suppressor_enabled;
226 }
227
228 changed |= first_update_;
229 first_update_ = false;
230 return changed;
231}
232
233bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
234 const {
235#if WEBRTC_INTELLIGIBILITY_ENHANCER
236 return CaptureMultiBandProcessingActive() ||
peah52775842017-05-16 06:14:09 -0700237 intelligibility_enhancer_enabled_ || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700238#else
peah52775842017-05-16 06:14:09 -0700239 return CaptureMultiBandProcessingActive() || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700240#endif
241}
242
243bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
244 const {
peah8271d042016-11-22 07:24:52 -0800245 return low_cut_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700246 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
peahe0eae3c2016-12-14 01:16:23 -0800247 beamformer_enabled_ || adaptive_gain_controller_enabled_ ||
248 echo_canceller3_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700249}
250
peah23ac8b42017-05-23 05:33:56 -0700251bool AudioProcessingImpl::ApmSubmoduleStates::CaptureFullBandProcessingActive()
252 const {
253 return level_controller_enabled_;
254}
255
peah2ace3f92016-09-10 04:42:27 -0700256bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
257 const {
258 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800259 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
peah52775842017-05-16 06:14:09 -0700260 echo_canceller3_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700261}
262
263bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
264 const {
265#if WEBRTC_INTELLIGIBILITY_ENHANCER
266 return intelligibility_enhancer_enabled_;
267#else
268 return false;
269#endif
270}
271
solenberg5e465c32015-12-08 13:22:33 -0800272struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800273 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800274 // Accessed externally of APM without any lock acquired.
peahb624d8c2016-03-05 03:01:14 -0800275 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
peahbb9edbd2016-03-10 12:54:25 -0800276 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
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 {
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700292 explicit ApmPrivateSubmodules(NonlinearBeamformer* beamformer)
solenberg5e465c32015-12-08 13:22:33 -0800293 : beamformer(beamformer) {}
294 // Accessed internally from capture or during initialization
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700295 std::unique_ptr<NonlinearBeamformer> beamformer;
kwiberg88788ad2016-02-19 07:04:49 -0800296 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700297 std::unique_ptr<GainController2> gain_controller2;
peah8271d042016-11-22 07:24:52 -0800298 std::unique_ptr<LowCutFilter> low_cut_filter;
peahca4cac72016-06-29 15:26:12 -0700299 std::unique_ptr<LevelController> level_controller;
ivoc9f4a4a02016-10-28 05:39:16 -0700300 std::unique_ptr<ResidualEchoDetector> residual_echo_detector;
peahe0eae3c2016-12-14 01:16:23 -0800301 std::unique_ptr<EchoCanceller3> echo_canceller3;
solenberg5e465c32015-12-08 13:22:33 -0800302};
303
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000304AudioProcessing* AudioProcessing::Create() {
peah88ac8532016-09-12 16:47:25 -0700305 webrtc::Config config;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000306 return Create(config, nullptr);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000307}
308
peah88ac8532016-09-12 16:47:25 -0700309AudioProcessing* AudioProcessing::Create(const webrtc::Config& config) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000310 return Create(config, nullptr);
311}
312
peah88ac8532016-09-12 16:47:25 -0700313AudioProcessing* AudioProcessing::Create(const webrtc::Config& config,
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700314 NonlinearBeamformer* beamformer) {
peaha9cc40b2017-06-29 08:32:09 -0700315 AudioProcessingImpl* apm =
316 new rtc::RefCountedObject<AudioProcessingImpl>(config, beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000317 if (apm->Initialize() != kNoError) {
318 delete apm;
peahdf3efa82015-11-28 12:35:15 -0800319 apm = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000320 }
321
322 return apm;
323}
324
peah88ac8532016-09-12 16:47:25 -0700325AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000326 : AudioProcessingImpl(config, nullptr) {}
327
peah88ac8532016-09-12 16:47:25 -0700328AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config,
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700329 NonlinearBeamformer* beamformer)
peah8271d042016-11-22 07:24:52 -0800330 : high_pass_filter_impl_(new HighPassFilterImpl(this)),
331 public_submodules_(new ApmPublicSubmodules()),
peahdf3efa82015-11-28 12:35:15 -0800332 private_submodules_(new ApmPrivateSubmodules(beamformer)),
333 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800334 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000335#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700336 false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000337#else
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700338 config.Get<ExperimentalAgc>().enabled),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000339#endif
andrew1c7075f2015-06-24 18:14:14 -0700340#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
aluebs2a346882016-01-11 18:04:30 -0800341 capture_(false,
andrew1c7075f2015-06-24 18:14:14 -0700342#else
aluebs2a346882016-01-11 18:04:30 -0800343 capture_(config.Get<ExperimentalNs>().enabled,
andrew1c7075f2015-06-24 18:14:14 -0700344#endif
aluebs2a346882016-01-11 18:04:30 -0800345 config.Get<Beamforming>().array_geometry,
aluebsb2328d12016-01-11 20:32:29 -0800346 config.Get<Beamforming>().target_direction),
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700347 capture_nonlocked_(config.Get<Beamforming>().enabled,
peah88ac8532016-09-12 16:47:25 -0700348 config.Get<Intelligibility>().enabled) {
peahdf3efa82015-11-28 12:35:15 -0800349 {
350 rtc::CritScope cs_render(&crit_render_);
351 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000352
peahb624d8c2016-03-05 03:01:14 -0800353 public_submodules_->echo_cancellation.reset(
peahb58a1582016-03-15 09:34:24 -0700354 new EchoCancellationImpl(&crit_render_, &crit_capture_));
peahbb9edbd2016-03-10 12:54:25 -0800355 public_submodules_->echo_control_mobile.reset(
peah253534d2016-03-15 04:32:28 -0700356 new EchoControlMobileImpl(&crit_render_, &crit_capture_));
peahbfa97112016-03-10 21:09:04 -0800357 public_submodules_->gain_control.reset(
peahb8fbb542016-03-15 02:28:08 -0700358 new GainControlImpl(&crit_capture_, &crit_capture_));
solenberg949028f2015-12-15 11:39:38 -0800359 public_submodules_->level_estimator.reset(
360 new LevelEstimatorImpl(&crit_capture_));
solenberg5e465c32015-12-08 13:22:33 -0800361 public_submodules_->noise_suppression.reset(
362 new NoiseSuppressionImpl(&crit_capture_));
solenberga29386c2015-12-16 03:31:12 -0800363 public_submodules_->voice_detection.reset(
364 new VoiceDetectionImpl(&crit_capture_));
peahbe615622016-02-13 16:40:47 -0800365 public_submodules_->gain_control_for_experimental_agc.reset(
peahbfa97112016-03-10 21:09:04 -0800366 new GainControlForExperimentalAgc(
367 public_submodules_->gain_control.get(), &crit_capture_));
ivoc9f4a4a02016-10-28 05:39:16 -0700368 private_submodules_->residual_echo_detector.reset(
369 new ResidualEchoDetector());
peahca4cac72016-06-29 15:26:12 -0700370
peahc19f3122016-10-07 14:54:10 -0700371 // TODO(peah): Move this creation to happen only when the level controller
372 // is enabled.
peahca4cac72016-06-29 15:26:12 -0700373 private_submodules_->level_controller.reset(new LevelController());
peahdf3efa82015-11-28 12:35:15 -0800374 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000375
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000376 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000377}
378
379AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800380 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800381 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800382 private_submodules_->agc_manager.reset();
383 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800384 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000385}
386
niklase@google.com470e71d2011-07-07 08:21:25 +0000387int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800388 // Run in a single-threaded manner during initialization.
389 rtc::CritScope cs_render(&crit_render_);
390 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000391 return InitializeLocked();
392}
393
peahde65ddc2016-09-16 15:02:15 -0700394int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
395 int capture_output_sample_rate_hz,
396 int render_input_sample_rate_hz,
397 ChannelLayout capture_input_layout,
398 ChannelLayout capture_output_layout,
399 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700400 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700401 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
402 LayoutHasKeyboard(capture_input_layout)},
403 {capture_output_sample_rate_hz,
404 ChannelsFromLayout(capture_output_layout),
405 LayoutHasKeyboard(capture_output_layout)},
406 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
407 LayoutHasKeyboard(render_input_layout)},
408 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
409 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700410
411 return Initialize(processing_config);
412}
413
414int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800415 // Run in a single-threaded manner during initialization.
416 rtc::CritScope cs_render(&crit_render_);
417 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700418 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000419}
420
peahdf3efa82015-11-28 12:35:15 -0800421int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800422 const ProcessingConfig& processing_config) {
peah2ace3f92016-09-10 04:42:27 -0700423 return MaybeInitialize(processing_config, false);
peah81b9bfe2015-11-27 02:47:28 -0800424}
425
peahdf3efa82015-11-28 12:35:15 -0800426int AudioProcessingImpl::MaybeInitializeCapture(
peah2ace3f92016-09-10 04:42:27 -0700427 const ProcessingConfig& processing_config,
428 bool force_initialization) {
429 return MaybeInitialize(processing_config, force_initialization);
peah81b9bfe2015-11-27 02:47:28 -0800430}
431
peah192164e2015-11-17 02:16:45 -0800432// Calls InitializeLocked() if any of the audio parameters have changed from
peahdf3efa82015-11-28 12:35:15 -0800433// their current values (needs to be called while holding the crit_render_lock).
434int AudioProcessingImpl::MaybeInitialize(
peah2ace3f92016-09-10 04:42:27 -0700435 const ProcessingConfig& processing_config,
436 bool force_initialization) {
peahdf3efa82015-11-28 12:35:15 -0800437 // Called from both threads. Thread check is therefore not possible.
peah2ace3f92016-09-10 04:42:27 -0700438 if (processing_config == formats_.api_format && !force_initialization) {
peah192164e2015-11-17 02:16:45 -0800439 return kNoError;
440 }
peahdf3efa82015-11-28 12:35:15 -0800441
442 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800443 return InitializeLocked(processing_config);
444}
445
niklase@google.com470e71d2011-07-07 08:21:25 +0000446int AudioProcessingImpl::InitializeLocked() {
Per Ã…hgren4bdced52017-06-27 16:00:38 +0200447 UpdateActiveSubmoduleStates();
448
peah522d71b2017-02-23 05:16:26 -0800449 const int capture_audiobuffer_num_channels =
450 capture_nonlocked_.beamformer_enabled
451 ? formats_.api_format.input_stream().num_channels()
452 : formats_.api_format.output_stream().num_channels();
453
peahde65ddc2016-09-16 15:02:15 -0700454 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800455 formats_.api_format.reverse_output_stream().num_frames() == 0
peahde65ddc2016-09-16 15:02:15 -0700456 ? formats_.render_processing_format.num_frames()
peahdf3efa82015-11-28 12:35:15 -0800457 : formats_.api_format.reverse_output_stream().num_frames();
458 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
459 render_.render_audio.reset(new AudioBuffer(
460 formats_.api_format.reverse_input_stream().num_frames(),
461 formats_.api_format.reverse_input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700462 formats_.render_processing_format.num_frames(),
463 formats_.render_processing_format.num_channels(),
464 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700465 if (formats_.api_format.reverse_input_stream() !=
466 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800467 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800468 formats_.api_format.reverse_input_stream().num_channels(),
469 formats_.api_format.reverse_input_stream().num_frames(),
470 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800471 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700472 } else {
peahdf3efa82015-11-28 12:35:15 -0800473 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700474 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700475 } else {
peahdf3efa82015-11-28 12:35:15 -0800476 render_.render_audio.reset(nullptr);
477 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700478 }
peahce4d9152017-05-19 01:28:05 -0700479
peahdf3efa82015-11-28 12:35:15 -0800480 capture_.capture_audio.reset(
481 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
482 formats_.api_format.input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700483 capture_nonlocked_.capture_processing_format.num_frames(),
484 capture_audiobuffer_num_channels,
peahdf3efa82015-11-28 12:35:15 -0800485 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000486
peahde65ddc2016-09-16 15:02:15 -0700487 public_submodules_->echo_cancellation->Initialize(
488 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
489 num_proc_channels());
peah764e3642016-10-22 05:04:30 -0700490 AllocateRenderQueue();
491
ivoc3e9a5372016-10-28 07:55:33 -0700492 int success = public_submodules_->echo_cancellation->enable_metrics(true);
493 RTC_DCHECK_EQ(0, success);
494 success = public_submodules_->echo_cancellation->enable_delay_logging(true);
495 RTC_DCHECK_EQ(0, success);
peahde65ddc2016-09-16 15:02:15 -0700496 public_submodules_->echo_control_mobile->Initialize(
497 proc_split_sample_rate_hz(), num_reverse_channels(),
498 num_output_channels());
peah135259a2016-10-28 03:12:11 -0700499
500 public_submodules_->gain_control->Initialize(num_proc_channels(),
501 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700502 if (constants_.use_experimental_agc) {
503 if (!private_submodules_->agc_manager.get()) {
504 private_submodules_->agc_manager.reset(new AgcManagerDirect(
505 public_submodules_->gain_control.get(),
506 public_submodules_->gain_control_for_experimental_agc.get(),
henrik.lundinbd681b92016-12-05 09:08:42 -0800507 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min));
peahde65ddc2016-09-16 15:02:15 -0700508 }
509 private_submodules_->agc_manager->Initialize();
510 private_submodules_->agc_manager->SetCaptureMuted(
511 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700512 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700513 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200514 InitializeTransient();
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000515 InitializeBeamformer();
peah1bcfce52016-08-26 07:16:04 -0700516#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -0700517 InitializeIntelligibility();
peah1bcfce52016-08-26 07:16:04 -0700518#endif
peah8271d042016-11-22 07:24:52 -0800519 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700520 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
521 proc_sample_rate_hz());
522 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
523 public_submodules_->level_estimator->Initialize();
peahca4cac72016-06-29 15:26:12 -0700524 InitializeLevelController();
ivoc9f4a4a02016-10-28 05:39:16 -0700525 InitializeResidualEchoDetector();
peahe0eae3c2016-12-14 01:16:23 -0800526 InitializeEchoCanceller3();
alessiob3ec96df2017-05-22 06:57:06 -0700527 InitializeGainController2();
solenberg70f99032015-12-08 11:07:32 -0800528
aleloi868f32f2017-05-23 07:20:05 -0700529 if (aec_dump_) {
530 aec_dump_->WriteInitMessage(ToStreamsConfig(formats_.api_format));
531 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000532 return kNoError;
533}
534
Michael Graczyk86c6d332015-07-23 11:41:39 -0700535int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
Per Ã…hgren4bdced52017-06-27 16:00:38 +0200536 UpdateActiveSubmoduleStates();
537
Michael Graczyk86c6d332015-07-23 11:41:39 -0700538 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700539 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
540 return kBadSampleRateError;
541 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000542 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700543
Peter Kasting69558702016-01-12 16:26:35 -0800544 const size_t num_in_channels = config.input_stream().num_channels();
545 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700546
547 // Need at least one input channel.
548 // Need either one output channel or as many outputs as there are inputs.
549 if (num_in_channels == 0 ||
550 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700551 return kBadNumberChannelsError;
552 }
553
aluebsb2328d12016-01-11 20:32:29 -0800554 if (capture_nonlocked_.beamformer_enabled &&
Peter Kasting69558702016-01-12 16:26:35 -0800555 num_in_channels != capture_.array_geometry.size()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700556 return kBadNumberChannelsError;
557 }
558
peahdf3efa82015-11-28 12:35:15 -0800559 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000560
peahde65ddc2016-09-16 15:02:15 -0700561 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700562 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700563 formats_.api_format.output_stream().sample_rate_hz()),
564 submodule_states_.CaptureMultiBandSubModulesActive() ||
565 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000566
peahde65ddc2016-09-16 15:02:15 -0700567 capture_nonlocked_.capture_processing_format =
568 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700569
peah2ce640f2017-04-07 03:57:48 -0700570 int render_processing_rate;
571 if (!config_.echo_canceller3.enabled) {
572 render_processing_rate = FindNativeProcessRateToUse(
573 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
574 formats_.api_format.reverse_output_stream().sample_rate_hz()),
575 submodule_states_.CaptureMultiBandSubModulesActive() ||
576 submodule_states_.RenderMultiBandSubModulesActive());
577 } else {
578 render_processing_rate = capture_processing_rate;
579 }
580
aluebseb3603b2016-04-20 15:27:58 -0700581 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
582 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700583 if (render_processing_rate > kSampleRate32kHz &&
584 !config_.echo_canceller3.enabled) {
peahde65ddc2016-09-16 15:02:15 -0700585 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
586 ? kSampleRate32kHz
587 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700588 }
peah2ce640f2017-04-07 03:57:48 -0700589
peahde65ddc2016-09-16 15:02:15 -0700590 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700591 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700592 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
593 kSampleRate8kHz) {
594 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000595 } else {
peahde65ddc2016-09-16 15:02:15 -0700596 render_processing_rate =
597 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000598 }
599
peahde65ddc2016-09-16 15:02:15 -0700600 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000601 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700602 if (submodule_states_.RenderMultiBandSubModulesActive()) {
603 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
604 } else {
605 formats_.render_processing_format = StreamConfig(
606 formats_.api_format.reverse_input_stream().sample_rate_hz(),
607 formats_.api_format.reverse_input_stream().num_channels());
608 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000609
peahde65ddc2016-09-16 15:02:15 -0700610 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
611 kSampleRate32kHz ||
612 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
613 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800614 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000615 } else {
peahdf3efa82015-11-28 12:35:15 -0800616 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700617 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000618 }
619
620 return InitializeLocked();
621}
622
peah88ac8532016-09-12 16:47:25 -0700623void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peahc19f3122016-10-07 14:54:10 -0700624 config_ = config;
peah88ac8532016-09-12 16:47:25 -0700625
peahc19f3122016-10-07 14:54:10 -0700626 bool config_ok = LevelController::Validate(config_.level_controller);
peah88ac8532016-09-12 16:47:25 -0700627 if (!config_ok) {
628 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
629 << "level_controller: "
peahc19f3122016-10-07 14:54:10 -0700630 << LevelController::ToString(config_.level_controller)
peah88ac8532016-09-12 16:47:25 -0700631 << std::endl
632 << "Reverting to default parameter set";
peahc19f3122016-10-07 14:54:10 -0700633 config_.level_controller = AudioProcessing::Config::LevelController();
peah88ac8532016-09-12 16:47:25 -0700634 }
635
636 // Run in a single-threaded manner when applying the settings.
637 rtc::CritScope cs_render(&crit_render_);
638 rtc::CritScope cs_capture(&crit_capture_);
639
peahc19f3122016-10-07 14:54:10 -0700640 // TODO(peah): Replace the use of capture_nonlocked_.level_controller_enabled
641 // with the value in config_ everywhere in the code.
642 if (capture_nonlocked_.level_controller_enabled !=
643 config_.level_controller.enabled) {
peah88ac8532016-09-12 16:47:25 -0700644 capture_nonlocked_.level_controller_enabled =
peahc19f3122016-10-07 14:54:10 -0700645 config_.level_controller.enabled;
646 // TODO(peah): Remove the conditional initialization to always initialize
647 // the level controller regardless of whether it is enabled or not.
648 InitializeLevelController();
peah88ac8532016-09-12 16:47:25 -0700649 }
peahc19f3122016-10-07 14:54:10 -0700650 LOG(LS_INFO) << "Level controller activated: "
651 << capture_nonlocked_.level_controller_enabled;
652
653 private_submodules_->level_controller->ApplyConfig(config_.level_controller);
peah8271d042016-11-22 07:24:52 -0800654
655 InitializeLowCutFilter();
656
657 LOG(LS_INFO) << "Highpass filter activated: "
658 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800659
660 config_ok = EchoCanceller3::Validate(config_.echo_canceller3);
661 if (!config_ok) {
662 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
663 << "echo canceller 3: "
664 << EchoCanceller3::ToString(config_.echo_canceller3)
665 << std::endl
666 << "Reverting to default parameter set";
667 config_.echo_canceller3 = AudioProcessing::Config::EchoCanceller3();
668 }
669
670 if (config.echo_canceller3.enabled !=
671 capture_nonlocked_.echo_canceller3_enabled) {
672 capture_nonlocked_.echo_canceller3_enabled =
673 config_.echo_canceller3.enabled;
674 InitializeEchoCanceller3();
675 LOG(LS_INFO) << "Echo canceller 3 activated: "
676 << capture_nonlocked_.echo_canceller3_enabled;
677 }
alessiob3ec96df2017-05-22 06:57:06 -0700678
679 config_ok = GainController2::Validate(config_.gain_controller2);
680 if (!config_ok) {
681 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
682 << "gain_controller2: "
683 << GainController2::ToString(config_.gain_controller2)
684 << std::endl
685 << "Reverting to default parameter set";
686 config_.gain_controller2 = AudioProcessing::Config::GainController2();
687 }
688
689 if (config.gain_controller2.enabled !=
690 capture_nonlocked_.gain_controller2_enabled) {
691 capture_nonlocked_.gain_controller2_enabled =
692 config_.gain_controller2.enabled;
693 InitializeGainController2();
694 LOG(LS_INFO) << "Gain controller 2 activated: "
695 << capture_nonlocked_.gain_controller2_enabled;
696 }
peah88ac8532016-09-12 16:47:25 -0700697}
698
699void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800700 // Run in a single-threaded manner when setting the extra options.
701 rtc::CritScope cs_render(&crit_render_);
702 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000703
peahb624d8c2016-03-05 03:01:14 -0800704 public_submodules_->echo_cancellation->SetExtraOptions(config);
705
peahdf3efa82015-11-28 12:35:15 -0800706 if (capture_.transient_suppressor_enabled !=
707 config.Get<ExperimentalNs>().enabled) {
708 capture_.transient_suppressor_enabled =
709 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000710 InitializeTransient();
711 }
aluebs2a346882016-01-11 18:04:30 -0800712
peah1bcfce52016-08-26 07:16:04 -0700713#if WEBRTC_INTELLIGIBILITY_ENHANCER
alessiob3ec96df2017-05-22 06:57:06 -0700714 if (capture_nonlocked_.intelligibility_enabled !=
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700715 config.Get<Intelligibility>().enabled) {
716 capture_nonlocked_.intelligibility_enabled =
717 config.Get<Intelligibility>().enabled;
718 InitializeIntelligibility();
719 }
peah1bcfce52016-08-26 07:16:04 -0700720#endif
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700721
aluebs2a346882016-01-11 18:04:30 -0800722#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
aluebsb2328d12016-01-11 20:32:29 -0800723 if (capture_nonlocked_.beamformer_enabled !=
724 config.Get<Beamforming>().enabled) {
725 capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled;
aluebs2a346882016-01-11 18:04:30 -0800726 if (config.Get<Beamforming>().array_geometry.size() > 1) {
727 capture_.array_geometry = config.Get<Beamforming>().array_geometry;
728 }
729 capture_.target_direction = config.Get<Beamforming>().target_direction;
730 InitializeBeamformer();
731 }
732#endif // WEBRTC_ANDROID_PLATFORM_BUILD
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000733}
734
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000735int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800736 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700737 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000738}
739
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000740int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800741 // Used as callback from submodules, hence locking is not allowed.
742 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000743}
744
Peter Kasting69558702016-01-12 16:26:35 -0800745size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800746 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700747 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000748}
749
Peter Kasting69558702016-01-12 16:26:35 -0800750size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800751 // Used as callback from submodules, hence locking is not allowed.
752 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000753}
754
Peter Kasting69558702016-01-12 16:26:35 -0800755size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800756 // Used as callback from submodules, hence locking is not allowed.
peahedddac52017-05-16 01:08:58 -0700757 return (capture_nonlocked_.beamformer_enabled ||
758 capture_nonlocked_.echo_canceller3_enabled)
759 ? 1
760 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800761}
762
Peter Kasting69558702016-01-12 16:26:35 -0800763size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800764 // Used as callback from submodules, hence locking is not allowed.
765 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000766}
767
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000768void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800769 rtc::CritScope cs(&crit_capture_);
770 capture_.output_will_be_muted = muted;
771 if (private_submodules_->agc_manager.get()) {
772 private_submodules_->agc_manager->SetCaptureMuted(
773 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000774 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000775}
776
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000777
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000778int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700779 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000780 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000781 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000782 int output_sample_rate_hz,
783 ChannelLayout output_layout,
784 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800785 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800786 StreamConfig input_stream;
787 StreamConfig output_stream;
788 {
789 // Access the formats_.api_format.input_stream beneath the capture lock.
790 // The lock must be released as it is later required in the call
791 // to ProcessStream(,,,);
792 rtc::CritScope cs(&crit_capture_);
793 input_stream = formats_.api_format.input_stream();
794 output_stream = formats_.api_format.output_stream();
795 }
796
Michael Graczyk86c6d332015-07-23 11:41:39 -0700797 input_stream.set_sample_rate_hz(input_sample_rate_hz);
798 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
799 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700800 output_stream.set_sample_rate_hz(output_sample_rate_hz);
801 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
802 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
803
804 if (samples_per_channel != input_stream.num_frames()) {
805 return kBadDataLengthError;
806 }
807 return ProcessStream(src, input_stream, output_stream, dest);
808}
809
810int AudioProcessingImpl::ProcessStream(const float* const* src,
811 const StreamConfig& input_config,
812 const StreamConfig& output_config,
813 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800814 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800815 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700816 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800817 {
818 // Acquire the capture lock in order to safely call the function
819 // that retrieves the render side data. This function accesses apm
820 // getters that need the capture lock held when being called.
821 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700822 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800823
824 if (!src || !dest) {
825 return kNullPointerError;
826 }
827
828 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700829 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000830 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000831
Michael Graczyk86c6d332015-07-23 11:41:39 -0700832 processing_config.input_stream() = input_config;
833 processing_config.output_stream() = output_config;
834
peahdf3efa82015-11-28 12:35:15 -0800835 {
836 // Do conditional reinitialization.
837 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -0700838 RETURN_ON_ERR(
839 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -0800840 }
841 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700842 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
843 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000844
aleloi868f32f2017-05-23 07:20:05 -0700845 if (aec_dump_) {
846 RecordUnprocessedCaptureStream(src);
847 }
848
peahdf3efa82015-11-28 12:35:15 -0800849 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700850 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800851 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000852
aleloi868f32f2017-05-23 07:20:05 -0700853 if (aec_dump_) {
854 RecordProcessedCaptureStream(dest);
855 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000856 return kNoError;
857}
858
peah9e6a2902017-05-15 07:19:21 -0700859void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
peah764e3642016-10-22 05:04:30 -0700860 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
861 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700862 &aec_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700863
kwibergaf476c72016-11-28 15:21:39 -0800864 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -0700865
866 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700867 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700868 // The data queue is full and needs to be emptied.
869 EmptyQueuedRenderAudio();
870
871 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700872 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700873 RTC_DCHECK(result);
874 }
875
876 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
877 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700878 &aecm_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700879
880 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700881 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700882 // The data queue is full and needs to be emptied.
883 EmptyQueuedRenderAudio();
884
885 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700886 bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700887 RTC_DCHECK(result);
888 }
peah701d6282016-10-25 05:42:20 -0700889
890 if (!constants_.use_experimental_agc) {
891 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
892 // Insert the samples into the queue.
893 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
894 // The data queue is full and needs to be emptied.
895 EmptyQueuedRenderAudio();
896
897 // Retry the insert (should always work).
898 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
899 RTC_DCHECK(result);
900 }
901 }
peah9e6a2902017-05-15 07:19:21 -0700902}
ivoc9f4a4a02016-10-28 05:39:16 -0700903
peah9e6a2902017-05-15 07:19:21 -0700904void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -0700905 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
906
907 // Insert the samples into the queue.
908 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
909 // The data queue is full and needs to be emptied.
910 EmptyQueuedRenderAudio();
911
912 // Retry the insert (should always work).
913 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
914 RTC_DCHECK(result);
915 }
peah764e3642016-10-22 05:04:30 -0700916}
917
918void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -0700919 const size_t new_aec_render_queue_element_max_size =
peah764e3642016-10-22 05:04:30 -0700920 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -0700921 kMaxAllowedValuesOfSamplesPerBand *
peah764e3642016-10-22 05:04:30 -0700922 EchoCancellationImpl::NumCancellersRequired(
923 num_output_channels(), num_reverse_channels()));
924
peah701d6282016-10-25 05:42:20 -0700925 const size_t new_aecm_render_queue_element_max_size =
peaha0624602016-10-25 04:45:24 -0700926 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -0700927 kMaxAllowedValuesOfSamplesPerBand *
peaha0624602016-10-25 04:45:24 -0700928 EchoControlMobileImpl::NumCancellersRequired(
929 num_output_channels(), num_reverse_channels()));
peah764e3642016-10-22 05:04:30 -0700930
peah701d6282016-10-25 05:42:20 -0700931 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -0700932 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -0700933
ivoc9f4a4a02016-10-28 05:39:16 -0700934 const size_t new_red_render_queue_element_max_size =
935 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
936
peaha0624602016-10-25 04:45:24 -0700937 // Reallocate the queues if the queue item sizes are too small to fit the
938 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -0700939 if (aec_render_queue_element_max_size_ <
940 new_aec_render_queue_element_max_size) {
941 aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
peah764e3642016-10-22 05:04:30 -0700942
peaha0624602016-10-25 04:45:24 -0700943 std::vector<float> template_queue_element(
peah701d6282016-10-25 05:42:20 -0700944 aec_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700945
peah701d6282016-10-25 05:42:20 -0700946 aec_render_signal_queue_.reset(
peah764e3642016-10-22 05:04:30 -0700947 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
948 kMaxNumFramesToBuffer, template_queue_element,
peaha0624602016-10-25 04:45:24 -0700949 RenderQueueItemVerifier<float>(
peah701d6282016-10-25 05:42:20 -0700950 aec_render_queue_element_max_size_)));
peah764e3642016-10-22 05:04:30 -0700951
peah701d6282016-10-25 05:42:20 -0700952 aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
953 aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
peah764e3642016-10-22 05:04:30 -0700954 } else {
peah701d6282016-10-25 05:42:20 -0700955 aec_render_signal_queue_->Clear();
peaha0624602016-10-25 04:45:24 -0700956 }
957
peah701d6282016-10-25 05:42:20 -0700958 if (aecm_render_queue_element_max_size_ <
959 new_aecm_render_queue_element_max_size) {
960 aecm_render_queue_element_max_size_ =
961 new_aecm_render_queue_element_max_size;
peaha0624602016-10-25 04:45:24 -0700962
963 std::vector<int16_t> template_queue_element(
peah701d6282016-10-25 05:42:20 -0700964 aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700965
peah701d6282016-10-25 05:42:20 -0700966 aecm_render_signal_queue_.reset(
peaha0624602016-10-25 04:45:24 -0700967 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
968 kMaxNumFramesToBuffer, template_queue_element,
969 RenderQueueItemVerifier<int16_t>(
peah701d6282016-10-25 05:42:20 -0700970 aecm_render_queue_element_max_size_)));
peaha0624602016-10-25 04:45:24 -0700971
peah701d6282016-10-25 05:42:20 -0700972 aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
973 aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700974 } else {
peah701d6282016-10-25 05:42:20 -0700975 aecm_render_signal_queue_->Clear();
976 }
977
978 if (agc_render_queue_element_max_size_ <
979 new_agc_render_queue_element_max_size) {
980 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
981
982 std::vector<int16_t> template_queue_element(
983 agc_render_queue_element_max_size_);
984
985 agc_render_signal_queue_.reset(
986 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
987 kMaxNumFramesToBuffer, template_queue_element,
988 RenderQueueItemVerifier<int16_t>(
989 agc_render_queue_element_max_size_)));
990
991 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
992 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
993 } else {
994 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -0700995 }
ivoc9f4a4a02016-10-28 05:39:16 -0700996
997 if (red_render_queue_element_max_size_ <
998 new_red_render_queue_element_max_size) {
999 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1000
1001 std::vector<float> template_queue_element(
1002 red_render_queue_element_max_size_);
1003
1004 red_render_signal_queue_.reset(
1005 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1006 kMaxNumFramesToBuffer, template_queue_element,
1007 RenderQueueItemVerifier<float>(
1008 red_render_queue_element_max_size_)));
1009
1010 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1011 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1012 } else {
1013 red_render_signal_queue_->Clear();
1014 }
peah764e3642016-10-22 05:04:30 -07001015}
1016
1017void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1018 rtc::CritScope cs_capture(&crit_capture_);
peah701d6282016-10-25 05:42:20 -07001019 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -07001020 public_submodules_->echo_cancellation->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001021 aec_capture_queue_buffer_);
peaha0624602016-10-25 04:45:24 -07001022 }
1023
peah701d6282016-10-25 05:42:20 -07001024 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -07001025 public_submodules_->echo_control_mobile->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001026 aecm_capture_queue_buffer_);
1027 }
1028
1029 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1030 public_submodules_->gain_control->ProcessRenderAudio(
1031 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001032 }
ivoc9f4a4a02016-10-28 05:39:16 -07001033
1034 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
1035 private_submodules_->residual_echo_detector->AnalyzeRenderAudio(
1036 red_capture_queue_buffer_);
1037 }
peah764e3642016-10-22 05:04:30 -07001038}
1039
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001040int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001041 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001042 {
1043 // Acquire the capture lock in order to safely call the function
1044 // that retrieves the render side data. This function accesses apm
1045 // getters that need the capture lock held when being called.
1046 // The lock needs to be released as
1047 // public_submodules_->echo_control_mobile->is_enabled() aquires this lock
1048 // as well.
1049 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001050 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001051 }
peahfa6228e2015-11-16 16:27:42 -08001052
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001053 if (!frame) {
1054 return kNullPointerError;
1055 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001056 // Must be a native rate.
1057 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1058 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001059 frame->sample_rate_hz_ != kSampleRate32kHz &&
1060 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001061 return kBadSampleRateError;
1062 }
peah192164e2015-11-17 02:16:45 -08001063
peahdf3efa82015-11-28 12:35:15 -08001064 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001065 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001066 {
1067 // Aquire lock for the access of api_format.
1068 // The lock is released immediately due to the conditional
1069 // reinitialization.
1070 rtc::CritScope cs_capture(&crit_capture_);
1071 // TODO(ajm): The input and output rates and channels are currently
1072 // constrained to be identical in the int16 interface.
1073 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001074
1075 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001076 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001077 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1078 processing_config.input_stream().set_num_channels(frame->num_channels_);
1079 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1080 processing_config.output_stream().set_num_channels(frame->num_channels_);
1081
peahdf3efa82015-11-28 12:35:15 -08001082 {
1083 // Do conditional reinitialization.
1084 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -07001085 RETURN_ON_ERR(
1086 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -08001087 }
1088 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001089 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001090 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001091 return kBadDataLengthError;
1092 }
1093
aleloi868f32f2017-05-23 07:20:05 -07001094 if (aec_dump_) {
1095 RecordUnprocessedCaptureStream(*frame);
1096 }
1097
peahdf3efa82015-11-28 12:35:15 -08001098 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001099 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001100 capture_.capture_audio->InterleaveTo(
peah23ac8b42017-05-23 05:33:56 -07001101 frame, submodule_states_.CaptureMultiBandProcessingActive() ||
1102 submodule_states_.CaptureFullBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001103
aleloi868f32f2017-05-23 07:20:05 -07001104 if (aec_dump_) {
1105 RecordProcessedCaptureStream(*frame);
1106 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001107
1108 return kNoError;
1109}
1110
peahde65ddc2016-09-16 15:02:15 -07001111int AudioProcessingImpl::ProcessCaptureStreamLocked() {
peahb58a1582016-03-15 09:34:24 -07001112 // Ensure that not both the AEC and AECM are active at the same time.
1113 // TODO(peah): Simplify once the public API Enable functions for these
1114 // are moved to APM.
1115 RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&
1116 public_submodules_->echo_control_mobile->is_enabled()));
1117
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001118 MaybeUpdateHistograms();
1119
peahde65ddc2016-09-16 15:02:15 -07001120 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001121
peah1b08dc32016-12-20 13:45:58 -08001122 capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>(
henrik.lundin290d43a2016-11-29 08:09:09 -08001123 capture_buffer->channels_const()[0],
1124 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001125 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1126 if (log_rms) {
1127 capture_rms_interval_counter_ = 0;
1128 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001129 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1130 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1131 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1132 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001133 }
1134
peahe0eae3c2016-12-14 01:16:23 -08001135 if (private_submodules_->echo_canceller3) {
Per Ã…hgren9aed31c2017-06-29 20:23:27 +02001136 // TODO(peah): Reactivate analogue AGC gain detection once the analogue AGC
1137 // issues have been addressed.
1138 capture_.echo_path_gain_change = false;
peahe0eae3c2016-12-14 01:16:23 -08001139 private_submodules_->echo_canceller3->AnalyzeCapture(capture_buffer);
1140 }
1141
peahbe615622016-02-13 16:40:47 -08001142 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001143 public_submodules_->gain_control->is_enabled()) {
1144 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001145 capture_buffer->channels()[0], capture_buffer->num_channels(),
1146 capture_nonlocked_.capture_processing_format.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001147 }
1148
peah2ace3f92016-09-10 04:42:27 -07001149 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1150 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001151 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1152 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001153 }
1154
peah522d71b2017-02-23 05:16:26 -08001155 if (private_submodules_->echo_canceller3) {
1156 // Force down-mixing of the number of channels after the detection of
1157 // capture signal saturation.
1158 // TODO(peah): Look into ensuring that this kind of tampering with the
1159 // AudioBuffer functionality should not be needed.
1160 capture_buffer->set_num_channels(1);
1161 }
1162
aluebsb2328d12016-01-11 20:32:29 -08001163 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001164 private_submodules_->beamformer->AnalyzeChunk(
1165 *capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001166 // Discards all channels by the leftmost one.
peahde65ddc2016-09-16 15:02:15 -07001167 capture_buffer->set_num_channels(1);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001168 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001169
peahe0eae3c2016-12-14 01:16:23 -08001170 // TODO(peah): Move the AEC3 low-cut filter to this place.
1171 if (private_submodules_->low_cut_filter &&
1172 !private_submodules_->echo_canceller3) {
peah8271d042016-11-22 07:24:52 -08001173 private_submodules_->low_cut_filter->Process(capture_buffer);
1174 }
peahde65ddc2016-09-16 15:02:15 -07001175 RETURN_ON_ERR(
1176 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1177 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001178
1179 // Ensure that the stream delay was set before the call to the
1180 // AEC ProcessCaptureAudio function.
1181 if (public_submodules_->echo_cancellation->is_enabled() &&
1182 !was_stream_delay_set()) {
1183 return AudioProcessing::kStreamParameterNotSetError;
1184 }
1185
peahe0eae3c2016-12-14 01:16:23 -08001186 if (private_submodules_->echo_canceller3) {
peah67995532017-04-10 14:12:41 -07001187 private_submodules_->echo_canceller3->ProcessCapture(
1188 capture_buffer, capture_.echo_path_gain_change);
peah61202ac2017-02-06 03:39:42 -08001189 } else {
1190 RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(
1191 capture_buffer, stream_delay_ms()));
peahe0eae3c2016-12-14 01:16:23 -08001192 }
1193
peahdf3efa82015-11-28 12:35:15 -08001194 if (public_submodules_->echo_control_mobile->is_enabled() &&
1195 public_submodules_->noise_suppression->is_enabled()) {
peahde65ddc2016-09-16 15:02:15 -07001196 capture_buffer->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +00001197 }
peahde65ddc2016-09-16 15:02:15 -07001198 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah1bcfce52016-08-26 07:16:04 -07001199#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001200 if (capture_nonlocked_.intelligibility_enabled) {
aluebsc466bad2016-02-10 12:03:00 -08001201 RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001202 int gain_db = public_submodules_->gain_control->is_enabled() ?
1203 public_submodules_->gain_control->compression_gain_db() :
1204 0;
Alejandro Luebs50411102016-06-30 15:35:41 -07001205 float gain = std::pow(10.f, gain_db / 20.f);
1206 gain *= capture_nonlocked_.level_controller_enabled ?
1207 private_submodules_->level_controller->GetLastGain() :
1208 1.f;
aluebsc466bad2016-02-10 12:03:00 -08001209 public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -07001210 public_submodules_->noise_suppression->NoiseEstimate(), gain);
aluebsc466bad2016-02-10 12:03:00 -08001211 }
peah1bcfce52016-08-26 07:16:04 -07001212#endif
peah253534d2016-03-15 04:32:28 -07001213
1214 // Ensure that the stream delay was set before the call to the
1215 // AECM ProcessCaptureAudio function.
1216 if (public_submodules_->echo_control_mobile->is_enabled() &&
1217 !was_stream_delay_set()) {
1218 return AudioProcessing::kStreamParameterNotSetError;
1219 }
1220
Per Ã…hgren46537a32017-06-07 10:08:10 +02001221 if (!(private_submodules_->echo_canceller3 ||
1222 public_submodules_->echo_cancellation->is_enabled())) {
1223 RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(
1224 capture_buffer, stream_delay_ms()));
1225 }
ivoc9f4a4a02016-10-28 05:39:16 -07001226
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001227 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001228 private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001229 }
1230
peahde65ddc2016-09-16 15:02:15 -07001231 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001232
peahbe615622016-02-13 16:40:47 -08001233 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001234 public_submodules_->gain_control->is_enabled() &&
aluebsb2328d12016-01-11 20:32:29 -08001235 (!capture_nonlocked_.beamformer_enabled ||
peahdf3efa82015-11-28 12:35:15 -08001236 private_submodules_->beamformer->is_target_present())) {
1237 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001238 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1239 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001240 }
peahb8fbb542016-03-15 02:28:08 -07001241 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001242 capture_buffer, echo_cancellation()->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001243
peah2ace3f92016-09-10 04:42:27 -07001244 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1245 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001246 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1247 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001248 }
1249
peah9e6a2902017-05-15 07:19:21 -07001250 if (config_.residual_echo_detector.enabled) {
1251 private_submodules_->residual_echo_detector->AnalyzeCaptureAudio(
1252 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
1253 capture_buffer->num_frames()));
1254 }
1255
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001256 // TODO(aluebs): Investigate if the transient suppression placement should be
1257 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001258 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001259 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001260 private_submodules_->agc_manager.get()
1261 ? private_submodules_->agc_manager->voice_probability()
1262 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001263
peahdf3efa82015-11-28 12:35:15 -08001264 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001265 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1266 capture_buffer->num_channels(),
1267 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1268 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1269 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001270 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001271 }
1272
alessiob3ec96df2017-05-22 06:57:06 -07001273 if (capture_nonlocked_.gain_controller2_enabled) {
1274 private_submodules_->gain_controller2->Process(capture_buffer);
1275 }
1276
peahca4cac72016-06-29 15:26:12 -07001277 if (capture_nonlocked_.level_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001278 private_submodules_->level_controller->Process(capture_buffer);
peahca4cac72016-06-29 15:26:12 -07001279 }
1280
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001281 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001282 public_submodules_->level_estimator->ProcessStream(capture_buffer);
ajm@google.com808e0e02011-08-03 21:08:51 +00001283
peah1b08dc32016-12-20 13:45:58 -08001284 capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>(
1285 capture_buffer->channels_const()[0],
1286 capture_nonlocked_.capture_processing_format.num_frames()));
1287 if (log_rms) {
1288 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1289 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1290 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1291 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1292 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1293 }
1294
peahdf3efa82015-11-28 12:35:15 -08001295 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001296 return kNoError;
1297}
1298
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001299int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001300 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001301 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001302 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001303 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001304 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001305 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001306 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001307 };
1308 if (samples_per_channel != reverse_config.num_frames()) {
1309 return kBadDataLengthError;
1310 }
peahdf3efa82015-11-28 12:35:15 -08001311 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001312}
1313
peahde65ddc2016-09-16 15:02:15 -07001314int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1315 const StreamConfig& input_config,
1316 const StreamConfig& output_config,
1317 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001318 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001319 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001320 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
peah2ace3f92016-09-10 04:42:27 -07001321 if (submodule_states_.RenderMultiBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001322 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1323 dest);
peah2ace3f92016-09-10 04:42:27 -07001324 } else if (formats_.api_format.reverse_input_stream() !=
1325 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001326 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1327 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001328 } else {
peahde65ddc2016-09-16 15:02:15 -07001329 CopyAudioIfNeeded(src, input_config.num_frames(),
1330 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001331 }
1332
1333 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001334}
1335
peahdf3efa82015-11-28 12:35:15 -08001336int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001337 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001338 const StreamConfig& input_config,
1339 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001340 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001341 return kNullPointerError;
1342 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001343
peahde65ddc2016-09-16 15:02:15 -07001344 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001345 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001346 }
1347
peahdf3efa82015-11-28 12:35:15 -08001348 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001349 processing_config.reverse_input_stream() = input_config;
1350 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001351
peahdf3efa82015-11-28 12:35:15 -08001352 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
peahde65ddc2016-09-16 15:02:15 -07001353 assert(input_config.num_frames() ==
1354 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001355
aleloi868f32f2017-05-23 07:20:05 -07001356 if (aec_dump_) {
1357 const size_t channel_size =
1358 formats_.api_format.reverse_input_stream().num_frames();
1359 const size_t num_channels =
1360 formats_.api_format.reverse_input_stream().num_channels();
1361 aec_dump_->WriteRenderStreamMessage(
1362 FloatAudioFrame(src, num_channels, channel_size));
1363 }
peahdf3efa82015-11-28 12:35:15 -08001364 render_.render_audio->CopyFrom(src,
1365 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001366 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001367}
1368
1369int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001370 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001371 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001372 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001373 return kNullPointerError;
1374 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001375 // Must be a native rate.
1376 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1377 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001378 frame->sample_rate_hz_ != kSampleRate32kHz &&
1379 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001380 return kBadSampleRateError;
1381 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001382
Michael Graczyk86c6d332015-07-23 11:41:39 -07001383 if (frame->num_channels_ <= 0) {
1384 return kBadNumberChannelsError;
1385 }
1386
peahdf3efa82015-11-28 12:35:15 -08001387 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001388 processing_config.reverse_input_stream().set_sample_rate_hz(
1389 frame->sample_rate_hz_);
1390 processing_config.reverse_input_stream().set_num_channels(
1391 frame->num_channels_);
1392 processing_config.reverse_output_stream().set_sample_rate_hz(
1393 frame->sample_rate_hz_);
1394 processing_config.reverse_output_stream().set_num_channels(
1395 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001396
peahdf3efa82015-11-28 12:35:15 -08001397 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001398 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001399 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001400 return kBadDataLengthError;
1401 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001402
aleloi868f32f2017-05-23 07:20:05 -07001403 if (aec_dump_) {
1404 aec_dump_->WriteRenderStreamMessage(*frame);
1405 }
1406
peahdf3efa82015-11-28 12:35:15 -08001407 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001408 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001409 render_.render_audio->InterleaveTo(
1410 frame, submodule_states_.RenderMultiBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001411 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001412}
niklase@google.com470e71d2011-07-07 08:21:25 +00001413
peahde65ddc2016-09-16 15:02:15 -07001414int AudioProcessingImpl::ProcessRenderStreamLocked() {
1415 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001416
1417 QueueNonbandedRenderAudio(render_buffer);
1418
peah2ace3f92016-09-10 04:42:27 -07001419 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001420 SampleRateSupportsMultiBand(
1421 formats_.render_processing_format.sample_rate_hz())) {
1422 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001423 }
1424
peah1bcfce52016-08-26 07:16:04 -07001425#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001426 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001427 public_submodules_->intelligibility_enhancer->ProcessRenderAudio(
Alejandro Luebsef009252016-09-20 14:51:56 -07001428 render_buffer);
ekmeyerson60d9b332015-08-14 10:35:55 -07001429 }
peah1bcfce52016-08-26 07:16:04 -07001430#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001431
peahce4d9152017-05-19 01:28:05 -07001432 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1433 QueueBandedRenderAudio(render_buffer);
1434 }
1435
peahe0eae3c2016-12-14 01:16:23 -08001436 // TODO(peah): Perform the queueing ínside QueueRenderAudiuo().
1437 if (private_submodules_->echo_canceller3) {
peahcf02cf12017-04-05 14:18:07 -07001438 private_submodules_->echo_canceller3->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001439 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001440
peah2ace3f92016-09-10 04:42:27 -07001441 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001442 SampleRateSupportsMultiBand(
1443 formats_.render_processing_format.sample_rate_hz())) {
1444 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001445 }
1446
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001447 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001448}
1449
1450int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001451 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001452 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001453 capture_.was_stream_delay_set = true;
1454 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001455
niklase@google.com470e71d2011-07-07 08:21:25 +00001456 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001457 delay = 0;
1458 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001459 }
1460
1461 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1462 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001463 delay = 500;
1464 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001465 }
1466
peahdf3efa82015-11-28 12:35:15 -08001467 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001468 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001469}
1470
1471int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001472 // Used as callback from submodules, hence locking is not allowed.
1473 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001474}
1475
1476bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001477 // Used as callback from submodules, hence locking is not allowed.
1478 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001479}
1480
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001481void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001482 rtc::CritScope cs(&crit_capture_);
1483 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001484}
1485
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001486void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001487 rtc::CritScope cs(&crit_capture_);
1488 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001489}
1490
1491int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001492 rtc::CritScope cs(&crit_capture_);
1493 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001494}
1495
aleloi868f32f2017-05-23 07:20:05 -07001496void AudioProcessingImpl::AttachAecDump(std::unique_ptr<AecDump> aec_dump) {
1497 RTC_DCHECK(aec_dump);
1498 rtc::CritScope cs_render(&crit_render_);
1499 rtc::CritScope cs_capture(&crit_capture_);
1500
1501 // The previously attached AecDump will be destroyed with the
1502 // 'aec_dump' parameter, which is after locks are released.
1503 aec_dump_.swap(aec_dump);
1504 WriteAecDumpConfigMessage(true);
1505 aec_dump_->WriteInitMessage(ToStreamsConfig(formats_.api_format));
1506}
1507
1508void AudioProcessingImpl::DetachAecDump() {
1509 // The d-tor of a task-queue based AecDump blocks until all pending
1510 // tasks are done. This construction avoids blocking while holding
1511 // the render and capture locks.
1512 std::unique_ptr<AecDump> aec_dump = nullptr;
1513 {
1514 rtc::CritScope cs_render(&crit_render_);
1515 rtc::CritScope cs_capture(&crit_capture_);
1516 aec_dump = std::move(aec_dump_);
1517 }
1518}
1519
ivoc4e477a12017-01-15 08:29:46 -08001520AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics() {
1521 residual_echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1522 echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1523 echo_return_loss_enhancement.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1524 a_nlp.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1525}
1526
1527AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics(
1528 const AudioProcessingStatistics& other) = default;
1529
1530AudioProcessing::AudioProcessingStatistics::~AudioProcessingStatistics() =
1531 default;
1532
ivoc3e9a5372016-10-28 07:55:33 -07001533// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
1534AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics()
1535 const {
1536 return AudioProcessingStatistics();
1537}
1538
1539AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics()
1540 const {
1541 AudioProcessingStatistics stats;
1542 EchoCancellation::Metrics metrics;
ivocd0a151c2016-11-02 09:14:37 -07001543 int success = public_submodules_->echo_cancellation->GetMetrics(&metrics);
1544 if (success == Error::kNoError) {
1545 stats.a_nlp.Set(metrics.a_nlp);
1546 stats.divergent_filter_fraction = metrics.divergent_filter_fraction;
1547 stats.echo_return_loss.Set(metrics.echo_return_loss);
1548 stats.echo_return_loss_enhancement.Set(
1549 metrics.echo_return_loss_enhancement);
1550 stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss);
1551 }
ivoc9c192b22017-03-16 04:22:14 -07001552 {
1553 rtc::CritScope cs_capture(&crit_capture_);
1554 stats.residual_echo_likelihood =
1555 private_submodules_->residual_echo_detector->echo_likelihood();
1556 stats.residual_echo_likelihood_recent_max =
1557 private_submodules_->residual_echo_detector
1558 ->echo_likelihood_recent_max();
1559 }
ivoc3e9a5372016-10-28 07:55:33 -07001560 public_submodules_->echo_cancellation->GetDelayMetrics(
1561 &stats.delay_median, &stats.delay_standard_deviation,
1562 &stats.fraction_poor_delays);
1563 return stats;
1564}
1565
niklase@google.com470e71d2011-07-07 08:21:25 +00001566EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
peahb624d8c2016-03-05 03:01:14 -08001567 return public_submodules_->echo_cancellation.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001568}
1569
1570EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
peahbb9edbd2016-03-10 12:54:25 -08001571 return public_submodules_->echo_control_mobile.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001572}
1573
1574GainControl* AudioProcessingImpl::gain_control() const {
peahbe615622016-02-13 16:40:47 -08001575 if (constants_.use_experimental_agc) {
1576 return public_submodules_->gain_control_for_experimental_agc.get();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001577 }
peahbfa97112016-03-10 21:09:04 -08001578 return public_submodules_->gain_control.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001579}
1580
1581HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
peah8271d042016-11-22 07:24:52 -08001582 return high_pass_filter_impl_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001583}
1584
1585LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001586 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001587}
1588
1589NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
solenberg5e465c32015-12-08 13:22:33 -08001590 return public_submodules_->noise_suppression.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001591}
1592
1593VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001594 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001595}
1596
peah8271d042016-11-22 07:24:52 -08001597void AudioProcessingImpl::MutateConfig(
1598 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1599 rtc::CritScope cs_render(&crit_render_);
1600 rtc::CritScope cs_capture(&crit_capture_);
1601 mutator(&config_);
1602 ApplyConfig(config_);
1603}
1604
1605AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1606 rtc::CritScope cs_render(&crit_render_);
1607 rtc::CritScope cs_capture(&crit_capture_);
1608 return config_;
1609}
1610
peah2ace3f92016-09-10 04:42:27 -07001611bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1612 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001613 config_.high_pass_filter.enabled,
peah2ace3f92016-09-10 04:42:27 -07001614 public_submodules_->echo_cancellation->is_enabled(),
1615 public_submodules_->echo_control_mobile->is_enabled(),
ivoc9f4a4a02016-10-28 05:39:16 -07001616 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001617 public_submodules_->noise_suppression->is_enabled(),
1618 capture_nonlocked_.intelligibility_enabled,
1619 capture_nonlocked_.beamformer_enabled,
1620 public_submodules_->gain_control->is_enabled(),
alessiob3ec96df2017-05-22 06:57:06 -07001621 capture_nonlocked_.gain_controller2_enabled,
peah2ace3f92016-09-10 04:42:27 -07001622 capture_nonlocked_.level_controller_enabled,
peahe0eae3c2016-12-14 01:16:23 -08001623 capture_nonlocked_.echo_canceller3_enabled,
peah2ace3f92016-09-10 04:42:27 -07001624 public_submodules_->voice_detection->is_enabled(),
1625 public_submodules_->level_estimator->is_enabled(),
1626 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001627}
1628
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001629
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001630void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001631 if (capture_.transient_suppressor_enabled) {
1632 if (!public_submodules_->transient_suppressor.get()) {
1633 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001634 }
peahdf3efa82015-11-28 12:35:15 -08001635 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001636 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1637 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001638 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001639}
1640
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001641void AudioProcessingImpl::InitializeBeamformer() {
aluebsb2328d12016-01-11 20:32:29 -08001642 if (capture_nonlocked_.beamformer_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001643 if (!private_submodules_->beamformer) {
1644 private_submodules_->beamformer.reset(new NonlinearBeamformer(
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001645 capture_.array_geometry, 1u, capture_.target_direction));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001646 }
peahdf3efa82015-11-28 12:35:15 -08001647 private_submodules_->beamformer->Initialize(kChunkSizeMs,
1648 capture_nonlocked_.split_rate);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001649 }
1650}
1651
ekmeyerson60d9b332015-08-14 10:35:55 -07001652void AudioProcessingImpl::InitializeIntelligibility() {
peah1bcfce52016-08-26 07:16:04 -07001653#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001654 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001655 public_submodules_->intelligibility_enhancer.reset(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -08001656 new IntelligibilityEnhancer(capture_nonlocked_.split_rate,
Alex Luebs57ae8292016-03-09 16:24:34 +01001657 render_.render_audio->num_channels(),
Alejandro Luebsef009252016-09-20 14:51:56 -07001658 render_.render_audio->num_bands(),
Alex Luebs57ae8292016-03-09 16:24:34 +01001659 NoiseSuppressionImpl::num_noise_bins()));
ekmeyerson60d9b332015-08-14 10:35:55 -07001660 }
peah1bcfce52016-08-26 07:16:04 -07001661#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001662}
1663
peah8271d042016-11-22 07:24:52 -08001664void AudioProcessingImpl::InitializeLowCutFilter() {
1665 if (config_.high_pass_filter.enabled) {
1666 private_submodules_->low_cut_filter.reset(
1667 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1668 } else {
1669 private_submodules_->low_cut_filter.reset();
1670 }
1671}
alessiob3ec96df2017-05-22 06:57:06 -07001672
peahe0eae3c2016-12-14 01:16:23 -08001673void AudioProcessingImpl::InitializeEchoCanceller3() {
1674 if (capture_nonlocked_.echo_canceller3_enabled) {
peah697a5902017-06-30 07:06:10 -07001675 private_submodules_->echo_canceller3.reset(new EchoCanceller3(
1676 config_.echo_canceller3, proc_sample_rate_hz(), true));
peahe0eae3c2016-12-14 01:16:23 -08001677 } else {
1678 private_submodules_->echo_canceller3.reset();
1679 }
1680}
peah8271d042016-11-22 07:24:52 -08001681
alessiob3ec96df2017-05-22 06:57:06 -07001682void AudioProcessingImpl::InitializeGainController2() {
1683 if (capture_nonlocked_.gain_controller2_enabled) {
1684 private_submodules_->gain_controller2.reset(
1685 new GainController2(proc_sample_rate_hz()));
1686 } else {
1687 private_submodules_->gain_controller2.reset();
1688 }
1689}
1690
peahca4cac72016-06-29 15:26:12 -07001691void AudioProcessingImpl::InitializeLevelController() {
1692 private_submodules_->level_controller->Initialize(proc_sample_rate_hz());
1693}
1694
ivoc9f4a4a02016-10-28 05:39:16 -07001695void AudioProcessingImpl::InitializeResidualEchoDetector() {
1696 private_submodules_->residual_echo_detector->Initialize();
1697}
1698
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001699void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001700 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001701
1702 if (echo_cancellation()->is_enabled()) {
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001703 // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
1704 // If a stream has echo we know that the echo_cancellation is in process.
peahdf3efa82015-11-28 12:35:15 -08001705 if (capture_.stream_delay_jumps == -1 &&
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001706 echo_cancellation()->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001707 capture_.stream_delay_jumps = 0;
1708 }
1709 if (capture_.aec_system_delay_jumps == -1 &&
1710 echo_cancellation()->stream_has_echo()) {
1711 capture_.aec_system_delay_jumps = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001712 }
1713
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001714 // Detect a jump in platform reported system delay and log the difference.
peahdf3efa82015-11-28 12:35:15 -08001715 const int diff_stream_delay_ms =
1716 capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
1717 if (diff_stream_delay_ms > kMinDiffDelayMs &&
1718 capture_.last_stream_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001719 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1720 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
peahdf3efa82015-11-28 12:35:15 -08001721 if (capture_.stream_delay_jumps == -1) {
1722 capture_.stream_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001723 }
peahdf3efa82015-11-28 12:35:15 -08001724 capture_.stream_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001725 }
peahdf3efa82015-11-28 12:35:15 -08001726 capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001727
1728 // Detect a jump in AEC system delay and log the difference.
peah20028c42016-03-04 11:50:54 -08001729 const int samples_per_ms =
peahdf3efa82015-11-28 12:35:15 -08001730 rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
peah20028c42016-03-04 11:50:54 -08001731 RTC_DCHECK_LT(0, samples_per_ms);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001732 const int aec_system_delay_ms =
peah20028c42016-03-04 11:50:54 -08001733 public_submodules_->echo_cancellation->GetSystemDelayInSamples() /
1734 samples_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001735 const int diff_aec_system_delay_ms =
peahdf3efa82015-11-28 12:35:15 -08001736 aec_system_delay_ms - capture_.last_aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001737 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
peahdf3efa82015-11-28 12:35:15 -08001738 capture_.last_aec_system_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001739 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1740 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1741 100);
peahdf3efa82015-11-28 12:35:15 -08001742 if (capture_.aec_system_delay_jumps == -1) {
1743 capture_.aec_system_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001744 }
peahdf3efa82015-11-28 12:35:15 -08001745 capture_.aec_system_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001746 }
peahdf3efa82015-11-28 12:35:15 -08001747 capture_.last_aec_system_delay_ms = aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001748 }
1749}
1750
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001751void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
peahdf3efa82015-11-28 12:35:15 -08001752 // Run in a single-threaded manner.
1753 rtc::CritScope cs_render(&crit_render_);
1754 rtc::CritScope cs_capture(&crit_capture_);
1755
1756 if (capture_.stream_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001757 RTC_HISTOGRAM_ENUMERATION(
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001758 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
peahdf3efa82015-11-28 12:35:15 -08001759 capture_.stream_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001760 }
peahdf3efa82015-11-28 12:35:15 -08001761 capture_.stream_delay_jumps = -1;
1762 capture_.last_stream_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001763
peahdf3efa82015-11-28 12:35:15 -08001764 if (capture_.aec_system_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001765 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1766 capture_.aec_system_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001767 }
peahdf3efa82015-11-28 12:35:15 -08001768 capture_.aec_system_delay_jumps = -1;
1769 capture_.last_aec_system_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001770}
1771
aleloi868f32f2017-05-23 07:20:05 -07001772void AudioProcessingImpl::WriteAecDumpConfigMessage(bool forced) {
1773 if (!aec_dump_) {
1774 return;
1775 }
1776 std::string experiments_description =
1777 public_submodules_->echo_cancellation->GetExperimentsDescription();
1778 // TODO(peah): Add semicolon-separated concatenations of experiment
1779 // descriptions for other submodules.
1780 if (capture_nonlocked_.level_controller_enabled) {
1781 experiments_description += "LevelController;";
1782 }
1783 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
1784 experiments_description += "AgcClippingLevelExperiment;";
1785 }
1786 if (capture_nonlocked_.echo_canceller3_enabled) {
1787 experiments_description += "EchoCanceller3;";
1788 }
1789
1790 InternalAPMConfig apm_config;
1791
1792 apm_config.aec_enabled = public_submodules_->echo_cancellation->is_enabled();
1793 apm_config.aec_delay_agnostic_enabled =
1794 public_submodules_->echo_cancellation->is_delay_agnostic_enabled();
1795 apm_config.aec_drift_compensation_enabled =
1796 public_submodules_->echo_cancellation->is_drift_compensation_enabled();
1797 apm_config.aec_extended_filter_enabled =
1798 public_submodules_->echo_cancellation->is_extended_filter_enabled();
1799 apm_config.aec_suppression_level = static_cast<int>(
1800 public_submodules_->echo_cancellation->suppression_level());
1801
1802 apm_config.aecm_enabled =
1803 public_submodules_->echo_control_mobile->is_enabled();
1804 apm_config.aecm_comfort_noise_enabled =
1805 public_submodules_->echo_control_mobile->is_comfort_noise_enabled();
1806 apm_config.aecm_routing_mode =
1807 static_cast<int>(public_submodules_->echo_control_mobile->routing_mode());
1808
1809 apm_config.agc_enabled = public_submodules_->gain_control->is_enabled();
1810 apm_config.agc_mode =
1811 static_cast<int>(public_submodules_->gain_control->mode());
1812 apm_config.agc_limiter_enabled =
1813 public_submodules_->gain_control->is_limiter_enabled();
1814 apm_config.noise_robust_agc_enabled = constants_.use_experimental_agc;
1815
1816 apm_config.hpf_enabled = config_.high_pass_filter.enabled;
1817
1818 apm_config.ns_enabled = public_submodules_->noise_suppression->is_enabled();
1819 apm_config.ns_level =
1820 static_cast<int>(public_submodules_->noise_suppression->level());
1821
1822 apm_config.transient_suppression_enabled =
1823 capture_.transient_suppressor_enabled;
1824 apm_config.intelligibility_enhancer_enabled =
1825 capture_nonlocked_.intelligibility_enabled;
1826 apm_config.experiments_description = experiments_description;
1827
1828 if (!forced && apm_config == apm_config_for_aec_dump_) {
1829 return;
1830 }
1831 aec_dump_->WriteConfig(apm_config);
1832 apm_config_for_aec_dump_ = apm_config;
1833}
1834
1835void AudioProcessingImpl::RecordUnprocessedCaptureStream(
1836 const float* const* src) {
1837 RTC_DCHECK(aec_dump_);
1838 WriteAecDumpConfigMessage(false);
1839
1840 const size_t channel_size = formats_.api_format.input_stream().num_frames();
1841 const size_t num_channels = formats_.api_format.input_stream().num_channels();
1842 aec_dump_->AddCaptureStreamInput(
1843 FloatAudioFrame(src, num_channels, channel_size));
1844 RecordAudioProcessingState();
1845}
1846
1847void AudioProcessingImpl::RecordUnprocessedCaptureStream(
1848 const AudioFrame& capture_frame) {
1849 RTC_DCHECK(aec_dump_);
1850 WriteAecDumpConfigMessage(false);
1851
1852 aec_dump_->AddCaptureStreamInput(capture_frame);
1853 RecordAudioProcessingState();
1854}
1855
1856void AudioProcessingImpl::RecordProcessedCaptureStream(
1857 const float* const* processed_capture_stream) {
1858 RTC_DCHECK(aec_dump_);
1859
1860 const size_t channel_size = formats_.api_format.output_stream().num_frames();
1861 const size_t num_channels =
1862 formats_.api_format.output_stream().num_channels();
1863 aec_dump_->AddCaptureStreamOutput(
1864 FloatAudioFrame(processed_capture_stream, num_channels, channel_size));
1865 aec_dump_->WriteCaptureStreamMessage();
1866}
1867
1868void AudioProcessingImpl::RecordProcessedCaptureStream(
1869 const AudioFrame& processed_capture_frame) {
1870 RTC_DCHECK(aec_dump_);
1871
1872 aec_dump_->AddCaptureStreamOutput(processed_capture_frame);
1873 aec_dump_->WriteCaptureStreamMessage();
1874}
1875
1876void AudioProcessingImpl::RecordAudioProcessingState() {
1877 RTC_DCHECK(aec_dump_);
1878 AecDump::AudioProcessingState audio_proc_state;
1879 audio_proc_state.delay = capture_nonlocked_.stream_delay_ms;
1880 audio_proc_state.drift =
1881 public_submodules_->echo_cancellation->stream_drift_samples();
1882 audio_proc_state.level = gain_control()->stream_analog_level();
1883 audio_proc_state.keypress = capture_.key_pressed;
1884 aec_dump_->AddAudioProcessingState(audio_proc_state);
1885}
1886
kwiberg83ffe452016-08-29 14:46:07 -07001887AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
1888 bool transient_suppressor_enabled,
1889 const std::vector<Point>& array_geometry,
1890 SphericalPointf target_direction)
1891 : aec_system_delay_jumps(-1),
1892 delay_offset_ms(0),
1893 was_stream_delay_set(false),
1894 last_stream_delay_ms(0),
1895 last_aec_system_delay_ms(0),
1896 stream_delay_jumps(-1),
1897 output_will_be_muted(false),
1898 key_pressed(false),
1899 transient_suppressor_enabled(transient_suppressor_enabled),
1900 array_geometry(array_geometry),
1901 target_direction(target_direction),
peahde65ddc2016-09-16 15:02:15 -07001902 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07001903 split_rate(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07001904 echo_path_gain_change(false) {}
kwiberg83ffe452016-08-29 14:46:07 -07001905
1906AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
1907
1908AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
1909
1910AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
1911
niklase@google.com470e71d2011-07-07 08:21:25 +00001912} // namespace webrtc