media-session: ensure unique device.name

See #525
This commit is contained in:
Wim Taymans 2021-01-02 19:52:14 +01:00
parent e871b761fe
commit 498b4bd080
2 changed files with 55 additions and 9 deletions

View file

@ -134,13 +134,18 @@ struct impl {
static int probe_device(struct device *device); static int probe_device(struct device *device);
static struct node *alsa_find_node(struct device *device, uint32_t id) static struct node *alsa_find_node(struct device *device, uint32_t id, const char *name)
{ {
struct node *node; struct node *node;
const char *str;
spa_list_for_each(node, &device->node_list, link) { spa_list_for_each(node, &device->node_list, link) {
if (node->id == id) if (node->id == id)
return node; return node;
if (name != NULL &&
(str = pw_properties_get(node->props, PW_KEY_NODE_NAME)) != NULL &&
strcmp(name, str) == 0)
return node;
} }
return NULL; return NULL;
} }
@ -474,7 +479,7 @@ static void alsa_device_object_info(void *data, uint32_t id,
struct device *device = data; struct device *device = data;
struct node *node; struct node *node;
node = alsa_find_node(device, id); node = alsa_find_node(device, id, NULL);
if (info == NULL) { if (info == NULL) {
if (node == NULL) { if (node == NULL) {
@ -502,7 +507,7 @@ static void alsa_device_event(void *data, const struct spa_event *event)
SPA_EVENT_DEVICE_Props, SPA_POD_OPT_Pod(&props)) < 0) SPA_EVENT_DEVICE_Props, SPA_POD_OPT_Pod(&props)) < 0)
return; return;
if ((node = alsa_find_node(device, id)) == NULL) if ((node = alsa_find_node(device, id, NULL)) == NULL)
return; return;
switch (type) { switch (type) {
@ -523,13 +528,18 @@ static const struct spa_device_events alsa_device_events = {
.event = alsa_device_event, .event = alsa_device_event,
}; };
static struct device *alsa_find_device(struct impl *impl, uint32_t id) static struct device *alsa_find_device(struct impl *impl, uint32_t id, const char *name)
{ {
struct device *device; struct device *device;
const char *str;
spa_list_for_each(device, &impl->device_list, link) { spa_list_for_each(device, &impl->device_list, link) {
if (device->id == id) if (device->id == id)
return device; return device;
if (name != NULL &&
(str = pw_properties_get(device->props, PW_KEY_DEVICE_NAME)) != NULL &&
strcmp(str, name) == 0)
return device;
} }
return NULL; return NULL;
} }
@ -550,6 +560,7 @@ static int update_device_props(struct device *device)
struct pw_properties *p = device->props; struct pw_properties *p = device->props;
const char *s, *d; const char *s, *d;
char temp[32]; char temp[32];
int i;
s = pw_properties_get(p, SPA_KEY_DEVICE_NAME); s = pw_properties_get(p, SPA_KEY_DEVICE_NAME);
if (s == NULL) if (s == NULL)
@ -562,6 +573,18 @@ static int update_device_props(struct device *device)
} }
pw_properties_setf(p, PW_KEY_DEVICE_NAME, "alsa_card.%s", s); pw_properties_setf(p, PW_KEY_DEVICE_NAME, "alsa_card.%s", s);
for (i = 2; i <= 99; i++) {
if ((d = pw_properties_get(p, PW_KEY_DEVICE_NAME)) == NULL)
break;
if (alsa_find_device(device->impl, SPA_ID_INVALID, d) == NULL)
break;
pw_properties_setf(p, PW_KEY_DEVICE_NAME, "alsa_card.%s.%d", s, i);
}
if (i == 99)
return -EEXIST;
if (pw_properties_get(p, PW_KEY_DEVICE_DESCRIPTION) == NULL) { if (pw_properties_get(p, PW_KEY_DEVICE_DESCRIPTION) == NULL) {
d = NULL; d = NULL;
@ -985,7 +1008,7 @@ static void alsa_udev_object_info(void *data, uint32_t id,
struct impl *impl = data; struct impl *impl = data;
struct device *device; struct device *device;
device = alsa_find_device(impl, id); device = alsa_find_device(impl, id, NULL);
if (info == NULL) { if (info == NULL) {
if (device == NULL) if (device == NULL)

View file

@ -90,13 +90,18 @@ struct impl {
struct spa_list device_list; struct spa_list device_list;
}; };
static struct node *v4l2_find_node(struct device *dev, uint32_t id) static struct node *v4l2_find_node(struct device *dev, uint32_t id, const char *name)
{ {
struct node *node; struct node *node;
const char *str;
spa_list_for_each(node, &dev->node_list, link) { spa_list_for_each(node, &dev->node_list, link) {
if (node->id == id) if (node->id == id)
return node; return node;
if (name != NULL &&
(str = pw_properties_get(node->props, PW_KEY_NODE_NAME)) != NULL &&
strcmp(name, str) == 0)
return node;
} }
return NULL; return NULL;
} }
@ -203,7 +208,7 @@ static void v4l2_device_object_info(void *data, uint32_t id,
struct device *dev = data; struct device *dev = data;
struct node *node; struct node *node;
node = v4l2_find_node(dev, id); node = v4l2_find_node(dev, id, NULL);
if (info == NULL) { if (info == NULL) {
if (node == NULL) { if (node == NULL) {
@ -224,13 +229,18 @@ static const struct spa_device_events v4l2_device_events = {
.object_info = v4l2_device_object_info .object_info = v4l2_device_object_info
}; };
static struct device *v4l2_find_device(struct impl *impl, uint32_t id) static struct device *v4l2_find_device(struct impl *impl, uint32_t id, const char *name)
{ {
struct device *dev; struct device *dev;
const char *str;
spa_list_for_each(dev, &impl->device_list, link) { spa_list_for_each(dev, &impl->device_list, link) {
if (dev->id == id) if (dev->id == id)
return dev; return dev;
if (name != NULL &&
(str = pw_properties_get(dev->props, PW_KEY_DEVICE_NAME)) != NULL &&
strcmp(str, name) == 0)
return dev;
} }
return NULL; return NULL;
} }
@ -251,6 +261,7 @@ static int v4l2_update_device_props(struct device *dev)
struct pw_properties *p = dev->props; struct pw_properties *p = dev->props;
const char *s, *d; const char *s, *d;
char temp[32]; char temp[32];
int i;
if ((s = pw_properties_get(p, SPA_KEY_DEVICE_NAME)) == NULL) { if ((s = pw_properties_get(p, SPA_KEY_DEVICE_NAME)) == NULL) {
if ((s = pw_properties_get(p, SPA_KEY_DEVICE_BUS_ID)) == NULL) { if ((s = pw_properties_get(p, SPA_KEY_DEVICE_BUS_ID)) == NULL) {
@ -262,6 +273,18 @@ static int v4l2_update_device_props(struct device *dev)
} }
pw_properties_setf(p, PW_KEY_DEVICE_NAME, "v4l2_device.%s", s); pw_properties_setf(p, PW_KEY_DEVICE_NAME, "v4l2_device.%s", s);
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;
pw_properties_setf(p, PW_KEY_DEVICE_NAME, "v4l2_device.%s.%d", s, i);
}
if (i == 99)
return -EEXIST;
if (pw_properties_get(p, PW_KEY_DEVICE_DESCRIPTION) == NULL) { if (pw_properties_get(p, PW_KEY_DEVICE_DESCRIPTION) == NULL) {
d = pw_properties_get(p, PW_KEY_DEVICE_PRODUCT_NAME); d = pw_properties_get(p, PW_KEY_DEVICE_PRODUCT_NAME);
if (!d) if (!d)
@ -425,7 +448,7 @@ static void v4l2_udev_object_info(void *data, uint32_t id,
struct impl *impl = data; struct impl *impl = data;
struct device *dev; struct device *dev;
dev = v4l2_find_device(impl, id); dev = v4l2_find_device(impl, id, NULL);
if (info == NULL) { if (info == NULL) {
if (dev == NULL) if (dev == NULL)