| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2019 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "object-manager.h" |
| 10 |
#include "log.h" |
| 11 |
#include "proxy-interfaces.h" |
| 12 |
#include "private/registry.h" |
| 13 |
|
| 14 |
#include <pipewire/pipewire.h> |
| 15 |
|
| 16 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-object-manager") |
| 17 |
|
| 18 |
/*! \defgroup wpobjectmanager WpObjectManager */ |
| 19 |
/*! |
| 20 |
* \struct WpObjectManager |
| 21 |
* |
| 22 |
* The WpObjectManager class provides a way to collect a set of objects and |
| 23 |
* be notified when objects that fulfill a certain set of criteria are created |
| 24 |
* or destroyed. |
| 25 |
* |
| 26 |
* There are 4 kinds of objects that can be managed by a |
| 27 |
* WpObjectManager: |
| 28 |
* * remote PipeWire global objects that are advertised on the registry; |
| 29 |
* these are bound locally to subclasses of WpGlobalProxy |
| 30 |
* * remote PipeWire global objects that are created by calling a remote |
| 31 |
* factory through the WirePlumber API; these are very similar to other |
| 32 |
* global objects but it should be noted that the same WpGlobalProxy |
| 33 |
* instance that created them appears in the WpObjectManager (as soon as |
| 34 |
* its WP_PROXY_FEATURE_BOUND is enabled) |
| 35 |
* * local PipeWire objects that are being exported to PipeWire |
| 36 |
* (WpImplMetadata, WpImplNode, etc); these appear in the |
| 37 |
* WpObjectManager as soon as they are exported (so, when their |
| 38 |
* WP_PROXY_FEATURE_BOUND is enabled) |
| 39 |
* * WirePlumber-specific objects, such as plugins, factories and session items |
| 40 |
* |
| 41 |
* To start an object manager, you first need to declare interest in a certain |
| 42 |
* kind of object by calling wp_object_manager_add_interest() and then install |
| 43 |
* it on the WpCore with wp_core_install_object_manager(). |
| 44 |
* |
| 45 |
* Upon installing a WpObjectManager on a WpCore, any pre-existing objects |
| 46 |
* that match the interests of this WpObjectManager will immediately become |
| 47 |
* available to get through wp_object_manager_new_iterator() and the |
| 48 |
* WpObjectManager \c object-added signal will be emitted for all of them. |
| 49 |
* However, note that if these objects need to be prepared (to activate some |
| 50 |
* features on them), the emission of \c object-added will be delayed. To know |
| 51 |
* when it is safe to access the initial set of objects, wait until the |
| 52 |
* \c installed signal has been emitted. That signal is emitted asynchronously |
| 53 |
* after all the initial objects have been prepared. |
| 54 |
* |
| 55 |
* \gproperties |
| 56 |
* |
| 57 |
* \gproperty{core, WpCore *, G_PARAM_READABLE, The core} |
| 58 |
* |
| 59 |
* \gsignals |
| 60 |
* |
| 61 |
* \par installed |
| 62 |
* \parblock |
| 63 |
* \code |
| 64 |
* void |
| 65 |
* installed_callback (WpObjectManager * self, |
| 66 |
* gpointer user_data) |
| 67 |
* \endcode |
| 68 |
* |
| 69 |
* This is emitted once after the object manager is installed with |
| 70 |
* wp_core_install_object_manager(). If there are objects that need to be |
| 71 |
* prepared asynchronously internally, emission of this signal is delayed |
| 72 |
* until all objects are ready. |
| 73 |
* |
| 74 |
* Flags: G_SIGNAL_RUN_FIRST |
| 75 |
* \endparblock |
| 76 |
* |
| 77 |
* \par object-added |
| 78 |
* \parblock |
| 79 |
* \code |
| 80 |
* void |
| 81 |
* object_added_callback (WpObjectManager * self, |
| 82 |
* gpointer object, |
| 83 |
* gpointer user_data) |
| 84 |
* \endcode |
| 85 |
* |
| 86 |
* Emitted when an object that matches the interests of this object manager |
| 87 |
* is made available. |
| 88 |
* |
| 89 |
* Parameters: |
| 90 |
* - `object` - the managed object that was just added |
| 91 |
* |
| 92 |
* Flags: G_SIGNAL_RUN_FIRST |
| 93 |
* \endparblock |
| 94 |
* |
| 95 |
* \par object-removed |
| 96 |
* \parblock |
| 97 |
* \code |
| 98 |
* void |
| 99 |
* object_removed_callback (WpObjectManager * self, |
| 100 |
* gpointer object, |
| 101 |
* gpointer user_data) |
| 102 |
* \endcode |
| 103 |
* |
| 104 |
* Emitted when an object that was previously added on this object manager is |
| 105 |
* now being removed (and most likely destroyed). At the time that this signal |
| 106 |
* is emitted, the object is still alive. |
| 107 |
* |
| 108 |
* Parameters: |
| 109 |
* - `object` - the managed object that is being removed |
| 110 |
* |
| 111 |
* Flags: G_SIGNAL_RUN_FIRST |
| 112 |
* \endparblock |
| 113 |
* |
| 114 |
* \par objects-changed |
| 115 |
* \parblock |
| 116 |
* \code |
| 117 |
* void |
| 118 |
* objects_changed_callback (WpObjectManager * self, |
| 119 |
* gpointer user_data) |
| 120 |
* \endcode |
| 121 |
* |
| 122 |
* Emitted when one or more objects have been recently added or removed from |
| 123 |
* this object manager. This signal is useful to get notified only once when |
| 124 |
* multiple changes happen in a short timespan. The receiving callback may |
| 125 |
* retrieve the updated list of objects by calling wp_object_manager_new_iterator() |
| 126 |
* |
| 127 |
* Flags: G_SIGNAL_RUN_FIRST |
| 128 |
* \endparblock |
| 129 |
*/ |
| 130 |
|
| 131 |
struct _WpObjectManager |
| 132 |
{ |
| 133 |
GObject parent; |
| 134 |
GWeakRef core; |
| 135 |
|
| 136 |
/* element-type: WpObjectInterest* */ |
| 137 |
GPtrArray *interests; |
| 138 |
/* element-type: <GType, WpProxyFeatures> */ |
| 139 |
GHashTable *features; |
| 140 |
/* objects that we are interested in, without a ref */ |
| 141 |
GPtrArray *objects; |
| 142 |
|
| 143 |
gboolean installed; |
| 144 |
gboolean changed; |
| 145 |
guint pending_objects; |
| 146 |
GSource *idle_source; |
| 147 |
}; |
| 148 |
|
| 149 |
enum { |
| 150 |
PROP_0, |
| 151 |
PROP_CORE, |
| 152 |
}; |
| 153 |
|
| 154 |
enum { |
| 155 |
SIGNAL_OBJECT_ADDED, |
| 156 |
SIGNAL_OBJECT_REMOVED, |
| 157 |
SIGNAL_OBJECTS_CHANGED, |
| 158 |
SIGNAL_INSTALLED, |
| 159 |
LAST_SIGNAL, |
| 160 |
}; |
| 161 |
|
| 162 |
static guint signals[LAST_SIGNAL] = { 0 }; |
| 163 |
|
| 164 |
G_DEFINE_TYPE (WpObjectManager, wp_object_manager, G_TYPE_OBJECT) |
| 165 |
|
| 166 |
static void |
| 167 |
wp_object_manager_init (WpObjectManager * self) |
| 168 |
{ |
| 169 |
g_weak_ref_init (&self->core, NULL); |
| 170 |
self->interests = g_ptr_array_new_with_free_func ( |
| 171 |
(GDestroyNotify) wp_object_interest_unref); |
| 172 |
self->features = g_hash_table_new (g_direct_hash, g_direct_equal); |
| 173 |
self->objects = g_ptr_array_new (); |
| 174 |
self->installed = FALSE; |
| 175 |
self->changed = FALSE; |
| 176 |
self->pending_objects = 0; |
| 177 |
} |
| 178 |
|
| 179 |
static void |
| 180 |
wp_object_manager_finalize (GObject * object) |
| 181 |
{ |
| 182 |
WpObjectManager *self = WP_OBJECT_MANAGER (object); |
| 183 |
|
| 184 |
if (self->idle_source) { |
| 185 |
g_source_destroy (self->idle_source); |
| 186 |
g_clear_pointer (&self->idle_source, g_source_unref); |
| 187 |
} |
| 188 |
g_clear_pointer (&self->objects, g_ptr_array_unref); |
| 189 |
g_clear_pointer (&self->features, g_hash_table_unref); |
| 190 |
g_clear_pointer (&self->interests, g_ptr_array_unref); |
| 191 |
g_weak_ref_clear (&self->core); |
| 192 |
|
| 193 |
G_OBJECT_CLASS (wp_object_manager_parent_class)->finalize (object); |
| 194 |
} |
| 195 |
|
| 196 |
static void |
| 197 |
wp_object_manager_get_property (GObject * object, guint property_id, |
| 198 |
GValue * value, GParamSpec * pspec) |
| 199 |
{ |
| 200 |
WpObjectManager *self = WP_OBJECT_MANAGER (object); |
| 201 |
|
| 202 |
switch (property_id) { |
| 203 |
case PROP_CORE: |
| 204 |
g_value_take_object (value, g_weak_ref_get (&self->core)); |
| 205 |
break; |
| 206 |
default: |
| 207 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
static void |
| 213 |
wp_object_manager_class_init (WpObjectManagerClass * klass) |
| 214 |
{ |
| 215 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 216 |
|
| 217 |
object_class->finalize = wp_object_manager_finalize; |
| 218 |
object_class->get_property = wp_object_manager_get_property; |
| 219 |
|
| 220 |
/* Install the properties */ |
| 221 |
|
| 222 |
g_object_class_install_property (object_class, PROP_CORE, |
| 223 |
g_param_spec_object ("core", "core", "The WpCore", WP_TYPE_CORE, |
| 224 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 225 |
|
| 226 |
signals[SIGNAL_OBJECT_ADDED] = g_signal_new ( |
| 227 |
"object-added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 228 |
0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_OBJECT); |
| 229 |
|
| 230 |
signals[SIGNAL_OBJECT_REMOVED] = g_signal_new ( |
| 231 |
"object-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 232 |
0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_OBJECT); |
| 233 |
|
| 234 |
signals[SIGNAL_OBJECTS_CHANGED] = g_signal_new ( |
| 235 |
"objects-changed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 236 |
0, NULL, NULL, NULL, G_TYPE_NONE, 0); |
| 237 |
|
| 238 |
signals[SIGNAL_INSTALLED] = g_signal_new ( |
| 239 |
"installed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 240 |
0, NULL, NULL, NULL, G_TYPE_NONE, 0); |
| 241 |
} |
| 242 |
|
| 243 |
/*! |
| 244 |
* \brief Constructs a new object manager. |
| 245 |
* \ingroup wpobjectmanager |
| 246 |
* \returns (transfer full): the newly constructed object manager |
| 247 |
*/ |
| 248 |
WpObjectManager * |
| 249 |
wp_object_manager_new (void) |
| 250 |
{ |
| 251 |
return g_object_new (WP_TYPE_OBJECT_MANAGER, NULL); |
| 252 |
} |
| 253 |
|
| 254 |
/*! |
| 255 |
* \brief Checks if an object manager is installed. |
| 256 |
* \ingroup wpobjectmanager |
| 257 |
* \param self the object manager |
| 258 |
* \returns TRUE if the object manager is installed (i.e. the |
| 259 |
* WpObjectManager \c installed signal has been emitted), FALSE otherwise |
| 260 |
*/ |
| 261 |
gboolean |
| 262 |
wp_object_manager_is_installed (WpObjectManager * self) |
| 263 |
{ |
| 264 |
g_return_val_if_fail (WP_IS_OBJECT_MANAGER (self), FALSE); |
| 265 |
return self->installed; |
| 266 |
} |
| 267 |
|
| 268 |
/*! |
| 269 |
* \brief Equivalent to: |
| 270 |
* \code |
| 271 |
* WpObjectInterest *i = wp_object_interest_new (gtype, ...); |
| 272 |
* wp_object_manager_add_interest_full (self, i); |
| 273 |
* \endcode |
| 274 |
* |
| 275 |
* The constraints specified in the variable arguments must follow the rules |
| 276 |
* documented in wp_object_interest_new(). |
| 277 |
* |
| 278 |
* \ingroup wpobjectmanager |
| 279 |
* \param self the object manager |
| 280 |
* \param gtype the GType of the objects that we are declaring interest in |
| 281 |
* \param ... a list of constraints, terminated by NULL |
| 282 |
*/ |
| 283 |
void |
| 284 |
wp_object_manager_add_interest (WpObjectManager * self, GType gtype, ...) |
| 285 |
{ |
| 286 |
WpObjectInterest *interest; |
| 287 |
va_list args; |
| 288 |
|
| 289 |
g_return_if_fail (WP_IS_OBJECT_MANAGER (self)); |
| 290 |
|
| 291 |
va_start (args, gtype); |
| 292 |
interest = wp_object_interest_new_valist (gtype, &args); |
| 293 |
wp_object_manager_add_interest_full (self, interest); |
| 294 |
va_end (args); |
| 295 |
} |
| 296 |
|
| 297 |
/*! |
| 298 |
* \brief Declares interest in a certain kind of object. |
| 299 |
* |
| 300 |
* Interest consists of a GType that the object must be an ancestor of |
| 301 |
* (g_type_is_a() must match) and optionally, a set of additional constraints |
| 302 |
* on certain properties of the object. Refer to WpObjectInterest for more details. |
| 303 |
* |
| 304 |
* \ingroup wpobjectmanager |
| 305 |
* \param self the object manager |
| 306 |
* \param interest (transfer full): the interest |
| 307 |
*/ |
| 308 |
void |
| 309 |
wp_object_manager_add_interest_full (WpObjectManager *self, |
| 310 |
WpObjectInterest * interest) |
| 311 |
{ |
| 312 |
g_autoptr (GError) error = NULL; |
| 313 |
|
| 314 |
g_return_if_fail (WP_IS_OBJECT_MANAGER (self)); |
| 315 |
|
| 316 |
if (G_UNLIKELY (!wp_object_interest_validate (interest, &error))) { |
| 317 |
wp_critical_object (self, "interest validation failed: %s", |
| 318 |
error->message); |
| 319 |
wp_object_interest_unref (interest); |
| 320 |
return; |
| 321 |
} |
| 322 |
g_ptr_array_add (self->interests, interest); |
| 323 |
} |
| 324 |
|
| 325 |
static void |
| 326 |
store_children_object_features (GHashTable *store, GType object_type, |
| 327 |
WpObjectFeatures wanted_features) |
| 328 |
{ |
| 329 |
g_autofree GType *children = NULL; |
| 330 |
GType *child; |
| 331 |
|
| 332 |
child = children = g_type_children (object_type, NULL); |
| 333 |
while (*child) { |
| 334 |
WpObjectFeatures existing_ft = (WpObjectFeatures) GPOINTER_TO_UINT ( |
| 335 |
g_hash_table_lookup (store, GSIZE_TO_POINTER (*child))); |
| 336 |
g_hash_table_insert (store, GSIZE_TO_POINTER (*child), |
| 337 |
GUINT_TO_POINTER (existing_ft | wanted_features)); |
| 338 |
store_children_object_features (store, *child, wanted_features); |
| 339 |
child++; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
/*! |
| 344 |
* \brief Requests the object manager to automatically prepare the |
| 345 |
* \a wanted_features on any managed object that is of the specified |
| 346 |
* \a object_type. |
| 347 |
* |
| 348 |
* These features will always be prepared before the object appears on the |
| 349 |
* object manager. |
| 350 |
* |
| 351 |
* \ingroup wpobjectmanager |
| 352 |
* \param self the object manager |
| 353 |
* \param object_type the WpProxy descendant type |
| 354 |
* \param wanted_features the features to enable on this kind of object |
| 355 |
*/ |
| 356 |
void |
| 357 |
wp_object_manager_request_object_features (WpObjectManager *self, |
| 358 |
GType object_type, WpObjectFeatures wanted_features) |
| 359 |
{ |
| 360 |
g_return_if_fail (WP_IS_OBJECT_MANAGER (self)); |
| 361 |
g_return_if_fail (g_type_is_a (object_type, WP_TYPE_OBJECT)); |
| 362 |
|
| 363 |
g_hash_table_insert (self->features, GSIZE_TO_POINTER (object_type), |
| 364 |
GUINT_TO_POINTER (wanted_features)); |
| 365 |
store_children_object_features (self->features, object_type, wanted_features); |
| 366 |
} |
| 367 |
|
| 368 |
/*! |
| 369 |
* \brief Gets the number of objects managed by the object manager. |
| 370 |
* \ingroup wpobjectmanager |
| 371 |
* \param self the object manager |
| 372 |
* \returns the number of objects managed by this WpObjectManager |
| 373 |
*/ |
| 374 |
guint |
| 375 |
wp_object_manager_get_n_objects (WpObjectManager * self) |
| 376 |
{ |
| 377 |
g_return_val_if_fail (WP_IS_OBJECT_MANAGER (self), 0); |
| 378 |
return self->objects->len; |
| 379 |
} |
| 380 |
|
| 381 |
struct om_iterator_data |
| 382 |
{ |
| 383 |
WpObjectManager *om; |
| 384 |
GPtrArray *objects; |
| 385 |
WpObjectInterest *interest; |
| 386 |
guint index; |
| 387 |
}; |
| 388 |
|
| 389 |
static void |
| 390 |
om_iterator_reset (WpIterator *it) |
| 391 |
{ |
| 392 |
struct om_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 393 |
it_data->index = 0; |
| 394 |
} |
| 395 |
|
| 396 |
static gboolean |
| 397 |
om_iterator_next (WpIterator *it, GValue *item) |
| 398 |
{ |
| 399 |
struct om_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 400 |
|
| 401 |
while (it_data->index < it_data->objects->len) { |
| 402 |
gpointer obj = g_ptr_array_index (it_data->objects, it_data->index++); |
| 403 |
|
| 404 |
/* take the next object that matches the interest, if any */ |
| 405 |
if (!it_data->interest || |
| 406 |
wp_object_interest_matches (it_data->interest, obj)) { |
| 407 |
g_value_init_from_instance (item, obj); |
| 408 |
return TRUE; |
| 409 |
} |
| 410 |
} |
| 411 |
return FALSE; |
| 412 |
} |
| 413 |
|
| 414 |
static gboolean |
| 415 |
om_iterator_fold (WpIterator *it, WpIteratorFoldFunc func, GValue *ret, |
| 416 |
gpointer data) |
| 417 |
{ |
| 418 |
struct om_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 419 |
gpointer *obj, *base; |
| 420 |
guint len; |
| 421 |
|
| 422 |
obj = base = it_data->objects->pdata; |
| 423 |
len = it_data->objects->len; |
| 424 |
|
| 425 |
while ((obj - base) < len) { |
| 426 |
/* only pass matching objects to the fold func if we have an interest */ |
| 427 |
if (!it_data->interest || |
| 428 |
wp_object_interest_matches (it_data->interest, *obj)) { |
| 429 |
g_auto (GValue) item = G_VALUE_INIT; |
| 430 |
g_value_init_from_instance (&item, *obj); |
| 431 |
if (!func (&item, ret, data)) |
| 432 |
return FALSE; |
| 433 |
} |
| 434 |
obj++; |
| 435 |
} |
| 436 |
return TRUE; |
| 437 |
} |
| 438 |
|
| 439 |
static void |
| 440 |
om_iterator_finalize (WpIterator *it) |
| 441 |
{ |
| 442 |
struct om_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 443 |
g_clear_pointer (&it_data->objects, g_ptr_array_unref); |
| 444 |
g_clear_pointer (&it_data->interest, wp_object_interest_unref); |
| 445 |
g_object_unref (it_data->om); |
| 446 |
} |
| 447 |
|
| 448 |
static const WpIteratorMethods om_iterator_methods = { |
| 449 |
.version = WP_ITERATOR_METHODS_VERSION, |
| 450 |
.reset = om_iterator_reset, |
| 451 |
.next = om_iterator_next, |
| 452 |
.fold = om_iterator_fold, |
| 453 |
.finalize = om_iterator_finalize, |
| 454 |
}; |
| 455 |
|
| 456 |
/*! |
| 457 |
* \brief Iterates through all the objects managed by this object manager. |
| 458 |
* \ingroup wpobjectmanager |
| 459 |
* \param self the object manager |
| 460 |
* \returns (transfer full): a WpIterator that iterates over all the managed |
| 461 |
* objects of this object manager |
| 462 |
*/ |
| 463 |
WpIterator * |
| 464 |
wp_object_manager_new_iterator (WpObjectManager * self) |
| 465 |
{ |
| 466 |
WpIterator *it; |
| 467 |
struct om_iterator_data *it_data; |
| 468 |
|
| 469 |
g_return_val_if_fail (WP_IS_OBJECT_MANAGER (self), NULL); |
| 470 |
|
| 471 |
it = wp_iterator_new (&om_iterator_methods, sizeof (struct om_iterator_data)); |
| 472 |
it_data = wp_iterator_get_user_data (it); |
| 473 |
it_data->om = g_object_ref (self); |
| 474 |
it_data->objects = g_ptr_array_copy (self->objects, NULL, NULL); |
| 475 |
it_data->index = 0; |
| 476 |
return it; |
| 477 |
} |
| 478 |
|
| 479 |
/*! |
| 480 |
* \brief Equivalent to: |
| 481 |
* \code |
| 482 |
* WpObjectInterest *i = wp_object_interest_new (gtype, ...); |
| 483 |
* return wp_object_manager_new_filtered_iterator_full (self, i); |
| 484 |
* \endcode |
| 485 |
* |
| 486 |
* The constraints specified in the variable arguments must follow the rules |
| 487 |
* documented in wp_object_interest_new(). |
| 488 |
* |
| 489 |
* \ingroup wpobjectmanager |
| 490 |
* \param self the object manager |
| 491 |
* \param gtype the GType of the objects to iterate through |
| 492 |
* \param ... a list of constraints, terminated by NULL |
| 493 |
* \returns (transfer full): a WpIterator that iterates over all the matching |
| 494 |
* objects of this object manager |
| 495 |
*/ |
| 496 |
WpIterator * |
| 497 |
wp_object_manager_new_filtered_iterator (WpObjectManager * self, GType gtype, |
| 498 |
...) |
| 499 |
{ |
| 500 |
WpObjectInterest *interest; |
| 501 |
va_list args; |
| 502 |
|
| 503 |
g_return_val_if_fail (WP_IS_OBJECT_MANAGER (self), NULL); |
| 504 |
|
| 505 |
va_start (args, gtype); |
| 506 |
interest = wp_object_interest_new_valist (gtype, &args); |
| 507 |
va_end (args); |
| 508 |
|
| 509 |
return wp_object_manager_new_filtered_iterator_full (self, interest); |
| 510 |
} |
| 511 |
|
| 512 |
/*! |
| 513 |
* \brief Iterates through all the objects managed by this object manager that |
| 514 |
* match the specified \a interest. |
| 515 |
* |
| 516 |
* \ingroup wpobjectmanager |
| 517 |
* \param self the object manager |
| 518 |
* \param interest (transfer full): the interest |
| 519 |
* \returns (transfer full): a WpIterator that iterates over all the matching |
| 520 |
* objects of this object manager |
| 521 |
*/ |
| 522 |
WpIterator * |
| 523 |
wp_object_manager_new_filtered_iterator_full (WpObjectManager * self, |
| 524 |
WpObjectInterest * interest) |
| 525 |
{ |
| 526 |
WpIterator *it; |
| 527 |
struct om_iterator_data *it_data; |
| 528 |
g_autoptr (GError) error = NULL; |
| 529 |
|
| 530 |
g_return_val_if_fail (WP_IS_OBJECT_MANAGER (self), NULL); |
| 531 |
|
| 532 |
if (G_UNLIKELY (!wp_object_interest_validate (interest, &error))) { |
| 533 |
wp_critical_object (self, "interest validation failed: %s", |
| 534 |
error->message); |
| 535 |
wp_object_interest_unref (interest); |
| 536 |
return NULL; |
| 537 |
} |
| 538 |
|
| 539 |
it = wp_iterator_new (&om_iterator_methods, sizeof (struct om_iterator_data)); |
| 540 |
it_data = wp_iterator_get_user_data (it); |
| 541 |
it_data->om = g_object_ref (self); |
| 542 |
it_data->objects = g_ptr_array_copy (self->objects, NULL, NULL); |
| 543 |
it_data->interest = interest; |
| 544 |
it_data->index = 0; |
| 545 |
return it; |
| 546 |
} |
| 547 |
|
| 548 |
/*! |
| 549 |
* \brief Equivalent to: |
| 550 |
* \code |
| 551 |
* WpObjectInterest *i = wp_object_interest_new (gtype, ...); |
| 552 |
* return wp_object_manager_lookup_full (self, i); |
| 553 |
* \endcode |
| 554 |
* |
| 555 |
* The constraints specified in the variable arguments must follow the rules |
| 556 |
* documented in wp_object_interest_new(). |
| 557 |
* |
| 558 |
* \ingroup wpobjectmanager |
| 559 |
* \param self the object manager |
| 560 |
* \param gtype the GType of the object to lookup |
| 561 |
* \param ... a list of constraints, terminated by NULL |
| 562 |
* \returns (type GObject)(transfer full)(nullable): the first managed object |
| 563 |
* that matches the lookup interest, or NULL if no object matches |
| 564 |
*/ |
| 565 |
gpointer |
| 566 |
wp_object_manager_lookup (WpObjectManager * self, GType gtype, ...) |
| 567 |
{ |
| 568 |
WpObjectInterest *interest; |
| 569 |
va_list args; |
| 570 |
|
| 571 |
g_return_val_if_fail (WP_IS_OBJECT_MANAGER (self), NULL); |
| 572 |
|
| 573 |
va_start (args, gtype); |
| 574 |
interest = wp_object_interest_new_valist (gtype, &args); |
| 575 |
va_end (args); |
| 576 |
|
| 577 |
return wp_object_manager_lookup_full (self, interest); |
| 578 |
} |
| 579 |
|
| 580 |
/*! |
| 581 |
* \brief Searches for an object that matches the specified \a interest and |
| 582 |
* returns it, if found. |
| 583 |
* |
| 584 |
* If more than one objects match, only the first one is returned. |
| 585 |
* To find multiple objects that match certain criteria, |
| 586 |
* wp_object_manager_new_filtered_iterator() is more suitable. |
| 587 |
* |
| 588 |
* \ingroup wpobjectmanager |
| 589 |
* \param self the object manager |
| 590 |
* \param interest (transfer full): the interest |
| 591 |
* \returns (type GObject)(transfer full)(nullable): the first managed object |
| 592 |
* that matches the lookup interest, or NULL if no object matches |
| 593 |
*/ |
| 594 |
gpointer |
| 595 |
wp_object_manager_lookup_full (WpObjectManager * self, |
| 596 |
WpObjectInterest * interest) |
| 597 |
{ |
| 598 |
g_auto (GValue) ret = G_VALUE_INIT; |
| 599 |
g_autoptr (WpIterator) it = |
| 600 |
wp_object_manager_new_filtered_iterator_full (self, interest); |
| 601 |
|
| 602 |
if (wp_iterator_next (it, &ret)) |
| 603 |
return g_value_dup_object (&ret); |
| 604 |
|
| 605 |
return NULL; |
| 606 |
} |
| 607 |
|
| 608 |
static gboolean |
| 609 |
wp_object_manager_is_interested_in_object (WpObjectManager * self, |
| 610 |
GObject * object) |
| 611 |
{ |
| 612 |
guint i; |
| 613 |
WpObjectInterest *interest = NULL; |
| 614 |
|
| 615 |
for (i = 0; i < self->interests->len; i++) { |
| 616 |
interest = g_ptr_array_index (self->interests, i); |
| 617 |
if (wp_object_interest_matches (interest, object)) |
| 618 |
return TRUE; |
| 619 |
} |
| 620 |
return FALSE; |
| 621 |
} |
| 622 |
|
| 623 |
static gboolean |
| 624 |
wp_object_manager_is_interested_in_global (WpObjectManager * self, |
| 625 |
WpGlobal * global, WpObjectFeatures * wanted_features) |
| 626 |
{ |
| 627 |
guint i; |
| 628 |
WpObjectInterest *interest = NULL; |
| 629 |
|
| 630 |
for (i = 0; i < self->interests->len; i++) { |
| 631 |
interest = g_ptr_array_index (self->interests, i); |
| 632 |
|
| 633 |
/* check all constraints */ |
| 634 |
WpInterestMatch match = wp_object_interest_matches_full (interest, |
| 635 |
WP_INTEREST_MATCH_FLAGS_CHECK_ALL, global->type, global->proxy, |
| 636 |
NULL, global->properties); |
| 637 |
|
| 638 |
/* and consider the manager interested if the type and the globals match... |
| 639 |
if pw_properties / g_properties fail, that's ok because they are not |
| 640 |
known yet (the proxy is likely NULL and properties not yet retrieved) */ |
| 641 |
if (SPA_FLAG_IS_SET (match, (WP_INTEREST_MATCH_GTYPE | |
| 642 |
WP_INTEREST_MATCH_PW_GLOBAL_PROPERTIES))) { |
| 643 |
gpointer ft = g_hash_table_lookup (self->features, |
| 644 |
GSIZE_TO_POINTER (global->type)); |
| 645 |
*wanted_features = (WpObjectFeatures) GPOINTER_TO_UINT (ft); |
| 646 |
|
| 647 |
/* force INFO to be present so that we can check PW_PROPERTIES constraints */ |
| 648 |
if (!(match & WP_INTEREST_MATCH_PW_PROPERTIES) && |
| 649 |
!(*wanted_features & WP_PIPEWIRE_OBJECT_FEATURE_INFO) && |
| 650 |
g_type_is_a (global->type, WP_TYPE_PIPEWIRE_OBJECT)) |
| 651 |
*wanted_features |= WP_PIPEWIRE_OBJECT_FEATURE_INFO; |
| 652 |
|
| 653 |
return TRUE; |
| 654 |
} |
| 655 |
} |
| 656 |
return FALSE; |
| 657 |
} |
| 658 |
|
| 659 |
static gboolean |
| 660 |
idle_emit_objects_changed (WpObjectManager * self) |
| 661 |
{ |
| 662 |
g_clear_pointer (&self->idle_source, g_source_unref); |
| 663 |
|
| 664 |
if (G_UNLIKELY (!self->installed)) { |
| 665 |
wp_trace_object (self, "installed"); |
| 666 |
self->installed = TRUE; |
| 667 |
g_signal_emit (self, signals[SIGNAL_INSTALLED], 0); |
| 668 |
} |
| 669 |
wp_trace_object (self, "emit objects-changed"); |
| 670 |
g_signal_emit (self, signals[SIGNAL_OBJECTS_CHANGED], 0); |
| 671 |
|
| 672 |
return G_SOURCE_REMOVE; |
| 673 |
} |
| 674 |
|
| 675 |
/*! |
| 676 |
* \brief Checks if the object manager should emit the 'objects-changed' signal |
| 677 |
* \private |
| 678 |
* \ingroup wpobjectmanager |
| 679 |
* \param self the object manager |
| 680 |
*/ |
| 681 |
void |
| 682 |
wp_object_manager_maybe_objects_changed (WpObjectManager * self) |
| 683 |
{ |
| 684 |
wp_trace_object (self, "pending:%u changed:%d idle_source:%p installed:%d", |
| 685 |
self->pending_objects, self->changed, self->idle_source, self->installed); |
| 686 |
|
| 687 |
/* always wait until there are no pending objects */ |
| 688 |
if (self->pending_objects > 0) |
| 689 |
return; |
| 690 |
|
| 691 |
/* Emit 'objects-changed' when: |
| 692 |
* - there are no pending objects |
| 693 |
* - object-added or object-removed has been emitted at least once |
| 694 |
*/ |
| 695 |
if (self->changed) { |
| 696 |
self->changed = FALSE; |
| 697 |
|
| 698 |
/* schedule emission in idle; if it is already scheduled from earlier, |
| 699 |
there is nothing to do; we will emit objects-changed once for all |
| 700 |
changes... win-win */ |
| 701 |
if (!self->idle_source) { |
| 702 |
g_autoptr (WpCore) core = g_weak_ref_get (&self->core); |
| 703 |
if (core) { |
| 704 |
wp_core_idle_add_closure (core, &self->idle_source, |
| 705 |
g_cclosure_new_object ( |
| 706 |
G_CALLBACK (idle_emit_objects_changed), G_OBJECT (self))); |
| 707 |
} |
| 708 |
} |
| 709 |
} |
| 710 |
/* Emit 'installed' when: |
| 711 |
* - there are no pending objects |
| 712 |
* - !changed: there was no object added |
| 713 |
* - !installed: not already installed |
| 714 |
* - the registry does not have pending globals; these may be interesting |
| 715 |
* to our object manager, so let's wait a bit until they are released |
| 716 |
* and re-evaluate again later |
| 717 |
* - the registry has globals; if we are on early startup where we don't |
| 718 |
* have any globals yet, wait... |
| 719 |
*/ |
| 720 |
else if (!self->installed) { |
| 721 |
g_autoptr (WpCore) core = g_weak_ref_get (&self->core); |
| 722 |
if (core) { |
| 723 |
WpRegistry *reg = wp_core_get_registry (core); |
| 724 |
if (reg->tmp_globals->len == 0 && reg->globals->len != 0) { |
| 725 |
wp_trace_object (self, "installed"); |
| 726 |
self->installed = TRUE; |
| 727 |
g_signal_emit (self, signals[SIGNAL_INSTALLED], 0); |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
/*! |
| 734 |
* \brief Adds an object to the object manager. |
| 735 |
* \private |
| 736 |
* \ingroup wpobjectmanager |
| 737 |
* \param self the object manager |
| 738 |
* \param object (transfer none): the object to add |
| 739 |
* \note caller must also call wp_object_manager_maybe_objects_changed() after |
| 740 |
*/ |
| 741 |
void |
| 742 |
wp_object_manager_add_object (WpObjectManager * self, gpointer object) |
| 743 |
{ |
| 744 |
if (wp_object_manager_is_interested_in_object (self, object)) { |
| 745 |
wp_trace_object (self, "added: " WP_OBJECT_FORMAT, WP_OBJECT_ARGS (object)); |
| 746 |
g_ptr_array_add (self->objects, object); |
| 747 |
g_signal_emit (self, signals[SIGNAL_OBJECT_ADDED], 0, object); |
| 748 |
self->changed = TRUE; |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
/*! |
| 753 |
* \brief Removes an object from the object manager. |
| 754 |
* \private |
| 755 |
* \ingroup wpobjectmanager |
| 756 |
* \param self the object manager |
| 757 |
* \param object the object to remove |
| 758 |
* \note caller must also call wp_object_manager_maybe_objects_changed() after |
| 759 |
*/ |
| 760 |
void |
| 761 |
wp_object_manager_rm_object (WpObjectManager * self, gpointer object) |
| 762 |
{ |
| 763 |
guint index; |
| 764 |
if (g_ptr_array_find (self->objects, object, &index)) { |
| 765 |
g_ptr_array_remove_index_fast (self->objects, index); |
| 766 |
g_signal_emit (self, signals[SIGNAL_OBJECT_REMOVED], 0, object); |
| 767 |
self->changed = TRUE; |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
static void |
| 772 |
on_proxy_ready (GObject * proxy, GAsyncResult * res, gpointer data) |
| 773 |
{ |
| 774 |
g_autoptr (WpObjectManager) self = WP_OBJECT_MANAGER (data); |
| 775 |
g_autoptr (GError) error = NULL; |
| 776 |
|
| 777 |
self->pending_objects--; |
| 778 |
|
| 779 |
if (!wp_object_activate_finish (WP_OBJECT (proxy), res, &error)) { |
| 780 |
wp_debug_object (self, "proxy activation failed: %s", error->message); |
| 781 |
} else { |
| 782 |
wp_object_manager_add_object (self, proxy); |
| 783 |
} |
| 784 |
|
| 785 |
wp_object_manager_maybe_objects_changed (self); |
| 786 |
} |
| 787 |
|
| 788 |
/*! |
| 789 |
* \brief Adds a global object to the object manager. |
| 790 |
* \private |
| 791 |
* \ingroup wpobjectmanager |
| 792 |
* \param self the object manager |
| 793 |
* \param global the global object to add |
| 794 |
* \note caller must also call wp_object_manager_maybe_objects_changed() after |
| 795 |
*/ |
| 796 |
void |
| 797 |
wp_object_manager_add_global (WpObjectManager * self, WpGlobal * global) |
| 798 |
{ |
| 799 |
WpProxyFeatures features = 0; |
| 800 |
|
| 801 |
/* do not allow proxies that don't have a defined subclass; |
| 802 |
bind will fail because proxy_class->pw_iface_type is NULL */ |
| 803 |
if (global->type == WP_TYPE_GLOBAL_PROXY) |
| 804 |
return; |
| 805 |
|
| 806 |
if (wp_object_manager_is_interested_in_global (self, global, &features)) { |
| 807 |
g_autoptr (WpCore) core = g_weak_ref_get (&self->core); |
| 808 |
|
| 809 |
self->pending_objects++; |
| 810 |
|
| 811 |
if (!global->proxy) |
| 812 |
global->proxy = g_object_new (global->type, |
| 813 |
"core", core, |
| 814 |
"global", global, |
| 815 |
NULL); |
| 816 |
|
| 817 |
wp_trace_object (self, "adding global:%u -> " WP_OBJECT_FORMAT, |
| 818 |
global->id, WP_OBJECT_ARGS (global->proxy)); |
| 819 |
|
| 820 |
wp_object_activate (WP_OBJECT (global->proxy), features, NULL, |
| 821 |
on_proxy_ready, g_object_ref (self)); |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
/*! |
| 826 |
* \brief Installs the object manager on this core, activating its internal |
| 827 |
* management engine. |
| 828 |
* |
| 829 |
* This will immediately emit signals about objects added on \a om |
| 830 |
* if objects that the \a om is interested in were in existence already. |
| 831 |
* |
| 832 |
* \ingroup wpobjectmanager |
| 833 |
* \param self the core |
| 834 |
* \param om (transfer none): a WpObjectManager |
| 835 |
*/ |
| 836 |
void |
| 837 |
wp_core_install_object_manager (WpCore * self, WpObjectManager * om) |
| 838 |
{ |
| 839 |
WpRegistry *reg; |
| 840 |
|
| 841 |
g_return_if_fail (WP_IS_CORE (self)); |
| 842 |
g_return_if_fail (WP_IS_OBJECT_MANAGER (om)); |
| 843 |
|
| 844 |
g_weak_ref_set (&om->core, self); |
| 845 |
|
| 846 |
reg = wp_core_get_registry (self); |
| 847 |
wp_registry_install_object_manager (reg, om); |
| 848 |
} |