mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-01 22:58:50 -04:00
monitor: use dynamic types
Make the monitor item a POD object and use dynamic types.
This commit is contained in:
parent
c44a7c9735
commit
7270986c3a
14 changed files with 360 additions and 248 deletions
|
|
@ -36,14 +36,13 @@ extern const SpaHandleFactory spa_v4l2_source_factory;
|
|||
typedef struct _SpaV4l2Monitor SpaV4l2Monitor;
|
||||
|
||||
typedef struct {
|
||||
SpaMonitorItem item;
|
||||
SpaMonitorItem *item;
|
||||
struct udev_device *udevice;
|
||||
SpaDict info;
|
||||
SpaDictItem info_items[32];
|
||||
} V4l2Item;
|
||||
|
||||
typedef struct {
|
||||
uint32_t monitor;
|
||||
uint32_t handle_factory;
|
||||
SpaMonitorTypes monitor_types;
|
||||
} URI;
|
||||
|
||||
struct _SpaV4l2Monitor {
|
||||
|
|
@ -63,6 +62,7 @@ struct _SpaV4l2Monitor {
|
|||
struct udev_enumerate *enumerate;
|
||||
uint32_t index;
|
||||
struct udev_list_entry *devices;
|
||||
uint8_t item_buffer[4096];
|
||||
|
||||
V4l2Item uitem;
|
||||
|
||||
|
|
@ -81,10 +81,11 @@ v4l2_udev_open (SpaV4l2Monitor *this)
|
|||
}
|
||||
|
||||
static void
|
||||
fill_item (V4l2Item *item, struct udev_device *udevice)
|
||||
fill_item (SpaV4l2Monitor *this, V4l2Item *item, struct udev_device *udevice)
|
||||
{
|
||||
uint32_t i;
|
||||
const char *str;
|
||||
const char *str, *name;
|
||||
SpaPODBuilder b = { NULL, };
|
||||
SpaPODFrame f[3];
|
||||
|
||||
if (item->udevice)
|
||||
udev_device_unref (item->udevice);
|
||||
|
|
@ -92,48 +93,64 @@ fill_item (V4l2Item *item, struct udev_device *udevice)
|
|||
if (udevice == NULL)
|
||||
return;
|
||||
|
||||
item->item.id = udev_device_get_syspath (item->udevice);
|
||||
item->item.flags = 0;
|
||||
item->item.state = SPA_MONITOR_ITEM_STATE_AVAILABLE;
|
||||
item->item.klass = "Video/Source";
|
||||
item->item.info = &item->info;
|
||||
item->item.factory = &spa_v4l2_source_factory;
|
||||
name = udev_device_get_property_value (item->udevice, "ID_V4L_PRODUCT");
|
||||
if (!(name && *name)) {
|
||||
name = udev_device_get_property_value (item->udevice, "ID_MODEL_FROM_DATABASE");
|
||||
if (!(name && *name)) {
|
||||
name = udev_device_get_property_value (item->udevice, "ID_MODEL_ENC");
|
||||
if (!(name && *name)) {
|
||||
name = udev_device_get_property_value (item->udevice, "ID_MODEL");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!(name && *name))
|
||||
name = "Unknown";
|
||||
|
||||
item->info.items = item->info_items;
|
||||
i = 0;
|
||||
item->info_items[i].key = "udev-probed";
|
||||
item->info_items[i++].value = "1";
|
||||
item->info_items[i].key = "device.api";
|
||||
item->info_items[i++].value = "v4l2";
|
||||
item->info_items[i].key = "device.path";
|
||||
item->info_items[i++].value = udev_device_get_devnode (item->udevice);
|
||||
spa_pod_builder_init (&b, this->item_buffer, sizeof (this->item_buffer));
|
||||
|
||||
spa_pod_builder_push_object (&b, &f[0], 0, this->uri.monitor_types.MonitorItem);
|
||||
|
||||
spa_pod_builder_add (&b,
|
||||
SPA_POD_PROP (&f[1], this->uri.monitor_types.id, 0, SPA_POD_TYPE_STRING, 1, udev_device_get_syspath (item->udevice)),
|
||||
SPA_POD_PROP (&f[1], this->uri.monitor_types.flags, 0, SPA_POD_TYPE_INT, 1, 0),
|
||||
SPA_POD_PROP (&f[1], this->uri.monitor_types.state, 0, SPA_POD_TYPE_INT, 1, SPA_MONITOR_ITEM_STATE_AVAILABLE),
|
||||
SPA_POD_PROP (&f[1], this->uri.monitor_types.name, 0, SPA_POD_TYPE_STRING, 1, name),
|
||||
SPA_POD_PROP (&f[1], this->uri.monitor_types.klass, 0, SPA_POD_TYPE_STRING, 1, "Video/Source"),
|
||||
SPA_POD_PROP (&f[1], this->uri.monitor_types.factory, 0, SPA_POD_TYPE_POINTER, 1, this->uri.handle_factory,
|
||||
&spa_v4l2_source_factory),
|
||||
0);
|
||||
|
||||
spa_pod_builder_add (&b,
|
||||
SPA_POD_TYPE_PROP, &f[1], this->uri.monitor_types.info, 0,
|
||||
SPA_POD_TYPE_STRUCT, 1, &f[2], 0);
|
||||
|
||||
|
||||
spa_pod_builder_add (&b,
|
||||
SPA_POD_TYPE_STRING, "udev-probed", SPA_POD_TYPE_STRING, "1",
|
||||
SPA_POD_TYPE_STRING, "device.api", SPA_POD_TYPE_STRING, "v4l2",
|
||||
SPA_POD_TYPE_STRING, "device.path", SPA_POD_TYPE_STRING, udev_device_get_devnode (item->udevice),
|
||||
0);
|
||||
|
||||
str = udev_device_get_property_value (item->udevice, "ID_PATH");
|
||||
if (!(str && *str))
|
||||
str = udev_device_get_syspath (item->udevice);
|
||||
if (str && *str) {
|
||||
item->info_items[i].key = "device.bus_path";
|
||||
item->info_items[i++].value = str;
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.bus_path", SPA_POD_TYPE_STRING, str, 0);
|
||||
}
|
||||
if ((str = udev_device_get_syspath (item->udevice)) && *str) {
|
||||
item->info_items[i].key = "sysfs.path";
|
||||
item->info_items[i++].value = str;
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "sysfs.path", SPA_POD_TYPE_STRING, str, 0);
|
||||
}
|
||||
if ((str = udev_device_get_property_value (item->udevice, "ID_ID")) && *str) {
|
||||
item->info_items[i].key = "udev.id";
|
||||
item->info_items[i++].value = str;
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "udev.id", SPA_POD_TYPE_STRING, str, 0);
|
||||
}
|
||||
if ((str = udev_device_get_property_value (item->udevice, "ID_BUS")) && *str) {
|
||||
item->info_items[i].key = "device.bus";
|
||||
item->info_items[i++].value = str;
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.bus", SPA_POD_TYPE_STRING, str, 0);
|
||||
}
|
||||
if ((str = udev_device_get_property_value (item->udevice, "SUBSYSTEM")) && *str) {
|
||||
item->info_items[i].key = "device.subsystem";
|
||||
item->info_items[i++].value = str;
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.subsystem", SPA_POD_TYPE_STRING, str, 0);
|
||||
}
|
||||
if ((str = udev_device_get_property_value (item->udevice, "ID_VENDOR_ID")) && *str) {
|
||||
item->info_items[i].key = "device.vendor.id";
|
||||
item->info_items[i++].value = str;
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.vendor.id", SPA_POD_TYPE_STRING, str, 0);
|
||||
}
|
||||
str = udev_device_get_property_value (item->udevice, "ID_VENDOR_FROM_DATABASE");
|
||||
if (!(str && *str)) {
|
||||
|
|
@ -143,69 +160,64 @@ fill_item (V4l2Item *item, struct udev_device *udevice)
|
|||
}
|
||||
}
|
||||
if (str && *str) {
|
||||
item->info_items[i].key = "device.vendor.name";
|
||||
item->info_items[i++].value = str;
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.vendor.name", SPA_POD_TYPE_STRING, str, 0);
|
||||
}
|
||||
if ((str = udev_device_get_property_value (item->udevice, "ID_MODEL_ID")) && *str) {
|
||||
item->info_items[i].key = "device.product.id";
|
||||
item->info_items[i++].value = str;
|
||||
}
|
||||
str = udev_device_get_property_value (item->udevice, "ID_V4L_PRODUCT");
|
||||
if (!(str && *str)) {
|
||||
str = udev_device_get_property_value (item->udevice, "ID_MODEL_FROM_DATABASE");
|
||||
if (!(str && *str)) {
|
||||
str = udev_device_get_property_value (item->udevice, "ID_MODEL_ENC");
|
||||
if (!(str && *str)) {
|
||||
str = udev_device_get_property_value (item->udevice, "ID_MODEL");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (str && *str) {
|
||||
item->info_items[i].key = "device.product.name";
|
||||
item->info_items[i++].value = str;
|
||||
item->item.name = str;
|
||||
} else {
|
||||
item->item.name = "Unknown";
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.product.id", SPA_POD_TYPE_STRING, str, 0);
|
||||
}
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.product.name", SPA_POD_TYPE_STRING, name, 0);
|
||||
if ((str = udev_device_get_property_value (item->udevice, "ID_SERIAL")) && *str) {
|
||||
item->info_items[i].key = "device.serial";
|
||||
item->info_items[i++].value = str;
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.serial", SPA_POD_TYPE_STRING, str, 0);
|
||||
}
|
||||
if ((str = udev_device_get_property_value (item->udevice, "ID_V4L_CAPABILITIES")) && *str) {
|
||||
item->info_items[i].key = "device.capabilities";
|
||||
item->info_items[i++].value = str;
|
||||
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.capabilities", SPA_POD_TYPE_STRING, str, 0);
|
||||
}
|
||||
item->info.n_items = i;
|
||||
}
|
||||
|
||||
spa_pod_builder_add (&b,
|
||||
-SPA_POD_TYPE_STRUCT, &f[2],
|
||||
-SPA_POD_TYPE_PROP, &f[1],
|
||||
0);
|
||||
|
||||
spa_pod_builder_pop (&b, &f[0]);
|
||||
|
||||
item->item = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaMonitorItem);
|
||||
}
|
||||
|
||||
static void
|
||||
v4l2_on_fd_events (SpaSource *source)
|
||||
{
|
||||
SpaV4l2Monitor *this = source->data;
|
||||
struct udev_device *dev;
|
||||
SpaEvent *event;
|
||||
const char *action;
|
||||
SpaMonitorItem *item;
|
||||
uint32_t type;
|
||||
SpaPODBuilder b = { NULL, };
|
||||
SpaPODFrame f[1];
|
||||
uint8_t buffer[4096];
|
||||
|
||||
dev = udev_monitor_receive_device (this->umonitor);
|
||||
fill_item (&this->uitem, dev);
|
||||
fill_item (this, &this->uitem, dev);
|
||||
if (dev == NULL)
|
||||
return;
|
||||
|
||||
if ((action = udev_device_get_action (dev)) == NULL)
|
||||
action = "change";
|
||||
|
||||
item = &this->uitem.item;
|
||||
|
||||
if (strcmp (action, "add") == 0) {
|
||||
item->event.type = SPA_MONITOR_EVENT_TYPE_ADDED;
|
||||
type = this->uri.monitor_types.Added;
|
||||
} else if (strcmp (action, "change") == 0) {
|
||||
item->event.type = SPA_MONITOR_EVENT_TYPE_CHANGED;
|
||||
type = this->uri.monitor_types.Changed;
|
||||
} else if (strcmp (action, "remove") == 0) {
|
||||
item->event.type = SPA_MONITOR_EVENT_TYPE_REMOVED;
|
||||
}
|
||||
item->event.size = sizeof (this->uitem);
|
||||
this->event_cb (&this->monitor, &item->event, this->user_data);
|
||||
type = this->uri.monitor_types.Removed;
|
||||
} else
|
||||
return;
|
||||
|
||||
spa_pod_builder_init (&b, buffer, sizeof (buffer));
|
||||
spa_pod_builder_object (&b, &f[0], 0, type,
|
||||
SPA_POD_TYPE_POD, this->uitem.item);
|
||||
|
||||
event = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaMonitorEvent);
|
||||
this->event_cb (&this->monitor, event, this->user_data);
|
||||
}
|
||||
|
||||
static SpaResult
|
||||
|
|
@ -283,18 +295,18 @@ spa_v4l2_monitor_enum_items (SpaMonitor *monitor,
|
|||
this->index++;
|
||||
}
|
||||
if (this->devices == NULL) {
|
||||
fill_item (&this->uitem, NULL);
|
||||
fill_item (this, &this->uitem, NULL);
|
||||
return SPA_RESULT_ENUM_END;
|
||||
}
|
||||
|
||||
dev = udev_device_new_from_syspath (this->udev,
|
||||
udev_list_entry_get_name (this->devices));
|
||||
|
||||
fill_item (&this->uitem, dev);
|
||||
fill_item (this, &this->uitem, dev);
|
||||
if (dev == NULL)
|
||||
return SPA_RESULT_ENUM_END;
|
||||
|
||||
*item = &this->uitem.item;
|
||||
*item = this->uitem.item;
|
||||
|
||||
this->devices = udev_list_entry_get_next (this->devices);
|
||||
this->index++;
|
||||
|
|
@ -321,7 +333,7 @@ spa_v4l2_monitor_get_interface (SpaHandle *handle,
|
|||
|
||||
this = (SpaV4l2Monitor *) handle;
|
||||
|
||||
if (interface_id == this->uri.monitor)
|
||||
if (interface_id == this->uri.monitor_types.Monitor)
|
||||
*interface = &this->monitor;
|
||||
else
|
||||
return SPA_RESULT_UNKNOWN_INTERFACE;
|
||||
|
|
@ -369,7 +381,8 @@ v4l2_monitor_init (const SpaHandleFactory *factory,
|
|||
spa_log_error (this->log, "a main-loop is needed");
|
||||
return SPA_RESULT_ERROR;
|
||||
}
|
||||
this->uri.monitor = spa_id_map_get_id (this->map, SPA_MONITOR_URI);
|
||||
this->uri.handle_factory = spa_id_map_get_id (this->map, SPA_HANDLE_FACTORY_URI);
|
||||
spa_monitor_types_map (this->map, &this->uri.monitor_types);
|
||||
|
||||
this->monitor = v4l2monitor;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue