Debian Codegraph @

lib/wp/global-proxy.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 "global-proxy.h"
10 #include "private/registry.h"
11 #include "core.h"
12 #include "error.h"
13 #include "log.h"
14
15 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-global-proxy")
16
17 /*! \defgroup wpglobalproxy WpGlobalProxy */
18 /*!
19 * \struct WpGlobalProxy
20 *
21 * A proxy that represents a PipeWire global object, i.e. an
22 * object that is made available through the PipeWire registry.
23 *
24 * \gproperties
25 *
26 * \gproperty{global, WpGlobal *, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY,
27 * Internal WpGlobal object}
28 *
29 * \gproperty{factory-name, gchar *, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY,
30 * The factory name}
31 *
32 * \gproperty{global-properties, WpProperties *,
33 * G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY,
34 * The pipewire global properties}
35 *
36 * \gproperty{permissions, guint, G_PARAM_READABLE,
37 * The pipewire global permissions}
38 */
39
40 typedef struct _WpGlobalProxyPrivate WpGlobalProxyPrivate;
41 struct _WpGlobalProxyPrivate
42 {
43 WpGlobal *global;
44 gchar factory_name[96];
45 WpProperties *properties;
46 };
47
48 enum {
49 PROP_0,
50 PROP_GLOBAL,
51 PROP_FACTORY_NAME,
52 PROP_GLOBAL_PROPERTIES,
53 PROP_PERMISSIONS,
54 };
55
56 G_DEFINE_TYPE_WITH_PRIVATE (WpGlobalProxy, wp_global_proxy, WP_TYPE_PROXY)
57
58 static void
59 wp_global_proxy_init (WpGlobalProxy * self)
60 {
61 }
62
63 static void
64 wp_global_proxy_dispose (GObject * object)
65 {
66 WpGlobalProxy *self = WP_GLOBAL_PROXY (object);
67 WpGlobalProxyPrivate *priv =
68 wp_global_proxy_get_instance_private (self);
69
70 if (priv->global)
71 wp_global_rm_flag (priv->global, WP_GLOBAL_FLAG_OWNED_BY_PROXY);
72
73 G_OBJECT_CLASS (wp_global_proxy_parent_class)->dispose (object);
74 }
75
76 static void
77 wp_global_proxy_finalize (GObject * object)
78 {
79 WpGlobalProxy *self = WP_GLOBAL_PROXY (object);
80 WpGlobalProxyPrivate *priv =
81 wp_global_proxy_get_instance_private (self);
82
83 g_clear_pointer (&priv->properties, wp_properties_unref);
84 g_clear_pointer (&priv->global, wp_global_unref);
85
86 G_OBJECT_CLASS (wp_global_proxy_parent_class)->finalize (object);
87 }
88
89 static void
90 wp_global_proxy_set_property (GObject * object, guint property_id,
91 const GValue * value, GParamSpec * pspec)
92 {
93 WpGlobalProxy *self = WP_GLOBAL_PROXY (object);
94 WpGlobalProxyPrivate *priv =
95 wp_global_proxy_get_instance_private (self);
96
97 switch (property_id) {
98 case PROP_GLOBAL:
99 priv->global = g_value_dup_boxed (value);
100 break;
101 case PROP_FACTORY_NAME:
102 priv->factory_name[0] = '\0';
103 strncpy (priv->factory_name, g_value_get_string (value),
104 sizeof (priv->factory_name) - 1);
105 break;
106 case PROP_GLOBAL_PROPERTIES:
107 priv->properties = g_value_dup_boxed (value);
108 break;
109 default:
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
111 break;
112 }
113 }
114
115 static void
116 wp_global_proxy_get_property (GObject * object, guint property_id,
117 GValue * value, GParamSpec * pspec)
118 {
119 WpGlobalProxy *self = WP_GLOBAL_PROXY (object);
120
121 switch (property_id) {
122 case PROP_PERMISSIONS:
123 g_value_set_uint (value, wp_global_proxy_get_permissions (self));
124 break;
125 case PROP_GLOBAL_PROPERTIES:
126 g_value_take_boxed (value, wp_global_proxy_get_global_properties (self));
127 break;
128 default:
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
130 break;
131 }
132 }
133
134 static WpObjectFeatures
135 wp_global_proxy_get_supported_features (WpObject * object)
136 {
137 return WP_PROXY_FEATURE_BOUND;
138 }
139
140 enum {
141 STEP_BIND = WP_TRANSITION_STEP_CUSTOM_START,
142 };
143
144 static guint
145 wp_global_proxy_activate_get_next_step (WpObject * object,
146 WpFeatureActivationTransition * transition, guint step,
147 WpObjectFeatures missing)
148 {
149 /* we only support BOUND, so this is the only
150 feature that can be in @em missing */
151 g_return_val_if_fail (missing == WP_PROXY_FEATURE_BOUND,
152 WP_TRANSITION_STEP_ERROR);
153
154 return STEP_BIND;
155 }
156
157 static void
158 wp_global_proxy_step_bind (WpObject * object,
159 WpFeatureActivationTransition * transition, guint step,
160 WpObjectFeatures missing)
161 {
162 WpProxyClass *proxy_klass = WP_PROXY_GET_CLASS (WP_PROXY (object));
163 WpGlobalProxy *self = WP_GLOBAL_PROXY (object);
164 WpGlobalProxyPrivate *priv = wp_global_proxy_get_instance_private (self);
165
166 /* Create the pipewire object if global is NULL */
167 if (priv->global == NULL && priv->factory_name[0] != '\0') {
168 g_autoptr (WpCore) core = NULL;
169 struct pw_core *pw_core = NULL;
170 struct pw_proxy * p = NULL;
171
172 core = wp_object_get_core (WP_OBJECT (self));
173 if (G_UNLIKELY (!core)) {
174 wp_transition_return_error (WP_TRANSITION (transition), g_error_new (
175 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
176 "The WirePlumber core is not valid; object cannot be created"));
177 return;
178 }
179
180 pw_core = wp_core_get_pw_core (core);
181 if (G_UNLIKELY (!pw_core)) {
182 wp_transition_return_error (WP_TRANSITION (transition), g_error_new (
183 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
184 "The WirePlumber core is not connected; object cannot be created"));
185 return;
186 }
187
188 p = pw_core_create_object (pw_core, priv->factory_name,
189 proxy_klass->pw_iface_type, proxy_klass->pw_iface_version,
190 priv->properties ?
191 wp_properties_peek_dict (priv->properties) : NULL, 0);
192 if (G_UNLIKELY (!p)) {
193 wp_transition_return_error (WP_TRANSITION (transition), g_error_new (
194 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
195 "Failed to create object with given factory name and properties"));
196 return;
197 }
198
199 wp_proxy_set_pw_proxy (WP_PROXY (self), p);
200 }
201
202 /* Bind */
203 if (wp_proxy_get_pw_proxy (WP_PROXY (self)) == NULL) {
204 if (!wp_global_proxy_bind (self)) {
205 wp_transition_return_error (WP_TRANSITION (transition), g_error_new (
206 WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVALID_ARGUMENT,
207 "global not specified or destroyed; cannot bind proxy"));
208 }
209 }
210 }
211
212
213 static void
214 wp_global_proxy_activate_execute_step (WpObject * object,
215 WpFeatureActivationTransition * transition, guint step,
216 WpObjectFeatures missing)
217 {
218 switch (step) {
219 case STEP_BIND:
220 wp_global_proxy_step_bind (object, transition, step, missing);
221 break;
222 case WP_TRANSITION_STEP_ERROR:
223 break;
224 default:
225 g_assert_not_reached ();
226 }
227 }
228
229 static void
230 wp_global_proxy_bound (WpProxy * proxy, guint32 global_id)
231 {
232 WpGlobalProxy *self = WP_GLOBAL_PROXY (proxy);
233 WpGlobalProxyPrivate *priv =
234 wp_global_proxy_get_instance_private (self);
235 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
236
237 if (!priv->global) {
238 wp_registry_prepare_new_global (wp_core_get_registry (core),
239 global_id, PW_PERM_ALL, WP_GLOBAL_FLAG_OWNED_BY_PROXY,
240 G_TYPE_FROM_INSTANCE (self), self,
241 priv->properties ? wp_properties_peek_dict (priv->properties) : NULL,
242 &priv->global);
243 }
244 }
245
246 static void
247 wp_global_proxy_destroyed (WpProxy * proxy)
248 {
249 WpGlobalProxy *self = WP_GLOBAL_PROXY (proxy);
250 WpGlobalProxyPrivate *priv =
251 wp_global_proxy_get_instance_private (self);
252
253 if (priv->global && priv->global->proxy &&
254 (priv->global->flags & WP_GLOBAL_FLAG_OWNED_BY_PROXY)) {
255 /* We can end up here as a result of _request_destroy() followed by
256 * _deactivate(FEATURE_BOUND), where the latter triggers this callback
257 * before _remove_global is processed.
258 * If proxy is owned, it is gone now, so not much owned left.
259 * self might be cleaned up soon, so this is a good time to remove
260 * the non-refcounted backreference in global. If not done now, _dispose()
261 * does not have a chance to cleanup (as the reference to global is gone).
262 * If remove_global then comes in later, there is no more real work to
263 * do when WP_GLOBAL_FLAG_APPEARS_ON_REGISTRY is removed
264 */
265 wp_global_rm_flag (priv->global, WP_GLOBAL_FLAG_OWNED_BY_PROXY);
266 }
267 g_clear_pointer (&priv->global, wp_global_unref);
268 }
269
270 static void
271 wp_global_proxy_class_init (WpGlobalProxyClass * klass)
272 {
273 GObjectClass *object_class = (GObjectClass *) klass;
274 WpObjectClass *wpobject_class = (WpObjectClass *) klass;
275 WpProxyClass *proxy_class = (WpProxyClass *) klass;
276
277 object_class->finalize = wp_global_proxy_finalize;
278 object_class->dispose = wp_global_proxy_dispose;
279 object_class->set_property = wp_global_proxy_set_property;
280 object_class->get_property = wp_global_proxy_get_property;
281
282 wpobject_class->get_supported_features =
283 wp_global_proxy_get_supported_features;
284 wpobject_class->activate_get_next_step =
285 wp_global_proxy_activate_get_next_step;
286 wpobject_class->activate_execute_step =
287 wp_global_proxy_activate_execute_step;
288
289 proxy_class->bound = wp_global_proxy_bound;
290 proxy_class->pw_proxy_destroyed = wp_global_proxy_destroyed;
291
292 g_object_class_install_property (object_class, PROP_GLOBAL,
293 g_param_spec_boxed ("global", "global", "Internal WpGlobal object",
294 wp_global_get_type (),
295 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
296
297 g_object_class_install_property (object_class, PROP_FACTORY_NAME,
298 g_param_spec_string ("factory-name", "factory-name",
299 "The factory name", "",
300 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
301
302 g_object_class_install_property (object_class, PROP_GLOBAL_PROPERTIES,
303 g_param_spec_boxed ("global-properties", "global-properties",
304 "The pipewire global properties", WP_TYPE_PROPERTIES,
305 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
306
307 g_object_class_install_property (object_class, PROP_PERMISSIONS,
308 g_param_spec_uint ("permissions", "permissions",
309 "The pipewire global permissions", 0, G_MAXUINT, 0,
310 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
311 }
312
313 /*!
314 * \brief Requests the PipeWire server to destroy the object represented by
315 * this proxy.
316 *
317 * If the server allows it, the object will be destroyed and the
318 * WpProxy's `pw-proxy-destroyed` signal will be emitted. If the server does
319 * not allow it, nothing will happen.
320 *
321 * This is mostly useful for destroying WpLink objects.
322 *
323 * \ingroup wpglobalproxy
324 * \param self the pipewire global
325 */
326 void
327 wp_global_proxy_request_destroy (WpGlobalProxy * self)
328 {
329 g_return_if_fail (WP_IS_GLOBAL_PROXY (self));
330
331 WpGlobalProxyPrivate *priv =
332 wp_global_proxy_get_instance_private (self);
333 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
334
335 if (priv->global && core) {
336 WpRegistry *reg = wp_core_get_registry (core);
337 if (reg->pw_registry)
338 pw_registry_destroy (reg->pw_registry, priv->global->id);
339 }
340 }
341
342 /*!
343 * \brief Gets the permissions of a pipewire global
344 * \ingroup wpglobalproxy
345 * \param self the pipewire global
346 * \returns the permissions that wireplumber has on this object
347 */
348 guint32
349 wp_global_proxy_get_permissions (WpGlobalProxy * self)
350 {
351 g_return_val_if_fail (WP_IS_GLOBAL_PROXY (self), 0);
352
353 WpGlobalProxyPrivate *priv =
354 wp_global_proxy_get_instance_private (self);
355
356 return priv->global ? priv->global->permissions : PW_PERM_ALL;
357 }
358
359 /*!
360 * \brief Gets the global properties of a pipewire global
361 * \ingroup wpglobalproxy
362 * \param self the pipewire global
363 * \returns (transfer full): the global (immutable) properties of this
364 * pipewire object
365 */
366 WpProperties *
367 wp_global_proxy_get_global_properties (WpGlobalProxy * self)
368 {
369 g_return_val_if_fail (WP_IS_GLOBAL_PROXY (self), NULL);
370
371 WpGlobalProxyPrivate *priv =
372 wp_global_proxy_get_instance_private (self);
373
374 if (priv->global && priv->global->properties)
375 return wp_properties_ref (priv->global->properties);
376 return NULL;
377 }
378
379 /*!
380 * \brief Binds to the global and creates the underlying `pw_proxy`.
381 *
382 * This is mostly meant to be called internally. It will create the `pw_proxy`
383 * and will activate the WP_PROXY_FEATURE_BOUND feature.
384 *
385 * This may only be called if there is no `pw_proxy` associated with this
386 * object yet.
387 *
388 * \ingroup wpglobalproxy
389 * \param self the pipewire global
390 * \returns TRUE on success, FALSE if there is no global to bind to
391 */
392 gboolean
393 wp_global_proxy_bind (WpGlobalProxy * self)
394 {
395 WpGlobalProxyPrivate *priv;
396 struct pw_proxy *p;
397
398 g_return_val_if_fail (WP_IS_GLOBAL_PROXY (self), FALSE);
399 g_return_val_if_fail (wp_proxy_get_pw_proxy (WP_PROXY (self)) == NULL, FALSE);
400
401 priv = wp_global_proxy_get_instance_private (self);
402
403 if (!priv->global || !priv->global->proxy)
404 return FALSE;
405
406 /*
407 * We can bind only if the WpGlobal will unbind this proxy on removal, so
408 * assert here that this is so. Why: pw_registry_bind can race with the global
409 * id being replaced with a different object on server side, so we must rely
410 * on the registry_global messages to know if the object was replaced. If the
411 * race occurs, and a wrong object is going to be bound here, what will then
412 * happen is that WpGlobal will dispose this proxy and no problem arises.
413 */
414 g_return_val_if_fail (priv->global->proxy == self, FALSE);
415
416 p = wp_global_bind (priv->global);
417 if (!p)
418 return FALSE;
419
420 wp_proxy_set_pw_proxy (WP_PROXY (self), p);
421 return TRUE;
422 }