2017-07-11 12:24:03 +02:00
|
|
|
/* PipeWire
|
|
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Copyright © 2018 Wim Taymans
|
2017-07-11 12:24:03 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01: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-07-11 12:24:03 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01: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-07-11 12:24:03 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
2019-01-14 12:58:23 +01:00
|
|
|
#include "pipewire/pipewire.h"
|
2017-07-11 15:57:20 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
#include "spa-node.h"
|
|
|
|
|
|
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, "Provide a factory to make SPA nodes" },
|
|
|
|
|
{ PW_MODULE_PROP_VERSION, PACKAGE_VERSION },
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-04 10:18:54 +02:00
|
|
|
struct factory_data {
|
|
|
|
|
struct pw_core *core;
|
2017-09-17 16:47:03 +02:00
|
|
|
struct pw_factory *this;
|
2017-07-11 12:24:03 +02:00
|
|
|
struct pw_properties *properties;
|
2017-11-14 10:59:10 +01:00
|
|
|
|
|
|
|
|
struct spa_hook factory_listener;
|
|
|
|
|
struct spa_hook module_listener;
|
|
|
|
|
|
|
|
|
|
struct spa_list node_list;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct node_data {
|
|
|
|
|
struct spa_list link;
|
|
|
|
|
struct pw_node *node;
|
2018-03-01 14:38:59 +01:00
|
|
|
struct spa_hook node_listener;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void node_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct node_data *nd = data;
|
|
|
|
|
spa_list_remove(&nd->link);
|
2018-05-17 17:30:30 +02:00
|
|
|
spa_hook_remove(&nd->node_listener);
|
2018-03-01 14:38:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_node_events node_events = {
|
|
|
|
|
PW_VERSION_NODE_EVENTS,
|
|
|
|
|
.destroy = node_destroy,
|
2017-07-11 12:24:03 +02:00
|
|
|
};
|
|
|
|
|
|
2017-09-17 16:47:03 +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-07-11 12:24:03 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
struct factory_data *data = _data;
|
2017-07-11 12:24:03 +02:00
|
|
|
struct pw_node *node;
|
2017-09-17 16:47:03 +02:00
|
|
|
const char *lib, *factory_name, *name;
|
2017-11-14 10:59:10 +01:00
|
|
|
struct node_data *nd;
|
2017-07-11 12:24:03 +02:00
|
|
|
|
|
|
|
|
if (properties == NULL)
|
|
|
|
|
goto no_properties;
|
|
|
|
|
|
|
|
|
|
lib = pw_properties_get(properties, "spa.library.name");
|
|
|
|
|
factory_name = pw_properties_get(properties, "spa.factory.name");
|
2017-09-17 16:47:03 +02:00
|
|
|
name = pw_properties_get(properties, "name");
|
2017-07-11 12:24:03 +02:00
|
|
|
|
2017-09-15 14:47:54 +02:00
|
|
|
if (lib == NULL || factory_name == NULL)
|
2017-07-11 12:24:03 +02:00
|
|
|
goto no_properties;
|
|
|
|
|
|
2017-09-17 16:47:03 +02:00
|
|
|
if (name == NULL)
|
|
|
|
|
name = "spa-node";
|
|
|
|
|
|
2017-08-04 10:18:54 +02:00
|
|
|
node = pw_spa_node_load(data->core,
|
2017-07-18 14:58:14 +02:00
|
|
|
NULL,
|
2017-09-18 09:35:00 +02:00
|
|
|
pw_factory_get_global(data->this),
|
2017-07-11 12:24:03 +02:00
|
|
|
lib,
|
|
|
|
|
factory_name,
|
|
|
|
|
name,
|
2018-01-16 16:50:53 +01:00
|
|
|
PW_SPA_NODE_FLAG_ACTIVATE,
|
2017-11-14 10:59:10 +01:00
|
|
|
properties,
|
|
|
|
|
sizeof(struct node_data));
|
2017-07-11 12:24:03 +02:00
|
|
|
if (node == NULL)
|
|
|
|
|
goto no_mem;
|
|
|
|
|
|
2017-11-14 10:59:10 +01:00
|
|
|
nd = pw_spa_node_get_user_data(node);
|
|
|
|
|
nd->node = node;
|
|
|
|
|
spa_list_append(&data->node_list, &nd->link);
|
|
|
|
|
|
2018-03-01 14:38:59 +01:00
|
|
|
pw_node_add_listener(node, &nd->node_listener, &node_events, nd);
|
|
|
|
|
|
2017-09-17 16:47:03 +02:00
|
|
|
if (resource)
|
|
|
|
|
pw_global_bind(pw_node_get_global(node),
|
|
|
|
|
pw_resource_get_client(resource),
|
|
|
|
|
PW_PERM_RWX,
|
|
|
|
|
version, new_id);
|
|
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
return node;
|
|
|
|
|
|
|
|
|
|
no_properties:
|
2017-09-15 14:47:54 +02:00
|
|
|
pw_log_error("needed properties: spa.library.name=<library-name> spa.factory.name=<factory-name>");
|
2017-07-11 12:24:03 +02:00
|
|
|
if (resource) {
|
2019-02-14 16:53:42 +01:00
|
|
|
pw_resource_error(resource, -EINVAL,
|
2017-09-15 14:47:54 +02:00
|
|
|
"needed properties: "
|
|
|
|
|
"spa.library.name=<library-name> "
|
|
|
|
|
"spa.factory.name=<factory-name>");
|
2017-07-11 12:24:03 +02:00
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
no_mem:
|
2019-02-14 16:53:42 +01:00
|
|
|
pw_log_error("can't create node: no memory");
|
2017-07-11 12:24:03 +02:00
|
|
|
if (resource) {
|
2019-02-14 16:53:42 +01:00
|
|
|
pw_resource_error(resource, -ENOMEM, "no memory");
|
2017-07-11 12:24:03 +02:00
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-14 10:59:10 +01:00
|
|
|
static const struct pw_factory_implementation factory_impl = {
|
2017-09-17 16:47:03 +02:00
|
|
|
PW_VERSION_FACTORY_IMPLEMENTATION,
|
|
|
|
|
.create_object = create_object,
|
2017-08-04 10:18:54 +02:00
|
|
|
};
|
|
|
|
|
|
2017-11-14 10:59:10 +01:00
|
|
|
static void factory_destroy(void *_data)
|
|
|
|
|
{
|
|
|
|
|
struct factory_data *data = _data;
|
|
|
|
|
struct node_data *nd, *t;
|
|
|
|
|
|
|
|
|
|
spa_hook_remove(&data->module_listener);
|
|
|
|
|
|
|
|
|
|
spa_list_for_each_safe(nd, t, &data->node_list, link)
|
|
|
|
|
pw_node_destroy(nd->node);
|
|
|
|
|
|
|
|
|
|
if (data->properties)
|
|
|
|
|
pw_properties_free(data->properties);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_factory_events factory_events = {
|
|
|
|
|
PW_VERSION_FACTORY_IMPLEMENTATION,
|
|
|
|
|
.destroy = factory_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void module_destroy(void *_data)
|
|
|
|
|
{
|
|
|
|
|
struct factory_data *data = _data;
|
|
|
|
|
pw_factory_destroy(data->this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_module_events module_events = {
|
|
|
|
|
PW_VERSION_MODULE_EVENTS,
|
|
|
|
|
.destroy = module_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-18 11:38:30 +01:00
|
|
|
static int module_init(struct pw_module *module, struct pw_properties *properties)
|
2017-07-11 12:24:03 +02:00
|
|
|
{
|
2017-08-06 06:42:26 +02:00
|
|
|
struct pw_core *core = pw_module_get_core(module);
|
2017-09-17 16:47:03 +02:00
|
|
|
struct pw_factory *factory;
|
2017-08-04 10:18:54 +02:00
|
|
|
struct factory_data *data;
|
2017-07-11 12:24:03 +02:00
|
|
|
|
2017-09-17 16:47:03 +02:00
|
|
|
factory = pw_factory_new(core,
|
|
|
|
|
"spa-node-factory",
|
2018-08-27 15:03:11 +02:00
|
|
|
PW_TYPE_INTERFACE_Node,
|
2017-09-17 16:47:03 +02:00
|
|
|
PW_VERSION_NODE,
|
|
|
|
|
NULL,
|
|
|
|
|
sizeof(*data));
|
2017-08-04 10:18:54 +02:00
|
|
|
if (factory == NULL)
|
2017-12-18 11:38:30 +01:00
|
|
|
return -ENOMEM;
|
2017-07-18 14:58:14 +02:00
|
|
|
|
2017-09-17 16:47:03 +02:00
|
|
|
data = pw_factory_get_user_data(factory);
|
2017-08-04 10:18:54 +02:00
|
|
|
data->this = factory;
|
2017-08-25 18:59:01 +02:00
|
|
|
data->core = core;
|
2017-08-04 10:18:54 +02:00
|
|
|
data->properties = properties;
|
2017-11-14 10:59:10 +01:00
|
|
|
spa_list_init(&data->node_list);
|
2017-07-11 12:24:03 +02:00
|
|
|
|
2017-11-14 10:59:10 +01:00
|
|
|
pw_factory_add_listener(factory, &data->factory_listener, &factory_events, data);
|
|
|
|
|
pw_factory_set_implementation(factory, &factory_impl, data);
|
2017-07-11 12:24:03 +02:00
|
|
|
|
2017-11-14 10:59:10 +01:00
|
|
|
pw_log_debug("module %p: new", module);
|
|
|
|
|
pw_module_add_listener(module, &data->module_listener, &module_events, data);
|
2017-07-11 12:24:03 +02:00
|
|
|
|
2018-06-01 11:23:02 +02:00
|
|
|
pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
|
|
|
|
|
|
2018-01-19 11:18:17 +01:00
|
|
|
pw_factory_register(factory, NULL, pw_module_get_global(module), NULL);
|
2017-07-11 12:24:03 +02:00
|
|
|
|
2017-12-18 11:38:30 +01:00
|
|
|
return 0;
|
2017-07-11 12:24:03 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-06 13:24:41 +01:00
|
|
|
SPA_EXPORT
|
2017-12-18 11:38:30 +01:00
|
|
|
int pipewire__module_init(struct pw_module *module, const char *args)
|
2017-07-11 12:24:03 +02:00
|
|
|
{
|
2017-07-18 14:58:14 +02:00
|
|
|
return module_init(module, NULL);
|
2017-07-11 12:24:03 +02:00
|
|
|
}
|