2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
17 #include "webrtc/modules/audio_processing/aec/aec_core.h"
19 #include "webrtc/modules/audio_processing/aec/include/echo_cancellation.h"
20 #include "webrtc/modules/audio_processing/audio_buffer.h"
21 #include "webrtc/modules/audio_processing/audio_processing_impl.h"
22 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
29 int16_t MapSetting(EchoCancellation::SuppressionLevel level) {
31 case EchoCancellation::kLowSuppression:
32 return kAecNlpConservative;
33 case EchoCancellation::kModerateSuppression:
34 return kAecNlpModerate;
35 case EchoCancellation::kHighSuppression:
36 return kAecNlpAggressive;
42 AudioProcessing::Error MapError(int err) {
44 case AEC_UNSUPPORTED_FUNCTION_ERROR:
45 return AudioProcessing::kUnsupportedFunctionError;
46 case AEC_BAD_PARAMETER_ERROR:
47 return AudioProcessing::kBadParameterError;
48 case AEC_BAD_PARAMETER_WARNING:
49 return AudioProcessing::kBadStreamParameterWarning;
51 // AEC_UNSPECIFIED_ERROR
52 // AEC_UNINITIALIZED_ERROR
53 // AEC_NULL_POINTER_ERROR
54 return AudioProcessing::kUnspecifiedError;
59 EchoCancellationImplWrapper* EchoCancellationImplWrapper::Create(
60 const AudioProcessingImpl* audioproc) {
61 return new EchoCancellationImpl(audioproc);
64 EchoCancellationImpl::EchoCancellationImpl(const AudioProcessingImpl* apm)
65 : ProcessingComponent(apm),
67 drift_compensation_enabled_(false),
68 metrics_enabled_(false),
69 suppression_level_(kModerateSuppression),
70 device_sample_rate_hz_(48000),
71 stream_drift_samples_(0),
72 was_stream_drift_set_(false),
73 stream_has_echo_(false),
74 delay_logging_enabled_(false),
75 delay_correction_enabled_(false) {}
77 EchoCancellationImpl::~EchoCancellationImpl() {}
79 int EchoCancellationImpl::ProcessRenderAudio(const AudioBuffer* audio) {
80 if (!is_component_enabled()) {
81 return apm_->kNoError;
84 assert(audio->samples_per_split_channel() <= 160);
85 assert(audio->num_channels() == apm_->num_reverse_channels());
87 int err = apm_->kNoError;
89 // The ordering convention must be followed to pass to the correct AEC.
90 size_t handle_index = 0;
91 for (int i = 0; i < apm_->num_output_channels(); i++) {
92 for (int j = 0; j < audio->num_channels(); j++) {
93 Handle* my_handle = static_cast<Handle*>(handle(handle_index));
94 err = WebRtcAec_BufferFarend(
96 audio->low_pass_split_data(j),
97 static_cast<int16_t>(audio->samples_per_split_channel()));
99 if (err != apm_->kNoError) {
100 return GetHandleError(my_handle); // TODO(ajm): warning possible?
107 return apm_->kNoError;
110 int EchoCancellationImpl::ProcessCaptureAudio(AudioBuffer* audio) {
111 if (!is_component_enabled()) {
112 return apm_->kNoError;
115 if (!apm_->was_stream_delay_set()) {
116 return apm_->kStreamParameterNotSetError;
119 if (drift_compensation_enabled_ && !was_stream_drift_set_) {
120 return apm_->kStreamParameterNotSetError;
123 assert(audio->samples_per_split_channel() <= 160);
124 assert(audio->num_channels() == apm_->num_output_channels());
126 int err = apm_->kNoError;
128 // The ordering convention must be followed to pass to the correct AEC.
129 size_t handle_index = 0;
130 stream_has_echo_ = false;
131 for (int i = 0; i < audio->num_channels(); i++) {
132 for (int j = 0; j < apm_->num_reverse_channels(); j++) {
133 Handle* my_handle = handle(handle_index);
134 err = WebRtcAec_Process(
136 audio->low_pass_split_data(i),
137 audio->high_pass_split_data(i),
138 audio->low_pass_split_data(i),
139 audio->high_pass_split_data(i),
140 static_cast<int16_t>(audio->samples_per_split_channel()),
141 apm_->stream_delay_ms(),
142 stream_drift_samples_);
144 if (err != apm_->kNoError) {
145 err = GetHandleError(my_handle);
146 // TODO(ajm): Figure out how to return warnings properly.
147 if (err != apm_->kBadStreamParameterWarning) {
153 err = WebRtcAec_get_echo_status(my_handle, &status);
154 if (err != apm_->kNoError) {
155 return GetHandleError(my_handle);
159 stream_has_echo_ = true;
166 was_stream_drift_set_ = false;
167 return apm_->kNoError;
170 int EchoCancellationImpl::Enable(bool enable) {
171 CriticalSectionScoped crit_scoped(apm_->crit());
172 // Ensure AEC and AECM are not both enabled.
173 if (enable && apm_->echo_control_mobile()->is_enabled()) {
174 return apm_->kBadParameterError;
177 return EnableComponent(enable);
180 bool EchoCancellationImpl::is_enabled() const {
181 return is_component_enabled();
184 int EchoCancellationImpl::set_suppression_level(SuppressionLevel level) {
185 CriticalSectionScoped crit_scoped(apm_->crit());
186 if (MapSetting(level) == -1) {
187 return apm_->kBadParameterError;
190 suppression_level_ = level;
194 EchoCancellation::SuppressionLevel EchoCancellationImpl::suppression_level()
196 return suppression_level_;
199 int EchoCancellationImpl::enable_drift_compensation(bool enable) {
200 CriticalSectionScoped crit_scoped(apm_->crit());
201 drift_compensation_enabled_ = enable;
205 bool EchoCancellationImpl::is_drift_compensation_enabled() const {
206 return drift_compensation_enabled_;
209 int EchoCancellationImpl::set_device_sample_rate_hz(int rate) {
210 CriticalSectionScoped crit_scoped(apm_->crit());
211 if (rate < 8000 || rate > 96000) {
212 return apm_->kBadParameterError;
215 device_sample_rate_hz_ = rate;
219 int EchoCancellationImpl::device_sample_rate_hz() const {
220 return device_sample_rate_hz_;
223 void EchoCancellationImpl::set_stream_drift_samples(int drift) {
224 was_stream_drift_set_ = true;
225 stream_drift_samples_ = drift;
228 int EchoCancellationImpl::stream_drift_samples() const {
229 return stream_drift_samples_;
232 int EchoCancellationImpl::enable_metrics(bool enable) {
233 CriticalSectionScoped crit_scoped(apm_->crit());
234 metrics_enabled_ = enable;
238 bool EchoCancellationImpl::are_metrics_enabled() const {
239 return metrics_enabled_;
242 // TODO(ajm): we currently just use the metrics from the first AEC. Think more
243 // aboue the best way to extend this to multi-channel.
244 int EchoCancellationImpl::GetMetrics(Metrics* metrics) {
245 CriticalSectionScoped crit_scoped(apm_->crit());
246 if (metrics == NULL) {
247 return apm_->kNullPointerError;
250 if (!is_component_enabled() || !metrics_enabled_) {
251 return apm_->kNotEnabledError;
254 AecMetrics my_metrics;
255 memset(&my_metrics, 0, sizeof(my_metrics));
256 memset(metrics, 0, sizeof(Metrics));
258 Handle* my_handle = static_cast<Handle*>(handle(0));
259 int err = WebRtcAec_GetMetrics(my_handle, &my_metrics);
260 if (err != apm_->kNoError) {
261 return GetHandleError(my_handle);
264 metrics->residual_echo_return_loss.instant = my_metrics.rerl.instant;
265 metrics->residual_echo_return_loss.average = my_metrics.rerl.average;
266 metrics->residual_echo_return_loss.maximum = my_metrics.rerl.max;
267 metrics->residual_echo_return_loss.minimum = my_metrics.rerl.min;
269 metrics->echo_return_loss.instant = my_metrics.erl.instant;
270 metrics->echo_return_loss.average = my_metrics.erl.average;
271 metrics->echo_return_loss.maximum = my_metrics.erl.max;
272 metrics->echo_return_loss.minimum = my_metrics.erl.min;
274 metrics->echo_return_loss_enhancement.instant = my_metrics.erle.instant;
275 metrics->echo_return_loss_enhancement.average = my_metrics.erle.average;
276 metrics->echo_return_loss_enhancement.maximum = my_metrics.erle.max;
277 metrics->echo_return_loss_enhancement.minimum = my_metrics.erle.min;
279 metrics->a_nlp.instant = my_metrics.aNlp.instant;
280 metrics->a_nlp.average = my_metrics.aNlp.average;
281 metrics->a_nlp.maximum = my_metrics.aNlp.max;
282 metrics->a_nlp.minimum = my_metrics.aNlp.min;
284 return apm_->kNoError;
287 bool EchoCancellationImpl::stream_has_echo() const {
288 return stream_has_echo_;
291 int EchoCancellationImpl::enable_delay_logging(bool enable) {
292 CriticalSectionScoped crit_scoped(apm_->crit());
293 delay_logging_enabled_ = enable;
297 bool EchoCancellationImpl::is_delay_logging_enabled() const {
298 return delay_logging_enabled_;
301 // TODO(bjornv): How should we handle the multi-channel case?
302 int EchoCancellationImpl::GetDelayMetrics(int* median, int* std) {
303 CriticalSectionScoped crit_scoped(apm_->crit());
304 if (median == NULL) {
305 return apm_->kNullPointerError;
308 return apm_->kNullPointerError;
311 if (!is_component_enabled() || !delay_logging_enabled_) {
312 return apm_->kNotEnabledError;
315 Handle* my_handle = static_cast<Handle*>(handle(0));
316 if (WebRtcAec_GetDelayMetrics(my_handle, median, std) !=
318 return GetHandleError(my_handle);
321 return apm_->kNoError;
324 struct AecCore* EchoCancellationImpl::aec_core() const {
325 CriticalSectionScoped crit_scoped(apm_->crit());
326 if (!is_component_enabled()) {
329 Handle* my_handle = static_cast<Handle*>(handle(0));
330 return WebRtcAec_aec_core(my_handle);
333 int EchoCancellationImpl::Initialize() {
334 int err = ProcessingComponent::Initialize();
335 if (err != apm_->kNoError || !is_component_enabled()) {
339 was_stream_drift_set_ = false;
341 return apm_->kNoError;
344 void EchoCancellationImpl::SetExtraOptions(const Config& config) {
345 delay_correction_enabled_ = config.Get<DelayCorrection>().enabled;
349 void* EchoCancellationImpl::CreateHandle() const {
350 Handle* handle = NULL;
351 if (WebRtcAec_Create(&handle) != apm_->kNoError) {
354 assert(handle != NULL);
360 int EchoCancellationImpl::DestroyHandle(void* handle) const {
361 assert(handle != NULL);
362 return WebRtcAec_Free(static_cast<Handle*>(handle));
365 int EchoCancellationImpl::InitializeHandle(void* handle) const {
366 assert(handle != NULL);
367 return WebRtcAec_Init(static_cast<Handle*>(handle),
368 apm_->sample_rate_hz(),
369 device_sample_rate_hz_);
372 int EchoCancellationImpl::ConfigureHandle(void* handle) const {
373 assert(handle != NULL);
375 config.metricsMode = metrics_enabled_;
376 config.nlpMode = MapSetting(suppression_level_);
377 config.skewMode = drift_compensation_enabled_;
378 config.delay_logging = delay_logging_enabled_;
380 WebRtcAec_enable_delay_correction(WebRtcAec_aec_core(
381 static_cast<Handle*>(handle)), delay_correction_enabled_ ? 1 : 0);
382 return WebRtcAec_set_config(static_cast<Handle*>(handle), config);
385 int EchoCancellationImpl::num_handles_required() const {
386 return apm_->num_output_channels() *
387 apm_->num_reverse_channels();
390 int EchoCancellationImpl::GetHandleError(void* handle) const {
391 assert(handle != NULL);
392 return MapError(WebRtcAec_get_error_code(static_cast<Handle*>(handle)));
394 } // namespace webrtc