blob: 0240012829ffc5d82d2ad55fea45e8225e52548b [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
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000011#include "webrtc/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
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020017#include "webrtc/base/checks.h"
nisse368f5cf2017-04-05 05:00:33 -070018#include "webrtc/base/logging.h"
xians@webrtc.orge46bc772014-10-10 08:36:56 +000019#include "webrtc/base/platform_file.h"
peah369f8282015-12-17 06:42:29 -080020#include "webrtc/base/trace_event.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070021#include "webrtc/common_audio/audio_converter.h"
Michael Graczykdfa36052015-03-25 16:37:27 -070022#include "webrtc/common_audio/channel_buffer.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070023#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000024#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020025#include "webrtc/modules/audio_processing/aec/aec_core.h"
peahe0eae3c2016-12-14 01:16:23 -080026#include "webrtc/modules/audio_processing/aec3/echo_canceller3.h"
alessiob3ec96df2017-05-22 06:57:06 -070027#include "webrtc/modules/audio_processing/agc2/gain_controller2.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000028#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000029#include "webrtc/modules/audio_processing/audio_buffer.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000030#include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000031#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000032#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000033#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
peahbe615622016-02-13 16:40:47 -080034#include "webrtc/modules/audio_processing/gain_control_for_experimental_agc.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000035#include "webrtc/modules/audio_processing/gain_control_impl.h"
peah1bcfce52016-08-26 07:16:04 -070036#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -070037#include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h"
peah1bcfce52016-08-26 07:16:04 -070038#endif
peahca4cac72016-06-29 15:26:12 -070039#include "webrtc/modules/audio_processing/level_controller/level_controller.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000040#include "webrtc/modules/audio_processing/level_estimator_impl.h"
peah8271d042016-11-22 07:24:52 -080041#include "webrtc/modules/audio_processing/low_cut_filter.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000042#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
ivoc9f4a4a02016-10-28 05:39:16 -070043#include "webrtc/modules/audio_processing/residual_echo_detector.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000044#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000045#include "webrtc/modules/audio_processing/voice_detection_impl.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010046#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010047#include "webrtc/system_wrappers/include/file_wrapper.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010048#include "webrtc/system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000049
50#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
51// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000052#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000053#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000054#else
kjellander78ddd732016-02-09 08:13:06 -080055#include "webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000056#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000057#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000058
peah1bcfce52016-08-26 07:16:04 -070059// Check to verify that the define for the intelligibility enhancer is properly
60// set.
61#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
62 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
63 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
64#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
65#endif
66
Michael Graczyk86c6d332015-07-23 11:41:39 -070067#define RETURN_ON_ERR(expr) \
68 do { \
69 int err = (expr); \
70 if (err != kNoError) { \
71 return err; \
72 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000073 } while (0)
74
niklase@google.com470e71d2011-07-07 08:21:25 +000075namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070076
kwibergd59d3bb2016-09-13 07:49:33 -070077constexpr int AudioProcessing::kNativeSampleRatesHz[];
aluebsdf6416a2016-03-16 18:26:35 -070078
Michael Graczyk86c6d332015-07-23 11:41:39 -070079namespace {
80
81static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
82 switch (layout) {
83 case AudioProcessing::kMono:
84 case AudioProcessing::kStereo:
85 return false;
86 case AudioProcessing::kMonoAndKeyboard:
87 case AudioProcessing::kStereoAndKeyboard:
88 return true;
89 }
90
kwiberg9e2be5f2016-09-14 05:23:22 -070091 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070092 return false;
93}
aluebsdf6416a2016-03-16 18:26:35 -070094
peah2ace3f92016-09-10 04:42:27 -070095bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070096 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
97 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
98}
99
peah2ace3f92016-09-10 04:42:27 -0700100int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) {
101#ifdef WEBRTC_ARCH_ARM_FAMILY
kwibergd59d3bb2016-09-13 07:49:33 -0700102 constexpr int kMaxSplittingNativeProcessRate =
103 AudioProcessing::kSampleRate32kHz;
peah2ace3f92016-09-10 04:42:27 -0700104#else
kwibergd59d3bb2016-09-13 07:49:33 -0700105 constexpr int kMaxSplittingNativeProcessRate =
106 AudioProcessing::kSampleRate48kHz;
peah2ace3f92016-09-10 04:42:27 -0700107#endif
kwibergd59d3bb2016-09-13 07:49:33 -0700108 static_assert(
109 kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz,
110 "");
peah2ace3f92016-09-10 04:42:27 -0700111 const int uppermost_native_rate = band_splitting_required
112 ? kMaxSplittingNativeProcessRate
113 : AudioProcessing::kSampleRate48kHz;
114
115 for (auto rate : AudioProcessing::kNativeSampleRatesHz) {
116 if (rate >= uppermost_native_rate) {
117 return uppermost_native_rate;
118 }
119 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700120 return rate;
121 }
122 }
peah2ace3f92016-09-10 04:42:27 -0700123 RTC_NOTREACHED();
124 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700125}
126
peah9e6a2902017-05-15 07:19:21 -0700127// Maximum lengths that frame of samples being passed from the render side to
128// the capture side can have (does not apply to AEC3).
129static const size_t kMaxAllowedValuesOfSamplesPerBand = 160;
130static const size_t kMaxAllowedValuesOfSamplesPerFrame = 480;
131
peah764e3642016-10-22 05:04:30 -0700132// Maximum number of frames to buffer in the render queue.
133// TODO(peah): Decrease this once we properly handle hugely unbalanced
134// reverse and forward call numbers.
135static const size_t kMaxNumFramesToBuffer = 100;
136
peah8271d042016-11-22 07:24:52 -0800137class HighPassFilterImpl : public HighPassFilter {
138 public:
139 explicit HighPassFilterImpl(AudioProcessingImpl* apm) : apm_(apm) {}
140 ~HighPassFilterImpl() override = default;
141
142 // HighPassFilter implementation.
143 int Enable(bool enable) override {
144 apm_->MutateConfig([enable](AudioProcessing::Config* config) {
145 config->high_pass_filter.enabled = enable;
146 });
147
148 return AudioProcessing::kNoError;
149 }
150
151 bool is_enabled() const override {
152 return apm_->GetConfig().high_pass_filter.enabled;
153 }
154
155 private:
156 AudioProcessingImpl* apm_;
157 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(HighPassFilterImpl);
158};
159
Michael Graczyk86c6d332015-07-23 11:41:39 -0700160} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000161
162// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000163static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000164
peah2ace3f92016-09-10 04:42:27 -0700165AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates() {}
166
167bool AudioProcessingImpl::ApmSubmoduleStates::Update(
peah8271d042016-11-22 07:24:52 -0800168 bool low_cut_filter_enabled,
peah2ace3f92016-09-10 04:42:27 -0700169 bool echo_canceller_enabled,
170 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700171 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700172 bool noise_suppressor_enabled,
173 bool intelligibility_enhancer_enabled,
174 bool beamformer_enabled,
175 bool adaptive_gain_controller_enabled,
alessiob3ec96df2017-05-22 06:57:06 -0700176 bool gain_controller2_enabled,
peah2ace3f92016-09-10 04:42:27 -0700177 bool level_controller_enabled,
peahe0eae3c2016-12-14 01:16:23 -0800178 bool echo_canceller3_enabled,
peah2ace3f92016-09-10 04:42:27 -0700179 bool voice_activity_detector_enabled,
180 bool level_estimator_enabled,
181 bool transient_suppressor_enabled) {
182 bool changed = false;
peah8271d042016-11-22 07:24:52 -0800183 changed |= (low_cut_filter_enabled != low_cut_filter_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700184 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
185 changed |=
186 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700187 changed |=
188 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700189 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
190 changed |=
191 (intelligibility_enhancer_enabled != intelligibility_enhancer_enabled_);
192 changed |= (beamformer_enabled != beamformer_enabled_);
193 changed |=
194 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
alessiob3ec96df2017-05-22 06:57:06 -0700195 changed |=
196 (gain_controller2_enabled != gain_controller2_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700197 changed |= (level_controller_enabled != level_controller_enabled_);
peahe0eae3c2016-12-14 01:16:23 -0800198 changed |= (echo_canceller3_enabled != echo_canceller3_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700199 changed |= (level_estimator_enabled != level_estimator_enabled_);
200 changed |=
201 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
202 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
203 if (changed) {
peah8271d042016-11-22 07:24:52 -0800204 low_cut_filter_enabled_ = low_cut_filter_enabled;
peah2ace3f92016-09-10 04:42:27 -0700205 echo_canceller_enabled_ = echo_canceller_enabled;
206 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700207 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700208 noise_suppressor_enabled_ = noise_suppressor_enabled;
209 intelligibility_enhancer_enabled_ = intelligibility_enhancer_enabled;
210 beamformer_enabled_ = beamformer_enabled;
211 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
alessiob3ec96df2017-05-22 06:57:06 -0700212 gain_controller2_enabled_ = gain_controller2_enabled;
peah2ace3f92016-09-10 04:42:27 -0700213 level_controller_enabled_ = level_controller_enabled;
peahe0eae3c2016-12-14 01:16:23 -0800214 echo_canceller3_enabled_ = echo_canceller3_enabled;
peah2ace3f92016-09-10 04:42:27 -0700215 level_estimator_enabled_ = level_estimator_enabled;
216 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
217 transient_suppressor_enabled_ = transient_suppressor_enabled;
218 }
219
220 changed |= first_update_;
221 first_update_ = false;
222 return changed;
223}
224
225bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
226 const {
227#if WEBRTC_INTELLIGIBILITY_ENHANCER
228 return CaptureMultiBandProcessingActive() ||
peah52775842017-05-16 06:14:09 -0700229 intelligibility_enhancer_enabled_ || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700230#else
peah52775842017-05-16 06:14:09 -0700231 return CaptureMultiBandProcessingActive() || voice_activity_detector_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700232#endif
233}
234
235bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
236 const {
peah8271d042016-11-22 07:24:52 -0800237 return low_cut_filter_enabled_ || echo_canceller_enabled_ ||
peah2ace3f92016-09-10 04:42:27 -0700238 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
peahe0eae3c2016-12-14 01:16:23 -0800239 beamformer_enabled_ || adaptive_gain_controller_enabled_ ||
240 echo_canceller3_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700241}
242
243bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
244 const {
245 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
ivoc20270be2016-11-15 05:24:35 -0800246 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_ ||
peah52775842017-05-16 06:14:09 -0700247 echo_canceller3_enabled_;
peah2ace3f92016-09-10 04:42:27 -0700248}
249
250bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
251 const {
252#if WEBRTC_INTELLIGIBILITY_ENHANCER
253 return intelligibility_enhancer_enabled_;
254#else
255 return false;
256#endif
257}
258
solenberg5e465c32015-12-08 13:22:33 -0800259struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800260 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800261 // Accessed externally of APM without any lock acquired.
peahb624d8c2016-03-05 03:01:14 -0800262 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
peahbb9edbd2016-03-10 12:54:25 -0800263 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
peahbfa97112016-03-10 21:09:04 -0800264 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800265 std::unique_ptr<LevelEstimatorImpl> level_estimator;
266 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
267 std::unique_ptr<VoiceDetectionImpl> voice_detection;
268 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800269 gain_control_for_experimental_agc;
solenberg5e465c32015-12-08 13:22:33 -0800270
271 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800272 std::unique_ptr<TransientSuppressor> transient_suppressor;
peah1bcfce52016-08-26 07:16:04 -0700273#if WEBRTC_INTELLIGIBILITY_ENHANCER
kwiberg88788ad2016-02-19 07:04:49 -0800274 std::unique_ptr<IntelligibilityEnhancer> intelligibility_enhancer;
peah1bcfce52016-08-26 07:16:04 -0700275#endif
solenberg5e465c32015-12-08 13:22:33 -0800276};
277
278struct AudioProcessingImpl::ApmPrivateSubmodules {
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700279 explicit ApmPrivateSubmodules(NonlinearBeamformer* beamformer)
solenberg5e465c32015-12-08 13:22:33 -0800280 : beamformer(beamformer) {}
281 // Accessed internally from capture or during initialization
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700282 std::unique_ptr<NonlinearBeamformer> beamformer;
kwiberg88788ad2016-02-19 07:04:49 -0800283 std::unique_ptr<AgcManagerDirect> agc_manager;
alessiob3ec96df2017-05-22 06:57:06 -0700284 std::unique_ptr<GainController2> gain_controller2;
peah8271d042016-11-22 07:24:52 -0800285 std::unique_ptr<LowCutFilter> low_cut_filter;
peahca4cac72016-06-29 15:26:12 -0700286 std::unique_ptr<LevelController> level_controller;
ivoc9f4a4a02016-10-28 05:39:16 -0700287 std::unique_ptr<ResidualEchoDetector> residual_echo_detector;
peahe0eae3c2016-12-14 01:16:23 -0800288 std::unique_ptr<EchoCanceller3> echo_canceller3;
solenberg5e465c32015-12-08 13:22:33 -0800289};
290
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000291AudioProcessing* AudioProcessing::Create() {
peah88ac8532016-09-12 16:47:25 -0700292 webrtc::Config config;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000293 return Create(config, nullptr);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000294}
295
peah88ac8532016-09-12 16:47:25 -0700296AudioProcessing* AudioProcessing::Create(const webrtc::Config& config) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000297 return Create(config, nullptr);
298}
299
peah88ac8532016-09-12 16:47:25 -0700300AudioProcessing* AudioProcessing::Create(const webrtc::Config& config,
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700301 NonlinearBeamformer* beamformer) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000302 AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000303 if (apm->Initialize() != kNoError) {
304 delete apm;
peahdf3efa82015-11-28 12:35:15 -0800305 apm = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000306 }
307
308 return apm;
309}
310
peah88ac8532016-09-12 16:47:25 -0700311AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000312 : AudioProcessingImpl(config, nullptr) {}
313
peah88ac8532016-09-12 16:47:25 -0700314AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config,
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700315 NonlinearBeamformer* beamformer)
peah8271d042016-11-22 07:24:52 -0800316 : high_pass_filter_impl_(new HighPassFilterImpl(this)),
317 public_submodules_(new ApmPublicSubmodules()),
peahdf3efa82015-11-28 12:35:15 -0800318 private_submodules_(new ApmPrivateSubmodules(beamformer)),
319 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
henrik.lundinbd681b92016-12-05 09:08:42 -0800320 config.Get<ExperimentalAgc>().clipped_level_min,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000321#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700322 false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000323#else
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700324 config.Get<ExperimentalAgc>().enabled),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000325#endif
andrew1c7075f2015-06-24 18:14:14 -0700326#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
aluebs2a346882016-01-11 18:04:30 -0800327 capture_(false,
andrew1c7075f2015-06-24 18:14:14 -0700328#else
aluebs2a346882016-01-11 18:04:30 -0800329 capture_(config.Get<ExperimentalNs>().enabled,
andrew1c7075f2015-06-24 18:14:14 -0700330#endif
aluebs2a346882016-01-11 18:04:30 -0800331 config.Get<Beamforming>().array_geometry,
aluebsb2328d12016-01-11 20:32:29 -0800332 config.Get<Beamforming>().target_direction),
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700333 capture_nonlocked_(config.Get<Beamforming>().enabled,
peah88ac8532016-09-12 16:47:25 -0700334 config.Get<Intelligibility>().enabled) {
peahdf3efa82015-11-28 12:35:15 -0800335 {
336 rtc::CritScope cs_render(&crit_render_);
337 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000338
peahb624d8c2016-03-05 03:01:14 -0800339 public_submodules_->echo_cancellation.reset(
peahb58a1582016-03-15 09:34:24 -0700340 new EchoCancellationImpl(&crit_render_, &crit_capture_));
peahbb9edbd2016-03-10 12:54:25 -0800341 public_submodules_->echo_control_mobile.reset(
peah253534d2016-03-15 04:32:28 -0700342 new EchoControlMobileImpl(&crit_render_, &crit_capture_));
peahbfa97112016-03-10 21:09:04 -0800343 public_submodules_->gain_control.reset(
peahb8fbb542016-03-15 02:28:08 -0700344 new GainControlImpl(&crit_capture_, &crit_capture_));
solenberg949028f2015-12-15 11:39:38 -0800345 public_submodules_->level_estimator.reset(
346 new LevelEstimatorImpl(&crit_capture_));
solenberg5e465c32015-12-08 13:22:33 -0800347 public_submodules_->noise_suppression.reset(
348 new NoiseSuppressionImpl(&crit_capture_));
solenberga29386c2015-12-16 03:31:12 -0800349 public_submodules_->voice_detection.reset(
350 new VoiceDetectionImpl(&crit_capture_));
peahbe615622016-02-13 16:40:47 -0800351 public_submodules_->gain_control_for_experimental_agc.reset(
peahbfa97112016-03-10 21:09:04 -0800352 new GainControlForExperimentalAgc(
353 public_submodules_->gain_control.get(), &crit_capture_));
ivoc9f4a4a02016-10-28 05:39:16 -0700354 private_submodules_->residual_echo_detector.reset(
355 new ResidualEchoDetector());
peahca4cac72016-06-29 15:26:12 -0700356
peahc19f3122016-10-07 14:54:10 -0700357 // TODO(peah): Move this creation to happen only when the level controller
358 // is enabled.
peahca4cac72016-06-29 15:26:12 -0700359 private_submodules_->level_controller.reset(new LevelController());
peahdf3efa82015-11-28 12:35:15 -0800360 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000361
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000362 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000363}
364
365AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800366 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800367 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800368 private_submodules_->agc_manager.reset();
369 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800370 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000371
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000372#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700373 debug_dump_.debug_file->CloseFile();
peahdf3efa82015-11-28 12:35:15 -0800374#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000375}
376
niklase@google.com470e71d2011-07-07 08:21:25 +0000377int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800378 // Run in a single-threaded manner during initialization.
379 rtc::CritScope cs_render(&crit_render_);
380 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000381 return InitializeLocked();
382}
383
peahde65ddc2016-09-16 15:02:15 -0700384int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
385 int capture_output_sample_rate_hz,
386 int render_input_sample_rate_hz,
387 ChannelLayout capture_input_layout,
388 ChannelLayout capture_output_layout,
389 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700390 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700391 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
392 LayoutHasKeyboard(capture_input_layout)},
393 {capture_output_sample_rate_hz,
394 ChannelsFromLayout(capture_output_layout),
395 LayoutHasKeyboard(capture_output_layout)},
396 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
397 LayoutHasKeyboard(render_input_layout)},
398 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
399 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700400
401 return Initialize(processing_config);
402}
403
404int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800405 // Run in a single-threaded manner during initialization.
406 rtc::CritScope cs_render(&crit_render_);
407 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700408 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000409}
410
peahdf3efa82015-11-28 12:35:15 -0800411int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800412 const ProcessingConfig& processing_config) {
peah2ace3f92016-09-10 04:42:27 -0700413 return MaybeInitialize(processing_config, false);
peah81b9bfe2015-11-27 02:47:28 -0800414}
415
peahdf3efa82015-11-28 12:35:15 -0800416int AudioProcessingImpl::MaybeInitializeCapture(
peah2ace3f92016-09-10 04:42:27 -0700417 const ProcessingConfig& processing_config,
418 bool force_initialization) {
419 return MaybeInitialize(processing_config, force_initialization);
peah81b9bfe2015-11-27 02:47:28 -0800420}
421
kwiberg83ffe452016-08-29 14:46:07 -0700422#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
423
424AudioProcessingImpl::ApmDebugDumpThreadState::ApmDebugDumpThreadState()
425 : event_msg(new audioproc::Event()) {}
426
427AudioProcessingImpl::ApmDebugDumpThreadState::~ApmDebugDumpThreadState() {}
428
429AudioProcessingImpl::ApmDebugDumpState::ApmDebugDumpState()
430 : debug_file(FileWrapper::Create()) {}
431
432AudioProcessingImpl::ApmDebugDumpState::~ApmDebugDumpState() {}
433
434#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
435
peah192164e2015-11-17 02:16:45 -0800436// Calls InitializeLocked() if any of the audio parameters have changed from
peahdf3efa82015-11-28 12:35:15 -0800437// their current values (needs to be called while holding the crit_render_lock).
438int AudioProcessingImpl::MaybeInitialize(
peah2ace3f92016-09-10 04:42:27 -0700439 const ProcessingConfig& processing_config,
440 bool force_initialization) {
peahdf3efa82015-11-28 12:35:15 -0800441 // Called from both threads. Thread check is therefore not possible.
peah2ace3f92016-09-10 04:42:27 -0700442 if (processing_config == formats_.api_format && !force_initialization) {
peah192164e2015-11-17 02:16:45 -0800443 return kNoError;
444 }
peahdf3efa82015-11-28 12:35:15 -0800445
446 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800447 return InitializeLocked(processing_config);
448}
449
niklase@google.com470e71d2011-07-07 08:21:25 +0000450int AudioProcessingImpl::InitializeLocked() {
peah522d71b2017-02-23 05:16:26 -0800451 const int capture_audiobuffer_num_channels =
452 capture_nonlocked_.beamformer_enabled
453 ? formats_.api_format.input_stream().num_channels()
454 : formats_.api_format.output_stream().num_channels();
455
peahde65ddc2016-09-16 15:02:15 -0700456 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800457 formats_.api_format.reverse_output_stream().num_frames() == 0
peahde65ddc2016-09-16 15:02:15 -0700458 ? formats_.render_processing_format.num_frames()
peahdf3efa82015-11-28 12:35:15 -0800459 : formats_.api_format.reverse_output_stream().num_frames();
460 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
461 render_.render_audio.reset(new AudioBuffer(
462 formats_.api_format.reverse_input_stream().num_frames(),
463 formats_.api_format.reverse_input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700464 formats_.render_processing_format.num_frames(),
465 formats_.render_processing_format.num_channels(),
466 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700467 if (formats_.api_format.reverse_input_stream() !=
468 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800469 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800470 formats_.api_format.reverse_input_stream().num_channels(),
471 formats_.api_format.reverse_input_stream().num_frames(),
472 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800473 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700474 } else {
peahdf3efa82015-11-28 12:35:15 -0800475 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700476 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700477 } else {
peahdf3efa82015-11-28 12:35:15 -0800478 render_.render_audio.reset(nullptr);
479 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700480 }
peahce4d9152017-05-19 01:28:05 -0700481
peahdf3efa82015-11-28 12:35:15 -0800482 capture_.capture_audio.reset(
483 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
484 formats_.api_format.input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700485 capture_nonlocked_.capture_processing_format.num_frames(),
486 capture_audiobuffer_num_channels,
peahdf3efa82015-11-28 12:35:15 -0800487 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000488
peahde65ddc2016-09-16 15:02:15 -0700489 public_submodules_->echo_cancellation->Initialize(
490 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
491 num_proc_channels());
peah764e3642016-10-22 05:04:30 -0700492 AllocateRenderQueue();
493
ivoc3e9a5372016-10-28 07:55:33 -0700494 int success = public_submodules_->echo_cancellation->enable_metrics(true);
495 RTC_DCHECK_EQ(0, success);
496 success = public_submodules_->echo_cancellation->enable_delay_logging(true);
497 RTC_DCHECK_EQ(0, success);
peahde65ddc2016-09-16 15:02:15 -0700498 public_submodules_->echo_control_mobile->Initialize(
499 proc_split_sample_rate_hz(), num_reverse_channels(),
500 num_output_channels());
peah135259a2016-10-28 03:12:11 -0700501
502 public_submodules_->gain_control->Initialize(num_proc_channels(),
503 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700504 if (constants_.use_experimental_agc) {
505 if (!private_submodules_->agc_manager.get()) {
506 private_submodules_->agc_manager.reset(new AgcManagerDirect(
507 public_submodules_->gain_control.get(),
508 public_submodules_->gain_control_for_experimental_agc.get(),
henrik.lundinbd681b92016-12-05 09:08:42 -0800509 constants_.agc_startup_min_volume, constants_.agc_clipped_level_min));
peahde65ddc2016-09-16 15:02:15 -0700510 }
511 private_submodules_->agc_manager->Initialize();
512 private_submodules_->agc_manager->SetCaptureMuted(
513 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700514 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700515 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200516 InitializeTransient();
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000517 InitializeBeamformer();
peah1bcfce52016-08-26 07:16:04 -0700518#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -0700519 InitializeIntelligibility();
peah1bcfce52016-08-26 07:16:04 -0700520#endif
peah8271d042016-11-22 07:24:52 -0800521 InitializeLowCutFilter();
peahde65ddc2016-09-16 15:02:15 -0700522 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
523 proc_sample_rate_hz());
524 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
525 public_submodules_->level_estimator->Initialize();
peahca4cac72016-06-29 15:26:12 -0700526 InitializeLevelController();
ivoc9f4a4a02016-10-28 05:39:16 -0700527 InitializeResidualEchoDetector();
peahe0eae3c2016-12-14 01:16:23 -0800528 InitializeEchoCanceller3();
alessiob3ec96df2017-05-22 06:57:06 -0700529 InitializeGainController2();
solenberg70f99032015-12-08 11:07:32 -0800530
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000531#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700532 if (debug_dump_.debug_file->is_open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000533 int err = WriteInitMessage();
534 if (err != kNoError) {
535 return err;
536 }
537 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000538#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000539
niklase@google.com470e71d2011-07-07 08:21:25 +0000540 return kNoError;
541}
542
Michael Graczyk86c6d332015-07-23 11:41:39 -0700543int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
544 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700545 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
546 return kBadSampleRateError;
547 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000548 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700549
Peter Kasting69558702016-01-12 16:26:35 -0800550 const size_t num_in_channels = config.input_stream().num_channels();
551 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700552
553 // Need at least one input channel.
554 // Need either one output channel or as many outputs as there are inputs.
555 if (num_in_channels == 0 ||
556 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700557 return kBadNumberChannelsError;
558 }
559
aluebsb2328d12016-01-11 20:32:29 -0800560 if (capture_nonlocked_.beamformer_enabled &&
Peter Kasting69558702016-01-12 16:26:35 -0800561 num_in_channels != capture_.array_geometry.size()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700562 return kBadNumberChannelsError;
563 }
564
peahdf3efa82015-11-28 12:35:15 -0800565 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000566
peahde65ddc2016-09-16 15:02:15 -0700567 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700568 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700569 formats_.api_format.output_stream().sample_rate_hz()),
570 submodule_states_.CaptureMultiBandSubModulesActive() ||
571 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000572
peahde65ddc2016-09-16 15:02:15 -0700573 capture_nonlocked_.capture_processing_format =
574 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700575
peah2ce640f2017-04-07 03:57:48 -0700576 int render_processing_rate;
577 if (!config_.echo_canceller3.enabled) {
578 render_processing_rate = FindNativeProcessRateToUse(
579 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
580 formats_.api_format.reverse_output_stream().sample_rate_hz()),
581 submodule_states_.CaptureMultiBandSubModulesActive() ||
582 submodule_states_.RenderMultiBandSubModulesActive());
583 } else {
584 render_processing_rate = capture_processing_rate;
585 }
586
aluebseb3603b2016-04-20 15:27:58 -0700587 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
588 // splitting filter degrades the AEC performance.
peahcf02cf12017-04-05 14:18:07 -0700589 if (render_processing_rate > kSampleRate32kHz &&
590 !config_.echo_canceller3.enabled) {
peahde65ddc2016-09-16 15:02:15 -0700591 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
592 ? kSampleRate32kHz
593 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700594 }
peah2ce640f2017-04-07 03:57:48 -0700595
peahde65ddc2016-09-16 15:02:15 -0700596 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700597 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700598 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
599 kSampleRate8kHz) {
600 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000601 } else {
peahde65ddc2016-09-16 15:02:15 -0700602 render_processing_rate =
603 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000604 }
605
peahde65ddc2016-09-16 15:02:15 -0700606 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000607 // demonstrated to work well for AEC in most practical scenarios.
peahce4d9152017-05-19 01:28:05 -0700608 if (submodule_states_.RenderMultiBandSubModulesActive()) {
609 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
610 } else {
611 formats_.render_processing_format = StreamConfig(
612 formats_.api_format.reverse_input_stream().sample_rate_hz(),
613 formats_.api_format.reverse_input_stream().num_channels());
614 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000615
peahde65ddc2016-09-16 15:02:15 -0700616 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
617 kSampleRate32kHz ||
618 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
619 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800620 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000621 } else {
peahdf3efa82015-11-28 12:35:15 -0800622 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700623 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000624 }
625
626 return InitializeLocked();
627}
628
peah88ac8532016-09-12 16:47:25 -0700629void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peahc19f3122016-10-07 14:54:10 -0700630 config_ = config;
peah88ac8532016-09-12 16:47:25 -0700631
peahc19f3122016-10-07 14:54:10 -0700632 bool config_ok = LevelController::Validate(config_.level_controller);
peah88ac8532016-09-12 16:47:25 -0700633 if (!config_ok) {
634 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
635 << "level_controller: "
peahc19f3122016-10-07 14:54:10 -0700636 << LevelController::ToString(config_.level_controller)
peah88ac8532016-09-12 16:47:25 -0700637 << std::endl
638 << "Reverting to default parameter set";
peahc19f3122016-10-07 14:54:10 -0700639 config_.level_controller = AudioProcessing::Config::LevelController();
peah88ac8532016-09-12 16:47:25 -0700640 }
641
642 // Run in a single-threaded manner when applying the settings.
643 rtc::CritScope cs_render(&crit_render_);
644 rtc::CritScope cs_capture(&crit_capture_);
645
peahc19f3122016-10-07 14:54:10 -0700646 // TODO(peah): Replace the use of capture_nonlocked_.level_controller_enabled
647 // with the value in config_ everywhere in the code.
648 if (capture_nonlocked_.level_controller_enabled !=
649 config_.level_controller.enabled) {
peah88ac8532016-09-12 16:47:25 -0700650 capture_nonlocked_.level_controller_enabled =
peahc19f3122016-10-07 14:54:10 -0700651 config_.level_controller.enabled;
652 // TODO(peah): Remove the conditional initialization to always initialize
653 // the level controller regardless of whether it is enabled or not.
654 InitializeLevelController();
peah88ac8532016-09-12 16:47:25 -0700655 }
peahc19f3122016-10-07 14:54:10 -0700656 LOG(LS_INFO) << "Level controller activated: "
657 << capture_nonlocked_.level_controller_enabled;
658
659 private_submodules_->level_controller->ApplyConfig(config_.level_controller);
peah8271d042016-11-22 07:24:52 -0800660
661 InitializeLowCutFilter();
662
663 LOG(LS_INFO) << "Highpass filter activated: "
664 << config_.high_pass_filter.enabled;
peahe0eae3c2016-12-14 01:16:23 -0800665
666 config_ok = EchoCanceller3::Validate(config_.echo_canceller3);
667 if (!config_ok) {
668 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
669 << "echo canceller 3: "
670 << EchoCanceller3::ToString(config_.echo_canceller3)
671 << std::endl
672 << "Reverting to default parameter set";
673 config_.echo_canceller3 = AudioProcessing::Config::EchoCanceller3();
674 }
675
676 if (config.echo_canceller3.enabled !=
677 capture_nonlocked_.echo_canceller3_enabled) {
678 capture_nonlocked_.echo_canceller3_enabled =
679 config_.echo_canceller3.enabled;
680 InitializeEchoCanceller3();
681 LOG(LS_INFO) << "Echo canceller 3 activated: "
682 << capture_nonlocked_.echo_canceller3_enabled;
683 }
alessiob3ec96df2017-05-22 06:57:06 -0700684
685 config_ok = GainController2::Validate(config_.gain_controller2);
686 if (!config_ok) {
687 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
688 << "gain_controller2: "
689 << GainController2::ToString(config_.gain_controller2)
690 << std::endl
691 << "Reverting to default parameter set";
692 config_.gain_controller2 = AudioProcessing::Config::GainController2();
693 }
694
695 if (config.gain_controller2.enabled !=
696 capture_nonlocked_.gain_controller2_enabled) {
697 capture_nonlocked_.gain_controller2_enabled =
698 config_.gain_controller2.enabled;
699 InitializeGainController2();
700 LOG(LS_INFO) << "Gain controller 2 activated: "
701 << capture_nonlocked_.gain_controller2_enabled;
702 }
peah88ac8532016-09-12 16:47:25 -0700703}
704
705void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800706 // Run in a single-threaded manner when setting the extra options.
707 rtc::CritScope cs_render(&crit_render_);
708 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000709
peahb624d8c2016-03-05 03:01:14 -0800710 public_submodules_->echo_cancellation->SetExtraOptions(config);
711
peahdf3efa82015-11-28 12:35:15 -0800712 if (capture_.transient_suppressor_enabled !=
713 config.Get<ExperimentalNs>().enabled) {
714 capture_.transient_suppressor_enabled =
715 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000716 InitializeTransient();
717 }
aluebs2a346882016-01-11 18:04:30 -0800718
peah1bcfce52016-08-26 07:16:04 -0700719#if WEBRTC_INTELLIGIBILITY_ENHANCER
alessiob3ec96df2017-05-22 06:57:06 -0700720 if (capture_nonlocked_.intelligibility_enabled !=
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700721 config.Get<Intelligibility>().enabled) {
722 capture_nonlocked_.intelligibility_enabled =
723 config.Get<Intelligibility>().enabled;
724 InitializeIntelligibility();
725 }
peah1bcfce52016-08-26 07:16:04 -0700726#endif
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700727
aluebs2a346882016-01-11 18:04:30 -0800728#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
aluebsb2328d12016-01-11 20:32:29 -0800729 if (capture_nonlocked_.beamformer_enabled !=
730 config.Get<Beamforming>().enabled) {
731 capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled;
aluebs2a346882016-01-11 18:04:30 -0800732 if (config.Get<Beamforming>().array_geometry.size() > 1) {
733 capture_.array_geometry = config.Get<Beamforming>().array_geometry;
734 }
735 capture_.target_direction = config.Get<Beamforming>().target_direction;
736 InitializeBeamformer();
737 }
738#endif // WEBRTC_ANDROID_PLATFORM_BUILD
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000739}
740
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000741int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800742 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700743 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000744}
745
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000746int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800747 // Used as callback from submodules, hence locking is not allowed.
748 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000749}
750
Peter Kasting69558702016-01-12 16:26:35 -0800751size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800752 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700753 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000754}
755
Peter Kasting69558702016-01-12 16:26:35 -0800756size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800757 // Used as callback from submodules, hence locking is not allowed.
758 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000759}
760
Peter Kasting69558702016-01-12 16:26:35 -0800761size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800762 // Used as callback from submodules, hence locking is not allowed.
peahedddac52017-05-16 01:08:58 -0700763 return (capture_nonlocked_.beamformer_enabled ||
764 capture_nonlocked_.echo_canceller3_enabled)
765 ? 1
766 : num_output_channels();
aluebsb2328d12016-01-11 20:32:29 -0800767}
768
Peter Kasting69558702016-01-12 16:26:35 -0800769size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800770 // Used as callback from submodules, hence locking is not allowed.
771 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000772}
773
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000774void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800775 rtc::CritScope cs(&crit_capture_);
776 capture_.output_will_be_muted = muted;
777 if (private_submodules_->agc_manager.get()) {
778 private_submodules_->agc_manager->SetCaptureMuted(
779 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000780 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000781}
782
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000783
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000784int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700785 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000786 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000787 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000788 int output_sample_rate_hz,
789 ChannelLayout output_layout,
790 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800791 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800792 StreamConfig input_stream;
793 StreamConfig output_stream;
794 {
795 // Access the formats_.api_format.input_stream beneath the capture lock.
796 // The lock must be released as it is later required in the call
797 // to ProcessStream(,,,);
798 rtc::CritScope cs(&crit_capture_);
799 input_stream = formats_.api_format.input_stream();
800 output_stream = formats_.api_format.output_stream();
801 }
802
Michael Graczyk86c6d332015-07-23 11:41:39 -0700803 input_stream.set_sample_rate_hz(input_sample_rate_hz);
804 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
805 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700806 output_stream.set_sample_rate_hz(output_sample_rate_hz);
807 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
808 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
809
810 if (samples_per_channel != input_stream.num_frames()) {
811 return kBadDataLengthError;
812 }
813 return ProcessStream(src, input_stream, output_stream, dest);
814}
815
816int AudioProcessingImpl::ProcessStream(const float* const* src,
817 const StreamConfig& input_config,
818 const StreamConfig& output_config,
819 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800820 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800821 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700822 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800823 {
824 // Acquire the capture lock in order to safely call the function
825 // that retrieves the render side data. This function accesses apm
826 // getters that need the capture lock held when being called.
827 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700828 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800829
830 if (!src || !dest) {
831 return kNullPointerError;
832 }
833
834 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700835 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000836 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000837
Michael Graczyk86c6d332015-07-23 11:41:39 -0700838 processing_config.input_stream() = input_config;
839 processing_config.output_stream() = output_config;
840
peahdf3efa82015-11-28 12:35:15 -0800841 {
842 // Do conditional reinitialization.
843 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -0700844 RETURN_ON_ERR(
845 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -0800846 }
847 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700848 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
849 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000850
851#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700852 if (debug_dump_.debug_file->is_open()) {
Minyue13b96ba2015-10-03 00:39:14 +0200853 RETURN_ON_ERR(WriteConfigMessage(false));
854
peahdf3efa82015-11-28 12:35:15 -0800855 debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM);
856 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000857 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -0800858 sizeof(float) * formats_.api_format.input_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -0800859 for (size_t i = 0; i < formats_.api_format.input_stream().num_channels();
860 ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000861 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000862 }
863#endif
864
peahdf3efa82015-11-28 12:35:15 -0800865 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700866 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800867 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000868
869#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700870 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -0800871 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000872 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -0800873 sizeof(float) * formats_.api_format.output_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -0800874 for (size_t i = 0; i < formats_.api_format.output_stream().num_channels();
875 ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000876 msg->add_output_channel(dest[i], channel_size);
peahdf3efa82015-11-28 12:35:15 -0800877 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -0800878 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -0800879 &crit_debug_, &debug_dump_.capture));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000880 }
881#endif
882
883 return kNoError;
884}
885
peah9e6a2902017-05-15 07:19:21 -0700886void AudioProcessingImpl::QueueBandedRenderAudio(AudioBuffer* audio) {
peah764e3642016-10-22 05:04:30 -0700887 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
888 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700889 &aec_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700890
kwibergaf476c72016-11-28 15:21:39 -0800891 RTC_DCHECK_GE(160, audio->num_frames_per_band());
peah764e3642016-10-22 05:04:30 -0700892
893 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700894 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700895 // The data queue is full and needs to be emptied.
896 EmptyQueuedRenderAudio();
897
898 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700899 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700900 RTC_DCHECK(result);
901 }
902
903 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
904 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700905 &aecm_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700906
907 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700908 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700909 // The data queue is full and needs to be emptied.
910 EmptyQueuedRenderAudio();
911
912 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700913 bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700914 RTC_DCHECK(result);
915 }
peah701d6282016-10-25 05:42:20 -0700916
917 if (!constants_.use_experimental_agc) {
918 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
919 // Insert the samples into the queue.
920 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
921 // The data queue is full and needs to be emptied.
922 EmptyQueuedRenderAudio();
923
924 // Retry the insert (should always work).
925 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
926 RTC_DCHECK(result);
927 }
928 }
peah9e6a2902017-05-15 07:19:21 -0700929}
ivoc9f4a4a02016-10-28 05:39:16 -0700930
peah9e6a2902017-05-15 07:19:21 -0700931void AudioProcessingImpl::QueueNonbandedRenderAudio(AudioBuffer* audio) {
ivoc9f4a4a02016-10-28 05:39:16 -0700932 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
933
934 // Insert the samples into the queue.
935 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
936 // The data queue is full and needs to be emptied.
937 EmptyQueuedRenderAudio();
938
939 // Retry the insert (should always work).
940 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
941 RTC_DCHECK(result);
942 }
peah764e3642016-10-22 05:04:30 -0700943}
944
945void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -0700946 const size_t new_aec_render_queue_element_max_size =
peah764e3642016-10-22 05:04:30 -0700947 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -0700948 kMaxAllowedValuesOfSamplesPerBand *
peah764e3642016-10-22 05:04:30 -0700949 EchoCancellationImpl::NumCancellersRequired(
950 num_output_channels(), num_reverse_channels()));
951
peah701d6282016-10-25 05:42:20 -0700952 const size_t new_aecm_render_queue_element_max_size =
peaha0624602016-10-25 04:45:24 -0700953 std::max(static_cast<size_t>(1),
peah9e6a2902017-05-15 07:19:21 -0700954 kMaxAllowedValuesOfSamplesPerBand *
peaha0624602016-10-25 04:45:24 -0700955 EchoControlMobileImpl::NumCancellersRequired(
956 num_output_channels(), num_reverse_channels()));
peah764e3642016-10-22 05:04:30 -0700957
peah701d6282016-10-25 05:42:20 -0700958 const size_t new_agc_render_queue_element_max_size =
peah9e6a2902017-05-15 07:19:21 -0700959 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerBand);
peah701d6282016-10-25 05:42:20 -0700960
ivoc9f4a4a02016-10-28 05:39:16 -0700961 const size_t new_red_render_queue_element_max_size =
962 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
963
peaha0624602016-10-25 04:45:24 -0700964 // Reallocate the queues if the queue item sizes are too small to fit the
965 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -0700966 if (aec_render_queue_element_max_size_ <
967 new_aec_render_queue_element_max_size) {
968 aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
peah764e3642016-10-22 05:04:30 -0700969
peaha0624602016-10-25 04:45:24 -0700970 std::vector<float> template_queue_element(
peah701d6282016-10-25 05:42:20 -0700971 aec_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700972
peah701d6282016-10-25 05:42:20 -0700973 aec_render_signal_queue_.reset(
peah764e3642016-10-22 05:04:30 -0700974 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
975 kMaxNumFramesToBuffer, template_queue_element,
peaha0624602016-10-25 04:45:24 -0700976 RenderQueueItemVerifier<float>(
peah701d6282016-10-25 05:42:20 -0700977 aec_render_queue_element_max_size_)));
peah764e3642016-10-22 05:04:30 -0700978
peah701d6282016-10-25 05:42:20 -0700979 aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
980 aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
peah764e3642016-10-22 05:04:30 -0700981 } else {
peah701d6282016-10-25 05:42:20 -0700982 aec_render_signal_queue_->Clear();
peaha0624602016-10-25 04:45:24 -0700983 }
984
peah701d6282016-10-25 05:42:20 -0700985 if (aecm_render_queue_element_max_size_ <
986 new_aecm_render_queue_element_max_size) {
987 aecm_render_queue_element_max_size_ =
988 new_aecm_render_queue_element_max_size;
peaha0624602016-10-25 04:45:24 -0700989
990 std::vector<int16_t> template_queue_element(
peah701d6282016-10-25 05:42:20 -0700991 aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700992
peah701d6282016-10-25 05:42:20 -0700993 aecm_render_signal_queue_.reset(
peaha0624602016-10-25 04:45:24 -0700994 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
995 kMaxNumFramesToBuffer, template_queue_element,
996 RenderQueueItemVerifier<int16_t>(
peah701d6282016-10-25 05:42:20 -0700997 aecm_render_queue_element_max_size_)));
peaha0624602016-10-25 04:45:24 -0700998
peah701d6282016-10-25 05:42:20 -0700999 aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
1000 aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -07001001 } else {
peah701d6282016-10-25 05:42:20 -07001002 aecm_render_signal_queue_->Clear();
1003 }
1004
1005 if (agc_render_queue_element_max_size_ <
1006 new_agc_render_queue_element_max_size) {
1007 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
1008
1009 std::vector<int16_t> template_queue_element(
1010 agc_render_queue_element_max_size_);
1011
1012 agc_render_signal_queue_.reset(
1013 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
1014 kMaxNumFramesToBuffer, template_queue_element,
1015 RenderQueueItemVerifier<int16_t>(
1016 agc_render_queue_element_max_size_)));
1017
1018 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
1019 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
1020 } else {
1021 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -07001022 }
ivoc9f4a4a02016-10-28 05:39:16 -07001023
1024 if (red_render_queue_element_max_size_ <
1025 new_red_render_queue_element_max_size) {
1026 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
1027
1028 std::vector<float> template_queue_element(
1029 red_render_queue_element_max_size_);
1030
1031 red_render_signal_queue_.reset(
1032 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
1033 kMaxNumFramesToBuffer, template_queue_element,
1034 RenderQueueItemVerifier<float>(
1035 red_render_queue_element_max_size_)));
1036
1037 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
1038 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
1039 } else {
1040 red_render_signal_queue_->Clear();
1041 }
peah764e3642016-10-22 05:04:30 -07001042}
1043
1044void AudioProcessingImpl::EmptyQueuedRenderAudio() {
1045 rtc::CritScope cs_capture(&crit_capture_);
peah701d6282016-10-25 05:42:20 -07001046 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -07001047 public_submodules_->echo_cancellation->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001048 aec_capture_queue_buffer_);
peaha0624602016-10-25 04:45:24 -07001049 }
1050
peah701d6282016-10-25 05:42:20 -07001051 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -07001052 public_submodules_->echo_control_mobile->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -07001053 aecm_capture_queue_buffer_);
1054 }
1055
1056 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
1057 public_submodules_->gain_control->ProcessRenderAudio(
1058 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -07001059 }
ivoc9f4a4a02016-10-28 05:39:16 -07001060
1061 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
1062 private_submodules_->residual_echo_detector->AnalyzeRenderAudio(
1063 red_capture_queue_buffer_);
1064 }
peah764e3642016-10-22 05:04:30 -07001065}
1066
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001067int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001068 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001069 {
1070 // Acquire the capture lock in order to safely call the function
1071 // that retrieves the render side data. This function accesses apm
1072 // getters that need the capture lock held when being called.
1073 // The lock needs to be released as
1074 // public_submodules_->echo_control_mobile->is_enabled() aquires this lock
1075 // as well.
1076 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -07001077 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -08001078 }
peahfa6228e2015-11-16 16:27:42 -08001079
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001080 if (!frame) {
1081 return kNullPointerError;
1082 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001083 // Must be a native rate.
1084 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1085 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001086 frame->sample_rate_hz_ != kSampleRate32kHz &&
1087 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001088 return kBadSampleRateError;
1089 }
peah192164e2015-11-17 02:16:45 -08001090
peahdf3efa82015-11-28 12:35:15 -08001091 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -07001092 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -08001093 {
1094 // Aquire lock for the access of api_format.
1095 // The lock is released immediately due to the conditional
1096 // reinitialization.
1097 rtc::CritScope cs_capture(&crit_capture_);
1098 // TODO(ajm): The input and output rates and channels are currently
1099 // constrained to be identical in the int16 interface.
1100 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -07001101
1102 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -08001103 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001104 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1105 processing_config.input_stream().set_num_channels(frame->num_channels_);
1106 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1107 processing_config.output_stream().set_num_channels(frame->num_channels_);
1108
peahdf3efa82015-11-28 12:35:15 -08001109 {
1110 // Do conditional reinitialization.
1111 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -07001112 RETURN_ON_ERR(
1113 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -08001114 }
1115 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001116 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001117 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001118 return kBadDataLengthError;
1119 }
1120
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001121#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001122 if (debug_dump_.debug_file->is_open()) {
peah644fa962016-08-18 06:48:33 -07001123 RETURN_ON_ERR(WriteConfigMessage(false));
1124
peahdf3efa82015-11-28 12:35:15 -08001125 debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM);
1126 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001127 const size_t data_size =
1128 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001129 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001130 }
1131#endif
1132
peahdf3efa82015-11-28 12:35:15 -08001133 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001134 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001135 capture_.capture_audio->InterleaveTo(
1136 frame, submodule_states_.CaptureMultiBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001137
1138#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001139 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001140 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001141 const size_t data_size =
1142 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001143 msg->set_output_data(frame->data_, data_size);
peahdf3efa82015-11-28 12:35:15 -08001144 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001145 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001146 &crit_debug_, &debug_dump_.capture));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001147 }
1148#endif
1149
1150 return kNoError;
1151}
1152
peahde65ddc2016-09-16 15:02:15 -07001153int AudioProcessingImpl::ProcessCaptureStreamLocked() {
peahb58a1582016-03-15 09:34:24 -07001154 // Ensure that not both the AEC and AECM are active at the same time.
1155 // TODO(peah): Simplify once the public API Enable functions for these
1156 // are moved to APM.
1157 RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&
1158 public_submodules_->echo_control_mobile->is_enabled()));
1159
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001160#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001161 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001162 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
1163 msg->set_delay(capture_nonlocked_.stream_delay_ms);
1164 msg->set_drift(
1165 public_submodules_->echo_cancellation->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +00001166 msg->set_level(gain_control()->stream_analog_level());
peahdf3efa82015-11-28 12:35:15 -08001167 msg->set_keypress(capture_.key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +00001168 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001169#endif
niklase@google.com470e71d2011-07-07 08:21:25 +00001170
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001171 MaybeUpdateHistograms();
1172
peahde65ddc2016-09-16 15:02:15 -07001173 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001174
peah1b08dc32016-12-20 13:45:58 -08001175 capture_input_rms_.Analyze(rtc::ArrayView<const int16_t>(
henrik.lundin290d43a2016-11-29 08:09:09 -08001176 capture_buffer->channels_const()[0],
1177 capture_nonlocked_.capture_processing_format.num_frames()));
peah1b08dc32016-12-20 13:45:58 -08001178 const bool log_rms = ++capture_rms_interval_counter_ >= 1000;
1179 if (log_rms) {
1180 capture_rms_interval_counter_ = 0;
1181 RmsLevel::Levels levels = capture_input_rms_.AverageAndPeak();
henrik.lundin45bb5132016-12-06 04:28:04 -08001182 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelAverageRms",
1183 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1184 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureInputLevelPeakRms",
1185 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
henrik.lundin290d43a2016-11-29 08:09:09 -08001186 }
1187
peahe0eae3c2016-12-14 01:16:23 -08001188 if (private_submodules_->echo_canceller3) {
peah67995532017-04-10 14:12:41 -07001189 const int new_agc_level = gain_control()->stream_analog_level();
1190 capture_.echo_path_gain_change =
peah103ac7e2017-04-12 05:40:55 -07001191 abs(capture_.previous_agc_level - new_agc_level) > 5;
peah67995532017-04-10 14:12:41 -07001192 capture_.previous_agc_level = new_agc_level;
peahe0eae3c2016-12-14 01:16:23 -08001193 private_submodules_->echo_canceller3->AnalyzeCapture(capture_buffer);
1194 }
1195
peahbe615622016-02-13 16:40:47 -08001196 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001197 public_submodules_->gain_control->is_enabled()) {
1198 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001199 capture_buffer->channels()[0], capture_buffer->num_channels(),
1200 capture_nonlocked_.capture_processing_format.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001201 }
1202
peah2ace3f92016-09-10 04:42:27 -07001203 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1204 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001205 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1206 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001207 }
1208
peah522d71b2017-02-23 05:16:26 -08001209 if (private_submodules_->echo_canceller3) {
1210 // Force down-mixing of the number of channels after the detection of
1211 // capture signal saturation.
1212 // TODO(peah): Look into ensuring that this kind of tampering with the
1213 // AudioBuffer functionality should not be needed.
1214 capture_buffer->set_num_channels(1);
1215 }
1216
aluebsb2328d12016-01-11 20:32:29 -08001217 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001218 private_submodules_->beamformer->AnalyzeChunk(
1219 *capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001220 // Discards all channels by the leftmost one.
peahde65ddc2016-09-16 15:02:15 -07001221 capture_buffer->set_num_channels(1);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001222 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001223
peahe0eae3c2016-12-14 01:16:23 -08001224 // TODO(peah): Move the AEC3 low-cut filter to this place.
1225 if (private_submodules_->low_cut_filter &&
1226 !private_submodules_->echo_canceller3) {
peah8271d042016-11-22 07:24:52 -08001227 private_submodules_->low_cut_filter->Process(capture_buffer);
1228 }
peahde65ddc2016-09-16 15:02:15 -07001229 RETURN_ON_ERR(
1230 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1231 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001232
1233 // Ensure that the stream delay was set before the call to the
1234 // AEC ProcessCaptureAudio function.
1235 if (public_submodules_->echo_cancellation->is_enabled() &&
1236 !was_stream_delay_set()) {
1237 return AudioProcessing::kStreamParameterNotSetError;
1238 }
1239
peahe0eae3c2016-12-14 01:16:23 -08001240 if (private_submodules_->echo_canceller3) {
peah67995532017-04-10 14:12:41 -07001241 private_submodules_->echo_canceller3->ProcessCapture(
1242 capture_buffer, capture_.echo_path_gain_change);
peah61202ac2017-02-06 03:39:42 -08001243 } else {
1244 RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(
1245 capture_buffer, stream_delay_ms()));
peahe0eae3c2016-12-14 01:16:23 -08001246 }
1247
peahdf3efa82015-11-28 12:35:15 -08001248 if (public_submodules_->echo_control_mobile->is_enabled() &&
1249 public_submodules_->noise_suppression->is_enabled()) {
peahde65ddc2016-09-16 15:02:15 -07001250 capture_buffer->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +00001251 }
peahde65ddc2016-09-16 15:02:15 -07001252 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah1bcfce52016-08-26 07:16:04 -07001253#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001254 if (capture_nonlocked_.intelligibility_enabled) {
aluebsc466bad2016-02-10 12:03:00 -08001255 RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001256 int gain_db = public_submodules_->gain_control->is_enabled() ?
1257 public_submodules_->gain_control->compression_gain_db() :
1258 0;
Alejandro Luebs50411102016-06-30 15:35:41 -07001259 float gain = std::pow(10.f, gain_db / 20.f);
1260 gain *= capture_nonlocked_.level_controller_enabled ?
1261 private_submodules_->level_controller->GetLastGain() :
1262 1.f;
aluebsc466bad2016-02-10 12:03:00 -08001263 public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -07001264 public_submodules_->noise_suppression->NoiseEstimate(), gain);
aluebsc466bad2016-02-10 12:03:00 -08001265 }
peah1bcfce52016-08-26 07:16:04 -07001266#endif
peah253534d2016-03-15 04:32:28 -07001267
1268 // Ensure that the stream delay was set before the call to the
1269 // AECM ProcessCaptureAudio function.
1270 if (public_submodules_->echo_control_mobile->is_enabled() &&
1271 !was_stream_delay_set()) {
1272 return AudioProcessing::kStreamParameterNotSetError;
1273 }
1274
1275 RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001276 capture_buffer, stream_delay_ms()));
peah253534d2016-03-15 04:32:28 -07001277
ivoc9f4a4a02016-10-28 05:39:16 -07001278
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001279 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001280 private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001281 }
1282
peahde65ddc2016-09-16 15:02:15 -07001283 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001284
peahbe615622016-02-13 16:40:47 -08001285 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001286 public_submodules_->gain_control->is_enabled() &&
aluebsb2328d12016-01-11 20:32:29 -08001287 (!capture_nonlocked_.beamformer_enabled ||
peahdf3efa82015-11-28 12:35:15 -08001288 private_submodules_->beamformer->is_target_present())) {
1289 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001290 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1291 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001292 }
peahb8fbb542016-03-15 02:28:08 -07001293 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001294 capture_buffer, echo_cancellation()->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001295
peah2ace3f92016-09-10 04:42:27 -07001296 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1297 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001298 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1299 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001300 }
1301
peah9e6a2902017-05-15 07:19:21 -07001302 if (config_.residual_echo_detector.enabled) {
1303 private_submodules_->residual_echo_detector->AnalyzeCaptureAudio(
1304 rtc::ArrayView<const float>(capture_buffer->channels_f()[0],
1305 capture_buffer->num_frames()));
1306 }
1307
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001308 // TODO(aluebs): Investigate if the transient suppression placement should be
1309 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001310 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001311 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001312 private_submodules_->agc_manager.get()
1313 ? private_submodules_->agc_manager->voice_probability()
1314 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001315
peahdf3efa82015-11-28 12:35:15 -08001316 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001317 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1318 capture_buffer->num_channels(),
1319 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1320 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1321 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001322 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001323 }
1324
alessiob3ec96df2017-05-22 06:57:06 -07001325 if (capture_nonlocked_.gain_controller2_enabled) {
1326 private_submodules_->gain_controller2->Process(capture_buffer);
1327 }
1328
peahca4cac72016-06-29 15:26:12 -07001329 if (capture_nonlocked_.level_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001330 private_submodules_->level_controller->Process(capture_buffer);
peahca4cac72016-06-29 15:26:12 -07001331 }
1332
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001333 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001334 public_submodules_->level_estimator->ProcessStream(capture_buffer);
ajm@google.com808e0e02011-08-03 21:08:51 +00001335
peah1b08dc32016-12-20 13:45:58 -08001336 capture_output_rms_.Analyze(rtc::ArrayView<const int16_t>(
1337 capture_buffer->channels_const()[0],
1338 capture_nonlocked_.capture_processing_format.num_frames()));
1339 if (log_rms) {
1340 RmsLevel::Levels levels = capture_output_rms_.AverageAndPeak();
1341 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelAverageRms",
1342 levels.average, 1, RmsLevel::kMinLevelDb, 64);
1343 RTC_HISTOGRAM_COUNTS_LINEAR("WebRTC.Audio.ApmCaptureOutputLevelPeakRms",
1344 levels.peak, 1, RmsLevel::kMinLevelDb, 64);
1345 }
1346
peahdf3efa82015-11-28 12:35:15 -08001347 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001348 return kNoError;
1349}
1350
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001351int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001352 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001353 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001354 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001355 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001356 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001357 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001358 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001359 };
1360 if (samples_per_channel != reverse_config.num_frames()) {
1361 return kBadDataLengthError;
1362 }
peahdf3efa82015-11-28 12:35:15 -08001363 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001364}
1365
peahde65ddc2016-09-16 15:02:15 -07001366int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1367 const StreamConfig& input_config,
1368 const StreamConfig& output_config,
1369 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001370 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001371 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001372 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
peah2ace3f92016-09-10 04:42:27 -07001373 if (submodule_states_.RenderMultiBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001374 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1375 dest);
peah2ace3f92016-09-10 04:42:27 -07001376 } else if (formats_.api_format.reverse_input_stream() !=
1377 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001378 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1379 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001380 } else {
peahde65ddc2016-09-16 15:02:15 -07001381 CopyAudioIfNeeded(src, input_config.num_frames(),
1382 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001383 }
1384
1385 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001386}
1387
peahdf3efa82015-11-28 12:35:15 -08001388int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001389 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001390 const StreamConfig& input_config,
1391 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001392 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001393 return kNullPointerError;
1394 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001395
peahde65ddc2016-09-16 15:02:15 -07001396 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001397 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001398 }
1399
peahdf3efa82015-11-28 12:35:15 -08001400 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001401 processing_config.reverse_input_stream() = input_config;
1402 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001403
peahdf3efa82015-11-28 12:35:15 -08001404 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
peahde65ddc2016-09-16 15:02:15 -07001405 assert(input_config.num_frames() ==
1406 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001407
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001408#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001409 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001410 debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM);
1411 audioproc::ReverseStream* msg =
1412 debug_dump_.render.event_msg->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +00001413 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -08001414 sizeof(float) * formats_.api_format.reverse_input_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -08001415 for (size_t i = 0;
peahdf3efa82015-11-28 12:35:15 -08001416 i < formats_.api_format.reverse_input_stream().num_channels(); ++i)
ekmeyerson60d9b332015-08-14 10:35:55 -07001417 msg->add_channel(src[i], channel_size);
peahdf3efa82015-11-28 12:35:15 -08001418 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001419 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001420 &crit_debug_, &debug_dump_.render));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001421 }
1422#endif
1423
peahdf3efa82015-11-28 12:35:15 -08001424 render_.render_audio->CopyFrom(src,
1425 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001426 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001427}
1428
1429int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001430 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001431 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001432 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001433 return kNullPointerError;
1434 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001435 // Must be a native rate.
1436 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1437 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001438 frame->sample_rate_hz_ != kSampleRate32kHz &&
1439 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001440 return kBadSampleRateError;
1441 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001442
Michael Graczyk86c6d332015-07-23 11:41:39 -07001443 if (frame->num_channels_ <= 0) {
1444 return kBadNumberChannelsError;
1445 }
1446
peahdf3efa82015-11-28 12:35:15 -08001447 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001448 processing_config.reverse_input_stream().set_sample_rate_hz(
1449 frame->sample_rate_hz_);
1450 processing_config.reverse_input_stream().set_num_channels(
1451 frame->num_channels_);
1452 processing_config.reverse_output_stream().set_sample_rate_hz(
1453 frame->sample_rate_hz_);
1454 processing_config.reverse_output_stream().set_num_channels(
1455 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001456
peahdf3efa82015-11-28 12:35:15 -08001457 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001458 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001459 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001460 return kBadDataLengthError;
1461 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001462
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001463#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001464 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001465 debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM);
1466 audioproc::ReverseStream* msg =
1467 debug_dump_.render.event_msg->mutable_reverse_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001468 const size_t data_size =
1469 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001470 msg->set_data(frame->data_, data_size);
peahdf3efa82015-11-28 12:35:15 -08001471 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001472 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001473 &crit_debug_, &debug_dump_.render));
niklase@google.com470e71d2011-07-07 08:21:25 +00001474 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001475#endif
peahdf3efa82015-11-28 12:35:15 -08001476 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001477 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001478 render_.render_audio->InterleaveTo(
1479 frame, submodule_states_.RenderMultiBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001480 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001481}
niklase@google.com470e71d2011-07-07 08:21:25 +00001482
peahde65ddc2016-09-16 15:02:15 -07001483int AudioProcessingImpl::ProcessRenderStreamLocked() {
1484 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah9e6a2902017-05-15 07:19:21 -07001485
1486 QueueNonbandedRenderAudio(render_buffer);
1487
peah2ace3f92016-09-10 04:42:27 -07001488 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001489 SampleRateSupportsMultiBand(
1490 formats_.render_processing_format.sample_rate_hz())) {
1491 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001492 }
1493
peah1bcfce52016-08-26 07:16:04 -07001494#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001495 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001496 public_submodules_->intelligibility_enhancer->ProcessRenderAudio(
Alejandro Luebsef009252016-09-20 14:51:56 -07001497 render_buffer);
ekmeyerson60d9b332015-08-14 10:35:55 -07001498 }
peah1bcfce52016-08-26 07:16:04 -07001499#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001500
peahce4d9152017-05-19 01:28:05 -07001501 if (submodule_states_.RenderMultiBandSubModulesActive()) {
1502 QueueBandedRenderAudio(render_buffer);
1503 }
1504
peahe0eae3c2016-12-14 01:16:23 -08001505 // TODO(peah): Perform the queueing ínside QueueRenderAudiuo().
1506 if (private_submodules_->echo_canceller3) {
peahcf02cf12017-04-05 14:18:07 -07001507 private_submodules_->echo_canceller3->AnalyzeRender(render_buffer);
peahe0eae3c2016-12-14 01:16:23 -08001508 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001509
peah2ace3f92016-09-10 04:42:27 -07001510 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001511 SampleRateSupportsMultiBand(
1512 formats_.render_processing_format.sample_rate_hz())) {
1513 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001514 }
1515
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001516 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001517}
1518
1519int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001520 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001521 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001522 capture_.was_stream_delay_set = true;
1523 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001524
niklase@google.com470e71d2011-07-07 08:21:25 +00001525 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001526 delay = 0;
1527 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001528 }
1529
1530 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1531 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001532 delay = 500;
1533 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001534 }
1535
peahdf3efa82015-11-28 12:35:15 -08001536 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001537 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001538}
1539
1540int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001541 // Used as callback from submodules, hence locking is not allowed.
1542 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001543}
1544
1545bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001546 // Used as callback from submodules, hence locking is not allowed.
1547 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001548}
1549
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001550void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001551 rtc::CritScope cs(&crit_capture_);
1552 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001553}
1554
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001555void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001556 rtc::CritScope cs(&crit_capture_);
1557 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001558}
1559
1560int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001561 rtc::CritScope cs(&crit_capture_);
1562 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001563}
1564
niklase@google.com470e71d2011-07-07 08:21:25 +00001565int AudioProcessingImpl::StartDebugRecording(
ivocd66b44d2016-01-15 03:06:36 -08001566 const char filename[AudioProcessing::kMaxFilenameSize],
1567 int64_t max_log_size_bytes) {
peahdf3efa82015-11-28 12:35:15 -08001568 // Run in a single-threaded manner.
1569 rtc::CritScope cs_render(&crit_render_);
1570 rtc::CritScope cs_capture(&crit_capture_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +02001571 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +00001572
peahdf3efa82015-11-28 12:35:15 -08001573 if (filename == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001574 return kNullPointerError;
1575 }
1576
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001577#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ivocd66b44d2016-01-15 03:06:36 -08001578 debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001579 // Stop any ongoing recording.
tommia6219cc2016-06-15 10:30:14 -07001580 debug_dump_.debug_file->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +00001581
tommia6219cc2016-06-15 10:30:14 -07001582 if (!debug_dump_.debug_file->OpenFile(filename, false)) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001583 return kFileError;
1584 }
1585
Minyue13b96ba2015-10-03 00:39:14 +02001586 RETURN_ON_ERR(WriteConfigMessage(true));
1587 RETURN_ON_ERR(WriteInitMessage());
niklase@google.com470e71d2011-07-07 08:21:25 +00001588 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001589#else
1590 return kUnsupportedFunctionError;
1591#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001592}
1593
ivocd66b44d2016-01-15 03:06:36 -08001594int AudioProcessingImpl::StartDebugRecording(FILE* handle,
1595 int64_t max_log_size_bytes) {
peahdf3efa82015-11-28 12:35:15 -08001596 // Run in a single-threaded manner.
1597 rtc::CritScope cs_render(&crit_render_);
1598 rtc::CritScope cs_capture(&crit_capture_);
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001599
peahdf3efa82015-11-28 12:35:15 -08001600 if (handle == nullptr) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001601 return kNullPointerError;
1602 }
1603
1604#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ivocd66b44d2016-01-15 03:06:36 -08001605 debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes;
1606
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001607 // Stop any ongoing recording.
tommia6219cc2016-06-15 10:30:14 -07001608 debug_dump_.debug_file->CloseFile();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001609
tommia6219cc2016-06-15 10:30:14 -07001610 if (!debug_dump_.debug_file->OpenFromFileHandle(handle)) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001611 return kFileError;
1612 }
1613
Minyue13b96ba2015-10-03 00:39:14 +02001614 RETURN_ON_ERR(WriteConfigMessage(true));
1615 RETURN_ON_ERR(WriteInitMessage());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001616 return kNoError;
1617#else
1618 return kUnsupportedFunctionError;
1619#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1620}
1621
peah73a28ee2016-10-12 03:01:49 -07001622int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
1623 return StartDebugRecording(handle, -1);
1624}
1625
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001626int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
1627 rtc::PlatformFile handle) {
peahdf3efa82015-11-28 12:35:15 -08001628 // Run in a single-threaded manner.
1629 rtc::CritScope cs_render(&crit_render_);
1630 rtc::CritScope cs_capture(&crit_capture_);
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001631 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
ivocd66b44d2016-01-15 03:06:36 -08001632 return StartDebugRecording(stream, -1);
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001633}
1634
niklase@google.com470e71d2011-07-07 08:21:25 +00001635int AudioProcessingImpl::StopDebugRecording() {
peahdf3efa82015-11-28 12:35:15 -08001636 // Run in a single-threaded manner.
1637 rtc::CritScope cs_render(&crit_render_);
1638 rtc::CritScope cs_capture(&crit_capture_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001639
1640#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001641 // We just return if recording hasn't started.
tommia6219cc2016-06-15 10:30:14 -07001642 debug_dump_.debug_file->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +00001643 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001644#else
1645 return kUnsupportedFunctionError;
1646#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001647}
1648
ivoc4e477a12017-01-15 08:29:46 -08001649AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics() {
1650 residual_echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1651 echo_return_loss.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1652 echo_return_loss_enhancement.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1653 a_nlp.Set(-100.0f, -100.0f, -100.0f, -100.0f);
1654}
1655
1656AudioProcessing::AudioProcessingStatistics::AudioProcessingStatistics(
1657 const AudioProcessingStatistics& other) = default;
1658
1659AudioProcessing::AudioProcessingStatistics::~AudioProcessingStatistics() =
1660 default;
1661
ivoc3e9a5372016-10-28 07:55:33 -07001662// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
1663AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics()
1664 const {
1665 return AudioProcessingStatistics();
1666}
1667
1668AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics()
1669 const {
1670 AudioProcessingStatistics stats;
1671 EchoCancellation::Metrics metrics;
ivocd0a151c2016-11-02 09:14:37 -07001672 int success = public_submodules_->echo_cancellation->GetMetrics(&metrics);
1673 if (success == Error::kNoError) {
1674 stats.a_nlp.Set(metrics.a_nlp);
1675 stats.divergent_filter_fraction = metrics.divergent_filter_fraction;
1676 stats.echo_return_loss.Set(metrics.echo_return_loss);
1677 stats.echo_return_loss_enhancement.Set(
1678 metrics.echo_return_loss_enhancement);
1679 stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss);
1680 }
ivoc9c192b22017-03-16 04:22:14 -07001681 {
1682 rtc::CritScope cs_capture(&crit_capture_);
1683 stats.residual_echo_likelihood =
1684 private_submodules_->residual_echo_detector->echo_likelihood();
1685 stats.residual_echo_likelihood_recent_max =
1686 private_submodules_->residual_echo_detector
1687 ->echo_likelihood_recent_max();
1688 }
ivoc3e9a5372016-10-28 07:55:33 -07001689 public_submodules_->echo_cancellation->GetDelayMetrics(
1690 &stats.delay_median, &stats.delay_standard_deviation,
1691 &stats.fraction_poor_delays);
1692 return stats;
1693}
1694
niklase@google.com470e71d2011-07-07 08:21:25 +00001695EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
peahb624d8c2016-03-05 03:01:14 -08001696 return public_submodules_->echo_cancellation.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001697}
1698
1699EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
peahbb9edbd2016-03-10 12:54:25 -08001700 return public_submodules_->echo_control_mobile.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001701}
1702
1703GainControl* AudioProcessingImpl::gain_control() const {
peahbe615622016-02-13 16:40:47 -08001704 if (constants_.use_experimental_agc) {
1705 return public_submodules_->gain_control_for_experimental_agc.get();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001706 }
peahbfa97112016-03-10 21:09:04 -08001707 return public_submodules_->gain_control.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001708}
1709
1710HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
peah8271d042016-11-22 07:24:52 -08001711 return high_pass_filter_impl_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001712}
1713
1714LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001715 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001716}
1717
1718NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
solenberg5e465c32015-12-08 13:22:33 -08001719 return public_submodules_->noise_suppression.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001720}
1721
1722VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001723 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001724}
1725
peah8271d042016-11-22 07:24:52 -08001726void AudioProcessingImpl::MutateConfig(
1727 rtc::FunctionView<void(AudioProcessing::Config*)> mutator) {
1728 rtc::CritScope cs_render(&crit_render_);
1729 rtc::CritScope cs_capture(&crit_capture_);
1730 mutator(&config_);
1731 ApplyConfig(config_);
1732}
1733
1734AudioProcessing::Config AudioProcessingImpl::GetConfig() const {
1735 rtc::CritScope cs_render(&crit_render_);
1736 rtc::CritScope cs_capture(&crit_capture_);
1737 return config_;
1738}
1739
peah2ace3f92016-09-10 04:42:27 -07001740bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1741 return submodule_states_.Update(
peah8271d042016-11-22 07:24:52 -08001742 config_.high_pass_filter.enabled,
peah2ace3f92016-09-10 04:42:27 -07001743 public_submodules_->echo_cancellation->is_enabled(),
1744 public_submodules_->echo_control_mobile->is_enabled(),
ivoc9f4a4a02016-10-28 05:39:16 -07001745 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001746 public_submodules_->noise_suppression->is_enabled(),
1747 capture_nonlocked_.intelligibility_enabled,
1748 capture_nonlocked_.beamformer_enabled,
1749 public_submodules_->gain_control->is_enabled(),
alessiob3ec96df2017-05-22 06:57:06 -07001750 capture_nonlocked_.gain_controller2_enabled,
peah2ace3f92016-09-10 04:42:27 -07001751 capture_nonlocked_.level_controller_enabled,
peahe0eae3c2016-12-14 01:16:23 -08001752 capture_nonlocked_.echo_canceller3_enabled,
peah2ace3f92016-09-10 04:42:27 -07001753 public_submodules_->voice_detection->is_enabled(),
1754 public_submodules_->level_estimator->is_enabled(),
1755 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001756}
1757
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001758
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001759void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001760 if (capture_.transient_suppressor_enabled) {
1761 if (!public_submodules_->transient_suppressor.get()) {
1762 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001763 }
peahdf3efa82015-11-28 12:35:15 -08001764 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001765 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1766 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001767 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001768}
1769
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001770void AudioProcessingImpl::InitializeBeamformer() {
aluebsb2328d12016-01-11 20:32:29 -08001771 if (capture_nonlocked_.beamformer_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001772 if (!private_submodules_->beamformer) {
1773 private_submodules_->beamformer.reset(new NonlinearBeamformer(
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001774 capture_.array_geometry, 1u, capture_.target_direction));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001775 }
peahdf3efa82015-11-28 12:35:15 -08001776 private_submodules_->beamformer->Initialize(kChunkSizeMs,
1777 capture_nonlocked_.split_rate);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001778 }
1779}
1780
ekmeyerson60d9b332015-08-14 10:35:55 -07001781void AudioProcessingImpl::InitializeIntelligibility() {
peah1bcfce52016-08-26 07:16:04 -07001782#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001783 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001784 public_submodules_->intelligibility_enhancer.reset(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -08001785 new IntelligibilityEnhancer(capture_nonlocked_.split_rate,
Alex Luebs57ae8292016-03-09 16:24:34 +01001786 render_.render_audio->num_channels(),
Alejandro Luebsef009252016-09-20 14:51:56 -07001787 render_.render_audio->num_bands(),
Alex Luebs57ae8292016-03-09 16:24:34 +01001788 NoiseSuppressionImpl::num_noise_bins()));
ekmeyerson60d9b332015-08-14 10:35:55 -07001789 }
peah1bcfce52016-08-26 07:16:04 -07001790#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001791}
1792
peah8271d042016-11-22 07:24:52 -08001793void AudioProcessingImpl::InitializeLowCutFilter() {
1794 if (config_.high_pass_filter.enabled) {
1795 private_submodules_->low_cut_filter.reset(
1796 new LowCutFilter(num_proc_channels(), proc_sample_rate_hz()));
1797 } else {
1798 private_submodules_->low_cut_filter.reset();
1799 }
1800}
alessiob3ec96df2017-05-22 06:57:06 -07001801
peahe0eae3c2016-12-14 01:16:23 -08001802void AudioProcessingImpl::InitializeEchoCanceller3() {
1803 if (capture_nonlocked_.echo_canceller3_enabled) {
1804 private_submodules_->echo_canceller3.reset(
1805 new EchoCanceller3(proc_sample_rate_hz(), true));
1806 } else {
1807 private_submodules_->echo_canceller3.reset();
1808 }
1809}
peah8271d042016-11-22 07:24:52 -08001810
alessiob3ec96df2017-05-22 06:57:06 -07001811void AudioProcessingImpl::InitializeGainController2() {
1812 if (capture_nonlocked_.gain_controller2_enabled) {
1813 private_submodules_->gain_controller2.reset(
1814 new GainController2(proc_sample_rate_hz()));
1815 } else {
1816 private_submodules_->gain_controller2.reset();
1817 }
1818}
1819
peahca4cac72016-06-29 15:26:12 -07001820void AudioProcessingImpl::InitializeLevelController() {
1821 private_submodules_->level_controller->Initialize(proc_sample_rate_hz());
1822}
1823
ivoc9f4a4a02016-10-28 05:39:16 -07001824void AudioProcessingImpl::InitializeResidualEchoDetector() {
1825 private_submodules_->residual_echo_detector->Initialize();
1826}
1827
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001828void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001829 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001830
1831 if (echo_cancellation()->is_enabled()) {
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001832 // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
1833 // If a stream has echo we know that the echo_cancellation is in process.
peahdf3efa82015-11-28 12:35:15 -08001834 if (capture_.stream_delay_jumps == -1 &&
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001835 echo_cancellation()->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001836 capture_.stream_delay_jumps = 0;
1837 }
1838 if (capture_.aec_system_delay_jumps == -1 &&
1839 echo_cancellation()->stream_has_echo()) {
1840 capture_.aec_system_delay_jumps = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001841 }
1842
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001843 // Detect a jump in platform reported system delay and log the difference.
peahdf3efa82015-11-28 12:35:15 -08001844 const int diff_stream_delay_ms =
1845 capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
1846 if (diff_stream_delay_ms > kMinDiffDelayMs &&
1847 capture_.last_stream_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001848 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1849 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
peahdf3efa82015-11-28 12:35:15 -08001850 if (capture_.stream_delay_jumps == -1) {
1851 capture_.stream_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001852 }
peahdf3efa82015-11-28 12:35:15 -08001853 capture_.stream_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001854 }
peahdf3efa82015-11-28 12:35:15 -08001855 capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001856
1857 // Detect a jump in AEC system delay and log the difference.
peah20028c42016-03-04 11:50:54 -08001858 const int samples_per_ms =
peahdf3efa82015-11-28 12:35:15 -08001859 rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
peah20028c42016-03-04 11:50:54 -08001860 RTC_DCHECK_LT(0, samples_per_ms);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001861 const int aec_system_delay_ms =
peah20028c42016-03-04 11:50:54 -08001862 public_submodules_->echo_cancellation->GetSystemDelayInSamples() /
1863 samples_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001864 const int diff_aec_system_delay_ms =
peahdf3efa82015-11-28 12:35:15 -08001865 aec_system_delay_ms - capture_.last_aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001866 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
peahdf3efa82015-11-28 12:35:15 -08001867 capture_.last_aec_system_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001868 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1869 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1870 100);
peahdf3efa82015-11-28 12:35:15 -08001871 if (capture_.aec_system_delay_jumps == -1) {
1872 capture_.aec_system_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001873 }
peahdf3efa82015-11-28 12:35:15 -08001874 capture_.aec_system_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001875 }
peahdf3efa82015-11-28 12:35:15 -08001876 capture_.last_aec_system_delay_ms = aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001877 }
1878}
1879
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001880void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
peahdf3efa82015-11-28 12:35:15 -08001881 // Run in a single-threaded manner.
1882 rtc::CritScope cs_render(&crit_render_);
1883 rtc::CritScope cs_capture(&crit_capture_);
1884
1885 if (capture_.stream_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001886 RTC_HISTOGRAM_ENUMERATION(
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001887 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
peahdf3efa82015-11-28 12:35:15 -08001888 capture_.stream_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001889 }
peahdf3efa82015-11-28 12:35:15 -08001890 capture_.stream_delay_jumps = -1;
1891 capture_.last_stream_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001892
peahdf3efa82015-11-28 12:35:15 -08001893 if (capture_.aec_system_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001894 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1895 capture_.aec_system_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001896 }
peahdf3efa82015-11-28 12:35:15 -08001897 capture_.aec_system_delay_jumps = -1;
1898 capture_.last_aec_system_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001899}
1900
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001901#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
peahdf3efa82015-11-28 12:35:15 -08001902int AudioProcessingImpl::WriteMessageToDebugFile(
1903 FileWrapper* debug_file,
ivocd66b44d2016-01-15 03:06:36 -08001904 int64_t* filesize_limit_bytes,
peahdf3efa82015-11-28 12:35:15 -08001905 rtc::CriticalSection* crit_debug,
1906 ApmDebugDumpThreadState* debug_state) {
1907 int32_t size = debug_state->event_msg->ByteSize();
ajm@google.com808e0e02011-08-03 21:08:51 +00001908 if (size <= 0) {
1909 return kUnspecifiedError;
1910 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001911#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Michael Graczyk86c6d332015-07-23 11:41:39 -07001912// TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1913// pretty safe in assuming little-endian.
ajm@google.com808e0e02011-08-03 21:08:51 +00001914#endif
1915
peahdf3efa82015-11-28 12:35:15 -08001916 if (!debug_state->event_msg->SerializeToString(&debug_state->event_str)) {
ajm@google.com808e0e02011-08-03 21:08:51 +00001917 return kUnspecifiedError;
1918 }
1919
peahdf3efa82015-11-28 12:35:15 -08001920 {
1921 // Ensure atomic writes of the message.
ivocd66b44d2016-01-15 03:06:36 -08001922 rtc::CritScope cs_debug(crit_debug);
1923
tommia6219cc2016-06-15 10:30:14 -07001924 RTC_DCHECK(debug_file->is_open());
ivocd66b44d2016-01-15 03:06:36 -08001925 // Update the byte counter.
1926 if (*filesize_limit_bytes >= 0) {
1927 *filesize_limit_bytes -=
1928 (sizeof(int32_t) + debug_state->event_str.length());
1929 if (*filesize_limit_bytes < 0) {
1930 // Not enough bytes are left to write this message, so stop logging.
1931 debug_file->CloseFile();
1932 return kNoError;
1933 }
1934 }
peahdf3efa82015-11-28 12:35:15 -08001935 // Write message preceded by its size.
1936 if (!debug_file->Write(&size, sizeof(int32_t))) {
1937 return kFileError;
1938 }
1939 if (!debug_file->Write(debug_state->event_str.data(),
1940 debug_state->event_str.length())) {
1941 return kFileError;
1942 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001943 }
1944
peahdf3efa82015-11-28 12:35:15 -08001945 debug_state->event_msg->Clear();
ajm@google.com808e0e02011-08-03 21:08:51 +00001946
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001947 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001948}
1949
1950int AudioProcessingImpl::WriteInitMessage() {
peahdf3efa82015-11-28 12:35:15 -08001951 debug_dump_.capture.event_msg->set_type(audioproc::Event::INIT);
1952 audioproc::Init* msg = debug_dump_.capture.event_msg->mutable_init();
1953 msg->set_sample_rate(formats_.api_format.input_stream().sample_rate_hz());
ajm@google.com808e0e02011-08-03 21:08:51 +00001954
mbonadei7c2c8432017-04-07 00:59:12 -07001955 msg->set_num_input_channels(static_cast<int32_t>(
Peter Kasting69558702016-01-12 16:26:35 -08001956 formats_.api_format.input_stream().num_channels()));
mbonadei7c2c8432017-04-07 00:59:12 -07001957 msg->set_num_output_channels(static_cast<int32_t>(
Peter Kasting69558702016-01-12 16:26:35 -08001958 formats_.api_format.output_stream().num_channels()));
mbonadei7c2c8432017-04-07 00:59:12 -07001959 msg->set_num_reverse_channels(static_cast<int32_t>(
Peter Kasting69558702016-01-12 16:26:35 -08001960 formats_.api_format.reverse_input_stream().num_channels()));
peahdf3efa82015-11-28 12:35:15 -08001961 msg->set_reverse_sample_rate(
1962 formats_.api_format.reverse_input_stream().sample_rate_hz());
1963 msg->set_output_sample_rate(
1964 formats_.api_format.output_stream().sample_rate_hz());
peahc7bdf8a2016-04-11 07:05:53 -07001965 msg->set_reverse_output_sample_rate(
1966 formats_.api_format.reverse_output_stream().sample_rate_hz());
1967 msg->set_num_reverse_output_channels(
1968 formats_.api_format.reverse_output_stream().num_channels());
peahdf3efa82015-11-28 12:35:15 -08001969
1970 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001971 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001972 &crit_debug_, &debug_dump_.capture));
Minyue13b96ba2015-10-03 00:39:14 +02001973 return kNoError;
1974}
1975
1976int AudioProcessingImpl::WriteConfigMessage(bool forced) {
1977 audioproc::Config config;
1978
peahdf3efa82015-11-28 12:35:15 -08001979 config.set_aec_enabled(public_submodules_->echo_cancellation->is_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001980 config.set_aec_delay_agnostic_enabled(
peahdf3efa82015-11-28 12:35:15 -08001981 public_submodules_->echo_cancellation->is_delay_agnostic_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001982 config.set_aec_drift_compensation_enabled(
peahdf3efa82015-11-28 12:35:15 -08001983 public_submodules_->echo_cancellation->is_drift_compensation_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001984 config.set_aec_extended_filter_enabled(
peahdf3efa82015-11-28 12:35:15 -08001985 public_submodules_->echo_cancellation->is_extended_filter_enabled());
1986 config.set_aec_suppression_level(static_cast<int>(
1987 public_submodules_->echo_cancellation->suppression_level()));
Minyue13b96ba2015-10-03 00:39:14 +02001988
peahdf3efa82015-11-28 12:35:15 -08001989 config.set_aecm_enabled(
1990 public_submodules_->echo_control_mobile->is_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001991 config.set_aecm_comfort_noise_enabled(
peahdf3efa82015-11-28 12:35:15 -08001992 public_submodules_->echo_control_mobile->is_comfort_noise_enabled());
1993 config.set_aecm_routing_mode(static_cast<int>(
1994 public_submodules_->echo_control_mobile->routing_mode()));
Minyue13b96ba2015-10-03 00:39:14 +02001995
peahdf3efa82015-11-28 12:35:15 -08001996 config.set_agc_enabled(public_submodules_->gain_control->is_enabled());
1997 config.set_agc_mode(
1998 static_cast<int>(public_submodules_->gain_control->mode()));
1999 config.set_agc_limiter_enabled(
2000 public_submodules_->gain_control->is_limiter_enabled());
peahbe615622016-02-13 16:40:47 -08002001 config.set_noise_robust_agc_enabled(constants_.use_experimental_agc);
Minyue13b96ba2015-10-03 00:39:14 +02002002
peah8271d042016-11-22 07:24:52 -08002003 config.set_hpf_enabled(config_.high_pass_filter.enabled);
Minyue13b96ba2015-10-03 00:39:14 +02002004
peahdf3efa82015-11-28 12:35:15 -08002005 config.set_ns_enabled(public_submodules_->noise_suppression->is_enabled());
2006 config.set_ns_level(
2007 static_cast<int>(public_submodules_->noise_suppression->level()));
Minyue13b96ba2015-10-03 00:39:14 +02002008
peahdf3efa82015-11-28 12:35:15 -08002009 config.set_transient_suppression_enabled(
2010 capture_.transient_suppressor_enabled);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07002011 config.set_intelligibility_enhancer_enabled(
2012 capture_nonlocked_.intelligibility_enabled);
Minyue13b96ba2015-10-03 00:39:14 +02002013
peah7789fe72016-04-15 01:19:44 -07002014 std::string experiments_description =
2015 public_submodules_->echo_cancellation->GetExperimentsDescription();
2016 // TODO(peah): Add semicolon-separated concatenations of experiment
2017 // descriptions for other submodules.
peahca4cac72016-06-29 15:26:12 -07002018 if (capture_nonlocked_.level_controller_enabled) {
2019 experiments_description += "LevelController;";
2020 }
henrik.lundinbd681b92016-12-05 09:08:42 -08002021 if (constants_.agc_clipped_level_min != kClippedLevelMin) {
2022 experiments_description += "AgcClippingLevelExperiment;";
2023 }
peahe0eae3c2016-12-14 01:16:23 -08002024 if (capture_nonlocked_.echo_canceller3_enabled) {
2025 experiments_description += "EchoCanceller3;";
2026 }
peah7789fe72016-04-15 01:19:44 -07002027 config.set_experiments_description(experiments_description);
2028
mbonadei7c2c8432017-04-07 00:59:12 -07002029 ProtoString serialized_config = config.SerializeAsString();
peahdf3efa82015-11-28 12:35:15 -08002030 if (!forced &&
2031 debug_dump_.capture.last_serialized_config == serialized_config) {
Minyue13b96ba2015-10-03 00:39:14 +02002032 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00002033 }
2034
peahdf3efa82015-11-28 12:35:15 -08002035 debug_dump_.capture.last_serialized_config = serialized_config;
Minyue13b96ba2015-10-03 00:39:14 +02002036
peahdf3efa82015-11-28 12:35:15 -08002037 debug_dump_.capture.event_msg->set_type(audioproc::Event::CONFIG);
2038 debug_dump_.capture.event_msg->mutable_config()->CopyFrom(config);
Minyue13b96ba2015-10-03 00:39:14 +02002039
peahdf3efa82015-11-28 12:35:15 -08002040 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08002041 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08002042 &crit_debug_, &debug_dump_.capture));
ajm@google.com808e0e02011-08-03 21:08:51 +00002043 return kNoError;
2044}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00002045#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00002046
kwiberg83ffe452016-08-29 14:46:07 -07002047AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
2048 bool transient_suppressor_enabled,
2049 const std::vector<Point>& array_geometry,
2050 SphericalPointf target_direction)
2051 : aec_system_delay_jumps(-1),
2052 delay_offset_ms(0),
2053 was_stream_delay_set(false),
2054 last_stream_delay_ms(0),
2055 last_aec_system_delay_ms(0),
2056 stream_delay_jumps(-1),
2057 output_will_be_muted(false),
2058 key_pressed(false),
2059 transient_suppressor_enabled(transient_suppressor_enabled),
2060 array_geometry(array_geometry),
2061 target_direction(target_direction),
peahde65ddc2016-09-16 15:02:15 -07002062 capture_processing_format(kSampleRate16kHz),
peah67995532017-04-10 14:12:41 -07002063 split_rate(kSampleRate16kHz),
2064 previous_agc_level(0),
2065 echo_path_gain_change(false) {}
kwiberg83ffe452016-08-29 14:46:07 -07002066
2067AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
2068
2069AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
2070
2071AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
2072
niklase@google.com470e71d2011-07-07 08:21:25 +00002073} // namespace webrtc