From 33ddd5f760a087d0fa4d32cec58e195c1928753e Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 9 Apr 2021 18:27:30 +0200 Subject: [PATCH] pulse-server: implement module-native-protocol-tcp The module creates a tcp server on the given ip/port pair. --- .../module-native-protocol-tcp.c | 121 ++++++++++++++++++ src/modules/module-protocol-pulse/module.c | 6 +- .../module-protocol-pulse/pulse-server.c | 7 + 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/modules/module-protocol-pulse/module-native-protocol-tcp.c diff --git a/src/modules/module-protocol-pulse/module-native-protocol-tcp.c b/src/modules/module-protocol-pulse/module-native-protocol-tcp.c new file mode 100644 index 000000000..70504af40 --- /dev/null +++ b/src/modules/module-protocol-pulse/module-native-protocol-tcp.c @@ -0,0 +1,121 @@ +/* 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. + */ + +#define ERROR_RETURN(str) \ + { \ + pw_log_error(str); \ + res = -EINVAL; \ + goto out; \ + } + +struct module_native_protocol_tcp_data { + struct module *module; + struct server *server; +}; + +static int module_native_protocol_tcp_load(struct client *client, struct module *module) +{ + struct module_native_protocol_tcp_data *data = module->user_data; + struct impl *impl = client->impl; + char *address; + + address = strdup(pw_properties_get(module->props, "pulse.tcp")); + data->server = create_server(impl, address); + free(address); + + if (data->server == NULL) + return -errno; + + 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_native_protocol_tcp_unload(struct client *client, struct module *module) +{ + struct module_native_protocol_tcp_data *d = module->user_data; + + pw_log_info("unload module %p id:%u name:%s", module, module->idx, module->name); + + if (d->server != NULL) + server_free(d->server); + + return 0; +} + +static const struct module_methods module_native_protocol_tcp_methods = { + VERSION_MODULE_METHODS, + .load = module_native_protocol_tcp_load, + .unload = module_native_protocol_tcp_unload, +}; + +static const struct spa_dict_item module_native_protocol_tcp_info[] = { + { PW_KEY_MODULE_AUTHOR, "Wim Taymans " }, + { PW_KEY_MODULE_DESCRIPTION, "Native protocol (TCP sockets)" }, + { PW_KEY_MODULE_USAGE, "port= " + "listen=
" }, + { PW_KEY_MODULE_VERSION, PACKAGE_VERSION }, +}; + +static struct module *create_module_native_protocol_tcp(struct impl *impl, const char *argument) +{ + struct module *module; + struct module_native_protocol_tcp_data *d; + struct pw_properties *props = NULL; + const char *port, *listen; + int res; + + props = pw_properties_new_dict(&SPA_DICT_INIT_ARRAY(module_native_protocol_tcp_info)); + if (props == NULL) { + res = -errno; + goto out; + } + if (argument) + add_props(props, argument); + + if ((port = pw_properties_get(props, "port")) == NULL) + port = "4713"; + listen = pw_properties_get(props, "listen"); + + pw_properties_setf(props, "pulse.tcp", "tcp:%s%s%s", + listen ? listen : "", listen ? ":" : "", port); + + module = module_new(impl, &module_native_protocol_tcp_methods, sizeof(*d)); + if (module == NULL) { + res = -errno; + goto out; + } + + module->props = props; + d = module->user_data; + d->module = module; + + return module; +out: + if (props) + pw_properties_free(props); + errno = -res; + return NULL; +} diff --git a/src/modules/module-protocol-pulse/module.c b/src/modules/module-protocol-pulse/module.c index 58a874ad3..61b1daceb 100644 --- a/src/modules/module-protocol-pulse/module.c +++ b/src/modules/module-protocol-pulse/module.c @@ -177,12 +177,14 @@ static void add_props(struct pw_properties *props, const char *str) free(s); } -#include "module-null-sink.c" #include "module-loopback.c" +#include "module-null-sink.c" +#include "module-native-protocol-tcp.c" static const struct module_info module_list[] = { - { "module-null-sink", create_module_null_sink, }, { "module-loopback", create_module_loopback, }, + { "module-null-sink", create_module_null_sink, }, + { "module-native-protocol-tcp", create_module_native_protocol_tcp, }, { NULL, } }; diff --git a/src/modules/module-protocol-pulse/pulse-server.c b/src/modules/module-protocol-pulse/pulse-server.c index 575446473..79c8d39a3 100644 --- a/src/modules/module-protocol-pulse/pulse-server.c +++ b/src/modules/module-protocol-pulse/pulse-server.c @@ -294,6 +294,9 @@ struct impl { /* Functions that modules can use */ static void broadcast_subscribe_event(struct impl *impl, uint32_t mask, uint32_t event, uint32_t id); +static struct server *create_server(struct impl *impl, char *address); +static void server_free(struct server *server); + #include "collect.c" #include "module.c" #include "message-handler.c" @@ -5920,6 +5923,10 @@ on_connect(void *data, int fd, uint32_t mask) if (client->props == NULL) goto error; + pw_properties_setf(client->props, + "pulse.server.type", "%s", + server->type == SERVER_TYPE_INET ? "tcp" : "unix"); + client->routes = pw_properties_new(NULL, NULL); if (client->routes == NULL) goto error;