blob: 8b9044f85a3bc7c87ee4fac3e885c14d8e88a1c5 [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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.orgf1de5e92013-03-01 16:36:19 +000011#include "webrtc/modules/audio_processing/audio_processing_impl.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000012
13#include <assert.h>
14
andrew@webrtc.orge95dc252014-01-07 17:45:09 +000015#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
andrew@webrtc.orgf1de5e92013-03-01 16:36:19 +000016#include "webrtc/modules/audio_processing/audio_buffer.h"
andrew@webrtc.org25613ea2013-07-25 18:28:29 +000017#include "webrtc/modules/audio_processing/echo_cancellation_impl_wrapper.h"
andrew@webrtc.orgf1de5e92013-03-01 16:36:19 +000018#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
19#include "webrtc/modules/audio_processing/gain_control_impl.h"
20#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
21#include "webrtc/modules/audio_processing/level_estimator_impl.h"
22#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
23#include "webrtc/modules/audio_processing/processing_component.h"
andrew@webrtc.orgf1de5e92013-03-01 16:36:19 +000024#include "webrtc/modules/audio_processing/voice_detection_impl.h"
25#include "webrtc/modules/interface/module_common_types.h"
andrew@webrtc.orge95dc252014-01-07 17:45:09 +000026#include "webrtc/system_wrappers/interface/compile_assert.h"
andrew@webrtc.orgf1de5e92013-03-01 16:36:19 +000027#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
28#include "webrtc/system_wrappers/interface/file_wrapper.h"
29#include "webrtc/system_wrappers/interface/logging.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000030
31#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
32// Files generated at build-time by the protobuf compiler.
33#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org5e512ae2012-10-22 21:21:52 +000034#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000035#else
36#include "webrtc/audio_processing/debug.pb.h"
37#endif
38#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
39
andrew@webrtc.orge95dc252014-01-07 17:45:09 +000040static const int kChunkSizeMs = 10;
41
42#define RETURN_ON_ERR(expr) \
43 do { \
44 int err = expr; \
45 if (err != kNoError) { \
46 return err; \
47 } \
48 } while (0)
49
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000050namespace webrtc {
andrew@webrtc.orge95dc252014-01-07 17:45:09 +000051
52// Throughout webrtc, it's assumed that success is represented by zero.
53COMPILE_ASSERT(AudioProcessing::kNoError == 0, no_error_must_be_zero);
54
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000055AudioProcessing* AudioProcessing::Create(int id) {
andrew@webrtc.org3fbe6662014-01-25 02:09:06 +000056 return Create();
57}
58
59AudioProcessing* AudioProcessing::Create() {
60 Config config;
61 return Create(config);
62}
63
64AudioProcessing* AudioProcessing::Create(const Config& config) {
65 AudioProcessingImpl* apm = new AudioProcessingImpl(config);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000066 if (apm->Initialize() != kNoError) {
67 delete apm;
68 apm = NULL;
69 }
70
71 return apm;
72}
73
pbos@webrtc.org24add922013-08-02 11:44:11 +000074int32_t AudioProcessing::TimeUntilNextProcess() { return -1; }
75int32_t AudioProcessing::Process() { return -1; }
76
andrew@webrtc.org3fbe6662014-01-25 02:09:06 +000077AudioProcessingImpl::AudioProcessingImpl(const Config& config)
andrew@webrtc.orge95dc252014-01-07 17:45:09 +000078 : echo_cancellation_(NULL),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000079 echo_control_mobile_(NULL),
80 gain_control_(NULL),
81 high_pass_filter_(NULL),
82 level_estimator_(NULL),
83 noise_suppression_(NULL),
84 voice_detection_(NULL),
85 crit_(CriticalSectionWrapper::CreateCriticalSection()),
86 render_audio_(NULL),
87 capture_audio_(NULL),
88#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
89 debug_file_(FileWrapper::Create()),
90 event_msg_(new audioproc::Event()),
91#endif
92 sample_rate_hz_(kSampleRate16kHz),
93 split_sample_rate_hz_(kSampleRate16kHz),
andrew@webrtc.orge95dc252014-01-07 17:45:09 +000094 samples_per_channel_(kChunkSizeMs * sample_rate_hz_ / 1000),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000095 stream_delay_ms_(0),
96 delay_offset_ms_(0),
97 was_stream_delay_set_(false),
98 num_reverse_channels_(1),
99 num_input_channels_(1),
100 num_output_channels_(1) {
andrew@webrtc.org25613ea2013-07-25 18:28:29 +0000101 echo_cancellation_ = EchoCancellationImplWrapper::Create(this);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000102 component_list_.push_back(echo_cancellation_);
103
104 echo_control_mobile_ = new EchoControlMobileImpl(this);
105 component_list_.push_back(echo_control_mobile_);
106
107 gain_control_ = new GainControlImpl(this);
108 component_list_.push_back(gain_control_);
109
110 high_pass_filter_ = new HighPassFilterImpl(this);
111 component_list_.push_back(high_pass_filter_);
112
113 level_estimator_ = new LevelEstimatorImpl(this);
114 component_list_.push_back(level_estimator_);
115
116 noise_suppression_ = new NoiseSuppressionImpl(this);
117 component_list_.push_back(noise_suppression_);
118
119 voice_detection_ = new VoiceDetectionImpl(this);
120 component_list_.push_back(voice_detection_);
andrew@webrtc.org3fbe6662014-01-25 02:09:06 +0000121
122 SetExtraOptions(config);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000123}
124
125AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org7838d792012-10-27 00:28:27 +0000126 {
127 CriticalSectionScoped crit_scoped(crit_);
128 while (!component_list_.empty()) {
129 ProcessingComponent* component = component_list_.front();
130 component->Destroy();
131 delete component;
132 component_list_.pop_front();
133 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000134
135#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org7838d792012-10-27 00:28:27 +0000136 if (debug_file_->Open()) {
137 debug_file_->CloseFile();
138 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000139#endif
140
andrew@webrtc.org7838d792012-10-27 00:28:27 +0000141 if (render_audio_) {
142 delete render_audio_;
143 render_audio_ = NULL;
144 }
145
146 if (capture_audio_) {
147 delete capture_audio_;
148 capture_audio_ = NULL;
149 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000150 }
151
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000152 delete crit_;
153 crit_ = NULL;
154}
155
156CriticalSectionWrapper* AudioProcessingImpl::crit() const {
157 return crit_;
158}
159
160int AudioProcessingImpl::split_sample_rate_hz() const {
161 return split_sample_rate_hz_;
162}
163
164int AudioProcessingImpl::Initialize() {
165 CriticalSectionScoped crit_scoped(crit_);
166 return InitializeLocked();
167}
168
169int AudioProcessingImpl::InitializeLocked() {
170 if (render_audio_ != NULL) {
171 delete render_audio_;
172 render_audio_ = NULL;
173 }
174
175 if (capture_audio_ != NULL) {
176 delete capture_audio_;
177 capture_audio_ = NULL;
178 }
179
180 render_audio_ = new AudioBuffer(num_reverse_channels_,
181 samples_per_channel_);
182 capture_audio_ = new AudioBuffer(num_input_channels_,
183 samples_per_channel_);
184
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000185 // Initialize all components.
186 std::list<ProcessingComponent*>::iterator it;
andrew@webrtc.org7838d792012-10-27 00:28:27 +0000187 for (it = component_list_.begin(); it != component_list_.end(); ++it) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000188 int err = (*it)->Initialize();
189 if (err != kNoError) {
190 return err;
191 }
192 }
193
194#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
195 if (debug_file_->Open()) {
196 int err = WriteInitMessage();
197 if (err != kNoError) {
198 return err;
199 }
200 }
201#endif
202
203 return kNoError;
204}
205
andrew@webrtc.org25613ea2013-07-25 18:28:29 +0000206void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.org3fbe6662014-01-25 02:09:06 +0000207 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org25613ea2013-07-25 18:28:29 +0000208 std::list<ProcessingComponent*>::iterator it;
209 for (it = component_list_.begin(); it != component_list_.end(); ++it)
210 (*it)->SetExtraOptions(config);
211}
212
aluebs@webrtc.org09b40ec2013-11-19 15:17:51 +0000213int AudioProcessingImpl::EnableExperimentalNs(bool enable) {
214 return kNoError;
215}
216
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000217int AudioProcessingImpl::set_sample_rate_hz(int rate) {
218 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7838d792012-10-27 00:28:27 +0000219 if (rate == sample_rate_hz_) {
220 return kNoError;
221 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000222 if (rate != kSampleRate8kHz &&
223 rate != kSampleRate16kHz &&
224 rate != kSampleRate32kHz) {
225 return kBadParameterError;
226 }
andrew@webrtc.orgf1de5e92013-03-01 16:36:19 +0000227 if (echo_control_mobile_->is_enabled() && rate > kSampleRate16kHz) {
228 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates";
229 return kUnsupportedComponentError;
230 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000231
232 sample_rate_hz_ = rate;
233 samples_per_channel_ = rate / 100;
234
235 if (sample_rate_hz_ == kSampleRate32kHz) {
236 split_sample_rate_hz_ = kSampleRate16kHz;
237 } else {
238 split_sample_rate_hz_ = sample_rate_hz_;
239 }
240
241 return InitializeLocked();
242}
243
244int AudioProcessingImpl::sample_rate_hz() const {
henrika@webrtc.org1d25eac2013-04-05 14:34:57 +0000245 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000246 return sample_rate_hz_;
247}
248
249int AudioProcessingImpl::set_num_reverse_channels(int channels) {
250 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7838d792012-10-27 00:28:27 +0000251 if (channels == num_reverse_channels_) {
252 return kNoError;
253 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000254 // Only stereo supported currently.
255 if (channels > 2 || channels < 1) {
256 return kBadParameterError;
257 }
258
259 num_reverse_channels_ = channels;
260
261 return InitializeLocked();
262}
263
264int AudioProcessingImpl::num_reverse_channels() const {
265 return num_reverse_channels_;
266}
267
268int AudioProcessingImpl::set_num_channels(
269 int input_channels,
270 int output_channels) {
271 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7838d792012-10-27 00:28:27 +0000272 if (input_channels == num_input_channels_ &&
273 output_channels == num_output_channels_) {
274 return kNoError;
275 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000276 if (output_channels > input_channels) {
277 return kBadParameterError;
278 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000279 // Only stereo supported currently.
andrew@webrtc.org7838d792012-10-27 00:28:27 +0000280 if (input_channels > 2 || input_channels < 1 ||
281 output_channels > 2 || output_channels < 1) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000282 return kBadParameterError;
283 }
284
285 num_input_channels_ = input_channels;
286 num_output_channels_ = output_channels;
287
288 return InitializeLocked();
289}
290
291int AudioProcessingImpl::num_input_channels() const {
292 return num_input_channels_;
293}
294
295int AudioProcessingImpl::num_output_channels() const {
296 return num_output_channels_;
297}
298
andrew@webrtc.orge95dc252014-01-07 17:45:09 +0000299int AudioProcessingImpl::MaybeInitializeLocked(int sample_rate_hz,
300 int num_input_channels, int num_output_channels, int num_reverse_channels) {
301 if (sample_rate_hz == sample_rate_hz_ &&
302 num_input_channels == num_input_channels_ &&
303 num_output_channels == num_output_channels_ &&
304 num_reverse_channels == num_reverse_channels_) {
305 return kNoError;
306 }
307
308 if (sample_rate_hz != kSampleRate8kHz &&
309 sample_rate_hz != kSampleRate16kHz &&
310 sample_rate_hz != kSampleRate32kHz) {
311 return kBadSampleRateError;
312 }
313 if (num_output_channels > num_input_channels) {
314 return kBadNumberChannelsError;
315 }
316 // Only mono and stereo supported currently.
317 if (num_input_channels > 2 || num_input_channels < 1 ||
318 num_output_channels > 2 || num_output_channels < 1 ||
319 num_reverse_channels > 2 || num_reverse_channels < 1) {
320 return kBadNumberChannelsError;
321 }
322 if (echo_control_mobile_->is_enabled() && sample_rate_hz > kSampleRate16kHz) {
323 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
324 return kUnsupportedComponentError;
325 }
326
327 sample_rate_hz_ = sample_rate_hz;
328 samples_per_channel_ = kChunkSizeMs * sample_rate_hz / 1000;
329 num_input_channels_ = num_input_channels;
330 num_output_channels_ = num_output_channels;
331 num_reverse_channels_ = num_reverse_channels;
332
333 if (sample_rate_hz_ == kSampleRate32kHz) {
334 split_sample_rate_hz_ = kSampleRate16kHz;
335 } else {
336 split_sample_rate_hz_ = sample_rate_hz_;
337 }
338
339 return InitializeLocked();
340}
341
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000342int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
343 CriticalSectionScoped crit_scoped(crit_);
344 int err = kNoError;
345
346 if (frame == NULL) {
347 return kNullPointerError;
348 }
andrew@webrtc.orge95dc252014-01-07 17:45:09 +0000349 // TODO(ajm): We now always set the output channels equal to the input
350 // channels here. Remove the ability to downmix entirely.
351 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
352 frame->num_channels_, frame->num_channels_, num_reverse_channels_));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000353 if (frame->samples_per_channel_ != samples_per_channel_) {
354 return kBadDataLengthError;
355 }
356
357#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
358 if (debug_file_->Open()) {
359 event_msg_->set_type(audioproc::Event::STREAM);
360 audioproc::Stream* msg = event_msg_->mutable_stream();
361 const size_t data_size = sizeof(int16_t) *
362 frame->samples_per_channel_ *
363 frame->num_channels_;
364 msg->set_input_data(frame->data_, data_size);
365 msg->set_delay(stream_delay_ms_);
366 msg->set_drift(echo_cancellation_->stream_drift_samples());
367 msg->set_level(gain_control_->stream_analog_level());
368 }
369#endif
370
371 capture_audio_->DeinterleaveFrom(frame);
372
373 // TODO(ajm): experiment with mixing and AEC placement.
374 if (num_output_channels_ < num_input_channels_) {
375 capture_audio_->Mix(num_output_channels_);
376 frame->num_channels_ = num_output_channels_;
377 }
378
379 bool data_processed = is_data_processed();
380 if (analysis_needed(data_processed)) {
381 for (int i = 0; i < num_output_channels_; i++) {
382 // Split into a low and high band.
andrew@webrtc.orge95dc252014-01-07 17:45:09 +0000383 WebRtcSpl_AnalysisQMF(capture_audio_->data(i),
384 capture_audio_->samples_per_channel(),
385 capture_audio_->low_pass_split_data(i),
386 capture_audio_->high_pass_split_data(i),
387 capture_audio_->analysis_filter_state1(i),
388 capture_audio_->analysis_filter_state2(i));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000389 }
390 }
391
392 err = high_pass_filter_->ProcessCaptureAudio(capture_audio_);
393 if (err != kNoError) {
394 return err;
395 }
396
397 err = gain_control_->AnalyzeCaptureAudio(capture_audio_);
398 if (err != kNoError) {
399 return err;
400 }
401
402 err = echo_cancellation_->ProcessCaptureAudio(capture_audio_);
403 if (err != kNoError) {
404 return err;
405 }
406
407 if (echo_control_mobile_->is_enabled() &&
408 noise_suppression_->is_enabled()) {
409 capture_audio_->CopyLowPassToReference();
410 }
411
412 err = noise_suppression_->ProcessCaptureAudio(capture_audio_);
413 if (err != kNoError) {
414 return err;
415 }
416
417 err = echo_control_mobile_->ProcessCaptureAudio(capture_audio_);
418 if (err != kNoError) {
419 return err;
420 }
421
422 err = voice_detection_->ProcessCaptureAudio(capture_audio_);
423 if (err != kNoError) {
424 return err;
425 }
426
427 err = gain_control_->ProcessCaptureAudio(capture_audio_);
428 if (err != kNoError) {
429 return err;
430 }
431
432 if (synthesis_needed(data_processed)) {
433 for (int i = 0; i < num_output_channels_; i++) {
434 // Recombine low and high bands.
andrew@webrtc.orge95dc252014-01-07 17:45:09 +0000435 WebRtcSpl_SynthesisQMF(capture_audio_->low_pass_split_data(i),
436 capture_audio_->high_pass_split_data(i),
437 capture_audio_->samples_per_split_channel(),
438 capture_audio_->data(i),
439 capture_audio_->synthesis_filter_state1(i),
440 capture_audio_->synthesis_filter_state2(i));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000441 }
442 }
443
444 // The level estimator operates on the recombined data.
445 err = level_estimator_->ProcessStream(capture_audio_);
446 if (err != kNoError) {
447 return err;
448 }
449
450 capture_audio_->InterleaveTo(frame, interleave_needed(data_processed));
451
452#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
453 if (debug_file_->Open()) {
454 audioproc::Stream* msg = event_msg_->mutable_stream();
455 const size_t data_size = sizeof(int16_t) *
456 frame->samples_per_channel_ *
457 frame->num_channels_;
458 msg->set_output_data(frame->data_, data_size);
459 err = WriteMessageToDebugFile();
460 if (err != kNoError) {
461 return err;
462 }
463 }
464#endif
465
466 was_stream_delay_set_ = false;
467 return kNoError;
468}
469
andrew@webrtc.orge95dc252014-01-07 17:45:09 +0000470// TODO(ajm): Have AnalyzeReverseStream accept sample rates not matching the
471// primary stream and convert ourselves rather than having the user manage it.
472// We can be smarter and use the splitting filter when appropriate. Similarly,
473// perform downmixing here.
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000474int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
475 CriticalSectionScoped crit_scoped(crit_);
476 int err = kNoError;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000477 if (frame == NULL) {
478 return kNullPointerError;
479 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000480 if (frame->sample_rate_hz_ != sample_rate_hz_) {
481 return kBadSampleRateError;
482 }
andrew@webrtc.orge95dc252014-01-07 17:45:09 +0000483 RETURN_ON_ERR(MaybeInitializeLocked(sample_rate_hz_, num_input_channels_,
484 num_output_channels_, frame->num_channels_));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000485
486#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
487 if (debug_file_->Open()) {
488 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
489 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
490 const size_t data_size = sizeof(int16_t) *
491 frame->samples_per_channel_ *
492 frame->num_channels_;
493 msg->set_data(frame->data_, data_size);
494 err = WriteMessageToDebugFile();
495 if (err != kNoError) {
496 return err;
497 }
498 }
499#endif
500
501 render_audio_->DeinterleaveFrom(frame);
502
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000503 if (sample_rate_hz_ == kSampleRate32kHz) {
504 for (int i = 0; i < num_reverse_channels_; i++) {
505 // Split into low and high band.
andrew@webrtc.orge95dc252014-01-07 17:45:09 +0000506 WebRtcSpl_AnalysisQMF(render_audio_->data(i),
507 render_audio_->samples_per_channel(),
508 render_audio_->low_pass_split_data(i),
509 render_audio_->high_pass_split_data(i),
510 render_audio_->analysis_filter_state1(i),
511 render_audio_->analysis_filter_state2(i));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000512 }
513 }
514
515 // TODO(ajm): warnings possible from components?
516 err = echo_cancellation_->ProcessRenderAudio(render_audio_);
517 if (err != kNoError) {
518 return err;
519 }
520
521 err = echo_control_mobile_->ProcessRenderAudio(render_audio_);
522 if (err != kNoError) {
523 return err;
524 }
525
526 err = gain_control_->ProcessRenderAudio(render_audio_);
527 if (err != kNoError) {
528 return err;
529 }
530
531 return err; // TODO(ajm): this is for returning warnings; necessary?
532}
533
534int AudioProcessingImpl::set_stream_delay_ms(int delay) {
535 Error retval = kNoError;
536 was_stream_delay_set_ = true;
537 delay += delay_offset_ms_;
538
539 if (delay < 0) {
540 delay = 0;
541 retval = kBadStreamParameterWarning;
542 }
543
544 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
545 if (delay > 500) {
546 delay = 500;
547 retval = kBadStreamParameterWarning;
548 }
549
550 stream_delay_ms_ = delay;
551 return retval;
552}
553
554int AudioProcessingImpl::stream_delay_ms() const {
555 return stream_delay_ms_;
556}
557
558bool AudioProcessingImpl::was_stream_delay_set() const {
559 return was_stream_delay_set_;
560}
561
562void AudioProcessingImpl::set_delay_offset_ms(int offset) {
563 CriticalSectionScoped crit_scoped(crit_);
564 delay_offset_ms_ = offset;
565}
566
567int AudioProcessingImpl::delay_offset_ms() const {
568 return delay_offset_ms_;
569}
570
571int AudioProcessingImpl::StartDebugRecording(
572 const char filename[AudioProcessing::kMaxFilenameSize]) {
573 CriticalSectionScoped crit_scoped(crit_);
574 assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize);
575
576 if (filename == NULL) {
577 return kNullPointerError;
578 }
579
580#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
581 // Stop any ongoing recording.
582 if (debug_file_->Open()) {
583 if (debug_file_->CloseFile() == -1) {
584 return kFileError;
585 }
586 }
587
588 if (debug_file_->OpenFile(filename, false) == -1) {
589 debug_file_->CloseFile();
590 return kFileError;
591 }
592
593 int err = WriteInitMessage();
594 if (err != kNoError) {
595 return err;
596 }
597 return kNoError;
598#else
599 return kUnsupportedFunctionError;
600#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
601}
602
henrikg@webrtc.org7b722642013-12-06 16:05:17 +0000603int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
604 CriticalSectionScoped crit_scoped(crit_);
605
606 if (handle == NULL) {
607 return kNullPointerError;
608 }
609
610#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
611 // Stop any ongoing recording.
612 if (debug_file_->Open()) {
613 if (debug_file_->CloseFile() == -1) {
614 return kFileError;
615 }
616 }
617
618 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
619 return kFileError;
620 }
621
622 int err = WriteInitMessage();
623 if (err != kNoError) {
624 return err;
625 }
626 return kNoError;
627#else
628 return kUnsupportedFunctionError;
629#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
630}
631
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000632int AudioProcessingImpl::StopDebugRecording() {
633 CriticalSectionScoped crit_scoped(crit_);
634
635#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
636 // We just return if recording hasn't started.
637 if (debug_file_->Open()) {
638 if (debug_file_->CloseFile() == -1) {
639 return kFileError;
640 }
641 }
642 return kNoError;
643#else
644 return kUnsupportedFunctionError;
645#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
646}
647
648EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
649 return echo_cancellation_;
650}
651
652EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
653 return echo_control_mobile_;
654}
655
656GainControl* AudioProcessingImpl::gain_control() const {
657 return gain_control_;
658}
659
660HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
661 return high_pass_filter_;
662}
663
664LevelEstimator* AudioProcessingImpl::level_estimator() const {
665 return level_estimator_;
666}
667
668NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
669 return noise_suppression_;
670}
671
672VoiceDetection* AudioProcessingImpl::voice_detection() const {
673 return voice_detection_;
674}
675
pbos@webrtc.org3f6d5e02013-04-10 07:50:54 +0000676int32_t AudioProcessingImpl::ChangeUniqueId(const int32_t id) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000677 return kNoError;
678}
679
680bool AudioProcessingImpl::is_data_processed() const {
681 int enabled_count = 0;
682 std::list<ProcessingComponent*>::const_iterator it;
683 for (it = component_list_.begin(); it != component_list_.end(); it++) {
684 if ((*it)->is_component_enabled()) {
685 enabled_count++;
686 }
687 }
688
689 // Data is unchanged if no components are enabled, or if only level_estimator_
690 // or voice_detection_ is enabled.
691 if (enabled_count == 0) {
692 return false;
693 } else if (enabled_count == 1) {
694 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
695 return false;
696 }
697 } else if (enabled_count == 2) {
698 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
699 return false;
700 }
701 }
702 return true;
703}
704
705bool AudioProcessingImpl::interleave_needed(bool is_data_processed) const {
706 // Check if we've upmixed or downmixed the audio.
707 return (num_output_channels_ != num_input_channels_ || is_data_processed);
708}
709
710bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
711 return (is_data_processed && sample_rate_hz_ == kSampleRate32kHz);
712}
713
714bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
715 if (!is_data_processed && !voice_detection_->is_enabled()) {
716 // Only level_estimator_ is enabled.
717 return false;
718 } else if (sample_rate_hz_ == kSampleRate32kHz) {
719 // Something besides level_estimator_ is enabled, and we have super-wb.
720 return true;
721 }
722 return false;
723}
724
725#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
726int AudioProcessingImpl::WriteMessageToDebugFile() {
727 int32_t size = event_msg_->ByteSize();
728 if (size <= 0) {
729 return kUnspecifiedError;
730 }
andrew@webrtc.orgd7e90412013-10-22 10:27:23 +0000731#if defined(WEBRTC_ARCH_BIG_ENDIAN)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000732 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
733 // pretty safe in assuming little-endian.
734#endif
735
736 if (!event_msg_->SerializeToString(&event_str_)) {
737 return kUnspecifiedError;
738 }
739
740 // Write message preceded by its size.
741 if (!debug_file_->Write(&size, sizeof(int32_t))) {
742 return kFileError;
743 }
744 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
745 return kFileError;
746 }
747
748 event_msg_->Clear();
749
750 return 0;
751}
752
753int AudioProcessingImpl::WriteInitMessage() {
754 event_msg_->set_type(audioproc::Event::INIT);
755 audioproc::Init* msg = event_msg_->mutable_init();
756 msg->set_sample_rate(sample_rate_hz_);
757 msg->set_device_sample_rate(echo_cancellation_->device_sample_rate_hz());
758 msg->set_num_input_channels(num_input_channels_);
759 msg->set_num_output_channels(num_output_channels_);
760 msg->set_num_reverse_channels(num_reverse_channels_);
761
762 int err = WriteMessageToDebugFile();
763 if (err != kNoError) {
764 return err;
765 }
766
767 return kNoError;
768}
769#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
770} // namespace webrtc