| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2020 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "proxy.h" |
| 10 |
#include "log.h" |
| 11 |
#include "error.h" |
| 12 |
|
| 13 |
#include <pipewire/pipewire.h> |
| 14 |
#include <spa/utils/hook.h> |
| 15 |
#include <spa/utils/result.h> |
| 16 |
|
| 17 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-proxy") |
| 18 |
|
| 19 |
/*! \defgroup wpproxy WpProxy */ |
| 20 |
/*! |
| 21 |
* \struct WpProxy |
| 22 |
* |
| 23 |
* Base class for all objects that expose PipeWire objects using `pw_proxy` |
| 24 |
* underneath. |
| 25 |
* |
| 26 |
* This base class cannot be instantiated. It provides handling of |
| 27 |
* pw_proxy's events and exposes common functionality. |
| 28 |
* |
| 29 |
* \gproperties |
| 30 |
* |
| 31 |
* \gproperty{bound-id, guint, G_PARAM_READABLE, |
| 32 |
* The id that this object has on the registry} |
| 33 |
* |
| 34 |
* \gproperty{pw-proxy, gpointer, G_PARAM_READABLE, |
| 35 |
* The `struct pw_proxy *`} |
| 36 |
* |
| 37 |
* \gsignals |
| 38 |
* |
| 39 |
* \par bound |
| 40 |
* \parblock |
| 41 |
* \code |
| 42 |
* void |
| 43 |
* bound_callback (WpProxy * self, |
| 44 |
* guint id, |
| 45 |
* gpointer user_data) |
| 46 |
* \endcode |
| 47 |
* Parameters: |
| 48 |
* - `id` - the bound id of the proxy |
| 49 |
* |
| 50 |
* Flags: G_SIGNAL_RUN_FIRST |
| 51 |
* \endparblock |
| 52 |
* |
| 53 |
* \par pw-proxy-created |
| 54 |
* \parblock |
| 55 |
* \code |
| 56 |
* void |
| 57 |
* pw_proxy_created_callback (WpProxy * self, |
| 58 |
* gpointer object, |
| 59 |
* gpointer user_data) |
| 60 |
* \endcode |
| 61 |
* Parameters: |
| 62 |
* - `object` - pointer to the pw_proxy that was just created |
| 63 |
* |
| 64 |
* Flags: G_SIGNAL_RUN_FIRST |
| 65 |
* \endparblock |
| 66 |
* |
| 67 |
* \par pw-proxy-destroyed |
| 68 |
* \parblock |
| 69 |
* \code |
| 70 |
* void |
| 71 |
* pw_proxy_destroyed_callback (WpProxy * self, |
| 72 |
* gpointer user_data) |
| 73 |
* \endcode |
| 74 |
* Flags: G_SIGNAL_RUN_FIRST |
| 75 |
* \endparblock |
| 76 |
* |
| 77 |
* \par error |
| 78 |
* \parblock |
| 79 |
* \code |
| 80 |
* void |
| 81 |
* error_callback (WpProxy * self, |
| 82 |
* gint seq, gint res, const gchar *message, |
| 83 |
* gpointer user_data) |
| 84 |
* \endcode |
| 85 |
* Emitted when an error occurs on the remote object. |
| 86 |
* The parameters are exactly the same as on the underlying pw_proxy error |
| 87 |
* callback. |
| 88 |
* |
| 89 |
* Parameters: |
| 90 |
* - `seq` - the sequence number of the operation that caused the error |
| 91 |
* - `res` - the error code |
| 92 |
* - `message` - a description of the error |
| 93 |
* |
| 94 |
* Flags: G_SIGNAL_RUN_FIRST |
| 95 |
* \endparblock |
| 96 |
*/ |
| 97 |
|
| 98 |
typedef struct _WpProxyPrivate WpProxyPrivate; |
| 99 |
struct _WpProxyPrivate |
| 100 |
{ |
| 101 |
struct pw_proxy *pw_proxy; |
| 102 |
struct spa_hook listener; |
| 103 |
}; |
| 104 |
|
| 105 |
enum { |
| 106 |
PROP_0, |
| 107 |
PROP_BOUND_ID, |
| 108 |
PROP_PW_PROXY, |
| 109 |
}; |
| 110 |
|
| 111 |
enum |
| 112 |
{ |
| 113 |
SIGNAL_PW_PROXY_CREATED, |
| 114 |
SIGNAL_PW_PROXY_DESTROYED, |
| 115 |
SIGNAL_BOUND, |
| 116 |
SIGNAL_ERROR, |
| 117 |
LAST_SIGNAL, |
| 118 |
}; |
| 119 |
|
| 120 |
static guint signals[LAST_SIGNAL] = { 0 }; |
| 121 |
|
| 122 |
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (WpProxy, wp_proxy, WP_TYPE_OBJECT) |
| 123 |
|
| 124 |
static void |
| 125 |
proxy_event_destroy (void *data) |
| 126 |
{ |
| 127 |
WpProxy *self = WP_PROXY (data); |
| 128 |
WpProxyPrivate *priv = wp_proxy_get_instance_private (self); |
| 129 |
|
| 130 |
wp_trace_object (self, "destroyed pw_proxy %p (%u)", priv->pw_proxy, |
| 131 |
pw_proxy_get_bound_id (priv->pw_proxy)); |
| 132 |
|
| 133 |
spa_hook_remove (&priv->listener); |
| 134 |
priv->pw_proxy = NULL; |
| 135 |
wp_object_update_features (WP_OBJECT (self), 0, WP_PROXY_FEATURE_BOUND); |
| 136 |
|
| 137 |
wp_object_abort_activation (WP_OBJECT (self), "PipeWire proxy destroyed"); |
| 138 |
|
| 139 |
g_signal_emit (self, signals[SIGNAL_PW_PROXY_DESTROYED], 0); |
| 140 |
} |
| 141 |
|
| 142 |
static void |
| 143 |
proxy_event_bound (void *data, uint32_t global_id) |
| 144 |
{ |
| 145 |
WpProxy *self = WP_PROXY (data); |
| 146 |
|
| 147 |
wp_trace_object (self, "bound to %u", global_id); |
| 148 |
|
| 149 |
wp_object_update_features (WP_OBJECT (self), WP_PROXY_FEATURE_BOUND, 0); |
| 150 |
g_signal_emit (self, signals[SIGNAL_BOUND], 0, global_id); |
| 151 |
} |
| 152 |
|
| 153 |
static void |
| 154 |
proxy_event_removed (void *data) |
| 155 |
{ |
| 156 |
wp_trace_object (data, "removed"); |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
proxy_event_error (void *data, int seq, int res, const char *message) |
| 161 |
{ |
| 162 |
WpProxy *self = WP_PROXY (data); |
| 163 |
WpProxyPrivate *priv = wp_proxy_get_instance_private (self); |
| 164 |
|
| 165 |
wp_trace_object (self, "error seq:%d res:%d (%s) %s", |
| 166 |
seq, res, spa_strerror(res), message); |
| 167 |
|
| 168 |
/* we destroy the proxy on error if feature bound is still not enabled */ |
| 169 |
if (priv->pw_proxy && |
| 170 |
!(wp_object_test_active_features (WP_OBJECT (self), WP_PROXY_FEATURE_BOUND))) |
| 171 |
pw_proxy_destroy (priv->pw_proxy); |
| 172 |
|
| 173 |
wp_object_abort_activation (WP_OBJECT (self), message); |
| 174 |
|
| 175 |
g_signal_emit (self, signals[SIGNAL_ERROR], 0, seq, res, message); |
| 176 |
} |
| 177 |
|
| 178 |
static const struct pw_proxy_events proxy_events = { |
| 179 |
PW_VERSION_PROXY_EVENTS, |
| 180 |
.destroy = proxy_event_destroy, |
| 181 |
.bound = proxy_event_bound, |
| 182 |
.removed = proxy_event_removed, |
| 183 |
.error = proxy_event_error, |
| 184 |
}; |
| 185 |
|
| 186 |
static void |
| 187 |
wp_proxy_init (WpProxy * self) |
| 188 |
{ |
| 189 |
} |
| 190 |
|
| 191 |
static void |
| 192 |
wp_proxy_dispose (GObject * object) |
| 193 |
{ |
| 194 |
WpProxyPrivate *priv = wp_proxy_get_instance_private (WP_PROXY (object)); |
| 195 |
|
| 196 |
if (priv->pw_proxy) |
| 197 |
pw_proxy_destroy (priv->pw_proxy); |
| 198 |
|
| 199 |
G_OBJECT_CLASS (wp_proxy_parent_class)->dispose (object); |
| 200 |
} |
| 201 |
|
| 202 |
static void |
| 203 |
wp_proxy_get_property (GObject * object, guint property_id, GValue * value, |
| 204 |
GParamSpec * pspec) |
| 205 |
{ |
| 206 |
WpProxy *self = WP_PROXY (object); |
| 207 |
|
| 208 |
switch (property_id) { |
| 209 |
case PROP_BOUND_ID: |
| 210 |
g_value_set_uint (value, wp_proxy_get_bound_id (self)); |
| 211 |
break; |
| 212 |
case PROP_PW_PROXY: |
| 213 |
g_value_set_pointer (value, wp_proxy_get_pw_proxy (self)); |
| 214 |
break; |
| 215 |
default: |
| 216 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 217 |
break; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
static void |
| 222 |
wp_proxy_deactivate (WpObject * object, WpObjectFeatures features) |
| 223 |
{ |
| 224 |
if (features & WP_PROXY_FEATURE_BOUND) { |
| 225 |
WpProxyPrivate *priv = wp_proxy_get_instance_private (WP_PROXY (object)); |
| 226 |
if (priv->pw_proxy) |
| 227 |
pw_proxy_destroy (priv->pw_proxy); |
| 228 |
wp_object_update_features (object, 0, WP_PROXY_FEATURE_BOUND); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
static void |
| 233 |
wp_proxy_class_init (WpProxyClass * klass) |
| 234 |
{ |
| 235 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 236 |
WpObjectClass *wpobject_class = (WpObjectClass *) klass; |
| 237 |
|
| 238 |
object_class->get_property = wp_proxy_get_property; |
| 239 |
object_class->dispose = wp_proxy_dispose; |
| 240 |
|
| 241 |
wpobject_class->deactivate = wp_proxy_deactivate; |
| 242 |
|
| 243 |
/* Install the properties */ |
| 244 |
|
| 245 |
g_object_class_install_property (object_class, PROP_BOUND_ID, |
| 246 |
g_param_spec_uint ("bound-id", "bound-id", |
| 247 |
"The id that this object has on the registry", 0, G_MAXUINT, 0, |
| 248 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 249 |
|
| 250 |
g_object_class_install_property (object_class, PROP_PW_PROXY, |
| 251 |
g_param_spec_pointer ("pw-proxy", "pw-proxy", "The struct pw_proxy *", |
| 252 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 253 |
|
| 254 |
/* Signals */ |
| 255 |
signals[SIGNAL_PW_PROXY_CREATED] = g_signal_new ( |
| 256 |
"pw-proxy-created", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 257 |
G_STRUCT_OFFSET (WpProxyClass, pw_proxy_created), NULL, NULL, NULL, |
| 258 |
G_TYPE_NONE, 1, G_TYPE_POINTER); |
| 259 |
|
| 260 |
signals[SIGNAL_PW_PROXY_DESTROYED] = g_signal_new ( |
| 261 |
"pw-proxy-destroyed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 262 |
G_STRUCT_OFFSET (WpProxyClass, pw_proxy_destroyed), NULL, NULL, NULL, |
| 263 |
G_TYPE_NONE, 0); |
| 264 |
|
| 265 |
signals[SIGNAL_BOUND] = g_signal_new ( |
| 266 |
"bound", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 267 |
G_STRUCT_OFFSET (WpProxyClass, bound), NULL, NULL, NULL, |
| 268 |
G_TYPE_NONE, 1, G_TYPE_UINT); |
| 269 |
|
| 270 |
signals[SIGNAL_ERROR] = g_signal_new ( |
| 271 |
"error", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 272 |
G_STRUCT_OFFSET (WpProxyClass, error), NULL, NULL, NULL, |
| 273 |
G_TYPE_NONE, 3, G_TYPE_INT, G_TYPE_INT, G_TYPE_STRING); |
| 274 |
} |
| 275 |
|
| 276 |
/*! |
| 277 |
* \brief Returns the proxy bound id. |
| 278 |
* |
| 279 |
* The bound id is the id that this object has on the |
| 280 |
* PipeWire registry (a.k.a. the global id). The object must have the |
| 281 |
* WP_PROXY_FEATURE_BOUND feature before this method can be called. |
| 282 |
* |
| 283 |
* \remarks Requires WP_PROXY_FEATURE_BOUND |
| 284 |
* |
| 285 |
* \ingroup wpproxy |
| 286 |
* \param self the proxy |
| 287 |
* \returns the bound id of this object |
| 288 |
*/ |
| 289 |
guint32 |
| 290 |
wp_proxy_get_bound_id (WpProxy * self) |
| 291 |
{ |
| 292 |
g_return_val_if_fail (WP_IS_PROXY (self), 0); |
| 293 |
g_warn_if_fail (wp_object_get_active_features (WP_OBJECT (self)) & |
| 294 |
WP_PROXY_FEATURE_BOUND); |
| 295 |
|
| 296 |
WpProxyPrivate *priv = wp_proxy_get_instance_private (self); |
| 297 |
return priv->pw_proxy ? pw_proxy_get_bound_id (priv->pw_proxy) : SPA_ID_INVALID; |
| 298 |
} |
| 299 |
|
| 300 |
/*! |
| 301 |
* \brief Gets the interface type of the proxied object |
| 302 |
* \ingroup wpproxy |
| 303 |
* \param self the proxy |
| 304 |
* \param version (out) (optional): the version of the interface |
| 305 |
* \returns the PipeWire type of the interface that is being proxied |
| 306 |
*/ |
| 307 |
const gchar * |
| 308 |
wp_proxy_get_interface_type (WpProxy * self, guint32 * version) |
| 309 |
{ |
| 310 |
g_return_val_if_fail (WP_IS_PROXY (self), NULL); |
| 311 |
|
| 312 |
WpProxyPrivate *priv = wp_proxy_get_instance_private (self); |
| 313 |
if (priv->pw_proxy) |
| 314 |
return pw_proxy_get_type (priv->pw_proxy, version); |
| 315 |
else { |
| 316 |
WpProxyClass *klass = WP_PROXY_GET_CLASS (self); |
| 317 |
if (version) |
| 318 |
*version = klass->pw_iface_version; |
| 319 |
return klass->pw_iface_type; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
/*! |
| 324 |
* \brief Gets the `pw_proxy` wrapped by this proxy object |
| 325 |
* \ingroup wpproxy |
| 326 |
* \param self the proxy |
| 327 |
* \returns a pointer to the underlying `pw_proxy` object |
| 328 |
*/ |
| 329 |
struct pw_proxy * |
| 330 |
wp_proxy_get_pw_proxy (WpProxy * self) |
| 331 |
{ |
| 332 |
g_return_val_if_fail (WP_IS_PROXY (self), NULL); |
| 333 |
|
| 334 |
WpProxyPrivate *priv = wp_proxy_get_instance_private (self); |
| 335 |
return priv->pw_proxy; |
| 336 |
} |
| 337 |
|
| 338 |
/*! |
| 339 |
* \brief Private method to be used by subclasses to set the `pw_proxy` pointer |
| 340 |
* when it is available. |
| 341 |
* |
| 342 |
* This can be called only if there is no `pw_proxy` already set. |
| 343 |
* Takes ownership of \a proxy. |
| 344 |
* |
| 345 |
* \ingroup wpproxy |
| 346 |
*/ |
| 347 |
void |
| 348 |
wp_proxy_set_pw_proxy (WpProxy * self, struct pw_proxy * proxy) |
| 349 |
{ |
| 350 |
g_return_if_fail (WP_IS_PROXY (self)); |
| 351 |
|
| 352 |
WpProxyPrivate *priv = wp_proxy_get_instance_private (self); |
| 353 |
|
| 354 |
g_return_if_fail (proxy); |
| 355 |
|
| 356 |
g_return_if_fail (priv->pw_proxy == NULL); |
| 357 |
priv->pw_proxy = proxy; |
| 358 |
|
| 359 |
pw_proxy_add_listener (priv->pw_proxy, &priv->listener, &proxy_events, |
| 360 |
self); |
| 361 |
|
| 362 |
/* inform subclasses and listeners */ |
| 363 |
g_signal_emit (self, signals[SIGNAL_PW_PROXY_CREATED], 0, priv->pw_proxy); |
| 364 |
} |