core: remove create-link

Make a link factory and use create-object to make links. That way
we can have different kinds of links based on the factory and we
can also hide the factory when link creation should be blocked.
This commit is contained in:
Wim Taymans 2018-01-16 16:53:08 +01:00
parent f1345f91a8
commit 67b06d4ad2
7 changed files with 250 additions and 244 deletions

View file

@ -48,6 +48,16 @@ pipewire_module_client_node = shared_library('pipewire-module-client-node',
dependencies : [mathlib, dl_lib, pipewire_dep],
)
pipewire_module_link_factory = shared_library('pipewire-module-link-factory',
[ 'module-link-factory.c' ],
c_args : pipewire_module_c_args,
include_directories : [configinc, spa_inc],
link_with : spalib,
install : true,
install_dir : modules_install_dir,
dependencies : [mathlib, dl_lib, pipewire_dep],
)
#pipewire_module_protocol_dbus = shared_library('pipewire-module-protocol-dbus', [ 'module-protocol-dbus.c', gdbus_target ],
# c_args : pipewire_module_c_args,
# include_directories : [configinc, spa_inc],

View file

@ -427,40 +427,9 @@ static void do_create_object(void *data,
return;
}
static void
do_create_link(void *data,
uint32_t output_node_id,
uint32_t output_port_id,
uint32_t input_node_id,
uint32_t input_port_id,
const struct spa_pod *filter,
const struct spa_dict *props,
uint32_t new_id)
{
struct resource *resource = data;
struct client_info *cinfo = resource->cinfo;
if (cinfo->is_sandboxed) {
pw_resource_error(resource->resource, -EPERM, "not allowed");
return;
}
pw_resource_do_parent(resource->resource,
&resource->override,
struct pw_core_proxy_methods,
create_link,
output_node_id,
output_port_id,
input_node_id,
input_port_id,
filter,
props,
new_id);
}
static const struct pw_core_proxy_methods core_override = {
PW_VERSION_CORE_PROXY_METHODS,
.create_object = do_create_object,
.create_link = do_create_link,
};
static void client_resource_impl(void *data, struct pw_resource *resource)

View file

