blob: 310522465613faa942a8ec7f965e1c12d9f7b793 [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
ajm@google.com808e0e02011-08-03 21:08:51 +000013#include <assert.h>
Michael Graczyk86c6d332015-07-23 11:41:39 -070014#include <algorithm>
niklase@google.com470e71d2011-07-07 08:21:25 +000015
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020016#include "webrtc/base/checks.h"
xians@webrtc.orge46bc772014-10-10 08:36:56 +000017#include "webrtc/base/platform_file.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 +020022extern "C" {
23#include "webrtc/modules/audio_processing/aec/aec_core.h"
24}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000025#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000026#include "webrtc/modules/audio_processing/audio_buffer.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000027#include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000028#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000029#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000030#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
31#include "webrtc/modules/audio_processing/gain_control_impl.h"
32#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070033#include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000034#include "webrtc/modules/audio_processing/level_estimator_impl.h"
35#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
36#include "webrtc/modules/audio_processing/processing_component.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000037#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000038#include "webrtc/modules/audio_processing/voice_detection_impl.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010039#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010040#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
41#include "webrtc/system_wrappers/include/file_wrapper.h"
42#include "webrtc/system_wrappers/include/logging.h"
43#include "webrtc/system_wrappers/include/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000044
45#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
46// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000047#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000048#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000049#else
ajm@google.com808e0e02011-08-03 21:08:51 +000050#include "webrtc/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000051#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000052#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000053
Michael Graczyk86c6d332015-07-23 11:41:39 -070054#define RETURN_ON_ERR(expr) \
55 do { \
56 int err = (expr); \
57 if (err != kNoError) { \
58 return err; \
59 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000060 } while (0)
61
niklase@google.com470e71d2011-07-07 08:21:25 +000062namespace webrtc {
Michael Graczyk86c6d332015-07-23 11:41:39 -070063namespace {
64
65static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
66 switch (layout) {
67 case AudioProcessing::kMono:
68 case AudioProcessing::kStereo:
69 return false;
70 case AudioProcessing::kMonoAndKeyboard:
71 case AudioProcessing::kStereoAndKeyboard:
72 return true;
73 }
74
75 assert(false);
76 return false;
77}
78
79} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000080
81// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000082static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000083
pbos@webrtc.org788acd12014-12-15 09:41:24 +000084// This class has two main functionalities:
85//
86// 1) It is returned instead of the real GainControl after the new AGC has been
87// enabled in order to prevent an outside user from overriding compression
88// settings. It doesn't do anything in its implementation, except for
89// delegating the const methods and Enable calls to the real GainControl, so
90// AGC can still be disabled.
91//
92// 2) It is injected into AgcManagerDirect and implements volume callbacks for
93// getting and setting the volume level. It just caches this value to be used
94// in VoiceEngine later.
95class GainControlForNewAgc : public GainControl, public VolumeCallbacks {
96 public:
97 explicit GainControlForNewAgc(GainControlImpl* gain_control)
Michael Graczyk86c6d332015-07-23 11:41:39 -070098 : real_gain_control_(gain_control), volume_(0) {}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000099
100 // GainControl implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000101 int Enable(bool enable) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000102 return real_gain_control_->Enable(enable);
103 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000104 bool is_enabled() const override { return real_gain_control_->is_enabled(); }
105 int set_stream_analog_level(int level) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000106 volume_ = level;
107 return AudioProcessing::kNoError;
108 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000109 int stream_analog_level() override { return volume_; }
110 int set_mode(Mode mode) override { return AudioProcessing::kNoError; }
111 Mode mode() const override { return GainControl::kAdaptiveAnalog; }
112 int set_target_level_dbfs(int level) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000113 return AudioProcessing::kNoError;
114 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000115 int target_level_dbfs() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000116 return real_gain_control_->target_level_dbfs();
117 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000118 int set_compression_gain_db(int gain) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000119 return AudioProcessing::kNoError;
120 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000121 int compression_gain_db() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000122 return real_gain_control_->compression_gain_db();
123 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000124 int enable_limiter(bool enable) override { return AudioProcessing::kNoError; }
125 bool is_limiter_enabled() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000126 return real_gain_control_->is_limiter_enabled();
127 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000128 int set_analog_level_limits(int minimum, int maximum) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000129 return AudioProcessing::kNoError;
130 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000131 int analog_level_minimum() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000132 return real_gain_control_->analog_level_minimum();
133 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000134 int analog_level_maximum() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000135 return real_gain_control_->analog_level_maximum();
136 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000137 bool stream_is_saturated() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000138 return real_gain_control_->stream_is_saturated();
139 }
140
141 // VolumeCallbacks implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000142 void SetMicVolume(int volume) override { volume_ = volume; }
143 int GetMicVolume() override { return volume_; }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000144
145 private:
146 GainControl* real_gain_control_;
147 int volume_;
148};
149
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700150const int AudioProcessing::kNativeSampleRatesHz[] = {
151 AudioProcessing::kSampleRate8kHz,
152 AudioProcessing::kSampleRate16kHz,
153 AudioProcessing::kSampleRate32kHz,
154 AudioProcessing::kSampleRate48kHz};
155const size_t AudioProcessing::kNumNativeSampleRates =
156 arraysize(AudioProcessing::kNativeSampleRatesHz);
157const int AudioProcessing::kMaxNativeSampleRateHz = AudioProcessing::
158 kNativeSampleRatesHz[AudioProcessing::kNumNativeSampleRates - 1];
159const int AudioProcessing::kMaxAECMSampleRateHz = kSampleRate16kHz;
160
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000161AudioProcessing* AudioProcessing::Create() {
162 Config config;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000163 return Create(config, nullptr);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000164}
165
166AudioProcessing* AudioProcessing::Create(const Config& config) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000167 return Create(config, nullptr);
168}
169
170AudioProcessing* AudioProcessing::Create(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700171 Beamformer<float>* beamformer) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000172 AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000173 if (apm->Initialize() != kNoError) {
174 delete apm;
175 apm = NULL;
176 }
177
178 return apm;
179}
180
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000181AudioProcessingImpl::AudioProcessingImpl(const Config& config)
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000182 : AudioProcessingImpl(config, nullptr) {}
183
184AudioProcessingImpl::AudioProcessingImpl(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700185 Beamformer<float>* beamformer)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000186 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 echo_control_mobile_(NULL),
188 gain_control_(NULL),
189 high_pass_filter_(NULL),
190 level_estimator_(NULL),
191 noise_suppression_(NULL),
192 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000193 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000194#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
195 debug_file_(FileWrapper::Create()),
196 event_msg_(new audioproc::Event()),
197#endif
Michael Graczyk86c6d332015-07-23 11:41:39 -0700198 api_format_({{{kSampleRate16kHz, 1, false},
199 {kSampleRate16kHz, 1, false},
ekmeyerson60d9b332015-08-14 10:35:55 -0700200 {kSampleRate16kHz, 1, false},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700201 {kSampleRate16kHz, 1, false}}}),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000202 fwd_proc_format_(kSampleRate16kHz),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000203 rev_proc_format_(kSampleRate16kHz, 1),
204 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000206 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 was_stream_delay_set_(false),
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200208 last_stream_delay_ms_(0),
209 last_aec_system_delay_ms_(0),
Bjorn Volcker4e7aa432015-07-07 11:50:05 +0200210 stream_delay_jumps_(-1),
211 aec_system_delay_jumps_(-1),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +0000212 output_will_be_muted_(false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000213 key_pressed_(false),
214#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
215 use_new_agc_(false),
216#else
217 use_new_agc_(config.Get<ExperimentalAgc>().enabled),
218#endif
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200219 agc_startup_min_volume_(config.Get<ExperimentalAgc>().startup_min_volume),
andrew1c7075f2015-06-24 18:14:14 -0700220#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
221 transient_suppressor_enabled_(false),
222#else
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000223 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700224#endif
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000225 beamformer_enabled_(config.Get<Beamforming>().enabled),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000226 beamformer_(beamformer),
ekmeyerson60d9b332015-08-14 10:35:55 -0700227 array_geometry_(config.Get<Beamforming>().array_geometry),
Alejandro Luebscb3f9bd2015-10-29 18:21:34 -0700228 target_direction_(config.Get<Beamforming>().target_direction),
ekmeyerson60d9b332015-08-14 10:35:55 -0700229 intelligibility_enabled_(config.Get<Intelligibility>().enabled) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000230 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000231 component_list_.push_back(echo_cancellation_);
232
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000233 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000234 component_list_.push_back(echo_control_mobile_);
235
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000236 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000237 component_list_.push_back(gain_control_);
238
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000239 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000240 component_list_.push_back(high_pass_filter_);
241
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000242 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000243 component_list_.push_back(level_estimator_);
244
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000245 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000246 component_list_.push_back(noise_suppression_);
247
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000248 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000249 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000250
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000251 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
252
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000253 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000254}
255
256AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000257 {
258 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000259 // Depends on gain_control_ and gain_control_for_new_agc_.
260 agc_manager_.reset();
261 // Depends on gain_control_.
262 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000263 while (!component_list_.empty()) {
264 ProcessingComponent* component = component_list_.front();
265 component->Destroy();
266 delete component;
267 component_list_.pop_front();
268 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000269
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000270#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000271 if (debug_file_->Open()) {
272 debug_file_->CloseFile();
273 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000274#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000275 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000276 delete crit_;
277 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000278}
279
niklase@google.com470e71d2011-07-07 08:21:25 +0000280int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000281 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000282 return InitializeLocked();
283}
284
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000285int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
286 int output_sample_rate_hz,
287 int reverse_sample_rate_hz,
288 ChannelLayout input_layout,
289 ChannelLayout output_layout,
290 ChannelLayout reverse_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700291 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700292 {{input_sample_rate_hz,
293 ChannelsFromLayout(input_layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -0700294 LayoutHasKeyboard(input_layout)},
ekmeyerson60d9b332015-08-14 10:35:55 -0700295 {output_sample_rate_hz,
296 ChannelsFromLayout(output_layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -0700297 LayoutHasKeyboard(output_layout)},
ekmeyerson60d9b332015-08-14 10:35:55 -0700298 {reverse_sample_rate_hz,
299 ChannelsFromLayout(reverse_layout),
300 LayoutHasKeyboard(reverse_layout)},
301 {reverse_sample_rate_hz,
302 ChannelsFromLayout(reverse_layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -0700303 LayoutHasKeyboard(reverse_layout)}}};
304
305 return Initialize(processing_config);
306}
307
308int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000309 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700310 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000311}
312
niklase@google.com470e71d2011-07-07 08:21:25 +0000313int AudioProcessingImpl::InitializeLocked() {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700314 const int fwd_audio_buffer_channels =
315 beamformer_enabled_ ? api_format_.input_stream().num_channels()
316 : api_format_.output_stream().num_channels();
ekmeyerson60d9b332015-08-14 10:35:55 -0700317 const int rev_audio_buffer_out_num_frames =
318 api_format_.reverse_output_stream().num_frames() == 0
319 ? rev_proc_format_.num_frames()
320 : api_format_.reverse_output_stream().num_frames();
321 if (api_format_.reverse_input_stream().num_channels() > 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700322 render_audio_.reset(new AudioBuffer(
ekmeyerson60d9b332015-08-14 10:35:55 -0700323 api_format_.reverse_input_stream().num_frames(),
324 api_format_.reverse_input_stream().num_channels(),
Michael Graczyk86c6d332015-07-23 11:41:39 -0700325 rev_proc_format_.num_frames(), rev_proc_format_.num_channels(),
ekmeyerson60d9b332015-08-14 10:35:55 -0700326 rev_audio_buffer_out_num_frames));
327 if (rev_conversion_needed()) {
328 render_converter_ = AudioConverter::Create(
329 api_format_.reverse_input_stream().num_channels(),
330 api_format_.reverse_input_stream().num_frames(),
331 api_format_.reverse_output_stream().num_channels(),
332 api_format_.reverse_output_stream().num_frames());
333 } else {
334 render_converter_.reset(nullptr);
335 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700336 } else {
337 render_audio_.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700338 render_converter_.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700339 }
340 capture_audio_.reset(new AudioBuffer(
341 api_format_.input_stream().num_frames(),
342 api_format_.input_stream().num_channels(), fwd_proc_format_.num_frames(),
343 fwd_audio_buffer_channels, api_format_.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000344
niklase@google.com470e71d2011-07-07 08:21:25 +0000345 // Initialize all components.
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000346 for (auto item : component_list_) {
347 int err = item->Initialize();
niklase@google.com470e71d2011-07-07 08:21:25 +0000348 if (err != kNoError) {
349 return err;
350 }
351 }
352
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200353 InitializeExperimentalAgc();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000354
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200355 InitializeTransient();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000356
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000357 InitializeBeamformer();
358
ekmeyerson60d9b332015-08-14 10:35:55 -0700359 InitializeIntelligibility();
360
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000361#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000362 if (debug_file_->Open()) {
363 int err = WriteInitMessage();
364 if (err != kNoError) {
365 return err;
366 }
367 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000368#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000369
niklase@google.com470e71d2011-07-07 08:21:25 +0000370 return kNoError;
371}
372
Michael Graczyk86c6d332015-07-23 11:41:39 -0700373int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
374 for (const auto& stream : config.streams) {
375 if (stream.num_channels() < 0) {
376 return kBadNumberChannelsError;
377 }
378 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
379 return kBadSampleRateError;
380 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000381 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700382
383 const int num_in_channels = config.input_stream().num_channels();
384 const int num_out_channels = config.output_stream().num_channels();
385
386 // Need at least one input channel.
387 // Need either one output channel or as many outputs as there are inputs.
388 if (num_in_channels == 0 ||
389 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700390 return kBadNumberChannelsError;
391 }
392
Michael Graczyk86c6d332015-07-23 11:41:39 -0700393 if (beamformer_enabled_ &&
394 (static_cast<size_t>(num_in_channels) != array_geometry_.size() ||
395 num_out_channels > 1)) {
396 return kBadNumberChannelsError;
397 }
398
399 api_format_ = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000400
401 // We process at the closest native rate >= min(input rate, output rate)...
Michael Graczyk86c6d332015-07-23 11:41:39 -0700402 const int min_proc_rate =
403 std::min(api_format_.input_stream().sample_rate_hz(),
404 api_format_.output_stream().sample_rate_hz());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000405 int fwd_proc_rate;
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700406 for (size_t i = 0; i < kNumNativeSampleRates; ++i) {
407 fwd_proc_rate = kNativeSampleRatesHz[i];
408 if (fwd_proc_rate >= min_proc_rate) {
409 break;
410 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000411 }
412 // ...with one exception.
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700413 if (echo_control_mobile_->is_enabled() &&
414 min_proc_rate > kMaxAECMSampleRateHz) {
415 fwd_proc_rate = kMaxAECMSampleRateHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000416 }
417
Michael Graczyk86c6d332015-07-23 11:41:39 -0700418 fwd_proc_format_ = StreamConfig(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000419
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000420 // We normally process the reverse stream at 16 kHz. Unless...
421 int rev_proc_rate = kSampleRate16kHz;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700422 if (fwd_proc_format_.sample_rate_hz() == kSampleRate8kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000423 // ...the forward stream is at 8 kHz.
424 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000425 } else {
ekmeyerson60d9b332015-08-14 10:35:55 -0700426 if (api_format_.reverse_input_stream().sample_rate_hz() ==
427 kSampleRate32kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000428 // ...or the input is at 32 kHz, in which case we use the splitting
429 // filter rather than the resampler.
430 rev_proc_rate = kSampleRate32kHz;
431 }
432 }
433
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000434 // Always downmix the reverse stream to mono for analysis. This has been
435 // demonstrated to work well for AEC in most practical scenarios.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700436 rev_proc_format_ = StreamConfig(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000437
Michael Graczyk86c6d332015-07-23 11:41:39 -0700438 if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
439 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000440 split_rate_ = kSampleRate16kHz;
441 } else {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700442 split_rate_ = fwd_proc_format_.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000443 }
444
445 return InitializeLocked();
446}
447
448// Calls InitializeLocked() if any of the audio parameters have changed from
449// their current values.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700450int AudioProcessingImpl::MaybeInitializeLocked(
451 const ProcessingConfig& processing_config) {
452 if (processing_config == api_format_) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000453 return kNoError;
454 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700455 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000456}
457
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000458void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000459 CriticalSectionScoped crit_scoped(crit_);
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000460 for (auto item : component_list_) {
461 item->SetExtraOptions(config);
462 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000463
464 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
465 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
466 InitializeTransient();
467 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000468}
469
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000470
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000471int AudioProcessingImpl::proc_sample_rate_hz() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700472 return fwd_proc_format_.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000473}
474
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000475int AudioProcessingImpl::proc_split_sample_rate_hz() const {
476 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000477}
478
479int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000480 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000481}
482
483int AudioProcessingImpl::num_input_channels() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700484 return api_format_.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000485}
486
487int AudioProcessingImpl::num_output_channels() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700488 return api_format_.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000489}
490
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000491void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000492 CriticalSectionScoped lock(crit_);
Bjorn Volcker424694c2015-03-27 11:30:43 +0100493 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000494 if (agc_manager_.get()) {
495 agc_manager_->SetCaptureMuted(output_will_be_muted_);
496 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000497}
498
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000499
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000500int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700501 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000502 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000503 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000504 int output_sample_rate_hz,
505 ChannelLayout output_layout,
506 float* const* dest) {
Michael Graczyk4bc66fc2015-08-10 15:26:38 -0700507 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700508 StreamConfig input_stream = api_format_.input_stream();
509 input_stream.set_sample_rate_hz(input_sample_rate_hz);
510 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
511 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
512
513 StreamConfig output_stream = api_format_.output_stream();
514 output_stream.set_sample_rate_hz(output_sample_rate_hz);
515 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
516 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
517
518 if (samples_per_channel != input_stream.num_frames()) {
519 return kBadDataLengthError;
520 }
521 return ProcessStream(src, input_stream, output_stream, dest);
522}
523
524int AudioProcessingImpl::ProcessStream(const float* const* src,
525 const StreamConfig& input_config,
526 const StreamConfig& output_config,
527 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000528 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000529 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000530 return kNullPointerError;
531 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000532
peahfa6228e2015-11-16 16:27:42 -0800533 echo_cancellation_->ReadQueuedRenderData();
534 echo_control_mobile_->ReadQueuedRenderData();
peah4d291f72015-11-16 23:52:25 -0800535 gain_control_->ReadQueuedRenderData();
peahfa6228e2015-11-16 16:27:42 -0800536
Michael Graczyk86c6d332015-07-23 11:41:39 -0700537 ProcessingConfig processing_config = api_format_;
538 processing_config.input_stream() = input_config;
539 processing_config.output_stream() = output_config;
540
541 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
542 assert(processing_config.input_stream().num_frames() ==
543 api_format_.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000544
545#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
546 if (debug_file_->Open()) {
Minyue13b96ba2015-10-03 00:39:14 +0200547 RETURN_ON_ERR(WriteConfigMessage(false));
548
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000549 event_msg_->set_type(audioproc::Event::STREAM);
550 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000551 const size_t channel_size =
Michael Graczyk86c6d332015-07-23 11:41:39 -0700552 sizeof(float) * api_format_.input_stream().num_frames();
553 for (int i = 0; i < api_format_.input_stream().num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000554 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000555 }
556#endif
557
Michael Graczyk86c6d332015-07-23 11:41:39 -0700558 capture_audio_->CopyFrom(src, api_format_.input_stream());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000559 RETURN_ON_ERR(ProcessStreamLocked());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700560 capture_audio_->CopyTo(api_format_.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000561
562#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
563 if (debug_file_->Open()) {
564 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000565 const size_t channel_size =
Michael Graczyk86c6d332015-07-23 11:41:39 -0700566 sizeof(float) * api_format_.output_stream().num_frames();
567 for (int i = 0; i < api_format_.output_stream().num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000568 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000569 RETURN_ON_ERR(WriteMessageToDebugFile());
570 }
571#endif
572
573 return kNoError;
574}
575
576int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
577 CriticalSectionScoped crit_scoped(crit_);
peahfa6228e2015-11-16 16:27:42 -0800578 echo_cancellation_->ReadQueuedRenderData();
579 echo_control_mobile_->ReadQueuedRenderData();
peah4d291f72015-11-16 23:52:25 -0800580 gain_control_->ReadQueuedRenderData();
peahfa6228e2015-11-16 16:27:42 -0800581
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000582 if (!frame) {
583 return kNullPointerError;
584 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000585 // Must be a native rate.
586 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
587 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000588 frame->sample_rate_hz_ != kSampleRate32kHz &&
589 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000590 return kBadSampleRateError;
591 }
592 if (echo_control_mobile_->is_enabled() &&
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700593 frame->sample_rate_hz_ > kMaxAECMSampleRateHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000594 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
595 return kUnsupportedComponentError;
596 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000597
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000598 // TODO(ajm): The input and output rates and channels are currently
599 // constrained to be identical in the int16 interface.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700600 ProcessingConfig processing_config = api_format_;
601 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
602 processing_config.input_stream().set_num_channels(frame->num_channels_);
603 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
604 processing_config.output_stream().set_num_channels(frame->num_channels_);
605
606 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
607 if (frame->samples_per_channel_ != api_format_.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000608 return kBadDataLengthError;
609 }
610
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000611#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000612 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000613 event_msg_->set_type(audioproc::Event::STREAM);
614 audioproc::Stream* msg = event_msg_->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700615 const size_t data_size =
616 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000617 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000618 }
619#endif
620
621 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000622 RETURN_ON_ERR(ProcessStreamLocked());
623 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
624
625#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
626 if (debug_file_->Open()) {
627 audioproc::Stream* msg = event_msg_->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700628 const size_t data_size =
629 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000630 msg->set_output_data(frame->data_, data_size);
631 RETURN_ON_ERR(WriteMessageToDebugFile());
632 }
633#endif
634
635 return kNoError;
636}
637
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000638int AudioProcessingImpl::ProcessStreamLocked() {
639#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
640 if (debug_file_->Open()) {
641 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000642 msg->set_delay(stream_delay_ms_);
643 msg->set_drift(echo_cancellation_->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +0000644 msg->set_level(gain_control()->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000645 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000646 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000647#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000648
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200649 MaybeUpdateHistograms();
650
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000651 AudioBuffer* ca = capture_audio_.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -0700652
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000653 if (use_new_agc_ && gain_control_->is_enabled()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700654 agc_manager_->AnalyzePreProcess(ca->channels()[0], ca->num_channels(),
655 fwd_proc_format_.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000656 }
657
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000658 bool data_processed = is_data_processed();
659 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000660 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000661 }
662
ekmeyerson60d9b332015-08-14 10:35:55 -0700663 if (intelligibility_enabled_) {
664 intelligibility_enhancer_->AnalyzeCaptureAudio(
665 ca->split_channels_f(kBand0To8kHz), split_rate_, ca->num_channels());
666 }
667
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000668 if (beamformer_enabled_) {
Michael Graczykdfa36052015-03-25 16:37:27 -0700669 beamformer_->ProcessChunk(*ca->split_data_f(), ca->split_data_f());
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000670 ca->set_num_channels(1);
671 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000672
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000673 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
674 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000675 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000676 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000677
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000678 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000679 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000680 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000681 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
682 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
683 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000684
Michael Graczyk86c6d332015-07-23 11:41:39 -0700685 if (use_new_agc_ && gain_control_->is_enabled() &&
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000686 (!beamformer_enabled_ || beamformer_->is_target_present())) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000687 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
Michael Graczyk86c6d332015-07-23 11:41:39 -0700688 ca->num_frames_per_band(), split_rate_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000689 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000690 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000691
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000692 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000693 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000694 }
695
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000696 // TODO(aluebs): Investigate if the transient suppression placement should be
697 // before or after the AGC.
698 if (transient_suppressor_enabled_) {
699 float voice_probability =
700 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
701
Michael Graczyk86c6d332015-07-23 11:41:39 -0700702 transient_suppressor_->Suppress(
703 ca->channels_f()[0], ca->num_frames(), ca->num_channels(),
704 ca->split_bands_const_f(0)[kBand0To8kHz], ca->num_frames_per_band(),
705 ca->keyboard_data(), ca->num_keyboard_frames(), voice_probability,
706 key_pressed_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000707 }
708
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000709 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000710 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000711
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000712 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000713 return kNoError;
714}
715
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000716int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700717 size_t samples_per_channel,
ekmeyerson60d9b332015-08-14 10:35:55 -0700718 int rev_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000719 ChannelLayout layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700720 const StreamConfig reverse_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700721 rev_sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -0700722 };
723 if (samples_per_channel != reverse_config.num_frames()) {
724 return kBadDataLengthError;
725 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700726 return AnalyzeReverseStream(data, reverse_config, reverse_config);
727}
728
729int AudioProcessingImpl::ProcessReverseStream(
730 const float* const* src,
731 const StreamConfig& reverse_input_config,
732 const StreamConfig& reverse_output_config,
733 float* const* dest) {
734 RETURN_ON_ERR(
735 AnalyzeReverseStream(src, reverse_input_config, reverse_output_config));
736 if (is_rev_processed()) {
737 render_audio_->CopyTo(api_format_.reverse_output_stream(), dest);
738 } else if (rev_conversion_needed()) {
739 render_converter_->Convert(src, reverse_input_config.num_samples(), dest,
740 reverse_output_config.num_samples());
741 } else {
742 CopyAudioIfNeeded(src, reverse_input_config.num_frames(),
743 reverse_input_config.num_channels(), dest);
744 }
745
746 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700747}
748
749int AudioProcessingImpl::AnalyzeReverseStream(
ekmeyerson60d9b332015-08-14 10:35:55 -0700750 const float* const* src,
751 const StreamConfig& reverse_input_config,
752 const StreamConfig& reverse_output_config) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000753 CriticalSectionScoped crit_scoped(crit_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700754 if (src == NULL) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000755 return kNullPointerError;
756 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000757
ekmeyerson60d9b332015-08-14 10:35:55 -0700758 if (reverse_input_config.num_channels() <= 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700759 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000760 }
761
Michael Graczyk86c6d332015-07-23 11:41:39 -0700762 ProcessingConfig processing_config = api_format_;
ekmeyerson60d9b332015-08-14 10:35:55 -0700763 processing_config.reverse_input_stream() = reverse_input_config;
764 processing_config.reverse_output_stream() = reverse_output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700765
766 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
ekmeyerson60d9b332015-08-14 10:35:55 -0700767 assert(reverse_input_config.num_frames() ==
768 api_format_.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700769
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000770#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
771 if (debug_file_->Open()) {
772 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
773 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000774 const size_t channel_size =
ekmeyerson60d9b332015-08-14 10:35:55 -0700775 sizeof(float) * api_format_.reverse_input_stream().num_frames();
776 for (int i = 0; i < api_format_.reverse_input_stream().num_channels(); ++i)
777 msg->add_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000778 RETURN_ON_ERR(WriteMessageToDebugFile());
779 }
780#endif
781
ekmeyerson60d9b332015-08-14 10:35:55 -0700782 render_audio_->CopyFrom(src, api_format_.reverse_input_stream());
783 return ProcessReverseStreamLocked();
784}
785
786int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
787 RETURN_ON_ERR(AnalyzeReverseStream(frame));
788 if (is_rev_processed()) {
789 render_audio_->InterleaveTo(frame, true);
790 }
791
792 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000793}
794
niklase@google.com470e71d2011-07-07 08:21:25 +0000795int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000796 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000797 if (frame == NULL) {
798 return kNullPointerError;
799 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000800 // Must be a native rate.
801 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
802 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000803 frame->sample_rate_hz_ != kSampleRate32kHz &&
804 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000805 return kBadSampleRateError;
806 }
807 // This interface does not tolerate different forward and reverse rates.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700808 if (frame->sample_rate_hz_ != api_format_.input_stream().sample_rate_hz()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000809 return kBadSampleRateError;
810 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000811
Michael Graczyk86c6d332015-07-23 11:41:39 -0700812 if (frame->num_channels_ <= 0) {
813 return kBadNumberChannelsError;
814 }
815
816 ProcessingConfig processing_config = api_format_;
ekmeyerson60d9b332015-08-14 10:35:55 -0700817 processing_config.reverse_input_stream().set_sample_rate_hz(
818 frame->sample_rate_hz_);
819 processing_config.reverse_input_stream().set_num_channels(
820 frame->num_channels_);
821 processing_config.reverse_output_stream().set_sample_rate_hz(
822 frame->sample_rate_hz_);
823 processing_config.reverse_output_stream().set_num_channels(
824 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700825
826 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
827 if (frame->samples_per_channel_ !=
ekmeyerson60d9b332015-08-14 10:35:55 -0700828 api_format_.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000829 return kBadDataLengthError;
830 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000831
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000832#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000833 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000834 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
835 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700836 const size_t data_size =
837 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000838 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000839 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000840 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000841#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000842 render_audio_->DeinterleaveFrom(frame);
ekmeyerson60d9b332015-08-14 10:35:55 -0700843 return ProcessReverseStreamLocked();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000844}
niklase@google.com470e71d2011-07-07 08:21:25 +0000845
ekmeyerson60d9b332015-08-14 10:35:55 -0700846int AudioProcessingImpl::ProcessReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000847 AudioBuffer* ra = render_audio_.get(); // For brevity.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700848 if (rev_proc_format_.sample_rate_hz() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000849 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000850 }
851
ekmeyerson60d9b332015-08-14 10:35:55 -0700852 if (intelligibility_enabled_) {
853 intelligibility_enhancer_->ProcessRenderAudio(
854 ra->split_channels_f(kBand0To8kHz), split_rate_, ra->num_channels());
855 }
856
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000857 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
858 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000859 if (!use_new_agc_) {
860 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
861 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000862
ekmeyerson60d9b332015-08-14 10:35:55 -0700863 if (rev_proc_format_.sample_rate_hz() == kSampleRate32kHz &&
864 is_rev_processed()) {
865 ra->MergeFrequencyBands();
866 }
867
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000868 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000869}
870
871int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000872 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000873 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000874 delay += delay_offset_ms_;
875
niklase@google.com470e71d2011-07-07 08:21:25 +0000876 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000877 delay = 0;
878 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000879 }
880
881 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
882 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000883 delay = 500;
884 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000885 }
886
887 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000888 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000889}
890
891int AudioProcessingImpl::stream_delay_ms() const {
892 return stream_delay_ms_;
893}
894
895bool AudioProcessingImpl::was_stream_delay_set() const {
896 return was_stream_delay_set_;
897}
898
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000899void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
900 key_pressed_ = key_pressed;
901}
902
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000903void AudioProcessingImpl::set_delay_offset_ms(int offset) {
904 CriticalSectionScoped crit_scoped(crit_);
905 delay_offset_ms_ = offset;
906}
907
908int AudioProcessingImpl::delay_offset_ms() const {
909 return delay_offset_ms_;
910}
911
niklase@google.com470e71d2011-07-07 08:21:25 +0000912int AudioProcessingImpl::StartDebugRecording(
913 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000914 CriticalSectionScoped crit_scoped(crit_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +0200915 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +0000916
917 if (filename == NULL) {
918 return kNullPointerError;
919 }
920
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000921#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000922 // Stop any ongoing recording.
923 if (debug_file_->Open()) {
924 if (debug_file_->CloseFile() == -1) {
925 return kFileError;
926 }
927 }
928
929 if (debug_file_->OpenFile(filename, false) == -1) {
930 debug_file_->CloseFile();
931 return kFileError;
932 }
933
Minyue13b96ba2015-10-03 00:39:14 +0200934 RETURN_ON_ERR(WriteConfigMessage(true));
935 RETURN_ON_ERR(WriteInitMessage());
niklase@google.com470e71d2011-07-07 08:21:25 +0000936 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000937#else
938 return kUnsupportedFunctionError;
939#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000940}
941
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000942int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
943 CriticalSectionScoped crit_scoped(crit_);
944
945 if (handle == NULL) {
946 return kNullPointerError;
947 }
948
949#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
950 // Stop any ongoing recording.
951 if (debug_file_->Open()) {
952 if (debug_file_->CloseFile() == -1) {
953 return kFileError;
954 }
955 }
956
957 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
958 return kFileError;
959 }
960
Minyue13b96ba2015-10-03 00:39:14 +0200961 RETURN_ON_ERR(WriteConfigMessage(true));
962 RETURN_ON_ERR(WriteInitMessage());
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000963 return kNoError;
964#else
965 return kUnsupportedFunctionError;
966#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
967}
968
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000969int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
970 rtc::PlatformFile handle) {
971 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
972 return StartDebugRecording(stream);
973}
974
niklase@google.com470e71d2011-07-07 08:21:25 +0000975int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000976 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000977
978#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000979 // We just return if recording hasn't started.
980 if (debug_file_->Open()) {
981 if (debug_file_->CloseFile() == -1) {
982 return kFileError;
983 }
984 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000985 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000986#else
987 return kUnsupportedFunctionError;
988#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000989}
990
991EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
992 return echo_cancellation_;
993}
994
995EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
996 return echo_control_mobile_;
997}
998
999GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001000 if (use_new_agc_) {
1001 return gain_control_for_new_agc_.get();
1002 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001003 return gain_control_;
1004}
1005
1006HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
1007 return high_pass_filter_;
1008}
1009
1010LevelEstimator* AudioProcessingImpl::level_estimator() const {
1011 return level_estimator_;
1012}
1013
1014NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
1015 return noise_suppression_;
1016}
1017
1018VoiceDetection* AudioProcessingImpl::voice_detection() const {
1019 return voice_detection_;
1020}
1021
andrew@webrtc.org369166a2012-04-24 18:38:03 +00001022bool AudioProcessingImpl::is_data_processed() const {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001023 if (beamformer_enabled_) {
1024 return true;
1025 }
1026
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001027 int enabled_count = 0;
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +00001028 for (auto item : component_list_) {
1029 if (item->is_component_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001030 enabled_count++;
1031 }
1032 }
1033
1034 // Data is unchanged if no components are enabled, or if only level_estimator_
1035 // or voice_detection_ is enabled.
1036 if (enabled_count == 0) {
1037 return false;
1038 } else if (enabled_count == 1) {
1039 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
1040 return false;
1041 }
1042 } else if (enabled_count == 2) {
1043 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
1044 return false;
1045 }
1046 }
1047 return true;
1048}
1049
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001050bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +00001051 // Check if we've upmixed or downmixed the audio.
Michael Graczyk86c6d332015-07-23 11:41:39 -07001052 return ((api_format_.output_stream().num_channels() !=
1053 api_format_.input_stream().num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001054 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001055}
1056
andrew@webrtc.org369166a2012-04-24 18:38:03 +00001057bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001058 return (is_data_processed &&
1059 (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
1060 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +00001061}
1062
1063bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001064 if (!is_data_processed && !voice_detection_->is_enabled() &&
1065 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001066 // Only level_estimator_ is enabled.
1067 return false;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001068 } else if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
1069 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001070 // Something besides level_estimator_ is enabled, and we have super-wb.
1071 return true;
1072 }
1073 return false;
1074}
1075
ekmeyerson60d9b332015-08-14 10:35:55 -07001076bool AudioProcessingImpl::is_rev_processed() const {
1077 return intelligibility_enabled_ && intelligibility_enhancer_->active();
1078}
1079
1080bool AudioProcessingImpl::rev_conversion_needed() const {
1081 return (api_format_.reverse_input_stream() !=
1082 api_format_.reverse_output_stream());
1083}
1084
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001085void AudioProcessingImpl::InitializeExperimentalAgc() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001086 if (use_new_agc_) {
1087 if (!agc_manager_.get()) {
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001088 agc_manager_.reset(new AgcManagerDirect(gain_control_,
1089 gain_control_for_new_agc_.get(),
1090 agc_startup_min_volume_));
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001091 }
1092 agc_manager_->Initialize();
1093 agc_manager_->SetCaptureMuted(output_will_be_muted_);
1094 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001095}
1096
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001097void AudioProcessingImpl::InitializeTransient() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001098 if (transient_suppressor_enabled_) {
1099 if (!transient_suppressor_.get()) {
1100 transient_suppressor_.reset(new TransientSuppressor());
1101 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001102 transient_suppressor_->Initialize(
1103 fwd_proc_format_.sample_rate_hz(), split_rate_,
1104 api_format_.output_stream().num_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001105 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001106}
1107
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001108void AudioProcessingImpl::InitializeBeamformer() {
1109 if (beamformer_enabled_) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001110 if (!beamformer_) {
Alejandro Luebscb3f9bd2015-10-29 18:21:34 -07001111 beamformer_.reset(
1112 new NonlinearBeamformer(array_geometry_, target_direction_));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001113 }
1114 beamformer_->Initialize(kChunkSizeMs, split_rate_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001115 }
1116}
1117
ekmeyerson60d9b332015-08-14 10:35:55 -07001118void AudioProcessingImpl::InitializeIntelligibility() {
1119 if (intelligibility_enabled_) {
1120 IntelligibilityEnhancer::Config config;
1121 config.sample_rate_hz = split_rate_;
1122 config.num_capture_channels = capture_audio_->num_channels();
1123 config.num_render_channels = render_audio_->num_channels();
1124 intelligibility_enhancer_.reset(new IntelligibilityEnhancer(config));
1125 }
1126}
1127
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001128void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001129 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001130
1131 if (echo_cancellation()->is_enabled()) {
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001132 // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
1133 // If a stream has echo we know that the echo_cancellation is in process.
1134 if (stream_delay_jumps_ == -1 && echo_cancellation()->stream_has_echo()) {
1135 stream_delay_jumps_ = 0;
1136 }
1137 if (aec_system_delay_jumps_ == -1 &&
1138 echo_cancellation()->stream_has_echo()) {
1139 aec_system_delay_jumps_ = 0;
1140 }
1141
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001142 // Detect a jump in platform reported system delay and log the difference.
1143 const int diff_stream_delay_ms = stream_delay_ms_ - last_stream_delay_ms_;
1144 if (diff_stream_delay_ms > kMinDiffDelayMs && last_stream_delay_ms_ != 0) {
1145 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1146 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001147 if (stream_delay_jumps_ == -1) {
1148 stream_delay_jumps_ = 0; // Activate counter if needed.
1149 }
1150 stream_delay_jumps_++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001151 }
1152 last_stream_delay_ms_ = stream_delay_ms_;
1153
1154 // Detect a jump in AEC system delay and log the difference.
1155 const int frames_per_ms = rtc::CheckedDivExact(split_rate_, 1000);
1156 const int aec_system_delay_ms =
1157 WebRtcAec_system_delay(echo_cancellation()->aec_core()) / frames_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001158 const int diff_aec_system_delay_ms =
1159 aec_system_delay_ms - last_aec_system_delay_ms_;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001160 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
1161 last_aec_system_delay_ms_ != 0) {
1162 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1163 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1164 100);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001165 if (aec_system_delay_jumps_ == -1) {
1166 aec_system_delay_jumps_ = 0; // Activate counter if needed.
1167 }
1168 aec_system_delay_jumps_++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001169 }
1170 last_aec_system_delay_ms_ = aec_system_delay_ms;
1171 }
1172}
1173
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001174void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
1175 CriticalSectionScoped crit_scoped(crit_);
1176 if (stream_delay_jumps_ > -1) {
1177 RTC_HISTOGRAM_ENUMERATION(
1178 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
1179 stream_delay_jumps_, 51);
1180 }
1181 stream_delay_jumps_ = -1;
1182 last_stream_delay_ms_ = 0;
1183
1184 if (aec_system_delay_jumps_ > -1) {
1185 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1186 aec_system_delay_jumps_, 51);
1187 }
1188 aec_system_delay_jumps_ = -1;
1189 last_aec_system_delay_ms_ = 0;
1190}
1191
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001192#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +00001193int AudioProcessingImpl::WriteMessageToDebugFile() {
1194 int32_t size = event_msg_->ByteSize();
1195 if (size <= 0) {
1196 return kUnspecifiedError;
1197 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001198#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Michael Graczyk86c6d332015-07-23 11:41:39 -07001199// TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1200// pretty safe in assuming little-endian.
ajm@google.com808e0e02011-08-03 21:08:51 +00001201#endif
1202
1203 if (!event_msg_->SerializeToString(&event_str_)) {
1204 return kUnspecifiedError;
1205 }
1206
1207 // Write message preceded by its size.
1208 if (!debug_file_->Write(&size, sizeof(int32_t))) {
1209 return kFileError;
1210 }
1211 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
1212 return kFileError;
1213 }
1214
1215 event_msg_->Clear();
1216
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001217 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001218}
1219
1220int AudioProcessingImpl::WriteInitMessage() {
1221 event_msg_->set_type(audioproc::Event::INIT);
1222 audioproc::Init* msg = event_msg_->mutable_init();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001223 msg->set_sample_rate(api_format_.input_stream().sample_rate_hz());
1224 msg->set_num_input_channels(api_format_.input_stream().num_channels());
1225 msg->set_num_output_channels(api_format_.output_stream().num_channels());
ekmeyerson60d9b332015-08-14 10:35:55 -07001226 msg->set_num_reverse_channels(
1227 api_format_.reverse_input_stream().num_channels());
1228 msg->set_reverse_sample_rate(
1229 api_format_.reverse_input_stream().sample_rate_hz());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001230 msg->set_output_sample_rate(api_format_.output_stream().sample_rate_hz());
ekmeyerson60d9b332015-08-14 10:35:55 -07001231 // TODO(ekmeyerson): Add reverse output fields to event_msg_.
ajm@google.com808e0e02011-08-03 21:08:51 +00001232
Minyue13b96ba2015-10-03 00:39:14 +02001233 RETURN_ON_ERR(WriteMessageToDebugFile());
1234 return kNoError;
1235}
1236
1237int AudioProcessingImpl::WriteConfigMessage(bool forced) {
1238 audioproc::Config config;
1239
1240 config.set_aec_enabled(echo_cancellation_->is_enabled());
1241 config.set_aec_delay_agnostic_enabled(
1242 echo_cancellation_->is_delay_agnostic_enabled());
1243 config.set_aec_drift_compensation_enabled(
1244 echo_cancellation_->is_drift_compensation_enabled());
1245 config.set_aec_extended_filter_enabled(
1246 echo_cancellation_->is_extended_filter_enabled());
1247 config.set_aec_suppression_level(
1248 static_cast<int>(echo_cancellation_->suppression_level()));
1249
1250 config.set_aecm_enabled(echo_control_mobile_->is_enabled());
1251 config.set_aecm_comfort_noise_enabled(
1252 echo_control_mobile_->is_comfort_noise_enabled());
1253 config.set_aecm_routing_mode(
1254 static_cast<int>(echo_control_mobile_->routing_mode()));
1255
1256 config.set_agc_enabled(gain_control_->is_enabled());
1257 config.set_agc_mode(static_cast<int>(gain_control_->mode()));
1258 config.set_agc_limiter_enabled(gain_control_->is_limiter_enabled());
1259 config.set_noise_robust_agc_enabled(use_new_agc_);
1260
1261 config.set_hpf_enabled(high_pass_filter_->is_enabled());
1262
1263 config.set_ns_enabled(noise_suppression_->is_enabled());
1264 config.set_ns_level(static_cast<int>(noise_suppression_->level()));
1265
1266 config.set_transient_suppression_enabled(transient_suppressor_enabled_);
1267
1268 std::string serialized_config = config.SerializeAsString();
1269 if (!forced && last_serialized_config_ == serialized_config) {
1270 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001271 }
1272
Minyue13b96ba2015-10-03 00:39:14 +02001273 last_serialized_config_ = serialized_config;
1274
1275 event_msg_->set_type(audioproc::Event::CONFIG);
1276 event_msg_->mutable_config()->CopyFrom(config);
1277
1278 RETURN_ON_ERR(WriteMessageToDebugFile());
ajm@google.com808e0e02011-08-03 21:08:51 +00001279 return kNoError;
1280}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001281#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001282
niklase@google.com470e71d2011-07-07 08:21:25 +00001283} // namespace webrtc