| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2020 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "session-item.h" |
| 10 |
#include "core.h" |
| 11 |
#include "log.h" |
| 12 |
#include "error.h" |
| 13 |
#include <spa/utils/defs.h> |
| 14 |
|
| 15 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-si") |
| 16 |
|
| 17 |
/*! \defgroup wpsessionitem WpSessionItem */ |
| 18 |
/*! |
| 19 |
* \struct WpSessionItem |
| 20 |
* |
| 21 |
* Session items are high level objects that wrap underlying PipeWire objects |
| 22 |
* and manage them. For example, a session item may be managing a node, taking |
| 23 |
* responsibility for configuring the PortConfig and Format parameters of the |
| 24 |
* node. Or another may be managing links between two nodes. |
| 25 |
* |
| 26 |
* All the implementations are provided by modules and instantiated via the |
| 27 |
* WpSiFactory class. |
| 28 |
* |
| 29 |
* \gproperties |
| 30 |
* |
| 31 |
* \gproperty{id, guint, G_PARAM_READABLE, |
| 32 |
* The session item unique id} |
| 33 |
* |
| 34 |
* \gproperty{properties, WpProperties *, G_PARAM_READABLE, |
| 35 |
* The session item properties} |
| 36 |
*/ |
| 37 |
|
| 38 |
enum { |
| 39 |
STEP_ACTIVATE = WP_TRANSITION_STEP_CUSTOM_START, |
| 40 |
STEP_EXPORT, |
| 41 |
}; |
| 42 |
|
| 43 |
typedef struct _WpSessionItemPrivate WpSessionItemPrivate; |
| 44 |
struct _WpSessionItemPrivate |
| 45 |
{ |
| 46 |
WpProperties *properties; |
| 47 |
}; |
| 48 |
|
| 49 |
enum { |
| 50 |
PROP_0, |
| 51 |
PROP_PROPERTIES, |
| 52 |
}; |
| 53 |
|
| 54 |
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (WpSessionItem, wp_session_item, |
| 55 |
WP_TYPE_OBJECT) |
| 56 |
|
| 57 |
static void |
| 58 |
wp_session_item_init (WpSessionItem * self) |
| 59 |
{ |
| 60 |
WpSessionItemPrivate *priv = wp_session_item_get_instance_private (self); |
| 61 |
|
| 62 |
priv->properties = NULL; |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
wp_session_item_default_reset (WpSessionItem * self) |
| 67 |
{ |
| 68 |
WpSessionItemPrivate *priv = wp_session_item_get_instance_private (self); |
| 69 |
|
| 70 |
g_clear_pointer (&priv->properties, wp_properties_unref); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
wp_session_item_dispose (GObject * object) |
| 75 |
{ |
| 76 |
WpSessionItem * self = WP_SESSION_ITEM (object); |
| 77 |
|
| 78 |
wp_trace_object (self, "dispose"); |
| 79 |
|
| 80 |
wp_session_item_reset (self); |
| 81 |
|
| 82 |
G_OBJECT_CLASS (wp_session_item_parent_class)->dispose (object); |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
wp_session_item_get_gobject_property (GObject * object, guint property_id, |
| 87 |
GValue * value, GParamSpec * pspec) |
| 88 |
{ |
| 89 |
WpSessionItem *self = WP_SESSION_ITEM (object); |
| 90 |
|
| 91 |
switch (property_id) { |
| 92 |
case PROP_PROPERTIES: |
| 93 |
g_value_take_boxed (value, wp_session_item_get_properties (self)); |
| 94 |
break; |
| 95 |
default: |
| 96 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 97 |
break; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
static WpObjectFeatures |
| 102 |
session_item_default_get_supported_features (WpObject * self) |
| 103 |
{ |
| 104 |
return WP_SESSION_ITEM_FEATURE_ACTIVE | WP_SESSION_ITEM_FEATURE_EXPORTED; |
| 105 |
} |
| 106 |
|
| 107 |
static guint |
| 108 |
session_item_default_activate_get_next_step (WpObject * object, |
| 109 |
WpFeatureActivationTransition * t, guint step, WpObjectFeatures missing) |
| 110 |
{ |
| 111 |
switch (step) { |
| 112 |
case WP_TRANSITION_STEP_NONE: |
| 113 |
if (missing & WP_SESSION_ITEM_FEATURE_ACTIVE) |
| 114 |
return STEP_ACTIVATE; |
| 115 |
else |
| 116 |
if (missing & WP_SESSION_ITEM_FEATURE_EXPORTED) |
| 117 |
return STEP_EXPORT; |
| 118 |
else |
| 119 |
return WP_TRANSITION_STEP_NONE; |
| 120 |
|
| 121 |
case STEP_ACTIVATE: |
| 122 |
if (missing & WP_SESSION_ITEM_FEATURE_EXPORTED) |
| 123 |
return STEP_EXPORT; |
| 124 |
else |
| 125 |
return WP_TRANSITION_STEP_NONE; |
| 126 |
|
| 127 |
case STEP_EXPORT: |
| 128 |
return WP_TRANSITION_STEP_NONE; |
| 129 |
|
| 130 |
default: |
| 131 |
return WP_TRANSITION_STEP_ERROR; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
session_item_default_activate_execute_step (WpObject * object, |
| 137 |
WpFeatureActivationTransition * t, guint step, WpObjectFeatures missing) |
| 138 |
{ |
| 139 |
WpSessionItem *self = WP_SESSION_ITEM (object); |
| 140 |
WpTransition *transition = WP_TRANSITION (t); |
| 141 |
|
| 142 |
switch (step) { |
| 143 |
case STEP_ACTIVATE: |
| 144 |
if (!WP_SESSION_ITEM_GET_CLASS (self)->enable_active) { |
| 145 |
wp_transition_return_error (transition, |
| 146 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT, |
| 147 |
"session-item: virtual enable_active method is not defined")); |
| 148 |
return; |
| 149 |
} |
| 150 |
WP_SESSION_ITEM_GET_CLASS (self)->enable_active (self, transition); |
| 151 |
break; |
| 152 |
|
| 153 |
case STEP_EXPORT: |
| 154 |
if (!WP_SESSION_ITEM_GET_CLASS (self)->enable_exported) { |
| 155 |
wp_transition_return_error (transition, |
| 156 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT, |
| 157 |
"session-item: virtual enable_exported method is not defined")); |
| 158 |
return; |
| 159 |
} |
| 160 |
WP_SESSION_ITEM_GET_CLASS (self)->enable_exported (self, transition); |
| 161 |
break; |
| 162 |
|
| 163 |
case WP_TRANSITION_STEP_ERROR: |
| 164 |
break; |
| 165 |
|
| 166 |
default: |
| 167 |
g_return_if_reached (); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
static void |
| 172 |
session_item_default_deactivate (WpObject * object, WpObjectFeatures features) |
| 173 |
{ |
| 174 |
WpSessionItem *self = WP_SESSION_ITEM (object); |
| 175 |
guint current = wp_object_get_active_features (object); |
| 176 |
|
| 177 |
if (features & current & WP_SESSION_ITEM_FEATURE_ACTIVE) { |
| 178 |
g_return_if_fail (WP_SESSION_ITEM_GET_CLASS (self)->disable_active); |
| 179 |
WP_SESSION_ITEM_GET_CLASS (self)->disable_active (self); |
| 180 |
} |
| 181 |
|
| 182 |
if (features & current & WP_SESSION_ITEM_FEATURE_EXPORTED) { |
| 183 |
g_return_if_fail (WP_SESSION_ITEM_GET_CLASS (self)->disable_exported); |
| 184 |
WP_SESSION_ITEM_GET_CLASS (self)->disable_exported (self); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
static void |
| 189 |
wp_session_item_class_init (WpSessionItemClass * klass) |
| 190 |
{ |
| 191 |
WpObjectClass * wpobject_class = (WpObjectClass *) klass; |
| 192 |
GObjectClass * object_class = (GObjectClass *) klass; |
| 193 |
|
| 194 |
object_class->dispose = wp_session_item_dispose; |
| 195 |
object_class->get_property = wp_session_item_get_gobject_property; |
| 196 |
|
| 197 |
wpobject_class->get_supported_features = |
| 198 |
session_item_default_get_supported_features; |
| 199 |
wpobject_class->activate_get_next_step = |
| 200 |
session_item_default_activate_get_next_step; |
| 201 |
wpobject_class->activate_execute_step = |
| 202 |
session_item_default_activate_execute_step; |
| 203 |
wpobject_class->deactivate = session_item_default_deactivate; |
| 204 |
|
| 205 |
klass->reset = wp_session_item_default_reset; |
| 206 |
|
| 207 |
g_object_class_install_property (object_class, PROP_PROPERTIES, |
| 208 |
g_param_spec_boxed ("properties", "properties", |
| 209 |
"The session item properties", WP_TYPE_PROPERTIES, |
| 210 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 211 |
} |
| 212 |
|
| 213 |
/*! |
| 214 |
* \brief Resets the session item. |
| 215 |
* |
| 216 |
* This essentially removes the configuration and deactivates all active features. |
| 217 |
* |
| 218 |
* \ingroup wpsessionitem |
| 219 |
* \param self the session item |
| 220 |
*/ |
| 221 |
void |
| 222 |
wp_session_item_reset (WpSessionItem * self) |
| 223 |
{ |
| 224 |
g_return_if_fail (WP_IS_SESSION_ITEM (self)); |
| 225 |
g_return_if_fail (WP_SESSION_ITEM_GET_CLASS (self)->reset); |
| 226 |
|
| 227 |
return WP_SESSION_ITEM_GET_CLASS (self)->reset (self); |
| 228 |
} |
| 229 |
|
| 230 |
/*! |
| 231 |
* \brief Configures the session item with a set of properties |
| 232 |
* |
| 233 |
* \ingroup wpsessionitem |
| 234 |
* \param self the session item |
| 235 |
* \param props (transfer full): the properties used to configure the item |
| 236 |
* \returns TRUE on success, FALSE if the item could not be configured |
| 237 |
*/ |
| 238 |
gboolean |
| 239 |
wp_session_item_configure (WpSessionItem * self, WpProperties * props) |
| 240 |
{ |
| 241 |
g_return_val_if_fail (WP_IS_SESSION_ITEM (self), FALSE); |
| 242 |
g_return_val_if_fail (WP_SESSION_ITEM_GET_CLASS (self)->configure, |
| 243 |
FALSE); |
| 244 |
|
| 245 |
return WP_SESSION_ITEM_GET_CLASS (self)->configure (self, props); |
| 246 |
} |
| 247 |
|
| 248 |
/*! |
| 249 |
* \brief Checks if the session item is configured |
| 250 |
* |
| 251 |
* \ingroup wpsessionitem |
| 252 |
* \param self the session item |
| 253 |
* \returns TRUE if the item is configured, FALSE otherwise |
| 254 |
*/ |
| 255 |
gboolean |
| 256 |
wp_session_item_is_configured (WpSessionItem * self) |
| 257 |
{ |
| 258 |
WpSessionItemPrivate *priv = NULL; |
| 259 |
|
| 260 |
g_return_val_if_fail (WP_IS_SESSION_ITEM (self), FALSE); |
| 261 |
|
| 262 |
priv = wp_session_item_get_instance_private (self); |
| 263 |
return priv->properties != NULL; |
| 264 |
} |
| 265 |
|
| 266 |
/*! |
| 267 |
* \brief An associated proxy is a WpProxy subclass instance that |
| 268 |
* is somehow related to this item. |
| 269 |
* |
| 270 |
* \ingroup wpsessionitem |
| 271 |
* \param self the session item |
| 272 |
* \param proxy_type a WpProxy subclass GType |
| 273 |
* \returns (nullable) (transfer full) (type WpProxy): the associated proxy |
| 274 |
* of the specified @em proxy_type, or NULL if there is no association to |
| 275 |
* such a proxy |
| 276 |
*/ |
| 277 |
gpointer |
| 278 |
wp_session_item_get_associated_proxy (WpSessionItem * self, GType proxy_type) |
| 279 |
{ |
| 280 |
g_return_val_if_fail (WP_IS_SESSION_ITEM (self), NULL); |
| 281 |
g_return_val_if_fail (WP_SESSION_ITEM_GET_CLASS (self)->get_associated_proxy, |
| 282 |
NULL); |
| 283 |
g_return_val_if_fail (g_type_is_a (proxy_type, WP_TYPE_PROXY), NULL); |
| 284 |
|
| 285 |
return WP_SESSION_ITEM_GET_CLASS (self)->get_associated_proxy (self, proxy_type); |
| 286 |
} |
| 287 |
|
| 288 |
/*! |
| 289 |
* \brief Gets the bound id of a proxy associated with the session item |
| 290 |
* |
| 291 |
* \ingroup wpsessionitem |
| 292 |
* \param self the session item |
| 293 |
* \param proxy_type a WpProxy subclass GType |
| 294 |
* \returns the bound id of the associated proxy of the specified @em proxy_type, |
| 295 |
* or `SPA_ID_INVALID` if there is no association to such a proxy |
| 296 |
*/ |
| 297 |
guint32 |
| 298 |
wp_session_item_get_associated_proxy_id (WpSessionItem * self, GType proxy_type) |
| 299 |
{ |
| 300 |
g_autoptr (WpProxy) proxy = wp_session_item_get_associated_proxy (self, |
| 301 |
proxy_type); |
| 302 |
if (!proxy) |
| 303 |
return SPA_ID_INVALID; |
| 304 |
|
| 305 |
return wp_proxy_get_bound_id (proxy); |
| 306 |
} |
| 307 |
|
| 308 |
/*! |
| 309 |
* \brief Registers the session item to its associated core |
| 310 |
* |
| 311 |
* \ingroup wpsessionitem |
| 312 |
* \param self (transfer full): the session item |
| 313 |
*/ |
| 314 |
void |
| 315 |
wp_session_item_register (WpSessionItem * self) |
| 316 |
{ |
| 317 |
g_autoptr (WpCore) core = NULL; |
| 318 |
|
| 319 |
g_return_if_fail (WP_IS_SESSION_ITEM (self)); |
| 320 |
|
| 321 |
core = wp_object_get_core (WP_OBJECT (self)); |
| 322 |
wp_core_register_object (core, self); |
| 323 |
} |
| 324 |
|
| 325 |
/*! |
| 326 |
* \brief Removes the session item from its associated core |
| 327 |
* |
| 328 |
* \ingroup wpsessionitem |
| 329 |
* \param self (transfer none): the session item |
| 330 |
*/ |
| 331 |
void |
| 332 |
wp_session_item_remove (WpSessionItem * self) |
| 333 |
{ |
| 334 |
g_autoptr (WpCore) core = NULL; |
| 335 |
|
| 336 |
g_return_if_fail (WP_IS_SESSION_ITEM (self)); |
| 337 |
|
| 338 |
core = wp_object_get_core (WP_OBJECT (self)); |
| 339 |
wp_core_remove_object (core, self); |
| 340 |
} |
| 341 |
|
| 342 |
/*! |
| 343 |
* \brief Gets the properties of a session item. |
| 344 |
* |
| 345 |
* \ingroup wpsessionitem |
| 346 |
* \param self the session item |
| 347 |
* \returns (transfer full): the item's properties. |
| 348 |
*/ |
| 349 |
WpProperties * |
| 350 |
wp_session_item_get_properties (WpSessionItem * self) |
| 351 |
{ |
| 352 |
WpSessionItemPrivate *priv = NULL; |
| 353 |
|
| 354 |
g_return_val_if_fail (WP_IS_SESSION_ITEM (self), NULL); |
| 355 |
|
| 356 |
priv = wp_session_item_get_instance_private (self); |
| 357 |
return priv->properties ? wp_properties_ref (priv->properties) : NULL; |
| 358 |
} |
| 359 |
|
| 360 |
/*! |
| 361 |
* \brief Looks up a named session item property value for a given key. |
| 362 |
* |
| 363 |
* \ingroup wpsessionitem |
| 364 |
* \param self the session item |
| 365 |
* \param key the property key |
| 366 |
* \returns the item property value for the given key. |
| 367 |
*/ |
| 368 |
const gchar * |
| 369 |
wp_session_item_get_property (WpSessionItem * self, const gchar *key) |
| 370 |
{ |
| 371 |
WpSessionItemPrivate *priv = NULL; |
| 372 |
|
| 373 |
g_return_val_if_fail (WP_IS_SESSION_ITEM (self), NULL); |
| 374 |
|
| 375 |
priv = wp_session_item_get_instance_private (self); |
| 376 |
return priv->properties ? wp_properties_get (priv->properties, key) : NULL; |
| 377 |
} |
| 378 |
|
| 379 |
/*! |
| 380 |
* \brief Sets the item's properties. |
| 381 |
* |
| 382 |
* This should only be done by sub-classes after the configuration has been done. |
| 383 |
* |
| 384 |
* \ingroup wpsessionitem |
| 385 |
* \param self the session item |
| 386 |
* \param props (transfer full): the new properties to set |
| 387 |
*/ |
| 388 |
void |
| 389 |
wp_session_item_set_properties (WpSessionItem * self, |
| 390 |
WpProperties *props) |
| 391 |
{ |
| 392 |
WpSessionItemPrivate *priv = NULL; |
| 393 |
|
| 394 |
g_return_if_fail (WP_IS_SESSION_ITEM (self)); |
| 395 |
|
| 396 |
priv = wp_session_item_get_instance_private (self); |
| 397 |
g_clear_pointer (&priv->properties, wp_properties_unref); |
| 398 |
priv->properties = wp_properties_ensure_unique_owner (props); |
| 399 |
} |
| 400 |
|
| 401 |
static gboolean |
| 402 |
on_session_item_proxy_destroyed_deferred (WpSessionItem * item) |
| 403 |
{ |
| 404 |
wp_info_object (item, "destroying session item upon request by the server"); |
| 405 |
wp_object_deactivate (WP_OBJECT (item), WP_SESSION_ITEM_FEATURE_EXPORTED); |
| 406 |
return G_SOURCE_REMOVE; |
| 407 |
} |
| 408 |
|
| 409 |
/*! |
| 410 |
* \brief Helper callback for sub-classes that defers and unexports |
| 411 |
* the session item. |
| 412 |
* |
| 413 |
* Only meant to be used when the pipewire proxy destroyed signal is triggered. |
| 414 |
* |
| 415 |
* \ingroup wpsessionitem |
| 416 |
* \param proxy the proxy that was destroyed by the server |
| 417 |
* \param item the associated session item |
| 418 |
*/ |
| 419 |
void |
| 420 |
wp_session_item_handle_proxy_destroyed (WpProxy * proxy, WpSessionItem * item) |
| 421 |
{ |
| 422 |
g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (proxy)); |
| 423 |
if (core) |
| 424 |
wp_core_idle_add_closure (core, NULL, g_cclosure_new_object ( |
| 425 |
G_CALLBACK (on_session_item_proxy_destroyed_deferred), |
| 426 |
G_OBJECT (item))); |
| 427 |
} |