blob: fe4dcee4cb7f5373c90f37c8b7f9ec3d3d17ca7b [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
Michael Graczyk86c6d332015-07-23 11:41:39 -070013#include <algorithm>
niklase@google.com470e71d2011-07-07 08:21:25 +000014
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020015#include "webrtc/base/checks.h"
xians@webrtc.orge46bc772014-10-10 08:36:56 +000016#include "webrtc/base/platform_file.h"
peah369f8282015-12-17 06:42:29 -080017#include "webrtc/base/trace_event.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070018#include "webrtc/common_audio/audio_converter.h"
Michael Graczykdfa36052015-03-25 16:37:27 -070019#include "webrtc/common_audio/channel_buffer.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070020#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000021#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020022#include "webrtc/modules/audio_processing/aec/aec_core.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000023#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000024#include "webrtc/modules/audio_processing/audio_buffer.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000025#include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000026#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000027#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000028#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
peahbe615622016-02-13 16:40:47 -080029#include "webrtc/modules/audio_processing/gain_control_for_experimental_agc.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000030#include "webrtc/modules/audio_processing/gain_control_impl.h"
31#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
peah1bcfce52016-08-26 07:16:04 -070032#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -070033#include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h"
peah1bcfce52016-08-26 07:16:04 -070034#endif
peahca4cac72016-06-29 15:26:12 -070035#include "webrtc/modules/audio_processing/level_controller/level_controller.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000036#include "webrtc/modules/audio_processing/level_estimator_impl.h"
37#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
ivoc9f4a4a02016-10-28 05:39:16 -070038#include "webrtc/modules/audio_processing/residual_echo_detector.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000039#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000040#include "webrtc/modules/audio_processing/voice_detection_impl.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010041#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010042#include "webrtc/system_wrappers/include/file_wrapper.h"
43#include "webrtc/system_wrappers/include/logging.h"
44#include "webrtc/system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000045
46#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
47// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000048#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000049#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000050#else
kjellander78ddd732016-02-09 08:13:06 -080051#include "webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000052#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000053#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000054
peah1bcfce52016-08-26 07:16:04 -070055// Check to verify that the define for the intelligibility enhancer is properly
56// set.
57#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
58 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
59 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
60#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
61#endif
62
Michael Graczyk86c6d332015-07-23 11:41:39 -070063#define RETURN_ON_ERR(expr) \
64 do { \
65 int err = (expr); \
66 if (err != kNoError) { \
67 return err; \
68 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000069 } while (0)
70
niklase@google.com470e71d2011-07-07 08:21:25 +000071namespace webrtc {
aluebsdf6416a2016-03-16 18:26:35 -070072
kwibergd59d3bb2016-09-13 07:49:33 -070073constexpr int AudioProcessing::kNativeSampleRatesHz[];
aluebsdf6416a2016-03-16 18:26:35 -070074
Michael Graczyk86c6d332015-07-23 11:41:39 -070075namespace {
76
77static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
78 switch (layout) {
79 case AudioProcessing::kMono:
80 case AudioProcessing::kStereo:
81 return false;
82 case AudioProcessing::kMonoAndKeyboard:
83 case AudioProcessing::kStereoAndKeyboard:
84 return true;
85 }
86
kwiberg9e2be5f2016-09-14 05:23:22 -070087 RTC_NOTREACHED();
Michael Graczyk86c6d332015-07-23 11:41:39 -070088 return false;
89}
aluebsdf6416a2016-03-16 18:26:35 -070090
peah2ace3f92016-09-10 04:42:27 -070091bool SampleRateSupportsMultiBand(int sample_rate_hz) {
aluebsdf6416a2016-03-16 18:26:35 -070092 return sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
93 sample_rate_hz == AudioProcessing::kSampleRate48kHz;
94}
95
peah2ace3f92016-09-10 04:42:27 -070096int FindNativeProcessRateToUse(int minimum_rate, bool band_splitting_required) {
97#ifdef WEBRTC_ARCH_ARM_FAMILY
kwibergd59d3bb2016-09-13 07:49:33 -070098 constexpr int kMaxSplittingNativeProcessRate =
99 AudioProcessing::kSampleRate32kHz;
peah2ace3f92016-09-10 04:42:27 -0700100#else
kwibergd59d3bb2016-09-13 07:49:33 -0700101 constexpr int kMaxSplittingNativeProcessRate =
102 AudioProcessing::kSampleRate48kHz;
peah2ace3f92016-09-10 04:42:27 -0700103#endif
kwibergd59d3bb2016-09-13 07:49:33 -0700104 static_assert(
105 kMaxSplittingNativeProcessRate <= AudioProcessing::kMaxNativeSampleRateHz,
106 "");
peah2ace3f92016-09-10 04:42:27 -0700107 const int uppermost_native_rate = band_splitting_required
108 ? kMaxSplittingNativeProcessRate
109 : AudioProcessing::kSampleRate48kHz;
110
111 for (auto rate : AudioProcessing::kNativeSampleRatesHz) {
112 if (rate >= uppermost_native_rate) {
113 return uppermost_native_rate;
114 }
115 if (rate >= minimum_rate) {
aluebsdf6416a2016-03-16 18:26:35 -0700116 return rate;
117 }
118 }
peah2ace3f92016-09-10 04:42:27 -0700119 RTC_NOTREACHED();
120 return uppermost_native_rate;
aluebsdf6416a2016-03-16 18:26:35 -0700121}
122
peah764e3642016-10-22 05:04:30 -0700123// Maximum length that a frame of samples can have.
124static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160;
125// Maximum number of frames to buffer in the render queue.
126// TODO(peah): Decrease this once we properly handle hugely unbalanced
127// reverse and forward call numbers.
128static const size_t kMaxNumFramesToBuffer = 100;
129
Michael Graczyk86c6d332015-07-23 11:41:39 -0700130} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000131
132// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +0000133static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000134
peah2ace3f92016-09-10 04:42:27 -0700135AudioProcessingImpl::ApmSubmoduleStates::ApmSubmoduleStates() {}
136
137bool AudioProcessingImpl::ApmSubmoduleStates::Update(
138 bool high_pass_filter_enabled,
139 bool echo_canceller_enabled,
140 bool mobile_echo_controller_enabled,
ivoc9f4a4a02016-10-28 05:39:16 -0700141 bool residual_echo_detector_enabled,
peah2ace3f92016-09-10 04:42:27 -0700142 bool noise_suppressor_enabled,
143 bool intelligibility_enhancer_enabled,
144 bool beamformer_enabled,
145 bool adaptive_gain_controller_enabled,
146 bool level_controller_enabled,
147 bool voice_activity_detector_enabled,
148 bool level_estimator_enabled,
149 bool transient_suppressor_enabled) {
150 bool changed = false;
151 changed |= (high_pass_filter_enabled != high_pass_filter_enabled_);
152 changed |= (echo_canceller_enabled != echo_canceller_enabled_);
153 changed |=
154 (mobile_echo_controller_enabled != mobile_echo_controller_enabled_);
ivoc9f4a4a02016-10-28 05:39:16 -0700155 changed |=
156 (residual_echo_detector_enabled != residual_echo_detector_enabled_);
peah2ace3f92016-09-10 04:42:27 -0700157 changed |= (noise_suppressor_enabled != noise_suppressor_enabled_);
158 changed |=
159 (intelligibility_enhancer_enabled != intelligibility_enhancer_enabled_);
160 changed |= (beamformer_enabled != beamformer_enabled_);
161 changed |=
162 (adaptive_gain_controller_enabled != adaptive_gain_controller_enabled_);
163 changed |= (level_controller_enabled != level_controller_enabled_);
164 changed |= (level_estimator_enabled != level_estimator_enabled_);
165 changed |=
166 (voice_activity_detector_enabled != voice_activity_detector_enabled_);
167 changed |= (transient_suppressor_enabled != transient_suppressor_enabled_);
168 if (changed) {
169 high_pass_filter_enabled_ = high_pass_filter_enabled;
170 echo_canceller_enabled_ = echo_canceller_enabled;
171 mobile_echo_controller_enabled_ = mobile_echo_controller_enabled;
ivoc9f4a4a02016-10-28 05:39:16 -0700172 residual_echo_detector_enabled_ = residual_echo_detector_enabled;
peah2ace3f92016-09-10 04:42:27 -0700173 noise_suppressor_enabled_ = noise_suppressor_enabled;
174 intelligibility_enhancer_enabled_ = intelligibility_enhancer_enabled;
175 beamformer_enabled_ = beamformer_enabled;
176 adaptive_gain_controller_enabled_ = adaptive_gain_controller_enabled;
177 level_controller_enabled_ = level_controller_enabled;
178 level_estimator_enabled_ = level_estimator_enabled;
179 voice_activity_detector_enabled_ = voice_activity_detector_enabled;
180 transient_suppressor_enabled_ = transient_suppressor_enabled;
181 }
182
183 changed |= first_update_;
184 first_update_ = false;
185 return changed;
186}
187
188bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandSubModulesActive()
189 const {
190#if WEBRTC_INTELLIGIBILITY_ENHANCER
191 return CaptureMultiBandProcessingActive() ||
192 intelligibility_enhancer_enabled_ || voice_activity_detector_enabled_;
193#else
194 return CaptureMultiBandProcessingActive() || voice_activity_detector_enabled_;
195#endif
196}
197
198bool AudioProcessingImpl::ApmSubmoduleStates::CaptureMultiBandProcessingActive()
199 const {
200 return high_pass_filter_enabled_ || echo_canceller_enabled_ ||
201 mobile_echo_controller_enabled_ || noise_suppressor_enabled_ ||
202 beamformer_enabled_ || adaptive_gain_controller_enabled_;
203}
204
205bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandSubModulesActive()
206 const {
207 return RenderMultiBandProcessingActive() || echo_canceller_enabled_ ||
208 mobile_echo_controller_enabled_ || adaptive_gain_controller_enabled_;
209}
210
211bool AudioProcessingImpl::ApmSubmoduleStates::RenderMultiBandProcessingActive()
212 const {
213#if WEBRTC_INTELLIGIBILITY_ENHANCER
214 return intelligibility_enhancer_enabled_;
215#else
216 return false;
217#endif
218}
219
solenberg5e465c32015-12-08 13:22:33 -0800220struct AudioProcessingImpl::ApmPublicSubmodules {
peahbfa97112016-03-10 21:09:04 -0800221 ApmPublicSubmodules() {}
solenberg5e465c32015-12-08 13:22:33 -0800222 // Accessed externally of APM without any lock acquired.
peahb624d8c2016-03-05 03:01:14 -0800223 std::unique_ptr<EchoCancellationImpl> echo_cancellation;
peahbb9edbd2016-03-10 12:54:25 -0800224 std::unique_ptr<EchoControlMobileImpl> echo_control_mobile;
peahbfa97112016-03-10 21:09:04 -0800225 std::unique_ptr<GainControlImpl> gain_control;
kwiberg88788ad2016-02-19 07:04:49 -0800226 std::unique_ptr<HighPassFilterImpl> high_pass_filter;
227 std::unique_ptr<LevelEstimatorImpl> level_estimator;
228 std::unique_ptr<NoiseSuppressionImpl> noise_suppression;
229 std::unique_ptr<VoiceDetectionImpl> voice_detection;
230 std::unique_ptr<GainControlForExperimentalAgc>
peahbe615622016-02-13 16:40:47 -0800231 gain_control_for_experimental_agc;
solenberg5e465c32015-12-08 13:22:33 -0800232
233 // Accessed internally from both render and capture.
kwiberg88788ad2016-02-19 07:04:49 -0800234 std::unique_ptr<TransientSuppressor> transient_suppressor;
peah1bcfce52016-08-26 07:16:04 -0700235#if WEBRTC_INTELLIGIBILITY_ENHANCER
kwiberg88788ad2016-02-19 07:04:49 -0800236 std::unique_ptr<IntelligibilityEnhancer> intelligibility_enhancer;
peah1bcfce52016-08-26 07:16:04 -0700237#endif
solenberg5e465c32015-12-08 13:22:33 -0800238};
239
240struct AudioProcessingImpl::ApmPrivateSubmodules {
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700241 explicit ApmPrivateSubmodules(NonlinearBeamformer* beamformer)
solenberg5e465c32015-12-08 13:22:33 -0800242 : beamformer(beamformer) {}
243 // Accessed internally from capture or during initialization
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700244 std::unique_ptr<NonlinearBeamformer> beamformer;
kwiberg88788ad2016-02-19 07:04:49 -0800245 std::unique_ptr<AgcManagerDirect> agc_manager;
peahca4cac72016-06-29 15:26:12 -0700246 std::unique_ptr<LevelController> level_controller;
ivoc9f4a4a02016-10-28 05:39:16 -0700247 std::unique_ptr<ResidualEchoDetector> residual_echo_detector;
solenberg5e465c32015-12-08 13:22:33 -0800248};
249
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000250AudioProcessing* AudioProcessing::Create() {
peah88ac8532016-09-12 16:47:25 -0700251 webrtc::Config config;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000252 return Create(config, nullptr);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000253}
254
peah88ac8532016-09-12 16:47:25 -0700255AudioProcessing* AudioProcessing::Create(const webrtc::Config& config) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000256 return Create(config, nullptr);
257}
258
peah88ac8532016-09-12 16:47:25 -0700259AudioProcessing* AudioProcessing::Create(const webrtc::Config& config,
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700260 NonlinearBeamformer* beamformer) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000261 AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000262 if (apm->Initialize() != kNoError) {
263 delete apm;
peahdf3efa82015-11-28 12:35:15 -0800264 apm = nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000265 }
266
267 return apm;
268}
269
peah88ac8532016-09-12 16:47:25 -0700270AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config)
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000271 : AudioProcessingImpl(config, nullptr) {}
272
peah88ac8532016-09-12 16:47:25 -0700273AudioProcessingImpl::AudioProcessingImpl(const webrtc::Config& config,
Alejandro Luebsf4022ff2016-07-01 17:19:09 -0700274 NonlinearBeamformer* beamformer)
peahdf3efa82015-11-28 12:35:15 -0800275 : public_submodules_(new ApmPublicSubmodules()),
276 private_submodules_(new ApmPrivateSubmodules(beamformer)),
277 constants_(config.Get<ExperimentalAgc>().startup_min_volume,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000278#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700279 false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000280#else
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700281 config.Get<ExperimentalAgc>().enabled),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000282#endif
andrew1c7075f2015-06-24 18:14:14 -0700283#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
aluebs2a346882016-01-11 18:04:30 -0800284 capture_(false,
andrew1c7075f2015-06-24 18:14:14 -0700285#else
aluebs2a346882016-01-11 18:04:30 -0800286 capture_(config.Get<ExperimentalNs>().enabled,
andrew1c7075f2015-06-24 18:14:14 -0700287#endif
aluebs2a346882016-01-11 18:04:30 -0800288 config.Get<Beamforming>().array_geometry,
aluebsb2328d12016-01-11 20:32:29 -0800289 config.Get<Beamforming>().target_direction),
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700290 capture_nonlocked_(config.Get<Beamforming>().enabled,
peah88ac8532016-09-12 16:47:25 -0700291 config.Get<Intelligibility>().enabled) {
peahdf3efa82015-11-28 12:35:15 -0800292 {
293 rtc::CritScope cs_render(&crit_render_);
294 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000295
peahb624d8c2016-03-05 03:01:14 -0800296 public_submodules_->echo_cancellation.reset(
peahb58a1582016-03-15 09:34:24 -0700297 new EchoCancellationImpl(&crit_render_, &crit_capture_));
peahbb9edbd2016-03-10 12:54:25 -0800298 public_submodules_->echo_control_mobile.reset(
peah253534d2016-03-15 04:32:28 -0700299 new EchoControlMobileImpl(&crit_render_, &crit_capture_));
peahbfa97112016-03-10 21:09:04 -0800300 public_submodules_->gain_control.reset(
peahb8fbb542016-03-15 02:28:08 -0700301 new GainControlImpl(&crit_capture_, &crit_capture_));
solenberg70f99032015-12-08 11:07:32 -0800302 public_submodules_->high_pass_filter.reset(
303 new HighPassFilterImpl(&crit_capture_));
solenberg949028f2015-12-15 11:39:38 -0800304 public_submodules_->level_estimator.reset(
305 new LevelEstimatorImpl(&crit_capture_));
solenberg5e465c32015-12-08 13:22:33 -0800306 public_submodules_->noise_suppression.reset(
307 new NoiseSuppressionImpl(&crit_capture_));
solenberga29386c2015-12-16 03:31:12 -0800308 public_submodules_->voice_detection.reset(
309 new VoiceDetectionImpl(&crit_capture_));
peahbe615622016-02-13 16:40:47 -0800310 public_submodules_->gain_control_for_experimental_agc.reset(
peahbfa97112016-03-10 21:09:04 -0800311 new GainControlForExperimentalAgc(
312 public_submodules_->gain_control.get(), &crit_capture_));
ivoc9f4a4a02016-10-28 05:39:16 -0700313 private_submodules_->residual_echo_detector.reset(
314 new ResidualEchoDetector());
peahca4cac72016-06-29 15:26:12 -0700315
peahc19f3122016-10-07 14:54:10 -0700316 // TODO(peah): Move this creation to happen only when the level controller
317 // is enabled.
peahca4cac72016-06-29 15:26:12 -0700318 private_submodules_->level_controller.reset(new LevelController());
peahdf3efa82015-11-28 12:35:15 -0800319 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000320
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000321 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000322}
323
324AudioProcessingImpl::~AudioProcessingImpl() {
peahdf3efa82015-11-28 12:35:15 -0800325 // Depends on gain_control_ and
peahbe615622016-02-13 16:40:47 -0800326 // public_submodules_->gain_control_for_experimental_agc.
peahdf3efa82015-11-28 12:35:15 -0800327 private_submodules_->agc_manager.reset();
328 // Depends on gain_control_.
peahbe615622016-02-13 16:40:47 -0800329 public_submodules_->gain_control_for_experimental_agc.reset();
niklase@google.com470e71d2011-07-07 08:21:25 +0000330
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000331#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700332 debug_dump_.debug_file->CloseFile();
peahdf3efa82015-11-28 12:35:15 -0800333#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000334}
335
niklase@google.com470e71d2011-07-07 08:21:25 +0000336int AudioProcessingImpl::Initialize() {
peahdf3efa82015-11-28 12:35:15 -0800337 // Run in a single-threaded manner during initialization.
338 rtc::CritScope cs_render(&crit_render_);
339 rtc::CritScope cs_capture(&crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000340 return InitializeLocked();
341}
342
peahde65ddc2016-09-16 15:02:15 -0700343int AudioProcessingImpl::Initialize(int capture_input_sample_rate_hz,
344 int capture_output_sample_rate_hz,
345 int render_input_sample_rate_hz,
346 ChannelLayout capture_input_layout,
347 ChannelLayout capture_output_layout,
348 ChannelLayout render_input_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700349 const ProcessingConfig processing_config = {
peahde65ddc2016-09-16 15:02:15 -0700350 {{capture_input_sample_rate_hz, ChannelsFromLayout(capture_input_layout),
351 LayoutHasKeyboard(capture_input_layout)},
352 {capture_output_sample_rate_hz,
353 ChannelsFromLayout(capture_output_layout),
354 LayoutHasKeyboard(capture_output_layout)},
355 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
356 LayoutHasKeyboard(render_input_layout)},
357 {render_input_sample_rate_hz, ChannelsFromLayout(render_input_layout),
358 LayoutHasKeyboard(render_input_layout)}}};
Michael Graczyk86c6d332015-07-23 11:41:39 -0700359
360 return Initialize(processing_config);
361}
362
363int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
peahdf3efa82015-11-28 12:35:15 -0800364 // Run in a single-threaded manner during initialization.
365 rtc::CritScope cs_render(&crit_render_);
366 rtc::CritScope cs_capture(&crit_capture_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700367 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000368}
369
peahdf3efa82015-11-28 12:35:15 -0800370int AudioProcessingImpl::MaybeInitializeRender(
peah81b9bfe2015-11-27 02:47:28 -0800371 const ProcessingConfig& processing_config) {
peah2ace3f92016-09-10 04:42:27 -0700372 return MaybeInitialize(processing_config, false);
peah81b9bfe2015-11-27 02:47:28 -0800373}
374
peahdf3efa82015-11-28 12:35:15 -0800375int AudioProcessingImpl::MaybeInitializeCapture(
peah2ace3f92016-09-10 04:42:27 -0700376 const ProcessingConfig& processing_config,
377 bool force_initialization) {
378 return MaybeInitialize(processing_config, force_initialization);
peah81b9bfe2015-11-27 02:47:28 -0800379}
380
kwiberg83ffe452016-08-29 14:46:07 -0700381#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
382
383AudioProcessingImpl::ApmDebugDumpThreadState::ApmDebugDumpThreadState()
384 : event_msg(new audioproc::Event()) {}
385
386AudioProcessingImpl::ApmDebugDumpThreadState::~ApmDebugDumpThreadState() {}
387
388AudioProcessingImpl::ApmDebugDumpState::ApmDebugDumpState()
389 : debug_file(FileWrapper::Create()) {}
390
391AudioProcessingImpl::ApmDebugDumpState::~ApmDebugDumpState() {}
392
393#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
394
peah192164e2015-11-17 02:16:45 -0800395// Calls InitializeLocked() if any of the audio parameters have changed from
peahdf3efa82015-11-28 12:35:15 -0800396// their current values (needs to be called while holding the crit_render_lock).
397int AudioProcessingImpl::MaybeInitialize(
peah2ace3f92016-09-10 04:42:27 -0700398 const ProcessingConfig& processing_config,
399 bool force_initialization) {
peahdf3efa82015-11-28 12:35:15 -0800400 // Called from both threads. Thread check is therefore not possible.
peah2ace3f92016-09-10 04:42:27 -0700401 if (processing_config == formats_.api_format && !force_initialization) {
peah192164e2015-11-17 02:16:45 -0800402 return kNoError;
403 }
peahdf3efa82015-11-28 12:35:15 -0800404
405 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -0800406 return InitializeLocked(processing_config);
407}
408
niklase@google.com470e71d2011-07-07 08:21:25 +0000409int AudioProcessingImpl::InitializeLocked() {
peahde65ddc2016-09-16 15:02:15 -0700410 const int capture_audiobuffer_num_channels =
aluebsb2328d12016-01-11 20:32:29 -0800411 capture_nonlocked_.beamformer_enabled
peahdf3efa82015-11-28 12:35:15 -0800412 ? formats_.api_format.input_stream().num_channels()
413 : formats_.api_format.output_stream().num_channels();
peahde65ddc2016-09-16 15:02:15 -0700414 const int render_audiobuffer_num_output_frames =
peahdf3efa82015-11-28 12:35:15 -0800415 formats_.api_format.reverse_output_stream().num_frames() == 0
peahde65ddc2016-09-16 15:02:15 -0700416 ? formats_.render_processing_format.num_frames()
peahdf3efa82015-11-28 12:35:15 -0800417 : formats_.api_format.reverse_output_stream().num_frames();
418 if (formats_.api_format.reverse_input_stream().num_channels() > 0) {
419 render_.render_audio.reset(new AudioBuffer(
420 formats_.api_format.reverse_input_stream().num_frames(),
421 formats_.api_format.reverse_input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700422 formats_.render_processing_format.num_frames(),
423 formats_.render_processing_format.num_channels(),
424 render_audiobuffer_num_output_frames));
peah2ace3f92016-09-10 04:42:27 -0700425 if (formats_.api_format.reverse_input_stream() !=
426 formats_.api_format.reverse_output_stream()) {
kwibergc2b785d2016-02-24 05:22:32 -0800427 render_.render_converter = AudioConverter::Create(
peahdf3efa82015-11-28 12:35:15 -0800428 formats_.api_format.reverse_input_stream().num_channels(),
429 formats_.api_format.reverse_input_stream().num_frames(),
430 formats_.api_format.reverse_output_stream().num_channels(),
kwibergc2b785d2016-02-24 05:22:32 -0800431 formats_.api_format.reverse_output_stream().num_frames());
ekmeyerson60d9b332015-08-14 10:35:55 -0700432 } else {
peahdf3efa82015-11-28 12:35:15 -0800433 render_.render_converter.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700434 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700435 } else {
peahdf3efa82015-11-28 12:35:15 -0800436 render_.render_audio.reset(nullptr);
437 render_.render_converter.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700438 }
peahdf3efa82015-11-28 12:35:15 -0800439 capture_.capture_audio.reset(
440 new AudioBuffer(formats_.api_format.input_stream().num_frames(),
441 formats_.api_format.input_stream().num_channels(),
peahde65ddc2016-09-16 15:02:15 -0700442 capture_nonlocked_.capture_processing_format.num_frames(),
443 capture_audiobuffer_num_channels,
peahdf3efa82015-11-28 12:35:15 -0800444 formats_.api_format.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000445
peahde65ddc2016-09-16 15:02:15 -0700446 public_submodules_->echo_cancellation->Initialize(
447 proc_sample_rate_hz(), num_reverse_channels(), num_output_channels(),
448 num_proc_channels());
peah764e3642016-10-22 05:04:30 -0700449 AllocateRenderQueue();
450
ivoc3e9a5372016-10-28 07:55:33 -0700451 int success = public_submodules_->echo_cancellation->enable_metrics(true);
452 RTC_DCHECK_EQ(0, success);
453 success = public_submodules_->echo_cancellation->enable_delay_logging(true);
454 RTC_DCHECK_EQ(0, success);
peahde65ddc2016-09-16 15:02:15 -0700455 public_submodules_->echo_control_mobile->Initialize(
456 proc_split_sample_rate_hz(), num_reverse_channels(),
457 num_output_channels());
peah135259a2016-10-28 03:12:11 -0700458
459 public_submodules_->gain_control->Initialize(num_proc_channels(),
460 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700461 if (constants_.use_experimental_agc) {
462 if (!private_submodules_->agc_manager.get()) {
463 private_submodules_->agc_manager.reset(new AgcManagerDirect(
464 public_submodules_->gain_control.get(),
465 public_submodules_->gain_control_for_experimental_agc.get(),
466 constants_.agc_startup_min_volume));
467 }
468 private_submodules_->agc_manager->Initialize();
469 private_submodules_->agc_manager->SetCaptureMuted(
470 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700471 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700472 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200473 InitializeTransient();
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000474 InitializeBeamformer();
peah1bcfce52016-08-26 07:16:04 -0700475#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -0700476 InitializeIntelligibility();
peah1bcfce52016-08-26 07:16:04 -0700477#endif
peahde65ddc2016-09-16 15:02:15 -0700478 public_submodules_->high_pass_filter->Initialize(num_proc_channels(),
479 proc_sample_rate_hz());
480 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
481 proc_sample_rate_hz());
482 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
483 public_submodules_->level_estimator->Initialize();
peahca4cac72016-06-29 15:26:12 -0700484 InitializeLevelController();
ivoc9f4a4a02016-10-28 05:39:16 -0700485 InitializeResidualEchoDetector();
solenberg70f99032015-12-08 11:07:32 -0800486
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000487#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700488 if (debug_dump_.debug_file->is_open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000489 int err = WriteInitMessage();
490 if (err != kNoError) {
491 return err;
492 }
493 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000494#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000495
niklase@google.com470e71d2011-07-07 08:21:25 +0000496 return kNoError;
497}
498
Michael Graczyk86c6d332015-07-23 11:41:39 -0700499int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
500 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700501 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
502 return kBadSampleRateError;
503 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000504 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700505
Peter Kasting69558702016-01-12 16:26:35 -0800506 const size_t num_in_channels = config.input_stream().num_channels();
507 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700508
509 // Need at least one input channel.
510 // Need either one output channel or as many outputs as there are inputs.
511 if (num_in_channels == 0 ||
512 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700513 return kBadNumberChannelsError;
514 }
515
aluebsb2328d12016-01-11 20:32:29 -0800516 if (capture_nonlocked_.beamformer_enabled &&
Peter Kasting69558702016-01-12 16:26:35 -0800517 num_in_channels != capture_.array_geometry.size()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700518 return kBadNumberChannelsError;
519 }
520
peahdf3efa82015-11-28 12:35:15 -0800521 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000522
peahde65ddc2016-09-16 15:02:15 -0700523 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700524 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700525 formats_.api_format.output_stream().sample_rate_hz()),
526 submodule_states_.CaptureMultiBandSubModulesActive() ||
527 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000528
peahde65ddc2016-09-16 15:02:15 -0700529 capture_nonlocked_.capture_processing_format =
530 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700531
peahde65ddc2016-09-16 15:02:15 -0700532 int render_processing_rate = FindNativeProcessRateToUse(
peah2ace3f92016-09-10 04:42:27 -0700533 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
534 formats_.api_format.reverse_output_stream().sample_rate_hz()),
535 submodule_states_.CaptureMultiBandSubModulesActive() ||
536 submodule_states_.RenderMultiBandSubModulesActive());
aluebseb3603b2016-04-20 15:27:58 -0700537 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
538 // splitting filter degrades the AEC performance.
peahde65ddc2016-09-16 15:02:15 -0700539 if (render_processing_rate > kSampleRate32kHz) {
540 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
541 ? kSampleRate32kHz
542 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700543 }
peahde65ddc2016-09-16 15:02:15 -0700544 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700545 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700546 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
547 kSampleRate8kHz) {
548 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000549 } else {
peahde65ddc2016-09-16 15:02:15 -0700550 render_processing_rate =
551 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000552 }
553
peahde65ddc2016-09-16 15:02:15 -0700554 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000555 // demonstrated to work well for AEC in most practical scenarios.
peahde65ddc2016-09-16 15:02:15 -0700556 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000557
peahde65ddc2016-09-16 15:02:15 -0700558 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
559 kSampleRate32kHz ||
560 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
561 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800562 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000563 } else {
peahdf3efa82015-11-28 12:35:15 -0800564 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700565 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000566 }
567
568 return InitializeLocked();
569}
570
peah88ac8532016-09-12 16:47:25 -0700571void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peahc19f3122016-10-07 14:54:10 -0700572 config_ = config;
peah88ac8532016-09-12 16:47:25 -0700573
peahc19f3122016-10-07 14:54:10 -0700574 bool config_ok = LevelController::Validate(config_.level_controller);
peah88ac8532016-09-12 16:47:25 -0700575 if (!config_ok) {
576 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
577 << "level_controller: "
peahc19f3122016-10-07 14:54:10 -0700578 << LevelController::ToString(config_.level_controller)
peah88ac8532016-09-12 16:47:25 -0700579 << std::endl
580 << "Reverting to default parameter set";
peahc19f3122016-10-07 14:54:10 -0700581 config_.level_controller = AudioProcessing::Config::LevelController();
peah88ac8532016-09-12 16:47:25 -0700582 }
583
584 // Run in a single-threaded manner when applying the settings.
585 rtc::CritScope cs_render(&crit_render_);
586 rtc::CritScope cs_capture(&crit_capture_);
587
peahc19f3122016-10-07 14:54:10 -0700588 // TODO(peah): Replace the use of capture_nonlocked_.level_controller_enabled
589 // with the value in config_ everywhere in the code.
590 if (capture_nonlocked_.level_controller_enabled !=
591 config_.level_controller.enabled) {
peah88ac8532016-09-12 16:47:25 -0700592 capture_nonlocked_.level_controller_enabled =
peahc19f3122016-10-07 14:54:10 -0700593 config_.level_controller.enabled;
594 // TODO(peah): Remove the conditional initialization to always initialize
595 // the level controller regardless of whether it is enabled or not.
596 InitializeLevelController();
peah88ac8532016-09-12 16:47:25 -0700597 }
peahc19f3122016-10-07 14:54:10 -0700598 LOG(LS_INFO) << "Level controller activated: "
599 << capture_nonlocked_.level_controller_enabled;
600
601 private_submodules_->level_controller->ApplyConfig(config_.level_controller);
peah88ac8532016-09-12 16:47:25 -0700602}
603
604void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800605 // Run in a single-threaded manner when setting the extra options.
606 rtc::CritScope cs_render(&crit_render_);
607 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000608
peahb624d8c2016-03-05 03:01:14 -0800609 public_submodules_->echo_cancellation->SetExtraOptions(config);
610
peahdf3efa82015-11-28 12:35:15 -0800611 if (capture_.transient_suppressor_enabled !=
612 config.Get<ExperimentalNs>().enabled) {
613 capture_.transient_suppressor_enabled =
614 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000615 InitializeTransient();
616 }
aluebs2a346882016-01-11 18:04:30 -0800617
peah1bcfce52016-08-26 07:16:04 -0700618#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700619 if(capture_nonlocked_.intelligibility_enabled !=
620 config.Get<Intelligibility>().enabled) {
621 capture_nonlocked_.intelligibility_enabled =
622 config.Get<Intelligibility>().enabled;
623 InitializeIntelligibility();
624 }
peah1bcfce52016-08-26 07:16:04 -0700625#endif
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700626
aluebs2a346882016-01-11 18:04:30 -0800627#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
aluebsb2328d12016-01-11 20:32:29 -0800628 if (capture_nonlocked_.beamformer_enabled !=
629 config.Get<Beamforming>().enabled) {
630 capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled;
aluebs2a346882016-01-11 18:04:30 -0800631 if (config.Get<Beamforming>().array_geometry.size() > 1) {
632 capture_.array_geometry = config.Get<Beamforming>().array_geometry;
633 }
634 capture_.target_direction = config.Get<Beamforming>().target_direction;
635 InitializeBeamformer();
636 }
637#endif // WEBRTC_ANDROID_PLATFORM_BUILD
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000638}
639
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000640int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800641 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700642 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000643}
644
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000645int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800646 // Used as callback from submodules, hence locking is not allowed.
647 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000648}
649
Peter Kasting69558702016-01-12 16:26:35 -0800650size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800651 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700652 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000653}
654
Peter Kasting69558702016-01-12 16:26:35 -0800655size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800656 // Used as callback from submodules, hence locking is not allowed.
657 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000658}
659
Peter Kasting69558702016-01-12 16:26:35 -0800660size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800661 // Used as callback from submodules, hence locking is not allowed.
662 return capture_nonlocked_.beamformer_enabled ? 1 : num_output_channels();
663}
664
Peter Kasting69558702016-01-12 16:26:35 -0800665size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800666 // Used as callback from submodules, hence locking is not allowed.
667 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000668}
669
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000670void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800671 rtc::CritScope cs(&crit_capture_);
672 capture_.output_will_be_muted = muted;
673 if (private_submodules_->agc_manager.get()) {
674 private_submodules_->agc_manager->SetCaptureMuted(
675 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000676 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000677}
678
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000679
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000680int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700681 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000682 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000683 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000684 int output_sample_rate_hz,
685 ChannelLayout output_layout,
686 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800687 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800688 StreamConfig input_stream;
689 StreamConfig output_stream;
690 {
691 // Access the formats_.api_format.input_stream beneath the capture lock.
692 // The lock must be released as it is later required in the call
693 // to ProcessStream(,,,);
694 rtc::CritScope cs(&crit_capture_);
695 input_stream = formats_.api_format.input_stream();
696 output_stream = formats_.api_format.output_stream();
697 }
698
Michael Graczyk86c6d332015-07-23 11:41:39 -0700699 input_stream.set_sample_rate_hz(input_sample_rate_hz);
700 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
701 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700702 output_stream.set_sample_rate_hz(output_sample_rate_hz);
703 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
704 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
705
706 if (samples_per_channel != input_stream.num_frames()) {
707 return kBadDataLengthError;
708 }
709 return ProcessStream(src, input_stream, output_stream, dest);
710}
711
712int AudioProcessingImpl::ProcessStream(const float* const* src,
713 const StreamConfig& input_config,
714 const StreamConfig& output_config,
715 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800716 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800717 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700718 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800719 {
720 // Acquire the capture lock in order to safely call the function
721 // that retrieves the render side data. This function accesses apm
722 // getters that need the capture lock held when being called.
723 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700724 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800725
726 if (!src || !dest) {
727 return kNullPointerError;
728 }
729
730 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700731 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000732 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000733
Michael Graczyk86c6d332015-07-23 11:41:39 -0700734 processing_config.input_stream() = input_config;
735 processing_config.output_stream() = output_config;
736
peahdf3efa82015-11-28 12:35:15 -0800737 {
738 // Do conditional reinitialization.
739 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -0700740 RETURN_ON_ERR(
741 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -0800742 }
743 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700744 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
745 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000746
747#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700748 if (debug_dump_.debug_file->is_open()) {
Minyue13b96ba2015-10-03 00:39:14 +0200749 RETURN_ON_ERR(WriteConfigMessage(false));
750
peahdf3efa82015-11-28 12:35:15 -0800751 debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM);
752 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000753 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -0800754 sizeof(float) * formats_.api_format.input_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -0800755 for (size_t i = 0; i < formats_.api_format.input_stream().num_channels();
756 ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000757 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000758 }
759#endif
760
peahdf3efa82015-11-28 12:35:15 -0800761 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700762 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800763 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000764
765#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700766 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -0800767 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000768 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -0800769 sizeof(float) * formats_.api_format.output_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -0800770 for (size_t i = 0; i < formats_.api_format.output_stream().num_channels();
771 ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000772 msg->add_output_channel(dest[i], channel_size);
peahdf3efa82015-11-28 12:35:15 -0800773 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -0800774 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -0800775 &crit_debug_, &debug_dump_.capture));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000776 }
777#endif
778
779 return kNoError;
780}
781
peah701d6282016-10-25 05:42:20 -0700782void AudioProcessingImpl::QueueRenderAudio(AudioBuffer* audio) {
peah764e3642016-10-22 05:04:30 -0700783 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
784 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700785 &aec_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700786
787 RTC_DCHECK_GE(160u, audio->num_frames_per_band());
788
789 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700790 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700791 // The data queue is full and needs to be emptied.
792 EmptyQueuedRenderAudio();
793
794 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700795 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700796 RTC_DCHECK(result);
797 }
798
799 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
800 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700801 &aecm_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700802
803 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700804 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700805 // The data queue is full and needs to be emptied.
806 EmptyQueuedRenderAudio();
807
808 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700809 bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700810 RTC_DCHECK(result);
811 }
peah701d6282016-10-25 05:42:20 -0700812
813 if (!constants_.use_experimental_agc) {
814 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
815 // Insert the samples into the queue.
816 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
817 // The data queue is full and needs to be emptied.
818 EmptyQueuedRenderAudio();
819
820 // Retry the insert (should always work).
821 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
822 RTC_DCHECK(result);
823 }
824 }
ivoc9f4a4a02016-10-28 05:39:16 -0700825
826 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
827
828 // Insert the samples into the queue.
829 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
830 // The data queue is full and needs to be emptied.
831 EmptyQueuedRenderAudio();
832
833 // Retry the insert (should always work).
834 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
835 RTC_DCHECK(result);
836 }
peah764e3642016-10-22 05:04:30 -0700837}
838
839void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -0700840 const size_t new_aec_render_queue_element_max_size =
peah764e3642016-10-22 05:04:30 -0700841 std::max(static_cast<size_t>(1),
842 kMaxAllowedValuesOfSamplesPerFrame *
843 EchoCancellationImpl::NumCancellersRequired(
844 num_output_channels(), num_reverse_channels()));
845
peah701d6282016-10-25 05:42:20 -0700846 const size_t new_aecm_render_queue_element_max_size =
peaha0624602016-10-25 04:45:24 -0700847 std::max(static_cast<size_t>(1),
848 kMaxAllowedValuesOfSamplesPerFrame *
849 EchoControlMobileImpl::NumCancellersRequired(
850 num_output_channels(), num_reverse_channels()));
peah764e3642016-10-22 05:04:30 -0700851
peah701d6282016-10-25 05:42:20 -0700852 const size_t new_agc_render_queue_element_max_size =
853 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
854
ivoc9f4a4a02016-10-28 05:39:16 -0700855 const size_t new_red_render_queue_element_max_size =
856 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
857
peaha0624602016-10-25 04:45:24 -0700858 // Reallocate the queues if the queue item sizes are too small to fit the
859 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -0700860 if (aec_render_queue_element_max_size_ <
861 new_aec_render_queue_element_max_size) {
862 aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
peah764e3642016-10-22 05:04:30 -0700863
peaha0624602016-10-25 04:45:24 -0700864 std::vector<float> template_queue_element(
peah701d6282016-10-25 05:42:20 -0700865 aec_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700866
peah701d6282016-10-25 05:42:20 -0700867 aec_render_signal_queue_.reset(
peah764e3642016-10-22 05:04:30 -0700868 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
869 kMaxNumFramesToBuffer, template_queue_element,
peaha0624602016-10-25 04:45:24 -0700870 RenderQueueItemVerifier<float>(
peah701d6282016-10-25 05:42:20 -0700871 aec_render_queue_element_max_size_)));
peah764e3642016-10-22 05:04:30 -0700872
peah701d6282016-10-25 05:42:20 -0700873 aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
874 aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
peah764e3642016-10-22 05:04:30 -0700875 } else {
peah701d6282016-10-25 05:42:20 -0700876 aec_render_signal_queue_->Clear();
peaha0624602016-10-25 04:45:24 -0700877 }
878
peah701d6282016-10-25 05:42:20 -0700879 if (aecm_render_queue_element_max_size_ <
880 new_aecm_render_queue_element_max_size) {
881 aecm_render_queue_element_max_size_ =
882 new_aecm_render_queue_element_max_size;
peaha0624602016-10-25 04:45:24 -0700883
884 std::vector<int16_t> template_queue_element(
peah701d6282016-10-25 05:42:20 -0700885 aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700886
peah701d6282016-10-25 05:42:20 -0700887 aecm_render_signal_queue_.reset(
peaha0624602016-10-25 04:45:24 -0700888 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
889 kMaxNumFramesToBuffer, template_queue_element,
890 RenderQueueItemVerifier<int16_t>(
peah701d6282016-10-25 05:42:20 -0700891 aecm_render_queue_element_max_size_)));
peaha0624602016-10-25 04:45:24 -0700892
peah701d6282016-10-25 05:42:20 -0700893 aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
894 aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700895 } else {
peah701d6282016-10-25 05:42:20 -0700896 aecm_render_signal_queue_->Clear();
897 }
898
899 if (agc_render_queue_element_max_size_ <
900 new_agc_render_queue_element_max_size) {
901 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
902
903 std::vector<int16_t> template_queue_element(
904 agc_render_queue_element_max_size_);
905
906 agc_render_signal_queue_.reset(
907 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
908 kMaxNumFramesToBuffer, template_queue_element,
909 RenderQueueItemVerifier<int16_t>(
910 agc_render_queue_element_max_size_)));
911
912 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
913 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
914 } else {
915 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -0700916 }
ivoc9f4a4a02016-10-28 05:39:16 -0700917
918 if (red_render_queue_element_max_size_ <
919 new_red_render_queue_element_max_size) {
920 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
921
922 std::vector<float> template_queue_element(
923 red_render_queue_element_max_size_);
924
925 red_render_signal_queue_.reset(
926 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
927 kMaxNumFramesToBuffer, template_queue_element,
928 RenderQueueItemVerifier<float>(
929 red_render_queue_element_max_size_)));
930
931 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
932 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
933 } else {
934 red_render_signal_queue_->Clear();
935 }
peah764e3642016-10-22 05:04:30 -0700936}
937
938void AudioProcessingImpl::EmptyQueuedRenderAudio() {
939 rtc::CritScope cs_capture(&crit_capture_);
peah701d6282016-10-25 05:42:20 -0700940 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700941 public_submodules_->echo_cancellation->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -0700942 aec_capture_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700943 }
944
peah701d6282016-10-25 05:42:20 -0700945 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700946 public_submodules_->echo_control_mobile->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -0700947 aecm_capture_queue_buffer_);
948 }
949
950 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
951 public_submodules_->gain_control->ProcessRenderAudio(
952 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700953 }
ivoc9f4a4a02016-10-28 05:39:16 -0700954
955 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
956 private_submodules_->residual_echo_detector->AnalyzeRenderAudio(
957 red_capture_queue_buffer_);
958 }
peah764e3642016-10-22 05:04:30 -0700959}
960
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000961int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -0800962 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -0800963 {
964 // Acquire the capture lock in order to safely call the function
965 // that retrieves the render side data. This function accesses apm
966 // getters that need the capture lock held when being called.
967 // The lock needs to be released as
968 // public_submodules_->echo_control_mobile->is_enabled() aquires this lock
969 // as well.
970 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700971 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800972 }
peahfa6228e2015-11-16 16:27:42 -0800973
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000974 if (!frame) {
975 return kNullPointerError;
976 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000977 // Must be a native rate.
978 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
979 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000980 frame->sample_rate_hz_ != kSampleRate32kHz &&
981 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000982 return kBadSampleRateError;
983 }
peah192164e2015-11-17 02:16:45 -0800984
peahdf3efa82015-11-28 12:35:15 -0800985 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700986 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800987 {
988 // Aquire lock for the access of api_format.
989 // The lock is released immediately due to the conditional
990 // reinitialization.
991 rtc::CritScope cs_capture(&crit_capture_);
992 // TODO(ajm): The input and output rates and channels are currently
993 // constrained to be identical in the int16 interface.
994 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700995
996 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -0800997 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700998 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
999 processing_config.input_stream().set_num_channels(frame->num_channels_);
1000 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
1001 processing_config.output_stream().set_num_channels(frame->num_channels_);
1002
peahdf3efa82015-11-28 12:35:15 -08001003 {
1004 // Do conditional reinitialization.
1005 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -07001006 RETURN_ON_ERR(
1007 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -08001008 }
1009 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001010 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001011 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001012 return kBadDataLengthError;
1013 }
1014
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001015#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001016 if (debug_dump_.debug_file->is_open()) {
peah644fa962016-08-18 06:48:33 -07001017 RETURN_ON_ERR(WriteConfigMessage(false));
1018
peahdf3efa82015-11-28 12:35:15 -08001019 debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM);
1020 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001021 const size_t data_size =
1022 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001023 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001024 }
1025#endif
1026
peahdf3efa82015-11-28 12:35:15 -08001027 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001028 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001029 capture_.capture_audio->InterleaveTo(
1030 frame, submodule_states_.CaptureMultiBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001031
1032#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001033 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001034 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001035 const size_t data_size =
1036 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001037 msg->set_output_data(frame->data_, data_size);
peahdf3efa82015-11-28 12:35:15 -08001038 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001039 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001040 &crit_debug_, &debug_dump_.capture));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001041 }
1042#endif
1043
1044 return kNoError;
1045}
1046
peahde65ddc2016-09-16 15:02:15 -07001047int AudioProcessingImpl::ProcessCaptureStreamLocked() {
peahb58a1582016-03-15 09:34:24 -07001048 // Ensure that not both the AEC and AECM are active at the same time.
1049 // TODO(peah): Simplify once the public API Enable functions for these
1050 // are moved to APM.
1051 RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&
1052 public_submodules_->echo_control_mobile->is_enabled()));
1053
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001054#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001055 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001056 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
1057 msg->set_delay(capture_nonlocked_.stream_delay_ms);
1058 msg->set_drift(
1059 public_submodules_->echo_cancellation->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +00001060 msg->set_level(gain_control()->stream_analog_level());
peahdf3efa82015-11-28 12:35:15 -08001061 msg->set_keypress(capture_.key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +00001062 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001063#endif
niklase@google.com470e71d2011-07-07 08:21:25 +00001064
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001065 MaybeUpdateHistograms();
1066
peahde65ddc2016-09-16 15:02:15 -07001067 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001068
peahbe615622016-02-13 16:40:47 -08001069 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001070 public_submodules_->gain_control->is_enabled()) {
1071 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001072 capture_buffer->channels()[0], capture_buffer->num_channels(),
1073 capture_nonlocked_.capture_processing_format.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001074 }
1075
peah2ace3f92016-09-10 04:42:27 -07001076 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1077 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001078 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1079 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001080 }
1081
aluebsb2328d12016-01-11 20:32:29 -08001082 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001083 private_submodules_->beamformer->AnalyzeChunk(
1084 *capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001085 // Discards all channels by the leftmost one.
peahde65ddc2016-09-16 15:02:15 -07001086 capture_buffer->set_num_channels(1);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001087 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001088
peahde65ddc2016-09-16 15:02:15 -07001089 public_submodules_->high_pass_filter->ProcessCaptureAudio(capture_buffer);
1090 RETURN_ON_ERR(
1091 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1092 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001093
1094 // Ensure that the stream delay was set before the call to the
1095 // AEC ProcessCaptureAudio function.
1096 if (public_submodules_->echo_cancellation->is_enabled() &&
1097 !was_stream_delay_set()) {
1098 return AudioProcessing::kStreamParameterNotSetError;
1099 }
1100
1101 RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001102 capture_buffer, stream_delay_ms()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001103
peahdf3efa82015-11-28 12:35:15 -08001104 if (public_submodules_->echo_control_mobile->is_enabled() &&
1105 public_submodules_->noise_suppression->is_enabled()) {
peahde65ddc2016-09-16 15:02:15 -07001106 capture_buffer->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +00001107 }
peahde65ddc2016-09-16 15:02:15 -07001108 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah1bcfce52016-08-26 07:16:04 -07001109#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001110 if (capture_nonlocked_.intelligibility_enabled) {
aluebsc466bad2016-02-10 12:03:00 -08001111 RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001112 int gain_db = public_submodules_->gain_control->is_enabled() ?
1113 public_submodules_->gain_control->compression_gain_db() :
1114 0;
Alejandro Luebs50411102016-06-30 15:35:41 -07001115 float gain = std::pow(10.f, gain_db / 20.f);
1116 gain *= capture_nonlocked_.level_controller_enabled ?
1117 private_submodules_->level_controller->GetLastGain() :
1118 1.f;
aluebsc466bad2016-02-10 12:03:00 -08001119 public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -07001120 public_submodules_->noise_suppression->NoiseEstimate(), gain);
aluebsc466bad2016-02-10 12:03:00 -08001121 }
peah1bcfce52016-08-26 07:16:04 -07001122#endif
peah253534d2016-03-15 04:32:28 -07001123
1124 // Ensure that the stream delay was set before the call to the
1125 // AECM ProcessCaptureAudio function.
1126 if (public_submodules_->echo_control_mobile->is_enabled() &&
1127 !was_stream_delay_set()) {
1128 return AudioProcessing::kStreamParameterNotSetError;
1129 }
1130
1131 RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001132 capture_buffer, stream_delay_ms()));
peah253534d2016-03-15 04:32:28 -07001133
ivoc9f4a4a02016-10-28 05:39:16 -07001134 if (config_.residual_echo_detector.enabled) {
1135 private_submodules_->residual_echo_detector->AnalyzeCaptureAudio(
1136 rtc::ArrayView<const float>(
1137 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1138 capture_buffer->num_frames_per_band()));
1139 }
1140
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001141 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001142 private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001143 }
1144
peahde65ddc2016-09-16 15:02:15 -07001145 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001146
peahbe615622016-02-13 16:40:47 -08001147 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001148 public_submodules_->gain_control->is_enabled() &&
aluebsb2328d12016-01-11 20:32:29 -08001149 (!capture_nonlocked_.beamformer_enabled ||
peahdf3efa82015-11-28 12:35:15 -08001150 private_submodules_->beamformer->is_target_present())) {
1151 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001152 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1153 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001154 }
peahb8fbb542016-03-15 02:28:08 -07001155 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001156 capture_buffer, echo_cancellation()->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001157
peah2ace3f92016-09-10 04:42:27 -07001158 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1159 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001160 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1161 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001162 }
1163
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001164 // TODO(aluebs): Investigate if the transient suppression placement should be
1165 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001166 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001167 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001168 private_submodules_->agc_manager.get()
1169 ? private_submodules_->agc_manager->voice_probability()
1170 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001171
peahdf3efa82015-11-28 12:35:15 -08001172 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001173 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1174 capture_buffer->num_channels(),
1175 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1176 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1177 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001178 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001179 }
1180
peahca4cac72016-06-29 15:26:12 -07001181 if (capture_nonlocked_.level_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001182 private_submodules_->level_controller->Process(capture_buffer);
peahca4cac72016-06-29 15:26:12 -07001183 }
1184
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001185 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001186 public_submodules_->level_estimator->ProcessStream(capture_buffer);
ajm@google.com808e0e02011-08-03 21:08:51 +00001187
peahdf3efa82015-11-28 12:35:15 -08001188 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 return kNoError;
1190}
1191
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001192int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001193 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001194 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001195 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001196 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001197 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001198 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001199 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001200 };
1201 if (samples_per_channel != reverse_config.num_frames()) {
1202 return kBadDataLengthError;
1203 }
peahdf3efa82015-11-28 12:35:15 -08001204 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001205}
1206
peahde65ddc2016-09-16 15:02:15 -07001207int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1208 const StreamConfig& input_config,
1209 const StreamConfig& output_config,
1210 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001211 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001212 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001213 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
peah2ace3f92016-09-10 04:42:27 -07001214 if (submodule_states_.RenderMultiBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001215 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1216 dest);
peah2ace3f92016-09-10 04:42:27 -07001217 } else if (formats_.api_format.reverse_input_stream() !=
1218 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001219 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1220 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001221 } else {
peahde65ddc2016-09-16 15:02:15 -07001222 CopyAudioIfNeeded(src, input_config.num_frames(),
1223 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001224 }
1225
1226 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001227}
1228
peahdf3efa82015-11-28 12:35:15 -08001229int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001230 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001231 const StreamConfig& input_config,
1232 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001233 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001234 return kNullPointerError;
1235 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001236
peahde65ddc2016-09-16 15:02:15 -07001237 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001238 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001239 }
1240
peahdf3efa82015-11-28 12:35:15 -08001241 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001242 processing_config.reverse_input_stream() = input_config;
1243 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001244
peahdf3efa82015-11-28 12:35:15 -08001245 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
peahde65ddc2016-09-16 15:02:15 -07001246 assert(input_config.num_frames() ==
1247 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001248
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001249#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001250 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001251 debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM);
1252 audioproc::ReverseStream* msg =
1253 debug_dump_.render.event_msg->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +00001254 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -08001255 sizeof(float) * formats_.api_format.reverse_input_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -08001256 for (size_t i = 0;
peahdf3efa82015-11-28 12:35:15 -08001257 i < formats_.api_format.reverse_input_stream().num_channels(); ++i)
ekmeyerson60d9b332015-08-14 10:35:55 -07001258 msg->add_channel(src[i], channel_size);
peahdf3efa82015-11-28 12:35:15 -08001259 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001260 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001261 &crit_debug_, &debug_dump_.render));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001262 }
1263#endif
1264
peahdf3efa82015-11-28 12:35:15 -08001265 render_.render_audio->CopyFrom(src,
1266 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001267 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001268}
1269
1270int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001271 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001272 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001273 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001274 return kNullPointerError;
1275 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001276 // Must be a native rate.
1277 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1278 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001279 frame->sample_rate_hz_ != kSampleRate32kHz &&
1280 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001281 return kBadSampleRateError;
1282 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001283
Michael Graczyk86c6d332015-07-23 11:41:39 -07001284 if (frame->num_channels_ <= 0) {
1285 return kBadNumberChannelsError;
1286 }
1287
peahdf3efa82015-11-28 12:35:15 -08001288 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001289 processing_config.reverse_input_stream().set_sample_rate_hz(
1290 frame->sample_rate_hz_);
1291 processing_config.reverse_input_stream().set_num_channels(
1292 frame->num_channels_);
1293 processing_config.reverse_output_stream().set_sample_rate_hz(
1294 frame->sample_rate_hz_);
1295 processing_config.reverse_output_stream().set_num_channels(
1296 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001297
peahdf3efa82015-11-28 12:35:15 -08001298 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001299 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001300 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001301 return kBadDataLengthError;
1302 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001303
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001304#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001305 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001306 debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM);
1307 audioproc::ReverseStream* msg =
1308 debug_dump_.render.event_msg->mutable_reverse_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001309 const size_t data_size =
1310 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001311 msg->set_data(frame->data_, data_size);
peahdf3efa82015-11-28 12:35:15 -08001312 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001313 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001314 &crit_debug_, &debug_dump_.render));
niklase@google.com470e71d2011-07-07 08:21:25 +00001315 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001316#endif
peahdf3efa82015-11-28 12:35:15 -08001317 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001318 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001319 render_.render_audio->InterleaveTo(
1320 frame, submodule_states_.RenderMultiBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001321 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001322}
niklase@google.com470e71d2011-07-07 08:21:25 +00001323
peahde65ddc2016-09-16 15:02:15 -07001324int AudioProcessingImpl::ProcessRenderStreamLocked() {
1325 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah2ace3f92016-09-10 04:42:27 -07001326 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001327 SampleRateSupportsMultiBand(
1328 formats_.render_processing_format.sample_rate_hz())) {
1329 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001330 }
1331
peah1bcfce52016-08-26 07:16:04 -07001332#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001333 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001334 public_submodules_->intelligibility_enhancer->ProcessRenderAudio(
Alejandro Luebsef009252016-09-20 14:51:56 -07001335 render_buffer);
ekmeyerson60d9b332015-08-14 10:35:55 -07001336 }
peah1bcfce52016-08-26 07:16:04 -07001337#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001338
peah764e3642016-10-22 05:04:30 -07001339 QueueRenderAudio(render_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +00001340
peah2ace3f92016-09-10 04:42:27 -07001341 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001342 SampleRateSupportsMultiBand(
1343 formats_.render_processing_format.sample_rate_hz())) {
1344 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001345 }
1346
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001347 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001348}
1349
1350int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001351 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001352 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001353 capture_.was_stream_delay_set = true;
1354 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001355
niklase@google.com470e71d2011-07-07 08:21:25 +00001356 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001357 delay = 0;
1358 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001359 }
1360
1361 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1362 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001363 delay = 500;
1364 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001365 }
1366
peahdf3efa82015-11-28 12:35:15 -08001367 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001368 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001369}
1370
1371int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001372 // Used as callback from submodules, hence locking is not allowed.
1373 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001374}
1375
1376bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001377 // Used as callback from submodules, hence locking is not allowed.
1378 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001379}
1380
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001381void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001382 rtc::CritScope cs(&crit_capture_);
1383 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001384}
1385
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001386void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001387 rtc::CritScope cs(&crit_capture_);
1388 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001389}
1390
1391int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001392 rtc::CritScope cs(&crit_capture_);
1393 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001394}
1395
niklase@google.com470e71d2011-07-07 08:21:25 +00001396int AudioProcessingImpl::StartDebugRecording(
ivocd66b44d2016-01-15 03:06:36 -08001397 const char filename[AudioProcessing::kMaxFilenameSize],
1398 int64_t max_log_size_bytes) {
peahdf3efa82015-11-28 12:35:15 -08001399 // Run in a single-threaded manner.
1400 rtc::CritScope cs_render(&crit_render_);
1401 rtc::CritScope cs_capture(&crit_capture_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +02001402 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +00001403
peahdf3efa82015-11-28 12:35:15 -08001404 if (filename == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001405 return kNullPointerError;
1406 }
1407
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001408#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ivocd66b44d2016-01-15 03:06:36 -08001409 debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001410 // Stop any ongoing recording.
tommia6219cc2016-06-15 10:30:14 -07001411 debug_dump_.debug_file->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +00001412
tommia6219cc2016-06-15 10:30:14 -07001413 if (!debug_dump_.debug_file->OpenFile(filename, false)) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001414 return kFileError;
1415 }
1416
Minyue13b96ba2015-10-03 00:39:14 +02001417 RETURN_ON_ERR(WriteConfigMessage(true));
1418 RETURN_ON_ERR(WriteInitMessage());
niklase@google.com470e71d2011-07-07 08:21:25 +00001419 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001420#else
1421 return kUnsupportedFunctionError;
1422#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001423}
1424
ivocd66b44d2016-01-15 03:06:36 -08001425int AudioProcessingImpl::StartDebugRecording(FILE* handle,
1426 int64_t max_log_size_bytes) {
peahdf3efa82015-11-28 12:35:15 -08001427 // Run in a single-threaded manner.
1428 rtc::CritScope cs_render(&crit_render_);
1429 rtc::CritScope cs_capture(&crit_capture_);
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001430
peahdf3efa82015-11-28 12:35:15 -08001431 if (handle == nullptr) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001432 return kNullPointerError;
1433 }
1434
1435#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ivocd66b44d2016-01-15 03:06:36 -08001436 debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes;
1437
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001438 // Stop any ongoing recording.
tommia6219cc2016-06-15 10:30:14 -07001439 debug_dump_.debug_file->CloseFile();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001440
tommia6219cc2016-06-15 10:30:14 -07001441 if (!debug_dump_.debug_file->OpenFromFileHandle(handle)) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001442 return kFileError;
1443 }
1444
Minyue13b96ba2015-10-03 00:39:14 +02001445 RETURN_ON_ERR(WriteConfigMessage(true));
1446 RETURN_ON_ERR(WriteInitMessage());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001447 return kNoError;
1448#else
1449 return kUnsupportedFunctionError;
1450#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1451}
1452
peah73a28ee2016-10-12 03:01:49 -07001453int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
1454 return StartDebugRecording(handle, -1);
1455}
1456
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001457int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
1458 rtc::PlatformFile handle) {
peahdf3efa82015-11-28 12:35:15 -08001459 // Run in a single-threaded manner.
1460 rtc::CritScope cs_render(&crit_render_);
1461 rtc::CritScope cs_capture(&crit_capture_);
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001462 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
ivocd66b44d2016-01-15 03:06:36 -08001463 return StartDebugRecording(stream, -1);
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001464}
1465
niklase@google.com470e71d2011-07-07 08:21:25 +00001466int AudioProcessingImpl::StopDebugRecording() {
peahdf3efa82015-11-28 12:35:15 -08001467 // Run in a single-threaded manner.
1468 rtc::CritScope cs_render(&crit_render_);
1469 rtc::CritScope cs_capture(&crit_capture_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001470
1471#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001472 // We just return if recording hasn't started.
tommia6219cc2016-06-15 10:30:14 -07001473 debug_dump_.debug_file->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +00001474 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001475#else
1476 return kUnsupportedFunctionError;
1477#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001478}
1479
ivoc3e9a5372016-10-28 07:55:33 -07001480// TODO(ivoc): Remove this when GetStatistics() becomes pure virtual.
1481AudioProcessing::AudioProcessingStatistics AudioProcessing::GetStatistics()
1482 const {
1483 return AudioProcessingStatistics();
1484}
1485
1486AudioProcessing::AudioProcessingStatistics AudioProcessingImpl::GetStatistics()
1487 const {
1488 AudioProcessingStatistics stats;
1489 EchoCancellation::Metrics metrics;
1490 public_submodules_->echo_cancellation->GetMetrics(&metrics);
1491 stats.a_nlp.Set(metrics.a_nlp);
1492 stats.divergent_filter_fraction = metrics.divergent_filter_fraction;
1493 stats.echo_return_loss.Set(metrics.echo_return_loss);
1494 stats.echo_return_loss_enhancement.Set(metrics.echo_return_loss_enhancement);
1495 stats.residual_echo_return_loss.Set(metrics.residual_echo_return_loss);
1496 public_submodules_->echo_cancellation->GetDelayMetrics(
1497 &stats.delay_median, &stats.delay_standard_deviation,
1498 &stats.fraction_poor_delays);
1499 return stats;
1500}
1501
niklase@google.com470e71d2011-07-07 08:21:25 +00001502EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
peahb624d8c2016-03-05 03:01:14 -08001503 return public_submodules_->echo_cancellation.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001504}
1505
1506EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
peahbb9edbd2016-03-10 12:54:25 -08001507 return public_submodules_->echo_control_mobile.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001508}
1509
1510GainControl* AudioProcessingImpl::gain_control() const {
peahbe615622016-02-13 16:40:47 -08001511 if (constants_.use_experimental_agc) {
1512 return public_submodules_->gain_control_for_experimental_agc.get();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001513 }
peahbfa97112016-03-10 21:09:04 -08001514 return public_submodules_->gain_control.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001515}
1516
1517HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
solenberg70f99032015-12-08 11:07:32 -08001518 return public_submodules_->high_pass_filter.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001519}
1520
1521LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001522 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001523}
1524
1525NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
solenberg5e465c32015-12-08 13:22:33 -08001526 return public_submodules_->noise_suppression.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001527}
1528
1529VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001530 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001531}
1532
peah2ace3f92016-09-10 04:42:27 -07001533bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1534 return submodule_states_.Update(
1535 public_submodules_->high_pass_filter->is_enabled(),
1536 public_submodules_->echo_cancellation->is_enabled(),
1537 public_submodules_->echo_control_mobile->is_enabled(),
ivoc9f4a4a02016-10-28 05:39:16 -07001538 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001539 public_submodules_->noise_suppression->is_enabled(),
1540 capture_nonlocked_.intelligibility_enabled,
1541 capture_nonlocked_.beamformer_enabled,
1542 public_submodules_->gain_control->is_enabled(),
1543 capture_nonlocked_.level_controller_enabled,
1544 public_submodules_->voice_detection->is_enabled(),
1545 public_submodules_->level_estimator->is_enabled(),
1546 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001547}
1548
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001549
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001550void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001551 if (capture_.transient_suppressor_enabled) {
1552 if (!public_submodules_->transient_suppressor.get()) {
1553 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001554 }
peahdf3efa82015-11-28 12:35:15 -08001555 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001556 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1557 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001558 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001559}
1560
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001561void AudioProcessingImpl::InitializeBeamformer() {
aluebsb2328d12016-01-11 20:32:29 -08001562 if (capture_nonlocked_.beamformer_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001563 if (!private_submodules_->beamformer) {
1564 private_submodules_->beamformer.reset(new NonlinearBeamformer(
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001565 capture_.array_geometry, 1u, capture_.target_direction));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001566 }
peahdf3efa82015-11-28 12:35:15 -08001567 private_submodules_->beamformer->Initialize(kChunkSizeMs,
1568 capture_nonlocked_.split_rate);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001569 }
1570}
1571
ekmeyerson60d9b332015-08-14 10:35:55 -07001572void AudioProcessingImpl::InitializeIntelligibility() {
peah1bcfce52016-08-26 07:16:04 -07001573#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001574 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001575 public_submodules_->intelligibility_enhancer.reset(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -08001576 new IntelligibilityEnhancer(capture_nonlocked_.split_rate,
Alex Luebs57ae8292016-03-09 16:24:34 +01001577 render_.render_audio->num_channels(),
Alejandro Luebsef009252016-09-20 14:51:56 -07001578 render_.render_audio->num_bands(),
Alex Luebs57ae8292016-03-09 16:24:34 +01001579 NoiseSuppressionImpl::num_noise_bins()));
ekmeyerson60d9b332015-08-14 10:35:55 -07001580 }
peah1bcfce52016-08-26 07:16:04 -07001581#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001582}
1583
peahca4cac72016-06-29 15:26:12 -07001584void AudioProcessingImpl::InitializeLevelController() {
1585 private_submodules_->level_controller->Initialize(proc_sample_rate_hz());
1586}
1587
ivoc9f4a4a02016-10-28 05:39:16 -07001588void AudioProcessingImpl::InitializeResidualEchoDetector() {
1589 private_submodules_->residual_echo_detector->Initialize();
1590}
1591
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001592void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001593 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001594
1595 if (echo_cancellation()->is_enabled()) {
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001596 // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
1597 // If a stream has echo we know that the echo_cancellation is in process.
peahdf3efa82015-11-28 12:35:15 -08001598 if (capture_.stream_delay_jumps == -1 &&
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001599 echo_cancellation()->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001600 capture_.stream_delay_jumps = 0;
1601 }
1602 if (capture_.aec_system_delay_jumps == -1 &&
1603 echo_cancellation()->stream_has_echo()) {
1604 capture_.aec_system_delay_jumps = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001605 }
1606
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001607 // Detect a jump in platform reported system delay and log the difference.
peahdf3efa82015-11-28 12:35:15 -08001608 const int diff_stream_delay_ms =
1609 capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
1610 if (diff_stream_delay_ms > kMinDiffDelayMs &&
1611 capture_.last_stream_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001612 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1613 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
peahdf3efa82015-11-28 12:35:15 -08001614 if (capture_.stream_delay_jumps == -1) {
1615 capture_.stream_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001616 }
peahdf3efa82015-11-28 12:35:15 -08001617 capture_.stream_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001618 }
peahdf3efa82015-11-28 12:35:15 -08001619 capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001620
1621 // Detect a jump in AEC system delay and log the difference.
peah20028c42016-03-04 11:50:54 -08001622 const int samples_per_ms =
peahdf3efa82015-11-28 12:35:15 -08001623 rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
peah20028c42016-03-04 11:50:54 -08001624 RTC_DCHECK_LT(0, samples_per_ms);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001625 const int aec_system_delay_ms =
peah20028c42016-03-04 11:50:54 -08001626 public_submodules_->echo_cancellation->GetSystemDelayInSamples() /
1627 samples_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001628 const int diff_aec_system_delay_ms =
peahdf3efa82015-11-28 12:35:15 -08001629 aec_system_delay_ms - capture_.last_aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001630 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
peahdf3efa82015-11-28 12:35:15 -08001631 capture_.last_aec_system_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001632 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1633 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1634 100);
peahdf3efa82015-11-28 12:35:15 -08001635 if (capture_.aec_system_delay_jumps == -1) {
1636 capture_.aec_system_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001637 }
peahdf3efa82015-11-28 12:35:15 -08001638 capture_.aec_system_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001639 }
peahdf3efa82015-11-28 12:35:15 -08001640 capture_.last_aec_system_delay_ms = aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001641 }
1642}
1643
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001644void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
peahdf3efa82015-11-28 12:35:15 -08001645 // Run in a single-threaded manner.
1646 rtc::CritScope cs_render(&crit_render_);
1647 rtc::CritScope cs_capture(&crit_capture_);
1648
1649 if (capture_.stream_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001650 RTC_HISTOGRAM_ENUMERATION(
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001651 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
peahdf3efa82015-11-28 12:35:15 -08001652 capture_.stream_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001653 }
peahdf3efa82015-11-28 12:35:15 -08001654 capture_.stream_delay_jumps = -1;
1655 capture_.last_stream_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001656
peahdf3efa82015-11-28 12:35:15 -08001657 if (capture_.aec_system_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001658 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1659 capture_.aec_system_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001660 }
peahdf3efa82015-11-28 12:35:15 -08001661 capture_.aec_system_delay_jumps = -1;
1662 capture_.last_aec_system_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001663}
1664
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001665#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
peahdf3efa82015-11-28 12:35:15 -08001666int AudioProcessingImpl::WriteMessageToDebugFile(
1667 FileWrapper* debug_file,
ivocd66b44d2016-01-15 03:06:36 -08001668 int64_t* filesize_limit_bytes,
peahdf3efa82015-11-28 12:35:15 -08001669 rtc::CriticalSection* crit_debug,
1670 ApmDebugDumpThreadState* debug_state) {
1671 int32_t size = debug_state->event_msg->ByteSize();
ajm@google.com808e0e02011-08-03 21:08:51 +00001672 if (size <= 0) {
1673 return kUnspecifiedError;
1674 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001675#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Michael Graczyk86c6d332015-07-23 11:41:39 -07001676// TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1677// pretty safe in assuming little-endian.
ajm@google.com808e0e02011-08-03 21:08:51 +00001678#endif
1679
peahdf3efa82015-11-28 12:35:15 -08001680 if (!debug_state->event_msg->SerializeToString(&debug_state->event_str)) {
ajm@google.com808e0e02011-08-03 21:08:51 +00001681 return kUnspecifiedError;
1682 }
1683
peahdf3efa82015-11-28 12:35:15 -08001684 {
1685 // Ensure atomic writes of the message.
ivocd66b44d2016-01-15 03:06:36 -08001686 rtc::CritScope cs_debug(crit_debug);
1687
tommia6219cc2016-06-15 10:30:14 -07001688 RTC_DCHECK(debug_file->is_open());
ivocd66b44d2016-01-15 03:06:36 -08001689 // Update the byte counter.
1690 if (*filesize_limit_bytes >= 0) {
1691 *filesize_limit_bytes -=
1692 (sizeof(int32_t) + debug_state->event_str.length());
1693 if (*filesize_limit_bytes < 0) {
1694 // Not enough bytes are left to write this message, so stop logging.
1695 debug_file->CloseFile();
1696 return kNoError;
1697 }
1698 }
peahdf3efa82015-11-28 12:35:15 -08001699 // Write message preceded by its size.
1700 if (!debug_file->Write(&size, sizeof(int32_t))) {
1701 return kFileError;
1702 }
1703 if (!debug_file->Write(debug_state->event_str.data(),
1704 debug_state->event_str.length())) {
1705 return kFileError;
1706 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001707 }
1708
peahdf3efa82015-11-28 12:35:15 -08001709 debug_state->event_msg->Clear();
ajm@google.com808e0e02011-08-03 21:08:51 +00001710
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001711 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001712}
1713
1714int AudioProcessingImpl::WriteInitMessage() {
peahdf3efa82015-11-28 12:35:15 -08001715 debug_dump_.capture.event_msg->set_type(audioproc::Event::INIT);
1716 audioproc::Init* msg = debug_dump_.capture.event_msg->mutable_init();
1717 msg->set_sample_rate(formats_.api_format.input_stream().sample_rate_hz());
ajm@google.com808e0e02011-08-03 21:08:51 +00001718
Peter Kasting69558702016-01-12 16:26:35 -08001719 msg->set_num_input_channels(static_cast<google::protobuf::int32>(
1720 formats_.api_format.input_stream().num_channels()));
1721 msg->set_num_output_channels(static_cast<google::protobuf::int32>(
1722 formats_.api_format.output_stream().num_channels()));
1723 msg->set_num_reverse_channels(static_cast<google::protobuf::int32>(
1724 formats_.api_format.reverse_input_stream().num_channels()));
peahdf3efa82015-11-28 12:35:15 -08001725 msg->set_reverse_sample_rate(
1726 formats_.api_format.reverse_input_stream().sample_rate_hz());
1727 msg->set_output_sample_rate(
1728 formats_.api_format.output_stream().sample_rate_hz());
peahc7bdf8a2016-04-11 07:05:53 -07001729 msg->set_reverse_output_sample_rate(
1730 formats_.api_format.reverse_output_stream().sample_rate_hz());
1731 msg->set_num_reverse_output_channels(
1732 formats_.api_format.reverse_output_stream().num_channels());
peahdf3efa82015-11-28 12:35:15 -08001733
1734 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001735 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001736 &crit_debug_, &debug_dump_.capture));
Minyue13b96ba2015-10-03 00:39:14 +02001737 return kNoError;
1738}
1739
1740int AudioProcessingImpl::WriteConfigMessage(bool forced) {
1741 audioproc::Config config;
1742
peahdf3efa82015-11-28 12:35:15 -08001743 config.set_aec_enabled(public_submodules_->echo_cancellation->is_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001744 config.set_aec_delay_agnostic_enabled(
peahdf3efa82015-11-28 12:35:15 -08001745 public_submodules_->echo_cancellation->is_delay_agnostic_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001746 config.set_aec_drift_compensation_enabled(
peahdf3efa82015-11-28 12:35:15 -08001747 public_submodules_->echo_cancellation->is_drift_compensation_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001748 config.set_aec_extended_filter_enabled(
peahdf3efa82015-11-28 12:35:15 -08001749 public_submodules_->echo_cancellation->is_extended_filter_enabled());
1750 config.set_aec_suppression_level(static_cast<int>(
1751 public_submodules_->echo_cancellation->suppression_level()));
Minyue13b96ba2015-10-03 00:39:14 +02001752
peahdf3efa82015-11-28 12:35:15 -08001753 config.set_aecm_enabled(
1754 public_submodules_->echo_control_mobile->is_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001755 config.set_aecm_comfort_noise_enabled(
peahdf3efa82015-11-28 12:35:15 -08001756 public_submodules_->echo_control_mobile->is_comfort_noise_enabled());
1757 config.set_aecm_routing_mode(static_cast<int>(
1758 public_submodules_->echo_control_mobile->routing_mode()));
Minyue13b96ba2015-10-03 00:39:14 +02001759
peahdf3efa82015-11-28 12:35:15 -08001760 config.set_agc_enabled(public_submodules_->gain_control->is_enabled());
1761 config.set_agc_mode(
1762 static_cast<int>(public_submodules_->gain_control->mode()));
1763 config.set_agc_limiter_enabled(
1764 public_submodules_->gain_control->is_limiter_enabled());
peahbe615622016-02-13 16:40:47 -08001765 config.set_noise_robust_agc_enabled(constants_.use_experimental_agc);
Minyue13b96ba2015-10-03 00:39:14 +02001766
peahdf3efa82015-11-28 12:35:15 -08001767 config.set_hpf_enabled(public_submodules_->high_pass_filter->is_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001768
peahdf3efa82015-11-28 12:35:15 -08001769 config.set_ns_enabled(public_submodules_->noise_suppression->is_enabled());
1770 config.set_ns_level(
1771 static_cast<int>(public_submodules_->noise_suppression->level()));
Minyue13b96ba2015-10-03 00:39:14 +02001772
peahdf3efa82015-11-28 12:35:15 -08001773 config.set_transient_suppression_enabled(
1774 capture_.transient_suppressor_enabled);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001775 config.set_intelligibility_enhancer_enabled(
1776 capture_nonlocked_.intelligibility_enabled);
Minyue13b96ba2015-10-03 00:39:14 +02001777
peah7789fe72016-04-15 01:19:44 -07001778 std::string experiments_description =
1779 public_submodules_->echo_cancellation->GetExperimentsDescription();
1780 // TODO(peah): Add semicolon-separated concatenations of experiment
1781 // descriptions for other submodules.
peahca4cac72016-06-29 15:26:12 -07001782 if (capture_nonlocked_.level_controller_enabled) {
1783 experiments_description += "LevelController;";
1784 }
peah7789fe72016-04-15 01:19:44 -07001785 config.set_experiments_description(experiments_description);
1786
Minyue13b96ba2015-10-03 00:39:14 +02001787 std::string serialized_config = config.SerializeAsString();
peahdf3efa82015-11-28 12:35:15 -08001788 if (!forced &&
1789 debug_dump_.capture.last_serialized_config == serialized_config) {
Minyue13b96ba2015-10-03 00:39:14 +02001790 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001791 }
1792
peahdf3efa82015-11-28 12:35:15 -08001793 debug_dump_.capture.last_serialized_config = serialized_config;
Minyue13b96ba2015-10-03 00:39:14 +02001794
peahdf3efa82015-11-28 12:35:15 -08001795 debug_dump_.capture.event_msg->set_type(audioproc::Event::CONFIG);
1796 debug_dump_.capture.event_msg->mutable_config()->CopyFrom(config);
Minyue13b96ba2015-10-03 00:39:14 +02001797
peahdf3efa82015-11-28 12:35:15 -08001798 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001799 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001800 &crit_debug_, &debug_dump_.capture));
ajm@google.com808e0e02011-08-03 21:08:51 +00001801 return kNoError;
1802}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001803#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001804
kwiberg83ffe452016-08-29 14:46:07 -07001805AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
1806 bool transient_suppressor_enabled,
1807 const std::vector<Point>& array_geometry,
1808 SphericalPointf target_direction)
1809 : aec_system_delay_jumps(-1),
1810 delay_offset_ms(0),
1811 was_stream_delay_set(false),
1812 last_stream_delay_ms(0),
1813 last_aec_system_delay_ms(0),
1814 stream_delay_jumps(-1),
1815 output_will_be_muted(false),
1816 key_pressed(false),
1817 transient_suppressor_enabled(transient_suppressor_enabled),
1818 array_geometry(array_geometry),
1819 target_direction(target_direction),
peahde65ddc2016-09-16 15:02:15 -07001820 capture_processing_format(kSampleRate16kHz),
kwiberg83ffe452016-08-29 14:46:07 -07001821 split_rate(kSampleRate16kHz) {}
1822
1823AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
1824
1825AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
1826
1827AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
1828
niklase@google.com470e71d2011-07-07 08:21:25 +00001829} // namespace webrtc