blob: bbfb771182cc7196f0e8e0ca9fb63860617d6b8f [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org40654032012-01-30 20:51:15 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000011#include "webrtc/modules/audio_processing/audio_processing_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
ajm@google.com808e0e02011-08-03 21:08:51 +000013#include <assert.h>
Michael Graczyk86c6d332015-07-23 11:41:39 -070014#include <algorithm>
niklase@google.com470e71d2011-07-07 08:21:25 +000015
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020016#include "webrtc/base/checks.h"
xians@webrtc.orge46bc772014-10-10 08:36:56 +000017#include "webrtc/base/platform_file.h"
andrew@webrtc.org17e40642014-03-04 20:58:13 +000018#include "webrtc/common_audio/include/audio_util.h"
Michael Graczykdfa36052015-03-25 16:37:27 -070019#include "webrtc/common_audio/channel_buffer.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000020#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020021extern "C" {
22#include "webrtc/modules/audio_processing/aec/aec_core.h"
23}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000024#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000025#include "webrtc/modules/audio_processing/audio_buffer.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000026#include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000027#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000028#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000029#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
30#include "webrtc/modules/audio_processing/gain_control_impl.h"
31#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
32#include "webrtc/modules/audio_processing/level_estimator_impl.h"
33#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
34#include "webrtc/modules/audio_processing/processing_component.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000035#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000036#include "webrtc/modules/audio_processing/voice_detection_impl.h"
37#include "webrtc/modules/interface/module_common_types.h"
38#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
39#include "webrtc/system_wrappers/interface/file_wrapper.h"
40#include "webrtc/system_wrappers/interface/logging.h"
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020041#include "webrtc/system_wrappers/interface/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000042
43#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
44// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000045#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000046#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000047#else
ajm@google.com808e0e02011-08-03 21:08:51 +000048#include "webrtc/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000049#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000050#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000051
Michael Graczyk86c6d332015-07-23 11:41:39 -070052#define RETURN_ON_ERR(expr) \
53 do { \
54 int err = (expr); \
55 if (err != kNoError) { \
56 return err; \
57 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000058 } while (0)
59
niklase@google.com470e71d2011-07-07 08:21:25 +000060namespace webrtc {
Michael Graczyk86c6d332015-07-23 11:41:39 -070061namespace {
62
63static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
64 switch (layout) {
65 case AudioProcessing::kMono:
66 case AudioProcessing::kStereo:
67 return false;
68 case AudioProcessing::kMonoAndKeyboard:
69 case AudioProcessing::kStereoAndKeyboard:
70 return true;
71 }
72
73 assert(false);
74 return false;
75}
76
77} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000078
79// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000080static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000081
pbos@webrtc.org788acd12014-12-15 09:41:24 +000082// This class has two main functionalities:
83//
84// 1) It is returned instead of the real GainControl after the new AGC has been
85// enabled in order to prevent an outside user from overriding compression
86// settings. It doesn't do anything in its implementation, except for
87// delegating the const methods and Enable calls to the real GainControl, so
88// AGC can still be disabled.
89//
90// 2) It is injected into AgcManagerDirect and implements volume callbacks for
91// getting and setting the volume level. It just caches this value to be used
92// in VoiceEngine later.
93class GainControlForNewAgc : public GainControl, public VolumeCallbacks {
94 public:
95 explicit GainControlForNewAgc(GainControlImpl* gain_control)
Michael Graczyk86c6d332015-07-23 11:41:39 -070096 : real_gain_control_(gain_control), volume_(0) {}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000097
98 // GainControl implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000099 int Enable(bool enable) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000100 return real_gain_control_->Enable(enable);
101 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000102 bool is_enabled() const override { return real_gain_control_->is_enabled(); }
103 int set_stream_analog_level(int level) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000104 volume_ = level;
105 return AudioProcessing::kNoError;
106 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000107 int stream_analog_level() override { return volume_; }
108 int set_mode(Mode mode) override { return AudioProcessing::kNoError; }
109 Mode mode() const override { return GainControl::kAdaptiveAnalog; }
110 int set_target_level_dbfs(int level) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000111 return AudioProcessing::kNoError;
112 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000113 int target_level_dbfs() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000114 return real_gain_control_->target_level_dbfs();
115 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000116 int set_compression_gain_db(int gain) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000117 return AudioProcessing::kNoError;
118 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000119 int compression_gain_db() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000120 return real_gain_control_->compression_gain_db();
121 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000122 int enable_limiter(bool enable) override { return AudioProcessing::kNoError; }
123 bool is_limiter_enabled() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000124 return real_gain_control_->is_limiter_enabled();
125 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000126 int set_analog_level_limits(int minimum, int maximum) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000127 return AudioProcessing::kNoError;
128 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000129 int analog_level_minimum() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000130 return real_gain_control_->analog_level_minimum();
131 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000132 int analog_level_maximum() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000133 return real_gain_control_->analog_level_maximum();
134 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000135 bool stream_is_saturated() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000136 return real_gain_control_->stream_is_saturated();
137 }
138
139 // VolumeCallbacks implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000140 void SetMicVolume(int volume) override { volume_ = volume; }
141 int GetMicVolume() override { return volume_; }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000142
143 private:
144 GainControl* real_gain_control_;
145 int volume_;
146};
147
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000148AudioProcessing* AudioProcessing::Create() {
149 Config config;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000150 return Create(config, nullptr);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000151}
152
153AudioProcessing* AudioProcessing::Create(const Config& config) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000154 return Create(config, nullptr);
155}
156
157AudioProcessing* AudioProcessing::Create(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700158 Beamformer<float>* beamformer) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000159 AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000160 if (apm->Initialize() != kNoError) {
161 delete apm;
162 apm = NULL;
163 }
164
165 return apm;
166}
167
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000168AudioProcessingImpl::AudioProcessingImpl(const Config& config)
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000169 : AudioProcessingImpl(config, nullptr) {}
170
171AudioProcessingImpl::AudioProcessingImpl(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700172 Beamformer<float>* beamformer)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000173 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 echo_control_mobile_(NULL),
175 gain_control_(NULL),
176 high_pass_filter_(NULL),
177 level_estimator_(NULL),
178 noise_suppression_(NULL),
179 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000181#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
182 debug_file_(FileWrapper::Create()),
183 event_msg_(new audioproc::Event()),
184#endif
Michael Graczyk86c6d332015-07-23 11:41:39 -0700185 api_format_({{{kSampleRate16kHz, 1, false},
186 {kSampleRate16kHz, 1, false},
187 {kSampleRate16kHz, 1, false}}}),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000188 fwd_proc_format_(kSampleRate16kHz),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000189 rev_proc_format_(kSampleRate16kHz, 1),
190 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +0000191 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000192 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000193 was_stream_delay_set_(false),
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200194 last_stream_delay_ms_(0),
195 last_aec_system_delay_ms_(0),
Bjorn Volcker4e7aa432015-07-07 11:50:05 +0200196 stream_delay_jumps_(-1),
197 aec_system_delay_jumps_(-1),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +0000198 output_will_be_muted_(false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000199 key_pressed_(false),
200#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
201 use_new_agc_(false),
202#else
203 use_new_agc_(config.Get<ExperimentalAgc>().enabled),
204#endif
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200205 agc_startup_min_volume_(config.Get<ExperimentalAgc>().startup_min_volume),
andrew1c7075f2015-06-24 18:14:14 -0700206#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
207 transient_suppressor_enabled_(false),
208#else
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000209 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700210#endif
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000211 beamformer_enabled_(config.Get<Beamforming>().enabled),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000212 beamformer_(beamformer),
aluebsb3b79b62015-07-27 10:17:58 -0700213 array_geometry_(config.Get<Beamforming>().array_geometry) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000214 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000215 component_list_.push_back(echo_cancellation_);
216
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000217 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000218 component_list_.push_back(echo_control_mobile_);
219
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000220 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000221 component_list_.push_back(gain_control_);
222
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000223 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000224 component_list_.push_back(high_pass_filter_);
225
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000226 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000227 component_list_.push_back(level_estimator_);
228
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000229 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 component_list_.push_back(noise_suppression_);
231
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000232 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000233 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000234
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000235 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
236
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000237 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000238}
239
240AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000241 {
242 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000243 // Depends on gain_control_ and gain_control_for_new_agc_.
244 agc_manager_.reset();
245 // Depends on gain_control_.
246 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000247 while (!component_list_.empty()) {
248 ProcessingComponent* component = component_list_.front();
249 component->Destroy();
250 delete component;
251 component_list_.pop_front();
252 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000253
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000254#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000255 if (debug_file_->Open()) {
256 debug_file_->CloseFile();
257 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000258#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000259 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000260 delete crit_;
261 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000262}
263
niklase@google.com470e71d2011-07-07 08:21:25 +0000264int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000265 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000266 return InitializeLocked();
267}
268
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000269int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000270 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700271
272 ProcessingConfig processing_config = api_format_;
273 processing_config.input_stream().set_sample_rate_hz(rate);
274 processing_config.output_stream().set_sample_rate_hz(rate);
275 return InitializeLocked(processing_config);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000276}
277
278int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
279 int output_sample_rate_hz,
280 int reverse_sample_rate_hz,
281 ChannelLayout input_layout,
282 ChannelLayout output_layout,
283 ChannelLayout reverse_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700284 const ProcessingConfig processing_config = {
285 {{input_sample_rate_hz, ChannelsFromLayout(input_layout),
286 LayoutHasKeyboard(input_layout)},
287 {output_sample_rate_hz, ChannelsFromLayout(output_layout),
288 LayoutHasKeyboard(output_layout)},
289 {reverse_sample_rate_hz, ChannelsFromLayout(reverse_layout),
290 LayoutHasKeyboard(reverse_layout)}}};
291
292 return Initialize(processing_config);
293}
294
295int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000296 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700297 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000298}
299
niklase@google.com470e71d2011-07-07 08:21:25 +0000300int AudioProcessingImpl::InitializeLocked() {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700301 const int fwd_audio_buffer_channels =
302 beamformer_enabled_ ? api_format_.input_stream().num_channels()
303 : api_format_.output_stream().num_channels();
304 if (api_format_.reverse_stream().num_channels() > 0) {
305 render_audio_.reset(new AudioBuffer(
306 api_format_.reverse_stream().num_frames(),
307 api_format_.reverse_stream().num_channels(),
308 rev_proc_format_.num_frames(), rev_proc_format_.num_channels(),
309 rev_proc_format_.num_frames()));
310 } else {
311 render_audio_.reset(nullptr);
312 }
313 capture_audio_.reset(new AudioBuffer(
314 api_format_.input_stream().num_frames(),
315 api_format_.input_stream().num_channels(), fwd_proc_format_.num_frames(),
316 fwd_audio_buffer_channels, api_format_.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000317
niklase@google.com470e71d2011-07-07 08:21:25 +0000318 // Initialize all components.
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000319 for (auto item : component_list_) {
320 int err = item->Initialize();
niklase@google.com470e71d2011-07-07 08:21:25 +0000321 if (err != kNoError) {
322 return err;
323 }
324 }
325
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200326 InitializeExperimentalAgc();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000327
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200328 InitializeTransient();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000329
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000330 InitializeBeamformer();
331
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000332#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000333 if (debug_file_->Open()) {
334 int err = WriteInitMessage();
335 if (err != kNoError) {
336 return err;
337 }
338 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000339#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000340
niklase@google.com470e71d2011-07-07 08:21:25 +0000341 return kNoError;
342}
343
Michael Graczyk86c6d332015-07-23 11:41:39 -0700344int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
345 for (const auto& stream : config.streams) {
346 if (stream.num_channels() < 0) {
347 return kBadNumberChannelsError;
348 }
349 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
350 return kBadSampleRateError;
351 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000352 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700353
354 const int num_in_channels = config.input_stream().num_channels();
355 const int num_out_channels = config.output_stream().num_channels();
356
357 // Need at least one input channel.
358 // Need either one output channel or as many outputs as there are inputs.
359 if (num_in_channels == 0 ||
360 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700361 return kBadNumberChannelsError;
362 }
363
Michael Graczyk86c6d332015-07-23 11:41:39 -0700364 if (beamformer_enabled_ &&
365 (static_cast<size_t>(num_in_channels) != array_geometry_.size() ||
366 num_out_channels > 1)) {
367 return kBadNumberChannelsError;
368 }
369
370 api_format_ = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000371
372 // We process at the closest native rate >= min(input rate, output rate)...
Michael Graczyk86c6d332015-07-23 11:41:39 -0700373 const int min_proc_rate =
374 std::min(api_format_.input_stream().sample_rate_hz(),
375 api_format_.output_stream().sample_rate_hz());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000376 int fwd_proc_rate;
aluebsb3b79b62015-07-27 10:17:58 -0700377 if (min_proc_rate > kSampleRate32kHz) {
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000378 fwd_proc_rate = kSampleRate48kHz;
379 } else if (min_proc_rate > kSampleRate16kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000380 fwd_proc_rate = kSampleRate32kHz;
381 } else if (min_proc_rate > kSampleRate8kHz) {
382 fwd_proc_rate = kSampleRate16kHz;
383 } else {
384 fwd_proc_rate = kSampleRate8kHz;
385 }
386 // ...with one exception.
387 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
388 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000389 }
390
Michael Graczyk86c6d332015-07-23 11:41:39 -0700391 fwd_proc_format_ = StreamConfig(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000392
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000393 // We normally process the reverse stream at 16 kHz. Unless...
394 int rev_proc_rate = kSampleRate16kHz;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700395 if (fwd_proc_format_.sample_rate_hz() == kSampleRate8kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000396 // ...the forward stream is at 8 kHz.
397 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000398 } else {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700399 if (api_format_.reverse_stream().sample_rate_hz() == kSampleRate32kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000400 // ...or the input is at 32 kHz, in which case we use the splitting
401 // filter rather than the resampler.
402 rev_proc_rate = kSampleRate32kHz;
403 }
404 }
405
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000406 // Always downmix the reverse stream to mono for analysis. This has been
407 // demonstrated to work well for AEC in most practical scenarios.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700408 rev_proc_format_ = StreamConfig(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000409
Michael Graczyk86c6d332015-07-23 11:41:39 -0700410 if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
411 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000412 split_rate_ = kSampleRate16kHz;
413 } else {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700414 split_rate_ = fwd_proc_format_.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000415 }
416
417 return InitializeLocked();
418}
419
420// Calls InitializeLocked() if any of the audio parameters have changed from
421// their current values.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700422int AudioProcessingImpl::MaybeInitializeLocked(
423 const ProcessingConfig& processing_config) {
424 if (processing_config == api_format_) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000425 return kNoError;
426 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700427 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000428}
429
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000430void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000431 CriticalSectionScoped crit_scoped(crit_);
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000432 for (auto item : component_list_) {
433 item->SetExtraOptions(config);
434 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000435
436 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
437 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
438 InitializeTransient();
439 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000440}
441
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000442int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000443 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700444 return api_format_.input_stream().sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000445}
446
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000447int AudioProcessingImpl::sample_rate_hz() const {
448 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700449 return api_format_.input_stream().sample_rate_hz();
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000450}
451
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000452int AudioProcessingImpl::proc_sample_rate_hz() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700453 return fwd_proc_format_.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000454}
455
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000456int AudioProcessingImpl::proc_split_sample_rate_hz() const {
457 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000458}
459
460int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000461 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000462}
463
464int AudioProcessingImpl::num_input_channels() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700465 return api_format_.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000466}
467
468int AudioProcessingImpl::num_output_channels() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700469 return api_format_.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000470}
471
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000472void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000473 CriticalSectionScoped lock(crit_);
Bjorn Volcker424694c2015-03-27 11:30:43 +0100474 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000475 if (agc_manager_.get()) {
476 agc_manager_->SetCaptureMuted(output_will_be_muted_);
477 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000478}
479
480bool AudioProcessingImpl::output_will_be_muted() const {
Bjorn Volcker424694c2015-03-27 11:30:43 +0100481 CriticalSectionScoped lock(crit_);
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000482 return output_will_be_muted_;
483}
484
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000485int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000486 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000487 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000488 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000489 int output_sample_rate_hz,
490 ChannelLayout output_layout,
491 float* const* dest) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700492 StreamConfig input_stream = api_format_.input_stream();
493 input_stream.set_sample_rate_hz(input_sample_rate_hz);
494 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
495 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
496
497 StreamConfig output_stream = api_format_.output_stream();
498 output_stream.set_sample_rate_hz(output_sample_rate_hz);
499 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
500 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
501
502 if (samples_per_channel != input_stream.num_frames()) {
503 return kBadDataLengthError;
504 }
505 return ProcessStream(src, input_stream, output_stream, dest);
506}
507
508int AudioProcessingImpl::ProcessStream(const float* const* src,
509 const StreamConfig& input_config,
510 const StreamConfig& output_config,
511 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000512 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000513 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000514 return kNullPointerError;
515 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000516
Michael Graczyk86c6d332015-07-23 11:41:39 -0700517 ProcessingConfig processing_config = api_format_;
518 processing_config.input_stream() = input_config;
519 processing_config.output_stream() = output_config;
520
521 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
522 assert(processing_config.input_stream().num_frames() ==
523 api_format_.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000524
525#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
526 if (debug_file_->Open()) {
527 event_msg_->set_type(audioproc::Event::STREAM);
528 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000529 const size_t channel_size =
Michael Graczyk86c6d332015-07-23 11:41:39 -0700530 sizeof(float) * api_format_.input_stream().num_frames();
531 for (int i = 0; i < api_format_.input_stream().num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000532 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000533 }
534#endif
535
Michael Graczyk86c6d332015-07-23 11:41:39 -0700536 capture_audio_->CopyFrom(src, api_format_.input_stream());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000537 RETURN_ON_ERR(ProcessStreamLocked());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700538 capture_audio_->CopyTo(api_format_.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000539
540#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
541 if (debug_file_->Open()) {
542 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000543 const size_t channel_size =
Michael Graczyk86c6d332015-07-23 11:41:39 -0700544 sizeof(float) * api_format_.output_stream().num_frames();
545 for (int i = 0; i < api_format_.output_stream().num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000546 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000547 RETURN_ON_ERR(WriteMessageToDebugFile());
548 }
549#endif
550
551 return kNoError;
552}
553
554int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
555 CriticalSectionScoped crit_scoped(crit_);
556 if (!frame) {
557 return kNullPointerError;
558 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000559 // Must be a native rate.
560 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
561 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000562 frame->sample_rate_hz_ != kSampleRate32kHz &&
563 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000564 return kBadSampleRateError;
565 }
566 if (echo_control_mobile_->is_enabled() &&
567 frame->sample_rate_hz_ > kSampleRate16kHz) {
568 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
569 return kUnsupportedComponentError;
570 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000571
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000572 // TODO(ajm): The input and output rates and channels are currently
573 // constrained to be identical in the int16 interface.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700574 ProcessingConfig processing_config = api_format_;
575 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
576 processing_config.input_stream().set_num_channels(frame->num_channels_);
577 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
578 processing_config.output_stream().set_num_channels(frame->num_channels_);
579
580 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
581 if (frame->samples_per_channel_ != api_format_.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000582 return kBadDataLengthError;
583 }
584
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000585#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000586 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000587 event_msg_->set_type(audioproc::Event::STREAM);
588 audioproc::Stream* msg = event_msg_->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700589 const size_t data_size =
590 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000591 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000592 }
593#endif
594
595 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000596 RETURN_ON_ERR(ProcessStreamLocked());
597 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
598
599#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
600 if (debug_file_->Open()) {
601 audioproc::Stream* msg = event_msg_->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700602 const size_t data_size =
603 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000604 msg->set_output_data(frame->data_, data_size);
605 RETURN_ON_ERR(WriteMessageToDebugFile());
606 }
607#endif
608
609 return kNoError;
610}
611
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000612int AudioProcessingImpl::ProcessStreamLocked() {
613#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
614 if (debug_file_->Open()) {
615 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000616 msg->set_delay(stream_delay_ms_);
617 msg->set_drift(echo_cancellation_->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +0000618 msg->set_level(gain_control()->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000619 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000620 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000621#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000622
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200623 MaybeUpdateHistograms();
624
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000625 AudioBuffer* ca = capture_audio_.get(); // For brevity.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000626 if (use_new_agc_ && gain_control_->is_enabled()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700627 agc_manager_->AnalyzePreProcess(ca->channels()[0], ca->num_channels(),
628 fwd_proc_format_.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000629 }
630
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000631 bool data_processed = is_data_processed();
632 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000633 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000634 }
635
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000636 if (beamformer_enabled_) {
Michael Graczykdfa36052015-03-25 16:37:27 -0700637 beamformer_->ProcessChunk(*ca->split_data_f(), ca->split_data_f());
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000638 ca->set_num_channels(1);
639 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000640
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000641 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
642 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000643 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000644 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000645
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000646 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000647 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000648 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000649 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
650 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
651 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000652
Michael Graczyk86c6d332015-07-23 11:41:39 -0700653 if (use_new_agc_ && gain_control_->is_enabled() &&
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000654 (!beamformer_enabled_ || beamformer_->is_target_present())) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000655 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
Michael Graczyk86c6d332015-07-23 11:41:39 -0700656 ca->num_frames_per_band(), split_rate_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000657 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000658 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000659
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000660 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000661 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000662 }
663
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000664 // TODO(aluebs): Investigate if the transient suppression placement should be
665 // before or after the AGC.
666 if (transient_suppressor_enabled_) {
667 float voice_probability =
668 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
669
Michael Graczyk86c6d332015-07-23 11:41:39 -0700670 transient_suppressor_->Suppress(
671 ca->channels_f()[0], ca->num_frames(), ca->num_channels(),
672 ca->split_bands_const_f(0)[kBand0To8kHz], ca->num_frames_per_band(),
673 ca->keyboard_data(), ca->num_keyboard_frames(), voice_probability,
674 key_pressed_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000675 }
676
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000677 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000678 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000679
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000680 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000681 return kNoError;
682}
683
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000684int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
685 int samples_per_channel,
686 int sample_rate_hz,
687 ChannelLayout layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700688 const StreamConfig reverse_config = {
689 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
690 };
691 if (samples_per_channel != reverse_config.num_frames()) {
692 return kBadDataLengthError;
693 }
694 return AnalyzeReverseStream(data, reverse_config);
695}
696
697int AudioProcessingImpl::AnalyzeReverseStream(
698 const float* const* data,
699 const StreamConfig& reverse_config) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000700 CriticalSectionScoped crit_scoped(crit_);
701 if (data == NULL) {
702 return kNullPointerError;
703 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000704
Michael Graczyk86c6d332015-07-23 11:41:39 -0700705 if (reverse_config.num_channels() <= 0) {
706 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000707 }
708
Michael Graczyk86c6d332015-07-23 11:41:39 -0700709 ProcessingConfig processing_config = api_format_;
710 processing_config.reverse_stream() = reverse_config;
711
712 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
713 assert(reverse_config.num_frames() ==
714 api_format_.reverse_stream().num_frames());
715
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000716#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
717 if (debug_file_->Open()) {
718 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
719 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000720 const size_t channel_size =
Michael Graczyk86c6d332015-07-23 11:41:39 -0700721 sizeof(float) * api_format_.reverse_stream().num_frames();
722 for (int i = 0; i < api_format_.reverse_stream().num_channels(); ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000723 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000724 RETURN_ON_ERR(WriteMessageToDebugFile());
725 }
726#endif
727
Michael Graczyk86c6d332015-07-23 11:41:39 -0700728 render_audio_->CopyFrom(data, api_format_.reverse_stream());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000729 return AnalyzeReverseStreamLocked();
730}
731
niklase@google.com470e71d2011-07-07 08:21:25 +0000732int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000733 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000734 if (frame == NULL) {
735 return kNullPointerError;
736 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000737 // Must be a native rate.
738 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
739 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000740 frame->sample_rate_hz_ != kSampleRate32kHz &&
741 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000742 return kBadSampleRateError;
743 }
744 // This interface does not tolerate different forward and reverse rates.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700745 if (frame->sample_rate_hz_ != api_format_.input_stream().sample_rate_hz()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000746 return kBadSampleRateError;
747 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000748
Michael Graczyk86c6d332015-07-23 11:41:39 -0700749 if (frame->num_channels_ <= 0) {
750 return kBadNumberChannelsError;
751 }
752
753 ProcessingConfig processing_config = api_format_;
754 processing_config.reverse_stream().set_sample_rate_hz(frame->sample_rate_hz_);
755 processing_config.reverse_stream().set_num_channels(frame->num_channels_);
756
757 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
758 if (frame->samples_per_channel_ !=
759 api_format_.reverse_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000760 return kBadDataLengthError;
761 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000762
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000763#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000764 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000765 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
766 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700767 const size_t data_size =
768 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000769 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000770 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000771 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000772#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000773
774 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000775 return AnalyzeReverseStreamLocked();
776}
niklase@google.com470e71d2011-07-07 08:21:25 +0000777
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000778int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000779 AudioBuffer* ra = render_audio_.get(); // For brevity.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700780 if (rev_proc_format_.sample_rate_hz() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000781 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000782 }
783
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000784 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
785 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000786 if (!use_new_agc_) {
787 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
788 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000789
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000790 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000791}
792
793int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000794 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000795 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000796 delay += delay_offset_ms_;
797
niklase@google.com470e71d2011-07-07 08:21:25 +0000798 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000799 delay = 0;
800 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000801 }
802
803 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
804 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000805 delay = 500;
806 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000807 }
808
809 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000810 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000811}
812
813int AudioProcessingImpl::stream_delay_ms() const {
814 return stream_delay_ms_;
815}
816
817bool AudioProcessingImpl::was_stream_delay_set() const {
818 return was_stream_delay_set_;
819}
820
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000821void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
822 key_pressed_ = key_pressed;
823}
824
825bool AudioProcessingImpl::stream_key_pressed() const {
826 return key_pressed_;
827}
828
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000829void AudioProcessingImpl::set_delay_offset_ms(int offset) {
830 CriticalSectionScoped crit_scoped(crit_);
831 delay_offset_ms_ = offset;
832}
833
834int AudioProcessingImpl::delay_offset_ms() const {
835 return delay_offset_ms_;
836}
837
niklase@google.com470e71d2011-07-07 08:21:25 +0000838int AudioProcessingImpl::StartDebugRecording(
839 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000840 CriticalSectionScoped crit_scoped(crit_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +0200841 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +0000842
843 if (filename == NULL) {
844 return kNullPointerError;
845 }
846
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000847#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000848 // Stop any ongoing recording.
849 if (debug_file_->Open()) {
850 if (debug_file_->CloseFile() == -1) {
851 return kFileError;
852 }
853 }
854
855 if (debug_file_->OpenFile(filename, false) == -1) {
856 debug_file_->CloseFile();
857 return kFileError;
858 }
859
ajm@google.com808e0e02011-08-03 21:08:51 +0000860 int err = WriteInitMessage();
861 if (err != kNoError) {
862 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000863 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000864 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000865#else
866 return kUnsupportedFunctionError;
867#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000868}
869
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000870int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
871 CriticalSectionScoped crit_scoped(crit_);
872
873 if (handle == NULL) {
874 return kNullPointerError;
875 }
876
877#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
878 // Stop any ongoing recording.
879 if (debug_file_->Open()) {
880 if (debug_file_->CloseFile() == -1) {
881 return kFileError;
882 }
883 }
884
885 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
886 return kFileError;
887 }
888
889 int err = WriteInitMessage();
890 if (err != kNoError) {
891 return err;
892 }
893 return kNoError;
894#else
895 return kUnsupportedFunctionError;
896#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
897}
898
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000899int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
900 rtc::PlatformFile handle) {
901 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
902 return StartDebugRecording(stream);
903}
904
niklase@google.com470e71d2011-07-07 08:21:25 +0000905int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000906 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000907
908#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000909 // We just return if recording hasn't started.
910 if (debug_file_->Open()) {
911 if (debug_file_->CloseFile() == -1) {
912 return kFileError;
913 }
914 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000915 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000916#else
917 return kUnsupportedFunctionError;
918#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000919}
920
921EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
922 return echo_cancellation_;
923}
924
925EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
926 return echo_control_mobile_;
927}
928
929GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000930 if (use_new_agc_) {
931 return gain_control_for_new_agc_.get();
932 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000933 return gain_control_;
934}
935
936HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
937 return high_pass_filter_;
938}
939
940LevelEstimator* AudioProcessingImpl::level_estimator() const {
941 return level_estimator_;
942}
943
944NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
945 return noise_suppression_;
946}
947
948VoiceDetection* AudioProcessingImpl::voice_detection() const {
949 return voice_detection_;
950}
951
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000952bool AudioProcessingImpl::is_data_processed() const {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000953 if (beamformer_enabled_) {
954 return true;
955 }
956
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000957 int enabled_count = 0;
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000958 for (auto item : component_list_) {
959 if (item->is_component_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000960 enabled_count++;
961 }
962 }
963
964 // Data is unchanged if no components are enabled, or if only level_estimator_
965 // or voice_detection_ is enabled.
966 if (enabled_count == 0) {
967 return false;
968 } else if (enabled_count == 1) {
969 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
970 return false;
971 }
972 } else if (enabled_count == 2) {
973 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
974 return false;
975 }
976 }
977 return true;
978}
979
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000980bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000981 // Check if we've upmixed or downmixed the audio.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700982 return ((api_format_.output_stream().num_channels() !=
983 api_format_.input_stream().num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000984 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000985}
986
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000987bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700988 return (is_data_processed &&
989 (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
990 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000991}
992
993bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000994 if (!is_data_processed && !voice_detection_->is_enabled() &&
995 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000996 // Only level_estimator_ is enabled.
997 return false;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700998 } else if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
999 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001000 // Something besides level_estimator_ is enabled, and we have super-wb.
1001 return true;
1002 }
1003 return false;
1004}
1005
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001006void AudioProcessingImpl::InitializeExperimentalAgc() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001007 if (use_new_agc_) {
1008 if (!agc_manager_.get()) {
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001009 agc_manager_.reset(new AgcManagerDirect(gain_control_,
1010 gain_control_for_new_agc_.get(),
1011 agc_startup_min_volume_));
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001012 }
1013 agc_manager_->Initialize();
1014 agc_manager_->SetCaptureMuted(output_will_be_muted_);
1015 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001016}
1017
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001018void AudioProcessingImpl::InitializeTransient() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001019 if (transient_suppressor_enabled_) {
1020 if (!transient_suppressor_.get()) {
1021 transient_suppressor_.reset(new TransientSuppressor());
1022 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001023 transient_suppressor_->Initialize(
1024 fwd_proc_format_.sample_rate_hz(), split_rate_,
1025 api_format_.output_stream().num_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001026 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001027}
1028
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001029void AudioProcessingImpl::InitializeBeamformer() {
1030 if (beamformer_enabled_) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001031 if (!beamformer_) {
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001032 beamformer_.reset(new NonlinearBeamformer(array_geometry_));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001033 }
1034 beamformer_->Initialize(kChunkSizeMs, split_rate_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001035 }
1036}
1037
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001038void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001039 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001040
1041 if (echo_cancellation()->is_enabled()) {
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001042 // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
1043 // If a stream has echo we know that the echo_cancellation is in process.
1044 if (stream_delay_jumps_ == -1 && echo_cancellation()->stream_has_echo()) {
1045 stream_delay_jumps_ = 0;
1046 }
1047 if (aec_system_delay_jumps_ == -1 &&
1048 echo_cancellation()->stream_has_echo()) {
1049 aec_system_delay_jumps_ = 0;
1050 }
1051
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001052 // Detect a jump in platform reported system delay and log the difference.
1053 const int diff_stream_delay_ms = stream_delay_ms_ - last_stream_delay_ms_;
1054 if (diff_stream_delay_ms > kMinDiffDelayMs && last_stream_delay_ms_ != 0) {
1055 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1056 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001057 if (stream_delay_jumps_ == -1) {
1058 stream_delay_jumps_ = 0; // Activate counter if needed.
1059 }
1060 stream_delay_jumps_++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001061 }
1062 last_stream_delay_ms_ = stream_delay_ms_;
1063
1064 // Detect a jump in AEC system delay and log the difference.
1065 const int frames_per_ms = rtc::CheckedDivExact(split_rate_, 1000);
1066 const int aec_system_delay_ms =
1067 WebRtcAec_system_delay(echo_cancellation()->aec_core()) / frames_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001068 const int diff_aec_system_delay_ms =
1069 aec_system_delay_ms - last_aec_system_delay_ms_;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001070 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
1071 last_aec_system_delay_ms_ != 0) {
1072 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1073 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1074 100);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001075 if (aec_system_delay_jumps_ == -1) {
1076 aec_system_delay_jumps_ = 0; // Activate counter if needed.
1077 }
1078 aec_system_delay_jumps_++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001079 }
1080 last_aec_system_delay_ms_ = aec_system_delay_ms;
1081 }
1082}
1083
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001084void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
1085 CriticalSectionScoped crit_scoped(crit_);
1086 if (stream_delay_jumps_ > -1) {
1087 RTC_HISTOGRAM_ENUMERATION(
1088 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
1089 stream_delay_jumps_, 51);
1090 }
1091 stream_delay_jumps_ = -1;
1092 last_stream_delay_ms_ = 0;
1093
1094 if (aec_system_delay_jumps_ > -1) {
1095 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1096 aec_system_delay_jumps_, 51);
1097 }
1098 aec_system_delay_jumps_ = -1;
1099 last_aec_system_delay_ms_ = 0;
1100}
1101
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001102#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +00001103int AudioProcessingImpl::WriteMessageToDebugFile() {
1104 int32_t size = event_msg_->ByteSize();
1105 if (size <= 0) {
1106 return kUnspecifiedError;
1107 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001108#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Michael Graczyk86c6d332015-07-23 11:41:39 -07001109// TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1110// pretty safe in assuming little-endian.
ajm@google.com808e0e02011-08-03 21:08:51 +00001111#endif
1112
1113 if (!event_msg_->SerializeToString(&event_str_)) {
1114 return kUnspecifiedError;
1115 }
1116
1117 // Write message preceded by its size.
1118 if (!debug_file_->Write(&size, sizeof(int32_t))) {
1119 return kFileError;
1120 }
1121 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
1122 return kFileError;
1123 }
1124
1125 event_msg_->Clear();
1126
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001127 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001128}
1129
1130int AudioProcessingImpl::WriteInitMessage() {
1131 event_msg_->set_type(audioproc::Event::INIT);
1132 audioproc::Init* msg = event_msg_->mutable_init();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001133 msg->set_sample_rate(api_format_.input_stream().sample_rate_hz());
1134 msg->set_num_input_channels(api_format_.input_stream().num_channels());
1135 msg->set_num_output_channels(api_format_.output_stream().num_channels());
1136 msg->set_num_reverse_channels(api_format_.reverse_stream().num_channels());
1137 msg->set_reverse_sample_rate(api_format_.reverse_stream().sample_rate_hz());
1138 msg->set_output_sample_rate(api_format_.output_stream().sample_rate_hz());
ajm@google.com808e0e02011-08-03 21:08:51 +00001139
1140 int err = WriteMessageToDebugFile();
1141 if (err != kNoError) {
1142 return err;
1143 }
1144
1145 return kNoError;
1146}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001147#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001148
niklase@google.com470e71d2011-07-07 08:21:25 +00001149} // namespace webrtc