2019-07-10 20:24:11 +02:00
|
|
|
/* PipeWire
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2019 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <spa/node/node.h>
|
|
|
|
|
#include <spa/utils/hook.h>
|
|
|
|
|
#include <spa/utils/names.h>
|
|
|
|
|
#include <spa/param/props.h>
|
|
|
|
|
#include <spa/debug/dict.h>
|
|
|
|
|
|
|
|
|
|
#include "pipewire/pipewire.h"
|
2019-11-20 16:18:46 +01:00
|
|
|
|
|
|
|
|
#include "media-session.h"
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
struct v4l2_object;
|
|
|
|
|
|
|
|
|
|
struct v4l2_node {
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl *impl;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct v4l2_object *object;
|
|
|
|
|
struct spa_list link;
|
|
|
|
|
uint32_t id;
|
|
|
|
|
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
|
|
|
|
|
struct pw_proxy *proxy;
|
|
|
|
|
struct spa_node *node;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct v4l2_object {
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl *impl;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct spa_list link;
|
|
|
|
|
uint32_t id;
|
|
|
|
|
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
|
|
|
|
|
struct spa_handle *handle;
|
|
|
|
|
struct pw_proxy *proxy;
|
|
|
|
|
struct spa_device *device;
|
|
|
|
|
struct spa_hook device_listener;
|
|
|
|
|
|
|
|
|
|
struct spa_list node_list;
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl {
|
|
|
|
|
struct sm_media_session *session;
|
|
|
|
|
|
|
|
|
|
struct spa_handle *handle;
|
|
|
|
|
struct spa_device *monitor;
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
|
|
|
|
|
struct spa_list object_list;
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-10 20:24:11 +02:00
|
|
|
static struct v4l2_node *v4l2_find_node(struct v4l2_object *obj, uint32_t id)
|
|
|
|
|
{
|
|
|
|
|
struct v4l2_node *node;
|
|
|
|
|
|
|
|
|
|
spa_list_for_each(node, &obj->node_list, link) {
|
|
|
|
|
if (node->id == id)
|
|
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void v4l2_update_node(struct v4l2_object *obj, struct v4l2_node *node,
|
|
|
|
|
const struct spa_device_object_info *info)
|
|
|
|
|
{
|
|
|
|
|
pw_log_debug("update node %u", node->id);
|
2019-10-22 13:06:20 +02:00
|
|
|
|
|
|
|
|
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
|
|
|
|
|
spa_debug_dict(0, info->props);
|
|
|
|
|
|
2019-07-10 20:24:11 +02:00
|
|
|
pw_properties_update(node->props, info->props);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct v4l2_node *v4l2_create_node(struct v4l2_object *obj, uint32_t id,
|
|
|
|
|
const struct spa_device_object_info *info)
|
|
|
|
|
{
|
|
|
|
|
struct v4l2_node *node;
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl *impl = obj->impl;
|
2019-07-10 20:24:11 +02:00
|
|
|
int res;
|
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("new node %u", id);
|
|
|
|
|
|
|
|
|
|
if (info->type != SPA_TYPE_INTERFACE_Node) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
node = calloc(1, sizeof(*node));
|
|
|
|
|
if (node == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 14:47:16 +02:00
|
|
|
node->props = pw_properties_new_dict(info->props);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-08-12 14:47:16 +02:00
|
|
|
str = pw_properties_get(obj->props, SPA_KEY_DEVICE_NAME);
|
2019-07-10 20:24:11 +02:00
|
|
|
if (str == NULL)
|
2019-08-12 14:47:16 +02:00
|
|
|
str = pw_properties_get(obj->props, SPA_KEY_DEVICE_NICK);
|
2019-07-10 20:24:11 +02:00
|
|
|
if (str == NULL)
|
|
|
|
|
str = pw_properties_get(obj->props, SPA_KEY_DEVICE_ALIAS);
|
|
|
|
|
if (str == NULL)
|
|
|
|
|
str = "v4l2-device";
|
2019-08-12 14:47:16 +02:00
|
|
|
pw_properties_setf(node->props, PW_KEY_NODE_NAME, "%s.%s", info->factory_name, str);
|
|
|
|
|
|
|
|
|
|
str = pw_properties_get(obj->props, SPA_KEY_DEVICE_DESCRIPTION);
|
|
|
|
|
if (str == NULL)
|
|
|
|
|
str = "v4l2-device";
|
|
|
|
|
pw_properties_set(node->props, PW_KEY_NODE_DESCRIPTION, str);
|
|
|
|
|
|
2019-07-10 20:24:11 +02:00
|
|
|
pw_properties_set(node->props, "factory.name", info->factory_name);
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
node->impl = impl;
|
2019-07-10 20:24:11 +02:00
|
|
|
node->object = obj;
|
|
|
|
|
node->id = id;
|
2019-11-14 18:35:29 +01:00
|
|
|
node->proxy = sm_media_session_create_object(impl->session,
|
2019-07-10 20:24:11 +02:00
|
|
|
"spa-node-factory",
|
|
|
|
|
PW_TYPE_INTERFACE_Node,
|
|
|
|
|
PW_VERSION_NODE_PROXY,
|
|
|
|
|
&node->props->dict,
|
|
|
|
|
0);
|
|
|
|
|
if (node->proxy == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto clean_node;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spa_list_append(&obj->node_list, &node->link);
|
|
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
|
|
|
|
|
clean_node:
|
|
|
|
|
pw_properties_free(node->props);
|
|
|
|
|
free(node);
|
|
|
|
|
exit:
|
|
|
|
|
errno = -res;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void v4l2_remove_node(struct v4l2_object *obj, struct v4l2_node *node)
|
|
|
|
|
{
|
|
|
|
|
pw_log_debug("remove node %u", node->id);
|
|
|
|
|
spa_list_remove(&node->link);
|
|
|
|
|
pw_proxy_destroy(node->proxy);
|
|
|
|
|
free(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void v4l2_device_info(void *data, const struct spa_device_info *info)
|
|
|
|
|
{
|
|
|
|
|
struct v4l2_object *obj = data;
|
2019-10-22 13:06:20 +02:00
|
|
|
|
|
|
|
|
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
|
|
|
|
|
spa_debug_dict(0, info->props);
|
|
|
|
|
|
2019-07-10 20:24:11 +02:00
|
|
|
pw_properties_update(obj->props, info->props);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void v4l2_device_object_info(void *data, uint32_t id,
|
|
|
|
|
const struct spa_device_object_info *info)
|
|
|
|
|
{
|
|
|
|
|
struct v4l2_object *obj = data;
|
|
|
|
|
struct v4l2_node *node;
|
|
|
|
|
|
|
|
|
|
node = v4l2_find_node(obj, id);
|
|
|
|
|
|
|
|
|
|
if (info == NULL) {
|
|
|
|
|
if (node == NULL) {
|
|
|
|
|
pw_log_warn("object %p: unknown node %u", obj, id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
v4l2_remove_node(obj, node);
|
|
|
|
|
} else if (node == NULL) {
|
|
|
|
|
v4l2_create_node(obj, id, info);
|
|
|
|
|
} else {
|
|
|
|
|
v4l2_update_node(obj, node, info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_device_events v4l2_device_events = {
|
|
|
|
|
SPA_VERSION_DEVICE_EVENTS,
|
|
|
|
|
.info = v4l2_device_info,
|
|
|
|
|
.object_info = v4l2_device_object_info
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
static struct v4l2_object *v4l2_find_object(struct impl *impl, uint32_t id)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
|
|
|
|
struct v4l2_object *obj;
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
spa_list_for_each(obj, &impl->object_list, link) {
|
2019-07-10 20:24:11 +02:00
|
|
|
if (obj->id == id)
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
static void v4l2_update_object(struct impl *impl, struct v4l2_object *obj,
|
2019-09-20 13:04:14 +02:00
|
|
|
const struct spa_device_object_info *info)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
|
|
|
|
pw_log_debug("update object %u", obj->id);
|
2019-10-22 13:06:20 +02:00
|
|
|
|
|
|
|
|
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
|
|
|
|
|
spa_debug_dict(0, info->props);
|
|
|
|
|
|
2019-07-10 20:24:11 +02:00
|
|
|
pw_properties_update(obj->props, info->props);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 14:47:16 +02:00
|
|
|
static int v4l2_update_device_props(struct v4l2_object *obj)
|
|
|
|
|
{
|
|
|
|
|
struct pw_properties *p = obj->props;
|
|
|
|
|
const char *s, *d;
|
|
|
|
|
char temp[32];
|
|
|
|
|
|
|
|
|
|
if ((s = pw_properties_get(p, SPA_KEY_DEVICE_NAME)) == NULL) {
|
2019-08-16 21:52:32 +02:00
|
|
|
if ((s = pw_properties_get(p, SPA_KEY_DEVICE_BUS_ID)) == NULL) {
|
2019-08-12 14:47:16 +02:00
|
|
|
if ((s = pw_properties_get(p, SPA_KEY_DEVICE_BUS_PATH)) == NULL) {
|
|
|
|
|
snprintf(temp, sizeof(temp), "%d", obj->id);
|
|
|
|
|
s = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pw_properties_setf(p, PW_KEY_DEVICE_NAME, "v4l2_device.%s", s);
|
|
|
|
|
|
|
|
|
|
if (pw_properties_get(p, PW_KEY_DEVICE_DESCRIPTION) == NULL) {
|
|
|
|
|
d = pw_properties_get(p, PW_KEY_DEVICE_PRODUCT_NAME);
|
|
|
|
|
if (!d)
|
|
|
|
|
d = "Unknown device";
|
|
|
|
|
|
|
|
|
|
pw_properties_set(p, PW_KEY_DEVICE_DESCRIPTION, d);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
static struct v4l2_object *v4l2_create_object(struct impl *impl, uint32_t id,
|
2019-09-20 13:04:14 +02:00
|
|
|
const struct spa_device_object_info *info)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-11-14 18:35:29 +01:00
|
|
|
struct pw_core *core = impl->session->core;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct v4l2_object *obj;
|
|
|
|
|
struct spa_handle *handle;
|
|
|
|
|
int res;
|
|
|
|
|
void *iface;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("new object %u", id);
|
|
|
|
|
|
|
|
|
|
if (info->type != SPA_TYPE_INTERFACE_Device) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handle = pw_core_load_spa_handle(core,
|
|
|
|
|
info->factory_name,
|
|
|
|
|
info->props);
|
|
|
|
|
if (handle == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error("can't make factory instance: %m");
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((res = spa_handle_get_interface(handle, info->type, &iface)) < 0) {
|
|
|
|
|
pw_log_error("can't get %d interface: %d", info->type, res);
|
|
|
|
|
goto unload_handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
obj = calloc(1, sizeof(*obj));
|
|
|
|
|
if (obj == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto unload_handle;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
obj->impl = impl;
|
2019-07-10 20:24:11 +02:00
|
|
|
obj->id = id;
|
|
|
|
|
obj->handle = handle;
|
|
|
|
|
obj->device = iface;
|
|
|
|
|
obj->props = pw_properties_new_dict(info->props);
|
2019-08-12 14:47:16 +02:00
|
|
|
v4l2_update_device_props(obj);
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
obj->proxy = sm_media_session_export(impl->session,
|
2019-09-27 10:13:21 +02:00
|
|
|
info->type, pw_properties_copy(obj->props), obj->device, 0);
|
2019-07-10 20:24:11 +02:00
|
|
|
if (obj->proxy == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto clean_object;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spa_list_init(&obj->node_list);
|
|
|
|
|
|
|
|
|
|
spa_device_add_listener(obj->device,
|
|
|
|
|
&obj->device_listener, &v4l2_device_events, obj);
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
spa_list_append(&impl->object_list, &obj->link);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
|
|
|
|
|
clean_object:
|
|
|
|
|
free(obj);
|
|
|
|
|
unload_handle:
|
|
|
|
|
pw_unload_spa_handle(handle);
|
|
|
|
|
exit:
|
|
|
|
|
errno = -res;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
static void v4l2_remove_object(struct impl *impl, struct v4l2_object *obj)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
|
|
|
|
pw_log_debug("remove object %u", obj->id);
|
|
|
|
|
spa_list_remove(&obj->link);
|
|
|
|
|
spa_hook_remove(&obj->device_listener);
|
|
|
|
|
pw_proxy_destroy(obj->proxy);
|
2019-07-31 09:27:05 +02:00
|
|
|
pw_unload_spa_handle(obj->handle);
|
2019-07-10 20:24:11 +02:00
|
|
|
free(obj);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-20 13:04:14 +02:00
|
|
|
static void v4l2_udev_object_info(void *data, uint32_t id,
|
|
|
|
|
const struct spa_device_object_info *info)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl *impl = data;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct v4l2_object *obj;
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
obj = v4l2_find_object(impl, id);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
if (info == NULL) {
|
|
|
|
|
if (obj == NULL)
|
2019-09-20 13:04:14 +02:00
|
|
|
return;
|
2019-11-20 16:18:46 +01:00
|
|
|
v4l2_remove_object(impl, obj);
|
2019-07-10 20:24:11 +02:00
|
|
|
} else if (obj == NULL) {
|
2019-11-20 16:18:46 +01:00
|
|
|
if ((obj = v4l2_create_object(impl, id, info)) == NULL)
|
2019-09-20 13:04:14 +02:00
|
|
|
return;
|
2019-07-10 20:24:11 +02:00
|
|
|
} else {
|
2019-11-20 16:18:46 +01:00
|
|
|
v4l2_update_object(impl, obj, info);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-20 13:04:14 +02:00
|
|
|
static const struct spa_device_events v4l2_udev_callbacks =
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-09-20 13:04:14 +02:00
|
|
|
SPA_VERSION_DEVICE_EVENTS,
|
|
|
|
|
.object_info = v4l2_udev_object_info,
|
2019-07-10 20:24:11 +02:00
|
|
|
};
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
void * sm_v4l2_monitor_start(struct sm_media_session *sess)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-11-20 16:18:46 +01:00
|
|
|
struct pw_core *core = sess->core;
|
|
|
|
|
struct impl *impl;
|
2019-07-10 20:24:11 +02:00
|
|
|
int res;
|
|
|
|
|
void *iface;
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
impl->session = sess;
|
|
|
|
|
|
|
|
|
|
impl->handle = pw_core_load_spa_handle(core, SPA_NAME_API_V4L2_ENUM_UDEV, NULL);
|
|
|
|
|
if (impl->handle == NULL) {
|
2019-07-10 20:24:11 +02:00
|
|
|
res = -errno;
|
2019-11-20 16:18:46 +01:00
|
|
|
goto out_free;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
if ((res = spa_handle_get_interface(impl->handle, SPA_TYPE_INTERFACE_Device, &iface)) < 0) {
|
2019-07-10 20:24:11 +02:00
|
|
|
pw_log_error("can't get MONITOR interface: %d", res);
|
|
|
|
|
goto out_unload;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
impl->monitor = iface;
|
|
|
|
|
spa_list_init(&impl->object_list);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
spa_device_add_listener(impl->monitor, &impl->listener,
|
|
|
|
|
&v4l2_udev_callbacks, impl);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
return impl;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
out_unload:
|
|
|
|
|
pw_unload_spa_handle(impl->handle);
|
|
|
|
|
out_free:
|
|
|
|
|
free(impl);
|
|
|
|
|
errno = -res;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sm_v4l2_monitor_stop(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
pw_unload_spa_handle(impl->handle);
|
|
|
|
|
free(impl);
|
|
|
|
|
return 0;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|