blob: 3d62439bd5c0384149b3d101a35695185f06faec [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),
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000213 array_geometry_(config.Get<Beamforming>().array_geometry),
214 supports_48kHz_(config.Get<AudioProcessing48kHzSupport>().enabled) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000215 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 component_list_.push_back(echo_cancellation_);
217
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000218 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000219 component_list_.push_back(echo_control_mobile_);
220
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000221 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222 component_list_.push_back(gain_control_);
223
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000224 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000225 component_list_.push_back(high_pass_filter_);
226
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000227 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000228 component_list_.push_back(level_estimator_);
229
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000230 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000231 component_list_.push_back(noise_suppression_);
232
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000233 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000234 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000235
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000236 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
237
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000238 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000239}
240
241AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000242 {
243 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000244 // Depends on gain_control_ and gain_control_for_new_agc_.
245 agc_manager_.reset();
246 // Depends on gain_control_.
247 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000248 while (!component_list_.empty()) {
249 ProcessingComponent* component = component_list_.front();
250 component->Destroy();
251 delete component;
252 component_list_.pop_front();
253 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000254
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000255#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000256 if (debug_file_->Open()) {
257 debug_file_->CloseFile();
258 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000259#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000260 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000261 delete crit_;
262 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000263}
264
niklase@google.com470e71d2011-07-07 08:21:25 +0000265int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000266 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000267 return InitializeLocked();
268}
269
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000270int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000271 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700272
273 ProcessingConfig processing_config = api_format_;
274 processing_config.input_stream().set_sample_rate_hz(rate);
275 processing_config.output_stream().set_sample_rate_hz(rate);
276 return InitializeLocked(processing_config);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000277}
278
279int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
280 int output_sample_rate_hz,
281 int reverse_sample_rate_hz,
282 ChannelLayout input_layout,
283 ChannelLayout output_layout,
284 ChannelLayout reverse_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700285 const ProcessingConfig processing_config = {
286 {{input_sample_rate_hz, ChannelsFromLayout(input_layout),
287 LayoutHasKeyboard(input_layout)},
288 {output_sample_rate_hz, ChannelsFromLayout(output_layout),
289 LayoutHasKeyboard(output_layout)},
290 {reverse_sample_rate_hz, ChannelsFromLayout(reverse_layout),
291 LayoutHasKeyboard(reverse_layout)}}};
292
293 return Initialize(processing_config);
294}
295
296int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000297 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700298 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000299}
300
niklase@google.com470e71d2011-07-07 08:21:25 +0000301int AudioProcessingImpl::InitializeLocked() {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700302 const int fwd_audio_buffer_channels =
303 beamformer_enabled_ ? api_format_.input_stream().num_channels()
304 : api_format_.output_stream().num_channels();
305 if (api_format_.reverse_stream().num_channels() > 0) {
306 render_audio_.reset(new AudioBuffer(
307 api_format_.reverse_stream().num_frames(),
308 api_format_.reverse_stream().num_channels(),
309 rev_proc_format_.num_frames(), rev_proc_format_.num_channels(),
310 rev_proc_format_.num_frames()));
311 } else {
312 render_audio_.reset(nullptr);
313 }
314 capture_audio_.reset(new AudioBuffer(
315 api_format_.input_stream().num_frames(),
316 api_format_.input_stream().num_channels(), fwd_proc_format_.num_frames(),
317 fwd_audio_buffer_channels, api_format_.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000318
niklase@google.com470e71d2011-07-07 08:21:25 +0000319 // Initialize all components.
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000320 for (auto item : component_list_) {
321 int err = item->Initialize();
niklase@google.com470e71d2011-07-07 08:21:25 +0000322 if (err != kNoError) {
323 return err;
324 }
325 }
326
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200327 InitializeExperimentalAgc();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000328
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200329 InitializeTransient();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000330
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000331 InitializeBeamformer();
332
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000333#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000334 if (debug_file_->Open()) {
335 int err = WriteInitMessage();
336 if (err != kNoError) {
337 return err;
338 }
339 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000340#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000341
niklase@google.com470e71d2011-07-07 08:21:25 +0000342 return kNoError;
343}
344
Michael Graczyk86c6d332015-07-23 11:41:39 -0700345int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
346 for (const auto& stream : config.streams) {
347 if (stream.num_channels() < 0) {
348 return kBadNumberChannelsError;
349 }
350 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
351 return kBadSampleRateError;
352 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000353 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700354
355 const int num_in_channels = config.input_stream().num_channels();
356 const int num_out_channels = config.output_stream().num_channels();
357
358 // Need at least one input channel.
359 // Need either one output channel or as many outputs as there are inputs.
360 if (num_in_channels == 0 ||
361 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700362 return kBadNumberChannelsError;
363 }
364
Michael Graczyk86c6d332015-07-23 11:41:39 -0700365 if (beamformer_enabled_ &&
366 (static_cast<size_t>(num_in_channels) != array_geometry_.size() ||
367 num_out_channels > 1)) {
368 return kBadNumberChannelsError;
369 }
370
371 api_format_ = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000372
373 // We process at the closest native rate >= min(input rate, output rate)...
Michael Graczyk86c6d332015-07-23 11:41:39 -0700374 const int min_proc_rate =
375 std::min(api_format_.input_stream().sample_rate_hz(),
376 api_format_.output_stream().sample_rate_hz());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000377 int fwd_proc_rate;
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000378 if (supports_48kHz_ && min_proc_rate > kSampleRate32kHz) {
379 fwd_proc_rate = kSampleRate48kHz;
380 } else if (min_proc_rate > kSampleRate16kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000381 fwd_proc_rate = kSampleRate32kHz;
382 } else if (min_proc_rate > kSampleRate8kHz) {
383 fwd_proc_rate = kSampleRate16kHz;
384 } else {
385 fwd_proc_rate = kSampleRate8kHz;
386 }
387 // ...with one exception.
388 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
389 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000390 }
391
Michael Graczyk86c6d332015-07-23 11:41:39 -0700392 fwd_proc_format_ = StreamConfig(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000393
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000394 // We normally process the reverse stream at 16 kHz. Unless...
395 int rev_proc_rate = kSampleRate16kHz;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700396 if (fwd_proc_format_.sample_rate_hz() == kSampleRate8kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000397 // ...the forward stream is at 8 kHz.
398 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000399 } else {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700400 if (api_format_.reverse_stream().sample_rate_hz() == kSampleRate32kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000401 // ...or the input is at 32 kHz, in which case we use the splitting
402 // filter rather than the resampler.
403 rev_proc_rate = kSampleRate32kHz;
404 }
405 }
406
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000407 // Always downmix the reverse stream to mono for analysis. This has been
408 // demonstrated to work well for AEC in most practical scenarios.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700409 rev_proc_format_ = StreamConfig(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000410
Michael Graczyk86c6d332015-07-23 11:41:39 -0700411 if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
412 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000413 split_rate_ = kSampleRate16kHz;
414 } else {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700415 split_rate_ = fwd_proc_format_.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000416 }
417
418 return InitializeLocked();
419}
420
421// Calls InitializeLocked() if any of the audio parameters have changed from
422// their current values.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700423int AudioProcessingImpl::MaybeInitializeLocked(
424 const ProcessingConfig& processing_config) {
425 if (processing_config == api_format_) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000426 return kNoError;
427 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700428 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000429}
430
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000431void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000432 CriticalSectionScoped crit_scoped(crit_);
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000433 for (auto item : component_list_) {
434 item->SetExtraOptions(config);
435 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000436
437 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
438 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
439 InitializeTransient();
440 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000441}
442
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000443int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000444 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700445 return api_format_.input_stream().sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000446}
447
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000448int AudioProcessingImpl::sample_rate_hz() const {
449 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700450 return api_format_.input_stream().sample_rate_hz();
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000451}
452
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000453int AudioProcessingImpl::proc_sample_rate_hz() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700454 return fwd_proc_format_.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000455}
456
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000457int AudioProcessingImpl::proc_split_sample_rate_hz() const {
458 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000459}
460
461int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000462 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000463}
464
465int AudioProcessingImpl::num_input_channels() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700466 return api_format_.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000467}
468
469int AudioProcessingImpl::num_output_channels() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700470 return api_format_.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000471}
472
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000473void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000474 CriticalSectionScoped lock(crit_);
Bjorn Volcker424694c2015-03-27 11:30:43 +0100475 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000476 if (agc_manager_.get()) {
477 agc_manager_->SetCaptureMuted(output_will_be_muted_);
478 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000479}
480
481bool AudioProcessingImpl::output_will_be_muted() const {
Bjorn Volcker424694c2015-03-27 11:30:43 +0100482 CriticalSectionScoped lock(crit_);
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000483 return output_will_be_muted_;
484}
485
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000486int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000487 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000488 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000489 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000490 int output_sample_rate_hz,
491 ChannelLayout output_layout,
492 float* const* dest) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700493 StreamConfig input_stream = api_format_.input_stream();
494 input_stream.set_sample_rate_hz(input_sample_rate_hz);
495 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
496 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
497
498 StreamConfig output_stream = api_format_.output_stream();
499 output_stream.set_sample_rate_hz(output_sample_rate_hz);
500 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
501 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
502
503 if (samples_per_channel != input_stream.num_frames()) {
504 return kBadDataLengthError;
505 }
506 return ProcessStream(src, input_stream, output_stream, dest);
507}
508
509int AudioProcessingImpl::ProcessStream(const float* const* src,
510 const StreamConfig& input_config,
511 const StreamConfig& output_config,
512 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000513 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000514 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000515 return kNullPointerError;
516 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000517
Michael Graczyk86c6d332015-07-23 11:41:39 -0700518 ProcessingConfig processing_config = api_format_;
519 processing_config.input_stream() = input_config;
520 processing_config.output_stream() = output_config;
521
522 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
523 assert(processing_config.input_stream().num_frames() ==
524 api_format_.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000525
526#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
527 if (debug_file_->Open()) {
528 event_msg_->set_type(audioproc::Event::STREAM);
529 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000530 const size_t channel_size =
Michael Graczyk86c6d332015-07-23 11:41:39 -0700531 sizeof(float) * api_format_.input_stream().num_frames();
532 for (int i = 0; i < api_format_.input_stream().num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000533 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000534 }
535#endif
536
Michael Graczyk86c6d332015-07-23 11:41:39 -0700537 capture_audio_->CopyFrom(src, api_format_.input_stream());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000538 RETURN_ON_ERR(ProcessStreamLocked());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700539 capture_audio_->CopyTo(api_format_.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000540
541#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
542 if (debug_file_->Open()) {
543 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000544 const size_t channel_size =
Michael Graczyk86c6d332015-07-23 11:41:39 -0700545 sizeof(float) * api_format_.output_stream().num_frames();
546 for (int i = 0; i < api_format_.output_stream().num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000547 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000548 RETURN_ON_ERR(WriteMessageToDebugFile());
549 }
550#endif
551
552 return kNoError;
553}
554
555int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
556 CriticalSectionScoped crit_scoped(crit_);
557 if (!frame) {
558 return kNullPointerError;
559 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000560 // Must be a native rate.
561 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
562 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000563 frame->sample_rate_hz_ != kSampleRate32kHz &&
564 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000565 return kBadSampleRateError;
566 }
567 if (echo_control_mobile_->is_enabled() &&
568 frame->sample_rate_hz_ > kSampleRate16kHz) {
569 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
570 return kUnsupportedComponentError;
571 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000572
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000573 // TODO(ajm): The input and output rates and channels are currently
574 // constrained to be identical in the int16 interface.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700575 ProcessingConfig processing_config = api_format_;
576 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
577 processing_config.input_stream().set_num_channels(frame->num_channels_);
578 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
579 processing_config.output_stream().set_num_channels(frame->num_channels_);
580
581 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
582 if (frame->samples_per_channel_ != api_format_.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000583 return kBadDataLengthError;
584 }
585
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000586#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000587 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000588 event_msg_->set_type(audioproc::Event::STREAM);
589 audioproc::Stream* msg = event_msg_->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700590 const size_t data_size =
591 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000592 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000593 }
594#endif
595
596 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000597 RETURN_ON_ERR(ProcessStreamLocked());
598 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
599
600#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
601 if (debug_file_->Open()) {
602 audioproc::Stream* msg = event_msg_->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700603 const size_t data_size =
604 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000605 msg->set_output_data(frame->data_, data_size);
606 RETURN_ON_ERR(WriteMessageToDebugFile());
607 }
608#endif
609
610 return kNoError;
611}
612
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000613int AudioProcessingImpl::ProcessStreamLocked() {
614#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
615 if (debug_file_->Open()) {
616 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000617 msg->set_delay(stream_delay_ms_);
618 msg->set_drift(echo_cancellation_->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +0000619 msg->set_level(gain_control()->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000620 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000621 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000622#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000623
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200624 MaybeUpdateHistograms();
625
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000626 AudioBuffer* ca = capture_audio_.get(); // For brevity.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000627 if (use_new_agc_ && gain_control_->is_enabled()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700628 agc_manager_->AnalyzePreProcess(ca->channels()[0], ca->num_channels(),
629 fwd_proc_format_.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000630 }
631
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000632 bool data_processed = is_data_processed();
633 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000634 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000635 }
636
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000637 if (beamformer_enabled_) {
Michael Graczykdfa36052015-03-25 16:37:27 -0700638 beamformer_->ProcessChunk(*ca->split_data_f(), ca->split_data_f());
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000639 ca->set_num_channels(1);
640 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000641
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000642 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
643 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000644 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000645 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000646
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000647 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000648 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000649 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000650 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
651 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
652 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000653
Michael Graczyk86c6d332015-07-23 11:41:39 -0700654 if (use_new_agc_ && gain_control_->is_enabled() &&
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000655 (!beamformer_enabled_ || beamformer_->is_target_present())) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000656 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
Michael Graczyk86c6d332015-07-23 11:41:39 -0700657 ca->num_frames_per_band(), split_rate_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000658 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000659 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000660
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000661 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000662 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000663 }
664
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000665 // TODO(aluebs): Investigate if the transient suppression placement should be
666 // before or after the AGC.
667 if (transient_suppressor_enabled_) {
668 float voice_probability =
669 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
670
Michael Graczyk86c6d332015-07-23 11:41:39 -0700671 transient_suppressor_->Suppress(
672 ca->channels_f()[0], ca->num_frames(), ca->num_channels(),
673 ca->split_bands_const_f(0)[kBand0To8kHz], ca->num_frames_per_band(),
674 ca->keyboard_data(), ca->num_keyboard_frames(), voice_probability,
675 key_pressed_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000676 }
677
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000678 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000679 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000680
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000681 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000682 return kNoError;
683}
684
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000685int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
686 int samples_per_channel,
687 int sample_rate_hz,
688 ChannelLayout layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700689 const StreamConfig reverse_config = {
690 sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
691 };
692 if (samples_per_channel != reverse_config.num_frames()) {
693 return kBadDataLengthError;
694 }
695 return AnalyzeReverseStream(data, reverse_config);
696}
697
698int AudioProcessingImpl::AnalyzeReverseStream(
699 const float* const* data,
700 const StreamConfig& reverse_config) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000701 CriticalSectionScoped crit_scoped(crit_);
702 if (data == NULL) {
703 return kNullPointerError;
704 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000705
Michael Graczyk86c6d332015-07-23 11:41:39 -0700706 if (reverse_config.num_channels() <= 0) {
707 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000708 }
709
Michael Graczyk86c6d332015-07-23 11:41:39 -0700710 ProcessingConfig processing_config = api_format_;
711 processing_config.reverse_stream() = reverse_config;
712
713 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
714 assert(reverse_config.num_frames() ==
715 api_format_.reverse_stream().num_frames());
716
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000717#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
718 if (debug_file_->Open()) {
719 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
720 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000721 const size_t channel_size =
Michael Graczyk86c6d332015-07-23 11:41:39 -0700722 sizeof(float) * api_format_.reverse_stream().num_frames();
723 for (int i = 0; i < api_format_.reverse_stream().num_channels(); ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000724 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000725 RETURN_ON_ERR(WriteMessageToDebugFile());
726 }
727#endif
728
Michael Graczyk86c6d332015-07-23 11:41:39 -0700729 render_audio_->CopyFrom(data, api_format_.reverse_stream());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000730 return AnalyzeReverseStreamLocked();
731}
732
niklase@google.com470e71d2011-07-07 08:21:25 +0000733int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000734 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000735 if (frame == NULL) {
736 return kNullPointerError;
737 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000738 // Must be a native rate.
739 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
740 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000741 frame->sample_rate_hz_ != kSampleRate32kHz &&
742 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000743 return kBadSampleRateError;
744 }
745 // This interface does not tolerate different forward and reverse rates.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700746 if (frame->sample_rate_hz_ != api_format_.input_stream().sample_rate_hz()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000747 return kBadSampleRateError;
748 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000749
Michael Graczyk86c6d332015-07-23 11:41:39 -0700750 if (frame->num_channels_ <= 0) {
751 return kBadNumberChannelsError;
752 }
753
754 ProcessingConfig processing_config = api_format_;
755 processing_config.reverse_stream().set_sample_rate_hz(frame->sample_rate_hz_);
756 processing_config.reverse_stream().set_num_channels(frame->num_channels_);
757
758 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
759 if (frame->samples_per_channel_ !=
760 api_format_.reverse_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000761 return kBadDataLengthError;
762 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000763
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000764#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000765 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000766 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
767 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700768 const size_t data_size =
769 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000770 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000771 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000772 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000773#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000774
775 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000776 return AnalyzeReverseStreamLocked();
777}
niklase@google.com470e71d2011-07-07 08:21:25 +0000778
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000779int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000780 AudioBuffer* ra = render_audio_.get(); // For brevity.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700781 if (rev_proc_format_.sample_rate_hz() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000782 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000783 }
784
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000785 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
786 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000787 if (!use_new_agc_) {
788 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
789 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000790
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000791 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000792}
793
794int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000795 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000796 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000797 delay += delay_offset_ms_;
798
niklase@google.com470e71d2011-07-07 08:21:25 +0000799 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000800 delay = 0;
801 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 }
803
804 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
805 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000806 delay = 500;
807 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000808 }
809
810 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000811 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000812}
813
814int AudioProcessingImpl::stream_delay_ms() const {
815 return stream_delay_ms_;
816}
817
818bool AudioProcessingImpl::was_stream_delay_set() const {
819 return was_stream_delay_set_;
820}
821
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000822void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
823 key_pressed_ = key_pressed;
824}
825
826bool AudioProcessingImpl::stream_key_pressed() const {
827 return key_pressed_;
828}
829
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000830void AudioProcessingImpl::set_delay_offset_ms(int offset) {
831 CriticalSectionScoped crit_scoped(crit_);
832 delay_offset_ms_ = offset;
833}
834
835int AudioProcessingImpl::delay_offset_ms() const {
836 return delay_offset_ms_;
837}
838
niklase@google.com470e71d2011-07-07 08:21:25 +0000839int AudioProcessingImpl::StartDebugRecording(
840 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000841 CriticalSectionScoped crit_scoped(crit_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +0200842 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +0000843
844 if (filename == NULL) {
845 return kNullPointerError;
846 }
847
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000848#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000849 // Stop any ongoing recording.
850 if (debug_file_->Open()) {
851 if (debug_file_->CloseFile() == -1) {
852 return kFileError;
853 }
854 }
855
856 if (debug_file_->OpenFile(filename, false) == -1) {
857 debug_file_->CloseFile();
858 return kFileError;
859 }
860
ajm@google.com808e0e02011-08-03 21:08:51 +0000861 int err = WriteInitMessage();
862 if (err != kNoError) {
863 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000864 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000865 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000866#else
867 return kUnsupportedFunctionError;
868#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000869}
870
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000871int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
872 CriticalSectionScoped crit_scoped(crit_);
873
874 if (handle == NULL) {
875 return kNullPointerError;
876 }
877
878#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
879 // Stop any ongoing recording.
880 if (debug_file_->Open()) {
881 if (debug_file_->CloseFile() == -1) {
882 return kFileError;
883 }
884 }
885
886 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
887 return kFileError;
888 }
889
890 int err = WriteInitMessage();
891 if (err != kNoError) {
892 return err;
893 }
894 return kNoError;
895#else
896 return kUnsupportedFunctionError;
897#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
898}
899
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000900int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
901 rtc::PlatformFile handle) {
902 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
903 return StartDebugRecording(stream);
904}
905
niklase@google.com470e71d2011-07-07 08:21:25 +0000906int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000907 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000908
909#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000910 // We just return if recording hasn't started.
911 if (debug_file_->Open()) {
912 if (debug_file_->CloseFile() == -1) {
913 return kFileError;
914 }
915 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000916 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000917#else
918 return kUnsupportedFunctionError;
919#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000920}
921
922EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
923 return echo_cancellation_;
924}
925
926EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
927 return echo_control_mobile_;
928}
929
930GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000931 if (use_new_agc_) {
932 return gain_control_for_new_agc_.get();
933 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000934 return gain_control_;
935}
936
937HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
938 return high_pass_filter_;
939}
940
941LevelEstimator* AudioProcessingImpl::level_estimator() const {
942 return level_estimator_;
943}
944
945NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
946 return noise_suppression_;
947}
948
949VoiceDetection* AudioProcessingImpl::voice_detection() const {
950 return voice_detection_;
951}
952
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000953bool AudioProcessingImpl::is_data_processed() const {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000954 if (beamformer_enabled_) {
955 return true;
956 }
957
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000958 int enabled_count = 0;
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000959 for (auto item : component_list_) {
960 if (item->is_component_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000961 enabled_count++;
962 }
963 }
964
965 // Data is unchanged if no components are enabled, or if only level_estimator_
966 // or voice_detection_ is enabled.
967 if (enabled_count == 0) {
968 return false;
969 } else if (enabled_count == 1) {
970 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
971 return false;
972 }
973 } else if (enabled_count == 2) {
974 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
975 return false;
976 }
977 }
978 return true;
979}
980
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000981bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000982 // Check if we've upmixed or downmixed the audio.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700983 return ((api_format_.output_stream().num_channels() !=
984 api_format_.input_stream().num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000985 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000986}
987
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000988bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700989 return (is_data_processed &&
990 (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
991 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000992}
993
994bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000995 if (!is_data_processed && !voice_detection_->is_enabled() &&
996 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000997 // Only level_estimator_ is enabled.
998 return false;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700999 } else if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
1000 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001001 // Something besides level_estimator_ is enabled, and we have super-wb.
1002 return true;
1003 }
1004 return false;
1005}
1006
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001007void AudioProcessingImpl::InitializeExperimentalAgc() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001008 if (use_new_agc_) {
1009 if (!agc_manager_.get()) {
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001010 agc_manager_.reset(new AgcManagerDirect(gain_control_,
1011 gain_control_for_new_agc_.get(),
1012 agc_startup_min_volume_));
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001013 }
1014 agc_manager_->Initialize();
1015 agc_manager_->SetCaptureMuted(output_will_be_muted_);
1016 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001017}
1018
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001019void AudioProcessingImpl::InitializeTransient() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001020 if (transient_suppressor_enabled_) {
1021 if (!transient_suppressor_.get()) {
1022 transient_suppressor_.reset(new TransientSuppressor());
1023 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001024 transient_suppressor_->Initialize(
1025 fwd_proc_format_.sample_rate_hz(), split_rate_,
1026 api_format_.output_stream().num_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001027 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001028}
1029
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001030void AudioProcessingImpl::InitializeBeamformer() {
1031 if (beamformer_enabled_) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001032 if (!beamformer_) {
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001033 beamformer_.reset(new NonlinearBeamformer(array_geometry_));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001034 }
1035 beamformer_->Initialize(kChunkSizeMs, split_rate_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001036 }
1037}
1038
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001039void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001040 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001041
1042 if (echo_cancellation()->is_enabled()) {
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001043 // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
1044 // If a stream has echo we know that the echo_cancellation is in process.
1045 if (stream_delay_jumps_ == -1 && echo_cancellation()->stream_has_echo()) {
1046 stream_delay_jumps_ = 0;
1047 }
1048 if (aec_system_delay_jumps_ == -1 &&
1049 echo_cancellation()->stream_has_echo()) {
1050 aec_system_delay_jumps_ = 0;
1051 }
1052
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001053 // Detect a jump in platform reported system delay and log the difference.
1054 const int diff_stream_delay_ms = stream_delay_ms_ - last_stream_delay_ms_;
1055 if (diff_stream_delay_ms > kMinDiffDelayMs && last_stream_delay_ms_ != 0) {
1056 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1057 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001058 if (stream_delay_jumps_ == -1) {
1059 stream_delay_jumps_ = 0; // Activate counter if needed.
1060 }
1061 stream_delay_jumps_++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001062 }
1063 last_stream_delay_ms_ = stream_delay_ms_;
1064
1065 // Detect a jump in AEC system delay and log the difference.
1066 const int frames_per_ms = rtc::CheckedDivExact(split_rate_, 1000);
1067 const int aec_system_delay_ms =
1068 WebRtcAec_system_delay(echo_cancellation()->aec_core()) / frames_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001069 const int diff_aec_system_delay_ms =
1070 aec_system_delay_ms - last_aec_system_delay_ms_;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001071 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
1072 last_aec_system_delay_ms_ != 0) {
1073 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1074 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1075 100);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001076 if (aec_system_delay_jumps_ == -1) {
1077 aec_system_delay_jumps_ = 0; // Activate counter if needed.
1078 }
1079 aec_system_delay_jumps_++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001080 }
1081 last_aec_system_delay_ms_ = aec_system_delay_ms;
1082 }
1083}
1084
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001085void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
1086 CriticalSectionScoped crit_scoped(crit_);
1087 if (stream_delay_jumps_ > -1) {
1088 RTC_HISTOGRAM_ENUMERATION(
1089 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
1090 stream_delay_jumps_, 51);
1091 }
1092 stream_delay_jumps_ = -1;
1093 last_stream_delay_ms_ = 0;
1094
1095 if (aec_system_delay_jumps_ > -1) {
1096 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1097 aec_system_delay_jumps_, 51);
1098 }
1099 aec_system_delay_jumps_ = -1;
1100 last_aec_system_delay_ms_ = 0;
1101}
1102
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001103#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +00001104int AudioProcessingImpl::WriteMessageToDebugFile() {
1105 int32_t size = event_msg_->ByteSize();
1106 if (size <= 0) {
1107 return kUnspecifiedError;
1108 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001109#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Michael Graczyk86c6d332015-07-23 11:41:39 -07001110// TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1111// pretty safe in assuming little-endian.
ajm@google.com808e0e02011-08-03 21:08:51 +00001112#endif
1113
1114 if (!event_msg_->SerializeToString(&event_str_)) {
1115 return kUnspecifiedError;
1116 }
1117
1118 // Write message preceded by its size.
1119 if (!debug_file_->Write(&size, sizeof(int32_t))) {
1120 return kFileError;
1121 }
1122 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
1123 return kFileError;
1124 }
1125
1126 event_msg_->Clear();
1127
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001128 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001129}
1130
1131int AudioProcessingImpl::WriteInitMessage() {
1132 event_msg_->set_type(audioproc::Event::INIT);
1133 audioproc::Init* msg = event_msg_->mutable_init();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001134 msg->set_sample_rate(api_format_.input_stream().sample_rate_hz());
1135 msg->set_num_input_channels(api_format_.input_stream().num_channels());
1136 msg->set_num_output_channels(api_format_.output_stream().num_channels());
1137 msg->set_num_reverse_channels(api_format_.reverse_stream().num_channels());
1138 msg->set_reverse_sample_rate(api_format_.reverse_stream().sample_rate_hz());
1139 msg->set_output_sample_rate(api_format_.output_stream().sample_rate_hz());
ajm@google.com808e0e02011-08-03 21:08:51 +00001140
1141 int err = WriteMessageToDebugFile();
1142 if (err != kNoError) {
1143 return err;
1144 }
1145
1146 return kNoError;
1147}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001148#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001149
niklase@google.com470e71d2011-07-07 08:21:25 +00001150} // namespace webrtc