niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 0c6f931 | 2012-01-30 09:39:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/echo_cancellation_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 13 | #include <assert.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 16 | extern "C" { |
| 17 | #include "webrtc/modules/audio_processing/aec/aec_core.h" |
| 18 | } |
Henrik Kjellander | 9b72af9 | 2015-11-11 20:16:11 +0100 | [diff] [blame] | 19 | #include "webrtc/modules/audio_processing/aec/echo_cancellation.h" |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/audio_buffer.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | namespace webrtc { |
| 24 | |
| 25 | typedef void Handle; |
| 26 | |
| 27 | namespace { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 28 | int16_t MapSetting(EchoCancellation::SuppressionLevel level) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | } |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 37 | assert(false); |
mflodman@webrtc.org | 657b2a4 | 2012-02-06 11:06:01 +0000 | [diff] [blame] | 38 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | } |
| 40 | |
andrew@webrtc.org | 648af74 | 2012-02-08 01:57:29 +0000 | [diff] [blame] | 41 | AudioProcessing::Error MapError(int err) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | switch (err) { |
| 43 | case AEC_UNSUPPORTED_FUNCTION_ERROR: |
| 44 | return AudioProcessing::kUnsupportedFunctionError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | case AEC_BAD_PARAMETER_ERROR: |
| 46 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | case AEC_BAD_PARAMETER_WARNING: |
| 48 | return AudioProcessing::kBadStreamParameterWarning; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | default: |
| 50 | // AEC_UNSPECIFIED_ERROR |
| 51 | // AEC_UNINITIALIZED_ERROR |
| 52 | // AEC_NULL_POINTER_ERROR |
| 53 | return AudioProcessing::kUnspecifiedError; |
| 54 | } |
| 55 | } |
| 56 | } // namespace |
| 57 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame^] | 58 | const size_t EchoCancellationImpl::kAllowedValuesOfSamplesPerFrame1; |
| 59 | const size_t EchoCancellationImpl::kAllowedValuesOfSamplesPerFrame2; |
| 60 | |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 61 | EchoCancellationImpl::EchoCancellationImpl(const AudioProcessing* apm, |
| 62 | CriticalSectionWrapper* crit) |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 63 | : ProcessingComponent(), |
| 64 | apm_(apm), |
| 65 | crit_(crit), |
| 66 | drift_compensation_enabled_(false), |
| 67 | metrics_enabled_(false), |
| 68 | suppression_level_(kModerateSuppression), |
| 69 | stream_drift_samples_(0), |
| 70 | was_stream_drift_set_(false), |
| 71 | stream_has_echo_(false), |
| 72 | delay_logging_enabled_(false), |
| 73 | extended_filter_enabled_(false), |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame^] | 74 | delay_agnostic_enabled_(false), |
| 75 | render_queue_element_max_size_(0) { |
| 76 | AllocateRenderQueue(); |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 77 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | |
| 79 | EchoCancellationImpl::~EchoCancellationImpl() {} |
| 80 | |
| 81 | int EchoCancellationImpl::ProcessRenderAudio(const AudioBuffer* audio) { |
| 82 | if (!is_component_enabled()) { |
| 83 | return apm_->kNoError; |
| 84 | } |
| 85 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 86 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | assert(audio->num_channels() == apm_->num_reverse_channels()); |
| 88 | |
| 89 | int err = apm_->kNoError; |
| 90 | |
| 91 | // The ordering convention must be followed to pass to the correct AEC. |
| 92 | size_t handle_index = 0; |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame^] | 93 | render_queue_buffer_.clear(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | for (int i = 0; i < apm_->num_output_channels(); i++) { |
| 95 | for (int j = 0; j < audio->num_channels(); j++) { |
| 96 | Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame^] | 97 | // Retrieve any error code produced by the buffering of the farend |
| 98 | // signal |
| 99 | err = WebRtcAec_GetBufferFarendError( |
| 100 | my_handle, audio->split_bands_const_f(j)[kBand0To8kHz], |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 101 | audio->num_frames_per_band()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | |
| 103 | if (err != apm_->kNoError) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 104 | return MapError(err); // TODO(ajm): warning possible? |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | } |
| 106 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame^] | 107 | // Buffer the samples in the render queue. |
| 108 | render_queue_buffer_.insert(render_queue_buffer_.end(), |
| 109 | audio->split_bands_const_f(j)[kBand0To8kHz], |
| 110 | (audio->split_bands_const_f(j)[kBand0To8kHz] + |
| 111 | audio->num_frames_per_band())); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame^] | 115 | // Insert the samples into the queue. |
| 116 | if (!render_signal_queue_->Insert(&render_queue_buffer_)) { |
| 117 | ReadQueuedRenderData(); |
| 118 | |
| 119 | // Retry the insert (should always work). |
| 120 | RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true); |
| 121 | } |
| 122 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | return apm_->kNoError; |
| 124 | } |
| 125 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame^] | 126 | // Read chunks of data that were received and queued on the render side from |
| 127 | // a queue. All the data chunks are buffered into the farend signal of the AEC. |
| 128 | void EchoCancellationImpl::ReadQueuedRenderData() { |
| 129 | if (!is_component_enabled()) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | while (render_signal_queue_->Remove(&capture_queue_buffer_)) { |
| 134 | size_t handle_index = 0; |
| 135 | int buffer_index = 0; |
| 136 | const int num_frames_per_band = |
| 137 | capture_queue_buffer_.size() / |
| 138 | (apm_->num_output_channels() * apm_->num_reverse_channels()); |
| 139 | for (int i = 0; i < apm_->num_output_channels(); i++) { |
| 140 | for (int j = 0; j < apm_->num_reverse_channels(); j++) { |
| 141 | Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
| 142 | WebRtcAec_BufferFarend(my_handle, &capture_queue_buffer_[buffer_index], |
| 143 | num_frames_per_band); |
| 144 | |
| 145 | buffer_index += num_frames_per_band; |
| 146 | handle_index++; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | int EchoCancellationImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
| 153 | if (!is_component_enabled()) { |
| 154 | return apm_->kNoError; |
| 155 | } |
| 156 | |
| 157 | if (!apm_->was_stream_delay_set()) { |
| 158 | return apm_->kStreamParameterNotSetError; |
| 159 | } |
| 160 | |
| 161 | if (drift_compensation_enabled_ && !was_stream_drift_set_) { |
| 162 | return apm_->kStreamParameterNotSetError; |
| 163 | } |
| 164 | |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 165 | assert(audio->num_frames_per_band() <= 160); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 166 | assert(audio->num_channels() == apm_->num_output_channels()); |
| 167 | |
| 168 | int err = apm_->kNoError; |
| 169 | |
| 170 | // The ordering convention must be followed to pass to the correct AEC. |
| 171 | size_t handle_index = 0; |
| 172 | stream_has_echo_ = false; |
| 173 | for (int i = 0; i < audio->num_channels(); i++) { |
| 174 | for (int j = 0; j < apm_->num_reverse_channels(); j++) { |
| 175 | Handle* my_handle = handle(handle_index); |
| 176 | err = WebRtcAec_Process( |
| 177 | my_handle, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 178 | audio->split_bands_const_f(i), |
| 179 | audio->num_bands(), |
| 180 | audio->split_bands_f(i), |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 181 | audio->num_frames_per_band(), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | apm_->stream_delay_ms(), |
| 183 | stream_drift_samples_); |
| 184 | |
| 185 | if (err != apm_->kNoError) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 186 | err = MapError(err); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | // TODO(ajm): Figure out how to return warnings properly. |
| 188 | if (err != apm_->kBadStreamParameterWarning) { |
| 189 | return err; |
| 190 | } |
| 191 | } |
| 192 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 193 | int status = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | err = WebRtcAec_get_echo_status(my_handle, &status); |
| 195 | if (err != apm_->kNoError) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 196 | return MapError(err); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (status == 1) { |
| 200 | stream_has_echo_ = true; |
| 201 | } |
| 202 | |
| 203 | handle_index++; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | was_stream_drift_set_ = false; |
| 208 | return apm_->kNoError; |
| 209 | } |
| 210 | |
| 211 | int EchoCancellationImpl::Enable(bool enable) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 212 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | // Ensure AEC and AECM are not both enabled. |
| 214 | if (enable && apm_->echo_control_mobile()->is_enabled()) { |
| 215 | return apm_->kBadParameterError; |
| 216 | } |
| 217 | |
| 218 | return EnableComponent(enable); |
| 219 | } |
| 220 | |
| 221 | bool EchoCancellationImpl::is_enabled() const { |
| 222 | return is_component_enabled(); |
| 223 | } |
| 224 | |
| 225 | int EchoCancellationImpl::set_suppression_level(SuppressionLevel level) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 226 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | if (MapSetting(level) == -1) { |
| 228 | return apm_->kBadParameterError; |
| 229 | } |
| 230 | |
| 231 | suppression_level_ = level; |
| 232 | return Configure(); |
| 233 | } |
| 234 | |
| 235 | EchoCancellation::SuppressionLevel EchoCancellationImpl::suppression_level() |
| 236 | const { |
| 237 | return suppression_level_; |
| 238 | } |
| 239 | |
| 240 | int EchoCancellationImpl::enable_drift_compensation(bool enable) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 241 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 242 | drift_compensation_enabled_ = enable; |
| 243 | return Configure(); |
| 244 | } |
| 245 | |
| 246 | bool EchoCancellationImpl::is_drift_compensation_enabled() const { |
| 247 | return drift_compensation_enabled_; |
| 248 | } |
| 249 | |
andrew@webrtc.org | 6be1e93 | 2013-03-01 18:47:28 +0000 | [diff] [blame] | 250 | void EchoCancellationImpl::set_stream_drift_samples(int drift) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 251 | was_stream_drift_set_ = true; |
| 252 | stream_drift_samples_ = drift; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | int EchoCancellationImpl::stream_drift_samples() const { |
| 256 | return stream_drift_samples_; |
| 257 | } |
| 258 | |
| 259 | int EchoCancellationImpl::enable_metrics(bool enable) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 260 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | metrics_enabled_ = enable; |
| 262 | return Configure(); |
| 263 | } |
| 264 | |
| 265 | bool EchoCancellationImpl::are_metrics_enabled() const { |
| 266 | return metrics_enabled_; |
| 267 | } |
| 268 | |
| 269 | // TODO(ajm): we currently just use the metrics from the first AEC. Think more |
| 270 | // aboue the best way to extend this to multi-channel. |
| 271 | int EchoCancellationImpl::GetMetrics(Metrics* metrics) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 272 | CriticalSectionScoped crit_scoped(crit_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | if (metrics == NULL) { |
| 274 | return apm_->kNullPointerError; |
| 275 | } |
| 276 | |
| 277 | if (!is_component_enabled() || !metrics_enabled_) { |
| 278 | return apm_->kNotEnabledError; |
| 279 | } |
| 280 | |
| 281 | AecMetrics my_metrics; |
| 282 | memset(&my_metrics, 0, sizeof(my_metrics)); |
| 283 | memset(metrics, 0, sizeof(Metrics)); |
| 284 | |
| 285 | Handle* my_handle = static_cast<Handle*>(handle(0)); |
| 286 | int err = WebRtcAec_GetMetrics(my_handle, &my_metrics); |
| 287 | if (err != apm_->kNoError) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 288 | return MapError(err); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | metrics->residual_echo_return_loss.instant = my_metrics.rerl.instant; |
| 292 | metrics->residual_echo_return_loss.average = my_metrics.rerl.average; |
| 293 | metrics->residual_echo_return_loss.maximum = my_metrics.rerl.max; |
| 294 | metrics->residual_echo_return_loss.minimum = my_metrics.rerl.min; |
| 295 | |
| 296 | metrics->echo_return_loss.instant = my_metrics.erl.instant; |
| 297 | metrics->echo_return_loss.average = my_metrics.erl.average; |
| 298 | metrics->echo_return_loss.maximum = my_metrics.erl.max; |
| 299 | metrics->echo_return_loss.minimum = my_metrics.erl.min; |
| 300 | |
| 301 | metrics->echo_return_loss_enhancement.instant = my_metrics.erle.instant; |
| 302 | metrics->echo_return_loss_enhancement.average = my_metrics.erle.average; |
| 303 | metrics->echo_return_loss_enhancement.maximum = my_metrics.erle.max; |
| 304 | metrics->echo_return_loss_enhancement.minimum = my_metrics.erle.min; |
| 305 | |
| 306 | metrics->a_nlp.instant = my_metrics.aNlp.instant; |
| 307 | metrics->a_nlp.average = my_metrics.aNlp.average; |
| 308 | metrics->a_nlp.maximum = my_metrics.aNlp.max; |
| 309 | metrics->a_nlp.minimum = my_metrics.aNlp.min; |
| 310 | |
| 311 | return apm_->kNoError; |
| 312 | } |
| 313 | |
| 314 | bool EchoCancellationImpl::stream_has_echo() const { |
| 315 | return stream_has_echo_; |
| 316 | } |
| 317 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 318 | int EchoCancellationImpl::enable_delay_logging(bool enable) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 319 | CriticalSectionScoped crit_scoped(crit_); |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 320 | delay_logging_enabled_ = enable; |
| 321 | return Configure(); |
| 322 | } |
| 323 | |
| 324 | bool EchoCancellationImpl::is_delay_logging_enabled() const { |
| 325 | return delay_logging_enabled_; |
| 326 | } |
| 327 | |
Minyue | 13b96ba | 2015-10-03 00:39:14 +0200 | [diff] [blame] | 328 | bool EchoCancellationImpl::is_delay_agnostic_enabled() const { |
| 329 | return delay_agnostic_enabled_; |
| 330 | } |
| 331 | |
| 332 | bool EchoCancellationImpl::is_extended_filter_enabled() const { |
| 333 | return extended_filter_enabled_; |
| 334 | } |
| 335 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 336 | // TODO(bjornv): How should we handle the multi-channel case? |
| 337 | int EchoCancellationImpl::GetDelayMetrics(int* median, int* std) { |
bjornv@webrtc.org | b1786db | 2015-02-03 06:06:26 +0000 | [diff] [blame] | 338 | float fraction_poor_delays = 0; |
| 339 | return GetDelayMetrics(median, std, &fraction_poor_delays); |
| 340 | } |
| 341 | |
| 342 | int EchoCancellationImpl::GetDelayMetrics(int* median, int* std, |
| 343 | float* fraction_poor_delays) { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 344 | CriticalSectionScoped crit_scoped(crit_); |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 345 | if (median == NULL) { |
| 346 | return apm_->kNullPointerError; |
| 347 | } |
| 348 | if (std == NULL) { |
| 349 | return apm_->kNullPointerError; |
| 350 | } |
| 351 | |
| 352 | if (!is_component_enabled() || !delay_logging_enabled_) { |
| 353 | return apm_->kNotEnabledError; |
| 354 | } |
| 355 | |
| 356 | Handle* my_handle = static_cast<Handle*>(handle(0)); |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 357 | const int err = |
| 358 | WebRtcAec_GetDelayMetrics(my_handle, median, std, fraction_poor_delays); |
| 359 | if (err != apm_->kNoError) { |
| 360 | return MapError(err); |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | return apm_->kNoError; |
| 364 | } |
| 365 | |
bjornv@webrtc.org | 91d11b3 | 2013-03-05 16:53:09 +0000 | [diff] [blame] | 366 | struct AecCore* EchoCancellationImpl::aec_core() const { |
andrew@webrtc.org | 56e4a05 | 2014-02-27 22:23:17 +0000 | [diff] [blame] | 367 | CriticalSectionScoped crit_scoped(crit_); |
bjornv@webrtc.org | 91d11b3 | 2013-03-05 16:53:09 +0000 | [diff] [blame] | 368 | if (!is_component_enabled()) { |
| 369 | return NULL; |
| 370 | } |
| 371 | Handle* my_handle = static_cast<Handle*>(handle(0)); |
| 372 | return WebRtcAec_aec_core(my_handle); |
| 373 | } |
| 374 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 375 | int EchoCancellationImpl::Initialize() { |
| 376 | int err = ProcessingComponent::Initialize(); |
| 377 | if (err != apm_->kNoError || !is_component_enabled()) { |
| 378 | return err; |
| 379 | } |
| 380 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame^] | 381 | AllocateRenderQueue(); |
| 382 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 383 | return apm_->kNoError; |
| 384 | } |
| 385 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame^] | 386 | void EchoCancellationImpl::AllocateRenderQueue() { |
| 387 | const size_t max_frame_size = std::max<size_t>( |
| 388 | kAllowedValuesOfSamplesPerFrame1, kAllowedValuesOfSamplesPerFrame2); |
| 389 | |
| 390 | const size_t new_render_queue_element_max_size = std::max<size_t>( |
| 391 | static_cast<size_t>(1), max_frame_size * num_handles_required()); |
| 392 | |
| 393 | // Reallocate the queue if the queue item size is too small to fit the |
| 394 | // data to put in the queue. |
| 395 | if (new_render_queue_element_max_size > render_queue_element_max_size_) { |
| 396 | render_queue_element_max_size_ = new_render_queue_element_max_size; |
| 397 | |
| 398 | std::vector<float> template_queue_element(render_queue_element_max_size_); |
| 399 | |
| 400 | render_signal_queue_.reset( |
| 401 | new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>( |
| 402 | kMaxNumFramesToBuffer, template_queue_element, |
| 403 | RenderQueueItemVerifier<float>(render_queue_element_max_size_))); |
| 404 | } else { |
| 405 | render_signal_queue_->Clear(); |
| 406 | } |
| 407 | |
| 408 | render_queue_buffer_.resize(new_render_queue_element_max_size); |
| 409 | capture_queue_buffer_.resize(new_render_queue_element_max_size); |
| 410 | } |
| 411 | |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 412 | void EchoCancellationImpl::SetExtraOptions(const Config& config) { |
Henrik Lundin | b02af18 | 2015-06-16 09:53:23 +0200 | [diff] [blame] | 413 | extended_filter_enabled_ = config.Get<ExtendedFilter>().enabled; |
henrik.lundin | 366e952 | 2015-07-03 00:50:05 -0700 | [diff] [blame] | 414 | delay_agnostic_enabled_ = config.Get<DelayAgnostic>().enabled; |
andrew@webrtc.org | 1760a17 | 2013-09-25 23:17:38 +0000 | [diff] [blame] | 415 | Configure(); |
| 416 | } |
| 417 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 418 | void* EchoCancellationImpl::CreateHandle() const { |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 419 | return WebRtcAec_Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 420 | } |
| 421 | |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 422 | void EchoCancellationImpl::DestroyHandle(void* handle) const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 423 | assert(handle != NULL); |
bjornv@webrtc.org | 5964fe0 | 2014-04-22 06:52:28 +0000 | [diff] [blame] | 424 | WebRtcAec_Free(static_cast<Handle*>(handle)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | int EchoCancellationImpl::InitializeHandle(void* handle) const { |
| 428 | assert(handle != NULL); |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 429 | // TODO(ajm): Drift compensation is disabled in practice. If restored, it |
| 430 | // should be managed internally and not depend on the hardware sample rate. |
| 431 | // For now, just hardcode a 48 kHz value. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 432 | return WebRtcAec_Init(static_cast<Handle*>(handle), |
andrew@webrtc.org | ddbb8a2 | 2014-04-22 21:00:04 +0000 | [diff] [blame] | 433 | apm_->proc_sample_rate_hz(), |
| 434 | 48000); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | int EchoCancellationImpl::ConfigureHandle(void* handle) const { |
| 438 | assert(handle != NULL); |
| 439 | AecConfig config; |
| 440 | config.metricsMode = metrics_enabled_; |
| 441 | config.nlpMode = MapSetting(suppression_level_); |
| 442 | config.skewMode = drift_compensation_enabled_; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 443 | config.delay_logging = delay_logging_enabled_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 444 | |
Henrik Lundin | 441f634 | 2015-06-09 16:03:13 +0200 | [diff] [blame] | 445 | WebRtcAec_enable_extended_filter( |
| 446 | WebRtcAec_aec_core(static_cast<Handle*>(handle)), |
| 447 | extended_filter_enabled_ ? 1 : 0); |
henrik.lundin | 0f133b9 | 2015-07-02 00:17:55 -0700 | [diff] [blame] | 448 | WebRtcAec_enable_delay_agnostic( |
| 449 | WebRtcAec_aec_core(static_cast<Handle*>(handle)), |
| 450 | delay_agnostic_enabled_ ? 1 : 0); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 451 | return WebRtcAec_set_config(static_cast<Handle*>(handle), config); |
| 452 | } |
| 453 | |
| 454 | int EchoCancellationImpl::num_handles_required() const { |
| 455 | return apm_->num_output_channels() * |
| 456 | apm_->num_reverse_channels(); |
| 457 | } |
| 458 | |
| 459 | int EchoCancellationImpl::GetHandleError(void* handle) const { |
| 460 | assert(handle != NULL); |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 461 | return AudioProcessing::kUnspecifiedError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 462 | } |
| 463 | } // namespace webrtc |