diff --git a/lib/wp/core.c b/lib/wp/core.c index 82ae5146904a2955cb3a16a250da49049454b2bc..5d9c24f47dec9357d02e3c601b3e9ee61605727a 100644 --- a/lib/wp/core.c +++ b/lib/wp/core.c @@ -32,9 +32,15 @@ struct _WpCore G_DEFINE_TYPE (WpCore, wp_core, G_TYPE_OBJECT) static void -free_global_object (gpointer g) +free_global_object (gpointer p) { - g_slice_free (struct global_object, g); + struct global_object *g = p; + + /* Destroy the object */ + if (g->destroy) + g->destroy(g->object); + + g_slice_free (struct global_object, p); } static void @@ -49,17 +55,16 @@ wp_core_dispose (GObject * obj) WpCore *self = WP_CORE (obj); g_autoptr (GPtrArray) global_objects; struct global_object *global; - gint i; global_objects = g_steal_pointer (&self->global_objects); - for (i = 0; i < global_objects->len; i++) { - global = g_ptr_array_index (global_objects, i); + /* Remove and emit the removed signal for all globals */ + while (global_objects->len > 0) { + global = g_ptr_array_steal_index_fast (global_objects, + global_objects->len - 1); g_signal_emit (self, signals[SIGNAL_GLOBAL_REMOVED], global->key, global->key, global->object); - - if (global->destroy) - global->destroy (global->object); + free_global_object (global); } G_OBJECT_CLASS (wp_core_parent_class)->dispose (obj); @@ -198,7 +203,6 @@ wp_core_remove_global (WpCore * self, GQuark key, gpointer obj) { gint i; struct global_object *global; - struct global_object tmp; g_return_if_fail (WP_IS_CORE (self)); @@ -212,15 +216,12 @@ wp_core_remove_global (WpCore * self, GQuark key, gpointer obj) } if (i < self->global_objects->len) { - tmp = *global; - - g_ptr_array_remove_index_fast (self->global_objects, i); + global = g_ptr_array_steal_index_fast (self->global_objects, i); g_signal_emit (self, signals[SIGNAL_GLOBAL_REMOVED], key, - key, tmp.object); + key, global->object); - if (tmp.destroy) - tmp.destroy (tmp.object); + free_global_object (global); } } diff --git a/lib/wp/endpoint.c b/lib/wp/endpoint.c index 12853e0b7cd34eb818b10e581900457efca0122c..2ce211a4dce7c85eee7497cd343e0684530ca850 100644 --- a/lib/wp/endpoint.c +++ b/lib/wp/endpoint.c @@ -345,9 +345,7 @@ wp_endpoint_unregister (WpEndpoint * self) g_info ("WpEndpoint:%p unregistering '%s' (%s)", self, priv->name, priv->media_class); - g_object_ref (self); wp_core_remove_global (core, WP_GLOBAL_ENDPOINT, self); - g_object_unref (self); } } diff --git a/lib/wp/proxy.c b/lib/wp/proxy.c index 8d9917b47d4fe44fe8b912f24cc8a516c65cffbf..54622d135da47aa0772de09f0fb6d5c6ab169878 100644 --- a/lib/wp/proxy.c +++ b/lib/wp/proxy.c @@ -34,7 +34,6 @@ enum { enum { - SIGNAL_DESTROYED, SIGNAL_DONE, LAST_SIGNAL, }; @@ -54,9 +53,6 @@ proxy_event_destroy (void *data) /* Set the proxy to NULL */ self->proxy = NULL; - - /* Emit the destroy signal */ - g_signal_emit (data, wp_proxy_signals[SIGNAL_DESTROYED], 0); } static void @@ -198,10 +194,6 @@ wp_proxy_class_init (WpProxyClass * klass) G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); /* Signals */ - wp_proxy_signals[SIGNAL_DESTROYED] = - g_signal_new ("destroyed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (WpProxyClass, destroyed), NULL, NULL, NULL, G_TYPE_NONE, - 0); wp_proxy_signals[SIGNAL_DONE] = g_signal_new ("done", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (WpProxyClass, done), NULL, NULL, NULL, G_TYPE_NONE, 0); diff --git a/lib/wp/proxy.h b/lib/wp/proxy.h index be3570b4b33a12b54d5e0fbed078543aac414373..5dab6a80ef9a6823e32e1cb70aa26ed8195f88d6 100644 --- a/lib/wp/proxy.h +++ b/lib/wp/proxy.h @@ -24,7 +24,6 @@ struct _WpProxyClass GObjectClass parent_class; /* Signals */ - void (*destroyed)(WpProxy *wp_proxy, gpointer data); void (*done)(WpProxy *wp_proxy, gpointer data); }; diff --git a/modules/module-pipewire.c b/modules/module-pipewire.c index 831e4f966bebd6354569b94f883e6f31eeca2bc7..9394b1e6e30b27fbb4287b252ae21857ca4e867a 100644 --- a/modules/module-pipewire.c +++ b/modules/module-pipewire.c @@ -26,12 +26,14 @@ struct module_data { WpModule *module; WpRemotePipewire *remote_pipewire; + GHashTable *registered_endpoints; }; static void on_endpoint_created(GObject *initable, GAsyncResult *res, gpointer d) { - g_autoptr (WpEndpoint) endpoint = NULL; + struct module_data *data = d; + WpEndpoint *endpoint = NULL; guint global_id = 0; /* Get the endpoint */ @@ -43,8 +45,10 @@ on_endpoint_created(GObject *initable, GAsyncResult *res, gpointer d) g_object_get (endpoint, "global-id", &global_id, NULL); g_debug ("Created client endpoint for global id %d", global_id); - /* Register the endpoint */ + /* Register the endpoint and add it to the table */ wp_endpoint_register (endpoint); + g_hash_table_insert (data->registered_endpoints, GUINT_TO_POINTER(global_id), + endpoint); } static void @@ -89,6 +93,23 @@ on_node_added (WpRemotePipewire *rp, guint id, guint parent_id, gconstpointer p, endpoint_props, on_endpoint_created, data); } +static void +on_global_removed (WpRemotePipewire *rp, guint id, gpointer d) +{ + struct module_data *data = d; + WpEndpoint *endpoint = NULL; + + /* Get the endpoint */ + endpoint = g_hash_table_lookup (data->registered_endpoints, + GUINT_TO_POINTER(id)); + if (!endpoint) + return; + + /* Unregister the endpoint and remove it from the table */ + wp_endpoint_unregister (endpoint); + g_hash_table_remove (data->registered_endpoints, GUINT_TO_POINTER(id)); +} + static void module_destroy (gpointer d) { @@ -98,6 +119,10 @@ module_destroy (gpointer d) data->module = NULL; data->remote_pipewire = NULL; + /* Destroy the registered endpoints table */ + g_hash_table_unref(data->registered_endpoints); + data->registered_endpoints = NULL; + /* Clean up */ g_slice_free (struct module_data, data); } @@ -122,12 +147,15 @@ wireplumber__module_init (WpModule * module, WpCore * core, GVariant * args) data = g_slice_new0 (struct module_data); data->module = module; data->remote_pipewire = rp; + data->registered_endpoints = g_hash_table_new_full (g_direct_hash, + g_direct_equal, NULL, (GDestroyNotify)g_object_unref); /* Set the module destroy callback */ wp_module_set_destroy_callback (module, module_destroy, data); /* Register the global added/removed callbacks */ g_signal_connect(rp, "global-added::node", (GCallback)on_node_added, data); + g_signal_connect(rp, "global-removed", (GCallback)on_global_removed, data); /* Init remoted endpoint */ g_object_get (rp, "pw-core", &pw_core, "pw-remote", &pw_remote, NULL); diff --git a/modules/module-pipewire/simple-endpoint.c b/modules/module-pipewire/simple-endpoint.c index 6f617d50e70bb143032f89e65cf304147d335517..3ca0fac1e12cf8a175b904fa817d075e9e466174 100644 --- a/modules/module-pipewire/simple-endpoint.c +++ b/modules/module-pipewire/simple-endpoint.c @@ -33,6 +33,9 @@ struct _WpPipewireSimpleEndpoint /* The remote pipewire */ WpRemotePipewire *remote_pipewire; + /* Handler */ + gulong proxy_node_done_handler_id; + /* Proxies */ WpProxyNode *proxy_node; struct spa_hook node_proxy_listener; @@ -116,15 +119,6 @@ static const struct pw_node_proxy_events node_node_proxy_events = { .param = node_proxy_param, }; -static void -on_proxy_node_destroyed (WpProxy* wp_proxy, WpEndpoint *endpoint) -{ - g_return_if_fail(WP_IS_ENDPOINT(endpoint)); - - /* Unregister the endpoint */ - wp_endpoint_unregister(endpoint); -} - static void on_all_ports_done(WpProxy *proxy, gpointer data) { @@ -134,10 +128,6 @@ on_all_ports_done(WpProxy *proxy, gpointer data) if (!self->init_task) return; - /* Set destroy handler to unregister endpoint on proxy_node destruction */ - g_signal_connect (self->proxy_node, "destroyed", - G_CALLBACK(on_proxy_node_destroyed), WP_ENDPOINT(self)); - /* Finish the creation of the endpoint */ g_task_return_boolean (self->init_task, TRUE); g_clear_object(&self->init_task); @@ -158,9 +148,11 @@ on_proxy_port_created(GObject *initable, GAsyncResult *res, gpointer data) g_ptr_array_add(self->proxies_port, proxy_port); /* Register the done callback */ - g_signal_connect(self->proxy_node, "done", (GCallback)on_all_ports_done, - self); - wp_proxy_sync (WP_PROXY(self->proxy_node)); + if (!self->proxy_node_done_handler_id) { + self->proxy_node_done_handler_id = g_signal_connect_object(self->proxy_node, + "done", (GCallback)on_all_ports_done, self, 0); + wp_proxy_sync (WP_PROXY(self->proxy_node)); + } } static void @@ -280,8 +272,8 @@ wp_simple_endpoint_init_async (GAsyncInitable *initable, int io_priority, /* Register a port_added callback */ self->remote_pipewire = wp_core_get_global (core, WP_GLOBAL_REMOTE_PIPEWIRE); g_return_if_fail(self->remote_pipewire); - g_signal_connect(self->remote_pipewire, "global-added::port", - (GCallback)on_port_added, self); + g_signal_connect_object(self->remote_pipewire, "global-added::port", + (GCallback)on_port_added, self, 0); /* Create the proxy node async */ node_proxy = wp_remote_pipewire_proxy_bind (self->remote_pipewire, diff --git a/modules/module-pw-alsa-udev.c b/modules/module-pw-alsa-udev.c index 4aaab88c8b03ed88b453ac3dff109a5d2a8fe989..74c34979e6e326224e771735aab63926fec4d60d 100644 --- a/modules/module-pw-alsa-udev.c +++ b/modules/module-pw-alsa-udev.c @@ -19,12 +19,14 @@ struct impl { WpModule *module; WpRemotePipewire *remote_pipewire; + GHashTable *registered_endpoints; }; static void on_endpoint_created(GObject *initable, GAsyncResult *res, gpointer d) { - g_autoptr (WpEndpoint) endpoint = NULL; + struct impl *impl = d; + WpEndpoint *endpoint = NULL; guint global_id = 0; /* Get the endpoint */ @@ -36,8 +38,10 @@ on_endpoint_created(GObject *initable, GAsyncResult *res, gpointer d) g_object_get (endpoint, "global-id", &global_id, NULL); g_debug ("Created alsa endpoint for global id %d", global_id); - /* Register the endpoint */ + /* Register the endpoint and add it to the table */ wp_endpoint_register (endpoint); + g_hash_table_insert (impl->registered_endpoints, GUINT_TO_POINTER(global_id), + endpoint); } static void @@ -76,6 +80,23 @@ on_node_added(WpRemotePipewire *rp, guint id, guint parent_id, gconstpointer p, endpoint_props, on_endpoint_created, impl); } +static void +on_global_removed (WpRemotePipewire *rp, guint id, gpointer d) +{ + struct impl *impl = d; + WpEndpoint *endpoint = NULL; + + /* Get the endpoint */ + endpoint = g_hash_table_lookup (impl->registered_endpoints, + GUINT_TO_POINTER(id)); + if (!endpoint) + return; + + /* Unregister the endpoint and remove it from the table */ + wp_endpoint_unregister (endpoint); + g_hash_table_remove (impl->registered_endpoints, GUINT_TO_POINTER(id)); +} + static void module_destroy (gpointer data) { @@ -85,6 +106,10 @@ module_destroy (gpointer data) impl->module = NULL; impl->remote_pipewire = NULL; + /* Destroy the registered endpoints table */ + g_hash_table_unref(impl->registered_endpoints); + impl->registered_endpoints = NULL; + /* Clean up */ g_slice_free (struct impl, impl); } @@ -107,10 +132,13 @@ wireplumber__module_init (WpModule * module, WpCore * core, GVariant * args) impl = g_slice_new0(struct impl); impl->module = module; impl->remote_pipewire = rp; + impl->registered_endpoints = g_hash_table_new_full (g_direct_hash, + g_direct_equal, NULL, (GDestroyNotify)g_object_unref); /* Set destroy callback for impl */ wp_module_set_destroy_callback (module, module_destroy, impl); - /* Register the global addded callbacks */ + /* Register the global addded/removed callbacks */ g_signal_connect(rp, "global-added::node", (GCallback)on_node_added, impl); + g_signal_connect(rp, "global-removed", (GCallback)on_global_removed, impl); } diff --git a/modules/module-pw-audio-softdsp-endpoint.c b/modules/module-pw-audio-softdsp-endpoint.c index c6f2886dc84c80ad6f5f5a86ffd86fd6131da5d8..dd2d5bc7a6c0f2150b9d5d1c047a5f5c96894e1b 100644 --- a/modules/module-pw-audio-softdsp-endpoint.c +++ b/modules/module-pw-audio-softdsp-endpoint.c @@ -35,6 +35,9 @@ struct _WpPwAudioSoftdspEndpoint /* The remote pipewire */ WpRemotePipewire *remote_pipewire; + /* Handler */ + gulong proxy_dsp_done_handler_id; + /* temporary method to select which endpoint * is going to be the default input/output */ gboolean selected; @@ -242,15 +245,6 @@ static const struct pw_node_proxy_events dsp_node_events = { .param = dsp_node_event_param, }; -static void -on_proxy_node_destroyed (WpProxy* wp_proxy, WpEndpoint *endpoint) -{ - g_return_if_fail(WP_IS_ENDPOINT(endpoint)); - - /* Unregister the endpoint */ - wp_endpoint_unregister(endpoint); -} - static void on_proxy_dsp_done(WpProxy *proxy, gpointer data) { @@ -260,10 +254,6 @@ on_proxy_dsp_done(WpProxy *proxy, gpointer data) if (!self->init_task) return; - /* Set destroy handler to unregister endpoint on proxy_node destruction */ - g_signal_connect (self->proxy_node, "destroyed", - G_CALLBACK(on_proxy_node_destroyed), WP_ENDPOINT(self)); - /* Finish the creation of the endpoint */ g_task_return_boolean (self->init_task, TRUE); g_clear_object(&self->init_task); @@ -451,8 +441,11 @@ on_proxy_dsp_port_created(GObject *initable, GAsyncResult *res, gpointer data) g_ptr_array_add(self->proxies_dsp_port, proxy_dsp_port); /* Register a callback to know when all the dsp ports have been emitted */ - g_signal_connect(self->proxy_dsp, "done", (GCallback)on_proxy_dsp_done, self); - wp_proxy_sync (WP_PROXY(self->proxy_dsp)); + if (!self->proxy_dsp_done_handler_id) { + self->proxy_dsp_done_handler_id = g_signal_connect_object(self->proxy_dsp, + "done", (GCallback)on_proxy_dsp_done, self, 0); + wp_proxy_sync (WP_PROXY(self->proxy_dsp)); + } } static void @@ -654,8 +647,8 @@ wp_endpoint_init_async (GAsyncInitable *initable, int io_priority, /* Register a port_added callback */ self->remote_pipewire = wp_core_get_global (core, WP_GLOBAL_REMOTE_PIPEWIRE); g_return_if_fail(self->remote_pipewire); - g_signal_connect(self->remote_pipewire, "global-added::port", - (GCallback)on_port_added, self); + g_signal_connect_object(self->remote_pipewire, "global-added::port", + (GCallback)on_port_added, self, 0); /* Call the parent interface */ wp_endpoint_parent_interface->init_async (initable, io_priority, cancellable,