2011-09-19 13:41:13 +05:30
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
|
|
|
|
|
|
|
|
|
Copyright 2011 Collabora Ltd.
|
|
|
|
|
|
|
|
|
|
Contributor: Arun Raghavan <arun.raghavan@collabora.co.uk>
|
|
|
|
|
|
|
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
|
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2014-11-26 14:14:51 +01:00
|
|
|
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
|
2011-09-19 13:41:13 +05:30
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <pulse/cdecl.h>
|
|
|
|
|
|
|
|
|
|
PA_C_DECL_BEGIN
|
|
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/modargs.h>
|
|
|
|
|
|
|
|
|
|
#include <pulse/timeval.h>
|
|
|
|
|
#include "echo-cancel.h"
|
|
|
|
|
PA_C_DECL_END
|
|
|
|
|
|
2016-02-17 19:46:53 +05:30
|
|
|
#include <webrtc/modules/audio_processing/include/audio_processing.h>
|
|
|
|
|
#include <webrtc/modules/interface/module_common_types.h>
|
2011-09-19 13:41:13 +05:30
|
|
|
|
|
|
|
|
#define BLOCK_SIZE_US 10000
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
#define DEFAULT_HIGH_PASS_FILTER true
|
|
|
|
|
#define DEFAULT_NOISE_SUPPRESSION true
|
|
|
|
|
#define DEFAULT_ANALOG_GAIN_CONTROL true
|
|
|
|
|
#define DEFAULT_DIGITAL_GAIN_CONTROL false
|
|
|
|
|
#define DEFAULT_MOBILE false
|
2011-09-19 13:41:13 +05:30
|
|
|
#define DEFAULT_ROUTING_MODE "speakerphone"
|
2013-06-27 19:28:09 +02:00
|
|
|
#define DEFAULT_COMFORT_NOISE true
|
|
|
|
|
#define DEFAULT_DRIFT_COMPENSATION false
|
2016-02-17 19:46:54 +05:30
|
|
|
#define DEFAULT_EXTENDED_FILTER false
|
2011-09-19 13:41:13 +05:30
|
|
|
|
|
|
|
|
static const char* const valid_modargs[] = {
|
|
|
|
|
"high_pass_filter",
|
|
|
|
|
"noise_suppression",
|
|
|
|
|
"analog_gain_control",
|
|
|
|
|
"digital_gain_control",
|
|
|
|
|
"mobile",
|
|
|
|
|
"routing_mode",
|
|
|
|
|
"comfort_noise",
|
2011-10-07 16:28:11 +05:30
|
|
|
"drift_compensation",
|
2016-02-17 19:46:54 +05:30
|
|
|
"extended_filter",
|
2011-09-19 13:41:13 +05:30
|
|
|
NULL
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int routing_mode_from_string(const char *rmode) {
|
|
|
|
|
if (pa_streq(rmode, "quiet-earpiece-or-headset"))
|
|
|
|
|
return webrtc::EchoControlMobile::kQuietEarpieceOrHeadset;
|
|
|
|
|
else if (pa_streq(rmode, "earpiece"))
|
|
|
|
|
return webrtc::EchoControlMobile::kEarpiece;
|
|
|
|
|
else if (pa_streq(rmode, "loud-earpiece"))
|
|
|
|
|
return webrtc::EchoControlMobile::kLoudEarpiece;
|
|
|
|
|
else if (pa_streq(rmode, "speakerphone"))
|
|
|
|
|
return webrtc::EchoControlMobile::kSpeakerphone;
|
|
|
|
|
else if (pa_streq(rmode, "loud-speakerphone"))
|
|
|
|
|
return webrtc::EchoControlMobile::kLoudSpeakerphone;
|
|
|
|
|
else
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
bool pa_webrtc_ec_init(pa_core *c, pa_echo_canceller *ec,
|
2013-06-28 01:03:47 +02:00
|
|
|
pa_sample_spec *rec_ss, pa_channel_map *rec_map,
|
|
|
|
|
pa_sample_spec *play_ss, pa_channel_map *play_map,
|
|
|
|
|
pa_sample_spec *out_ss, pa_channel_map *out_map,
|
|
|
|
|
uint32_t *nframes, const char *args) {
|
2011-09-19 13:41:13 +05:30
|
|
|
webrtc::AudioProcessing *apm = NULL;
|
2016-02-17 19:46:53 +05:30
|
|
|
webrtc::ProcessingConfig pconfig;
|
2016-02-17 19:46:54 +05:30
|
|
|
webrtc::Config config;
|
|
|
|
|
bool hpf, ns, agc, dgc, mobile, cn, ext_filter;
|
2012-10-22 23:48:27 +02:00
|
|
|
int rm = -1;
|
2011-09-19 13:41:13 +05:30
|
|
|
pa_modargs *ma;
|
|
|
|
|
|
|
|
|
|
if (!(ma = pa_modargs_new(args, valid_modargs))) {
|
|
|
|
|
pa_log("Failed to parse submodule arguments.");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hpf = DEFAULT_HIGH_PASS_FILTER;
|
|
|
|
|
if (pa_modargs_get_value_boolean(ma, "high_pass_filter", &hpf) < 0) {
|
|
|
|
|
pa_log("Failed to parse high_pass_filter value");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ns = DEFAULT_NOISE_SUPPRESSION;
|
|
|
|
|
if (pa_modargs_get_value_boolean(ma, "noise_suppression", &ns) < 0) {
|
|
|
|
|
pa_log("Failed to parse noise_suppression value");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
agc = DEFAULT_ANALOG_GAIN_CONTROL;
|
|
|
|
|
if (pa_modargs_get_value_boolean(ma, "analog_gain_control", &agc) < 0) {
|
|
|
|
|
pa_log("Failed to parse analog_gain_control value");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
dgc = agc ? false : DEFAULT_DIGITAL_GAIN_CONTROL;
|
2011-11-03 19:16:47 +05:30
|
|
|
if (pa_modargs_get_value_boolean(ma, "digital_gain_control", &dgc) < 0) {
|
2011-09-19 13:41:13 +05:30
|
|
|
pa_log("Failed to parse digital_gain_control value");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (agc && dgc) {
|
|
|
|
|
pa_log("You must pick only one between analog and digital gain control");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mobile = DEFAULT_MOBILE;
|
|
|
|
|
if (pa_modargs_get_value_boolean(ma, "mobile", &mobile) < 0) {
|
|
|
|
|
pa_log("Failed to parse mobile value");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 16:28:11 +05:30
|
|
|
ec->params.drift_compensation = DEFAULT_DRIFT_COMPENSATION;
|
|
|
|
|
if (pa_modargs_get_value_boolean(ma, "drift_compensation", &ec->params.drift_compensation) < 0) {
|
|
|
|
|
pa_log("Failed to parse drift_compensation value");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-19 13:41:13 +05:30
|
|
|
if (mobile) {
|
2011-10-07 16:28:11 +05:30
|
|
|
if (ec->params.drift_compensation) {
|
|
|
|
|
pa_log("Can't use drift_compensation in mobile mode");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-19 13:41:13 +05:30
|
|
|
if ((rm = routing_mode_from_string(pa_modargs_get_value(ma, "routing_mode", DEFAULT_ROUTING_MODE))) < 0) {
|
|
|
|
|
pa_log("Failed to parse routing_mode value");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cn = DEFAULT_COMFORT_NOISE;
|
|
|
|
|
if (pa_modargs_get_value_boolean(ma, "comfort_noise", &cn) < 0) {
|
|
|
|
|
pa_log("Failed to parse cn value");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (pa_modargs_get_value(ma, "comfort_noise", NULL) || pa_modargs_get_value(ma, "routing_mode", NULL)) {
|
|
|
|
|
pa_log("The routing_mode and comfort_noise options are only valid with mobile=true");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 19:46:54 +05:30
|
|
|
ext_filter = DEFAULT_EXTENDED_FILTER;
|
|
|
|
|
if (pa_modargs_get_value_boolean(ma, "extended_filter", &ext_filter) < 0) {
|
|
|
|
|
pa_log("Failed to parse extended_filter value");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ext_filter)
|
|
|
|
|
config.Set<webrtc::ExtendedFilter>(new webrtc::ExtendedFilter(true));
|
|
|
|
|
|
|
|
|
|
apm = webrtc::AudioProcessing::Create(config);
|
2011-09-19 13:41:13 +05:30
|
|
|
|
echo-cancel: Enable different sample specs for rec and out stream
Enable advanced AEC methods to use different specs (i.e., number of
channels) for rec and out stream. A typical application is beam forming
resp. multi-channel AEC, which takes multiple record channels to produce
an echo-canceled output stream.
This commit alters the EC API as follows: the EC's init() used to get
source and sink's sample spec/channel map. The new interface renamed
source to rec and sink to play and additionally passes sample spec and
channel map of the out stream. The new parameter names of init()
{rec,play,out}_{ss,map} are more intuitive and also resemble to the
parameter names known from run(). Both rec_{ss,map} and out_{ss,map} are
initialized as we knew it from source_{ss,map} before being passed to
init(). The previous EC implementations only require trivial changes,
i.e., setting rec_{ss,map} to out_{ss,map} at the end of init() in case
that out_{ss,map} is modified in init().
2013-02-18 16:31:03 +01:00
|
|
|
out_ss->format = PA_SAMPLE_S16NE;
|
|
|
|
|
*play_ss = *out_ss;
|
2011-09-19 13:41:13 +05:30
|
|
|
/* FIXME: the implementation actually allows a different number of
|
|
|
|
|
* source/sink channels. Do we want to support that? */
|
echo-cancel: Enable different sample specs for rec and out stream
Enable advanced AEC methods to use different specs (i.e., number of
channels) for rec and out stream. A typical application is beam forming
resp. multi-channel AEC, which takes multiple record channels to produce
an echo-canceled output stream.
This commit alters the EC API as follows: the EC's init() used to get
source and sink's sample spec/channel map. The new interface renamed
source to rec and sink to play and additionally passes sample spec and
channel map of the out stream. The new parameter names of init()
{rec,play,out}_{ss,map} are more intuitive and also resemble to the
parameter names known from run(). Both rec_{ss,map} and out_{ss,map} are
initialized as we knew it from source_{ss,map} before being passed to
init(). The previous EC implementations only require trivial changes,
i.e., setting rec_{ss,map} to out_{ss,map} at the end of init() in case
that out_{ss,map} is modified in init().
2013-02-18 16:31:03 +01:00
|
|
|
*play_map = *out_map;
|
|
|
|
|
*rec_ss = *out_ss;
|
|
|
|
|
*rec_map = *out_map;
|
2011-09-19 13:41:13 +05:30
|
|
|
|
2016-02-17 19:46:53 +05:30
|
|
|
pconfig = {
|
|
|
|
|
webrtc::StreamConfig(rec_ss->rate, rec_ss->channels, false), /* input stream */
|
|
|
|
|
webrtc::StreamConfig(out_ss->rate, out_ss->channels, false), /* output stream */
|
|
|
|
|
webrtc::StreamConfig(play_ss->rate, play_ss->channels, false), /* reverse input stream */
|
|
|
|
|
webrtc::StreamConfig(play_ss->rate, play_ss->channels, false), /* reverse output stream */
|
|
|
|
|
};
|
|
|
|
|
apm->Initialize(pconfig);
|
2011-09-19 13:41:13 +05:30
|
|
|
|
|
|
|
|
if (hpf)
|
|
|
|
|
apm->high_pass_filter()->Enable(true);
|
|
|
|
|
|
|
|
|
|
if (!mobile) {
|
2016-02-17 19:46:53 +05:30
|
|
|
apm->echo_cancellation()->enable_drift_compensation(ec->params.drift_compensation);
|
2011-09-19 13:41:13 +05:30
|
|
|
apm->echo_cancellation()->Enable(true);
|
|
|
|
|
} else {
|
|
|
|
|
apm->echo_control_mobile()->set_routing_mode(static_cast<webrtc::EchoControlMobile::RoutingMode>(rm));
|
|
|
|
|
apm->echo_control_mobile()->enable_comfort_noise(cn);
|
|
|
|
|
apm->echo_control_mobile()->Enable(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ns) {
|
|
|
|
|
apm->noise_suppression()->set_level(webrtc::NoiseSuppression::kHigh);
|
|
|
|
|
apm->noise_suppression()->Enable(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (agc || dgc) {
|
2011-11-04 16:38:30 +05:30
|
|
|
if (mobile && rm <= webrtc::EchoControlMobile::kEarpiece) {
|
2011-09-19 13:41:13 +05:30
|
|
|
/* Maybe this should be a knob, but we've got a lot of knobs already */
|
|
|
|
|
apm->gain_control()->set_mode(webrtc::GainControl::kFixedDigital);
|
2013-06-27 19:28:09 +02:00
|
|
|
ec->params.priv.webrtc.agc = false;
|
2011-11-04 16:38:30 +05:30
|
|
|
} else if (dgc) {
|
2011-09-19 13:41:13 +05:30
|
|
|
apm->gain_control()->set_mode(webrtc::GainControl::kAdaptiveDigital);
|
2013-06-27 19:28:09 +02:00
|
|
|
ec->params.priv.webrtc.agc = false;
|
2011-11-04 16:38:30 +05:30
|
|
|
} else {
|
|
|
|
|
apm->gain_control()->set_mode(webrtc::GainControl::kAdaptiveAnalog);
|
|
|
|
|
if (apm->gain_control()->set_analog_level_limits(0, PA_VOLUME_NORM-1) != apm->kNoError) {
|
|
|
|
|
pa_log("Failed to initialise AGC");
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2013-06-27 19:28:09 +02:00
|
|
|
ec->params.priv.webrtc.agc = true;
|
2011-09-19 13:41:13 +05:30
|
|
|
}
|
2011-11-03 19:16:47 +05:30
|
|
|
|
|
|
|
|
apm->gain_control()->Enable(true);
|
2011-09-19 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apm->voice_detection()->Enable(true);
|
|
|
|
|
|
|
|
|
|
ec->params.priv.webrtc.apm = apm;
|
echo-cancel: Enable different sample specs for rec and out stream
Enable advanced AEC methods to use different specs (i.e., number of
channels) for rec and out stream. A typical application is beam forming
resp. multi-channel AEC, which takes multiple record channels to produce
an echo-canceled output stream.
This commit alters the EC API as follows: the EC's init() used to get
source and sink's sample spec/channel map. The new interface renamed
source to rec and sink to play and additionally passes sample spec and
channel map of the out stream. The new parameter names of init()
{rec,play,out}_{ss,map} are more intuitive and also resemble to the
parameter names known from run(). Both rec_{ss,map} and out_{ss,map} are
initialized as we knew it from source_{ss,map} before being passed to
init(). The previous EC implementations only require trivial changes,
i.e., setting rec_{ss,map} to out_{ss,map} at the end of init() in case
that out_{ss,map} is modified in init().
2013-02-18 16:31:03 +01:00
|
|
|
ec->params.priv.webrtc.sample_spec = *out_ss;
|
|
|
|
|
ec->params.priv.webrtc.blocksize = (uint64_t)pa_bytes_per_second(out_ss) * BLOCK_SIZE_US / PA_USEC_PER_SEC;
|
|
|
|
|
*nframes = ec->params.priv.webrtc.blocksize / pa_frame_size(out_ss);
|
2011-09-19 13:41:13 +05:30
|
|
|
|
|
|
|
|
pa_modargs_free(ma);
|
2013-06-27 19:28:09 +02:00
|
|
|
return true;
|
2011-09-19 13:41:13 +05:30
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
if (ma)
|
|
|
|
|
pa_modargs_free(ma);
|
|
|
|
|
if (apm)
|
2016-02-17 19:46:53 +05:30
|
|
|
delete apm;
|
2011-09-19 13:41:13 +05:30
|
|
|
|
2013-06-27 19:28:09 +02:00
|
|
|
return false;
|
2011-09-19 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
2011-10-07 16:28:11 +05:30
|
|
|
void pa_webrtc_ec_play(pa_echo_canceller *ec, const uint8_t *play) {
|
2011-09-19 13:41:13 +05:30
|
|
|
webrtc::AudioProcessing *apm = (webrtc::AudioProcessing*)ec->params.priv.webrtc.apm;
|
2011-10-07 16:28:11 +05:30
|
|
|
webrtc::AudioFrame play_frame;
|
2011-09-19 13:41:13 +05:30
|
|
|
const pa_sample_spec *ss = &ec->params.priv.webrtc.sample_spec;
|
|
|
|
|
|
2016-02-17 19:46:53 +05:30
|
|
|
play_frame.num_channels_ = ss->channels;
|
|
|
|
|
play_frame.sample_rate_hz_ = ss->rate;
|
|
|
|
|
play_frame.interleaved_ = true;
|
|
|
|
|
play_frame.samples_per_channel_ = ec->params.priv.webrtc.blocksize / pa_frame_size(ss);
|
|
|
|
|
|
|
|
|
|
pa_assert(play_frame.samples_per_channel_ <= webrtc::AudioFrame::kMaxDataSizeSamples);
|
|
|
|
|
memcpy(play_frame.data_, play, ec->params.priv.webrtc.blocksize);
|
2011-09-19 13:41:13 +05:30
|
|
|
|
2011-10-07 16:28:11 +05:30
|
|
|
apm->AnalyzeReverseStream(&play_frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_webrtc_ec_record(pa_echo_canceller *ec, const uint8_t *rec, uint8_t *out) {
|
|
|
|
|
webrtc::AudioProcessing *apm = (webrtc::AudioProcessing*)ec->params.priv.webrtc.apm;
|
|
|
|
|
webrtc::AudioFrame out_frame;
|
|
|
|
|
const pa_sample_spec *ss = &ec->params.priv.webrtc.sample_spec;
|
2011-11-04 16:38:30 +05:30
|
|
|
pa_cvolume v;
|
2011-10-07 16:28:11 +05:30
|
|
|
|
2016-02-17 19:46:53 +05:30
|
|
|
out_frame.num_channels_ = ss->channels;
|
|
|
|
|
out_frame.sample_rate_hz_ = ss->rate;
|
|
|
|
|
out_frame.interleaved_ = true;
|
|
|
|
|
out_frame.samples_per_channel_ = ec->params.priv.webrtc.blocksize / pa_frame_size(ss);
|
|
|
|
|
|
|
|
|
|
pa_assert(out_frame.samples_per_channel_ <= webrtc::AudioFrame::kMaxDataSizeSamples);
|
|
|
|
|
memcpy(out_frame.data_, rec, ec->params.priv.webrtc.blocksize);
|
2011-09-19 13:41:13 +05:30
|
|
|
|
2011-11-04 16:38:30 +05:30
|
|
|
if (ec->params.priv.webrtc.agc) {
|
|
|
|
|
pa_cvolume_init(&v);
|
|
|
|
|
pa_echo_canceller_get_capture_volume(ec, &v);
|
|
|
|
|
apm->gain_control()->set_stream_analog_level(pa_cvolume_avg(&v));
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-19 13:41:13 +05:30
|
|
|
apm->set_stream_delay_ms(0);
|
|
|
|
|
apm->ProcessStream(&out_frame);
|
|
|
|
|
|
2011-11-04 16:38:30 +05:30
|
|
|
if (ec->params.priv.webrtc.agc) {
|
|
|
|
|
pa_cvolume_set(&v, ss->channels, apm->gain_control()->stream_analog_level());
|
|
|
|
|
pa_echo_canceller_set_capture_volume(ec, &v);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 19:46:53 +05:30
|
|
|
memcpy(out, out_frame.data_, ec->params.priv.webrtc.blocksize);
|
2011-09-19 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
2011-10-07 16:28:11 +05:30
|
|
|
void pa_webrtc_ec_set_drift(pa_echo_canceller *ec, float drift) {
|
|
|
|
|
webrtc::AudioProcessing *apm = (webrtc::AudioProcessing*)ec->params.priv.webrtc.apm;
|
|
|
|
|
const pa_sample_spec *ss = &ec->params.priv.webrtc.sample_spec;
|
|
|
|
|
|
|
|
|
|
apm->echo_cancellation()->set_stream_drift_samples(drift * ec->params.priv.webrtc.blocksize / pa_frame_size(ss));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_webrtc_ec_run(pa_echo_canceller *ec, const uint8_t *rec, const uint8_t *play, uint8_t *out) {
|
|
|
|
|
pa_webrtc_ec_play(ec, play);
|
|
|
|
|
pa_webrtc_ec_record(ec, rec, out);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-19 13:41:13 +05:30
|
|
|
void pa_webrtc_ec_done(pa_echo_canceller *ec) {
|
|
|
|
|
if (ec->params.priv.webrtc.apm) {
|
2016-02-17 19:46:53 +05:30
|
|
|
delete (webrtc::AudioProcessing*)ec->params.priv.webrtc.apm;
|
2011-09-19 13:41:13 +05:30
|
|
|
ec->params.priv.webrtc.apm = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|