| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <wp/wp.h> |
| 10 |
#include <math.h> |
| 11 |
#include <pipewire/pipewire.h> |
| 12 |
#include <spa/pod/iter.h> |
| 13 |
#include <spa/param/audio/raw.h> |
| 14 |
|
| 15 |
WP_DEFINE_LOCAL_LOG_TOPIC ("m-mixer-api") |
| 16 |
|
| 17 |
struct volume { |
| 18 |
uint8_t channels; |
| 19 |
float values[SPA_AUDIO_MAX_CHANNELS]; |
| 20 |
}; |
| 21 |
|
| 22 |
struct channel_map { |
| 23 |
uint8_t channels; |
| 24 |
uint32_t map[SPA_AUDIO_MAX_CHANNELS]; |
| 25 |
}; |
| 26 |
|
| 27 |
struct node_info { |
| 28 |
guint32 seq; |
| 29 |
|
| 30 |
guint32 device_id; |
| 31 |
gint32 route_index; |
| 32 |
gint32 route_device; |
| 33 |
|
| 34 |
struct volume volume; |
| 35 |
struct volume monitorVolume; |
| 36 |
struct channel_map map; |
| 37 |
gboolean mute; |
| 38 |
gboolean monitorMute; |
| 39 |
float svolume; |
| 40 |
float base; |
| 41 |
float step; |
| 42 |
}; |
| 43 |
|
| 44 |
struct _WpMixerApi |
| 45 |
{ |
| 46 |
WpPlugin parent; |
| 47 |
WpObjectManager *om; |
| 48 |
GHashTable *node_infos; |
| 49 |
guint32 seq; |
| 50 |
|
| 51 |
/* properties */ |
| 52 |
gint scale; |
| 53 |
}; |
| 54 |
|
| 55 |
enum { |
| 56 |
ACTION_SET_VOLUME, |
| 57 |
ACTION_GET_VOLUME, |
| 58 |
SIGNAL_CHANGED, |
| 59 |
N_SIGNALS |
| 60 |
}; |
| 61 |
|
| 62 |
enum { |
| 63 |
PROP_0, |
| 64 |
PROP_SCALE, |
| 65 |
}; |
| 66 |
|
| 67 |
static guint signals[N_SIGNALS] = {0}; |
| 68 |
|
| 69 |
G_DECLARE_FINAL_TYPE (WpMixerApi, wp_mixer_api, WP, MIXER_API, WpPlugin) |
| 70 |
G_DEFINE_TYPE (WpMixerApi, wp_mixer_api, WP_TYPE_PLUGIN) |
| 71 |
|
| 72 |
enum { |
| 73 |
SCALE_LINEAR, |
| 74 |
SCALE_CUBIC, |
| 75 |
}; |
| 76 |
|
| 77 |
static GType |
| 78 |
wp_mixer_api_volume_scale_enum_get_type (void) |
| 79 |
{ |
| 80 |
static gsize gtype_id = 0; |
| 81 |
static const GEnumValue values[] = { |
| 82 |
{ (gint) SCALE_LINEAR, "SCALE_LINEAR", "linear" }, |
| 83 |
{ (gint) SCALE_CUBIC, "SCALE_CUBIC", "cubic" }, |
| 84 |
{ 0, NULL, NULL } |
| 85 |
}; |
| 86 |
if (g_once_init_enter (>ype_id)) { |
| 87 |
GType new_type = g_enum_register_static ( |
| 88 |
g_intern_static_string ("WpMixerApiVolumeScale"), values); |
| 89 |
g_once_init_leave (>ype_id, new_type); |
| 90 |
} |
| 91 |
return (GType) gtype_id; |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
wp_mixer_api_init (WpMixerApi * self) |
| 96 |
{ |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
wp_mixer_api_get_property (GObject * object, guint property_id, |
| 101 |
GValue * value, GParamSpec * pspec) |
| 102 |
{ |
| 103 |
WpMixerApi *self = WP_MIXER_API (object); |
| 104 |
|
| 105 |
switch (property_id) { |
| 106 |
case PROP_SCALE: |
| 107 |
g_value_set_enum (value, self->scale); |
| 108 |
break; |
| 109 |
default: |
| 110 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
static void |
| 116 |
wp_mixer_api_set_property (GObject * object, guint property_id, |
| 117 |
const GValue * value, GParamSpec * pspec) |
| 118 |
{ |
| 119 |
WpMixerApi *self = WP_MIXER_API (object); |
| 120 |
|
| 121 |
switch (property_id) { |
| 122 |
case PROP_SCALE: |
| 123 |
self->scale = g_value_get_enum (value); |
| 124 |
break; |
| 125 |
default: |
| 126 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
static gboolean |
| 132 |
node_info_fill (struct node_info * info, WpSpaPod * props) |
| 133 |
{ |
| 134 |
g_autoptr (WpSpaPod) channelVolumes = NULL; |
| 135 |
g_autoptr (WpSpaPod) channelMap = NULL; |
| 136 |
g_autoptr (WpSpaPod) monitorVolumes = NULL; |
| 137 |
|
| 138 |
if (!wp_spa_pod_get_object (props, NULL, |
| 139 |
"mute", "b", &info->mute, |
| 140 |
"channelVolumes", "P", &channelVolumes, |
| 141 |
NULL)) |
| 142 |
return FALSE; |
| 143 |
|
| 144 |
/* default values */ |
| 145 |
info->svolume = 1.0; |
| 146 |
info->base = 1.0; |
| 147 |
info->step = 1.0 / 65536.0; |
| 148 |
|
| 149 |
wp_spa_pod_get_object (props, NULL, |
| 150 |
"channelMap", "?P", &channelMap, |
| 151 |
"volumeBase", "?f", &info->base, |
| 152 |
"volumeStep", "?f", &info->step, |
| 153 |
"volume", "?f", &info->svolume, |
| 154 |
"monitorVolumes", "?P", &monitorVolumes, |
| 155 |
"monitorMute", "?b", &info->monitorMute, |
| 156 |
NULL); |
| 157 |
|
| 158 |
info->volume.channels = spa_pod_copy_array ( |
| 159 |
wp_spa_pod_get_spa_pod (channelVolumes), SPA_TYPE_Float, |
| 160 |
info->volume.values, SPA_AUDIO_MAX_CHANNELS); |
| 161 |
|
| 162 |
if (channelMap) |
| 163 |
info->map.channels = spa_pod_copy_array ( |
| 164 |
wp_spa_pod_get_spa_pod (channelMap), SPA_TYPE_Id, |
| 165 |
info->map.map, SPA_AUDIO_MAX_CHANNELS); |
| 166 |
|
| 167 |
if (monitorVolumes) |
| 168 |
info->monitorVolume.channels = spa_pod_copy_array ( |
| 169 |
wp_spa_pod_get_spa_pod (monitorVolumes), SPA_TYPE_Float, |
| 170 |
info->monitorVolume.values, SPA_AUDIO_MAX_CHANNELS); |
| 171 |
|
| 172 |
return TRUE; |
| 173 |
} |
| 174 |
|
| 175 |
static void |
| 176 |
collect_node_info (WpMixerApi * self, struct node_info *info, |
| 177 |
WpPipewireObject * node) |
| 178 |
{ |
| 179 |
g_autoptr (WpPipewireObject) dev = NULL; |
| 180 |
const gchar *str = NULL; |
| 181 |
gboolean have_volume = FALSE; |
| 182 |
|
| 183 |
info->device_id = SPA_ID_INVALID; |
| 184 |
info->route_index = -1; |
| 185 |
info->route_device = -1; |
| 186 |
|
| 187 |
if ((str = wp_pipewire_object_get_property (node, PW_KEY_DEVICE_ID))) { |
| 188 |
dev = wp_object_manager_lookup (self->om, WP_TYPE_DEVICE, |
| 189 |
WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id", "=s", str, NULL); |
| 190 |
} |
| 191 |
|
| 192 |
if (dev && (str = wp_pipewire_object_get_property (node, "card.profile.device"))) { |
| 193 |
gint32 p_device = atoi (str); |
| 194 |
g_autoptr (WpIterator) it = NULL; |
| 195 |
g_auto (GValue) val = G_VALUE_INIT; |
| 196 |
|
| 197 |
it = wp_pipewire_object_enum_params_sync (dev, "Route", NULL); |
| 198 |
for (; it && wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 199 |
WpSpaPod *param = g_value_get_boxed (&val); |
| 200 |
gint32 r_index = -1, r_device = -1; |
| 201 |
g_autoptr (WpSpaPod) props = NULL; |
| 202 |
|
| 203 |
if (!wp_spa_pod_get_object (param, NULL, |
| 204 |
"index", "i", &r_index, |
| 205 |
"device", "i", &r_device, |
| 206 |
"props", "P", &props, |
| 207 |
NULL)) |
| 208 |
continue; |
| 209 |
if (r_device != p_device) |
| 210 |
continue; |
| 211 |
|
| 212 |
if (props && node_info_fill (info, props)) { |
| 213 |
info->device_id = wp_proxy_get_bound_id (WP_PROXY (dev)); |
| 214 |
info->route_index = r_index; |
| 215 |
info->route_device = r_device; |
| 216 |
have_volume = TRUE; |
| 217 |
g_value_unset (&val); |
| 218 |
break; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if (!have_volume) { |
| 224 |
g_autoptr (WpIterator) it = NULL; |
| 225 |
g_auto (GValue) val = G_VALUE_INIT; |
| 226 |
|
| 227 |
it = wp_pipewire_object_enum_params_sync (node, "Props", NULL); |
| 228 |
for (; it && wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 229 |
WpSpaPod *param = g_value_get_boxed (&val); |
| 230 |
if (node_info_fill (info, param)) { |
| 231 |
g_value_unset (&val); |
| 232 |
break; |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
static void on_objects_changed (WpObjectManager * om, WpMixerApi * self); |
| 239 |
|
| 240 |
static void |
| 241 |
on_sync_done (WpCore * core, GAsyncResult * res, WpMixerApi * self) |
| 242 |
{ |
| 243 |
g_autoptr (GError) error = NULL; |
| 244 |
if (!wp_core_sync_finish (core, res, &error)) |
| 245 |
wp_warning_object (core, "sync error: %s", error->message); |
| 246 |
if (self->om) { |
| 247 |
on_objects_changed (self->om, self); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
static void |
| 252 |
on_params_changed (WpPipewireObject * obj, const gchar * param_name, |
| 253 |
WpMixerApi * self) |
| 254 |
{ |
| 255 |
if ((WP_IS_NODE (obj) && !g_strcmp0 (param_name, "Props")) || |
| 256 |
(WP_IS_DEVICE (obj) && !g_strcmp0 (param_name, "Route"))) { |
| 257 |
g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self)); |
| 258 |
wp_core_sync (core, NULL, (GAsyncReadyCallback) on_sync_done, self); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
static void |
| 263 |
on_objects_changed (WpObjectManager * om, WpMixerApi * self) |
| 264 |
{ |
| 265 |
g_autoptr (WpIterator) it = |
| 266 |
wp_object_manager_new_filtered_iterator (om, WP_TYPE_NODE, NULL); |
| 267 |
g_auto (GValue) val = G_VALUE_INIT; |
| 268 |
GHashTableIter infos_it; |
| 269 |
struct node_info *info; |
| 270 |
struct node_info old; |
| 271 |
|
| 272 |
self->seq++; |
| 273 |
|
| 274 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 275 |
WpPipewireObject *node = g_value_get_object (&val); |
| 276 |
guint id = wp_proxy_get_bound_id (WP_PROXY (node)); |
| 277 |
|
| 278 |
info = g_hash_table_lookup (self->node_infos, GUINT_TO_POINTER (id)); |
| 279 |
if (!info) { |
| 280 |
info = g_slice_new0 (struct node_info); |
| 281 |
g_hash_table_insert (self->node_infos, GUINT_TO_POINTER (id), info); |
| 282 |
} |
| 283 |
info->seq = self->seq; |
| 284 |
|
| 285 |
old = *info; |
| 286 |
collect_node_info (self, info, node); |
| 287 |
if (memcmp (&old, info, sizeof (struct node_info)) != 0) { |
| 288 |
wp_debug_object (self, "node %u changed volume props", id); |
| 289 |
g_signal_emit (self, signals[SIGNAL_CHANGED], 0, id); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
/* remove node_info of nodes that were removed from the object manager */ |
| 294 |
g_hash_table_iter_init (&infos_it, self->node_infos); |
| 295 |
while (g_hash_table_iter_next (&infos_it, NULL, (gpointer *) &info)) { |
| 296 |
if (info->seq != self->seq) |
| 297 |
g_hash_table_iter_remove (&infos_it); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
static void |
| 302 |
on_object_added (WpObjectManager * om, WpProxy * obj, WpMixerApi * self) |
| 303 |
{ |
| 304 |
g_signal_connect (obj, "params-changed", G_CALLBACK (on_params_changed), self); |
| 305 |
} |
| 306 |
|
| 307 |
static void |
| 308 |
on_object_removed (WpObjectManager * om, WpProxy * obj, WpMixerApi * self) |
| 309 |
{ |
| 310 |
g_signal_handlers_disconnect_by_func (obj, G_CALLBACK (on_params_changed), self); |
| 311 |
} |
| 312 |
|
| 313 |
static void |
| 314 |
node_info_free (gpointer info) |
| 315 |
{ |
| 316 |
g_slice_free (struct node_info, info); |
| 317 |
} |
| 318 |
|
| 319 |
static void |
| 320 |
on_om_installed (WpObjectManager * om, WpMixerApi * self) |
| 321 |
{ |
| 322 |
wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0); |
| 323 |
} |
| 324 |
|
| 325 |
static void |
| 326 |
wp_mixer_api_enable (WpPlugin * plugin, WpTransition * transition) |
| 327 |
{ |
| 328 |
WpMixerApi * self = WP_MIXER_API (plugin); |
| 329 |
g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (plugin)); |
| 330 |
g_return_if_fail (core); |
| 331 |
|
| 332 |
self->node_infos = g_hash_table_new_full (g_direct_hash, g_direct_equal, |
| 333 |
NULL, node_info_free); |
| 334 |
|
| 335 |
self->om = wp_object_manager_new (); |
| 336 |
wp_object_manager_add_interest (self->om, WP_TYPE_NODE, |
| 337 |
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "media.class", "#s", "*Audio*", |
| 338 |
NULL); |
| 339 |
wp_object_manager_add_interest (self->om, WP_TYPE_DEVICE, |
| 340 |
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "media.class", "=s", "Audio/Device", |
| 341 |
NULL); |
| 342 |
wp_object_manager_request_object_features (self->om, |
| 343 |
WP_TYPE_GLOBAL_PROXY, WP_OBJECT_FEATURES_ALL); |
| 344 |
g_signal_connect_object (self->om, "objects-changed", |
| 345 |
G_CALLBACK (on_objects_changed), self, 0); |
| 346 |
g_signal_connect_object (self->om, "object-added", |
| 347 |
G_CALLBACK (on_object_added), self, 0); |
| 348 |
g_signal_connect_object (self->om, "object-removed", |
| 349 |
G_CALLBACK (on_object_removed), self, 0); |
| 350 |
g_signal_connect_object (self->om, "installed", |
| 351 |
G_CALLBACK (on_om_installed), self, 0); |
| 352 |
wp_core_install_object_manager (core, self->om); |
| 353 |
} |
| 354 |
|
| 355 |
static void |
| 356 |
wp_mixer_api_disable (WpPlugin * plugin) |
| 357 |
{ |
| 358 |
WpMixerApi * self = WP_MIXER_API (plugin); |
| 359 |
|
| 360 |
{ |
| 361 |
g_autoptr (WpIterator) it = wp_object_manager_new_iterator (self->om); |
| 362 |
g_auto (GValue) val = G_VALUE_INIT; |
| 363 |
|
| 364 |
for (; wp_iterator_next (it, &val); g_value_unset (&val)) { |
| 365 |
WpProxy *obj = g_value_get_object (&val); |
| 366 |
on_object_removed (self->om, obj, self); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
g_clear_object (&self->om); |
| 371 |
g_clear_pointer (&self->node_infos, g_hash_table_unref); |
| 372 |
} |
| 373 |
|
| 374 |
static inline gdouble |
| 375 |
volume_from_linear (float vol, gint scale) |
| 376 |
{ |
| 377 |
if (vol <= 0.0f) |
| 378 |
return 0.0; |
| 379 |
else if (scale == SCALE_CUBIC) |
| 380 |
return cbrt(vol); |
| 381 |
else |
| 382 |
return vol; |
| 383 |
} |
| 384 |
|
| 385 |
static inline float |
| 386 |
volume_to_linear (gdouble vol, gint scale) |
| 387 |
{ |
| 388 |
if (vol <= 0.0f) |
| 389 |
return 0.0; |
| 390 |
else if (scale == SCALE_CUBIC) |
| 391 |
return vol * vol * vol; |
| 392 |
else |
| 393 |
return vol; |
| 394 |
} |
| 395 |
|
| 396 |
static gboolean |
| 397 |
wp_mixer_api_set_volume (WpMixerApi * self, guint32 id, GVariant * vvolume) |
| 398 |
{ |
| 399 |
struct node_info *info = self->node_infos ? |
| 400 |
g_hash_table_lookup (self->node_infos, GUINT_TO_POINTER (id)) : NULL; |
| 401 |
struct volume new_volume = {0}; |
| 402 |
struct volume new_monVolume = {0}; |
| 403 |
gboolean has_mute = FALSE; |
| 404 |
gboolean has_monitorMute = FALSE; |
| 405 |
gboolean mute = FALSE; |
| 406 |
gboolean monitorMute = FALSE; |
| 407 |
WpSpaIdTable t_audioChannel = |
| 408 |
wp_spa_id_table_from_name ("Spa:Enum:AudioChannel"); |
| 409 |
|
| 410 |
if (!info || !vvolume) |
| 411 |
return FALSE; |
| 412 |
|
| 413 |
if (g_variant_is_of_type (vvolume, G_VARIANT_TYPE_DOUBLE)) { |
| 414 |
gdouble val = g_variant_get_double (vvolume); |
| 415 |
new_volume = info->volume; |
| 416 |
for (uint i = 0; i < new_volume.channels; i++) |
| 417 |
new_volume.values[i] = volume_to_linear (val, self->scale); |
| 418 |
} |
| 419 |
else if (g_variant_is_of_type (vvolume, G_VARIANT_TYPE_VARDICT)) { |
| 420 |
GVariantIter *iter; |
| 421 |
const gchar *idx_str; |
| 422 |
GVariant *v; |
| 423 |
gdouble val; |
| 424 |
|
| 425 |
has_mute = g_variant_lookup (vvolume, "mute", "b", &mute); |
| 426 |
has_monitorMute = g_variant_lookup (vvolume, "monitorMute", "b", &monitorMute); |
| 427 |
|
| 428 |
if (g_variant_lookup (vvolume, "volume", "d", &val)) { |
| 429 |
new_volume = info->volume; |
| 430 |
for (uint i = 0; i < new_volume.channels; i++) |
| 431 |
new_volume.values[i] = volume_to_linear (val, self->scale); |
| 432 |
} |
| 433 |
|
| 434 |
if (g_variant_lookup (vvolume, "monitorVolume", "d", &val)) { |
| 435 |
new_monVolume = info->monitorVolume; |
| 436 |
for (uint i = 0; i < new_monVolume.channels; i++) |
| 437 |
new_monVolume.values[i] = volume_to_linear (val, self->scale); |
| 438 |
} |
| 439 |
|
| 440 |
if (g_variant_lookup (vvolume, "channelVolumes", "a{sv}", &iter)) { |
| 441 |
/* keep the existing volume values for unspecified channels */ |
| 442 |
new_volume = info->volume; |
| 443 |
new_monVolume = info->monitorVolume; |
| 444 |
|
| 445 |
while (g_variant_iter_loop (iter, "{&sv}", &idx_str, &v)) { |
| 446 |
guint index = atoi (idx_str); |
| 447 |
const gchar *channel_str = NULL; |
| 448 |
WpSpaIdValue channel = NULL; |
| 449 |
|
| 450 |
if (g_variant_lookup (v, "channel", "&s", &channel_str)) { |
| 451 |
channel = wp_spa_id_table_find_value_from_short_name ( |
| 452 |
t_audioChannel, channel_str); |
| 453 |
if (!channel) |
| 454 |
wp_notice_object (self, "invalid channel: %s", channel_str); |
| 455 |
} |
| 456 |
|
| 457 |
if (channel) { |
| 458 |
for (uint i = 0; i < info->map.channels; i++) |
| 459 |
if (info->map.map[i] == wp_spa_id_value_number (channel)) { |
| 460 |
index = i; |
| 461 |
break; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
if (index >= MIN(new_volume.channels, SPA_AUDIO_MAX_CHANNELS)) { |
| 466 |
wp_notice_object (self, "invalid channel index: %u", index); |
| 467 |
continue; |
| 468 |
} |
| 469 |
|
| 470 |
if (g_variant_lookup (v, "volume", "d", &val)) { |
| 471 |
new_volume.values[index] = volume_to_linear (val, self->scale); |
| 472 |
} |
| 473 |
if (g_variant_lookup (v, "monitorVolume", "d", &val)) { |
| 474 |
new_monVolume.values[index] = volume_to_linear (val, self->scale); |
| 475 |
} |
| 476 |
} |
| 477 |
g_variant_iter_free (iter); |
| 478 |
} |
| 479 |
} else { |
| 480 |
return FALSE; |
| 481 |
} |
| 482 |
|
| 483 |
/* set param */ |
| 484 |
g_autoptr (WpSpaPod) props = NULL; |
| 485 |
g_autoptr (WpSpaPodBuilder) b = |
| 486 |
wp_spa_pod_builder_new_object ("Spa:Pod:Object:Param:Props", "Props"); |
| 487 |
|
| 488 |
if (new_volume.channels > 0) |
| 489 |
wp_spa_pod_builder_add (b, "channelVolumes", "a", |
| 490 |
sizeof(float), SPA_TYPE_Float, |
| 491 |
new_volume.channels, new_volume.values, NULL); |
| 492 |
if (new_monVolume.channels > 0) |
| 493 |
wp_spa_pod_builder_add (b, "monitorVolumes", "a", |
| 494 |
sizeof(float), SPA_TYPE_Float, |
| 495 |
new_monVolume.channels, new_monVolume.values, NULL); |
| 496 |
if (has_mute) |
| 497 |
wp_spa_pod_builder_add (b, "mute", "b", mute, NULL); |
| 498 |
if (has_monitorMute) |
| 499 |
wp_spa_pod_builder_add (b, "monitorMute", "b", monitorMute, NULL); |
| 500 |
|
| 501 |
props = wp_spa_pod_builder_end (b); |
| 502 |
|
| 503 |
if (info->device_id != SPA_ID_INVALID) { |
| 504 |
g_autoptr (WpPipewireObject) device = wp_object_manager_lookup (self->om, |
| 505 |
WP_TYPE_DEVICE, WP_CONSTRAINT_TYPE_G_PROPERTY, |
| 506 |
"bound-id", "=u", info->device_id, NULL); |
| 507 |
g_return_val_if_fail (device != NULL, FALSE); |
| 508 |
|
| 509 |
wp_pipewire_object_set_param (device, "Route", 0, wp_spa_pod_new_object ( |
| 510 |
"Spa:Pod:Object:Param:Route", "Route", |
| 511 |
"index", "i", info->route_index, |
| 512 |
"device", "i", info->route_device, |
| 513 |
"props", "P", props, |
| 514 |
"save", "b", true, |
| 515 |
NULL)); |
| 516 |
} else { |
| 517 |
g_autoptr (WpPipewireObject) node = wp_object_manager_lookup (self->om, |
| 518 |
WP_TYPE_NODE, WP_CONSTRAINT_TYPE_G_PROPERTY, |
| 519 |
"bound-id", "=u", id, NULL); |
| 520 |
g_return_val_if_fail (node != NULL, FALSE); |
| 521 |
|
| 522 |
wp_pipewire_object_set_param (node, "Props", 0, g_steal_pointer (&props)); |
| 523 |
} |
| 524 |
|
| 525 |
return TRUE; |
| 526 |
} |
| 527 |
|
| 528 |
static GVariant * |
| 529 |
wp_mixer_api_get_volume (WpMixerApi * self, guint32 id) |
| 530 |
{ |
| 531 |
struct node_info *info = self->node_infos ? |
| 532 |
g_hash_table_lookup (self->node_infos, GUINT_TO_POINTER (id)) : NULL; |
| 533 |
g_auto (GVariantBuilder) b = |
| 534 |
G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT); |
| 535 |
g_auto (GVariantBuilder) b_vol = |
| 536 |
G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT); |
| 537 |
WpSpaIdTable t_audioChannel = |
| 538 |
wp_spa_id_table_from_name ("Spa:Enum:AudioChannel"); |
| 539 |
|
| 540 |
if (!info) |
| 541 |
return NULL; |
| 542 |
|
| 543 |
g_variant_builder_add (&b, "{sv}", "id", g_variant_new_uint32 (id)); |
| 544 |
g_variant_builder_add (&b, "{sv}", "mute", g_variant_new_boolean (info->mute)); |
| 545 |
g_variant_builder_add (&b, "{sv}", "base", g_variant_new_double (info->base)); |
| 546 |
g_variant_builder_add (&b, "{sv}", "step", g_variant_new_double (info->step)); |
| 547 |
g_variant_builder_add (&b, "{sv}", "volume", g_variant_new_double ( |
| 548 |
volume_from_linear ((info->volume.channels > 0) ? |
| 549 |
info->volume.values[0] : info->svolume, self->scale))); |
| 550 |
if (info->monitorVolume.channels > 0) { |
| 551 |
g_variant_builder_add (&b, "{sv}", "monitorVolume", g_variant_new_double ( |
| 552 |
volume_from_linear (info->monitorVolume.values[0], self->scale))); |
| 553 |
} |
| 554 |
g_variant_builder_add (&b, "{sv}", "monitorMute", g_variant_new_boolean (info->monitorMute)); |
| 555 |
|
| 556 |
for (guint i = 0; i < info->volume.channels; i++) { |
| 557 |
gchar index_str[10]; |
| 558 |
g_auto (GVariantBuilder) b_vol_nested = |
| 559 |
G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT); |
| 560 |
|
| 561 |
g_variant_builder_add (&b_vol_nested, "{sv}", |
| 562 |
"volume", g_variant_new_double ( |
| 563 |
volume_from_linear (info->volume.values[i], self->scale))); |
| 564 |
|
| 565 |
if (i < info->map.channels) { |
| 566 |
WpSpaIdValue v = |
| 567 |
wp_spa_id_table_find_value (t_audioChannel, info->map.map[i]); |
| 568 |
if (v) { |
| 569 |
const gchar *channel_str = wp_spa_id_value_short_name (v); |
| 570 |
g_variant_builder_add (&b_vol_nested, "{sv}", |
| 571 |
"channel", g_variant_new_string (channel_str)); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
if (i < info->monitorVolume.channels) { |
| 576 |
g_variant_builder_add (&b_vol_nested, "{sv}", |
| 577 |
"monitorVolume", g_variant_new_double ( |
| 578 |
volume_from_linear (info->monitorVolume.values[i], self->scale))); |
| 579 |
} |
| 580 |
|
| 581 |
g_snprintf (index_str, 10, "%u", i); |
| 582 |
g_variant_builder_add (&b_vol, "{sv}", index_str, |
| 583 |
g_variant_builder_end (&b_vol_nested)); |
| 584 |
} |
| 585 |
|
| 586 |
g_variant_builder_add (&b, "{sv}", |
| 587 |
"channelVolumes", g_variant_builder_end (&b_vol)); |
| 588 |
return g_variant_builder_end (&b); |
| 589 |
} |
| 590 |
|
| 591 |
static void |
| 592 |
wp_mixer_api_class_init (WpMixerApiClass * klass) |
| 593 |
{ |
| 594 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 595 |
WpPluginClass *plugin_class = (WpPluginClass *) klass; |
| 596 |
|
| 597 |
object_class->set_property = wp_mixer_api_set_property; |
| 598 |
object_class->get_property = wp_mixer_api_get_property; |
| 599 |
|
| 600 |
plugin_class->enable = wp_mixer_api_enable; |
| 601 |
plugin_class->disable = wp_mixer_api_disable; |
| 602 |
|
| 603 |
g_object_class_install_property (object_class, PROP_SCALE, |
| 604 |
g_param_spec_enum ("scale", "scale", "scale", |
| 605 |
wp_mixer_api_volume_scale_enum_get_type (), |
| 606 |
SCALE_LINEAR, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 607 |
|
| 608 |
signals[ACTION_SET_VOLUME] = g_signal_new_class_handler ( |
| 609 |
"set-volume", G_TYPE_FROM_CLASS (klass), |
| 610 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
| 611 |
(GCallback) wp_mixer_api_set_volume, |
| 612 |
NULL, NULL, NULL, |
| 613 |
G_TYPE_BOOLEAN, 2, G_TYPE_UINT, G_TYPE_VARIANT); |
| 614 |
|
| 615 |
signals[ACTION_GET_VOLUME] = g_signal_new_class_handler ( |
| 616 |
"get-volume", G_TYPE_FROM_CLASS (klass), |
| 617 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
| 618 |
(GCallback) wp_mixer_api_get_volume, |
| 619 |
NULL, NULL, NULL, |
| 620 |
G_TYPE_VARIANT, 1, G_TYPE_UINT); |
| 621 |
|
| 622 |
signals[SIGNAL_CHANGED] = g_signal_new ( |
| 623 |
"changed", G_TYPE_FROM_CLASS (klass), |
| 624 |
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_UINT); |
| 625 |
} |
| 626 |
|
| 627 |
WP_PLUGIN_EXPORT GObject * |
| 628 |
wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error) |
| 629 |
{ |
| 630 |
return G_OBJECT (g_object_new (wp_mixer_api_get_type (), |
| 631 |
"name", "mixer-api", |
| 632 |
"core", core, |
| 633 |
NULL)); |
| 634 |
} |