@ -0,0 +1,219 @@
/* 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"
struct factory_data {
struct pw_factory *this;
struct pw_properties *properties;
struct spa_hook module_listener;
};
static void *create_object(void *_data,
struct pw_resource *resource,
uint32_t type,
uint32_t version,
struct pw_properties *properties,
uint32_t new_id)
{
struct pw_client *client;
struct pw_node *output_node, *input_node;
struct pw_port *outport, *inport;
struct pw_core *core;
struct pw_type *t;
struct pw_global *global;
struct pw_link *link;
uint32_t output_node_id, input_node_id;
uint32_t output_port_id, input_port_id;
char *error;
const char *str;
int res;
if (resource == NULL)
goto no_resource;
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;
client = pw_resource_get_client(resource);
core = pw_client_get_core(client);
t = pw_core_get_type(core);
global = pw_core_find_global(core, output_node_id);
if (global == NULL || pw_global_get_type(global) != t->node)
goto no_output;
output_node = pw_global_get_object(global);
global = pw_core_find_global(core, input_node_id);
if (global == NULL || pw_global_get_type(global) != t->node)
goto no_input;
input_node = pw_global_get_object(global);
if (output_port_id == -1)
outport = pw_node_get_free_port(output_node, SPA_DIRECTION_OUTPUT);
else
outport = pw_node_find_port(output_node, SPA_DIRECTION_OUTPUT, output_port_id);
if (outport == NULL)
goto no_output_port;
if (input_port_id == -1)
inport = pw_node_get_free_port(input_node, SPA_DIRECTION_INPUT);
else
inport = pw_node_find_port(input_node, SPA_DIRECTION_INPUT, input_port_id);
if (inport == NULL)
goto no_input_port;
link = pw_link_new(core, outport, inport, NULL, NULL, &error, 0);
if (link == NULL)
goto no_mem;
pw_link_register(link, client, pw_client_get_global(client));
res = pw_global_bind(pw_link_get_global(link), client, PW_PERM_RWX, PW_VERSION_LINK, new_id);
if (res < 0)
goto no_bind;
return link;
no_resource:
pw_log_error("link factory needs a resource");
pw_resource_error(resource, -EINVAL, "no resource");
goto done;
no_properties:
pw_log_error("link-factory needs properties");
pw_resource_error(resource, -EINVAL, "no properties");
goto done;
no_output:
pw_log_error("link-factory unknown output node %d", output_node_id);
pw_resource_error(resource, -EINVAL, "unknown output node");
goto done;
no_input:
pw_log_error("link-factory unknown input node %d", input_node_id);
pw_resource_error(resource, -EINVAL, "unknown input node");
goto done;
no_output_port:
pw_log_error("link-factory unknown output port %d", output_port_id);
pw_resource_error(resource, -EINVAL, "unknown output port");
goto done;
no_input_port:
pw_log_error("link-factory unknown input port %d", input_port_id);
pw_resource_error(resource, -EINVAL, "unknown input port");
goto done;
no_mem:
pw_log_error("can't create link");
pw_resource_error(resource, -ENOMEM, "no memory");
goto done;
no_bind:
pw_resource_error(resource, res, "can't bind link");
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;
spa_hook_remove(&d->module_listener);
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_type *t = pw_core_get_type(core);
struct pw_factory *factory;
struct factory_data *data;
factory = pw_factory_new(core,
"link-factory",
t->link,
PW_VERSION_LINK,
NULL,
sizeof(*data));
if (factory == NULL)
return -ENOMEM;
data = pw_factory_get_user_data(factory);
data->this = factory;
data->properties = properties;
pw_log_debug("module %p: new", module);
pw_factory_set_implementation(factory,
&impl_factory,
data);
pw_factory_register(factory, NULL, pw_module_get_global(module));
pw_module_add_listener(module, &data->module_listener, &module_events, data);
return 0;
}
int pipewire__module_init(struct pw_module *module, const char *args)
{
return module_init(module, NULL);
}

View file

@ -133,45 +133,6 @@ core_marshal_create_object(void *object,
pw_protocol_native_end_proxy(proxy, b);
}
static void
core_marshal_create_link(void *object,
uint32_t output_node_id,
uint32_t output_port_id,
uint32_t input_node_id,
uint32_t input_port_id,
const struct spa_pod *filter,
const struct spa_dict *props,
uint32_t new_id)
{
struct pw_proxy *proxy = object;
struct spa_pod_builder *b;
uint32_t i, n_items;
b = pw_protocol_native_begin_proxy(proxy, PW_CORE_PROXY_METHOD_CREATE_LINK);
n_items = props ? props->n_items : 0;
spa_pod_builder_add(b,
"["
"i", output_node_id,
"i", output_port_id,
"i", input_node_id,
"i", input_port_id,
"P", filter,
"i", n_items, NULL);
for (i = 0; i < n_items; i++) {
spa_pod_builder_add(b,
"s", props->items[i].key,
"s", props->items[i].value, NULL);
}
spa_pod_builder_add(b,
"i", new_id,
"]", NULL);
pw_protocol_native_end_proxy(proxy, b);
}
static void
core_marshal_update_types_client(void *object, uint32_t first_id, const char **types, uint32_t n_types)
{
@ -502,45 +463,6 @@ static int core_demarshal_create_object(void *object, void *data, size_t size)
return 0;
}
static int core_demarshal_create_link(void *object, void *data, size_t size)
{
struct pw_resource *resource = object;
struct spa_pod_parser prs;
uint32_t new_id, i;
uint32_t output_node_id, output_port_id, input_node_id, input_port_id;
struct spa_pod *filter = NULL;
struct spa_dict props;
spa_pod_parser_init(&prs, data, size, 0);
if (spa_pod_parser_get(&prs,
"["
"i", &output_node_id,
"i", &output_port_id,
"i", &input_node_id,
"i", &input_port_id,
"P", &filter,
"i", &props.n_items, NULL) < 0)
return -EINVAL;
props.items = alloca(props.n_items * sizeof(struct spa_dict_item));
for (i = 0; i < props.n_items; i++) {
if (spa_pod_parser_get(&prs, "ss",
&props.items[i].key, &props.items[i].value, NULL) < 0)
return -EINVAL;
}
if (spa_pod_parser_get(&prs, "i", &new_id, NULL) < 0)
return -EINVAL;
pw_resource_do(resource, struct pw_core_proxy_methods, create_link, output_node_id,
output_port_id,
input_node_id,
input_port_id,
filter,
&props,
new_id);
return 0;
}
static int core_demarshal_update_types_server(void *object, void *data, size_t size)
{
struct pw_resource *resource = object;
@ -1006,8 +928,7 @@ static const struct pw_core_proxy_methods pw_protocol_native_core_method_marshal
&core_marshal_get_registry,
&core_marshal_client_update,
&core_marshal_permissions,
&core_marshal_create_object,
&core_marshal_create_link
&core_marshal_create_object
};
static const struct pw_protocol_native_demarshal pw_protocol_native_core_method_demarshal[PW_CORE_PROXY_METHOD_NUM] = {
@ -1016,8 +937,7 @@ static const struct pw_protocol_native_demarshal pw_protocol_native_core_method_
{ &core_demarshal_get_registry, 0, },
{ &core_demarshal_client_update, 0, },
{ &core_demarshal_permissions, 0, },
{ &core_demarshal_create_object, PW_PROTOCOL_NATIVE_REMAP, },
{ &core_demarshal_create_link, PW_PROTOCOL_NATIVE_REMAP, }
{ &core_demarshal_create_object, PW_PROTOCOL_NATIVE_REMAP, }
};
static const struct pw_core_proxy_events pw_protocol_native_core_event_marshal = {