| 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/keys.h> |
| 11 |
#include <pipewire/properties.h> |
| 12 |
|
| 13 |
#include <spa/param/format.h> |
| 14 |
#include <spa/param/audio/raw.h> |
| 15 |
#include <spa/param/audio/format-utils.h> |
| 16 |
#include <spa/param/param.h> |
| 17 |
|
| 18 |
WP_DEFINE_LOCAL_LOG_TOPIC ("m-si-audio-adapter") |
| 19 |
|
| 20 |
#define SI_FACTORY_NAME "si-audio-adapter" |
| 21 |
|
| 22 |
struct _WpSiAudioAdapter |
| 23 |
{ |
| 24 |
WpSessionItem parent; |
| 25 |
|
| 26 |
/* configuration */ |
| 27 |
gboolean reconfigured; |
| 28 |
WpNode *node; |
| 29 |
WpPort *port; /* only used for passthrough or convert mode */ |
| 30 |
gboolean no_format; |
| 31 |
gboolean mono; |
| 32 |
gboolean control_port; |
| 33 |
gboolean monitor; |
| 34 |
gboolean disable_dsp; |
| 35 |
WpDirection portconfig_direction; |
| 36 |
gboolean is_device; |
| 37 |
gboolean dont_remix; |
| 38 |
gboolean is_autoconnect; |
| 39 |
gboolean have_encoded; |
| 40 |
gboolean encoded_only; |
| 41 |
gboolean is_unpositioned; |
| 42 |
struct spa_audio_info_raw raw_format; |
| 43 |
|
| 44 |
gulong ports_changed_sigid; |
| 45 |
gulong params_changed_sigid; |
| 46 |
|
| 47 |
WpSpaPod *format; |
| 48 |
gchar mode[32]; |
| 49 |
GTask *format_task; |
| 50 |
WpSiAdapterPortsState ports_state; |
| 51 |
}; |
| 52 |
|
| 53 |
static void si_audio_adapter_linkable_init (WpSiLinkableInterface * iface); |
| 54 |
static void si_audio_adapter_adapter_init (WpSiAdapterInterface * iface); |
| 55 |
|
| 56 |
G_DECLARE_FINAL_TYPE(WpSiAudioAdapter, si_audio_adapter, WP, SI_AUDIO_ADAPTER, |
| 57 |
WpSessionItem) |
| 58 |
G_DEFINE_TYPE_WITH_CODE (WpSiAudioAdapter, si_audio_adapter, |
| 59 |
WP_TYPE_SESSION_ITEM, |
| 60 |
G_IMPLEMENT_INTERFACE (WP_TYPE_SI_LINKABLE, si_audio_adapter_linkable_init) |
| 61 |
G_IMPLEMENT_INTERFACE (WP_TYPE_SI_ADAPTER, si_audio_adapter_adapter_init)) |
| 62 |
|
| 63 |
static void |
| 64 |
si_audio_adapter_init (WpSiAudioAdapter * self) |
| 65 |
{ |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
si_audio_adapter_set_ports_state (WpSiAudioAdapter *self, WpSiAdapterPortsState |
| 70 |
new_state) |
| 71 |
{ |
| 72 |
if (self->ports_state != new_state) { |
| 73 |
WpSiAdapterPortsState old_state = self->ports_state; |
| 74 |
self->ports_state = new_state; |
| 75 |
g_signal_emit_by_name (self, "adapter-ports-state-changed", old_state, |
| 76 |
new_state); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
static void |
| 81 |
si_audio_adapter_soft_reset (WpSiAudioAdapter *self) |
| 82 |
{ |
| 83 |
/* deactivate first */ |
| 84 |
wp_object_deactivate (WP_OBJECT (self), WP_SESSION_ITEM_FEATURE_ACTIVE); |
| 85 |
|
| 86 |
/* reset back to the initial "configured" state */ |
| 87 |
if (self->format_task) { |
| 88 |
g_task_return_new_error (self->format_task, WP_DOMAIN_LIBRARY, |
| 89 |
WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 90 |
"item deactivated before format was set"); |
| 91 |
g_clear_object (&self->format_task); |
| 92 |
} |
| 93 |
g_clear_pointer (&self->format, wp_spa_pod_unref); |
| 94 |
self->mode[0] = '\0'; |
| 95 |
si_audio_adapter_set_ports_state (self, WP_SI_ADAPTER_PORTS_STATE_NONE); |
| 96 |
} |
| 97 |
|
| 98 |
static void |
| 99 |
si_audio_adapter_reset (WpSessionItem * item) |
| 100 |
{ |
| 101 |
WpSiAudioAdapter *self = WP_SI_AUDIO_ADAPTER (item); |
| 102 |
|
| 103 |
si_audio_adapter_soft_reset (self); |
| 104 |
|
| 105 |
/* reset */ |
| 106 |
g_clear_object (&self->node); |
| 107 |
g_clear_object (&self->port); |
| 108 |
self->no_format = FALSE; |
| 109 |
self->mono = FALSE; |
| 110 |
self->control_port = FALSE; |
| 111 |
self->monitor = FALSE; |
| 112 |
self->disable_dsp = FALSE; |
| 113 |
self->portconfig_direction = WP_DIRECTION_INPUT; |
| 114 |
self->is_device = FALSE; |
| 115 |
self->dont_remix = FALSE; |
| 116 |
self->is_autoconnect = FALSE; |
| 117 |
self->have_encoded = FALSE; |
| 118 |
self->encoded_only = FALSE; |
| 119 |
spa_memzero (&self->raw_format, sizeof(struct spa_audio_info_raw)); |
| 120 |
|
| 121 |
WP_SESSION_ITEM_CLASS (si_audio_adapter_parent_class)->reset (item); |
| 122 |
} |
| 123 |
|
| 124 |
static guint |
| 125 |
si_audio_adapter_get_default_clock_rate (WpSiAudioAdapter * self) |
| 126 |
{ |
| 127 |
g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self)); |
| 128 |
g_autoptr (WpProperties) props = NULL; |
| 129 |
const gchar *rate_str = NULL; |
| 130 |
g_return_val_if_fail (core, 48000); |
| 131 |
props = wp_core_get_remote_properties (core); |
| 132 |
g_return_val_if_fail (props, 48000); |
| 133 |
rate_str = wp_properties_get (props, "default.clock.rate"); |
| 134 |
return rate_str ? atoi (rate_str) : 48000; |
| 135 |
} |
| 136 |
|
| 137 |
static gboolean |
| 138 |
is_unpositioned (struct spa_audio_info_raw *info) |
| 139 |
{ |
| 140 |
uint32_t i, n_pos; |
| 141 |
if (SPA_FLAG_IS_SET(info->flags, SPA_AUDIO_FLAG_UNPOSITIONED)) |
| 142 |
return TRUE; |
| 143 |
n_pos = SPA_MIN(info->channels, SPA_N_ELEMENTS(info->position)); |
| 144 |
for (i = 0; i < n_pos; i++) |
| 145 |
if (info->position[i] >= SPA_AUDIO_CHANNEL_START_Aux && |
| 146 |
info->position[i] <= SPA_AUDIO_CHANNEL_LAST_Aux) |
| 147 |
return TRUE; |
| 148 |
return FALSE; |
| 149 |
} |
| 150 |
|
| 151 |
static gboolean |
| 152 |
si_audio_adapter_find_format (WpSiAudioAdapter * self, WpNode * node, |
| 153 |
gboolean mono) |
| 154 |
{ |
| 155 |
g_autoptr (WpIterator) formats = NULL; |
| 156 |
g_auto (GValue) value = G_VALUE_INIT; |
| 157 |
gboolean have_format = FALSE; |
| 158 |
|
| 159 |
formats = wp_pipewire_object_enum_params_sync (WP_PIPEWIRE_OBJECT (node), |
| 160 |
"EnumFormat", NULL); |
| 161 |
if (!formats) |
| 162 |
return FALSE; |
| 163 |
|
| 164 |
for (; wp_iterator_next (formats, &value); g_value_unset (&value)) { |
| 165 |
WpSpaPod *pod = g_value_get_boxed (&value); |
| 166 |
uint32_t mtype, msubtype; |
| 167 |
|
| 168 |
if (!wp_spa_pod_is_object (pod)) { |
| 169 |
wp_warning_object (self, |
| 170 |
"non-object POD appeared on formats list; this node is buggy"); |
| 171 |
continue; |
| 172 |
} |
| 173 |
|
| 174 |
if (!wp_spa_pod_get_object (pod, NULL, |
| 175 |
"mediaType", "I", &mtype, |
| 176 |
"mediaSubtype", "I", &msubtype, |
| 177 |
NULL)) { |
| 178 |
wp_warning_object (self, "format does not have media type / subtype"); |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
if (mtype != SPA_MEDIA_TYPE_audio) |
| 183 |
continue; |
| 184 |
|
| 185 |
switch (msubtype) { |
| 186 |
case SPA_MEDIA_SUBTYPE_raw: { |
| 187 |
struct spa_audio_info_raw raw_format; |
| 188 |
struct spa_pod *position = NULL; |
| 189 |
wp_spa_pod_fixate (pod); |
| 190 |
|
| 191 |
spa_zero(raw_format); |
| 192 |
if (spa_pod_parse_object(wp_spa_pod_get_spa_pod (pod), |
| 193 |
SPA_TYPE_OBJECT_Format, NULL, |
| 194 |
SPA_FORMAT_AUDIO_format, SPA_POD_OPT_Id(&raw_format.format), |
| 195 |
SPA_FORMAT_AUDIO_rate, SPA_POD_OPT_Int(&raw_format.rate), |
| 196 |
SPA_FORMAT_AUDIO_channels, SPA_POD_OPT_Int(&raw_format.channels), |
| 197 |
SPA_FORMAT_AUDIO_position, SPA_POD_OPT_Pod(&position)) < 0) |
| 198 |
continue; |
| 199 |
|
| 200 |
if (position == NULL || |
| 201 |
!spa_pod_copy_array(position, SPA_TYPE_Id, raw_format.position, SPA_N_ELEMENTS(raw_format.position))) |
| 202 |
SPA_FLAG_SET(raw_format.flags, SPA_AUDIO_FLAG_UNPOSITIONED); |
| 203 |
|
| 204 |
if (mono) { |
| 205 |
raw_format.channels = 1; |
| 206 |
raw_format.position [0] = SPA_AUDIO_CHANNEL_MONO; |
| 207 |
} |
| 208 |
|
| 209 |
if (self->raw_format.channels < raw_format.channels) { |
| 210 |
self->raw_format = raw_format; |
| 211 |
if (is_unpositioned(&raw_format)) |
| 212 |
self->is_unpositioned = TRUE; |
| 213 |
} |
| 214 |
have_format = TRUE; |
| 215 |
break; |
| 216 |
} |
| 217 |
case SPA_MEDIA_SUBTYPE_iec958: |
| 218 |
case SPA_MEDIA_SUBTYPE_dsd: |
| 219 |
wp_info_object (self, "passthrough IEC958/DSD node %d found", |
| 220 |
wp_proxy_get_bound_id (WP_PROXY (node))); |
| 221 |
self->have_encoded = TRUE; |
| 222 |
break; |
| 223 |
default: { |
| 224 |
enum spa_audio_format audio_format; |
| 225 |
if (spa_pod_parse_object(wp_spa_pod_get_spa_pod (pod), |
| 226 |
SPA_TYPE_OBJECT_Format, NULL, |
| 227 |
SPA_FORMAT_AUDIO_format, SPA_POD_OPT_Id(&audio_format)) >= 0) { |
| 228 |
self->have_encoded = (audio_format == SPA_AUDIO_FORMAT_ENCODED); |
| 229 |
} |
| 230 |
break; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
if (!have_format && self->have_encoded) { |
| 235 |
wp_info_object (self, ".. passthrough IEC958/DSD/encoded only"); |
| 236 |
self->encoded_only = TRUE; |
| 237 |
have_format = TRUE; |
| 238 |
} |
| 239 |
|
| 240 |
return have_format; |
| 241 |
} |
| 242 |
|
| 243 |
static void |
| 244 |
on_proxy_destroyed (WpNode * proxy, WpSiAudioAdapter * self) |
| 245 |
{ |
| 246 |
if (self->node == proxy) { |
| 247 |
wp_object_abort_activation (WP_OBJECT (self), "proxy destroyed"); |
| 248 |
si_audio_adapter_soft_reset (self); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
static gboolean |
| 253 |
si_audio_adapter_configure (WpSessionItem * item, WpProperties *p) |
| 254 |
{ |
| 255 |
WpSiAudioAdapter *self = WP_SI_AUDIO_ADAPTER (item); |
| 256 |
g_autoptr (WpProperties) si_props = wp_properties_ensure_unique_owner (p); |
| 257 |
WpNode *node = NULL; |
| 258 |
const gchar *str; |
| 259 |
|
| 260 |
self->reconfigured = self->node != NULL; |
| 261 |
|
| 262 |
/* reset previous config */ |
| 263 |
si_audio_adapter_reset (item); |
| 264 |
|
| 265 |
str = wp_properties_get (si_props, "item.node"); |
| 266 |
if (!str || sscanf(str, "%p", &node) != 1 || !WP_IS_NODE (node)) |
| 267 |
return FALSE; |
| 268 |
|
| 269 |
str = wp_properties_get (si_props, PW_KEY_MEDIA_CLASS); |
| 270 |
if (!str) |
| 271 |
return FALSE; |
| 272 |
if ((strstr (str, "Source") || strstr (str, "Output")) |
| 273 |
&& !strstr (str, "Virtual")) { |
| 274 |
self->portconfig_direction = WP_DIRECTION_OUTPUT; |
| 275 |
} |
| 276 |
|
| 277 |
str = wp_properties_get (si_props, "item.features.no-format"); |
| 278 |
self->no_format = str && pw_properties_parse_bool (str); |
| 279 |
|
| 280 |
str = wp_properties_get (si_props, "item.features.mono"); |
| 281 |
self->mono = str && pw_properties_parse_bool (str); |
| 282 |
|
| 283 |
if (!self->no_format && !si_audio_adapter_find_format (self, node, |
| 284 |
self->mono)) { |
| 285 |
wp_notice_object (item, "no usable format found for node %d", |
| 286 |
wp_proxy_get_bound_id (WP_PROXY (node))); |
| 287 |
return FALSE; |
| 288 |
} |
| 289 |
|
| 290 |
str = wp_properties_get (si_props, "item.features.control-port"); |
| 291 |
self->control_port = str && pw_properties_parse_bool (str); |
| 292 |
|
| 293 |
str = wp_properties_get (si_props, "item.features.monitor"); |
| 294 |
self->monitor = str && pw_properties_parse_bool (str); |
| 295 |
|
| 296 |
str = wp_properties_get (si_props, "item.features.no-dsp"); |
| 297 |
self->disable_dsp = str && pw_properties_parse_bool (str); |
| 298 |
|
| 299 |
str = wp_properties_get (si_props, "item.node.type"); |
| 300 |
self->is_device = !g_strcmp0 (str, "device"); |
| 301 |
|
| 302 |
str = wp_properties_get (si_props, PW_KEY_STREAM_DONT_REMIX); |
| 303 |
self->dont_remix = str && pw_properties_parse_bool (str); |
| 304 |
|
| 305 |
str = wp_properties_get (si_props, PW_KEY_NODE_AUTOCONNECT); |
| 306 |
self->is_autoconnect = str && pw_properties_parse_bool (str); |
| 307 |
|
| 308 |
self->node = g_object_ref (node); |
| 309 |
g_signal_connect_object (self->node, "pw-proxy-destroyed", |
| 310 |
G_CALLBACK (on_proxy_destroyed), self, 0); |
| 311 |
|
| 312 |
wp_properties_set (si_props, "item.node.supports-encoded-fmts", |
| 313 |
self->have_encoded ? "true" : "false"); |
| 314 |
|
| 315 |
wp_properties_set (si_props, "item.node.encoded-only", |
| 316 |
self->encoded_only ? "true" : "false"); |
| 317 |
|
| 318 |
wp_properties_set (si_props, "item.node.unpositioned", |
| 319 |
self->is_unpositioned ? "true" : "false"); |
| 320 |
|
| 321 |
wp_properties_set (si_props, "item.factory.name", SI_FACTORY_NAME); |
| 322 |
wp_session_item_set_properties (item, g_steal_pointer (&si_props)); |
| 323 |
return TRUE; |
| 324 |
} |
| 325 |
|
| 326 |
static gpointer |
| 327 |
si_audio_adapter_get_associated_proxy (WpSessionItem * item, GType proxy_type) |
| 328 |
{ |
| 329 |
WpSiAudioAdapter *self = WP_SI_AUDIO_ADAPTER (item); |
| 330 |
|
| 331 |
if (proxy_type == WP_TYPE_NODE) |
| 332 |
return self->node ? g_object_ref (self->node) : NULL; |
| 333 |
|
| 334 |
return NULL; |
| 335 |
} |
| 336 |
|
| 337 |
static WpSpaPod * |
| 338 |
format_audio_raw_build (const struct spa_audio_info_raw *info) |
| 339 |
{ |
| 340 |
g_autoptr (WpSpaPodBuilder) builder = wp_spa_pod_builder_new_object ( |
| 341 |
"Spa:Pod:Object:Param:Format", "Format"); |
| 342 |
wp_spa_pod_builder_add (builder, |
| 343 |
"mediaType", "K", "audio", |
| 344 |
"mediaSubtype", "K", "raw", |
| 345 |
"format", "I", info->format, |
| 346 |
"rate", "i", info->rate, |
| 347 |
"channels", "i", info->channels, |
| 348 |
NULL); |
| 349 |
|
| 350 |
if (!SPA_FLAG_IS_SET (info->flags, SPA_AUDIO_FLAG_UNPOSITIONED)) { |
| 351 |
/* Build the position array spa pod */ |
| 352 |
g_autoptr (WpSpaPodBuilder) position_builder = wp_spa_pod_builder_new_array (); |
| 353 |
guint n_pos = SPA_MIN(info->channels, SPA_N_ELEMENTS(info->position)); |
| 354 |
for (guint i = 0; i < n_pos; i++) |
| 355 |
wp_spa_pod_builder_add_id (position_builder, info->position[i]); |
| 356 |
|
| 357 |
/* Add the position property */ |
| 358 |
wp_spa_pod_builder_add_property (builder, "position"); |
| 359 |
g_autoptr (WpSpaPod) position = wp_spa_pod_builder_end (position_builder); |
| 360 |
wp_spa_pod_builder_add_pod (builder, position); |
| 361 |
} |
| 362 |
|
| 363 |
return wp_spa_pod_builder_end (builder); |
| 364 |
} |
| 365 |
|
| 366 |
static gboolean |
| 367 |
parse_adapter_format (WpSpaPod *format, gint *channels, |
| 368 |
WpSpaPod **position) |
| 369 |
{ |
| 370 |
g_autoptr (WpSpaPodParser) parser = NULL; |
| 371 |
guint32 t = 0, s = 0, f = 0; |
| 372 |
gint r = 0, c = 0; |
| 373 |
g_autoptr (WpSpaPod) p = NULL; |
| 374 |
|
| 375 |
g_return_val_if_fail (format, FALSE); |
| 376 |
parser = wp_spa_pod_parser_new_object (format, NULL); |
| 377 |
g_return_val_if_fail (parser, FALSE); |
| 378 |
|
| 379 |
if (!wp_spa_pod_parser_get (parser, "mediaType", "I", &t, NULL) || |
| 380 |
!wp_spa_pod_parser_get (parser, "mediaSubtype", "I", &s, NULL) || |
| 381 |
!wp_spa_pod_parser_get (parser, "format", "I", &f, NULL) || |
| 382 |
!wp_spa_pod_parser_get (parser, "rate", "i", &r, NULL) || |
| 383 |
!wp_spa_pod_parser_get (parser, "channels", "i", &c, NULL)) |
| 384 |
return FALSE; |
| 385 |
|
| 386 |
/* position is optional */ |
| 387 |
wp_spa_pod_parser_get (parser, "position", "P", &p, NULL); |
| 388 |
|
| 389 |
if (channels && c != 0) |
| 390 |
*channels = c; |
| 391 |
if (position) |
| 392 |
*position = p ? wp_spa_pod_ref (p) : NULL; |
| 393 |
|
| 394 |
return TRUE; |
| 395 |
} |
| 396 |
|
| 397 |
static WpSpaPod * |
| 398 |
build_adapter_format (WpSiAudioAdapter * self, guint32 format, gint channels, |
| 399 |
WpSpaPod *pos) |
| 400 |
{ |
| 401 |
g_autoptr (WpSpaPod) position = pos; |
| 402 |
g_autoptr (WpSpaPodBuilder) b = NULL; |
| 403 |
|
| 404 |
g_return_val_if_fail (channels > 0, NULL); |
| 405 |
|
| 406 |
/* build the position array if not given */ |
| 407 |
if (!position) { |
| 408 |
switch (channels) { |
| 409 |
case 1: { |
| 410 |
g_autoptr (WpSpaPodBuilder) pos_b = wp_spa_pod_builder_new_array (); |
| 411 |
wp_spa_pod_builder_add_id (pos_b, SPA_AUDIO_CHANNEL_MONO); |
| 412 |
position = wp_spa_pod_builder_end (pos_b); |
| 413 |
break; |
| 414 |
} |
| 415 |
case 2: { |
| 416 |
g_autoptr (WpSpaPodBuilder) pos_b = wp_spa_pod_builder_new_array (); |
| 417 |
wp_spa_pod_builder_add_id (pos_b, SPA_AUDIO_CHANNEL_FL); |
| 418 |
wp_spa_pod_builder_add_id (pos_b, SPA_AUDIO_CHANNEL_FR); |
| 419 |
position = wp_spa_pod_builder_end (pos_b); |
| 420 |
break; |
| 421 |
} |
| 422 |
default: |
| 423 |
break; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
/* build the format */ |
| 428 |
b = wp_spa_pod_builder_new_object ("Spa:Pod:Object:Param:Format", "Format"); |
| 429 |
wp_spa_pod_builder_add_property (b, "mediaType"); |
| 430 |
wp_spa_pod_builder_add_id (b, SPA_MEDIA_TYPE_audio); |
| 431 |
wp_spa_pod_builder_add_property (b, "mediaSubtype"); |
| 432 |
wp_spa_pod_builder_add_id (b, SPA_MEDIA_SUBTYPE_raw); |
| 433 |
wp_spa_pod_builder_add_property (b, "format"); |
| 434 |
wp_spa_pod_builder_add_id (b, format); |
| 435 |
wp_spa_pod_builder_add_property (b, "rate"); |
| 436 |
wp_spa_pod_builder_add_int (b, si_audio_adapter_get_default_clock_rate (self)); |
| 437 |
wp_spa_pod_builder_add_property (b, "channels"); |
| 438 |
wp_spa_pod_builder_add_int (b, channels); |
| 439 |
if (position) { |
| 440 |
wp_spa_pod_builder_add_property (b, "position"); |
| 441 |
wp_spa_pod_builder_add_pod (b, position); |
| 442 |
} |
| 443 |
return wp_spa_pod_builder_end (b); |
| 444 |
} |
| 445 |
|
| 446 |
static WpSpaPod * |
| 447 |
build_adapter_dsp_format (WpSiAudioAdapter * self, WpSpaPod *dev_format) |
| 448 |
{ |
| 449 |
g_autoptr (WpSpaPod) position = NULL; |
| 450 |
gint channels = 2; |
| 451 |
|
| 452 |
/* parse device format */ |
| 453 |
if (dev_format && !parse_adapter_format (dev_format, &channels, &position)) |
| 454 |
return NULL; |
| 455 |
|
| 456 |
/* build F32P with same channels and position as device format */ |
| 457 |
return build_adapter_format (self, SPA_AUDIO_FORMAT_F32P, channels, |
| 458 |
g_steal_pointer (&position)); |
| 459 |
} |
| 460 |
|
| 461 |
static WpSpaPod * |
| 462 |
build_adapter_default_format (WpSiAudioAdapter * self, const gchar *mode) |
| 463 |
{ |
| 464 |
guint32 format = SPA_AUDIO_FORMAT_F32; |
| 465 |
|
| 466 |
/* if dsp, use plannar format */ |
| 467 |
if (!mode || g_strcmp0 (mode, "dsp") == 0) |
| 468 |
format = SPA_AUDIO_FORMAT_F32P; |
| 469 |
|
| 470 |
return build_adapter_format (self, format, 2, NULL); |
| 471 |
} |
| 472 |
|
| 473 |
static void |
| 474 |
on_format_set (GObject *obj, GAsyncResult * res, gpointer p) |
| 475 |
{ |
| 476 |
g_autoptr(WpTransition) transition = p; |
| 477 |
WpSiAudioAdapter *self = wp_transition_get_source_object (transition); |
| 478 |
g_autoptr (GError) error = NULL; |
| 479 |
|
| 480 |
if (wp_transition_get_completed (transition)) |
| 481 |
return; |
| 482 |
|
| 483 |
wp_si_adapter_set_ports_format_finish (WP_SI_ADAPTER (self), res, &error); |
| 484 |
if (error) { |
| 485 |
wp_transition_return_error (transition, g_steal_pointer (&error)); |
| 486 |
return; |
| 487 |
} |
| 488 |
|
| 489 |
wp_object_update_features (WP_OBJECT (self), |
| 490 |
WP_SESSION_ITEM_FEATURE_ACTIVE, 0); |
| 491 |
} |
| 492 |
|
| 493 |
static void |
| 494 |
si_audio_adapter_configure_node (WpSiAudioAdapter *self, |
| 495 |
WpTransition * transition) |
| 496 |
{ |
| 497 |
g_autoptr (WpSpaPod) current_format = NULL; |
| 498 |
g_autoptr (WpSpaPod) new_format = NULL; |
| 499 |
g_autoptr (WpSpaPod) ports_format = NULL; |
| 500 |
const gchar *mode = NULL; |
| 501 |
|
| 502 |
/* set the chosen format on the node */ |
| 503 |
current_format = wp_si_adapter_get_ports_format (WP_SI_ADAPTER (self), NULL); |
| 504 |
new_format = format_audio_raw_build (&self->raw_format); |
| 505 |
if (current_format == NULL || !wp_spa_pod_equal (current_format, new_format)) { |
| 506 |
if (current_format == NULL) { |
| 507 |
wp_debug_object (self, "setting Format param since it is not set yet"); |
| 508 |
} else { |
| 509 |
wp_debug_object (self, "setting Format param since the new format differs " |
| 510 |
"from the current one"); |
| 511 |
} |
| 512 |
/* ensure the node is suspended, since setting the Format param only |
| 513 |
* works if the node is not running */ |
| 514 |
if (wp_node_get_state (self->node, NULL) >= WP_NODE_STATE_IDLE) { |
| 515 |
wp_debug_object (self, "suspending node before setting its Format param"); |
| 516 |
wp_node_send_command (self->node, "Suspend"); |
| 517 |
} |
| 518 |
|
| 519 |
wp_pipewire_object_set_param (WP_PIPEWIRE_OBJECT (self->node), "Format", 0, |
| 520 |
wp_spa_pod_ref (new_format)); |
| 521 |
} else { |
| 522 |
wp_debug_object (self, "not setting Format param since the new format is " |
| 523 |
"the same as the current one"); |
| 524 |
} |
| 525 |
|
| 526 |
/* build the ports format */ |
| 527 |
if (self->disable_dsp) { |
| 528 |
mode = "passthrough"; |
| 529 |
ports_format = g_steal_pointer (&new_format); |
| 530 |
} else { |
| 531 |
mode = "dsp"; |
| 532 |
ports_format = build_adapter_dsp_format (self, new_format); |
| 533 |
if (!ports_format) { |
| 534 |
wp_transition_return_error (transition, |
| 535 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 536 |
"failed to build ports format")); |
| 537 |
return; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
/* set chosen format in the ports */ |
| 542 |
wp_si_adapter_set_ports_format (WP_SI_ADAPTER (self), |
| 543 |
g_steal_pointer (&ports_format), mode, on_format_set, g_object_ref (transition)); |
| 544 |
} |
| 545 |
|
| 546 |
static void |
| 547 |
on_port_param_info (WpPipewireObject * port, GParamSpec * param, |
| 548 |
WpSiAudioAdapter *self) |
| 549 |
{ |
| 550 |
/* finish the task started by _set_ports_format() */ |
| 551 |
if (self->format_task) { |
| 552 |
g_autoptr (GTask) t = g_steal_pointer (&self->format_task); |
| 553 |
si_audio_adapter_set_ports_state (self, |
| 554 |
WP_SI_ADAPTER_PORTS_STATE_CONFIGURED); |
| 555 |
g_task_return_boolean (t, TRUE); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
static void |
| 560 |
on_node_ports_changed (WpObject * node, WpSiAudioAdapter *self) |
| 561 |
{ |
| 562 |
/* clear port and handler */ |
| 563 |
if (self->port) { |
| 564 |
g_signal_handlers_disconnect_by_func (self->port, on_port_param_info, self); |
| 565 |
g_clear_object (&self->port); |
| 566 |
} |
| 567 |
|
| 568 |
if (wp_node_get_n_ports (self->node) > 0) { |
| 569 |
/* if non DSP mode, listen for param-info on the single port in order to |
| 570 |
* be notified of format changed events */ |
| 571 |
if (g_strcmp0 (self->mode, "dsp") != 0) { |
| 572 |
self->port = wp_node_lookup_port (self->node, |
| 573 |
WP_CONSTRAINT_TYPE_PW_PROPERTY, "port.direction", "=s", |
| 574 |
self->portconfig_direction == WP_DIRECTION_INPUT ? "in" : "out", |
| 575 |
NULL); |
| 576 |
if (self->port) |
| 577 |
g_signal_connect_object (self->port, "notify::param-info", |
| 578 |
G_CALLBACK (on_port_param_info), self, 0); |
| 579 |
} |
| 580 |
|
| 581 |
/* finish the task started by _set_ports_format() */ |
| 582 |
if (self->format_task) { |
| 583 |
g_autoptr (GTask) t = g_steal_pointer (&self->format_task); |
| 584 |
si_audio_adapter_set_ports_state (self, |
| 585 |
WP_SI_ADAPTER_PORTS_STATE_CONFIGURED); |
| 586 |
g_task_return_boolean (t, TRUE); |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
static void |
| 592 |
on_node_params_changed (WpPipewireObject * proxy, const gchar *param_name, |
| 593 |
WpSiAudioAdapter *self) |
| 594 |
{ |
| 595 |
/* Only handle Props param that has been emitted when setting ports format */ |
| 596 |
if (!g_str_equal (param_name, "Props") || !self->format_task) |
| 597 |
return; |
| 598 |
|
| 599 |
/* If ports are already configured, finish task. This is the case for already |
| 600 |
* loaded nodes such as virtual filters */ |
| 601 |
if (wp_node_get_n_ports (self->node) > 0) |
| 602 |
on_node_ports_changed (WP_OBJECT (self->node), self); |
| 603 |
} |
| 604 |
|
| 605 |
static void |
| 606 |
si_audio_adapter_enable_active (WpSessionItem *si, WpTransition *transition) |
| 607 |
{ |
| 608 |
WpSiAudioAdapter *self = WP_SI_AUDIO_ADAPTER (si); |
| 609 |
|
| 610 |
if (!wp_session_item_is_configured (si)) { |
| 611 |
wp_transition_return_error (transition, |
| 612 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT, |
| 613 |
"si-audio-adapter: item is not configured")); |
| 614 |
return; |
| 615 |
} |
| 616 |
|
| 617 |
if (!(wp_object_test_active_features (WP_OBJECT (self->node), |
| 618 |
WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL))) { |
| 619 |
wp_transition_return_error (transition, |
| 620 |
g_error_new (WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT, |
| 621 |
"si-audio-adapter: node minimal feature not enabled")); |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
self->ports_changed_sigid = g_signal_connect_object (self->node, |
| 626 |
"ports-changed", (GCallback) on_node_ports_changed, self, 0); |
| 627 |
self->params_changed_sigid = g_signal_connect_object (self->node, |
| 628 |
"params-changed", (GCallback) on_node_params_changed, self, 0); |
| 629 |
|
| 630 |
/* If device node, enum available formats and set one of them */ |
| 631 |
if (!self->no_format && (self->is_device || self->dont_remix || |
| 632 |
!self->is_autoconnect || self->disable_dsp || self->is_unpositioned || |
| 633 |
self->reconfigured)) { |
| 634 |
si_audio_adapter_configure_node (self, transition); |
| 635 |
} |
| 636 |
|
| 637 |
/* Otherwise just finish activating */ |
| 638 |
else |
| 639 |
wp_object_update_features (WP_OBJECT (self), |
| 640 |
WP_SESSION_ITEM_FEATURE_ACTIVE, 0); |
| 641 |
} |
| 642 |
|
| 643 |
static void |
| 644 |
si_audio_adapter_disable_active (WpSessionItem *si) |
| 645 |
{ |
| 646 |
WpSiAudioAdapter *self = WP_SI_AUDIO_ADAPTER (si); |
| 647 |
|
| 648 |
if (self->ports_changed_sigid) { |
| 649 |
g_signal_handler_disconnect (self->node, self->ports_changed_sigid); |
| 650 |
self->ports_changed_sigid = 0; |
| 651 |
} |
| 652 |
if (self->params_changed_sigid) { |
| 653 |
g_signal_handler_disconnect (self->node, self->params_changed_sigid); |
| 654 |
self->params_changed_sigid = 0; |
| 655 |
} |
| 656 |
|
| 657 |
wp_object_update_features (WP_OBJECT (self), 0, |
| 658 |
WP_SESSION_ITEM_FEATURE_ACTIVE); |
| 659 |
} |
| 660 |
|
| 661 |
static WpObjectFeatures |
| 662 |
si_audio_adapter_get_supported_features (WpObject * self) |
| 663 |
{ |
| 664 |
return WP_SESSION_ITEM_FEATURE_ACTIVE; |
| 665 |
} |
| 666 |
|
| 667 |
static void |
| 668 |
si_audio_adapter_class_init (WpSiAudioAdapterClass * klass) |
| 669 |
{ |
| 670 |
WpObjectClass * wpobject_class = (WpObjectClass *) klass; |
| 671 |
WpSessionItemClass *si_class = (WpSessionItemClass *) klass; |
| 672 |
|
| 673 |
wpobject_class->get_supported_features = |
| 674 |
si_audio_adapter_get_supported_features; |
| 675 |
|
| 676 |
si_class->reset = si_audio_adapter_reset; |
| 677 |
si_class->configure = si_audio_adapter_configure; |
| 678 |
si_class->get_associated_proxy = si_audio_adapter_get_associated_proxy; |
| 679 |
si_class->disable_active = si_audio_adapter_disable_active; |
| 680 |
si_class->enable_active = si_audio_adapter_enable_active; |
| 681 |
} |
| 682 |
|
| 683 |
static WpSiAdapterPortsState |
| 684 |
si_audio_adapter_get_ports_state (WpSiAdapter * item) |
| 685 |
{ |
| 686 |
WpSiAudioAdapter *self = WP_SI_AUDIO_ADAPTER (item); |
| 687 |
return self->ports_state; |
| 688 |
} |
| 689 |
|
| 690 |
static WpSpaPod * |
| 691 |
si_audio_adapter_get_ports_format (WpSiAdapter * item, const gchar **mode) |
| 692 |
{ |
| 693 |
WpSiAudioAdapter *self = WP_SI_AUDIO_ADAPTER (item); |
| 694 |
if (mode) |
| 695 |
*mode = self->mode; |
| 696 |
return self->format ? wp_spa_pod_ref (self->format) : NULL; |
| 697 |
} |
| 698 |
|
| 699 |
static void |
| 700 |
si_audio_adapter_set_ports_format (WpSiAdapter * item, WpSpaPod *f, |
| 701 |
const gchar *mode, GAsyncReadyCallback callback, gpointer data) |
| 702 |
{ |
| 703 |
WpSiAudioAdapter *self = WP_SI_AUDIO_ADAPTER (item); |
| 704 |
g_autoptr (WpSpaPod) format = f; |
| 705 |
g_autoptr (GTask) task = g_task_new (self, NULL, callback, data); |
| 706 |
|
| 707 |
/* cancel previous task if any */ |
| 708 |
if (self->format_task) { |
| 709 |
g_autoptr (GTask) t = g_steal_pointer (&self->format_task); |
| 710 |
g_task_return_new_error (t, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVARIANT, |
| 711 |
"setting new format before previous done"); |
| 712 |
} |
| 713 |
|
| 714 |
/* build default format if NULL was given */ |
| 715 |
if (!format && !g_strcmp0 (mode, "dsp")) { |
| 716 |
format = build_adapter_default_format (self, mode); |
| 717 |
if (!format) { |
| 718 |
g_task_return_new_error (task, WP_DOMAIN_LIBRARY, |
| 719 |
WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 720 |
"failed to build default format, aborting set format operation"); |
| 721 |
return; |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
/* make sure the node has WP_NODE_FEATURE_PORTS */ |
| 726 |
if (G_UNLIKELY (!(wp_object_test_active_features (WP_OBJECT (self->node), |
| 727 |
WP_NODE_FEATURE_PORTS)))) { |
| 728 |
g_task_return_new_error (task, WP_DOMAIN_LIBRARY, |
| 729 |
WP_LIBRARY_ERROR_OPERATION_FAILED, |
| 730 |
"node feature ports is not enabled, aborting set format operation"); |
| 731 |
return; |
| 732 |
} |
| 733 |
|
| 734 |
/* skip reconfiguring if the same mode & format are requested */ |
| 735 |
if (!g_strcmp0 (mode, self->mode) && |
| 736 |
((format == NULL && self->format == NULL) || |
| 737 |
wp_spa_pod_equal (format, self->format))) { |
| 738 |
wp_debug_object (self, "new PortConfig mode and format are identical " |
| 739 |
"to the current ones; skipping reconfiguration"); |
| 740 |
g_task_return_boolean (task, TRUE); |
| 741 |
return; |
| 742 |
} |
| 743 |
|
| 744 |
/* ensure the node is suspended, since setting the PortConfig param |
| 745 |
* only works if the node is not running */ |
| 746 |
if (wp_node_get_state (self->node, NULL) >= WP_NODE_STATE_IDLE) { |
| 747 |
wp_debug_object (self, "suspending node before setting its PortConfig param"); |
| 748 |
wp_node_send_command (self->node, "Suspend"); |
| 749 |
} |
| 750 |
|
| 751 |
/* set task, format and mode */ |
| 752 |
self->format_task = g_steal_pointer (&task); |
| 753 |
g_clear_pointer (&self->format, wp_spa_pod_unref); |
| 754 |
self->format = g_steal_pointer (&format); |
| 755 |
strncpy (self->mode, mode ? mode : "dsp", sizeof (self->mode) - 1); |
| 756 |
|
| 757 |
si_audio_adapter_set_ports_state (self, |
| 758 |
WP_SI_ADAPTER_PORTS_STATE_CONFIGURING); |
| 759 |
|
| 760 |
/* configure DSP with chosen format */ |
| 761 |
wp_pipewire_object_set_param (WP_PIPEWIRE_OBJECT (self->node), |
| 762 |
"PortConfig", 0, wp_spa_pod_new_object ( |
| 763 |
"Spa:Pod:Object:Param:PortConfig", "PortConfig", |
| 764 |
"direction", "I", self->portconfig_direction, |
| 765 |
"mode", "K", self->mode, |
| 766 |
"monitor", "b", self->monitor, |
| 767 |
"control", "b", self->control_port, |
| 768 |
"format", "P", self->format, |
| 769 |
NULL)); |
| 770 |
|
| 771 |
/* the task finishes with new ports being emitted -> on_node_ports_changed */ |
| 772 |
} |
| 773 |
|
| 774 |
static gboolean |
| 775 |
si_audio_adapter_set_ports_format_finish (WpSiAdapter * item, |
| 776 |
GAsyncResult * res, GError ** error) |
| 777 |
{ |
| 778 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 779 |
} |
| 780 |
|
| 781 |
static void |
| 782 |
si_audio_adapter_adapter_init (WpSiAdapterInterface * iface) |
| 783 |
{ |
| 784 |
iface->get_ports_state = si_audio_adapter_get_ports_state; |
| 785 |
iface->get_ports_format = si_audio_adapter_get_ports_format; |
| 786 |
iface->set_ports_format = si_audio_adapter_set_ports_format; |
| 787 |
iface->set_ports_format_finish = si_audio_adapter_set_ports_format_finish; |
| 788 |
} |
| 789 |
|
| 790 |
static GVariant * |
| 791 |
si_audio_adapter_get_ports (WpSiLinkable * item, const gchar * context) |
| 792 |
{ |
| 793 |
WpSiAudioAdapter *self = WP_SI_AUDIO_ADAPTER (item); |
| 794 |
g_auto (GVariantBuilder) b = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_ARRAY); |
| 795 |
g_autoptr (WpIterator) it = NULL; |
| 796 |
g_auto (GValue) val = G_VALUE_INIT; |
| 797 |
WpDirection direction; |
| 798 |
guint32 node_id; |
| 799 |
|
| 800 |
if (!g_strcmp0 (context, "output")) { |
| 801 |
direction = WP_DIRECTION_OUTPUT; |
| 802 |
} |
| 803 |
else if (!g_strcmp0 (context, "input")) { |
| 804 |
direction = WP_DIRECTION_INPUT; |
| 805 |
} |
| 806 |
else { |
| 807 |
/* on any other context, return an empty list of ports */ |
| 808 |
return g_variant_new_array (G_VARIANT_TYPE ("(uuu)"), NULL, 0); |
| 809 |
} |
| 810 |
|
| 811 |
g_variant_builder_init (&b, G_VARIANT_TYPE ("a(uuu)")); |
| 812 |
node_id = wp_proxy_get_bound_id (WP_PROXY (self->node)); |
| 813 |
|
| 814 |
for (it = wp_node_new_ports_iterator (self->node); |
| 815 |
wp_iterator_next (it, &val); |
| 816 |
g_value_unset (&val)) |
| 817 |
{ |
| 818 |
WpPort *port = g_value_get_object (&val); |
| 819 |
g_autoptr (WpProperties) props = NULL; |
| 820 |
const gchar *channel; |
| 821 |
guint32 port_id, channel_id = 0; |
| 822 |
|
| 823 |
if (wp_port_get_direction (port) != direction) |
| 824 |
continue; |
| 825 |
|
| 826 |
port_id = wp_proxy_get_bound_id (WP_PROXY (port)); |
| 827 |
props = wp_pipewire_object_get_properties (WP_PIPEWIRE_OBJECT (port)); |
| 828 |
|
| 829 |
/* skip control ports for now */ |
| 830 |
if (spa_atob (wp_properties_get (props, PW_KEY_PORT_CONTROL))) |
| 831 |
continue; |
| 832 |
|
| 833 |
/* try to find the audio channel; if channel is NULL, this will silently |
| 834 |
leave the channel_id to its default value, 0 */ |
| 835 |
channel = wp_properties_get (props, PW_KEY_AUDIO_CHANNEL); |
| 836 |
if (channel) { |
| 837 |
WpSpaIdValue idval = wp_spa_id_value_from_short_name ( |
| 838 |
"Spa:Enum:AudioChannel", channel); |
| 839 |
if (idval) |
| 840 |
channel_id = wp_spa_id_value_number (idval); |
| 841 |
} |
| 842 |
|
| 843 |
g_variant_builder_add (&b, "(uuu)", node_id, port_id, channel_id); |
| 844 |
} |
| 845 |
|
| 846 |
return g_variant_builder_end (&b); |
| 847 |
} |
| 848 |
|
| 849 |
static void |
| 850 |
si_audio_adapter_linkable_init (WpSiLinkableInterface * iface) |
| 851 |
{ |
| 852 |
iface->get_ports = si_audio_adapter_get_ports; |
| 853 |
} |
| 854 |
|
| 855 |
WP_PLUGIN_EXPORT GObject * |
| 856 |
wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error) |
| 857 |
{ |
| 858 |
return G_OBJECT (wp_si_factory_new_simple (SI_FACTORY_NAME, |
| 859 |
si_audio_adapter_get_type ())); |
| 860 |
} |