modules: handle factory destroy

Listen for factory destroy and clean up the module.

See #565
This commit is contained in:
Wim Taymans 2022-05-06 12:11:20 +02:00
parent 95f8c569a0
commit 383df85466
12 changed files with 264 additions and 90 deletions

View file

@ -56,14 +56,16 @@ static const struct spa_dict_item module_props[] = {
};
struct factory_data {
struct pw_impl_module *module;
struct pw_context *context;
struct pw_impl_factory *this;
struct pw_impl_module *module;
struct spa_hook module_listener;
struct pw_impl_factory *factory;
struct spa_hook factory_listener;
struct spa_list link_list;
struct spa_hook module_listener;
struct pw_work_queue *work;
};
@ -407,7 +409,7 @@ static void *create_object(void *_data,
linger = pw_properties_get_bool(properties, PW_KEY_OBJECT_LINGER, false);
pw_properties_setf(properties, PW_KEY_FACTORY_ID, "%d",
pw_impl_factory_get_info(d->this)->id);
pw_impl_factory_get_info(d->factory)->id);
client = resource ? pw_resource_get_client(resource) : NULL;
if (client && !linger)
@ -467,24 +469,40 @@ static const struct pw_impl_factory_implementation impl_factory = {
.create_object = create_object,
};
static void module_destroy(void *data)
static void factory_destroy(void *data)
{
struct factory_data *d = data;
struct link_data *ld, *t;
spa_hook_remove(&d->module_listener);
spa_hook_remove(&d->factory_listener);
spa_list_for_each_safe(ld, t, &d->link_list, l)
pw_impl_link_destroy(ld->link);
pw_impl_factory_destroy(d->this);
d->factory = NULL;
if (d->module)
pw_impl_module_destroy(d->module);
}
static const struct pw_impl_factory_events factory_events = {
PW_VERSION_IMPL_FACTORY_EVENTS,
.destroy = factory_destroy,
};
static void module_destroy(void *data)
{
struct factory_data *d = data;
spa_hook_remove(&d->module_listener);
d->module = NULL;
if (d->factory)
pw_impl_factory_destroy(d->factory);
}
static void module_registered(void *data)
{
struct factory_data *d = data;
struct pw_impl_module *module = d->module;
struct pw_impl_factory *factory = d->this;
struct pw_impl_factory *factory = d->factory;
struct spa_dict_item items[1];
char id[16];
int res;
@ -525,7 +543,7 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
return -errno;
data = pw_impl_factory_get_user_data(factory);
data->this = factory;
data->factory = factory;
data->module = module;
data->context = context;
data->work = pw_context_get_work_queue(context);
@ -540,6 +558,7 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
pw_impl_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
pw_impl_factory_add_listener(factory, &data->factory_listener, &factory_events, data);
pw_impl_module_add_listener(module, &data->module_listener, &module_events, data);
return 0;