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-03-01 16:36:19 +00:00
|
|
|
#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2013-08-05 16:22:53 +00:00
|
|
|
#include <string.h>
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-04-26 08:14:39 -07:00
|
|
|
#include "webrtc/base/constructormagic.h"
|
2015-11-11 20:16:11 +01:00
|
|
|
#include "webrtc/modules/audio_processing/aecm/echo_control_mobile.h"
|
2013-03-01 16:36:19 +00:00
|
|
|
#include "webrtc/modules/audio_processing/audio_buffer.h"
|
2015-10-28 18:17:40 +01:00
|
|
|
#include "webrtc/system_wrappers/include/logging.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
|
|
namespace {
|
2013-04-10 07:50:54 +00:00
|
|
|
int16_t MapSetting(EchoControlMobile::RoutingMode mode) {
|
2011-07-07 08:21:25 +00:00
|
|
|
switch (mode) {
|
|
|
|
|
case EchoControlMobile::kQuietEarpieceOrHeadset:
|
|
|
|
|
return 0;
|
|
|
|
|
case EchoControlMobile::kEarpiece:
|
|
|
|
|
return 1;
|
|
|
|
|
case EchoControlMobile::kLoudEarpiece:
|
|
|
|
|
return 2;
|
|
|
|
|
case EchoControlMobile::kSpeakerphone:
|
|
|
|
|
return 3;
|
|
|
|
|
case EchoControlMobile::kLoudSpeakerphone:
|
|
|
|
|
return 4;
|
|
|
|
|
}
|
2016-03-10 12:54:25 -08:00
|
|
|
RTC_DCHECK(false);
|
2012-02-06 11:06:01 +00:00
|
|
|
return -1;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-16 16:27:42 -08:00
|
|
|
AudioProcessing::Error MapError(int err) {
|
|
|
|
|
switch (err) {
|
|
|
|
|
case AECM_UNSUPPORTED_FUNCTION_ERROR:
|
|
|
|
|
return AudioProcessing::kUnsupportedFunctionError;
|
|
|
|
|
case AECM_NULL_POINTER_ERROR:
|
|
|
|
|
return AudioProcessing::kNullPointerError;
|
|
|
|
|
case AECM_BAD_PARAMETER_ERROR:
|
|
|
|
|
return AudioProcessing::kBadParameterError;
|
|
|
|
|
case AECM_BAD_PARAMETER_WARNING:
|
|
|
|
|
return AudioProcessing::kBadStreamParameterWarning;
|
|
|
|
|
default:
|
|
|
|
|
// AECM_UNSPECIFIED_ERROR
|
|
|
|
|
// AECM_UNINITIALIZED_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;
|
2011-07-07 08:21:25 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
2011-07-18 18:03:01 +00:00
|
|
|
size_t EchoControlMobile::echo_path_size_bytes() {
|
2016-03-10 12:54:25 -08:00
|
|
|
return WebRtcAecm_echo_path_size_bytes();
|
2011-07-18 18:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 04:32:28 -07:00
|
|
|
struct EchoControlMobileImpl::StreamProperties {
|
|
|
|
|
StreamProperties() = delete;
|
|
|
|
|
StreamProperties(int sample_rate_hz,
|
|
|
|
|
size_t num_reverse_channels,
|
|
|
|
|
size_t num_output_channels)
|
|
|
|
|
: sample_rate_hz(sample_rate_hz),
|
|
|
|
|
num_reverse_channels(num_reverse_channels),
|
|
|
|
|
num_output_channels(num_output_channels) {}
|
|
|
|
|
|
|
|
|
|
int sample_rate_hz;
|
|
|
|
|
size_t num_reverse_channels;
|
|
|
|
|
size_t num_output_channels;
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-10 12:54:25 -08:00
|
|
|
class EchoControlMobileImpl::Canceller {
|
|
|
|
|
public:
|
|
|
|
|
Canceller() {
|
|
|
|
|
state_ = WebRtcAecm_Create();
|
|
|
|
|
RTC_CHECK(state_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~Canceller() {
|
|
|
|
|
RTC_DCHECK(state_);
|
|
|
|
|
WebRtcAecm_Free(state_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* state() {
|
|
|
|
|
RTC_DCHECK(state_);
|
|
|
|
|
return state_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Initialize(int sample_rate_hz,
|
|
|
|
|
unsigned char* external_echo_path,
|
|
|
|
|
size_t echo_path_size_bytes) {
|
|
|
|
|
RTC_DCHECK(state_);
|
|
|
|
|
int error = WebRtcAecm_Init(state_, sample_rate_hz);
|
|
|
|
|
RTC_DCHECK_EQ(AudioProcessing::kNoError, error);
|
|
|
|
|
if (external_echo_path != NULL) {
|
|
|
|
|
error = WebRtcAecm_InitEchoPath(state_, external_echo_path,
|
|
|
|
|
echo_path_size_bytes);
|
|
|
|
|
RTC_DCHECK_EQ(AudioProcessing::kNoError, error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void* state_;
|
|
|
|
|
RTC_DISALLOW_COPY_AND_ASSIGN(Canceller);
|
|
|
|
|
};
|
|
|
|
|
|
2016-03-15 04:32:28 -07:00
|
|
|
EchoControlMobileImpl::EchoControlMobileImpl(rtc::CriticalSection* crit_render,
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CriticalSection* crit_capture)
|
2016-03-15 04:32:28 -07:00
|
|
|
: crit_render_(crit_render),
|
2015-11-28 12:35:15 -08:00
|
|
|
crit_capture_(crit_capture),
|
2015-11-16 16:27:42 -08:00
|
|
|
routing_mode_(kSpeakerphone),
|
|
|
|
|
comfort_noise_enabled_(true),
|
|
|
|
|
external_echo_path_(NULL),
|
2015-11-28 12:35:15 -08:00
|
|
|
render_queue_element_max_size_(0) {
|
|
|
|
|
RTC_DCHECK(crit_render);
|
|
|
|
|
RTC_DCHECK(crit_capture);
|
|
|
|
|
}
|
2011-07-13 08:09:56 +00:00
|
|
|
|
|
|
|
|
EchoControlMobileImpl::~EchoControlMobileImpl() {
|
|
|
|
|
if (external_echo_path_ != NULL) {
|
|
|
|
|
delete [] external_echo_path_;
|
|
|
|
|
external_echo_path_ = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
int EchoControlMobileImpl::ProcessRenderAudio(const AudioBuffer* audio) {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs_render(crit_render_);
|
2016-03-10 12:54:25 -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 04:32:28 -07:00
|
|
|
RTC_DCHECK(stream_properties_);
|
2016-03-10 12:54:25 -08:00
|
|
|
RTC_DCHECK_GE(160u, audio->num_frames_per_band());
|
2016-03-15 04:32:28 -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 AECM.
|
2015-11-16 16:27:42 -08:00
|
|
|
render_queue_buffer_.clear();
|
2016-03-10 12:54:25 -08:00
|
|
|
int render_channel = 0;
|
|
|
|
|
for (auto& canceller : cancellers_) {
|
|
|
|
|
err = WebRtcAecm_GetBufferFarendError(
|
|
|
|
|
canceller->state(),
|
|
|
|
|
audio->split_bands_const(render_channel)[kBand0To8kHz],
|
|
|
|
|
audio->num_frames_per_band());
|
|
|
|
|
|
|
|
|
|
if (err != AudioProcessing::kNoError)
|
|
|
|
|
return MapError(err); // TODO(ajm): warning possible?);
|
|
|
|
|
|
|
|
|
|
// Buffer the samples in the render queue.
|
|
|
|
|
render_queue_buffer_.insert(
|
|
|
|
|
render_queue_buffer_.end(),
|
|
|
|
|
audio->split_bands_const(render_channel)[kBand0To8kHz],
|
|
|
|
|
(audio->split_bands_const(render_channel)[kBand0To8kHz] +
|
|
|
|
|
audio->num_frames_per_band()));
|
|
|
|
|
render_channel = (render_channel + 1) % audio->num_channels();
|
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).
|
2016-10-14 03:23:32 -07:00
|
|
|
bool result = render_signal_queue_->Insert(&render_queue_buffer_);
|
|
|
|
|
RTC_DCHECK(result);
|
2015-11-16 16:27:42 -08:00
|
|
|
}
|
|
|
|
|
|
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 EchoControlMobileImpl::ReadQueuedRenderData() {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
2016-03-15 04:32:28 -07:00
|
|
|
RTC_DCHECK(stream_properties_);
|
2015-11-28 12:35:15 -08:00
|
|
|
|
2016-03-10 12:54:25 -08:00
|
|
|
if (!enabled_) {
|
2015-11-16 16:27:42 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (render_signal_queue_->Remove(&capture_queue_buffer_)) {
|
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;
|
2016-03-15 04:32:28 -07:00
|
|
|
size_t num_frames_per_band = capture_queue_buffer_.size() /
|
|
|
|
|
(stream_properties_->num_output_channels *
|
|
|
|
|
stream_properties_->num_reverse_channels);
|
2016-03-10 12:54:25 -08:00
|
|
|
|
|
|
|
|
for (auto& canceller : cancellers_) {
|
|
|
|
|
WebRtcAecm_BufferFarend(canceller->state(),
|
|
|
|
|
&capture_queue_buffer_[buffer_index],
|
|
|
|
|
num_frames_per_band);
|
|
|
|
|
|
|
|
|
|
buffer_index += num_frames_per_band;
|
2015-11-16 16:27:42 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-15 04:32:28 -07:00
|
|
|
int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio,
|
|
|
|
|
int stream_delay_ms) {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
2016-03-10 12:54:25 -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 04:32:28 -07:00
|
|
|
RTC_DCHECK(stream_properties_);
|
2016-03-10 12:54:25 -08:00
|
|
|
RTC_DCHECK_GE(160u, audio->num_frames_per_band());
|
2016-03-15 04:32:28 -07:00
|
|
|
RTC_DCHECK_EQ(audio->num_channels(), stream_properties_->num_output_channels);
|
|
|
|
|
RTC_DCHECK_GE(cancellers_.size(), stream_properties_->num_reverse_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 AECM.
|
|
|
|
|
size_t handle_index = 0;
|
2016-03-10 12:54:25 -08:00
|
|
|
for (size_t capture = 0; capture < audio->num_channels(); ++capture) {
|
2011-07-07 08:21:25 +00:00
|
|
|
// TODO(ajm): improve how this works, possibly inside AECM.
|
|
|
|
|
// This is kind of hacked up.
|
2016-03-10 12:54:25 -08:00
|
|
|
const int16_t* noisy = audio->low_pass_reference(capture);
|
|
|
|
|
const int16_t* clean = audio->split_bands_const(capture)[kBand0To8kHz];
|
2011-07-07 08:21:25 +00:00
|
|
|
if (noisy == NULL) {
|
|
|
|
|
noisy = clean;
|
|
|
|
|
clean = NULL;
|
|
|
|
|
}
|
2016-03-15 04:32:28 -07:00
|
|
|
for (size_t render = 0; render < stream_properties_->num_reverse_channels;
|
|
|
|
|
++render) {
|
2016-03-10 12:54:25 -08:00
|
|
|
err = WebRtcAecm_Process(cancellers_[handle_index]->state(), noisy, clean,
|
|
|
|
|
audio->split_bands(capture)[kBand0To8kHz],
|
2016-03-15 04:32:28 -07:00
|
|
|
audio->num_frames_per_band(), stream_delay_ms);
|
2016-03-10 12:54:25 -08:00
|
|
|
|
|
|
|
|
if (err != AudioProcessing::kNoError) {
|
2015-11-16 16:27:42 -08:00
|
|
|
return MapError(err);
|
2016-03-10 12:54:25 -08:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-03-10 12:54:25 -08:00
|
|
|
++handle_index;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2016-03-15 14:04:58 -07:00
|
|
|
for (size_t band = 1u; band < audio->num_bands(); ++band) {
|
|
|
|
|
memset(audio->split_bands(capture)[band],
|
|
|
|
|
0,
|
|
|
|
|
audio->num_frames_per_band() *
|
|
|
|
|
sizeof(audio->split_bands(capture)[band][0]));
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EchoControlMobileImpl::Enable(bool enable) {
|
|
|
|
|
// Ensure AEC and AECM are not both enabled.
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs_render(crit_render_);
|
|
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
2016-03-15 04:32:28 -07:00
|
|
|
RTC_DCHECK(stream_properties_);
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2016-03-10 12:54:25 -08:00
|
|
|
if (enable &&
|
2016-03-15 04:32:28 -07:00
|
|
|
stream_properties_->sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
|
2016-03-10 12:54:25 -08:00
|
|
|
return AudioProcessing::kBadSampleRateError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (enable && !enabled_) {
|
|
|
|
|
enabled_ = enable; // Must be set before Initialize() is called.
|
2016-03-15 04:32:28 -07:00
|
|
|
|
|
|
|
|
// TODO(peah): Simplify once the Enable function has been removed from
|
|
|
|
|
// the public APM API.
|
|
|
|
|
Initialize(stream_properties_->sample_rate_hz,
|
|
|
|
|
stream_properties_->num_reverse_channels,
|
|
|
|
|
stream_properties_->num_output_channels);
|
2016-03-10 12:54:25 -08:00
|
|
|
} else {
|
|
|
|
|
enabled_ = enable;
|
|
|
|
|
}
|
|
|
|
|
return AudioProcessing::kNoError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EchoControlMobileImpl::is_enabled() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2016-03-10 12:54:25 -08:00
|
|
|
return enabled_;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EchoControlMobileImpl::set_routing_mode(RoutingMode mode) {
|
|
|
|
|
if (MapSetting(mode) == -1) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kBadParameterError;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2015-11-28 12:35:15 -08:00
|
|
|
{
|
|
|
|
|
rtc::CritScope cs(crit_capture_);
|
|
|
|
|
routing_mode_ = mode;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
return Configure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EchoControlMobile::RoutingMode EchoControlMobileImpl::routing_mode()
|
|
|
|
|
const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
return routing_mode_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EchoControlMobileImpl::enable_comfort_noise(bool enable) {
|
2015-11-28 12:35:15 -08:00
|
|
|
{
|
|
|
|
|
rtc::CritScope cs(crit_capture_);
|
|
|
|
|
comfort_noise_enabled_ = enable;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
return Configure();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EchoControlMobileImpl::is_comfort_noise_enabled() const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-07-07 08:21:25 +00:00
|
|
|
return comfort_noise_enabled_;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-13 08:09:56 +00:00
|
|
|
int EchoControlMobileImpl::SetEchoPath(const void* echo_path,
|
2011-07-18 18:03:01 +00:00
|
|
|
size_t size_bytes) {
|
2015-11-28 12:35:15 -08:00
|
|
|
{
|
|
|
|
|
rtc::CritScope cs_render(crit_render_);
|
|
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
|
|
|
|
if (echo_path == NULL) {
|
|
|
|
|
return AudioProcessing::kNullPointerError;
|
|
|
|
|
}
|
|
|
|
|
if (size_bytes != echo_path_size_bytes()) {
|
|
|
|
|
// Size mismatch
|
|
|
|
|
return AudioProcessing::kBadParameterError;
|
|
|
|
|
}
|
2011-07-13 08:09:56 +00:00
|
|
|
|
2015-11-28 12:35:15 -08:00
|
|
|
if (external_echo_path_ == NULL) {
|
|
|
|
|
external_echo_path_ = new unsigned char[size_bytes];
|
|
|
|
|
}
|
|
|
|
|
memcpy(external_echo_path_, echo_path, size_bytes);
|
2011-07-13 08:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 04:32:28 -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);
|
2016-03-10 12:54:25 -08:00
|
|
|
return AudioProcessing::kNoError;
|
2011-07-13 08:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int EchoControlMobileImpl::GetEchoPath(void* echo_path,
|
2011-07-18 18:03:01 +00:00
|
|
|
size_t size_bytes) const {
|
2015-11-28 12:35:15 -08:00
|
|
|
rtc::CritScope cs(crit_capture_);
|
2011-07-13 08:09:56 +00:00
|
|
|
if (echo_path == NULL) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNullPointerError;
|
2011-07-13 08:09:56 +00:00
|
|
|
}
|
2011-07-18 18:03:01 +00:00
|
|
|
if (size_bytes != echo_path_size_bytes()) {
|
2011-07-13 08:09:56 +00:00
|
|
|
// Size mismatch
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kBadParameterError;
|
2011-07-13 08:09:56 +00:00
|
|
|
}
|
2016-03-10 12:54:25 -08:00
|
|
|
if (!enabled_) {
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNotEnabledError;
|
2011-07-13 08:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the echo path from the first channel
|
2016-03-10 12:54:25 -08:00
|
|
|
int32_t err =
|
|
|
|
|
WebRtcAecm_GetEchoPath(cancellers_[0]->state(), echo_path, size_bytes);
|
|
|
|
|
if (err != 0) {
|
2015-11-16 16:27:42 -08:00
|
|
|
return MapError(err);
|
2016-03-10 12:54:25 -08:00
|
|
|
}
|
2011-07-13 08:09:56 +00:00
|
|
|
|
2015-11-28 12:35:15 -08:00
|
|
|
return AudioProcessing::kNoError;
|
2011-07-13 08:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 04:32:28 -07:00
|
|
|
void EchoControlMobileImpl::Initialize(int sample_rate_hz,
|
|
|
|
|
size_t num_reverse_channels,
|
|
|
|
|
size_t num_output_channels) {
|
2016-03-10 12:54:25 -08:00
|
|
|
rtc::CritScope cs_render(crit_render_);
|
|
|
|
|
rtc::CritScope cs_capture(crit_capture_);
|
2016-03-15 04:32:28 -07:00
|
|
|
|
|
|
|
|
stream_properties_.reset(new StreamProperties(
|
|
|
|
|
sample_rate_hz, num_reverse_channels, num_output_channels));
|
|
|
|
|
|
2016-03-10 12:54:25 -08:00
|
|
|
if (!enabled_) {
|
|
|
|
|
return;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-15 04:32:28 -07:00
|
|
|
if (stream_properties_->sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
|
2016-03-09 16:23:23 -08:00
|
|
|
LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates";
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 12:54:25 -08:00
|
|
|
cancellers_.resize(num_handles_required());
|
|
|
|
|
for (auto& canceller : cancellers_) {
|
|
|
|
|
if (!canceller) {
|
|
|
|
|
canceller.reset(new Canceller());
|
|
|
|
|
}
|
|
|
|
|
canceller->Initialize(sample_rate_hz, external_echo_path_,
|
|
|
|
|
echo_path_size_bytes());
|
2015-11-16 16:27:42 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 12:54:25 -08:00
|
|
|
Configure();
|
2015-11-16 16:27:42 -08:00
|
|
|
|
2016-03-10 12:54:25 -08:00
|
|
|
AllocateRenderQueue();
|
2015-11-16 16:27:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EchoControlMobileImpl::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),
|
|
|
|
|
kMaxAllowedValuesOfSamplesPerFrame * num_handles_required());
|
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<int16_t> template_queue_element(render_queue_element_max_size_);
|
|
|
|
|
|
|
|
|
|
render_signal_queue_.reset(
|
|
|
|
|
new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
|
|
|
|
|
kMaxNumFramesToBuffer, template_queue_element,
|
|
|
|
|
RenderQueueItemVerifier<int16_t>(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();
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-10 12:54:25 -08:00
|
|
|
int EchoControlMobileImpl::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
|
|
|
AecmConfig config;
|
|
|
|
|
config.cngMode = comfort_noise_enabled_;
|
|
|
|
|
config.echoMode = MapSetting(routing_mode_);
|
2016-03-10 12:54:25 -08:00
|
|
|
int error = AudioProcessing::kNoError;
|
|
|
|
|
for (auto& canceller : cancellers_) {
|
|
|
|
|
int handle_error = WebRtcAecm_set_config(canceller->state(), config);
|
|
|
|
|
if (handle_error != AudioProcessing::kNoError) {
|
|
|
|
|
error = handle_error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return error;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
size_t EchoControlMobileImpl::num_handles_required() const {
|
2016-03-15 04:32:28 -07:00
|
|
|
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
|