- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_processing / echo_cancellation_impl.cc
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
11 #include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
12
13 #include <assert.h>
14 #include <string.h>
15
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"
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"
23
24 namespace webrtc {
25
26 typedef void Handle;
27
28 namespace {
29 int16_t MapSetting(EchoCancellation::SuppressionLevel level) {
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
59 EchoCancellationImplWrapper* EchoCancellationImplWrapper::Create(
60     const AudioProcessingImpl* audioproc) {
61   return new EchoCancellationImpl(audioproc);
62 }
63
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),
74     delay_logging_enabled_(false),
75     delay_correction_enabled_(false) {}
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),
97           static_cast<int16_t>(audio->samples_per_split_channel()));
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),
140           static_cast<int16_t>(audio->samples_per_split_channel()),
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
152       int status = 0;
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
223 void EchoCancellationImpl::set_stream_drift_samples(int drift) {
224   was_stream_drift_set_ = true;
225   stream_drift_samples_ = drift;
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
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
333 int EchoCancellationImpl::Initialize() {
334   int err = ProcessingComponent::Initialize();
335   if (err != apm_->kNoError || !is_component_enabled()) {
336     return err;
337   }
338
339   was_stream_drift_set_ = false;
340
341   return apm_->kNoError;
342 }
343
344 void EchoCancellationImpl::SetExtraOptions(const Config& config) {
345   delay_correction_enabled_ = config.Get<DelayCorrection>().enabled;
346   Configure();
347 }
348
349 void* EchoCancellationImpl::CreateHandle() const {
350   Handle* handle = NULL;
351   if (WebRtcAec_Create(&handle) != apm_->kNoError) {
352     handle = NULL;
353   } else {
354     assert(handle != NULL);
355   }
356
357   return handle;
358 }
359
360 int EchoCancellationImpl::DestroyHandle(void* handle) const {
361   assert(handle != NULL);
362   return WebRtcAec_Free(static_cast<Handle*>(handle));
363 }
364
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_);
370 }
371
372 int EchoCancellationImpl::ConfigureHandle(void* handle) const {
373   assert(handle != NULL);
374   AecConfig config;
375   config.metricsMode = metrics_enabled_;
376   config.nlpMode = MapSetting(suppression_level_);
377   config.skewMode = drift_compensation_enabled_;
378   config.delay_logging = delay_logging_enabled_;
379
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);
383 }
384
385 int EchoCancellationImpl::num_handles_required() const {
386   return apm_->num_output_channels() *
387          apm_->num_reverse_channels();
388 }
389
390 int EchoCancellationImpl::GetHandleError(void* handle) const {
391   assert(handle != NULL);
392   return MapError(WebRtcAec_get_error_code(static_cast<Handle*>(handle)));
393 }
394 }  // namespace webrtc