blob: 1b16c7c609ae049e0c6c23b0c6aece334128b9e9 [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"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000018#include "webrtc/modules/audio_processing/audio_buffer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000019#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000020#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000021#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
22#include "webrtc/modules/audio_processing/gain_control_impl.h"
23#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
24#include "webrtc/modules/audio_processing/level_estimator_impl.h"
25#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
26#include "webrtc/modules/audio_processing/processing_component.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000027#include "webrtc/modules/audio_processing/voice_detection_impl.h"
28#include "webrtc/modules/interface/module_common_types.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000029#include "webrtc/system_wrappers/interface/compile_assert.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000030#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
31#include "webrtc/system_wrappers/interface/file_wrapper.h"
32#include "webrtc/system_wrappers/interface/logging.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000033
34#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
35// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000036#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000037#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000038#else
ajm@google.com808e0e02011-08-03 21:08:51 +000039#include "webrtc/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000040#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000041#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000042
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000043#define RETURN_ON_ERR(expr) \
44 do { \
45 int err = expr; \
46 if (err != kNoError) { \
47 return err; \
48 } \
49 } while (0)
50
niklase@google.com470e71d2011-07-07 08:21:25 +000051namespace webrtc {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000052
53// Throughout webrtc, it's assumed that success is represented by zero.
54COMPILE_ASSERT(AudioProcessing::kNoError == 0, no_error_must_be_zero);
55
niklase@google.com470e71d2011-07-07 08:21:25 +000056AudioProcessing* AudioProcessing::Create(int id) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000057 return Create();
58}
59
60AudioProcessing* AudioProcessing::Create() {
61 Config config;
62 return Create(config);
63}
64
65AudioProcessing* AudioProcessing::Create(const Config& config) {
66 AudioProcessingImpl* apm = new AudioProcessingImpl(config);
niklase@google.com470e71d2011-07-07 08:21:25 +000067 if (apm->Initialize() != kNoError) {
68 delete apm;
69 apm = NULL;
70 }
71
72 return apm;
73}
74
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000075AudioProcessingImpl::AudioProcessingImpl(const Config& config)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000076 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +000077 echo_control_mobile_(NULL),
78 gain_control_(NULL),
79 high_pass_filter_(NULL),
80 level_estimator_(NULL),
81 noise_suppression_(NULL),
82 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +000083 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000084#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
85 debug_file_(FileWrapper::Create()),
86 event_msg_(new audioproc::Event()),
87#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000088 fwd_in_format_(kSampleRate16kHz, 1),
89 fwd_proc_format_(kSampleRate16kHz, 1),
90 fwd_out_format_(kSampleRate16kHz),
91 rev_in_format_(kSampleRate16kHz, 1),
92 rev_proc_format_(kSampleRate16kHz, 1),
93 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +000094 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +000095 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +000096 was_stream_delay_set_(false),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +000097 output_will_be_muted_(false),
andrew@webrtc.org07b59502014-02-12 16:41:13 +000098 key_pressed_(false) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000099 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100 component_list_.push_back(echo_cancellation_);
101
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000102 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103 component_list_.push_back(echo_control_mobile_);
104
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000105 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000106 component_list_.push_back(gain_control_);
107
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000108 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 component_list_.push_back(high_pass_filter_);
110
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000111 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 component_list_.push_back(level_estimator_);
113
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000114 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 component_list_.push_back(noise_suppression_);
116
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000117 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000118 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000119
120 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121}
122
123AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000124 {
125 CriticalSectionScoped crit_scoped(crit_);
126 while (!component_list_.empty()) {
127 ProcessingComponent* component = component_list_.front();
128 component->Destroy();
129 delete component;
130 component_list_.pop_front();
131 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000133#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000134 if (debug_file_->Open()) {
135 debug_file_->CloseFile();
136 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000137#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000138 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000139 delete crit_;
140 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
142
niklase@google.com470e71d2011-07-07 08:21:25 +0000143int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000144 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145 return InitializeLocked();
146}
147
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000148int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000149 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000150 return InitializeLocked(rate,
151 rate,
152 rev_in_format_.rate(),
153 fwd_in_format_.num_channels(),
154 fwd_proc_format_.num_channels(),
155 rev_in_format_.num_channels());
156}
157
158int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
159 int output_sample_rate_hz,
160 int reverse_sample_rate_hz,
161 ChannelLayout input_layout,
162 ChannelLayout output_layout,
163 ChannelLayout reverse_layout) {
164 CriticalSectionScoped crit_scoped(crit_);
165 return InitializeLocked(input_sample_rate_hz,
166 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000167 reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000168 ChannelsFromLayout(input_layout),
169 ChannelsFromLayout(output_layout),
170 ChannelsFromLayout(reverse_layout));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000171}
172
niklase@google.com470e71d2011-07-07 08:21:25 +0000173int AudioProcessingImpl::InitializeLocked() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000174 render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(),
175 rev_in_format_.num_channels(),
176 rev_proc_format_.samples_per_channel(),
177 rev_proc_format_.num_channels(),
178 rev_proc_format_.samples_per_channel()));
179 capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
180 fwd_in_format_.num_channels(),
181 fwd_proc_format_.samples_per_channel(),
182 fwd_proc_format_.num_channels(),
183 fwd_out_format_.samples_per_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
niklase@google.com470e71d2011-07-07 08:21:25 +0000185 // Initialize all components.
186 std::list<ProcessingComponent*>::iterator it;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000187 for (it = component_list_.begin(); it != component_list_.end(); ++it) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000188 int err = (*it)->Initialize();
189 if (err != kNoError) {
190 return err;
191 }
192 }
193
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000194#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000195 if (debug_file_->Open()) {
196 int err = WriteInitMessage();
197 if (err != kNoError) {
198 return err;
199 }
200 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000201#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000202
niklase@google.com470e71d2011-07-07 08:21:25 +0000203 return kNoError;
204}
205
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000206int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz,
207 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000208 int reverse_sample_rate_hz,
209 int num_input_channels,
210 int num_output_channels,
211 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000212 if (input_sample_rate_hz <= 0 ||
213 output_sample_rate_hz <= 0 ||
214 reverse_sample_rate_hz <= 0) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000215 return kBadSampleRateError;
216 }
217 if (num_output_channels > num_input_channels) {
218 return kBadNumberChannelsError;
219 }
220 // Only mono and stereo supported currently.
221 if (num_input_channels > 2 || num_input_channels < 1 ||
222 num_output_channels > 2 || num_output_channels < 1 ||
223 num_reverse_channels > 2 || num_reverse_channels < 1) {
224 return kBadNumberChannelsError;
225 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000226
227 fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
228 fwd_out_format_.set(output_sample_rate_hz);
229 rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
230
231 // We process at the closest native rate >= min(input rate, output rate)...
232 int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate());
233 int fwd_proc_rate;
234 if (min_proc_rate > kSampleRate16kHz) {
235 fwd_proc_rate = kSampleRate32kHz;
236 } else if (min_proc_rate > kSampleRate8kHz) {
237 fwd_proc_rate = kSampleRate16kHz;
238 } else {
239 fwd_proc_rate = kSampleRate8kHz;
240 }
241 // ...with one exception.
242 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
243 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000244 }
245
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000246 fwd_proc_format_.set(fwd_proc_rate, num_output_channels);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000247
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000248 // We normally process the reverse stream at 16 kHz. Unless...
249 int rev_proc_rate = kSampleRate16kHz;
250 if (fwd_proc_format_.rate() == kSampleRate8kHz) {
251 // ...the forward stream is at 8 kHz.
252 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000253 } else {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000254 if (rev_in_format_.rate() == kSampleRate32kHz) {
255 // ...or the input is at 32 kHz, in which case we use the splitting
256 // filter rather than the resampler.
257 rev_proc_rate = kSampleRate32kHz;
258 }
259 }
260
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000261 // Always downmix the reverse stream to mono for analysis. This has been
262 // demonstrated to work well for AEC in most practical scenarios.
263 rev_proc_format_.set(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000264
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000265 if (fwd_proc_format_.rate() == kSampleRate32kHz ||
266 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000267 split_rate_ = kSampleRate16kHz;
268 } else {
269 split_rate_ = fwd_proc_format_.rate();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000270 }
271
272 return InitializeLocked();
273}
274
275// Calls InitializeLocked() if any of the audio parameters have changed from
276// their current values.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000277int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz,
278 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000279 int reverse_sample_rate_hz,
280 int num_input_channels,
281 int num_output_channels,
282 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000283 if (input_sample_rate_hz == fwd_in_format_.rate() &&
284 output_sample_rate_hz == fwd_out_format_.rate() &&
285 reverse_sample_rate_hz == rev_in_format_.rate() &&
286 num_input_channels == fwd_in_format_.num_channels() &&
287 num_output_channels == fwd_proc_format_.num_channels() &&
288 num_reverse_channels == rev_in_format_.num_channels()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000289 return kNoError;
290 }
291
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000292 return InitializeLocked(input_sample_rate_hz,
293 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000294 reverse_sample_rate_hz,
295 num_input_channels,
296 num_output_channels,
297 num_reverse_channels);
298}
299
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000300void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000301 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000302 std::list<ProcessingComponent*>::iterator it;
303 for (it = component_list_.begin(); it != component_list_.end(); ++it)
304 (*it)->SetExtraOptions(config);
305}
306
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000307int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000308 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000309 return fwd_in_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000310}
311
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000312int AudioProcessingImpl::sample_rate_hz() const {
313 CriticalSectionScoped crit_scoped(crit_);
314 return fwd_in_format_.rate();
315}
316
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000317int AudioProcessingImpl::proc_sample_rate_hz() const {
318 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000319}
320
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000321int AudioProcessingImpl::proc_split_sample_rate_hz() const {
322 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000323}
324
325int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000326 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000327}
328
329int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000330 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000331}
332
333int AudioProcessingImpl::num_output_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000334 return fwd_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000335}
336
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000337void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
338 output_will_be_muted_ = muted;
339}
340
341bool AudioProcessingImpl::output_will_be_muted() const {
342 return output_will_be_muted_;
343}
344
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000345int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000346 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000347 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000348 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000349 int output_sample_rate_hz,
350 ChannelLayout output_layout,
351 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000352 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000353 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000354 return kNullPointerError;
355 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000356
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000357 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
358 output_sample_rate_hz,
359 rev_in_format_.rate(),
360 ChannelsFromLayout(input_layout),
361 ChannelsFromLayout(output_layout),
362 rev_in_format_.num_channels()));
363 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000364 return kBadDataLengthError;
365 }
366
367#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
368 if (debug_file_->Open()) {
369 event_msg_->set_type(audioproc::Event::STREAM);
370 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000371 const size_t channel_size =
372 sizeof(float) * fwd_in_format_.samples_per_channel();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000373 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
374 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000375 }
376#endif
377
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000378 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000379 RETURN_ON_ERR(ProcessStreamLocked());
380 if (output_copy_needed(is_data_processed())) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000381 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
382 output_layout,
383 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000384 }
385
386#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
387 if (debug_file_->Open()) {
388 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000389 const size_t channel_size =
390 sizeof(float) * fwd_out_format_.samples_per_channel();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000391 for (int i = 0; i < fwd_proc_format_.num_channels(); ++i)
392 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000393 RETURN_ON_ERR(WriteMessageToDebugFile());
394 }
395#endif
396
397 return kNoError;
398}
399
400int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
401 CriticalSectionScoped crit_scoped(crit_);
402 if (!frame) {
403 return kNullPointerError;
404 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000405 // Must be a native rate.
406 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
407 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000408 frame->sample_rate_hz_ != kSampleRate32kHz &&
409 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000410 return kBadSampleRateError;
411 }
412 if (echo_control_mobile_->is_enabled() &&
413 frame->sample_rate_hz_ > kSampleRate16kHz) {
414 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
415 return kUnsupportedComponentError;
416 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000417
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000418 // TODO(ajm): The input and output rates and channels are currently
419 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000420 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000421 frame->sample_rate_hz_,
422 rev_in_format_.rate(),
423 frame->num_channels_,
424 frame->num_channels_,
425 rev_in_format_.num_channels()));
426 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 return kBadDataLengthError;
428 }
429
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000430#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000431 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000432 event_msg_->set_type(audioproc::Event::STREAM);
433 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000434 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000435 frame->samples_per_channel_ *
436 frame->num_channels_;
437 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000438 }
439#endif
440
441 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000442 RETURN_ON_ERR(ProcessStreamLocked());
443 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
444
445#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
446 if (debug_file_->Open()) {
447 audioproc::Stream* msg = event_msg_->mutable_stream();
448 const size_t data_size = sizeof(int16_t) *
449 frame->samples_per_channel_ *
450 frame->num_channels_;
451 msg->set_output_data(frame->data_, data_size);
452 RETURN_ON_ERR(WriteMessageToDebugFile());
453 }
454#endif
455
456 return kNoError;
457}
458
459
460int AudioProcessingImpl::ProcessStreamLocked() {
461#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
462 if (debug_file_->Open()) {
463 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000464 msg->set_delay(stream_delay_ms_);
465 msg->set_drift(echo_cancellation_->stream_drift_samples());
466 msg->set_level(gain_control_->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000467 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000469#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000471 AudioBuffer* ca = capture_audio_.get(); // For brevity.
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000472 bool data_processed = is_data_processed();
473 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000474 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000475 }
476
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000477 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
478 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000479 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000480 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000481
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000482 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000483 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000484 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000485 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
486 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
487 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
488 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000489
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000490 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000491 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000492 }
493
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000494 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000495 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000496
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000497 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000498 return kNoError;
499}
500
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000501int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
502 int samples_per_channel,
503 int sample_rate_hz,
504 ChannelLayout layout) {
505 CriticalSectionScoped crit_scoped(crit_);
506 if (data == NULL) {
507 return kNullPointerError;
508 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000509
510 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000511 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
512 fwd_out_format_.rate(),
513 sample_rate_hz,
514 fwd_in_format_.num_channels(),
515 fwd_proc_format_.num_channels(),
516 num_channels));
517 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000518 return kBadDataLengthError;
519 }
520
521#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
522 if (debug_file_->Open()) {
523 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
524 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000525 const size_t channel_size =
526 sizeof(float) * rev_in_format_.samples_per_channel();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000527 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000528 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000529 RETURN_ON_ERR(WriteMessageToDebugFile());
530 }
531#endif
532
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000533 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000534 return AnalyzeReverseStreamLocked();
535}
536
niklase@google.com470e71d2011-07-07 08:21:25 +0000537int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000538 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000539 if (frame == NULL) {
540 return kNullPointerError;
541 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000542 // Must be a native rate.
543 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
544 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000545 frame->sample_rate_hz_ != kSampleRate32kHz &&
546 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000547 return kBadSampleRateError;
548 }
549 // This interface does not tolerate different forward and reverse rates.
550 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000551 return kBadSampleRateError;
552 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000553
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000554 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
555 fwd_out_format_.rate(),
556 frame->sample_rate_hz_,
557 fwd_in_format_.num_channels(),
558 fwd_in_format_.num_channels(),
559 frame->num_channels_));
560 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000561 return kBadDataLengthError;
562 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000563
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000564#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000565 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000566 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
567 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000568 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000569 frame->samples_per_channel_ *
570 frame->num_channels_;
571 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000572 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000573 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000574#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000575
576 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000577 return AnalyzeReverseStreamLocked();
578}
niklase@google.com470e71d2011-07-07 08:21:25 +0000579
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000580int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000581 AudioBuffer* ra = render_audio_.get(); // For brevity.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000582 if (rev_proc_format_.rate() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000583 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000584 }
585
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000586 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
587 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
588 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
niklase@google.com470e71d2011-07-07 08:21:25 +0000589
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000590 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000591}
592
593int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000594 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000595 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000596 delay += delay_offset_ms_;
597
niklase@google.com470e71d2011-07-07 08:21:25 +0000598 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000599 delay = 0;
600 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000601 }
602
603 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
604 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000605 delay = 500;
606 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000607 }
608
609 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000610 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000611}
612
613int AudioProcessingImpl::stream_delay_ms() const {
614 return stream_delay_ms_;
615}
616
617bool AudioProcessingImpl::was_stream_delay_set() const {
618 return was_stream_delay_set_;
619}
620
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000621void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
622 key_pressed_ = key_pressed;
623}
624
625bool AudioProcessingImpl::stream_key_pressed() const {
626 return key_pressed_;
627}
628
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000629void AudioProcessingImpl::set_delay_offset_ms(int offset) {
630 CriticalSectionScoped crit_scoped(crit_);
631 delay_offset_ms_ = offset;
632}
633
634int AudioProcessingImpl::delay_offset_ms() const {
635 return delay_offset_ms_;
636}
637
niklase@google.com470e71d2011-07-07 08:21:25 +0000638int AudioProcessingImpl::StartDebugRecording(
639 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000640 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000641 assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize);
642
643 if (filename == NULL) {
644 return kNullPointerError;
645 }
646
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000647#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000648 // Stop any ongoing recording.
649 if (debug_file_->Open()) {
650 if (debug_file_->CloseFile() == -1) {
651 return kFileError;
652 }
653 }
654
655 if (debug_file_->OpenFile(filename, false) == -1) {
656 debug_file_->CloseFile();
657 return kFileError;
658 }
659
ajm@google.com808e0e02011-08-03 21:08:51 +0000660 int err = WriteInitMessage();
661 if (err != kNoError) {
662 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000663 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000664 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000665#else
666 return kUnsupportedFunctionError;
667#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000668}
669
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000670int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
671 CriticalSectionScoped crit_scoped(crit_);
672
673 if (handle == NULL) {
674 return kNullPointerError;
675 }
676
677#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
678 // Stop any ongoing recording.
679 if (debug_file_->Open()) {
680 if (debug_file_->CloseFile() == -1) {
681 return kFileError;
682 }
683 }
684
685 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
686 return kFileError;
687 }
688
689 int err = WriteInitMessage();
690 if (err != kNoError) {
691 return err;
692 }
693 return kNoError;
694#else
695 return kUnsupportedFunctionError;
696#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
697}
698
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000699int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
700 rtc::PlatformFile handle) {
701 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
702 return StartDebugRecording(stream);
703}
704
niklase@google.com470e71d2011-07-07 08:21:25 +0000705int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000706 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000707
708#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000709 // We just return if recording hasn't started.
710 if (debug_file_->Open()) {
711 if (debug_file_->CloseFile() == -1) {
712 return kFileError;
713 }
714 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000715 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000716#else
717 return kUnsupportedFunctionError;
718#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000719}
720
721EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
722 return echo_cancellation_;
723}
724
725EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
726 return echo_control_mobile_;
727}
728
729GainControl* AudioProcessingImpl::gain_control() const {
730 return gain_control_;
731}
732
733HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
734 return high_pass_filter_;
735}
736
737LevelEstimator* AudioProcessingImpl::level_estimator() const {
738 return level_estimator_;
739}
740
741NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
742 return noise_suppression_;
743}
744
745VoiceDetection* AudioProcessingImpl::voice_detection() const {
746 return voice_detection_;
747}
748
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000749bool AudioProcessingImpl::is_data_processed() const {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000750 int enabled_count = 0;
751 std::list<ProcessingComponent*>::const_iterator it;
752 for (it = component_list_.begin(); it != component_list_.end(); it++) {
753 if ((*it)->is_component_enabled()) {
754 enabled_count++;
755 }
756 }
757
758 // Data is unchanged if no components are enabled, or if only level_estimator_
759 // or voice_detection_ is enabled.
760 if (enabled_count == 0) {
761 return false;
762 } else if (enabled_count == 1) {
763 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
764 return false;
765 }
766 } else if (enabled_count == 2) {
767 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
768 return false;
769 }
770 }
771 return true;
772}
773
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000774bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000775 // Check if we've upmixed or downmixed the audio.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000776 return ((fwd_proc_format_.num_channels() != fwd_in_format_.num_channels()) ||
777 is_data_processed);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000778}
779
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000780bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000781 return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz ||
782 fwd_proc_format_.rate() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000783}
784
785bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
786 if (!is_data_processed && !voice_detection_->is_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000787 // Only level_estimator_ is enabled.
788 return false;
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000789 } else if (fwd_proc_format_.rate() == kSampleRate32kHz ||
790 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000791 // Something besides level_estimator_ is enabled, and we have super-wb.
792 return true;
793 }
794 return false;
795}
796
797#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000798int AudioProcessingImpl::WriteMessageToDebugFile() {
799 int32_t size = event_msg_->ByteSize();
800 if (size <= 0) {
801 return kUnspecifiedError;
802 }
andrew@webrtc.org621df672013-10-22 10:27:23 +0000803#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +0000804 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
805 // pretty safe in assuming little-endian.
806#endif
807
808 if (!event_msg_->SerializeToString(&event_str_)) {
809 return kUnspecifiedError;
810 }
811
812 // Write message preceded by its size.
813 if (!debug_file_->Write(&size, sizeof(int32_t))) {
814 return kFileError;
815 }
816 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
817 return kFileError;
818 }
819
820 event_msg_->Clear();
821
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000822 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +0000823}
824
825int AudioProcessingImpl::WriteInitMessage() {
826 event_msg_->set_type(audioproc::Event::INIT);
827 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000828 msg->set_sample_rate(fwd_in_format_.rate());
829 msg->set_num_input_channels(fwd_in_format_.num_channels());
830 msg->set_num_output_channels(fwd_proc_format_.num_channels());
831 msg->set_num_reverse_channels(rev_in_format_.num_channels());
832 msg->set_reverse_sample_rate(rev_in_format_.rate());
833 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +0000834
835 int err = WriteMessageToDebugFile();
836 if (err != kNoError) {
837 return err;
838 }
839
840 return kNoError;
841}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000842#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000843
niklase@google.com470e71d2011-07-07 08:21:25 +0000844} // namespace webrtc