blob: 5d7de3a1fe36e281b6ff47baeb558ba774f74237 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org40654032012-01-30 20:51:15 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000011#include "webrtc/modules/audio_processing/audio_processing_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
ajm@google.com808e0e02011-08-03 21:08:51 +000013#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014
xians@webrtc.orge46bc772014-10-10 08:36:56 +000015#include "webrtc/base/platform_file.h"
andrew@webrtc.org17e40642014-03-04 20:58:13 +000016#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000017#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000018#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000019#include "webrtc/modules/audio_processing/audio_buffer.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000020#include "webrtc/modules/audio_processing/beamformer/beamformer.h"
aluebs@webrtc.org87893762014-11-27 23:40:25 +000021#include "webrtc/modules/audio_processing/channel_buffer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000022#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000023#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000024#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
25#include "webrtc/modules/audio_processing/gain_control_impl.h"
26#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
27#include "webrtc/modules/audio_processing/level_estimator_impl.h"
28#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
29#include "webrtc/modules/audio_processing/processing_component.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000030#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000031#include "webrtc/modules/audio_processing/voice_detection_impl.h"
32#include "webrtc/modules/interface/module_common_types.h"
33#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
34#include "webrtc/system_wrappers/interface/file_wrapper.h"
35#include "webrtc/system_wrappers/interface/logging.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000036
37#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
38// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000039#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000040#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000041#else
ajm@google.com808e0e02011-08-03 21:08:51 +000042#include "webrtc/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000043#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000044#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000045
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000046#define RETURN_ON_ERR(expr) \
47 do { \
48 int err = expr; \
49 if (err != kNoError) { \
50 return err; \
51 } \
52 } while (0)
53
niklase@google.com470e71d2011-07-07 08:21:25 +000054namespace webrtc {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000055
56// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000057static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000058
pbos@webrtc.org788acd12014-12-15 09:41:24 +000059// This class has two main functionalities:
60//
61// 1) It is returned instead of the real GainControl after the new AGC has been
62// enabled in order to prevent an outside user from overriding compression
63// settings. It doesn't do anything in its implementation, except for
64// delegating the const methods and Enable calls to the real GainControl, so
65// AGC can still be disabled.
66//
67// 2) It is injected into AgcManagerDirect and implements volume callbacks for
68// getting and setting the volume level. It just caches this value to be used
69// in VoiceEngine later.
70class GainControlForNewAgc : public GainControl, public VolumeCallbacks {
71 public:
72 explicit GainControlForNewAgc(GainControlImpl* gain_control)
73 : real_gain_control_(gain_control),
74 volume_(0) {
75 }
76
77 // GainControl implementation.
78 virtual int Enable(bool enable) OVERRIDE {
79 return real_gain_control_->Enable(enable);
80 }
81 virtual bool is_enabled() const OVERRIDE {
82 return real_gain_control_->is_enabled();
83 }
84 virtual int set_stream_analog_level(int level) OVERRIDE {
85 volume_ = level;
86 return AudioProcessing::kNoError;
87 }
88 virtual int stream_analog_level() OVERRIDE {
89 return volume_;
90 }
91 virtual int set_mode(Mode mode) OVERRIDE { return AudioProcessing::kNoError; }
92 virtual Mode mode() const OVERRIDE { return GainControl::kAdaptiveAnalog; }
93 virtual int set_target_level_dbfs(int level) OVERRIDE {
94 return AudioProcessing::kNoError;
95 }
96 virtual int target_level_dbfs() const OVERRIDE {
97 return real_gain_control_->target_level_dbfs();
98 }
99 virtual int set_compression_gain_db(int gain) OVERRIDE {
100 return AudioProcessing::kNoError;
101 }
102 virtual int compression_gain_db() const OVERRIDE {
103 return real_gain_control_->compression_gain_db();
104 }
105 virtual int enable_limiter(bool enable) OVERRIDE {
106 return AudioProcessing::kNoError;
107 }
108 virtual bool is_limiter_enabled() const OVERRIDE {
109 return real_gain_control_->is_limiter_enabled();
110 }
111 virtual int set_analog_level_limits(int minimum,
112 int maximum) OVERRIDE {
113 return AudioProcessing::kNoError;
114 }
115 virtual int analog_level_minimum() const OVERRIDE {
116 return real_gain_control_->analog_level_minimum();
117 }
118 virtual int analog_level_maximum() const OVERRIDE {
119 return real_gain_control_->analog_level_maximum();
120 }
121 virtual bool stream_is_saturated() const OVERRIDE {
122 return real_gain_control_->stream_is_saturated();
123 }
124
125 // VolumeCallbacks implementation.
126 virtual void SetMicVolume(int volume) OVERRIDE {
127 volume_ = volume;
128 }
129 virtual int GetMicVolume() OVERRIDE {
130 return volume_;
131 }
132
133 private:
134 GainControl* real_gain_control_;
135 int volume_;
136};
137
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000138AudioProcessing* AudioProcessing::Create() {
139 Config config;
140 return Create(config);
141}
142
143AudioProcessing* AudioProcessing::Create(const Config& config) {
144 AudioProcessingImpl* apm = new AudioProcessingImpl(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145 if (apm->Initialize() != kNoError) {
146 delete apm;
147 apm = NULL;
148 }
149
150 return apm;
151}
152
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000153AudioProcessingImpl::AudioProcessingImpl(const Config& config)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000154 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000155 echo_control_mobile_(NULL),
156 gain_control_(NULL),
157 high_pass_filter_(NULL),
158 level_estimator_(NULL),
159 noise_suppression_(NULL),
160 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000162#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
163 debug_file_(FileWrapper::Create()),
164 event_msg_(new audioproc::Event()),
165#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000166 fwd_in_format_(kSampleRate16kHz, 1),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000167 fwd_proc_format_(kSampleRate16kHz),
168 fwd_out_format_(kSampleRate16kHz, 1),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000169 rev_in_format_(kSampleRate16kHz, 1),
170 rev_proc_format_(kSampleRate16kHz, 1),
171 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000173 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 was_stream_delay_set_(false),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +0000175 output_will_be_muted_(false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000176 key_pressed_(false),
177#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
178 use_new_agc_(false),
179#else
180 use_new_agc_(config.Get<ExperimentalAgc>().enabled),
181#endif
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000182 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled),
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000183 beamformer_enabled_(config.Get<Beamforming>().enabled),
184 array_geometry_(config.Get<Beamforming>().array_geometry) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000185 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000186 component_list_.push_back(echo_cancellation_);
187
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000188 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000189 component_list_.push_back(echo_control_mobile_);
190
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000191 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000192 component_list_.push_back(gain_control_);
193
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000194 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 component_list_.push_back(high_pass_filter_);
196
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000197 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000198 component_list_.push_back(level_estimator_);
199
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000200 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000201 component_list_.push_back(noise_suppression_);
202
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000203 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000205
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000206 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
207
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000208 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000209}
210
211AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000212 {
213 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000214 // Depends on gain_control_ and gain_control_for_new_agc_.
215 agc_manager_.reset();
216 // Depends on gain_control_.
217 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000218 while (!component_list_.empty()) {
219 ProcessingComponent* component = component_list_.front();
220 component->Destroy();
221 delete component;
222 component_list_.pop_front();
223 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000224
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000225#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000226 if (debug_file_->Open()) {
227 debug_file_->CloseFile();
228 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000229#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000231 delete crit_;
232 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233}
234
niklase@google.com470e71d2011-07-07 08:21:25 +0000235int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000236 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000237 return InitializeLocked();
238}
239
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000240int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000241 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000242 return InitializeLocked(rate,
243 rate,
244 rev_in_format_.rate(),
245 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000246 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000247 rev_in_format_.num_channels());
248}
249
250int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
251 int output_sample_rate_hz,
252 int reverse_sample_rate_hz,
253 ChannelLayout input_layout,
254 ChannelLayout output_layout,
255 ChannelLayout reverse_layout) {
256 CriticalSectionScoped crit_scoped(crit_);
257 return InitializeLocked(input_sample_rate_hz,
258 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000259 reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000260 ChannelsFromLayout(input_layout),
261 ChannelsFromLayout(output_layout),
262 ChannelsFromLayout(reverse_layout));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000263}
264
niklase@google.com470e71d2011-07-07 08:21:25 +0000265int AudioProcessingImpl::InitializeLocked() {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000266 const int fwd_audio_buffer_channels = beamformer_enabled_ ?
267 fwd_in_format_.num_channels() :
268 fwd_out_format_.num_channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000269 render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(),
270 rev_in_format_.num_channels(),
271 rev_proc_format_.samples_per_channel(),
272 rev_proc_format_.num_channels(),
273 rev_proc_format_.samples_per_channel()));
274 capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
275 fwd_in_format_.num_channels(),
276 fwd_proc_format_.samples_per_channel(),
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000277 fwd_audio_buffer_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000278 fwd_out_format_.samples_per_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000279
niklase@google.com470e71d2011-07-07 08:21:25 +0000280 // Initialize all components.
281 std::list<ProcessingComponent*>::iterator it;
andrew@webrtc.org81865342012-10-27 00:28:27 +0000282 for (it = component_list_.begin(); it != component_list_.end(); ++it) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000283 int err = (*it)->Initialize();
284 if (err != kNoError) {
285 return err;
286 }
287 }
288
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000289 int err = InitializeExperimentalAgc();
290 if (err != kNoError) {
291 return err;
292 }
293
294 err = InitializeTransient();
295 if (err != kNoError) {
296 return err;
297 }
298
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000299 InitializeBeamformer();
300
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000301#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000302 if (debug_file_->Open()) {
303 int err = WriteInitMessage();
304 if (err != kNoError) {
305 return err;
306 }
307 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000308#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000309
niklase@google.com470e71d2011-07-07 08:21:25 +0000310 return kNoError;
311}
312
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000313int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz,
314 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000315 int reverse_sample_rate_hz,
316 int num_input_channels,
317 int num_output_channels,
318 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000319 if (input_sample_rate_hz <= 0 ||
320 output_sample_rate_hz <= 0 ||
321 reverse_sample_rate_hz <= 0) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000322 return kBadSampleRateError;
323 }
324 if (num_output_channels > num_input_channels) {
325 return kBadNumberChannelsError;
326 }
327 // Only mono and stereo supported currently.
328 if (num_input_channels > 2 || num_input_channels < 1 ||
329 num_output_channels > 2 || num_output_channels < 1 ||
330 num_reverse_channels > 2 || num_reverse_channels < 1) {
331 return kBadNumberChannelsError;
332 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000333
334 fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000335 fwd_out_format_.set(output_sample_rate_hz, num_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000336 rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
337
338 // We process at the closest native rate >= min(input rate, output rate)...
339 int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate());
340 int fwd_proc_rate;
341 if (min_proc_rate > kSampleRate16kHz) {
342 fwd_proc_rate = kSampleRate32kHz;
343 } else if (min_proc_rate > kSampleRate8kHz) {
344 fwd_proc_rate = kSampleRate16kHz;
345 } else {
346 fwd_proc_rate = kSampleRate8kHz;
347 }
348 // ...with one exception.
349 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
350 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000351 }
352
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000353 fwd_proc_format_.set(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000354
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000355 // We normally process the reverse stream at 16 kHz. Unless...
356 int rev_proc_rate = kSampleRate16kHz;
357 if (fwd_proc_format_.rate() == kSampleRate8kHz) {
358 // ...the forward stream is at 8 kHz.
359 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000360 } else {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000361 if (rev_in_format_.rate() == kSampleRate32kHz) {
362 // ...or the input is at 32 kHz, in which case we use the splitting
363 // filter rather than the resampler.
364 rev_proc_rate = kSampleRate32kHz;
365 }
366 }
367
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000368 // Always downmix the reverse stream to mono for analysis. This has been
369 // demonstrated to work well for AEC in most practical scenarios.
370 rev_proc_format_.set(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000371
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000372 if (fwd_proc_format_.rate() == kSampleRate32kHz ||
373 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000374 split_rate_ = kSampleRate16kHz;
375 } else {
376 split_rate_ = fwd_proc_format_.rate();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000377 }
378
379 return InitializeLocked();
380}
381
382// Calls InitializeLocked() if any of the audio parameters have changed from
383// their current values.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000384int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz,
385 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000386 int reverse_sample_rate_hz,
387 int num_input_channels,
388 int num_output_channels,
389 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000390 if (input_sample_rate_hz == fwd_in_format_.rate() &&
391 output_sample_rate_hz == fwd_out_format_.rate() &&
392 reverse_sample_rate_hz == rev_in_format_.rate() &&
393 num_input_channels == fwd_in_format_.num_channels() &&
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000394 num_output_channels == fwd_out_format_.num_channels() &&
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000395 num_reverse_channels == rev_in_format_.num_channels()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000396 return kNoError;
397 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000398 if (beamformer_enabled_ &&
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000399 (static_cast<size_t>(num_input_channels) != array_geometry_.size() ||
400 num_output_channels > 1)) {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000401 return kBadNumberChannelsError;
402 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000403 return InitializeLocked(input_sample_rate_hz,
404 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000405 reverse_sample_rate_hz,
406 num_input_channels,
407 num_output_channels,
408 num_reverse_channels);
409}
410
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000411void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000412 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000413 std::list<ProcessingComponent*>::iterator it;
414 for (it = component_list_.begin(); it != component_list_.end(); ++it)
415 (*it)->SetExtraOptions(config);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000416
417 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
418 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
419 InitializeTransient();
420 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000421}
422
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000423int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000424 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000425 return fwd_in_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000426}
427
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000428int AudioProcessingImpl::sample_rate_hz() const {
429 CriticalSectionScoped crit_scoped(crit_);
430 return fwd_in_format_.rate();
431}
432
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000433int AudioProcessingImpl::proc_sample_rate_hz() const {
434 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000435}
436
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000437int AudioProcessingImpl::proc_split_sample_rate_hz() const {
438 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000439}
440
441int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000442 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000443}
444
445int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000446 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000447}
448
449int AudioProcessingImpl::num_output_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000450 return fwd_out_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000451}
452
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000453void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
454 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000455 CriticalSectionScoped lock(crit_);
456 if (agc_manager_.get()) {
457 agc_manager_->SetCaptureMuted(output_will_be_muted_);
458 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000459}
460
461bool AudioProcessingImpl::output_will_be_muted() const {
462 return output_will_be_muted_;
463}
464
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000465int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000466 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000467 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000468 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000469 int output_sample_rate_hz,
470 ChannelLayout output_layout,
471 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000472 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000473 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000474 return kNullPointerError;
475 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000476
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000477 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
478 output_sample_rate_hz,
479 rev_in_format_.rate(),
480 ChannelsFromLayout(input_layout),
481 ChannelsFromLayout(output_layout),
482 rev_in_format_.num_channels()));
483 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000484 return kBadDataLengthError;
485 }
486
487#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
488 if (debug_file_->Open()) {
489 event_msg_->set_type(audioproc::Event::STREAM);
490 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000491 const size_t channel_size =
492 sizeof(float) * fwd_in_format_.samples_per_channel();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000493 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
494 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000495 }
496#endif
497
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000498 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000499 RETURN_ON_ERR(ProcessStreamLocked());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +0000500 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
501 output_layout,
502 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000503
504#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
505 if (debug_file_->Open()) {
506 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000507 const size_t channel_size =
508 sizeof(float) * fwd_out_format_.samples_per_channel();
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000509 for (int i = 0; i < fwd_out_format_.num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000510 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000511 RETURN_ON_ERR(WriteMessageToDebugFile());
512 }
513#endif
514
515 return kNoError;
516}
517
518int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
519 CriticalSectionScoped crit_scoped(crit_);
520 if (!frame) {
521 return kNullPointerError;
522 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000523 // Must be a native rate.
524 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
525 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000526 frame->sample_rate_hz_ != kSampleRate32kHz &&
527 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000528 return kBadSampleRateError;
529 }
530 if (echo_control_mobile_->is_enabled() &&
531 frame->sample_rate_hz_ > kSampleRate16kHz) {
532 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
533 return kUnsupportedComponentError;
534 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000535
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000536 // TODO(ajm): The input and output rates and channels are currently
537 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000538 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000539 frame->sample_rate_hz_,
540 rev_in_format_.rate(),
541 frame->num_channels_,
542 frame->num_channels_,
543 rev_in_format_.num_channels()));
544 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000545 return kBadDataLengthError;
546 }
547
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000548#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000549 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000550 event_msg_->set_type(audioproc::Event::STREAM);
551 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000552 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000553 frame->samples_per_channel_ *
554 frame->num_channels_;
555 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000556 }
557#endif
558
559 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000560 RETURN_ON_ERR(ProcessStreamLocked());
561 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
562
563#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
564 if (debug_file_->Open()) {
565 audioproc::Stream* msg = event_msg_->mutable_stream();
566 const size_t data_size = sizeof(int16_t) *
567 frame->samples_per_channel_ *
568 frame->num_channels_;
569 msg->set_output_data(frame->data_, data_size);
570 RETURN_ON_ERR(WriteMessageToDebugFile());
571 }
572#endif
573
574 return kNoError;
575}
576
577
578int AudioProcessingImpl::ProcessStreamLocked() {
579#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
580 if (debug_file_->Open()) {
581 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000582 msg->set_delay(stream_delay_ms_);
583 msg->set_drift(echo_cancellation_->stream_drift_samples());
584 msg->set_level(gain_control_->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000585 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000586 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000587#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000588
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000589 AudioBuffer* ca = capture_audio_.get(); // For brevity.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000590 if (use_new_agc_ && gain_control_->is_enabled()) {
591 agc_manager_->AnalyzePreProcess(ca->data(0),
592 ca->num_channels(),
593 fwd_proc_format_.samples_per_channel());
594 }
595
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000596 bool data_processed = is_data_processed();
597 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000598 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000599 }
600
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000601#ifdef WEBRTC_BEAMFORMER
602 if (beamformer_enabled_) {
603 beamformer_->ProcessChunk(ca->split_channels_const_f(kBand0To8kHz),
604 ca->split_channels_const_f(kBand8To16kHz),
605 ca->num_channels(),
606 ca->samples_per_split_channel(),
607 ca->split_channels_f(kBand0To8kHz),
608 ca->split_channels_f(kBand8To16kHz));
609 ca->set_num_channels(1);
610 }
611#endif
612
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000613 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
614 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000615 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000616 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000617
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000618 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000619 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000620 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000621 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
622 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
623 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000624
625 if (use_new_agc_ && gain_control_->is_enabled()) {
626 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
627 ca->samples_per_split_channel(),
628 split_rate_);
629 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000630 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000631
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000632 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000633 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000634 }
635
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000636 // TODO(aluebs): Investigate if the transient suppression placement should be
637 // before or after the AGC.
638 if (transient_suppressor_enabled_) {
639 float voice_probability =
640 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
641
642 transient_suppressor_->Suppress(ca->data_f(0),
643 ca->samples_per_channel(),
644 ca->num_channels(),
645 ca->split_bands_const_f(0)[kBand0To8kHz],
646 ca->samples_per_split_channel(),
647 ca->keyboard_data(),
648 ca->samples_per_keyboard_channel(),
649 voice_probability,
650 key_pressed_);
651 }
652
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000653 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000654 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000655
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000656 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000657 return kNoError;
658}
659
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000660int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
661 int samples_per_channel,
662 int sample_rate_hz,
663 ChannelLayout layout) {
664 CriticalSectionScoped crit_scoped(crit_);
665 if (data == NULL) {
666 return kNullPointerError;
667 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000668
669 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000670 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
671 fwd_out_format_.rate(),
672 sample_rate_hz,
673 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000674 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000675 num_channels));
676 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000677 return kBadDataLengthError;
678 }
679
680#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
681 if (debug_file_->Open()) {
682 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
683 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000684 const size_t channel_size =
685 sizeof(float) * rev_in_format_.samples_per_channel();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000686 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000687 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000688 RETURN_ON_ERR(WriteMessageToDebugFile());
689 }
690#endif
691
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000692 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000693 return AnalyzeReverseStreamLocked();
694}
695
niklase@google.com470e71d2011-07-07 08:21:25 +0000696int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000697 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000698 if (frame == NULL) {
699 return kNullPointerError;
700 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000701 // Must be a native rate.
702 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
703 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000704 frame->sample_rate_hz_ != kSampleRate32kHz &&
705 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000706 return kBadSampleRateError;
707 }
708 // This interface does not tolerate different forward and reverse rates.
709 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000710 return kBadSampleRateError;
711 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000712
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000713 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
714 fwd_out_format_.rate(),
715 frame->sample_rate_hz_,
716 fwd_in_format_.num_channels(),
717 fwd_in_format_.num_channels(),
718 frame->num_channels_));
719 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000720 return kBadDataLengthError;
721 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000722
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000723#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000724 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000725 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
726 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000727 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000728 frame->samples_per_channel_ *
729 frame->num_channels_;
730 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000731 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000732 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000733#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000734
735 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000736 return AnalyzeReverseStreamLocked();
737}
niklase@google.com470e71d2011-07-07 08:21:25 +0000738
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000739int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000740 AudioBuffer* ra = render_audio_.get(); // For brevity.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000741 if (rev_proc_format_.rate() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000742 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000743 }
744
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000745 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
746 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000747 if (!use_new_agc_) {
748 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
749 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000750
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000751 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000752}
753
754int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000755 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000756 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000757 delay += delay_offset_ms_;
758
niklase@google.com470e71d2011-07-07 08:21:25 +0000759 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000760 delay = 0;
761 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000762 }
763
764 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
765 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000766 delay = 500;
767 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000768 }
769
770 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000771 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000772}
773
774int AudioProcessingImpl::stream_delay_ms() const {
775 return stream_delay_ms_;
776}
777
778bool AudioProcessingImpl::was_stream_delay_set() const {
779 return was_stream_delay_set_;
780}
781
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000782void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
783 key_pressed_ = key_pressed;
784}
785
786bool AudioProcessingImpl::stream_key_pressed() const {
787 return key_pressed_;
788}
789
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000790void AudioProcessingImpl::set_delay_offset_ms(int offset) {
791 CriticalSectionScoped crit_scoped(crit_);
792 delay_offset_ms_ = offset;
793}
794
795int AudioProcessingImpl::delay_offset_ms() const {
796 return delay_offset_ms_;
797}
798
niklase@google.com470e71d2011-07-07 08:21:25 +0000799int AudioProcessingImpl::StartDebugRecording(
800 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000801 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000802 assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize);
803
804 if (filename == NULL) {
805 return kNullPointerError;
806 }
807
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000808#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000809 // Stop any ongoing recording.
810 if (debug_file_->Open()) {
811 if (debug_file_->CloseFile() == -1) {
812 return kFileError;
813 }
814 }
815
816 if (debug_file_->OpenFile(filename, false) == -1) {
817 debug_file_->CloseFile();
818 return kFileError;
819 }
820
ajm@google.com808e0e02011-08-03 21:08:51 +0000821 int err = WriteInitMessage();
822 if (err != kNoError) {
823 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000824 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000825 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000826#else
827 return kUnsupportedFunctionError;
828#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000829}
830
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000831int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
832 CriticalSectionScoped crit_scoped(crit_);
833
834 if (handle == NULL) {
835 return kNullPointerError;
836 }
837
838#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
839 // Stop any ongoing recording.
840 if (debug_file_->Open()) {
841 if (debug_file_->CloseFile() == -1) {
842 return kFileError;
843 }
844 }
845
846 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
847 return kFileError;
848 }
849
850 int err = WriteInitMessage();
851 if (err != kNoError) {
852 return err;
853 }
854 return kNoError;
855#else
856 return kUnsupportedFunctionError;
857#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
858}
859
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000860int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
861 rtc::PlatformFile handle) {
862 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
863 return StartDebugRecording(stream);
864}
865
niklase@google.com470e71d2011-07-07 08:21:25 +0000866int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000867 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000868
869#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000870 // We just return if recording hasn't started.
871 if (debug_file_->Open()) {
872 if (debug_file_->CloseFile() == -1) {
873 return kFileError;
874 }
875 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000876 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000877#else
878 return kUnsupportedFunctionError;
879#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000880}
881
882EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
883 return echo_cancellation_;
884}
885
886EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
887 return echo_control_mobile_;
888}
889
890GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000891 if (use_new_agc_) {
892 return gain_control_for_new_agc_.get();
893 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000894 return gain_control_;
895}
896
897HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
898 return high_pass_filter_;
899}
900
901LevelEstimator* AudioProcessingImpl::level_estimator() const {
902 return level_estimator_;
903}
904
905NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
906 return noise_suppression_;
907}
908
909VoiceDetection* AudioProcessingImpl::voice_detection() const {
910 return voice_detection_;
911}
912
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000913bool AudioProcessingImpl::is_data_processed() const {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000914 if (beamformer_enabled_) {
915 return true;
916 }
917
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000918 int enabled_count = 0;
919 std::list<ProcessingComponent*>::const_iterator it;
920 for (it = component_list_.begin(); it != component_list_.end(); it++) {
921 if ((*it)->is_component_enabled()) {
922 enabled_count++;
923 }
924 }
925
926 // Data is unchanged if no components are enabled, or if only level_estimator_
927 // or voice_detection_ is enabled.
928 if (enabled_count == 0) {
929 return false;
930 } else if (enabled_count == 1) {
931 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
932 return false;
933 }
934 } else if (enabled_count == 2) {
935 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
936 return false;
937 }
938 }
939 return true;
940}
941
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000942bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000943 // Check if we've upmixed or downmixed the audio.
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000944 return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000945 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000946}
947
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000948bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000949 return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz ||
950 fwd_proc_format_.rate() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000951}
952
953bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000954 if (!is_data_processed && !voice_detection_->is_enabled() &&
955 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000956 // Only level_estimator_ is enabled.
957 return false;
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000958 } else if (fwd_proc_format_.rate() == kSampleRate32kHz ||
959 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000960 // Something besides level_estimator_ is enabled, and we have super-wb.
961 return true;
962 }
963 return false;
964}
965
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000966int AudioProcessingImpl::InitializeExperimentalAgc() {
967 if (use_new_agc_) {
968 if (!agc_manager_.get()) {
969 agc_manager_.reset(
970 new AgcManagerDirect(gain_control_, gain_control_for_new_agc_.get()));
971 }
972 agc_manager_->Initialize();
973 agc_manager_->SetCaptureMuted(output_will_be_muted_);
974 }
975 return kNoError;
976}
977
978int AudioProcessingImpl::InitializeTransient() {
979 if (transient_suppressor_enabled_) {
980 if (!transient_suppressor_.get()) {
981 transient_suppressor_.reset(new TransientSuppressor());
982 }
983 transient_suppressor_->Initialize(fwd_proc_format_.rate(),
984 split_rate_,
985 fwd_out_format_.num_channels());
986 }
987 return kNoError;
988}
989
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000990void AudioProcessingImpl::InitializeBeamformer() {
991 if (beamformer_enabled_) {
992#ifdef WEBRTC_BEAMFORMER
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000993 beamformer_.reset(new Beamformer(kChunkSizeMs,
994 split_rate_,
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000995 array_geometry_));
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000996#else
997 assert(false);
998#endif
999 }
1000}
1001
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001002#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +00001003int AudioProcessingImpl::WriteMessageToDebugFile() {
1004 int32_t size = event_msg_->ByteSize();
1005 if (size <= 0) {
1006 return kUnspecifiedError;
1007 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001008#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +00001009 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1010 // pretty safe in assuming little-endian.
1011#endif
1012
1013 if (!event_msg_->SerializeToString(&event_str_)) {
1014 return kUnspecifiedError;
1015 }
1016
1017 // Write message preceded by its size.
1018 if (!debug_file_->Write(&size, sizeof(int32_t))) {
1019 return kFileError;
1020 }
1021 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
1022 return kFileError;
1023 }
1024
1025 event_msg_->Clear();
1026
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001027 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001028}
1029
1030int AudioProcessingImpl::WriteInitMessage() {
1031 event_msg_->set_type(audioproc::Event::INIT);
1032 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001033 msg->set_sample_rate(fwd_in_format_.rate());
1034 msg->set_num_input_channels(fwd_in_format_.num_channels());
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +00001035 msg->set_num_output_channels(fwd_out_format_.num_channels());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001036 msg->set_num_reverse_channels(rev_in_format_.num_channels());
1037 msg->set_reverse_sample_rate(rev_in_format_.rate());
1038 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +00001039
1040 int err = WriteMessageToDebugFile();
1041 if (err != kNoError) {
1042 return err;
1043 }
1044
1045 return kNoError;
1046}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001047#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001048
niklase@google.com470e71d2011-07-07 08:21:25 +00001049} // namespace webrtc