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"
|
|
|
|
|
|
2019-12-11 10:57:10 +01:00
|
|
|
#include <spa/monitor/device.h>
|
2019-07-10 20:24:11 +02:00
|
|
|
#include <spa/node/node.h>
|
|
|
|
|
#include <spa/utils/hook.h>
|
|
|
|
|
#include <spa/utils/names.h>
|
2019-12-19 13:15:10 +01:00
|
|
|
#include <spa/utils/result.h>
|
2019-07-10 20:24:11 +02:00
|
|
|
#include <spa/param/props.h>
|
|
|
|
|
#include <spa/debug/dict.h>
|
2019-12-16 13:39:42 +01:00
|
|
|
#include <spa/pod/builder.h>
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
#include "pipewire/pipewire.h"
|
2019-11-20 16:18:46 +01:00
|
|
|
|
|
|
|
|
#include "media-session.h"
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
#define SESSION_CONF "v4l2-monitor.conf"
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
struct device;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
struct node {
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl *impl;
|
2019-12-16 10:45:29 +01:00
|
|
|
struct device *device;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct spa_list link;
|
|
|
|
|
uint32_t id;
|
|
|
|
|
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
|
|
|
|
|
struct pw_proxy *proxy;
|
|
|
|
|
struct spa_node *node;
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
struct device {
|
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;
|
2019-12-16 13:39:42 +01:00
|
|
|
uint32_t device_id;
|
|
|
|
|
|
|
|
|
|
int priority;
|
|
|
|
|
int profile;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
|
|
|
|
|
struct spa_handle *handle;
|
|
|
|
|
struct spa_device *device;
|
|
|
|
|
struct spa_hook device_listener;
|
|
|
|
|
|
2019-12-16 13:39:42 +01:00
|
|
|
struct sm_device *sdevice;
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
|
|
|
|
|
unsigned int appeared:1;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct spa_list node_list;
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl {
|
|
|
|
|
struct sm_media_session *session;
|
2019-12-18 12:15:03 +01:00
|
|
|
struct spa_hook session_listener;
|
2019-11-20 16:18:46 +01:00
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
struct pw_properties *conf;
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
struct spa_handle *handle;
|
|
|
|
|
struct spa_device *monitor;
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
struct spa_list device_list;
|
2019-11-20 16:18:46 +01:00
|
|
|
};
|
|
|
|
|
|
2021-01-02 19:52:14 +01:00
|
|
|
static struct node *v4l2_find_node(struct device *dev, uint32_t id, const char *name)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-12-16 10:45:29 +01:00
|
|
|
struct node *node;
|
2021-01-02 19:52:14 +01:00
|
|
|
const char *str;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
spa_list_for_each(node, &dev->node_list, link) {
|
2019-07-10 20:24:11 +02:00
|
|
|
if (node->id == id)
|
|
|
|
|
return node;
|
2021-01-02 19:52:14 +01:00
|
|
|
if (name != NULL &&
|
|
|
|
|
(str = pw_properties_get(node->props, PW_KEY_NODE_NAME)) != NULL &&
|
|
|
|
|
strcmp(name, str) == 0)
|
|
|
|
|
return node;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
static void v4l2_update_node(struct device *dev, struct node *node,
|
2019-07-10 20:24:11 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
static struct node *v4l2_create_node(struct device *dev, uint32_t id,
|
2019-07-10 20:24:11 +02:00
|
|
|
const struct spa_device_object_info *info)
|
|
|
|
|
{
|
2019-12-16 10:45:29 +01:00
|
|
|
struct node *node;
|
|
|
|
|
struct impl *impl = dev->impl;
|
2021-01-02 20:07:27 +01:00
|
|
|
int i, res;
|
2021-01-12 21:45:28 +01:00
|
|
|
const char *prefix, *str, *d, *rules;
|
2021-02-17 13:12:22 +01:00
|
|
|
char tmp[1024];
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
pw_log_debug("new node %u", id);
|
|
|
|
|
|
2019-12-19 13:15:10 +01:00
|
|
|
if (strcmp(info->type, SPA_TYPE_INTERFACE_Node) != 0) {
|
2019-07-10 20:24:11 +02:00
|
|
|
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-12-16 13:39:42 +01:00
|
|
|
pw_properties_setf(node->props, PW_KEY_DEVICE_ID, "%d", dev->device_id);
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
str = pw_properties_get(dev->props, SPA_KEY_DEVICE_NAME);
|
2019-07-10 20:24:11 +02:00
|
|
|
if (str == NULL)
|
2019-12-16 10:45:29 +01:00
|
|
|
str = pw_properties_get(dev->props, SPA_KEY_DEVICE_NICK);
|
2019-07-10 20:24:11 +02:00
|
|
|
if (str == NULL)
|
2019-12-16 10:45:29 +01:00
|
|
|
str = pw_properties_get(dev->props, SPA_KEY_DEVICE_ALIAS);
|
2019-07-10 20:24:11 +02:00
|
|
|
if (str == NULL)
|
|
|
|
|
str = "v4l2-device";
|
2021-01-12 21:45:28 +01:00
|
|
|
if (strstr(str, "v4l2_device.") == str)
|
|
|
|
|
str += 12;
|
|
|
|
|
|
|
|
|
|
if (strstr(info->factory_name, "sink") != NULL)
|
|
|
|
|
prefix = "v4l2_output";
|
2021-02-12 16:13:00 +01:00
|
|
|
else if (strstr(info->factory_name, "source") != NULL)
|
|
|
|
|
prefix = "v4l2_input";
|
2021-01-12 21:45:28 +01:00
|
|
|
else
|
|
|
|
|
prefix = info->factory_name;
|
|
|
|
|
|
2021-02-12 20:47:41 +01:00
|
|
|
pw_properties_set(node->props, PW_KEY_NODE_NAME,
|
2021-02-17 13:12:22 +01:00
|
|
|
sm_media_session_sanitize_name(tmp, sizeof(tmp),
|
2021-02-12 20:47:41 +01:00
|
|
|
'_', "%s.%s", prefix, str));
|
2021-01-02 20:07:27 +01:00
|
|
|
for (i = 2; i <= 99; i++) {
|
|
|
|
|
if ((d = pw_properties_get(node->props, PW_KEY_NODE_NAME)) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (v4l2_find_node(dev, SPA_ID_INVALID, d) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
2021-02-12 20:47:41 +01:00
|
|
|
pw_properties_set(node->props, PW_KEY_NODE_NAME,
|
2021-02-17 13:12:22 +01:00
|
|
|
sm_media_session_sanitize_name(tmp, sizeof(tmp),
|
2021-02-12 20:47:41 +01:00
|
|
|
'_', "%s.%s.%d", prefix, str, i));
|
2021-01-02 20:07:27 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
str = pw_properties_get(dev->props, SPA_KEY_DEVICE_DESCRIPTION);
|
2019-08-12 14:47:16 +02:00
|
|
|
if (str == NULL)
|
|
|
|
|
str = "v4l2-device";
|
2021-02-17 13:12:22 +01:00
|
|
|
|
|
|
|
|
pw_properties_set(node->props, PW_KEY_NODE_DESCRIPTION,
|
|
|
|
|
sm_media_session_sanitize_description(tmp, sizeof(tmp),
|
|
|
|
|
' ', "%s", str));
|
2019-08-12 14:47:16 +02:00
|
|
|
|
2020-01-09 09:45:28 +01:00
|
|
|
pw_properties_set(node->props, PW_KEY_FACTORY_NAME, info->factory_name);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
if ((rules = pw_properties_get(impl->conf, "rules")) != NULL)
|
2021-01-12 21:45:28 +01:00
|
|
|
sm_media_session_match_rules(rules, strlen(rules), node->props);
|
2021-01-12 20:38:47 +01:00
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
node->impl = impl;
|
2019-12-16 10:45:29 +01:00
|
|
|
node->device = dev;
|
2019-07-10 20:24:11 +02:00
|
|
|
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,
|
2019-12-11 15:26:11 +01:00
|
|
|
PW_VERSION_NODE,
|
2019-07-10 20:24:11 +02:00
|
|
|
&node->props->dict,
|
|
|
|
|
0);
|
|
|
|
|
if (node->proxy == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto clean_node;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
spa_list_append(&dev->node_list, &node->link);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
return node;
|
|
|
|
|
|
|
|
|
|
clean_node:
|
|
|
|
|
pw_properties_free(node->props);
|
|
|
|
|
free(node);
|
|
|
|
|
exit:
|
|
|
|
|
errno = -res;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
static void v4l2_remove_node(struct device *dev, struct node *node)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
|
|
|
|
pw_log_debug("remove node %u", node->id);
|
|
|
|
|
spa_list_remove(&node->link);
|
|
|
|
|
pw_proxy_destroy(node->proxy);
|
2019-12-18 12:15:03 +01:00
|
|
|
pw_properties_free(node->props);
|
2019-07-10 20:24:11 +02:00
|
|
|
free(node);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void v4l2_device_info(void *data, const struct spa_device_info *info)
|
|
|
|
|
{
|
2019-12-16 10:45:29 +01:00
|
|
|
struct device *dev = 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-12-16 10:45:29 +01:00
|
|
|
pw_properties_update(dev->props, info->props);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void v4l2_device_object_info(void *data, uint32_t id,
|
|
|
|
|
const struct spa_device_object_info *info)
|
|
|
|
|
{
|
2019-12-16 10:45:29 +01:00
|
|
|
struct device *dev = data;
|
|
|
|
|
struct node *node;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-02 19:52:14 +01:00
|
|
|
node = v4l2_find_node(dev, id, NULL);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
if (info == NULL) {
|
|
|
|
|
if (node == NULL) {
|
2019-12-16 10:45:29 +01:00
|
|
|
pw_log_warn("device %p: unknown node %u", dev, id);
|
2019-07-10 20:24:11 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2019-12-16 10:45:29 +01:00
|
|
|
v4l2_remove_node(dev, node);
|
2019-07-10 20:24:11 +02:00
|
|
|
} else if (node == NULL) {
|
2019-12-16 10:45:29 +01:00
|
|
|
v4l2_create_node(dev, id, info);
|
2019-07-10 20:24:11 +02:00
|
|
|
} else {
|
2019-12-16 10:45:29 +01:00
|
|
|
v4l2_update_node(dev, node, info);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
2021-03-08 13:20:02 +01:00
|
|
|
sm_media_session_schedule_rescan(dev->impl->session);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct spa_device_events v4l2_device_events = {
|
|
|
|
|
SPA_VERSION_DEVICE_EVENTS,
|
|
|
|
|
.info = v4l2_device_info,
|
|
|
|
|
.object_info = v4l2_device_object_info
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-02 19:52:14 +01:00
|
|
|
static struct device *v4l2_find_device(struct impl *impl, uint32_t id, const char *name)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-12-16 10:45:29 +01:00
|
|
|
struct device *dev;
|
2021-01-02 19:52:14 +01:00
|
|
|
const char *str;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
spa_list_for_each(dev, &impl->device_list, link) {
|
|
|
|
|
if (dev->id == id)
|
|
|
|
|
return dev;
|
2021-01-02 19:52:14 +01:00
|
|
|
if (name != NULL &&
|
|
|
|
|
(str = pw_properties_get(dev->props, PW_KEY_DEVICE_NAME)) != NULL &&
|
|
|
|
|
strcmp(str, name) == 0)
|
|
|
|
|
return dev;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
static void v4l2_update_device(struct impl *impl, struct device *dev,
|
2019-09-20 13:04:14 +02:00
|
|
|
const struct spa_device_object_info *info)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-12-16 10:45:29 +01:00
|
|
|
pw_log_debug("update device %u", dev->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-12-16 10:45:29 +01:00
|
|
|
pw_properties_update(dev->props, info->props);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
static int v4l2_update_device_props(struct device *dev)
|
2019-08-12 14:47:16 +02:00
|
|
|
{
|
2019-12-16 10:45:29 +01:00
|
|
|
struct pw_properties *p = dev->props;
|
2019-08-12 14:47:16 +02:00
|
|
|
const char *s, *d;
|
2021-03-04 12:45:35 +01:00
|
|
|
char temp[32], tmp[1024];
|
2021-01-02 19:52:14 +01:00
|
|
|
int i;
|
2019-08-12 14:47:16 +02:00
|
|
|
|
|
|
|
|
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) {
|
2019-12-16 10:45:29 +01:00
|
|
|
snprintf(temp, sizeof(temp), "%d", dev->id);
|
2019-08-12 14:47:16 +02:00
|
|
|
s = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-04 12:45:35 +01:00
|
|
|
pw_properties_set(p, PW_KEY_DEVICE_NAME,
|
|
|
|
|
sm_media_session_sanitize_name(tmp, sizeof(tmp),
|
|
|
|
|
'_', "v4l2_device.%s", s));
|
2019-08-12 14:47:16 +02:00
|
|
|
|
2021-01-02 19:52:14 +01:00
|
|
|
for (i = 2; i <= 99; i++) {
|
|
|
|
|
if ((d = pw_properties_get(p, PW_KEY_DEVICE_NAME)) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (v4l2_find_device(dev->impl, SPA_ID_INVALID, d) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
2021-03-04 12:45:35 +01:00
|
|
|
pw_properties_set(p, PW_KEY_DEVICE_NAME,
|
|
|
|
|
sm_media_session_sanitize_name(tmp, sizeof(tmp),
|
|
|
|
|
'_', "v4l2_device.%s.%d", s, i));
|
2021-01-02 19:52:14 +01:00
|
|
|
}
|
|
|
|
|
if (i == 99)
|
|
|
|
|
return -EEXIST;
|
|
|
|
|
|
2019-08-12 14:47:16 +02:00
|
|
|
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-12-16 13:39:42 +01:00
|
|
|
static void set_profile(struct device *device, int index)
|
|
|
|
|
{
|
|
|
|
|
char buf[1024];
|
|
|
|
|
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf));
|
|
|
|
|
|
|
|
|
|
pw_log_debug("%p: set profile %d id:%d", device, index, device->device_id);
|
|
|
|
|
|
|
|
|
|
device->profile = index;
|
|
|
|
|
if (device->device_id != 0) {
|
|
|
|
|
spa_device_set_param(device->device,
|
|
|
|
|
SPA_PARAM_Profile, 0,
|
|
|
|
|
spa_pod_builder_add_object(&b,
|
|
|
|
|
SPA_TYPE_OBJECT_ParamProfile, SPA_PARAM_Profile,
|
|
|
|
|
SPA_PARAM_PROFILE_index, SPA_POD_Int(index)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 10:47:31 +01:00
|
|
|
static void device_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct device *device = data;
|
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("device %p destroy", device);
|
2020-06-04 17:41:01 +02:00
|
|
|
spa_list_remove(&device->link);
|
2019-12-17 10:47:31 +01:00
|
|
|
|
|
|
|
|
spa_list_consume(node, &device->node_list, link)
|
|
|
|
|
v4l2_remove_node(device, node);
|
2020-06-04 17:41:01 +02:00
|
|
|
|
|
|
|
|
if (device->appeared)
|
|
|
|
|
spa_hook_remove(&device->device_listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void device_free(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct device *device = data;
|
|
|
|
|
pw_log_debug("device %p free", device);
|
|
|
|
|
spa_hook_remove(&device->listener);
|
|
|
|
|
pw_unload_spa_handle(device->handle);
|
|
|
|
|
pw_properties_free(device->props);
|
media-session: deal with global id race conditions
To resolve monitor and policy core global ids racing with each other,
use separate registry event handlers for both cores. Each handles only
their own objects, determined by where the object handle was created.
Postpone handling of policy core new global events after monitor sync,
which orders them after the corresponding monitor proxy and registry
events. Monitor core is then more up-to-date, so we resolve id clashes
in favor of monitor globals, which avoids duplicate objects.
Fix use-after-free by tracking whether a monitor holds references to
sm_object. Keep also objects pending for id in a list, so that they can
be cleaned up on session_shutdown (monitors may leak objects at
shutdown, because spa objectinfo events won't be handled then).
Caveats:
Zombie objects may still created if policy core is late by several
events, but in those cases the corresponding remove events are already
in the queue.
Also, there's a (theoretical) possibility that pw_registry_bind will
bind the wrong object, if the registry event is handled too late and an
id is reused by the server.
For details, see reverted 77e4fdb1e485681635b282579d1c8d26f828a0a6
for which this is a another approach.
2021-02-27 14:55:13 +02:00
|
|
|
sm_object_discard(&device->sdevice->obj);
|
2020-06-04 17:41:01 +02:00
|
|
|
free(device);
|
2019-12-17 10:47:31 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-16 13:39:42 +01:00
|
|
|
static void device_update(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct device *device = data;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("device %p appeared %d %d", device, device->appeared, device->profile);
|
|
|
|
|
|
|
|
|
|
if (device->appeared)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
device->device_id = device->sdevice->obj.id;
|
|
|
|
|
device->appeared = true;
|
|
|
|
|
|
|
|
|
|
spa_device_add_listener(device->device,
|
|
|
|
|
&device->device_listener,
|
|
|
|
|
&v4l2_device_events, device);
|
|
|
|
|
|
|
|
|
|
set_profile(device, 1);
|
|
|
|
|
sm_object_sync_update(&device->sdevice->obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct sm_object_events device_events = {
|
|
|
|
|
SM_VERSION_OBJECT_EVENTS,
|
2020-06-04 17:41:01 +02:00
|
|
|
.destroy = device_destroy,
|
|
|
|
|
.free = device_free,
|
|
|
|
|
.update = device_update,
|
2019-12-16 13:39:42 +01:00
|
|
|
};
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
static struct device *v4l2_create_device(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-12-10 18:19:56 +01:00
|
|
|
struct pw_context *context = impl->session->context;
|
2019-12-16 10:45:29 +01:00
|
|
|
struct device *dev;
|
2019-07-10 20:24:11 +02:00
|
|
|
struct spa_handle *handle;
|
|
|
|
|
int res;
|
|
|
|
|
void *iface;
|
2021-01-12 20:38:47 +01:00
|
|
|
const char *rules;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
pw_log_debug("new device %u", id);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-12-19 13:15:10 +01:00
|
|
|
if (strcmp(info->type, SPA_TYPE_INTERFACE_Device) != 0) {
|
2019-07-10 20:24:11 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 13:47:54 +08:00
|
|
|
dev = calloc(1, sizeof(*dev));
|
|
|
|
|
if (dev == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dev->impl = impl;
|
|
|
|
|
dev->id = id;
|
|
|
|
|
dev->props = pw_properties_new_dict(info->props);
|
|
|
|
|
v4l2_update_device_props(dev);
|
|
|
|
|
|
|
|
|
|
if ((rules = pw_properties_get(impl->conf, "rules")) != NULL)
|
|
|
|
|
sm_media_session_match_rules(rules, strlen(rules), dev->props);
|
|
|
|
|
|
2019-12-10 18:19:56 +01:00
|
|
|
handle = pw_context_load_spa_handle(context,
|
2021-02-16 13:47:54 +08:00
|
|
|
info->factory_name,
|
|
|
|
|
&dev->props->dict);
|
2019-07-10 20:24:11 +02:00
|
|
|
if (handle == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_error("can't make factory instance: %m");
|
2021-02-16 13:47:54 +08:00
|
|
|
goto clean_device;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((res = spa_handle_get_interface(handle, info->type, &iface)) < 0) {
|
2019-12-19 13:15:10 +01:00
|
|
|
pw_log_error("can't get %s interface: %s", info->type, spa_strerror(res));
|
2019-07-10 20:24:11 +02:00
|
|
|
goto unload_handle;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
dev->handle = handle;
|
|
|
|
|
dev->device = iface;
|
2021-01-12 20:38:47 +01:00
|
|
|
|
2019-12-16 13:39:42 +01:00
|
|
|
dev->sdevice = sm_media_session_export_device(impl->session,
|
2021-02-16 13:47:54 +08:00
|
|
|
&dev->props->dict, dev->device);
|
2019-12-16 13:39:42 +01:00
|
|
|
|
|
|
|
|
if (dev->sdevice == NULL) {
|
2019-07-10 20:24:11 +02:00
|
|
|
res = -errno;
|
2019-12-16 10:45:29 +01:00
|
|
|
goto clean_device;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-17 10:47:31 +01:00
|
|
|
pw_log_debug("got object %p", &dev->sdevice->obj);
|
|
|
|
|
|
2019-12-16 13:39:42 +01:00
|
|
|
sm_object_add_listener(&dev->sdevice->obj,
|
|
|
|
|
&dev->listener,
|
|
|
|
|
&device_events, dev);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-12-16 13:39:42 +01:00
|
|
|
spa_list_init(&dev->node_list);
|
2019-12-16 10:45:29 +01:00
|
|
|
spa_list_append(&impl->device_list, &dev->link);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
return dev;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
unload_handle:
|
|
|
|
|
pw_unload_spa_handle(handle);
|
2021-02-16 13:47:54 +08:00
|
|
|
clean_device:
|
|
|
|
|
pw_properties_free(dev->props);
|
|
|
|
|
free(dev);
|
2019-07-10 20:24:11 +02:00
|
|
|
exit:
|
|
|
|
|
errno = -res;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 10:45:29 +01:00
|
|
|
static void v4l2_remove_device(struct impl *impl, struct device *dev)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-12-16 10:45:29 +01:00
|
|
|
pw_log_debug("remove device %u", dev->id);
|
2019-12-16 13:39:42 +01:00
|
|
|
if (dev->sdevice)
|
|
|
|
|
sm_object_destroy(&dev->sdevice->obj);
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
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-12-16 10:45:29 +01:00
|
|
|
struct device *dev;
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-02 19:52:14 +01:00
|
|
|
dev = v4l2_find_device(impl, id, NULL);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
|
|
|
|
if (info == NULL) {
|
2019-12-16 10:45:29 +01:00
|
|
|
if (dev == NULL)
|
2019-09-20 13:04:14 +02:00
|
|
|
return;
|
2019-12-16 10:45:29 +01:00
|
|
|
v4l2_remove_device(impl, dev);
|
|
|
|
|
} else if (dev == NULL) {
|
2020-05-20 13:52:10 +02:00
|
|
|
if (v4l2_create_device(impl, id, info) == NULL)
|
2019-09-20 13:04:14 +02:00
|
|
|
return;
|
2019-07-10 20:24:11 +02:00
|
|
|
} else {
|
2019-12-16 10:45:29 +01:00
|
|
|
v4l2_update_device(impl, dev, 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-12-18 12:15:03 +01:00
|
|
|
static void session_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
spa_hook_remove(&impl->session_listener);
|
|
|
|
|
spa_hook_remove(&impl->listener);
|
|
|
|
|
pw_unload_spa_handle(impl->handle);
|
2021-01-12 20:38:47 +01:00
|
|
|
pw_properties_free(impl->conf);
|
2019-12-18 12:15:03 +01:00
|
|
|
free(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct sm_media_session_events session_events = {
|
|
|
|
|
SM_VERSION_MEDIA_SESSION_EVENTS,
|
|
|
|
|
.destroy = session_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int sm_v4l2_monitor_start(struct sm_media_session *sess)
|
2019-07-10 20:24:11 +02:00
|
|
|
{
|
2019-12-10 18:19:56 +01:00
|
|
|
struct pw_context *context = sess->context;
|
2019-11-20 16:18:46 +01:00
|
|
|
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)
|
2019-12-18 12:15:03 +01:00
|
|
|
return -errno;
|
2019-11-20 16:18:46 +01:00
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
impl->conf = pw_properties_new(NULL, NULL);
|
|
|
|
|
if (impl->conf == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto out_free;
|
|
|
|
|
}
|
2019-11-20 16:18:46 +01:00
|
|
|
impl->session = sess;
|
|
|
|
|
|
2019-12-10 18:19:56 +01:00
|
|
|
impl->handle = pw_context_load_spa_handle(context, SPA_NAME_API_V4L2_ENUM_UDEV, NULL);
|
2019-11-20 16:18:46 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
impl->monitor = iface;
|
2019-12-16 10:45:29 +01:00
|
|
|
spa_list_init(&impl->device_list);
|
2019-07-10 20:24:11 +02:00
|
|
|
|
2021-01-12 20:38:47 +01:00
|
|
|
if ((res = sm_media_session_load_conf(impl->session,
|
|
|
|
|
SESSION_CONF, impl->conf)) < 0)
|
|
|
|
|
pw_log_info("can't load "SESSION_CONF" config: %s", spa_strerror(res));
|
|
|
|
|
|
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-12-18 12:15:03 +01:00
|
|
|
sm_media_session_add_listener(sess, &impl->session_listener, &session_events, impl);
|
|
|
|
|
|
|
|
|
|
return 0;
|
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);
|
2019-12-18 12:15:03 +01:00
|
|
|
return res;
|
2019-07-10 20:24:11 +02:00
|
|
|
}
|