diff --git a/src/modules/meson.build b/src/modules/meson.build index 89dd4ff0d..f51aa29c7 100644 --- a/src/modules/meson.build +++ b/src/modules/meson.build @@ -152,6 +152,7 @@ pipewire_module_protocol_pulse = shared_library('pipewire-module-protocol-pulse' 'module-protocol-pulse/manager.c', 'module-protocol-pulse/pulse-server.c', 'module-protocol-pulse/modules/module-combine-sink.c', + 'module-protocol-pulse/modules/module-echo-cancel.c', 'module-protocol-pulse/modules/module-ladspa-sink.c', 'module-protocol-pulse/modules/module-ladspa-source.c', 'module-protocol-pulse/modules/module-loopback.c', diff --git a/src/modules/module-protocol-pulse/module.c b/src/modules/module-protocol-pulse/module.c index 7fd8e09af..8c0d64f76 100644 --- a/src/modules/module-protocol-pulse/module.c +++ b/src/modules/module-protocol-pulse/module.c @@ -214,6 +214,7 @@ int module_args_to_audioinfo(struct impl *impl, struct pw_properties *props, str static const struct module_info module_list[] = { { "module-combine-sink", create_module_combine_sink, }, + { "module-echo-cancel", create_module_echo_cancel, }, { "module-ladspa-sink", create_module_ladspa_sink, }, { "module-ladspa-source", create_module_ladspa_source, }, { "module-loopback", create_module_loopback, }, diff --git a/src/modules/module-protocol-pulse/modules/module-echo-cancel.c b/src/modules/module-protocol-pulse/modules/module-echo-cancel.c new file mode 100644 index 000000000..0a49c01d6 --- /dev/null +++ b/src/modules/module-protocol-pulse/modules/module-echo-cancel.c @@ -0,0 +1,262 @@ +/* PipeWire + * + * Copyright © 2021 Wim Taymans + * Copyright © 2021 Arun Raghavan + * + * 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 +#include +#include +#include +#include + +#include "../defs.h" +#include "../module.h" +#include "registry.h" + +#define ERROR_RETURN(str) \ + { \ + pw_log_error(str); \ + res = -EINVAL; \ + goto out; \ + } + +struct module_echo_cancel_data { + struct module *module; + + struct pw_impl_module *mod; + struct spa_hook mod_listener; + + struct pw_properties *props; + struct pw_properties *source_props; + struct pw_properties *sink_props; + + struct spa_audio_info_raw info; +}; + +static void module_destroy(void *data) +{ + struct module_echo_cancel_data *d = data; + spa_hook_remove(&d->mod_listener); + d->mod = NULL; + module_schedule_unload(d->module); +} + +static const struct pw_impl_module_events module_events = { + PW_VERSION_IMPL_MODULE_EVENTS, + .destroy = module_destroy +}; + +static int module_echo_cancel_load(struct client *client, struct module *module) +{ + struct module_echo_cancel_data *data = module->user_data; + FILE *f; + const char *str; + char *args; + size_t size; + + f = open_memstream(&args, &size); + fprintf(f, "{"); + /* Can't just serialise this dict because the "null" method gets + * interpreted as a JSON null */ + if ((str = pw_properties_get(data->props, "aec.method"))) + fprintf(f, " aec.method = \"%s\"", str); + if ((str = pw_properties_get(data->props, "aec.args"))) + fprintf(f, " aec.args = \"%s\"", str); + if (data->info.rate != 0) + fprintf(f, " audio.rate = %u", data->info.rate); + if (data->info.channels != 0) { + fprintf(f, " audio.channels = %u", data->info.channels); + /* TODO: convert channel positions to string */ + } + fprintf(f, " source.props = {"); + pw_properties_serialize_dict(f, &data->source_props->dict, 0); + fprintf(f, " } sink.props = {"); + pw_properties_serialize_dict(f, &data->sink_props->dict, 0); + fprintf(f, " } }"); + fclose(f); + + data->mod = pw_context_load_module(module->impl->context, + "libpipewire-module-echo-cancel", + args, NULL); + free(args); + + if (data->mod == NULL) + return -errno; + + pw_impl_module_add_listener(data->mod, + &data->mod_listener, + &module_events, data); + + pw_log_info("loaded module %p id:%u name:%s", module, module->idx, module->name); + module_emit_loaded(module, 0); + + return 0; +} + +static int module_echo_cancel_unload(struct client *client, struct module *module) +{ + struct module_echo_cancel_data *d = module->user_data; + + pw_log_info("unload module %p id:%u name:%s", module, module->idx, module->name); + + if (d->mod) { + spa_hook_remove(&d->mod_listener); + pw_impl_module_destroy(d->mod); + d->mod = NULL; + } + return 0; +} + +static const struct module_methods module_echo_cancel_methods = { + VERSION_MODULE_METHODS, + .load = module_echo_cancel_load, + .unload = module_echo_cancel_unload, +}; + +static const struct spa_dict_item module_echo_cancel_info[] = { + { PW_KEY_MODULE_AUTHOR, "Arun Raghavan " }, + { PW_KEY_MODULE_DESCRIPTION, "Acoustic echo canceller" }, + { PW_KEY_MODULE_USAGE, "source_name= " + "source_properties= " + "source_master= " + "sink_name= " + "sink_properties= " + "sink_master= " + "rate= " + "channels= " + "channel_map= " + "aec_method= " + "aec_args= " +#if 0 + /* These are not implemented because they don't + * really make sense in the PipeWire context */ + "format= " + "adjust_time= " + "adjust_threshold= " + "autoloaded= " + "save_aec= " + "use_volume_sharing= " + "use_master_format= " +#endif + }, + { PW_KEY_MODULE_VERSION, PACKAGE_VERSION }, +}; + +struct module *create_module_echo_cancel(struct impl *impl, const char *argument) +{ + struct module *module; + struct module_echo_cancel_data *d; + struct pw_properties *props = NULL, *aec_props = NULL, *sink_props = NULL, *source_props = NULL; + const char *str; + struct spa_audio_info_raw info = { 0 }; + int res; + + props = pw_properties_new_dict(&SPA_DICT_INIT_ARRAY(module_echo_cancel_info)); + aec_props = pw_properties_new(NULL, NULL); + source_props = pw_properties_new(NULL, NULL); + sink_props = pw_properties_new(NULL, NULL); + if (!props ||!aec_props || !source_props || !sink_props) { + res = -EINVAL; + goto out; + } + if (argument) + module_args_add_props(props, argument); + + if ((str = pw_properties_get(props, "source_name")) != NULL) { + pw_properties_set(source_props, PW_KEY_NODE_NAME, str); + pw_properties_set(props, "source_name", NULL); + } else { + pw_properties_set(source_props, PW_KEY_NODE_NAME, "echo-cancel"); + } + + if ((str = pw_properties_get(props, "sink_name")) != NULL) { + pw_properties_set(sink_props, PW_KEY_NODE_NAME, str); + pw_properties_set(props, "sink_name", NULL); + } else { + pw_properties_set(sink_props, PW_KEY_NODE_NAME, "echo-cancel"); + } + + if ((str = pw_properties_get(props, "source_master")) != NULL) { + if (pw_endswith(str, ".monitor")) { + pw_properties_setf(source_props, PW_KEY_NODE_TARGET, + "%.*s", (int)strlen(str)-8, str); + } else { + pw_properties_set(source_props, PW_KEY_NODE_TARGET, str); + } + pw_properties_set(props, "source_master", NULL); + } + + if ((str = pw_properties_get(props, "sink_master")) != NULL) { + pw_properties_set(sink_props, PW_KEY_NODE_TARGET, str); + pw_properties_set(props, "sink_master", NULL); + } + + if (module_args_to_audioinfo(impl, props, &info) < 0) { + res = -EINVAL; + goto out; + } + + if ((str = pw_properties_get(props, "source_properties")) != NULL) { + module_args_add_props(source_props, str); + pw_properties_set(props, "source_properties", NULL); + } + + if ((str = pw_properties_get(props, "sink_properties")) != NULL) { + module_args_add_props(sink_props, str); + pw_properties_set(props, "sink_properties", NULL); + } + + if ((str = pw_properties_get(props, "aec_method")) != NULL) { + pw_properties_set(aec_props, "aec.method", str); + pw_properties_set(props, "aec_method", NULL); + } + + if ((str = pw_properties_get(props, "aec_args")) != NULL) { + pw_properties_set(aec_props, "aec.args", str); + pw_properties_set(props, "aec_args", NULL); + } + + module = module_new(impl, &module_echo_cancel_methods, sizeof(*d)); + if (module == NULL) { + res = -errno; + goto out; + } + + module->props = props; + d = module->user_data; + d->module = module; + d->props = aec_props; + d->source_props = source_props; + d->sink_props = sink_props; + d->info = info; + + return module; +out: + pw_properties_free(props); + pw_properties_free(aec_props); + pw_properties_free(sink_props); + pw_properties_free(source_props); + errno = -res; + + return NULL; +} diff --git a/src/modules/module-protocol-pulse/modules/registry.h b/src/modules/module-protocol-pulse/modules/registry.h index 066000065..1612ae871 100644 --- a/src/modules/module-protocol-pulse/modules/registry.h +++ b/src/modules/module-protocol-pulse/modules/registry.h @@ -29,6 +29,7 @@ #include "../internal.h" struct module *create_module_combine_sink(struct impl *impl, const char *argument); +struct module *create_module_echo_cancel(struct impl *impl, const char *argument); struct module *create_module_ladspa_sink(struct impl *impl, const char *argument); struct module *create_module_ladspa_source(struct impl *impl, const char *argument); struct module *create_module_loopback(struct impl *impl, const char *argument);