blob: fd6b2581413138bd5080f874be9df0cf35e6fb1c [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
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020015#include "webrtc/base/checks.h"
xians@webrtc.orge46bc772014-10-10 08:36:56 +000016#include "webrtc/base/platform_file.h"
andrew@webrtc.org17e40642014-03-04 20:58:13 +000017#include "webrtc/common_audio/include/audio_util.h"
Michael Graczykdfa36052015-03-25 16:37:27 -070018#include "webrtc/common_audio/channel_buffer.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000019#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020020extern "C" {
21#include "webrtc/modules/audio_processing/aec/aec_core.h"
22}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000023#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000024#include "webrtc/modules/audio_processing/audio_buffer.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000025#include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000026#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000027#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000028#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
29#include "webrtc/modules/audio_processing/gain_control_impl.h"
30#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
31#include "webrtc/modules/audio_processing/level_estimator_impl.h"
32#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
33#include "webrtc/modules/audio_processing/processing_component.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000034#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000035#include "webrtc/modules/audio_processing/voice_detection_impl.h"
36#include "webrtc/modules/interface/module_common_types.h"
37#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
38#include "webrtc/system_wrappers/interface/file_wrapper.h"
39#include "webrtc/system_wrappers/interface/logging.h"
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020040#include "webrtc/system_wrappers/interface/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000041
42#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
43// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000044#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000045#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000046#else
ajm@google.com808e0e02011-08-03 21:08:51 +000047#include "webrtc/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000048#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000049#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000050
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000051#define RETURN_ON_ERR(expr) \
52 do { \
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000053 int err = (expr); \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000054 if (err != kNoError) { \
55 return err; \
56 } \
57 } while (0)
58
niklase@google.com470e71d2011-07-07 08:21:25 +000059namespace webrtc {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000060
61// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000062static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000063
pbos@webrtc.org788acd12014-12-15 09:41:24 +000064// This class has two main functionalities:
65//
66// 1) It is returned instead of the real GainControl after the new AGC has been
67// enabled in order to prevent an outside user from overriding compression
68// settings. It doesn't do anything in its implementation, except for
69// delegating the const methods and Enable calls to the real GainControl, so
70// AGC can still be disabled.
71//
72// 2) It is injected into AgcManagerDirect and implements volume callbacks for
73// getting and setting the volume level. It just caches this value to be used
74// in VoiceEngine later.
75class GainControlForNewAgc : public GainControl, public VolumeCallbacks {
76 public:
77 explicit GainControlForNewAgc(GainControlImpl* gain_control)
78 : real_gain_control_(gain_control),
79 volume_(0) {
80 }
81
82 // GainControl implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000083 int Enable(bool enable) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000084 return real_gain_control_->Enable(enable);
85 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000086 bool is_enabled() const override { return real_gain_control_->is_enabled(); }
87 int set_stream_analog_level(int level) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000088 volume_ = level;
89 return AudioProcessing::kNoError;
90 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000091 int stream_analog_level() override { return volume_; }
92 int set_mode(Mode mode) override { return AudioProcessing::kNoError; }
93 Mode mode() const override { return GainControl::kAdaptiveAnalog; }
94 int set_target_level_dbfs(int level) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000095 return AudioProcessing::kNoError;
96 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000097 int target_level_dbfs() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000098 return real_gain_control_->target_level_dbfs();
99 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000100 int set_compression_gain_db(int gain) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000101 return AudioProcessing::kNoError;
102 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000103 int compression_gain_db() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000104 return real_gain_control_->compression_gain_db();
105 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000106 int enable_limiter(bool enable) override { return AudioProcessing::kNoError; }
107 bool is_limiter_enabled() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000108 return real_gain_control_->is_limiter_enabled();
109 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000110 int set_analog_level_limits(int minimum, int maximum) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000111 return AudioProcessing::kNoError;
112 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000113 int analog_level_minimum() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000114 return real_gain_control_->analog_level_minimum();
115 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000116 int analog_level_maximum() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000117 return real_gain_control_->analog_level_maximum();
118 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000119 bool stream_is_saturated() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000120 return real_gain_control_->stream_is_saturated();
121 }
122
123 // VolumeCallbacks implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000124 void SetMicVolume(int volume) override { volume_ = volume; }
125 int GetMicVolume() override { return volume_; }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000126
127 private:
128 GainControl* real_gain_control_;
129 int volume_;
130};
131
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000132AudioProcessing* AudioProcessing::Create() {
133 Config config;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000134 return Create(config, nullptr);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000135}
136
137AudioProcessing* AudioProcessing::Create(const Config& config) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000138 return Create(config, nullptr);
139}
140
141AudioProcessing* AudioProcessing::Create(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700142 Beamformer<float>* beamformer) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000143 AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 if (apm->Initialize() != kNoError) {
145 delete apm;
146 apm = NULL;
147 }
148
149 return apm;
150}
151
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000152AudioProcessingImpl::AudioProcessingImpl(const Config& config)
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000153 : AudioProcessingImpl(config, nullptr) {}
154
155AudioProcessingImpl::AudioProcessingImpl(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700156 Beamformer<float>* beamformer)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000157 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000158 echo_control_mobile_(NULL),
159 gain_control_(NULL),
160 high_pass_filter_(NULL),
161 level_estimator_(NULL),
162 noise_suppression_(NULL),
163 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000164 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000165#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
166 debug_file_(FileWrapper::Create()),
167 event_msg_(new audioproc::Event()),
168#endif
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000169 fwd_in_format_(kSampleRate16kHz, 1),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000170 fwd_proc_format_(kSampleRate16kHz),
171 fwd_out_format_(kSampleRate16kHz, 1),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000172 rev_in_format_(kSampleRate16kHz, 1),
173 rev_proc_format_(kSampleRate16kHz, 1),
174 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +0000175 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000176 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000177 was_stream_delay_set_(false),
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200178 last_stream_delay_ms_(0),
179 last_aec_system_delay_ms_(0),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +0000180 output_will_be_muted_(false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000181 key_pressed_(false),
182#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
183 use_new_agc_(false),
184#else
185 use_new_agc_(config.Get<ExperimentalAgc>().enabled),
186#endif
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200187 agc_startup_min_volume_(config.Get<ExperimentalAgc>().startup_min_volume),
andrew1c7075f2015-06-24 18:14:14 -0700188#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
189 transient_suppressor_enabled_(false),
190#else
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000191 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700192#endif
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000193 beamformer_enabled_(config.Get<Beamforming>().enabled),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000194 beamformer_(beamformer),
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000195 array_geometry_(config.Get<Beamforming>().array_geometry),
196 supports_48kHz_(config.Get<AudioProcessing48kHzSupport>().enabled) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000197 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000198 component_list_.push_back(echo_cancellation_);
199
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000200 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000201 component_list_.push_back(echo_control_mobile_);
202
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000203 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 component_list_.push_back(gain_control_);
205
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000206 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 component_list_.push_back(high_pass_filter_);
208
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000209 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000210 component_list_.push_back(level_estimator_);
211
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000212 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000213 component_list_.push_back(noise_suppression_);
214
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000215 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000217
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000218 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
219
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000220 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000221}
222
223AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000224 {
225 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000226 // Depends on gain_control_ and gain_control_for_new_agc_.
227 agc_manager_.reset();
228 // Depends on gain_control_.
229 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000230 while (!component_list_.empty()) {
231 ProcessingComponent* component = component_list_.front();
232 component->Destroy();
233 delete component;
234 component_list_.pop_front();
235 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000236
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000237#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000238 if (debug_file_->Open()) {
239 debug_file_->CloseFile();
240 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000241#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000243 delete crit_;
244 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245}
246
niklase@google.com470e71d2011-07-07 08:21:25 +0000247int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000248 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000249 return InitializeLocked();
250}
251
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000252int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000253 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000254 return InitializeLocked(rate,
255 rate,
256 rev_in_format_.rate(),
257 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000258 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000259 rev_in_format_.num_channels());
260}
261
262int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
263 int output_sample_rate_hz,
264 int reverse_sample_rate_hz,
265 ChannelLayout input_layout,
266 ChannelLayout output_layout,
267 ChannelLayout reverse_layout) {
268 CriticalSectionScoped crit_scoped(crit_);
269 return InitializeLocked(input_sample_rate_hz,
270 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000271 reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000272 ChannelsFromLayout(input_layout),
273 ChannelsFromLayout(output_layout),
274 ChannelsFromLayout(reverse_layout));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000275}
276
niklase@google.com470e71d2011-07-07 08:21:25 +0000277int AudioProcessingImpl::InitializeLocked() {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000278 const int fwd_audio_buffer_channels = beamformer_enabled_ ?
279 fwd_in_format_.num_channels() :
280 fwd_out_format_.num_channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000281 render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(),
282 rev_in_format_.num_channels(),
283 rev_proc_format_.samples_per_channel(),
284 rev_proc_format_.num_channels(),
285 rev_proc_format_.samples_per_channel()));
286 capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
287 fwd_in_format_.num_channels(),
288 fwd_proc_format_.samples_per_channel(),
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000289 fwd_audio_buffer_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000290 fwd_out_format_.samples_per_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000291
niklase@google.com470e71d2011-07-07 08:21:25 +0000292 // Initialize all components.
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000293 for (auto item : component_list_) {
294 int err = item->Initialize();
niklase@google.com470e71d2011-07-07 08:21:25 +0000295 if (err != kNoError) {
296 return err;
297 }
298 }
299
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200300 InitializeExperimentalAgc();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000301
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200302 InitializeTransient();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000303
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000304 InitializeBeamformer();
305
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000306#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000307 if (debug_file_->Open()) {
308 int err = WriteInitMessage();
309 if (err != kNoError) {
310 return err;
311 }
312 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000313#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000314
niklase@google.com470e71d2011-07-07 08:21:25 +0000315 return kNoError;
316}
317
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000318int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz,
319 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000320 int reverse_sample_rate_hz,
321 int num_input_channels,
322 int num_output_channels,
323 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000324 if (input_sample_rate_hz <= 0 ||
325 output_sample_rate_hz <= 0 ||
326 reverse_sample_rate_hz <= 0) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000327 return kBadSampleRateError;
328 }
329 if (num_output_channels > num_input_channels) {
330 return kBadNumberChannelsError;
331 }
332 // Only mono and stereo supported currently.
333 if (num_input_channels > 2 || num_input_channels < 1 ||
334 num_output_channels > 2 || num_output_channels < 1 ||
335 num_reverse_channels > 2 || num_reverse_channels < 1) {
336 return kBadNumberChannelsError;
337 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000338 if (beamformer_enabled_ &&
339 (static_cast<size_t>(num_input_channels) != array_geometry_.size() ||
340 num_output_channels > 1)) {
341 return kBadNumberChannelsError;
342 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000343
344 fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000345 fwd_out_format_.set(output_sample_rate_hz, num_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000346 rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
347
348 // We process at the closest native rate >= min(input rate, output rate)...
349 int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate());
350 int fwd_proc_rate;
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000351 if (supports_48kHz_ && min_proc_rate > kSampleRate32kHz) {
352 fwd_proc_rate = kSampleRate48kHz;
353 } else if (min_proc_rate > kSampleRate16kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000354 fwd_proc_rate = kSampleRate32kHz;
355 } else if (min_proc_rate > kSampleRate8kHz) {
356 fwd_proc_rate = kSampleRate16kHz;
357 } else {
358 fwd_proc_rate = kSampleRate8kHz;
359 }
360 // ...with one exception.
361 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
362 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000363 }
364
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000365 fwd_proc_format_.set(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000366
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000367 // We normally process the reverse stream at 16 kHz. Unless...
368 int rev_proc_rate = kSampleRate16kHz;
369 if (fwd_proc_format_.rate() == kSampleRate8kHz) {
370 // ...the forward stream is at 8 kHz.
371 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000372 } else {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000373 if (rev_in_format_.rate() == kSampleRate32kHz) {
374 // ...or the input is at 32 kHz, in which case we use the splitting
375 // filter rather than the resampler.
376 rev_proc_rate = kSampleRate32kHz;
377 }
378 }
379
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000380 // Always downmix the reverse stream to mono for analysis. This has been
381 // demonstrated to work well for AEC in most practical scenarios.
382 rev_proc_format_.set(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000383
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000384 if (fwd_proc_format_.rate() == kSampleRate32kHz ||
385 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000386 split_rate_ = kSampleRate16kHz;
387 } else {
388 split_rate_ = fwd_proc_format_.rate();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000389 }
390
391 return InitializeLocked();
392}
393
394// Calls InitializeLocked() if any of the audio parameters have changed from
395// their current values.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000396int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz,
397 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000398 int reverse_sample_rate_hz,
399 int num_input_channels,
400 int num_output_channels,
401 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000402 if (input_sample_rate_hz == fwd_in_format_.rate() &&
403 output_sample_rate_hz == fwd_out_format_.rate() &&
404 reverse_sample_rate_hz == rev_in_format_.rate() &&
405 num_input_channels == fwd_in_format_.num_channels() &&
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000406 num_output_channels == fwd_out_format_.num_channels() &&
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000407 num_reverse_channels == rev_in_format_.num_channels()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000408 return kNoError;
409 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000410 return InitializeLocked(input_sample_rate_hz,
411 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000412 reverse_sample_rate_hz,
413 num_input_channels,
414 num_output_channels,
415 num_reverse_channels);
416}
417
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000418void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000419 CriticalSectionScoped crit_scoped(crit_);
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000420 for (auto item : component_list_) {
421 item->SetExtraOptions(config);
422 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000423
424 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
425 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
426 InitializeTransient();
427 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000428}
429
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000430int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000431 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000432 return fwd_in_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000433}
434
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000435int AudioProcessingImpl::sample_rate_hz() const {
436 CriticalSectionScoped crit_scoped(crit_);
437 return fwd_in_format_.rate();
438}
439
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000440int AudioProcessingImpl::proc_sample_rate_hz() const {
441 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000442}
443
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000444int AudioProcessingImpl::proc_split_sample_rate_hz() const {
445 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000446}
447
448int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000449 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000450}
451
452int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000453 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000454}
455
456int AudioProcessingImpl::num_output_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000457 return fwd_out_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000458}
459
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000460void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000461 CriticalSectionScoped lock(crit_);
Bjorn Volcker424694c2015-03-27 11:30:43 +0100462 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000463 if (agc_manager_.get()) {
464 agc_manager_->SetCaptureMuted(output_will_be_muted_);
465 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000466}
467
468bool AudioProcessingImpl::output_will_be_muted() const {
Bjorn Volcker424694c2015-03-27 11:30:43 +0100469 CriticalSectionScoped lock(crit_);
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000470 return output_will_be_muted_;
471}
472
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000473int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000474 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000475 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000476 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000477 int output_sample_rate_hz,
478 ChannelLayout output_layout,
479 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000480 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000481 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000482 return kNullPointerError;
483 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000484
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000485 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
486 output_sample_rate_hz,
487 rev_in_format_.rate(),
488 ChannelsFromLayout(input_layout),
489 ChannelsFromLayout(output_layout),
490 rev_in_format_.num_channels()));
491 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000492 return kBadDataLengthError;
493 }
494
495#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
496 if (debug_file_->Open()) {
497 event_msg_->set_type(audioproc::Event::STREAM);
498 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000499 const size_t channel_size =
500 sizeof(float) * fwd_in_format_.samples_per_channel();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000501 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
502 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000503 }
504#endif
505
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000506 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000507 RETURN_ON_ERR(ProcessStreamLocked());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +0000508 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
509 output_layout,
510 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000511
512#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
513 if (debug_file_->Open()) {
514 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000515 const size_t channel_size =
516 sizeof(float) * fwd_out_format_.samples_per_channel();
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000517 for (int i = 0; i < fwd_out_format_.num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000518 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000519 RETURN_ON_ERR(WriteMessageToDebugFile());
520 }
521#endif
522
523 return kNoError;
524}
525
526int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
527 CriticalSectionScoped crit_scoped(crit_);
528 if (!frame) {
529 return kNullPointerError;
530 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000531 // Must be a native rate.
532 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
533 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000534 frame->sample_rate_hz_ != kSampleRate32kHz &&
535 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000536 return kBadSampleRateError;
537 }
538 if (echo_control_mobile_->is_enabled() &&
539 frame->sample_rate_hz_ > kSampleRate16kHz) {
540 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
541 return kUnsupportedComponentError;
542 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000543
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000544 // TODO(ajm): The input and output rates and channels are currently
545 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000546 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000547 frame->sample_rate_hz_,
548 rev_in_format_.rate(),
549 frame->num_channels_,
550 frame->num_channels_,
551 rev_in_format_.num_channels()));
552 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000553 return kBadDataLengthError;
554 }
555
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000556#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000557 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000558 event_msg_->set_type(audioproc::Event::STREAM);
559 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000560 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000561 frame->samples_per_channel_ *
562 frame->num_channels_;
563 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000564 }
565#endif
566
567 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000568 RETURN_ON_ERR(ProcessStreamLocked());
569 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
570
571#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
572 if (debug_file_->Open()) {
573 audioproc::Stream* msg = event_msg_->mutable_stream();
574 const size_t data_size = sizeof(int16_t) *
575 frame->samples_per_channel_ *
576 frame->num_channels_;
577 msg->set_output_data(frame->data_, data_size);
578 RETURN_ON_ERR(WriteMessageToDebugFile());
579 }
580#endif
581
582 return kNoError;
583}
584
585
586int AudioProcessingImpl::ProcessStreamLocked() {
587#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
588 if (debug_file_->Open()) {
589 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000590 msg->set_delay(stream_delay_ms_);
591 msg->set_drift(echo_cancellation_->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +0000592 msg->set_level(gain_control()->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000593 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000594 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000595#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000596
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200597 MaybeUpdateHistograms();
598
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000599 AudioBuffer* ca = capture_audio_.get(); // For brevity.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000600 if (use_new_agc_ && gain_control_->is_enabled()) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000601 agc_manager_->AnalyzePreProcess(ca->channels()[0],
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000602 ca->num_channels(),
603 fwd_proc_format_.samples_per_channel());
604 }
605
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000606 bool data_processed = is_data_processed();
607 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000608 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000609 }
610
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000611 if (beamformer_enabled_) {
Michael Graczykdfa36052015-03-25 16:37:27 -0700612 beamformer_->ProcessChunk(*ca->split_data_f(), ca->split_data_f());
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000613 ca->set_num_channels(1);
614 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000615
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000616 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
617 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000618 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000619 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000620
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000621 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000622 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000623 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000624 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
625 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
626 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000627
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000628 if (use_new_agc_ &&
629 gain_control_->is_enabled() &&
630 (!beamformer_enabled_ || beamformer_->is_target_present())) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000631 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000632 ca->num_frames_per_band(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000633 split_rate_);
634 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000635 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000636
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000637 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000638 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000639 }
640
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000641 // TODO(aluebs): Investigate if the transient suppression placement should be
642 // before or after the AGC.
643 if (transient_suppressor_enabled_) {
644 float voice_probability =
645 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
646
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000647 transient_suppressor_->Suppress(ca->channels_f()[0],
648 ca->num_frames(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000649 ca->num_channels(),
650 ca->split_bands_const_f(0)[kBand0To8kHz],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000651 ca->num_frames_per_band(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000652 ca->keyboard_data(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000653 ca->num_keyboard_frames(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000654 voice_probability,
655 key_pressed_);
656 }
657
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000658 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000659 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000660
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000661 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000662 return kNoError;
663}
664
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000665int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
666 int samples_per_channel,
667 int sample_rate_hz,
668 ChannelLayout layout) {
669 CriticalSectionScoped crit_scoped(crit_);
670 if (data == NULL) {
671 return kNullPointerError;
672 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000673
674 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000675 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
676 fwd_out_format_.rate(),
677 sample_rate_hz,
678 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000679 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000680 num_channels));
681 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000682 return kBadDataLengthError;
683 }
684
685#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
686 if (debug_file_->Open()) {
687 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
688 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000689 const size_t channel_size =
690 sizeof(float) * rev_in_format_.samples_per_channel();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000691 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000692 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000693 RETURN_ON_ERR(WriteMessageToDebugFile());
694 }
695#endif
696
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000697 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000698 return AnalyzeReverseStreamLocked();
699}
700
niklase@google.com470e71d2011-07-07 08:21:25 +0000701int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000702 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000703 if (frame == NULL) {
704 return kNullPointerError;
705 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000706 // Must be a native rate.
707 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
708 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000709 frame->sample_rate_hz_ != kSampleRate32kHz &&
710 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000711 return kBadSampleRateError;
712 }
713 // This interface does not tolerate different forward and reverse rates.
714 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000715 return kBadSampleRateError;
716 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000717
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000718 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
719 fwd_out_format_.rate(),
720 frame->sample_rate_hz_,
721 fwd_in_format_.num_channels(),
722 fwd_in_format_.num_channels(),
723 frame->num_channels_));
724 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000725 return kBadDataLengthError;
726 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000727
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000728#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000729 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000730 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
731 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000732 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000733 frame->samples_per_channel_ *
734 frame->num_channels_;
735 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000736 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000737 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000738#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000739
740 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000741 return AnalyzeReverseStreamLocked();
742}
niklase@google.com470e71d2011-07-07 08:21:25 +0000743
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000744int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000745 AudioBuffer* ra = render_audio_.get(); // For brevity.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000746 if (rev_proc_format_.rate() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000747 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000748 }
749
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000750 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
751 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000752 if (!use_new_agc_) {
753 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
754 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000755
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000756 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000757}
758
759int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000760 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000761 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000762 delay += delay_offset_ms_;
763
niklase@google.com470e71d2011-07-07 08:21:25 +0000764 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000765 delay = 0;
766 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000767 }
768
769 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
770 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000771 delay = 500;
772 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000773 }
774
775 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000776 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000777}
778
779int AudioProcessingImpl::stream_delay_ms() const {
780 return stream_delay_ms_;
781}
782
783bool AudioProcessingImpl::was_stream_delay_set() const {
784 return was_stream_delay_set_;
785}
786
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000787void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
788 key_pressed_ = key_pressed;
789}
790
791bool AudioProcessingImpl::stream_key_pressed() const {
792 return key_pressed_;
793}
794
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000795void AudioProcessingImpl::set_delay_offset_ms(int offset) {
796 CriticalSectionScoped crit_scoped(crit_);
797 delay_offset_ms_ = offset;
798}
799
800int AudioProcessingImpl::delay_offset_ms() const {
801 return delay_offset_ms_;
802}
803
niklase@google.com470e71d2011-07-07 08:21:25 +0000804int AudioProcessingImpl::StartDebugRecording(
805 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000806 CriticalSectionScoped crit_scoped(crit_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +0200807 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +0000808
809 if (filename == NULL) {
810 return kNullPointerError;
811 }
812
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000813#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000814 // Stop any ongoing recording.
815 if (debug_file_->Open()) {
816 if (debug_file_->CloseFile() == -1) {
817 return kFileError;
818 }
819 }
820
821 if (debug_file_->OpenFile(filename, false) == -1) {
822 debug_file_->CloseFile();
823 return kFileError;
824 }
825
ajm@google.com808e0e02011-08-03 21:08:51 +0000826 int err = WriteInitMessage();
827 if (err != kNoError) {
828 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000829 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000830 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000831#else
832 return kUnsupportedFunctionError;
833#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000834}
835
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000836int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
837 CriticalSectionScoped crit_scoped(crit_);
838
839 if (handle == NULL) {
840 return kNullPointerError;
841 }
842
843#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
844 // Stop any ongoing recording.
845 if (debug_file_->Open()) {
846 if (debug_file_->CloseFile() == -1) {
847 return kFileError;
848 }
849 }
850
851 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
852 return kFileError;
853 }
854
855 int err = WriteInitMessage();
856 if (err != kNoError) {
857 return err;
858 }
859 return kNoError;
860#else
861 return kUnsupportedFunctionError;
862#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
863}
864
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000865int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
866 rtc::PlatformFile handle) {
867 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
868 return StartDebugRecording(stream);
869}
870
niklase@google.com470e71d2011-07-07 08:21:25 +0000871int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000872 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000873
874#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000875 // We just return if recording hasn't started.
876 if (debug_file_->Open()) {
877 if (debug_file_->CloseFile() == -1) {
878 return kFileError;
879 }
880 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000881 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000882#else
883 return kUnsupportedFunctionError;
884#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000885}
886
887EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
888 return echo_cancellation_;
889}
890
891EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
892 return echo_control_mobile_;
893}
894
895GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000896 if (use_new_agc_) {
897 return gain_control_for_new_agc_.get();
898 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000899 return gain_control_;
900}
901
902HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
903 return high_pass_filter_;
904}
905
906LevelEstimator* AudioProcessingImpl::level_estimator() const {
907 return level_estimator_;
908}
909
910NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
911 return noise_suppression_;
912}
913
914VoiceDetection* AudioProcessingImpl::voice_detection() const {
915 return voice_detection_;
916}
917
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000918bool AudioProcessingImpl::is_data_processed() const {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000919 if (beamformer_enabled_) {
920 return true;
921 }
922
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000923 int enabled_count = 0;
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000924 for (auto item : component_list_) {
925 if (item->is_component_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000926 enabled_count++;
927 }
928 }
929
930 // Data is unchanged if no components are enabled, or if only level_estimator_
931 // or voice_detection_ is enabled.
932 if (enabled_count == 0) {
933 return false;
934 } else if (enabled_count == 1) {
935 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
936 return false;
937 }
938 } else if (enabled_count == 2) {
939 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
940 return false;
941 }
942 }
943 return true;
944}
945
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000946bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000947 // Check if we've upmixed or downmixed the audio.
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000948 return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000949 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000950}
951
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000952bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000953 return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz ||
954 fwd_proc_format_.rate() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000955}
956
957bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000958 if (!is_data_processed && !voice_detection_->is_enabled() &&
959 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000960 // Only level_estimator_ is enabled.
961 return false;
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000962 } else if (fwd_proc_format_.rate() == kSampleRate32kHz ||
963 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000964 // Something besides level_estimator_ is enabled, and we have super-wb.
965 return true;
966 }
967 return false;
968}
969
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200970void AudioProcessingImpl::InitializeExperimentalAgc() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000971 if (use_new_agc_) {
972 if (!agc_manager_.get()) {
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200973 agc_manager_.reset(new AgcManagerDirect(gain_control_,
974 gain_control_for_new_agc_.get(),
975 agc_startup_min_volume_));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000976 }
977 agc_manager_->Initialize();
978 agc_manager_->SetCaptureMuted(output_will_be_muted_);
979 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000980}
981
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200982void AudioProcessingImpl::InitializeTransient() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000983 if (transient_suppressor_enabled_) {
984 if (!transient_suppressor_.get()) {
985 transient_suppressor_.reset(new TransientSuppressor());
986 }
987 transient_suppressor_->Initialize(fwd_proc_format_.rate(),
988 split_rate_,
989 fwd_out_format_.num_channels());
990 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000991}
992
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000993void AudioProcessingImpl::InitializeBeamformer() {
994 if (beamformer_enabled_) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000995 if (!beamformer_) {
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +0000996 beamformer_.reset(new NonlinearBeamformer(array_geometry_));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000997 }
998 beamformer_->Initialize(kChunkSizeMs, split_rate_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000999 }
1000}
1001
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001002void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001003 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001004
1005 if (echo_cancellation()->is_enabled()) {
1006 // Detect a jump in platform reported system delay and log the difference.
1007 const int diff_stream_delay_ms = stream_delay_ms_ - last_stream_delay_ms_;
1008 if (diff_stream_delay_ms > kMinDiffDelayMs && last_stream_delay_ms_ != 0) {
1009 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1010 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
1011 }
1012 last_stream_delay_ms_ = stream_delay_ms_;
1013
1014 // Detect a jump in AEC system delay and log the difference.
1015 const int frames_per_ms = rtc::CheckedDivExact(split_rate_, 1000);
1016 const int aec_system_delay_ms =
1017 WebRtcAec_system_delay(echo_cancellation()->aec_core()) / frames_per_ms;
1018 const int diff_aec_system_delay_ms = aec_system_delay_ms -
1019 last_aec_system_delay_ms_;
1020 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
1021 last_aec_system_delay_ms_ != 0) {
1022 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1023 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1024 100);
1025 }
1026 last_aec_system_delay_ms_ = aec_system_delay_ms;
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001027 // TODO(bjornv): Consider also logging amount of jumps. This gives a better
1028 // indication of how frequent jumps are.
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001029 }
1030}
1031
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001032#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +00001033int AudioProcessingImpl::WriteMessageToDebugFile() {
1034 int32_t size = event_msg_->ByteSize();
1035 if (size <= 0) {
1036 return kUnspecifiedError;
1037 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001038#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +00001039 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1040 // pretty safe in assuming little-endian.
1041#endif
1042
1043 if (!event_msg_->SerializeToString(&event_str_)) {
1044 return kUnspecifiedError;
1045 }
1046
1047 // Write message preceded by its size.
1048 if (!debug_file_->Write(&size, sizeof(int32_t))) {
1049 return kFileError;
1050 }
1051 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
1052 return kFileError;
1053 }
1054
1055 event_msg_->Clear();
1056
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001057 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001058}
1059
1060int AudioProcessingImpl::WriteInitMessage() {
1061 event_msg_->set_type(audioproc::Event::INIT);
1062 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001063 msg->set_sample_rate(fwd_in_format_.rate());
1064 msg->set_num_input_channels(fwd_in_format_.num_channels());
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +00001065 msg->set_num_output_channels(fwd_out_format_.num_channels());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001066 msg->set_num_reverse_channels(rev_in_format_.num_channels());
1067 msg->set_reverse_sample_rate(rev_in_format_.rate());
1068 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +00001069
1070 int err = WriteMessageToDebugFile();
1071 if (err != kNoError) {
1072 return err;
1073 }
1074
1075 return kNoError;
1076}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001077#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001078
niklase@google.com470e71d2011-07-07 08:21:25 +00001079} // namespace webrtc