module-echo-cancel: Move backends to dynamic libaries

Move all backends to dynamic libaries loaded with spa_plugin_loader so
new backends not needs changes in pipewire or pipewire dependency to
external code

Change-Id: I702ce047598d0c318d6dc6ac8248062a5c12f643
This commit is contained in:
Joakim Olsson 2022-02-10 09:33:06 +01:00 committed by Wim Taymans
parent 761199be70
commit 9386c70b3a
11 changed files with 576 additions and 247 deletions

View file

@ -110,22 +110,15 @@ pipewire_module_filter_chain = shared_library('pipewire-module-filter-chain',
pipewire_module_echo_cancel_sources = [
'module-echo-cancel.c',
'module-echo-cancel/aec-null.c',
]
if webrtc_dep.found()
pipewire_module_echo_cancel_sources += [
'module-echo-cancel/aec-webrtc.cpp'
]
endif
pipewire_module_echo_cancel = shared_library('pipewire-module-echo-cancel',
pipewire_module_echo_cancel_sources,
include_directories : [configinc],
install : true,
install_dir : modules_install_dir,
install_rpath: modules_install_dir,
dependencies : [mathlib, dl_lib, pipewire_dep, webrtc_dep],
dependencies : [mathlib, dl_lib, pipewire_dep],
)
pipewire_module_profiler = shared_library('pipewire-module-profiler',

View file

@ -24,6 +24,7 @@
*/
#include "config.h"
#include "module-echo-cancel/echo-cancel.h"
#include <errno.h>
#include <fcntl.h>
@ -43,10 +44,14 @@
#include <spa/param/audio/raw.h>
#include <spa/param/profiler.h>
#include <spa/pod/builder.h>
#include <spa/support/plugin.h>
#include <spa/utils/json.h>
#include <spa/utils/names.h>
#include <spa/utils/result.h>
#include <spa/utils/ringbuffer.h>
#include <spa/utils/string.h>
#include <spa/support/plugin-loader.h>
#include <spa/interfaces/audio/aec.h>
#include <pipewire/private.h>
#include <pipewire/impl.h>
@ -54,8 +59,6 @@
#include <pipewire/extensions/profiler.h>
#include "module-echo-cancel/echo-cancel.h"
/** \page page_module_echo_cancel PipeWire Module: Echo Cancel
*
* The `echo-cancel` module performs echo cancellation. The module creates
@ -68,8 +71,8 @@
*
* - `source.props = {}`: properties to be passed to the source stream
* - `sink.props = {}`: properties to be passed to the sink stream
* - `aec.method = <str>`: the echo cancellation method. Currently supported:
* `webrtc`. Leave unset to use the default method (`webrtc`).
* - `library.name = <str>`: the echo cancellation library Currently supported:
* `aec/libspa-aec-exaudio`. Leave unset to use the default method (`aec/libspa-aec-exaudio`).
* - `aec.args = <str>`: arguments to pass to the echo cancellation method
*
* ## General options
@ -94,7 +97,7 @@
* context.modules = [
* { name = libpipewire-module-echo-cancel
* args = {
* # aec.method = webrtc
* # library.name = aec/libspa-aec-exaudio
* # node.latency = 1024/48000
* source.props = {
* node.name = "Echo Cancellation Source"
@ -138,7 +141,7 @@ static const struct spa_dict_item module_props[] = {
"[ audio.position=<channel map> ] "
"[ buffer.max_size=<max buffer size in ms> ] "
"[ buffer.play_delay=<play delay in ms> ] "
"[ aec.method=<aec method> ] "
"[ library.name =<library name> ] "
"[ aec.args=<aec arguments> ] "
"[ source.props=<properties> ] "
"[ sink.props=<properties> ] " },
@ -186,7 +189,6 @@ struct impl {
struct spa_ringbuffer out_ring;
const struct echo_cancel_info *aec_info;
void *aec;
uint32_t aec_blocksize;
unsigned int capture_ready:1;
@ -197,6 +199,9 @@ struct impl {
uint32_t max_buffer_size;
uint32_t buffer_delay;
struct spa_handle *spa_handle;
struct spa_plugin_loader *loader;
};
static void do_unload_module(void *obj, void *data, int res, uint32_t id)
@ -283,7 +288,7 @@ static void process(struct impl *impl)
pw_stream_queue_buffer(impl->playback, pout);
/* Now run the canceller */
echo_cancel_run(impl->aec_info, impl->aec, rec, play_delayed, out, size / sizeof(float));
echo_cancel_run(impl->aec_info, impl->spa_handle, rec, play_delayed, out, size / sizeof(float));
/* Next, copy over the output to the output ringbuffer */
avail = spa_ringbuffer_get_write_index(&impl->out_ring, &oindex);
@ -803,8 +808,8 @@ static void impl_destroy(struct impl *impl)
pw_stream_destroy(impl->sink);
if (impl->core && impl->do_disconnect)
pw_core_disconnect(impl->core);
if (impl->aec)
echo_cancel_destroy(impl->aec_info, impl->aec);
if (impl->spa_handle)
spa_plugin_loader_unload(impl->loader, impl->spa_handle);
pw_properties_free(impl->source_props);
pw_properties_free(impl->sink_props);
@ -893,7 +898,10 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
struct impl *impl;
uint32_t id = pw_global_get_id(pw_impl_module_get_global(module));
const char *str;
int res;
const char *path;
int res = 0;
struct spa_handle *handle = NULL;
void *iface;
PW_LOG_TOPIC_INIT(mod_topic);
@ -967,22 +975,57 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
if (pw_properties_get(impl->sink_props, PW_KEY_MEDIA_CLASS) == NULL)
pw_properties_set(impl->sink_props, PW_KEY_MEDIA_CLASS, "Audio/Sink");
if ((str = pw_properties_get(props, "aec.method")) == NULL)
str = "webrtc";
if ((str = pw_properties_get(props, "aec.method")) != NULL)
pw_log_warn("aec.method is not supported anymore use library.name");
#ifdef HAVE_WEBRTC
if (spa_streq(str, "webrtc"))
impl->aec_info = echo_cancel_webrtc;
else
#endif
impl->aec_info = echo_cancel_null;
/* Use webrtc as default */
if ((path = pw_properties_get(props, "library.name")) == NULL)
path = "aec/libspa-aec-webrtc";
struct spa_dict_item info_items[] = {
{ SPA_KEY_LIBRARY_NAME, path },
};
struct spa_dict info = SPA_DICT_INIT_ARRAY(info_items);
impl->loader = spa_support_find(context->support, context->n_support, SPA_TYPE_INTERFACE_PluginLoader);
if (impl->loader == NULL) {
pw_log_error("a plugin loader is needed");
return -EINVAL;
}
handle = spa_plugin_loader_load(impl->loader, SPA_NAME_AEC, &info);
if (handle == NULL) {
pw_log_error("AEC codec plugin %s not available library.name %s", SPA_NAME_AEC, path);
return -ENOENT;
}
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_AEC, &iface)) < 0) {
pw_log_error("can't get %s interface %d", SPA_TYPE_INTERFACE_AEC, res);
return res;
}
impl->aec_info = iface;
impl->spa_handle = handle;
if (impl->aec_info->iface.version != SPA_VERSION_AUDIO_AEC) {
pw_log_error("codec plugin %s has incompatible ABI version (%d != %d)",
SPA_NAME_AEC, impl->aec_info->iface.version, SPA_VERSION_AUDIO_AEC);
res = -ENOENT;
goto error;
}
(void)SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_AEC, (struct echo_cancel_info *)impl->aec_info);
pw_log_info("Using plugin AEC %s", impl->aec_info->name);
if ((str = pw_properties_get(props, "aec.args")) != NULL)
aec_props = pw_properties_new_string(str);
else
aec_props = pw_properties_new(NULL, NULL);
impl->aec = echo_cancel_create(impl->aec_info, aec_props, &impl->info);
if (echo_cancel_create(impl->aec_info, impl->spa_handle, &aec_props->dict, &impl->info)) {
pw_log_error("codec plugin %s create failed", impl->aec_info->name);
res = -ENOENT;
goto error;
}
pw_properties_free(aec_props);

