blob: 086380e405f7e34b9d6638059e88adea86df340f [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>
niklase@google.com470e71d2011-07-07 08:21:25 +000014
xians@webrtc.orge46bc772014-10-10 08:36:56 +000015#include "webrtc/base/platform_file.h"
andrew@webrtc.org17e40642014-03-04 20:58:13 +000016#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000017#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000018#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000019#include "webrtc/modules/audio_processing/audio_buffer.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000020#include "webrtc/modules/audio_processing/beamformer/beamformer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000021#include "webrtc/modules/audio_processing/channel_buffer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000022#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000023#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000024#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
25#include "webrtc/modules/audio_processing/gain_control_impl.h"
26#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
27#include "webrtc/modules/audio_processing/level_estimator_impl.h"
28#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
29#include "webrtc/modules/audio_processing/processing_component.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000030#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000031#include "webrtc/modules/audio_processing/voice_detection_impl.h"
32#include "webrtc/modules/interface/module_common_types.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000033#include "webrtc/system_wrappers/interface/compile_assert.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000034#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
35#include "webrtc/system_wrappers/interface/file_wrapper.h"
36#include "webrtc/system_wrappers/interface/logging.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000037
38#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
39// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000040#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000041#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000042#else
ajm@google.com808e0e02011-08-03 21:08:51 +000043#include "webrtc/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000044#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000045#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000046
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000047#define RETURN_ON_ERR(expr) \
48 do { \
49 int err = expr; \
50 if (err != kNoError) { \
51 return err; \
52 } \
53 } while (0)
54
niklase@google.com470e71d2011-07-07 08:21:25 +000055namespace webrtc {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000056
57// Throughout webrtc, it's assumed that success is represented by zero.
58COMPILE_ASSERT(AudioProcessing::kNoError == 0, no_error_must_be_zero);
59
pbos@webrtc.org788acd12014-12-15 09:41:24 +000060// This class has two main functionalities:
61//
62// 1) It is returned instead of the real GainControl after the new AGC has been
63// enabled in order to prevent an outside user from overriding compression
64// settings. It doesn't do anything in its implementation, except for
65// delegating the const methods and Enable calls to the real GainControl, so
66// AGC can still be disabled.
67//
68// 2) It is injected into AgcManagerDirect and implements volume callbacks for
69// getting and setting the volume level. It just caches this value to be used
70// in VoiceEngine later.
71class GainControlForNewAgc : public GainControl, public VolumeCallbacks {
72 public:
73 explicit GainControlForNewAgc(GainControlImpl* gain_control)
74 : real_gain_control_(gain_control),
75 volume_(0) {
76 }
77
78 // GainControl implementation.
79 virtual int Enable(bool enable) OVERRIDE {
80 return real_gain_control_->Enable(enable);
81 }
82 virtual bool is_enabled() const OVERRIDE {
83 return real_gain_control_->is_enabled();
84 }
85 virtual int set_stream_analog_level(int level) OVERRIDE {
86 volume_ = level;
87 return AudioProcessing::kNoError;
88 }
89 virtual int stream_analog_level() OVERRIDE {
90 return volume_;
91 }
92 virtual int set_mode(Mode mode) OVERRIDE { return AudioProcessing::kNoError; }
93 virtual Mode mode() const OVERRIDE { return GainControl::kAdaptiveAnalog; }
94 virtual int set_target_level_dbfs(int level) OVERRIDE {
95 return AudioProcessing::kNoError;
96 }
97 virtual int target_level_dbfs() const OVERRIDE {
98 return real_gain_control_->target_level_dbfs();
99 }
100 virtual int set_compression_gain_db(int gain) OVERRIDE {
101 return AudioProcessing::kNoError;
102 }
103 virtual int compression_gain_db() const OVERRIDE {
104 return real_gain_control_->compression_gain_db();
105 }
106 virtual int enable_limiter(bool enable) OVERRIDE {
107 return AudioProcessing::kNoError;
108 }
109 virtual bool is_limiter_enabled() const OVERRIDE {
110 return real_gain_control_->is_limiter_enabled();
111 }
112 virtual int set_analog_level_limits(int minimum,
113 int maximum) OVERRIDE {
114 return AudioProcessing::kNoError;
115 }
116 virtual int analog_level_minimum() const OVERRIDE {
117 return real_gain_control_->analog_level_minimum();
118 }
119 virtual int analog_level_maximum() const OVERRIDE {
120 return real_gain_control_->analog_level_maximum();
121 }
122 virtual bool stream_is_saturated() const OVERRIDE {
123 return real_gain_control_->stream_is_saturated();
124 }
125
126 // VolumeCallbacks implementation.
127 virtual void SetMicVolume(int volume) OVERRIDE {
128 volume_ = volume;
129 }
130 virtual int GetMicVolume() OVERRIDE {
131 return volume_;
132 }
133
134 private:
135 GainControl* real_gain_control_;
136 int volume_;
137};
138
niklase@google.com470e71d2011-07-07 08:21:25 +0000139AudioProcessing* AudioProcessing::Create(int id) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000140 return Create();
141}
142
143AudioProcessing* AudioProcessing::Create() {
144 Config config;
145 return Create(config);
146}
147
148AudioProcessing* AudioProcessing::Create(const Config& config) {
149 AudioProcessingImpl* apm = new AudioProcessingImpl(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000150 if (apm->Initialize() != kNoError) {
151 delete apm;
152 apm = NULL;
153 }
154
155 return apm;
156}
157
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000158AudioProcessingImpl::AudioProcessingImpl(const Config& config)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000159 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000160 echo_control_mobile_(NULL),
161 gain_control_(NULL),
162 high_pass_filter_(NULL),
163 level_estimator_(NULL),
164 noise_suppression_(NULL),
165 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000166 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000167#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
168 debug_file_(FileWrapper::Create()),
169 event_msg_(new audioproc::Event()),
170#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000171 fwd_in_format_(kSampleRate16kHz, 1),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000172 fwd_proc_format_(kSampleRate16kHz),
173 fwd_out_format_(kSampleRate16kHz, 1),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000174 rev_in_format_(kSampleRate16kHz, 1),
175 rev_proc_format_(kSampleRate16kHz, 1),
176 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +0000177 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000178 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000179 was_stream_delay_set_(false),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +0000180 output_will_be_muted_(false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000181 key_pressed_(false),
182#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
183 use_new_agc_(false),
184#else
185 use_new_agc_(config.Get<ExperimentalAgc>().enabled),
186#endif
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000187 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled),
188 beamformer_enabled_(config.Get<Beamforming>().enabled) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000189 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000190 component_list_.push_back(echo_cancellation_);
191
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000192 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000193 component_list_.push_back(echo_control_mobile_);
194
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000195 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000196 component_list_.push_back(gain_control_);
197
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000198 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199 component_list_.push_back(high_pass_filter_);
200
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000201 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 component_list_.push_back(level_estimator_);
203
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000204 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 component_list_.push_back(noise_suppression_);
206
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000207 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000208 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000209
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000210 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
211
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000212 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000213}
214
215AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000216 {
217 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000218 // Depends on gain_control_ and gain_control_for_new_agc_.
219 agc_manager_.reset();
220 // Depends on gain_control_.
221 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000222 while (!component_list_.empty()) {
223 ProcessingComponent* component = component_list_.front();
224 component->Destroy();
225 delete component;
226 component_list_.pop_front();
227 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000229#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000230 if (debug_file_->Open()) {
231 debug_file_->CloseFile();
232 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000233#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000234 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000235 delete crit_;
236 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000237}
238
niklase@google.com470e71d2011-07-07 08:21:25 +0000239int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000240 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000241 return InitializeLocked();
242}
243
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000244int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000245 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000246 return InitializeLocked(rate,
247 rate,
248 rev_in_format_.rate(),
249 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000250 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000251 rev_in_format_.num_channels());
252}
253
254int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
255 int output_sample_rate_hz,
256 int reverse_sample_rate_hz,
257 ChannelLayout input_layout,
258 ChannelLayout output_layout,
259 ChannelLayout reverse_layout) {
260 CriticalSectionScoped crit_scoped(crit_);
261 return InitializeLocked(input_sample_rate_hz,
262 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000263 reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000264 ChannelsFromLayout(input_layout),
265 ChannelsFromLayout(output_layout),
266 ChannelsFromLayout(reverse_layout));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000267}
268
niklase@google.com470e71d2011-07-07 08:21:25 +0000269int AudioProcessingImpl::InitializeLocked() {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000270 const int fwd_audio_buffer_channels = beamformer_enabled_ ?
271 fwd_in_format_.num_channels() :
272 fwd_out_format_.num_channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000273 render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(),
274 rev_in_format_.num_channels(),
275 rev_proc_format_.samples_per_channel(),
276 rev_proc_format_.num_channels(),
277 rev_proc_format_.samples_per_channel()));
278 capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
279 fwd_in_format_.num_channels(),
280 fwd_proc_format_.samples_per_channel(),
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000281 fwd_audio_buffer_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000282 fwd_out_format_.samples_per_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000283
niklase@google.com470e71d2011-07-07 08:21:25 +0000284 // Initialize all components.
285 std::list<ProcessingComponent*>::iterator it;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000286 for (it = component_list_.begin(); it != component_list_.end(); ++it) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000287 int err = (*it)->Initialize();
288 if (err != kNoError) {
289 return err;
290 }
291 }
292
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000293 int err = InitializeExperimentalAgc();
294 if (err != kNoError) {
295 return err;
296 }
297
298 err = InitializeTransient();
299 if (err != kNoError) {
300 return err;
301 }
302
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000303 InitializeBeamformer();
304
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000305#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000306 if (debug_file_->Open()) {
307 int err = WriteInitMessage();
308 if (err != kNoError) {
309 return err;
310 }
311 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000312#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000313
niklase@google.com470e71d2011-07-07 08:21:25 +0000314 return kNoError;
315}
316
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000317int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz,
318 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000319 int reverse_sample_rate_hz,
320 int num_input_channels,
321 int num_output_channels,
322 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000323 if (input_sample_rate_hz <= 0 ||
324 output_sample_rate_hz <= 0 ||
325 reverse_sample_rate_hz <= 0) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000326 return kBadSampleRateError;
327 }
328 if (num_output_channels > num_input_channels) {
329 return kBadNumberChannelsError;
330 }
331 // Only mono and stereo supported currently.
332 if (num_input_channels > 2 || num_input_channels < 1 ||
333 num_output_channels > 2 || num_output_channels < 1 ||
334 num_reverse_channels > 2 || num_reverse_channels < 1) {
335 return kBadNumberChannelsError;
336 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000337
338 fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000339 fwd_out_format_.set(output_sample_rate_hz, num_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000340 rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
341
342 // We process at the closest native rate >= min(input rate, output rate)...
343 int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate());
344 int fwd_proc_rate;
345 if (min_proc_rate > kSampleRate16kHz) {
346 fwd_proc_rate = kSampleRate32kHz;
347 } else if (min_proc_rate > kSampleRate8kHz) {
348 fwd_proc_rate = kSampleRate16kHz;
349 } else {
350 fwd_proc_rate = kSampleRate8kHz;
351 }
352 // ...with one exception.
353 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
354 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000355 }
356
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000357 fwd_proc_format_.set(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000358
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000359 // We normally process the reverse stream at 16 kHz. Unless...
360 int rev_proc_rate = kSampleRate16kHz;
361 if (fwd_proc_format_.rate() == kSampleRate8kHz) {
362 // ...the forward stream is at 8 kHz.
363 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000364 } else {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000365 if (rev_in_format_.rate() == kSampleRate32kHz) {
366 // ...or the input is at 32 kHz, in which case we use the splitting
367 // filter rather than the resampler.
368 rev_proc_rate = kSampleRate32kHz;
369 }
370 }
371
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000372 // Always downmix the reverse stream to mono for analysis. This has been
373 // demonstrated to work well for AEC in most practical scenarios.
374 rev_proc_format_.set(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000375
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000376 if (fwd_proc_format_.rate() == kSampleRate32kHz ||
377 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000378 split_rate_ = kSampleRate16kHz;
379 } else {
380 split_rate_ = fwd_proc_format_.rate();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000381 }
382
383 return InitializeLocked();
384}
385
386// Calls InitializeLocked() if any of the audio parameters have changed from
387// their current values.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000388int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz,
389 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000390 int reverse_sample_rate_hz,
391 int num_input_channels,
392 int num_output_channels,
393 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000394 if (input_sample_rate_hz == fwd_in_format_.rate() &&
395 output_sample_rate_hz == fwd_out_format_.rate() &&
396 reverse_sample_rate_hz == rev_in_format_.rate() &&
397 num_input_channels == fwd_in_format_.num_channels() &&
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000398 num_output_channels == fwd_out_format_.num_channels() &&
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000399 num_reverse_channels == rev_in_format_.num_channels()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000400 return kNoError;
401 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000402 if (beamformer_enabled_ &&
403 (num_input_channels < 2 || num_output_channels > 1)) {
404 return kBadNumberChannelsError;
405 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000406 return InitializeLocked(input_sample_rate_hz,
407 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000408 reverse_sample_rate_hz,
409 num_input_channels,
410 num_output_channels,
411 num_reverse_channels);
412}
413
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000414void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000415 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000416 std::list<ProcessingComponent*>::iterator it;
417 for (it = component_list_.begin(); it != component_list_.end(); ++it)
418 (*it)->SetExtraOptions(config);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000419
420 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
421 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
422 InitializeTransient();
423 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000424}
425
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000426int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000427 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000428 return fwd_in_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000429}
430
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000431int AudioProcessingImpl::sample_rate_hz() const {
432 CriticalSectionScoped crit_scoped(crit_);
433 return fwd_in_format_.rate();
434}
435
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000436int AudioProcessingImpl::proc_sample_rate_hz() const {
437 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000438}
439
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000440int AudioProcessingImpl::proc_split_sample_rate_hz() const {
441 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000442}
443
444int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000445 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000446}
447
448int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000449 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000450}
451
452int AudioProcessingImpl::num_output_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000453 return fwd_out_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000454}
455
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000456void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
457 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000458 CriticalSectionScoped lock(crit_);
459 if (agc_manager_.get()) {
460 agc_manager_->SetCaptureMuted(output_will_be_muted_);
461 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000462}
463
464bool AudioProcessingImpl::output_will_be_muted() const {
465 return output_will_be_muted_;
466}
467
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000468int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000469 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000470 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000471 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000472 int output_sample_rate_hz,
473 ChannelLayout output_layout,
474 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000475 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000476 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000477 return kNullPointerError;
478 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000479
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000480 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
481 output_sample_rate_hz,
482 rev_in_format_.rate(),
483 ChannelsFromLayout(input_layout),
484 ChannelsFromLayout(output_layout),
485 rev_in_format_.num_channels()));
486 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000487 return kBadDataLengthError;
488 }
489
490#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
491 if (debug_file_->Open()) {
492 event_msg_->set_type(audioproc::Event::STREAM);
493 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000494 const size_t channel_size =
495 sizeof(float) * fwd_in_format_.samples_per_channel();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000496 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
497 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000498 }
499#endif
500
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000501 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000502 RETURN_ON_ERR(ProcessStreamLocked());
503 if (output_copy_needed(is_data_processed())) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000504 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
505 output_layout,
506 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000507 }
508
509#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
510 if (debug_file_->Open()) {
511 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000512 const size_t channel_size =
513 sizeof(float) * fwd_out_format_.samples_per_channel();
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000514 for (int i = 0; i < fwd_out_format_.num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000515 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000516 RETURN_ON_ERR(WriteMessageToDebugFile());
517 }
518#endif
519
520 return kNoError;
521}
522
523int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
524 CriticalSectionScoped crit_scoped(crit_);
525 if (!frame) {
526 return kNullPointerError;
527 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000528 // Must be a native rate.
529 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
530 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000531 frame->sample_rate_hz_ != kSampleRate32kHz &&
532 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000533 return kBadSampleRateError;
534 }
535 if (echo_control_mobile_->is_enabled() &&
536 frame->sample_rate_hz_ > kSampleRate16kHz) {
537 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
538 return kUnsupportedComponentError;
539 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000540
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000541 // TODO(ajm): The input and output rates and channels are currently
542 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000543 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000544 frame->sample_rate_hz_,
545 rev_in_format_.rate(),
546 frame->num_channels_,
547 frame->num_channels_,
548 rev_in_format_.num_channels()));
549 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000550 return kBadDataLengthError;
551 }
552
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000553#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000554 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000555 event_msg_->set_type(audioproc::Event::STREAM);
556 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000557 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000558 frame->samples_per_channel_ *
559 frame->num_channels_;
560 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000561 }
562#endif
563
564 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000565 RETURN_ON_ERR(ProcessStreamLocked());
566 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
567
568#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
569 if (debug_file_->Open()) {
570 audioproc::Stream* msg = event_msg_->mutable_stream();
571 const size_t data_size = sizeof(int16_t) *
572 frame->samples_per_channel_ *
573 frame->num_channels_;
574 msg->set_output_data(frame->data_, data_size);
575 RETURN_ON_ERR(WriteMessageToDebugFile());
576 }
577#endif
578
579 return kNoError;
580}
581
582
583int AudioProcessingImpl::ProcessStreamLocked() {
584#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
585 if (debug_file_->Open()) {
586 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000587 msg->set_delay(stream_delay_ms_);
588 msg->set_drift(echo_cancellation_->stream_drift_samples());
589 msg->set_level(gain_control_->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000590 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000591 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000592#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000593
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000594 AudioBuffer* ca = capture_audio_.get(); // For brevity.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000595 if (use_new_agc_ && gain_control_->is_enabled()) {
596 agc_manager_->AnalyzePreProcess(ca->data(0),
597 ca->num_channels(),
598 fwd_proc_format_.samples_per_channel());
599 }
600
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000601 bool data_processed = is_data_processed();
602 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000603 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000604 }
605
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000606#ifdef WEBRTC_BEAMFORMER
607 if (beamformer_enabled_) {
608 beamformer_->ProcessChunk(ca->split_channels_const_f(kBand0To8kHz),
609 ca->split_channels_const_f(kBand8To16kHz),
610 ca->num_channels(),
611 ca->samples_per_split_channel(),
612 ca->split_channels_f(kBand0To8kHz),
613 ca->split_channels_f(kBand8To16kHz));
614 ca->set_num_channels(1);
615 }
616#endif
617
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000618 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
619 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000620 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000621 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000622
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000623 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000624 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000625 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000626 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
627 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
628 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000629
630 if (use_new_agc_ && gain_control_->is_enabled()) {
631 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
632 ca->samples_per_split_channel(),
633 split_rate_);
634 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000635 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000636
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000637 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000638 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000639 }
640
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000641 // TODO(aluebs): Investigate if the transient suppression placement should be
642 // before or after the AGC.
643 if (transient_suppressor_enabled_) {
644 float voice_probability =
645 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
646
647 transient_suppressor_->Suppress(ca->data_f(0),
648 ca->samples_per_channel(),
649 ca->num_channels(),
650 ca->split_bands_const_f(0)[kBand0To8kHz],
651 ca->samples_per_split_channel(),
652 ca->keyboard_data(),
653 ca->samples_per_keyboard_channel(),
654 voice_probability,
655 key_pressed_);
656 }
657
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000658 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000659 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000660
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000661 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000662 return kNoError;
663}
664
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000665int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
666 int samples_per_channel,
667 int sample_rate_hz,
668 ChannelLayout layout) {
669 CriticalSectionScoped crit_scoped(crit_);
670 if (data == NULL) {
671 return kNullPointerError;
672 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000673
674 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000675 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
676 fwd_out_format_.rate(),
677 sample_rate_hz,
678 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000679 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000680 num_channels));
681 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000682 return kBadDataLengthError;
683 }
684
685#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
686 if (debug_file_->Open()) {
687 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
688 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000689 const size_t channel_size =
690 sizeof(float) * rev_in_format_.samples_per_channel();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000691 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000692 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000693 RETURN_ON_ERR(WriteMessageToDebugFile());
694 }
695#endif
696
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000697 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000698 return AnalyzeReverseStreamLocked();
699}
700
niklase@google.com470e71d2011-07-07 08:21:25 +0000701int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000702 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000703 if (frame == NULL) {
704 return kNullPointerError;
705 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000706 // Must be a native rate.
707 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
708 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000709 frame->sample_rate_hz_ != kSampleRate32kHz &&
710 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000711 return kBadSampleRateError;
712 }
713 // This interface does not tolerate different forward and reverse rates.
714 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000715 return kBadSampleRateError;
716 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000717
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000718 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
719 fwd_out_format_.rate(),
720 frame->sample_rate_hz_,
721 fwd_in_format_.num_channels(),
722 fwd_in_format_.num_channels(),
723 frame->num_channels_));
724 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000725 return kBadDataLengthError;
726 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000727
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000728#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000729 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000730 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
731 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000732 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000733 frame->samples_per_channel_ *
734 frame->num_channels_;
735 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000736 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000737 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000738#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000739
740 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000741 return AnalyzeReverseStreamLocked();
742}
niklase@google.com470e71d2011-07-07 08:21:25 +0000743
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000744int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000745 AudioBuffer* ra = render_audio_.get(); // For brevity.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000746 if (rev_proc_format_.rate() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000747 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 }
749
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000750 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
751 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000752 if (!use_new_agc_) {
753 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
754 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000755
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000756 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000757}
758
759int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000760 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000761 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000762 delay += delay_offset_ms_;
763
niklase@google.com470e71d2011-07-07 08:21:25 +0000764 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000765 delay = 0;
766 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000767 }
768
769 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
770 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000771 delay = 500;
772 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000773 }
774
775 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000776 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000777}
778
779int AudioProcessingImpl::stream_delay_ms() const {
780 return stream_delay_ms_;
781}
782
783bool AudioProcessingImpl::was_stream_delay_set() const {
784 return was_stream_delay_set_;
785}
786
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000787void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
788 key_pressed_ = key_pressed;
789}
790
791bool AudioProcessingImpl::stream_key_pressed() const {
792 return key_pressed_;
793}
794
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000795void AudioProcessingImpl::set_delay_offset_ms(int offset) {
796 CriticalSectionScoped crit_scoped(crit_);
797 delay_offset_ms_ = offset;
798}
799
800int AudioProcessingImpl::delay_offset_ms() const {
801 return delay_offset_ms_;
802}
803
niklase@google.com470e71d2011-07-07 08:21:25 +0000804int AudioProcessingImpl::StartDebugRecording(
805 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000806 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000807 assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize);
808
809 if (filename == NULL) {
810 return kNullPointerError;
811 }
812
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000813#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000814 // Stop any ongoing recording.
815 if (debug_file_->Open()) {
816 if (debug_file_->CloseFile() == -1) {
817 return kFileError;
818 }
819 }
820
821 if (debug_file_->OpenFile(filename, false) == -1) {
822 debug_file_->CloseFile();
823 return kFileError;
824 }
825
ajm@google.com808e0e02011-08-03 21:08:51 +0000826 int err = WriteInitMessage();
827 if (err != kNoError) {
828 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000829 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000830 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000831#else
832 return kUnsupportedFunctionError;
833#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000834}
835
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000836int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
837 CriticalSectionScoped crit_scoped(crit_);
838
839 if (handle == NULL) {
840 return kNullPointerError;
841 }
842
843#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
844 // Stop any ongoing recording.
845 if (debug_file_->Open()) {
846 if (debug_file_->CloseFile() == -1) {
847 return kFileError;
848 }
849 }
850
851 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
852 return kFileError;
853 }
854
855 int err = WriteInitMessage();
856 if (err != kNoError) {
857 return err;
858 }
859 return kNoError;
860#else
861 return kUnsupportedFunctionError;
862#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
863}
864
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000865int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
866 rtc::PlatformFile handle) {
867 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
868 return StartDebugRecording(stream);
869}
870
niklase@google.com470e71d2011-07-07 08:21:25 +0000871int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000872 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000873
874#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000875 // We just return if recording hasn't started.
876 if (debug_file_->Open()) {
877 if (debug_file_->CloseFile() == -1) {
878 return kFileError;
879 }
880 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000881 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000882#else
883 return kUnsupportedFunctionError;
884#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000885}
886
887EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
888 return echo_cancellation_;
889}
890
891EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
892 return echo_control_mobile_;
893}
894
895GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000896 if (use_new_agc_) {
897 return gain_control_for_new_agc_.get();
898 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000899 return gain_control_;
900}
901
902HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
903 return high_pass_filter_;
904}
905
906LevelEstimator* AudioProcessingImpl::level_estimator() const {
907 return level_estimator_;
908}
909
910NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
911 return noise_suppression_;
912}
913
914VoiceDetection* AudioProcessingImpl::voice_detection() const {
915 return voice_detection_;
916}
917
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000918bool AudioProcessingImpl::is_data_processed() const {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000919 if (beamformer_enabled_) {
920 return true;
921 }
922
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000923 int enabled_count = 0;
924 std::list<ProcessingComponent*>::const_iterator it;
925 for (it = component_list_.begin(); it != component_list_.end(); it++) {
926 if ((*it)->is_component_enabled()) {
927 enabled_count++;
928 }
929 }
930
931 // Data is unchanged if no components are enabled, or if only level_estimator_
932 // or voice_detection_ is enabled.
933 if (enabled_count == 0) {
934 return false;
935 } else if (enabled_count == 1) {
936 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
937 return false;
938 }
939 } else if (enabled_count == 2) {
940 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
941 return false;
942 }
943 }
944 return true;
945}
946
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000947bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000948 // Check if we've upmixed or downmixed the audio.
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000949 return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000950 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000951}
952
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000953bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000954 return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz ||
955 fwd_proc_format_.rate() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000956}
957
958bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000959 if (!is_data_processed && !voice_detection_->is_enabled() &&
960 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000961 // Only level_estimator_ is enabled.
962 return false;
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000963 } else if (fwd_proc_format_.rate() == kSampleRate32kHz ||
964 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000965 // Something besides level_estimator_ is enabled, and we have super-wb.
966 return true;
967 }
968 return false;
969}
970
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000971int AudioProcessingImpl::InitializeExperimentalAgc() {
972 if (use_new_agc_) {
973 if (!agc_manager_.get()) {
974 agc_manager_.reset(
975 new AgcManagerDirect(gain_control_, gain_control_for_new_agc_.get()));
976 }
977 agc_manager_->Initialize();
978 agc_manager_->SetCaptureMuted(output_will_be_muted_);
979 }
980 return kNoError;
981}
982
983int AudioProcessingImpl::InitializeTransient() {
984 if (transient_suppressor_enabled_) {
985 if (!transient_suppressor_.get()) {
986 transient_suppressor_.reset(new TransientSuppressor());
987 }
988 transient_suppressor_->Initialize(fwd_proc_format_.rate(),
989 split_rate_,
990 fwd_out_format_.num_channels());
991 }
992 return kNoError;
993}
994
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000995void AudioProcessingImpl::InitializeBeamformer() {
996 if (beamformer_enabled_) {
997#ifdef WEBRTC_BEAMFORMER
998 // TODO(aluebs): Don't use a hard-coded microphone spacing.
999 beamformer_.reset(new Beamformer(kChunkSizeMs,
1000 split_rate_,
1001 fwd_in_format_.num_channels(),
1002 0.05f));
1003#else
1004 assert(false);
1005#endif
1006 }
1007}
1008
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001009#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +00001010int AudioProcessingImpl::WriteMessageToDebugFile() {
1011 int32_t size = event_msg_->ByteSize();
1012 if (size <= 0) {
1013 return kUnspecifiedError;
1014 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001015#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +00001016 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1017 // pretty safe in assuming little-endian.
1018#endif
1019
1020 if (!event_msg_->SerializeToString(&event_str_)) {
1021 return kUnspecifiedError;
1022 }
1023
1024 // Write message preceded by its size.
1025 if (!debug_file_->Write(&size, sizeof(int32_t))) {
1026 return kFileError;
1027 }
1028 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
1029 return kFileError;
1030 }
1031
1032 event_msg_->Clear();
1033
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001034 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001035}
1036
1037int AudioProcessingImpl::WriteInitMessage() {
1038 event_msg_->set_type(audioproc::Event::INIT);
1039 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001040 msg->set_sample_rate(fwd_in_format_.rate());
1041 msg->set_num_input_channels(fwd_in_format_.num_channels());
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +00001042 msg->set_num_output_channels(fwd_out_format_.num_channels());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001043 msg->set_num_reverse_channels(rev_in_format_.num_channels());
1044 msg->set_reverse_sample_rate(rev_in_format_.rate());
1045 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +00001046
1047 int err = WriteMessageToDebugFile();
1048 if (err != kNoError) {
1049 return err;
1050 }
1051
1052 return kNoError;
1053}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001054#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001055
niklase@google.com470e71d2011-07-07 08:21:25 +00001056} // namespace webrtc