| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2019 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "core.h" |
| 10 |
#include "wp.h" |
| 11 |
#include "private/registry.h" |
| 12 |
#include "private/internal-comp-loader.h" |
| 13 |
|
| 14 |
#include <pipewire/pipewire.h> |
| 15 |
|
| 16 |
#include <spa/utils/result.h> |
| 17 |
#include <spa/debug/types.h> |
| 18 |
#include <spa/support/cpu.h> |
| 19 |
|
| 20 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-core") |
| 21 |
|
| 22 |
/* |
| 23 |
* Integration between the PipeWire main loop and GMainLoop |
| 24 |
*/ |
| 25 |
|
| 26 |
#define WP_LOOP_SOURCE(x) ((WpLoopSource *) x) |
| 27 |
|
| 28 |
typedef struct _WpLoopSource WpLoopSource; |
| 29 |
struct _WpLoopSource |
| 30 |
{ |
| 31 |
GSource parent; |
| 32 |
struct pw_loop *loop; |
| 33 |
gboolean entered; |
| 34 |
}; |
| 35 |
|
| 36 |
static gboolean |
| 37 |
wp_loop_source_dispatch (GSource * s, GSourceFunc callback, gpointer user_data) |
| 38 |
{ |
| 39 |
WpLoopSource *ls = WP_LOOP_SOURCE (s); |
| 40 |
int result; |
| 41 |
|
| 42 |
if (!ls->entered) { |
| 43 |
wp_trace_boxed (G_TYPE_SOURCE, s, "entering pw main loop"); |
| 44 |
pw_loop_enter (ls->loop); |
| 45 |
ls->entered = TRUE; |
| 46 |
g_source_set_ready_time (s, -1); |
| 47 |
} |
| 48 |
|
| 49 |
result = pw_loop_iterate (ls->loop, 0); |
| 50 |
|
| 51 |
if (G_UNLIKELY (result < 0)) |
| 52 |
wp_warning_boxed (G_TYPE_SOURCE, s, |
| 53 |
"pw_loop_iterate failed: %s", spa_strerror (result)); |
| 54 |
|
| 55 |
return G_SOURCE_CONTINUE; |
| 56 |
} |
| 57 |
|
| 58 |
static void |
| 59 |
wp_loop_source_finalize (GSource * s) |
| 60 |
{ |
| 61 |
WpLoopSource *ls = WP_LOOP_SOURCE (s); |
| 62 |
|
| 63 |
wp_trace_boxed (G_TYPE_SOURCE, s, "finalize loop source"); |
| 64 |
|
| 65 |
/* Source should be left from the thread it was entered from. |
| 66 |
* |
| 67 |
* This puts additional restrictions to upper layers on how WpLoopSource (and |
| 68 |
* WpCore) can be used: they must be finalized from the GMainContext thread. |
| 69 |
*/ |
| 70 |
if (ls->entered) { |
| 71 |
wp_trace_boxed (G_TYPE_SOURCE, s, "leaving pw main loop"); |
| 72 |
pw_loop_leave (ls->loop); |
| 73 |
} |
| 74 |
|
| 75 |
pw_loop_destroy (ls->loop); |
| 76 |
} |
| 77 |
|
| 78 |
static GSourceFuncs source_funcs = { |
| 79 |
NULL, |
| 80 |
NULL, |
| 81 |
wp_loop_source_dispatch, |
| 82 |
wp_loop_source_finalize |
| 83 |
}; |
| 84 |
|
| 85 |
static GSource * |
| 86 |
wp_loop_source_new (void) |
| 87 |
{ |
| 88 |
GSource *s = g_source_new (&source_funcs, sizeof (WpLoopSource)); |
| 89 |
WP_LOOP_SOURCE(s)->loop = pw_loop_new (NULL); |
| 90 |
|
| 91 |
g_source_add_unix_fd (s, |
| 92 |
pw_loop_get_fd (WP_LOOP_SOURCE(s)->loop), |
| 93 |
G_IO_IN | G_IO_ERR | G_IO_HUP); |
| 94 |
|
| 95 |
/* dispatch immediately to enter the loop */ |
| 96 |
g_source_set_ready_time (s, 0); |
| 97 |
|
| 98 |
return (GSource *) s; |
| 99 |
} |
| 100 |
|
| 101 |
/*! \defgroup wpcore WpCore */ |
| 102 |
/*! |
| 103 |
* \struct WpCore |
| 104 |
* |
| 105 |
* The core is the central object around which everything operates. It is |
| 106 |
* essential to create a WpCore before using any other WirePlumber API. |
| 107 |
* |
| 108 |
* The core object has the following responsibilities: |
| 109 |
* * it initializes the PipeWire library |
| 110 |
* * it creates a `pw_context` and allows connecting to the PipeWire server, |
| 111 |
* creating a local `pw_core` |
| 112 |
* * it glues the PipeWire library's event loop system with GMainLoop |
| 113 |
* * it maintains a list of registered objects, which other classes use |
| 114 |
* to keep objects loaded permanently into memory |
| 115 |
* * it watches the PipeWire registry and keeps track of remote and local |
| 116 |
* objects that appear in the registry, making them accessible through |
| 117 |
* the WpObjectManager API. |
| 118 |
* |
| 119 |
* The core is also responsible for loading components, which are defined in |
| 120 |
* the main configuration file. Components are loaded when |
| 121 |
* WP_CORE_FEATURE_COMPONENTS is activated. |
| 122 |
* |
| 123 |
* \b Configuration |
| 124 |
* |
| 125 |
* The main configuration file needs to be created and opened before the core |
| 126 |
* is created, using the WpConf API. It is then passed to the core as an |
| 127 |
* argument in the constructor. |
| 128 |
* |
| 129 |
* If a configuration file is not provided, the core will let the underlying |
| 130 |
* `pw_context` load its own configuration, based on the rules that apply to |
| 131 |
* all pipewire clients (e.g. it respects the `PIPEWIRE_CONFIG_NAME` environment |
| 132 |
* variable and loads "client.conf" as a last resort). |
| 133 |
* |
| 134 |
* If a configuration file is provided, the core does not let the underlying |
| 135 |
* `pw_context` load any configuration and instead uses the provided WpConf |
| 136 |
* object. |
| 137 |
* |
| 138 |
* \gproperties |
| 139 |
* |
| 140 |
* \gproperty{g-main-context, GMainContext *, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY, |
| 141 |
* A GMainContext to attach to} |
| 142 |
* |
| 143 |
* \gproperty{properties, WpProperties *, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY, |
| 144 |
* The pipewire properties of the pw_core} |
| 145 |
* |
| 146 |
* \gproperty{pw-context, gpointer (struct pw_context *), G_PARAM_READABLE, |
| 147 |
* The pipewire context} |
| 148 |
* |
| 149 |
* \gproperty{pw-core, gpointer (struct pw_core *), G_PARAM_READABLE, |
| 150 |
* The pipewire core} |
| 151 |
* |
| 152 |
* \gproperty{conf, WpConf *, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY, |
| 153 |
* The main configuration file} |
| 154 |
* |
| 155 |
* \gsignals |
| 156 |
* |
| 157 |
* \par connected |
| 158 |
* \parblock |
| 159 |
* \code |
| 160 |
* void |
| 161 |
* connected_callback (WpCore * self, |
| 162 |
* gpointer user_data) |
| 163 |
* \endcode |
| 164 |
* Emitted when the core is successfully connected to the PipeWire server |
| 165 |
* \endparblock |
| 166 |
* |
| 167 |
* \par disconnected |
| 168 |
* \parblock |
| 169 |
* \code |
| 170 |
* void |
| 171 |
* disconnected_callback (WpCore * self, |
| 172 |
* gpointer user_data) |
| 173 |
* \endcode |
| 174 |
* Emitted when the core is disconnected from the PipeWire server |
| 175 |
* \endparblock |
| 176 |
*/ |
| 177 |
struct _WpCore |
| 178 |
{ |
| 179 |
WpObject parent; |
| 180 |
|
| 181 |
/* main loop integration */ |
| 182 |
GMainContext *g_main_context; |
| 183 |
|
| 184 |
/* extra properties */ |
| 185 |
WpProperties *properties; |
| 186 |
|
| 187 |
/* pipewire main objects */ |
| 188 |
struct pw_context *pw_context; |
| 189 |
struct pw_core *pw_core; |
| 190 |
struct pw_core_info *info; |
| 191 |
|
| 192 |
/* pipewire main listeners */ |
| 193 |
struct spa_hook core_listener; |
| 194 |
struct spa_hook proxy_core_listener; |
| 195 |
|
| 196 |
/* the main configuration file */ |
| 197 |
WpConf *conf; |
| 198 |
|
| 199 |
WpRegistry registry; |
| 200 |
GHashTable *async_tasks; // <int seq, GTask*> |
| 201 |
}; |
| 202 |
|
| 203 |
struct context_data { |
| 204 |
grefcount rc; |
| 205 |
GSource *loop_source; |
| 206 |
}; |
| 207 |
|
| 208 |
enum { |
| 209 |
PROP_0, |
| 210 |
PROP_G_MAIN_CONTEXT, |
| 211 |
PROP_PROPERTIES, |
| 212 |
PROP_PW_CONTEXT, |
| 213 |
PROP_PW_CORE, |
| 214 |
PROP_CONF, |
| 215 |
}; |
| 216 |
|
| 217 |
enum { |
| 218 |
SIGNAL_CONNECTED, |
| 219 |
SIGNAL_DISCONNECTED, |
| 220 |
NUM_SIGNALS |
| 221 |
}; |
| 222 |
|
| 223 |
static guint32 signals[NUM_SIGNALS]; |
| 224 |
|
| 225 |
G_DEFINE_TYPE (WpCore, wp_core, WP_TYPE_OBJECT) |
| 226 |
|
| 227 |
static void |
| 228 |
core_info (void *data, const struct pw_core_info * info) |
| 229 |
{ |
| 230 |
WpCore *self = WP_CORE (data); |
| 231 |
gboolean new_connection = (self->info == NULL); |
| 232 |
|
| 233 |
self->info = pw_core_info_update (self->info, info); |
| 234 |
|
| 235 |
wp_info_object (self, "connected to server: %s, cookie: %u", |
| 236 |
self->info->name, self->info->cookie); |
| 237 |
|
| 238 |
if (new_connection) { |
| 239 |
g_signal_emit (self, signals[SIGNAL_CONNECTED], 0); |
| 240 |
wp_object_update_features (WP_OBJECT (self), WP_CORE_FEATURE_CONNECTED, 0); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
static void |
| 245 |
core_done (void *data, uint32_t id, int seq) |
| 246 |
{ |
| 247 |
WpCore *self = WP_CORE (data); |
| 248 |
g_autoptr (GTask) task = NULL; |
| 249 |
|
| 250 |
g_hash_table_steal_extended (self->async_tasks, GINT_TO_POINTER (seq), NULL, |
| 251 |
(gpointer *) &task); |
| 252 |
wp_debug_object (self, "done, seq 0x%x, task " WP_OBJECT_FORMAT, |
| 253 |
seq, WP_OBJECT_ARGS (task)); |
| 254 |
|
| 255 |
if (task) |
| 256 |
g_task_return_boolean (task, TRUE); |
| 257 |
} |
| 258 |
|
| 259 |
static gboolean |
| 260 |
core_disconnect_async (WpCore * self) |
| 261 |
{ |
| 262 |
wp_core_disconnect (self); |
| 263 |
return G_SOURCE_REMOVE; |
| 264 |
} |
| 265 |
|
| 266 |
static void |
| 267 |
core_error (void *data, uint32_t id, int seq, int res, const char *message) |
| 268 |
{ |
| 269 |
WpCore *self = WP_CORE (data); |
| 270 |
|
| 271 |
/* protocol socket disconnected; schedule disconnecting our core */ |
| 272 |
if (id == 0 && res == -EPIPE) { |
| 273 |
wp_core_idle_add_closure (self, NULL, g_cclosure_new_object ( |
| 274 |
G_CALLBACK (core_disconnect_async), G_OBJECT (self))); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
static const struct pw_core_events core_events = { |
| 279 |
PW_VERSION_CORE_EVENTS, |
| 280 |
.info = core_info, |
| 281 |
.done = core_done, |
| 282 |
.error = core_error, |
| 283 |
}; |
| 284 |
|
| 285 |
static gboolean |
| 286 |
async_tasks_finish (gpointer key, gpointer value, gpointer user_data) |
| 287 |
{ |
| 288 |
GTask *task = G_TASK (value); |
| 289 |
g_return_val_if_fail (task, FALSE); |
| 290 |
|
| 291 |
g_task_return_new_error (task, WP_DOMAIN_LIBRARY, |
| 292 |
WP_LIBRARY_ERROR_INVARIANT, "core disconnected"); |
| 293 |
return TRUE; |
| 294 |
} |
| 295 |
|
| 296 |
static void |
| 297 |
proxy_core_destroy (void *data) |
| 298 |
{ |
| 299 |
WpCore *self = WP_CORE (data); |
| 300 |
g_hash_table_foreach_remove (self->async_tasks, async_tasks_finish, NULL); |
| 301 |
g_clear_pointer (&self->info, pw_core_info_free); |
| 302 |
spa_hook_remove(&self->core_listener); |
| 303 |
spa_hook_remove(&self->proxy_core_listener); |
| 304 |
self->pw_core = NULL; |
| 305 |
wp_debug_object (self, "emit disconnected"); |
| 306 |
g_signal_emit (self, signals[SIGNAL_DISCONNECTED], 0); |
| 307 |
wp_object_update_features (WP_OBJECT (self), 0, WP_CORE_FEATURE_CONNECTED); |
| 308 |
} |
| 309 |
|
| 310 |
static const struct pw_proxy_events proxy_core_events = { |
| 311 |
PW_VERSION_PROXY_EVENTS, |
| 312 |
.destroy = proxy_core_destroy, |
| 313 |
}; |
| 314 |
|
| 315 |
static void |
| 316 |
wp_core_init (WpCore * self) |
| 317 |
{ |
| 318 |
wp_registry_init (&self->registry); |
| 319 |
self->async_tasks = g_hash_table_new_full (g_direct_hash, g_direct_equal, |
| 320 |
NULL, g_object_unref); |
| 321 |
|
| 322 |
wp_core_register_object (self, |
| 323 |
g_object_new (WP_TYPE_INTERNAL_COMP_LOADER, NULL)); |
| 324 |
} |
| 325 |
|
| 326 |
static void |
| 327 |
wp_core_constructed (GObject *object) |
| 328 |
{ |
| 329 |
WpCore *self = WP_CORE (object); |
| 330 |
|
| 331 |
/* context */ |
| 332 |
if (!self->pw_context) { |
| 333 |
struct pw_properties *p = NULL; |
| 334 |
const gchar *str = NULL; |
| 335 |
g_autoptr (GSource) source = wp_loop_source_new (); |
| 336 |
|
| 337 |
/* use our own configuration file, if specified */ |
| 338 |
if (self->conf) { |
| 339 |
wp_info_object (self, "using configuration file: %s", |
| 340 |
wp_conf_get_name (self->conf)); |
| 341 |
|
| 342 |
/* ensure we have our very own properties set, |
| 343 |
since we are going to modify it */ |
| 344 |
self->properties = self->properties ? |
| 345 |
wp_properties_ensure_unique_owner (self->properties) : |
| 346 |
wp_properties_new_empty (); |
| 347 |
|
| 348 |
/* load context.properties */ |
| 349 |
wp_conf_section_update_props (self->conf, "context.properties", |
| 350 |
self->properties); |
| 351 |
|
| 352 |
/* disable loading of a configuration file in pw_context */ |
| 353 |
wp_properties_set (self->properties, PW_KEY_CONFIG_NAME, "null"); |
| 354 |
wp_properties_set (self->properties, "context.modules.allow-empty", "true"); |
| 355 |
} |
| 356 |
|
| 357 |
/* properties are fully stored in the pw_context, no need to keep a copy */ |
| 358 |
p = self->properties ? |
| 359 |
wp_properties_unref_and_take_pw_properties (self->properties) : NULL; |
| 360 |
self->properties = NULL; |
| 361 |
|
| 362 |
self->pw_context = pw_context_new (WP_LOOP_SOURCE(source)->loop, p, |
| 363 |
sizeof (struct context_data)); |
| 364 |
g_return_if_fail (self->pw_context); |
| 365 |
|
| 366 |
/* use the same config option as pipewire to set the log level */ |
| 367 |
p = (struct pw_properties *) pw_context_get_properties (self->pw_context); |
| 368 |
if (!g_getenv("WIREPLUMBER_DEBUG") && |
| 369 |
(str = pw_properties_get(p, "log.level")) != NULL) { |
| 370 |
if (!wp_log_set_level (str)) |
| 371 |
wp_warning ("ignoring invalid log.level in config file: %s", str); |
| 372 |
} |
| 373 |
|
| 374 |
/* parse pw_context specific configuration sections */ |
| 375 |
if (self->conf) |
| 376 |
wp_conf_parse_pw_context_sections (self->conf, self->pw_context); |
| 377 |
|
| 378 |
/* Init refcount */ |
| 379 |
struct context_data *cd = pw_context_get_user_data (self->pw_context); |
| 380 |
g_return_if_fail (cd); |
| 381 |
g_ref_count_init (&cd->rc); |
| 382 |
cd->loop_source = g_source_ref (source); |
| 383 |
|
| 384 |
/* Start source */ |
| 385 |
g_source_attach (source, self->g_main_context); |
| 386 |
} else { |
| 387 |
/* Increase refcount */ |
| 388 |
struct context_data *cd = pw_context_get_user_data (self->pw_context); |
| 389 |
g_return_if_fail (cd); |
| 390 |
g_ref_count_inc (&cd->rc); |
| 391 |
} |
| 392 |
|
| 393 |
G_OBJECT_CLASS (wp_core_parent_class)->constructed (object); |
| 394 |
} |
| 395 |
|
| 396 |
static void |
| 397 |
wp_core_dispose (GObject * obj) |
| 398 |
{ |
| 399 |
WpCore *self = WP_CORE (obj); |
| 400 |
|
| 401 |
wp_registry_clear (&self->registry); |
| 402 |
wp_object_update_features (WP_OBJECT (self), 0, WP_CORE_FEATURE_COMPONENTS); |
| 403 |
|
| 404 |
G_OBJECT_CLASS (wp_core_parent_class)->dispose (obj); |
| 405 |
} |
| 406 |
|
| 407 |
static void |
| 408 |
wp_core_finalize (GObject * obj) |
| 409 |
{ |
| 410 |
WpCore *self = WP_CORE (obj); |
| 411 |
struct context_data *cd = pw_context_get_user_data (self->pw_context); |
| 412 |
g_return_if_fail (cd); |
| 413 |
|
| 414 |
wp_core_disconnect (self); |
| 415 |
|
| 416 |
/* Clear pw-context if refcount reaches 0 */ |
| 417 |
if (g_ref_count_dec (&cd->rc)) { |
| 418 |
GSource *source = cd->loop_source; |
| 419 |
|
| 420 |
g_clear_pointer (&self->pw_context, pw_context_destroy); |
| 421 |
g_source_destroy (source); |
| 422 |
g_source_unref (source); |
| 423 |
} |
| 424 |
|
| 425 |
g_clear_pointer (&self->properties, wp_properties_unref); |
| 426 |
g_clear_pointer (&self->g_main_context, g_main_context_unref); |
| 427 |
g_clear_pointer (&self->async_tasks, g_hash_table_unref); |
| 428 |
g_clear_object (&self->conf); |
| 429 |
|
| 430 |
wp_debug_object (self, "WpCore destroyed"); |
| 431 |
|
| 432 |
G_OBJECT_CLASS (wp_core_parent_class)->finalize (obj); |
| 433 |
} |
| 434 |
|
| 435 |
static void |
| 436 |
wp_core_get_property (GObject * object, guint property_id, |
| 437 |
GValue * value, GParamSpec * pspec) |
| 438 |
{ |
| 439 |
WpCore *self = WP_CORE (object); |
| 440 |
|
| 441 |
switch (property_id) { |
| 442 |
case PROP_G_MAIN_CONTEXT: |
| 443 |
g_value_set_boxed (value, self->g_main_context); |
| 444 |
break; |
| 445 |
case PROP_PROPERTIES: |
| 446 |
g_value_take_boxed (value, wp_core_get_properties (self)); |
| 447 |
break; |
| 448 |
case PROP_PW_CONTEXT: |
| 449 |
g_value_set_pointer (value, self->pw_context); |
| 450 |
break; |
| 451 |
case PROP_PW_CORE: |
| 452 |
g_value_set_pointer (value, self->pw_core); |
| 453 |
break; |
| 454 |
case PROP_CONF: |
| 455 |
g_value_set_object (value, self->conf); |
| 456 |
break; |
| 457 |
default: |
| 458 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 459 |
break; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
static void |
| 464 |
wp_core_set_property (GObject * object, guint property_id, |
| 465 |
const GValue * value, GParamSpec * pspec) |
| 466 |
{ |
| 467 |
WpCore *self = WP_CORE (object); |
| 468 |
|
| 469 |
switch (property_id) { |
| 470 |
case PROP_G_MAIN_CONTEXT: |
| 471 |
self->g_main_context = g_value_dup_boxed (value); |
| 472 |
break; |
| 473 |
case PROP_PROPERTIES: |
| 474 |
self->properties = g_value_dup_boxed (value); |
| 475 |
break; |
| 476 |
case PROP_PW_CONTEXT: |
| 477 |
self->pw_context = g_value_get_pointer (value); |
| 478 |
break; |
| 479 |
case PROP_CONF: |
| 480 |
self->conf = g_value_dup_object (value); |
| 481 |
break; |
| 482 |
default: |
| 483 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 484 |
break; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
static WpObjectFeatures |
| 489 |
wp_core_get_supported_features (WpObject * self) |
| 490 |
{ |
| 491 |
return WP_CORE_FEATURE_CONNECTED | |
| 492 |
WP_CORE_FEATURE_COMPONENTS; |
| 493 |
} |
| 494 |
|
| 495 |
enum { |
| 496 |
STEP_CONNECT = WP_TRANSITION_STEP_CUSTOM_START, |
| 497 |
STEP_LOAD_COMPONENTS, |
| 498 |
}; |
| 499 |
|
| 500 |
static guint |
| 501 |
wp_core_activate_get_next_step (WpObject * self, |
| 502 |
WpFeatureActivationTransition * transition, guint step, |
| 503 |
WpObjectFeatures missing) |
| 504 |
{ |
| 505 |
switch (step) { |
| 506 |
case WP_TRANSITION_STEP_NONE: |
| 507 |
if (missing & WP_CORE_FEATURE_CONNECTED) |
| 508 |
return STEP_CONNECT; |
| 509 |
G_GNUC_FALLTHROUGH; |
| 510 |
|
| 511 |
case STEP_CONNECT: |
| 512 |
if (missing & WP_CORE_FEATURE_COMPONENTS) |
| 513 |
return STEP_LOAD_COMPONENTS; |
| 514 |
G_GNUC_FALLTHROUGH; |
| 515 |
|
| 516 |
case STEP_LOAD_COMPONENTS: |
| 517 |
return WP_TRANSITION_STEP_NONE; |
| 518 |
|
| 519 |
default: |
| 520 |
return WP_TRANSITION_STEP_ERROR; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
static void |
| 525 |
on_components_loaded (WpCore * self, GAsyncResult *res, |
| 526 |
WpTransition * transition) |
| 527 |
{ |
| 528 |
g_autoptr (GError) error = NULL; |
| 529 |
|
| 530 |
if (!wp_core_load_component_finish (self, res, &error)) { |
| 531 |
wp_transition_return_error (transition, g_error_new ( |
| 532 |
WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVALID_ARGUMENT, |
| 533 |
"failed to load components: %s", error->message)); |
| 534 |
return; |
| 535 |
} |
| 536 |
|
| 537 |
if (self->conf) { |
| 538 |
wp_info_object (self, "done loading components, closing conf file..."); |
| 539 |
wp_conf_close (self->conf); |
| 540 |
} |
| 541 |
|
| 542 |
wp_object_update_features (WP_OBJECT (self), WP_CORE_FEATURE_COMPONENTS, 0); |
| 543 |
} |
| 544 |
|
| 545 |
static void |
| 546 |
wp_core_activate_execute_step (WpObject * object, |
| 547 |
WpFeatureActivationTransition * transition, guint step, |
| 548 |
WpObjectFeatures missing) |
| 549 |
{ |
| 550 |
WpCore *self = WP_CORE (object); |
| 551 |
|
| 552 |
switch (step) { |
| 553 |
case STEP_CONNECT: { |
| 554 |
wp_info_object (self, "connecting to pipewire..."); |
| 555 |
|
| 556 |
if (!wp_core_connect (self)) { |
| 557 |
wp_transition_return_error (WP_TRANSITION (transition), g_error_new ( |
| 558 |
WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_SERVICE_UNAVAILABLE, |
| 559 |
"Failed to connect to PipeWire")); |
| 560 |
} |
| 561 |
break; |
| 562 |
} |
| 563 |
|
| 564 |
case STEP_LOAD_COMPONENTS: { |
| 565 |
g_autoptr (WpProperties) props = wp_core_get_properties (self); |
| 566 |
|
| 567 |
if (spa_atob (wp_properties_get (props, "wireplumber.export-core"))) { |
| 568 |
/* do not load any components on the export core */ |
| 569 |
wp_object_update_features (WP_OBJECT (self), WP_CORE_FEATURE_COMPONENTS, 0); |
| 570 |
return; |
| 571 |
} |
| 572 |
else { |
| 573 |
const gchar *profile = wp_properties_get (props, "wireplumber.profile"); |
| 574 |
|
| 575 |
wp_info_object (self, |
| 576 |
"parsing & loading components for profile [%s]...", profile); |
| 577 |
|
| 578 |
/* Load components that are defined in the configuration section */ |
| 579 |
wp_core_load_component (self, profile, "profile", NULL, NULL, NULL, |
| 580 |
(GAsyncReadyCallback) on_components_loaded, transition); |
| 581 |
} |
| 582 |
break; |
| 583 |
} |
| 584 |
|
| 585 |
case WP_TRANSITION_STEP_ERROR: |
| 586 |
break; |
| 587 |
default: |
| 588 |
g_assert_not_reached (); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
static void |
| 593 |
wp_core_deactivate (WpObject * self, WpObjectFeatures features) |
| 594 |
{ |
| 595 |
if (features & WP_CORE_FEATURE_CONNECTED) |
| 596 |
wp_core_disconnect (WP_CORE (self)); |
| 597 |
|
| 598 |
/* WP_CORE_FEATURE_COMPONENTS cannot be manually deactivated */ |
| 599 |
} |
| 600 |
|
| 601 |
static void |
| 602 |
wp_core_class_init (WpCoreClass * klass) |
| 603 |
{ |
| 604 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 605 |
WpObjectClass *wpobject_class = (WpObjectClass *) klass; |
| 606 |
|
| 607 |
object_class->constructed = wp_core_constructed; |
| 608 |
object_class->dispose = wp_core_dispose; |
| 609 |
object_class->finalize = wp_core_finalize; |
| 610 |
object_class->get_property = wp_core_get_property; |
| 611 |
object_class->set_property = wp_core_set_property; |
| 612 |
|
| 613 |
wpobject_class->get_supported_features = wp_core_get_supported_features; |
| 614 |
wpobject_class->activate_get_next_step = wp_core_activate_get_next_step; |
| 615 |
wpobject_class->activate_execute_step = wp_core_activate_execute_step; |
| 616 |
wpobject_class->deactivate = wp_core_deactivate; |
| 617 |
|
| 618 |
g_object_class_install_property (object_class, PROP_G_MAIN_CONTEXT, |
| 619 |
g_param_spec_boxed ("g-main-context", "g-main-context", |
| 620 |
"A GMainContext to attach to", G_TYPE_MAIN_CONTEXT, |
| 621 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 622 |
|
| 623 |
g_object_class_install_property (object_class, PROP_PROPERTIES, |
| 624 |
g_param_spec_boxed ("properties", "properties", "Extra properties", |
| 625 |
WP_TYPE_PROPERTIES, |
| 626 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 627 |
|
| 628 |
g_object_class_install_property (object_class, PROP_PW_CONTEXT, |
| 629 |
g_param_spec_pointer ("pw-context", "pw-context", "The pipewire context", |
| 630 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 631 |
|
| 632 |
g_object_class_install_property (object_class, PROP_PW_CORE, |
| 633 |
g_param_spec_pointer ("pw-core", "pw-core", "The pipewire core", |
| 634 |
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 635 |
|
| 636 |
g_object_class_install_property (object_class, PROP_CONF, |
| 637 |
g_param_spec_object ("conf", "conf", "The main configuration file", |
| 638 |
WP_TYPE_CONF, |
| 639 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 640 |
|
| 641 |
signals[SIGNAL_CONNECTED] = g_signal_new ("connected", |
| 642 |
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, |
| 643 |
G_TYPE_NONE, 0); |
| 644 |
|
| 645 |
signals[SIGNAL_DISCONNECTED] = g_signal_new ("disconnected", |
| 646 |
G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, |
| 647 |
G_TYPE_NONE, 0); |
| 648 |
} |
| 649 |
|
| 650 |
/*! |
| 651 |
* \brief Creates a new core object |
| 652 |
* |
| 653 |
* \ingroup wpcore |
| 654 |
* \param context (transfer none) (nullable): the GMainContext to use for events |
| 655 |
* \param conf (transfer full) (nullable): the main configuration file |
| 656 |
* \param properties (transfer full) (nullable): additional properties, which |
| 657 |
* are also passed to pw_context_new() and pw_context_connect() |
| 658 |
* \returns (transfer full): a new WpCore |
| 659 |
*/ |
| 660 |
WpCore * |
| 661 |
wp_core_new (GMainContext * context, WpConf * conf, WpProperties * properties) |
| 662 |
{ |
| 663 |
g_autoptr (WpConf) c = conf; |
| 664 |
g_autoptr (WpProperties) props = properties; |
| 665 |
|
| 666 |
return g_object_new (WP_TYPE_CORE, |
| 667 |
"g-main-context", context, |
| 668 |
"conf", conf, |
| 669 |
"properties", properties, |
| 670 |
"pw-context", NULL, |
| 671 |
NULL); |
| 672 |
} |
| 673 |
|
| 674 |
/*! |
| 675 |
* \brief Clones a core with the same context as \a self |
| 676 |
* |
| 677 |
* \ingroup wpcore |
| 678 |
* \param self the core |
| 679 |
* \returns (transfer full): the clone WpCore |
| 680 |
*/ |
| 681 |
WpCore * |
| 682 |
wp_core_clone (WpCore * self) |
| 683 |
{ |
| 684 |
return g_object_new (WP_TYPE_CORE, |
| 685 |
"core", self, |
| 686 |
"g-main-context", self->g_main_context, |
| 687 |
"conf", self->conf, |
| 688 |
"properties", self->properties, |
| 689 |
"pw-context", self->pw_context, |
| 690 |
NULL); |
| 691 |
} |
| 692 |
|
| 693 |
static gboolean |
| 694 |
find_export_core (gconstpointer a, gconstpointer b) |
| 695 |
{ |
| 696 |
gpointer obj = (gpointer) a; |
| 697 |
if (WP_IS_CORE ((gpointer) obj)) { |
| 698 |
g_autoptr (WpProperties) props = wp_core_get_properties (WP_CORE (obj)); |
| 699 |
if (spa_atob (wp_properties_get (props, "wireplumber.export-core"))) |
| 700 |
return TRUE; |
| 701 |
} |
| 702 |
return FALSE; |
| 703 |
} |
| 704 |
|
| 705 |
/*! |
| 706 |
* \brief Returns the special WpCore that is used to maintain a secondary |
| 707 |
* connection to PipeWire, for exporting objects |
| 708 |
* |
| 709 |
* The export core is enabled by loading the built-in "export-core" component. |
| 710 |
* |
| 711 |
* \ingroup wpcore |
| 712 |
* \param self the core |
| 713 |
* \returns (transfer full): the export WpCore |
| 714 |
*/ |
| 715 |
WpCore * |
| 716 |
wp_core_get_export_core (WpCore * self) |
| 717 |
{ |
| 718 |
g_return_val_if_fail (WP_IS_CORE (self), NULL); |
| 719 |
|
| 720 |
return wp_core_find_object (self, find_export_core, NULL); |
| 721 |
} |
| 722 |
|
| 723 |
/*! |
| 724 |
* \brief Gets the main configuration file of the core |
| 725 |
* |
| 726 |
* \ingroup wpcore |
| 727 |
* \param self the core |
| 728 |
* \returns (transfer full) (nullable): the main configuration file |
| 729 |
*/ |
| 730 |
WpConf * |
| 731 |
wp_core_get_conf (WpCore * self) |
| 732 |
{ |
| 733 |
g_return_val_if_fail (WP_IS_CORE (self), NULL); |
| 734 |
return self->conf ? g_object_ref (self->conf) : NULL; |
| 735 |
} |
| 736 |
|
| 737 |
/*! |
| 738 |
* \brief Gets the GMainContext of the core |
| 739 |
* |
| 740 |
* \ingroup wpcore |
| 741 |
* \param self the core |
| 742 |
* \returns (transfer none) (nullable): the GMainContext that is in |
| 743 |
* use by this core for events |
| 744 |
*/ |
| 745 |
GMainContext * |
| 746 |
wp_core_get_g_main_context (WpCore * self) |
| 747 |
{ |
| 748 |
g_return_val_if_fail (WP_IS_CORE (self), NULL); |
| 749 |
return self->g_main_context; |
| 750 |
} |
| 751 |
|
| 752 |
/*! |
| 753 |
* \brief Gets the internal PipeWire context of the core |
| 754 |
* |
| 755 |
* \ingroup wpcore |
| 756 |
* \param self the core |
| 757 |
* \returns (transfer none): the internal pw_context object |
| 758 |
*/ |
| 759 |
struct pw_context * |
| 760 |
wp_core_get_pw_context (WpCore * self) |
| 761 |
{ |
| 762 |
g_return_val_if_fail (WP_IS_CORE (self), NULL); |
| 763 |
return self->pw_context; |
| 764 |
} |
| 765 |
|
| 766 |
/*! |
| 767 |
* \brief Gets the internal PipeWire core of the core |
| 768 |
* |
| 769 |
* \ingroup wpcore |
| 770 |
* \param self the core |
| 771 |
* \returns (transfer none) (nullable): the internal pw_core object, |
| 772 |
* or NULL if the core is not connected to PipeWire |
| 773 |
*/ |
| 774 |
struct pw_core * |
| 775 |
wp_core_get_pw_core (WpCore * self) |
| 776 |
{ |
| 777 |
g_return_val_if_fail (WP_IS_CORE (self), NULL); |
| 778 |
return self->pw_core; |
| 779 |
} |
| 780 |
|
| 781 |
/*! |
| 782 |
* \brief Gets the virtual machine type of the core |
| 783 |
* |
| 784 |
* \ingroup wpcore |
| 785 |
* \param self the core |
| 786 |
* \returns (transfer full) (nullable): a comma separated string with all the |
| 787 |
* virtual machine types that this core matches, or NULL if the core is not |
| 788 |
* running in a virtual machine. |
| 789 |
* |
| 790 |
* \since 0.4.11 |
| 791 |
*/ |
| 792 |
gchar * |
| 793 |
wp_core_get_vm_type (WpCore *self) |
| 794 |
{ |
| 795 |
const struct spa_support *support; |
| 796 |
uint32_t n_support; |
| 797 |
struct spa_cpu *spa_cpu; |
| 798 |
uint32_t vm_type; |
| 799 |
gchar *res; |
| 800 |
gboolean first = TRUE; |
| 801 |
|
| 802 |
struct vm_type_info { |
| 803 |
uint32_t type; |
| 804 |
const gchar *name; |
| 805 |
}; |
| 806 |
|
| 807 |
static struct vm_type_info vm_types[] = { |
| 808 |
{SPA_CPU_VM_OTHER, "other"}, |
| 809 |
{SPA_CPU_VM_KVM, "kvm"}, |
| 810 |
{SPA_CPU_VM_QEMU, "qemu"}, |
| 811 |
{SPA_CPU_VM_BOCHS, "bochs"}, |
| 812 |
{SPA_CPU_VM_XEN, "zen"}, |
| 813 |
{SPA_CPU_VM_UML, "uml"}, |
| 814 |
{SPA_CPU_VM_VMWARE, "vmware"}, |
| 815 |
{SPA_CPU_VM_ORACLE, "oracle"}, |
| 816 |
{SPA_CPU_VM_MICROSOFT, "microsoft"}, |
| 817 |
{SPA_CPU_VM_ZVM, "zvm"}, |
| 818 |
{SPA_CPU_VM_PARALLELS, "parallels"}, |
| 819 |
{SPA_CPU_VM_BHYVE, "bhyve"}, |
| 820 |
{SPA_CPU_VM_QNX, "qnx"}, |
| 821 |
{SPA_CPU_VM_ACRN, "acrn"}, |
| 822 |
{SPA_CPU_VM_POWERVM, "powervm"}, |
| 823 |
{0, NULL}, |
| 824 |
}; |
| 825 |
|
| 826 |
g_return_val_if_fail (WP_IS_CORE (self), NULL); |
| 827 |
g_return_val_if_fail (self->pw_context, NULL); |
| 828 |
|
| 829 |
/* Get spa_cpu */ |
| 830 |
support = pw_context_get_support (self->pw_context, &n_support); |
| 831 |
spa_cpu = spa_support_find (support, n_support, SPA_TYPE_INTERFACE_CPU); |
| 832 |
g_return_val_if_fail (spa_cpu, NULL); |
| 833 |
|
| 834 |
/* Just return NULL if CPU is not a VM */ |
| 835 |
vm_type = spa_cpu_get_vm_type (spa_cpu); |
| 836 |
if (vm_type == SPA_CPU_VM_NONE) |
| 837 |
return NULL; |
| 838 |
|
| 839 |
/* Otherwise return a string with all matching VM types */ |
| 840 |
res = g_strdup (""); |
| 841 |
for (guint i = 0; vm_types[i].name; i++) { |
| 842 |
if (vm_type & vm_types[i].type) { |
| 843 |
gchar *tmp = g_strdup_printf ("%s%s%s", res, first ? "": ",", |
| 844 |
vm_types[i].name); |
| 845 |
g_free (res); |
| 846 |
res = tmp; |
| 847 |
first = FALSE; |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
return res; |
| 852 |
} |
| 853 |
|
| 854 |
static gboolean |
| 855 |
wp_core_connect_internal (WpCore *self, int fd) |
| 856 |
{ |
| 857 |
struct pw_properties *p = NULL; |
| 858 |
|
| 859 |
/* Don't do anything if core is already connected */ |
| 860 |
if (self->pw_core) |
| 861 |
return TRUE; |
| 862 |
|
| 863 |
/* Connect */ |
| 864 |
p = self->properties ? wp_properties_to_pw_properties (self->properties) : NULL; |
| 865 |
|
| 866 |
if (fd == -1) |
| 867 |
self->pw_core = pw_context_connect (self->pw_context, p, 0); |
| 868 |
else |
| 869 |
self->pw_core = pw_context_connect_fd (self->pw_context, fd, p, 0); |
| 870 |
|
| 871 |
if (!self->pw_core) |
| 872 |
return FALSE; |
| 873 |
|
| 874 |
/* Add the core listeners */ |
| 875 |
pw_core_add_listener (self->pw_core, &self->core_listener, &core_events, self); |
| 876 |
pw_proxy_add_listener((struct pw_proxy*)self->pw_core, |
| 877 |
&self->proxy_core_listener, &proxy_core_events, self); |
| 878 |
|
| 879 |
/* Add the registry listener */ |
| 880 |
wp_registry_attach (&self->registry, self->pw_core); |
| 881 |
|
| 882 |
return TRUE; |
| 883 |
} |
| 884 |
|
| 885 |
/*! |
| 886 |
* \brief Connects this core to the PipeWire server. |
| 887 |
* |
| 888 |
* When connection succeeds, the WpCore \c "connected" signal is emitted. |
| 889 |
* |
| 890 |
* \ingroup wpcore |
| 891 |
* \param self the core |
| 892 |
* \returns TRUE if the core is effectively connected or FALSE if |
| 893 |
* connection failed |
| 894 |
*/ |
| 895 |
gboolean |
| 896 |
wp_core_connect (WpCore *self) |
| 897 |
{ |
| 898 |
g_return_val_if_fail (WP_IS_CORE (self), FALSE); |
| 899 |
|
| 900 |
return wp_core_connect_internal (self, -1); |
| 901 |
} |
| 902 |
|
| 903 |
/*! |
| 904 |
* \brief Connects this core to the PipeWire server on the given socket. |
| 905 |
* |
| 906 |
* When connection succeeds, the WpCore \c "connected" signal is emitted. |
| 907 |
* |
| 908 |
* \ingroup wpcore |
| 909 |
* \param self the core |
| 910 |
* \param fd the connected socket to use, the socket will be closed |
| 911 |
* automatically on disconnect or error |
| 912 |
* \returns TRUE if the core is effectively connected or FALSE if |
| 913 |
* connection failed |
| 914 |
* \since 0.5.6 |
| 915 |
*/ |
| 916 |
gboolean |
| 917 |
wp_core_connect_fd (WpCore *self, int fd) |
| 918 |
{ |
| 919 |
g_return_val_if_fail (WP_IS_CORE (self), FALSE); |
| 920 |
g_return_val_if_fail (fd > -1, FALSE); |
| 921 |
|
| 922 |
return wp_core_connect_internal (self, fd); |
| 923 |
} |
| 924 |
|
| 925 |
/*! |
| 926 |
* \brief Disconnects this core from the PipeWire server. |
| 927 |
* |
| 928 |
* This also effectively destroys all WpCore objects that were created through |
| 929 |
* the registry, destroys the pw_core and finally emits the WpCore |
| 930 |
* \c "disconnected" signal. |
| 931 |
* |
| 932 |
* \ingroup wpcore |
| 933 |
* \param self the core |
| 934 |
*/ |
| 935 |
void |
| 936 |
wp_core_disconnect (WpCore *self) |
| 937 |
{ |
| 938 |
wp_registry_detach (&self->registry); |
| 939 |
|
| 940 |
/* pw_core_disconnect destroys the core proxy |
| 941 |
and we continue in proxy_core_destroy() */ |
| 942 |
if (self->pw_core) |
| 943 |
pw_core_disconnect (self->pw_core); |
| 944 |
} |
| 945 |
|
| 946 |
/*! |
| 947 |
* \brief Checks if the core is connected to PipeWire |
| 948 |
* |
| 949 |
* \ingroup wpcore |
| 950 |
* \param self the core |
| 951 |
* \returns TRUE if the core is connected to PipeWire, FALSE otherwise |
| 952 |
*/ |
| 953 |
gboolean |
| 954 |
wp_core_is_connected (WpCore * self) |
| 955 |
{ |
| 956 |
g_return_val_if_fail (WP_IS_CORE (self), FALSE); |
| 957 |
return self->pw_core && self->info; |
| 958 |
} |
| 959 |
|
| 960 |
/*! |
| 961 |
* \brief Gets the bound id of the client object that is created as a result |
| 962 |
* of this core being connected to the PipeWire daemon |
| 963 |
* |
| 964 |
* \ingroup wpcore |
| 965 |
* \since 0.4.16 |
| 966 |
* \param self the core |
| 967 |
* \returns the bound id of this client |
| 968 |
*/ |
| 969 |
guint32 |
| 970 |
wp_core_get_own_bound_id (WpCore * self) |
| 971 |
{ |
| 972 |
struct pw_client *client; |
| 973 |
|
| 974 |
g_return_val_if_fail (wp_core_is_connected (self), SPA_ID_INVALID); |
| 975 |
|
| 976 |
client = pw_core_get_client (self->pw_core); |
| 977 |
return pw_proxy_get_bound_id ((struct pw_proxy *) client); |
| 978 |
} |
| 979 |
|
| 980 |
/*! |
| 981 |
* \brief Gets the cookie of the core's connected PipeWire instance |
| 982 |
* |
| 983 |
* \ingroup wpcore |
| 984 |
* \param self the core |
| 985 |
* \returns The cookie of the PipeWire instance that \a self is connected to. |
| 986 |
* The cookie is a unique random number for identifying an instance of |
| 987 |
* PipeWire |
| 988 |
*/ |
| 989 |
guint32 |
| 990 |
wp_core_get_remote_cookie (WpCore * self) |
| 991 |
{ |
| 992 |
g_return_val_if_fail (wp_core_is_connected (self), 0); |
| 993 |
|
| 994 |
return self->info->cookie; |
| 995 |
} |
| 996 |
|
| 997 |
/*! |
| 998 |
* \brief Gets the name of the core's connected PipeWire instance |
| 999 |
* \ingroup wpcore |
| 1000 |
* \param self the core |
| 1001 |
* \returns The name of the PipeWire instance that \a self is connected to |
| 1002 |
*/ |
| 1003 |
const gchar * |
| 1004 |
wp_core_get_remote_name (WpCore * self) |
| 1005 |
{ |
| 1006 |
g_return_val_if_fail (wp_core_is_connected (self), NULL); |
| 1007 |
|
| 1008 |
return self->info->name; |
| 1009 |
} |
| 1010 |
|
| 1011 |
/*! |
| 1012 |
* \brief Gets the user name of the core's connected PipeWire instance |
| 1013 |
* \ingroup wpcore |
| 1014 |
* \param self the core |
| 1015 |
* \returns The name of the user that started the PipeWire instance that |
| 1016 |
* \a self is connected to |
| 1017 |
*/ |
| 1018 |
const gchar * |
| 1019 |
wp_core_get_remote_user_name (WpCore * self) |
| 1020 |
{ |
| 1021 |
g_return_val_if_fail (wp_core_is_connected (self), NULL); |
| 1022 |
|
| 1023 |
return self->info->user_name; |
| 1024 |
} |
| 1025 |
|
| 1026 |
/*! |
| 1027 |
* \brief Gets the host name of the core's connected PipeWire instance |
| 1028 |
* \ingroup wpcore |
| 1029 |
* \param self the core |
| 1030 |
* \returns The name of the host where the PipeWire instance that |
| 1031 |
* \a self is connected to is running on |
| 1032 |
*/ |
| 1033 |
const gchar * |
| 1034 |
wp_core_get_remote_host_name (WpCore * self) |
| 1035 |
{ |
| 1036 |
g_return_val_if_fail (wp_core_is_connected (self), NULL); |
| 1037 |
|
| 1038 |
return self->info->host_name; |
| 1039 |
} |
| 1040 |
|
| 1041 |
/*! |
| 1042 |
* \brief Gets the version of the core's connected PipeWire instance |
| 1043 |
* \ingroup wpcore |
| 1044 |
* \param self the core |
| 1045 |
* \returns The version of the PipeWire instance that \a self is connected to |
| 1046 |
*/ |
| 1047 |
const gchar * |
| 1048 |
wp_core_get_remote_version (WpCore * self) |
| 1049 |
{ |
| 1050 |
g_return_val_if_fail (wp_core_is_connected (self), NULL); |
| 1051 |
|
| 1052 |
return self->info->version; |
| 1053 |
} |
| 1054 |
|
| 1055 |
/*! |
| 1056 |
* \brief Gets the properties of the core's connected PipeWire instance |
| 1057 |
* \ingroup wpcore |
| 1058 |
* \param self the core |
| 1059 |
* \returns (transfer full): the properties of the PipeWire instance that |
| 1060 |
* \a self is connected to |
| 1061 |
*/ |
| 1062 |
WpProperties * |
| 1063 |
wp_core_get_remote_properties (WpCore * self) |
| 1064 |
{ |
| 1065 |
g_return_val_if_fail (wp_core_is_connected (self), NULL); |
| 1066 |
|
| 1067 |
return wp_properties_new_wrap_dict (self->info->props); |
| 1068 |
} |
| 1069 |
|
| 1070 |
/*! |
| 1071 |
* \brief Gets the properties of the core |
| 1072 |
* \ingroup wpcore |
| 1073 |
* \param self the core |
| 1074 |
* \returns (transfer full): the properties of \a self |
| 1075 |
*/ |
| 1076 |
WpProperties * |
| 1077 |
wp_core_get_properties (WpCore * self) |
| 1078 |
{ |
| 1079 |
g_return_val_if_fail (WP_IS_CORE (self), NULL); |
| 1080 |
|
| 1081 |
/* pw_core has all the properties of the pw_context, |
| 1082 |
plus our updates, passed in pw_context_connect() */ |
| 1083 |
if (self->pw_core) |
| 1084 |
return wp_properties_new_wrap (pw_core_get_properties (self->pw_core)); |
| 1085 |
|
| 1086 |
/* if there is no connection yet, return the properties of the context */ |
| 1087 |
else if (!self->properties) |
| 1088 |
return wp_properties_new_wrap (pw_context_get_properties (self->pw_context)); |
| 1089 |
|
| 1090 |
/* ... plus any further updates that we got from wp_core_update_properties() */ |
| 1091 |
else { |
| 1092 |
/* we need to copy here in order to augment with the updates */ |
| 1093 |
WpProperties *ret = |
| 1094 |
wp_properties_new_copy (pw_context_get_properties (self->pw_context)); |
| 1095 |
wp_properties_update (ret, self->properties); |
| 1096 |
return ret; |
| 1097 |
} |
| 1098 |
} |
| 1099 |
|
| 1100 |
/*! |
| 1101 |
* \brief Updates the properties of \a self on the connection, making them |
| 1102 |
* appear on the client object that represents this connection. |
| 1103 |
* |
| 1104 |
* If \a self is not connected yet, these properties are stored and passed to |
| 1105 |
* pw_context_connect() when connecting. |
| 1106 |
* |
| 1107 |
* \ingroup wpcore |
| 1108 |
* \param self the core |
| 1109 |
* \param updates (transfer full): updates to apply to the properties of |
| 1110 |
* \a self; this does not need to include properties that have not changed |
| 1111 |
*/ |
| 1112 |
void |
| 1113 |
wp_core_update_properties (WpCore * self, WpProperties * updates) |
| 1114 |
{ |
| 1115 |
g_autoptr (WpProperties) upd = updates; |
| 1116 |
|
| 1117 |
g_return_if_fail (WP_IS_CORE (self)); |
| 1118 |
g_return_if_fail (updates != NULL); |
| 1119 |
|
| 1120 |
/* store updates locally so that |
| 1121 |
- they persist after disconnection |
| 1122 |
- we can pass them to pw_context_connect */ |
| 1123 |
if (!self->properties) |
| 1124 |
self->properties = wp_properties_new_empty (); |
| 1125 |
wp_properties_update (self->properties, upd); |
| 1126 |
|
| 1127 |
if (self->pw_core) |
| 1128 |
pw_core_update_properties (self->pw_core, wp_properties_peek_dict (upd)); |
| 1129 |
} |
| 1130 |
|
| 1131 |
/*! |
| 1132 |
* \brief Adds an idle callback to be called in the same GMainContext as the |
| 1133 |
* one used by this core. |
| 1134 |
* |
| 1135 |
* This is essentially the same as g_idle_add_full(), but it adds the created |
| 1136 |
* GSource on the GMainContext used by this core instead of the default context. |
| 1137 |
* |
| 1138 |
* \ingroup wpcore |
| 1139 |
* \param self the core |
| 1140 |
* \param source (out) (optional): the source |
| 1141 |
* \param function (scope notified)(closure data)(destroy destroy): the function to call |
| 1142 |
* \param data data to pass to \a function |
| 1143 |
* \param destroy (nullable): a function to destroy \a data |
| 1144 |
*/ |
| 1145 |
void |
| 1146 |
wp_core_idle_add (WpCore * self, GSource **source, GSourceFunc function, |
| 1147 |
gpointer data, GDestroyNotify destroy) |
| 1148 |
{ |
| 1149 |
g_autoptr (GSource) s = NULL; |
| 1150 |
|
| 1151 |
g_return_if_fail (WP_IS_CORE (self)); |
| 1152 |
|
| 1153 |
s = g_idle_source_new (); |
| 1154 |
g_source_set_callback (s, function, data, destroy); |
| 1155 |
g_source_attach (s, self->g_main_context); |
| 1156 |
|
| 1157 |
if (source) |
| 1158 |
*source = g_source_ref (s); |
| 1159 |
} |
| 1160 |
|
| 1161 |
/*! |
| 1162 |
* \brief Adds an idle callback to be called in the same GMainContext as |
| 1163 |
* the one used by this core. |
| 1164 |
* |
| 1165 |
* This is the same as wp_core_idle_add(), but it allows you to specify a |
| 1166 |
* GClosure instead of a C callback. |
| 1167 |
* |
| 1168 |
* \ingroup wpcore |
| 1169 |
* \param self the core |
| 1170 |
* \param source (out) (optional): the source |
| 1171 |
* \param closure the closure to invoke |
| 1172 |
*/ |
| 1173 |
void |
| 1174 |
wp_core_idle_add_closure (WpCore * self, GSource **source, GClosure * closure) |
| 1175 |
{ |
| 1176 |
g_autoptr (GSource) s = NULL; |
| 1177 |
|
| 1178 |
g_return_if_fail (WP_IS_CORE (self)); |
| 1179 |
g_return_if_fail (closure != NULL); |
| 1180 |
|
| 1181 |
s = g_idle_source_new (); |
| 1182 |
g_source_set_closure (s, closure); |
| 1183 |
g_source_attach (s, self->g_main_context); |
| 1184 |
|
| 1185 |
if (source) |
| 1186 |
*source = g_source_ref (s); |
| 1187 |
} |
| 1188 |
|
| 1189 |
/*! |
| 1190 |
* \brief Adds a timeout callback to be called at regular intervals in the same |
| 1191 |
* GMainContext as the one used by this core. |
| 1192 |
* |
| 1193 |
* The function is called repeatedly until it returns FALSE, at which point |
| 1194 |
* the timeout is automatically destroyed and the function will not be called |
| 1195 |
* again. The first call to the function will be at the end of the first |
| 1196 |
* interval. |
| 1197 |
|
| 1198 |
* This is essentially the same as g_timeout_add_full(), but it adds |
| 1199 |
* the created GSource on the GMainContext used by this core instead of the |
| 1200 |
* default context. |
| 1201 |
* |
| 1202 |
* \ingroup wpcore |
| 1203 |
* \param self the core |
| 1204 |
* \param source (out) (optional): the source |
| 1205 |
* \param timeout_ms the timeout in milliseconds |
| 1206 |
* \param function (scope notified)(closure data)(destroy destroy): the function to call |
| 1207 |
* \param data data to pass to \a function |
| 1208 |
* \param destroy (nullable): a function to destroy \a data |
| 1209 |
*/ |
| 1210 |
void |
| 1211 |
wp_core_timeout_add (WpCore * self, GSource **source, guint timeout_ms, |
| 1212 |
GSourceFunc function, gpointer data, GDestroyNotify destroy) |
| 1213 |
{ |
| 1214 |
g_autoptr (GSource) s = NULL; |
| 1215 |
|
| 1216 |
g_return_if_fail (WP_IS_CORE (self)); |
| 1217 |
|
| 1218 |
s = g_timeout_source_new (timeout_ms); |
| 1219 |
g_source_set_callback (s, function, data, destroy); |
| 1220 |
g_source_attach (s, self->g_main_context); |
| 1221 |
|
| 1222 |
if (source) |
| 1223 |
*source = g_source_ref (s); |
| 1224 |
} |
| 1225 |
|
| 1226 |
/*! |
| 1227 |
* \brief Adds a timeout callback to be called at regular intervals in the same |
| 1228 |
* GMainContext as the one used by this core. |
| 1229 |
* |
| 1230 |
* This is the same as wp_core_timeout_add(), but it allows you to specify a |
| 1231 |
* GClosure instead of a C callback. |
| 1232 |
* |
| 1233 |
* \ingroup wpcore |
| 1234 |
* \param self the core |
| 1235 |
* \param source (out) (optional): the source |
| 1236 |
* \param timeout_ms the timeout in milliseconds |
| 1237 |
* \param closure the closure to invoke |
| 1238 |
*/ |
| 1239 |
void |
| 1240 |
wp_core_timeout_add_closure (WpCore * self, GSource **source, guint timeout_ms, |
| 1241 |
GClosure * closure) |
| 1242 |
{ |
| 1243 |
g_autoptr (GSource) s = NULL; |
| 1244 |
|
| 1245 |
g_return_if_fail (WP_IS_CORE (self)); |
| 1246 |
g_return_if_fail (closure != NULL); |
| 1247 |
|
| 1248 |
s = g_timeout_source_new (timeout_ms); |
| 1249 |
g_source_set_closure (s, closure); |
| 1250 |
g_source_attach (s, self->g_main_context); |
| 1251 |
|
| 1252 |
if (source) |
| 1253 |
*source = g_source_ref (s); |
| 1254 |
} |
| 1255 |
|
| 1256 |
/*! |
| 1257 |
* \brief Asks the PipeWire server to call the \a callback via an event. |
| 1258 |
* |
| 1259 |
* Since methods are handled in-order and events are delivered |
| 1260 |
* in-order, this can be used as a barrier to ensure all previous |
| 1261 |
* methods and the resulting events have been handled. |
| 1262 |
* |
| 1263 |
* In both success and error cases, \a callback is always called. |
| 1264 |
* Use wp_core_sync_finish() from within the \a callback to determine whether |
| 1265 |
* the operation completed successfully or if an error occurred. |
| 1266 |
* |
| 1267 |
* \ingroup wpcore |
| 1268 |
* \param self the core |
| 1269 |
* \param cancellable (nullable): a GCancellable to cancel the operation |
| 1270 |
* \param callback (scope async)(closure user_data): a function to call when the operation is done |
| 1271 |
* \param user_data data to pass to \a callback |
| 1272 |
* \returns TRUE if the sync operation was started, FALSE if an error |
| 1273 |
* occurred before returning from this function |
| 1274 |
*/ |
| 1275 |
gboolean |
| 1276 |
wp_core_sync (WpCore * self, GCancellable * cancellable, |
| 1277 |
GAsyncReadyCallback callback, gpointer user_data) |
| 1278 |
{ |
| 1279 |
return wp_core_sync_closure (self, cancellable, |
| 1280 |
g_cclosure_new (G_CALLBACK (callback), user_data, NULL)); |
| 1281 |
} |
| 1282 |
|
| 1283 |
static void |
| 1284 |
invoke_closure (GObject * obj, GAsyncResult * res, gpointer data) |
| 1285 |
{ |
| 1286 |
GClosure *closure = data; |
| 1287 |
GValue values[2] = { G_VALUE_INIT, G_VALUE_INIT }; |
| 1288 |
g_value_init (&values[0], G_TYPE_OBJECT); |
| 1289 |
g_value_init (&values[1], G_TYPE_OBJECT); |
| 1290 |
g_value_set_object (&values[0], obj); |
| 1291 |
g_value_set_object (&values[1], res); |
| 1292 |
g_closure_invoke (closure, NULL, 2, values, NULL); |
| 1293 |
g_value_unset (&values[0]); |
| 1294 |
g_value_unset (&values[1]); |
| 1295 |
g_closure_unref (closure); |
| 1296 |
} |
| 1297 |
|
| 1298 |
/*! |
| 1299 |
* \brief Asks the PipeWire server to invoke the \a closure via an event. |
| 1300 |
* |
| 1301 |
* Since methods are handled in-order and events are delivered |
| 1302 |
* in-order, this can be used as a barrier to ensure all previous |
| 1303 |
* methods and the resulting events have been handled. |
| 1304 |
* |
| 1305 |
* In both success and error cases, \a closure is always invoked. |
| 1306 |
* Use wp_core_sync_finish() from within the \a closure to determine whether |
| 1307 |
* the operation completed successfully or if an error occurred. |
| 1308 |
* |
| 1309 |
* \ingroup wpcore |
| 1310 |
* \since 0.4.6 |
| 1311 |
* \param self the core |
| 1312 |
* \param cancellable (nullable): a GCancellable to cancel the operation |
| 1313 |
* \param closure (transfer floating): a closure to invoke when the operation |
| 1314 |
* is done |
| 1315 |
* \returns TRUE if the sync operation was started, FALSE if an error |
| 1316 |
* occurred before returning from this function |
| 1317 |
*/ |
| 1318 |
gboolean |
| 1319 |
wp_core_sync_closure (WpCore * self, GCancellable * cancellable, |
| 1320 |
GClosure * closure) |
| 1321 |
{ |
| 1322 |
g_autoptr (GTask) task = NULL; |
| 1323 |
int seq; |
| 1324 |
|
| 1325 |
g_return_val_if_fail (WP_IS_CORE (self), FALSE); |
| 1326 |
g_return_val_if_fail (closure, FALSE); |
| 1327 |
|
| 1328 |
closure = g_closure_ref (closure); |
| 1329 |
g_closure_sink (closure); |
| 1330 |
if (G_CLOSURE_NEEDS_MARSHAL (closure)) |
| 1331 |
g_closure_set_marshal (closure, g_cclosure_marshal_VOID__OBJECT); |
| 1332 |
|
| 1333 |
task = g_task_new (self, cancellable, invoke_closure, closure); |
| 1334 |
|
| 1335 |
if (G_UNLIKELY (!self->pw_core)) { |
| 1336 |
g_warn_if_reached (); |
| 1337 |
g_task_return_new_error (task, WP_DOMAIN_LIBRARY, |
| 1338 |
WP_LIBRARY_ERROR_INVARIANT, "No pipewire core"); |
| 1339 |
return FALSE; |
| 1340 |
} |
| 1341 |
|
| 1342 |
seq = pw_core_sync (self->pw_core, 0, 0); |
| 1343 |
if (G_UNLIKELY (seq < 0)) { |
| 1344 |
g_task_return_new_error (task, WP_DOMAIN_LIBRARY, |
| 1345 |
WP_LIBRARY_ERROR_OPERATION_FAILED, "pw_core_sync failed: %s", |
| 1346 |
g_strerror (-seq)); |
| 1347 |
return FALSE; |
| 1348 |
} |
| 1349 |
|
| 1350 |
wp_debug_object (self, "sync, seq 0x%x, task " WP_OBJECT_FORMAT, |
| 1351 |
seq, WP_OBJECT_ARGS (task)); |
| 1352 |
|
| 1353 |
g_hash_table_insert (self->async_tasks, GINT_TO_POINTER (seq), |
| 1354 |
g_steal_pointer (&task)); |
| 1355 |
return TRUE; |
| 1356 |
} |
| 1357 |
|
| 1358 |
/*! |
| 1359 |
* \brief This function is meant to be called from within the callback of |
| 1360 |
* wp_core_sync() in order to determine the success or failure of the operation. |
| 1361 |
* |
| 1362 |
* \ingroup wpcore |
| 1363 |
* \param self the core |
| 1364 |
* \param res a GAsyncResult |
| 1365 |
* \param error (out) (optional): the error that occurred, if any |
| 1366 |
* \returns TRUE if the operation succeeded, FALSE otherwise |
| 1367 |
*/ |
| 1368 |
gboolean |
| 1369 |
wp_core_sync_finish (WpCore * self, GAsyncResult * res, GError ** error) |
| 1370 |
{ |
| 1371 |
g_return_val_if_fail (WP_IS_CORE (self), FALSE); |
| 1372 |
g_return_val_if_fail (g_task_is_valid (res, self), FALSE); |
| 1373 |
|
| 1374 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 1375 |
} |
| 1376 |
|
| 1377 |
|
| 1378 |
/*! |
| 1379 |
* \brief Finds a registered object |
| 1380 |
* |
| 1381 |
* \ingroup wpcore |
| 1382 |
* \param self the core |
| 1383 |
* \param func (scope call): a function that takes the object being searched |
| 1384 |
* as the first argument and \a data as the second. it should return TRUE if |
| 1385 |
* the object is found or FALSE otherwise |
| 1386 |
* \param data the second argument to \a func |
| 1387 |
* \returns (transfer full) (type GObject*) (nullable): the registered object |
| 1388 |
* or NULL if not found |
| 1389 |
*/ |
| 1390 |
gpointer |
| 1391 |
wp_core_find_object (WpCore * self, GEqualFunc func, gconstpointer data) |
| 1392 |
{ |
| 1393 |
GObject *object; |
| 1394 |
guint i; |
| 1395 |
|
| 1396 |
g_return_val_if_fail (WP_IS_CORE (self), NULL); |
| 1397 |
|
| 1398 |
/* prevent bad things when called from within wp_registry_clear() */ |
| 1399 |
if (G_UNLIKELY (!self->registry.objects)) |
| 1400 |
return NULL; |
| 1401 |
|
| 1402 |
for (i = 0; i < self->registry.objects->len; i++) { |
| 1403 |
object = g_ptr_array_index (self->registry.objects, i); |
| 1404 |
if (func (object, data)) |
| 1405 |
return g_object_ref (object); |
| 1406 |
} |
| 1407 |
|
| 1408 |
return NULL; |
| 1409 |
} |
| 1410 |
|
| 1411 |
/*! |
| 1412 |
* \brief Registers \a obj with the core, making it appear on WpObjectManager |
| 1413 |
* instances as well. |
| 1414 |
* |
| 1415 |
* The core will also maintain a ref to that object until it |
| 1416 |
* is removed. |
| 1417 |
* |
| 1418 |
* \ingroup wpcore |
| 1419 |
* \param self the core |
| 1420 |
* \param obj (transfer full) (type GObject*): the object to register |
| 1421 |
*/ |
| 1422 |
void |
| 1423 |
wp_core_register_object (WpCore * self, gpointer obj) |
| 1424 |
{ |
| 1425 |
g_return_if_fail (WP_IS_CORE (self)); |
| 1426 |
g_return_if_fail (G_IS_OBJECT (obj)); |
| 1427 |
|
| 1428 |
/* prevent bad things when called from within wp_registry_clear() */ |
| 1429 |
if (G_UNLIKELY (!self->registry.objects)) { |
| 1430 |
g_object_unref (obj); |
| 1431 |
return; |
| 1432 |
} |
| 1433 |
|
| 1434 |
/* ensure the registered object is associated with this core */ |
| 1435 |
if (WP_IS_OBJECT (obj)) { |
| 1436 |
g_autoptr (WpCore) obj_core = wp_object_get_core (WP_OBJECT (obj)); |
| 1437 |
g_return_if_fail (obj_core == self); |
| 1438 |
} |
| 1439 |
|
| 1440 |
g_ptr_array_add (self->registry.objects, obj); |
| 1441 |
|
| 1442 |
/* notify object managers */ |
| 1443 |
wp_registry_notify_add_object (&self->registry, obj); |
| 1444 |
} |
| 1445 |
|
| 1446 |
/*! |
| 1447 |
* \brief Detaches and unrefs the specified object from this core. |
| 1448 |
* |
| 1449 |
* \ingroup wpcore |
| 1450 |
* \param self the core |
| 1451 |
* \param obj (transfer none) (type GObject*): a pointer to the object to remove |
| 1452 |
*/ |
| 1453 |
void |
| 1454 |
wp_core_remove_object (WpCore * self, gpointer obj) |
| 1455 |
{ |
| 1456 |
g_return_if_fail (WP_IS_CORE (self)); |
| 1457 |
g_return_if_fail (G_IS_OBJECT (obj)); |
| 1458 |
|
| 1459 |
/* prevent bad things when called from within wp_registry_clear() */ |
| 1460 |
if (G_UNLIKELY (!self->registry.objects)) |
| 1461 |
return; |
| 1462 |
|
| 1463 |
/* notify object managers */ |
| 1464 |
wp_registry_notify_rm_object (&self->registry, obj); |
| 1465 |
|
| 1466 |
g_ptr_array_remove_fast (self->registry.objects, obj); |
| 1467 |
} |
| 1468 |
|
| 1469 |
/*! |
| 1470 |
* \brief Test if a global feature is provided |
| 1471 |
* |
| 1472 |
* \ingroup wpcore |
| 1473 |
* \param self the core |
| 1474 |
* \param feature the feature name |
| 1475 |
* \returns TRUE if the feature is provided, FALSE otherwise |
| 1476 |
*/ |
| 1477 |
gboolean |
| 1478 |
wp_core_test_feature (WpCore * self, const gchar * feature) |
| 1479 |
{ |
| 1480 |
return g_ptr_array_find_with_equal_func (self->registry.features, feature, |
| 1481 |
g_str_equal, NULL); |
| 1482 |
} |
| 1483 |
|
| 1484 |
WpRegistry * |
| 1485 |
wp_core_get_registry (WpCore * self) |
| 1486 |
{ |
| 1487 |
return &self->registry; |
| 1488 |
} |
| 1489 |
|
| 1490 |
WpCore * |
| 1491 |
wp_registry_get_core (WpRegistry * self) |
| 1492 |
{ |
| 1493 |
return SPA_CONTAINER_OF (self, WpCore, registry); |
| 1494 |
} |