Debian Codegraph @

lib/wp/metadata.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author Raghavendra Rao <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "metadata.h"
10 #include "core.h"
11 #include "log.h"
12 #include "error.h"
13 #include "wpenums.h"
14
15 #include <pipewire/impl.h>
16 #include <pipewire/pipewire.h>
17 #include <pipewire/extensions/metadata.h>
18
19 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-metadata")
20
21 /*! \defgroup wpmetadata WpMetadata */
22 /*!
23 * \struct WpMetadata
24 *
25 * The WpMetadata class allows accessing the properties and methods of
26 * PipeWire metadata object (`struct pw_metadata`).
27 *
28 * A WpMetadata is constructed internally when a new metadata object appears on the
29 * PipeWire registry and it is made available through the WpObjectManager API.
30 *
31 * \gsignals
32 *
33 * \par changed
34 * \parblock
35 * \code
36 * void
37 * changed_callback (WpMetadata * self,
38 * guint subject,
39 * gchar * key,
40 * gchar * type,
41 * gchar * value,
42 * gpointer user_data)
43 * \endcode
44 * Emitted when metadata change
45 *
46 * Parameters:
47 * - `subject` - the metadata subject id
48 * - `key` - the metadata key
49 * - `type` - the value type
50 * - `value` - the metadata value
51 *
52 * Flags: G_SIGNAL_RUN_LAST
53 * \endparblock
54 */
55 enum {
56 SIGNAL_CHANGED,
57 N_SIGNALS,
58 };
59
60 static guint32 signals[N_SIGNALS] = {0};
61
62 /* data structure */
63
64 struct item
65 {
66 uint32_t subject;
67 gchar *key;
68 gchar *type;
69 gchar *value;
70 };
71
72 static void
73 set_item (struct item * item, uint32_t subject, const char * key,
74 const char * type, const char * value)
75 {
76 item->subject = subject;
77 item->key = g_strdup (key);
78 item->type = g_strdup (type);
79 item->value = g_strdup (value);
80 }
81
82 static void
83 clear_item (struct item * item)
84 {
85 g_free (item->key);
86 g_free (item->type);
87 g_free (item->value);
88 spa_zero (*item);
89 }
90
91 static struct item *
92 find_item (struct pw_array * metadata, uint32_t subject, const char * key)
93 {
94 struct item *item;
95
96 pw_array_for_each (item, metadata) {
97 if (item->subject == subject && (key == NULL || !strcmp (item->key, key))) {
98 return item;
99 }
100 }
101 return NULL;
102 }
103
104 static int
105 clear_subject (struct pw_array * metadata, uint32_t subject)
106 {
107 struct item *item;
108 uint32_t removed = 0;
109
110 while (true) {
111 item = find_item (metadata, subject, NULL);
112 if (item == NULL)
113 break;
114 clear_item (item);
115 pw_array_remove (metadata, item);
116 removed++;
117 }
118
119 return removed;
120 }
121
122 static void
123 clear_items (struct pw_array * metadata)
124 {
125 struct item *item;
126
127 pw_array_consume (item, metadata) {
128 clear_item (item);
129 pw_array_remove (metadata, item);
130 }
131 pw_array_reset (metadata);
132 }
133
134 typedef struct _WpMetadataPrivate WpMetadataPrivate;
135 struct _WpMetadataPrivate
136 {
137 struct pw_metadata *iface;
138 struct spa_hook listener;
139 struct pw_array metadata;
140 gboolean remove_listener;
141 };
142
143 G_DEFINE_TYPE_WITH_PRIVATE (WpMetadata, wp_metadata, WP_TYPE_GLOBAL_PROXY)
144
145 static void
146 wp_metadata_init (WpMetadata * self)
147 {
148 WpMetadataPrivate *priv = wp_metadata_get_instance_private (self);
149 pw_array_init (&priv->metadata, 4096);
150 }
151
152 static void
153 wp_metadata_finalize (GObject * object)
154 {
155 WpMetadataPrivate *priv =
156 wp_metadata_get_instance_private (WP_METADATA (object));
157
158 pw_array_clear (&priv->metadata);
159
160 G_OBJECT_CLASS (wp_metadata_parent_class)->finalize (object);
161 }
162
163 static WpObjectFeatures
164 wp_metadata_get_supported_features (WpObject * object)
165 {
166 return WP_PROXY_FEATURE_BOUND | WP_METADATA_FEATURE_DATA;
167 }
168
169 enum {
170 STEP_BIND = WP_TRANSITION_STEP_CUSTOM_START,
171 STEP_CACHE
172 };
173
174 static guint
175 wp_metadata_activate_get_next_step (WpObject * object,
176 WpFeatureActivationTransition * transition, guint step,
177 WpObjectFeatures missing)
178 {
179 g_return_val_if_fail (
180 missing & (WP_PROXY_FEATURE_BOUND | WP_METADATA_FEATURE_DATA),
181 WP_TRANSITION_STEP_ERROR);
182
183 /* bind if not already bound */
184 if (missing & WP_PROXY_FEATURE_BOUND)
185 return STEP_BIND;
186 else
187 return STEP_CACHE;
188 }
189
190 static void
191 wp_metadata_activate_execute_step (WpObject * object,
192 WpFeatureActivationTransition * transition, guint step,
193 WpObjectFeatures missing)
194 {
195 switch (step) {
196 case STEP_CACHE:
197 /* just wait for initial_sync_done() */
198 break;
199 default:
200 WP_OBJECT_CLASS (wp_metadata_parent_class)->
201 activate_execute_step (object, transition, step, missing);
202 break;
203 }
204 }
205
206 static int
207 metadata_event_property (void *object, uint32_t subject, const char *key,
208 const char *type, const char *value)
209 {
210 WpMetadata *self = WP_METADATA (object);
211 WpMetadataPrivate *priv =
212 wp_metadata_get_instance_private (WP_METADATA (self));
213 struct item *item = NULL;
214
215 if (key == NULL) {
216 if (clear_subject (&priv->metadata, subject) > 0) {
217 wp_debug_object (self, "remove id:%d", subject);
218 g_signal_emit (self, signals[SIGNAL_CHANGED], 0, subject, NULL, NULL,
219 NULL);
220 }
221 return 0;
222 }
223
224 item = find_item (&priv->metadata, subject, key);
225 if (item == NULL) {
226 if (value == NULL)
227 return 0;
228 item = pw_array_add (&priv->metadata, sizeof (*item));
229 if (item == NULL)
230 return -errno;
231 } else {
232 clear_item (item);
233 }
234
235 if (value != NULL) {
236 if (type == NULL)
237 type = "string";
238 set_item (item, subject, key, type, value);
239 wp_debug_object (self, "add id:%d key:%s type:%s value:%s",
240 subject, key, type, value);
241 } else {
242 type = NULL;
243 pw_array_remove (&priv->metadata, item);
244 wp_debug_object (self, "remove id:%d key:%s", subject, key);
245 }
246
247 g_signal_emit (self, signals[SIGNAL_CHANGED], 0, subject, key, type, value);
248 return 0;
249 }
250
251 static const struct pw_metadata_events metadata_events = {
252 PW_VERSION_METADATA_EVENTS,
253 .property = metadata_event_property,
254 };
255
256 static void
257 initial_sync_done (WpCore * core, GAsyncResult * res, WpMetadata * self)
258 {
259 g_autoptr (GError) error = NULL;
260 if (!wp_core_sync_finish (core, res, &error)) {
261 wp_warning_object (self, "core sync error: %s", error->message);
262 return;
263 }
264
265 wp_object_update_features (WP_OBJECT (self), WP_METADATA_FEATURE_DATA, 0);
266 }
267
268 static void
269 wp_metadata_pw_proxy_created (WpProxy * proxy, struct pw_proxy * pw_proxy)
270 {
271 WpMetadata *self = WP_METADATA (proxy);
272 WpMetadataPrivate *priv = wp_metadata_get_instance_private (self);
273 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
274
275 priv->iface = (struct pw_metadata *) pw_proxy;
276 pw_metadata_add_listener (priv->iface, &priv->listener,
277 &metadata_events, self);
278 priv->remove_listener = TRUE;
279 wp_core_sync_closure (core, NULL,
280 g_cclosure_new_object ((GCallback) initial_sync_done, G_OBJECT (self)));
281 }
282
283 static void
284 wp_metadata_pw_proxy_destroyed (WpProxy * proxy)
285 {
286 WpMetadata *self = WP_METADATA (proxy);
287 WpMetadataPrivate *priv = wp_metadata_get_instance_private (self);
288
289 if (priv->remove_listener) {
290 spa_hook_remove (&priv->listener);
291 priv->remove_listener = FALSE;
292 }
293 clear_items (&priv->metadata);
294 wp_object_update_features (WP_OBJECT (self), 0, WP_METADATA_FEATURE_DATA);
295
296 WP_PROXY_CLASS (wp_metadata_parent_class)->pw_proxy_destroyed (proxy);
297 }
298
299 static void
300 wp_metadata_class_init (WpMetadataClass * klass)
301 {
302 GObjectClass *object_class = (GObjectClass *) klass;
303 WpObjectClass *wpobject_class = (WpObjectClass *) klass;
304 WpProxyClass *proxy_class = (WpProxyClass *) klass;
305
306 object_class->finalize = wp_metadata_finalize;
307
308 wpobject_class->get_supported_features = wp_metadata_get_supported_features;
309 wpobject_class->activate_get_next_step = wp_metadata_activate_get_next_step;
310 wpobject_class->activate_execute_step = wp_metadata_activate_execute_step;
311
312 proxy_class->pw_iface_type = PW_TYPE_INTERFACE_Metadata;
313 proxy_class->pw_iface_version = PW_VERSION_METADATA;
314 proxy_class->pw_proxy_created = wp_metadata_pw_proxy_created;
315 proxy_class->pw_proxy_destroyed = wp_metadata_pw_proxy_destroyed;
316
317 signals[SIGNAL_CHANGED] = g_signal_new ("changed", G_TYPE_FROM_CLASS (klass),
318 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 4,
319 G_TYPE_UINT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
320 }
321
322 /*!
323 * \struct WpMetadataItem
324 *
325 * WpMetadataItem holds the subject, key, type and value of a metadata entry.
326 */
327 struct _WpMetadataItem
328 {
329 WpMetadata *metadata;
330 guint32 subject;
331 const gchar *key;
332 const gchar *type;
333 const gchar *value;
334 };
335
336 G_DEFINE_BOXED_TYPE (WpMetadataItem, wp_metadata_item,
337 wp_metadata_item_ref, wp_metadata_item_unref)
338
339 static WpMetadataItem *
340 wp_metadata_item_new (WpMetadata *metadata, guint32 subject, const gchar *key,
341 const gchar *type, const gchar *value)
342 {
343 WpMetadataItem *self = g_rc_box_new0 (WpMetadataItem);
344 self->metadata = g_object_ref (metadata);
345 self->subject = subject;
346 self->key = key;
347 self->type = type;
348 self->value = value;
349 return self;
350 }
351
352 static void
353 wp_metadata_item_free (gpointer p)
354 {
355 WpMetadataItem *self = p;
356 g_clear_object (&self->metadata);
357 }
358
359 /*!
360 * \brief Increases the reference count of a metadata item object
361 * \ingroup wpmetadata
362 * \param self a metadata item object
363 * \returns (transfer full): \a self with an additional reference count on it
364 * \since 0.5.0
365 */
366 WpMetadataItem *
367 wp_metadata_item_ref (WpMetadataItem *self)
368 {
369 return g_rc_box_acquire (self);
370 }
371
372 /*!
373 * \brief Decreases the reference count on \a self and frees it when the ref
374 * count reaches zero.
375 * \ingroup wpmetadata
376 * \param self (transfer full): a metadata item object
377 * \since 0.5.0
378 */
379 void
380 wp_metadata_item_unref (WpMetadataItem *self)
381 {
382 g_rc_box_release_full (self, wp_metadata_item_free);
383 }
384
385 /*!
386 * \brief Gets the subject from a metadata item
387 *
388 * \ingroup wpmetadata
389 * \param self the item held by the GValue that was returned from the WpIterator
390 * of wp_metadata_new_iterator()
391 * \returns the metadata subject of the \a item
392 * \since 0.5.0
393 */
394 guint32
395 wp_metadata_item_get_subject (WpMetadataItem * self)
396 {
397 return self->subject;
398 }
399
400 /*!
401 * \brief Gets the key from a metadata item
402 *
403 * \ingroup wpmetadata
404 * \param self the item held by the GValue that was returned from the WpIterator
405 * of wp_metadata_new_iterator()
406 * \returns (transfer none): the metadata key of the \a item
407 * \since 0.5.0
408 */
409 const gchar *
410 wp_metadata_item_get_key (WpMetadataItem * self)
411 {
412 return self->key;
413 }
414
415 /*!
416 * \brief Gets the value type from a metadata item
417 *
418 * \ingroup wpmetadata
419 * \param self the item held by the GValue that was returned from the WpIterator
420 * of wp_metadata_new_iterator()
421 * \returns (transfer none): the metadata value type of the \a item
422 * \since 0.5.0
423 */
424 const gchar *
425 wp_metadata_item_get_value_type (WpMetadataItem * self)
426 {
427 return self->type;
428 }
429
430 /*!
431 * \brief Gets the value from a metadata item
432 *
433 * \ingroup wpmetadata
434 * \param self the item held by the GValue that was returned from the WpIterator
435 * of wp_metadata_new_iterator()
436 * \returns (transfer none): the metadata value of the \a item
437 * \since 0.5.0
438 */
439 const gchar *
440 wp_metadata_item_get_value (WpMetadataItem * self)
441 {
442 return self->value;
443 }
444
445 struct metadata_iterator_data
446 {
447 WpMetadata *metadata;
448 const struct item *item;
449 guint32 subject;
450 };
451
452 static void
453 metadata_iterator_reset (WpIterator *it)
454 {
455 struct metadata_iterator_data *it_data = wp_iterator_get_user_data (it);
456 WpMetadataPrivate *priv =
457 wp_metadata_get_instance_private (it_data->metadata);
458
459 it_data->item = pw_array_first (&priv->metadata);
460 }
461
462 static gboolean
463 metadata_iterator_next (WpIterator *it, GValue *item)
464 {
465 struct metadata_iterator_data *it_data = wp_iterator_get_user_data (it);
466 WpMetadataPrivate *priv =
467 wp_metadata_get_instance_private (it_data->metadata);
468
469 while (pw_array_check (&priv->metadata, it_data->item)) {
470 if ((it_data->subject == PW_ID_ANY ||
471 it_data->subject == it_data->item->subject)) {
472 g_autoptr (WpMetadataItem) mi = wp_metadata_item_new (it_data->metadata,
473 it_data->item->subject, it_data->item->key, it_data->item->type,
474 it_data->item->value);
475 g_value_init (item, WP_TYPE_METADATA_ITEM);
476 g_value_take_boxed (item, g_steal_pointer (&mi));
477 it_data->item++;
478 return TRUE;
479 }
480 it_data->item++;
481 }
482 return FALSE;
483 }
484
485 static gboolean
486 metadata_iterator_fold (WpIterator *it, WpIteratorFoldFunc func, GValue *ret,
487 gpointer data)
488 {
489 struct metadata_iterator_data *it_data = wp_iterator_get_user_data (it);
490 WpMetadataPrivate *priv =
491 wp_metadata_get_instance_private (it_data->metadata);
492 const struct item *i;
493
494 pw_array_for_each (i, &priv->metadata) {
495 if ((it_data->subject == PW_ID_ANY ||
496 it_data->subject == it_data->item->subject)) {
497 g_auto (GValue) item = G_VALUE_INIT;
498 g_autoptr (WpMetadataItem) mi = wp_metadata_item_new (it_data->metadata,
499 it_data->item->subject, it_data->item->key, it_data->item->type,
500 it_data->item->value);
501 g_value_init (&item, WP_TYPE_METADATA_ITEM);
502 g_value_take_boxed (&item, g_steal_pointer (&mi));
503 if (!func (&item, ret, data))
504 return FALSE;
505 }
506 }
507 return TRUE;
508 }
509
510 static void
511 metadata_iterator_finalize (WpIterator *it)
512 {
513 struct metadata_iterator_data *it_data = wp_iterator_get_user_data (it);
514 g_object_unref (it_data->metadata);
515 }
516
517 static const WpIteratorMethods metadata_iterator_methods = {
518 .version = WP_ITERATOR_METHODS_VERSION,
519 .reset = metadata_iterator_reset,
520 .next = metadata_iterator_next,
521 .fold = metadata_iterator_fold,
522 .finalize = metadata_iterator_finalize,
523 };
524
525 /*!
526 * \brief Iterates over metadata items that matches the given \a subject.
527 *
528 * If no constraints are specified, the returned iterator iterates over all the
529 * stored metadata.
530 *
531 * Note that this method works on cached metadata. When you change metadata
532 * with wp_metadata_set(), this cache will be updated on the next round-trip
533 * with the pipewire server.
534 *
535 * \ingroup wpmetadata
536 * \param self a metadata object
537 * \param subject the metadata subject id, or -1 (PW_ID_ANY)
538 * \returns (transfer full): an iterator that iterates over the found metadata.
539 * The type of the iterator item is WpMetadataItem.
540 */
541 WpIterator *
542 wp_metadata_new_iterator (WpMetadata * self, guint32 subject)
543 {
544 WpMetadataPrivate *priv;
545 g_autoptr (WpIterator) it = NULL;
546 struct metadata_iterator_data *it_data;
547
548 g_return_val_if_fail (self != NULL, NULL);
549 priv = wp_metadata_get_instance_private (self);
550
551 it = wp_iterator_new (&metadata_iterator_methods,
552 sizeof (struct metadata_iterator_data));
553 it_data = wp_iterator_get_user_data (it);
554 it_data->metadata = g_object_ref (self);
555 it_data->item = pw_array_first (&priv->metadata);
556 it_data->subject = subject;
557 return g_steal_pointer (&it);
558 }
559
560 /*!
561 * \brief Finds the metadata value given its \a subject and \a key.
562 *
563 * \ingroup wpmetadata
564 * \param self a metadata object
565 * \param subject the metadata subject id
566 * \param key the metadata key name
567 * \param type (out)(optional): the metadata type name
568 * \returns the metadata string value, or NULL if not found.
569 */
570 const gchar *
571 wp_metadata_find (WpMetadata * self, guint32 subject, const gchar * key,
572 const gchar ** type)
573 {
574 g_autoptr (WpIterator) it = NULL;
575 g_auto (GValue) val = G_VALUE_INIT;
576 it = wp_metadata_new_iterator (self, subject);
577 for (; wp_iterator_next (it, &val); g_value_unset (&val)) {
578 WpMetadataItem *mi = g_value_get_boxed (&val);
579 const gchar *k = wp_metadata_item_get_key (mi);
580 const gchar *t = wp_metadata_item_get_value_type (mi);
581 const gchar *v = wp_metadata_item_get_value (mi);
582 if (g_strcmp0 (k, key) == 0) {
583 if (type)
584 *type = t;
585 g_value_unset (&val);
586 return v;
587 }
588 }
589 return NULL;
590 }
591
592 /*!
593 * \brief Sets the metadata associated with the given \a subject and \a key.
594 * Use NULL as a value to unset the given \a key and use NULL in both \a key
595 * and \a value to remove all metadata associated with the given \a subject.
596 *
597 * \ingroup wpmetadata
598 * \param self the metadata object
599 * \param subject the subject id for which this metadata property is being set
600 * \param key (nullable): the key to set, or NULL to remove all metadata for
601 * \a subject
602 * \param type (nullable): the type of the value; NULL is synonymous to "string"
603 * \param value (nullable): the value to set, or NULL to unset the given \a key
604 */
605 void
606 wp_metadata_set (WpMetadata * self, guint32 subject,
607 const gchar * key, const gchar * type, const gchar * value)
608 {
609 WpMetadataPrivate *priv = wp_metadata_get_instance_private (self);
610 pw_metadata_set_property (priv->iface, subject, key, type, value);
611 }
612
613 /*!
614 * \brief Clears permanently all stored metadata.
615 * \ingroup wpmetadata
616 * \param self the metadata object
617 */
618 void
619 wp_metadata_clear (WpMetadata * self)
620 {
621 WpMetadataPrivate *priv = wp_metadata_get_instance_private (self);
622 pw_metadata_clear (priv->iface);
623 }
624
625 /*!
626 * \struct WpImplMetadata
627 * Implementation of the metadata object.
628 *
629 * Activate this object with at least WP_PROXY_FEATURE_BOUND to export it to
630 * PipeWire.
631 */
632 struct _WpImplMetadata
633 {
634 WpMetadata parent;
635
636 gchar *name;
637 WpProperties *properties;
638
639 struct pw_impl_metadata *impl;
640 struct spa_hook listener;
641 };
642
643 enum {
644 PROP_0,
645 PROP_NAME,
646 PROP_PROPERTIES,
647 };
648
649 G_DEFINE_TYPE (WpImplMetadata, wp_impl_metadata, WP_TYPE_METADATA)
650
651 static void
652 wp_impl_metadata_init (WpImplMetadata * self)
653 {
654 }
655
656 static const struct pw_impl_metadata_events impl_metadata_events = {
657 PW_VERSION_IMPL_METADATA_EVENTS,
658 .property = metadata_event_property,
659 };
660
661 static void
662 wp_impl_metadata_constructed (GObject *object)
663 {
664 WpImplMetadata *self = WP_IMPL_METADATA (object);
665 WpMetadataPrivate *priv =
666 wp_metadata_get_instance_private (WP_METADATA (self));
667 g_autoptr (WpCore) core = NULL;
668 struct pw_context *pw_context;
669 struct pw_properties *props = NULL;
670
671 core = wp_object_get_core (WP_OBJECT (self));
672 g_return_if_fail (core);
673 pw_context = wp_core_get_pw_context (core);
674 g_return_if_fail (pw_context);
675
676 if (self->properties)
677 props = wp_properties_to_pw_properties (self->properties);
678
679 self->impl = pw_context_create_metadata (pw_context, self->name, props , 0);
680 g_return_if_fail (self->impl);
681 priv->iface = pw_impl_metadata_get_implementation (self->impl);
682 g_return_if_fail (priv->iface);
683
684 pw_impl_metadata_add_listener (self->impl, &self->listener,
685 &impl_metadata_events, self);
686
687 wp_object_update_features (WP_OBJECT (self), WP_METADATA_FEATURE_DATA, 0);
688 G_OBJECT_CLASS (wp_impl_metadata_parent_class)->constructed (object);
689 }
690
691 static void
692 wp_impl_metadata_finalize (GObject * object)
693 {
694 WpImplMetadata *self = WP_IMPL_METADATA (object);
695
696 spa_hook_remove (&self->listener);
697 g_clear_pointer (&self->impl, pw_impl_metadata_destroy);
698 g_clear_pointer (&self->properties, wp_properties_unref);
699 g_clear_pointer (&self->name, g_free);
700
701 G_OBJECT_CLASS (wp_impl_metadata_parent_class)->finalize (object);
702 }
703
704 static void
705 wp_impl_metadata_set_property (GObject * object, guint property_id,
706 const GValue * value, GParamSpec * pspec)
707 {
708 WpImplMetadata *self = WP_IMPL_METADATA (object);
709
710 switch (property_id) {
711 case PROP_NAME:
712 g_clear_pointer (&self->name, g_free);
713 self->name = g_value_dup_string (value);
714 break;
715 case PROP_PROPERTIES:
716 g_clear_pointer (&self->properties, wp_properties_unref);
717 self->properties = g_value_dup_boxed (value);
718 break;
719 default:
720 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
721 break;
722 }
723 }
724
725 static void
726 wp_impl_metadata_get_property (GObject * object, guint property_id,
727 GValue * value, GParamSpec * pspec)
728 {
729 WpImplMetadata *self = WP_IMPL_METADATA (object);
730
731 switch (property_id) {
732 case PROP_NAME:
733 g_value_set_string (value, self->name);
734 break;
735 case PROP_PROPERTIES:
736 g_value_set_boxed (value, self->properties);
737 break;
738 default:
739 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
740 break;
741 }
742 }
743
744 static void
745 wp_impl_metadata_activate_execute_step (WpObject * object,
746 WpFeatureActivationTransition * transition, guint step,
747 WpObjectFeatures missing)
748 {
749 WpImplMetadata *self = WP_IMPL_METADATA (object);
750 WpMetadataPrivate *priv =
751 wp_metadata_get_instance_private (WP_METADATA (self));
752
753 switch (step) {
754 case STEP_BIND: {
755 g_autoptr (WpCore) core = wp_object_get_core (object);
756 struct pw_core *pw_core = wp_core_get_pw_core (core);
757 const struct pw_properties *props = NULL;
758
759 /* no pw_core -> we are not connected */
760 if (!pw_core) {
761 wp_transition_return_error (WP_TRANSITION (transition), g_error_new (
762 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
763 "The WirePlumber core is not connected; "
764 "object cannot be exported to PipeWire"));
765 return;
766 }
767
768 props = pw_impl_metadata_get_properties (self->impl);
769 wp_proxy_set_pw_proxy (WP_PROXY (self), pw_core_export (pw_core,
770 PW_TYPE_INTERFACE_Metadata, &props->dict, priv->iface, 0)
771 );
772 break;
773 }
774 case STEP_CACHE:
775 /* never reached because WP_METADATA_FEATURE_DATA is always enabled */
776 g_assert_not_reached ();
777 break;
778 default:
779 WP_OBJECT_CLASS (wp_impl_metadata_parent_class)->
780 activate_execute_step (object, transition, step, missing);
781 break;
782 }
783 }
784
785 static void
786 wp_impl_metadata_class_init (WpImplMetadataClass * klass)
787 {
788 GObjectClass *object_class = (GObjectClass *) klass;
789 WpObjectClass *wpobject_class = (WpObjectClass *) klass;
790 WpProxyClass *proxy_class = (WpProxyClass *) klass;
791
792 object_class->constructed = wp_impl_metadata_constructed;
793 object_class->finalize = wp_impl_metadata_finalize;
794 object_class->set_property = wp_impl_metadata_set_property;
795 object_class->get_property = wp_impl_metadata_get_property;
796
797 wpobject_class->activate_execute_step =
798 wp_impl_metadata_activate_execute_step;
799
800 /* disable adding a listener for events */
801 proxy_class->pw_proxy_created = NULL;
802
803 g_object_class_install_property (object_class, PROP_NAME,
804 g_param_spec_string ("name", "name", "The metadata name", "",
805 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
806
807 g_object_class_install_property (object_class, PROP_PROPERTIES,
808 g_param_spec_boxed ("properties", "properties",
809 "The metadata properties", WP_TYPE_PROPERTIES,
810 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
811 }
812
813 /*!
814 * \brief Creates a new metadata implementation
815 * \ingroup wpmetadata
816 * \param core the core
817 * \returns (transfer full): a new WpImplMetadata
818 */
819 WpImplMetadata *
820 wp_impl_metadata_new (WpCore * core)
821 {
822 return wp_impl_metadata_new_full (core, NULL, NULL);
823 }
824
825 /*!
826 * \brief Creates a new metadata implementation with name and properties
827 * \ingroup wpmetadata
828 * \param core the core
829 * \param name (nullable): the metadata name
830 * \param properties (nullable) (transfer full): the metadata properties
831 * \returns (transfer full): a new WpImplMetadata
832 * \since 0.4.3
833 */
834 WpImplMetadata *
835 wp_impl_metadata_new_full (WpCore * core, const gchar *name,
836 WpProperties *properties)
837 {
838 g_autoptr (WpProperties) props = properties;
839
840 g_return_val_if_fail (WP_IS_CORE (core), NULL);
841
842 return g_object_new (WP_TYPE_IMPL_METADATA,
843 "core", core,
844 "name", name,
845 "properties", props,
846 NULL);
847 }