2023-02-08 18:12:00 +01:00
|
|
|
/* PipeWire */
|
|
|
|
|
/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
|
|
|
|
|
/* SPDX-License-Identifier: MIT */
|
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>
|
|
|
|
|
|
2019-12-11 10:57:10 +01:00
|
|
|
#include <pipewire/impl.h>
|
2017-07-11 15:57:20 +02:00
|
|
|
|
2019-10-08 22:52:25 +02:00
|
|
|
#include "module-client-node/v0/client-node.h"
|
2017-06-21 09:03:29 +02:00
|
|
|
#include "module-client-node/client-node.h"
|
2017-06-05 10:53:54 +02:00
|
|
|
|
2023-11-18 15:11:03 +02:00
|
|
|
/** \page page_module_client_node Client Node
|
2021-06-24 14:06:30 +10:00
|
|
|
*/
|
|
|
|
|
|
2019-08-16 22:11:42 +02:00
|
|
|
#define NAME "client-node"
|
|
|
|
|
|
2021-09-16 15:35:10 +10:00
|
|
|
PW_LOG_TOPIC(mod_topic, "mod." NAME);
|
|
|
|
|
#define PW_LOG_TOPIC_DEFAULT mod_topic
|
|
|
|
|
|
2018-06-01 11:23:02 +02:00
|
|
|
static const struct spa_dict_item module_props[] = {
|
2019-05-24 15:47:48 +02:00
|
|
|
{ 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
|
|
|
};
|
|
|
|
|
|
2019-12-11 07:46:59 +01:00
|
|
|
struct pw_proxy *pw_core_node_export(struct pw_core *core,
|
2019-12-19 13:15:10 +01:00
|
|
|
const char *type, const struct spa_dict *props, void *object, size_t user_data_size);
|
2019-12-11 07:46:59 +01:00
|
|
|
struct pw_proxy *pw_core_spa_node_export(struct pw_core *core,
|
2019-12-19 13:15:10 +01:00
|
|
|
const char *type, const struct spa_dict *props, void *object, size_t user_data_size);
|
2019-01-31 11:02:13 +01:00
|
|
|
|
2019-12-10 18:19:56 +01:00
|
|
|
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);
|
2017-06-21 12:11:54 +02:00
|
|
|
|
2017-08-04 10:18:54 +02:00
|
|
|
struct factory_data {
|
2022-05-06 12:11:20 +02:00
|
|
|
struct pw_impl_factory *factory;
|
|
|
|
|
struct spa_hook factory_listener;
|
2017-08-22 18:30:10 +02:00
|
|
|
|
2019-12-11 11:57:56 +01:00
|
|
|
struct pw_impl_module *module;
|
2017-08-22 18:30:10 +02:00
|
|
|
struct spa_hook module_listener;
|
2019-01-31 11:02:13 +01:00
|
|
|
|
|
|
|
|
struct pw_export_type export_node;
|
|
|
|
|
struct pw_export_type export_spanode;
|
2017-06-05 10:53:54 +02:00
|
|
|
};
|
|
|
|
|
|
2017-09-17 16:47:03 +02:00
|
|
|
static void *create_object(void *_data,
|
|
|
|
|
struct pw_resource *resource,
|
2019-12-19 13:15:10 +01:00
|
|
|
const char *type,
|
2017-09-17 16:47:03 +02:00
|
|
|
uint32_t version,
|
|
|
|
|
struct pw_properties *properties,
|
|
|
|
|
uint32_t new_id)
|
2017-06-05 10:53:54 +02:00
|
|
|
{
|
2018-04-13 20:06:37 +02:00
|
|
|
void *result;
|
2017-09-17 16:47:03 +02:00
|
|
|
struct pw_resource *node_resource;
|
2021-11-19 09:58:49 +10:00
|
|
|
struct pw_impl_client *client;
|
2019-06-19 16:22:22 +02:00
|
|
|
int res;
|
2017-06-05 10:53:54 +02:00
|
|
|
|
2021-11-19 09:58:49 +10:00
|
|
|
if (resource == NULL) {
|
|
|
|
|
res = -EINVAL;
|
|
|
|
|
goto error_exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client = pw_resource_get_client(resource);
|
2020-07-30 16:26:29 +02:00
|
|
|
node_resource = pw_resource_new(client, new_id, PW_PERM_ALL, type, version, 0);
|
2019-06-19 16:22:22 +02:00
|
|
|
if (node_resource == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto error_resource;
|
|
|
|
|
}
|
2017-09-17 16:47:03 +02:00
|
|
|
|
2019-10-08 22:52:25 +02:00
|
|
|
if (version == 0) {
|
2019-12-11 16:13:46 +01:00
|
|
|
result = pw_impl_client_node0_new(node_resource, properties);
|
2019-10-08 22:52:25 +02:00
|
|
|
} else {
|
2019-12-11 16:13:46 +01:00
|
|
|
result = pw_impl_client_node_new(node_resource, properties, true);
|
2019-10-08 22:52:25 +02:00
|
|
|
}
|
2019-06-19 16:22:22 +02:00
|
|
|
if (result == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto error_node;
|
|
|
|
|
}
|
2018-04-13 20:06:37 +02:00
|
|
|
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));
|
2020-02-25 16:38:13 +01:00
|
|
|
pw_resource_errorf_id(resource, new_id, 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));
|
2020-05-20 15:18:19 +02:00
|
|
|
pw_resource_errorf_id(resource, new_id, res, "can't create node: %s", spa_strerror(res));
|
|
|
|
|
goto error_exit;
|
2019-06-19 16:22:22 +02:00
|
|
|
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,
|
2017-09-17 16:47:03 +02:00
|
|
|
.create_object = create_object,
|
2017-08-04 10:18:54 +02:00
|
|
|
};
|
|
|
|
|
|
2022-05-06 12:11:20 +02:00
|
|
|
static void factory_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct factory_data *d = data;
|
|
|
|
|
spa_hook_remove(&d->factory_listener);
|
|
|
|
|
d->factory = NULL;
|
|
|
|
|
if (d->module)
|
|
|
|
|
pw_impl_module_destroy(d->module);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_impl_factory_events factory_events = {
|
|
|
|
|
PW_VERSION_IMPL_FACTORY_EVENTS,
|
|
|
|
|
.destroy = factory_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-22 18:30:10 +02:00
|
|
|
static void module_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct factory_data *d = data;
|
|
|
|
|
|
|
|
|
|
spa_hook_remove(&d->module_listener);
|
2019-01-31 11:02:13 +01:00
|
|
|
spa_list_remove(&d->export_node.link);
|
|
|
|
|
spa_list_remove(&d->export_spanode.link);
|
|
|
|
|
|
2022-05-06 12:11:20 +02:00
|
|
|
d->module = NULL;
|
|
|
|
|
if (d->factory)
|
|
|
|
|
pw_impl_factory_destroy(d->factory);
|
2017-08-22 18:30:10 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-16 22:11:42 +02:00
|
|
|
static void module_registered(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct factory_data *d = data;
|
2019-12-11 11:57:56 +01:00
|
|
|
struct pw_impl_module *module = d->module;
|
2022-05-06 12:11:20 +02:00
|
|
|
struct pw_impl_factory *factory = d->factory;
|
2019-08-16 22:11:42 +02:00
|
|
|
struct spa_dict_item items[1];
|
|
|
|
|
char id[16];
|
|
|
|
|
int res;
|
|
|
|
|
|
2019-12-11 11:57:56 +01:00
|
|
|
snprintf(id, sizeof(id), "%d", pw_global_get_id(pw_impl_module_get_global(module)));
|
2019-08-16 22:11:42 +02:00
|
|
|
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-08-16 22:11:42 +02:00
|
|
|
|
2019-12-11 11:45:27 +01:00
|
|
|
if ((res = pw_impl_factory_register(factory, NULL)) < 0) {
|
2021-09-16 15:35:10 +10:00
|
|
|
pw_log_error("%p: can't register factory: %s", factory, spa_strerror(res));
|
2019-08-16 22:11:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-11 11:57:56 +01:00
|
|
|
static const struct pw_impl_module_events module_events = {
|
|
|
|
|
PW_VERSION_IMPL_MODULE_EVENTS,
|
2017-08-22 18:30:10 +02:00
|
|
|
.destroy = module_destroy,
|
2019-08-16 22:11:42 +02:00
|
|
|
.registered = module_registered,
|
2017-08-22 18:30:10 +02:00
|
|
|
};
|
|
|
|
|
|
2019-08-16 22:11:42 +02:00
|
|
|
SPA_EXPORT
|
2019-12-11 11:57:56 +01:00
|
|
|
int pipewire__module_init(struct pw_impl_module *module, const char *args)
|
2017-06-05 10:53:54 +02:00
|
|
|
{
|
2019-12-11 11:57:56 +01:00
|
|
|
struct pw_context *context = pw_impl_module_get_context(module);
|
2019-12-11 11:45:27 +01:00
|
|
|
struct pw_impl_factory *factory;
|
2017-08-04 10:18:54 +02:00
|
|
|
struct factory_data *data;
|
2022-04-11 21:15:13 +02:00
|
|
|
int res;
|
2017-06-05 10:53:54 +02:00
|
|
|
|
2021-09-16 15:35:10 +10:00
|
|
|
PW_LOG_TOPIC_INIT(mod_topic);
|
|
|
|
|
|
2019-12-11 17:50:27 +01:00
|
|
|
factory = pw_context_create_factory(context,
|
2017-09-17 16:47:03 +02:00
|
|
|
"client-node",
|
2018-08-27 15:03:11 +02:00
|
|
|
PW_TYPE_INTERFACE_ClientNode,
|
2017-09-17 16:47:03 +02:00
|
|
|
PW_VERSION_CLIENT_NODE,
|
|
|
|
|
NULL,
|
|
|
|
|
sizeof(*data));
|
2017-08-04 10:18:54 +02:00
|
|
|
if (factory == NULL)
|
2019-06-19 16:22:22 +02:00
|
|
|
return -errno;
|
2017-07-18 14:58:14 +02:00
|
|
|
|
2019-12-11 11:45:27 +01:00
|
|
|
data = pw_impl_factory_get_user_data(factory);
|
2022-05-06 12:11:20 +02:00
|
|
|
data->factory = factory;
|
2018-07-03 21:43:21 +02:00
|
|
|
data->module = module;
|
2017-06-05 10:53:54 +02:00
|
|
|
|
2017-08-04 10:18: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,
|
2017-09-17 16:47:03 +02:00
|
|
|
&impl_factory,
|
|
|
|
|
data);
|
2017-06-05 10:53:54 +02:00
|
|
|
|
2019-01-31 11:02:13 +01:00
|
|
|
data->export_node.type = PW_TYPE_INTERFACE_Node;
|
2019-12-11 07:46:59 +01:00
|
|
|
data->export_node.func = pw_core_node_export;
|
2022-04-11 21:15:13 +02:00
|
|
|
if ((res = pw_context_register_export_type(context, &data->export_node)) < 0)
|
|
|
|
|
goto error;
|
2019-01-31 11:02:13 +01:00
|
|
|
|
|
|
|
|
data->export_spanode.type = SPA_TYPE_INTERFACE_Node;
|
2019-12-11 07:46:59 +01:00
|
|
|
data->export_spanode.func = pw_core_spa_node_export;
|
2022-04-11 21:15:13 +02:00
|
|
|
if ((res = pw_context_register_export_type(context, &data->export_spanode)) < 0)
|
|
|
|
|
goto error_remove;
|
|
|
|
|
|
|
|
|
|
pw_protocol_native_ext_client_node_init(context);
|
|
|
|
|
pw_protocol_native_ext_client_node0_init(context);
|
2019-01-31 11:02:13 +01:00
|
|
|
|
2022-05-06 12:11:20 +02:00
|
|
|
pw_impl_factory_add_listener(factory, &data->factory_listener, &factory_events, data);
|
2019-12-11 11:57:56 +01:00
|
|
|
pw_impl_module_add_listener(module, &data->module_listener, &module_events, data);
|
2017-06-05 10:53:54 +02:00
|
|
|
|
2019-12-11 11:57:56 +01:00
|
|
|
pw_impl_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
|
2018-06-01 11:23:02 +02:00
|
|
|
|
2017-12-18 11:38:30 +01:00
|
|
|
return 0;
|
2022-04-11 21:15:13 +02:00
|
|
|
error_remove:
|
|
|
|
|
spa_list_remove(&data->export_node.link);
|
|
|
|
|
error:
|
2022-05-06 12:11:20 +02:00
|
|
|
pw_impl_factory_destroy(data->factory);
|
2022-04-11 21:15:13 +02:00
|
|
|
return res;
|
2017-06-05 10:53:54 +02:00
|
|
|
}
|