pipewire/src/modules/module-client-node.c

194 lines
5.7 KiB
C
Raw Normal View History

2017-06-05 10:53:54 +02:00
/* PipeWire
*
* Copyright © 2018 Wim Taymans
2017-06-05 10:53:54 +02:00
*
* 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:
2017-06-05 10:53:54 +02:00
*
* 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.
2017-06-05 10:53:54 +02:00
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <dlfcn.h>
#include "config.h"
2019-10-25 15:01:02 +02:00
#include <spa/utils/result.h>
#include <pipewire/impl.h>
2017-07-11 15:57:20 +02:00
protocol: add v0 compatibility For flatpaks we need to be able to support older v0 protocol clients. To handle this we have: - the connection detects an old client when it receives the first message. It can do this by checking the sequence number, on old versions it contains the message size and is never 0, on new clients the sequence number is 0. - We add a new signal at the start of the connection with the detected version number. This installs the right version of the core proxy. We also move the binding of the client until the hello message is received. This way we can have a new client connect (portal), hand over the connection to an old client, which then removes the client binding again in the hello request with a v0 version. There are some changes to the passing of fds in v0 vs v3 which need to investigated some more. - bump version of our interfaces to 3. This makes it possible to have v0 and v3 protocol marshal functions. - Add version number in the proxy. This is mostly automatically done internally based on the version numbers the library is compiled with. Where the version number was in the API before, it is now actually used to look up the right protocol marshal functions. For Proxies there is usually just 1 version, the current one. It is the server that will support different versions. - Add v0 compat marshal functions to convert from and to v0 format. This has some complications. v0 has a type map it keeps in sync with the server. For this we have a static type map with mappings to our own v3 types. Pods are mostly the same except for objects that used to have arbitrary pods in v0 vs spa_pod_prop in v3. Also convert between v0 spa_pod_prop and v3 spa_pod_choice. Formats and commands are also slightly different so handle those mappings as well. We only have marshal functions for the server side (resource) v0 functions. - Add v0 compatible client-node again. It's a bit tricky to map, v0 client-node basically lets the server to the mixing and teeing and just does the processing of the internal node.
2019-10-08 22:52:25 +02:00
#include "module-client-node/v0/client-node.h"
#include "module-client-node/client-node.h"
2017-06-05 10:53:54 +02:00
#define NAME "client-node"
2018-06-01 11:23:02 +02:00
static const struct spa_dict_item module_props[] = {
{ PW_KEY_MODULE_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
{ PW_KEY_MODULE_DESCRIPTION, "Allow clients to create and control remote nodes" },
{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
2018-06-01 11:23:02 +02:00
};
struct pw_proxy *pw_core_node_export(struct pw_core *core,
uint32_t type, struct pw_properties *props, void *object, size_t user_data_size);
struct pw_proxy *pw_core_spa_node_export(struct pw_core *core,
uint32_t type, struct pw_properties *props, void *object, size_t user_data_size);
struct pw_protocol *pw_protocol_native_ext_client_node_init(struct pw_context *context);
struct pw_protocol *pw_protocol_native_ext_client_node0_init(struct pw_context *context);
struct factory_data {
2019-12-11 11:45:27 +01:00
struct pw_impl_factory *this;
struct pw_module *module;
struct spa_hook module_listener;
struct pw_export_type export_node;
struct pw_export_type export_spanode;
2017-06-05 10:53:54 +02:00
};
static void *create_object(void *_data,
struct pw_resource *resource,
uint32_t type,
uint32_t version,
struct pw_properties *properties,
uint32_t new_id)
2017-06-05 10:53:54 +02:00
{
void *result;
struct pw_resource *node_resource;
2019-12-11 11:21:43 +01:00
struct pw_impl_client *client = pw_resource_get_client(resource);
2019-06-19 16:22:22 +02:00
int res;
2017-06-05 10:53:54 +02:00
2018-10-03 20:09:47 +02:00
node_resource = pw_resource_new(client, new_id, PW_PERM_RWX, type, version, 0);
2019-06-19 16:22:22 +02:00
if (node_resource == NULL) {
res = -errno;
goto error_resource;
}
protocol: add v0 compatibility For flatpaks we need to be able to support older v0 protocol clients. To handle this we have: - the connection detects an old client when it receives the first message. It can do this by checking the sequence number, on old versions it contains the message size and is never 0, on new clients the sequence number is 0. - We add a new signal at the start of the connection with the detected version number. This installs the right version of the core proxy. We also move the binding of the client until the hello message is received. This way we can have a new client connect (portal), hand over the connection to an old client, which then removes the client binding again in the hello request with a v0 version. There are some changes to the passing of fds in v0 vs v3 which need to investigated some more. - bump version of our interfaces to 3. This makes it possible to have v0 and v3 protocol marshal functions. - Add version number in the proxy. This is mostly automatically done internally based on the version numbers the library is compiled with. Where the version number was in the API before, it is now actually used to look up the right protocol marshal functions. For Proxies there is usually just 1 version, the current one. It is the server that will support different versions. - Add v0 compat marshal functions to convert from and to v0 format. This has some complications. v0 has a type map it keeps in sync with the server. For this we have a static type map with mappings to our own v3 types. Pods are mostly the same except for objects that used to have arbitrary pods in v0 vs spa_pod_prop in v3. Also convert between v0 spa_pod_prop and v3 spa_pod_choice. Formats and commands are also slightly different so handle those mappings as well. We only have marshal functions for the server side (resource) v0 functions. - Add v0 compatible client-node again. It's a bit tricky to map, v0 client-node basically lets the server to the mixing and teeing and just does the processing of the internal node.
2019-10-08 22:52:25 +02:00
if (version == 0) {
result = pw_client_node0_new(node_resource, properties);
} else {
result = pw_client_node_new(node_resource, properties, true);
}
2019-06-19 16:22:22 +02:00
if (result == NULL) {
res = -errno;
goto error_node;
}
return result;
2017-06-05 10:53:54 +02:00
2019-06-19 16:22:22 +02:00
error_resource:
pw_log_error("can't create resource: %s", spa_strerror(res));
pw_resource_errorf(resource, res, "can't create resource: %s", spa_strerror(res));
2019-06-19 16:22:22 +02:00
goto error_exit;
error_node:
pw_log_error("can't create node: %s", spa_strerror(res));
pw_resource_errorf(resource, res, "can't create node: %s", spa_strerror(res));
2019-06-19 16:22:22 +02:00
goto error_exit_free;
2019-06-20 11:04:34 +02:00
2019-06-19 16:22:22 +02:00
error_exit_free:
pw_resource_destroy(node_resource);
error_exit:
errno = -res;
2017-06-05 10:53:54 +02:00
return NULL;
}
2019-12-11 11:45:27 +01:00
static const struct pw_impl_factory_implementation impl_factory = {
PW_VERSION_IMPL_FACTORY_IMPLEMENTATION,
.create_object = create_object,
};
static void module_destroy(void *data)
{
struct factory_data *d = data;
spa_hook_remove(&d->module_listener);
spa_list_remove(&d->export_node.link);
spa_list_remove(&d->export_spanode.link);
2019-12-11 11:45:27 +01:00
pw_impl_factory_destroy(d->this);
}
static void module_registered(void *data)
{
struct factory_data *d = data;
struct pw_module *module = d->module;
2019-12-11 11:45:27 +01:00
struct pw_impl_factory *factory = d->this;
struct spa_dict_item items[1];
char id[16];
int res;
snprintf(id, sizeof(id), "%d", pw_global_get_id(pw_module_get_global(module)));
items[0] = SPA_DICT_ITEM_INIT(PW_KEY_MODULE_ID, id);
2019-12-11 11:45:27 +01:00
pw_impl_factory_update_properties(factory, &SPA_DICT_INIT(items, 1));
2019-12-11 11:45:27 +01:00
if ((res = pw_impl_factory_register(factory, NULL)) < 0) {
pw_log_error(NAME" %p: can't register factory: %s", factory, spa_strerror(res));
}
}
static const struct pw_module_events module_events = {
PW_VERSION_MODULE_EVENTS,
.destroy = module_destroy,
.registered = module_registered,
};
SPA_EXPORT
int pipewire__module_init(struct pw_module *module, const char *args)
2017-06-05 10:53:54 +02:00
{
struct pw_context *context = pw_module_get_context(module);
2019-12-11 11:45:27 +01:00
struct pw_impl_factory *factory;
struct factory_data *data;
2017-06-05 10:53:54 +02:00
2019-12-11 11:45:27 +01:00
factory = pw_impl_factory_new(context,
"client-node",
PW_TYPE_INTERFACE_ClientNode,
PW_VERSION_CLIENT_NODE,
NULL,
sizeof(*data));
if (factory == NULL)
2019-06-19 16:22:22 +02:00
return -errno;
2019-12-11 11:45:27 +01:00
data = pw_impl_factory_get_user_data(factory);
data->this = factory;
data->module = module;
2017-06-05 10:53:54 +02:00
pw_log_debug("module %p: new", module);
2017-06-05 10:53:54 +02:00
2019-12-11 11:45:27 +01:00
pw_impl_factory_set_implementation(factory,
&impl_factory,
data);
2017-06-05 10:53:54 +02:00
pw_protocol_native_ext_client_node_init(context);
pw_protocol_native_ext_client_node0_init(context);
data->export_node.type = PW_TYPE_INTERFACE_Node;
data->export_node.func = pw_core_node_export;
pw_context_register_export_type(context, &data->export_node);
data->export_spanode.type = SPA_TYPE_INTERFACE_Node;
data->export_spanode.func = pw_core_spa_node_export;
pw_context_register_export_type(context, &data->export_spanode);
pw_module_add_listener(module, &data->module_listener, &module_events, data);
2017-06-05 10:53:54 +02:00
2018-06-01 11:23:02 +02:00
pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
return 0;
2017-06-05 10:53:54 +02:00
}