2018-01-16 16:53:08 +01:00
|
|
|
/* PipeWire
|
|
|
|
|
* Copyright (C) 2018 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include "pipewire/core.h"
|
|
|
|
|
#include "pipewire/interfaces.h"
|
|
|
|
|
#include "pipewire/log.h"
|
|
|
|
|
#include "pipewire/module.h"
|
|
|
|
|
#include "pipewire/link.h"
|
2018-09-10 18:49:41 +02:00
|
|
|
#include "pipewire/private.h"
|
2018-01-16 16:53:08 +01:00
|
|
|
|
2018-06-01 11:23:02 +02:00
|
|
|
static const struct spa_dict_item module_props[] = {
|
|
|
|
|
{ PW_MODULE_PROP_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
|
|
|
|
|
{ PW_MODULE_PROP_DESCRIPTION, "Allow clients to create links" },
|
|
|
|
|
{ PW_MODULE_PROP_VERSION, PACKAGE_VERSION },
|
|
|
|
|
};
|
|
|
|
|
|
2018-01-16 16:53:08 +01:00
|
|
|
struct factory_data {
|
|
|
|
|
struct pw_factory *this;
|
|
|
|
|
struct pw_properties *properties;
|
|
|
|
|
|
2018-09-18 12:33:01 +02:00
|
|
|
struct spa_list link_list;
|
|
|
|
|
|
2018-01-16 16:53:08 +01:00
|
|
|
struct spa_hook module_listener;
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-18 12:33:01 +02:00
|
|
|
struct link_data {
|
|
|
|
|
struct factory_data *data;
|
|
|
|
|
struct pw_link *link;
|
|
|
|
|
struct spa_list l;
|
|
|
|
|
struct spa_hook link_listener;
|
|
|
|
|
struct spa_hook resource_listener;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void resource_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct link_data *ld = data;
|
2018-09-24 09:30:14 +02:00
|
|
|
spa_hook_remove(&ld->resource_listener);
|
2018-09-20 10:13:00 +02:00
|
|
|
if (ld->link)
|
2018-09-18 12:33:01 +02:00
|
|
|
pw_link_destroy(ld->link);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_resource_events resource_events = {
|
|
|
|
|
PW_VERSION_RESOURCE_EVENTS,
|
|
|
|
|
.destroy = resource_destroy
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void link_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct link_data *ld = data;
|
2018-09-20 10:13:00 +02:00
|
|
|
spa_list_remove(&ld->l);
|
2018-09-18 12:33:01 +02:00
|
|
|
ld->link = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_link_events link_events = {
|
|
|
|
|
PW_VERSION_LINK_EVENTS,
|
|
|
|
|
.destroy = link_destroy
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-10 18:49:41 +02:00
|
|
|
static struct pw_port *get_port(struct pw_node *node, enum spa_direction direction)
|
|
|
|
|
{
|
|
|
|
|
struct pw_port *p;
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
p = pw_node_find_port(node, direction, SPA_ID_INVALID);
|
|
|
|
|
|
|
|
|
|
if (p == NULL || pw_port_is_linked(p)) {
|
|
|
|
|
uint32_t port_id;
|
|
|
|
|
|
|
|
|
|
port_id = pw_node_get_free_port_id(node, direction);
|
|
|
|
|
if (port_id == SPA_ID_INVALID)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
p = pw_port_new(direction, port_id, NULL, 0);
|
|
|
|
|
if (p == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if ((res = pw_port_add(p, node)) < 0) {
|
|
|
|
|
pw_log_warn("can't add port: %s", spa_strerror(res));
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-01-16 16:53:08 +01: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)
|
|
|
|
|
{
|
2018-09-18 12:33:01 +02:00
|
|
|
struct factory_data *d = _data;
|
2018-10-03 19:24:53 +02:00
|
|
|
struct pw_client *client = NULL;
|
2018-01-16 16:53:08 +01:00
|
|
|
struct pw_node *output_node, *input_node;
|
|
|
|
|
struct pw_port *outport, *inport;
|
|
|
|
|
struct pw_core *core;
|
|
|
|
|
struct pw_global *global;
|
|
|
|
|
struct pw_link *link;
|
|
|
|
|
uint32_t output_node_id, input_node_id;
|
|
|
|
|
uint32_t output_port_id, input_port_id;
|
2018-09-18 12:33:01 +02:00
|
|
|
struct link_data *ld;
|
|
|
|
|
struct pw_resource *bound_resource;
|
2018-01-16 16:53:08 +01:00
|
|
|
char *error;
|
|
|
|
|
const char *str;
|
|
|
|
|
int res;
|
2018-09-21 16:45:52 +02:00
|
|
|
bool linger;
|
2018-01-16 16:53:08 +01:00
|
|
|
|
2018-10-03 19:24:53 +02:00
|
|
|
client = pw_resource_get_client(resource);
|
|
|
|
|
core = pw_client_get_core(client);
|
2018-01-16 16:53:08 +01:00
|
|
|
|
|
|
|
|
if (properties == NULL)
|
|
|
|
|
goto no_properties;
|
|
|
|
|
|
|
|
|
|
if ((str = pw_properties_get(properties, PW_LINK_OUTPUT_NODE_ID)) == NULL)
|
|
|
|
|
goto no_properties;
|
|
|
|
|
|
|
|
|
|
output_node_id = pw_properties_parse_int(str);
|
|
|
|
|
|
|
|
|
|
if ((str = pw_properties_get(properties, PW_LINK_INPUT_NODE_ID)) == NULL)
|
|
|
|
|
goto no_properties;
|
|
|
|
|
|
|
|
|
|
input_node_id = pw_properties_parse_int(str);
|
|
|
|
|
|
|
|
|
|
str = pw_properties_get(properties, PW_LINK_OUTPUT_PORT_ID);
|
|
|
|
|
output_port_id = str ? pw_properties_parse_int(str) : -1;
|
|
|
|
|
|
|
|
|
|
str = pw_properties_get(properties, PW_LINK_INPUT_PORT_ID);
|
|
|
|
|
input_port_id = str ? pw_properties_parse_int(str) : -1;
|
|
|
|
|
|
2018-01-24 11:41:40 +01:00
|
|
|
global = pw_core_find_global(core, output_node_id);
|
2018-08-27 15:03:11 +02:00
|
|
|
if (global == NULL || pw_global_get_type(global) != PW_TYPE_INTERFACE_Node)
|
2018-01-16 16:53:08 +01:00
|
|
|
goto no_output;
|
|
|
|
|
|
|
|
|
|
output_node = pw_global_get_object(global);
|
|
|
|
|
|
2018-01-24 11:41:40 +01:00
|
|
|
global = pw_core_find_global(core, input_node_id);
|
2018-08-27 15:03:11 +02:00
|
|
|
if (global == NULL || pw_global_get_type(global) != PW_TYPE_INTERFACE_Node)
|
2018-01-16 16:53:08 +01:00
|
|
|
goto no_input;
|
|
|
|
|
|
|
|
|
|
input_node = pw_global_get_object(global);
|
|
|
|
|
|
2018-09-10 18:49:41 +02:00
|
|
|
if (output_port_id == -1) {
|
|
|
|
|
outport = get_port(output_node, SPA_DIRECTION_OUTPUT);
|
|
|
|
|
}
|
2018-02-15 17:49:04 +01:00
|
|
|
else {
|
|
|
|
|
global = pw_core_find_global(core, output_port_id);
|
2018-08-27 15:03:11 +02:00
|
|
|
if (global == NULL || pw_global_get_type(global) != PW_TYPE_INTERFACE_Port)
|
2018-02-15 17:49:04 +01:00
|
|
|
goto no_output_port;
|
|
|
|
|
|
|
|
|
|
outport = pw_global_get_object(global);
|
|
|
|
|
}
|
2018-01-16 16:53:08 +01:00
|
|
|
if (outport == NULL)
|
|
|
|
|
goto no_output_port;
|
|
|
|
|
|
|
|
|
|
if (input_port_id == -1)
|
2018-09-10 18:49:41 +02:00
|
|
|
inport = get_port(input_node, SPA_DIRECTION_INPUT);
|
2018-02-15 17:49:04 +01:00
|
|
|
else {
|
|
|
|
|
global = pw_core_find_global(core, input_port_id);
|
2018-08-27 15:03:11 +02:00
|
|
|
if (global == NULL || pw_global_get_type(global) != PW_TYPE_INTERFACE_Port)
|
2018-02-15 17:49:04 +01:00
|
|
|
goto no_output_port;
|
|
|
|
|
|
|
|
|
|
inport = pw_global_get_object(global);
|
|
|
|
|
}
|
2018-01-16 16:53:08 +01:00
|
|
|
if (inport == NULL)
|
|
|
|
|
goto no_input_port;
|
|
|
|
|
|
2018-09-21 16:45:52 +02:00
|
|
|
str = pw_properties_get(properties, "object.linger");
|
|
|
|
|
linger = str ? pw_properties_parse_bool(str) : false;
|
|
|
|
|
|
2018-09-18 12:33:01 +02:00
|
|
|
link = pw_link_new(core, outport, inport, NULL, properties, &error, sizeof(struct link_data));
|
2018-01-16 16:53:08 +01:00
|
|
|
if (link == NULL)
|
|
|
|
|
goto no_mem;
|
|
|
|
|
|
2018-09-18 12:33:01 +02:00
|
|
|
ld = pw_link_get_user_data(link);
|
|
|
|
|
ld->data = d;
|
|
|
|
|
ld->link = link;
|
|
|
|
|
spa_list_append(&d->link_list, &ld->l);
|
2018-02-27 12:49:22 +01:00
|
|
|
|
2018-09-18 12:33:01 +02:00
|
|
|
pw_link_add_listener(link, &ld->link_listener, &link_events, ld);
|
2018-09-21 16:45:52 +02:00
|
|
|
pw_link_register(link,
|
|
|
|
|
linger ? NULL : client,
|
|
|
|
|
linger ? NULL : pw_client_get_global(client),
|
|
|
|
|
NULL);
|
2018-01-16 16:53:08 +01:00
|
|
|
|
2018-09-18 12:33:01 +02:00
|
|
|
properties = NULL;
|
|
|
|
|
|
2018-01-16 16:53:08 +01:00
|
|
|
res = pw_global_bind(pw_link_get_global(link), client, PW_PERM_RWX, PW_VERSION_LINK, new_id);
|
|
|
|
|
if (res < 0)
|
|
|
|
|
goto no_bind;
|
|
|
|
|
|
2018-09-21 16:45:52 +02:00
|
|
|
if (!linger) {
|
|
|
|
|
if ((bound_resource = pw_client_find_resource(client, new_id)) == NULL)
|
|
|
|
|
goto no_bind;
|
2018-09-18 12:33:01 +02:00
|
|
|
|
2018-09-21 16:45:52 +02:00
|
|
|
pw_resource_add_listener(bound_resource, &ld->resource_listener, &resource_events, ld);
|
|
|
|
|
}
|
2018-09-18 12:33:01 +02:00
|
|
|
|
2018-01-16 16:53:08 +01:00
|
|
|
return link;
|
|
|
|
|
|
|
|
|
|
no_properties:
|
|
|
|
|
pw_log_error("link-factory needs properties");
|
2018-10-03 19:24:53 +02:00
|
|
|
pw_resource_error(resource, new_id, -EINVAL, "no properties");
|
2018-01-16 16:53:08 +01:00
|
|
|
goto done;
|
|
|
|
|
no_output:
|
|
|
|
|
pw_log_error("link-factory unknown output node %d", output_node_id);
|
2018-10-03 19:24:53 +02:00
|
|
|
pw_resource_error(resource, new_id, -EINVAL, "unknown output node");
|
2018-01-16 16:53:08 +01:00
|
|
|
goto done;
|
|
|
|
|
no_input:
|
|
|
|
|
pw_log_error("link-factory unknown input node %d", input_node_id);
|
2018-10-03 19:24:53 +02:00
|
|
|
pw_resource_error(resource, new_id, -EINVAL, "unknown input node");
|
2018-01-16 16:53:08 +01:00
|
|
|
goto done;
|
|
|
|
|
no_output_port:
|
|
|
|
|
pw_log_error("link-factory unknown output port %d", output_port_id);
|
2018-10-03 19:24:53 +02:00
|
|
|
pw_resource_error(resource, new_id, -EINVAL, "unknown output port");
|
2018-01-16 16:53:08 +01:00
|
|
|
goto done;
|
|
|
|
|
no_input_port:
|
|
|
|
|
pw_log_error("link-factory unknown input port %d", input_port_id);
|
2018-10-03 19:24:53 +02:00
|
|
|
pw_resource_error(resource, new_id, -EINVAL, "unknown input port");
|
2018-01-16 16:53:08 +01:00
|
|
|
goto done;
|
|
|
|
|
no_mem:
|
2018-10-17 10:02:44 +02:00
|
|
|
pw_log_error("can't create link: %s", error);
|
|
|
|
|
pw_resource_error(resource, new_id, -ENOMEM, error);
|
2018-01-16 16:53:08 +01:00
|
|
|
goto done;
|
|
|
|
|
no_bind:
|
2018-10-03 19:24:53 +02:00
|
|
|
pw_resource_error(resource, new_id, res, "can't bind link");
|
2018-01-16 16:53:08 +01:00
|
|
|
goto done;
|
|
|
|
|
done:
|
|
|
|
|
if (properties)
|
|
|
|
|
pw_properties_free(properties);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_factory_implementation impl_factory = {
|
|
|
|
|
PW_VERSION_FACTORY_IMPLEMENTATION,
|
|
|
|
|
.create_object = create_object,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void module_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct factory_data *d = data;
|
2018-09-18 12:33:01 +02:00
|
|
|
struct link_data *ld, *t;
|
2018-01-16 16:53:08 +01:00
|
|
|
|
|
|
|
|
spa_hook_remove(&d->module_listener);
|
|
|
|
|
|
2018-09-18 12:33:01 +02:00
|
|
|
spa_list_for_each_safe(ld, t, &d->link_list, l)
|
|
|
|
|
pw_link_destroy(ld->link);
|
|
|
|
|
|
2018-01-16 16:53:08 +01:00
|
|
|
if (d->properties)
|
|
|
|
|
pw_properties_free(d->properties);
|
|
|
|
|
|
|
|
|
|
pw_factory_destroy(d->this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_module_events module_events = {
|
|
|
|
|
PW_VERSION_MODULE_EVENTS,
|
|
|
|
|
.destroy = module_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int module_init(struct pw_module *module, struct pw_properties *properties)
|
|
|
|
|
{
|
|
|
|
|
struct pw_core *core = pw_module_get_core(module);
|
|
|
|
|
struct pw_factory *factory;
|
|
|
|
|
struct factory_data *data;
|
|
|
|
|
|
|
|
|
|
factory = pw_factory_new(core,
|
|
|
|
|
"link-factory",
|
2018-08-27 15:03:11 +02:00
|
|
|
PW_TYPE_INTERFACE_Link,
|
2018-01-16 16:53:08 +01:00
|
|
|
PW_VERSION_LINK,
|
|
|
|
|
NULL,
|
|
|
|
|
sizeof(*data));
|
|
|
|
|
if (factory == NULL)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
data = pw_factory_get_user_data(factory);
|
|
|
|
|
data->this = factory;
|
|
|
|
|
data->properties = properties;
|
2018-09-18 12:33:01 +02:00
|
|
|
spa_list_init(&data->link_list);
|
2018-01-16 16:53:08 +01:00
|
|
|
|
|
|
|
|
pw_log_debug("module %p: new", module);
|
|
|
|
|
|
|
|
|
|
pw_factory_set_implementation(factory,
|
|
|
|
|
&impl_factory,
|
|
|
|
|
data);
|
|
|
|
|
|
2018-01-19 11:18:17 +01:00
|
|
|
pw_factory_register(factory, NULL, pw_module_get_global(module), NULL);
|
2018-01-16 16:53:08 +01:00
|
|
|
|
|
|
|
|
pw_module_add_listener(module, &data->module_listener, &module_events, data);
|
|
|
|
|
|
2018-06-01 11:23:02 +02:00
|
|
|
pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
|
|
|
|
|
|
2018-01-16 16:53:08 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pipewire__module_init(struct pw_module *module, const char *args)
|
|
|
|
|
{
|
|
|
|
|
return module_init(module, NULL);
|
|
|
|
|
}
|