blob: 32f1d9f5a19f7887b38a0b3c8c0094d065e923ea [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
peahde65ddc2016-09-16 15:02:15 -0700451 public_submodules_->echo_control_mobile->Initialize(
452 proc_split_sample_rate_hz(), num_reverse_channels(),
453 num_output_channels());
peah135259a2016-10-28 03:12:11 -0700454
455 public_submodules_->gain_control->Initialize(num_proc_channels(),
456 proc_sample_rate_hz());
peahde65ddc2016-09-16 15:02:15 -0700457 if (constants_.use_experimental_agc) {
458 if (!private_submodules_->agc_manager.get()) {
459 private_submodules_->agc_manager.reset(new AgcManagerDirect(
460 public_submodules_->gain_control.get(),
461 public_submodules_->gain_control_for_experimental_agc.get(),
462 constants_.agc_startup_min_volume));
463 }
464 private_submodules_->agc_manager->Initialize();
465 private_submodules_->agc_manager->SetCaptureMuted(
466 capture_.output_will_be_muted);
peah135259a2016-10-28 03:12:11 -0700467 public_submodules_->gain_control_for_experimental_agc->Initialize();
peahde65ddc2016-09-16 15:02:15 -0700468 }
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200469 InitializeTransient();
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000470 InitializeBeamformer();
peah1bcfce52016-08-26 07:16:04 -0700471#if WEBRTC_INTELLIGIBILITY_ENHANCER
ekmeyerson60d9b332015-08-14 10:35:55 -0700472 InitializeIntelligibility();
peah1bcfce52016-08-26 07:16:04 -0700473#endif
peahde65ddc2016-09-16 15:02:15 -0700474 public_submodules_->high_pass_filter->Initialize(num_proc_channels(),
475 proc_sample_rate_hz());
476 public_submodules_->noise_suppression->Initialize(num_proc_channels(),
477 proc_sample_rate_hz());
478 public_submodules_->voice_detection->Initialize(proc_split_sample_rate_hz());
479 public_submodules_->level_estimator->Initialize();
peahca4cac72016-06-29 15:26:12 -0700480 InitializeLevelController();
ivoc9f4a4a02016-10-28 05:39:16 -0700481 InitializeResidualEchoDetector();
solenberg70f99032015-12-08 11:07:32 -0800482
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000483#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700484 if (debug_dump_.debug_file->is_open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000485 int err = WriteInitMessage();
486 if (err != kNoError) {
487 return err;
488 }
489 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000490#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000491
niklase@google.com470e71d2011-07-07 08:21:25 +0000492 return kNoError;
493}
494
Michael Graczyk86c6d332015-07-23 11:41:39 -0700495int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
496 for (const auto& stream : config.streams) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700497 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
498 return kBadSampleRateError;
499 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000500 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700501
Peter Kasting69558702016-01-12 16:26:35 -0800502 const size_t num_in_channels = config.input_stream().num_channels();
503 const size_t num_out_channels = config.output_stream().num_channels();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700504
505 // Need at least one input channel.
506 // Need either one output channel or as many outputs as there are inputs.
507 if (num_in_channels == 0 ||
508 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700509 return kBadNumberChannelsError;
510 }
511
aluebsb2328d12016-01-11 20:32:29 -0800512 if (capture_nonlocked_.beamformer_enabled &&
Peter Kasting69558702016-01-12 16:26:35 -0800513 num_in_channels != capture_.array_geometry.size()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700514 return kBadNumberChannelsError;
515 }
516
peahdf3efa82015-11-28 12:35:15 -0800517 formats_.api_format = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000518
peahde65ddc2016-09-16 15:02:15 -0700519 int capture_processing_rate = FindNativeProcessRateToUse(
peah423d2362016-04-09 16:06:52 -0700520 std::min(formats_.api_format.input_stream().sample_rate_hz(),
peah2ace3f92016-09-10 04:42:27 -0700521 formats_.api_format.output_stream().sample_rate_hz()),
522 submodule_states_.CaptureMultiBandSubModulesActive() ||
523 submodule_states_.RenderMultiBandSubModulesActive());
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000524
peahde65ddc2016-09-16 15:02:15 -0700525 capture_nonlocked_.capture_processing_format =
526 StreamConfig(capture_processing_rate);
peah2ace3f92016-09-10 04:42:27 -0700527
peahde65ddc2016-09-16 15:02:15 -0700528 int render_processing_rate = FindNativeProcessRateToUse(
peah2ace3f92016-09-10 04:42:27 -0700529 std::min(formats_.api_format.reverse_input_stream().sample_rate_hz(),
530 formats_.api_format.reverse_output_stream().sample_rate_hz()),
531 submodule_states_.CaptureMultiBandSubModulesActive() ||
532 submodule_states_.RenderMultiBandSubModulesActive());
aluebseb3603b2016-04-20 15:27:58 -0700533 // TODO(aluebs): Remove this restriction once we figure out why the 3-band
534 // splitting filter degrades the AEC performance.
peahde65ddc2016-09-16 15:02:15 -0700535 if (render_processing_rate > kSampleRate32kHz) {
536 render_processing_rate = submodule_states_.RenderMultiBandProcessingActive()
537 ? kSampleRate32kHz
538 : kSampleRate16kHz;
aluebseb3603b2016-04-20 15:27:58 -0700539 }
peahde65ddc2016-09-16 15:02:15 -0700540 // If the forward sample rate is 8 kHz, the render stream is also processed
aluebseb3603b2016-04-20 15:27:58 -0700541 // at this rate.
peahde65ddc2016-09-16 15:02:15 -0700542 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
543 kSampleRate8kHz) {
544 render_processing_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000545 } else {
peahde65ddc2016-09-16 15:02:15 -0700546 render_processing_rate =
547 std::max(render_processing_rate, static_cast<int>(kSampleRate16kHz));
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000548 }
549
peahde65ddc2016-09-16 15:02:15 -0700550 // Always downmix the render stream to mono for analysis. This has been
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000551 // demonstrated to work well for AEC in most practical scenarios.
peahde65ddc2016-09-16 15:02:15 -0700552 formats_.render_processing_format = StreamConfig(render_processing_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000553
peahde65ddc2016-09-16 15:02:15 -0700554 if (capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
555 kSampleRate32kHz ||
556 capture_nonlocked_.capture_processing_format.sample_rate_hz() ==
557 kSampleRate48kHz) {
peahdf3efa82015-11-28 12:35:15 -0800558 capture_nonlocked_.split_rate = kSampleRate16kHz;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000559 } else {
peahdf3efa82015-11-28 12:35:15 -0800560 capture_nonlocked_.split_rate =
peahde65ddc2016-09-16 15:02:15 -0700561 capture_nonlocked_.capture_processing_format.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000562 }
563
564 return InitializeLocked();
565}
566
peah88ac8532016-09-12 16:47:25 -0700567void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) {
peahc19f3122016-10-07 14:54:10 -0700568 config_ = config;
peah88ac8532016-09-12 16:47:25 -0700569
peahc19f3122016-10-07 14:54:10 -0700570 bool config_ok = LevelController::Validate(config_.level_controller);
peah88ac8532016-09-12 16:47:25 -0700571 if (!config_ok) {
572 LOG(LS_ERROR) << "AudioProcessing module config error" << std::endl
573 << "level_controller: "
peahc19f3122016-10-07 14:54:10 -0700574 << LevelController::ToString(config_.level_controller)
peah88ac8532016-09-12 16:47:25 -0700575 << std::endl
576 << "Reverting to default parameter set";
peahc19f3122016-10-07 14:54:10 -0700577 config_.level_controller = AudioProcessing::Config::LevelController();
peah88ac8532016-09-12 16:47:25 -0700578 }
579
580 // Run in a single-threaded manner when applying the settings.
581 rtc::CritScope cs_render(&crit_render_);
582 rtc::CritScope cs_capture(&crit_capture_);
583
peahc19f3122016-10-07 14:54:10 -0700584 // TODO(peah): Replace the use of capture_nonlocked_.level_controller_enabled
585 // with the value in config_ everywhere in the code.
586 if (capture_nonlocked_.level_controller_enabled !=
587 config_.level_controller.enabled) {
peah88ac8532016-09-12 16:47:25 -0700588 capture_nonlocked_.level_controller_enabled =
peahc19f3122016-10-07 14:54:10 -0700589 config_.level_controller.enabled;
590 // TODO(peah): Remove the conditional initialization to always initialize
591 // the level controller regardless of whether it is enabled or not.
592 InitializeLevelController();
peah88ac8532016-09-12 16:47:25 -0700593 }
peahc19f3122016-10-07 14:54:10 -0700594 LOG(LS_INFO) << "Level controller activated: "
595 << capture_nonlocked_.level_controller_enabled;
596
597 private_submodules_->level_controller->ApplyConfig(config_.level_controller);
peah88ac8532016-09-12 16:47:25 -0700598}
599
600void AudioProcessingImpl::SetExtraOptions(const webrtc::Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800601 // Run in a single-threaded manner when setting the extra options.
602 rtc::CritScope cs_render(&crit_render_);
603 rtc::CritScope cs_capture(&crit_capture_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000604
peahb624d8c2016-03-05 03:01:14 -0800605 public_submodules_->echo_cancellation->SetExtraOptions(config);
606
peahdf3efa82015-11-28 12:35:15 -0800607 if (capture_.transient_suppressor_enabled !=
608 config.Get<ExperimentalNs>().enabled) {
609 capture_.transient_suppressor_enabled =
610 config.Get<ExperimentalNs>().enabled;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000611 InitializeTransient();
612 }
aluebs2a346882016-01-11 18:04:30 -0800613
peah1bcfce52016-08-26 07:16:04 -0700614#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700615 if(capture_nonlocked_.intelligibility_enabled !=
616 config.Get<Intelligibility>().enabled) {
617 capture_nonlocked_.intelligibility_enabled =
618 config.Get<Intelligibility>().enabled;
619 InitializeIntelligibility();
620 }
peah1bcfce52016-08-26 07:16:04 -0700621#endif
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700622
aluebs2a346882016-01-11 18:04:30 -0800623#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
aluebsb2328d12016-01-11 20:32:29 -0800624 if (capture_nonlocked_.beamformer_enabled !=
625 config.Get<Beamforming>().enabled) {
626 capture_nonlocked_.beamformer_enabled = config.Get<Beamforming>().enabled;
aluebs2a346882016-01-11 18:04:30 -0800627 if (config.Get<Beamforming>().array_geometry.size() > 1) {
628 capture_.array_geometry = config.Get<Beamforming>().array_geometry;
629 }
630 capture_.target_direction = config.Get<Beamforming>().target_direction;
631 InitializeBeamformer();
632 }
633#endif // WEBRTC_ANDROID_PLATFORM_BUILD
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000634}
635
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000636int AudioProcessingImpl::proc_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800637 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700638 return capture_nonlocked_.capture_processing_format.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000639}
640
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000641int AudioProcessingImpl::proc_split_sample_rate_hz() const {
peahdf3efa82015-11-28 12:35:15 -0800642 // Used as callback from submodules, hence locking is not allowed.
643 return capture_nonlocked_.split_rate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000644}
645
Peter Kasting69558702016-01-12 16:26:35 -0800646size_t AudioProcessingImpl::num_reverse_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800647 // Used as callback from submodules, hence locking is not allowed.
peahde65ddc2016-09-16 15:02:15 -0700648 return formats_.render_processing_format.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000649}
650
Peter Kasting69558702016-01-12 16:26:35 -0800651size_t AudioProcessingImpl::num_input_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800652 // Used as callback from submodules, hence locking is not allowed.
653 return formats_.api_format.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000654}
655
Peter Kasting69558702016-01-12 16:26:35 -0800656size_t AudioProcessingImpl::num_proc_channels() const {
aluebsb2328d12016-01-11 20:32:29 -0800657 // Used as callback from submodules, hence locking is not allowed.
658 return capture_nonlocked_.beamformer_enabled ? 1 : num_output_channels();
659}
660
Peter Kasting69558702016-01-12 16:26:35 -0800661size_t AudioProcessingImpl::num_output_channels() const {
peahdf3efa82015-11-28 12:35:15 -0800662 // Used as callback from submodules, hence locking is not allowed.
663 return formats_.api_format.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000664}
665
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000666void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
peahdf3efa82015-11-28 12:35:15 -0800667 rtc::CritScope cs(&crit_capture_);
668 capture_.output_will_be_muted = muted;
669 if (private_submodules_->agc_manager.get()) {
670 private_submodules_->agc_manager->SetCaptureMuted(
671 capture_.output_will_be_muted);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000672 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000673}
674
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000675
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000676int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700677 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000678 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000679 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000680 int output_sample_rate_hz,
681 ChannelLayout output_layout,
682 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800683 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -0800684 StreamConfig input_stream;
685 StreamConfig output_stream;
686 {
687 // Access the formats_.api_format.input_stream beneath the capture lock.
688 // The lock must be released as it is later required in the call
689 // to ProcessStream(,,,);
690 rtc::CritScope cs(&crit_capture_);
691 input_stream = formats_.api_format.input_stream();
692 output_stream = formats_.api_format.output_stream();
693 }
694
Michael Graczyk86c6d332015-07-23 11:41:39 -0700695 input_stream.set_sample_rate_hz(input_sample_rate_hz);
696 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
697 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
Michael Graczyk86c6d332015-07-23 11:41:39 -0700698 output_stream.set_sample_rate_hz(output_sample_rate_hz);
699 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
700 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
701
702 if (samples_per_channel != input_stream.num_frames()) {
703 return kBadDataLengthError;
704 }
705 return ProcessStream(src, input_stream, output_stream, dest);
706}
707
708int AudioProcessingImpl::ProcessStream(const float* const* src,
709 const StreamConfig& input_config,
710 const StreamConfig& output_config,
711 float* const* dest) {
peah369f8282015-12-17 06:42:29 -0800712 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -0800713 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700714 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800715 {
716 // Acquire the capture lock in order to safely call the function
717 // that retrieves the render side data. This function accesses apm
718 // getters that need the capture lock held when being called.
719 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700720 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800721
722 if (!src || !dest) {
723 return kNullPointerError;
724 }
725
726 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700727 reinitialization_required = UpdateActiveSubmoduleStates();
niklase@google.com470e71d2011-07-07 08:21:25 +0000728 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000729
Michael Graczyk86c6d332015-07-23 11:41:39 -0700730 processing_config.input_stream() = input_config;
731 processing_config.output_stream() = output_config;
732
peahdf3efa82015-11-28 12:35:15 -0800733 {
734 // Do conditional reinitialization.
735 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -0700736 RETURN_ON_ERR(
737 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -0800738 }
739 rtc::CritScope cs_capture(&crit_capture_);
kwiberg9e2be5f2016-09-14 05:23:22 -0700740 RTC_DCHECK_EQ(processing_config.input_stream().num_frames(),
741 formats_.api_format.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000742
743#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700744 if (debug_dump_.debug_file->is_open()) {
Minyue13b96ba2015-10-03 00:39:14 +0200745 RETURN_ON_ERR(WriteConfigMessage(false));
746
peahdf3efa82015-11-28 12:35:15 -0800747 debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM);
748 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000749 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -0800750 sizeof(float) * formats_.api_format.input_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -0800751 for (size_t i = 0; i < formats_.api_format.input_stream().num_channels();
752 ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000753 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000754 }
755#endif
756
peahdf3efa82015-11-28 12:35:15 -0800757 capture_.capture_audio->CopyFrom(src, formats_.api_format.input_stream());
peahde65ddc2016-09-16 15:02:15 -0700758 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peahdf3efa82015-11-28 12:35:15 -0800759 capture_.capture_audio->CopyTo(formats_.api_format.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000760
761#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -0700762 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -0800763 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000764 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -0800765 sizeof(float) * formats_.api_format.output_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -0800766 for (size_t i = 0; i < formats_.api_format.output_stream().num_channels();
767 ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000768 msg->add_output_channel(dest[i], channel_size);
peahdf3efa82015-11-28 12:35:15 -0800769 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -0800770 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -0800771 &crit_debug_, &debug_dump_.capture));
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000772 }
773#endif
774
775 return kNoError;
776}
777
peah701d6282016-10-25 05:42:20 -0700778void AudioProcessingImpl::QueueRenderAudio(AudioBuffer* audio) {
peah764e3642016-10-22 05:04:30 -0700779 EchoCancellationImpl::PackRenderAudioBuffer(audio, num_output_channels(),
780 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700781 &aec_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700782
783 RTC_DCHECK_GE(160u, audio->num_frames_per_band());
784
785 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700786 if (!aec_render_signal_queue_->Insert(&aec_render_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700787 // The data queue is full and needs to be emptied.
788 EmptyQueuedRenderAudio();
789
790 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700791 bool result = aec_render_signal_queue_->Insert(&aec_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700792 RTC_DCHECK(result);
793 }
794
795 EchoControlMobileImpl::PackRenderAudioBuffer(audio, num_output_channels(),
796 num_reverse_channels(),
peah701d6282016-10-25 05:42:20 -0700797 &aecm_render_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700798
799 // Insert the samples into the queue.
peah701d6282016-10-25 05:42:20 -0700800 if (!aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700801 // The data queue is full and needs to be emptied.
802 EmptyQueuedRenderAudio();
803
804 // Retry the insert (should always work).
peah701d6282016-10-25 05:42:20 -0700805 bool result = aecm_render_signal_queue_->Insert(&aecm_render_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700806 RTC_DCHECK(result);
807 }
peah701d6282016-10-25 05:42:20 -0700808
809 if (!constants_.use_experimental_agc) {
810 GainControlImpl::PackRenderAudioBuffer(audio, &agc_render_queue_buffer_);
811 // Insert the samples into the queue.
812 if (!agc_render_signal_queue_->Insert(&agc_render_queue_buffer_)) {
813 // The data queue is full and needs to be emptied.
814 EmptyQueuedRenderAudio();
815
816 // Retry the insert (should always work).
817 bool result = agc_render_signal_queue_->Insert(&agc_render_queue_buffer_);
818 RTC_DCHECK(result);
819 }
820 }
ivoc9f4a4a02016-10-28 05:39:16 -0700821
822 ResidualEchoDetector::PackRenderAudioBuffer(audio, &red_render_queue_buffer_);
823
824 // Insert the samples into the queue.
825 if (!red_render_signal_queue_->Insert(&red_render_queue_buffer_)) {
826 // The data queue is full and needs to be emptied.
827 EmptyQueuedRenderAudio();
828
829 // Retry the insert (should always work).
830 bool result = red_render_signal_queue_->Insert(&red_render_queue_buffer_);
831 RTC_DCHECK(result);
832 }
peah764e3642016-10-22 05:04:30 -0700833}
834
835void AudioProcessingImpl::AllocateRenderQueue() {
peah701d6282016-10-25 05:42:20 -0700836 const size_t new_aec_render_queue_element_max_size =
peah764e3642016-10-22 05:04:30 -0700837 std::max(static_cast<size_t>(1),
838 kMaxAllowedValuesOfSamplesPerFrame *
839 EchoCancellationImpl::NumCancellersRequired(
840 num_output_channels(), num_reverse_channels()));
841
peah701d6282016-10-25 05:42:20 -0700842 const size_t new_aecm_render_queue_element_max_size =
peaha0624602016-10-25 04:45:24 -0700843 std::max(static_cast<size_t>(1),
844 kMaxAllowedValuesOfSamplesPerFrame *
845 EchoControlMobileImpl::NumCancellersRequired(
846 num_output_channels(), num_reverse_channels()));
peah764e3642016-10-22 05:04:30 -0700847
peah701d6282016-10-25 05:42:20 -0700848 const size_t new_agc_render_queue_element_max_size =
849 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
850
ivoc9f4a4a02016-10-28 05:39:16 -0700851 const size_t new_red_render_queue_element_max_size =
852 std::max(static_cast<size_t>(1), kMaxAllowedValuesOfSamplesPerFrame);
853
peaha0624602016-10-25 04:45:24 -0700854 // Reallocate the queues if the queue item sizes are too small to fit the
855 // data to put in the queues.
peah701d6282016-10-25 05:42:20 -0700856 if (aec_render_queue_element_max_size_ <
857 new_aec_render_queue_element_max_size) {
858 aec_render_queue_element_max_size_ = new_aec_render_queue_element_max_size;
peah764e3642016-10-22 05:04:30 -0700859
peaha0624602016-10-25 04:45:24 -0700860 std::vector<float> template_queue_element(
peah701d6282016-10-25 05:42:20 -0700861 aec_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700862
peah701d6282016-10-25 05:42:20 -0700863 aec_render_signal_queue_.reset(
peah764e3642016-10-22 05:04:30 -0700864 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
865 kMaxNumFramesToBuffer, template_queue_element,
peaha0624602016-10-25 04:45:24 -0700866 RenderQueueItemVerifier<float>(
peah701d6282016-10-25 05:42:20 -0700867 aec_render_queue_element_max_size_)));
peah764e3642016-10-22 05:04:30 -0700868
peah701d6282016-10-25 05:42:20 -0700869 aec_render_queue_buffer_.resize(aec_render_queue_element_max_size_);
870 aec_capture_queue_buffer_.resize(aec_render_queue_element_max_size_);
peah764e3642016-10-22 05:04:30 -0700871 } else {
peah701d6282016-10-25 05:42:20 -0700872 aec_render_signal_queue_->Clear();
peaha0624602016-10-25 04:45:24 -0700873 }
874
peah701d6282016-10-25 05:42:20 -0700875 if (aecm_render_queue_element_max_size_ <
876 new_aecm_render_queue_element_max_size) {
877 aecm_render_queue_element_max_size_ =
878 new_aecm_render_queue_element_max_size;
peaha0624602016-10-25 04:45:24 -0700879
880 std::vector<int16_t> template_queue_element(
peah701d6282016-10-25 05:42:20 -0700881 aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700882
peah701d6282016-10-25 05:42:20 -0700883 aecm_render_signal_queue_.reset(
peaha0624602016-10-25 04:45:24 -0700884 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
885 kMaxNumFramesToBuffer, template_queue_element,
886 RenderQueueItemVerifier<int16_t>(
peah701d6282016-10-25 05:42:20 -0700887 aecm_render_queue_element_max_size_)));
peaha0624602016-10-25 04:45:24 -0700888
peah701d6282016-10-25 05:42:20 -0700889 aecm_render_queue_buffer_.resize(aecm_render_queue_element_max_size_);
890 aecm_capture_queue_buffer_.resize(aecm_render_queue_element_max_size_);
peaha0624602016-10-25 04:45:24 -0700891 } else {
peah701d6282016-10-25 05:42:20 -0700892 aecm_render_signal_queue_->Clear();
893 }
894
895 if (agc_render_queue_element_max_size_ <
896 new_agc_render_queue_element_max_size) {
897 agc_render_queue_element_max_size_ = new_agc_render_queue_element_max_size;
898
899 std::vector<int16_t> template_queue_element(
900 agc_render_queue_element_max_size_);
901
902 agc_render_signal_queue_.reset(
903 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
904 kMaxNumFramesToBuffer, template_queue_element,
905 RenderQueueItemVerifier<int16_t>(
906 agc_render_queue_element_max_size_)));
907
908 agc_render_queue_buffer_.resize(agc_render_queue_element_max_size_);
909 agc_capture_queue_buffer_.resize(agc_render_queue_element_max_size_);
910 } else {
911 agc_render_signal_queue_->Clear();
peah764e3642016-10-22 05:04:30 -0700912 }
ivoc9f4a4a02016-10-28 05:39:16 -0700913
914 if (red_render_queue_element_max_size_ <
915 new_red_render_queue_element_max_size) {
916 red_render_queue_element_max_size_ = new_red_render_queue_element_max_size;
917
918 std::vector<float> template_queue_element(
919 red_render_queue_element_max_size_);
920
921 red_render_signal_queue_.reset(
922 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
923 kMaxNumFramesToBuffer, template_queue_element,
924 RenderQueueItemVerifier<float>(
925 red_render_queue_element_max_size_)));
926
927 red_render_queue_buffer_.resize(red_render_queue_element_max_size_);
928 red_capture_queue_buffer_.resize(red_render_queue_element_max_size_);
929 } else {
930 red_render_signal_queue_->Clear();
931 }
peah764e3642016-10-22 05:04:30 -0700932}
933
934void AudioProcessingImpl::EmptyQueuedRenderAudio() {
935 rtc::CritScope cs_capture(&crit_capture_);
peah701d6282016-10-25 05:42:20 -0700936 while (aec_render_signal_queue_->Remove(&aec_capture_queue_buffer_)) {
peah764e3642016-10-22 05:04:30 -0700937 public_submodules_->echo_cancellation->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -0700938 aec_capture_queue_buffer_);
peaha0624602016-10-25 04:45:24 -0700939 }
940
peah701d6282016-10-25 05:42:20 -0700941 while (aecm_render_signal_queue_->Remove(&aecm_capture_queue_buffer_)) {
peaha0624602016-10-25 04:45:24 -0700942 public_submodules_->echo_control_mobile->ProcessRenderAudio(
peah701d6282016-10-25 05:42:20 -0700943 aecm_capture_queue_buffer_);
944 }
945
946 while (agc_render_signal_queue_->Remove(&agc_capture_queue_buffer_)) {
947 public_submodules_->gain_control->ProcessRenderAudio(
948 agc_capture_queue_buffer_);
peah764e3642016-10-22 05:04:30 -0700949 }
ivoc9f4a4a02016-10-28 05:39:16 -0700950
951 while (red_render_signal_queue_->Remove(&red_capture_queue_buffer_)) {
952 private_submodules_->residual_echo_detector->AnalyzeRenderAudio(
953 red_capture_queue_buffer_);
954 }
peah764e3642016-10-22 05:04:30 -0700955}
956
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000957int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -0800958 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -0800959 {
960 // Acquire the capture lock in order to safely call the function
961 // that retrieves the render side data. This function accesses apm
962 // getters that need the capture lock held when being called.
963 // The lock needs to be released as
964 // public_submodules_->echo_control_mobile->is_enabled() aquires this lock
965 // as well.
966 rtc::CritScope cs_capture(&crit_capture_);
peah764e3642016-10-22 05:04:30 -0700967 EmptyQueuedRenderAudio();
peahdf3efa82015-11-28 12:35:15 -0800968 }
peahfa6228e2015-11-16 16:27:42 -0800969
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000970 if (!frame) {
971 return kNullPointerError;
972 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000973 // Must be a native rate.
974 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
975 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000976 frame->sample_rate_hz_ != kSampleRate32kHz &&
977 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000978 return kBadSampleRateError;
979 }
peah192164e2015-11-17 02:16:45 -0800980
peahdf3efa82015-11-28 12:35:15 -0800981 ProcessingConfig processing_config;
peah2ace3f92016-09-10 04:42:27 -0700982 bool reinitialization_required = false;
peahdf3efa82015-11-28 12:35:15 -0800983 {
984 // Aquire lock for the access of api_format.
985 // The lock is released immediately due to the conditional
986 // reinitialization.
987 rtc::CritScope cs_capture(&crit_capture_);
988 // TODO(ajm): The input and output rates and channels are currently
989 // constrained to be identical in the int16 interface.
990 processing_config = formats_.api_format;
peah2ace3f92016-09-10 04:42:27 -0700991
992 reinitialization_required = UpdateActiveSubmoduleStates();
peahdf3efa82015-11-28 12:35:15 -0800993 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700994 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
995 processing_config.input_stream().set_num_channels(frame->num_channels_);
996 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
997 processing_config.output_stream().set_num_channels(frame->num_channels_);
998
peahdf3efa82015-11-28 12:35:15 -0800999 {
1000 // Do conditional reinitialization.
1001 rtc::CritScope cs_render(&crit_render_);
peah2ace3f92016-09-10 04:42:27 -07001002 RETURN_ON_ERR(
1003 MaybeInitializeCapture(processing_config, reinitialization_required));
peahdf3efa82015-11-28 12:35:15 -08001004 }
1005 rtc::CritScope cs_capture(&crit_capture_);
peah192164e2015-11-17 02:16:45 -08001006 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001007 formats_.api_format.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001008 return kBadDataLengthError;
1009 }
1010
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001011#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001012 if (debug_dump_.debug_file->is_open()) {
peah644fa962016-08-18 06:48:33 -07001013 RETURN_ON_ERR(WriteConfigMessage(false));
1014
peahdf3efa82015-11-28 12:35:15 -08001015 debug_dump_.capture.event_msg->set_type(audioproc::Event::STREAM);
1016 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001017 const size_t data_size =
1018 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001019 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001020 }
1021#endif
1022
peahdf3efa82015-11-28 12:35:15 -08001023 capture_.capture_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001024 RETURN_ON_ERR(ProcessCaptureStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001025 capture_.capture_audio->InterleaveTo(
1026 frame, submodule_states_.CaptureMultiBandProcessingActive());
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001027
1028#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001029 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001030 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001031 const size_t data_size =
1032 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001033 msg->set_output_data(frame->data_, data_size);
peahdf3efa82015-11-28 12:35:15 -08001034 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001035 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001036 &crit_debug_, &debug_dump_.capture));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001037 }
1038#endif
1039
1040 return kNoError;
1041}
1042
peahde65ddc2016-09-16 15:02:15 -07001043int AudioProcessingImpl::ProcessCaptureStreamLocked() {
peahb58a1582016-03-15 09:34:24 -07001044 // Ensure that not both the AEC and AECM are active at the same time.
1045 // TODO(peah): Simplify once the public API Enable functions for these
1046 // are moved to APM.
1047 RTC_DCHECK(!(public_submodules_->echo_cancellation->is_enabled() &&
1048 public_submodules_->echo_control_mobile->is_enabled()));
1049
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001050#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001051 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001052 audioproc::Stream* msg = debug_dump_.capture.event_msg->mutable_stream();
1053 msg->set_delay(capture_nonlocked_.stream_delay_ms);
1054 msg->set_drift(
1055 public_submodules_->echo_cancellation->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +00001056 msg->set_level(gain_control()->stream_analog_level());
peahdf3efa82015-11-28 12:35:15 -08001057 msg->set_keypress(capture_.key_pressed);
niklase@google.com470e71d2011-07-07 08:21:25 +00001058 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001059#endif
niklase@google.com470e71d2011-07-07 08:21:25 +00001060
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001061 MaybeUpdateHistograms();
1062
peahde65ddc2016-09-16 15:02:15 -07001063 AudioBuffer* capture_buffer = capture_.capture_audio.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -07001064
peahbe615622016-02-13 16:40:47 -08001065 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001066 public_submodules_->gain_control->is_enabled()) {
1067 private_submodules_->agc_manager->AnalyzePreProcess(
peahde65ddc2016-09-16 15:02:15 -07001068 capture_buffer->channels()[0], capture_buffer->num_channels(),
1069 capture_nonlocked_.capture_processing_format.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001070 }
1071
peah2ace3f92016-09-10 04:42:27 -07001072 if (submodule_states_.CaptureMultiBandSubModulesActive() &&
1073 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001074 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1075 capture_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001076 }
1077
aluebsb2328d12016-01-11 20:32:29 -08001078 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001079 private_submodules_->beamformer->AnalyzeChunk(
1080 *capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001081 // Discards all channels by the leftmost one.
peahde65ddc2016-09-16 15:02:15 -07001082 capture_buffer->set_num_channels(1);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001083 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001084
peahde65ddc2016-09-16 15:02:15 -07001085 public_submodules_->high_pass_filter->ProcessCaptureAudio(capture_buffer);
1086 RETURN_ON_ERR(
1087 public_submodules_->gain_control->AnalyzeCaptureAudio(capture_buffer));
1088 public_submodules_->noise_suppression->AnalyzeCaptureAudio(capture_buffer);
peahb58a1582016-03-15 09:34:24 -07001089
1090 // Ensure that the stream delay was set before the call to the
1091 // AEC ProcessCaptureAudio function.
1092 if (public_submodules_->echo_cancellation->is_enabled() &&
1093 !was_stream_delay_set()) {
1094 return AudioProcessing::kStreamParameterNotSetError;
1095 }
1096
1097 RETURN_ON_ERR(public_submodules_->echo_cancellation->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001098 capture_buffer, stream_delay_ms()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001099
peahdf3efa82015-11-28 12:35:15 -08001100 if (public_submodules_->echo_control_mobile->is_enabled() &&
1101 public_submodules_->noise_suppression->is_enabled()) {
peahde65ddc2016-09-16 15:02:15 -07001102 capture_buffer->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +00001103 }
peahde65ddc2016-09-16 15:02:15 -07001104 public_submodules_->noise_suppression->ProcessCaptureAudio(capture_buffer);
peah1bcfce52016-08-26 07:16:04 -07001105#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001106 if (capture_nonlocked_.intelligibility_enabled) {
aluebsc466bad2016-02-10 12:03:00 -08001107 RTC_DCHECK(public_submodules_->noise_suppression->is_enabled());
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001108 int gain_db = public_submodules_->gain_control->is_enabled() ?
1109 public_submodules_->gain_control->compression_gain_db() :
1110 0;
Alejandro Luebs50411102016-06-30 15:35:41 -07001111 float gain = std::pow(10.f, gain_db / 20.f);
1112 gain *= capture_nonlocked_.level_controller_enabled ?
1113 private_submodules_->level_controller->GetLastGain() :
1114 1.f;
aluebsc466bad2016-02-10 12:03:00 -08001115 public_submodules_->intelligibility_enhancer->SetCaptureNoiseEstimate(
Alejandro Luebs50411102016-06-30 15:35:41 -07001116 public_submodules_->noise_suppression->NoiseEstimate(), gain);
aluebsc466bad2016-02-10 12:03:00 -08001117 }
peah1bcfce52016-08-26 07:16:04 -07001118#endif
peah253534d2016-03-15 04:32:28 -07001119
1120 // Ensure that the stream delay was set before the call to the
1121 // AECM ProcessCaptureAudio function.
1122 if (public_submodules_->echo_control_mobile->is_enabled() &&
1123 !was_stream_delay_set()) {
1124 return AudioProcessing::kStreamParameterNotSetError;
1125 }
1126
1127 RETURN_ON_ERR(public_submodules_->echo_control_mobile->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001128 capture_buffer, stream_delay_ms()));
peah253534d2016-03-15 04:32:28 -07001129
ivoc9f4a4a02016-10-28 05:39:16 -07001130 if (config_.residual_echo_detector.enabled) {
1131 private_submodules_->residual_echo_detector->AnalyzeCaptureAudio(
1132 rtc::ArrayView<const float>(
1133 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1134 capture_buffer->num_frames_per_band()));
1135 }
1136
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001137 if (capture_nonlocked_.beamformer_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001138 private_submodules_->beamformer->PostFilter(capture_buffer->split_data_f());
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001139 }
1140
peahde65ddc2016-09-16 15:02:15 -07001141 public_submodules_->voice_detection->ProcessCaptureAudio(capture_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001142
peahbe615622016-02-13 16:40:47 -08001143 if (constants_.use_experimental_agc &&
peahdf3efa82015-11-28 12:35:15 -08001144 public_submodules_->gain_control->is_enabled() &&
aluebsb2328d12016-01-11 20:32:29 -08001145 (!capture_nonlocked_.beamformer_enabled ||
peahdf3efa82015-11-28 12:35:15 -08001146 private_submodules_->beamformer->is_target_present())) {
1147 private_submodules_->agc_manager->Process(
peahde65ddc2016-09-16 15:02:15 -07001148 capture_buffer->split_bands_const(0)[kBand0To8kHz],
1149 capture_buffer->num_frames_per_band(), capture_nonlocked_.split_rate);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001150 }
peahb8fbb542016-03-15 02:28:08 -07001151 RETURN_ON_ERR(public_submodules_->gain_control->ProcessCaptureAudio(
peahde65ddc2016-09-16 15:02:15 -07001152 capture_buffer, echo_cancellation()->stream_has_echo()));
niklase@google.com470e71d2011-07-07 08:21:25 +00001153
peah2ace3f92016-09-10 04:42:27 -07001154 if (submodule_states_.CaptureMultiBandProcessingActive() &&
1155 SampleRateSupportsMultiBand(
peahde65ddc2016-09-16 15:02:15 -07001156 capture_nonlocked_.capture_processing_format.sample_rate_hz())) {
1157 capture_buffer->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001158 }
1159
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001160 // TODO(aluebs): Investigate if the transient suppression placement should be
1161 // before or after the AGC.
peahdf3efa82015-11-28 12:35:15 -08001162 if (capture_.transient_suppressor_enabled) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001163 float voice_probability =
peahdf3efa82015-11-28 12:35:15 -08001164 private_submodules_->agc_manager.get()
1165 ? private_submodules_->agc_manager->voice_probability()
1166 : 1.f;
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001167
peahdf3efa82015-11-28 12:35:15 -08001168 public_submodules_->transient_suppressor->Suppress(
peahde65ddc2016-09-16 15:02:15 -07001169 capture_buffer->channels_f()[0], capture_buffer->num_frames(),
1170 capture_buffer->num_channels(),
1171 capture_buffer->split_bands_const_f(0)[kBand0To8kHz],
1172 capture_buffer->num_frames_per_band(), capture_buffer->keyboard_data(),
1173 capture_buffer->num_keyboard_frames(), voice_probability,
peahdf3efa82015-11-28 12:35:15 -08001174 capture_.key_pressed);
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001175 }
1176
peahca4cac72016-06-29 15:26:12 -07001177 if (capture_nonlocked_.level_controller_enabled) {
peahde65ddc2016-09-16 15:02:15 -07001178 private_submodules_->level_controller->Process(capture_buffer);
peahca4cac72016-06-29 15:26:12 -07001179 }
1180
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001181 // The level estimator operates on the recombined data.
peahde65ddc2016-09-16 15:02:15 -07001182 public_submodules_->level_estimator->ProcessStream(capture_buffer);
ajm@google.com808e0e02011-08-03 21:08:51 +00001183
peahdf3efa82015-11-28 12:35:15 -08001184 capture_.was_stream_delay_set = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001185 return kNoError;
1186}
1187
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001188int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001189 size_t samples_per_channel,
peahde65ddc2016-09-16 15:02:15 -07001190 int sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001191 ChannelLayout layout) {
peah369f8282015-12-17 06:42:29 -08001192 TRACE_EVENT0("webrtc", "AudioProcessing::AnalyzeReverseStream_ChannelLayout");
peahdf3efa82015-11-28 12:35:15 -08001193 rtc::CritScope cs(&crit_render_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001194 const StreamConfig reverse_config = {
peahde65ddc2016-09-16 15:02:15 -07001195 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -07001196 };
1197 if (samples_per_channel != reverse_config.num_frames()) {
1198 return kBadDataLengthError;
1199 }
peahdf3efa82015-11-28 12:35:15 -08001200 return AnalyzeReverseStreamLocked(data, reverse_config, reverse_config);
ekmeyerson60d9b332015-08-14 10:35:55 -07001201}
1202
peahde65ddc2016-09-16 15:02:15 -07001203int AudioProcessingImpl::ProcessReverseStream(const float* const* src,
1204 const StreamConfig& input_config,
1205 const StreamConfig& output_config,
1206 float* const* dest) {
peah369f8282015-12-17 06:42:29 -08001207 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_StreamConfig");
peahdf3efa82015-11-28 12:35:15 -08001208 rtc::CritScope cs(&crit_render_);
peahde65ddc2016-09-16 15:02:15 -07001209 RETURN_ON_ERR(AnalyzeReverseStreamLocked(src, input_config, output_config));
peah2ace3f92016-09-10 04:42:27 -07001210 if (submodule_states_.RenderMultiBandProcessingActive()) {
peahdf3efa82015-11-28 12:35:15 -08001211 render_.render_audio->CopyTo(formats_.api_format.reverse_output_stream(),
1212 dest);
peah2ace3f92016-09-10 04:42:27 -07001213 } else if (formats_.api_format.reverse_input_stream() !=
1214 formats_.api_format.reverse_output_stream()) {
peahde65ddc2016-09-16 15:02:15 -07001215 render_.render_converter->Convert(src, input_config.num_samples(), dest,
1216 output_config.num_samples());
ekmeyerson60d9b332015-08-14 10:35:55 -07001217 } else {
peahde65ddc2016-09-16 15:02:15 -07001218 CopyAudioIfNeeded(src, input_config.num_frames(),
1219 input_config.num_channels(), dest);
ekmeyerson60d9b332015-08-14 10:35:55 -07001220 }
1221
1222 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001223}
1224
peahdf3efa82015-11-28 12:35:15 -08001225int AudioProcessingImpl::AnalyzeReverseStreamLocked(
ekmeyerson60d9b332015-08-14 10:35:55 -07001226 const float* const* src,
peahde65ddc2016-09-16 15:02:15 -07001227 const StreamConfig& input_config,
1228 const StreamConfig& output_config) {
peahdf3efa82015-11-28 12:35:15 -08001229 if (src == nullptr) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001230 return kNullPointerError;
1231 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001232
peahde65ddc2016-09-16 15:02:15 -07001233 if (input_config.num_channels() == 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001234 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001235 }
1236
peahdf3efa82015-11-28 12:35:15 -08001237 ProcessingConfig processing_config = formats_.api_format;
peahde65ddc2016-09-16 15:02:15 -07001238 processing_config.reverse_input_stream() = input_config;
1239 processing_config.reverse_output_stream() = output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001240
peahdf3efa82015-11-28 12:35:15 -08001241 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
peahde65ddc2016-09-16 15:02:15 -07001242 assert(input_config.num_frames() ==
1243 formats_.api_format.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001244
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001245#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001246 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001247 debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM);
1248 audioproc::ReverseStream* msg =
1249 debug_dump_.render.event_msg->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +00001250 const size_t channel_size =
peahdf3efa82015-11-28 12:35:15 -08001251 sizeof(float) * formats_.api_format.reverse_input_stream().num_frames();
Peter Kasting69558702016-01-12 16:26:35 -08001252 for (size_t i = 0;
peahdf3efa82015-11-28 12:35:15 -08001253 i < formats_.api_format.reverse_input_stream().num_channels(); ++i)
ekmeyerson60d9b332015-08-14 10:35:55 -07001254 msg->add_channel(src[i], channel_size);
peahdf3efa82015-11-28 12:35:15 -08001255 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001256 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001257 &crit_debug_, &debug_dump_.render));
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001258 }
1259#endif
1260
peahdf3efa82015-11-28 12:35:15 -08001261 render_.render_audio->CopyFrom(src,
1262 formats_.api_format.reverse_input_stream());
peahde65ddc2016-09-16 15:02:15 -07001263 return ProcessRenderStreamLocked();
ekmeyerson60d9b332015-08-14 10:35:55 -07001264}
1265
1266int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
peah369f8282015-12-17 06:42:29 -08001267 TRACE_EVENT0("webrtc", "AudioProcessing::ProcessReverseStream_AudioFrame");
peahdf3efa82015-11-28 12:35:15 -08001268 rtc::CritScope cs(&crit_render_);
peahdf3efa82015-11-28 12:35:15 -08001269 if (frame == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001270 return kNullPointerError;
1271 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001272 // Must be a native rate.
1273 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
1274 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +00001275 frame->sample_rate_hz_ != kSampleRate32kHz &&
1276 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001277 return kBadSampleRateError;
1278 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +00001279
Michael Graczyk86c6d332015-07-23 11:41:39 -07001280 if (frame->num_channels_ <= 0) {
1281 return kBadNumberChannelsError;
1282 }
1283
peahdf3efa82015-11-28 12:35:15 -08001284 ProcessingConfig processing_config = formats_.api_format;
ekmeyerson60d9b332015-08-14 10:35:55 -07001285 processing_config.reverse_input_stream().set_sample_rate_hz(
1286 frame->sample_rate_hz_);
1287 processing_config.reverse_input_stream().set_num_channels(
1288 frame->num_channels_);
1289 processing_config.reverse_output_stream().set_sample_rate_hz(
1290 frame->sample_rate_hz_);
1291 processing_config.reverse_output_stream().set_num_channels(
1292 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -07001293
peahdf3efa82015-11-28 12:35:15 -08001294 RETURN_ON_ERR(MaybeInitializeRender(processing_config));
Michael Graczyk86c6d332015-07-23 11:41:39 -07001295 if (frame->samples_per_channel_ !=
peahdf3efa82015-11-28 12:35:15 -08001296 formats_.api_format.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001297 return kBadDataLengthError;
1298 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001299
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001300#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
tommia6219cc2016-06-15 10:30:14 -07001301 if (debug_dump_.debug_file->is_open()) {
peahdf3efa82015-11-28 12:35:15 -08001302 debug_dump_.render.event_msg->set_type(audioproc::Event::REVERSE_STREAM);
1303 audioproc::ReverseStream* msg =
1304 debug_dump_.render.event_msg->mutable_reverse_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001305 const size_t data_size =
1306 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001307 msg->set_data(frame->data_, data_size);
peahdf3efa82015-11-28 12:35:15 -08001308 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001309 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001310 &crit_debug_, &debug_dump_.render));
niklase@google.com470e71d2011-07-07 08:21:25 +00001311 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001312#endif
peahdf3efa82015-11-28 12:35:15 -08001313 render_.render_audio->DeinterleaveFrom(frame);
peahde65ddc2016-09-16 15:02:15 -07001314 RETURN_ON_ERR(ProcessRenderStreamLocked());
peah2ace3f92016-09-10 04:42:27 -07001315 render_.render_audio->InterleaveTo(
1316 frame, submodule_states_.RenderMultiBandProcessingActive());
aluebsb0319552016-03-17 20:39:53 -07001317 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001318}
niklase@google.com470e71d2011-07-07 08:21:25 +00001319
peahde65ddc2016-09-16 15:02:15 -07001320int AudioProcessingImpl::ProcessRenderStreamLocked() {
1321 AudioBuffer* render_buffer = render_.render_audio.get(); // For brevity.
peah2ace3f92016-09-10 04:42:27 -07001322 if (submodule_states_.RenderMultiBandSubModulesActive() &&
peahde65ddc2016-09-16 15:02:15 -07001323 SampleRateSupportsMultiBand(
1324 formats_.render_processing_format.sample_rate_hz())) {
1325 render_buffer->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +00001326 }
1327
peah1bcfce52016-08-26 07:16:04 -07001328#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001329 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001330 public_submodules_->intelligibility_enhancer->ProcessRenderAudio(
Alejandro Luebsef009252016-09-20 14:51:56 -07001331 render_buffer);
ekmeyerson60d9b332015-08-14 10:35:55 -07001332 }
peah1bcfce52016-08-26 07:16:04 -07001333#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001334
peah764e3642016-10-22 05:04:30 -07001335 QueueRenderAudio(render_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +00001336
peah2ace3f92016-09-10 04:42:27 -07001337 if (submodule_states_.RenderMultiBandProcessingActive() &&
peahde65ddc2016-09-16 15:02:15 -07001338 SampleRateSupportsMultiBand(
1339 formats_.render_processing_format.sample_rate_hz())) {
1340 render_buffer->MergeFrequencyBands();
ekmeyerson60d9b332015-08-14 10:35:55 -07001341 }
1342
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001343 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +00001344}
1345
1346int AudioProcessingImpl::set_stream_delay_ms(int delay) {
peahdf3efa82015-11-28 12:35:15 -08001347 rtc::CritScope cs(&crit_capture_);
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001348 Error retval = kNoError;
peahdf3efa82015-11-28 12:35:15 -08001349 capture_.was_stream_delay_set = true;
1350 delay += capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001351
niklase@google.com470e71d2011-07-07 08:21:25 +00001352 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001353 delay = 0;
1354 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001355 }
1356
1357 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
1358 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001359 delay = 500;
1360 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +00001361 }
1362
peahdf3efa82015-11-28 12:35:15 -08001363 capture_nonlocked_.stream_delay_ms = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +00001364 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +00001365}
1366
1367int AudioProcessingImpl::stream_delay_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001368 // Used as callback from submodules, hence locking is not allowed.
1369 return capture_nonlocked_.stream_delay_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +00001370}
1371
1372bool AudioProcessingImpl::was_stream_delay_set() const {
peahdf3efa82015-11-28 12:35:15 -08001373 // Used as callback from submodules, hence locking is not allowed.
1374 return capture_.was_stream_delay_set;
niklase@google.com470e71d2011-07-07 08:21:25 +00001375}
1376
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001377void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
peahdf3efa82015-11-28 12:35:15 -08001378 rtc::CritScope cs(&crit_capture_);
1379 capture_.key_pressed = key_pressed;
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001380}
1381
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001382void AudioProcessingImpl::set_delay_offset_ms(int offset) {
peahdf3efa82015-11-28 12:35:15 -08001383 rtc::CritScope cs(&crit_capture_);
1384 capture_.delay_offset_ms = offset;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001385}
1386
1387int AudioProcessingImpl::delay_offset_ms() const {
peahdf3efa82015-11-28 12:35:15 -08001388 rtc::CritScope cs(&crit_capture_);
1389 return capture_.delay_offset_ms;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +00001390}
1391
niklase@google.com470e71d2011-07-07 08:21:25 +00001392int AudioProcessingImpl::StartDebugRecording(
ivocd66b44d2016-01-15 03:06:36 -08001393 const char filename[AudioProcessing::kMaxFilenameSize],
1394 int64_t max_log_size_bytes) {
peahdf3efa82015-11-28 12:35:15 -08001395 // Run in a single-threaded manner.
1396 rtc::CritScope cs_render(&crit_render_);
1397 rtc::CritScope cs_capture(&crit_capture_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +02001398 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +00001399
peahdf3efa82015-11-28 12:35:15 -08001400 if (filename == nullptr) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001401 return kNullPointerError;
1402 }
1403
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001404#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ivocd66b44d2016-01-15 03:06:36 -08001405 debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes;
niklase@google.com470e71d2011-07-07 08:21:25 +00001406 // Stop any ongoing recording.
tommia6219cc2016-06-15 10:30:14 -07001407 debug_dump_.debug_file->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +00001408
tommia6219cc2016-06-15 10:30:14 -07001409 if (!debug_dump_.debug_file->OpenFile(filename, false)) {
niklase@google.com470e71d2011-07-07 08:21:25 +00001410 return kFileError;
1411 }
1412
Minyue13b96ba2015-10-03 00:39:14 +02001413 RETURN_ON_ERR(WriteConfigMessage(true));
1414 RETURN_ON_ERR(WriteInitMessage());
niklase@google.com470e71d2011-07-07 08:21:25 +00001415 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001416#else
1417 return kUnsupportedFunctionError;
1418#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001419}
1420
ivocd66b44d2016-01-15 03:06:36 -08001421int AudioProcessingImpl::StartDebugRecording(FILE* handle,
1422 int64_t max_log_size_bytes) {
peahdf3efa82015-11-28 12:35:15 -08001423 // Run in a single-threaded manner.
1424 rtc::CritScope cs_render(&crit_render_);
1425 rtc::CritScope cs_capture(&crit_capture_);
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001426
peahdf3efa82015-11-28 12:35:15 -08001427 if (handle == nullptr) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001428 return kNullPointerError;
1429 }
1430
1431#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ivocd66b44d2016-01-15 03:06:36 -08001432 debug_dump_.num_bytes_left_for_log_ = max_log_size_bytes;
1433
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001434 // Stop any ongoing recording.
tommia6219cc2016-06-15 10:30:14 -07001435 debug_dump_.debug_file->CloseFile();
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001436
tommia6219cc2016-06-15 10:30:14 -07001437 if (!debug_dump_.debug_file->OpenFromFileHandle(handle)) {
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001438 return kFileError;
1439 }
1440
Minyue13b96ba2015-10-03 00:39:14 +02001441 RETURN_ON_ERR(WriteConfigMessage(true));
1442 RETURN_ON_ERR(WriteInitMessage());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +00001443 return kNoError;
1444#else
1445 return kUnsupportedFunctionError;
1446#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
1447}
1448
peah73a28ee2016-10-12 03:01:49 -07001449int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
1450 return StartDebugRecording(handle, -1);
1451}
1452
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001453int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
1454 rtc::PlatformFile handle) {
peahdf3efa82015-11-28 12:35:15 -08001455 // Run in a single-threaded manner.
1456 rtc::CritScope cs_render(&crit_render_);
1457 rtc::CritScope cs_capture(&crit_capture_);
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001458 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
ivocd66b44d2016-01-15 03:06:36 -08001459 return StartDebugRecording(stream, -1);
xians@webrtc.orge46bc772014-10-10 08:36:56 +00001460}
1461
niklase@google.com470e71d2011-07-07 08:21:25 +00001462int AudioProcessingImpl::StopDebugRecording() {
peahdf3efa82015-11-28 12:35:15 -08001463 // Run in a single-threaded manner.
1464 rtc::CritScope cs_render(&crit_render_);
1465 rtc::CritScope cs_capture(&crit_capture_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001466
1467#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001468 // We just return if recording hasn't started.
tommia6219cc2016-06-15 10:30:14 -07001469 debug_dump_.debug_file->CloseFile();
niklase@google.com470e71d2011-07-07 08:21:25 +00001470 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001471#else
1472 return kUnsupportedFunctionError;
1473#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +00001474}
1475
1476EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
peahb624d8c2016-03-05 03:01:14 -08001477 return public_submodules_->echo_cancellation.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001478}
1479
1480EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
peahbb9edbd2016-03-10 12:54:25 -08001481 return public_submodules_->echo_control_mobile.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001482}
1483
1484GainControl* AudioProcessingImpl::gain_control() const {
peahbe615622016-02-13 16:40:47 -08001485 if (constants_.use_experimental_agc) {
1486 return public_submodules_->gain_control_for_experimental_agc.get();
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001487 }
peahbfa97112016-03-10 21:09:04 -08001488 return public_submodules_->gain_control.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001489}
1490
1491HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
solenberg70f99032015-12-08 11:07:32 -08001492 return public_submodules_->high_pass_filter.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001493}
1494
1495LevelEstimator* AudioProcessingImpl::level_estimator() const {
solenberg949028f2015-12-15 11:39:38 -08001496 return public_submodules_->level_estimator.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001497}
1498
1499NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
solenberg5e465c32015-12-08 13:22:33 -08001500 return public_submodules_->noise_suppression.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001501}
1502
1503VoiceDetection* AudioProcessingImpl::voice_detection() const {
solenberga29386c2015-12-16 03:31:12 -08001504 return public_submodules_->voice_detection.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00001505}
1506
peah2ace3f92016-09-10 04:42:27 -07001507bool AudioProcessingImpl::UpdateActiveSubmoduleStates() {
1508 return submodule_states_.Update(
1509 public_submodules_->high_pass_filter->is_enabled(),
1510 public_submodules_->echo_cancellation->is_enabled(),
1511 public_submodules_->echo_control_mobile->is_enabled(),
ivoc9f4a4a02016-10-28 05:39:16 -07001512 config_.residual_echo_detector.enabled,
peah2ace3f92016-09-10 04:42:27 -07001513 public_submodules_->noise_suppression->is_enabled(),
1514 capture_nonlocked_.intelligibility_enabled,
1515 capture_nonlocked_.beamformer_enabled,
1516 public_submodules_->gain_control->is_enabled(),
1517 capture_nonlocked_.level_controller_enabled,
1518 public_submodules_->voice_detection->is_enabled(),
1519 public_submodules_->level_estimator->is_enabled(),
1520 capture_.transient_suppressor_enabled);
ekmeyerson60d9b332015-08-14 10:35:55 -07001521}
1522
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001523
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001524void AudioProcessingImpl::InitializeTransient() {
peahdf3efa82015-11-28 12:35:15 -08001525 if (capture_.transient_suppressor_enabled) {
1526 if (!public_submodules_->transient_suppressor.get()) {
1527 public_submodules_->transient_suppressor.reset(new TransientSuppressor());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001528 }
peahdf3efa82015-11-28 12:35:15 -08001529 public_submodules_->transient_suppressor->Initialize(
peahde65ddc2016-09-16 15:02:15 -07001530 capture_nonlocked_.capture_processing_format.sample_rate_hz(),
1531 capture_nonlocked_.split_rate, num_proc_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001532 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001533}
1534
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001535void AudioProcessingImpl::InitializeBeamformer() {
aluebsb2328d12016-01-11 20:32:29 -08001536 if (capture_nonlocked_.beamformer_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001537 if (!private_submodules_->beamformer) {
1538 private_submodules_->beamformer.reset(new NonlinearBeamformer(
Alejandro Luebsf4022ff2016-07-01 17:19:09 -07001539 capture_.array_geometry, 1u, capture_.target_direction));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001540 }
peahdf3efa82015-11-28 12:35:15 -08001541 private_submodules_->beamformer->Initialize(kChunkSizeMs,
1542 capture_nonlocked_.split_rate);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001543 }
1544}
1545
ekmeyerson60d9b332015-08-14 10:35:55 -07001546void AudioProcessingImpl::InitializeIntelligibility() {
peah1bcfce52016-08-26 07:16:04 -07001547#if WEBRTC_INTELLIGIBILITY_ENHANCER
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001548 if (capture_nonlocked_.intelligibility_enabled) {
peahdf3efa82015-11-28 12:35:15 -08001549 public_submodules_->intelligibility_enhancer.reset(
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -08001550 new IntelligibilityEnhancer(capture_nonlocked_.split_rate,
Alex Luebs57ae8292016-03-09 16:24:34 +01001551 render_.render_audio->num_channels(),
Alejandro Luebsef009252016-09-20 14:51:56 -07001552 render_.render_audio->num_bands(),
Alex Luebs57ae8292016-03-09 16:24:34 +01001553 NoiseSuppressionImpl::num_noise_bins()));
ekmeyerson60d9b332015-08-14 10:35:55 -07001554 }
peah1bcfce52016-08-26 07:16:04 -07001555#endif
ekmeyerson60d9b332015-08-14 10:35:55 -07001556}
1557
peahca4cac72016-06-29 15:26:12 -07001558void AudioProcessingImpl::InitializeLevelController() {
1559 private_submodules_->level_controller->Initialize(proc_sample_rate_hz());
1560}
1561
ivoc9f4a4a02016-10-28 05:39:16 -07001562void AudioProcessingImpl::InitializeResidualEchoDetector() {
1563 private_submodules_->residual_echo_detector->Initialize();
1564}
1565
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001566void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001567 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001568
1569 if (echo_cancellation()->is_enabled()) {
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001570 // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
1571 // If a stream has echo we know that the echo_cancellation is in process.
peahdf3efa82015-11-28 12:35:15 -08001572 if (capture_.stream_delay_jumps == -1 &&
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001573 echo_cancellation()->stream_has_echo()) {
peahdf3efa82015-11-28 12:35:15 -08001574 capture_.stream_delay_jumps = 0;
1575 }
1576 if (capture_.aec_system_delay_jumps == -1 &&
1577 echo_cancellation()->stream_has_echo()) {
1578 capture_.aec_system_delay_jumps = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001579 }
1580
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001581 // Detect a jump in platform reported system delay and log the difference.
peahdf3efa82015-11-28 12:35:15 -08001582 const int diff_stream_delay_ms =
1583 capture_nonlocked_.stream_delay_ms - capture_.last_stream_delay_ms;
1584 if (diff_stream_delay_ms > kMinDiffDelayMs &&
1585 capture_.last_stream_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001586 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1587 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
peahdf3efa82015-11-28 12:35:15 -08001588 if (capture_.stream_delay_jumps == -1) {
1589 capture_.stream_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001590 }
peahdf3efa82015-11-28 12:35:15 -08001591 capture_.stream_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001592 }
peahdf3efa82015-11-28 12:35:15 -08001593 capture_.last_stream_delay_ms = capture_nonlocked_.stream_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001594
1595 // Detect a jump in AEC system delay and log the difference.
peah20028c42016-03-04 11:50:54 -08001596 const int samples_per_ms =
peahdf3efa82015-11-28 12:35:15 -08001597 rtc::CheckedDivExact(capture_nonlocked_.split_rate, 1000);
peah20028c42016-03-04 11:50:54 -08001598 RTC_DCHECK_LT(0, samples_per_ms);
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001599 const int aec_system_delay_ms =
peah20028c42016-03-04 11:50:54 -08001600 public_submodules_->echo_cancellation->GetSystemDelayInSamples() /
1601 samples_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001602 const int diff_aec_system_delay_ms =
peahdf3efa82015-11-28 12:35:15 -08001603 aec_system_delay_ms - capture_.last_aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001604 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
peahdf3efa82015-11-28 12:35:15 -08001605 capture_.last_aec_system_delay_ms != 0) {
asaperssona2c58e22016-03-07 01:52:59 -08001606 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1607 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1608 100);
peahdf3efa82015-11-28 12:35:15 -08001609 if (capture_.aec_system_delay_jumps == -1) {
1610 capture_.aec_system_delay_jumps = 0; // Activate counter if needed.
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001611 }
peahdf3efa82015-11-28 12:35:15 -08001612 capture_.aec_system_delay_jumps++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001613 }
peahdf3efa82015-11-28 12:35:15 -08001614 capture_.last_aec_system_delay_ms = aec_system_delay_ms;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001615 }
1616}
1617
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001618void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
peahdf3efa82015-11-28 12:35:15 -08001619 // Run in a single-threaded manner.
1620 rtc::CritScope cs_render(&crit_render_);
1621 rtc::CritScope cs_capture(&crit_capture_);
1622
1623 if (capture_.stream_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001624 RTC_HISTOGRAM_ENUMERATION(
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001625 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
peahdf3efa82015-11-28 12:35:15 -08001626 capture_.stream_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001627 }
peahdf3efa82015-11-28 12:35:15 -08001628 capture_.stream_delay_jumps = -1;
1629 capture_.last_stream_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001630
peahdf3efa82015-11-28 12:35:15 -08001631 if (capture_.aec_system_delay_jumps > -1) {
asaperssona2c58e22016-03-07 01:52:59 -08001632 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1633 capture_.aec_system_delay_jumps, 51);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001634 }
peahdf3efa82015-11-28 12:35:15 -08001635 capture_.aec_system_delay_jumps = -1;
1636 capture_.last_aec_system_delay_ms = 0;
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001637}
1638
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001639#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
peahdf3efa82015-11-28 12:35:15 -08001640int AudioProcessingImpl::WriteMessageToDebugFile(
1641 FileWrapper* debug_file,
ivocd66b44d2016-01-15 03:06:36 -08001642 int64_t* filesize_limit_bytes,
peahdf3efa82015-11-28 12:35:15 -08001643 rtc::CriticalSection* crit_debug,
1644 ApmDebugDumpThreadState* debug_state) {
1645 int32_t size = debug_state->event_msg->ByteSize();
ajm@google.com808e0e02011-08-03 21:08:51 +00001646 if (size <= 0) {
1647 return kUnspecifiedError;
1648 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001649#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Michael Graczyk86c6d332015-07-23 11:41:39 -07001650// TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1651// pretty safe in assuming little-endian.
ajm@google.com808e0e02011-08-03 21:08:51 +00001652#endif
1653
peahdf3efa82015-11-28 12:35:15 -08001654 if (!debug_state->event_msg->SerializeToString(&debug_state->event_str)) {
ajm@google.com808e0e02011-08-03 21:08:51 +00001655 return kUnspecifiedError;
1656 }
1657
peahdf3efa82015-11-28 12:35:15 -08001658 {
1659 // Ensure atomic writes of the message.
ivocd66b44d2016-01-15 03:06:36 -08001660 rtc::CritScope cs_debug(crit_debug);
1661
tommia6219cc2016-06-15 10:30:14 -07001662 RTC_DCHECK(debug_file->is_open());
ivocd66b44d2016-01-15 03:06:36 -08001663 // Update the byte counter.
1664 if (*filesize_limit_bytes >= 0) {
1665 *filesize_limit_bytes -=
1666 (sizeof(int32_t) + debug_state->event_str.length());
1667 if (*filesize_limit_bytes < 0) {
1668 // Not enough bytes are left to write this message, so stop logging.
1669 debug_file->CloseFile();
1670 return kNoError;
1671 }
1672 }
peahdf3efa82015-11-28 12:35:15 -08001673 // Write message preceded by its size.
1674 if (!debug_file->Write(&size, sizeof(int32_t))) {
1675 return kFileError;
1676 }
1677 if (!debug_file->Write(debug_state->event_str.data(),
1678 debug_state->event_str.length())) {
1679 return kFileError;
1680 }
ajm@google.com808e0e02011-08-03 21:08:51 +00001681 }
1682
peahdf3efa82015-11-28 12:35:15 -08001683 debug_state->event_msg->Clear();
ajm@google.com808e0e02011-08-03 21:08:51 +00001684
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001685 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001686}
1687
1688int AudioProcessingImpl::WriteInitMessage() {
peahdf3efa82015-11-28 12:35:15 -08001689 debug_dump_.capture.event_msg->set_type(audioproc::Event::INIT);
1690 audioproc::Init* msg = debug_dump_.capture.event_msg->mutable_init();
1691 msg->set_sample_rate(formats_.api_format.input_stream().sample_rate_hz());
ajm@google.com808e0e02011-08-03 21:08:51 +00001692
Peter Kasting69558702016-01-12 16:26:35 -08001693 msg->set_num_input_channels(static_cast<google::protobuf::int32>(
1694 formats_.api_format.input_stream().num_channels()));
1695 msg->set_num_output_channels(static_cast<google::protobuf::int32>(
1696 formats_.api_format.output_stream().num_channels()));
1697 msg->set_num_reverse_channels(static_cast<google::protobuf::int32>(
1698 formats_.api_format.reverse_input_stream().num_channels()));
peahdf3efa82015-11-28 12:35:15 -08001699 msg->set_reverse_sample_rate(
1700 formats_.api_format.reverse_input_stream().sample_rate_hz());
1701 msg->set_output_sample_rate(
1702 formats_.api_format.output_stream().sample_rate_hz());
peahc7bdf8a2016-04-11 07:05:53 -07001703 msg->set_reverse_output_sample_rate(
1704 formats_.api_format.reverse_output_stream().sample_rate_hz());
1705 msg->set_num_reverse_output_channels(
1706 formats_.api_format.reverse_output_stream().num_channels());
peahdf3efa82015-11-28 12:35:15 -08001707
1708 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001709 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001710 &crit_debug_, &debug_dump_.capture));
Minyue13b96ba2015-10-03 00:39:14 +02001711 return kNoError;
1712}
1713
1714int AudioProcessingImpl::WriteConfigMessage(bool forced) {
1715 audioproc::Config config;
1716
peahdf3efa82015-11-28 12:35:15 -08001717 config.set_aec_enabled(public_submodules_->echo_cancellation->is_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001718 config.set_aec_delay_agnostic_enabled(
peahdf3efa82015-11-28 12:35:15 -08001719 public_submodules_->echo_cancellation->is_delay_agnostic_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001720 config.set_aec_drift_compensation_enabled(
peahdf3efa82015-11-28 12:35:15 -08001721 public_submodules_->echo_cancellation->is_drift_compensation_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001722 config.set_aec_extended_filter_enabled(
peahdf3efa82015-11-28 12:35:15 -08001723 public_submodules_->echo_cancellation->is_extended_filter_enabled());
1724 config.set_aec_suppression_level(static_cast<int>(
1725 public_submodules_->echo_cancellation->suppression_level()));
Minyue13b96ba2015-10-03 00:39:14 +02001726
peahdf3efa82015-11-28 12:35:15 -08001727 config.set_aecm_enabled(
1728 public_submodules_->echo_control_mobile->is_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001729 config.set_aecm_comfort_noise_enabled(
peahdf3efa82015-11-28 12:35:15 -08001730 public_submodules_->echo_control_mobile->is_comfort_noise_enabled());
1731 config.set_aecm_routing_mode(static_cast<int>(
1732 public_submodules_->echo_control_mobile->routing_mode()));
Minyue13b96ba2015-10-03 00:39:14 +02001733
peahdf3efa82015-11-28 12:35:15 -08001734 config.set_agc_enabled(public_submodules_->gain_control->is_enabled());
1735 config.set_agc_mode(
1736 static_cast<int>(public_submodules_->gain_control->mode()));
1737 config.set_agc_limiter_enabled(
1738 public_submodules_->gain_control->is_limiter_enabled());
peahbe615622016-02-13 16:40:47 -08001739 config.set_noise_robust_agc_enabled(constants_.use_experimental_agc);
Minyue13b96ba2015-10-03 00:39:14 +02001740
peahdf3efa82015-11-28 12:35:15 -08001741 config.set_hpf_enabled(public_submodules_->high_pass_filter->is_enabled());
Minyue13b96ba2015-10-03 00:39:14 +02001742
peahdf3efa82015-11-28 12:35:15 -08001743 config.set_ns_enabled(public_submodules_->noise_suppression->is_enabled());
1744 config.set_ns_level(
1745 static_cast<int>(public_submodules_->noise_suppression->level()));
Minyue13b96ba2015-10-03 00:39:14 +02001746
peahdf3efa82015-11-28 12:35:15 -08001747 config.set_transient_suppression_enabled(
1748 capture_.transient_suppressor_enabled);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -07001749 config.set_intelligibility_enhancer_enabled(
1750 capture_nonlocked_.intelligibility_enabled);
Minyue13b96ba2015-10-03 00:39:14 +02001751
peah7789fe72016-04-15 01:19:44 -07001752 std::string experiments_description =
1753 public_submodules_->echo_cancellation->GetExperimentsDescription();
1754 // TODO(peah): Add semicolon-separated concatenations of experiment
1755 // descriptions for other submodules.
peahca4cac72016-06-29 15:26:12 -07001756 if (capture_nonlocked_.level_controller_enabled) {
1757 experiments_description += "LevelController;";
1758 }
peah7789fe72016-04-15 01:19:44 -07001759 config.set_experiments_description(experiments_description);
1760
Minyue13b96ba2015-10-03 00:39:14 +02001761 std::string serialized_config = config.SerializeAsString();
peahdf3efa82015-11-28 12:35:15 -08001762 if (!forced &&
1763 debug_dump_.capture.last_serialized_config == serialized_config) {
Minyue13b96ba2015-10-03 00:39:14 +02001764 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001765 }
1766
peahdf3efa82015-11-28 12:35:15 -08001767 debug_dump_.capture.last_serialized_config = serialized_config;
Minyue13b96ba2015-10-03 00:39:14 +02001768
peahdf3efa82015-11-28 12:35:15 -08001769 debug_dump_.capture.event_msg->set_type(audioproc::Event::CONFIG);
1770 debug_dump_.capture.event_msg->mutable_config()->CopyFrom(config);
Minyue13b96ba2015-10-03 00:39:14 +02001771
peahdf3efa82015-11-28 12:35:15 -08001772 RETURN_ON_ERR(WriteMessageToDebugFile(debug_dump_.debug_file.get(),
ivocd66b44d2016-01-15 03:06:36 -08001773 &debug_dump_.num_bytes_left_for_log_,
peahdf3efa82015-11-28 12:35:15 -08001774 &crit_debug_, &debug_dump_.capture));
ajm@google.com808e0e02011-08-03 21:08:51 +00001775 return kNoError;
1776}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001777#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001778
kwiberg83ffe452016-08-29 14:46:07 -07001779AudioProcessingImpl::ApmCaptureState::ApmCaptureState(
1780 bool transient_suppressor_enabled,
1781 const std::vector<Point>& array_geometry,
1782 SphericalPointf target_direction)
1783 : aec_system_delay_jumps(-1),
1784 delay_offset_ms(0),
1785 was_stream_delay_set(false),
1786 last_stream_delay_ms(0),
1787 last_aec_system_delay_ms(0),
1788 stream_delay_jumps(-1),
1789 output_will_be_muted(false),
1790 key_pressed(false),
1791 transient_suppressor_enabled(transient_suppressor_enabled),
1792 array_geometry(array_geometry),
1793 target_direction(target_direction),
peahde65ddc2016-09-16 15:02:15 -07001794 capture_processing_format(kSampleRate16kHz),
kwiberg83ffe452016-08-29 14:46:07 -07001795 split_rate(kSampleRate16kHz) {}
1796
1797AudioProcessingImpl::ApmCaptureState::~ApmCaptureState() = default;
1798
1799AudioProcessingImpl::ApmRenderState::ApmRenderState() = default;
1800
1801AudioProcessingImpl::ApmRenderState::~ApmRenderState() = default;
1802
niklase@google.com470e71d2011-07-07 08:21:25 +00001803} // namespace webrtc