From 3ae2b9e189fbe32de85d86f5ec0b80111c7b3bd9 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 12 May 2021 17:00:45 +0200 Subject: [PATCH] pulse-server: implement module-tunnel-sink/source See #865 --- src/modules/meson.build | 4 +- src/modules/module-protocol-pulse/module.c | 4 +- .../modules/module-tunnel-sink.c | 244 ++++++++++++++++++ .../modules/module-tunnel-source.c | 244 ++++++++++++++++++ .../module-protocol-pulse/modules/registry.h | 2 + 5 files changed, 496 insertions(+), 2 deletions(-) create mode 100644 src/modules/module-protocol-pulse/modules/module-tunnel-sink.c create mode 100644 src/modules/module-protocol-pulse/modules/module-tunnel-source.c diff --git a/src/modules/meson.build b/src/modules/meson.build index 173359a2a..f58443187 100644 --- a/src/modules/meson.build +++ b/src/modules/meson.build @@ -136,10 +136,12 @@ pipewire_module_protocol_pulse = shared_library('pipewire-module-protocol-pulse' 'module-protocol-pulse/modules/module-loopback.c', 'module-protocol-pulse/modules/module-native-protocol-tcp.c', 'module-protocol-pulse/modules/module-null-sink.c', + 'module-protocol-pulse/modules/module-pipe-sink.c', 'module-protocol-pulse/modules/module-remap-sink.c', 'module-protocol-pulse/modules/module-remap-source.c', 'module-protocol-pulse/modules/module-simple-protocol-tcp.c', - 'module-protocol-pulse/modules/module-pipe-sink.c', + 'module-protocol-pulse/modules/module-tunnel-sink.c', + 'module-protocol-pulse/modules/module-tunnel-source.c', ], c_args : pipewire_module_c_args, include_directories : [configinc, spa_inc], diff --git a/src/modules/module-protocol-pulse/module.c b/src/modules/module-protocol-pulse/module.c index f4b59260b..4ef64b76f 100644 --- a/src/modules/module-protocol-pulse/module.c +++ b/src/modules/module-protocol-pulse/module.c @@ -212,11 +212,13 @@ static const struct module_info module_list[] = { { "module-ladspa-source", create_module_ladspa_source, }, { "module-loopback", create_module_loopback, }, { "module-null-sink", create_module_null_sink, }, - { "module-pipe-sink", create_module_pipe_sink, }, { "module-native-protocol-tcp", create_module_native_protocol_tcp, }, + { "module-pipe-sink", create_module_pipe_sink, }, { "module-remap-sink", create_module_remap_sink, }, { "module-remap-source", create_module_remap_source, }, { "module-simple-protocol-tcp", create_module_simple_protocol_tcp, }, + { "module-tunnel-sink", create_module_tunnel_sink, }, + { "module-tunnel-source", create_module_tunnel_source, }, { NULL, } }; diff --git a/src/modules/module-protocol-pulse/modules/module-tunnel-sink.c b/src/modules/module-protocol-pulse/modules/module-tunnel-sink.c new file mode 100644 index 000000000..17c44e11e --- /dev/null +++ b/src/modules/module-protocol-pulse/modules/module-tunnel-sink.c @@ -0,0 +1,244 @@ +/* PipeWire + * + * Copyright © 2021 Wim Taymans + * + * 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 + +#include "../defs.h" +#include "../module.h" +#include "registry.h" + +#define ERROR_RETURN(str) \ + { \ + pw_log_error(str); \ + res = -EINVAL; \ + goto out; \ + } + +struct module_tunnel_sink_data { + struct module *module; + + struct pw_impl_module *mod; + struct spa_hook mod_listener; + + struct pw_properties *stream_props; +}; + +static void module_destroy(void *data) +{ + struct module_tunnel_sink_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 void serialize_dict(FILE *f, const struct spa_dict *dict) +{ + const struct spa_dict_item *it; + spa_dict_for_each(it, dict) { + size_t len = it->value ? strlen(it->value) : 0; + fprintf(f, " \"%s\" = ", it->key); + if (it->value == NULL) { + fprintf(f, "null"); + } else if ( spa_json_is_null(it->value, len) || + spa_json_is_float(it->value, len) || + spa_json_is_object(it->value, len)) { + fprintf(f, "%s", it->value); + } else { + size_t size = (len+1) * 4; + char str[size]; + spa_json_encode_string(str, size, it->value); + fprintf(f, "%s", str); + } + } +} + +static int module_tunnel_sink_load(struct client *client, struct module *module) +{ + struct module_tunnel_sink_data *data = module->user_data; + FILE *f; + char *args; + size_t size; + const char *server; + + server = pw_properties_get(module->props, "server"); + + f = open_memstream(&args, &size); + fprintf(f, "{"); + serialize_dict(f, &module->props->dict); + fprintf(f, " pulse.server.address = \"%s\" ", server); + fprintf(f, " tunnel.mode = playback "); + fprintf(f, " stream.props = {"); + serialize_dict(f, &data->stream_props->dict); + fprintf(f, " } }"); + fclose(f); + + data->mod = pw_context_load_module(module->impl->context, + "libpipewire-module-pulse-tunnel", + 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_tunnel_sink_unload(struct client *client, struct module *module) +{ + struct module_tunnel_sink_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_tunnel_sink_methods = { + VERSION_MODULE_METHODS, + .load = module_tunnel_sink_load, + .unload = module_tunnel_sink_unload, +}; + +static const struct spa_dict_item module_tunnel_sink_info[] = { + { PW_KEY_MODULE_AUTHOR, "Wim Taymans " }, + { PW_KEY_MODULE_DESCRIPTION, "Create a network sink which connects to a remote PulseAudio server" }, + { PW_KEY_MODULE_USAGE, + "server=
" + "sink= " + "sink_name= " + "sink_properties= " + "format= " + "channels= " + "rate= " + "channel_map= " + "cookie=" }, + { PW_KEY_MODULE_VERSION, PACKAGE_VERSION }, +}; + +static void audio_info_to_props(struct spa_audio_info_raw *info, struct pw_properties *props) +{ + char *s, *p; + uint32_t i; + + pw_properties_setf(props, SPA_KEY_AUDIO_CHANNELS, "%u", info->channels); + p = s = alloca(info->channels * 6); + for (i = 0; i < info->channels; i++) + p += snprintf(p, 6, "%s%s", i == 0 ? "" : ",", + channel_id2name(info->position[i])); + pw_properties_set(props, SPA_KEY_AUDIO_POSITION, s); +} + +struct module *create_module_tunnel_sink(struct impl *impl, const char *argument) +{ + struct module *module; + struct module_tunnel_sink_data *d; + struct pw_properties *props = NULL, *stream_props = NULL; + const char *str, *server, *remote_sink_name; + struct spa_audio_info_raw info = { 0 }; + int res; + + props = pw_properties_new_dict(&SPA_DICT_INIT_ARRAY(module_tunnel_sink_info)); + stream_props = pw_properties_new(NULL, NULL); + if (props == NULL || stream_props == NULL) { + res = -ENOMEM; + goto out; + } + if (argument) + module_args_add_props(props, argument); + + remote_sink_name = pw_properties_get(props, "sink"); + if (remote_sink_name) + pw_properties_set(props, PW_KEY_NODE_TARGET, remote_sink_name); + + if ((server = pw_properties_get(props, "server")) == NULL) { + pw_log_error("no server given"); + res = -EINVAL; + goto out; + } + + pw_properties_setf(stream_props, PW_KEY_NODE_DESCRIPTION, + _("Tunnel to %s/%s"), server, + remote_sink_name ? remote_sink_name : ""); + pw_properties_set(stream_props, PW_KEY_MEDIA_CLASS, "Audio/Sink"); + + if ((str = pw_properties_get(props, "sink_name")) != NULL) { + pw_properties_set(stream_props, PW_KEY_NODE_NAME, str); + pw_properties_set(props, "sink_name", NULL); + } else { + pw_properties_setf(stream_props, PW_KEY_NODE_NAME, + "tunnel-sink.%s", server); + } + if ((str = pw_properties_get(props, "sink_properties")) != NULL) { + module_args_add_props(stream_props, str); + pw_properties_set(props, "sink_properties", NULL); + } + if (module_args_to_audioinfo(impl, props, &info) < 0) { + res = -EINVAL; + goto out; + } + + audio_info_to_props(&info, stream_props); + + module = module_new(impl, &module_tunnel_sink_methods, sizeof(*d)); + if (module == NULL) { + res = -errno; + goto out; + } + + module->props = props; + d = module->user_data; + d->module = module; + d->stream_props = stream_props; + + return module; +out: + if (props) + pw_properties_free(props); + if (stream_props) + pw_properties_free(stream_props); + errno = -res; + return NULL; +} diff --git a/src/modules/module-protocol-pulse/modules/module-tunnel-source.c b/src/modules/module-protocol-pulse/modules/module-tunnel-source.c new file mode 100644 index 000000000..1bf8f7d67 --- /dev/null +++ b/src/modules/module-protocol-pulse/modules/module-tunnel-source.c @@ -0,0 +1,244 @@ +/* PipeWire + * + * Copyright © 2021 Wim Taymans + * + * 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 + +#include "../defs.h" +#include "../module.h" +#include "registry.h" + +#define ERROR_RETURN(str) \ + { \ + pw_log_error(str); \ + res = -EINVAL; \ + goto out; \ + } + +struct module_tunnel_source_data { + struct module *module; + + struct pw_impl_module *mod; + struct spa_hook mod_listener; + + struct pw_properties *stream_props; +}; + +static void module_destroy(void *data) +{ + struct module_tunnel_source_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 void serialize_dict(FILE *f, const struct spa_dict *dict) +{ + const struct spa_dict_item *it; + spa_dict_for_each(it, dict) { + size_t len = it->value ? strlen(it->value) : 0; + fprintf(f, " \"%s\" = ", it->key); + if (it->value == NULL) { + fprintf(f, "null"); + } else if ( spa_json_is_null(it->value, len) || + spa_json_is_float(it->value, len) || + spa_json_is_object(it->value, len)) { + fprintf(f, "%s", it->value); + } else { + size_t size = (len+1) * 4; + char str[size]; + spa_json_encode_string(str, size, it->value); + fprintf(f, "%s", str); + } + } +} + +static int module_tunnel_source_load(struct client *client, struct module *module) +{ + struct module_tunnel_source_data *data = module->user_data; + FILE *f; + char *args; + size_t size; + const char *server; + + server = pw_properties_get(module->props, "server"); + + f = open_memstream(&args, &size); + fprintf(f, "{"); + serialize_dict(f, &module->props->dict); + fprintf(f, " pulse.server.address = \"%s\" ", server); + fprintf(f, " tunnel.mode = capture "); + fprintf(f, " stream.props = {"); + serialize_dict(f, &data->stream_props->dict); + fprintf(f, " } }"); + fclose(f); + + data->mod = pw_context_load_module(module->impl->context, + "libpipewire-module-pulse-tunnel", + 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_tunnel_source_unload(struct client *client, struct module *module) +{ + struct module_tunnel_source_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_tunnel_source_methods = { + VERSION_MODULE_METHODS, + .load = module_tunnel_source_load, + .unload = module_tunnel_source_unload, +}; + +static const struct spa_dict_item module_tunnel_source_info[] = { + { PW_KEY_MODULE_AUTHOR, "Wim Taymans " }, + { PW_KEY_MODULE_DESCRIPTION, "Create a network source which connects to a remote PulseAudio server" }, + { PW_KEY_MODULE_USAGE, + "server=
" + "source= " + "source_name= " + "source_properties= " + "format= " + "channels= " + "rate= " + "channel_map= " + "cookie=" }, + { PW_KEY_MODULE_VERSION, PACKAGE_VERSION }, +}; + +static void audio_info_to_props(struct spa_audio_info_raw *info, struct pw_properties *props) +{ + char *s, *p; + uint32_t i; + + pw_properties_setf(props, SPA_KEY_AUDIO_CHANNELS, "%u", info->channels); + p = s = alloca(info->channels * 6); + for (i = 0; i < info->channels; i++) + p += snprintf(p, 6, "%s%s", i == 0 ? "" : ",", + channel_id2name(info->position[i])); + pw_properties_set(props, SPA_KEY_AUDIO_POSITION, s); +} + +struct module *create_module_tunnel_source(struct impl *impl, const char *argument) +{ + struct module *module; + struct module_tunnel_source_data *d; + struct pw_properties *props = NULL, *stream_props = NULL; + const char *str, *server, *remote_source_name; + struct spa_audio_info_raw info = { 0 }; + int res; + + props = pw_properties_new_dict(&SPA_DICT_INIT_ARRAY(module_tunnel_source_info)); + stream_props = pw_properties_new(NULL, NULL); + if (props == NULL || stream_props == NULL) { + res = -ENOMEM; + goto out; + } + if (argument) + module_args_add_props(props, argument); + + remote_source_name = pw_properties_get(props, "source"); + if (remote_source_name) + pw_properties_set(props, PW_KEY_NODE_TARGET, remote_source_name); + + if ((server = pw_properties_get(props, "server")) == NULL) { + pw_log_error("no server given"); + res = -EINVAL; + goto out; + } + + pw_properties_setf(stream_props, PW_KEY_NODE_DESCRIPTION, + _("Tunnel to %s/%s"), server, + remote_source_name ? remote_source_name : ""); + pw_properties_set(stream_props, PW_KEY_MEDIA_CLASS, "Audio/Source"); + + if ((str = pw_properties_get(props, "source_name")) != NULL) { + pw_properties_set(stream_props, PW_KEY_NODE_NAME, str); + pw_properties_set(props, "source_name", NULL); + } else { + pw_properties_setf(stream_props, PW_KEY_NODE_NAME, + "tunnel-source.%s", server); + } + if ((str = pw_properties_get(props, "source_properties")) != NULL) { + module_args_add_props(stream_props, str); + pw_properties_set(props, "source_properties", NULL); + } + if (module_args_to_audioinfo(impl, props, &info) < 0) { + res = -EINVAL; + goto out; + } + + audio_info_to_props(&info, stream_props); + + module = module_new(impl, &module_tunnel_source_methods, sizeof(*d)); + if (module == NULL) { + res = -errno; + goto out; + } + + module->props = props; + d = module->user_data; + d->module = module; + d->stream_props = stream_props; + + return module; +out: + if (props) + pw_properties_free(props); + if (stream_props) + pw_properties_free(stream_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 d77fe6cde..4497aa195 100644 --- a/src/modules/module-protocol-pulse/modules/registry.h +++ b/src/modules/module-protocol-pulse/modules/registry.h @@ -35,6 +35,8 @@ struct module *create_module_native_protocol_tcp(struct impl *impl, const char * struct module *create_module_null_sink(struct impl *impl, const char *argument); struct module *create_module_remap_sink(struct impl *impl, const char *argument); struct module *create_module_remap_source(struct impl *impl, const char *argument); +struct module *create_module_tunnel_sink(struct impl *impl, const char *argument); +struct module *create_module_tunnel_source(struct impl *impl, const char *argument); struct module *create_module_simple_protocol_tcp(struct impl *impl, const char *argument); struct module *create_module_pipe_sink(struct impl *impl, const char *argument);