blob: ba22f33536fdd70b8e2873fcd5b26475e2a994ba [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"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000019#include "webrtc/modules/audio_processing/channel_buffer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000020#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000021#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000022#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
23#include "webrtc/modules/audio_processing/gain_control_impl.h"
24#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
25#include "webrtc/modules/audio_processing/level_estimator_impl.h"
26#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
27#include "webrtc/modules/audio_processing/processing_component.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000028#include "webrtc/modules/audio_processing/voice_detection_impl.h"
29#include "webrtc/modules/interface/module_common_types.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000030#include "webrtc/system_wrappers/interface/compile_assert.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000031#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
32#include "webrtc/system_wrappers/interface/file_wrapper.h"
33#include "webrtc/system_wrappers/interface/logging.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000034
35#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
36// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000037#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000038#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000039#else
ajm@google.com808e0e02011-08-03 21:08:51 +000040#include "webrtc/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000041#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000042#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000043
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000044#define RETURN_ON_ERR(expr) \
45 do { \
46 int err = expr; \
47 if (err != kNoError) { \
48 return err; \
49 } \
50 } while (0)
51
niklase@google.com470e71d2011-07-07 08:21:25 +000052namespace webrtc {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000053
54// Throughout webrtc, it's assumed that success is represented by zero.
55COMPILE_ASSERT(AudioProcessing::kNoError == 0, no_error_must_be_zero);
56
niklase@google.com470e71d2011-07-07 08:21:25 +000057AudioProcessing* AudioProcessing::Create(int id) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000058 return Create();
59}
60
61AudioProcessing* AudioProcessing::Create() {
62 Config config;
63 return Create(config);
64}
65
66AudioProcessing* AudioProcessing::Create(const Config& config) {
67 AudioProcessingImpl* apm = new AudioProcessingImpl(config);
niklase@google.com470e71d2011-07-07 08:21:25 +000068 if (apm->Initialize() != kNoError) {
69 delete apm;
70 apm = NULL;
71 }
72
73 return apm;
74}
75
andrew@webrtc.orge84978f2014-01-25 02:09:06 +000076AudioProcessingImpl::AudioProcessingImpl(const Config& config)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000077 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +000078 echo_control_mobile_(NULL),
79 gain_control_(NULL),
80 high_pass_filter_(NULL),
81 level_estimator_(NULL),
82 noise_suppression_(NULL),
83 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +000084 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000085#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
86 debug_file_(FileWrapper::Create()),
87 event_msg_(new audioproc::Event()),
88#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000089 fwd_in_format_(kSampleRate16kHz, 1),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +000090 fwd_proc_format_(kSampleRate16kHz),
91 fwd_out_format_(kSampleRate16kHz, 1),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000092 rev_in_format_(kSampleRate16kHz, 1),
93 rev_proc_format_(kSampleRate16kHz, 1),
94 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +000095 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +000096 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +000097 was_stream_delay_set_(false),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +000098 output_will_be_muted_(false),
andrew@webrtc.org07b59502014-02-12 16:41:13 +000099 key_pressed_(false) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000100 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 component_list_.push_back(echo_cancellation_);
102
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000103 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 component_list_.push_back(echo_control_mobile_);
105
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000106 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 component_list_.push_back(gain_control_);
108
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000109 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 component_list_.push_back(high_pass_filter_);
111
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000112 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 component_list_.push_back(level_estimator_);
114
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000115 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000116 component_list_.push_back(noise_suppression_);
117
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000118 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000119 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000120
121 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122}
123
124AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000125 {
126 CriticalSectionScoped crit_scoped(crit_);
127 while (!component_list_.empty()) {
128 ProcessingComponent* component = component_list_.front();
129 component->Destroy();
130 delete component;
131 component_list_.pop_front();
132 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000134#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000135 if (debug_file_->Open()) {
136 debug_file_->CloseFile();
137 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000138#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000139 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000140 delete crit_;
141 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000142}
143
niklase@google.com470e71d2011-07-07 08:21:25 +0000144int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000145 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146 return InitializeLocked();
147}
148
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000149int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000150 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000151 return InitializeLocked(rate,
152 rate,
153 rev_in_format_.rate(),
154 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000155 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000156 rev_in_format_.num_channels());
157}
158
159int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
160 int output_sample_rate_hz,
161 int reverse_sample_rate_hz,
162 ChannelLayout input_layout,
163 ChannelLayout output_layout,
164 ChannelLayout reverse_layout) {
165 CriticalSectionScoped crit_scoped(crit_);
166 return InitializeLocked(input_sample_rate_hz,
167 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000168 reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000169 ChannelsFromLayout(input_layout),
170 ChannelsFromLayout(output_layout),
171 ChannelsFromLayout(reverse_layout));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000172}
173
niklase@google.com470e71d2011-07-07 08:21:25 +0000174int AudioProcessingImpl::InitializeLocked() {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000175 render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(),
176 rev_in_format_.num_channels(),
177 rev_proc_format_.samples_per_channel(),
178 rev_proc_format_.num_channels(),
179 rev_proc_format_.samples_per_channel()));
180 capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
181 fwd_in_format_.num_channels(),
182 fwd_proc_format_.samples_per_channel(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000183 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000184 fwd_out_format_.samples_per_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
niklase@google.com470e71d2011-07-07 08:21:25 +0000186 // Initialize all components.
187 std::list<ProcessingComponent*>::iterator it;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000188 for (it = component_list_.begin(); it != component_list_.end(); ++it) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000189 int err = (*it)->Initialize();
190 if (err != kNoError) {
191 return err;
192 }
193 }
194
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000195#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000196 if (debug_file_->Open()) {
197 int err = WriteInitMessage();
198 if (err != kNoError) {
199 return err;
200 }
201 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000202#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000203
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 return kNoError;
205}
206
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000207int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz,
208 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000209 int reverse_sample_rate_hz,
210 int num_input_channels,
211 int num_output_channels,
212 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000213 if (input_sample_rate_hz <= 0 ||
214 output_sample_rate_hz <= 0 ||
215 reverse_sample_rate_hz <= 0) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000216 return kBadSampleRateError;
217 }
218 if (num_output_channels > num_input_channels) {
219 return kBadNumberChannelsError;
220 }
221 // Only mono and stereo supported currently.
222 if (num_input_channels > 2 || num_input_channels < 1 ||
223 num_output_channels > 2 || num_output_channels < 1 ||
224 num_reverse_channels > 2 || num_reverse_channels < 1) {
225 return kBadNumberChannelsError;
226 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000227
228 fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000229 fwd_out_format_.set(output_sample_rate_hz, num_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000230 rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
231
232 // We process at the closest native rate >= min(input rate, output rate)...
233 int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate());
234 int fwd_proc_rate;
235 if (min_proc_rate > kSampleRate16kHz) {
236 fwd_proc_rate = kSampleRate32kHz;
237 } else if (min_proc_rate > kSampleRate8kHz) {
238 fwd_proc_rate = kSampleRate16kHz;
239 } else {
240 fwd_proc_rate = kSampleRate8kHz;
241 }
242 // ...with one exception.
243 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
244 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000245 }
246
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000247 fwd_proc_format_.set(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000248
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000249 // We normally process the reverse stream at 16 kHz. Unless...
250 int rev_proc_rate = kSampleRate16kHz;
251 if (fwd_proc_format_.rate() == kSampleRate8kHz) {
252 // ...the forward stream is at 8 kHz.
253 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000254 } else {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000255 if (rev_in_format_.rate() == kSampleRate32kHz) {
256 // ...or the input is at 32 kHz, in which case we use the splitting
257 // filter rather than the resampler.
258 rev_proc_rate = kSampleRate32kHz;
259 }
260 }
261
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000262 // Always downmix the reverse stream to mono for analysis. This has been
263 // demonstrated to work well for AEC in most practical scenarios.
264 rev_proc_format_.set(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000265
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000266 if (fwd_proc_format_.rate() == kSampleRate32kHz ||
267 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000268 split_rate_ = kSampleRate16kHz;
269 } else {
270 split_rate_ = fwd_proc_format_.rate();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000271 }
272
273 return InitializeLocked();
274}
275
276// Calls InitializeLocked() if any of the audio parameters have changed from
277// their current values.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000278int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz,
279 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000280 int reverse_sample_rate_hz,
281 int num_input_channels,
282 int num_output_channels,
283 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000284 if (input_sample_rate_hz == fwd_in_format_.rate() &&
285 output_sample_rate_hz == fwd_out_format_.rate() &&
286 reverse_sample_rate_hz == rev_in_format_.rate() &&
287 num_input_channels == fwd_in_format_.num_channels() &&
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000288 num_output_channels == fwd_out_format_.num_channels() &&
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000289 num_reverse_channels == rev_in_format_.num_channels()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000290 return kNoError;
291 }
292
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000293 return InitializeLocked(input_sample_rate_hz,
294 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000295 reverse_sample_rate_hz,
296 num_input_channels,
297 num_output_channels,
298 num_reverse_channels);
299}
300
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000301void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000302 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000303 std::list<ProcessingComponent*>::iterator it;
304 for (it = component_list_.begin(); it != component_list_.end(); ++it)
305 (*it)->SetExtraOptions(config);
306}
307
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000308int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000309 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000310 return fwd_in_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000311}
312
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000313int AudioProcessingImpl::sample_rate_hz() const {
314 CriticalSectionScoped crit_scoped(crit_);
315 return fwd_in_format_.rate();
316}
317
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000318int AudioProcessingImpl::proc_sample_rate_hz() const {
319 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000320}
321
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000322int AudioProcessingImpl::proc_split_sample_rate_hz() const {
323 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000324}
325
326int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000327 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000328}
329
330int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000331 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000332}
333
334int AudioProcessingImpl::num_output_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000335 return fwd_out_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000336}
337
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000338void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
339 output_will_be_muted_ = muted;
340}
341
342bool AudioProcessingImpl::output_will_be_muted() const {
343 return output_will_be_muted_;
344}
345
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000346int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000347 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000348 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000349 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000350 int output_sample_rate_hz,
351 ChannelLayout output_layout,
352 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000353 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000354 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000355 return kNullPointerError;
356 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000357
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000358 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
359 output_sample_rate_hz,
360 rev_in_format_.rate(),
361 ChannelsFromLayout(input_layout),
362 ChannelsFromLayout(output_layout),
363 rev_in_format_.num_channels()));
364 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000365 return kBadDataLengthError;
366 }
367
368#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
369 if (debug_file_->Open()) {
370 event_msg_->set_type(audioproc::Event::STREAM);
371 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000372 const size_t channel_size =
373 sizeof(float) * fwd_in_format_.samples_per_channel();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000374 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
375 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000376 }
377#endif
378
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000379 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000380 RETURN_ON_ERR(ProcessStreamLocked());
381 if (output_copy_needed(is_data_processed())) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000382 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
383 output_layout,
384 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000385 }
386
387#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
388 if (debug_file_->Open()) {
389 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000390 const size_t channel_size =
391 sizeof(float) * fwd_out_format_.samples_per_channel();
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000392 for (int i = 0; i < fwd_out_format_.num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000393 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000394 RETURN_ON_ERR(WriteMessageToDebugFile());
395 }
396#endif
397
398 return kNoError;
399}
400
401int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
402 CriticalSectionScoped crit_scoped(crit_);
403 if (!frame) {
404 return kNullPointerError;
405 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000406 // Must be a native rate.
407 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
408 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000409 frame->sample_rate_hz_ != kSampleRate32kHz &&
410 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000411 return kBadSampleRateError;
412 }
413 if (echo_control_mobile_->is_enabled() &&
414 frame->sample_rate_hz_ > kSampleRate16kHz) {
415 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
416 return kUnsupportedComponentError;
417 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000418
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000419 // TODO(ajm): The input and output rates and channels are currently
420 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000421 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000422 frame->sample_rate_hz_,
423 rev_in_format_.rate(),
424 frame->num_channels_,
425 frame->num_channels_,
426 rev_in_format_.num_channels()));
427 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000428 return kBadDataLengthError;
429 }
430
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000431#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000432 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000433 event_msg_->set_type(audioproc::Event::STREAM);
434 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000435 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000436 frame->samples_per_channel_ *
437 frame->num_channels_;
438 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000439 }
440#endif
441
442 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000443 RETURN_ON_ERR(ProcessStreamLocked());
444 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
445
446#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
447 if (debug_file_->Open()) {
448 audioproc::Stream* msg = event_msg_->mutable_stream();
449 const size_t data_size = sizeof(int16_t) *
450 frame->samples_per_channel_ *
451 frame->num_channels_;
452 msg->set_output_data(frame->data_, data_size);
453 RETURN_ON_ERR(WriteMessageToDebugFile());
454 }
455#endif
456
457 return kNoError;
458}
459
460
461int AudioProcessingImpl::ProcessStreamLocked() {
462#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
463 if (debug_file_->Open()) {
464 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000465 msg->set_delay(stream_delay_ms_);
466 msg->set_drift(echo_cancellation_->stream_drift_samples());
467 msg->set_level(gain_control_->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000468 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000469 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000470#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000471
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000472 AudioBuffer* ca = capture_audio_.get(); // For brevity.
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000473 bool data_processed = is_data_processed();
474 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000475 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000476 }
477
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000478 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
479 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000480 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000481 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000482
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000483 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000484 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000485 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000486 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
487 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
488 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
489 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000490
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000491 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000492 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000493 }
494
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000495 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000496 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000497
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000498 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000499 return kNoError;
500}
501
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000502int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
503 int samples_per_channel,
504 int sample_rate_hz,
505 ChannelLayout layout) {
506 CriticalSectionScoped crit_scoped(crit_);
507 if (data == NULL) {
508 return kNullPointerError;
509 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000510
511 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000512 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
513 fwd_out_format_.rate(),
514 sample_rate_hz,
515 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000516 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000517 num_channels));
518 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000519 return kBadDataLengthError;
520 }
521
522#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
523 if (debug_file_->Open()) {
524 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
525 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000526 const size_t channel_size =
527 sizeof(float) * rev_in_format_.samples_per_channel();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000528 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000529 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000530 RETURN_ON_ERR(WriteMessageToDebugFile());
531 }
532#endif
533
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000534 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000535 return AnalyzeReverseStreamLocked();
536}
537
niklase@google.com470e71d2011-07-07 08:21:25 +0000538int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000539 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000540 if (frame == NULL) {
541 return kNullPointerError;
542 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000543 // Must be a native rate.
544 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
545 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000546 frame->sample_rate_hz_ != kSampleRate32kHz &&
547 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000548 return kBadSampleRateError;
549 }
550 // This interface does not tolerate different forward and reverse rates.
551 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000552 return kBadSampleRateError;
553 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000554
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000555 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
556 fwd_out_format_.rate(),
557 frame->sample_rate_hz_,
558 fwd_in_format_.num_channels(),
559 fwd_in_format_.num_channels(),
560 frame->num_channels_));
561 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000562 return kBadDataLengthError;
563 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000564
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000565#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000566 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000567 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
568 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000569 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000570 frame->samples_per_channel_ *
571 frame->num_channels_;
572 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000573 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000574 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000575#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000576
577 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000578 return AnalyzeReverseStreamLocked();
579}
niklase@google.com470e71d2011-07-07 08:21:25 +0000580
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000581int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000582 AudioBuffer* ra = render_audio_.get(); // For brevity.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000583 if (rev_proc_format_.rate() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000584 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000585 }
586
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000587 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
588 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
589 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
niklase@google.com470e71d2011-07-07 08:21:25 +0000590
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000591 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000592}
593
594int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000595 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000596 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000597 delay += delay_offset_ms_;
598
niklase@google.com470e71d2011-07-07 08:21:25 +0000599 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000600 delay = 0;
601 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000602 }
603
604 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
605 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000606 delay = 500;
607 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000608 }
609
610 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000611 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000612}
613
614int AudioProcessingImpl::stream_delay_ms() const {
615 return stream_delay_ms_;
616}
617
618bool AudioProcessingImpl::was_stream_delay_set() const {
619 return was_stream_delay_set_;
620}
621
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000622void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
623 key_pressed_ = key_pressed;
624}
625
626bool AudioProcessingImpl::stream_key_pressed() const {
627 return key_pressed_;
628}
629
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000630void AudioProcessingImpl::set_delay_offset_ms(int offset) {
631 CriticalSectionScoped crit_scoped(crit_);
632 delay_offset_ms_ = offset;
633}
634
635int AudioProcessingImpl::delay_offset_ms() const {
636 return delay_offset_ms_;
637}
638
niklase@google.com470e71d2011-07-07 08:21:25 +0000639int AudioProcessingImpl::StartDebugRecording(
640 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000641 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000642 assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize);
643
644 if (filename == NULL) {
645 return kNullPointerError;
646 }
647
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000648#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000649 // Stop any ongoing recording.
650 if (debug_file_->Open()) {
651 if (debug_file_->CloseFile() == -1) {
652 return kFileError;
653 }
654 }
655
656 if (debug_file_->OpenFile(filename, false) == -1) {
657 debug_file_->CloseFile();
658 return kFileError;
659 }
660
ajm@google.com808e0e02011-08-03 21:08:51 +0000661 int err = WriteInitMessage();
662 if (err != kNoError) {
663 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000664 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000665 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000666#else
667 return kUnsupportedFunctionError;
668#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000669}
670
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000671int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
672 CriticalSectionScoped crit_scoped(crit_);
673
674 if (handle == NULL) {
675 return kNullPointerError;
676 }
677
678#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
679 // Stop any ongoing recording.
680 if (debug_file_->Open()) {
681 if (debug_file_->CloseFile() == -1) {
682 return kFileError;
683 }
684 }
685
686 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
687 return kFileError;
688 }
689
690 int err = WriteInitMessage();
691 if (err != kNoError) {
692 return err;
693 }
694 return kNoError;
695#else
696 return kUnsupportedFunctionError;
697#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
698}
699
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000700int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
701 rtc::PlatformFile handle) {
702 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
703 return StartDebugRecording(stream);
704}
705
niklase@google.com470e71d2011-07-07 08:21:25 +0000706int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000707 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000708
709#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000710 // We just return if recording hasn't started.
711 if (debug_file_->Open()) {
712 if (debug_file_->CloseFile() == -1) {
713 return kFileError;
714 }
715 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000716 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000717#else
718 return kUnsupportedFunctionError;
719#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000720}
721
722EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
723 return echo_cancellation_;
724}
725
726EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
727 return echo_control_mobile_;
728}
729
730GainControl* AudioProcessingImpl::gain_control() const {
731 return gain_control_;
732}
733
734HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
735 return high_pass_filter_;
736}
737
738LevelEstimator* AudioProcessingImpl::level_estimator() const {
739 return level_estimator_;
740}
741
742NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
743 return noise_suppression_;
744}
745
746VoiceDetection* AudioProcessingImpl::voice_detection() const {
747 return voice_detection_;
748}
749
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000750bool AudioProcessingImpl::is_data_processed() const {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000751 int enabled_count = 0;
752 std::list<ProcessingComponent*>::const_iterator it;
753 for (it = component_list_.begin(); it != component_list_.end(); it++) {
754 if ((*it)->is_component_enabled()) {
755 enabled_count++;
756 }
757 }
758
759 // Data is unchanged if no components are enabled, or if only level_estimator_
760 // or voice_detection_ is enabled.
761 if (enabled_count == 0) {
762 return false;
763 } else if (enabled_count == 1) {
764 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
765 return false;
766 }
767 } else if (enabled_count == 2) {
768 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
769 return false;
770 }
771 }
772 return true;
773}
774
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000775bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000776 // Check if we've upmixed or downmixed the audio.
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000777 return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) ||
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000778 is_data_processed);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000779}
780
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000781bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000782 return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz ||
783 fwd_proc_format_.rate() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000784}
785
786bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
787 if (!is_data_processed && !voice_detection_->is_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000788 // Only level_estimator_ is enabled.
789 return false;
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000790 } else if (fwd_proc_format_.rate() == kSampleRate32kHz ||
791 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000792 // Something besides level_estimator_ is enabled, and we have super-wb.
793 return true;
794 }
795 return false;
796}
797
798#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000799int AudioProcessingImpl::WriteMessageToDebugFile() {
800 int32_t size = event_msg_->ByteSize();
801 if (size <= 0) {
802 return kUnspecifiedError;
803 }
andrew@webrtc.org621df672013-10-22 10:27:23 +0000804#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +0000805 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
806 // pretty safe in assuming little-endian.
807#endif
808
809 if (!event_msg_->SerializeToString(&event_str_)) {
810 return kUnspecifiedError;
811 }
812
813 // Write message preceded by its size.
814 if (!debug_file_->Write(&size, sizeof(int32_t))) {
815 return kFileError;
816 }
817 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
818 return kFileError;
819 }
820
821 event_msg_->Clear();
822
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000823 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +0000824}
825
826int AudioProcessingImpl::WriteInitMessage() {
827 event_msg_->set_type(audioproc::Event::INIT);
828 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000829 msg->set_sample_rate(fwd_in_format_.rate());
830 msg->set_num_input_channels(fwd_in_format_.num_channels());
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000831 msg->set_num_output_channels(fwd_out_format_.num_channels());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000832 msg->set_num_reverse_channels(rev_in_format_.num_channels());
833 msg->set_reverse_sample_rate(rev_in_format_.rate());
834 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +0000835
836 int err = WriteMessageToDebugFile();
837 if (err != kNoError) {
838 return err;
839 }
840
841 return kNoError;
842}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000843#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000844
niklase@google.com470e71d2011-07-07 08:21:25 +0000845} // namespace webrtc