Debian Codegraph @

lib/wp/node.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2019-2020 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "node.h"
10 #include "core.h"
11 #include "object-manager.h"
12 #include "log.h"
13 #include "wpenums.h"
14 #include "private/pipewire-object-mixin.h"
15
16 #include <pipewire/impl.h>
17
18 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-node")
19
20 /*! \defgroup wpnode WpNode */
21 /*!
22 * \struct WpNode
23 *
24 * The WpNode class allows accessing the properties and methods of a
25 * PipeWire node object (`struct pw_node`).
26 *
27 * A WpNode is constructed internally when a new node appears on the
28 * PipeWire registry and it is made available through the WpObjectManager API.
29 * Alternatively, a WpNode can also be constructed using
30 * wp_node_new_from_factory(), which creates a new node object
31 * on the remote PipeWire server by calling into a factory.
32 *
33 * \gproperties
34 *
35 * \gproperty{state, WpNodeState, G_PARAM_READABLE, The current state of the node}
36 *
37 * \gproperty{n-input-ports, uint, G_PARAM_READABLE, The number of input ports}
38 *
39 * \gproperty{n-output-ports, uint, G_PARAM_READABLE, The number of output ports}
40 *
41 * \gproperty{max-input-ports, uint, G_PARAM_READABLE, The max number of input ports}
42 *
43 * \gproperty{max-output-ports, uint, G_PARAM_READABLE, The max number of output ports}
44 *
45 * \gsignals
46 *
47 * \par ports-changed
48 * \parblock
49 * \code
50 * void
51 * ports_changed_callback (WpNode * self,
52 * gpointer user_data)
53 * \endcode
54 *
55 * Emitted when the node's ports change. This is only emitted when
56 * WP_NODE_FEATURE_PORTS is enabled.
57 *
58 * Flags: G_SIGNAL_RUN_LAST
59 * \endparblock
60 *
61 * \par state-changed
62 * \parblock
63 * \code
64 * void
65 * state_changed_callback (WpNode * self,
66 * WpNodeState old_state,
67 * WpNodeState new_state,
68 * gpointer user_data)
69 * \endcode
70 *
71 * Emitted when the node changes state. This is only emitted when
72 * WP_PIPEWIRE_OBJECT_FEATURE_INFO is enabled.
73 *
74 * Parameters:
75 * - `old_state` - the old state
76 * - `new_state` - the new state
77 *
78 * Flags: G_SIGNAL_RUN_LAST
79 * \endparblock
80 */
81
82
83 enum {
84 PROP_STATE = WP_PW_OBJECT_MIXIN_PROP_CUSTOM_START,
85 PROP_N_INPUT_PORTS,
86 PROP_N_OUTPUT_PORTS,
87 PROP_MAX_INPUT_PORTS,
88 PROP_MAX_OUTPUT_PORTS,
89 };
90
91 enum {
92 SIGNAL_STATE_CHANGED,
93 SIGNAL_PORTS_CHANGED,
94 N_SIGNALS,
95 };
96
97 static guint32 signals[N_SIGNALS] = {0};
98
99 struct _WpNode
100 {
101 WpGlobalProxy parent;
102 WpObjectManager *ports_om;
103 };
104
105 static void wp_node_pw_object_mixin_priv_interface_init (
106 WpPwObjectMixinPrivInterface * iface);
107
108 G_DEFINE_TYPE_WITH_CODE (WpNode, wp_node, WP_TYPE_GLOBAL_PROXY,
109 G_IMPLEMENT_INTERFACE (WP_TYPE_PIPEWIRE_OBJECT,
110 wp_pw_object_mixin_object_interface_init)
111 G_IMPLEMENT_INTERFACE (WP_TYPE_PW_OBJECT_MIXIN_PRIV,
112 wp_node_pw_object_mixin_priv_interface_init))
113
114 static void
115 wp_node_init (WpNode * self)
116 {
117 }
118
119 static void
120 wp_node_get_property (GObject * object, guint property_id,
121 GValue * value, GParamSpec * pspec)
122 {
123 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (object);
124
125 switch (property_id) {
126 case PROP_STATE:
127 g_value_set_enum (value, d->info ?
128 ((struct pw_node_info *) d->info)->state : 0);
129 break;
130 case PROP_N_INPUT_PORTS:
131 g_value_set_uint (value, d->info ?
132 ((struct pw_node_info *) d->info)->n_input_ports : 0);
133 break;
134 case PROP_N_OUTPUT_PORTS:
135 g_value_set_uint (value, d->info ?
136 ((struct pw_node_info *) d->info)->n_output_ports : 0);
137 break;
138 case PROP_MAX_INPUT_PORTS:
139 g_value_set_uint (value, d->info ?
140 ((struct pw_node_info *) d->info)->max_input_ports : 0);
141 break;
142 case PROP_MAX_OUTPUT_PORTS:
143 g_value_set_uint (value, d->info ?
144 ((struct pw_node_info *) d->info)->max_output_ports : 0);
145 break;
146 default:
147 wp_pw_object_mixin_get_property (object, property_id, value, pspec);
148 break;
149 }
150 }
151
152 static void
153 wp_node_on_ports_om_installed (WpObjectManager *ports_om, WpNode * self)
154 {
155 wp_object_update_features (WP_OBJECT (self), WP_NODE_FEATURE_PORTS, 0);
156 }
157
158 static void
159 wp_node_emit_ports_changed (WpObjectManager *ports_om, WpNode * self)
160 {
161 g_signal_emit (self, signals[SIGNAL_PORTS_CHANGED], 0);
162 }
163
164 static void
165 wp_node_enable_feature_ports (WpNode * self)
166 {
167 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
168 guint32 bound_id = wp_proxy_get_bound_id (WP_PROXY (self));
169
170 wp_debug_object (self, "enabling WP_NODE_FEATURE_PORTS, bound_id:%u",
171 bound_id);
172
173 self->ports_om = wp_object_manager_new ();
174 wp_object_manager_add_interest (self->ports_om,
175 WP_TYPE_PORT,
176 WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, PW_KEY_NODE_ID, "=u", bound_id,
177 NULL);
178 wp_object_manager_request_object_features (self->ports_om,
179 WP_TYPE_PORT, WP_OBJECT_FEATURES_ALL);
180
181 g_signal_connect_object (self->ports_om, "installed",
182 G_CALLBACK (wp_node_on_ports_om_installed), self, 0);
183 g_signal_connect_object (self->ports_om, "objects-changed",
184 G_CALLBACK (wp_node_emit_ports_changed), self, 0);
185
186 wp_core_install_object_manager (core, self->ports_om);
187 }
188
189 static WpObjectFeatures
190 wp_node_get_supported_features (WpObject * object)
191 {
192 return wp_pw_object_mixin_get_supported_features (object)
193 | WP_NODE_FEATURE_PORTS;
194 }
195
196 enum {
197 STEP_PORTS = WP_PW_OBJECT_MIXIN_STEP_CUSTOM_START,
198 };
199
200 static void
201 wp_node_activate_execute_step (WpObject * object,
202 WpFeatureActivationTransition * transition, guint step,
203 WpObjectFeatures missing)
204 {
205 switch (step) {
206 case WP_PW_OBJECT_MIXIN_STEP_BIND:
207 case WP_TRANSITION_STEP_ERROR:
208 /* base class can handle BIND and ERROR */
209 WP_OBJECT_CLASS (wp_node_parent_class)->
210 activate_execute_step (object, transition, step, missing);
211 break;
212 case WP_PW_OBJECT_MIXIN_STEP_WAIT_INFO:
213 /* just wait, info will be emitted anyway after binding */
214 break;
215 case WP_PW_OBJECT_MIXIN_STEP_CACHE_PARAMS:
216 wp_pw_object_mixin_cache_params (object, missing);
217 break;
218 case STEP_PORTS:
219 wp_node_enable_feature_ports (WP_NODE (object));
220 break;
221 default:
222 g_assert_not_reached ();
223 }
224 }
225
226 static void
227 wp_node_deactivate (WpObject * object, WpObjectFeatures features)
228 {
229 wp_pw_object_mixin_deactivate (object, features);
230
231 if (features & WP_NODE_FEATURE_PORTS) {
232 WpNode *self = WP_NODE (object);
233 g_clear_object (&self->ports_om);
234 wp_object_update_features (object, 0, WP_NODE_FEATURE_PORTS);
235 }
236
237 WP_OBJECT_CLASS (wp_node_parent_class)->deactivate (object, features);
238 }
239
240 static const struct pw_node_events node_events = {
241 PW_VERSION_NODE_EVENTS,
242 .info = (HandleEventInfoFunc(node)) wp_pw_object_mixin_handle_event_info,
243 .param = wp_pw_object_mixin_handle_event_param,
244 };
245
246 static void
247 wp_node_pw_proxy_created (WpProxy * proxy, struct pw_proxy * pw_proxy)
248 {
249 wp_pw_object_mixin_handle_pw_proxy_created (proxy, pw_proxy,
250 node, &node_events);
251 }
252
253 static void
254 wp_node_pw_proxy_destroyed (WpProxy * proxy)
255 {
256 WpNode *self = WP_NODE (proxy);
257
258 wp_pw_object_mixin_handle_pw_proxy_destroyed (proxy);
259
260 g_clear_object (&self->ports_om);
261 wp_object_update_features (WP_OBJECT (self), 0, WP_NODE_FEATURE_PORTS);
262
263 WP_PROXY_CLASS (wp_node_parent_class)->pw_proxy_destroyed (proxy);
264 }
265
266 static void
267 wp_node_class_init (WpNodeClass * klass)
268 {
269 GObjectClass *object_class = (GObjectClass *) klass;
270 WpObjectClass *wpobject_class = (WpObjectClass *) klass;
271 WpProxyClass *proxy_class = (WpProxyClass *) klass;
272
273 object_class->get_property = wp_node_get_property;
274
275 wpobject_class->get_supported_features = wp_node_get_supported_features;
276 wpobject_class->activate_get_next_step =
277 wp_pw_object_mixin_activate_get_next_step;
278 wpobject_class->activate_execute_step = wp_node_activate_execute_step;
279 wpobject_class->deactivate = wp_node_deactivate;
280
281 proxy_class->pw_iface_type = PW_TYPE_INTERFACE_Node;
282 proxy_class->pw_iface_version = PW_VERSION_NODE;
283 proxy_class->pw_proxy_created = wp_node_pw_proxy_created;
284 proxy_class->pw_proxy_destroyed = wp_node_pw_proxy_destroyed;
285
286 wp_pw_object_mixin_class_override_properties (object_class);
287
288 g_object_class_install_property (object_class, PROP_STATE,
289 g_param_spec_enum ("state", "state", "state", WP_TYPE_NODE_STATE, 0,
290 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
291
292 g_object_class_install_property (object_class, PROP_N_INPUT_PORTS,
293 g_param_spec_uint ("n-input-ports", "n-input-ports", "n-input-ports",
294 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
295
296 g_object_class_install_property (object_class, PROP_N_OUTPUT_PORTS,
297 g_param_spec_uint ("n-output-ports", "n-output-ports", "n-output-ports",
298 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
299
300 g_object_class_install_property (object_class, PROP_MAX_INPUT_PORTS,
301 g_param_spec_uint ("max-input-ports", "max-input-ports", "max-input-ports",
302 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
303
304 g_object_class_install_property (object_class, PROP_MAX_OUTPUT_PORTS,
305 g_param_spec_uint ("max-output-ports", "max-output-ports", "max-output-ports",
306 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
307
308 signals[SIGNAL_STATE_CHANGED] = g_signal_new (
309 "state-changed", G_TYPE_FROM_CLASS (klass),
310 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 2,
311 WP_TYPE_NODE_STATE, WP_TYPE_NODE_STATE);
312
313 signals[SIGNAL_PORTS_CHANGED] = g_signal_new (
314 "ports-changed", G_TYPE_FROM_CLASS (klass),
315 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
316 }
317
318 static void
319 wp_node_process_info (gpointer instance, gpointer old_info, gpointer i)
320 {
321 const struct pw_node_info *info = i;
322
323 if (info->change_mask & PW_NODE_CHANGE_MASK_STATE) {
324 enum pw_node_state old_state = old_info ?
325 ((struct pw_node_info *) old_info)->state : PW_NODE_STATE_CREATING;
326 g_signal_emit (instance, signals[SIGNAL_STATE_CHANGED], 0,
327 old_state, info->state);
328 }
329 if (info->change_mask & PW_NODE_CHANGE_MASK_INPUT_PORTS) {
330 g_object_notify (G_OBJECT (instance), "n-input-ports");
331 g_object_notify (G_OBJECT (instance), "max-input-ports");
332 }
333 if (info->change_mask & PW_NODE_CHANGE_MASK_OUTPUT_PORTS) {
334 g_object_notify (G_OBJECT (instance), "n-output-ports");
335 g_object_notify (G_OBJECT (instance), "max-output-ports");
336 }
337 }
338
339 static gint
340 wp_node_enum_params (gpointer instance, guint32 id,
341 guint32 start, guint32 num, WpSpaPod *filter)
342 {
343 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
344 return pw_node_enum_params (d->iface, 0, id, start, num,
345 filter ? wp_spa_pod_get_spa_pod (filter) : NULL);
346 }
347
348 static gint
349 wp_node_set_param (gpointer instance, guint32 id, guint32 flags,
350 WpSpaPod * param)
351 {
352 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
353 g_autoptr (WpSpaPod) p = param;
354 return pw_node_set_param (d->iface, id, flags,
355 wp_spa_pod_get_spa_pod (p));
356 }
357
358 static void
359 wp_node_pw_object_mixin_priv_interface_init (
360 WpPwObjectMixinPrivInterface * iface)
361 {
362 wp_pw_object_mixin_priv_interface_info_init (iface, node, NODE);
363 iface->process_info = wp_node_process_info;
364 iface->enum_params = wp_node_enum_params;
365 iface->set_param = wp_node_set_param;
366 }
367
368 /*!
369 * \brief Constructs a node on the PipeWire server by asking the remote factory
370 * \a factory_name to create it.
371 *
372 * Because of the nature of the PipeWire protocol, this operation completes
373 * asynchronously at some point in the future. In order to find out when
374 * this is done, you should call wp_object_activate(), requesting at least
375 * WP_PROXY_FEATURE_BOUND. When this feature is ready, the node is ready for
376 * use on the server. If the node cannot be created, this activation operation
377 * will fail.
378 *
379 * \ingroup wpnode
380 * \param core the wireplumber core
381 * \param factory_name the pipewire factory name to construct the node
382 * \param properties (nullable) (transfer full): the properties to pass to the
383 * factory
384 * \returns (nullable) (transfer full): the new node or NULL if the core
385 * is not connected and therefore the node cannot be created
386 */
387 WpNode *
388 wp_node_new_from_factory (WpCore * core,
389 const gchar * factory_name, WpProperties * properties)
390 {
391 g_autoptr (WpProperties) props = properties;
392 return g_object_new (WP_TYPE_NODE,
393 "core", core,
394 "factory-name", factory_name,
395 "global-properties", props,
396 NULL);
397 }
398
399 /*!
400 * \brief Gets the current state of the node
401 * \ingroup wpnode
402 * \param self the node
403 * \param error (out) (optional) (transfer none): the error
404 * \returns the current state of the node
405 */
406 WpNodeState
407 wp_node_get_state (WpNode * self, const gchar ** error)
408 {
409 g_return_val_if_fail (WP_IS_NODE (self), WP_NODE_STATE_ERROR);
410 g_return_val_if_fail (wp_object_test_active_features (WP_OBJECT (self),
411 WP_PIPEWIRE_OBJECT_FEATURE_INFO), WP_NODE_STATE_ERROR);
412
413 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (self);
414 const struct pw_node_info *info = d->info;
415
416 if (error)
417 *error = info->error;
418 return (WpNodeState) info->state;
419 }
420
421 /*!
422 * \brief Gets the number of input ports of this node
423 *
424 * \remarks Requires WP_PIPEWIRE_OBJECT_FEATURE_INFO
425 *
426 * \ingroup wpnode
427 * \param self the node
428 * \param max (out) (optional): the maximum supported number of input ports
429 * \returns the number of input ports of this node, as reported by the node info
430 */
431 guint
432 wp_node_get_n_input_ports (WpNode * self, guint * max)
433 {
434 g_return_val_if_fail (WP_IS_NODE (self), 0);
435 g_return_val_if_fail (wp_object_test_active_features (WP_OBJECT (self),
436 WP_PIPEWIRE_OBJECT_FEATURE_INFO), 0);
437
438 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (self);
439 const struct pw_node_info *info = d->info;
440
441 if (max)
442 *max = info->max_input_ports;
443 return info->n_input_ports;
444 }
445
446 /*!
447 * \brief Gets the number of output ports of this node
448 *
449 * \remarks Requires WP_PIPEWIRE_OBJECT_FEATURE_INFO
450 *
451 * \ingroup wpnode
452 * \param self the node
453 * \param max (out) (optional): the maximum supported number of output ports
454 * \returns the number of output ports of this node, as reported by the node info
455 */
456 guint
457 wp_node_get_n_output_ports (WpNode * self, guint * max)
458 {
459 g_return_val_if_fail (WP_IS_NODE (self), 0);
460 g_return_val_if_fail (wp_object_test_active_features (WP_OBJECT (self),
461 WP_PIPEWIRE_OBJECT_FEATURE_INFO), 0);
462
463 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (self);
464 const struct pw_node_info *info = d->info;
465
466 if (max)
467 *max = info->max_output_ports;
468 return info->n_output_ports;
469 }
470
471 /*!
472 * \brief Gets the number of ports of this node
473 *
474 * Note that this number may not add up to
475 * wp_node_get_n_input_ports() + wp_node_get_n_output_ports()
476 * because it is discovered by looking at the number of available ports
477 * in the registry, however ports may appear there with a delay or may
478 * not appear at all if this client does not have permission to read them
479 *
480 * \remarks Requires WP_NODE_FEATURE_PORTS
481 *
482 * \ingroup wpnode
483 * \param self the node
484 * \returns the number of ports of this node.
485 */
486 guint
487 wp_node_get_n_ports (WpNode * self)
488 {
489 g_return_val_if_fail (WP_IS_NODE (self), 0);
490 g_return_val_if_fail (wp_object_test_active_features (WP_OBJECT (self),
491 WP_NODE_FEATURE_PORTS), 0);
492
493 return wp_object_manager_get_n_objects (self->ports_om);
494 }
495
496 /*!
497 * \brief Gets a new iterator that iterates over all the ports that belong
498 * to this node
499 *
500 * \remarks Requires WP_NODE_FEATURE_PORTS
501 *
502 * \ingroup wpnode
503 * \param self the node
504 * \returns (transfer full): a WpIterator that iterates over WpPort objects
505 */
506 WpIterator *
507 wp_node_new_ports_iterator (WpNode * self)
508 {
509 g_return_val_if_fail (WP_IS_NODE (self), NULL);
510 g_return_val_if_fail (wp_object_test_active_features (WP_OBJECT (self),
511 WP_NODE_FEATURE_PORTS), NULL);
512
513 return wp_object_manager_new_iterator (self->ports_om);
514 }
515
516 /*!
517 * \brief Gets a new iterator that iterates over all
518 * the ports that belong to this node and match the constraints
519 *
520 * The constraints specified in the variable arguments must follow the rules
521 * documented in wp_object_interest_new().
522 *
523 * \remarks Requires WP_NODE_FEATURE_PORTS
524 *
525 * \ingroup wpnode
526 * \param self the node
527 * \param ... a list of constraints, terminated by NULL
528 * \returns (transfer full): a WpIterator that iterates over WpPort objects
529 */
530 WpIterator *
531 wp_node_new_ports_filtered_iterator (WpNode * self, ...)
532 {
533 WpObjectInterest *interest;
534 va_list args;
535 va_start (args, self);
536 interest = wp_object_interest_new_valist (WP_TYPE_PORT, &args);
537 va_end (args);
538 return wp_node_new_ports_filtered_iterator_full (self, interest);
539 }
540
541 /*!
542 * \brief Gets a new iterator that iterates over all
543 * the ports that belong to this node and match the \a interest
544 *
545 * \remarks Requires WP_NODE_FEATURE_PORTS
546 *
547 * \ingroup wpnode
548 * \param self the node
549 * \param interest (transfer full): the interest
550 * \returns (transfer full): a WpIterator that iterates over WpPort objects
551 */
552 WpIterator *
553 wp_node_new_ports_filtered_iterator_full (WpNode * self,
554 WpObjectInterest * interest)
555 {
556 g_return_val_if_fail (WP_IS_NODE (self), NULL);
557 g_return_val_if_fail (wp_object_test_active_features (WP_OBJECT (self),
558 WP_NODE_FEATURE_PORTS), NULL);
559
560 return wp_object_manager_new_filtered_iterator_full (self->ports_om,
561 interest);
562 }
563
564 /*!
565 * \brief Retrieves the first port that matches the constraints
566 *
567 * The constraints specified in the variable arguments must follow the rules
568 * documented in wp_object_interest_new().
569 *
570 * \remarks Requires WP_NODE_FEATURE_PORTS
571 *
572 * \ingroup wpnode
573 * \param self the node
574 * \param ... a list of constraints, terminated by NULL
575 * \returns (transfer full) (nullable): the first port that matches the
576 * constraints, or NULL if there is no such port
577 */
578 WpPort *
579 wp_node_lookup_port (WpNode * self, ...)
580 {
581 WpObjectInterest *interest;
582 va_list args;
583 va_start (args, self);
584 interest = wp_object_interest_new_valist (WP_TYPE_PORT, &args);
585 va_end (args);
586 return wp_node_lookup_port_full (self, interest);
587 }
588
589 /*!
590 * \brief Retrieves the first port that matches the \a interest
591 *
592 * \remarks Requires WP_NODE_FEATURE_PORTS
593 *
594 * \ingroup wpnode
595 * \param self the node
596 * \param interest (transfer full): the interest
597 * \returns (transfer full) (nullable): the first port that matches the
598 * \a interest, or NULL if there is no such port
599 */
600 WpPort *
601 wp_node_lookup_port_full (WpNode * self, WpObjectInterest * interest)
602 {
603 g_return_val_if_fail (WP_IS_NODE (self), NULL);
604 g_return_val_if_fail (wp_object_test_active_features (WP_OBJECT (self),
605 WP_NODE_FEATURE_PORTS), NULL);
606
607 return (WpPort *)
608 wp_object_manager_lookup_full (self->ports_om, interest);
609 }
610
611 /*!
612 * \brief Sends a command to a node
613 *
614 * Valid commands are the short string reprepsentations of
615 * `enum spa_node_command`. For example, "Suspend" or "Flush" are valid commands
616 *
617 * \ingroup wpnode
618 * \param self the node
619 * \param command the command
620 */
621 void
622 wp_node_send_command (WpNode * self, const gchar * command)
623 {
624 WpSpaIdValue command_value = wp_spa_id_value_from_short_name (
625 "Spa:Pod:Object:Command:Node", command);
626
627 g_return_if_fail (WP_IS_NODE (self));
628 g_return_if_fail (command_value != NULL);
629 g_return_if_fail (wp_object_test_active_features (WP_OBJECT (self),
630 WP_PROXY_FEATURE_BOUND));
631
632 struct spa_command cmd =
633 SPA_NODE_COMMAND_INIT(wp_spa_id_value_number (command_value));
634 pw_node_send_command ((struct pw_node*)wp_proxy_get_pw_proxy (WP_PROXY (self)), &cmd);
635 }
636
637 /*! \defgroup wpimplnode WpImplNode */
638
639 enum {
640 PROP_PW_IMPL_NODE = WP_PW_OBJECT_MIXIN_PROP_CUSTOM_START,
641 };
642
643 struct _WpImplNode
644 {
645 WpProxy parent;
646 GWeakRef core;
647 struct pw_impl_node *pw_impl_node;
648 };
649
650 static void wp_impl_node_pw_object_mixin_priv_interface_init (
651 WpPwObjectMixinPrivInterface * iface);
652
653 G_DEFINE_TYPE_WITH_CODE (WpImplNode, wp_impl_node, WP_TYPE_PROXY,
654 G_IMPLEMENT_INTERFACE (WP_TYPE_PIPEWIRE_OBJECT,
655 wp_pw_object_mixin_object_interface_init)
656 G_IMPLEMENT_INTERFACE (WP_TYPE_PW_OBJECT_MIXIN_PRIV,
657 wp_impl_node_pw_object_mixin_priv_interface_init))
658
659 static void
660 wp_impl_node_init (WpImplNode * self)
661 {
662 }
663
664 static void
665 wp_impl_node_constructed (GObject * object)
666 {
667 WpImplNode * self = WP_IMPL_NODE (object);
668 WpPwObjectMixinData * data = wp_pw_object_mixin_get_data (self);
669
670 data->info = (gpointer) pw_impl_node_get_info (self->pw_impl_node);
671 data->iface = pw_impl_node_get_implementation (self->pw_impl_node);
672
673 /* TODO handle the actual node properties */
674 data->properties = wp_properties_new_empty();
675
676 WpObjectFeatures ft =
677 wp_pw_object_mixin_get_supported_features (WP_OBJECT (self))
678 & ~WP_PROXY_FEATURE_BOUND;
679 wp_object_update_features (WP_OBJECT (self), ft, 0);
680
681 G_OBJECT_CLASS (wp_impl_node_parent_class)->constructed (object);
682 }
683
684 static void
685 wp_impl_node_dispose (GObject * object)
686 {
687 WpImplNode *self = WP_IMPL_NODE (object);
688
689 WpObjectFeatures ft =
690 wp_pw_object_mixin_get_supported_features (WP_OBJECT (self))
691 & ~WP_PROXY_FEATURE_BOUND;
692 wp_object_update_features (WP_OBJECT (self), 0, ft);
693
694 G_OBJECT_CLASS (wp_impl_node_parent_class)->dispose (object);
695 }
696
697 static void
698 wp_impl_node_finalize (GObject * object)
699 {
700 WpImplNode *self = WP_IMPL_NODE (object);
701
702 g_clear_pointer (&self->pw_impl_node, pw_impl_node_destroy);
703
704 G_OBJECT_CLASS (wp_impl_node_parent_class)->finalize (object);
705 }
706
707 static void
708 wp_impl_node_set_property (GObject * object, guint property_id,
709 const GValue * value, GParamSpec * pspec)
710 {
711 WpImplNode *self = WP_IMPL_NODE (object);
712
713 switch (property_id) {
714 case PROP_PW_IMPL_NODE:
715 self->pw_impl_node = g_value_get_pointer (value);
716 break;
717 default:
718 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
719 break;
720 }
721 }
722
723 static void
724 wp_impl_node_get_property (GObject * object, guint property_id, GValue * value,
725 GParamSpec * pspec)
726 {
727 WpImplNode *self = WP_IMPL_NODE (object);
728
729 switch (property_id) {
730 case PROP_PW_IMPL_NODE:
731 g_value_set_pointer (value, self->pw_impl_node);
732 break;
733 default:
734 wp_pw_object_mixin_get_property (object, property_id, value, pspec);
735 break;
736 }
737 }
738
739 enum {
740 STEP_EXPORT = WP_TRANSITION_STEP_CUSTOM_START,
741 };
742
743 static guint
744 wp_impl_node_activate_get_next_step (WpObject * object,
745 WpFeatureActivationTransition * transition, guint step,
746 WpObjectFeatures missing)
747 {
748 /* BOUND is the only feature that can be in @em missing */
749 g_return_val_if_fail (missing == WP_PROXY_FEATURE_BOUND,
750 WP_TRANSITION_STEP_ERROR);
751
752 return STEP_EXPORT;
753 }
754
755 static void
756 wp_impl_node_activate_execute_step (WpObject * object,
757 WpFeatureActivationTransition * transition, guint step,
758 WpObjectFeatures missing)
759 {
760 WpImplNode *self = WP_IMPL_NODE (object);
761
762 switch (step) {
763 case STEP_EXPORT: {
764 g_autoptr (WpCore) core = wp_object_get_core (object);
765 struct pw_core *pw_core = wp_core_get_pw_core (core);
766 g_return_if_fail (pw_core);
767
768 wp_proxy_set_pw_proxy (WP_PROXY (self),
769 pw_core_export (pw_core, PW_TYPE_INTERFACE_Node, NULL,
770 self->pw_impl_node, 0));
771 break;
772 }
773 default:
774 g_assert_not_reached ();
775 }
776 }
777
778 /*!
779 * \struct WpImplNode
780 *
781 * A WpImplNode allows running a node implementation (`struct pw_impl_node`)
782 * locally, loading the implementation from factory or wrapping a manually
783 * constructed `pw_impl_node`. This object can then be exported to PipeWire
784 * by requesting WP_PROXY_FEATURE_BOUND.
785 *
786 * \gproperties
787 *
788 * \gproperty{pw-impl-node, gpointer, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY,
789 * The actual node implementation\, `struct pw_impl_node *`}
790 */
791 static void
792 wp_impl_node_class_init (WpImplNodeClass * klass)
793 {
794 GObjectClass *object_class = (GObjectClass *) klass;
795 WpObjectClass *wpobject_class = (WpObjectClass *) klass;
796
797 object_class->constructed = wp_impl_node_constructed;
798 object_class->dispose = wp_impl_node_dispose;
799 object_class->finalize = wp_impl_node_finalize;
800 object_class->set_property = wp_impl_node_set_property;
801 object_class->get_property = wp_impl_node_get_property;
802
803 wpobject_class->get_supported_features =
804 wp_pw_object_mixin_get_supported_features;
805 wpobject_class->activate_get_next_step = wp_impl_node_activate_get_next_step;
806 wpobject_class->activate_execute_step = wp_impl_node_activate_execute_step;
807
808 wp_pw_object_mixin_class_override_properties (object_class);
809
810 g_object_class_install_property (object_class, PROP_PW_IMPL_NODE,
811 g_param_spec_pointer ("pw-impl-node", "pw-impl-node",
812 "The actual node implementation, struct pw_impl_node *",
813 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
814 }
815
816 static int
817 impl_node_collect_params (void *data, int seq,
818 uint32_t id, uint32_t index, uint32_t next, struct spa_pod *param)
819 {
820 GPtrArray *result = data;
821 g_ptr_array_add (result, wp_spa_pod_new_wrap_const (param));
822 return 0;
823 }
824
825 static GPtrArray *
826 wp_impl_node_enum_params_sync (gpointer instance, guint32 id,
827 guint32 start, guint32 num, WpSpaPod *filter)
828 {
829 WpImplNode *self = WP_IMPL_NODE (instance);
830 GPtrArray *result =
831 g_ptr_array_new_with_free_func ((GDestroyNotify) wp_spa_pod_unref);
832
833 pw_impl_node_for_each_param (self->pw_impl_node, 1, id, start, num,
834 filter ? wp_spa_pod_get_spa_pod (filter) : NULL,
835 impl_node_collect_params, result);
836 return result;
837 }
838
839 static gint
840 wp_impl_node_set_param (gpointer instance, guint32 id, guint32 flags,
841 WpSpaPod * param)
842 {
843 WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance);
844 g_autoptr (WpSpaPod) p = param;
845 return spa_node_set_param (d->iface, id, flags,
846 wp_spa_pod_get_spa_pod (p));
847 }
848
849 static void
850 wp_impl_node_pw_object_mixin_priv_interface_init (
851 WpPwObjectMixinPrivInterface * iface)
852 {
853 wp_pw_object_mixin_priv_interface_info_init (iface, node, NODE);
854 iface->flags = WP_PW_OBJECT_MIXIN_PRIV_NO_PARAM_CACHE;
855 iface->enum_params_sync = wp_impl_node_enum_params_sync;
856 iface->set_param = wp_impl_node_set_param;
857 }
858
859 /*!
860 * \brief Constructs a node object from an existing `pw_impl_node`.
861 * \ingroup wpimplnode
862 * \param core the wireplumber core
863 * \param node an existing `pw_impl_node` to wrap
864 * \returns (transfer full): A new WpImplNode wrapping \a node
865 */
866 WpImplNode *
867 wp_impl_node_new_wrap (WpCore * core, struct pw_impl_node * node)
868 {
869 return g_object_new (WP_TYPE_IMPL_NODE,
870 "core", core,
871 "pw-impl-node", node,
872 NULL);
873 }
874
875 /*!
876 * \brief Constructs a new node, locally on this process, using the specified
877 * \a factory_name.
878 *
879 * To export this node to the PipeWire server, you need to call
880 * wp_object_activate() requesting WP_PROXY_FEATURE_BOUND and
881 * wait for the operation to complete.
882 *
883 * \ingroup wpimplnode
884 * \param core the wireplumber core
885 * \param factory_name the name of the pipewire factory
886 * \param properties (nullable) (transfer full): properties to be passed to node
887 * constructor
888 * \returns (nullable) (transfer full): A new WpImplNode wrapping the
889 * node that was constructed by the factory, or %NULL if the factory
890 * does not exist or was unable to construct the node
891 */
892 WpImplNode *
893 wp_impl_node_new_from_pw_factory (WpCore * core,
894 const gchar * factory_name, WpProperties * properties)
895 {
896 g_autoptr (WpProperties) props = properties;
897 struct pw_context *pw_context = wp_core_get_pw_context (core);
898 struct pw_impl_factory *factory = NULL;
899 struct pw_impl_node *node = NULL;
900
901 g_return_val_if_fail (pw_context != NULL, NULL);
902
903 factory = pw_context_find_factory (pw_context, factory_name);
904 if (!factory) {
905 wp_warning ("pipewire factory '%s' not found", factory_name);
906 return NULL;
907 }
908
909 node = pw_impl_factory_create_object (factory,
910 NULL, PW_TYPE_INTERFACE_Node, PW_VERSION_NODE,
911 props ? wp_properties_to_pw_properties (props) : NULL, 0);
912 if (!node) {
913 wp_warning ("failed to create node from factory '%s'", factory_name);
914 return NULL;
915 }
916
917 return wp_impl_node_new_wrap (core, node);
918 }