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

@ -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);