| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2020 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <wp/wp.h> |
| 10 |
#include <pipewire/pipewire.h> |
| 11 |
#include <spa/debug/types.h> |
| 12 |
#include <spa/param/audio/type-info.h> |
| 13 |
|
| 14 |
WP_DEFINE_LOCAL_LOG_TOPIC ("m-si-standard-link") |
| 15 |
|
| 16 |
#define SI_FACTORY_NAME "si-standard-link" |
| 17 |
|
| 18 |
struct _WpSiStandardLink |
| 19 |
{ |
| 20 |
WpSessionItem parent; |
| 21 |
|
| 22 |
/* configuration */ |
| 23 |
GWeakRef out_item; |
| 24 |
GWeakRef in_item; |
| 25 |
const gchar *out_item_port_context; |
| 26 |
const gchar *in_item_port_context; |
| 27 |
gboolean passthrough; |
| 28 |
|
| 29 |
/* activate */ |
| 30 |
GPtrArray *node_links; |
| 31 |
guint n_active_links; |
| 32 |
guint n_failed_links; |
| 33 |
guint n_async_ops_wait; |
| 34 |
}; |
| 35 |
|
| 36 |
enum { |
| 37 |
SIGNAL_LINK_ERROR, |
| 38 |
LAST_SIGNAL, |
| 39 |
}; |
| 40 |
|
| 41 |
static guint signals[LAST_SIGNAL] = { 0 }; |
| 42 |
|
| 43 |
static void si_standard_link_link_init (WpSiLinkInterface * iface); |
| 44 |
|
| 45 |
G_DECLARE_FINAL_TYPE (WpSiStandardLink, si_standard_link, WP, SI_STANDARD_LINK, |
| 46 |
WpSessionItem) |
| 47 |
G_DEFINE_TYPE_WITH_CODE (WpSiStandardLink, si_standard_link, |
| 48 |
WP_TYPE_SESSION_ITEM, |
| 49 |
G_IMPLEMENT_INTERFACE (WP_TYPE_SI_LINK, si_standard_link_link_init)) |
| 50 |
|
| 51 |
static void |
| 52 |
si_standard_link_init (WpSiStandardLink * self) |
| 53 |
{ |
| 54 |
g_weak_ref_init (&self->out_item, NULL); |
| 55 |
g_weak_ref_init (&self->in_item, NULL); |
| 56 |
} |
| 57 |
|
| 58 |
static void |
| 59 |
si_standard_link_reset (WpSessionItem * item) |
| 60 |
{ |
| 61 |
WpSiStandardLink *self = WP_SI_STANDARD_LINK (item); |
| 62 |
|
| 63 |
/* deactivate first */ |
| 64 |
wp_object_deactivate (WP_OBJECT (self), |
| 65 |
WP_SESSION_ITEM_FEATURE_ACTIVE | WP_SESSION_ITEM_FEATURE_EXPORTED); |
| 66 |
|
| 67 |
/* reset */ |
| 68 |
g_weak_ref_set (&self->out_item, NULL); |
| 69 |
g_weak_ref_set (&self->in_item, NULL); |
| 70 |
self->out_item_port_context = NULL; |
| 71 |
self->in_item_port_context = NULL; |
| 72 |
self->passthrough = FALSE; |
| 73 |
|
| 74 |
WP_SESSION_ITEM_CLASS (si_standard_link_parent_class)->reset (item); |
| 75 |
} |
| 76 |
|
| 77 |
static WpSessionItem * |
| 78 |
get_and_validate_item (WpProperties * props, const gchar *key) |
| 79 |
{ |
| 80 |
WpSessionItem *res = NULL; |
| 81 |
const gchar *str = NULL; |
| 82 |
|
| 83 |
str = wp_properties_get (props, key); |
| 84 |
if (!str || sscanf(str, "%p", &res) != 1 || !WP_IS_SI_LINKABLE (res) || |
| 85 |
!(wp_object_test_active_features (WP_OBJECT (res), |
| 86 |
WP_SESSION_ITEM_FEATURE_ACTIVE))) |
| 87 |
return NULL; |
| 88 |
|
| 89 |
return res; |
| 90 |
} |
| 91 |
|
| 92 |
static gboolean |
| 93 |
si_standard_link_configure (WpSessionItem * item, WpProperties * p) |
| 94 |
{ |
| 95 |
WpSiStandardLink *self = WP_SI_STANDARD_LINK (item); |
| 96 |
g_autoptr (WpProperties) si_props = wp_properties_ensure_unique_owner (p); |
| 97 |
WpSessionItem *out_item, *in_item; |
| 98 |
const gchar *str; |
| 99 |
|
| 100 |
/* reset previous config */ |
| 101 |
si_standard_link_reset (item); |
| 102 |
|
| 103 |
out_item = get_and_validate_item (si_props, "out.item"); |
| 104 |
if (!out_item) |
| 105 |
return FALSE; |
| 106 |
wp_properties_setf (si_props, "out.item.id", "%u", |
| 107 |
wp_object_get_id (WP_OBJECT (out_item))); |
| 108 |
|
| 109 |
in_item = get_and_validate_item (si_props, "in.item"); |
| 110 |
if (!in_item) |
| 111 |
return FALSE; |
| 112 |
wp_properties_setf (si_props, "in.item.id", "%u", |
| 113 |
wp_object_get_id (WP_OBJECT (in_item))); |
| 114 |
|
| 115 |
self->out_item_port_context = wp_properties_get (si_props, |
| 116 |
"out.item.port.context"); |
| 117 |
|
| 118 |
self->in_item_port_context = wp_properties_get (si_props, |
| 119 |
"in.item.port.context"); |
| 120 |
|
| 121 |
str = wp_properties_get (si_props, "passthrough"); |
| 122 |
self->passthrough = str && pw_properties_parse_bool (str); |
| 123 |
|
| 124 |
g_weak_ref_set(&self->out_item, out_item); |
| 125 |
g_weak_ref_set(&self->in_item, in_item); |
| 126 |
|
| 127 |
wp_properties_set (si_props, "item.factory.name", SI_FACTORY_NAME); |
| 128 |
wp_session_item_set_properties (WP_SESSION_ITEM (self), |
| 129 |
g_steal_pointer (&si_props)); |
| 130 |
return TRUE; |
| 131 |
} |
| 132 |
|
| 133 |
static gpointer |
| 134 |
si_standard_link_get_associated_proxy (WpSessionItem * item, GType proxy_type) |
| 135 |
{ |
| 136 |
return NULL; |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
request_destroy_link (gpointer data, gpointer user_data) |
| 141 |
{ |
| 142 |
WpLink *link = WP_LINK (data); |
| 143 |
|
| 144 |
wp_global_proxy_request_destroy (WP_GLOBAL_PROXY (link)); |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
clear_node_links (GPtrArray **node_links_p) |
| 149 |
{ |
| 150 |
/* |
| 151 |
* Something else (eg. object managers) may be keeping the WpLink |
| 152 |
* objects alive. Deactivate the links now, to destroy the PW objects. |
| 153 |
*/ |
| 154 |
if (*node_links_p) |
| 155 |
g_ptr_array_foreach (*node_links_p, request_destroy_link, NULL); |
| 156 |
|
| 157 |
g_clear_pointer (node_links_p, g_ptr_array_unref); |
| 158 |
} |
| 159 |
|
| 160 |
static void |
| 161 |
si_standard_link_disable_active (WpSessionItem *si) |
| 162 |
{ |
| 163 |
WpSiStandardLink *self = WP_SI_STANDARD_LINK (si); |
| 164 |
g_autoptr (WpSessionItem) si_out = g_weak_ref_get (&self->out_item); |
| 165 |
g_autoptr (WpSessionItem) si_in = g_weak_ref_get (&self->in_item); |
| 166 |
WpSiAcquisition *out_acquisition, *in_acquisition; |
| 167 |
|
| 168 |
if (si_out) { |
| 169 |
out_acquisition = wp_si_linkable_get_acquisition ( |
| 170 |
WP_SI_LINKABLE (si_out)); |
| 171 |
if (out_acquisition) |
| 172 |
wp_si_acquisition_release (out_acquisition, WP_SI_LINK (self), |
| 173 |
WP_SI_LINKABLE (si_out)); |
| 174 |
} |
| 175 |
if (si_in) { |
| 176 |
in_acquisition = wp_si_linkable_get_acquisition (WP_SI_LINKABLE (si_in)); |
| 177 |
if (in_acquisition) |
| 178 |
wp_si_acquisition_release (in_acquisition, WP_SI_LINK (self), |
| 179 |
WP_SI_LINKABLE (si_in)); |
| 180 |
} |
| 181 |
|
| 182 |
clear_node_links (&self->node_links); |
| 183 |
|
| 184 |
self->n_active_links = 0; |
| 185 |
self->n_failed_links = 0; |
| 186 |
self->n_async_ops_wait = 0; |
| 187 |
|
| 188 |
wp_object_update_features (WP_OBJECT (self), 0, |
| 189 |
WP_SESSION_ITEM_FEATURE_ACTIVE); |
| 190 |
} |
| 191 |
|
| 192 |
static void |
| 193 |
on_link_activated (WpObject * proxy, GAsyncResult * res, |
| 194 |
WpTransition * transition) |
| 195 |
{ |
| 196 |
WpSiStandardLink *self = wp_transition_get_source_object (transition); |
| 197 |
guint len = self->node_links ? self->node_links->len : 0; |
| 198 |
g_autoptr (GError) error = NULL; |
| 199 |
|
| 200 |
/* Count the number of failed and active links */ |
| 201 |
if (wp_object_activate_finish (proxy, res, &error)) { |
| 202 |
self->n_active_links++; |
| 203 |
} else { |
| 204 |
self->n_failed_links++; |
| 205 |
wp_info_object (self, "Failed to activate link %p: %s", proxy, |
| 206 |
error->message); |
| 207 |
} |
| 208 |
|
| 209 |
/* Wait for all links to finish activation */ |
| 210 |
if (self->n_failed_links + self->n_active_links != len) |
| 211 |
return; |
| 212 |
|
| 213 |
/* We only active feature if all links activated successfully */ |
| 214 |
if (self->n_failed_links > 0) { |
| 215 |
clear_node_links (&self->node_links); |
| 216 |
wp_transition_return_error (transition, g_error_new ( |
| 217 |
WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 218 |
"%d of %d PipeWire links failed to activate", |
| 219 |
self->n_failed_links, len)); |
| 220 |
} else { |
| 221 |
wp_object_update_features (WP_OBJECT (self), |
| 222 |
WP_SESSION_ITEM_FEATURE_ACTIVE, 0); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
static void |
| 227 |
on_link_state_changed (WpLink *link, WpLinkState old_state, |
| 228 |
WpLinkState new_state, WpSiStandardLink * self) |
| 229 |
{ |
| 230 |
if (new_state == WP_LINK_STATE_ERROR) { |
| 231 |
const gchar *error_msg; |
| 232 |
wp_link_get_state (link, &error_msg); |
| 233 |
g_signal_emit_by_name (self, "link-error", error_msg); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
struct port |
| 238 |
{ |
| 239 |
guint32 node_id; |
| 240 |
guint32 port_id; |
| 241 |
guint32 channel; |
| 242 |
gboolean visited; |
| 243 |
}; |
| 244 |
|
| 245 |
static inline bool |
| 246 |
channel_is_aux(guint32 channel) |
| 247 |
{ |
| 248 |
return channel >= SPA_AUDIO_CHANNEL_START_Aux && |
| 249 |
channel <= SPA_AUDIO_CHANNEL_LAST_Aux; |
| 250 |
} |
| 251 |
|
| 252 |
static inline int |
| 253 |
score_ports(struct port *out, struct port *in) |
| 254 |
{ |
| 255 |
int score = 0; |
| 256 |
|
| 257 |
if (out->channel == in->channel) |
| 258 |
score += 100; |
| 259 |
else if ((out->channel == SPA_AUDIO_CHANNEL_SL && in->channel == SPA_AUDIO_CHANNEL_RL) || |
| 260 |
(out->channel == SPA_AUDIO_CHANNEL_RL && in->channel == SPA_AUDIO_CHANNEL_SL) || |
| 261 |
(out->channel == SPA_AUDIO_CHANNEL_SR && in->channel == SPA_AUDIO_CHANNEL_RR) || |
| 262 |
(out->channel == SPA_AUDIO_CHANNEL_RR && in->channel == SPA_AUDIO_CHANNEL_SR)) |
| 263 |
score += 60; |
| 264 |
else if ((out->channel == SPA_AUDIO_CHANNEL_FC && in->channel == SPA_AUDIO_CHANNEL_MONO) || |
| 265 |
(out->channel == SPA_AUDIO_CHANNEL_MONO && in->channel == SPA_AUDIO_CHANNEL_FC)) |
| 266 |
score += 50; |
| 267 |
else if (in->channel == SPA_AUDIO_CHANNEL_UNKNOWN || |
| 268 |
in->channel == SPA_AUDIO_CHANNEL_MONO || |
| 269 |
out->channel == SPA_AUDIO_CHANNEL_UNKNOWN || |
| 270 |
out->channel == SPA_AUDIO_CHANNEL_MONO) |
| 271 |
score += 10; |
| 272 |
else if (channel_is_aux(in->channel) != channel_is_aux(out->channel)) |
| 273 |
score += 7; |
| 274 |
if (score > 0 && !in->visited) |
| 275 |
score += 5; |
| 276 |
if (score <= 10) |
| 277 |
score = 0; |
| 278 |
return score; |
| 279 |
} |
| 280 |
|
| 281 |
static gboolean |
| 282 |
create_links (WpSiStandardLink * self, WpTransition * transition, |
| 283 |
GVariant * out_ports, GVariant * in_ports) |
| 284 |
{ |
| 285 |
g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self)); |
| 286 |
g_autoptr (GArray) in_ports_arr = NULL; |
| 287 |
struct port out_port = {0}; |
| 288 |
struct port *in_port; |
| 289 |
GVariantIter *iter = NULL; |
| 290 |
guint i; |
| 291 |
|
| 292 |
/* Clear old links if any */ |
| 293 |
self->n_active_links = 0; |
| 294 |
self->n_failed_links = 0; |
| 295 |
clear_node_links (&self->node_links); |
| 296 |
|
| 297 |
/* tuple format: |
| 298 |
uint32 node_id; |
| 299 |
uint32 port_id; |
| 300 |
uint32 channel; // enum spa_audio_channel |
| 301 |
*/ |
| 302 |
if (!g_variant_is_of_type (out_ports, G_VARIANT_TYPE("a(uuu)"))) |
| 303 |
return FALSE; |
| 304 |
if (!g_variant_is_of_type (in_ports, G_VARIANT_TYPE("a(uuu)"))) |
| 305 |
return FALSE; |
| 306 |
|
| 307 |
i = g_variant_n_children (in_ports); |
| 308 |
if (i == 0) |
| 309 |
return FALSE; |
| 310 |
|
| 311 |
self->node_links = g_ptr_array_new_with_free_func (g_object_unref); |
| 312 |
|
| 313 |
/* transfer the in ports to an array so that we can |
| 314 |
mark them when they are linked */ |
| 315 |
in_ports_arr = g_array_sized_new (FALSE, TRUE, sizeof (struct port), i + 1); |
| 316 |
g_array_set_size (in_ports_arr, i + 1); |
| 317 |
g_variant_get (in_ports, "a(uuu)", &iter); |
| 318 |
i = 0; |
| 319 |
do { |
| 320 |
in_port = &g_array_index (in_ports_arr, struct port, i++); |
| 321 |
} while (g_variant_iter_loop (iter, "(uuu)", &in_port->node_id, |
| 322 |
&in_port->port_id, &in_port->channel)); |
| 323 |
g_variant_iter_free (iter); |
| 324 |
|
| 325 |
/* now loop over the out ports and figure out where they should be linked */ |
| 326 |
g_variant_get (out_ports, "a(uuu)", &iter); |
| 327 |
while (g_variant_iter_loop (iter, "(uuu)", &out_port.node_id, |
| 328 |
&out_port.port_id, &out_port.channel)) |
| 329 |
{ |
| 330 |
int best_score = 0; |
| 331 |
struct port *best_port = NULL; |
| 332 |
WpProperties *props = NULL; |
| 333 |
WpLink *link; |
| 334 |
|
| 335 |
for (i = 0; i < in_ports_arr->len - 1; i++) { |
| 336 |
in_port = &g_array_index (in_ports_arr, struct port, i); |
| 337 |
int score = score_ports (&out_port, in_port); |
| 338 |
if (score > best_score) { |
| 339 |
best_score = score; |
| 340 |
best_port = in_port; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/* not all output ports have to be linked ... */ |
| 345 |
if (!best_port || best_port->visited) |
| 346 |
continue; |
| 347 |
|
| 348 |
best_port->visited = TRUE; |
| 349 |
|
| 350 |
/* Create the properties */ |
| 351 |
props = wp_properties_new_empty (); |
| 352 |
wp_properties_setf (props, PW_KEY_LINK_OUTPUT_NODE, "%u", out_port.node_id); |
| 353 |
wp_properties_setf (props, PW_KEY_LINK_OUTPUT_PORT, "%u", out_port.port_id); |
| 354 |
wp_properties_setf (props, PW_KEY_LINK_INPUT_NODE, "%u", best_port->node_id); |
| 355 |
wp_properties_setf (props, PW_KEY_LINK_INPUT_PORT, "%u", best_port->port_id); |
| 356 |
|
| 357 |
wp_debug_object (self, "create pw link: %u:%u (%s) -> %u:%u (%s)", |
| 358 |
out_port.node_id, out_port.port_id, |
| 359 |
spa_debug_type_find_name (spa_type_audio_channel, out_port.channel), |
| 360 |
best_port->node_id, best_port->port_id, |
| 361 |
spa_debug_type_find_name (spa_type_audio_channel, best_port->channel)); |
| 362 |
|
| 363 |
/* create the link */ |
| 364 |
link = wp_link_new_from_factory (core, "link-factory", props); |
| 365 |
g_ptr_array_add (self->node_links, link); |
| 366 |
|
| 367 |
/* activate to ensure it is created without errors */ |
| 368 |
wp_object_activate_closure (WP_OBJECT (link), |
| 369 |
WP_OBJECT_FEATURES_ALL, NULL, |
| 370 |
g_cclosure_new_object ( |
| 371 |
(GCallback) on_link_activated, G_OBJECT (transition))); |
| 372 |
|
| 373 |
g_signal_connect_object (link, "state-changed", |
| 374 |
G_CALLBACK (on_link_state_changed), self, 0); |
| 375 |
} |
| 376 |
g_variant_iter_free (iter); |
| 377 |
return self->node_links->len > 0; |
| 378 |
} |
| 379 |
|
| 380 |
static void |
| 381 |
get_ports_and_create_links (WpSiStandardLink *self, WpTransition *transition) |
| 382 |
{ |
| 383 |
g_autoptr (WpSiLinkable) si_out = NULL; |
| 384 |
g_autoptr (WpSiLinkable) si_in = NULL; |
| 385 |
g_autoptr (GVariant) out_ports = NULL; |
| 386 |
g_autoptr (GVariant) in_ports = NULL; |
| 387 |
|
| 388 |
si_out = WP_SI_LINKABLE (g_weak_ref_get (&self->out_item)); |
| 389 |
si_in = WP_SI_LINKABLE (g_weak_ref_get (&self->in_item)); |
| 390 |
|
| 391 |
if (!si_out || !si_in || |
| 392 |
!wp_object_test_active_features (WP_OBJECT (si_out), WP_SESSION_ITEM_FEATURE_ACTIVE) || |
| 393 |
!wp_object_test_active_features (WP_OBJECT (si_in), WP_SESSION_ITEM_FEATURE_ACTIVE)) { |
| 394 |
wp_transition_return_error (transition, |
| 395 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 396 |
"some node was destroyed before the link was created")); |
| 397 |
return; |
| 398 |
} |
| 399 |
|
| 400 |
out_ports = wp_si_linkable_get_ports (si_out, self->out_item_port_context); |
| 401 |
in_ports = wp_si_linkable_get_ports (si_in, self->in_item_port_context); |
| 402 |
if (!out_ports || !in_ports) { |
| 403 |
wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY, |
| 404 |
WP_LIBRARY_ERROR_INVARIANT, |
| 405 |
"Failed to create links because one of the nodes has no ports")); |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
if (!create_links (self, transition, out_ports, in_ports)) |
| 410 |
wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY, |
| 411 |
WP_LIBRARY_ERROR_INVARIANT, |
| 412 |
"Failed to create links because of wrong ports")); |
| 413 |
} |
| 414 |
|
| 415 |
static void |
| 416 |
on_adapters_ready (GObject *obj, GAsyncResult * res, gpointer p) |
| 417 |
{ |
| 418 |
WpTransition *transition = p; |
| 419 |
WpSiStandardLink *self = wp_transition_get_source_object (transition); |
| 420 |
g_autoptr (GError) error = NULL; |
| 421 |
|
| 422 |
wp_si_adapter_set_ports_format_finish (WP_SI_ADAPTER (obj), res, &error); |
| 423 |
if (error) { |
| 424 |
wp_transition_return_error (transition, g_steal_pointer (&error)); |
| 425 |
return; |
| 426 |
} |
| 427 |
|
| 428 |
/* create links */ |
| 429 |
get_ports_and_create_links (self, transition); |
| 430 |
} |
| 431 |
|
| 432 |
struct adapter |
| 433 |
{ |
| 434 |
WpSiAdapter *si; |
| 435 |
gboolean is_device; |
| 436 |
gboolean dont_remix; |
| 437 |
gboolean unpositioned; |
| 438 |
gboolean no_dsp; |
| 439 |
WpSpaPod *fmt; |
| 440 |
const gchar *mode; |
| 441 |
}; |
| 442 |
|
| 443 |
static void |
| 444 |
adapter_free (struct adapter *a) |
| 445 |
{ |
| 446 |
g_clear_object (&a->si); |
| 447 |
g_clear_pointer (&a->fmt, wp_spa_pod_unref); |
| 448 |
g_slice_free (struct adapter, a); |
| 449 |
} |
| 450 |
|
| 451 |
static void |
| 452 |
configure_adapter (WpSiStandardLink *self, WpTransition *transition, |
| 453 |
struct adapter *main, struct adapter *other) |
| 454 |
{ |
| 455 |
/* configure other to have the same format with main, if necessary */ |
| 456 |
if (!main->no_dsp && !other->dont_remix && !other->unpositioned && !main->unpositioned) { |
| 457 |
/* if formats are the same, no need to reconfigure */ |
| 458 |
if (other->fmt && !g_strcmp0 (main->mode, other->mode) |
| 459 |
&& wp_spa_pod_equal (main->fmt, other->fmt)) |
| 460 |
get_ports_and_create_links (self, transition); |
| 461 |
else |
| 462 |
wp_si_adapter_set_ports_format (other->si, wp_spa_pod_ref (main->fmt), |
| 463 |
"dsp", on_adapters_ready, transition); |
| 464 |
} else if (main->no_dsp) { |
| 465 |
/* if formats are the same, no need to reconfigure */ |
| 466 |
if (other->fmt && !g_strcmp0 (other->mode, "convert") |
| 467 |
&& wp_spa_pod_equal (main->fmt, other->fmt)) |
| 468 |
get_ports_and_create_links (self, transition); |
| 469 |
else |
| 470 |
wp_si_adapter_set_ports_format (other->si, wp_spa_pod_ref (main->fmt), |
| 471 |
"convert", on_adapters_ready, transition); |
| 472 |
} else { |
| 473 |
/* dont_remix or unpositioned case */ |
| 474 |
if (other->fmt) |
| 475 |
get_ports_and_create_links (self, transition); |
| 476 |
else |
| 477 |
wp_si_adapter_set_ports_format (other->si, NULL, |
| 478 |
"dsp", on_adapters_ready, transition); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
static void |
| 483 |
on_main_adapter_ready (GObject *obj, GAsyncResult * res, gpointer p) |
| 484 |
{ |
| 485 |
WpTransition *transition = p; |
| 486 |
WpSiStandardLink *self = wp_transition_get_source_object (transition); |
| 487 |
g_autoptr (GError) error = NULL; |
| 488 |
struct adapter *main, *other; |
| 489 |
|
| 490 |
wp_si_adapter_set_ports_format_finish (WP_SI_ADAPTER (obj), res, &error); |
| 491 |
if (error) { |
| 492 |
wp_transition_return_error (transition, g_steal_pointer (&error)); |
| 493 |
return; |
| 494 |
} |
| 495 |
|
| 496 |
main = g_object_get_data (G_OBJECT (transition), "adapter_main"); |
| 497 |
other = g_object_get_data (G_OBJECT (transition), "adapter_other"); |
| 498 |
|
| 499 |
if (!wp_object_test_active_features (WP_OBJECT (main->si), WP_SESSION_ITEM_FEATURE_ACTIVE) || |
| 500 |
!wp_object_test_active_features (WP_OBJECT (other->si), WP_SESSION_ITEM_FEATURE_ACTIVE)) { |
| 501 |
wp_transition_return_error (transition, |
| 502 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 503 |
"some node was destroyed before the link was created")); |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
if (self->passthrough) { |
| 508 |
wp_si_adapter_set_ports_format (other->si, NULL, "passthrough", |
| 509 |
on_adapters_ready, transition); |
| 510 |
} else { |
| 511 |
/* get the up-to-date formats */ |
| 512 |
g_clear_pointer (&main->fmt, wp_spa_pod_unref); |
| 513 |
g_clear_pointer (&other->fmt, wp_spa_pod_unref); |
| 514 |
main->fmt = wp_si_adapter_get_ports_format (main->si, &main->mode); |
| 515 |
other->fmt = wp_si_adapter_get_ports_format (other->si, &other->mode); |
| 516 |
|
| 517 |
/* now configure other based on main */ |
| 518 |
configure_adapter (self, transition, main, other); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
static void |
| 523 |
configure_and_link_adapters (WpSiStandardLink *self, WpTransition *transition) |
| 524 |
{ |
| 525 |
g_autoptr (WpSiAdapter) si_out = |
| 526 |
WP_SI_ADAPTER (g_weak_ref_get (&self->out_item)); |
| 527 |
g_autoptr (WpSiAdapter) si_in = |
| 528 |
WP_SI_ADAPTER (g_weak_ref_get (&self->in_item)); |
| 529 |
struct adapter *out, *in, *main, *other; |
| 530 |
const gchar *str = NULL; |
| 531 |
|
| 532 |
if (!si_out || !si_in || |
| 533 |
!wp_object_test_active_features (WP_OBJECT (si_out), WP_SESSION_ITEM_FEATURE_ACTIVE) || |
| 534 |
!wp_object_test_active_features (WP_OBJECT (si_in), WP_SESSION_ITEM_FEATURE_ACTIVE)) { |
| 535 |
wp_transition_return_error (transition, |
| 536 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 537 |
"some node was destroyed before the link was created")); |
| 538 |
return; |
| 539 |
} |
| 540 |
|
| 541 |
out = g_slice_new0 (struct adapter); |
| 542 |
in = g_slice_new0 (struct adapter); |
| 543 |
out->si = g_steal_pointer (&si_out); |
| 544 |
in->si = g_steal_pointer (&si_in); |
| 545 |
|
| 546 |
str = wp_session_item_get_property (WP_SESSION_ITEM (out->si), "item.node.type"); |
| 547 |
out->is_device = !g_strcmp0 (str, "device"); |
| 548 |
str = wp_session_item_get_property (WP_SESSION_ITEM (in->si), "item.node.type"); |
| 549 |
in->is_device = !g_strcmp0 (str, "device"); |
| 550 |
|
| 551 |
str = wp_session_item_get_property (WP_SESSION_ITEM (out->si), "stream.dont-remix"); |
| 552 |
out->dont_remix = str && pw_properties_parse_bool (str); |
| 553 |
str = wp_session_item_get_property (WP_SESSION_ITEM (in->si), "stream.dont-remix"); |
| 554 |
in->dont_remix = str && pw_properties_parse_bool (str); |
| 555 |
|
| 556 |
str = wp_session_item_get_property (WP_SESSION_ITEM (out->si), "item.node.unpositioned"); |
| 557 |
out->unpositioned = str && pw_properties_parse_bool (str); |
| 558 |
str = wp_session_item_get_property (WP_SESSION_ITEM (in->si), "item.node.unpositioned"); |
| 559 |
in->unpositioned = str && pw_properties_parse_bool (str); |
| 560 |
|
| 561 |
str = wp_session_item_get_property (WP_SESSION_ITEM (out->si), "item.features.no-dsp"); |
| 562 |
out->no_dsp = str && pw_properties_parse_bool (str); |
| 563 |
str = wp_session_item_get_property (WP_SESSION_ITEM (in->si), "item.features.no-dsp"); |
| 564 |
in->no_dsp = str && pw_properties_parse_bool (str); |
| 565 |
|
| 566 |
wp_debug_object (self, "out [device:%d, dont_remix %d, unpos %d], " |
| 567 |
"in: [device %d, dont_remix %d, unpos %d]", |
| 568 |
out->is_device, out->dont_remix, out->unpositioned, |
| 569 |
in->is_device, in->dont_remix, in->unpositioned); |
| 570 |
|
| 571 |
/* we always use out->si format, unless in->si is device */ |
| 572 |
if (!out->is_device && in->is_device) { |
| 573 |
main = in; |
| 574 |
other = out; |
| 575 |
} else { |
| 576 |
main = out; |
| 577 |
other = in; |
| 578 |
} |
| 579 |
|
| 580 |
/* always configure both adapters in passthrough mode |
| 581 |
if this is a passthrough link */ |
| 582 |
if (self->passthrough) { |
| 583 |
g_object_set_data_full (G_OBJECT (transition), "adapter_main", main, |
| 584 |
(GDestroyNotify) adapter_free); |
| 585 |
g_object_set_data_full (G_OBJECT (transition), "adapter_other", other, |
| 586 |
(GDestroyNotify) adapter_free); |
| 587 |
wp_si_adapter_set_ports_format (main->si, NULL, "passthrough", |
| 588 |
on_main_adapter_ready, transition); |
| 589 |
return; |
| 590 |
} |
| 591 |
|
| 592 |
main->fmt = wp_si_adapter_get_ports_format (main->si, &main->mode); |
| 593 |
other->fmt = wp_si_adapter_get_ports_format (other->si, &other->mode); |
| 594 |
|
| 595 |
if (main->fmt) |
| 596 |
/* ideally, configure other based on main */ |
| 597 |
configure_adapter (self, transition, main, other); |
| 598 |
else if (other->fmt) |
| 599 |
/* if main is not configured but other is, do it the other way around */ |
| 600 |
configure_adapter (self, transition, other, main); |
| 601 |
else { |
| 602 |
/* no adapter configured, let's configure main first */ |
| 603 |
g_object_set_data_full (G_OBJECT (transition), "adapter_main", main, |
| 604 |
(GDestroyNotify) adapter_free); |
| 605 |
g_object_set_data_full (G_OBJECT (transition), "adapter_other", other, |
| 606 |
(GDestroyNotify) adapter_free); |
| 607 |
wp_si_adapter_set_ports_format (main->si, NULL, |
| 608 |
main->no_dsp ? "passthrough" : "dsp", on_main_adapter_ready, transition); |
| 609 |
return; |
| 610 |
} |
| 611 |
|
| 612 |
adapter_free (main); |
| 613 |
adapter_free (other); |
| 614 |
} |
| 615 |
|
| 616 |
static void |
| 617 |
si_standard_link_do_link (WpSiStandardLink *self, WpTransition *transition) |
| 618 |
{ |
| 619 |
g_autoptr (WpSessionItem) si_out = g_weak_ref_get (&self->out_item); |
| 620 |
g_autoptr (WpSessionItem) si_in = g_weak_ref_get (&self->in_item); |
| 621 |
|
| 622 |
if (!si_out || !si_in || |
| 623 |
!wp_object_test_active_features ((WP_OBJECT (si_out)), WP_SESSION_ITEM_FEATURE_ACTIVE) || |
| 624 |
!wp_object_test_active_features ((WP_OBJECT (si_in)), WP_SESSION_ITEM_FEATURE_ACTIVE)) { |
| 625 |
wp_transition_return_error (transition, |
| 626 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 627 |
"some node was destroyed before the link was created")); |
| 628 |
return; |
| 629 |
} |
| 630 |
|
| 631 |
if (WP_IS_SI_ADAPTER (si_out) && WP_IS_SI_ADAPTER (si_in)) |
| 632 |
configure_and_link_adapters (self, transition); |
| 633 |
else if (!WP_IS_SI_ADAPTER (si_out) && !WP_IS_SI_ADAPTER (si_in)) |
| 634 |
get_ports_and_create_links (self, transition); |
| 635 |
else |
| 636 |
wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY, |
| 637 |
WP_LIBRARY_ERROR_INVARIANT, |
| 638 |
"Adapters cannot be linked with non-adapters")); |
| 639 |
} |
| 640 |
|
| 641 |
static void |
| 642 |
on_item_acquired (WpSiAcquisition * acq, GAsyncResult * res, |
| 643 |
WpTransition * transition) |
| 644 |
{ |
| 645 |
WpSiStandardLink *self = wp_transition_get_source_object (transition); |
| 646 |
g_autoptr (GError) error = NULL; |
| 647 |
|
| 648 |
if (!wp_si_acquisition_acquire_finish (acq, res, &error)) { |
| 649 |
wp_transition_return_error (transition, g_steal_pointer (&error)); |
| 650 |
return; |
| 651 |
} |
| 652 |
|
| 653 |
self->n_async_ops_wait--; |
| 654 |
if (self->n_async_ops_wait == 0) |
| 655 |
si_standard_link_do_link (self, transition); |
| 656 |
} |
| 657 |
|
| 658 |
static void |
| 659 |
si_standard_link_enable_active (WpSessionItem *si, WpTransition *transition) |
| 660 |
{ |
| 661 |
WpSiStandardLink *self = WP_SI_STANDARD_LINK (si); |
| 662 |
g_autoptr (WpSessionItem) si_out = NULL; |
| 663 |
g_autoptr (WpSessionItem) si_in = NULL; |
| 664 |
WpSiAcquisition *out_acquisition = NULL, *in_acquisition = NULL; |
| 665 |
|
| 666 |
if (!wp_session_item_is_configured (si)) { |
| 667 |
wp_transition_return_error (transition, |
| 668 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT, |
| 669 |
"si-standard-link: item is not configured")); |
| 670 |
return; |
| 671 |
} |
| 672 |
|
| 673 |
/* make sure in/out items are valid */ |
| 674 |
si_out = g_weak_ref_get (&self->out_item); |
| 675 |
si_in = g_weak_ref_get (&self->in_item); |
| 676 |
if (!si_out || !si_in || |
| 677 |
!wp_object_test_active_features ((WP_OBJECT (si_out)), WP_SESSION_ITEM_FEATURE_ACTIVE) || |
| 678 |
!wp_object_test_active_features ((WP_OBJECT (si_in)), WP_SESSION_ITEM_FEATURE_ACTIVE)) { |
| 679 |
wp_transition_return_error (transition, |
| 680 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 681 |
"some node was destroyed before the link was created")); |
| 682 |
return; |
| 683 |
} |
| 684 |
|
| 685 |
/* acquire */ |
| 686 |
out_acquisition = wp_si_linkable_get_acquisition (WP_SI_LINKABLE (si_out)); |
| 687 |
in_acquisition = wp_si_linkable_get_acquisition (WP_SI_LINKABLE (si_in)); |
| 688 |
if (out_acquisition && in_acquisition) |
| 689 |
self->n_async_ops_wait = 2; |
| 690 |
else if (out_acquisition || in_acquisition) |
| 691 |
self->n_async_ops_wait = 1; |
| 692 |
else { |
| 693 |
self->n_async_ops_wait = 0; |
| 694 |
si_standard_link_do_link (self, transition); |
| 695 |
return; |
| 696 |
} |
| 697 |
|
| 698 |
if (out_acquisition) { |
| 699 |
wp_si_acquisition_acquire (out_acquisition, WP_SI_LINK (self), |
| 700 |
WP_SI_LINKABLE (si_out), (GAsyncReadyCallback) on_item_acquired, |
| 701 |
transition); |
| 702 |
} |
| 703 |
if (in_acquisition) { |
| 704 |
wp_si_acquisition_acquire (in_acquisition, WP_SI_LINK (self), |
| 705 |
WP_SI_LINKABLE (si_in), (GAsyncReadyCallback) on_item_acquired, |
| 706 |
transition); |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
static void |
| 711 |
si_standard_link_finalize (GObject * object) |
| 712 |
{ |
| 713 |
WpSiStandardLink *self = WP_SI_STANDARD_LINK (object); |
| 714 |
|
| 715 |
g_weak_ref_clear (&self->out_item); |
| 716 |
g_weak_ref_clear (&self->in_item); |
| 717 |
|
| 718 |
G_OBJECT_CLASS (si_standard_link_parent_class)->finalize (object); |
| 719 |
} |
| 720 |
|
| 721 |
static void |
| 722 |
si_standard_link_class_init (WpSiStandardLinkClass * klass) |
| 723 |
{ |
| 724 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 725 |
WpSessionItemClass *si_class = (WpSessionItemClass *) klass; |
| 726 |
|
| 727 |
object_class->finalize = si_standard_link_finalize; |
| 728 |
|
| 729 |
si_class->reset = si_standard_link_reset; |
| 730 |
si_class->configure = si_standard_link_configure; |
| 731 |
si_class->get_associated_proxy = si_standard_link_get_associated_proxy; |
| 732 |
si_class->disable_active = si_standard_link_disable_active; |
| 733 |
si_class->enable_active = si_standard_link_enable_active; |
| 734 |
|
| 735 |
signals[SIGNAL_LINK_ERROR] = g_signal_new ( |
| 736 |
"link-error", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_FIRST, |
| 737 |
0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_STRING); |
| 738 |
} |
| 739 |
|
| 740 |
static GVariant * |
| 741 |
si_standard_link_get_registration_info (WpSiLink * item) |
| 742 |
{ |
| 743 |
GVariantBuilder b; |
| 744 |
g_variant_builder_init (&b, G_VARIANT_TYPE ("a{ss}")); |
| 745 |
return g_variant_builder_end (&b); |
| 746 |
} |
| 747 |
|
| 748 |
static WpSiLinkable * |
| 749 |
si_standard_link_get_out_item (WpSiLink * item) |
| 750 |
{ |
| 751 |
WpSiStandardLink *self = WP_SI_STANDARD_LINK (item); |
| 752 |
return WP_SI_LINKABLE (g_weak_ref_get (&self->out_item)); |
| 753 |
} |
| 754 |
|
| 755 |
static WpSiLinkable * |
| 756 |
si_standard_link_get_in_item (WpSiLink * item) |
| 757 |
{ |
| 758 |
WpSiStandardLink *self = WP_SI_STANDARD_LINK (item); |
| 759 |
return WP_SI_LINKABLE (g_weak_ref_get (&self->in_item)); |
| 760 |
} |
| 761 |
|
| 762 |
static void |
| 763 |
si_standard_link_link_init (WpSiLinkInterface * iface) |
| 764 |
{ |
| 765 |
iface->get_registration_info = si_standard_link_get_registration_info; |
| 766 |
iface->get_out_item = si_standard_link_get_out_item; |
| 767 |
iface->get_in_item = si_standard_link_get_in_item; |
| 768 |
} |
| 769 |
|
| 770 |
WP_PLUGIN_EXPORT GObject * |
| 771 |
wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error) |
| 772 |
{ |
| 773 |
return G_OBJECT (wp_si_factory_new_simple (SI_FACTORY_NAME, |
| 774 |
si_standard_link_get_type ())); |
| 775 |
} |