Update to current webrtc library

This is from the upstream library commit id
3326535126e435f1ba647885ce43a8f0f3d317eb, corresponding to Chromium
88.0.4290.1.
This commit is contained in:
Arun Raghavan
2020-10-12 18:08:02 -04:00
parent b1b02581d3
commit bcec8b0b21
859 changed files with 76187 additions and 49580 deletions

View File

@ -8,35 +8,35 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/modules/audio_processing/echo_control_mobile_impl.h"
#include "modules/audio_processing/echo_control_mobile_impl.h"
#include <assert.h>
#include <string.h>
#include "webrtc/modules/audio_processing/aecm/include/echo_control_mobile.h"
#include "webrtc/modules/audio_processing/audio_buffer.h"
#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
#include "webrtc/system_wrappers/include/logging.h"
#include <cstdint>
#include "modules/audio_processing/aecm/echo_control_mobile.h"
#include "modules/audio_processing/audio_buffer.h"
#include "modules/audio_processing/include/audio_processing.h"
#include "rtc_base/checks.h"
#include "rtc_base/constructor_magic.h"
namespace webrtc {
typedef void Handle;
namespace {
int16_t MapSetting(EchoControlMobile::RoutingMode mode) {
int16_t MapSetting(EchoControlMobileImpl::RoutingMode mode) {
switch (mode) {
case EchoControlMobile::kQuietEarpieceOrHeadset:
case EchoControlMobileImpl::kQuietEarpieceOrHeadset:
return 0;
case EchoControlMobile::kEarpiece:
case EchoControlMobileImpl::kEarpiece:
return 1;
case EchoControlMobile::kLoudEarpiece:
case EchoControlMobileImpl::kLoudEarpiece:
return 2;
case EchoControlMobile::kSpeakerphone:
case EchoControlMobileImpl::kSpeakerphone:
return 3;
case EchoControlMobile::kLoudSpeakerphone:
case EchoControlMobileImpl::kLoudSpeakerphone:
return 4;
}
assert(false);
RTC_NOTREACHED();
return -1;
}
@ -56,136 +56,183 @@ AudioProcessing::Error MapError(int err) {
return AudioProcessing::kUnspecifiedError;
}
}
} // namespace
size_t EchoControlMobile::echo_path_size_bytes() {
return WebRtcAecm_echo_path_size_bytes();
}
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) {}
EchoControlMobileImpl::EchoControlMobileImpl(const AudioProcessing* apm,
CriticalSectionWrapper* crit)
: ProcessingComponent(),
apm_(apm),
crit_(crit),
routing_mode_(kSpeakerphone),
comfort_noise_enabled_(true),
external_echo_path_(NULL) {}
int sample_rate_hz;
size_t num_reverse_channels;
size_t num_output_channels;
};
EchoControlMobileImpl::~EchoControlMobileImpl() {
if (external_echo_path_ != NULL) {
delete [] external_echo_path_;
external_echo_path_ = NULL;
}
}
int EchoControlMobileImpl::ProcessRenderAudio(const AudioBuffer* audio) {
if (!is_component_enabled()) {
return apm_->kNoError;
class EchoControlMobileImpl::Canceller {
public:
Canceller() {
state_ = WebRtcAecm_Create();
RTC_CHECK(state_);
}
assert(audio->num_frames_per_band() <= 160);
assert(audio->num_channels() == apm_->num_reverse_channels());
~Canceller() {
RTC_DCHECK(state_);
WebRtcAecm_Free(state_);
}
int err = apm_->kNoError;
void* state() {
RTC_DCHECK(state_);
return state_;
}
void Initialize(int sample_rate_hz) {
RTC_DCHECK(state_);
int error = WebRtcAecm_Init(state_, sample_rate_hz);
RTC_DCHECK_EQ(AudioProcessing::kNoError, error);
}
private:
void* state_;
RTC_DISALLOW_COPY_AND_ASSIGN(Canceller);
};
EchoControlMobileImpl::EchoControlMobileImpl()
: routing_mode_(kSpeakerphone), comfort_noise_enabled_(false) {}
EchoControlMobileImpl::~EchoControlMobileImpl() {}
void EchoControlMobileImpl::ProcessRenderAudio(
rtc::ArrayView<const int16_t> packed_render_audio) {
RTC_DCHECK(stream_properties_);
size_t buffer_index = 0;
size_t num_frames_per_band =
packed_render_audio.size() / (stream_properties_->num_output_channels *
stream_properties_->num_reverse_channels);
for (auto& canceller : cancellers_) {
WebRtcAecm_BufferFarend(canceller->state(),
&packed_render_audio[buffer_index],
num_frames_per_band);
buffer_index += num_frames_per_band;
}
}
void EchoControlMobileImpl::PackRenderAudioBuffer(
const AudioBuffer* audio,
size_t num_output_channels,
size_t num_channels,
std::vector<int16_t>* packed_buffer) {
RTC_DCHECK_GE(AudioBuffer::kMaxSplitFrameLength,
audio->num_frames_per_band());
RTC_DCHECK_EQ(num_channels, audio->num_channels());
// The ordering convention must be followed to pass to the correct AECM.
packed_buffer->clear();
int render_channel = 0;
for (size_t i = 0; i < num_output_channels; i++) {
for (size_t j = 0; j < audio->num_channels(); j++) {
std::array<int16_t, AudioBuffer::kMaxSplitFrameLength> data_to_buffer;
FloatS16ToS16(audio->split_bands_const(render_channel)[kBand0To8kHz],
audio->num_frames_per_band(), data_to_buffer.data());
// Buffer the samples in the render queue.
packed_buffer->insert(
packed_buffer->end(), data_to_buffer.data(),
data_to_buffer.data() + audio->num_frames_per_band());
render_channel = (render_channel + 1) % audio->num_channels();
}
}
}
size_t EchoControlMobileImpl::NumCancellersRequired(
size_t num_output_channels,
size_t num_reverse_channels) {
return num_output_channels * num_reverse_channels;
}
int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio,
int stream_delay_ms) {
RTC_DCHECK(stream_properties_);
RTC_DCHECK_GE(160, audio->num_frames_per_band());
RTC_DCHECK_EQ(audio->num_channels(), stream_properties_->num_output_channels);
RTC_DCHECK_GE(cancellers_.size(), stream_properties_->num_reverse_channels *
audio->num_channels());
int err = AudioProcessing::kNoError;
// The ordering convention must be followed to pass to the correct AECM.
size_t handle_index = 0;
for (int i = 0; i < apm_->num_output_channels(); i++) {
for (int j = 0; j < audio->num_channels(); j++) {
Handle* my_handle = static_cast<Handle*>(handle(handle_index));
err = WebRtcAecm_BufferFarend(
my_handle,
audio->split_bands_const(j)[kBand0To8kHz],
audio->num_frames_per_band());
if (err != apm_->kNoError) {
return GetHandleError(my_handle); // TODO(ajm): warning possible?
}
handle_index++;
}
}
return apm_->kNoError;
}
int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) {
if (!is_component_enabled()) {
return apm_->kNoError;
}
if (!apm_->was_stream_delay_set()) {
return apm_->kStreamParameterNotSetError;
}
assert(audio->num_frames_per_band() <= 160);
assert(audio->num_channels() == apm_->num_output_channels());
int err = apm_->kNoError;
// The ordering convention must be followed to pass to the correct AECM.
size_t handle_index = 0;
for (int i = 0; i < audio->num_channels(); i++) {
for (size_t capture = 0; capture < audio->num_channels(); ++capture) {
// TODO(ajm): improve how this works, possibly inside AECM.
// This is kind of hacked up.
const int16_t* noisy = audio->low_pass_reference(i);
const int16_t* clean = audio->split_bands_const(i)[kBand0To8kHz];
RTC_DCHECK_LT(capture, low_pass_reference_.size());
const int16_t* noisy =
reference_copied_ ? low_pass_reference_[capture].data() : nullptr;
RTC_DCHECK_GE(AudioBuffer::kMaxSplitFrameLength,
audio->num_frames_per_band());
std::array<int16_t, AudioBuffer::kMaxSplitFrameLength> split_bands_data;
int16_t* split_bands = split_bands_data.data();
const int16_t* clean = split_bands_data.data();
if (audio->split_bands(capture)[kBand0To8kHz]) {
FloatS16ToS16(audio->split_bands(capture)[kBand0To8kHz],
audio->num_frames_per_band(), split_bands_data.data());
} else {
clean = nullptr;
split_bands = nullptr;
}
if (noisy == NULL) {
noisy = clean;
clean = NULL;
}
for (int j = 0; j < apm_->num_reverse_channels(); j++) {
Handle* my_handle = static_cast<Handle*>(handle(handle_index));
err = WebRtcAecm_Process(
my_handle,
noisy,
clean,
audio->split_bands(i)[kBand0To8kHz],
audio->num_frames_per_band(),
apm_->stream_delay_ms());
for (size_t render = 0; render < stream_properties_->num_reverse_channels;
++render) {
err = WebRtcAecm_Process(cancellers_[handle_index]->state(), noisy, clean,
split_bands, audio->num_frames_per_band(),
stream_delay_ms);
if (err != apm_->kNoError) {
return GetHandleError(my_handle); // TODO(ajm): warning possible?
if (split_bands) {
S16ToFloatS16(split_bands, audio->num_frames_per_band(),
audio->split_bands(capture)[kBand0To8kHz]);
}
handle_index++;
if (err != AudioProcessing::kNoError) {
return MapError(err);
}
++handle_index;
}
for (size_t band = 1u; band < audio->num_bands(); ++band) {
memset(audio->split_bands_f(capture)[band], 0,
audio->num_frames_per_band() *
sizeof(audio->split_bands_f(capture)[band][0]));
}
}
return apm_->kNoError;
}
int EchoControlMobileImpl::Enable(bool enable) {
CriticalSectionScoped crit_scoped(crit_);
// Ensure AEC and AECM are not both enabled.
if (enable && apm_->echo_cancellation()->is_enabled()) {
return apm_->kBadParameterError;
}
return EnableComponent(enable);
}
bool EchoControlMobileImpl::is_enabled() const {
return is_component_enabled();
return AudioProcessing::kNoError;
}
int EchoControlMobileImpl::set_routing_mode(RoutingMode mode) {
CriticalSectionScoped crit_scoped(crit_);
if (MapSetting(mode) == -1) {
return apm_->kBadParameterError;
return AudioProcessing::kBadParameterError;
}
routing_mode_ = mode;
return Configure();
}
EchoControlMobile::RoutingMode EchoControlMobileImpl::routing_mode()
const {
EchoControlMobileImpl::RoutingMode EchoControlMobileImpl::routing_mode() const {
return routing_mode_;
}
int EchoControlMobileImpl::enable_comfort_noise(bool enable) {
CriticalSectionScoped crit_scoped(crit_);
comfort_noise_enabled_ = enable;
return Configure();
}
@ -194,101 +241,46 @@ bool EchoControlMobileImpl::is_comfort_noise_enabled() const {
return comfort_noise_enabled_;
}
int EchoControlMobileImpl::SetEchoPath(const void* echo_path,
size_t size_bytes) {
CriticalSectionScoped crit_scoped(crit_);
if (echo_path == NULL) {
return apm_->kNullPointerError;
}
if (size_bytes != echo_path_size_bytes()) {
// Size mismatch
return apm_->kBadParameterError;
void EchoControlMobileImpl::Initialize(int sample_rate_hz,
size_t num_reverse_channels,
size_t num_output_channels) {
low_pass_reference_.resize(num_output_channels);
for (auto& reference : low_pass_reference_) {
reference.fill(0);
}
if (external_echo_path_ == NULL) {
external_echo_path_ = new unsigned char[size_bytes];
}
memcpy(external_echo_path_, echo_path, size_bytes);
stream_properties_.reset(new StreamProperties(
sample_rate_hz, num_reverse_channels, num_output_channels));
return Initialize();
}
// AECM only supports 16 kHz or lower sample rates.
RTC_DCHECK_LE(stream_properties_->sample_rate_hz,
AudioProcessing::kSampleRate16kHz);
int EchoControlMobileImpl::GetEchoPath(void* echo_path,
size_t size_bytes) const {
CriticalSectionScoped crit_scoped(crit_);
if (echo_path == NULL) {
return apm_->kNullPointerError;
}
if (size_bytes != echo_path_size_bytes()) {
// Size mismatch
return apm_->kBadParameterError;
}
if (!is_component_enabled()) {
return apm_->kNotEnabledError;
}
cancellers_.resize(
NumCancellersRequired(stream_properties_->num_output_channels,
stream_properties_->num_reverse_channels));
// Get the echo path from the first channel
Handle* my_handle = static_cast<Handle*>(handle(0));
if (WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes) != 0) {
return GetHandleError(my_handle);
}
return apm_->kNoError;
}
int EchoControlMobileImpl::Initialize() {
if (!is_component_enabled()) {
return apm_->kNoError;
}
if (apm_->proc_sample_rate_hz() > apm_->kSampleRate16kHz) {
LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates";
return apm_->kBadSampleRateError;
}
return ProcessingComponent::Initialize();
}
void* EchoControlMobileImpl::CreateHandle() const {
return WebRtcAecm_Create();
}
void EchoControlMobileImpl::DestroyHandle(void* handle) const {
WebRtcAecm_Free(static_cast<Handle*>(handle));
}
int EchoControlMobileImpl::InitializeHandle(void* handle) const {
assert(handle != NULL);
Handle* my_handle = static_cast<Handle*>(handle);
if (WebRtcAecm_Init(my_handle, apm_->proc_sample_rate_hz()) != 0) {
return GetHandleError(my_handle);
}
if (external_echo_path_ != NULL) {
if (WebRtcAecm_InitEchoPath(my_handle,
external_echo_path_,
echo_path_size_bytes()) != 0) {
return GetHandleError(my_handle);
for (auto& canceller : cancellers_) {
if (!canceller) {
canceller.reset(new Canceller());
}
canceller->Initialize(sample_rate_hz);
}
return apm_->kNoError;
Configure();
}
int EchoControlMobileImpl::ConfigureHandle(void* handle) const {
int EchoControlMobileImpl::Configure() {
AecmConfig config;
config.cngMode = comfort_noise_enabled_;
config.echoMode = MapSetting(routing_mode_);
return WebRtcAecm_set_config(static_cast<Handle*>(handle), config);
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;
}
int EchoControlMobileImpl::num_handles_required() const {
return apm_->num_output_channels() *
apm_->num_reverse_channels();
}
int EchoControlMobileImpl::GetHandleError(void* handle) const {
assert(handle != NULL);
return MapError(WebRtcAecm_get_error_code(static_cast<Handle*>(handle)));
}
} // namespace webrtc