blob: 87b82a6a3509131adae9ed698cc0f896fd01d4c0 [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),
Bjorn Volcker4e7aa432015-07-07 11:50:05 +0200180 stream_delay_jumps_(-1),
181 aec_system_delay_jumps_(-1),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +0000182 output_will_be_muted_(false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000183 key_pressed_(false),
184#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
185 use_new_agc_(false),
186#else
187 use_new_agc_(config.Get<ExperimentalAgc>().enabled),
188#endif
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200189 agc_startup_min_volume_(config.Get<ExperimentalAgc>().startup_min_volume),
andrew1c7075f2015-06-24 18:14:14 -0700190#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
191 transient_suppressor_enabled_(false),
192#else
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000193 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700194#endif
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000195 beamformer_enabled_(config.Get<Beamforming>().enabled),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000196 beamformer_(beamformer),
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000197 array_geometry_(config.Get<Beamforming>().array_geometry),
198 supports_48kHz_(config.Get<AudioProcessing48kHzSupport>().enabled) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000199 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 component_list_.push_back(echo_cancellation_);
201
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000202 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000203 component_list_.push_back(echo_control_mobile_);
204
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000205 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 component_list_.push_back(gain_control_);
207
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000208 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000209 component_list_.push_back(high_pass_filter_);
210
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000211 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000212 component_list_.push_back(level_estimator_);
213
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000214 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000215 component_list_.push_back(noise_suppression_);
216
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000217 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000218 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000219
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000220 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
221
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000222 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000223}
224
225AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000226 {
227 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000228 // Depends on gain_control_ and gain_control_for_new_agc_.
229 agc_manager_.reset();
230 // Depends on gain_control_.
231 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000232 while (!component_list_.empty()) {
233 ProcessingComponent* component = component_list_.front();
234 component->Destroy();
235 delete component;
236 component_list_.pop_front();
237 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000238
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000239#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000240 if (debug_file_->Open()) {
241 debug_file_->CloseFile();
242 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000243#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000244 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000245 delete crit_;
246 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000247}
248
niklase@google.com470e71d2011-07-07 08:21:25 +0000249int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000250 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000251 return InitializeLocked();
252}
253
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000254int AudioProcessingImpl::set_sample_rate_hz(int rate) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000255 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000256 return InitializeLocked(rate,
257 rate,
258 rev_in_format_.rate(),
259 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000260 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000261 rev_in_format_.num_channels());
262}
263
264int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
265 int output_sample_rate_hz,
266 int reverse_sample_rate_hz,
267 ChannelLayout input_layout,
268 ChannelLayout output_layout,
269 ChannelLayout reverse_layout) {
270 CriticalSectionScoped crit_scoped(crit_);
271 return InitializeLocked(input_sample_rate_hz,
272 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000273 reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000274 ChannelsFromLayout(input_layout),
275 ChannelsFromLayout(output_layout),
276 ChannelsFromLayout(reverse_layout));
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000277}
278
niklase@google.com470e71d2011-07-07 08:21:25 +0000279int AudioProcessingImpl::InitializeLocked() {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000280 const int fwd_audio_buffer_channels = beamformer_enabled_ ?
281 fwd_in_format_.num_channels() :
282 fwd_out_format_.num_channels();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000283 render_audio_.reset(new AudioBuffer(rev_in_format_.samples_per_channel(),
284 rev_in_format_.num_channels(),
285 rev_proc_format_.samples_per_channel(),
286 rev_proc_format_.num_channels(),
287 rev_proc_format_.samples_per_channel()));
288 capture_audio_.reset(new AudioBuffer(fwd_in_format_.samples_per_channel(),
289 fwd_in_format_.num_channels(),
290 fwd_proc_format_.samples_per_channel(),
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000291 fwd_audio_buffer_channels,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000292 fwd_out_format_.samples_per_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000293
niklase@google.com470e71d2011-07-07 08:21:25 +0000294 // Initialize all components.
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000295 for (auto item : component_list_) {
296 int err = item->Initialize();
niklase@google.com470e71d2011-07-07 08:21:25 +0000297 if (err != kNoError) {
298 return err;
299 }
300 }
301
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200302 InitializeExperimentalAgc();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000303
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200304 InitializeTransient();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000305
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000306 InitializeBeamformer();
307
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000308#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000309 if (debug_file_->Open()) {
310 int err = WriteInitMessage();
311 if (err != kNoError) {
312 return err;
313 }
314 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000315#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000316
niklase@google.com470e71d2011-07-07 08:21:25 +0000317 return kNoError;
318}
319
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000320int AudioProcessingImpl::InitializeLocked(int input_sample_rate_hz,
321 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000322 int reverse_sample_rate_hz,
323 int num_input_channels,
324 int num_output_channels,
325 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000326 if (input_sample_rate_hz <= 0 ||
327 output_sample_rate_hz <= 0 ||
328 reverse_sample_rate_hz <= 0) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000329 return kBadSampleRateError;
330 }
331 if (num_output_channels > num_input_channels) {
332 return kBadNumberChannelsError;
333 }
334 // Only mono and stereo supported currently.
335 if (num_input_channels > 2 || num_input_channels < 1 ||
336 num_output_channels > 2 || num_output_channels < 1 ||
337 num_reverse_channels > 2 || num_reverse_channels < 1) {
338 return kBadNumberChannelsError;
339 }
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000340 if (beamformer_enabled_ &&
341 (static_cast<size_t>(num_input_channels) != array_geometry_.size() ||
342 num_output_channels > 1)) {
343 return kBadNumberChannelsError;
344 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000345
346 fwd_in_format_.set(input_sample_rate_hz, num_input_channels);
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000347 fwd_out_format_.set(output_sample_rate_hz, num_output_channels);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000348 rev_in_format_.set(reverse_sample_rate_hz, num_reverse_channels);
349
350 // We process at the closest native rate >= min(input rate, output rate)...
351 int min_proc_rate = std::min(fwd_in_format_.rate(), fwd_out_format_.rate());
352 int fwd_proc_rate;
aluebs@webrtc.orgc9ce07e2015-03-02 20:07:31 +0000353 if (supports_48kHz_ && min_proc_rate > kSampleRate32kHz) {
354 fwd_proc_rate = kSampleRate48kHz;
355 } else if (min_proc_rate > kSampleRate16kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000356 fwd_proc_rate = kSampleRate32kHz;
357 } else if (min_proc_rate > kSampleRate8kHz) {
358 fwd_proc_rate = kSampleRate16kHz;
359 } else {
360 fwd_proc_rate = kSampleRate8kHz;
361 }
362 // ...with one exception.
363 if (echo_control_mobile_->is_enabled() && min_proc_rate > kSampleRate16kHz) {
364 fwd_proc_rate = kSampleRate16kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000365 }
366
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000367 fwd_proc_format_.set(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000368
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000369 // We normally process the reverse stream at 16 kHz. Unless...
370 int rev_proc_rate = kSampleRate16kHz;
371 if (fwd_proc_format_.rate() == kSampleRate8kHz) {
372 // ...the forward stream is at 8 kHz.
373 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000374 } else {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000375 if (rev_in_format_.rate() == kSampleRate32kHz) {
376 // ...or the input is at 32 kHz, in which case we use the splitting
377 // filter rather than the resampler.
378 rev_proc_rate = kSampleRate32kHz;
379 }
380 }
381
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000382 // Always downmix the reverse stream to mono for analysis. This has been
383 // demonstrated to work well for AEC in most practical scenarios.
384 rev_proc_format_.set(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000385
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000386 if (fwd_proc_format_.rate() == kSampleRate32kHz ||
387 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000388 split_rate_ = kSampleRate16kHz;
389 } else {
390 split_rate_ = fwd_proc_format_.rate();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000391 }
392
393 return InitializeLocked();
394}
395
396// Calls InitializeLocked() if any of the audio parameters have changed from
397// their current values.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000398int AudioProcessingImpl::MaybeInitializeLocked(int input_sample_rate_hz,
399 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000400 int reverse_sample_rate_hz,
401 int num_input_channels,
402 int num_output_channels,
403 int num_reverse_channels) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000404 if (input_sample_rate_hz == fwd_in_format_.rate() &&
405 output_sample_rate_hz == fwd_out_format_.rate() &&
406 reverse_sample_rate_hz == rev_in_format_.rate() &&
407 num_input_channels == fwd_in_format_.num_channels() &&
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000408 num_output_channels == fwd_out_format_.num_channels() &&
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000409 num_reverse_channels == rev_in_format_.num_channels()) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000410 return kNoError;
411 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000412 return InitializeLocked(input_sample_rate_hz,
413 output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000414 reverse_sample_rate_hz,
415 num_input_channels,
416 num_output_channels,
417 num_reverse_channels);
418}
419
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000420void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000421 CriticalSectionScoped crit_scoped(crit_);
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000422 for (auto item : component_list_) {
423 item->SetExtraOptions(config);
424 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000425
426 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
427 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
428 InitializeTransient();
429 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000430}
431
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000432int AudioProcessingImpl::input_sample_rate_hz() const {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000433 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000434 return fwd_in_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000435}
436
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000437int AudioProcessingImpl::sample_rate_hz() const {
438 CriticalSectionScoped crit_scoped(crit_);
439 return fwd_in_format_.rate();
440}
441
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000442int AudioProcessingImpl::proc_sample_rate_hz() const {
443 return fwd_proc_format_.rate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000444}
445
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000446int AudioProcessingImpl::proc_split_sample_rate_hz() const {
447 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000448}
449
450int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000451 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000452}
453
454int AudioProcessingImpl::num_input_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000455 return fwd_in_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000456}
457
458int AudioProcessingImpl::num_output_channels() const {
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000459 return fwd_out_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000460}
461
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000462void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000463 CriticalSectionScoped lock(crit_);
Bjorn Volcker424694c2015-03-27 11:30:43 +0100464 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000465 if (agc_manager_.get()) {
466 agc_manager_->SetCaptureMuted(output_will_be_muted_);
467 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000468}
469
470bool AudioProcessingImpl::output_will_be_muted() const {
Bjorn Volcker424694c2015-03-27 11:30:43 +0100471 CriticalSectionScoped lock(crit_);
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000472 return output_will_be_muted_;
473}
474
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000475int AudioProcessingImpl::ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000476 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000477 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000478 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000479 int output_sample_rate_hz,
480 ChannelLayout output_layout,
481 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000482 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000483 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000484 return kNullPointerError;
485 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000486
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000487 RETURN_ON_ERR(MaybeInitializeLocked(input_sample_rate_hz,
488 output_sample_rate_hz,
489 rev_in_format_.rate(),
490 ChannelsFromLayout(input_layout),
491 ChannelsFromLayout(output_layout),
492 rev_in_format_.num_channels()));
493 if (samples_per_channel != fwd_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000494 return kBadDataLengthError;
495 }
496
497#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
498 if (debug_file_->Open()) {
499 event_msg_->set_type(audioproc::Event::STREAM);
500 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000501 const size_t channel_size =
502 sizeof(float) * fwd_in_format_.samples_per_channel();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000503 for (int i = 0; i < fwd_in_format_.num_channels(); ++i)
504 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000505 }
506#endif
507
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000508 capture_audio_->CopyFrom(src, samples_per_channel, input_layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000509 RETURN_ON_ERR(ProcessStreamLocked());
mgraczyk@chromium.orgd6e84d92015-01-14 01:33:54 +0000510 capture_audio_->CopyTo(fwd_out_format_.samples_per_channel(),
511 output_layout,
512 dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000513
514#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
515 if (debug_file_->Open()) {
516 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000517 const size_t channel_size =
518 sizeof(float) * fwd_out_format_.samples_per_channel();
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000519 for (int i = 0; i < fwd_out_format_.num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000520 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000521 RETURN_ON_ERR(WriteMessageToDebugFile());
522 }
523#endif
524
525 return kNoError;
526}
527
528int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
529 CriticalSectionScoped crit_scoped(crit_);
530 if (!frame) {
531 return kNullPointerError;
532 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000533 // Must be a native rate.
534 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
535 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000536 frame->sample_rate_hz_ != kSampleRate32kHz &&
537 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000538 return kBadSampleRateError;
539 }
540 if (echo_control_mobile_->is_enabled() &&
541 frame->sample_rate_hz_ > kSampleRate16kHz) {
542 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
543 return kUnsupportedComponentError;
544 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000545
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000546 // TODO(ajm): The input and output rates and channels are currently
547 // constrained to be identical in the int16 interface.
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000548 RETURN_ON_ERR(MaybeInitializeLocked(frame->sample_rate_hz_,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000549 frame->sample_rate_hz_,
550 rev_in_format_.rate(),
551 frame->num_channels_,
552 frame->num_channels_,
553 rev_in_format_.num_channels()));
554 if (frame->samples_per_channel_ != fwd_in_format_.samples_per_channel()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000555 return kBadDataLengthError;
556 }
557
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000558#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000559 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000560 event_msg_->set_type(audioproc::Event::STREAM);
561 audioproc::Stream* msg = event_msg_->mutable_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000562 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000563 frame->samples_per_channel_ *
564 frame->num_channels_;
565 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000566 }
567#endif
568
569 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000570 RETURN_ON_ERR(ProcessStreamLocked());
571 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
572
573#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
574 if (debug_file_->Open()) {
575 audioproc::Stream* msg = event_msg_->mutable_stream();
576 const size_t data_size = sizeof(int16_t) *
577 frame->samples_per_channel_ *
578 frame->num_channels_;
579 msg->set_output_data(frame->data_, data_size);
580 RETURN_ON_ERR(WriteMessageToDebugFile());
581 }
582#endif
583
584 return kNoError;
585}
586
587
588int AudioProcessingImpl::ProcessStreamLocked() {
589#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
590 if (debug_file_->Open()) {
591 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000592 msg->set_delay(stream_delay_ms_);
593 msg->set_drift(echo_cancellation_->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +0000594 msg->set_level(gain_control()->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000595 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000596 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000597#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000598
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200599 MaybeUpdateHistograms();
600
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000601 AudioBuffer* ca = capture_audio_.get(); // For brevity.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000602 if (use_new_agc_ && gain_control_->is_enabled()) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000603 agc_manager_->AnalyzePreProcess(ca->channels()[0],
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000604 ca->num_channels(),
605 fwd_proc_format_.samples_per_channel());
606 }
607
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000608 bool data_processed = is_data_processed();
609 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000610 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000611 }
612
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000613 if (beamformer_enabled_) {
Michael Graczykdfa36052015-03-25 16:37:27 -0700614 beamformer_->ProcessChunk(*ca->split_data_f(), ca->split_data_f());
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000615 ca->set_num_channels(1);
616 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000617
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000618 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
619 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000620 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000621 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000622
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000623 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000624 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000625 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000626 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
627 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
628 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000629
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000630 if (use_new_agc_ &&
631 gain_control_->is_enabled() &&
632 (!beamformer_enabled_ || beamformer_->is_target_present())) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000633 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000634 ca->num_frames_per_band(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000635 split_rate_);
636 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000637 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000638
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000639 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000640 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000641 }
642
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000643 // TODO(aluebs): Investigate if the transient suppression placement should be
644 // before or after the AGC.
645 if (transient_suppressor_enabled_) {
646 float voice_probability =
647 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
648
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000649 transient_suppressor_->Suppress(ca->channels_f()[0],
650 ca->num_frames(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000651 ca->num_channels(),
652 ca->split_bands_const_f(0)[kBand0To8kHz],
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000653 ca->num_frames_per_band(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000654 ca->keyboard_data(),
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000655 ca->num_keyboard_frames(),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000656 voice_probability,
657 key_pressed_);
658 }
659
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000660 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000661 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000662
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000663 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000664 return kNoError;
665}
666
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000667int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
668 int samples_per_channel,
669 int sample_rate_hz,
670 ChannelLayout layout) {
671 CriticalSectionScoped crit_scoped(crit_);
672 if (data == NULL) {
673 return kNullPointerError;
674 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000675
676 const int num_channels = ChannelsFromLayout(layout);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000677 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
678 fwd_out_format_.rate(),
679 sample_rate_hz,
680 fwd_in_format_.num_channels(),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000681 fwd_out_format_.num_channels(),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000682 num_channels));
683 if (samples_per_channel != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000684 return kBadDataLengthError;
685 }
686
687#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
688 if (debug_file_->Open()) {
689 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
690 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000691 const size_t channel_size =
692 sizeof(float) * rev_in_format_.samples_per_channel();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000693 for (int i = 0; i < num_channels; ++i)
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000694 msg->add_channel(data[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000695 RETURN_ON_ERR(WriteMessageToDebugFile());
696 }
697#endif
698
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000699 render_audio_->CopyFrom(data, samples_per_channel, layout);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000700 return AnalyzeReverseStreamLocked();
701}
702
niklase@google.com470e71d2011-07-07 08:21:25 +0000703int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000704 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 if (frame == NULL) {
706 return kNullPointerError;
707 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000708 // Must be a native rate.
709 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
710 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000711 frame->sample_rate_hz_ != kSampleRate32kHz &&
712 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000713 return kBadSampleRateError;
714 }
715 // This interface does not tolerate different forward and reverse rates.
716 if (frame->sample_rate_hz_ != fwd_in_format_.rate()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000717 return kBadSampleRateError;
718 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000719
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000720 RETURN_ON_ERR(MaybeInitializeLocked(fwd_in_format_.rate(),
721 fwd_out_format_.rate(),
722 frame->sample_rate_hz_,
723 fwd_in_format_.num_channels(),
724 fwd_in_format_.num_channels(),
725 frame->num_channels_));
726 if (frame->samples_per_channel_ != rev_in_format_.samples_per_channel()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000727 return kBadDataLengthError;
728 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000729
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000730#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000732 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
733 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000734 const size_t data_size = sizeof(int16_t) *
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000735 frame->samples_per_channel_ *
736 frame->num_channels_;
737 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000738 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000740#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000741
742 render_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000743 return AnalyzeReverseStreamLocked();
744}
niklase@google.com470e71d2011-07-07 08:21:25 +0000745
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000746int AudioProcessingImpl::AnalyzeReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000747 AudioBuffer* ra = render_audio_.get(); // For brevity.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000748 if (rev_proc_format_.rate() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000749 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000750 }
751
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000752 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
753 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000754 if (!use_new_agc_) {
755 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
756 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000757
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000758 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000759}
760
761int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000762 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000763 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000764 delay += delay_offset_ms_;
765
niklase@google.com470e71d2011-07-07 08:21:25 +0000766 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000767 delay = 0;
768 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000769 }
770
771 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
772 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000773 delay = 500;
774 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000775 }
776
777 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000778 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000779}
780
781int AudioProcessingImpl::stream_delay_ms() const {
782 return stream_delay_ms_;
783}
784
785bool AudioProcessingImpl::was_stream_delay_set() const {
786 return was_stream_delay_set_;
787}
788
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000789void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
790 key_pressed_ = key_pressed;
791}
792
793bool AudioProcessingImpl::stream_key_pressed() const {
794 return key_pressed_;
795}
796
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000797void AudioProcessingImpl::set_delay_offset_ms(int offset) {
798 CriticalSectionScoped crit_scoped(crit_);
799 delay_offset_ms_ = offset;
800}
801
802int AudioProcessingImpl::delay_offset_ms() const {
803 return delay_offset_ms_;
804}
805
niklase@google.com470e71d2011-07-07 08:21:25 +0000806int AudioProcessingImpl::StartDebugRecording(
807 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000808 CriticalSectionScoped crit_scoped(crit_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +0200809 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +0000810
811 if (filename == NULL) {
812 return kNullPointerError;
813 }
814
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000815#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000816 // Stop any ongoing recording.
817 if (debug_file_->Open()) {
818 if (debug_file_->CloseFile() == -1) {
819 return kFileError;
820 }
821 }
822
823 if (debug_file_->OpenFile(filename, false) == -1) {
824 debug_file_->CloseFile();
825 return kFileError;
826 }
827
ajm@google.com808e0e02011-08-03 21:08:51 +0000828 int err = WriteInitMessage();
829 if (err != kNoError) {
830 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000831 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000832 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000833#else
834 return kUnsupportedFunctionError;
835#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000836}
837
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000838int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
839 CriticalSectionScoped crit_scoped(crit_);
840
841 if (handle == NULL) {
842 return kNullPointerError;
843 }
844
845#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
846 // Stop any ongoing recording.
847 if (debug_file_->Open()) {
848 if (debug_file_->CloseFile() == -1) {
849 return kFileError;
850 }
851 }
852
853 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
854 return kFileError;
855 }
856
857 int err = WriteInitMessage();
858 if (err != kNoError) {
859 return err;
860 }
861 return kNoError;
862#else
863 return kUnsupportedFunctionError;
864#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
865}
866
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000867int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
868 rtc::PlatformFile handle) {
869 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
870 return StartDebugRecording(stream);
871}
872
niklase@google.com470e71d2011-07-07 08:21:25 +0000873int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000874 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000875
876#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000877 // We just return if recording hasn't started.
878 if (debug_file_->Open()) {
879 if (debug_file_->CloseFile() == -1) {
880 return kFileError;
881 }
882 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000883 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000884#else
885 return kUnsupportedFunctionError;
886#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000887}
888
889EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
890 return echo_cancellation_;
891}
892
893EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
894 return echo_control_mobile_;
895}
896
897GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000898 if (use_new_agc_) {
899 return gain_control_for_new_agc_.get();
900 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000901 return gain_control_;
902}
903
904HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
905 return high_pass_filter_;
906}
907
908LevelEstimator* AudioProcessingImpl::level_estimator() const {
909 return level_estimator_;
910}
911
912NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
913 return noise_suppression_;
914}
915
916VoiceDetection* AudioProcessingImpl::voice_detection() const {
917 return voice_detection_;
918}
919
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000920bool AudioProcessingImpl::is_data_processed() const {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000921 if (beamformer_enabled_) {
922 return true;
923 }
924
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000925 int enabled_count = 0;
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000926 for (auto item : component_list_) {
927 if (item->is_component_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000928 enabled_count++;
929 }
930 }
931
932 // Data is unchanged if no components are enabled, or if only level_estimator_
933 // or voice_detection_ is enabled.
934 if (enabled_count == 0) {
935 return false;
936 } else if (enabled_count == 1) {
937 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
938 return false;
939 }
940 } else if (enabled_count == 2) {
941 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
942 return false;
943 }
944 }
945 return true;
946}
947
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000948bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000949 // Check if we've upmixed or downmixed the audio.
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000950 return ((fwd_out_format_.num_channels() != fwd_in_format_.num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000951 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000952}
953
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000954bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000955 return (is_data_processed && (fwd_proc_format_.rate() == kSampleRate32kHz ||
956 fwd_proc_format_.rate() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000957}
958
959bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000960 if (!is_data_processed && !voice_detection_->is_enabled() &&
961 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000962 // Only level_estimator_ is enabled.
963 return false;
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000964 } else if (fwd_proc_format_.rate() == kSampleRate32kHz ||
965 fwd_proc_format_.rate() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000966 // Something besides level_estimator_ is enabled, and we have super-wb.
967 return true;
968 }
969 return false;
970}
971
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200972void AudioProcessingImpl::InitializeExperimentalAgc() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000973 if (use_new_agc_) {
974 if (!agc_manager_.get()) {
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200975 agc_manager_.reset(new AgcManagerDirect(gain_control_,
976 gain_control_for_new_agc_.get(),
977 agc_startup_min_volume_));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000978 }
979 agc_manager_->Initialize();
980 agc_manager_->SetCaptureMuted(output_will_be_muted_);
981 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000982}
983
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200984void AudioProcessingImpl::InitializeTransient() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000985 if (transient_suppressor_enabled_) {
986 if (!transient_suppressor_.get()) {
987 transient_suppressor_.reset(new TransientSuppressor());
988 }
989 transient_suppressor_->Initialize(fwd_proc_format_.rate(),
990 split_rate_,
991 fwd_out_format_.num_channels());
992 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000993}
994
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000995void AudioProcessingImpl::InitializeBeamformer() {
996 if (beamformer_enabled_) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000997 if (!beamformer_) {
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +0000998 beamformer_.reset(new NonlinearBeamformer(array_geometry_));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000999 }
1000 beamformer_->Initialize(kChunkSizeMs, split_rate_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001001 }
1002}
1003
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001004void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001005 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001006
1007 if (echo_cancellation()->is_enabled()) {
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001008 // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
1009 // If a stream has echo we know that the echo_cancellation is in process.
1010 if (stream_delay_jumps_ == -1 && echo_cancellation()->stream_has_echo()) {
1011 stream_delay_jumps_ = 0;
1012 }
1013 if (aec_system_delay_jumps_ == -1 &&
1014 echo_cancellation()->stream_has_echo()) {
1015 aec_system_delay_jumps_ = 0;
1016 }
1017
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001018 // Detect a jump in platform reported system delay and log the difference.
1019 const int diff_stream_delay_ms = stream_delay_ms_ - last_stream_delay_ms_;
1020 if (diff_stream_delay_ms > kMinDiffDelayMs && last_stream_delay_ms_ != 0) {
1021 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1022 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001023 if (stream_delay_jumps_ == -1) {
1024 stream_delay_jumps_ = 0; // Activate counter if needed.
1025 }
1026 stream_delay_jumps_++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001027 }
1028 last_stream_delay_ms_ = stream_delay_ms_;
1029
1030 // Detect a jump in AEC system delay and log the difference.
1031 const int frames_per_ms = rtc::CheckedDivExact(split_rate_, 1000);
1032 const int aec_system_delay_ms =
1033 WebRtcAec_system_delay(echo_cancellation()->aec_core()) / frames_per_ms;
1034 const int diff_aec_system_delay_ms = aec_system_delay_ms -
1035 last_aec_system_delay_ms_;
1036 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
1037 last_aec_system_delay_ms_ != 0) {
1038 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1039 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1040 100);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001041 if (aec_system_delay_jumps_ == -1) {
1042 aec_system_delay_jumps_ = 0; // Activate counter if needed.
1043 }
1044 aec_system_delay_jumps_++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001045 }
1046 last_aec_system_delay_ms_ = aec_system_delay_ms;
1047 }
1048}
1049
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001050void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
1051 CriticalSectionScoped crit_scoped(crit_);
1052 if (stream_delay_jumps_ > -1) {
1053 RTC_HISTOGRAM_ENUMERATION(
1054 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
1055 stream_delay_jumps_, 51);
1056 }
1057 stream_delay_jumps_ = -1;
1058 last_stream_delay_ms_ = 0;
1059
1060 if (aec_system_delay_jumps_ > -1) {
1061 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1062 aec_system_delay_jumps_, 51);
1063 }
1064 aec_system_delay_jumps_ = -1;
1065 last_aec_system_delay_ms_ = 0;
1066}
1067
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001068#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +00001069int AudioProcessingImpl::WriteMessageToDebugFile() {
1070 int32_t size = event_msg_->ByteSize();
1071 if (size <= 0) {
1072 return kUnspecifiedError;
1073 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001074#if defined(WEBRTC_ARCH_BIG_ENDIAN)
ajm@google.com808e0e02011-08-03 21:08:51 +00001075 // TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1076 // pretty safe in assuming little-endian.
1077#endif
1078
1079 if (!event_msg_->SerializeToString(&event_str_)) {
1080 return kUnspecifiedError;
1081 }
1082
1083 // Write message preceded by its size.
1084 if (!debug_file_->Write(&size, sizeof(int32_t))) {
1085 return kFileError;
1086 }
1087 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
1088 return kFileError;
1089 }
1090
1091 event_msg_->Clear();
1092
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001093 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001094}
1095
1096int AudioProcessingImpl::WriteInitMessage() {
1097 event_msg_->set_type(audioproc::Event::INIT);
1098 audioproc::Init* msg = event_msg_->mutable_init();
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001099 msg->set_sample_rate(fwd_in_format_.rate());
1100 msg->set_num_input_channels(fwd_in_format_.num_channels());
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +00001101 msg->set_num_output_channels(fwd_out_format_.num_channels());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001102 msg->set_num_reverse_channels(rev_in_format_.num_channels());
1103 msg->set_reverse_sample_rate(rev_in_format_.rate());
1104 msg->set_output_sample_rate(fwd_out_format_.rate());
ajm@google.com808e0e02011-08-03 21:08:51 +00001105
1106 int err = WriteMessageToDebugFile();
1107 if (err != kNoError) {
1108 return err;
1109 }
1110
1111 return kNoError;
1112}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001113#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001114
niklase@google.com470e71d2011-07-07 08:21:25 +00001115} // namespace webrtc