View file

@ -1,64 +0,0 @@
/* PipeWire
*
* Copyright © 2021 Wim Taymans <wim.taymans@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "echo-cancel.h"
struct impl {
uint32_t channels;
};
static void *null_create(const struct pw_properties *args, const struct spa_audio_info_raw *info)
{
struct impl *impl;
impl = calloc(1, sizeof(struct impl));
impl->channels = info->channels;
return impl;
}
static void null_destroy(void *ec)
{
free(ec);
}
static int null_run(void *ec, const float *rec[], const float *play[], float *out[], uint32_t n_samples)
{
struct impl *impl = ec;
uint32_t i;
for (i = 0; i < impl->channels; i++)
memcpy(out[i], rec[i], n_samples * sizeof(float));
return 0;
}
static const struct echo_cancel_info echo_cancel_null_impl = {
.name = "null",
.info = SPA_DICT_INIT(NULL, 0),
.latency = NULL,
.create = null_create,
.destroy = null_destroy,
.run = null_run,
};
const struct echo_cancel_info *echo_cancel_null = &echo_cancel_null_impl;

View file

@ -1,163 +0,0 @@
/* PipeWire
*
* Copyright © 2021 Wim Taymans <wim.taymans@gmail.com>
* © 2021 Arun Raghavan <arun@asymptotic.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <memory>
#include <utility>
#include "echo-cancel.h"
#include <pipewire/pipewire.h>
#include <webrtc/modules/audio_processing/include/audio_processing.h>
#include <webrtc/modules/interface/module_common_types.h>
#include <webrtc/system_wrappers/include/trace.h>
struct impl {
std::unique_ptr<webrtc::AudioProcessing> apm;
spa_audio_info_raw info;
std::unique_ptr<float *[]> play_buffer, rec_buffer, out_buffer;
impl(std::unique_ptr<webrtc::AudioProcessing> apm, const spa_audio_info_raw& info)
: apm(std::move(apm)),
info(info),
play_buffer(std::make_unique<float *[]>(info.channels)),
rec_buffer(std::make_unique<float *[]>(info.channels)),
out_buffer(std::make_unique<float *[]>(info.channels))
{ }
};
static void *webrtc_create(const struct pw_properties *args, const spa_audio_info_raw *info)
{
bool extended_filter = pw_properties_get_bool(args, "webrtc.extended_filter", true);
bool delay_agnostic = pw_properties_get_bool(args, "webrtc.delay_agnostic", true);
bool high_pass_filter = pw_properties_get_bool(args, "webrtc.high_pass_filter", true);
bool noise_suppression = pw_properties_get_bool(args, "webrtc.noise_suppression", true);
bool voice_detection = pw_properties_get_bool(args, "webrtc.voice_detection", true);
// Note: AGC seems to mess up with Agnostic Delay Detection, especially with speech,
// result in very poor performance, disable by default
bool gain_control = pw_properties_get_bool(args, "webrtc.gain_control", false);
// Disable experimental flags by default
bool experimental_agc = pw_properties_get_bool(args, "webrtc.experimental_agc", false);
bool experimental_ns = pw_properties_get_bool(args, "webrtc.experimental_ns", false);
// FIXME: Intelligibility enhancer is not currently supported
// This filter will modify playback buffer (when calling ProcessReverseStream), but now
// playback buffer modifications are discarded.
webrtc::Config config;
config.Set<webrtc::ExtendedFilter>(new webrtc::ExtendedFilter(extended_filter));
config.Set<webrtc::DelayAgnostic>(new webrtc::DelayAgnostic(delay_agnostic));
config.Set<webrtc::ExperimentalAgc>(new webrtc::ExperimentalAgc(experimental_agc));
config.Set<webrtc::ExperimentalNs>(new webrtc::ExperimentalNs(experimental_ns));
webrtc::ProcessingConfig pconfig = {{
webrtc::StreamConfig(info->rate, info->channels, false), /* input stream */
webrtc::StreamConfig(info->rate, info->channels, false), /* output stream */
webrtc::StreamConfig(info->rate, info->channels, false), /* reverse input stream */
webrtc::StreamConfig(info->rate, info->channels, false), /* reverse output stream */
}};
auto apm = std::unique_ptr<webrtc::AudioProcessing>(webrtc::AudioProcessing::Create(config));
if (apm->Initialize(pconfig) != webrtc::AudioProcessing::kNoError) {
pw_log_error("Error initialising webrtc audio processing module");
return nullptr;
}
apm->high_pass_filter()->Enable(high_pass_filter);
// Always disable drift compensation since it requires drift sampling
apm->echo_cancellation()->enable_drift_compensation(false);
apm->echo_cancellation()->Enable(true);
// TODO: wire up supression levels to args
apm->echo_cancellation()->set_suppression_level(webrtc::EchoCancellation::kHighSuppression);
apm->noise_suppression()->set_level(webrtc::NoiseSuppression::kHigh);
apm->noise_suppression()->Enable(noise_suppression);
apm->voice_detection()->Enable(voice_detection);
// TODO: wire up AGC parameters to args
apm->gain_control()->set_analog_level_limits(0, 255);
apm->gain_control()->set_mode(webrtc::GainControl::kAdaptiveDigital);
apm->gain_control()->Enable(gain_control);
return new impl(std::move(apm), *info);
}
static void webrtc_destroy(void *ec)
{
auto impl = static_cast<struct impl *>(ec);
delete impl;
}
static int webrtc_run(void *ec, const float *rec[], const float *play[], float *out[], uint32_t n_samples)
{
auto impl = static_cast<struct impl *>(ec);
webrtc::StreamConfig config =
webrtc::StreamConfig(impl->info.rate, impl->info.channels, false);
unsigned int num_blocks = n_samples * 1000 / impl->info.rate / 10;
if (n_samples * 1000 / impl->info.rate % 10 != 0) {
pw_log_error("Buffers must be multiples of 10ms in length (currently %u samples)", n_samples);
return -1;
}
for (size_t i = 0; i < num_blocks; i ++) {
for (size_t j = 0; j < impl->info.channels; j++) {
impl->play_buffer[j] = const_cast<float *>(play[j]) + config.num_frames() * i;
impl->rec_buffer[j] = const_cast<float *>(rec[j]) + config.num_frames() * i;
impl->out_buffer[j] = out[j] + config.num_frames() * i;
}
/* FIXME: ProcessReverseStream may change the playback buffer, in which
* case we should use that, if we ever expose the intelligibility
* enhancer */
if (impl->apm->ProcessReverseStream(impl->play_buffer.get(), config, config, impl->play_buffer.get()) !=
webrtc::AudioProcessing::kNoError) {
pw_log_error("Processing reverse stream failed");
}
// Extra delay introduced by multiple frames
impl->apm->set_stream_delay_ms((num_blocks - 1) * 10);
if (impl->apm->ProcessStream(impl->rec_buffer.get(), config, config, impl->out_buffer.get()) !=
webrtc::AudioProcessing::kNoError) {
pw_log_error("Processing stream failed");
}
}
return 0;
}
static const struct echo_cancel_info echo_cancel_webrtc_impl = {
.name = "webrtc",
.info = SPA_DICT_INIT(NULL, 0),
.latency = "480/48000",
.create = webrtc_create,
.destroy = webrtc_destroy,
.run = webrtc_run,
};
const struct echo_cancel_info *echo_cancel_webrtc = &echo_cancel_webrtc_impl;

View file

@ -22,29 +22,13 @@
* DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <spa/utils/dict.h>
#include <spa/utils/hook.h>
#include <spa/param/audio/raw.h>
#include <spa/support/plugin.h>
#include <pipewire/properties.h>
struct echo_cancel_info {
const char *name;
const struct spa_dict info;
const char *latency;
void *(*create) (const struct pw_properties *args, const struct spa_audio_info_raw *info);
void (*destroy) (void *ec);
int (*run) (void *ec, const float *rec[], const float *play[], float *out[], uint32_t n_samples);
};
#define echo_cancel_create(i,...) (i)->create(__VA_ARGS__)
#define echo_cancel_destroy(i,...) (i)->destroy(__VA_ARGS__)
#define echo_cancel_run(i,...) (i)->run(__VA_ARGS__)
#ifdef HAVE_WEBRTC
extern const struct echo_cancel_info *echo_cancel_webrtc;
#endif
extern const struct echo_cancel_info *echo_cancel_null;