andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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.org | dba34dd | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/echo_cancellation_impl.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 12 | |
bjornv@webrtc.org | dba34dd | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 13 | #include <assert.h> |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
andrew@webrtc.org | 8ddec2c | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 16 | extern "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.org | dba34dd | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 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" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 23 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 24 | namespace webrtc { |
| 25 | |
| 26 | typedef void Handle; |
| 27 | |
| 28 | namespace { |
pbos@webrtc.org | 3f6d5e0 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 29 | int16_t MapSetting(EchoCancellation::SuppressionLevel level) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 30 | switch (level) { |
| 31 | case EchoCancellation::kLowSuppression: |
| 32 | return kAecNlpConservative; |
| 33 | case EchoCancellation::kModerateSuppression: |
| 34 | return kAecNlpModerate; |
| 35 | case EchoCancellation::kHighSuppression: |
| 36 | return kAecNlpAggressive; |
| 37 | } |
| 38 | assert(false); |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | AudioProcessing::Error MapError(int err) { |
| 43 | switch (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; |
| 50 | default: |
| 51 | // AEC_UNSPECIFIED_ERROR |
| 52 | // AEC_UNINITIALIZED_ERROR |
| 53 | // AEC_NULL_POINTER_ERROR |
| 54 | return AudioProcessing::kUnspecifiedError; |
| 55 | } |
| 56 | } |
| 57 | } // namespace |
| 58 | |
andrew@webrtc.org | 25613ea | 2013-07-25 18:28:29 +0000 | [diff] [blame] | 59 | EchoCancellationImplWrapper* EchoCancellationImplWrapper::Create( |
| 60 | const AudioProcessingImpl* audioproc) { |
| 61 | return new EchoCancellationImpl(audioproc); |
| 62 | } |
| 63 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 64 | EchoCancellationImpl::EchoCancellationImpl(const AudioProcessingImpl* apm) |
| 65 | : ProcessingComponent(apm), |
| 66 | apm_(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), |
andrew@webrtc.org | 8ddec2c | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 74 | delay_logging_enabled_(false), |
| 75 | delay_correction_enabled_(false) {} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 76 | |
| 77 | EchoCancellationImpl::~EchoCancellationImpl() {} |
| 78 | |
| 79 | int EchoCancellationImpl::ProcessRenderAudio(const AudioBuffer* audio) { |
| 80 | if (!is_component_enabled()) { |
| 81 | return apm_->kNoError; |
| 82 | } |
| 83 | |
| 84 | assert(audio->samples_per_split_channel() <= 160); |
| 85 | assert(audio->num_channels() == apm_->num_reverse_channels()); |
| 86 | |
| 87 | int err = apm_->kNoError; |
| 88 | |
| 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( |
| 95 | my_handle, |
| 96 | audio->low_pass_split_data(j), |
pbos@webrtc.org | 3f6d5e0 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 97 | static_cast<int16_t>(audio->samples_per_split_channel())); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 98 | |
| 99 | if (err != apm_->kNoError) { |
| 100 | return GetHandleError(my_handle); // TODO(ajm): warning possible? |
| 101 | } |
| 102 | |
| 103 | handle_index++; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return apm_->kNoError; |
| 108 | } |
| 109 | |
| 110 | int EchoCancellationImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
| 111 | if (!is_component_enabled()) { |
| 112 | return apm_->kNoError; |
| 113 | } |
| 114 | |
| 115 | if (!apm_->was_stream_delay_set()) { |
| 116 | return apm_->kStreamParameterNotSetError; |
| 117 | } |
| 118 | |
| 119 | if (drift_compensation_enabled_ && !was_stream_drift_set_) { |
| 120 | return apm_->kStreamParameterNotSetError; |
| 121 | } |
| 122 | |
| 123 | assert(audio->samples_per_split_channel() <= 160); |
| 124 | assert(audio->num_channels() == apm_->num_output_channels()); |
| 125 | |
| 126 | int err = apm_->kNoError; |
| 127 | |
| 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( |
| 135 | my_handle, |
| 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), |
pbos@webrtc.org | 3f6d5e0 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 140 | static_cast<int16_t>(audio->samples_per_split_channel()), |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 141 | apm_->stream_delay_ms(), |
| 142 | stream_drift_samples_); |
| 143 | |
| 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) { |
| 148 | return err; |
| 149 | } |
| 150 | } |
| 151 | |
bjornv@webrtc.org | dba34dd | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 152 | int status = 0; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 153 | err = WebRtcAec_get_echo_status(my_handle, &status); |
| 154 | if (err != apm_->kNoError) { |
| 155 | return GetHandleError(my_handle); |
| 156 | } |
| 157 | |
| 158 | if (status == 1) { |
| 159 | stream_has_echo_ = true; |
| 160 | } |
| 161 | |
| 162 | handle_index++; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | was_stream_drift_set_ = false; |
| 167 | return apm_->kNoError; |
| 168 | } |
| 169 | |
| 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; |
| 175 | } |
| 176 | |
| 177 | return EnableComponent(enable); |
| 178 | } |
| 179 | |
| 180 | bool EchoCancellationImpl::is_enabled() const { |
| 181 | return is_component_enabled(); |
| 182 | } |
| 183 | |
| 184 | int EchoCancellationImpl::set_suppression_level(SuppressionLevel level) { |
| 185 | CriticalSectionScoped crit_scoped(apm_->crit()); |
| 186 | if (MapSetting(level) == -1) { |
| 187 | return apm_->kBadParameterError; |
| 188 | } |
| 189 | |
| 190 | suppression_level_ = level; |
| 191 | return Configure(); |
| 192 | } |
| 193 | |
| 194 | EchoCancellation::SuppressionLevel EchoCancellationImpl::suppression_level() |
| 195 | const { |
| 196 | return suppression_level_; |
| 197 | } |
| 198 | |
| 199 | int EchoCancellationImpl::enable_drift_compensation(bool enable) { |
| 200 | CriticalSectionScoped crit_scoped(apm_->crit()); |
| 201 | drift_compensation_enabled_ = enable; |
| 202 | return Configure(); |
| 203 | } |
| 204 | |
| 205 | bool EchoCancellationImpl::is_drift_compensation_enabled() const { |
| 206 | return drift_compensation_enabled_; |
| 207 | } |
| 208 | |
| 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; |
| 213 | } |
| 214 | |
| 215 | device_sample_rate_hz_ = rate; |
| 216 | return Initialize(); |
| 217 | } |
| 218 | |
| 219 | int EchoCancellationImpl::device_sample_rate_hz() const { |
| 220 | return device_sample_rate_hz_; |
| 221 | } |
| 222 | |
andrew@webrtc.org | b563e5e | 2013-03-01 18:47:28 +0000 | [diff] [blame] | 223 | void EchoCancellationImpl::set_stream_drift_samples(int drift) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 224 | was_stream_drift_set_ = true; |
| 225 | stream_drift_samples_ = drift; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int EchoCancellationImpl::stream_drift_samples() const { |
| 229 | return stream_drift_samples_; |
| 230 | } |
| 231 | |
| 232 | int EchoCancellationImpl::enable_metrics(bool enable) { |
| 233 | CriticalSectionScoped crit_scoped(apm_->crit()); |
| 234 | metrics_enabled_ = enable; |
| 235 | return Configure(); |
| 236 | } |
| 237 | |
| 238 | bool EchoCancellationImpl::are_metrics_enabled() const { |
| 239 | return metrics_enabled_; |
| 240 | } |
| 241 | |
| 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; |
| 248 | } |
| 249 | |
| 250 | if (!is_component_enabled() || !metrics_enabled_) { |
| 251 | return apm_->kNotEnabledError; |
| 252 | } |
| 253 | |
| 254 | AecMetrics my_metrics; |
| 255 | memset(&my_metrics, 0, sizeof(my_metrics)); |
| 256 | memset(metrics, 0, sizeof(Metrics)); |
| 257 | |
| 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); |
| 262 | } |
| 263 | |
| 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; |
| 268 | |
| 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; |
| 273 | |
| 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; |
| 278 | |
| 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; |
| 283 | |
| 284 | return apm_->kNoError; |
| 285 | } |
| 286 | |
| 287 | bool EchoCancellationImpl::stream_has_echo() const { |
| 288 | return stream_has_echo_; |
| 289 | } |
| 290 | |
| 291 | int EchoCancellationImpl::enable_delay_logging(bool enable) { |
| 292 | CriticalSectionScoped crit_scoped(apm_->crit()); |
| 293 | delay_logging_enabled_ = enable; |
| 294 | return Configure(); |
| 295 | } |
| 296 | |
| 297 | bool EchoCancellationImpl::is_delay_logging_enabled() const { |
| 298 | return delay_logging_enabled_; |
| 299 | } |
| 300 | |
| 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; |
| 306 | } |
| 307 | if (std == NULL) { |
| 308 | return apm_->kNullPointerError; |
| 309 | } |
| 310 | |
| 311 | if (!is_component_enabled() || !delay_logging_enabled_) { |
| 312 | return apm_->kNotEnabledError; |
| 313 | } |
| 314 | |
| 315 | Handle* my_handle = static_cast<Handle*>(handle(0)); |
| 316 | if (WebRtcAec_GetDelayMetrics(my_handle, median, std) != |
| 317 | apm_->kNoError) { |
| 318 | return GetHandleError(my_handle); |
| 319 | } |
| 320 | |
| 321 | return apm_->kNoError; |
| 322 | } |
| 323 | |
bjornv@webrtc.org | 4c2225f | 2013-03-05 16:53:09 +0000 | [diff] [blame] | 324 | struct AecCore* EchoCancellationImpl::aec_core() const { |
| 325 | CriticalSectionScoped crit_scoped(apm_->crit()); |
| 326 | if (!is_component_enabled()) { |
| 327 | return NULL; |
| 328 | } |
| 329 | Handle* my_handle = static_cast<Handle*>(handle(0)); |
| 330 | return WebRtcAec_aec_core(my_handle); |
| 331 | } |
| 332 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 333 | int EchoCancellationImpl::Initialize() { |
| 334 | int err = ProcessingComponent::Initialize(); |
| 335 | if (err != apm_->kNoError || !is_component_enabled()) { |
| 336 | return err; |
| 337 | } |
| 338 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 339 | return apm_->kNoError; |
| 340 | } |
| 341 | |
andrew@webrtc.org | 8ddec2c | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 342 | void EchoCancellationImpl::SetExtraOptions(const Config& config) { |
| 343 | delay_correction_enabled_ = config.Get<DelayCorrection>().enabled; |
| 344 | Configure(); |
| 345 | } |
| 346 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 347 | void* EchoCancellationImpl::CreateHandle() const { |
| 348 | Handle* handle = NULL; |
| 349 | if (WebRtcAec_Create(&handle) != apm_->kNoError) { |
| 350 | handle = NULL; |
| 351 | } else { |
| 352 | assert(handle != NULL); |
| 353 | } |
| 354 | |
| 355 | return handle; |
| 356 | } |
| 357 | |
| 358 | int EchoCancellationImpl::DestroyHandle(void* handle) const { |
| 359 | assert(handle != NULL); |
| 360 | return WebRtcAec_Free(static_cast<Handle*>(handle)); |
| 361 | } |
| 362 | |
| 363 | int EchoCancellationImpl::InitializeHandle(void* handle) const { |
| 364 | assert(handle != NULL); |
| 365 | return WebRtcAec_Init(static_cast<Handle*>(handle), |
| 366 | apm_->sample_rate_hz(), |
| 367 | device_sample_rate_hz_); |
| 368 | } |
| 369 | |
| 370 | int EchoCancellationImpl::ConfigureHandle(void* handle) const { |
| 371 | assert(handle != NULL); |
| 372 | AecConfig config; |
| 373 | config.metricsMode = metrics_enabled_; |
| 374 | config.nlpMode = MapSetting(suppression_level_); |
| 375 | config.skewMode = drift_compensation_enabled_; |
| 376 | config.delay_logging = delay_logging_enabled_; |
| 377 | |
andrew@webrtc.org | 8ddec2c | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 378 | WebRtcAec_enable_delay_correction(WebRtcAec_aec_core( |
| 379 | static_cast<Handle*>(handle)), delay_correction_enabled_ ? 1 : 0); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 380 | return WebRtcAec_set_config(static_cast<Handle*>(handle), config); |
| 381 | } |
| 382 | |
| 383 | int EchoCancellationImpl::num_handles_required() const { |
| 384 | return apm_->num_output_channels() * |
| 385 | apm_->num_reverse_channels(); |
| 386 | } |
| 387 | |
| 388 | int EchoCancellationImpl::GetHandleError(void* handle) const { |
| 389 | assert(handle != NULL); |
| 390 | return MapError(WebRtcAec_get_error_code(static_cast<Handle*>(handle))); |
| 391 | } |
| 392 | } // namespace webrtc |