blob: 99def9336c4e109ed074c66bc64d52f084b5124b [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 +000016#include "webrtc/modules/audio_processing/aec/aec_core.h"
Henrik Kjellander9b72af92015-11-11 20:16:11 +010017#include "webrtc/modules/audio_processing/aec/echo_cancellation.h"
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +000018#include "webrtc/modules/audio_processing/audio_buffer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
niklase@google.com470e71d2011-07-07 08:21:25 +000020namespace webrtc {
21
22typedef void Handle;
23
24namespace {
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000025int16_t MapSetting(EchoCancellation::SuppressionLevel level) {
niklase@google.com470e71d2011-07-07 08:21:25 +000026 switch (level) {
27 case EchoCancellation::kLowSuppression:
28 return kAecNlpConservative;
29 case EchoCancellation::kModerateSuppression:
30 return kAecNlpModerate;
31 case EchoCancellation::kHighSuppression:
32 return kAecNlpAggressive;
niklase@google.com470e71d2011-07-07 08:21:25 +000033 }
andrew@webrtc.org648af742012-02-08 01:57:29 +000034 assert(false);
mflodman@webrtc.org657b2a42012-02-06 11:06:01 +000035 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000036}
37
andrew@webrtc.org648af742012-02-08 01:57:29 +000038AudioProcessing::Error MapError(int err) {
niklase@google.com470e71d2011-07-07 08:21:25 +000039 switch (err) {
40 case AEC_UNSUPPORTED_FUNCTION_ERROR:
41 return AudioProcessing::kUnsupportedFunctionError;
niklase@google.com470e71d2011-07-07 08:21:25 +000042 case AEC_BAD_PARAMETER_ERROR:
43 return AudioProcessing::kBadParameterError;
niklase@google.com470e71d2011-07-07 08:21:25 +000044 case AEC_BAD_PARAMETER_WARNING:
45 return AudioProcessing::kBadStreamParameterWarning;
niklase@google.com470e71d2011-07-07 08:21:25 +000046 default:
47 // AEC_UNSPECIFIED_ERROR
48 // AEC_UNINITIALIZED_ERROR
49 // AEC_NULL_POINTER_ERROR
50 return AudioProcessing::kUnspecifiedError;
51 }
52}
niklase@google.com470e71d2011-07-07 08:21:25 +000053
peah2446e5a2015-11-18 06:11:13 -080054// Maximum length that a frame of samples can have.
55static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160;
56// Maximum number of frames to buffer in the render queue.
57// TODO(peah): Decrease this once we properly handle hugely unbalanced
58// reverse and forward call numbers.
59static const size_t kMaxNumFramesToBuffer = 100;
60} // namespace
peahfa6228e2015-11-16 16:27:42 -080061
andrew@webrtc.org56e4a052014-02-27 22:23:17 +000062EchoCancellationImpl::EchoCancellationImpl(const AudioProcessing* apm,
peahdf3efa82015-11-28 12:35:15 -080063 rtc::CriticalSection* crit_render,
64 rtc::CriticalSection* crit_capture)
solenberg92586f02016-03-04 12:29:03 -080065 : ProcessingComponent(),
66 apm_(apm),
peahdf3efa82015-11-28 12:35:15 -080067 crit_render_(crit_render),
68 crit_capture_(crit_capture),
Henrik Lundin441f6342015-06-09 16:03:13 +020069 drift_compensation_enabled_(false),
70 metrics_enabled_(false),
71 suppression_level_(kModerateSuppression),
72 stream_drift_samples_(0),
73 was_stream_drift_set_(false),
74 stream_has_echo_(false),
75 delay_logging_enabled_(false),
76 extended_filter_enabled_(false),
peahfa6228e2015-11-16 16:27:42 -080077 delay_agnostic_enabled_(false),
peaha332e2d2016-02-17 01:11:16 -080078 next_generation_aec_enabled_(false),
peahdf3efa82015-11-28 12:35:15 -080079 render_queue_element_max_size_(0) {
80 RTC_DCHECK(apm);
81 RTC_DCHECK(crit_render);
82 RTC_DCHECK(crit_capture);
83}
niklase@google.com470e71d2011-07-07 08:21:25 +000084
85EchoCancellationImpl::~EchoCancellationImpl() {}
86
87int EchoCancellationImpl::ProcessRenderAudio(const AudioBuffer* audio) {
peahdf3efa82015-11-28 12:35:15 -080088 rtc::CritScope cs_render(crit_render_);
solenberg92586f02016-03-04 12:29:03 -080089 if (!is_component_enabled()) {
peahdf3efa82015-11-28 12:35:15 -080090 return AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +000091 }
92
solenberg92586f02016-03-04 12:29:03 -080093 assert(audio->num_frames_per_band() <= 160);
94 assert(audio->num_channels() == apm_->num_reverse_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +000095
peahdf3efa82015-11-28 12:35:15 -080096 int err = AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +000097
98 // The ordering convention must be followed to pass to the correct AEC.
99 size_t handle_index = 0;
peahfa6228e2015-11-16 16:27:42 -0800100 render_queue_buffer_.clear();
Peter Kasting69558702016-01-12 16:26:35 -0800101 for (size_t i = 0; i < apm_->num_output_channels(); i++) {
102 for (size_t j = 0; j < audio->num_channels(); j++) {
solenberg92586f02016-03-04 12:29:03 -0800103 Handle* my_handle = static_cast<Handle*>(handle(handle_index));
peahfa6228e2015-11-16 16:27:42 -0800104 // Retrieve any error code produced by the buffering of the farend
solenberg92586f02016-03-04 12:29:03 -0800105 // signal
peahfa6228e2015-11-16 16:27:42 -0800106 err = WebRtcAec_GetBufferFarendError(
107 my_handle, audio->split_bands_const_f(j)[kBand0To8kHz],
Peter Kastingdce40cf2015-08-24 14:52:23 -0700108 audio->num_frames_per_band());
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
peahdf3efa82015-11-28 12:35:15 -0800110 if (err != AudioProcessing::kNoError) {
peahc12be392015-11-09 23:53:50 -0800111 return MapError(err); // TODO(ajm): warning possible?
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 }
113
peahfa6228e2015-11-16 16:27:42 -0800114 // Buffer the samples in the render queue.
115 render_queue_buffer_.insert(render_queue_buffer_.end(),
116 audio->split_bands_const_f(j)[kBand0To8kHz],
117 (audio->split_bands_const_f(j)[kBand0To8kHz] +
118 audio->num_frames_per_band()));
solenberg92586f02016-03-04 12:29:03 -0800119
120 handle_index++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000121 }
122 }
123
peahfa6228e2015-11-16 16:27:42 -0800124 // Insert the samples into the queue.
125 if (!render_signal_queue_->Insert(&render_queue_buffer_)) {
peahdf3efa82015-11-28 12:35:15 -0800126 // The data queue is full and needs to be emptied.
peahfa6228e2015-11-16 16:27:42 -0800127 ReadQueuedRenderData();
128
129 // Retry the insert (should always work).
130 RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true);
131 }
132
peahdf3efa82015-11-28 12:35:15 -0800133 return AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134}
135
peahfa6228e2015-11-16 16:27:42 -0800136// Read chunks of data that were received and queued on the render side from
137// a queue. All the data chunks are buffered into the farend signal of the AEC.
138void EchoCancellationImpl::ReadQueuedRenderData() {
peahdf3efa82015-11-28 12:35:15 -0800139 rtc::CritScope cs_capture(crit_capture_);
solenberg92586f02016-03-04 12:29:03 -0800140 if (!is_component_enabled()) {
peahfa6228e2015-11-16 16:27:42 -0800141 return;
142 }
143
144 while (render_signal_queue_->Remove(&capture_queue_buffer_)) {
145 size_t handle_index = 0;
pkasting25702cb2016-01-08 13:50:27 -0800146 size_t buffer_index = 0;
147 const size_t num_frames_per_band =
peahfa6228e2015-11-16 16:27:42 -0800148 capture_queue_buffer_.size() /
149 (apm_->num_output_channels() * apm_->num_reverse_channels());
Peter Kasting69558702016-01-12 16:26:35 -0800150 for (size_t i = 0; i < apm_->num_output_channels(); i++) {
151 for (size_t j = 0; j < apm_->num_reverse_channels(); j++) {
solenberg92586f02016-03-04 12:29:03 -0800152 Handle* my_handle = static_cast<Handle*>(handle(handle_index));
peahfa6228e2015-11-16 16:27:42 -0800153 WebRtcAec_BufferFarend(my_handle, &capture_queue_buffer_[buffer_index],
154 num_frames_per_band);
155
156 buffer_index += num_frames_per_band;
solenberg92586f02016-03-04 12:29:03 -0800157 handle_index++;
peahfa6228e2015-11-16 16:27:42 -0800158 }
159 }
160 }
161}
162
niklase@google.com470e71d2011-07-07 08:21:25 +0000163int EchoCancellationImpl::ProcessCaptureAudio(AudioBuffer* audio) {
peahdf3efa82015-11-28 12:35:15 -0800164 rtc::CritScope cs_capture(crit_capture_);
solenberg92586f02016-03-04 12:29:03 -0800165 if (!is_component_enabled()) {
peahdf3efa82015-11-28 12:35:15 -0800166 return AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000167 }
168
169 if (!apm_->was_stream_delay_set()) {
peahdf3efa82015-11-28 12:35:15 -0800170 return AudioProcessing::kStreamParameterNotSetError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000171 }
172
173 if (drift_compensation_enabled_ && !was_stream_drift_set_) {
peahdf3efa82015-11-28 12:35:15 -0800174 return AudioProcessing::kStreamParameterNotSetError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000175 }
176
solenberg92586f02016-03-04 12:29:03 -0800177 assert(audio->num_frames_per_band() <= 160);
178 assert(audio->num_channels() == apm_->num_proc_channels());
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
peahdf3efa82015-11-28 12:35:15 -0800180 int err = AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181
182 // The ordering convention must be followed to pass to the correct AEC.
183 size_t handle_index = 0;
184 stream_has_echo_ = false;
Peter Kasting69558702016-01-12 16:26:35 -0800185 for (size_t i = 0; i < audio->num_channels(); i++) {
186 for (size_t j = 0; j < apm_->num_reverse_channels(); j++) {
solenberg92586f02016-03-04 12:29:03 -0800187 Handle* my_handle = handle(handle_index);
peahdf3efa82015-11-28 12:35:15 -0800188 err = WebRtcAec_Process(my_handle, audio->split_bands_const_f(i),
189 audio->num_bands(), audio->split_bands_f(i),
190 audio->num_frames_per_band(),
191 apm_->stream_delay_ms(), stream_drift_samples_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000192
peahdf3efa82015-11-28 12:35:15 -0800193 if (err != AudioProcessing::kNoError) {
peahc12be392015-11-09 23:53:50 -0800194 err = MapError(err);
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 // TODO(ajm): Figure out how to return warnings properly.
peahdf3efa82015-11-28 12:35:15 -0800196 if (err != AudioProcessing::kBadStreamParameterWarning) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 return err;
198 }
199 }
200
bjornv@webrtc.org21a2fc92013-02-15 17:01:03 +0000201 int status = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 err = WebRtcAec_get_echo_status(my_handle, &status);
peahdf3efa82015-11-28 12:35:15 -0800203 if (err != AudioProcessing::kNoError) {
peahc12be392015-11-09 23:53:50 -0800204 return MapError(err);
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 }
206
207 if (status == 1) {
208 stream_has_echo_ = true;
209 }
solenberg92586f02016-03-04 12:29:03 -0800210
211 handle_index++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000212 }
213 }
214
215 was_stream_drift_set_ = false;
peahdf3efa82015-11-28 12:35:15 -0800216 return AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000217}
218
219int EchoCancellationImpl::Enable(bool enable) {
peahdf3efa82015-11-28 12:35:15 -0800220 // Run in a single-threaded manner.
221 rtc::CritScope cs_render(crit_render_);
222 rtc::CritScope cs_capture(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000223 // Ensure AEC and AECM are not both enabled.
peahdf3efa82015-11-28 12:35:15 -0800224 // The is_enabled call is safe from a deadlock perspective
225 // as both locks are already held in the correct order.
niklase@google.com470e71d2011-07-07 08:21:25 +0000226 if (enable && apm_->echo_control_mobile()->is_enabled()) {
peahdf3efa82015-11-28 12:35:15 -0800227 return AudioProcessing::kBadParameterError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000228 }
229
solenberg92586f02016-03-04 12:29:03 -0800230 return EnableComponent(enable);
niklase@google.com470e71d2011-07-07 08:21:25 +0000231}
232
233bool EchoCancellationImpl::is_enabled() const {
peahdf3efa82015-11-28 12:35:15 -0800234 rtc::CritScope cs(crit_capture_);
solenberg92586f02016-03-04 12:29:03 -0800235 return is_component_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +0000236}
237
238int EchoCancellationImpl::set_suppression_level(SuppressionLevel level) {
peahdf3efa82015-11-28 12:35:15 -0800239 {
240 if (MapSetting(level) == -1) {
241 return AudioProcessing::kBadParameterError;
242 }
243 rtc::CritScope cs(crit_capture_);
244 suppression_level_ = level;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000246 return Configure();
247}
248
249EchoCancellation::SuppressionLevel EchoCancellationImpl::suppression_level()
250 const {
peahdf3efa82015-11-28 12:35:15 -0800251 rtc::CritScope cs(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000252 return suppression_level_;
253}
254
255int EchoCancellationImpl::enable_drift_compensation(bool enable) {
peahdf3efa82015-11-28 12:35:15 -0800256 {
257 rtc::CritScope cs(crit_capture_);
258 drift_compensation_enabled_ = enable;
259 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000260 return Configure();
261}
262
263bool EchoCancellationImpl::is_drift_compensation_enabled() const {
peahdf3efa82015-11-28 12:35:15 -0800264 rtc::CritScope cs(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000265 return drift_compensation_enabled_;
266}
267
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000268void EchoCancellationImpl::set_stream_drift_samples(int drift) {
peahdf3efa82015-11-28 12:35:15 -0800269 rtc::CritScope cs(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000270 was_stream_drift_set_ = true;
271 stream_drift_samples_ = drift;
niklase@google.com470e71d2011-07-07 08:21:25 +0000272}
273
274int EchoCancellationImpl::stream_drift_samples() const {
peahdf3efa82015-11-28 12:35:15 -0800275 rtc::CritScope cs(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000276 return stream_drift_samples_;
277}
278
279int EchoCancellationImpl::enable_metrics(bool enable) {
peahdf3efa82015-11-28 12:35:15 -0800280 {
281 rtc::CritScope cs(crit_capture_);
282 metrics_enabled_ = enable;
283 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000284 return Configure();
285}
286
287bool EchoCancellationImpl::are_metrics_enabled() const {
peahdf3efa82015-11-28 12:35:15 -0800288 rtc::CritScope cs(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000289 return metrics_enabled_;
290}
291
292// TODO(ajm): we currently just use the metrics from the first AEC. Think more
293// aboue the best way to extend this to multi-channel.
294int EchoCancellationImpl::GetMetrics(Metrics* metrics) {
peahdf3efa82015-11-28 12:35:15 -0800295 rtc::CritScope cs(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000296 if (metrics == NULL) {
peahdf3efa82015-11-28 12:35:15 -0800297 return AudioProcessing::kNullPointerError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000298 }
299
solenberg92586f02016-03-04 12:29:03 -0800300 if (!is_component_enabled() || !metrics_enabled_) {
peahdf3efa82015-11-28 12:35:15 -0800301 return AudioProcessing::kNotEnabledError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000302 }
303
304 AecMetrics my_metrics;
305 memset(&my_metrics, 0, sizeof(my_metrics));
306 memset(metrics, 0, sizeof(Metrics));
307
solenberg92586f02016-03-04 12:29:03 -0800308 Handle* my_handle = static_cast<Handle*>(handle(0));
niklase@google.com470e71d2011-07-07 08:21:25 +0000309 int err = WebRtcAec_GetMetrics(my_handle, &my_metrics);
peahdf3efa82015-11-28 12:35:15 -0800310 if (err != AudioProcessing::kNoError) {
peahc12be392015-11-09 23:53:50 -0800311 return MapError(err);
niklase@google.com470e71d2011-07-07 08:21:25 +0000312 }
313
314 metrics->residual_echo_return_loss.instant = my_metrics.rerl.instant;
315 metrics->residual_echo_return_loss.average = my_metrics.rerl.average;
316 metrics->residual_echo_return_loss.maximum = my_metrics.rerl.max;
317 metrics->residual_echo_return_loss.minimum = my_metrics.rerl.min;
318
319 metrics->echo_return_loss.instant = my_metrics.erl.instant;
320 metrics->echo_return_loss.average = my_metrics.erl.average;
321 metrics->echo_return_loss.maximum = my_metrics.erl.max;
322 metrics->echo_return_loss.minimum = my_metrics.erl.min;
323
324 metrics->echo_return_loss_enhancement.instant = my_metrics.erle.instant;
325 metrics->echo_return_loss_enhancement.average = my_metrics.erle.average;
326 metrics->echo_return_loss_enhancement.maximum = my_metrics.erle.max;
327 metrics->echo_return_loss_enhancement.minimum = my_metrics.erle.min;
328
329 metrics->a_nlp.instant = my_metrics.aNlp.instant;
330 metrics->a_nlp.average = my_metrics.aNlp.average;
331 metrics->a_nlp.maximum = my_metrics.aNlp.max;
332 metrics->a_nlp.minimum = my_metrics.aNlp.min;
333
peahdf3efa82015-11-28 12:35:15 -0800334 return AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000335}
336
337bool EchoCancellationImpl::stream_has_echo() const {
peahdf3efa82015-11-28 12:35:15 -0800338 rtc::CritScope cs(crit_capture_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000339 return stream_has_echo_;
340}
341
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000342int EchoCancellationImpl::enable_delay_logging(bool enable) {
peahdf3efa82015-11-28 12:35:15 -0800343 {
344 rtc::CritScope cs(crit_capture_);
345 delay_logging_enabled_ = enable;
346 }
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000347 return Configure();
348}
349
350bool EchoCancellationImpl::is_delay_logging_enabled() const {
peahdf3efa82015-11-28 12:35:15 -0800351 rtc::CritScope cs(crit_capture_);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000352 return delay_logging_enabled_;
353}
354
Minyue13b96ba2015-10-03 00:39:14 +0200355bool EchoCancellationImpl::is_delay_agnostic_enabled() const {
peahdf3efa82015-11-28 12:35:15 -0800356 rtc::CritScope cs(crit_capture_);
Minyue13b96ba2015-10-03 00:39:14 +0200357 return delay_agnostic_enabled_;
358}
359
peaha332e2d2016-02-17 01:11:16 -0800360bool EchoCancellationImpl::is_next_generation_aec_enabled() const {
361 rtc::CritScope cs(crit_capture_);
362 return next_generation_aec_enabled_;
363}
364
Minyue13b96ba2015-10-03 00:39:14 +0200365bool EchoCancellationImpl::is_extended_filter_enabled() const {
peahdf3efa82015-11-28 12:35:15 -0800366 rtc::CritScope cs(crit_capture_);
Minyue13b96ba2015-10-03 00:39:14 +0200367 return extended_filter_enabled_;
368}
369
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000370// TODO(bjornv): How should we handle the multi-channel case?
371int EchoCancellationImpl::GetDelayMetrics(int* median, int* std) {
peahdf3efa82015-11-28 12:35:15 -0800372 rtc::CritScope cs(crit_capture_);
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000373 float fraction_poor_delays = 0;
374 return GetDelayMetrics(median, std, &fraction_poor_delays);
375}
376
377int EchoCancellationImpl::GetDelayMetrics(int* median, int* std,
378 float* fraction_poor_delays) {
peahdf3efa82015-11-28 12:35:15 -0800379 rtc::CritScope cs(crit_capture_);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000380 if (median == NULL) {
peahdf3efa82015-11-28 12:35:15 -0800381 return AudioProcessing::kNullPointerError;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000382 }
383 if (std == NULL) {
peahdf3efa82015-11-28 12:35:15 -0800384 return AudioProcessing::kNullPointerError;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000385 }
386
solenberg92586f02016-03-04 12:29:03 -0800387 if (!is_component_enabled() || !delay_logging_enabled_) {
peahdf3efa82015-11-28 12:35:15 -0800388 return AudioProcessing::kNotEnabledError;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000389 }
390
solenberg92586f02016-03-04 12:29:03 -0800391 Handle* my_handle = static_cast<Handle*>(handle(0));
peahc12be392015-11-09 23:53:50 -0800392 const int err =
393 WebRtcAec_GetDelayMetrics(my_handle, median, std, fraction_poor_delays);
peahdf3efa82015-11-28 12:35:15 -0800394 if (err != AudioProcessing::kNoError) {
peahc12be392015-11-09 23:53:50 -0800395 return MapError(err);
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000396 }
397
peahdf3efa82015-11-28 12:35:15 -0800398 return AudioProcessing::kNoError;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000399}
400
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000401struct AecCore* EchoCancellationImpl::aec_core() const {
peahdf3efa82015-11-28 12:35:15 -0800402 rtc::CritScope cs(crit_capture_);
solenberg92586f02016-03-04 12:29:03 -0800403 if (!is_component_enabled()) {
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000404 return NULL;
405 }
solenberg92586f02016-03-04 12:29:03 -0800406 Handle* my_handle = static_cast<Handle*>(handle(0));
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000407 return WebRtcAec_aec_core(my_handle);
408}
409
solenberg92586f02016-03-04 12:29:03 -0800410int EchoCancellationImpl::Initialize() {
411 int err = ProcessingComponent::Initialize();
412 {
413 rtc::CritScope cs(crit_capture_);
414 if (err != AudioProcessing::kNoError || !is_component_enabled()) {
415 return err;
peahdf3efa82015-11-28 12:35:15 -0800416 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000417 }
418
peah3af0a002016-03-04 12:13:31 -0800419 AllocateRenderQueue();
solenberg92586f02016-03-04 12:29:03 -0800420
421 return AudioProcessing::kNoError;
niklase@google.com470e71d2011-07-07 08:21:25 +0000422}
423
peah20028c42016-03-04 11:50:54 -0800424int EchoCancellationImpl::GetSystemDelayInSamples() const {
425 rtc::CritScope cs(crit_capture_);
426 RTC_DCHECK(is_component_enabled());
427 // Report the delay for the first AEC component.
428 return WebRtcAec_system_delay(
429 WebRtcAec_aec_core(static_cast<Handle*>(handle(0))));
430}
431
peahfa6228e2015-11-16 16:27:42 -0800432void EchoCancellationImpl::AllocateRenderQueue() {
peahfa6228e2015-11-16 16:27:42 -0800433 const size_t new_render_queue_element_max_size = std::max<size_t>(
peah2446e5a2015-11-18 06:11:13 -0800434 static_cast<size_t>(1),
435 kMaxAllowedValuesOfSamplesPerFrame * num_handles_required());
peahfa6228e2015-11-16 16:27:42 -0800436
peahdf3efa82015-11-28 12:35:15 -0800437 rtc::CritScope cs_render(crit_render_);
438 rtc::CritScope cs_capture(crit_capture_);
439
peahfa6228e2015-11-16 16:27:42 -0800440 // Reallocate the queue if the queue item size is too small to fit the
441 // data to put in the queue.
peah2446e5a2015-11-18 06:11:13 -0800442 if (render_queue_element_max_size_ < new_render_queue_element_max_size) {
peahfa6228e2015-11-16 16:27:42 -0800443 render_queue_element_max_size_ = new_render_queue_element_max_size;
444
445 std::vector<float> template_queue_element(render_queue_element_max_size_);
446
447 render_signal_queue_.reset(
448 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
449 kMaxNumFramesToBuffer, template_queue_element,
450 RenderQueueItemVerifier<float>(render_queue_element_max_size_)));
peah2446e5a2015-11-18 06:11:13 -0800451
452 render_queue_buffer_.resize(render_queue_element_max_size_);
453 capture_queue_buffer_.resize(render_queue_element_max_size_);
peahfa6228e2015-11-16 16:27:42 -0800454 } else {
455 render_signal_queue_->Clear();
456 }
peahfa6228e2015-11-16 16:27:42 -0800457}
458
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000459void EchoCancellationImpl::SetExtraOptions(const Config& config) {
peahdf3efa82015-11-28 12:35:15 -0800460 {
461 rtc::CritScope cs(crit_capture_);
462 extended_filter_enabled_ = config.Get<ExtendedFilter>().enabled;
463 delay_agnostic_enabled_ = config.Get<DelayAgnostic>().enabled;
peaha332e2d2016-02-17 01:11:16 -0800464 next_generation_aec_enabled_ = config.Get<NextGenerationAec>().enabled;
peahdf3efa82015-11-28 12:35:15 -0800465 }
andrew@webrtc.org1760a172013-09-25 23:17:38 +0000466 Configure();
467}
468
solenberg92586f02016-03-04 12:29:03 -0800469void* EchoCancellationImpl::CreateHandle() const {
470 return WebRtcAec_Create();
471}
472
473void EchoCancellationImpl::DestroyHandle(void* handle) const {
474 assert(handle != NULL);
475 WebRtcAec_Free(static_cast<Handle*>(handle));
476}
477
478int EchoCancellationImpl::InitializeHandle(void* handle) const {
479 // Not locked as it only relies on APM public API which is threadsafe.
480
481 assert(handle != NULL);
482 // TODO(ajm): Drift compensation is disabled in practice. If restored, it
483 // should be managed internally and not depend on the hardware sample rate.
484 // For now, just hardcode a 48 kHz value.
485 return WebRtcAec_Init(static_cast<Handle*>(handle),
486 apm_->proc_sample_rate_hz(), 48000);
487}
488
489int EchoCancellationImpl::ConfigureHandle(void* handle) const {
peahdf3efa82015-11-28 12:35:15 -0800490 rtc::CritScope cs_render(crit_render_);
491 rtc::CritScope cs_capture(crit_capture_);
solenberg92586f02016-03-04 12:29:03 -0800492 assert(handle != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000493 AecConfig config;
494 config.metricsMode = metrics_enabled_;
495 config.nlpMode = MapSetting(suppression_level_);
496 config.skewMode = drift_compensation_enabled_;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000497 config.delay_logging = delay_logging_enabled_;
solenberg92586f02016-03-04 12:29:03 -0800498 WebRtcAec_enable_extended_filter(
499 WebRtcAec_aec_core(static_cast<Handle*>(handle)),
500 extended_filter_enabled_ ? 1 : 0);
501 WebRtcAec_enable_delay_agnostic(
502 WebRtcAec_aec_core(static_cast<Handle*>(handle)),
503 delay_agnostic_enabled_ ? 1 : 0);
504 WebRtcAec_enable_next_generation_aec(
505 WebRtcAec_aec_core(static_cast<Handle*>(handle)),
506 next_generation_aec_enabled_ ? 1 : 0);
507 return WebRtcAec_set_config(static_cast<Handle*>(handle), config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000508}
509
Peter Kasting69558702016-01-12 16:26:35 -0800510size_t EchoCancellationImpl::num_handles_required() const {
peahdf3efa82015-11-28 12:35:15 -0800511 // Not locked as it only relies on APM public API which is threadsafe.
pkasting25702cb2016-01-08 13:50:27 -0800512 return apm_->num_output_channels() * apm_->num_reverse_channels();
niklase@google.com470e71d2011-07-07 08:21:25 +0000513}
514
solenberg92586f02016-03-04 12:29:03 -0800515int EchoCancellationImpl::GetHandleError(void* handle) const {
516 // Not locked as it does not rely on anything in the state.
517 assert(handle != NULL);
518 return AudioProcessing::kUnspecifiedError;
519}
niklase@google.com470e71d2011-07-07 08:21:25 +0000520} // namespace webrtc