Debian Codegraph @

lib/wp/private/pipewire-object-mixin.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "private/pipewire-object-mixin.h"
10 #include "core.h"
11 #include "spa-type.h"
12 #include "spa-pod.h"
13 #include "log.h"
14 #include "error.h"
15
16 #include <spa/utils/result.h>
17
18 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-pw-obj-mixin")
19
20 G_DEFINE_INTERFACE (WpPwObjectMixinPriv, wp_pw_object_mixin_priv, WP_TYPE_PROXY)
21
22 static void
23 wp_pw_object_mixin_priv_default_init (WpPwObjectMixinPrivInterface * iface)
24 {
25 }
26
27 static struct spa_param_info *
28 find_param_info (gpointer instance, guint32 id)
29 {
30 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
31 WpPwObjectMixinPrivInterface *iface =
32 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (instance);
33
34 /* offsets are 0 on objects that don't support params */
35 if (d->info && iface->n_params_offset && iface->param_info_offset) {
36 struct spa_param_info * param_info =
37 G_STRUCT_MEMBER (struct spa_param_info *, d->info, iface->param_info_offset);
38 guint32 n_params =
39 G_STRUCT_MEMBER (guint32, d->info, iface->n_params_offset);
40
41 for (guint i = 0; i < n_params; i++) {
42 if (param_info[i].id == id)
43 return &param_info[i];
44 }
45 }
46 return NULL;
47 }
48
49 /*************/
50 /* INTERFACE */
51
52 static gconstpointer
53 wp_pw_object_mixin_get_native_info (WpPipewireObject * obj)
54 {
55 return wp_pw_object_mixin_get_data (obj)->info;
56 }
57
58 static WpProperties *
59 wp_pw_object_mixin_get_properties (WpPipewireObject * obj)
60 {
61 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (obj);
62 return d->properties ? wp_properties_ref (d->properties) : NULL;
63 }
64
65 static GVariant *
66 wp_pw_object_mixin_get_param_info (WpPipewireObject * obj)
67 {
68 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (obj);
69 WpPwObjectMixinPrivInterface *iface = WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (obj);
70 struct spa_param_info *info;
71 guint32 n_params;
72 g_auto (GVariantBuilder) b =
73 G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_DICTIONARY);
74
75 if (!d->info ||
76 iface->param_info_offset == 0 ||
77 iface->n_params_offset == 0)
78 return NULL;
79
80 info = G_STRUCT_MEMBER (struct spa_param_info *, d->info, iface->param_info_offset);
81 n_params = G_STRUCT_MEMBER (guint32, d->info, iface->n_params_offset);
82
83 g_variant_builder_init (&b, G_VARIANT_TYPE ("a{ss}"));
84
85 for (guint i = 0; i < n_params; i++) {
86 WpSpaIdValue idval;
87 const gchar *nick = NULL;
88 gchar flags[3];
89 guint flags_idx = 0;
90
91 idval = wp_spa_id_value_from_number ("Spa:Enum:ParamId", info[i].id);
92 nick = wp_spa_id_value_short_name (idval);
93 g_return_val_if_fail (nick != NULL, NULL);
94
95 if (info[i].flags & SPA_PARAM_INFO_READ)
96 flags[flags_idx++] = 'r';
97 if (info[i].flags & SPA_PARAM_INFO_WRITE)
98 flags[flags_idx++] = 'w';
99 flags[flags_idx] = '\0';
100
101 g_variant_builder_add (&b, "{ss}", nick, flags);
102 }
103
104 return g_variant_builder_end (&b);
105 }
106
107 static void
108 enum_params_done (WpCore * core, GAsyncResult * res, gpointer data)
109 {
110 g_autoptr (GTask) task = G_TASK (data);
111 GList *taskl = NULL;
112 g_autoptr (GError) error = NULL;
113 gpointer instance = g_task_get_source_object (G_TASK (data));
114 GPtrArray *params = g_task_get_task_data (task);
115 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
116
117 /* finish the sync task */
118 wp_core_sync_finish (core, res, &error);
119
120 /* return if task was previously removed from the list */
121 taskl = g_list_find (d->enum_params_tasks, task);
122 if (!taskl)
123 return;
124
125 /* remove the task from the stored list; ref is held by the g_autoptr */
126 d->enum_params_tasks = g_list_delete_link (d->enum_params_tasks, taskl);
127
128 wp_debug_object (instance, "got %u params, %s, task " WP_OBJECT_FORMAT,
129 params->len, error ? "with error" : "ok", WP_OBJECT_ARGS (task));
130
131 if (error)
132 g_task_return_error (task, g_steal_pointer (&error));
133 else {
134 g_task_return_pointer (task, g_ptr_array_ref (params),
135 (GDestroyNotify) g_ptr_array_unref);
136 }
137 }
138
139 static void
140 enum_params_error (WpProxy * proxy, int seq, int res, const gchar *msg,
141 GTask * task)
142 {
143 gint t_seq = GPOINTER_TO_INT (g_task_get_source_tag (task));
144
145 if (SPA_RESULT_ASYNC_SEQ (t_seq) == SPA_RESULT_ASYNC_SEQ (seq)) {
146 gpointer instance = g_task_get_source_object (task);
147 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
148 GList *taskl = NULL;
149
150 taskl = g_list_find (d->enum_params_tasks, task);
151 if (taskl) {
152 d->enum_params_tasks = g_list_delete_link (d->enum_params_tasks, taskl);
153 g_task_return_new_error (task, WP_DOMAIN_LIBRARY,
154 WP_LIBRARY_ERROR_OPERATION_FAILED, "%s", msg);
155 }
156 }
157 }
158
159 static void
160 wp_pw_object_mixin_enum_params_unchecked (gpointer obj,
161 guint32 id, WpSpaPod *filter, GCancellable * cancellable,
162 GAsyncReadyCallback callback, gpointer user_data)
163 {
164 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (obj);
165 WpPwObjectMixinPrivInterface *iface = WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (obj);
166 g_autoptr (GTask) task = NULL;
167 gint seq = 0;
168 GPtrArray *params = NULL;
169
170 g_return_if_fail (iface->enum_params_sync || iface->enum_params);
171
172 if (iface->enum_params_sync) {
173 params = iface->enum_params_sync (obj, id, 0, -1, filter);
174 } else {
175 seq = iface->enum_params (obj, id, 0, -1, filter);
176
177 /* return early if seq contains an error */
178 if (G_UNLIKELY (SPA_RESULT_IS_ERROR (seq))) {
179 wp_notice_object (obj, "enum_params failed: %s", spa_strerror (seq));
180 g_task_report_new_error (obj, callback, user_data, NULL,
181 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
182 "enum_params failed: %s", spa_strerror (seq));
183 return;
184 }
185 }
186
187 if (!params)
188 params = g_ptr_array_new_with_free_func ((GDestroyNotify) wp_spa_pod_unref);
189
190 /* create task */
191 task = g_task_new (obj, cancellable, callback, user_data);
192
193 /* debug */
194 if (wp_local_log_topic_is_enabled (G_LOG_LEVEL_DEBUG)) {
195 const gchar *name = NULL;
196 name = wp_spa_id_value_short_name (
197 wp_spa_id_value_from_number ("Spa:Enum:ParamId", id));
198 wp_debug_object (obj, "enum id %u (%s), seq 0x%x (%u), task "
199 WP_OBJECT_FORMAT "%s", id, name, seq, seq, WP_OBJECT_ARGS (task),
200 iface->enum_params_sync ? ", sync" : "");
201 }
202
203 if (iface->enum_params_sync) {
204 g_task_return_pointer (task, params, (GDestroyNotify) g_ptr_array_unref);
205 } else {
206 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (obj));
207
208 /* watch for errors */
209 g_signal_connect_object (obj, "error", G_CALLBACK (enum_params_error),
210 task, 0);
211
212 /* store */
213 g_task_set_task_data (task, params, (GDestroyNotify) g_ptr_array_unref);
214 g_task_set_source_tag (task, GINT_TO_POINTER (seq));
215 d->enum_params_tasks = g_list_append (d->enum_params_tasks, task);
216
217 /* call sync */
218 wp_core_sync (core, cancellable, (GAsyncReadyCallback) enum_params_done,
219 g_object_ref (task));
220 }
221 }
222
223 static void
224 wp_pw_object_mixin_enum_params (WpPipewireObject * obj, const gchar * id,
225 WpSpaPod *filter, GCancellable * cancellable,
226 GAsyncReadyCallback callback, gpointer user_data)
227 {
228 WpPwObjectMixinPrivInterface *iface = WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (obj);
229 WpSpaIdValue param_id;
230
231 if (!(iface->enum_params || iface->enum_params_sync)) {
232 g_task_report_new_error (obj, callback, user_data, NULL,
233 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT,
234 "enum_params is not supported on this object");
235 return;
236 }
237
238 /* translate the id */
239 param_id = wp_spa_id_value_from_short_name ("Spa:Enum:ParamId", id);
240 if (!param_id) {
241 wp_critical_object (obj, "invalid param id: %s", id);
242 return;
243 }
244
245 wp_pw_object_mixin_enum_params_unchecked (obj,
246 wp_spa_id_value_number (param_id), filter,
247 cancellable, callback, user_data);
248 }
249
250 static WpIterator *
251 wp_pw_object_mixin_enum_params_finish (WpPipewireObject * obj,
252 GAsyncResult * res, GError ** error)
253 {
254 g_return_val_if_fail (g_task_is_valid (res, obj), NULL);
255
256 GPtrArray *array = g_task_propagate_pointer (G_TASK (res), error);
257 if (!array)
258 return NULL;
259
260 return wp_iterator_new_ptr_array (array, WP_TYPE_SPA_POD);
261 }
262
263 static WpIterator *
264 wp_pw_object_mixin_enum_params_sync (WpPipewireObject * obj, const gchar * id,
265 WpSpaPod * filter)
266 {
267 WpPwObjectMixinPrivInterface *iface = WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (obj);
268 GPtrArray *params = NULL;
269 WpSpaIdValue param_id;
270
271 /* translate the id */
272 param_id = wp_spa_id_value_from_short_name ("Spa:Enum:ParamId", id);
273 if (!param_id) {
274 wp_critical_object (obj, "invalid param id: %s", id);
275 return NULL;
276 }
277
278 if (iface->enum_params_sync) {
279 /* use enum_params_sync if supported */
280 params = iface->enum_params_sync (obj, wp_spa_id_value_number (param_id),
281 0, -1, filter);
282 } else {
283 /* otherwise, find and return the cached params */
284 WpPwObjectMixinData *data = wp_pw_object_mixin_get_data (obj);
285 params = wp_pw_object_mixin_get_stored_params (data,
286 wp_spa_id_value_number (param_id));
287 /* TODO filter */
288 }
289
290 return params ? wp_iterator_new_ptr_array (params, WP_TYPE_SPA_POD) : NULL;
291 }
292
293 static gboolean
294 wp_pw_object_mixin_set_param (WpPipewireObject * obj, const gchar * id,
295 guint32 flags, WpSpaPod * param)
296 {
297 WpPwObjectMixinPrivInterface *iface = WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (obj);
298 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (obj);
299 WpSpaIdValue param_id;
300 gint ret;
301
302 if (!d->iface) {
303 wp_notice_object (obj, "ignoring set_param on already destroyed objects");
304 return FALSE;
305 }
306
307 if (!iface->set_param) {
308 wp_warning_object (obj, "set_param is not supported on this object");
309 return FALSE;
310 }
311
312 param_id = wp_spa_id_value_from_short_name ("Spa:Enum:ParamId", id);
313 if (!param_id) {
314 wp_critical_object (obj, "invalid param id: %s", id);
315 wp_spa_pod_unref (param);
316 return FALSE;
317 }
318
319 ret = iface->set_param (obj, wp_spa_id_value_number (param_id), flags, param);
320
321 if (G_UNLIKELY (SPA_RESULT_IS_ERROR (ret))) {
322 wp_notice_object (obj, "set_param failed: %s", spa_strerror (ret));
323 return FALSE;
324 }
325 return TRUE;
326 }
327
328 void
329 wp_pw_object_mixin_object_interface_init (WpPipewireObjectInterface * iface)
330 {
331 iface->get_native_info = wp_pw_object_mixin_get_native_info;
332 iface->get_properties = wp_pw_object_mixin_get_properties;
333 iface->get_param_info = wp_pw_object_mixin_get_param_info;
334 iface->enum_params = wp_pw_object_mixin_enum_params;
335 iface->enum_params_finish = wp_pw_object_mixin_enum_params_finish;
336 iface->enum_params_sync = wp_pw_object_mixin_enum_params_sync;
337 iface->set_param = wp_pw_object_mixin_set_param;
338 }
339
340 /********/
341 /* DATA */
342
343 G_DEFINE_QUARK (WpPwObjectMixinData, wp_pw_object_mixin_data)
344
345 static void wp_pw_object_mixin_param_store_free (gpointer data);
346
347 static WpPwObjectMixinData *
348 wp_pw_object_mixin_data_new (void)
349 {
350 WpPwObjectMixinData *d = g_slice_new0 (WpPwObjectMixinData);
351 spa_hook_list_init (&d->hooks);
352 return d;
353 }
354
355 static void
356 wp_pw_object_mixin_data_free (gpointer data)
357 {
358 WpPwObjectMixinData *d = data;
359 spa_hook_list_clean (&d->hooks);
360 g_clear_pointer (&d->properties, wp_properties_unref);
361 g_list_free_full (d->params, wp_pw_object_mixin_param_store_free);
362 g_clear_pointer (&d->subscribed_ids, g_array_unref);
363 g_warn_if_fail (d->enum_params_tasks == NULL);
364 g_slice_free (WpPwObjectMixinData, d);
365 }
366
367 WpPwObjectMixinData *
368 wp_pw_object_mixin_get_data (gpointer instance)
369 {
370 WpPwObjectMixinData *d = g_object_get_qdata (G_OBJECT (instance),
371 wp_pw_object_mixin_data_quark ());
372 if (G_UNLIKELY (!d)) {
373 d = wp_pw_object_mixin_data_new ();
374 g_object_set_qdata_full (G_OBJECT (instance),
375 wp_pw_object_mixin_data_quark (), d, wp_pw_object_mixin_data_free);
376 }
377 return d;
378 }
379
380 /****************/
381 /* PARAMS STORE */
382
383 typedef struct _WpPwObjectMixinParamStore WpPwObjectMixinParamStore;
384 struct _WpPwObjectMixinParamStore
385 {
386 guint32 param_id;
387 GPtrArray *params;
388 };
389
390 static WpPwObjectMixinParamStore *
391 wp_pw_object_mixin_param_store_new (void)
392 {
393 WpPwObjectMixinParamStore *d = g_slice_new0 (WpPwObjectMixinParamStore);
394 return d;
395 }
396
397 static void
398 wp_pw_object_mixin_param_store_free (gpointer data)
399 {
400 WpPwObjectMixinParamStore * p = data;
401 g_clear_pointer (&p->params, g_ptr_array_unref);
402 g_slice_free (WpPwObjectMixinParamStore, p);
403 }
404
405 static gint
406 param_store_has_id (gconstpointer param, gconstpointer id)
407 {
408 guint32 param_id = ((const WpPwObjectMixinParamStore *) param)->param_id;
409 return (param_id == GPOINTER_TO_UINT (id)) ? 0 : 1;
410 }
411
412 GPtrArray *
413 wp_pw_object_mixin_get_stored_params (WpPwObjectMixinData * data, guint32 id)
414 {
415 GList *link = g_list_find_custom (data->params, GUINT_TO_POINTER (id),
416 param_store_has_id);
417 WpPwObjectMixinParamStore *s = link ? link->data : NULL;
418 return (s && s->params) ? g_ptr_array_ref (s->params) : NULL;
419 }
420
421 void
422 wp_pw_object_mixin_store_param (WpPwObjectMixinData * data, guint32 id,
423 guint32 flags, gpointer param)
424 {
425 GList *link = g_list_find_custom (data->params, GUINT_TO_POINTER (id),
426 param_store_has_id);
427 WpPwObjectMixinParamStore *s = link ? link->data : NULL;
428 gint16 index = (gint16) (flags & 0xffff);
429
430 /* if the link exists, data must also exist */
431 g_warn_if_fail (!link || link->data);
432
433 if (!s) {
434 if (flags & WP_PW_OBJECT_MIXIN_STORE_PARAM_REMOVE)
435 return;
436 s = wp_pw_object_mixin_param_store_new ();
437 s->param_id = id;
438 data->params = g_list_append (data->params, s);
439 }
440 else if (s && (flags & WP_PW_OBJECT_MIXIN_STORE_PARAM_REMOVE)) {
441 wp_pw_object_mixin_param_store_free (s);
442 data->params = g_list_delete_link (data->params, link);
443 return;
444 }
445
446 if (flags & WP_PW_OBJECT_MIXIN_STORE_PARAM_CLEAR)
447 g_clear_pointer (&s->params, g_ptr_array_unref);
448
449 if (!param)
450 return;
451
452 if (flags & WP_PW_OBJECT_MIXIN_STORE_PARAM_ARRAY) {
453 if (!s->params)
454 s->params = (GPtrArray *) param;
455 else
456 g_ptr_array_extend_and_steal (s->params, (GPtrArray *) param);
457 }
458 else {
459 WpSpaPod *param_pod = param;
460
461 if (!s->params)
462 s->params =
463 g_ptr_array_new_with_free_func ((GDestroyNotify) wp_spa_pod_unref);
464
465 /* copy if necessary to make sure we don't reference
466 `const struct spa_pod *` data allocated on the stack */
467 param_pod = wp_spa_pod_ensure_unique_owner (param_pod);
468 g_ptr_array_insert (s->params, index, param_pod);
469 }
470 }
471
472 /******************/
473 /* PROPERTIES API */
474
475 void
476 wp_pw_object_mixin_get_property (GObject * object, guint property_id,
477 GValue * value, GParamSpec * pspec)
478 {
479 switch (property_id) {
480 case WP_PW_OBJECT_MIXIN_PROP_NATIVE_INFO:
481 g_value_set_pointer (value, (gpointer)
482 wp_pipewire_object_get_native_info (WP_PIPEWIRE_OBJECT (object)));
483 break;
484 case WP_PW_OBJECT_MIXIN_PROP_PROPERTIES:
485 g_value_take_boxed (value,
486 wp_pipewire_object_get_properties (WP_PIPEWIRE_OBJECT (object)));
487 break;
488 case WP_PW_OBJECT_MIXIN_PROP_PARAM_INFO:
489 g_value_set_variant (value,
490 wp_pipewire_object_get_param_info (WP_PIPEWIRE_OBJECT (object)));
491 break;
492 default:
493 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
494 break;
495 }
496 }
497
498 void
499 wp_pw_object_mixin_class_override_properties (GObjectClass * klass)
500 {
501 g_object_class_override_property (klass,
502 WP_PW_OBJECT_MIXIN_PROP_NATIVE_INFO, "native-info");
503 g_object_class_override_property (klass,
504 WP_PW_OBJECT_MIXIN_PROP_PROPERTIES, "properties");
505 g_object_class_override_property (klass,
506 WP_PW_OBJECT_MIXIN_PROP_PARAM_INFO, "param-info");
507 }
508
509 /****************/
510 /* FEATURES API */
511
512 static const struct {
513 WpObjectFeatures feature;
514 guint32 param_ids[2];
515 } params_features[] = {
516 { WP_PIPEWIRE_OBJECT_FEATURE_PARAM_PROPS, { SPA_PARAM_PropInfo, SPA_PARAM_Props } },
517 { WP_PIPEWIRE_OBJECT_FEATURE_PARAM_FORMAT, { SPA_PARAM_EnumFormat, SPA_PARAM_Format } },
518 { WP_PIPEWIRE_OBJECT_FEATURE_PARAM_PROFILE, { SPA_PARAM_EnumProfile, SPA_PARAM_Profile } },
519 { WP_PIPEWIRE_OBJECT_FEATURE_PARAM_PORT_CONFIG, { SPA_PARAM_EnumPortConfig, SPA_PARAM_PortConfig } },
520 { WP_PIPEWIRE_OBJECT_FEATURE_PARAM_ROUTE, { SPA_PARAM_EnumRoute, SPA_PARAM_Route } },
521 };
522
523 static WpObjectFeatures
524 get_feature_for_param_id (guint32 param_id)
525 {
526 for (guint i = 0; i < G_N_ELEMENTS (params_features); i++) {
527 if (params_features[i].param_ids[0] == param_id ||
528 params_features[i].param_ids[1] == param_id)
529 return params_features[i].feature;
530 }
531 return 0;
532 }
533
534 WpObjectFeatures
535 wp_pw_object_mixin_get_supported_features (WpObject * object)
536 {
537 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (object);
538 WpPwObjectMixinPrivInterface *iface =
539 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (object);
540 WpObjectFeatures ft =
541 WP_PROXY_FEATURE_BOUND | WP_PIPEWIRE_OBJECT_FEATURE_INFO;
542
543 if (d->info && iface->n_params_offset && iface->param_info_offset) {
544 struct spa_param_info * param_info =
545 G_STRUCT_MEMBER (struct spa_param_info *, d->info, iface->param_info_offset);
546 guint32 n_params =
547 G_STRUCT_MEMBER (guint32, d->info, iface->n_params_offset);
548
549 for (guint i = 0; i < n_params; i++)
550 ft |= get_feature_for_param_id (param_info[i].id);
551 }
552 return ft;
553 }
554
555 guint
556 wp_pw_object_mixin_activate_get_next_step (WpObject * object,
557 WpFeatureActivationTransition * transition, guint step,
558 WpObjectFeatures missing)
559 {
560 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (object);
561
562 /* bind if not already bound */
563 if (missing & WP_PROXY_FEATURE_BOUND || !d->iface)
564 return WP_PW_OBJECT_MIXIN_STEP_BIND;
565 /* wait for info before proceeding, if necessary */
566 else if ((missing & WP_PIPEWIRE_OBJECT_FEATURES_ALL) && !d->info)
567 return WP_PW_OBJECT_MIXIN_STEP_WAIT_INFO;
568 /* then cache params */
569 else if (missing & WP_PIPEWIRE_OBJECT_FEATURES_ALL)
570 return WP_PW_OBJECT_MIXIN_STEP_CACHE_PARAMS;
571 else
572 return WP_PW_OBJECT_MIXIN_STEP_CUSTOM_START;
573
574 /* returning to STEP_NONE is handled by WpFeatureActivationTransition */
575 }
576
577 static void
578 enum_params_for_cache_done (GObject * object, GAsyncResult * res, gpointer data)
579 {
580 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (object);
581 guint32 param_id = GPOINTER_TO_UINT (data);
582 g_autoptr (GError) error = NULL;
583 g_autoptr (GPtrArray) params = NULL;
584 const gchar *name = NULL;
585
586 params = g_task_propagate_pointer (G_TASK (res), &error);
587 if (error) {
588 wp_debug_object (object, "enum params failed: %s", error->message);
589 return;
590 }
591
592 name = wp_spa_id_value_short_name (wp_spa_id_value_from_number (
593 "Spa:Enum:ParamId", param_id));
594
595 wp_debug_object (object, "cached params id:%u (%s), n_params:%u", param_id,
596 name, params->len);
597
598 wp_pw_object_mixin_store_param (d, param_id,
599 WP_PW_OBJECT_MIXIN_STORE_PARAM_ARRAY |
600 WP_PW_OBJECT_MIXIN_STORE_PARAM_CLEAR |
601 WP_PW_OBJECT_MIXIN_STORE_PARAM_APPEND,
602 g_steal_pointer (&params));
603
604 g_signal_emit_by_name (object, "params-changed", name);
605 }
606
607 G_DEFINE_QUARK (WpPwObjectMixinParamCacheActivatedFeatures, activated_features)
608
609 static void
610 param_cache_features_enabled (WpCore * core, GAsyncResult * res, gpointer data)
611 {
612 WpObject *object = WP_OBJECT (data);
613 WpObjectFeatures activated = GPOINTER_TO_UINT (
614 g_object_get_qdata (G_OBJECT (object), activated_features_quark ()));
615 wp_object_update_features (object, activated, 0);
616 }
617
618 void
619 wp_pw_object_mixin_cache_params (WpObject * object, WpObjectFeatures missing)
620 {
621 WpPwObjectMixinPrivInterface *iface =
622 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (object);
623 g_autoptr (WpCore) core = wp_object_get_core (object);
624 struct spa_param_info * param_info;
625 WpObjectFeatures activated = 0;
626
627 g_return_if_fail (!(iface->flags & WP_PW_OBJECT_MIXIN_PRIV_NO_PARAM_CACHE));
628
629 for (guint i = 0; i < G_N_ELEMENTS (params_features); i++) {
630 if (missing & params_features[i].feature) {
631 param_info = find_param_info (object, params_features[i].param_ids[0]);
632 if (param_info && param_info->flags & SPA_PARAM_INFO_READ) {
633 wp_pw_object_mixin_enum_params_unchecked (object,
634 param_info->id, NULL, NULL, enum_params_for_cache_done,
635 GUINT_TO_POINTER (param_info->id));
636 }
637
638 param_info = find_param_info (object, params_features[i].param_ids[1]);
639 if (param_info && param_info->flags & SPA_PARAM_INFO_READ) {
640 wp_pw_object_mixin_enum_params_unchecked (object,
641 param_info->id, NULL, NULL, enum_params_for_cache_done,
642 GUINT_TO_POINTER (param_info->id));
643 }
644
645 activated |= params_features[i].feature;
646 }
647 }
648
649 g_object_set_qdata (G_OBJECT (object),
650 activated_features_quark (), GUINT_TO_POINTER (activated));
651 wp_core_sync_closure (core, NULL, g_cclosure_new_object (
652 G_CALLBACK (param_cache_features_enabled),
653 G_OBJECT (object)));
654 }
655
656 void
657 wp_pw_object_mixin_deactivate (WpObject * object, WpObjectFeatures features)
658 {
659 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (object);
660 WpPwObjectMixinPrivInterface *iface =
661 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (object);
662
663 /* deactivate param caching */
664 if (!(iface->flags & WP_PW_OBJECT_MIXIN_PRIV_NO_PARAM_CACHE)) {
665 for (guint i = 0; i < G_N_ELEMENTS (params_features); i++) {
666 if (features & params_features[i].feature) {
667 wp_pw_object_mixin_store_param (d, params_features[i].param_ids[0],
668 WP_PW_OBJECT_MIXIN_STORE_PARAM_REMOVE, NULL);
669 wp_pw_object_mixin_store_param (d, params_features[i].param_ids[1],
670 WP_PW_OBJECT_MIXIN_STORE_PARAM_REMOVE, NULL);
671 wp_object_update_features (object, 0, params_features[i].feature);
672 }
673 }
674 }
675 }
676
677 /************************/
678 /* PROXY EVENT HANDLERS */
679
680 void
681 wp_pw_object_mixin_handle_pw_proxy_destroyed (WpProxy * proxy)
682 {
683 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (proxy);
684 WpPwObjectMixinPrivInterface *iface =
685 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (proxy);
686
687 spa_hook_remove (&d->listener);
688 g_clear_pointer (&d->properties, wp_properties_unref);
689 g_clear_pointer (&d->info, iface->free_info);
690 d->iface = NULL;
691
692 /* deactivate param caching */
693 if (!(iface->flags & WP_PW_OBJECT_MIXIN_PRIV_NO_PARAM_CACHE)) {
694 for (guint i = 0; i < G_N_ELEMENTS (params_features); i++) {
695 wp_pw_object_mixin_store_param (d, params_features[i].param_ids[0],
696 WP_PW_OBJECT_MIXIN_STORE_PARAM_REMOVE, NULL);
697 wp_pw_object_mixin_store_param (d, params_features[i].param_ids[1],
698 WP_PW_OBJECT_MIXIN_STORE_PARAM_REMOVE, NULL);
699 }
700 }
701
702 /* cancel enum_params tasks */
703 {
704 GList *link;
705 for (link = g_list_first (d->enum_params_tasks);
706 link; link = g_list_first (d->enum_params_tasks)) {
707 GTask *task = G_TASK (link->data);
708 d->enum_params_tasks = g_list_delete_link (d->enum_params_tasks, link);
709 g_task_return_new_error (task,
710 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
711 "pipewire proxy destroyed before finishing");
712 }
713 }
714
715 wp_object_update_features (WP_OBJECT (proxy), 0,
716 WP_PIPEWIRE_OBJECT_FEATURES_ALL);
717 }
718
719 /***************************/
720 /* PIPEWIRE EVENT HANDLERS */
721
722 void
723 wp_pw_object_mixin_handle_event_info (gpointer instance, gconstpointer update)
724 {
725 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
726 WpPwObjectMixinPrivInterface *iface =
727 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (instance);
728 guint64 change_mask =
729 G_STRUCT_MEMBER (guint64, update, iface->change_mask_offset);
730 guint64 process_info_change_mask =
731 change_mask & ~(iface->CHANGE_MASK_PROPS | iface->CHANGE_MASK_PARAMS);
732 gpointer old_info = NULL;
733
734 wp_debug_object (instance, "info, change_mask:0x%"G_GINT64_MODIFIER"x [%s%s]",
735 change_mask,
736 (change_mask & iface->CHANGE_MASK_PROPS) ? "props," : "",
737 (change_mask & iface->CHANGE_MASK_PARAMS) ? "params," : "");
738
739 /* make a copy of d->info for process_info() */
740 if (iface->process_info && d->info && process_info_change_mask) {
741 /* copy everything that changed except props and params, for efficiency;
742 process_info() is only interested in variables that are not PROPS & PARAMS */
743 G_STRUCT_MEMBER (guint64, d->info, iface->change_mask_offset) =
744 process_info_change_mask;
745 old_info = iface->update_info (NULL, d->info);
746 }
747
748 /* update params */
749 if (!(iface->flags & WP_PW_OBJECT_MIXIN_PRIV_NO_PARAM_CACHE) &&
750 (change_mask & iface->CHANGE_MASK_PARAMS) && d->info) {
751 struct spa_param_info * old_param_info =
752 G_STRUCT_MEMBER (struct spa_param_info *, d->info, iface->param_info_offset);
753 struct spa_param_info * param_info =
754 G_STRUCT_MEMBER (struct spa_param_info *, update, iface->param_info_offset);
755 guint32 old_n_params =
756 G_STRUCT_MEMBER (guint32, d->info, iface->n_params_offset);
757 guint32 n_params =
758 G_STRUCT_MEMBER (guint32, update, iface->n_params_offset);
759 WpObjectFeatures active_ft =
760 wp_object_get_active_features (WP_OBJECT (instance));
761
762 for (guint i = 0; i < n_params; i++) {
763 /* param changes when flags change */
764 if (i >= old_n_params || old_param_info[i].flags != param_info[i].flags) {
765 /* update cached params if the relevant feature is active */
766 if (active_ft & get_feature_for_param_id (param_info[i].id) &&
767 param_info[i].flags & SPA_PARAM_INFO_READ)
768 {
769 wp_pw_object_mixin_enum_params_unchecked (instance,
770 param_info[i].id, NULL, NULL, enum_params_for_cache_done,
771 GUINT_TO_POINTER (param_info[i].id));
772 }
773 }
774 }
775 }
776
777 /* update our info struct */
778 d->info = iface->update_info (d->info, update);
779
780 /* update properties */
781 if (change_mask & iface->CHANGE_MASK_PROPS) {
782 const struct spa_dict * props =
783 G_STRUCT_MEMBER (const struct spa_dict *, d->info, iface->props_offset);
784
785 g_clear_pointer (&d->properties, wp_properties_unref);
786 d->properties = wp_properties_new_copy_dict (props);
787
788 g_object_notify (G_OBJECT (instance), "properties");
789 }
790
791 if (change_mask & iface->CHANGE_MASK_PARAMS)
792 g_object_notify (G_OBJECT (instance), "param-info");
793
794 /* custom handling, if required */
795 if (iface->process_info && process_info_change_mask) {
796 iface->process_info (instance, old_info, d->info);
797 g_clear_pointer (&old_info, iface->free_info);
798 }
799
800 wp_object_update_features (WP_OBJECT (instance),
801 WP_PIPEWIRE_OBJECT_FEATURE_INFO, 0);
802 }
803
804 static gint
805 task_has_seq (gconstpointer task, gconstpointer seq)
806 {
807 gpointer t_seq = g_task_get_source_tag (G_TASK (task));
808 return (GPOINTER_TO_INT (t_seq) == GPOINTER_TO_INT (seq)) ? 0 : 1;
809 }
810
811 void
812 wp_pw_object_mixin_handle_event_param (gpointer instance, int seq,
813 uint32_t id, uint32_t index, uint32_t next, const struct spa_pod *param)
814 {
815 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
816 g_autoptr (WpSpaPod) w_param = wp_spa_pod_new_wrap_const (param);
817 GList *list;
818 GTask *task;
819
820 list = g_list_find_custom (d->enum_params_tasks, GINT_TO_POINTER (seq),
821 task_has_seq);
822 task = list ? G_TASK (list->data) : NULL;
823
824 wp_trace_boxed (WP_TYPE_SPA_POD, w_param,
825 WP_OBJECT_FORMAT " param id:%u, index:%u",
826 WP_OBJECT_ARGS (instance), id, index);
827
828 if (task) {
829 GPtrArray *array = g_task_get_task_data (task);
830 g_ptr_array_add (array, wp_spa_pod_copy (w_param));
831 } else {
832 /* this should never happen */
833 wp_warning_object (instance,
834 "param event was received without calling enum_params");
835 }
836 }
837
838 /***********************************/
839 /* PIPEWIRE METHOD IMPLEMENTATIONS */
840
841 int
842 wp_pw_object_mixin_impl_add_listener (gpointer instance,
843 struct spa_hook *listener, gconstpointer events, gpointer data)
844 {
845 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
846 WpPwObjectMixinPrivInterface *iface =
847 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (instance);
848 struct spa_hook_list save;
849
850 spa_hook_list_isolate (&d->hooks, &save, listener, events, data);
851
852 G_STRUCT_MEMBER (guint64, d->info, iface->change_mask_offset) = iface->CHANGE_MASK_ALL;
853 iface->emit_info (&d->hooks, d->info);
854 G_STRUCT_MEMBER (guint64, d->info, iface->change_mask_offset) = 0;
855
856 spa_hook_list_join (&d->hooks, &save);
857 return 0;
858 }
859
860 int
861 wp_pw_object_mixin_impl_enum_params (gpointer instance, int seq,
862 guint32 id, guint32 start, guint32 num, const struct spa_pod *filter)
863 {
864 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
865 WpPwObjectMixinPrivInterface *iface =
866 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (instance);
867 g_autoptr (GPtrArray) params = NULL;
868 g_autoptr (WpSpaPod) filter_pod = NULL;
869
870 if (!iface->enum_params_sync)
871 return -ENOTSUP;
872
873 struct spa_param_info * info = find_param_info (instance, id);
874 if (!info || !(info->flags & SPA_PARAM_INFO_READ))
875 return -EINVAL;
876
877 filter_pod = filter ? wp_spa_pod_new_wrap_const (filter) : NULL;
878 params = iface->enum_params_sync (instance, id, start, num, filter_pod);
879
880 if (params) {
881 for (guint i = 0; i < params->len; i++) {
882 WpSpaPod *pod = g_ptr_array_index (params, i);
883
884 wp_trace_boxed (WP_TYPE_SPA_POD, pod,
885 WP_OBJECT_FORMAT " emit param id:%u, index:%u",
886 WP_OBJECT_ARGS (instance), id, start+i);
887
888 iface->emit_param (&d->hooks, seq, id, start+i, start+i+1,
889 wp_spa_pod_get_spa_pod (pod));
890 }
891 }
892 return 0;
893 }
894
895 int
896 wp_pw_object_mixin_impl_subscribe_params (gpointer instance,
897 guint32 *ids, guint32 n_ids)
898 {
899 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
900 WpPwObjectMixinPrivInterface *iface =
901 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (instance);
902
903 if (!iface->enum_params_sync)
904 return -ENOTSUP;
905
906 for (guint i = 0; i < n_ids; i++)
907 wp_pw_object_mixin_impl_enum_params (instance, 1, ids[i], 0, -1, NULL);
908
909 if (!d->subscribed_ids)
910 d->subscribed_ids = g_array_new (FALSE, FALSE, sizeof (guint32));
911
912 /* FIXME: deduplicate stored ids */
913 g_array_append_vals (d->subscribed_ids, ids, n_ids);
914 return 0;
915 }
916
917 int
918 wp_pw_object_mixin_impl_set_param (gpointer instance, guint32 id,
919 guint32 flags, const struct spa_pod *param)
920 {
921 WpPwObjectMixinPrivInterface *iface =
922 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (instance);
923
924 if (!iface->set_param)
925 return -ENOTSUP;
926
927 struct spa_param_info * info = find_param_info (instance, id);
928 if (!info || !(info->flags & SPA_PARAM_INFO_WRITE))
929 return -EINVAL;
930
931 WpSpaPod *param_pod = wp_spa_pod_new_wrap_const (param);
932
933 wp_trace_boxed (WP_TYPE_SPA_POD, param_pod,
934 WP_OBJECT_FORMAT " set_param id:%u flags:0x%x",
935 WP_OBJECT_ARGS (instance), id, flags);
936
937 return iface->set_param (instance, id, flags, param_pod);
938 }
939
940 /**********************/
941 /* NOTIFIERS */
942
943 void
944 wp_pw_object_mixin_notify_info (gpointer instance, guint32 change_mask)
945 {
946 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
947 WpPwObjectMixinPrivInterface *iface =
948 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (instance);
949
950 wp_debug_object (instance, "notify info, change_mask:0x%x [%s%s]",
951 change_mask,
952 (change_mask & iface->CHANGE_MASK_PROPS) ? "props," : "",
953 (change_mask & iface->CHANGE_MASK_PARAMS) ? "params," : "");
954
955 G_STRUCT_MEMBER (guint64, d->info, iface->change_mask_offset) =
956 (change_mask & iface->CHANGE_MASK_ALL);
957 iface->emit_info (&d->hooks, d->info);
958 G_STRUCT_MEMBER (guint64, d->info, iface->change_mask_offset) = 0;
959
960 if (change_mask & iface->CHANGE_MASK_PROPS)
961 g_object_notify (G_OBJECT (instance), "properties");
962
963 if (change_mask & iface->CHANGE_MASK_PARAMS)
964 g_object_notify (G_OBJECT (instance), "param-info");
965 }
966
967 void
968 wp_pw_object_mixin_notify_params_changed (gpointer instance, guint32 id)
969 {
970 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
971 WpPwObjectMixinPrivInterface *iface =
972 WP_PW_OBJECT_MIXIN_PRIV_GET_IFACE (instance);
973 gboolean subscribed = FALSE;
974 const gchar *name = NULL;
975
976 struct spa_param_info * info = find_param_info (instance, id);
977 g_return_if_fail (info);
978
979 if (d->subscribed_ids) {
980 for (guint i = 0; i < d->subscribed_ids->len; i++) {
981 if (g_array_index (d->subscribed_ids, guint32, i) == id) {
982 subscribed = TRUE;
983 break;
984 }
985 }
986 }
987
988 name = wp_spa_id_value_short_name (wp_spa_id_value_from_number (
989 "Spa:Enum:ParamId", id));
990
991 wp_debug_object (instance, "notify param id:%u (%s)", id, name);
992
993 /* toggle the serial flag; this notifies that there is a data change */
994 info->flags ^= SPA_PARAM_INFO_SERIAL;
995
996 G_STRUCT_MEMBER (guint64, d->info, iface->change_mask_offset) =
997 iface->CHANGE_MASK_PARAMS;
998 iface->emit_info (&d->hooks, d->info);
999 G_STRUCT_MEMBER (guint64, d->info, iface->change_mask_offset) = 0;
1000
1001 if (subscribed)
1002 wp_pw_object_mixin_impl_enum_params (instance, 1, id, 0, -1, NULL);
1003
1004 g_signal_emit_by_name (instance, "params-changed", name);
1005 }