2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-01-30 09:39:08 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-02-15 17:01:03 +00:00
|
|
|
#include "webrtc/modules/audio_processing/echo_cancellation_impl.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-02-15 17:01:03 +00:00
|
|
|
#include <assert.h>
|
2011-07-07 08:21:25 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
2013-09-25 23:17:38 +00:00
|
|
|
#include "webrtc/modules/audio_processing/aec/aec_core.h"
|
2015-11-11 20:16:11 +01:00
|
|
|
#include "webrtc/modules/audio_processing/aec/echo_cancellation.h"
|
2013-02-15 17:01:03 +00:00
|
|
|
#include "webrtc/modules/audio_processing/audio_buffer.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
namespace {
|
2013-04-10 07:50:54 +00:00
|
|
|
int16_t MapSetting(EchoCancellation::SuppressionLevel level) {
|
2011-07-07 08:21:25 +00:00
|
|
|
switch (level) {
|
|
|
|
|
case EchoCancellation::kLowSuppression:
|
|
|
|
|
return kAecNlpConservative;
|
|
|
|
|
case EchoCancellation::kModerateSuppression:
|
|
|
|
|
return kAecNlpModerate;
|
|
|
|
|
case EchoCancellation::kHighSuppression:
|
|
|
|
|
return kAecNlpAggressive;
|
|
|
|
|
}
|
2012-02-08 01:57:29 +00:00
|
|
|
assert(false);
|
2012-02-06 11:06:01 +00:00
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-02-08 01:57:29 +00:00
|
|
|
AudioProcessing::Error MapError(int err) {
|
2011-07-07 08:21:25 +00:00
|
|
|
switch (err) {
|
|
|
|
|
case AEC_UNSUPPORTED_FUNCTION_ERROR:
|
|
|
|
|
return AudioProcessing::kUnsupportedFunctionError;
|
|
|
|
|
case AEC_BAD_PARAMETER_ERROR:
|
|
|
|
|
return AudioProcessing::kBadParameterError;
|
|
|
|
|
case AEC_BAD_PARAMETER_WARNING:
|
|
|
|
|
return AudioProcessing::kBadStreamParameterWarning;
|
|
|
|
|
default:
|
|
|
|
|
// AEC_UNSPECIFIED_ERROR
|
|
|
|
|
// AEC_UNINITIALIZED_ERROR
|
|
|
|
|
// AEC_NULL_POINTER_ERROR
|
|
|
|
|
return AudioProcessing::kUnspecifiedError;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-18 06:11:13 -08:00
|
|
|
// Maximum length that a frame of samples can have.
|
|
|
|
|
static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160;
|
|
|
|
|
// Maximum number of frames to buffer in the render queue.
|
|
|
|
|
// TODO(peah): Decrease this once we properly handle hugely unbalanced
|
|
|
|
|
// reverse and forward call numbers.
|
|
|
|
|
static const size_t kMaxNumFramesToBuffer = 100;
|
|
|
|
|
} // namespace
|
2015-11-16 16:27:42 -08:00
|
|
|
|
2016-03-15 09:34:24 -07:00
|
|
|
struct EchoCancellationImpl::StreamProperties {
|
|
|
|
|
StreamProperties() = delete;
|
|
|
|
|
StreamProperties(int sample_rate_hz,
|
|
|
|
|
size_t num_reverse_channels,
|
|
|
|
|
size_t num_output_channels,
|
|
|
|
|
size_t num_proc_channels)
|
|
|
|
|
: sample_rate_hz(sample_rate_hz),
|
|
|
|
|
num_reverse_channels(num_reverse_channels),
|
|
|
|
|
num_output_channels(num_output_channels),
|
|
|
|
|
num_proc_channels(num_proc_channels) {}
|
|
|
|
|
|
|
|
|
|
const int sample_rate_hz;
|
|
|
|
|
const size_t num_reverse_channels;
|
|
|
|
|
const size_t num_output_channels;
|
|
|
|
|
const size_t num_proc_channels;
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-05 03:01:14 -08:00
|
|
|
class EchoCancellationImpl::Canceller {
|
|
|
|
|
public:
|
2016-03-07 22:50:14 -08:00
|
|
|
Canceller() {
|
2016-03-05 03:01:14 -08:00
|
|
|
state_ = WebRtcAec_Create();
|
|
|
|
|
RTC_DCHECK(state_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~Canceller() {
|
|
|
|
|
RTC_CHECK(state_);
|
|
|
|
|
WebRtcAec_Free(state_);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:50:14 -08:00
|
|
|
void* state() { return state_; }
|
2016-03-05 03:01:14 -08:00
|
|
|
|
|
|
|
|
void Initialize(int sample_rate_hz) {
|
|
|
|
|
// TODO(ajm): Drift compensation is disabled in practice. If restored, it
|
|
|
|
|
// should be managed internally and not depend on the hardware sample rate.
|
|
|
|
|
// For now, just hardcode a 48 kHz value.
|
|
|
|
|
const int error = WebRtcAec_Init(state_, sample_rate_hz, 48000);
|
|
|
|
|
RTC_DCHECK_EQ(0, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2016-03-07 22:50:14 -08:00
|
|
|
void* state_;
|
2016-03-05 03:01:14 -08:00
|
|
|
};
|
|
|
|
|
|
2016-03-15 09:34:24 -07:00
|
|
|
EchoCancellationImpl::EchoCancellationImpl(rtc::CriticalSection* crit_render,
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CriticalSection* crit_capture)
|
2016-03-15 09:34:24 -07:00
|
|
|
: crit_render_(crit_render),
|
2015-11-28 12:35:15 -08:00
|
|
|
crit_capture_(crit_capture),
|
2015-06-09 16:03:13 +02:00
|
|
|
drift_compensation_enabled_(false),
|
|
|
|
|
metrics_enabled_(false),
|
|
|
|
|
suppression_level_(kModerateSuppression),
|
|
|
|
|
stream_drift_samples_(0),
|
|
|
|
|
was_stream_drift_set_(false),
|
|
|
|
|
stream_has_echo_(false),
|
|
|
|
|
delay_logging_enabled_(false),
|
|
|
|
|
extended_filter_enabled_(false),
|
2015-11-16 16:27:42 -08:00
|
|
|
delay_agnostic_enabled_(false),
|
2016-03-07 16:59:39 -08:00
|
|
|
aec3_enabled_(false),
|
2015-11-28 12:35:15 -08:00
|
|
|
render_queue_element_max_size_(0) {
|
|
|
|
|
RTC_DCHECK(crit_render);
|
|
|
|
|
RTC_DCHECK(crit_capture);
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
EchoCancellationImpl::~EchoCancellationImpl() {}
|
|
|
|
|
|
|
|
|
|
int EchoCancellationImpl::ProcessRenderAudio(const AudioBuffer* audio) {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs_render(crit_render_);
|
2016-03-05 03:01:14 -08:00
|
|
|
if (!enabled_) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 09:34:24 -07:00
|
|
|
RTC_DCHECK(stream_properties_);
|
2016-03-05 03:01:14 -08:00
|
|
|
RTC_DCHECK_GE(160u, audio->num_frames_per_band());
|
2016-03-15 09:34:24 -07:00
|
|
|
RTC_DCHECK_EQ(audio->num_channels(),
|
|
|
|
|
stream_properties_->num_reverse_channels);
|
|
|
|
|
RTC_DCHECK_GE(cancellers_.size(), stream_properties_->num_output_channels *
|
|
|
|
|
audio->num_channels());
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-11-28 12:35:15 -08:00
|
|
|
int err = AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
// The ordering convention must be followed to pass to the correct AEC.
|
|
|
|
|
size_t handle_index = 0;
|
2015-11-16 16:27:42 -08:00
|
|
|
render_queue_buffer_.clear();
|
2016-03-15 09:34:24 -07:00
|
|
|
for (size_t i = 0; i < stream_properties_->num_output_channels; i++) {
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
for (size_t j = 0; j < audio->num_channels(); j++) {
|
2015-11-16 16:27:42 -08:00
|
|
|
// Retrieve any error code produced by the buffering of the farend
|
2016-03-05 03:01:14 -08:00
|
|
|
// signal.
|
2015-11-16 16:27:42 -08:00
|
|
|
err = WebRtcAec_GetBufferFarendError(
|
2016-03-07 22:50:14 -08:00
|
|
|
cancellers_[handle_index++]->state(),
|
|
|
|
|
audio->split_bands_const_f(j)[kBand0To8kHz],
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
audio->num_frames_per_band());
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-11-28 12:35:15 -08:00
|
|
|
if (err != AudioProcessing::kNoError) {
|
2015-11-09 23:53:50 -08:00
|
|
|
return MapError(err); // TODO(ajm): warning possible?
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-16 16:27:42 -08:00
|
|
|
// Buffer the samples in the render queue.
|
|
|
|
|
render_queue_buffer_.insert(render_queue_buffer_.end(),
|
|
|
|
|
audio->split_bands_const_f(j)[kBand0To8kHz],
|
|
|
|
|
(audio->split_bands_const_f(j)[kBand0To8kHz] +
|
|
|
|
|
audio->num_frames_per_band()));
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 16:27:42 -08:00
|
|
|
// Insert the samples into the queue.
|
|
|
|
|
if (!render_signal_queue_->Insert(&render_queue_buffer_)) {
|
2015-11-28 12:35:15 -08:00
|
|
|
// The data queue is full and needs to be emptied.
|
2015-11-16 16:27:42 -08:00
|
|
|
ReadQueuedRenderData();
|
|
|
|
|
|
|
|
|
|
// Retry the insert (should always work).
|
|
|
|
|
RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-16 16:27:42 -08:00
|
|
|
// Read chunks of data that were received and queued on the render side from
|
|
|
|
|
// a queue. All the data chunks are buffered into the farend signal of the AEC.
|
|
|
|
|
void EchoCancellationImpl::ReadQueuedRenderData() {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
2016-03-05 03:01:14 -08:00
|
|
|
if (!enabled_) {
|
2015-11-16 16:27:42 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-15 09:34:24 -07:00
|
|
|
RTC_DCHECK(stream_properties_);
|
2015-11-16 16:27:42 -08:00
|
|
|
while (render_signal_queue_->Remove(&capture_queue_buffer_)) {
|
|
|
|
|
size_t handle_index = 0;
|
Misc. small cleanups.
* Better param names
* Avoid using negative values for (bogus) placeholder channel counts (mostly in tests). Since channels will be changing to size_t, negative values will be illegal; it's sufficient to use 0 in these cases.
* Use arraysize()
* Use size_t for counting frames, samples, blocks, buffers, and bytes -- most of these are already size_t in most places, this just fixes some stragglers
* reinterpret_cast<int64_t>(void*) is not necessarily safe; use uintptr_t instead
* Remove unnecessary code, e.g. dead code, needlessly long/repetitive code, or function overrides that exactly match the base definition
* Fix indenting
* Use uint32_t for timestamps (matching how it's already a uint32_t in most places)
* Spelling
* RTC_CHECK_EQ(expected, actual)
* Rewrap
* Use .empty()
* Be more pedantic about matching int/int32_t/
* Remove pointless consts on input parameters to functions
* Add missing sanity checks
All this was found in the course of constructing https://codereview.webrtc.org/1316523002/ , and is being landed separately first.
BUG=none
TEST=none
Review URL: https://codereview.webrtc.org/1534193008
Cr-Commit-Position: refs/heads/master@{#11191}
2016-01-08 13:50:27 -08:00
|
|
|
size_t buffer_index = 0;
|
|
|
|
|
const size_t num_frames_per_band =
|
2015-11-16 16:27:42 -08:00
|
|
|
capture_queue_buffer_.size() /
|
2016-03-15 09:34:24 -07:00
|
|
|
(stream_properties_->num_output_channels *
|
|
|
|
|
stream_properties_->num_reverse_channels);
|
|
|
|
|
for (size_t i = 0; i < stream_properties_->num_output_channels; i++) {
|
|
|
|
|
for (size_t j = 0; j < stream_properties_->num_reverse_channels; j++) {
|
2016-03-07 22:50:14 -08:00
|
|
|
WebRtcAec_BufferFarend(cancellers_[handle_index++]->state(),
|
|
|
|
|
&capture_queue_buffer_[buffer_index],
|
2015-11-16 16:27:42 -08:00
|
|
|
num_frames_per_band);
|
|
|
|
|
|
|
|
|
|
buffer_index += num_frames_per_band;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-15 09:34:24 -07:00
|
|
|
int EchoCancellationImpl::ProcessCaptureAudio(AudioBuffer* audio,
|
|
|
|
|
int stream_delay_ms) {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
2016-03-05 03:01:14 -08:00
|
|
|
if (!enabled_) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (drift_compensation_enabled_ && !was_stream_drift_set_) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kStreamParameterNotSetError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 09:34:24 -07:00
|
|
|
RTC_DCHECK(stream_properties_);
|
2016-03-05 03:01:14 -08:00
|
|
|
RTC_DCHECK_GE(160u, audio->num_frames_per_band());
|
2016-03-15 09:34:24 -07:00
|
|
|
RTC_DCHECK_EQ(audio->num_channels(), stream_properties_->num_proc_channels);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-11-28 12:35:15 -08:00
|
|
|
int err = AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
// The ordering convention must be followed to pass to the correct AEC.
|
|
|
|
|
size_t handle_index = 0;
|
|
|
|
|
stream_has_echo_ = false;
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
for (size_t i = 0; i < audio->num_channels(); i++) {
|
2016-03-15 09:34:24 -07:00
|
|
|
for (size_t j = 0; j < stream_properties_->num_reverse_channels; j++) {
|
|
|
|
|
err = WebRtcAec_Process(
|
|
|
|
|
cancellers_[handle_index]->state(), audio->split_bands_const_f(i),
|
|
|
|
|
audio->num_bands(), audio->split_bands_f(i),
|
|
|
|
|
audio->num_frames_per_band(), stream_delay_ms, stream_drift_samples_);
|
2015-11-28 12:35:15 -08:00
|
|
|
|
|
|
|
|
if (err != AudioProcessing::kNoError) {
|
2015-11-09 23:53:50 -08:00
|
|
|
err = MapError(err);
|
2011-07-07 08:21:25 +00:00
|
|
|
// TODO(ajm): Figure out how to return warnings properly.
|
2015-11-28 12:35:15 -08:00
|
|
|
if (err != AudioProcessing::kBadStreamParameterWarning) {
|
2011-07-07 08:21:25 +00:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-15 17:01:03 +00:00
|
|
|
int status = 0;
|
2016-03-07 22:50:14 -08:00
|
|
|
err = WebRtcAec_get_echo_status(cancellers_[handle_index]->state(),
|
|
|
|
|
&status);
|
2015-11-28 12:35:15 -08:00
|
|
|
if (err != AudioProcessing::kNoError) {
|
2015-11-09 23:53:50 -08:00
|
|
|
return MapError(err);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status == 1) {
|
|
|
|
|
stream_has_echo_ = true;
|
|
|
|
|
}
|
2016-03-07 22:50:14 -08:00
|
|
|
|
|
|
|
|
handle_index++;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
was_stream_drift_set_ = false;
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EchoCancellationImpl::Enable(bool enable) {
|
2015-11-28 12:35:15 -08:00
|
|
|
// Run in a single-threaded manner.
|
|
|
|
|
rtc::CritScope cs_render(crit_render_);
|
|
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-03-05 03:01:14 -08:00
|
|
|
if (enable && !enabled_) {
|
|
|
|
|
enabled_ = enable; // Must be set before Initialize() is called.
|
2016-03-15 09:34:24 -07:00
|
|
|
|
|
|
|
|
// TODO(peah): Simplify once the Enable function has been removed from
|
|
|
|
|
// the public APM API.
|
|
|
|
|
RTC_DCHECK(stream_properties_);
|
|
|
|
|
Initialize(stream_properties_->sample_rate_hz,
|
|
|
|
|
stream_properties_->num_reverse_channels,
|
|
|
|
|
stream_properties_->num_output_channels,
|
|
|
|
|
stream_properties_->num_proc_channels);
|
2016-03-05 03:01:14 -08:00
|
|
|
} else {
|
|
|
|
|
enabled_ = enable;
|
|
|
|
|
}
|
|
|
|
|
return AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-04-06 09:30:58 -07:00
|
|
|
bool EchoCancellationImpl::is_enabled_render_side_query() const {
|
|
|
|
|
// TODO(peah): Add threadchecker.
|
|
|
|
|
rtc::CritScope cs_render(crit_render_);
|
|
|
|
|
return enabled_;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-07 08:21:25 +00:00
|
|
|
bool EchoCancellationImpl::is_enabled() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2016-03-05 03:01:14 -08:00
|
|
|
return enabled_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EchoCancellationImpl::set_suppression_level(SuppressionLevel level) {
|
2015-11-28 12:35:15 -08:00
|
|
|
{
|
|
|
|
|
if (MapSetting(level) == -1) {
|
|
|
|
|
return AudioProcessing::kBadParameterError;
|
|
|
|
|
}
|
|
|
|
|
rtc::CritScope cs(crit_capture_);
|
|
|
|
|
suppression_level_ = level;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
return Configure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EchoCancellation::SuppressionLevel EchoCancellationImpl::suppression_level()
|
|
|
|
|
const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
return suppression_level_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EchoCancellationImpl::enable_drift_compensation(bool enable) {
|
2015-11-28 12:35:15 -08:00
|
|
|
{
|
|
|
|
|
rtc::CritScope cs(crit_capture_);
|
|
|
|
|
drift_compensation_enabled_ = enable;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
return Configure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EchoCancellationImpl::is_drift_compensation_enabled() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
return drift_compensation_enabled_;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-01 18:47:28 +00:00
|
|
|
void EchoCancellationImpl::set_stream_drift_samples(int drift) {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
was_stream_drift_set_ = true;
|
|
|
|
|
stream_drift_samples_ = drift;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EchoCancellationImpl::stream_drift_samples() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
return stream_drift_samples_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EchoCancellationImpl::enable_metrics(bool enable) {
|
2015-11-28 12:35:15 -08:00
|
|
|
{
|
|
|
|
|
rtc::CritScope cs(crit_capture_);
|
|
|
|
|
metrics_enabled_ = enable;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
return Configure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EchoCancellationImpl::are_metrics_enabled() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
return metrics_enabled_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO(ajm): we currently just use the metrics from the first AEC. Think more
|
|
|
|
|
// aboue the best way to extend this to multi-channel.
|
|
|
|
|
int EchoCancellationImpl::GetMetrics(Metrics* metrics) {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
if (metrics == NULL) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNullPointerError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-05 03:01:14 -08:00
|
|
|
if (!enabled_ || !metrics_enabled_) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNotEnabledError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AecMetrics my_metrics;
|
|
|
|
|
memset(&my_metrics, 0, sizeof(my_metrics));
|
|
|
|
|
memset(metrics, 0, sizeof(Metrics));
|
|
|
|
|
|
2016-03-07 22:50:14 -08:00
|
|
|
const int err = WebRtcAec_GetMetrics(cancellers_[0]->state(), &my_metrics);
|
2015-11-28 12:35:15 -08:00
|
|
|
if (err != AudioProcessing::kNoError) {
|
2015-11-09 23:53:50 -08:00
|
|
|
return MapError(err);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metrics->residual_echo_return_loss.instant = my_metrics.rerl.instant;
|
|
|
|
|
metrics->residual_echo_return_loss.average = my_metrics.rerl.average;
|
|
|
|
|
metrics->residual_echo_return_loss.maximum = my_metrics.rerl.max;
|
|
|
|
|
metrics->residual_echo_return_loss.minimum = my_metrics.rerl.min;
|
|
|
|
|
|
|
|
|
|
metrics->echo_return_loss.instant = my_metrics.erl.instant;
|
|
|
|
|
metrics->echo_return_loss.average = my_metrics.erl.average;
|
|
|
|
|
metrics->echo_return_loss.maximum = my_metrics.erl.max;
|
|
|
|
|
metrics->echo_return_loss.minimum = my_metrics.erl.min;
|
|
|
|
|
|
|
|
|
|
metrics->echo_return_loss_enhancement.instant = my_metrics.erle.instant;
|
|
|
|
|
metrics->echo_return_loss_enhancement.average = my_metrics.erle.average;
|
|
|
|
|
metrics->echo_return_loss_enhancement.maximum = my_metrics.erle.max;
|
|
|
|
|
metrics->echo_return_loss_enhancement.minimum = my_metrics.erle.min;
|
|
|
|
|
|
|
|
|
|
metrics->a_nlp.instant = my_metrics.aNlp.instant;
|
|
|
|
|
metrics->a_nlp.average = my_metrics.aNlp.average;
|
|
|
|
|
metrics->a_nlp.maximum = my_metrics.aNlp.max;
|
|
|
|
|
metrics->a_nlp.minimum = my_metrics.aNlp.min;
|
|
|
|
|
|
2016-04-07 06:36:43 -07:00
|
|
|
metrics->divergent_filter_fraction = my_metrics.divergent_filter_fraction;
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EchoCancellationImpl::stream_has_echo() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
return stream_has_echo_;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-03 08:18:10 +00:00
|
|
|
int EchoCancellationImpl::enable_delay_logging(bool enable) {
|
2015-11-28 12:35:15 -08:00
|
|
|
{
|
|
|
|
|
rtc::CritScope cs(crit_capture_);
|
|
|
|
|
delay_logging_enabled_ = enable;
|
|
|
|
|
}
|
2011-10-03 08:18:10 +00:00
|
|
|
return Configure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EchoCancellationImpl::is_delay_logging_enabled() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-10-03 08:18:10 +00:00
|
|
|
return delay_logging_enabled_;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 00:39:14 +02:00
|
|
|
bool EchoCancellationImpl::is_delay_agnostic_enabled() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2015-10-03 00:39:14 +02:00
|
|
|
return delay_agnostic_enabled_;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 16:59:39 -08:00
|
|
|
bool EchoCancellationImpl::is_aec3_enabled() const {
|
2016-02-17 01:11:16 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2016-03-07 16:59:39 -08:00
|
|
|
return aec3_enabled_;
|
2016-02-17 01:11:16 -08:00
|
|
|
}
|
|
|
|
|
|
2016-04-15 01:19:44 -07:00
|
|
|
std::string EchoCancellationImpl::GetExperimentsDescription() {
|
|
|
|
|
rtc::CritScope cs(crit_capture_);
|
|
|
|
|
return aec3_enabled_ ? "AEC3" : "";
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-03 00:39:14 +02:00
|
|
|
bool EchoCancellationImpl::is_extended_filter_enabled() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2015-10-03 00:39:14 +02:00
|
|
|
return extended_filter_enabled_;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-03 08:18:10 +00:00
|
|
|
// TODO(bjornv): How should we handle the multi-channel case?
|
|
|
|
|
int EchoCancellationImpl::GetDelayMetrics(int* median, int* std) {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2015-02-03 06:06:26 +00:00
|
|
|
float fraction_poor_delays = 0;
|
|
|
|
|
return GetDelayMetrics(median, std, &fraction_poor_delays);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EchoCancellationImpl::GetDelayMetrics(int* median, int* std,
|
|
|
|
|
float* fraction_poor_delays) {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-10-03 08:18:10 +00:00
|
|
|
if (median == NULL) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNullPointerError;
|
2011-10-03 08:18:10 +00:00
|
|
|
}
|
|
|
|
|
if (std == NULL) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNullPointerError;
|
2011-10-03 08:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-05 03:01:14 -08:00
|
|
|
if (!enabled_ || !delay_logging_enabled_) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNotEnabledError;
|
2011-10-03 08:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:50:14 -08:00
|
|
|
const int err = WebRtcAec_GetDelayMetrics(cancellers_[0]->state(), median,
|
|
|
|
|
std, fraction_poor_delays);
|
2015-11-28 12:35:15 -08:00
|
|
|
if (err != AudioProcessing::kNoError) {
|
2015-11-09 23:53:50 -08:00
|
|
|
return MapError(err);
|
2011-10-03 08:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNoError;
|
2011-10-03 08:18:10 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-05 16:53:09 +00:00
|
|
|
struct AecCore* EchoCancellationImpl::aec_core() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2016-03-05 03:01:14 -08:00
|
|
|
if (!enabled_) {
|
2013-03-05 16:53:09 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2016-03-07 22:50:14 -08:00
|
|
|
return WebRtcAec_aec_core(cancellers_[0]->state());
|
2013-03-05 16:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 09:34:24 -07:00
|
|
|
void EchoCancellationImpl::Initialize(int sample_rate_hz,
|
|
|
|
|
size_t num_reverse_channels,
|
|
|
|
|
size_t num_output_channels,
|
|
|
|
|
size_t num_proc_channels) {
|
2016-03-05 03:01:14 -08:00
|
|
|
rtc::CritScope cs_render(crit_render_);
|
|
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
2016-03-15 09:34:24 -07:00
|
|
|
|
|
|
|
|
stream_properties_.reset(
|
|
|
|
|
new StreamProperties(sample_rate_hz, num_reverse_channels,
|
|
|
|
|
num_output_channels, num_proc_channels));
|
|
|
|
|
|
2016-03-05 03:01:14 -08:00
|
|
|
if (!enabled_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-15 09:34:24 -07:00
|
|
|
if (NumCancellersRequired() > cancellers_.size()) {
|
2016-03-05 03:01:14 -08:00
|
|
|
const size_t cancellers_old_size = cancellers_.size();
|
2016-03-15 09:34:24 -07:00
|
|
|
cancellers_.resize(NumCancellersRequired());
|
2016-03-05 03:01:14 -08:00
|
|
|
|
|
|
|
|
for (size_t i = cancellers_old_size; i < cancellers_.size(); ++i) {
|
2016-03-07 22:50:14 -08:00
|
|
|
cancellers_[i].reset(new Canceller());
|
2015-11-28 12:35:15 -08:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-07 22:50:14 -08:00
|
|
|
for (auto& canceller : cancellers_) {
|
|
|
|
|
canceller->Initialize(sample_rate_hz);
|
2016-03-05 03:01:14 -08:00
|
|
|
}
|
2016-03-04 12:29:03 -08:00
|
|
|
|
2016-03-05 03:01:14 -08:00
|
|
|
Configure();
|
|
|
|
|
|
|
|
|
|
AllocateRenderQueue();
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-04 11:50:54 -08:00
|
|
|
int EchoCancellationImpl::GetSystemDelayInSamples() const {
|
|
|
|
|
rtc::CritScope cs(crit_capture_);
|
2016-03-05 03:01:14 -08:00
|
|
|
RTC_DCHECK(enabled_);
|
2016-03-04 11:50:54 -08:00
|
|
|
// Report the delay for the first AEC component.
|
|
|
|
|
return WebRtcAec_system_delay(
|
2016-03-05 03:01:14 -08:00
|
|
|
WebRtcAec_aec_core(cancellers_[0]->state()));
|
2016-03-04 11:50:54 -08:00
|
|
|
}
|
|
|
|
|
|
2015-11-16 16:27:42 -08:00
|
|
|
void EchoCancellationImpl::AllocateRenderQueue() {
|
|
|
|
|
const size_t new_render_queue_element_max_size = std::max<size_t>(
|
2015-11-18 06:11:13 -08:00
|
|
|
static_cast<size_t>(1),
|
2016-03-15 09:34:24 -07:00
|
|
|
kMaxAllowedValuesOfSamplesPerFrame * NumCancellersRequired());
|
2015-11-16 16:27:42 -08:00
|
|
|
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs_render(crit_render_);
|
|
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
|
|
|
|
|
2015-11-16 16:27:42 -08:00
|
|
|
// Reallocate the queue if the queue item size is too small to fit the
|
|
|
|
|
// data to put in the queue.
|
2015-11-18 06:11:13 -08:00
|
|
|
if (render_queue_element_max_size_ < new_render_queue_element_max_size) {
|
2015-11-16 16:27:42 -08:00
|
|
|
render_queue_element_max_size_ = new_render_queue_element_max_size;
|
|
|
|
|
|
|
|
|
|
std::vector<float> template_queue_element(render_queue_element_max_size_);
|
|
|
|
|
|
|
|
|
|
render_signal_queue_.reset(
|
|
|
|
|
new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>(
|
|
|
|
|
kMaxNumFramesToBuffer, template_queue_element,
|
|
|
|
|
RenderQueueItemVerifier<float>(render_queue_element_max_size_)));
|
2015-11-18 06:11:13 -08:00
|
|
|
|
|
|
|
|
render_queue_buffer_.resize(render_queue_element_max_size_);
|
|
|
|
|
capture_queue_buffer_.resize(render_queue_element_max_size_);
|
2015-11-16 16:27:42 -08:00
|
|
|
} else {
|
|
|
|
|
render_signal_queue_->Clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-25 23:17:38 +00:00
|
|
|
void EchoCancellationImpl::SetExtraOptions(const Config& config) {
|
2015-11-28 12:35:15 -08:00
|
|
|
{
|
|
|
|
|
rtc::CritScope cs(crit_capture_);
|
|
|
|
|
extended_filter_enabled_ = config.Get<ExtendedFilter>().enabled;
|
|
|
|
|
delay_agnostic_enabled_ = config.Get<DelayAgnostic>().enabled;
|
2016-03-07 16:59:39 -08:00
|
|
|
aec3_enabled_ = config.Get<EchoCanceller3>().enabled;
|
2015-11-28 12:35:15 -08:00
|
|
|
}
|
2013-09-25 23:17:38 +00:00
|
|
|
Configure();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-05 03:01:14 -08:00
|
|
|
int EchoCancellationImpl::Configure() {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs_render(crit_render_);
|
|
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
AecConfig config;
|
|
|
|
|
config.metricsMode = metrics_enabled_;
|
|
|
|
|
config.nlpMode = MapSetting(suppression_level_);
|
|
|
|
|
config.skewMode = drift_compensation_enabled_;
|
2011-10-03 08:18:10 +00:00
|
|
|
config.delay_logging = delay_logging_enabled_;
|
2016-03-05 03:01:14 -08:00
|
|
|
|
|
|
|
|
int error = AudioProcessing::kNoError;
|
2016-03-07 22:50:14 -08:00
|
|
|
for (auto& canceller : cancellers_) {
|
|
|
|
|
WebRtcAec_enable_extended_filter(WebRtcAec_aec_core(canceller->state()),
|
2016-03-05 03:01:14 -08:00
|
|
|
extended_filter_enabled_ ? 1 : 0);
|
2016-03-07 22:50:14 -08:00
|
|
|
WebRtcAec_enable_delay_agnostic(WebRtcAec_aec_core(canceller->state()),
|
2016-03-05 03:01:14 -08:00
|
|
|
delay_agnostic_enabled_ ? 1 : 0);
|
2016-03-07 22:50:14 -08:00
|
|
|
WebRtcAec_enable_aec3(WebRtcAec_aec_core(canceller->state()),
|
|
|
|
|
aec3_enabled_ ? 1 : 0);
|
|
|
|
|
const int handle_error = WebRtcAec_set_config(canceller->state(), config);
|
2016-03-05 03:01:14 -08:00
|
|
|
if (handle_error != AudioProcessing::kNoError) {
|
|
|
|
|
error = AudioProcessing::kNoError;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return error;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 09:34:24 -07:00
|
|
|
size_t EchoCancellationImpl::NumCancellersRequired() const {
|
|
|
|
|
RTC_DCHECK(stream_properties_);
|
|
|
|
|
return stream_properties_->num_output_channels *
|
|
|
|
|
stream_properties_->num_reverse_channels;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|