blob: 0de6bf03968c48d61fb4dacc24beeef294e38017 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org40654032012-01-30 20:51:15 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000011#include "webrtc/modules/audio_processing/audio_processing_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
ajm@google.com808e0e02011-08-03 21:08:51 +000013#include <assert.h>
Michael Graczyk86c6d332015-07-23 11:41:39 -070014#include <algorithm>
niklase@google.com470e71d2011-07-07 08:21:25 +000015
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020016#include "webrtc/base/checks.h"
xians@webrtc.orge46bc772014-10-10 08:36:56 +000017#include "webrtc/base/platform_file.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070018#include "webrtc/common_audio/audio_converter.h"
Michael Graczykdfa36052015-03-25 16:37:27 -070019#include "webrtc/common_audio/channel_buffer.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070020#include "webrtc/common_audio/include/audio_util.h"
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000021#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020022extern "C" {
23#include "webrtc/modules/audio_processing/aec/aec_core.h"
24}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000025#include "webrtc/modules/audio_processing/agc/agc_manager_direct.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000026#include "webrtc/modules/audio_processing/audio_buffer.h"
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +000027#include "webrtc/modules/audio_processing/beamformer/nonlinear_beamformer.h"
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +000028#include "webrtc/modules/audio_processing/common.h"
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000029#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000030#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
31#include "webrtc/modules/audio_processing/gain_control_impl.h"
32#include "webrtc/modules/audio_processing/high_pass_filter_impl.h"
ekmeyerson60d9b332015-08-14 10:35:55 -070033#include "webrtc/modules/audio_processing/intelligibility/intelligibility_enhancer.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000034#include "webrtc/modules/audio_processing/level_estimator_impl.h"
35#include "webrtc/modules/audio_processing/noise_suppression_impl.h"
36#include "webrtc/modules/audio_processing/processing_component.h"
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +000037#include "webrtc/modules/audio_processing/transient/transient_suppressor.h"
andrew@webrtc.org78693fe2013-03-01 16:36:19 +000038#include "webrtc/modules/audio_processing/voice_detection_impl.h"
39#include "webrtc/modules/interface/module_common_types.h"
40#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
41#include "webrtc/system_wrappers/interface/file_wrapper.h"
42#include "webrtc/system_wrappers/interface/logging.h"
Bjorn Volcker1ca324f2015-06-29 14:57:29 +020043#include "webrtc/system_wrappers/interface/metrics.h"
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000044
45#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
46// Files generated at build-time by the protobuf compiler.
leozwang@webrtc.orga3736342012-03-16 21:36:00 +000047#ifdef WEBRTC_ANDROID_PLATFORM_BUILD
leozwang@webrtc.org534e4952012-10-22 21:21:52 +000048#include "external/webrtc/webrtc/modules/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000049#else
ajm@google.com808e0e02011-08-03 21:08:51 +000050#include "webrtc/audio_processing/debug.pb.h"
leozwang@google.comce9bfbb2011-08-03 23:34:31 +000051#endif
andrew@webrtc.org7bf26462011-12-03 00:03:31 +000052#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000053
Michael Graczyk86c6d332015-07-23 11:41:39 -070054#define RETURN_ON_ERR(expr) \
55 do { \
56 int err = (expr); \
57 if (err != kNoError) { \
58 return err; \
59 } \
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000060 } while (0)
61
niklase@google.com470e71d2011-07-07 08:21:25 +000062namespace webrtc {
Michael Graczyk86c6d332015-07-23 11:41:39 -070063namespace {
64
65static bool LayoutHasKeyboard(AudioProcessing::ChannelLayout layout) {
66 switch (layout) {
67 case AudioProcessing::kMono:
68 case AudioProcessing::kStereo:
69 return false;
70 case AudioProcessing::kMonoAndKeyboard:
71 case AudioProcessing::kStereoAndKeyboard:
72 return true;
73 }
74
75 assert(false);
76 return false;
77}
78
79} // namespace
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000080
81// Throughout webrtc, it's assumed that success is represented by zero.
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000082static_assert(AudioProcessing::kNoError == 0, "kNoError must be zero");
andrew@webrtc.org60730cf2014-01-07 17:45:09 +000083
pbos@webrtc.org788acd12014-12-15 09:41:24 +000084// This class has two main functionalities:
85//
86// 1) It is returned instead of the real GainControl after the new AGC has been
87// enabled in order to prevent an outside user from overriding compression
88// settings. It doesn't do anything in its implementation, except for
89// delegating the const methods and Enable calls to the real GainControl, so
90// AGC can still be disabled.
91//
92// 2) It is injected into AgcManagerDirect and implements volume callbacks for
93// getting and setting the volume level. It just caches this value to be used
94// in VoiceEngine later.
95class GainControlForNewAgc : public GainControl, public VolumeCallbacks {
96 public:
97 explicit GainControlForNewAgc(GainControlImpl* gain_control)
Michael Graczyk86c6d332015-07-23 11:41:39 -070098 : real_gain_control_(gain_control), volume_(0) {}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000099
100 // GainControl implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000101 int Enable(bool enable) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000102 return real_gain_control_->Enable(enable);
103 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000104 bool is_enabled() const override { return real_gain_control_->is_enabled(); }
105 int set_stream_analog_level(int level) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000106 volume_ = level;
107 return AudioProcessing::kNoError;
108 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000109 int stream_analog_level() override { return volume_; }
110 int set_mode(Mode mode) override { return AudioProcessing::kNoError; }
111 Mode mode() const override { return GainControl::kAdaptiveAnalog; }
112 int set_target_level_dbfs(int level) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000113 return AudioProcessing::kNoError;
114 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000115 int target_level_dbfs() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000116 return real_gain_control_->target_level_dbfs();
117 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000118 int set_compression_gain_db(int gain) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000119 return AudioProcessing::kNoError;
120 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000121 int compression_gain_db() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000122 return real_gain_control_->compression_gain_db();
123 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000124 int enable_limiter(bool enable) override { return AudioProcessing::kNoError; }
125 bool is_limiter_enabled() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000126 return real_gain_control_->is_limiter_enabled();
127 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000128 int set_analog_level_limits(int minimum, int maximum) override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000129 return AudioProcessing::kNoError;
130 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000131 int analog_level_minimum() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000132 return real_gain_control_->analog_level_minimum();
133 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000134 int analog_level_maximum() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000135 return real_gain_control_->analog_level_maximum();
136 }
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000137 bool stream_is_saturated() const override {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000138 return real_gain_control_->stream_is_saturated();
139 }
140
141 // VolumeCallbacks implementation.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000142 void SetMicVolume(int volume) override { volume_ = volume; }
143 int GetMicVolume() override { return volume_; }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000144
145 private:
146 GainControl* real_gain_control_;
147 int volume_;
148};
149
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700150const int AudioProcessing::kNativeSampleRatesHz[] = {
151 AudioProcessing::kSampleRate8kHz,
152 AudioProcessing::kSampleRate16kHz,
153 AudioProcessing::kSampleRate32kHz,
154 AudioProcessing::kSampleRate48kHz};
155const size_t AudioProcessing::kNumNativeSampleRates =
156 arraysize(AudioProcessing::kNativeSampleRatesHz);
157const int AudioProcessing::kMaxNativeSampleRateHz = AudioProcessing::
158 kNativeSampleRatesHz[AudioProcessing::kNumNativeSampleRates - 1];
159const int AudioProcessing::kMaxAECMSampleRateHz = kSampleRate16kHz;
160
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000161AudioProcessing* AudioProcessing::Create() {
162 Config config;
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000163 return Create(config, nullptr);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000164}
165
166AudioProcessing* AudioProcessing::Create(const Config& config) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000167 return Create(config, nullptr);
168}
169
170AudioProcessing* AudioProcessing::Create(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700171 Beamformer<float>* beamformer) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000172 AudioProcessingImpl* apm = new AudioProcessingImpl(config, beamformer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000173 if (apm->Initialize() != kNoError) {
174 delete apm;
175 apm = NULL;
176 }
177
178 return apm;
179}
180
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000181AudioProcessingImpl::AudioProcessingImpl(const Config& config)
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000182 : AudioProcessingImpl(config, nullptr) {}
183
184AudioProcessingImpl::AudioProcessingImpl(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700185 Beamformer<float>* beamformer)
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000186 : echo_cancellation_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 echo_control_mobile_(NULL),
188 gain_control_(NULL),
189 high_pass_filter_(NULL),
190 level_estimator_(NULL),
191 noise_suppression_(NULL),
192 voice_detection_(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000193 crit_(CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000194#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
195 debug_file_(FileWrapper::Create()),
196 event_msg_(new audioproc::Event()),
197#endif
Michael Graczyk86c6d332015-07-23 11:41:39 -0700198 api_format_({{{kSampleRate16kHz, 1, false},
199 {kSampleRate16kHz, 1, false},
ekmeyerson60d9b332015-08-14 10:35:55 -0700200 {kSampleRate16kHz, 1, false},
Michael Graczyk86c6d332015-07-23 11:41:39 -0700201 {kSampleRate16kHz, 1, false}}}),
aluebs@webrtc.org27d106b2014-12-11 17:09:21 +0000202 fwd_proc_format_(kSampleRate16kHz),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000203 rev_proc_format_(kSampleRate16kHz, 1),
204 split_rate_(kSampleRate16kHz),
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 stream_delay_ms_(0),
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000206 delay_offset_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 was_stream_delay_set_(false),
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200208 last_stream_delay_ms_(0),
209 last_aec_system_delay_ms_(0),
Bjorn Volcker4e7aa432015-07-07 11:50:05 +0200210 stream_delay_jumps_(-1),
211 aec_system_delay_jumps_(-1),
andrew@webrtc.org38bf2492014-02-13 17:43:44 +0000212 output_will_be_muted_(false),
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000213 key_pressed_(false),
214#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
215 use_new_agc_(false),
216#else
217 use_new_agc_(config.Get<ExperimentalAgc>().enabled),
218#endif
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200219 agc_startup_min_volume_(config.Get<ExperimentalAgc>().startup_min_volume),
andrew1c7075f2015-06-24 18:14:14 -0700220#if defined(WEBRTC_ANDROID) || defined(WEBRTC_IOS)
221 transient_suppressor_enabled_(false),
222#else
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000223 transient_suppressor_enabled_(config.Get<ExperimentalNs>().enabled),
andrew1c7075f2015-06-24 18:14:14 -0700224#endif
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000225 beamformer_enabled_(config.Get<Beamforming>().enabled),
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000226 beamformer_(beamformer),
ekmeyerson60d9b332015-08-14 10:35:55 -0700227 array_geometry_(config.Get<Beamforming>().array_geometry),
228 intelligibility_enabled_(config.Get<Intelligibility>().enabled) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000229 echo_cancellation_ = new EchoCancellationImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 component_list_.push_back(echo_cancellation_);
231
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000232 echo_control_mobile_ = new EchoControlMobileImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000233 component_list_.push_back(echo_control_mobile_);
234
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000235 gain_control_ = new GainControlImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000236 component_list_.push_back(gain_control_);
237
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000238 high_pass_filter_ = new HighPassFilterImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 component_list_.push_back(high_pass_filter_);
240
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000241 level_estimator_ = new LevelEstimatorImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 component_list_.push_back(level_estimator_);
243
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000244 noise_suppression_ = new NoiseSuppressionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000245 component_list_.push_back(noise_suppression_);
246
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000247 voice_detection_ = new VoiceDetectionImpl(this, crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000248 component_list_.push_back(voice_detection_);
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000249
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000250 gain_control_for_new_agc_.reset(new GainControlForNewAgc(gain_control_));
251
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000252 SetExtraOptions(config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000253}
254
255AudioProcessingImpl::~AudioProcessingImpl() {
andrew@webrtc.org81865342012-10-27 00:28:27 +0000256 {
257 CriticalSectionScoped crit_scoped(crit_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000258 // Depends on gain_control_ and gain_control_for_new_agc_.
259 agc_manager_.reset();
260 // Depends on gain_control_.
261 gain_control_for_new_agc_.reset();
andrew@webrtc.org81865342012-10-27 00:28:27 +0000262 while (!component_list_.empty()) {
263 ProcessingComponent* component = component_list_.front();
264 component->Destroy();
265 delete component;
266 component_list_.pop_front();
267 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000268
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000269#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.org81865342012-10-27 00:28:27 +0000270 if (debug_file_->Open()) {
271 debug_file_->CloseFile();
272 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000273#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000274 }
andrew@webrtc.org16cfbe22012-08-29 16:58:25 +0000275 delete crit_;
276 crit_ = NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000277}
278
niklase@google.com470e71d2011-07-07 08:21:25 +0000279int AudioProcessingImpl::Initialize() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000280 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000281 return InitializeLocked();
282}
283
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000284int AudioProcessingImpl::Initialize(int input_sample_rate_hz,
285 int output_sample_rate_hz,
286 int reverse_sample_rate_hz,
287 ChannelLayout input_layout,
288 ChannelLayout output_layout,
289 ChannelLayout reverse_layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700290 const ProcessingConfig processing_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700291 {{input_sample_rate_hz,
292 ChannelsFromLayout(input_layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -0700293 LayoutHasKeyboard(input_layout)},
ekmeyerson60d9b332015-08-14 10:35:55 -0700294 {output_sample_rate_hz,
295 ChannelsFromLayout(output_layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -0700296 LayoutHasKeyboard(output_layout)},
ekmeyerson60d9b332015-08-14 10:35:55 -0700297 {reverse_sample_rate_hz,
298 ChannelsFromLayout(reverse_layout),
299 LayoutHasKeyboard(reverse_layout)},
300 {reverse_sample_rate_hz,
301 ChannelsFromLayout(reverse_layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -0700302 LayoutHasKeyboard(reverse_layout)}}};
303
304 return Initialize(processing_config);
305}
306
307int AudioProcessingImpl::Initialize(const ProcessingConfig& processing_config) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000308 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700309 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000310}
311
niklase@google.com470e71d2011-07-07 08:21:25 +0000312int AudioProcessingImpl::InitializeLocked() {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700313 const int fwd_audio_buffer_channels =
314 beamformer_enabled_ ? api_format_.input_stream().num_channels()
315 : api_format_.output_stream().num_channels();
ekmeyerson60d9b332015-08-14 10:35:55 -0700316 const int rev_audio_buffer_out_num_frames =
317 api_format_.reverse_output_stream().num_frames() == 0
318 ? rev_proc_format_.num_frames()
319 : api_format_.reverse_output_stream().num_frames();
320 if (api_format_.reverse_input_stream().num_channels() > 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700321 render_audio_.reset(new AudioBuffer(
ekmeyerson60d9b332015-08-14 10:35:55 -0700322 api_format_.reverse_input_stream().num_frames(),
323 api_format_.reverse_input_stream().num_channels(),
Michael Graczyk86c6d332015-07-23 11:41:39 -0700324 rev_proc_format_.num_frames(), rev_proc_format_.num_channels(),
ekmeyerson60d9b332015-08-14 10:35:55 -0700325 rev_audio_buffer_out_num_frames));
326 if (rev_conversion_needed()) {
327 render_converter_ = AudioConverter::Create(
328 api_format_.reverse_input_stream().num_channels(),
329 api_format_.reverse_input_stream().num_frames(),
330 api_format_.reverse_output_stream().num_channels(),
331 api_format_.reverse_output_stream().num_frames());
332 } else {
333 render_converter_.reset(nullptr);
334 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700335 } else {
336 render_audio_.reset(nullptr);
ekmeyerson60d9b332015-08-14 10:35:55 -0700337 render_converter_.reset(nullptr);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700338 }
339 capture_audio_.reset(new AudioBuffer(
340 api_format_.input_stream().num_frames(),
341 api_format_.input_stream().num_channels(), fwd_proc_format_.num_frames(),
342 fwd_audio_buffer_channels, api_format_.output_stream().num_frames()));
niklase@google.com470e71d2011-07-07 08:21:25 +0000343
niklase@google.com470e71d2011-07-07 08:21:25 +0000344 // Initialize all components.
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000345 for (auto item : component_list_) {
346 int err = item->Initialize();
niklase@google.com470e71d2011-07-07 08:21:25 +0000347 if (err != kNoError) {
348 return err;
349 }
350 }
351
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200352 InitializeExperimentalAgc();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000353
Bjorn Volckeradc46c42015-04-15 11:42:40 +0200354 InitializeTransient();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000355
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000356 InitializeBeamformer();
357
ekmeyerson60d9b332015-08-14 10:35:55 -0700358 InitializeIntelligibility();
359
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000360#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +0000361 if (debug_file_->Open()) {
362 int err = WriteInitMessage();
363 if (err != kNoError) {
364 return err;
365 }
366 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000367#endif
ajm@google.com808e0e02011-08-03 21:08:51 +0000368
niklase@google.com470e71d2011-07-07 08:21:25 +0000369 return kNoError;
370}
371
Michael Graczyk86c6d332015-07-23 11:41:39 -0700372int AudioProcessingImpl::InitializeLocked(const ProcessingConfig& config) {
373 for (const auto& stream : config.streams) {
374 if (stream.num_channels() < 0) {
375 return kBadNumberChannelsError;
376 }
377 if (stream.num_channels() > 0 && stream.sample_rate_hz() <= 0) {
378 return kBadSampleRateError;
379 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000380 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700381
382 const int num_in_channels = config.input_stream().num_channels();
383 const int num_out_channels = config.output_stream().num_channels();
384
385 // Need at least one input channel.
386 // Need either one output channel or as many outputs as there are inputs.
387 if (num_in_channels == 0 ||
388 !(num_out_channels == 1 || num_out_channels == num_in_channels)) {
Michael Graczykc2047542015-07-22 21:06:11 -0700389 return kBadNumberChannelsError;
390 }
391
Michael Graczyk86c6d332015-07-23 11:41:39 -0700392 if (beamformer_enabled_ &&
393 (static_cast<size_t>(num_in_channels) != array_geometry_.size() ||
394 num_out_channels > 1)) {
395 return kBadNumberChannelsError;
396 }
397
398 api_format_ = config;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000399
400 // We process at the closest native rate >= min(input rate, output rate)...
Michael Graczyk86c6d332015-07-23 11:41:39 -0700401 const int min_proc_rate =
402 std::min(api_format_.input_stream().sample_rate_hz(),
403 api_format_.output_stream().sample_rate_hz());
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000404 int fwd_proc_rate;
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700405 for (size_t i = 0; i < kNumNativeSampleRates; ++i) {
406 fwd_proc_rate = kNativeSampleRatesHz[i];
407 if (fwd_proc_rate >= min_proc_rate) {
408 break;
409 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000410 }
411 // ...with one exception.
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700412 if (echo_control_mobile_->is_enabled() &&
413 min_proc_rate > kMaxAECMSampleRateHz) {
414 fwd_proc_rate = kMaxAECMSampleRateHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000415 }
416
Michael Graczyk86c6d332015-07-23 11:41:39 -0700417 fwd_proc_format_ = StreamConfig(fwd_proc_rate);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000418
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000419 // We normally process the reverse stream at 16 kHz. Unless...
420 int rev_proc_rate = kSampleRate16kHz;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700421 if (fwd_proc_format_.sample_rate_hz() == kSampleRate8kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000422 // ...the forward stream is at 8 kHz.
423 rev_proc_rate = kSampleRate8kHz;
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000424 } else {
ekmeyerson60d9b332015-08-14 10:35:55 -0700425 if (api_format_.reverse_input_stream().sample_rate_hz() ==
426 kSampleRate32kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000427 // ...or the input is at 32 kHz, in which case we use the splitting
428 // filter rather than the resampler.
429 rev_proc_rate = kSampleRate32kHz;
430 }
431 }
432
andrew@webrtc.org30be8272014-09-24 20:06:23 +0000433 // Always downmix the reverse stream to mono for analysis. This has been
434 // demonstrated to work well for AEC in most practical scenarios.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700435 rev_proc_format_ = StreamConfig(rev_proc_rate, 1);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000436
Michael Graczyk86c6d332015-07-23 11:41:39 -0700437 if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
438 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000439 split_rate_ = kSampleRate16kHz;
440 } else {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700441 split_rate_ = fwd_proc_format_.sample_rate_hz();
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000442 }
443
444 return InitializeLocked();
445}
446
447// Calls InitializeLocked() if any of the audio parameters have changed from
448// their current values.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700449int AudioProcessingImpl::MaybeInitializeLocked(
450 const ProcessingConfig& processing_config) {
451 if (processing_config == api_format_) {
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000452 return kNoError;
453 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700454 return InitializeLocked(processing_config);
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000455}
456
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000457void AudioProcessingImpl::SetExtraOptions(const Config& config) {
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000458 CriticalSectionScoped crit_scoped(crit_);
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +0000459 for (auto item : component_list_) {
460 item->SetExtraOptions(config);
461 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000462
463 if (transient_suppressor_enabled_ != config.Get<ExperimentalNs>().enabled) {
464 transient_suppressor_enabled_ = config.Get<ExperimentalNs>().enabled;
465 InitializeTransient();
466 }
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000467}
468
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000469
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000470int AudioProcessingImpl::proc_sample_rate_hz() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700471 return fwd_proc_format_.sample_rate_hz();
niklase@google.com470e71d2011-07-07 08:21:25 +0000472}
473
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000474int AudioProcessingImpl::proc_split_sample_rate_hz() const {
475 return split_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000476}
477
478int AudioProcessingImpl::num_reverse_channels() const {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000479 return rev_proc_format_.num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000480}
481
482int AudioProcessingImpl::num_input_channels() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700483 return api_format_.input_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000484}
485
486int AudioProcessingImpl::num_output_channels() const {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700487 return api_format_.output_stream().num_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000488}
489
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000490void AudioProcessingImpl::set_output_will_be_muted(bool muted) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000491 CriticalSectionScoped lock(crit_);
Bjorn Volcker424694c2015-03-27 11:30:43 +0100492 output_will_be_muted_ = muted;
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000493 if (agc_manager_.get()) {
494 agc_manager_->SetCaptureMuted(output_will_be_muted_);
495 }
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000496}
497
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000498
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000499int AudioProcessingImpl::ProcessStream(const float* const* src,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700500 size_t samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000501 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000502 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000503 int output_sample_rate_hz,
504 ChannelLayout output_layout,
505 float* const* dest) {
Michael Graczyk4bc66fc2015-08-10 15:26:38 -0700506 CriticalSectionScoped crit_scoped(crit_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700507 StreamConfig input_stream = api_format_.input_stream();
508 input_stream.set_sample_rate_hz(input_sample_rate_hz);
509 input_stream.set_num_channels(ChannelsFromLayout(input_layout));
510 input_stream.set_has_keyboard(LayoutHasKeyboard(input_layout));
511
512 StreamConfig output_stream = api_format_.output_stream();
513 output_stream.set_sample_rate_hz(output_sample_rate_hz);
514 output_stream.set_num_channels(ChannelsFromLayout(output_layout));
515 output_stream.set_has_keyboard(LayoutHasKeyboard(output_layout));
516
517 if (samples_per_channel != input_stream.num_frames()) {
518 return kBadDataLengthError;
519 }
520 return ProcessStream(src, input_stream, output_stream, dest);
521}
522
523int AudioProcessingImpl::ProcessStream(const float* const* src,
524 const StreamConfig& input_config,
525 const StreamConfig& output_config,
526 float* const* dest) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000527 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000528 if (!src || !dest) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000529 return kNullPointerError;
530 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000531
Michael Graczyk86c6d332015-07-23 11:41:39 -0700532 ProcessingConfig processing_config = api_format_;
533 processing_config.input_stream() = input_config;
534 processing_config.output_stream() = output_config;
535
536 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
537 assert(processing_config.input_stream().num_frames() ==
538 api_format_.input_stream().num_frames());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000539
540#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
541 if (debug_file_->Open()) {
542 event_msg_->set_type(audioproc::Event::STREAM);
543 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000544 const size_t channel_size =
Michael Graczyk86c6d332015-07-23 11:41:39 -0700545 sizeof(float) * api_format_.input_stream().num_frames();
546 for (int i = 0; i < api_format_.input_stream().num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000547 msg->add_input_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000548 }
549#endif
550
Michael Graczyk86c6d332015-07-23 11:41:39 -0700551 capture_audio_->CopyFrom(src, api_format_.input_stream());
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000552 RETURN_ON_ERR(ProcessStreamLocked());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700553 capture_audio_->CopyTo(api_format_.output_stream(), dest);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000554
555#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
556 if (debug_file_->Open()) {
557 audioproc::Stream* msg = event_msg_->mutable_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000558 const size_t channel_size =
Michael Graczyk86c6d332015-07-23 11:41:39 -0700559 sizeof(float) * api_format_.output_stream().num_frames();
560 for (int i = 0; i < api_format_.output_stream().num_channels(); ++i)
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000561 msg->add_output_channel(dest[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000562 RETURN_ON_ERR(WriteMessageToDebugFile());
563 }
564#endif
565
566 return kNoError;
567}
568
569int AudioProcessingImpl::ProcessStream(AudioFrame* frame) {
570 CriticalSectionScoped crit_scoped(crit_);
571 if (!frame) {
572 return kNullPointerError;
573 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000574 // Must be a native rate.
575 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
576 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000577 frame->sample_rate_hz_ != kSampleRate32kHz &&
578 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000579 return kBadSampleRateError;
580 }
581 if (echo_control_mobile_->is_enabled() &&
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700582 frame->sample_rate_hz_ > kMaxAECMSampleRateHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000583 LOG(LS_ERROR) << "AECM only supports 16 or 8 kHz sample rates";
584 return kUnsupportedComponentError;
585 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000586
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000587 // TODO(ajm): The input and output rates and channels are currently
588 // constrained to be identical in the int16 interface.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700589 ProcessingConfig processing_config = api_format_;
590 processing_config.input_stream().set_sample_rate_hz(frame->sample_rate_hz_);
591 processing_config.input_stream().set_num_channels(frame->num_channels_);
592 processing_config.output_stream().set_sample_rate_hz(frame->sample_rate_hz_);
593 processing_config.output_stream().set_num_channels(frame->num_channels_);
594
595 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
596 if (frame->samples_per_channel_ != api_format_.input_stream().num_frames()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000597 return kBadDataLengthError;
598 }
599
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000600#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000601 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000602 event_msg_->set_type(audioproc::Event::STREAM);
603 audioproc::Stream* msg = event_msg_->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700604 const size_t data_size =
605 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000606 msg->set_input_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000607 }
608#endif
609
610 capture_audio_->DeinterleaveFrom(frame);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000611 RETURN_ON_ERR(ProcessStreamLocked());
612 capture_audio_->InterleaveTo(frame, output_copy_needed(is_data_processed()));
613
614#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
615 if (debug_file_->Open()) {
616 audioproc::Stream* msg = event_msg_->mutable_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700617 const size_t data_size =
618 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000619 msg->set_output_data(frame->data_, data_size);
620 RETURN_ON_ERR(WriteMessageToDebugFile());
621 }
622#endif
623
624 return kNoError;
625}
626
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000627int AudioProcessingImpl::ProcessStreamLocked() {
628#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
629 if (debug_file_->Open()) {
630 audioproc::Stream* msg = event_msg_->mutable_stream();
ajm@google.com808e0e02011-08-03 21:08:51 +0000631 msg->set_delay(stream_delay_ms_);
632 msg->set_drift(echo_cancellation_->stream_drift_samples());
bjornv@webrtc.org63da1dd2015-02-06 19:44:21 +0000633 msg->set_level(gain_control()->stream_analog_level());
andrew@webrtc.orgce8e0772014-02-12 15:28:30 +0000634 msg->set_keypress(key_pressed_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000635 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000636#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000637
Bjorn Volcker1ca324f2015-06-29 14:57:29 +0200638 MaybeUpdateHistograms();
639
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000640 AudioBuffer* ca = capture_audio_.get(); // For brevity.
ekmeyerson60d9b332015-08-14 10:35:55 -0700641
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000642 if (use_new_agc_ && gain_control_->is_enabled()) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700643 agc_manager_->AnalyzePreProcess(ca->channels()[0], ca->num_channels(),
644 fwd_proc_format_.num_frames());
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000645 }
646
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000647 bool data_processed = is_data_processed();
648 if (analysis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000649 ca->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000650 }
651
ekmeyerson60d9b332015-08-14 10:35:55 -0700652 if (intelligibility_enabled_) {
653 intelligibility_enhancer_->AnalyzeCaptureAudio(
654 ca->split_channels_f(kBand0To8kHz), split_rate_, ca->num_channels());
655 }
656
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000657 if (beamformer_enabled_) {
Michael Graczykdfa36052015-03-25 16:37:27 -0700658 beamformer_->ProcessChunk(*ca->split_data_f(), ca->split_data_f());
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000659 ca->set_num_channels(1);
660 }
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000661
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000662 RETURN_ON_ERR(high_pass_filter_->ProcessCaptureAudio(ca));
663 RETURN_ON_ERR(gain_control_->AnalyzeCaptureAudio(ca));
aluebs@webrtc.orga0ce9fa2014-09-24 14:18:03 +0000664 RETURN_ON_ERR(noise_suppression_->AnalyzeCaptureAudio(ca));
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000665 RETURN_ON_ERR(echo_cancellation_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000666
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000667 if (echo_control_mobile_->is_enabled() && noise_suppression_->is_enabled()) {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000668 ca->CopyLowPassToReference();
niklase@google.com470e71d2011-07-07 08:21:25 +0000669 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000670 RETURN_ON_ERR(noise_suppression_->ProcessCaptureAudio(ca));
671 RETURN_ON_ERR(echo_control_mobile_->ProcessCaptureAudio(ca));
672 RETURN_ON_ERR(voice_detection_->ProcessCaptureAudio(ca));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000673
Michael Graczyk86c6d332015-07-23 11:41:39 -0700674 if (use_new_agc_ && gain_control_->is_enabled() &&
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000675 (!beamformer_enabled_ || beamformer_->is_target_present())) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000676 agc_manager_->Process(ca->split_bands_const(0)[kBand0To8kHz],
Michael Graczyk86c6d332015-07-23 11:41:39 -0700677 ca->num_frames_per_band(), split_rate_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000678 }
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000679 RETURN_ON_ERR(gain_control_->ProcessCaptureAudio(ca));
niklase@google.com470e71d2011-07-07 08:21:25 +0000680
andrew@webrtc.org369166a2012-04-24 18:38:03 +0000681 if (synthesis_needed(data_processed)) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000682 ca->MergeFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000683 }
684
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000685 // TODO(aluebs): Investigate if the transient suppression placement should be
686 // before or after the AGC.
687 if (transient_suppressor_enabled_) {
688 float voice_probability =
689 agc_manager_.get() ? agc_manager_->voice_probability() : 1.f;
690
Michael Graczyk86c6d332015-07-23 11:41:39 -0700691 transient_suppressor_->Suppress(
692 ca->channels_f()[0], ca->num_frames(), ca->num_channels(),
693 ca->split_bands_const_f(0)[kBand0To8kHz], ca->num_frames_per_band(),
694 ca->keyboard_data(), ca->num_keyboard_frames(), voice_probability,
695 key_pressed_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000696 }
697
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000698 // The level estimator operates on the recombined data.
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000699 RETURN_ON_ERR(level_estimator_->ProcessStream(ca));
ajm@google.com808e0e02011-08-03 21:08:51 +0000700
andrew@webrtc.org1e916932011-11-29 18:28:57 +0000701 was_stream_delay_set_ = false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000702 return kNoError;
703}
704
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000705int AudioProcessingImpl::AnalyzeReverseStream(const float* const* data,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700706 size_t samples_per_channel,
ekmeyerson60d9b332015-08-14 10:35:55 -0700707 int rev_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000708 ChannelLayout layout) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700709 const StreamConfig reverse_config = {
ekmeyerson60d9b332015-08-14 10:35:55 -0700710 rev_sample_rate_hz, ChannelsFromLayout(layout), LayoutHasKeyboard(layout),
Michael Graczyk86c6d332015-07-23 11:41:39 -0700711 };
712 if (samples_per_channel != reverse_config.num_frames()) {
713 return kBadDataLengthError;
714 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700715 return AnalyzeReverseStream(data, reverse_config, reverse_config);
716}
717
718int AudioProcessingImpl::ProcessReverseStream(
719 const float* const* src,
720 const StreamConfig& reverse_input_config,
721 const StreamConfig& reverse_output_config,
722 float* const* dest) {
723 RETURN_ON_ERR(
724 AnalyzeReverseStream(src, reverse_input_config, reverse_output_config));
725 if (is_rev_processed()) {
726 render_audio_->CopyTo(api_format_.reverse_output_stream(), dest);
727 } else if (rev_conversion_needed()) {
728 render_converter_->Convert(src, reverse_input_config.num_samples(), dest,
729 reverse_output_config.num_samples());
730 } else {
731 CopyAudioIfNeeded(src, reverse_input_config.num_frames(),
732 reverse_input_config.num_channels(), dest);
733 }
734
735 return kNoError;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700736}
737
738int AudioProcessingImpl::AnalyzeReverseStream(
ekmeyerson60d9b332015-08-14 10:35:55 -0700739 const float* const* src,
740 const StreamConfig& reverse_input_config,
741 const StreamConfig& reverse_output_config) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000742 CriticalSectionScoped crit_scoped(crit_);
ekmeyerson60d9b332015-08-14 10:35:55 -0700743 if (src == NULL) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000744 return kNullPointerError;
745 }
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000746
ekmeyerson60d9b332015-08-14 10:35:55 -0700747 if (reverse_input_config.num_channels() <= 0) {
Michael Graczyk86c6d332015-07-23 11:41:39 -0700748 return kBadNumberChannelsError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000749 }
750
Michael Graczyk86c6d332015-07-23 11:41:39 -0700751 ProcessingConfig processing_config = api_format_;
ekmeyerson60d9b332015-08-14 10:35:55 -0700752 processing_config.reverse_input_stream() = reverse_input_config;
753 processing_config.reverse_output_stream() = reverse_output_config;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700754
755 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
ekmeyerson60d9b332015-08-14 10:35:55 -0700756 assert(reverse_input_config.num_frames() ==
757 api_format_.reverse_input_stream().num_frames());
Michael Graczyk86c6d332015-07-23 11:41:39 -0700758
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000759#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
760 if (debug_file_->Open()) {
761 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
762 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
aluebs@webrtc.org59a1b1b2014-08-28 10:43:09 +0000763 const size_t channel_size =
ekmeyerson60d9b332015-08-14 10:35:55 -0700764 sizeof(float) * api_format_.reverse_input_stream().num_frames();
765 for (int i = 0; i < api_format_.reverse_input_stream().num_channels(); ++i)
766 msg->add_channel(src[i], channel_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000767 RETURN_ON_ERR(WriteMessageToDebugFile());
768 }
769#endif
770
ekmeyerson60d9b332015-08-14 10:35:55 -0700771 render_audio_->CopyFrom(src, api_format_.reverse_input_stream());
772 return ProcessReverseStreamLocked();
773}
774
775int AudioProcessingImpl::ProcessReverseStream(AudioFrame* frame) {
776 RETURN_ON_ERR(AnalyzeReverseStream(frame));
777 if (is_rev_processed()) {
778 render_audio_->InterleaveTo(frame, true);
779 }
780
781 return kNoError;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000782}
783
niklase@google.com470e71d2011-07-07 08:21:25 +0000784int AudioProcessingImpl::AnalyzeReverseStream(AudioFrame* frame) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000785 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000786 if (frame == NULL) {
787 return kNullPointerError;
788 }
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000789 // Must be a native rate.
790 if (frame->sample_rate_hz_ != kSampleRate8kHz &&
791 frame->sample_rate_hz_ != kSampleRate16kHz &&
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000792 frame->sample_rate_hz_ != kSampleRate32kHz &&
793 frame->sample_rate_hz_ != kSampleRate48kHz) {
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000794 return kBadSampleRateError;
795 }
796 // This interface does not tolerate different forward and reverse rates.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700797 if (frame->sample_rate_hz_ != api_format_.input_stream().sample_rate_hz()) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000798 return kBadSampleRateError;
799 }
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000800
Michael Graczyk86c6d332015-07-23 11:41:39 -0700801 if (frame->num_channels_ <= 0) {
802 return kBadNumberChannelsError;
803 }
804
805 ProcessingConfig processing_config = api_format_;
ekmeyerson60d9b332015-08-14 10:35:55 -0700806 processing_config.reverse_input_stream().set_sample_rate_hz(
807 frame->sample_rate_hz_);
808 processing_config.reverse_input_stream().set_num_channels(
809 frame->num_channels_);
810 processing_config.reverse_output_stream().set_sample_rate_hz(
811 frame->sample_rate_hz_);
812 processing_config.reverse_output_stream().set_num_channels(
813 frame->num_channels_);
Michael Graczyk86c6d332015-07-23 11:41:39 -0700814
815 RETURN_ON_ERR(MaybeInitializeLocked(processing_config));
816 if (frame->samples_per_channel_ !=
ekmeyerson60d9b332015-08-14 10:35:55 -0700817 api_format_.reverse_input_stream().num_frames()) {
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000818 return kBadDataLengthError;
819 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000820
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000821#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000822 if (debug_file_->Open()) {
ajm@google.com808e0e02011-08-03 21:08:51 +0000823 event_msg_->set_type(audioproc::Event::REVERSE_STREAM);
824 audioproc::ReverseStream* msg = event_msg_->mutable_reverse_stream();
Michael Graczyk86c6d332015-07-23 11:41:39 -0700825 const size_t data_size =
826 sizeof(int16_t) * frame->samples_per_channel_ * frame->num_channels_;
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000827 msg->set_data(frame->data_, data_size);
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000828 RETURN_ON_ERR(WriteMessageToDebugFile());
niklase@google.com470e71d2011-07-07 08:21:25 +0000829 }
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000830#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000831 render_audio_->DeinterleaveFrom(frame);
ekmeyerson60d9b332015-08-14 10:35:55 -0700832 return ProcessReverseStreamLocked();
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000833}
niklase@google.com470e71d2011-07-07 08:21:25 +0000834
ekmeyerson60d9b332015-08-14 10:35:55 -0700835int AudioProcessingImpl::ProcessReverseStreamLocked() {
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000836 AudioBuffer* ra = render_audio_.get(); // For brevity.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700837 if (rev_proc_format_.sample_rate_hz() == kSampleRate32kHz) {
aluebs@webrtc.orgbe05c742014-11-14 22:18:10 +0000838 ra->SplitIntoFrequencyBands();
niklase@google.com470e71d2011-07-07 08:21:25 +0000839 }
840
ekmeyerson60d9b332015-08-14 10:35:55 -0700841 if (intelligibility_enabled_) {
842 intelligibility_enhancer_->ProcessRenderAudio(
843 ra->split_channels_f(kBand0To8kHz), split_rate_, ra->num_channels());
844 }
845
andrew@webrtc.org103657b2014-04-24 18:28:56 +0000846 RETURN_ON_ERR(echo_cancellation_->ProcessRenderAudio(ra));
847 RETURN_ON_ERR(echo_control_mobile_->ProcessRenderAudio(ra));
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000848 if (!use_new_agc_) {
849 RETURN_ON_ERR(gain_control_->ProcessRenderAudio(ra));
850 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000851
ekmeyerson60d9b332015-08-14 10:35:55 -0700852 if (rev_proc_format_.sample_rate_hz() == kSampleRate32kHz &&
853 is_rev_processed()) {
854 ra->MergeFrequencyBands();
855 }
856
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000857 return kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000858}
859
860int AudioProcessingImpl::set_stream_delay_ms(int delay) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000861 Error retval = kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000862 was_stream_delay_set_ = true;
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000863 delay += delay_offset_ms_;
864
niklase@google.com470e71d2011-07-07 08:21:25 +0000865 if (delay < 0) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000866 delay = 0;
867 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000868 }
869
870 // TODO(ajm): the max is rather arbitrarily chosen; investigate.
871 if (delay > 500) {
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000872 delay = 500;
873 retval = kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +0000874 }
875
876 stream_delay_ms_ = delay;
andrew@webrtc.org5f23d642012-05-29 21:14:06 +0000877 return retval;
niklase@google.com470e71d2011-07-07 08:21:25 +0000878}
879
880int AudioProcessingImpl::stream_delay_ms() const {
881 return stream_delay_ms_;
882}
883
884bool AudioProcessingImpl::was_stream_delay_set() const {
885 return was_stream_delay_set_;
886}
887
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000888void AudioProcessingImpl::set_stream_key_pressed(bool key_pressed) {
889 key_pressed_ = key_pressed;
890}
891
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000892void AudioProcessingImpl::set_delay_offset_ms(int offset) {
893 CriticalSectionScoped crit_scoped(crit_);
894 delay_offset_ms_ = offset;
895}
896
897int AudioProcessingImpl::delay_offset_ms() const {
898 return delay_offset_ms_;
899}
900
niklase@google.com470e71d2011-07-07 08:21:25 +0000901int AudioProcessingImpl::StartDebugRecording(
902 const char filename[AudioProcessing::kMaxFilenameSize]) {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000903 CriticalSectionScoped crit_scoped(crit_);
André Susano Pinto664cdaf2015-05-20 11:11:07 +0200904 static_assert(kMaxFilenameSize == FileWrapper::kMaxFileNameSize, "");
niklase@google.com470e71d2011-07-07 08:21:25 +0000905
906 if (filename == NULL) {
907 return kNullPointerError;
908 }
909
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000910#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000911 // Stop any ongoing recording.
912 if (debug_file_->Open()) {
913 if (debug_file_->CloseFile() == -1) {
914 return kFileError;
915 }
916 }
917
918 if (debug_file_->OpenFile(filename, false) == -1) {
919 debug_file_->CloseFile();
920 return kFileError;
921 }
922
ajm@google.com808e0e02011-08-03 21:08:51 +0000923 int err = WriteInitMessage();
924 if (err != kNoError) {
925 return err;
niklase@google.com470e71d2011-07-07 08:21:25 +0000926 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000927 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000928#else
929 return kUnsupportedFunctionError;
930#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000931}
932
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000933int AudioProcessingImpl::StartDebugRecording(FILE* handle) {
934 CriticalSectionScoped crit_scoped(crit_);
935
936 if (handle == NULL) {
937 return kNullPointerError;
938 }
939
940#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
941 // Stop any ongoing recording.
942 if (debug_file_->Open()) {
943 if (debug_file_->CloseFile() == -1) {
944 return kFileError;
945 }
946 }
947
948 if (debug_file_->OpenFromFileHandle(handle, true, false) == -1) {
949 return kFileError;
950 }
951
952 int err = WriteInitMessage();
953 if (err != kNoError) {
954 return err;
955 }
956 return kNoError;
957#else
958 return kUnsupportedFunctionError;
959#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
960}
961
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000962int AudioProcessingImpl::StartDebugRecordingForPlatformFile(
963 rtc::PlatformFile handle) {
964 FILE* stream = rtc::FdopenPlatformFileForWriting(handle);
965 return StartDebugRecording(stream);
966}
967
niklase@google.com470e71d2011-07-07 08:21:25 +0000968int AudioProcessingImpl::StopDebugRecording() {
andrew@webrtc.org40654032012-01-30 20:51:15 +0000969 CriticalSectionScoped crit_scoped(crit_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000970
971#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000972 // We just return if recording hasn't started.
973 if (debug_file_->Open()) {
974 if (debug_file_->CloseFile() == -1) {
975 return kFileError;
976 }
977 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000978 return kNoError;
andrew@webrtc.org7bf26462011-12-03 00:03:31 +0000979#else
980 return kUnsupportedFunctionError;
981#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +0000982}
983
984EchoCancellation* AudioProcessingImpl::echo_cancellation() const {
985 return echo_cancellation_;
986}
987
988EchoControlMobile* AudioProcessingImpl::echo_control_mobile() const {
989 return echo_control_mobile_;
990}
991
992GainControl* AudioProcessingImpl::gain_control() const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000993 if (use_new_agc_) {
994 return gain_control_for_new_agc_.get();
995 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000996 return gain_control_;
997}
998
999HighPassFilter* AudioProcessingImpl::high_pass_filter() const {
1000 return high_pass_filter_;
1001}
1002
1003LevelEstimator* AudioProcessingImpl::level_estimator() const {
1004 return level_estimator_;
1005}
1006
1007NoiseSuppression* AudioProcessingImpl::noise_suppression() const {
1008 return noise_suppression_;
1009}
1010
1011VoiceDetection* AudioProcessingImpl::voice_detection() const {
1012 return voice_detection_;
1013}
1014
andrew@webrtc.org369166a2012-04-24 18:38:03 +00001015bool AudioProcessingImpl::is_data_processed() const {
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001016 if (beamformer_enabled_) {
1017 return true;
1018 }
1019
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001020 int enabled_count = 0;
mgraczyk@chromium.orge5340862015-03-12 23:23:38 +00001021 for (auto item : component_list_) {
1022 if (item->is_component_enabled()) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001023 enabled_count++;
1024 }
1025 }
1026
1027 // Data is unchanged if no components are enabled, or if only level_estimator_
1028 // or voice_detection_ is enabled.
1029 if (enabled_count == 0) {
1030 return false;
1031 } else if (enabled_count == 1) {
1032 if (level_estimator_->is_enabled() || voice_detection_->is_enabled()) {
1033 return false;
1034 }
1035 } else if (enabled_count == 2) {
1036 if (level_estimator_->is_enabled() && voice_detection_->is_enabled()) {
1037 return false;
1038 }
1039 }
1040 return true;
1041}
1042
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001043bool AudioProcessingImpl::output_copy_needed(bool is_data_processed) const {
andrew@webrtc.org369166a2012-04-24 18:38:03 +00001044 // Check if we've upmixed or downmixed the audio.
Michael Graczyk86c6d332015-07-23 11:41:39 -07001045 return ((api_format_.output_stream().num_channels() !=
1046 api_format_.input_stream().num_channels()) ||
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001047 is_data_processed || transient_suppressor_enabled_);
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001048}
1049
andrew@webrtc.org369166a2012-04-24 18:38:03 +00001050bool AudioProcessingImpl::synthesis_needed(bool is_data_processed) const {
Michael Graczyk86c6d332015-07-23 11:41:39 -07001051 return (is_data_processed &&
1052 (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
1053 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz));
andrew@webrtc.org369166a2012-04-24 18:38:03 +00001054}
1055
1056bool AudioProcessingImpl::analysis_needed(bool is_data_processed) const {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001057 if (!is_data_processed && !voice_detection_->is_enabled() &&
1058 !transient_suppressor_enabled_) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001059 // Only level_estimator_ is enabled.
1060 return false;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001061 } else if (fwd_proc_format_.sample_rate_hz() == kSampleRate32kHz ||
1062 fwd_proc_format_.sample_rate_hz() == kSampleRate48kHz) {
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001063 // Something besides level_estimator_ is enabled, and we have super-wb.
1064 return true;
1065 }
1066 return false;
1067}
1068
ekmeyerson60d9b332015-08-14 10:35:55 -07001069bool AudioProcessingImpl::is_rev_processed() const {
1070 return intelligibility_enabled_ && intelligibility_enhancer_->active();
1071}
1072
1073bool AudioProcessingImpl::rev_conversion_needed() const {
1074 return (api_format_.reverse_input_stream() !=
1075 api_format_.reverse_output_stream());
1076}
1077
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001078void AudioProcessingImpl::InitializeExperimentalAgc() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001079 if (use_new_agc_) {
1080 if (!agc_manager_.get()) {
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001081 agc_manager_.reset(new AgcManagerDirect(gain_control_,
1082 gain_control_for_new_agc_.get(),
1083 agc_startup_min_volume_));
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001084 }
1085 agc_manager_->Initialize();
1086 agc_manager_->SetCaptureMuted(output_will_be_muted_);
1087 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001088}
1089
Bjorn Volckeradc46c42015-04-15 11:42:40 +02001090void AudioProcessingImpl::InitializeTransient() {
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001091 if (transient_suppressor_enabled_) {
1092 if (!transient_suppressor_.get()) {
1093 transient_suppressor_.reset(new TransientSuppressor());
1094 }
Michael Graczyk86c6d332015-07-23 11:41:39 -07001095 transient_suppressor_->Initialize(
1096 fwd_proc_format_.sample_rate_hz(), split_rate_,
1097 api_format_.output_stream().num_channels());
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001098 }
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001099}
1100
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001101void AudioProcessingImpl::InitializeBeamformer() {
1102 if (beamformer_enabled_) {
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001103 if (!beamformer_) {
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +00001104 beamformer_.reset(new NonlinearBeamformer(array_geometry_));
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +00001105 }
1106 beamformer_->Initialize(kChunkSizeMs, split_rate_);
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +00001107 }
1108}
1109
ekmeyerson60d9b332015-08-14 10:35:55 -07001110void AudioProcessingImpl::InitializeIntelligibility() {
1111 if (intelligibility_enabled_) {
1112 IntelligibilityEnhancer::Config config;
1113 config.sample_rate_hz = split_rate_;
1114 config.num_capture_channels = capture_audio_->num_channels();
1115 config.num_render_channels = render_audio_->num_channels();
1116 intelligibility_enhancer_.reset(new IntelligibilityEnhancer(config));
1117 }
1118}
1119
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001120void AudioProcessingImpl::MaybeUpdateHistograms() {
Bjorn Volckerd92f2672015-07-05 10:46:01 +02001121 static const int kMinDiffDelayMs = 60;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001122
1123 if (echo_cancellation()->is_enabled()) {
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001124 // Activate delay_jumps_ counters if we know echo_cancellation is runnning.
1125 // If a stream has echo we know that the echo_cancellation is in process.
1126 if (stream_delay_jumps_ == -1 && echo_cancellation()->stream_has_echo()) {
1127 stream_delay_jumps_ = 0;
1128 }
1129 if (aec_system_delay_jumps_ == -1 &&
1130 echo_cancellation()->stream_has_echo()) {
1131 aec_system_delay_jumps_ = 0;
1132 }
1133
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001134 // Detect a jump in platform reported system delay and log the difference.
1135 const int diff_stream_delay_ms = stream_delay_ms_ - last_stream_delay_ms_;
1136 if (diff_stream_delay_ms > kMinDiffDelayMs && last_stream_delay_ms_ != 0) {
1137 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.PlatformReportedStreamDelayJump",
1138 diff_stream_delay_ms, kMinDiffDelayMs, 1000, 100);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001139 if (stream_delay_jumps_ == -1) {
1140 stream_delay_jumps_ = 0; // Activate counter if needed.
1141 }
1142 stream_delay_jumps_++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001143 }
1144 last_stream_delay_ms_ = stream_delay_ms_;
1145
1146 // Detect a jump in AEC system delay and log the difference.
1147 const int frames_per_ms = rtc::CheckedDivExact(split_rate_, 1000);
1148 const int aec_system_delay_ms =
1149 WebRtcAec_system_delay(echo_cancellation()->aec_core()) / frames_per_ms;
Michael Graczyk86c6d332015-07-23 11:41:39 -07001150 const int diff_aec_system_delay_ms =
1151 aec_system_delay_ms - last_aec_system_delay_ms_;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001152 if (diff_aec_system_delay_ms > kMinDiffDelayMs &&
1153 last_aec_system_delay_ms_ != 0) {
1154 RTC_HISTOGRAM_COUNTS("WebRTC.Audio.AecSystemDelayJump",
1155 diff_aec_system_delay_ms, kMinDiffDelayMs, 1000,
1156 100);
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001157 if (aec_system_delay_jumps_ == -1) {
1158 aec_system_delay_jumps_ = 0; // Activate counter if needed.
1159 }
1160 aec_system_delay_jumps_++;
Bjorn Volcker1ca324f2015-06-29 14:57:29 +02001161 }
1162 last_aec_system_delay_ms_ = aec_system_delay_ms;
1163 }
1164}
1165
Bjorn Volcker4e7aa432015-07-07 11:50:05 +02001166void AudioProcessingImpl::UpdateHistogramsOnCallEnd() {
1167 CriticalSectionScoped crit_scoped(crit_);
1168 if (stream_delay_jumps_ > -1) {
1169 RTC_HISTOGRAM_ENUMERATION(
1170 "WebRTC.Audio.NumOfPlatformReportedStreamDelayJumps",
1171 stream_delay_jumps_, 51);
1172 }
1173 stream_delay_jumps_ = -1;
1174 last_stream_delay_ms_ = 0;
1175
1176 if (aec_system_delay_jumps_ > -1) {
1177 RTC_HISTOGRAM_ENUMERATION("WebRTC.Audio.NumOfAecSystemDelayJumps",
1178 aec_system_delay_jumps_, 51);
1179 }
1180 aec_system_delay_jumps_ = -1;
1181 last_aec_system_delay_ms_ = 0;
1182}
1183
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001184#ifdef WEBRTC_AUDIOPROC_DEBUG_DUMP
ajm@google.com808e0e02011-08-03 21:08:51 +00001185int AudioProcessingImpl::WriteMessageToDebugFile() {
1186 int32_t size = event_msg_->ByteSize();
1187 if (size <= 0) {
1188 return kUnspecifiedError;
1189 }
andrew@webrtc.org621df672013-10-22 10:27:23 +00001190#if defined(WEBRTC_ARCH_BIG_ENDIAN)
Michael Graczyk86c6d332015-07-23 11:41:39 -07001191// TODO(ajm): Use little-endian "on the wire". For the moment, we can be
1192// pretty safe in assuming little-endian.
ajm@google.com808e0e02011-08-03 21:08:51 +00001193#endif
1194
1195 if (!event_msg_->SerializeToString(&event_str_)) {
1196 return kUnspecifiedError;
1197 }
1198
1199 // Write message preceded by its size.
1200 if (!debug_file_->Write(&size, sizeof(int32_t))) {
1201 return kFileError;
1202 }
1203 if (!debug_file_->Write(event_str_.data(), event_str_.length())) {
1204 return kFileError;
1205 }
1206
1207 event_msg_->Clear();
1208
andrew@webrtc.org17e40642014-03-04 20:58:13 +00001209 return kNoError;
ajm@google.com808e0e02011-08-03 21:08:51 +00001210}
1211
1212int AudioProcessingImpl::WriteInitMessage() {
1213 event_msg_->set_type(audioproc::Event::INIT);
1214 audioproc::Init* msg = event_msg_->mutable_init();
Michael Graczyk86c6d332015-07-23 11:41:39 -07001215 msg->set_sample_rate(api_format_.input_stream().sample_rate_hz());
1216 msg->set_num_input_channels(api_format_.input_stream().num_channels());
1217 msg->set_num_output_channels(api_format_.output_stream().num_channels());
ekmeyerson60d9b332015-08-14 10:35:55 -07001218 msg->set_num_reverse_channels(
1219 api_format_.reverse_input_stream().num_channels());
1220 msg->set_reverse_sample_rate(
1221 api_format_.reverse_input_stream().sample_rate_hz());
Michael Graczyk86c6d332015-07-23 11:41:39 -07001222 msg->set_output_sample_rate(api_format_.output_stream().sample_rate_hz());
ekmeyerson60d9b332015-08-14 10:35:55 -07001223 // TODO(ekmeyerson): Add reverse output fields to event_msg_.
ajm@google.com808e0e02011-08-03 21:08:51 +00001224
1225 int err = WriteMessageToDebugFile();
1226 if (err != kNoError) {
1227 return err;
1228 }
1229
1230 return kNoError;
1231}
andrew@webrtc.org7bf26462011-12-03 00:03:31 +00001232#endif // WEBRTC_AUDIOPROC_DEBUG_DUMP
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +00001233
niklase@google.com470e71d2011-07-07 08:21:25 +00001234} // namespace webrtc