blob: 0de5e620ceb41dbea2bc1f50f47c8bda640cd69b [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"
Henrik Kjellander98f53512015-10-28 18:17:40 +010021#include "webrtc/system_wrappers/include/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)
Henrik Lundin441f6342015-06-09 16:03:13 +020060 : ProcessingComponent(),
61 apm_(apm),
62 crit_(crit),
63 drift_compensation_enabled_(false),
64 metrics_enabled_(false),
65 suppression_level_(kModerateSuppression),
66 stream_drift_samples_(0),
67 was_stream_drift_set_(false),
68 stream_has_echo_(false),
69 delay_logging_enabled_(false),
70 extended_filter_enabled_(false),
henrik.lundin0f133b92015-07-02 00:17:55 -070071 delay_agnostic_enabled_(false) {
Henrik Lundin441f6342015-06-09 16:03:13 +020072}
niklase@google.com470e71d2011-07-07 08:21:25 +000073
74EchoCancellationImpl::~EchoCancellationImpl() {}
75
76int EchoCancellationImpl::ProcessRenderAudio(const AudioBuffer* audio) {
77 if (!is_component_enabled()) {
78 return apm_->kNoError;
79 }
80
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000081 assert(audio->num_frames_per_band() <= 160);
niklase@google.com470e71d2011-07-07 08:21:25 +000082 assert(audio->num_channels() == apm_->num_reverse_channels());
83
84 int err = apm_->kNoError;
85
86 // The ordering convention must be followed to pass to the correct AEC.
87 size_t handle_index = 0;
88 for (int i = 0; i < apm_->num_output_channels(); i++) {
89 for (int j = 0; j < audio->num_channels(); j++) {
90 Handle* my_handle = static_cast<Handle*>(handle(handle_index));
91 err = WebRtcAec_BufferFarend(
92 my_handle,
aluebs@webrtc.orgc5ebbd92014-12-10 19:30:57 +000093 audio->split_bands_const_f(j)[kBand0To8kHz],
Peter Kastingdce40cf2015-08-24 14:52:23 -070094 audio->num_frames_per_band());
niklase@google.com470e71d2011-07-07 08:21:25 +000095
96 if (err != apm_->kNoError) {
peahc12be392015-11-09 23:53:50 -080097 return MapError(err); // TODO(ajm): warning possible?
niklase@google.com470e71d2011-07-07 08:21:25 +000098 }
99
100 handle_index++;
101 }
102 }
103
104 return apm_->kNoError;
105}
106
107int EchoCancellationImpl::ProcessCaptureAudio(AudioBuffer* audio) {
108 if (!is_component_enabled()) {
109 return apm_->kNoError;
110 }
111
112 if (!apm_->was_stream_delay_set()) {
113 return apm_->kStreamParameterNotSetError;
114 }
115
116 if (drift_compensation_enabled_ && !was_stream_drift_set_) {
117 return apm_->kStreamParameterNotSetError;
118 }
119
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +0000120 assert(audio->num_frames_per_band() <= 160);
niklase@google.com470e71d2011-07-07 08:21:25 +0000121 assert(audio->num_channels() == apm_->num_output_channels());
122
123 int err = apm_->kNoError;
124
125 // The ordering convention must be followed to pass to the correct AEC.
126 size_t handle_index = 0;
127 stream_has_echo_ = false;
128 for (int i = 0; i < audio->num_channels(); i++) {
129 for (int j = 0; j < apm_->num_reverse_channels(); j++) {
130 Handle* my_handle = handle(handle_index);
131 err = WebRtcAec_Process(
132 my_handle,
aluebs@webrtc.orgc78d81a2015-01-21 19:10:55 +0000133 audio->split_bands_const_f(i),
134 audio->num_bands(),
135 audio->split_bands_f(i),
Peter Kastingdce40cf2015-08-24 14:52:23 -0700136 audio->num_frames_per_band(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000137 apm_->stream_delay_ms(),
138 stream_drift_samples_);
139
140 if (err != apm_->kNoError) {
peahc12be392015-11-09 23:53:50 -0800141 err = MapError(err);
niklase@google.com470e71d2011-07-07 08:21:25 +0000142 // 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) {
peahc12be392015-11-09 23:53:50 -0800151 return MapError(err);
niklase@google.com470e71d2011-07-07 08:21:25 +0000152 }
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
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000205void EchoCancellationImpl::set_stream_drift_samples(int drift) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 was_stream_drift_set_ = true;
207 stream_drift_samples_ = drift;
niklase@google.com470e71d2011-07-07 08:21:25 +0000208}
209
210int EchoCancellationImpl::stream_drift_samples() const {
211 return stream_drift_samples_;
212}
213
214int EchoCancellationImpl::enable_metrics(bool enable) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000215 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 metrics_enabled_ = enable;
217 return Configure();
218}
219
220bool EchoCancellationImpl::are_metrics_enabled() const {
221 return metrics_enabled_;
222}
223
224// TODO(ajm): we currently just use the metrics from the first AEC. Think more
225// aboue the best way to extend this to multi-channel.
226int EchoCancellationImpl::GetMetrics(Metrics* metrics) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000227 CriticalSectionScoped crit_scoped(crit_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000228 if (metrics == NULL) {
229 return apm_->kNullPointerError;
230 }
231
232 if (!is_component_enabled() || !metrics_enabled_) {
233 return apm_->kNotEnabledError;
234 }
235
236 AecMetrics my_metrics;
237 memset(&my_metrics, 0, sizeof(my_metrics));
238 memset(metrics, 0, sizeof(Metrics));
239
240 Handle* my_handle = static_cast<Handle*>(handle(0));
241 int err = WebRtcAec_GetMetrics(my_handle, &my_metrics);
242 if (err != apm_->kNoError) {
peahc12be392015-11-09 23:53:50 -0800243 return MapError(err);
niklase@google.com470e71d2011-07-07 08:21:25 +0000244 }
245
246 metrics->residual_echo_return_loss.instant = my_metrics.rerl.instant;
247 metrics->residual_echo_return_loss.average = my_metrics.rerl.average;
248 metrics->residual_echo_return_loss.maximum = my_metrics.rerl.max;
249 metrics->residual_echo_return_loss.minimum = my_metrics.rerl.min;
250
251 metrics->echo_return_loss.instant = my_metrics.erl.instant;
252 metrics->echo_return_loss.average = my_metrics.erl.average;
253 metrics->echo_return_loss.maximum = my_metrics.erl.max;
254 metrics->echo_return_loss.minimum = my_metrics.erl.min;
255
256 metrics->echo_return_loss_enhancement.instant = my_metrics.erle.instant;
257 metrics->echo_return_loss_enhancement.average = my_metrics.erle.average;
258 metrics->echo_return_loss_enhancement.maximum = my_metrics.erle.max;
259 metrics->echo_return_loss_enhancement.minimum = my_metrics.erle.min;
260
261 metrics->a_nlp.instant = my_metrics.aNlp.instant;
262 metrics->a_nlp.average = my_metrics.aNlp.average;
263 metrics->a_nlp.maximum = my_metrics.aNlp.max;
264 metrics->a_nlp.minimum = my_metrics.aNlp.min;
265
266 return apm_->kNoError;
267}
268
269bool EchoCancellationImpl::stream_has_echo() const {
270 return stream_has_echo_;
271}
272
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000273int EchoCancellationImpl::enable_delay_logging(bool enable) {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000274 CriticalSectionScoped crit_scoped(crit_);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000275 delay_logging_enabled_ = enable;
276 return Configure();
277}
278
279bool EchoCancellationImpl::is_delay_logging_enabled() const {
280 return delay_logging_enabled_;
281}
282
Minyue13b96ba2015-10-03 00:39:14 +0200283bool EchoCancellationImpl::is_delay_agnostic_enabled() const {
284 return delay_agnostic_enabled_;
285}
286
287bool EchoCancellationImpl::is_extended_filter_enabled() const {
288 return extended_filter_enabled_;
289}
290
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000291// TODO(bjornv): How should we handle the multi-channel case?
292int EchoCancellationImpl::GetDelayMetrics(int* median, int* std) {
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000293 float fraction_poor_delays = 0;
294 return GetDelayMetrics(median, std, &fraction_poor_delays);
295}
296
297int EchoCancellationImpl::GetDelayMetrics(int* median, int* std,
298 float* fraction_poor_delays) {
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));
peahc12be392015-11-09 23:53:50 -0800312 const int err =
313 WebRtcAec_GetDelayMetrics(my_handle, median, std, fraction_poor_delays);
314 if (err != apm_->kNoError) {
315 return MapError(err);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000316 }
317
318 return apm_->kNoError;
319}
320
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000321struct AecCore* EchoCancellationImpl::aec_core() const {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000322 CriticalSectionScoped crit_scoped(crit_);
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000323 if (!is_component_enabled()) {
324 return NULL;
325 }
326 Handle* my_handle = static_cast<Handle*>(handle(0));
327 return WebRtcAec_aec_core(my_handle);
328}
329
niklase@google.com470e71d2011-07-07 08:21:25 +0000330int EchoCancellationImpl::Initialize() {
331 int err = ProcessingComponent::Initialize();
332 if (err != apm_->kNoError || !is_component_enabled()) {
333 return err;
334 }
335
niklase@google.com470e71d2011-07-07 08:21:25 +0000336 return apm_->kNoError;
337}
338
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000339void EchoCancellationImpl::SetExtraOptions(const Config& config) {
Henrik Lundinb02af182015-06-16 09:53:23 +0200340 extended_filter_enabled_ = config.Get<ExtendedFilter>().enabled;
henrik.lundin366e9522015-07-03 00:50:05 -0700341 delay_agnostic_enabled_ = config.Get<DelayAgnostic>().enabled;
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000342 Configure();
343}
344
niklase@google.com470e71d2011-07-07 08:21:25 +0000345void* EchoCancellationImpl::CreateHandle() const {
Bjorn Volcker9345e862015-06-10 21:43:36 +0200346 return WebRtcAec_Create();
niklase@google.com470e71d2011-07-07 08:21:25 +0000347}
348
bjornv@webrtc.org5964fe02014-04-22 06:52:28 +0000349void EchoCancellationImpl::DestroyHandle(void* handle) const {
niklase@google.com470e71d2011-07-07 08:21:25 +0000350 assert(handle != NULL);
bjornv@webrtc.org5964fe02014-04-22 06:52:28 +0000351 WebRtcAec_Free(static_cast<Handle*>(handle));
niklase@google.com470e71d2011-07-07 08:21:25 +0000352}
353
354int EchoCancellationImpl::InitializeHandle(void* handle) const {
355 assert(handle != NULL);
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000356 // TODO(ajm): Drift compensation is disabled in practice. If restored, it
357 // should be managed internally and not depend on the hardware sample rate.
358 // For now, just hardcode a 48 kHz value.
niklase@google.com470e71d2011-07-07 08:21:25 +0000359 return WebRtcAec_Init(static_cast<Handle*>(handle),
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000360 apm_->proc_sample_rate_hz(),
361 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +0000362}
363
364int EchoCancellationImpl::ConfigureHandle(void* handle) const {
365 assert(handle != NULL);
366 AecConfig config;
367 config.metricsMode = metrics_enabled_;
368 config.nlpMode = MapSetting(suppression_level_);
369 config.skewMode = drift_compensation_enabled_;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000370 config.delay_logging = delay_logging_enabled_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000371
Henrik Lundin441f6342015-06-09 16:03:13 +0200372 WebRtcAec_enable_extended_filter(
373 WebRtcAec_aec_core(static_cast<Handle*>(handle)),
374 extended_filter_enabled_ ? 1 : 0);
henrik.lundin0f133b92015-07-02 00:17:55 -0700375 WebRtcAec_enable_delay_agnostic(
376 WebRtcAec_aec_core(static_cast<Handle*>(handle)),
377 delay_agnostic_enabled_ ? 1 : 0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000378 return WebRtcAec_set_config(static_cast<Handle*>(handle), config);
379}
380
381int EchoCancellationImpl::num_handles_required() const {
382 return apm_->num_output_channels() *
383 apm_->num_reverse_channels();
384}
385
386int EchoCancellationImpl::GetHandleError(void* handle) const {
387 assert(handle != NULL);
peahc12be392015-11-09 23:53:50 -0800388 return AudioProcessing::kUnspecifiedError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000389}
390} // namespace webrtc