Debian Codegraph @

pipewire-alsa/alsa-plugins/ctl_pipewire.c

raw symbols SCIP
1 /* CTL - PipeWire plugin */
2 /* SPDX-FileCopyrightText: Copyright © 2020 Wim Taymans */
3 /* SPDX-License-Identifier: MIT */
4
5 #include <alsa/asoundlib.h>
6 #include <alsa/control_external.h>
7
8 #include <spa/utils/result.h>
9 #include <spa/utils/string.h>
10 #include <spa/utils/json.h>
11 #include <spa/param/props.h>
12 #include <spa/param/audio/format-utils.h>
13
14 #include <pipewire/pipewire.h>
15 #include <pipewire/extensions/metadata.h>
16
17 PW_LOG_TOPIC_STATIC(alsa_log_topic, "alsa.ctl");
18 #define PW_LOG_TOPIC_DEFAULT alsa_log_topic
19
20 #define DEFAULT_VOLUME_METHOD "cubic"
21
22 #define VOLUME_MIN ((uint32_t) 0U)
23 #define VOLUME_MAX ((uint32_t) 0x10000U)
24
25 #define MAX_CHANNELS SPA_AUDIO_MAX_CHANNELS
26
27 struct volume {
28 uint32_t channels;
29 long values[MAX_CHANNELS];
30 };
31
32 typedef struct {
33 snd_ctl_ext_t ext;
34
35 struct pw_properties *props;
36
37 struct spa_system *system;
38 struct pw_thread_loop *mainloop;
39
40 struct pw_context *context;
41 struct pw_core *core;
42 struct spa_hook core_listener;
43
44 struct pw_registry *registry;
45 struct spa_hook registry_listener;
46
47 struct pw_metadata *metadata;
48 struct spa_hook metadata_listener;
49
50 int fd;
51 int last_seq;
52 int pending_seq;
53 int error;
54
55 char default_sink[1024];
56 int sink_muted;
57 struct volume sink_volume;
58
59 char default_source[1024];
60 int source_muted;
61 struct volume source_volume;
62
63 int subscribed;
64 #define VOLUME_METHOD_LINEAR (0)
65 #define VOLUME_METHOD_CUBIC (1)
66 int volume_method;
67
68 #define UPDATE_SINK_VOL (1<<0)
69 #define UPDATE_SINK_MUTE (1<<1)
70 #define UPDATE_SOURCE_VOL (1<<2)
71 #define UPDATE_SOURCE_MUTE (1<<3)
72 int updated;
73
74 struct spa_list globals;
75 } snd_ctl_pipewire_t;
76
77 static inline uint32_t volume_from_linear(float vol, int method)
78 {
79 if (vol <= 0.0f)
80 vol = 0.0f;
81
82 switch (method) {
83 case VOLUME_METHOD_CUBIC:
84 vol = cbrtf(vol);
85 break;
86 }
87 return SPA_CLAMP((uint64_t)lroundf(vol * VOLUME_MAX),
88 VOLUME_MIN, VOLUME_MAX);
89 }
90
91 static inline float volume_to_linear(uint32_t vol, int method)
92 {
93 float v = ((float)vol) / VOLUME_MAX;
94
95 switch (method) {
96 case VOLUME_METHOD_CUBIC:
97 v = v * v * v;
98 break;
99 }
100 return v;
101 }
102
103 struct global;
104
105 struct global_info {
106 const char *type;
107 uint32_t version;
108 const void *events;
109 pw_destroy_t destroy;
110 int (*init) (struct global *g);
111 };
112
113 struct global {
114 struct spa_list link;
115
116 snd_ctl_pipewire_t *ctl;
117
118 const struct global_info *ginfo;
119
120 uint32_t id;
121 uint32_t permissions;
122 struct pw_properties *props;
123
124 struct pw_proxy *proxy;
125 struct spa_hook proxy_listener;
126 struct spa_hook object_listener;
127
128 union {
129 struct {
130 #define NODE_FLAG_SINK (1<<0)
131 #define NODE_FLAG_SOURCE (1<<1)
132 #define NODE_FLAG_DEVICE_VOLUME (1<<2)
133 #define NODE_FLAG_DEVICE_MUTE (1<<3)
134 uint32_t flags;
135 uint32_t device_id;
136 uint32_t profile_device_id;
137 int priority;
138 float volume;
139 bool mute;
140 struct volume channel_volume;
141 } node;
142 struct {
143 uint32_t active_route_output;
144 uint32_t active_route_input;
145 } device;
146 };
147 };
148
149 #define SOURCE_VOL_NAME "Capture Volume"
150 #define SOURCE_MUTE_NAME "Capture Switch"
151 #define SINK_VOL_NAME "Master Playback Volume"
152 #define SINK_MUTE_NAME "Master Playback Switch"
153
154 static void do_resync(snd_ctl_pipewire_t *ctl)
155 {
156 ctl->pending_seq = pw_core_sync(ctl->core, PW_ID_CORE, ctl->pending_seq);
157 }
158
159 static int wait_resync(snd_ctl_pipewire_t *ctl)
160 {
161 int res;
162 do_resync(ctl);
163
164 while (true) {
165 pw_thread_loop_wait(ctl->mainloop);
166
167 res = ctl->error;
168 if (res < 0) {
169 ctl->error = 0;
170 return res;
171 }
172
173 if (ctl->pending_seq == ctl->last_seq)
174 break;
175 }
176 return 0;
177 }
178
179 static struct global *find_global(snd_ctl_pipewire_t *ctl, uint32_t id,
180 const char *name, const char *type)
181 {
182 struct global *g;
183 uint32_t name_id = name ? (uint32_t)atoi(name) : SPA_ID_INVALID;
184 const char *str;
185
186 spa_list_for_each(g, &ctl->globals, link) {
187 if ((g->id == id || g->id == name_id) &&
188 (type == NULL || spa_streq(g->ginfo->type, type)))
189 return g;
190 if (name != NULL && name[0] != '\0' &&
191 (str = pw_properties_get(g->props, PW_KEY_NODE_NAME)) != NULL &&
192 spa_streq(name, str))
193 return g;
194 }
195 return NULL;
196 }
197
198 static struct global *find_best_node(snd_ctl_pipewire_t *ctl, uint32_t flags)
199 {
200 struct global *g, *best = NULL;
201 spa_list_for_each(g, &ctl->globals, link) {
202 if ((spa_streq(g->ginfo->type, PW_TYPE_INTERFACE_Node)) &&
203 (flags == 0 || (g->node.flags & flags) == flags) &&
204 (best == NULL || best->node.priority < g->node.priority))
205 best = g;
206 }
207 return best;
208 }
209
210 static inline int poll_activate(snd_ctl_pipewire_t *ctl)
211 {
212 spa_system_eventfd_write(ctl->system, ctl->fd, 1);
213 return 1;
214 }
215
216 static inline int poll_deactivate(snd_ctl_pipewire_t *ctl)
217 {
218 uint64_t val;
219 spa_system_eventfd_read(ctl->system, ctl->fd, &val);
220 return 1;
221 }
222
223 static bool volume_equal(struct volume *a, struct volume *b)
224 {
225 if (a == b)
226 return true;
227 if (a->channels != b->channels)
228 return false;
229 return memcmp(a->values, b->values, sizeof(float) * a->channels) == 0;
230 }
231
232 static int pipewire_update_volume(snd_ctl_pipewire_t * ctl)
233 {
234 bool changed = false;
235 struct global *g;
236
237 if (ctl->default_sink[0] == '\0')
238 g = find_best_node(ctl, NODE_FLAG_SINK);
239 else
240 g = find_global(ctl, SPA_ID_INVALID, ctl->default_sink,
241 PW_TYPE_INTERFACE_Node);
242
243 if (g) {
244 if (!!ctl->sink_muted != !!g->node.mute) {
245 ctl->sink_muted = g->node.mute;
246 ctl->updated |= UPDATE_SINK_MUTE;
247 changed = true;
248 }
249 if (!volume_equal(&ctl->sink_volume, &g->node.channel_volume)) {
250 ctl->sink_volume = g->node.channel_volume;
251 ctl->updated |= UPDATE_SINK_VOL;
252 changed = true;
253 }
254 }
255
256 if (ctl->default_source[0] == '\0')
257 g = find_best_node(ctl, NODE_FLAG_SOURCE);
258 else
259 g = find_global(ctl, SPA_ID_INVALID, ctl->default_source,
260 PW_TYPE_INTERFACE_Node);
261
262 if (g) {
263 if (!!ctl->source_muted != !!g->node.mute) {
264 ctl->source_muted = g->node.mute;
265 ctl->updated |= UPDATE_SOURCE_MUTE;
266 changed = true;
267 }
268 if (!volume_equal(&ctl->source_volume, &g->node.channel_volume)) {
269 ctl->source_volume = g->node.channel_volume;
270 ctl->updated |= UPDATE_SOURCE_VOL;
271 changed = true;
272 }
273 }
274
275 if (changed)
276 poll_activate(ctl);
277
278 return 0;
279 }
280
281 static int pipewire_elem_count(snd_ctl_ext_t * ext)
282 {
283 snd_ctl_pipewire_t *ctl = ext->private_data;
284 int count = 0, err;
285
286 assert(ctl);
287
288 if (!ctl->mainloop)
289 return -EBADFD;
290
291 pw_thread_loop_lock(ctl->mainloop);
292
293 err = ctl->error;
294 if (err < 0) {
295 ctl->error = 0;
296 count = err;
297 goto finish;
298 }
299 err = pipewire_update_volume(ctl);
300 if (err < 0) {
301 count = err;
302 goto finish;
303 }
304
305 if (ctl->default_source[0] != '\0')
306 count += 2;
307 if (ctl->default_sink[0] != '\0')
308 count += 2;
309
310 finish:
311 pw_thread_loop_unlock(ctl->mainloop);
312
313 return count;
314 }
315
316 static int pipewire_elem_list(snd_ctl_ext_t * ext, unsigned int offset,
317 snd_ctl_elem_id_t * id)
318 {
319 snd_ctl_pipewire_t *ctl = ext->private_data;
320 int err;
321
322 assert(ctl);
323
324 if (!ctl->mainloop)
325 return -EBADFD;
326
327 snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);
328
329 pw_thread_loop_lock(ctl->mainloop);
330
331 err = ctl->error;
332 if (err < 0) {
333 ctl->error = 0;
334 goto finish;
335 }
336
337 if (ctl->default_source[0] != '\0') {
338 if (offset == 0)
339 snd_ctl_elem_id_set_name(id, SOURCE_VOL_NAME);
340 else if (offset == 1)
341 snd_ctl_elem_id_set_name(id, SOURCE_MUTE_NAME);
342 } else
343 offset += 2;
344
345 err = 0;
346 finish:
347 pw_thread_loop_unlock(ctl->mainloop);
348
349 if (err >= 0) {
350 if (offset == 2)
351 snd_ctl_elem_id_set_name(id, SINK_VOL_NAME);
352 else if (offset == 3)
353 snd_ctl_elem_id_set_name(id, SINK_MUTE_NAME);
354 }
355
356 return err;
357 }
358
359 static snd_ctl_ext_key_t pipewire_find_elem(snd_ctl_ext_t * ext,
360 const snd_ctl_elem_id_t * id)
361 {
362 const char *name;
363 unsigned int numid;
364
365 numid = snd_ctl_elem_id_get_numid(id);
366 if (numid > 0 && numid <= 4)
367 return numid - 1;
368
369 name = snd_ctl_elem_id_get_name(id);
370
371 if (spa_streq(name, SOURCE_VOL_NAME))
372 return 0;
373 if (spa_streq(name, SOURCE_MUTE_NAME))
374 return 1;
375 if (spa_streq(name, SINK_VOL_NAME))
376 return 2;
377 if (spa_streq(name, SINK_MUTE_NAME))
378 return 3;
379
380 return SND_CTL_EXT_KEY_NOT_FOUND;
381 }
382
383 static int pipewire_get_attribute(snd_ctl_ext_t * ext, snd_ctl_ext_key_t key,
384 int *type, unsigned int *acc,
385 unsigned int *count)
386 {
387 snd_ctl_pipewire_t *ctl = ext->private_data;
388 int err = 0;
389
390 if (key > 3)
391 return -EINVAL;
392
393 assert(ctl);
394
395 if (!ctl->mainloop)
396 return -EBADFD;
397
398 pw_thread_loop_lock(ctl->mainloop);
399
400 err = ctl->error;
401 if (err < 0) {
402 ctl->error = 0;
403 goto finish;
404 }
405
406 err = pipewire_update_volume(ctl);
407 if (err < 0)
408 goto finish;
409
410 if (key & 1)
411 *type = SND_CTL_ELEM_TYPE_BOOLEAN;
412 else
413 *type = SND_CTL_ELEM_TYPE_INTEGER;
414
415 *acc = SND_CTL_EXT_ACCESS_READWRITE;
416
417 if (key == 0)
418 *count = ctl->source_volume.channels;
419 else if (key == 2)
420 *count = ctl->sink_volume.channels;
421 else
422 *count = 1;
423
424 finish:
425 pw_thread_loop_unlock(ctl->mainloop);
426
427 return err;
428 }
429
430 static int pipewire_get_integer_info(snd_ctl_ext_t * ext,
431 snd_ctl_ext_key_t key, long *imin,
432 long *imax, long *istep)
433 {
434 *istep = 1;
435 *imin = VOLUME_MIN;
436 *imax = VOLUME_MAX;
437
438 return 0;
439 }
440
441 static int pipewire_read_integer(snd_ctl_ext_t * ext, snd_ctl_ext_key_t key,
442 long *value)
443 {
444 snd_ctl_pipewire_t *ctl = ext->private_data;
445 int err = 0;
446 uint32_t i;
447 struct volume *vol = NULL;
448
449 assert(ctl);
450
451 if (!ctl->mainloop)
452 return -EBADFD;
453
454 pw_thread_loop_lock(ctl->mainloop);
455
456 err = ctl->error;
457 if (err < 0) {
458 ctl->error = 0;
459 goto finish;
460 }
461
462 err = pipewire_update_volume(ctl);
463 if (err < 0)
464 goto finish;
465
466 switch (key) {
467 case 0:
468 vol = &ctl->source_volume;
469 break;
470 case 1:
471 *value = !ctl->source_muted;
472 break;
473 case 2:
474 vol = &ctl->sink_volume;
475 break;
476 case 3:
477 *value = !ctl->sink_muted;
478 break;
479 default:
480 err = -EINVAL;
481 goto finish;
482 }
483
484 if (vol) {
485 for (i = 0; i < vol->channels; i++)
486 value[i] = vol->values[i];
487 }
488
489 finish:
490 pw_thread_loop_unlock(ctl->mainloop);
491
492 return err;
493 }
494
495 static struct spa_pod *build_volume_mute(struct spa_pod_builder *b, struct volume *volume,
496 int *mute, int volume_method)
497 {
498 struct spa_pod_frame f[1];
499
500 spa_pod_builder_push_object(b, &f[0],
501 SPA_TYPE_OBJECT_Props, SPA_PARAM_Props);
502 if (volume) {
503 float volumes[MAX_CHANNELS];
504 uint32_t i, n_volumes = 0;
505
506 n_volumes = volume->channels;
507 for (i = 0; i < n_volumes; i++)
508 volumes[i] = volume_to_linear(volume->values[i], volume_method);
509
510 spa_pod_builder_prop(b, SPA_PROP_channelVolumes, 0);
511 spa_pod_builder_array(b, sizeof(float),
512 SPA_TYPE_Float, n_volumes, volumes);
513 }
514 if (mute) {
515 spa_pod_builder_prop(b, SPA_PROP_mute, 0);
516 spa_pod_builder_bool(b, *mute ? true : false);
517 }
518 return spa_pod_builder_pop(b, &f[0]);
519 }
520
521 static int set_volume_mute(snd_ctl_pipewire_t *ctl, const char *name, struct volume *volume, int *mute)
522 {
523 struct global *g, *dg = NULL;
524 uint32_t id = SPA_ID_INVALID, device_id = SPA_ID_INVALID;
525 char buf[1024];
526 struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf));
527 struct spa_pod_frame f[2];
528 struct spa_pod *param;
529
530 g = find_global(ctl, SPA_ID_INVALID, name, PW_TYPE_INTERFACE_Node);
531 if (g == NULL)
532 return -EINVAL;
533
534 if (SPA_FLAG_IS_SET(g->node.flags, NODE_FLAG_DEVICE_VOLUME) &&
535 (dg = find_global(ctl, g->node.device_id, NULL, PW_TYPE_INTERFACE_Device)) != NULL) {
536 if (g->node.flags & NODE_FLAG_SINK)
537 id = dg->device.active_route_output;
538 else if (g->node.flags & NODE_FLAG_SOURCE)
539 id = dg->device.active_route_input;
540 device_id = g->node.profile_device_id;
541 }
542 pw_log_debug("id %d device_id %d flags:%08x", id, device_id, g->node.flags);
543 if (id != SPA_ID_INVALID && device_id != SPA_ID_INVALID && dg != NULL) {
544 if (!SPA_FLAG_IS_SET(dg->permissions, PW_PERM_W | PW_PERM_X))
545 return -EPERM;
546
547 spa_pod_builder_push_object(&b, &f[0],
548 SPA_TYPE_OBJECT_ParamRoute, SPA_PARAM_Route);
549 spa_pod_builder_add(&b,
550 SPA_PARAM_ROUTE_index, SPA_POD_Int(id),
551 SPA_PARAM_ROUTE_device, SPA_POD_Int(device_id),
552 SPA_PARAM_ROUTE_save, SPA_POD_Bool(true),
553 0);
554
555 spa_pod_builder_prop(&b, SPA_PARAM_ROUTE_props, 0);
556 build_volume_mute(&b, volume, mute, ctl->volume_method);
557 param = spa_pod_builder_pop(&b, &f[0]);
558
559 pw_log_debug("set device %d mute/volume for node %d", dg->id, g->id);
560 pw_device_set_param((struct pw_device*)dg->proxy,
561 SPA_PARAM_Route, 0, param);
562 } else {
563 if (!SPA_FLAG_IS_SET(g->permissions, PW_PERM_W | PW_PERM_X))
564 return -EPERM;
565
566 param = build_volume_mute(&b, volume, mute, ctl->volume_method);
567
568 pw_log_debug("set node %d mute/volume", g->id);
569 pw_node_set_param((struct pw_node*)g->proxy,
570 SPA_PARAM_Props, 0, param);
571 }
572 return 0;
573 }
574
575 static int pipewire_write_integer(snd_ctl_ext_t * ext, snd_ctl_ext_key_t key,
576 long *value)
577 {
578 snd_ctl_pipewire_t *ctl = ext->private_data;
579 int err = 0;
580 uint32_t i;
581 struct volume *vol = NULL;
582
583 assert(ctl);
584
585 if (!ctl->mainloop)
586 return -EBADFD;
587
588 pw_thread_loop_lock(ctl->mainloop);
589
590 err = ctl->error;
591 if (err < 0) {
592 ctl->error = 0;
593 goto finish;
594 }
595
596 err = pipewire_update_volume(ctl);
597 if (err < 0)
598 goto finish;
599
600 switch (key) {
601 case 0:
602 vol = &ctl->source_volume;
603 break;
604 case 1:
605 if (!!ctl->source_muted == !*value)
606 goto finish;
607 ctl->source_muted = !*value;
608 break;
609 case 2:
610 vol = &ctl->sink_volume;
611 break;
612 case 3:
613 if (!!ctl->sink_muted == !*value)
614 goto finish;
615 ctl->sink_muted = !*value;
616 break;
617 default:
618 err = -EINVAL;
619 goto finish;
620 }
621
622 if (vol) {
623 for (i = 0; i < vol->channels; i++)
624 if (value[i] != vol->values[i])
625 break;
626
627 if (i == vol->channels)
628 goto finish;
629
630 for (i = 0; i < vol->channels; i++)
631 vol->values[i] = value[i];
632
633 if (key == 0)
634 err = set_volume_mute(ctl, ctl->default_source, vol, NULL);
635 else
636 err = set_volume_mute(ctl, ctl->default_sink, vol, NULL);
637 } else {
638 if (key == 1)
639 err = set_volume_mute(ctl, ctl->default_source, NULL, &ctl->source_muted);
640 else
641 err = set_volume_mute(ctl, ctl->default_sink, NULL, &ctl->sink_muted);
642 }
643 if (err < 0)
644 goto finish;
645
646 err = wait_resync(ctl);
647
648 if (err < 0)
649 goto finish;
650
651 err = 1;
652
653 finish:
654 pw_thread_loop_unlock(ctl->mainloop);
655
656 return err;
657 }
658
659 static void pipewire_subscribe_events(snd_ctl_ext_t * ext, int subscribe)
660 {
661 snd_ctl_pipewire_t *ctl = ext->private_data;
662
663 assert(ctl);
664
665 if (!ctl->mainloop)
666 return;
667
668 pw_thread_loop_lock(ctl->mainloop);
669
670 ctl->subscribed = !!(subscribe & SND_CTL_EVENT_MASK_VALUE);
671
672 pw_thread_loop_unlock(ctl->mainloop);
673 }
674
675 static int pipewire_read_event(snd_ctl_ext_t * ext, snd_ctl_elem_id_t * id,
676 unsigned int *event_mask)
677 {
678 snd_ctl_pipewire_t *ctl = ext->private_data;
679 int offset;
680 int err;
681
682 assert(ctl);
683
684 if (!ctl->mainloop)
685 return -EBADFD;
686
687 pw_thread_loop_lock(ctl->mainloop);
688
689 err = ctl->error;
690 if (err < 0) {
691 ctl->error = 0;
692 goto finish;
693 }
694
695 if (!ctl->updated || !ctl->subscribed) {
696 err = -EAGAIN;
697 goto finish;
698 }
699
700 if (ctl->default_source[0] != '\0')
701 offset = 2;
702 else
703 offset = 0;
704
705 if (ctl->updated & UPDATE_SOURCE_VOL) {
706 pipewire_elem_list(ext, 0, id);
707 ctl->updated &= ~UPDATE_SOURCE_VOL;
708 } else if (ctl->updated & UPDATE_SOURCE_MUTE) {
709 pipewire_elem_list(ext, 1, id);
710 ctl->updated &= ~UPDATE_SOURCE_MUTE;
711 } else if (ctl->updated & UPDATE_SINK_VOL) {
712 pipewire_elem_list(ext, offset + 0, id);
713 ctl->updated &= ~UPDATE_SINK_VOL;
714 } else if (ctl->updated & UPDATE_SINK_MUTE) {
715 pipewire_elem_list(ext, offset + 1, id);
716 ctl->updated &= ~UPDATE_SINK_MUTE;
717 }
718
719 *event_mask = SND_CTL_EVENT_MASK_VALUE;
720
721 err = 1;
722
723 finish:
724 if (!ctl->updated)
725 poll_deactivate(ctl);
726
727 pw_thread_loop_unlock(ctl->mainloop);
728
729 return err;
730 }
731
732 static int pipewire_ctl_poll_revents(snd_ctl_ext_t * ext, struct pollfd *pfd,
733 unsigned int nfds,
734 unsigned short *revents)
735 {
736 snd_ctl_pipewire_t *ctl = ext->private_data;
737 int err = 0;
738
739 assert(ctl);
740
741 if (!ctl->mainloop)
742 return -EBADFD;
743
744 pw_thread_loop_lock(ctl->mainloop);
745
746 err = ctl->error;
747 if (err < 0) {
748 ctl->error = 0;
749 goto finish;
750 }
751
752 if (ctl->updated)
753 *revents = POLLIN;
754 else
755 *revents = 0;
756
757 err = 0;
758
759 finish:
760 pw_thread_loop_unlock(ctl->mainloop);
761
762 return err;
763 }
764
765 static void snd_ctl_pipewire_free(snd_ctl_pipewire_t *ctl)
766 {
767 if (ctl == NULL)
768 return;
769
770 pw_log_debug("%p:", ctl);
771 if (ctl->mainloop)
772 pw_thread_loop_stop(ctl->mainloop);
773 if (ctl->registry)
774 pw_proxy_destroy((struct pw_proxy*)ctl->registry);
775 if (ctl->context)
776 pw_context_destroy(ctl->context);
777 if (ctl->fd >= 0)
778 spa_system_close(ctl->system, ctl->fd);
779 if (ctl->mainloop)
780 pw_thread_loop_destroy(ctl->mainloop);
781 pw_properties_free(ctl->props);
782 free(ctl);
783 }
784
785 static void pipewire_close(snd_ctl_ext_t * ext)
786 {
787 snd_ctl_pipewire_t *ctl = ext->private_data;
788 snd_ctl_pipewire_free(ctl);
789 }
790
791 static const snd_ctl_ext_callback_t pipewire_ext_callback = {
792 .elem_count = pipewire_elem_count,
793 .elem_list = pipewire_elem_list,
794 .find_elem = pipewire_find_elem,
795 .get_attribute = pipewire_get_attribute,
796 .get_integer_info = pipewire_get_integer_info,
797 .read_integer = pipewire_read_integer,
798 .write_integer = pipewire_write_integer,
799 .subscribe_events = pipewire_subscribe_events,
800 .read_event = pipewire_read_event,
801 .poll_revents = pipewire_ctl_poll_revents,
802 .close = pipewire_close,
803 };
804
805 /** device */
806 static void device_event_info(void *data, const struct pw_device_info *info)
807 {
808 struct global *g = data;
809 snd_ctl_pipewire_t *ctl = g->ctl;
810 uint32_t n;
811
812 pw_log_debug("info");
813
814 if (info->change_mask & PW_DEVICE_CHANGE_MASK_PARAMS) {
815 for (n = 0; n < info->n_params; n++) {
816 if (!(info->params[n].flags & SPA_PARAM_INFO_READ))
817 continue;
818
819 switch (info->params[n].id) {
820 case SPA_PARAM_Route:
821 pw_device_enum_params((struct pw_device*)g->proxy,
822 0, info->params[n].id, 0, -1, NULL);
823 break;
824 default:
825 break;
826 }
827 }
828
829 }
830 do_resync(ctl);
831 }
832
833 static void parse_props(struct global *g, const struct spa_pod *param, bool device)
834 {
835 struct spa_pod_prop *prop;
836 struct spa_pod_object *obj = (struct spa_pod_object *) param;
837 snd_ctl_pipewire_t *ctl = g->ctl;
838
839 SPA_POD_OBJECT_FOREACH(obj, prop) {
840 switch (prop->key) {
841 case SPA_PROP_volume:
842 if (spa_pod_get_float(&prop->value, &g->node.volume) < 0)
843 continue;
844 pw_log_debug("update node %d volume", g->id);
845 SPA_FLAG_UPDATE(g->node.flags, NODE_FLAG_DEVICE_VOLUME, device);
846 break;
847 case SPA_PROP_mute:
848 if (spa_pod_get_bool(&prop->value, &g->node.mute) < 0)
849 continue;
850 SPA_FLAG_UPDATE(g->node.flags, NODE_FLAG_DEVICE_MUTE, device);
851 pw_log_debug("update node %d mute", g->id);
852 break;
853 case SPA_PROP_channelVolumes:
854 {
855 float volumes[MAX_CHANNELS];
856 uint32_t n_volumes, i;
857
858 n_volumes = spa_pod_copy_array(&prop->value, SPA_TYPE_Float,
859 volumes, SPA_N_ELEMENTS(volumes));
860
861 g->node.channel_volume.channels = n_volumes;
862 for (i = 0; i < n_volumes; i++)
863 g->node.channel_volume.values[i] =
864 volume_from_linear(volumes[i], ctl->volume_method);
865
866 SPA_FLAG_UPDATE(g->node.flags, NODE_FLAG_DEVICE_VOLUME, device);
867 pw_log_debug("update node %d channelVolumes", g->id);
868 break;
869 }
870 default:
871 break;
872 }
873 }
874 }
875
876 static struct global *find_node_for_route(snd_ctl_pipewire_t *ctl, uint32_t card, uint32_t device)
877 {
878 struct global *n;
879 spa_list_for_each(n, &ctl->globals, link) {
880 if (spa_streq(n->ginfo->type, PW_TYPE_INTERFACE_Node) &&
881 (n->node.device_id == card) &&
882 (n->node.profile_device_id == device))
883 return n;
884 }
885 return NULL;
886 }
887
888 static void device_event_param(void *data, int seq,
889 uint32_t id, uint32_t index, uint32_t next,
890 const struct spa_pod *param)
891 {
892 struct global *g = data;
893 snd_ctl_pipewire_t *ctl = g->ctl;
894
895 pw_log_debug("param %d", id);
896
897 switch (id) {
898 case SPA_PARAM_Route:
899 {
900 uint32_t idx, device;
901 enum spa_direction direction;
902 struct spa_pod *props = NULL;
903 struct global *ng;
904
905 if (spa_pod_parse_object(param,
906 SPA_TYPE_OBJECT_ParamRoute, NULL,
907 SPA_PARAM_ROUTE_index, SPA_POD_Int(&idx),
908 SPA_PARAM_ROUTE_direction, SPA_POD_Id(&direction),
909 SPA_PARAM_ROUTE_device, SPA_POD_Int(&device),
910 SPA_PARAM_ROUTE_props, SPA_POD_OPT_Pod(&props)) < 0) {
911 pw_log_warn("device %d: can't parse route", g->id);
912 return;
913 }
914 if (direction == SPA_DIRECTION_OUTPUT)
915 g->device.active_route_output = idx;
916 else
917 g->device.active_route_input = idx;
918
919 pw_log_debug("device %d: active %s route %d", g->id,
920 direction == SPA_DIRECTION_OUTPUT ? "output" : "input",
921 idx);
922
923 ng = find_node_for_route(ctl, g->id, device);
924 if (props && ng)
925 parse_props(ng, props, true);
926 break;
927 }
928 default:
929 break;
930 }
931 }
932
933 static const struct pw_device_events device_events = {
934 PW_VERSION_DEVICE_EVENTS,
935 .info = device_event_info,
936 .param = device_event_param,
937 };
938
939 static const struct global_info device_info = {
940 .type = PW_TYPE_INTERFACE_Device,
941 .version = PW_VERSION_DEVICE,
942 .events = &device_events,
943 };
944
945 /** node */
946 static void node_event_info(void *data, const struct pw_node_info *info)
947 {
948 struct global *g = data;
949 snd_ctl_pipewire_t *ctl = g->ctl;
950 const char *str;
951 uint32_t i;
952
953 pw_log_debug("update %d %"PRIu64, g->id, info->change_mask);
954
955 if (info->change_mask & PW_NODE_CHANGE_MASK_PROPS && info->props) {
956 if ((str = spa_dict_lookup(info->props, "card.profile.device")))
957 g->node.profile_device_id = atoi(str);
958 else
959 g->node.profile_device_id = SPA_ID_INVALID;
960
961 if ((str = spa_dict_lookup(info->props, PW_KEY_DEVICE_ID)))
962 g->node.device_id = atoi(str);
963 else
964 g->node.device_id = SPA_ID_INVALID;
965
966 if ((str = spa_dict_lookup(info->props, PW_KEY_PRIORITY_SESSION)))
967 g->node.priority = atoi(str);
968 if ((str = spa_dict_lookup(info->props, PW_KEY_MEDIA_CLASS))) {
969 if (spa_streq(str, "Audio/Sink"))
970 g->node.flags |= NODE_FLAG_SINK;
971 else if (spa_streq(str, "Audio/Source"))
972 g->node.flags |= NODE_FLAG_SOURCE;
973 }
974 }
975 if (info->change_mask & PW_NODE_CHANGE_MASK_PARAMS) {
976 for (i = 0; i < info->n_params; i++) {
977 if (!(info->params[i].flags & SPA_PARAM_INFO_READ))
978 continue;
979
980 switch (info->params[i].id) {
981 case SPA_PARAM_Props:
982 pw_node_enum_params((struct pw_node*)g->proxy,
983 0, info->params[i].id, 0, -1, NULL);
984 break;
985 default:
986 break;
987 }
988 }
989 }
990 do_resync(ctl);
991 }
992
993
994 static void node_event_param(void *data, int seq,
995 uint32_t id, uint32_t index, uint32_t next,
996 const struct spa_pod *param)
997 {
998 struct global *g = data;
999 pw_log_debug("update param %d %d", g->id, id);
1000
1001 switch (id) {
1002 case SPA_PARAM_Props:
1003 if (!SPA_FLAG_IS_SET(g->node.flags, NODE_FLAG_DEVICE_VOLUME | NODE_FLAG_DEVICE_MUTE))
1004 parse_props(g, param, false);
1005 break;
1006 default:
1007 break;
1008 }
1009 }
1010
1011 static const struct pw_node_events node_events = {
1012 PW_VERSION_NODE_EVENTS,
1013 .info = node_event_info,
1014 .param = node_event_param,
1015 };
1016
1017 static const struct global_info node_info = {
1018 .type = PW_TYPE_INTERFACE_Node,
1019 .version = PW_VERSION_NODE,
1020 .events = &node_events,
1021 };
1022
1023 /** metadata */
1024 static int metadata_property(void *data,
1025 uint32_t subject,
1026 const char *key,
1027 const char *type,
1028 const char *value)
1029 {
1030 struct global *g = data;
1031 snd_ctl_pipewire_t *ctl = g->ctl;
1032
1033 if (subject == PW_ID_CORE) {
1034 if (key == NULL || spa_streq(key, "default.audio.sink")) {
1035 if (value == NULL ||
1036 spa_json_str_object_find(value, strlen(value), "name",
1037 ctl->default_sink, sizeof(ctl->default_sink)) < 0)
1038 ctl->default_sink[0] = '\0';
1039 pw_log_debug("found default sink: %s", ctl->default_sink);
1040 }
1041 if (key == NULL || spa_streq(key, "default.audio.source")) {
1042 if (value == NULL ||
1043 spa_json_str_object_find(value, strlen(value), "name",
1044 ctl->default_source, sizeof(ctl->default_source)) < 0)
1045 ctl->default_source[0] = '\0';
1046 pw_log_debug("found default source: %s", ctl->default_source);
1047 }
1048 }
1049 return 0;
1050 }
1051
1052 static int metadata_init(struct global *g)
1053 {
1054 snd_ctl_pipewire_t *ctl = g->ctl;
1055 ctl->metadata = (struct pw_metadata*)g->proxy;
1056 return 0;
1057 }
1058
1059 static const struct pw_metadata_events metadata_events = {
1060 PW_VERSION_METADATA_EVENTS,
1061 .property = metadata_property,
1062 };
1063
1064 static const struct global_info metadata_info = {
1065 .type = PW_TYPE_INTERFACE_Metadata,
1066 .version = PW_VERSION_METADATA,
1067 .events = &metadata_events,
1068 .init = metadata_init
1069 };
1070
1071 /** proxy */
1072 static void proxy_removed(void *data)
1073 {
1074 struct global *g = data;
1075 pw_proxy_destroy(g->proxy);
1076 }
1077
1078 static void proxy_destroy(void *data)
1079 {
1080 struct global *g = data;
1081 spa_list_remove(&g->link);
1082 g->proxy = NULL;
1083 pw_properties_free(g->props);
1084 }
1085
1086 static const struct pw_proxy_events proxy_events = {
1087 PW_VERSION_PROXY_EVENTS,
1088 .removed = proxy_removed,
1089 .destroy = proxy_destroy
1090 };
1091
1092 static void registry_event_global(void *data, uint32_t id,
1093 uint32_t permissions, const char *type, uint32_t version,
1094 const struct spa_dict *props)
1095 {
1096 snd_ctl_pipewire_t *ctl = data;
1097 const struct global_info *info = NULL;
1098 struct pw_proxy *proxy;
1099 const char *str;
1100
1101 pw_log_debug("got %d %s", id, type);
1102
1103 if (spa_streq(type, PW_TYPE_INTERFACE_Device)) {
1104 if (props == NULL ||
1105 ((str = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS)) == NULL) ||
1106 (!spa_streq(str, "Audio/Device")))
1107 return;
1108
1109 pw_log_debug("found device %d", id);
1110 info = &device_info;
1111 } else if (spa_streq(type, PW_TYPE_INTERFACE_Node)) {
1112 if (props == NULL ||
1113 ((str = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS)) == NULL) ||
1114 ((!spa_streq(str, "Audio/Sink")) &&
1115 (!spa_streq(str, "Audio/Source"))))
1116 return;
1117
1118 pw_log_debug("found node %d type:%s", id, str);
1119 info = &node_info;
1120 } else if (spa_streq(type, PW_TYPE_INTERFACE_Metadata)) {
1121 if (props == NULL ||
1122 ((str = spa_dict_lookup(props, PW_KEY_METADATA_NAME)) == NULL) ||
1123 (!spa_streq(str, "default")))
1124 return;
1125 if (ctl->metadata != NULL)
1126 return;
1127 info = &metadata_info;
1128 }
1129 if (info) {
1130 struct global *g;
1131
1132 proxy = pw_registry_bind(ctl->registry,
1133 id, info->type, info->version,
1134 sizeof(struct global));
1135
1136 g = pw_proxy_get_user_data(proxy);
1137 g->ctl = ctl;
1138 g->ginfo = info;
1139 g->id = id;
1140 g->permissions = permissions;
1141 g->props = props ? pw_properties_new_dict(props) : NULL;
1142 g->proxy = proxy;
1143 spa_list_append(&ctl->globals, &g->link);
1144
1145 pw_proxy_add_listener(proxy,
1146 &g->proxy_listener,
1147 &proxy_events, g);
1148
1149 if (info->events) {
1150 pw_proxy_add_object_listener(proxy,
1151 &g->object_listener,
1152 info->events, g);
1153 }
1154 if (info->init)
1155 info->init(g);
1156
1157 do_resync(ctl);
1158 }
1159 }
1160
1161 static void registry_event_global_remove(void *data, uint32_t id)
1162 {
1163 snd_ctl_pipewire_t *ctl = data;
1164 struct global *g;
1165 const char *name;
1166
1167 if ((g = find_global(ctl, id, NULL, NULL)) == NULL)
1168 return;
1169
1170 if (spa_streq(g->ginfo->type, PW_TYPE_INTERFACE_Node)) {
1171 if ((name = pw_properties_get(g->props, PW_KEY_NODE_NAME)) == NULL)
1172 return;
1173
1174 if (spa_streq(name, ctl->default_sink))
1175 ctl->default_sink[0] = '\0';
1176 if (spa_streq(name, ctl->default_source))
1177 ctl->default_source[0] = '\0';
1178 }
1179 pw_proxy_destroy(g->proxy);
1180 }
1181
1182 static const struct pw_registry_events registry_events = {
1183 PW_VERSION_REGISTRY_EVENTS,
1184 .global = registry_event_global,
1185 .global_remove = registry_event_global_remove,
1186 };
1187
1188 static void on_core_error(void *data, uint32_t id, int seq, int res, const char *message)
1189 {
1190 snd_ctl_pipewire_t *ctl = data;
1191
1192 pw_log_warn("%p: error id:%u seq:%d res:%d (%s): %s", ctl,
1193 id, seq, res, spa_strerror(res), message);
1194
1195 if (id == PW_ID_CORE) {
1196 switch (res) {
1197 case -ENOENT:
1198 break;
1199 default:
1200 ctl->error = res;
1201 if (ctl->fd != -1)
1202 poll_activate(ctl);
1203 }
1204 }
1205 pw_thread_loop_signal(ctl->mainloop, false);
1206 }
1207
1208 static void on_core_done(void *data, uint32_t id, int seq)
1209 {
1210 snd_ctl_pipewire_t *ctl = data;
1211
1212 pw_log_debug("done %d %d %d", id, seq, ctl->pending_seq);
1213
1214 if (id != PW_ID_CORE)
1215 return;
1216
1217 ctl->last_seq = seq;
1218 if (seq == ctl->pending_seq) {
1219 pipewire_update_volume(ctl);
1220 pw_thread_loop_signal(ctl->mainloop, false);
1221 }
1222 }
1223
1224 static const struct pw_core_events core_events = {
1225 PW_VERSION_CORE_EVENTS,
1226 .error = on_core_error,
1227 .done = on_core_done,
1228 };
1229
1230 static int execute_match(void *data, const char *location, const char *action,
1231 const char *val, size_t len)
1232 {
1233 snd_ctl_pipewire_t *ctl = data;
1234 if (spa_streq(action, "update-props"))
1235 pw_properties_update_string(ctl->props, val, len);
1236 return 1;
1237 }
1238
1239 SPA_EXPORT
1240 SND_CTL_PLUGIN_DEFINE_FUNC(pipewire)
1241 {
1242 snd_config_iterator_t i, next;
1243 const char *server = NULL;
1244 const char *device = NULL;
1245 const char *source = NULL;
1246 const char *sink = NULL;
1247 const char *fallback_name = NULL;
1248 int err;
1249 const char *str;
1250 snd_ctl_pipewire_t *ctl;
1251 struct pw_loop *loop;
1252
1253 pw_init(NULL, NULL);
1254
1255 PW_LOG_TOPIC_INIT(alsa_log_topic);
1256
1257 snd_config_for_each(i, next, conf) {
1258 snd_config_t *n = snd_config_iterator_entry(i);
1259 const char *id;
1260 if (snd_config_get_id(n, &id) < 0)
1261 continue;
1262 if (spa_streq(id, "comment") || spa_streq(id, "type")
1263 || spa_streq(id, "hint"))
1264 continue;
1265 if (spa_streq(id, "server")) {
1266 if (snd_config_get_string(n, &server) < 0) {
1267 SNDERR("Invalid type for %s", id);
1268 return -EINVAL;
1269 } else if (!*server) {
1270 server = NULL;
1271 }
1272 continue;
1273 }
1274 if (spa_streq(id, "device")) {
1275 if (snd_config_get_string(n, &device) < 0) {
1276 SNDERR("Invalid type for %s", id);
1277 return -EINVAL;
1278 } else if (!*device) {
1279 device = NULL;
1280 }
1281 continue;
1282 }
1283 if (spa_streq(id, "source")) {
1284 if (snd_config_get_string(n, &source) < 0) {
1285 SNDERR("Invalid type for %s", id);
1286 return -EINVAL;
1287 } else if (!*source) {
1288 source = NULL;
1289 }
1290 continue;
1291 }
1292 if (spa_streq(id, "sink")) {
1293 if (snd_config_get_string(n, &sink) < 0) {
1294 SNDERR("Invalid type for %s", id);
1295 return -EINVAL;
1296 } else if (!*sink) {
1297 sink = NULL;
1298 }
1299 continue;
1300 }
1301 if (spa_streq(id, "fallback")) {
1302 if (snd_config_get_string(n, &fallback_name) < 0) {
1303 SNDERR("Invalid value for %s", id);
1304 return -EINVAL;
1305 }
1306 continue;
1307 }
1308 SNDERR("Unknown field %s", id);
1309 return -EINVAL;
1310 }
1311
1312 if (fallback_name && name && spa_streq(name, fallback_name))
1313 fallback_name = NULL; /* no fallback for the same name */
1314
1315 ctl = calloc(1, sizeof(*ctl));
1316 if (!ctl)
1317 return -ENOMEM;
1318
1319 spa_list_init(&ctl->globals);
1320
1321 if (source == NULL)
1322 source = device;
1323 if (source != NULL)
1324 snprintf(ctl->default_source, sizeof(ctl->default_source),
1325 "%s", source);
1326 if (sink == NULL)
1327 sink = device;
1328 if (sink != NULL)
1329 snprintf(ctl->default_sink, sizeof(ctl->default_sink),
1330 "%s", sink);
1331
1332 ctl->mainloop = pw_thread_loop_new("alsa-pipewire", NULL);
1333 if (ctl->mainloop == NULL) {
1334 err = -errno;
1335 goto error;
1336 }
1337 loop = pw_thread_loop_get_loop(ctl->mainloop);
1338
1339 ctl->system = loop->system;
1340 ctl->fd = spa_system_eventfd_create(ctl->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
1341 if (ctl->fd < 0) {
1342 err = ctl->fd;
1343 goto error;
1344 }
1345
1346 ctl->context = pw_context_new(loop,
1347 pw_properties_new(
1348 PW_KEY_CLIENT_API, "alsa",
1349 NULL),
1350 0);
1351 if (ctl->context == NULL) {
1352 err = -errno;
1353 goto error;
1354 }
1355
1356 ctl->props = pw_properties_new(NULL, NULL);
1357 if (ctl->props == NULL) {
1358 err = -errno;
1359 goto error;
1360 }
1361
1362 if (server)
1363 pw_properties_set(ctl->props, PW_KEY_REMOTE_NAME, server);
1364
1365 pw_context_conf_update_props(ctl->context, "alsa.properties", ctl->props);
1366
1367 pw_context_conf_section_match_rules(ctl->context, "alsa.rules",
1368 &pw_context_get_properties(ctl->context)->dict,
1369 execute_match, ctl);
1370
1371 if (pw_properties_get(ctl->props, PW_KEY_APP_NAME) == NULL)
1372 pw_properties_setf(ctl->props, PW_KEY_APP_NAME, "PipeWire ALSA [%s]",
1373 pw_get_prgname());
1374
1375 str = getenv("PIPEWIRE_ALSA");
1376 if (str != NULL)
1377 pw_properties_update_string(ctl->props, str, strlen(str));
1378
1379 if ((str = pw_properties_get(ctl->props, "alsa.volume-method")) == NULL)
1380 str = DEFAULT_VOLUME_METHOD;
1381
1382 if (spa_streq(str, "cubic"))
1383 ctl->volume_method = VOLUME_METHOD_CUBIC;
1384 else if (spa_streq(str, "linear"))
1385 ctl->volume_method = VOLUME_METHOD_LINEAR;
1386 else {
1387 ctl->volume_method = VOLUME_METHOD_CUBIC;
1388 SNDERR("unknown alsa.volume-method %s, using cubic", str);
1389 }
1390
1391 if ((err = pw_thread_loop_start(ctl->mainloop)) < 0)
1392 goto error;
1393
1394 pw_thread_loop_lock(ctl->mainloop);
1395 ctl->core = pw_context_connect(ctl->context, pw_properties_copy(ctl->props), 0);
1396 if (ctl->core == NULL) {
1397 err = -errno;
1398 goto error_unlock;
1399 }
1400 pw_core_add_listener(ctl->core,
1401 &ctl->core_listener,
1402 &core_events, ctl);
1403
1404 ctl->registry = pw_core_get_registry(ctl->core, PW_VERSION_REGISTRY, 0);
1405 if (ctl->registry == NULL) {
1406 err = -errno;
1407 goto error_unlock;
1408 }
1409
1410 pw_registry_add_listener(ctl->registry,
1411 &ctl->registry_listener,
1412 &registry_events, ctl);
1413
1414 err = wait_resync(ctl);
1415 if (err < 0)
1416 goto error_unlock;
1417
1418 pw_thread_loop_unlock(ctl->mainloop);
1419
1420 ctl->ext.version = SND_CTL_EXT_VERSION;
1421 ctl->ext.card_idx = 0;
1422 strncpy(ctl->ext.id, "pipewire", sizeof(ctl->ext.id) - 1);
1423 strncpy(ctl->ext.driver, "PW plugin", sizeof(ctl->ext.driver) - 1);
1424 strncpy(ctl->ext.name, "PipeWire", sizeof(ctl->ext.name) - 1);
1425 strncpy(ctl->ext.longname, "PipeWire", sizeof(ctl->ext.longname) - 1);
1426 strncpy(ctl->ext.mixername, "PipeWire", sizeof(ctl->ext.mixername) - 1);
1427 ctl->ext.poll_fd = ctl->fd;
1428
1429 ctl->ext.callback = &pipewire_ext_callback;
1430 ctl->ext.private_data = ctl;
1431
1432 err = snd_ctl_ext_create(&ctl->ext, name, mode);
1433 if (err < 0)
1434 goto error;
1435
1436 *handlep = ctl->ext.handle;
1437
1438 return 0;
1439
1440 error_unlock:
1441 pw_thread_loop_unlock(ctl->mainloop);
1442 error:
1443 snd_ctl_pipewire_free(ctl);
1444 pw_log_error("error %d (%s)", err, spa_strerror(err));
1445
1446 if (fallback_name)
1447 return snd_ctl_open_fallback(handlep, root,
1448 fallback_name, name, mode);
1449
1450 return err;
1451 }
1452
1453 SPA_EXPORT
1454 SND_CTL_PLUGIN_SYMBOL(pipewire);