| 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 "device.h" |
| 10 |
#include "node.h" |
| 11 |
#include "core.h" |
| 12 |
#include "log.h" |
| 13 |
#include "error.h" |
| 14 |
#include "private/pipewire-object-mixin.h" |
| 15 |
|
| 16 |
#include <pipewire/impl.h> |
| 17 |
#include <spa/debug/types.h> |
| 18 |
#include <spa/monitor/device.h> |
| 19 |
#include <spa/utils/result.h> |
| 20 |
|
| 21 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-device") |
| 22 |
|
| 23 |
/*! \defgroup wpdevice WpDevice */ |
| 24 |
/*! |
| 25 |
* \struct WpDevice |
| 26 |
* |
| 27 |
* The WpDevice class allows accessing the properties and methods of a |
| 28 |
* PipeWire device object (`struct pw_device`). |
| 29 |
* |
| 30 |
* A WpDevice is constructed internally when a new device appears on the |
| 31 |
* PipeWire registry and it is made available through the WpObjectManager API. |
| 32 |
* Alternatively, a WpDevice can also be constructed using |
| 33 |
* wp_device_new_from_factory(), which creates a new device object |
| 34 |
* on the remote PipeWire server by calling into a factory. |
| 35 |
* |
| 36 |
*/ |
| 37 |
|
| 38 |
struct _WpDevice |
| 39 |
{ |
| 40 |
WpGlobalProxy parent; |
| 41 |
}; |
| 42 |
|
| 43 |
static void wp_device_pw_object_mixin_priv_interface_init ( |
| 44 |
WpPwObjectMixinPrivInterface * iface); |
| 45 |
|
| 46 |
G_DEFINE_TYPE_WITH_CODE (WpDevice, wp_device, WP_TYPE_GLOBAL_PROXY, |
| 47 |
G_IMPLEMENT_INTERFACE (WP_TYPE_PIPEWIRE_OBJECT, |
| 48 |
wp_pw_object_mixin_object_interface_init) |
| 49 |
G_IMPLEMENT_INTERFACE (WP_TYPE_PW_OBJECT_MIXIN_PRIV, |
| 50 |
wp_device_pw_object_mixin_priv_interface_init)); |
| 51 |
|
| 52 |
static void |
| 53 |
wp_device_init (WpDevice * self) |
| 54 |
{ |
| 55 |
} |
| 56 |
|
| 57 |
static void |
| 58 |
wp_device_activate_execute_step (WpObject * object, |
| 59 |
WpFeatureActivationTransition * transition, guint step, |
| 60 |
WpObjectFeatures missing) |
| 61 |
{ |
| 62 |
switch (step) { |
| 63 |
case WP_PW_OBJECT_MIXIN_STEP_BIND: |
| 64 |
case WP_TRANSITION_STEP_ERROR: |
| 65 |
/* base class can handle BIND and ERROR */ |
| 66 |
WP_OBJECT_CLASS (wp_device_parent_class)-> |
| 67 |
activate_execute_step (object, transition, step, missing); |
| 68 |
break; |
| 69 |
case WP_PW_OBJECT_MIXIN_STEP_WAIT_INFO: |
| 70 |
/* just wait, info will be emitted anyway after binding */ |
| 71 |
break; |
| 72 |
case WP_PW_OBJECT_MIXIN_STEP_CACHE_PARAMS: |
| 73 |
wp_pw_object_mixin_cache_params (object, missing); |
| 74 |
break; |
| 75 |
default: |
| 76 |
g_assert_not_reached (); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
static void |
| 81 |
wp_device_deactivate (WpObject * object, WpObjectFeatures features) |
| 82 |
{ |
| 83 |
wp_pw_object_mixin_deactivate (object, features); |
| 84 |
WP_OBJECT_CLASS (wp_device_parent_class)->deactivate (object, features); |
| 85 |
} |
| 86 |
|
| 87 |
static const struct pw_device_events device_events = { |
| 88 |
PW_VERSION_DEVICE_EVENTS, |
| 89 |
.info = (HandleEventInfoFunc(device)) wp_pw_object_mixin_handle_event_info, |
| 90 |
.param = wp_pw_object_mixin_handle_event_param, |
| 91 |
}; |
| 92 |
|
| 93 |
static void |
| 94 |
wp_device_pw_proxy_created (WpProxy * proxy, struct pw_proxy * pw_proxy) |
| 95 |
{ |
| 96 |
wp_pw_object_mixin_handle_pw_proxy_created (proxy, pw_proxy, |
| 97 |
device, &device_events); |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
wp_device_pw_proxy_destroyed (WpProxy * proxy) |
| 102 |
{ |
| 103 |
wp_pw_object_mixin_handle_pw_proxy_destroyed (proxy); |
| 104 |
|
| 105 |
WP_PROXY_CLASS (wp_device_parent_class)->pw_proxy_destroyed (proxy); |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
wp_device_class_init (WpDeviceClass * klass) |
| 110 |
{ |
| 111 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 112 |
WpObjectClass *wpobject_class = (WpObjectClass *) klass; |
| 113 |
WpProxyClass *proxy_class = (WpProxyClass *) klass; |
| 114 |
|
| 115 |
object_class->get_property = wp_pw_object_mixin_get_property; |
| 116 |
|
| 117 |
wpobject_class->get_supported_features = |
| 118 |
wp_pw_object_mixin_get_supported_features; |
| 119 |
wpobject_class->activate_get_next_step = |
| 120 |
wp_pw_object_mixin_activate_get_next_step; |
| 121 |
wpobject_class->activate_execute_step = wp_device_activate_execute_step; |
| 122 |
wpobject_class->deactivate = wp_device_deactivate; |
| 123 |
|
| 124 |
proxy_class->pw_iface_type = PW_TYPE_INTERFACE_Device; |
| 125 |
proxy_class->pw_iface_version = PW_VERSION_DEVICE; |
| 126 |
proxy_class->pw_proxy_created = wp_device_pw_proxy_created; |
| 127 |
proxy_class->pw_proxy_destroyed = wp_device_pw_proxy_destroyed; |
| 128 |
|
| 129 |
wp_pw_object_mixin_class_override_properties (object_class); |
| 130 |
} |
| 131 |
|
| 132 |
static gint |
| 133 |
wp_device_enum_params (gpointer instance, guint32 id, |
| 134 |
guint32 start, guint32 num, WpSpaPod *filter) |
| 135 |
{ |
| 136 |
WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance); |
| 137 |
return pw_device_enum_params (d->iface, 0, id, start, num, |
| 138 |
filter ? wp_spa_pod_get_spa_pod (filter) : NULL); |
| 139 |
} |
| 140 |
|
| 141 |
static gint |
| 142 |
wp_device_set_param (gpointer instance, guint32 id, guint32 flags, |
| 143 |
WpSpaPod * param) |
| 144 |
{ |
| 145 |
WpPwObjectMixinData *d = wp_pw_object_mixin_get_data (instance); |
| 146 |
g_autoptr (WpSpaPod) p = param; |
| 147 |
return pw_device_set_param (d->iface, id, flags, |
| 148 |
wp_spa_pod_get_spa_pod (p)); |
| 149 |
} |
| 150 |
|
| 151 |
static void |
| 152 |
wp_device_pw_object_mixin_priv_interface_init ( |
| 153 |
WpPwObjectMixinPrivInterface * iface) |
| 154 |
{ |
| 155 |
wp_pw_object_mixin_priv_interface_info_init (iface, device, DEVICE); |
| 156 |
iface->enum_params = wp_device_enum_params; |
| 157 |
iface->set_param = wp_device_set_param; |
| 158 |
} |
| 159 |
|
| 160 |
/*! |
| 161 |
* \brief Constructs a device on the PipeWire server by asking the remote |
| 162 |
* factory \a factory_name to create it. |
| 163 |
* |
| 164 |
* Because of the nature of the PipeWire protocol, this operation completes |
| 165 |
* asynchronously at some point in the future. In order to find out when |
| 166 |
* this is done, you should call wp_object_activate(), requesting at least |
| 167 |
* %WP_PROXY_FEATURE_BOUND. When this feature is ready, the device is ready for |
| 168 |
* use on the server. If the device cannot be created, this activation operation |
| 169 |
* will fail. |
| 170 |
* |
| 171 |
* \ingroup wpdevice |
| 172 |
* \param core the wireplumber core |
| 173 |
* \param factory_name the pipewire factory name to construct the device |
| 174 |
* \param properties (nullable) (transfer full): the properties to pass to the |
| 175 |
* factory |
| 176 |
* \returns (nullable) (transfer full): the new device or %NULL if the core |
| 177 |
* is not connected and therefore the device cannot be created |
| 178 |
*/ |
| 179 |
|
| 180 |
WpDevice * |
| 181 |
wp_device_new_from_factory (WpCore * core, |
| 182 |
const gchar * factory_name, WpProperties * properties) |
| 183 |
{ |
| 184 |
g_autoptr (WpProperties) props = properties; |
| 185 |
return g_object_new (WP_TYPE_DEVICE, |
| 186 |
"core", core, |
| 187 |
"factory-name", factory_name, |
| 188 |
"global-properties", props, |
| 189 |
NULL); |
| 190 |
} |
| 191 |
|
| 192 |
/*! \defgroup wpspadevice WpSpaDevice */ |
| 193 |
|
| 194 |
struct _WpSpaDevice |
| 195 |
{ |
| 196 |
WpProxy parent; |
| 197 |
struct spa_handle *handle; |
| 198 |
struct spa_device *device; |
| 199 |
struct spa_hook listener; |
| 200 |
WpProperties *properties; |
| 201 |
GPtrArray *managed_objs; |
| 202 |
GPtrArray *pending_obj_config; |
| 203 |
}; |
| 204 |
|
| 205 |
enum { |
| 206 |
PROP_0, |
| 207 |
PROP_SPA_DEVICE_HANDLE, |
| 208 |
PROP_PROPERTIES, |
| 209 |
}; |
| 210 |
|
| 211 |
enum |
| 212 |
{ |
| 213 |
SIGNAL_CREATE_OBJECT, |
| 214 |
SIGNAL_OBJECT_REMOVED, |
| 215 |
SPA_DEVICE_LAST_SIGNAL, |
| 216 |
}; |
| 217 |
|
| 218 |
static guint spa_device_signals[SPA_DEVICE_LAST_SIGNAL] = { 0 }; |
| 219 |
|
| 220 |
G_DEFINE_TYPE (WpSpaDevice, wp_spa_device, WP_TYPE_PROXY) |
| 221 |
|
| 222 |
static void |
| 223 |
object_unref_safe (gpointer object) |
| 224 |
{ |
| 225 |
if (object) |
| 226 |
g_object_unref (object); |
| 227 |
} |
| 228 |
|
| 229 |
static void |
| 230 |
pod_unref_safe (gpointer object) |
| 231 |
{ |
| 232 |
if (object) |
| 233 |
wp_spa_pod_unref (object); |
| 234 |
} |
| 235 |
|
| 236 |
static void |
| 237 |
wp_spa_device_init (WpSpaDevice * self) |
| 238 |
{ |
| 239 |
self->properties = wp_properties_new_empty (); |
| 240 |
self->managed_objs = g_ptr_array_new_with_free_func (object_unref_safe); |
| 241 |
self->pending_obj_config = g_ptr_array_new_with_free_func (pod_unref_safe); |
| 242 |
} |
| 243 |
|
| 244 |
static void |
| 245 |
wp_spa_device_constructed (GObject *object) |
| 246 |
{ |
| 247 |
WpSpaDevice *self = WP_SPA_DEVICE (object); |
| 248 |
gint res; |
| 249 |
|
| 250 |
g_return_if_fail (self->handle); |
| 251 |
|
| 252 |
/* Get the handle interface */ |
| 253 |
res = spa_handle_get_interface (self->handle, SPA_TYPE_INTERFACE_Device, |
| 254 |
(gpointer *) &self->device); |
| 255 |
if (res < 0) { |
| 256 |
wp_warning_object (self, |
| 257 |
"Could not get device interface from SPA handle: %s", |
| 258 |
spa_strerror (res)); |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
G_OBJECT_CLASS (wp_spa_device_parent_class)->constructed (object); |
| 263 |
} |
| 264 |
|
| 265 |
static void |
| 266 |
wp_spa_device_finalize (GObject * object) |
| 267 |
{ |
| 268 |
WpSpaDevice *self = WP_SPA_DEVICE (object); |
| 269 |
|
| 270 |
self->device = NULL; |
| 271 |
g_clear_pointer (&self->handle, pw_unload_spa_handle); |
| 272 |
g_clear_pointer (&self->properties, wp_properties_unref); |
| 273 |
g_clear_pointer (&self->managed_objs, g_ptr_array_unref); |
| 274 |
g_clear_pointer (&self->pending_obj_config, g_ptr_array_unref); |
| 275 |
|
| 276 |
G_OBJECT_CLASS (wp_spa_device_parent_class)->finalize (object); |
| 277 |
} |
| 278 |
|
| 279 |
static void |
| 280 |
wp_spa_device_set_property (GObject * object, guint property_id, |
| 281 |
const GValue * value, GParamSpec * pspec) |
| 282 |
{ |
| 283 |
WpSpaDevice *self = WP_SPA_DEVICE (object); |
| 284 |
|
| 285 |
switch (property_id) { |
| 286 |
case PROP_SPA_DEVICE_HANDLE: |
| 287 |
self->handle = g_value_get_pointer (value); |
| 288 |
break; |
| 289 |
case PROP_PROPERTIES: { |
| 290 |
WpProperties *p = g_value_get_boxed (value); |
| 291 |
if (p) |
| 292 |
wp_properties_update (self->properties, p); |
| 293 |
break; |
| 294 |
} |
| 295 |
default: |
| 296 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 297 |
break; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
static void |
| 302 |
wp_spa_device_get_property (GObject * object, guint property_id, GValue * value, |
| 303 |
GParamSpec * pspec) |
| 304 |
{ |
| 305 |
WpSpaDevice *self = WP_SPA_DEVICE (object); |
| 306 |
|
| 307 |
switch (property_id) { |
| 308 |
case PROP_SPA_DEVICE_HANDLE: |
| 309 |
g_value_set_pointer (value, self->handle); |
| 310 |
break; |
| 311 |
case PROP_PROPERTIES: |
| 312 |
g_value_take_boxed (value, wp_properties_ref (self->properties)); |
| 313 |
break; |
| 314 |
default: |
| 315 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 316 |
break; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
static void |
| 321 |
spa_device_event_info (void *data, const struct spa_device_info *info) |
| 322 |
{ |
| 323 |
WpSpaDevice *self = WP_SPA_DEVICE (data); |
| 324 |
|
| 325 |
/* |
| 326 |
* This is emitted synchronously at the time we add the listener and |
| 327 |
* before object_info is emitted. It gives us additional properties |
| 328 |
* about the device, like the "api.alsa.card.*" ones that are not |
| 329 |
* set by the monitor |
| 330 |
*/ |
| 331 |
if (info->change_mask & SPA_DEVICE_CHANGE_MASK_PROPS) |
| 332 |
wp_properties_update_from_dict (self->properties, info->props); |
| 333 |
} |
| 334 |
|
| 335 |
static WpSpaPod * |
| 336 |
pending_obj_config_pop (WpSpaDevice *self, guint32 id) |
| 337 |
{ |
| 338 |
if (id < self->pending_obj_config->len) |
| 339 |
return g_steal_pointer (&g_ptr_array_index (self->pending_obj_config, id)); |
| 340 |
return NULL; |
| 341 |
} |
| 342 |
|
| 343 |
static void |
| 344 |
pending_obj_config_set (WpSpaDevice *self, guint32 id, WpSpaPod *props) |
| 345 |
{ |
| 346 |
if (id >= self->pending_obj_config->len) |
| 347 |
g_ptr_array_set_size (self->pending_obj_config, id + 1); |
| 348 |
|
| 349 |
gpointer *ptr = &g_ptr_array_index (self->pending_obj_config, id); |
| 350 |
pod_unref_safe (*ptr); |
| 351 |
*ptr = props; |
| 352 |
} |
| 353 |
|
| 354 |
static void |
| 355 |
append_props (WpSpaPodBuilder *b, WpSpaPod *props, GHashTable *used) |
| 356 |
{ |
| 357 |
g_autoptr (WpIterator) it = wp_spa_pod_new_iterator (props); |
| 358 |
GValue next = G_VALUE_INIT; |
| 359 |
|
| 360 |
for (; wp_iterator_next (it, &next); g_value_unset (&next)) { |
| 361 |
WpSpaPod *p = g_value_get_boxed (&next); |
| 362 |
const char *key; |
| 363 |
g_autoptr (WpSpaPod) value = NULL; |
| 364 |
|
| 365 |
if (!wp_spa_pod_get_property (p, &key, &value)) |
| 366 |
continue; |
| 367 |
if (g_hash_table_contains(used, key)) |
| 368 |
continue; |
| 369 |
|
| 370 |
wp_spa_pod_builder_add_property (b, key); |
| 371 |
wp_spa_pod_builder_add_pod (b, value); |
| 372 |
|
| 373 |
g_hash_table_add (used, (gpointer) g_strdup (key)); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
static WpSpaPod * |
| 378 |
merge_props (WpSpaPod *old_props, WpSpaPod *new_props) |
| 379 |
{ |
| 380 |
g_autoptr (GHashTable) used = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); |
| 381 |
g_autoptr (WpSpaPodBuilder) b = wp_spa_pod_builder_new_object ( |
| 382 |
"Spa:Pod:Object:Param:Props", "Props"); |
| 383 |
|
| 384 |
if (new_props) { |
| 385 |
append_props (b, new_props, used); |
| 386 |
wp_spa_pod_unref (new_props); |
| 387 |
} |
| 388 |
if (old_props) { |
| 389 |
append_props (b, old_props, used); |
| 390 |
wp_spa_pod_unref (old_props); |
| 391 |
} |
| 392 |
|
| 393 |
return wp_spa_pod_builder_end (b); |
| 394 |
} |
| 395 |
|
| 396 |
static void |
| 397 |
spa_device_event_event (void *data, const struct spa_event *event) |
| 398 |
{ |
| 399 |
WpSpaDevice *self = WP_SPA_DEVICE (data); |
| 400 |
g_autoptr (WpSpaPod) pod = |
| 401 |
wp_spa_pod_new_wrap_const ((const struct spa_pod *) event); |
| 402 |
guint32 id = -1; |
| 403 |
const gchar *type = NULL; |
| 404 |
g_autoptr (WpSpaPod) props = NULL; |
| 405 |
g_autoptr (GObject) child = NULL; |
| 406 |
|
| 407 |
wp_trace_boxed (WP_TYPE_SPA_POD, pod, "device event"); |
| 408 |
|
| 409 |
if (wp_spa_pod_get_object (pod, &type, |
| 410 |
"Object", "i", &id, |
| 411 |
"Props", "?P", &props, |
| 412 |
NULL)) |
| 413 |
child = wp_spa_device_get_managed_object (self, id); |
| 414 |
|
| 415 |
if (!g_strcmp0 (type, "ObjectConfig") && props) { |
| 416 |
if (child && WP_IS_PIPEWIRE_OBJECT (child)) { |
| 417 |
wp_pipewire_object_set_param (WP_PIPEWIRE_OBJECT (child), "Props", 0, |
| 418 |
g_steal_pointer (&props)); |
| 419 |
} else if (!child) { |
| 420 |
/* Save Props set on ids pending for a managed object */ |
| 421 |
WpSpaPod *pending_props = pending_obj_config_pop (self, id); |
| 422 |
if (pending_props) { |
| 423 |
pending_props = merge_props (pending_props, g_steal_pointer(&props)); |
| 424 |
pending_obj_config_set (self, id, pending_props); |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
static void |
| 431 |
spa_device_event_object_info (void *data, uint32_t id, |
| 432 |
const struct spa_device_object_info *info) |
| 433 |
{ |
| 434 |
WpSpaDevice *self = WP_SPA_DEVICE (data); |
| 435 |
|
| 436 |
if (info) { |
| 437 |
const gchar *type; |
| 438 |
g_autoptr (WpProperties) props = NULL; |
| 439 |
|
| 440 |
type = spa_debug_type_short_name (info->type); |
| 441 |
props = wp_properties_new_copy_dict (info->props); |
| 442 |
|
| 443 |
wp_debug_object (self, "object info: id:%u type:%s factory:%s", |
| 444 |
id, type, info->factory_name); |
| 445 |
|
| 446 |
if (id < self->managed_objs->len && |
| 447 |
g_ptr_array_index (self->managed_objs, id) != NULL) { |
| 448 |
wp_debug_object (self, "object already exists, removing"); |
| 449 |
g_signal_emit (self, spa_device_signals[SIGNAL_OBJECT_REMOVED], 0, id); |
| 450 |
wp_spa_device_store_managed_object (self, id, NULL); |
| 451 |
} |
| 452 |
|
| 453 |
g_signal_emit (self, spa_device_signals[SIGNAL_CREATE_OBJECT], 0, |
| 454 |
id, type, info->factory_name, props); |
| 455 |
} |
| 456 |
else { |
| 457 |
wp_debug_object (self, "object removed: id:%u", id); |
| 458 |
g_signal_emit (self, spa_device_signals[SIGNAL_OBJECT_REMOVED], 0, id); |
| 459 |
wp_spa_device_store_managed_object (self, id, NULL); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
static const struct spa_device_events spa_device_events = { |
| 464 |
SPA_VERSION_DEVICE_EVENTS, |
| 465 |
.info = spa_device_event_info, |
| 466 |
.event = spa_device_event_event, |
| 467 |
.object_info = spa_device_event_object_info, |
| 468 |
}; |
| 469 |
|
| 470 |
static WpObjectFeatures |
| 471 |
wp_spa_device_get_supported_features (WpObject * object) |
| 472 |
{ |
| 473 |
return WP_PROXY_FEATURE_BOUND | WP_SPA_DEVICE_FEATURE_ENABLED; |
| 474 |
} |
| 475 |
|
| 476 |
enum { |
| 477 |
STEP_EXPORT = WP_TRANSITION_STEP_CUSTOM_START, |
| 478 |
STEP_ADD_DEVICE_LISTENER, |
| 479 |
}; |
| 480 |
|
| 481 |
static guint |
| 482 |
wp_spa_device_activate_get_next_step (WpObject * object, |
| 483 |
WpFeatureActivationTransition * transition, guint step, |
| 484 |
WpObjectFeatures missing) |
| 485 |
{ |
| 486 |
if (missing & WP_PROXY_FEATURE_BOUND) |
| 487 |
return STEP_EXPORT; |
| 488 |
else if (missing & WP_SPA_DEVICE_FEATURE_ENABLED) |
| 489 |
return STEP_ADD_DEVICE_LISTENER; |
| 490 |
else |
| 491 |
return WP_TRANSITION_STEP_NONE; |
| 492 |
} |
| 493 |
|
| 494 |
static void |
| 495 |
wp_spa_device_activate_execute_step (WpObject * object, |
| 496 |
WpFeatureActivationTransition * transition, guint step, |
| 497 |
WpObjectFeatures missing) |
| 498 |
{ |
| 499 |
WpSpaDevice *self = WP_SPA_DEVICE (object); |
| 500 |
|
| 501 |
switch (step) { |
| 502 |
case STEP_EXPORT: { |
| 503 |
g_autoptr (WpCore) core = wp_object_get_core (object); |
| 504 |
struct pw_core *pw_core = wp_core_get_pw_core (core); |
| 505 |
g_return_if_fail (pw_core); |
| 506 |
|
| 507 |
wp_proxy_set_pw_proxy (WP_PROXY (self), |
| 508 |
pw_core_export (pw_core, SPA_TYPE_INTERFACE_Device, |
| 509 |
wp_properties_peek_dict (self->properties), |
| 510 |
self->device, 0)); |
| 511 |
break; |
| 512 |
} |
| 513 |
case STEP_ADD_DEVICE_LISTENER: { |
| 514 |
gint res = spa_device_add_listener (self->device, &self->listener, |
| 515 |
&spa_device_events, self); |
| 516 |
if (res < 0) |
| 517 |
wp_transition_return_error (WP_TRANSITION (transition), |
| 518 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 519 |
"failed to activate device: %s", spa_strerror (res))); |
| 520 |
else |
| 521 |
wp_object_update_features (object, WP_SPA_DEVICE_FEATURE_ENABLED, 0); |
| 522 |
break; |
| 523 |
} |
| 524 |
case WP_TRANSITION_STEP_ERROR: |
| 525 |
break; |
| 526 |
default: |
| 527 |
g_assert_not_reached (); |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
static void |
| 532 |
wp_spa_device_deactivate (WpObject * object, WpObjectFeatures features) |
| 533 |
{ |
| 534 |
WP_OBJECT_CLASS (wp_spa_device_parent_class)->deactivate (object, features); |
| 535 |
|
| 536 |
if (features & WP_SPA_DEVICE_FEATURE_ENABLED) { |
| 537 |
WpSpaDevice *self = WP_SPA_DEVICE (object); |
| 538 |
spa_hook_remove (&self->listener); |
| 539 |
g_ptr_array_set_size (self->managed_objs, 0); |
| 540 |
g_ptr_array_set_size (self->pending_obj_config, 0); |
| 541 |
wp_object_update_features (object, 0, WP_SPA_DEVICE_FEATURE_ENABLED); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
/*! |
| 546 |
* \struct WpSpaDevice |
| 547 |
* |
| 548 |
* A WpSpaDevice allows running a `spa_device` object locally, |
| 549 |
* loading the implementation from a SPA factory. This is useful to run device |
| 550 |
* monitors inside the session manager and have control over creating the |
| 551 |
* actual nodes that the `spa_device` requests to create. |
| 552 |
* |
| 553 |
* To enable the spa device, call wp_object_activate() requesting |
| 554 |
* WP_SPA_DEVICE_FEATURE_ENABLED. |
| 555 |
* |
| 556 |
* For actual devices (not device monitors) it also possible and desirable |
| 557 |
* to export the device to PipeWire, which can be done by requesting |
| 558 |
* WP_PROXY_FEATURE_BOUND from wp_object_activate(). When exporting, the |
| 559 |
* export should be done before enabling the device, by requesting both |
| 560 |
* features at the same time. |
| 561 |
* |
| 562 |
* \gproperties |
| 563 |
* |
| 564 |
* \gproperty{properties, WpProperties *, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY, |
| 565 |
* Properties of the spa device} |
| 566 |
* |
| 567 |
* \gproperty{spa-device-handle, gpointer, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY, |
| 568 |
* The spa device handle} |
| 569 |
* |
| 570 |
* \gsignals |
| 571 |
* |
| 572 |
* \par create-object |
| 573 |
* \parblock |
| 574 |
* \code |
| 575 |
* void |
| 576 |
* create_object_callback (WpSpaDevice * self, |
| 577 |
* guint id, |
| 578 |
* gchar * type, |
| 579 |
* gchar * factory, |
| 580 |
* WpProperties * properties, |
| 581 |
* gpointer user_data) |
| 582 |
* \endcode |
| 583 |
* |
| 584 |
* This signal is emitted when the device is creating a managed object |
| 585 |
* The handler is expected to actually construct the object using the requested |
| 586 |
* SPA factory and with the given properties. The handler should then store the |
| 587 |
* object with wp_spa_device_store_managed_object. The WpSpaDevice will later |
| 588 |
* unref the reference stored by this function when the managed object is to be |
| 589 |
* destroyed. |
| 590 |
* |
| 591 |
* Parameters: |
| 592 |
* - `id` - the id of the managed object |
| 593 |
* - `type` - the SPA type that the managed object should have |
| 594 |
* - `factory` - the name of the SPA factory to use to construct the managed object |
| 595 |
* - `properties` - additional properties that the managed object should have |
| 596 |
* |
| 597 |
* Flags: G_SIGNAL_RUN_FIRST |
| 598 |
* \endparblock |
| 599 |
* |
| 600 |
* \par object-removed |
| 601 |
* \parblock |
| 602 |
* \code |
| 603 |
* void |
| 604 |
* object_removed_callback (WpSpaDevice * self, |
| 605 |
* guint id, |
| 606 |
* gpointer user_data) |
| 607 |
* \endcode |
| 608 |
* |
| 609 |
* This signal is emitted when the device has deleted a managed object. |
| 610 |
* The handler may optionally release additional resources associated with this |
| 611 |
* object. |
| 612 |
* |
| 613 |
* It is not necessary to call wp_spa_device_store_managed_object() to remove |
| 614 |
* the managed object, as this is done internally after this signal is fired. |
| 615 |
* |
| 616 |
* Parameters: |
| 617 |
* - `id` - the id of the managed object |
| 618 |
* |
| 619 |
* Flags: G_SIGNAL_RUN_FIRST |
| 620 |
* \endparblock |
| 621 |
*/ |
| 622 |
static void |
| 623 |
wp_spa_device_class_init (WpSpaDeviceClass * klass) |
| 624 |
{ |
| 625 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 626 |
WpObjectClass *wpobject_class = (WpObjectClass *) klass; |
| 627 |
|
| 628 |
object_class->constructed = wp_spa_device_constructed; |
| 629 |
object_class->finalize = wp_spa_device_finalize; |
| 630 |
object_class->set_property = wp_spa_device_set_property; |
| 631 |
object_class->get_property = wp_spa_device_get_property; |
| 632 |
|
| 633 |
wpobject_class->get_supported_features = wp_spa_device_get_supported_features; |
| 634 |
wpobject_class->activate_get_next_step = wp_spa_device_activate_get_next_step; |
| 635 |
wpobject_class->activate_execute_step = wp_spa_device_activate_execute_step; |
| 636 |
wpobject_class->deactivate = wp_spa_device_deactivate; |
| 637 |
|
| 638 |
g_object_class_install_property (object_class, PROP_SPA_DEVICE_HANDLE, |
| 639 |
g_param_spec_pointer ("spa-device-handle", "spa-device-handle", |
| 640 |
"The spa device handle", |
| 641 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 642 |
|
| 643 |
g_object_class_install_property (object_class, PROP_PROPERTIES, |
| 644 |
g_param_spec_boxed ("properties", "properties", |
| 645 |
"Properties of the device", WP_TYPE_PROPERTIES, |
| 646 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 647 |
|
| 648 |
spa_device_signals[SIGNAL_CREATE_OBJECT] = g_signal_new ( |
| 649 |
"create-object", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 650 |
0, NULL, NULL, NULL, G_TYPE_NONE, 4, G_TYPE_UINT, G_TYPE_STRING, |
| 651 |
G_TYPE_STRING, WP_TYPE_PROPERTIES); |
| 652 |
|
| 653 |
spa_device_signals[SIGNAL_OBJECT_REMOVED] = g_signal_new ( |
| 654 |
"object-removed", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 655 |
0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_UINT); |
| 656 |
} |
| 657 |
|
| 658 |
/*! |
| 659 |
* \brief Constructs an SPA Device object from an existing device handle. |
| 660 |
* |
| 661 |
* \ingroup wpspadevice |
| 662 |
* \param core the wireplumber core |
| 663 |
* \param spa_device_handle the spa device handle |
| 664 |
* \param properties (nullable) (transfer full): additional properties of the device |
| 665 |
* \returns (transfer full): A new WpSpaDevice |
| 666 |
*/ |
| 667 |
WpSpaDevice * |
| 668 |
wp_spa_device_new_wrap (WpCore * core, gpointer spa_device_handle, |
| 669 |
WpProperties * properties) |
| 670 |
{ |
| 671 |
g_autoptr (WpProperties) props = properties; |
| 672 |
return g_object_new (WP_TYPE_SPA_DEVICE, |
| 673 |
"core", core, |
| 674 |
"spa-device-handle", spa_device_handle, |
| 675 |
"properties", props, |
| 676 |
NULL); |
| 677 |
} |
| 678 |
|
| 679 |
/*! |
| 680 |
* \brief Constructs a `SPA_TYPE_INTERFACE_Device` by loading the given SPA |
| 681 |
* \a factory_name. |
| 682 |
* |
| 683 |
* To export this device to the PipeWire server, you need to call |
| 684 |
* wp_object_activate() requesting WP_PROXY_FEATURE_BOUND and |
| 685 |
* wait for the operation to complete. |
| 686 |
* |
| 687 |
* \ingroup wpspadevice |
| 688 |
* \param core the wireplumber core |
| 689 |
* \param factory_name the name of the SPA factory |
| 690 |
* \param properties (nullable) (transfer full): properties to be passed to device |
| 691 |
* constructor |
| 692 |
* \returns (nullable) (transfer full): A new WpSpaDevice wrapping the |
| 693 |
* device that was constructed by the factory, or NULL if the factory |
| 694 |
* does not exist or was unable to construct the device |
| 695 |
*/ |
| 696 |
WpSpaDevice * |
| 697 |
wp_spa_device_new_from_spa_factory (WpCore * core, |
| 698 |
const gchar * factory_name, WpProperties * properties) |
| 699 |
{ |
| 700 |
g_autoptr (WpProperties) props = properties; |
| 701 |
struct pw_context *pw_context = wp_core_get_pw_context (core); |
| 702 |
struct spa_handle *handle = NULL; |
| 703 |
|
| 704 |
g_return_val_if_fail (pw_context != NULL, NULL); |
| 705 |
|
| 706 |
/* Load the monitor handle */ |
| 707 |
handle = pw_context_load_spa_handle (pw_context, factory_name, |
| 708 |
props ? wp_properties_peek_dict (props) : NULL); |
| 709 |
if (!handle) { |
| 710 |
wp_notice ("SPA handle '%s' could not be loaded; is it installed?", |
| 711 |
factory_name); |
| 712 |
return NULL; |
| 713 |
} |
| 714 |
|
| 715 |
return wp_spa_device_new_wrap (core, handle, g_steal_pointer (&props)); |
| 716 |
} |
| 717 |
|
| 718 |
/*! |
| 719 |
* \brief Gets the properties of this device. |
| 720 |
* |
| 721 |
* \ingroup wpspadevice |
| 722 |
* \param self the spa device |
| 723 |
* \returns (transfer full): the device properties |
| 724 |
*/ |
| 725 |
WpProperties * |
| 726 |
wp_spa_device_get_properties (WpSpaDevice * self) |
| 727 |
{ |
| 728 |
g_return_val_if_fail (WP_IS_SPA_DEVICE (self), NULL); |
| 729 |
return wp_properties_ref (self->properties); |
| 730 |
} |
| 731 |
|
| 732 |
/*! |
| 733 |
* \brief Iterates through all the objects managed by this device. |
| 734 |
* |
| 735 |
* \ingroup wpspadevice |
| 736 |
* \param self the spa device |
| 737 |
* \returns (transfer full): a WpIterator that iterates over all the objects |
| 738 |
* managed by this device |
| 739 |
* \since 0.4.11 |
| 740 |
*/ |
| 741 |
WpIterator * |
| 742 |
wp_spa_device_new_managed_object_iterator (WpSpaDevice * self) |
| 743 |
{ |
| 744 |
g_return_val_if_fail (WP_IS_SPA_DEVICE (self), NULL); |
| 745 |
return wp_iterator_new_ptr_array (g_ptr_array_ref (self->managed_objs), |
| 746 |
G_TYPE_OBJECT); |
| 747 |
} |
| 748 |
|
| 749 |
/*! |
| 750 |
* \brief Gets one of the objects managed by this device. |
| 751 |
* |
| 752 |
* \ingroup wpspadevice |
| 753 |
* \param self the spa device |
| 754 |
* \param id the (device-internal) id of the object to get |
| 755 |
* \returns (transfer full): the managed object associated with \a id |
| 756 |
*/ |
| 757 |
GObject * |
| 758 |
wp_spa_device_get_managed_object (WpSpaDevice * self, guint id) |
| 759 |
{ |
| 760 |
g_return_val_if_fail (WP_IS_SPA_DEVICE (self), NULL); |
| 761 |
|
| 762 |
GObject *ret = (id < self->managed_objs->len) ? |
| 763 |
g_ptr_array_index (self->managed_objs, id) : NULL; |
| 764 |
return ret ? g_object_ref (ret) : ret; |
| 765 |
} |
| 766 |
|
| 767 |
/*! |
| 768 |
* \brief Stores or removes a managed object into/from a device. |
| 769 |
* |
| 770 |
* \ingroup wpspadevice |
| 771 |
* \param self the spa device |
| 772 |
* \param id the (device-internal) id of the object |
| 773 |
* \param object (transfer full) (nullable): the object to store or NULL to remove |
| 774 |
* the managed object associated with \a id |
| 775 |
*/ |
| 776 |
void |
| 777 |
wp_spa_device_store_managed_object (WpSpaDevice * self, guint id, |
| 778 |
GObject * object) |
| 779 |
{ |
| 780 |
g_return_if_fail (WP_IS_SPA_DEVICE (self)); |
| 781 |
|
| 782 |
if (id >= self->managed_objs->len) |
| 783 |
g_ptr_array_set_size (self->managed_objs, id + 1); |
| 784 |
|
| 785 |
/* replace the item at @em id; g_ptr_array_insert is tempting to use here |
| 786 |
instead, but it's wrong because it will not remove the previous item */ |
| 787 |
gpointer *ptr = &g_ptr_array_index (self->managed_objs, id); |
| 788 |
if (*ptr) |
| 789 |
g_object_unref (*ptr); |
| 790 |
*ptr = object; |
| 791 |
|
| 792 |
/* Clear pending status, and set pending props if any */ |
| 793 |
g_autoptr(WpSpaPod) props = pending_obj_config_pop (self, id); |
| 794 |
|
| 795 |
if (props && object && WP_IS_PIPEWIRE_OBJECT (object)) { |
| 796 |
wp_trace_boxed (WP_TYPE_SPA_POD, props, "pending ObjectConfig, object %d", id); |
| 797 |
wp_pipewire_object_set_param (WP_PIPEWIRE_OBJECT (object), "Props", 0, |
| 798 |
g_steal_pointer (&props)); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
/*! |
| 803 |
* \brief Marks a managed object id pending. |
| 804 |
* |
| 805 |
* When an object id is pending, Props from received ObjectConfig events |
| 806 |
* for the id are saved. When \ref wp_spa_device_store_managed_object later sets |
| 807 |
* an object for the id, the saved Props are immediately set on the object and |
| 808 |
* pending status is cleared. |
| 809 |
* |
| 810 |
* If an object is already set for the id, this has no effect. |
| 811 |
* |
| 812 |
* \ingroup wpspadevice |
| 813 |
* \param self the spa device |
| 814 |
* \param id the (device-internal) id of the object |
| 815 |
*/ |
| 816 |
void |
| 817 |
wp_spa_device_set_managed_pending (WpSpaDevice * self, guint id) |
| 818 |
{ |
| 819 |
g_return_if_fail (WP_IS_SPA_DEVICE (self)); |
| 820 |
|
| 821 |
g_autoptr (GObject) obj = wp_spa_device_get_managed_object (self, id); |
| 822 |
if (obj) |
| 823 |
return; |
| 824 |
|
| 825 |
pending_obj_config_set (self, id, |
| 826 |
wp_spa_pod_new_object ("Spa:Pod:Object:Param:Props", "Props", NULL)); |
| 827 |
} |