blob: 0d6d159b1730dbbb45331721fbd30b00e705b253 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
bjornv@webrtc.org0c6f9312012-01-30 09:39:08 +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
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +000011#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +000013#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include <string.h>
15
andrew@webrtc.org1760a172013-09-25 23:17:38 +000016extern "C" {
17#include "webrtc/modules/audio_processing/aec/aec_core.h"
18}
19#include "webrtc/modules/audio_processing/aec/include/echo_cancellation.h"
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +000020#include "webrtc/modules/audio_processing/audio_buffer.h"
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +000021#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
niklase@google.com470e71d2011-07-07 08:21:25 +000023namespace webrtc {
24
25typedef void Handle;
26
27namespace {
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000028int16_t MapSetting(EchoCancellation::SuppressionLevel level) {
niklase@google.com470e71d2011-07-07 08:21:25 +000029 switch (level) {
30 case EchoCancellation::kLowSuppression:
31 return kAecNlpConservative;
32 case EchoCancellation::kModerateSuppression:
33 return kAecNlpModerate;
34 case EchoCancellation::kHighSuppression:
35 return kAecNlpAggressive;
niklase@google.com470e71d2011-07-07 08:21:25 +000036 }
andrew@webrtc.org648af742012-02-08 01:57:29 +000037 assert(false);
mflodman@webrtc.org657b2a42012-02-06 11:06:01 +000038 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
40
andrew@webrtc.org648af742012-02-08 01:57:29 +000041AudioProcessing::Error MapError(int err) {
niklase@google.com470e71d2011-07-07 08:21:25 +000042 switch (err) {
43 case AEC_UNSUPPORTED_FUNCTION_ERROR:
44 return AudioProcessing::kUnsupportedFunctionError;
niklase@google.com470e71d2011-07-07 08:21:25 +000045 case AEC_BAD_PARAMETER_ERROR:
46 return AudioProcessing::kBadParameterError;
niklase@google.com470e71d2011-07-07 08:21:25 +000047 case AEC_BAD_PARAMETER_WARNING:
48 return AudioProcessing::kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +000049 default:
50 // AEC_UNSPECIFIED_ERROR
51 // AEC_UNINITIALIZED_ERROR
52 // AEC_NULL_POINTER_ERROR
53 return AudioProcessing::kUnspecifiedError;
54 }
55}
56} // namespace
57
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000058EchoCancellationImpl::EchoCancellationImpl(const AudioProcessing* apm,
59 CriticalSectionWrapper* crit)
60 : ProcessingComponent(),
niklase@google.com470e71d2011-07-07 08:21:25 +000061 apm_(apm),
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000062 crit_(crit),
niklase@google.com470e71d2011-07-07 08:21:25 +000063 drift_compensation_enabled_(false),
64 metrics_enabled_(false),
65 suppression_level_(kModerateSuppression),
66 device_sample_rate_hz_(48000),
67 stream_drift_samples_(0),
68 was_stream_drift_set_(false),
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +000069 stream_has_echo_(false),
andrew@webrtc.org1760a172013-09-25 23:17:38 +000070 delay_logging_enabled_(false),
71 delay_correction_enabled_(false) {}
niklase@google.com470e71d2011-07-07 08:21:25 +000072
73EchoCancellationImpl::~EchoCancellationImpl() {}
74
75int EchoCancellationImpl::ProcessRenderAudio(const AudioBuffer* audio) {
76 if (!is_component_enabled()) {
77 return apm_->kNoError;
78 }
79
80 assert(audio->samples_per_split_channel() <= 160);
81 assert(audio->num_channels() == apm_->num_reverse_channels());
82
83 int err = apm_->kNoError;
84
85 // The ordering convention must be followed to pass to the correct AEC.
86 size_t handle_index = 0;
87 for (int i = 0; i < apm_->num_output_channels(); i++) {
88 for (int j = 0; j < audio->num_channels(); j++) {
89 Handle* my_handle = static_cast<Handle*>(handle(handle_index));
90 err = WebRtcAec_BufferFarend(
91 my_handle,
92 audio->low_pass_split_data(j),
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000093 static_cast<int16_t>(audio->samples_per_split_channel()));
niklase@google.com470e71d2011-07-07 08:21:25 +000094
95 if (err != apm_->kNoError) {
96 return GetHandleError(my_handle); // TODO(ajm): warning possible?
97 }
98
99 handle_index++;
100 }
101 }
102
103 return apm_->kNoError;
104}
105
106int EchoCancellationImpl::ProcessCaptureAudio(AudioBuffer* audio) {
107 if (!is_component_enabled()) {
108 return apm_->kNoError;
109 }
110
111 if (!apm_->was_stream_delay_set()) {
112 return apm_->kStreamParameterNotSetError;
113 }
114
115 if (drift_compensation_enabled_ && !was_stream_drift_set_) {
116 return apm_->kStreamParameterNotSetError;
117 }
118
119 assert(audio->samples_per_split_channel() <= 160);
120 assert(audio->num_channels() == apm_->num_output_channels());
121
122 int err = apm_->kNoError;
123
124 // The ordering convention must be followed to pass to the correct AEC.
125 size_t handle_index = 0;
126 stream_has_echo_ = false;
127 for (int i = 0; i < audio->num_channels(); i++) {
128 for (int j = 0; j < apm_->num_reverse_channels(); j++) {
129 Handle* my_handle = handle(handle_index);
130 err = WebRtcAec_Process(
131 my_handle,
132 audio->low_pass_split_data(i),
133 audio->high_pass_split_data(i),
134 audio->low_pass_split_data(i),
135 audio->high_pass_split_data(i),
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000136 static_cast<int16_t>(audio->samples_per_split_channel()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000137 apm_->stream_delay_ms(),
138 stream_drift_samples_);
139
140 if (err != apm_->kNoError) {
141 err = GetHandleError(my_handle);
142 // TODO(ajm): Figure out how to return warnings properly.
143 if (err != apm_->kBadStreamParameterWarning) {
144 return err;
145 }
146 }
147
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000148 int status = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000149 err = WebRtcAec_get_echo_status(my_handle, &status);
150 if (err != apm_->kNoError) {
151 return GetHandleError(my_handle);
152 }
153
154 if (status == 1) {
155 stream_has_echo_ = true;
156 }
157
158 handle_index++;
159 }
160 }
161
162 was_stream_drift_set_ = false;
163 return apm_->kNoError;
164}
165
166int EchoCancellationImpl::Enable(bool enable) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000167 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168 // Ensure AEC and AECM are not both enabled.
169 if (enable && apm_->echo_control_mobile()->is_enabled()) {
170 return apm_->kBadParameterError;
171 }
172
173 return EnableComponent(enable);
174}
175
176bool EchoCancellationImpl::is_enabled() const {
177 return is_component_enabled();
178}
179
180int EchoCancellationImpl::set_suppression_level(SuppressionLevel level) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000181 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000182 if (MapSetting(level) == -1) {
183 return apm_->kBadParameterError;
184 }
185
186 suppression_level_ = level;
187 return Configure();
188}
189
190EchoCancellation::SuppressionLevel EchoCancellationImpl::suppression_level()
191 const {
192 return suppression_level_;
193}
194
195int EchoCancellationImpl::enable_drift_compensation(bool enable) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000196 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 drift_compensation_enabled_ = enable;
198 return Configure();
199}
200
201bool EchoCancellationImpl::is_drift_compensation_enabled() const {
202 return drift_compensation_enabled_;
203}
204
205int EchoCancellationImpl::set_device_sample_rate_hz(int rate) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000206 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 if (rate < 8000 || rate > 96000) {
208 return apm_->kBadParameterError;
209 }
210
211 device_sample_rate_hz_ = rate;
212 return Initialize();
213}
214
215int EchoCancellationImpl::device_sample_rate_hz() const {
216 return device_sample_rate_hz_;
217}
218
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000219void EchoCancellationImpl::set_stream_drift_samples(int drift) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000220 was_stream_drift_set_ = true;
221 stream_drift_samples_ = drift;
niklase@google.com470e71d2011-07-07 08:21:25 +0000222}
223
224int EchoCancellationImpl::stream_drift_samples() const {
225 return stream_drift_samples_;
226}
227
228int EchoCancellationImpl::enable_metrics(bool enable) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000229 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 metrics_enabled_ = enable;
231 return Configure();
232}
233
234bool EchoCancellationImpl::are_metrics_enabled() const {
235 return metrics_enabled_;
236}
237
238// TODO(ajm): we currently just use the metrics from the first AEC. Think more
239// aboue the best way to extend this to multi-channel.
240int EchoCancellationImpl::GetMetrics(Metrics* metrics) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000241 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000242 if (metrics == NULL) {
243 return apm_->kNullPointerError;
244 }
245
246 if (!is_component_enabled() || !metrics_enabled_) {
247 return apm_->kNotEnabledError;
248 }
249
250 AecMetrics my_metrics;
251 memset(&my_metrics, 0, sizeof(my_metrics));
252 memset(metrics, 0, sizeof(Metrics));
253
254 Handle* my_handle = static_cast<Handle*>(handle(0));
255 int err = WebRtcAec_GetMetrics(my_handle, &my_metrics);
256 if (err != apm_->kNoError) {
257 return GetHandleError(my_handle);
258 }
259
260 metrics->residual_echo_return_loss.instant = my_metrics.rerl.instant;
261 metrics->residual_echo_return_loss.average = my_metrics.rerl.average;
262 metrics->residual_echo_return_loss.maximum = my_metrics.rerl.max;
263 metrics->residual_echo_return_loss.minimum = my_metrics.rerl.min;
264
265 metrics->echo_return_loss.instant = my_metrics.erl.instant;
266 metrics->echo_return_loss.average = my_metrics.erl.average;
267 metrics->echo_return_loss.maximum = my_metrics.erl.max;
268 metrics->echo_return_loss.minimum = my_metrics.erl.min;
269
270 metrics->echo_return_loss_enhancement.instant = my_metrics.erle.instant;
271 metrics->echo_return_loss_enhancement.average = my_metrics.erle.average;
272 metrics->echo_return_loss_enhancement.maximum = my_metrics.erle.max;
273 metrics->echo_return_loss_enhancement.minimum = my_metrics.erle.min;
274
275 metrics->a_nlp.instant = my_metrics.aNlp.instant;
276 metrics->a_nlp.average = my_metrics.aNlp.average;
277 metrics->a_nlp.maximum = my_metrics.aNlp.max;
278 metrics->a_nlp.minimum = my_metrics.aNlp.min;
279
280 return apm_->kNoError;
281}
282
283bool EchoCancellationImpl::stream_has_echo() const {
284 return stream_has_echo_;
285}
286
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000287int EchoCancellationImpl::enable_delay_logging(bool enable) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000288 CriticalSectionScoped crit_scoped(crit_);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000289 delay_logging_enabled_ = enable;
290 return Configure();
291}
292
293bool EchoCancellationImpl::is_delay_logging_enabled() const {
294 return delay_logging_enabled_;
295}
296
297// TODO(bjornv): How should we handle the multi-channel case?
298int EchoCancellationImpl::GetDelayMetrics(int* median, int* std) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000299 CriticalSectionScoped crit_scoped(crit_);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000300 if (median == NULL) {
301 return apm_->kNullPointerError;
302 }
303 if (std == NULL) {
304 return apm_->kNullPointerError;
305 }
306
307 if (!is_component_enabled() || !delay_logging_enabled_) {
308 return apm_->kNotEnabledError;
309 }
310
311 Handle* my_handle = static_cast<Handle*>(handle(0));
312 if (WebRtcAec_GetDelayMetrics(my_handle, median, std) !=
313 apm_->kNoError) {
314 return GetHandleError(my_handle);
315 }
316
317 return apm_->kNoError;
318}
319
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000320struct AecCore* EchoCancellationImpl::aec_core() const {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000321 CriticalSectionScoped crit_scoped(crit_);
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000322 if (!is_component_enabled()) {
323 return NULL;
324 }
325 Handle* my_handle = static_cast<Handle*>(handle(0));
326 return WebRtcAec_aec_core(my_handle);
327}
328
niklase@google.com470e71d2011-07-07 08:21:25 +0000329int EchoCancellationImpl::Initialize() {
330 int err = ProcessingComponent::Initialize();
331 if (err != apm_->kNoError || !is_component_enabled()) {
332 return err;
333 }
334
niklase@google.com470e71d2011-07-07 08:21:25 +0000335 return apm_->kNoError;
336}
337
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000338void EchoCancellationImpl::SetExtraOptions(const Config& config) {
339 delay_correction_enabled_ = config.Get<DelayCorrection>().enabled;
340 Configure();
341}
342
niklase@google.com470e71d2011-07-07 08:21:25 +0000343void* EchoCancellationImpl::CreateHandle() const {
344 Handle* handle = NULL;
345 if (WebRtcAec_Create(&handle) != apm_->kNoError) {
346 handle = NULL;
347 } else {
348 assert(handle != NULL);
349 }
350
351 return handle;
352}
353
354int EchoCancellationImpl::DestroyHandle(void* handle) const {
355 assert(handle != NULL);
356 return WebRtcAec_Free(static_cast<Handle*>(handle));
357}
358
359int EchoCancellationImpl::InitializeHandle(void* handle) const {
360 assert(handle != NULL);
361 return WebRtcAec_Init(static_cast<Handle*>(handle),
362 apm_->sample_rate_hz(),
363 device_sample_rate_hz_);
364}
365
366int EchoCancellationImpl::ConfigureHandle(void* handle) const {
367 assert(handle != NULL);
368 AecConfig config;
369 config.metricsMode = metrics_enabled_;
370 config.nlpMode = MapSetting(suppression_level_);
371 config.skewMode = drift_compensation_enabled_;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000372 config.delay_logging = delay_logging_enabled_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000373
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000374 WebRtcAec_enable_delay_correction(WebRtcAec_aec_core(
375 static_cast<Handle*>(handle)), delay_correction_enabled_ ? 1 : 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000376 return WebRtcAec_set_config(static_cast<Handle*>(handle), config);
377}
378
379int EchoCancellationImpl::num_handles_required() const {
380 return apm_->num_output_channels() *
381 apm_->num_reverse_channels();
382}
383
384int EchoCancellationImpl::GetHandleError(void* handle) const {
385 assert(handle != NULL);
386 return MapError(WebRtcAec_get_error_code(static_cast<Handle*>(handle)));
387}
388} // namespace webrtc