Debian Codegraph @

lib/wp/private/registry.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2019-2024 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "registry.h"
10 #include "object-manager.h"
11 #include "log.h"
12
13 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-registry")
14
15 /*
16 * WpRegistry:
17 *
18 * The registry keeps track of registered objects on the wireplumber core.
19 * There are 3 kinds of registered objects:
20 *
21 * 1) PipeWire global objects, which live in another process.
22 *
23 * These objects are represented by a WpGlobal with the
24 * WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY flag set. They appear when
25 * the registry_global() event is fired and are removed by
26 * registry_global_remove(). These objects do not have an associated
27 * WpProxy, unless there is at least one WpObjectManager that is interested
28 * in them. In this case, a WpProxy is constructed and it is owned by the
29 * WpGlobal until the global is removed by the registry_global_remove() event.
30 *
31 * 2) PipeWire global objects, which were constructed by this process, either
32 * by calling into a remove factory (see wp_node_new_from_factory()) or
33 * by exporting a local object (WpImplSession etc...).
34 *
35 * These objects are also represented by a WpGlobal, which may however be
36 * constructed before they appear on the registry. The associated WpProxy
37 * calls into wp_registry_prepare_new_global() at the time it receives
38 * the 'bound' event and creates a global that has the
39 * WP_GLOBAL_FLAG_OWNED_BY_PROXY flag enabled. As the flag name suggests,
40 * these globals are "owned" by the WpProxy and the WpGlobal has no ref
41 * on the WpProxy itself. This allows destroying the proxy in client code
42 * by dropping its last reference.
43 *
44 * Normally, these global objects also appear on the pipewire registry. When
45 * this happens, the WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY flag is also added
46 * and that keeps an additional reference on the global (both flags must
47 * be dropped before the WpGlobal is destroyed).
48 *
49 * In some cases, such an object might appear first on the registry and
50 * then receive the 'bound' event. In order to handle this situation, globals
51 * are not advertised immediately when they appear on the registry, but
52 * they are added on a tmp_globals list instead, which is emptied on the
53 * next core sync. In all cases, the proxy 'bound' and the registry 'global'
54 * events will be fired in the same sync cycle, so we can catch a late
55 * 'bound' event and still associate the proxy with the WpGlobal before
56 * object managers are notified about the existence of this global.
57 *
58 * 3) WirePlumber global objects (WpModule, WpPlugin, WpSiFactory).
59 *
60 * These are local objects that have nothing to do with PipeWire. They do not
61 * have a global id and they are also not subclasses of WpProxy. The registry
62 * always owns a reference on them, so that they are kept alive for as long
63 * as the WpCore is alive.
64 */
65
66 static void
67 object_manager_destroyed (gpointer data, GObject * om)
68 {
69 WpRegistry *self = data;
70 g_ptr_array_remove_fast (self->object_managers, om);
71 }
72
73 /* find the subclass of WpPipewireGloabl that can handle
74 the given pipewire interface type of the given version */
75 static inline GType
76 find_proxy_instance_type (const char * type, guint32 version)
77 {
78 g_autofree GType *children;
79 guint n_children;
80
81 children = g_type_children (WP_TYPE_GLOBAL_PROXY, &n_children);
82
83 for (guint i = 0; i < n_children; i++) {
84 WpProxyClass *klass = (WpProxyClass *) g_type_class_ref (children[i]);
85 if (g_strcmp0 (klass->pw_iface_type, type) == 0 &&
86 klass->pw_iface_version == version) {
87 g_type_class_unref (klass);
88 return children[i];
89 }
90
91 g_type_class_unref (klass);
92 }
93
94 return WP_TYPE_GLOBAL_PROXY;
95 }
96
97 /* called by the registry when a global appears */
98 static void
99 registry_global (void *data, uint32_t id, uint32_t permissions,
100 const char *type, uint32_t version, const struct spa_dict *props)
101 {
102 WpRegistry *self = data;
103 GType gtype = find_proxy_instance_type (type, version);
104
105 wp_debug_object (wp_registry_get_core (self),
106 "global:%u perm:0x%x type:%s/%u -> %s",
107 id, permissions, type, version, g_type_name (gtype));
108
109 wp_registry_prepare_new_global (self, id, permissions,
110 WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY, gtype, NULL, props, NULL);
111 }
112
113 /* called by the registry when a global is removed */
114 static void
115 registry_global_remove (void *data, uint32_t id)
116 {
117 WpRegistry *self = data;
118 WpGlobal *global = NULL;
119
120 if (id < self->globals->len)
121 global = g_ptr_array_index (self->globals, id);
122
123 /* if not found, look in the tmp_globals, as it may still not be exposed */
124 if (!global) {
125 for (guint i = 0; i < self->tmp_globals->len; i++) {
126 WpGlobal *g = g_ptr_array_index (self->tmp_globals, i);
127 if (g->id == id) {
128 global = g;
129 break;
130 }
131 }
132 }
133
134 g_return_if_fail (global &&
135 global->flags & WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY);
136
137 wp_debug_object (wp_registry_get_core (self),
138 "global removed:%u type:%s", id, g_type_name (global->type));
139
140 wp_global_rm_flag (global, WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY);
141 }
142
143 static const struct pw_registry_events registry_events = {
144 PW_VERSION_REGISTRY_EVENTS,
145 .global = registry_global,
146 .global_remove = registry_global_remove,
147 };
148
149 void
150 wp_registry_init (WpRegistry *self)
151 {
152 self->globals =
153 g_ptr_array_new_with_free_func ((GDestroyNotify) wp_global_unref);
154 self->tmp_globals =
155 g_ptr_array_new_with_free_func ((GDestroyNotify) wp_global_unref);
156 self->objects = g_ptr_array_new_with_free_func (g_object_unref);
157 self->object_managers = g_ptr_array_new ();
158 self->features = g_ptr_array_new_with_free_func (g_free);
159 }
160
161 void
162 wp_registry_clear (WpRegistry *self)
163 {
164 wp_registry_detach (self);
165 g_clear_pointer (&self->globals, g_ptr_array_unref);
166 g_clear_pointer (&self->tmp_globals, g_ptr_array_unref);
167 g_clear_pointer (&self->features, g_ptr_array_unref);
168
169 /* remove all the registered objects
170 this will normally also destroy the object managers, eventually, since
171 they are normally ref'ed by modules, which are registered objects */
172 {
173 g_autoptr (GPtrArray) objlist = g_steal_pointer (&self->objects);
174
175 while (objlist->len > 0) {
176 g_autoptr (GObject) object = g_ptr_array_steal_index_fast (objlist,
177 objlist->len - 1);
178 wp_registry_notify_rm_object (self, object);
179 }
180 }
181
182 /* in case there are any object managers left,
183 remove the weak ref on them and let them be... */
184 {
185 g_autoptr (GPtrArray) object_mgrs;
186 GObject *om;
187
188 object_mgrs = g_steal_pointer (&self->object_managers);
189
190 while (object_mgrs->len > 0) {
191 om = g_ptr_array_steal_index_fast (object_mgrs, object_mgrs->len - 1);
192 g_object_weak_unref (om, object_manager_destroyed, self);
193 }
194 }
195 }
196
197 void
198 wp_registry_attach (WpRegistry *self, struct pw_core *pw_core)
199 {
200 self->pw_registry = pw_core_get_registry (pw_core,
201 PW_VERSION_REGISTRY, 0);
202 pw_registry_add_listener (self->pw_registry, &self->listener,
203 &registry_events, self);
204 }
205
206 void
207 wp_registry_detach (WpRegistry *self)
208 {
209 if (self->pw_registry) {
210 spa_hook_remove (&self->listener);
211 pw_proxy_destroy ((struct pw_proxy *) self->pw_registry);
212 self->pw_registry = NULL;
213 }
214
215 /* remove pipewire globals */
216 GPtrArray *objlist = self->globals;
217 while (objlist && objlist->len > 0) {
218 g_autoptr (WpGlobal) global = g_ptr_array_steal_index_fast (objlist,
219 objlist->len - 1);
220
221 if (!global)
222 continue;
223
224 if (global->proxy)
225 wp_registry_notify_rm_object (self, global->proxy);
226
227 /* remove the APPEARS_ON_REGISTRY flag to unref the proxy if it is owned
228 by the registry; set registry to NULL to avoid further interference */
229 global->registry = NULL;
230 wp_global_rm_flag (global, WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY);
231
232 /* the registry's ref on global is dropped here; it may still live if
233 there is a proxy that owns a ref on it, but global->registry is set
234 to NULL, so there is no further interference */
235 }
236
237 /* drop tmp globals as well */
238 objlist = self->tmp_globals;
239 while (objlist && objlist->len > 0) {
240 g_autoptr (WpGlobal) global = g_ptr_array_steal_index_fast (objlist,
241 objlist->len - 1);
242 wp_global_rm_flag (global, WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY);
243 }
244 }
245
246 static gboolean
247 expose_tmp_globals (WpCore *core)
248 {
249 WpRegistry *self = wp_core_get_registry (core);
250 g_autoptr (GPtrArray) tmp_globals = NULL;
251 g_autoptr (GPtrArray) object_managers = NULL;
252
253 /* in case the registry was cleared in the meantime... */
254 if (G_UNLIKELY (!self->tmp_globals))
255 return G_SOURCE_REMOVE;
256
257 /* steal the tmp_globals list and replace it with an empty one */
258 tmp_globals = self->tmp_globals;
259 self->tmp_globals =
260 g_ptr_array_new_with_free_func ((GDestroyNotify) wp_global_unref);
261
262 wp_debug_object (core, "exposing %u new globals", tmp_globals->len);
263
264 /* traverse in the order that the globals appeared on the registry */
265 for (guint i = 0; i < tmp_globals->len; i++) {
266 WpGlobal *g = g_ptr_array_index (tmp_globals, i);
267
268 /* if global was already removed, drop it */
269 if (g->flags == 0 || g->id == SPA_ID_INVALID)
270 continue;
271
272 /* if old global is owned by proxy, remove it */
273 if (self->globals->len > g->id) {
274 WpGlobal *old_g = g_ptr_array_index (self->globals, g->id);
275 if (old_g && (old_g->flags & WP_GLOBAL_FLAG_OWNED_BY_PROXY))
276 wp_global_rm_flag (old_g, WP_GLOBAL_FLAG_OWNED_BY_PROXY);
277 }
278
279 g_return_val_if_fail (self->globals->len <= g->id ||
280 g_ptr_array_index (self->globals, g->id) == NULL, G_SOURCE_REMOVE);
281
282 /* set the registry, so that wp_global_rm_flag() can work full-scale */
283 g->registry = self;
284
285 /* store it in the globals list */
286 if (self->globals->len <= g->id)
287 g_ptr_array_set_size (self->globals, g->id + 1);
288 g_ptr_array_index (self->globals, g->id) = wp_global_ref (g);
289 }
290
291 object_managers = g_ptr_array_copy (self->object_managers,
292 (GCopyFunc) g_object_ref, NULL);
293 g_ptr_array_set_free_func (object_managers, g_object_unref);
294
295 /* notify object managers */
296 for (guint i = 0; i < object_managers->len; i++) {
297 WpObjectManager *om = g_ptr_array_index (object_managers, i);
298
299 for (guint i = 0; i < tmp_globals->len; i++) {
300 WpGlobal *g = g_ptr_array_index (tmp_globals, i);
301
302 /* if global was already removed, drop it */
303 if (g->flags == 0 || g->id == SPA_ID_INVALID)
304 continue;
305
306 wp_object_manager_add_global (om, g);
307 }
308 wp_object_manager_maybe_objects_changed (om);
309 }
310
311 return G_SOURCE_REMOVE;
312 }
313
314 /*
315 * \param new_global (out) (transfer full) (optional): the new global
316 *
317 * This is normally called up to 2 times in the same sync cycle:
318 * one from registry_global(), another from the proxy bound event
319 * Unfortunately the order in which those 2 events happen is specific
320 * to the implementation of the object, which is why this is implemented
321 * with a temporary globals list that get exposed later to the object managers
322 */
323 void
324 wp_registry_prepare_new_global (WpRegistry * self, guint32 id,
325 guint32 permissions, guint32 flag, GType type,
326 WpGlobalProxy *proxy, const struct spa_dict *props,
327 WpGlobal ** new_global)
328 {
329 g_autoptr (WpGlobal) global = NULL;
330 WpCore *core = wp_registry_get_core (self);
331
332 g_return_if_fail (flag != 0);
333
334 for (guint i = 0; i < self->tmp_globals->len; i++) {
335 WpGlobal *g = g_ptr_array_index (self->tmp_globals, i);
336 if (g->id == id) {
337 global = wp_global_ref (g);
338 break;
339 }
340 }
341
342 wp_debug_object (core, "%s WpGlobal:%u type:%s proxy:%p",
343 global ? "reuse" : "new", id, g_type_name (type),
344 (global && global->proxy) ? global->proxy : proxy);
345
346 if (!global) {
347 global = g_rc_box_new0 (WpGlobal);
348 global->flags = flag;
349 global->id = id;
350 global->type = type;
351 global->permissions = permissions;
352 global->properties = props ?
353 wp_properties_new_copy_dict (props) : wp_properties_new_empty ();
354 global->proxy = proxy;
355 g_ptr_array_add (self->tmp_globals, wp_global_ref (global));
356
357 /* ensure we have 'object.id' so that we can filter by id on object managers */
358 wp_properties_setf (global->properties, PW_KEY_OBJECT_ID, "%u", global->id);
359
360 /* schedule exposing when adding the first global */
361 if (self->tmp_globals->len == 1) {
362 wp_core_idle_add_closure (core, NULL,
363 g_cclosure_new_object (G_CALLBACK (expose_tmp_globals), G_OBJECT (core)));
364 }
365 } else {
366 /* store the most permissive permissions */
367 if (permissions > global->permissions)
368 global->permissions = permissions;
369
370 global->flags |= flag;
371
372 /* store the most deep type (i.e. WpImplNode instead of WpNode),
373 so that object-manager interests can work more accurately
374 if the interest is on a specific subclass */
375 if (g_type_depth (type) > g_type_depth (global->type))
376 global->type = type;
377
378 if (proxy) {
379 g_return_if_fail (global->proxy == NULL);
380 global->proxy = proxy;
381 }
382
383 if (props)
384 wp_properties_update_from_dict (global->properties, props);
385 }
386
387 if (new_global)
388 *new_global = g_steal_pointer (&global);
389 }
390
391 void
392 wp_registry_notify_add_object (WpRegistry *self, gpointer object)
393 {
394 for (guint i = 0; i < self->object_managers->len; i++) {
395 WpObjectManager *om = g_ptr_array_index (self->object_managers, i);
396 wp_object_manager_add_object (om, object);
397 wp_object_manager_maybe_objects_changed (om);
398 }
399 }
400
401 void
402 wp_registry_notify_rm_object (WpRegistry *self, gpointer object)
403 {
404 for (guint i = 0; i < self->object_managers->len; i++) {
405 WpObjectManager *om = g_ptr_array_index (self->object_managers, i);
406 wp_object_manager_rm_object (om, object);
407 wp_object_manager_maybe_objects_changed (om);
408 }
409 }
410
411 void
412 wp_registry_install_object_manager (WpRegistry * self, WpObjectManager * om)
413 {
414 guint i;
415
416 g_object_weak_ref (G_OBJECT (om), object_manager_destroyed, self);
417 g_ptr_array_add (self->object_managers, om);
418
419 /* add pre-existing objects to the object manager,
420 in case it's interested in them */
421 for (i = 0; i < self->globals->len; i++) {
422 WpGlobal *g = g_ptr_array_index (self->globals, i);
423 /* check if null because the globals array can have gaps */
424 if (g)
425 wp_object_manager_add_global (om, g);
426 }
427 for (i = 0; i < self->objects->len; i++) {
428 GObject *o = g_ptr_array_index (self->objects, i);
429 wp_object_manager_add_object (om, o);
430 }
431
432 wp_object_manager_maybe_objects_changed (om);
433 }
434
435 /* WpGlobal */
436
437 G_DEFINE_BOXED_TYPE (WpGlobal, wp_global, wp_global_ref, wp_global_unref)
438
439 void
440 wp_global_rm_flag (WpGlobal *global, guint rm_flag)
441 {
442 WpRegistry *reg = global->registry;
443 guint32 id = global->id;
444
445 /* no flag to remove */
446 if (!(global->flags & rm_flag))
447 return;
448
449 wp_trace_boxed (WP_TYPE_GLOBAL, global,
450 "remove global %u flag 0x%x [flags:0x%x, reg:%p]",
451 id, rm_flag, global->flags, reg);
452
453 /* global was owned by the proxy; by removing the flag, we clear out
454 also the proxy pointer, which is presumably no longer valid and we
455 notify all listeners that the proxy is gone */
456 if (rm_flag == WP_GLOBAL_FLAG_OWNED_BY_PROXY) {
457 global->flags &= ~WP_GLOBAL_FLAG_OWNED_BY_PROXY;
458 if (reg && global->proxy) {
459 wp_registry_notify_rm_object (reg, global->proxy);
460 }
461 global->proxy = NULL;
462 }
463 /* registry removed the global */
464 else if (rm_flag == WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY) {
465 global->flags &= ~WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY;
466
467 /* destroy the proxy if it exists */
468 if (global->proxy) {
469 /* steal the proxy to avoid calling wp_registry_notify_rm_object()
470 again while removing OWNED_BY_PROXY;
471 keep a temporary ref so that _deactivate() doesn't crash in case the
472 pw-proxy-destroyed signal causes external references to be dropped */
473 g_autoptr (WpGlobalProxy) proxy =
474 g_object_ref (g_steal_pointer (&global->proxy));
475
476 /* notify all listeners that the proxy is gone */
477 if (reg)
478 wp_registry_notify_rm_object (reg, proxy);
479
480 /* remove FEATURE_BOUND to destroy the underlying pw_proxy */
481 wp_object_deactivate (WP_OBJECT (proxy), WP_PROXY_FEATURE_BOUND);
482
483 /* stop all in-progress activations */
484 wp_object_abort_activation (WP_OBJECT (proxy), "PipeWire proxy removed");
485
486 /* if the proxy is not owning the global, unref it */
487 if (global->flags == 0)
488 g_object_unref (proxy);
489 }
490
491 /* It's possible to receive consecutive {add, remove, add} events for the
492 * same id. Since the WpGlobal might not be destroyed immediately below,
493 * (e.g. it's in tmp_globals list), we must invalidate the id now, so that
494 * this WpGlobal is not used in reference to objects added later.
495 */
496 global->id = SPA_ID_INVALID;
497 wp_properties_setf (global->properties, PW_KEY_OBJECT_ID, NULL);
498 }
499
500 /* drop the registry's ref on global when it does not appear on the registry anymore */
501 if (!(global->flags & WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY) && reg) {
502 g_clear_pointer (&g_ptr_array_index (reg->globals, id), wp_global_unref);
503 }
504 }
505
506 struct pw_proxy *
507 wp_global_bind (WpGlobal * global)
508 {
509 g_return_val_if_fail (global->proxy, NULL);
510 g_return_val_if_fail (global->registry, NULL);
511
512 WpProxyClass *klass = WP_PROXY_GET_CLASS (global->proxy);
513 return pw_registry_bind (global->registry->pw_registry, global->id,
514 klass->pw_iface_type, klass->pw_iface_version, 0);
515 }