device: add device object

Make a device object, let the v4l2 monitor create device objects
The device object is responsible for dynamically creating nodes.
This commit is contained in:
Wim Taymans 2018-11-23 12:43:47 +01:00
parent 79253c6d46
commit e1bd12e599
43 changed files with 1784 additions and 237 deletions

View file

@ -593,6 +593,160 @@ static int module_demarshal_info(void *object, void *data, size_t size)
return 0;
}
static void device_marshal_info(void *object, struct pw_device_info *info)
{
struct pw_resource *resource = object;
struct spa_pod_builder *b;
uint32_t i, n_items;
b = pw_protocol_native_begin_resource(resource, PW_DEVICE_PROXY_EVENT_INFO);
n_items = info->props ? info->props->n_items : 0;
spa_pod_builder_add(b,
"[",
"i", info->id,
"l", info->change_mask,
"i", n_items, NULL);
for (i = 0; i < n_items; i++) {
spa_pod_builder_add(b,
"s", info->props->items[i].key,
"s", info->props->items[i].value, NULL);
}
spa_pod_builder_add(b, "]", NULL);
pw_protocol_native_end_resource(resource, b);
}
static int device_demarshal_info(void *object, void *data, size_t size)
{
struct pw_proxy *proxy = object;
struct spa_pod_parser prs;
struct spa_dict props;
struct pw_device_info info;
uint32_t i;
spa_pod_parser_init(&prs, data, size, 0);
if (spa_pod_parser_get(&prs,
"["
"i", &info.id,
"l", &info.change_mask,
"i", &props.n_items, NULL) < 0)
return -EINVAL;
info.props = &props;
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;
}
pw_proxy_notify(proxy, struct pw_device_proxy_events, info, 0, &info);
return 0;
}
static void device_marshal_param(void *object, uint32_t id, uint32_t index, uint32_t next,
const struct spa_pod *param)
{
struct pw_resource *resource = object;
struct spa_pod_builder *b;
b = pw_protocol_native_begin_resource(resource, PW_DEVICE_PROXY_EVENT_PARAM);
spa_pod_builder_add_struct(b, "I", id, "i", index, "i", next, "P", param);
pw_protocol_native_end_resource(resource, b);
}
static int device_demarshal_param(void *object, void *data, size_t size)
{
struct pw_proxy *proxy = object;
struct spa_pod_parser prs;
uint32_t id, index, next;
struct spa_pod *param;
spa_pod_parser_init(&prs, data, size, 0);
if (spa_pod_parser_get(&prs,
"[ I", &id,
"i", &index,
"i", &next,
"P", &param, NULL) < 0)
return -EINVAL;
pw_proxy_notify(proxy, struct pw_device_proxy_events, param, 0, id, index, next, param);
return 0;
}
static void device_marshal_enum_params(void *object, uint32_t id, uint32_t index, uint32_t num,
const struct spa_pod *filter)
{
struct pw_proxy *proxy = object;
struct spa_pod_builder *b;
b = pw_protocol_native_begin_proxy(proxy, PW_DEVICE_PROXY_METHOD_ENUM_PARAMS);
spa_pod_builder_add_struct(b,
"I", id,
"i", index,
"i", num,
"P", filter);
pw_protocol_native_end_proxy(proxy, b);
}
static int device_demarshal_enum_params(void *object, void *data, size_t size)
{
struct pw_resource *resource = object;
struct spa_pod_parser prs;
uint32_t id, index, num;
struct spa_pod *filter;
spa_pod_parser_init(&prs, data, size, 0);
if (spa_pod_parser_get(&prs,
"[ I", &id,
"i", &index,
"i", &num,
"P", &filter, NULL) < 0)
return -EINVAL;
pw_resource_do(resource, struct pw_device_proxy_methods, enum_params, 0, id, index, num, filter);
return 0;
}
static void device_marshal_set_param(void *object, uint32_t id, uint32_t flags,
const struct spa_pod *param)
{
struct pw_proxy *proxy = object;
struct spa_pod_builder *b;
b = pw_protocol_native_begin_proxy(proxy, PW_DEVICE_PROXY_METHOD_SET_PARAM);
spa_pod_builder_add_struct(b,
"I", id,
"i", flags,
"P", param);
pw_protocol_native_end_proxy(proxy, b);
}
static int device_demarshal_set_param(void *object, void *data, size_t size)
{
struct pw_resource *resource = object;
struct spa_pod_parser prs;
uint32_t id, flags;
struct spa_pod *param;
spa_pod_parser_init(&prs, data, size, 0);
if (spa_pod_parser_get(&prs,
"[ I", &id,
"i", &flags,
"P", &param, NULL) < 0)
return -EINVAL;
pw_resource_do(resource, struct pw_device_proxy_methods, set_param, 0, id, flags, param);
return 0;
}
static void factory_marshal_info(void *object, struct pw_factory_info *info)
{
struct pw_resource *resource = object;
@ -1436,6 +1590,39 @@ const struct pw_protocol_marshal pw_protocol_native_factory_marshal = {
PW_FACTORY_PROXY_EVENT_NUM,
};
static const struct pw_device_proxy_methods pw_protocol_native_device_method_marshal = {
PW_VERSION_DEVICE_PROXY_METHODS,
&device_marshal_enum_params,
&device_marshal_set_param,
};
static const struct pw_protocol_native_demarshal pw_protocol_native_device_method_demarshal[] = {
{ &device_demarshal_enum_params, 0, },
{ &device_demarshal_set_param, PW_PERM_W, },
};
static const struct pw_device_proxy_events pw_protocol_native_device_event_marshal = {
PW_VERSION_DEVICE_PROXY_EVENTS,
&device_marshal_info,
&device_marshal_param,
};
static const struct pw_protocol_native_demarshal pw_protocol_native_device_event_demarshal[] = {
{ &device_demarshal_info, 0, },
{ &device_demarshal_param, 0, }
};
static const struct pw_protocol_marshal pw_protocol_native_device_marshal = {
PW_TYPE_INTERFACE_Device,
PW_VERSION_DEVICE,
&pw_protocol_native_device_method_marshal,
pw_protocol_native_device_method_demarshal,
PW_DEVICE_PROXY_METHOD_NUM,
&pw_protocol_native_device_event_marshal,
pw_protocol_native_device_event_demarshal,
PW_DEVICE_PROXY_EVENT_NUM,
};
static const struct pw_node_proxy_methods pw_protocol_native_node_method_marshal = {
PW_VERSION_NODE_PROXY_METHODS,
&node_marshal_enum_params,
@ -1561,6 +1748,7 @@ void pw_protocol_native_init(struct pw_protocol *protocol)
pw_protocol_add_marshal(protocol, &pw_protocol_native_core_marshal);
pw_protocol_add_marshal(protocol, &pw_protocol_native_registry_marshal);
pw_protocol_add_marshal(protocol, &pw_protocol_native_module_marshal);
pw_protocol_add_marshal(protocol, &pw_protocol_native_device_marshal);
pw_protocol_add_marshal(protocol, &pw_protocol_native_node_marshal);
pw_protocol_add_marshal(protocol, &pw_protocol_native_port_marshal);
pw_protocol_add_marshal(protocol, &pw_protocol_native_factory_marshal);

View file

@ -4,7 +4,7 @@ pipewire_module_spa_c_args = [
]
pipewire_module_spa_monitor = shared_library('pipewire-module-spa-monitor',
[ 'module-monitor.c', 'spa-monitor.c', 'spa-node.c' ],
[ 'module-monitor.c', 'spa-monitor.c', 'spa-node.c', 'spa-device.c' ],
c_args : pipewire_module_spa_c_args,
include_directories : [configinc, spa_inc],
install : true,
@ -21,6 +21,15 @@ pipewire_module_spa_node = shared_library('pipewire-module-spa-node',
dependencies : [mathlib, dl_lib, pipewire_dep],
)
pipewire_module_spa_device = shared_library('pipewire-module-spa-device',
[ 'module-device.c', 'spa-device.c' ],
c_args : pipewire_module_spa_c_args,
include_directories : [configinc, spa_inc],
install : true,
install_dir : modules_install_dir,
dependencies : [mathlib, dl_lib, pipewire_dep],
)
pipewire_module_spa_node_factory = shared_library('pipewire-module-spa-node-factory',
[ 'module-node-factory.c', 'spa-node.c' ],
c_args : pipewire_module_spa_c_args,

View file

@ -0,0 +1,117 @@
/* PipeWire
* Copyright © 2018 Wim Taymans
*
* 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:
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <pipewire/core.h>
#include <pipewire/log.h>
#include <pipewire/module.h>
#include <pipewire/utils.h>
#include "spa-monitor.h"
#include "spa-device.h"
static const struct spa_dict_item module_props[] = {
{ PW_MODULE_PROP_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
{ PW_MODULE_PROP_DESCRIPTION, "Load and manage an SPA device" },
{ PW_MODULE_PROP_VERSION, PACKAGE_VERSION },
};
struct device_data {
struct pw_device *this;
struct pw_core *core;
struct pw_properties *properties;
struct spa_hook module_listener;
};
static void module_destroy(void *_data)
{
struct device_data *data = _data;
pw_device_destroy(data->this);
}
static const struct pw_module_events module_events = {
PW_VERSION_MODULE_EVENTS,
.destroy = module_destroy,
};
int pipewire__module_init(struct pw_module *module, const char *args)
{
struct pw_properties *props = NULL;
char **argv;
int n_tokens;
struct pw_core *core = pw_module_get_core(module);
struct pw_device *device;
struct device_data *data;
if (args == NULL)
goto wrong_arguments;
argv = pw_split_strv(args, " \t", 4, &n_tokens);
if (n_tokens < 3)
goto not_enough_arguments;
if (n_tokens == 4) {
props = pw_properties_new_string(argv[3]);
if (props == NULL)
return -ENOMEM;
}
device = pw_spa_device_load(core,
NULL,
pw_module_get_global(module),
argv[0], argv[1], argv[2],
0,
props,
sizeof(struct device_data));
pw_free_strv(argv);
if (device == NULL)
return -ENOMEM;
data = pw_spa_device_get_user_data(device);
data->this = device;
data->core = core;
data->properties = props;
pw_log_debug("module %p: new", module);
pw_module_add_listener(module, &data->module_listener, &module_events, data);
pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
return 0;
not_enough_arguments:
pw_free_strv(argv);
wrong_arguments:
pw_log_error("usage: module-spa-device <plugin> <factory> <name> [key=value ...]");
return -EINVAL;
}

View file

@ -0,0 +1,164 @@
/* PipeWire
*
* Copyright © 2018 Wim Taymans
*
* 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:
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include <dlfcn.h>
#include <spa/param/props.h>
#include <spa/pod/iter.h>
#include <spa/debug/types.h>
#include "spa-device.h"
#include "pipewire/device.h"
#include "pipewire/port.h"
#include "pipewire/log.h"
#include "pipewire/private.h"
struct impl {
struct pw_device *this;
struct pw_client *owner;
struct pw_global *parent;
enum pw_spa_device_flags flags;
void *unload;
struct spa_handle *handle;
struct spa_device *device;
char *lib;
char *factory_name;
struct spa_hook device_listener;
void *user_data;
};
static void device_destroy(void *data)
{
struct impl *impl = data;
struct pw_device *device = impl->this;
pw_log_debug("spa-device %p: free", device);
spa_hook_remove(&impl->device_listener);
if (impl->unload)
pw_unload_spa_interface(impl->unload);
if (impl->handle) {
spa_handle_clear(impl->handle);
free(impl->handle);
}
free(impl->lib);
free(impl->factory_name);
}
static const struct pw_device_events device_events = {
PW_VERSION_DEVICE_EVENTS,
.destroy = device_destroy,
};
struct pw_device *
pw_spa_device_new(struct pw_core *core,
struct pw_client *owner,
struct pw_global *parent,
const char *name,
enum pw_spa_device_flags flags,
struct spa_device *device,
struct spa_handle *handle,
struct pw_properties *properties,
size_t user_data_size)
{
struct pw_device *this;
struct impl *impl;
this = pw_device_new(core, name, properties, sizeof(struct impl) + user_data_size);
if (this == NULL)
return NULL;
impl = this->user_data;
impl->this = this;
impl->owner = owner;
impl->parent = parent;
impl->device = device;
impl->flags = flags;
if (user_data_size > 0)
impl->user_data = SPA_MEMBER(impl, sizeof(struct impl), void);
pw_device_add_listener(this, &impl->device_listener, &device_events, impl);
if (!SPA_FLAG_CHECK(impl->flags, PW_SPA_DEVICE_FLAG_NO_REGISTER))
pw_device_register(this, impl->owner, impl->parent, NULL);
pw_device_set_implementation(this, impl->device);
return this;
}
void *pw_spa_device_get_user_data(struct pw_device *device)
{
struct impl *impl = device->user_data;
return impl->user_data;
}
struct pw_device *pw_spa_device_load(struct pw_core *core,
struct pw_client *owner,
struct pw_global *parent,
const char *lib,
const char *factory_name,
const char *name,
enum pw_spa_device_flags flags,
struct pw_properties *properties,
size_t user_data_size)
{
struct pw_device *this;
struct impl *impl;
struct spa_device *device;
const struct spa_support *support;
uint32_t n_support;
support = pw_core_get_support(core, &n_support);
device = pw_load_spa_interface(lib, factory_name, SPA_TYPE_INTERFACE_Device,
properties ? &properties->dict : NULL, n_support, support);
if (device == NULL)
goto open_failed;
this = pw_spa_device_new(core, owner, parent, name, flags,
device, NULL, properties, user_data_size);
impl = this->user_data;
impl->unload = device;
impl->lib = strdup(lib);
impl->factory_name = strdup(factory_name);
return this;
open_failed:
return NULL;
}

View file

@ -0,0 +1,70 @@
/* PipeWire
*
* Copyright © 2018 Wim Taymans
*
* 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:
*
* 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.
*/
#ifndef __PIPEWIRE_SPA_DEVICE_H__
#define __PIPEWIRE_SPA_DEVICE_H__
#include <spa/monitor/device.h>
#include <pipewire/core.h>
#include <pipewire/device.h>
#ifdef __cplusplus
extern "C" {
#endif
enum pw_spa_device_flags {
PW_SPA_DEVICE_FLAG_DISABLE = (1 << 0),
PW_SPA_DEVICE_FLAG_NO_REGISTER = (1 << 1),
};
struct pw_device *
pw_spa_device_new(struct pw_core *core,
struct pw_client *owner, /**< optional owner */
struct pw_global *parent, /**< optional parent */
const char *name,
enum pw_spa_device_flags flags,
struct spa_device *device,
struct spa_handle *handle,
struct pw_properties *properties,
size_t user_data_size);
struct pw_device *
pw_spa_device_load(struct pw_core *core,
struct pw_client *owner, /**< optional owner */
struct pw_global *parent, /**< optional parent */
const char *lib,
const char *factory_name,
const char *name,
enum pw_spa_device_flags flags,
struct pw_properties *properties,
size_t user_data_size);
void *pw_spa_device_get_user_data(struct pw_device *device);
#ifdef __cplusplus
}
#endif
#endif /* __PIPEWIRE_SPA_DEVICE_H__ */

View file

@ -41,15 +41,19 @@
#include <pipewire/log.h>
#include <pipewire/type.h>
#include <pipewire/node.h>
#include <pipewire/device.h>
#include "spa-monitor.h"
#include "spa-node.h"
#include "spa-device.h"
struct monitor_item {
char *id;
struct spa_list link;
struct pw_node *node;
struct spa_handle *handle;
uint32_t type;
void *iface;
void *object;
};
struct impl {
@ -70,7 +74,7 @@ static struct monitor_item *add_item(struct pw_spa_monitor *this,
int res;
struct spa_handle *handle;
struct monitor_item *mitem;
void *node_iface;
void *iface;
struct pw_properties *props = NULL;
const char *name, *id, *klass, *str;
struct spa_handle_factory *factory;
@ -78,7 +82,7 @@ static struct monitor_item *add_item(struct pw_spa_monitor *this,
struct spa_pod *info = NULL;
const struct spa_support *support;
enum pw_spa_node_flags flags;
uint32_t n_support;
uint32_t n_support, type;
if (spa_pod_object_parse(item,
":", SPA_MONITOR_ITEM_id, "s", &id,
@ -86,6 +90,7 @@ static struct monitor_item *add_item(struct pw_spa_monitor *this,
":", SPA_MONITOR_ITEM_name, "s", &name,
":", SPA_MONITOR_ITEM_class, "s", &klass,
":", SPA_MONITOR_ITEM_factory, "p", &factory,
":", SPA_MONITOR_ITEM_type, "I", &type,
":", SPA_MONITOR_ITEM_info, "T", &info, NULL) < 0) {
pw_log_warn("monitor %p: could not parse item", this);
spa_debug_pod(0, NULL, item);
@ -128,21 +133,34 @@ static struct monitor_item *add_item(struct pw_spa_monitor *this,
pw_log_error("can't make factory instance: %d", res);
return NULL;
}
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &node_iface)) < 0) {
if ((res = spa_handle_get_interface(handle, type, &iface)) < 0) {
pw_log_error("can't get NODE interface: %d", res);
pw_properties_free(props);
return NULL;
}
flags = PW_SPA_NODE_FLAG_ACTIVATE;
flags |= (state == SPA_MONITOR_ITEM_STATE_Available) ? 0 : PW_SPA_NODE_FLAG_DISABLE;
mitem = calloc(1, sizeof(struct monitor_item));
mitem->id = strdup(id);
mitem->handle = handle;
mitem->node = pw_spa_node_new(impl->core, NULL, impl->parent, name,
mitem->type = type;
switch (type) {
case SPA_TYPE_INTERFACE_Node:
flags = PW_SPA_NODE_FLAG_ACTIVATE;
flags |= (state == SPA_MONITOR_ITEM_STATE_Available) ? 0 : PW_SPA_NODE_FLAG_DISABLE;
mitem->object = pw_spa_node_new(impl->core, NULL, impl->parent, name,
flags,
node_iface, handle, props, 0);
iface, handle, props, 0);
break;
case SPA_TYPE_INTERFACE_Device:
mitem->object = pw_spa_device_new(impl->core, NULL, impl->parent, name,
0, iface, handle, props, 0);
break;
default:
return NULL;
}
spa_list_append(&impl->item_list, &mitem->link);
@ -164,7 +182,16 @@ static struct monitor_item *find_item(struct pw_spa_monitor *this, const char *i
void destroy_item(struct monitor_item *mitem)
{
pw_node_destroy(mitem->node);
switch (mitem->type) {
case SPA_TYPE_INTERFACE_Node:
pw_node_destroy(mitem->object);
break;
case SPA_TYPE_INTERFACE_Device:
pw_device_destroy(mitem->object);
break;
default:
break;
}
spa_list_remove(&mitem->link);
spa_handle_clear(mitem->handle);
free(mitem->handle);
@ -209,11 +236,11 @@ static void change_item(struct pw_spa_monitor *this, struct spa_pod *item, uint6
switch (state) {
case SPA_MONITOR_ITEM_STATE_Available:
pw_node_set_enabled(mitem->node, true);
pw_node_set_enabled(mitem->object, true);
break;
case SPA_MONITOR_ITEM_STATE_Disabled:
case SPA_MONITOR_ITEM_STATE_Unavailable:
pw_node_set_enabled(mitem->node, false);
pw_node_set_enabled(mitem->object, false);
break;
default:
break;