mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-03 09:01:54 -05:00
monitor: remove enum_items
Make the monitor and device emit the info and items when the callbacks are set. This makes it easier to implement the monitor because there is much less state to keep around and invalidate etc. Rework the monitors Add bluez5 device
This commit is contained in:
parent
10d277ce03
commit
57e94f7bc4
15 changed files with 510 additions and 332 deletions
|
|
@ -126,8 +126,7 @@ static int emit_device(struct impl *this, snd_ctl_card_info_t *info, snd_pcm_inf
|
|||
else
|
||||
factory = &spa_alsa_source_factory;
|
||||
|
||||
if (this->callbacks->add)
|
||||
this->callbacks->add(this->callbacks_data, 0,
|
||||
this->callbacks->add(this->callbacks_data, 0,
|
||||
factory,
|
||||
SPA_TYPE_INTERFACE_Node,
|
||||
&SPA_DICT_INIT(items, 13));
|
||||
|
|
@ -191,6 +190,10 @@ static int emit_devices(struct impl *this)
|
|||
return err;
|
||||
}
|
||||
|
||||
static const struct spa_dict_item info_items[] = {
|
||||
{ "media.class", "Audio/Device" },
|
||||
};
|
||||
|
||||
static int impl_set_callbacks(struct spa_device *device,
|
||||
const struct spa_device_callbacks *callbacks,
|
||||
void *data)
|
||||
|
|
@ -204,8 +207,13 @@ static int impl_set_callbacks(struct spa_device *device,
|
|||
this->callbacks = callbacks;
|
||||
this->callbacks_data = data;
|
||||
|
||||
if (callbacks)
|
||||
emit_devices(this);
|
||||
if (callbacks) {
|
||||
if (callbacks->info)
|
||||
callbacks->info(data, &SPA_DICT_INIT_ARRAY(info_items));
|
||||
|
||||
if (this->callbacks->add)
|
||||
emit_devices(this);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -227,18 +235,8 @@ static int impl_set_param(struct spa_device *device,
|
|||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
static const struct spa_dict_item info_items[] = {
|
||||
{ "media.class", "Audio/Device" },
|
||||
};
|
||||
|
||||
static const struct spa_dict info = {
|
||||
info_items,
|
||||
SPA_N_ELEMENTS(info_items)
|
||||
};
|
||||
|
||||
static const struct spa_device impl_device = {
|
||||
SPA_VERSION_DEVICE,
|
||||
&info,
|
||||
impl_set_callbacks,
|
||||
impl_enum_params,
|
||||
impl_set_param,
|
||||
|
|
|
|||
|
|
@ -44,10 +44,6 @@
|
|||
|
||||
extern const struct spa_handle_factory spa_alsa_device_factory;
|
||||
|
||||
struct item {
|
||||
struct udev_device *udevice;
|
||||
};
|
||||
|
||||
struct impl {
|
||||
struct spa_handle handle;
|
||||
struct spa_monitor monitor;
|
||||
|
|
@ -60,11 +56,7 @@ struct impl {
|
|||
|
||||
struct udev *udev;
|
||||
struct udev_monitor *umonitor;
|
||||
struct udev_enumerate *enumerate;
|
||||
uint32_t index;
|
||||
struct udev_list_entry *devices;
|
||||
|
||||
struct item uitem;
|
||||
uint32_t cards[MAX_CARDS];
|
||||
int n_cards;
|
||||
|
||||
|
|
@ -72,14 +64,20 @@ struct impl {
|
|||
};
|
||||
|
||||
static int impl_udev_open(struct impl *this)
|
||||
{
|
||||
if (this->udev == NULL) {
|
||||
this->udev = udev_new();
|
||||
if (this->udev == NULL)
|
||||
return -ENOMEM;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int impl_udev_close(struct impl *this)
|
||||
{
|
||||
if (this->udev != NULL)
|
||||
return 0;
|
||||
|
||||
this->udev = udev_new();
|
||||
if (this->udev == NULL)
|
||||
return -ENOMEM;
|
||||
|
||||
udev_unref(this->udev);
|
||||
this->udev = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -112,17 +110,11 @@ static const char *path_get_card_id(const char *path)
|
|||
return e + 5;
|
||||
}
|
||||
|
||||
static int fill_item(struct impl *this, struct item *item, struct udev_device *dev,
|
||||
static int fill_item(struct impl *this, struct udev_device *dev,
|
||||
struct spa_pod **result, struct spa_pod_builder *builder)
|
||||
{
|
||||
const char *str, *name;
|
||||
|
||||
if (item->udevice)
|
||||
udev_device_unref(item->udevice);
|
||||
item->udevice = dev;
|
||||
if (dev == NULL)
|
||||
return 0;
|
||||
|
||||
name = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
|
||||
if (!(name && *name)) {
|
||||
name = udev_device_get_property_value(dev, "ID_MODEL_ENC");
|
||||
|
|
@ -263,16 +255,27 @@ static int need_notify(struct impl *this, struct udev_device *dev, uint32_t id,
|
|||
return 1;
|
||||
}
|
||||
|
||||
static int emit_device(struct impl *this, uint32_t id, struct udev_device *dev)
|
||||
{
|
||||
uint8_t buffer[4096];
|
||||
struct spa_pod_builder b = { NULL, };
|
||||
struct spa_event *event;
|
||||
struct spa_pod *item;
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
event = spa_pod_builder_object(&b, SPA_TYPE_EVENT_Monitor, id, 0);
|
||||
fill_item(this, dev, &item, &b);
|
||||
|
||||
this->callbacks->event(this->callbacks_data, event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void impl_on_fd_events(struct spa_source *source)
|
||||
{
|
||||
struct impl *this = source->data;
|
||||
struct udev_device *dev;
|
||||
const char *action;
|
||||
uint32_t id;
|
||||
uint8_t buffer[4096];
|
||||
struct spa_pod_builder b = { NULL };
|
||||
struct spa_event *event;
|
||||
struct spa_pod *item;
|
||||
|
||||
dev = udev_monitor_receive_device(this->umonitor);
|
||||
if (dev == NULL)
|
||||
|
|
@ -292,13 +295,75 @@ static void impl_on_fd_events(struct spa_source *source)
|
|||
} else
|
||||
return;
|
||||
|
||||
if (!need_notify(this, dev, id, false))
|
||||
return;
|
||||
if (need_notify(this, dev, id, false))
|
||||
emit_device(this, id, dev);
|
||||
|
||||
spa_pod_builder_init(&b, buffer, sizeof(buffer));
|
||||
event = spa_pod_builder_object(&b, SPA_TYPE_EVENT_Monitor, id, 0);
|
||||
if (fill_item(this, &this->uitem, dev, &item, &b) == 1)
|
||||
this->callbacks->event(this->callbacks_data, event);
|
||||
udev_device_unref(dev);
|
||||
}
|
||||
|
||||
static int start_monitor(struct impl *this)
|
||||
{
|
||||
if (this->umonitor != NULL)
|
||||
return 0;
|
||||
|
||||
this->umonitor = udev_monitor_new_from_netlink(this->udev, "udev");
|
||||
if (this->umonitor == NULL)
|
||||
return -ENOMEM;
|
||||
|
||||
udev_monitor_filter_add_match_subsystem_devtype(this->umonitor,
|
||||
"sound", NULL);
|
||||
udev_monitor_enable_receiving(this->umonitor);
|
||||
|
||||
this->source.func = impl_on_fd_events;
|
||||
this->source.data = this;
|
||||
this->source.fd = udev_monitor_get_fd(this->umonitor);;
|
||||
this->source.mask = SPA_IO_IN | SPA_IO_ERR;
|
||||
|
||||
spa_loop_add_source(this->main_loop, &this->source);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int stop_monitor(struct impl *this)
|
||||
{
|
||||
if (this->umonitor == NULL)
|
||||
return 0;
|
||||
|
||||
spa_loop_remove_source(this->main_loop, &this->source);
|
||||
udev_monitor_unref(this->umonitor);
|
||||
this->umonitor = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int enum_devices(struct impl *this)
|
||||
{
|
||||
struct udev_enumerate *enumerate;
|
||||
struct udev_list_entry *devices;
|
||||
|
||||
enumerate = udev_enumerate_new(this->udev);
|
||||
if (enumerate == NULL)
|
||||
return -ENOMEM;
|
||||
|
||||
udev_enumerate_add_match_subsystem(enumerate, "sound");
|
||||
udev_enumerate_scan_devices(enumerate);
|
||||
|
||||
devices = udev_enumerate_get_list_entry(enumerate);
|
||||
|
||||
while (devices) {
|
||||
struct udev_device *dev;
|
||||
|
||||
dev = udev_device_new_from_syspath(this->udev, udev_list_entry_get_name(devices));
|
||||
|
||||
if (need_notify(this, dev, SPA_MONITOR_EVENT_Added, true))
|
||||
emit_device(this, SPA_MONITOR_EVENT_Added, dev);
|
||||
|
||||
udev_device_unref(dev);
|
||||
|
||||
devices = udev_list_entry_get_next(devices);
|
||||
}
|
||||
udev_enumerate_unref(enumerate);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
|
|
@ -320,90 +385,22 @@ impl_monitor_set_callbacks(struct spa_monitor *monitor,
|
|||
if ((res = impl_udev_open(this)) < 0)
|
||||
return res;
|
||||
|
||||
if (this->umonitor)
|
||||
udev_monitor_unref(this->umonitor);
|
||||
this->umonitor = udev_monitor_new_from_netlink(this->udev, "udev");
|
||||
if (this->umonitor == NULL)
|
||||
return -ENODEV;
|
||||
if ((res = enum_devices(this)) < 0)
|
||||
return res;
|
||||
|
||||
udev_monitor_filter_add_match_subsystem_devtype(this->umonitor, "sound", NULL);
|
||||
udev_monitor_enable_receiving(this->umonitor);
|
||||
|
||||
this->source.func = impl_on_fd_events;
|
||||
this->source.data = this;
|
||||
this->source.fd = udev_monitor_get_fd(this->umonitor);;
|
||||
this->source.mask = SPA_IO_IN | SPA_IO_ERR;
|
||||
|
||||
spa_loop_add_source(this->main_loop, &this->source);
|
||||
if ((res = start_monitor(this)) < 0)
|
||||
return res;
|
||||
} else {
|
||||
spa_loop_remove_source(this->main_loop, &this->source);
|
||||
stop_monitor(this);
|
||||
impl_udev_close(this);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int impl_monitor_enum_items(struct spa_monitor *monitor,
|
||||
uint32_t *index,
|
||||
struct spa_pod **item,
|
||||
struct spa_pod_builder *builder)
|
||||
{
|
||||
int res;
|
||||
struct impl *this;
|
||||
struct udev_device *dev;
|
||||
|
||||
spa_return_val_if_fail(monitor != NULL, -EINVAL);
|
||||
spa_return_val_if_fail(item != NULL, -EINVAL);
|
||||
|
||||
this = SPA_CONTAINER_OF(monitor, struct impl, monitor);
|
||||
|
||||
if ((res = impl_udev_open(this)) < 0)
|
||||
return res;
|
||||
|
||||
if (*index == 0 || this->index > *index) {
|
||||
if (this->enumerate)
|
||||
udev_enumerate_unref(this->enumerate);
|
||||
this->enumerate = udev_enumerate_new(this->udev);
|
||||
|
||||
udev_enumerate_add_match_subsystem(this->enumerate, "sound");
|
||||
udev_enumerate_scan_devices(this->enumerate);
|
||||
|
||||
this->devices = udev_enumerate_get_list_entry(this->enumerate);
|
||||
this->index = 0;
|
||||
}
|
||||
while (*index > this->index && this->devices) {
|
||||
next:
|
||||
this->devices = udev_list_entry_get_next(this->devices);
|
||||
this->index++;
|
||||
}
|
||||
if (this->devices == NULL) {
|
||||
fill_item(this, &this->uitem, NULL, item, builder);
|
||||
return 0;
|
||||
}
|
||||
|
||||
dev = udev_device_new_from_syspath(this->udev,
|
||||
udev_list_entry_get_name(this->devices));
|
||||
|
||||
if (dev == NULL)
|
||||
goto next;
|
||||
|
||||
if (!need_notify(this, dev, SPA_MONITOR_EVENT_Added, true))
|
||||
goto next;
|
||||
|
||||
if (fill_item(this, &this->uitem, dev, item, builder) != 1)
|
||||
goto next;
|
||||
|
||||
this->devices = udev_list_entry_get_next(this->devices);
|
||||
this->index++;
|
||||
(*index)++;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const struct spa_monitor impl_monitor = {
|
||||
SPA_VERSION_MONITOR,
|
||||
NULL,
|
||||
impl_monitor_set_callbacks,
|
||||
impl_monitor_enum_items,
|
||||
};
|
||||
|
||||
static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **interface)
|
||||
|
|
@ -426,16 +423,7 @@ static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **i
|
|||
static int impl_clear(struct spa_handle *handle)
|
||||
{
|
||||
struct impl *this = (struct impl *) handle;
|
||||
|
||||
if (this->uitem.udevice)
|
||||
udev_device_unref(this->uitem.udevice);
|
||||
if (this->enumerate)
|
||||
udev_enumerate_unref(this->enumerate);
|
||||
if (this->umonitor)
|
||||
udev_monitor_unref(this->umonitor);
|
||||
if (this->udev)
|
||||
udev_unref(this->udev);
|
||||
|
||||
impl_monitor_set_callbacks(&this->monitor, NULL, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue