1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
|
/* PipeWire
*
* Copyright © 2019 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <time.h>
#include "config.h"
#include <alsa/asoundlib.h>
#include <spa/monitor/device.h>
#include <spa/monitor/event.h>
#include <spa/node/node.h>
#include <spa/node/keys.h>
#include <spa/utils/result.h>
#include <spa/utils/hook.h>
#include <spa/utils/names.h>
#include <spa/utils/keys.h>
#include <spa/param/props.h>
#include <spa/pod/builder.h>
#include <spa/pod/parser.h>
#include <spa/debug/dict.h>
#include <spa/debug/pod.h>
#include <spa/support/dbus.h>
#include <pipewire/pipewire.h>
#include <pipewire/main-loop.h>
#include <extensions/session-manager.h>
#include "media-session.h"
#include "reserve.c"
#define DEFAULT_JACK_SECONDS 1
struct node {
struct impl *impl;
enum pw_direction direction;
struct device *device;
struct spa_list link;
uint32_t id;
struct pw_properties *props;
struct spa_node *node;
struct sm_node *snode;
unsigned int acquired:1;
};
struct device {
struct impl *impl;
struct spa_list link;
uint32_t id;
uint32_t device_id;
struct rd_device *reserve;
struct spa_hook sync_listener;
int seq;
int priority;
int profile;
int pending_profile;
struct pw_properties *props;
struct spa_handle *handle;
struct spa_device *device;
struct spa_hook device_listener;
struct sm_device *sdevice;
struct spa_hook listener;
uint32_t n_acquired;
unsigned int first:1;
unsigned int appeared:1;
struct spa_list node_list;
};
struct impl {
struct sm_media_session *session;
struct spa_hook session_listener;
DBusConnection *conn;
struct spa_handle *handle;
struct spa_device *monitor;
struct spa_hook listener;
struct spa_list device_list;
struct spa_source *jack_timeout;
struct pw_proxy *jack_device;
unsigned int use_acp:1;
};
#undef NAME
#define NAME "alsa-monitor"
static struct node *alsa_find_node(struct device *device, uint32_t id)
{
struct node *node;
spa_list_for_each(node, &device->node_list, link) {
if (node->id == id)
return node;
}
return NULL;
}
static void alsa_update_node(struct device *device, struct node *node,
const struct spa_device_object_info *info)
{
pw_log_debug("update node %u", node->id);
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
spa_debug_dict(0, info->props);
pw_properties_update(node->props, info->props);
}
static int node_acquire(void *data)
{
struct node *node = data;
struct device *device = node->device;
pw_log_debug("acquire %u", node->id);
if (node->acquired)
return 0;
node->acquired = true;
if (device && device->n_acquired++ == 0 && device->reserve)
rd_device_acquire(device->reserve);
return 0;
}
static int node_release(void *data)
{
struct node *node = data;
struct device *device = node->device;
pw_log_debug("release %u", node->id);
if (!node->acquired)
return 0;
node->acquired = false;
if (device && --device->n_acquired == 0 && device->reserve)
rd_device_release(device->reserve);
return 0;
}
static const struct sm_object_methods node_methods = {
SM_VERSION_OBJECT_METHODS,
.acquire = node_acquire,
.release = node_release,
};
static struct node *alsa_create_node(struct device *device, uint32_t id,
const struct spa_device_object_info *info)
{
struct node *node;
struct impl *impl = device->impl;
int res;
const char *dev, *subdev, *stream, *profile, *profile_desc;
int priority;
pw_log_debug("new node %u", id);
if (strcmp(info->type, SPA_TYPE_INTERFACE_Node) != 0) {
errno = EINVAL;
return NULL;
}
node = calloc(1, sizeof(*node));
if (node == NULL) {
res = -errno;
goto exit;
}
node->props = pw_properties_new_dict(info->props);
pw_properties_setf(node->props, PW_KEY_DEVICE_ID, "%d", device->device_id);
pw_properties_set(node->props, PW_KEY_FACTORY_NAME, info->factory_name);
if ((dev = pw_properties_get(node->props, SPA_KEY_API_ALSA_PCM_DEVICE)) == NULL)
if ((dev = pw_properties_get(node->props, "alsa.device")) == NULL)
dev = "0";
if ((subdev = pw_properties_get(node->props, SPA_KEY_API_ALSA_PCM_SUBDEVICE)) == NULL)
if ((subdev = pw_properties_get(node->props, "alsa.subdevice")) == NULL)
subdev = "0";
if ((stream = pw_properties_get(node->props, SPA_KEY_API_ALSA_PCM_STREAM)) == NULL)
stream = "unknown";
if ((profile = pw_properties_get(node->props, "device.profile.name")) == NULL)
profile = "unknown";
profile_desc = pw_properties_get(node->props, "device.profile.description");
if (!strcmp(stream, "capture"))
node->direction = PW_DIRECTION_OUTPUT;
else
node->direction = PW_DIRECTION_INPUT;
if (device->first) {
if (atol(dev) != 0)
device->priority -= 256;
device->first = false;
}
priority = device->priority;
if (!strcmp(stream, "capture"))
priority += 1000;
priority -= atol(dev) * 16;
priority -= atol(subdev);
if (strstr(profile, "analog-") == profile)
priority += 9;
else if (strstr(profile, "iec958-") == profile)
priority += 8;
if (pw_properties_get(node->props, PW_KEY_PRIORITY_MASTER) == NULL) {
pw_properties_setf(node->props, PW_KEY_PRIORITY_MASTER, "%d", priority);
pw_properties_setf(node->props, PW_KEY_PRIORITY_SESSION, "%d", priority);
}
if (pw_properties_get(node->props, SPA_KEY_MEDIA_CLASS) == NULL) {
if (node->direction == PW_DIRECTION_OUTPUT)
pw_properties_setf(node->props, SPA_KEY_MEDIA_CLASS, "Audio/Source");
else
pw_properties_setf(node->props, SPA_KEY_MEDIA_CLASS, "Audio/Sink");
}
if (pw_properties_get(node->props, SPA_KEY_NODE_NAME) == NULL) {
const char *devname;
if ((devname = pw_properties_get(device->props, SPA_KEY_DEVICE_NAME)) == NULL)
devname = "unknown";
pw_properties_setf(node->props, SPA_KEY_NODE_NAME, "%s.%s.%s.%s",
devname, stream, dev, subdev);
}
if (pw_properties_get(node->props, PW_KEY_NODE_DESCRIPTION) == NULL) {
const char *desc, *name = NULL;
if ((desc = pw_properties_get(device->props, SPA_KEY_DEVICE_DESCRIPTION)) == NULL)
desc = "unknown";
name = pw_properties_get(node->props, SPA_KEY_API_ALSA_PCM_NAME);
if (name == NULL)
name = pw_properties_get(node->props, SPA_KEY_API_ALSA_PCM_ID);
if (name == NULL)
name = dev;
if (profile_desc != NULL) {
pw_properties_setf(node->props, PW_KEY_NODE_DESCRIPTION, "%s %s",
desc, profile_desc);
} else if (strcmp(subdev, "0")) {
pw_properties_setf(node->props, PW_KEY_NODE_DESCRIPTION, "%s (%s %s)",
desc, name, subdev);
} else if (strcmp(dev, "0")) {
pw_properties_setf(node->props, PW_KEY_NODE_DESCRIPTION, "%s (%s)",
desc, name);
} else {
pw_properties_setf(node->props, PW_KEY_NODE_DESCRIPTION, "%s",
desc);
}
}
node->impl = impl;
node->device = device;
node->id = id;
node->snode = sm_media_session_create_node(impl->session,
"adapter",
&node->props->dict);
if (node->snode == NULL) {
res = -errno;
goto clean_node;
}
node->snode->obj.methods = SPA_CALLBACKS_INIT(&node_methods, node);
spa_list_append(&device->node_list, &node->link);
return node;
clean_node:
pw_properties_free(node->props);
free(node);
exit:
errno = -res;
return NULL;
}
static void alsa_remove_node(struct device *device, struct node *node)
{
pw_log_debug("remove node %u", node->id);
spa_list_remove(&node->link);
sm_object_destroy(&node->snode->obj);
pw_properties_free(node->props);
free(node);
}
static void alsa_device_info(void *data, const struct spa_device_info *info)
{
struct device *device = data;
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
spa_debug_dict(0, info->props);
pw_properties_update(device->props, info->props);
}
static void alsa_device_object_info(void *data, uint32_t id,
const struct spa_device_object_info *info)
{
struct device *device = data;
struct node *node;
node = alsa_find_node(device, id);
if (info == NULL) {
if (node == NULL) {
pw_log_warn("device %p: unknown node %u", device, id);
return;
}
alsa_remove_node(device, node);
} else if (node == NULL) {
alsa_create_node(device, id, info);
} else {
alsa_update_node(device, node, info);
}
}
static void alsa_device_event(void *data, const struct spa_event *event)
{
struct device *device = data;
struct node *node;
uint32_t id, type;
struct spa_pod *props = NULL;
if (spa_pod_parse_object(&event->pod,
SPA_TYPE_EVENT_Device, &type,
SPA_EVENT_DEVICE_Object, SPA_POD_Int(&id),
SPA_EVENT_DEVICE_Props, SPA_POD_OPT_Pod(&props)) < 0)
return;
if ((node = alsa_find_node(device, id)) == NULL)
return;
switch (type) {
case SPA_DEVICE_EVENT_ObjectConfig:
if (props)
pw_node_set_param((struct pw_node*)node->snode->obj.proxy,
SPA_PARAM_Props, 0, props);
break;
default:
break;
}
}
static const struct spa_device_events alsa_device_events = {
SPA_VERSION_DEVICE_EVENTS,
.info = alsa_device_info,
.object_info = alsa_device_object_info,
.event = alsa_device_event,
};
static struct device *alsa_find_device(struct impl *impl, uint32_t id)
{
struct device *device;
spa_list_for_each(device, &impl->device_list, link) {
if (device->id == id)
return device;
}
return NULL;
}
static void alsa_update_device(struct impl *impl, struct device *device,
const struct spa_device_object_info *info)
{
pw_log_debug("update device %u", device->id);
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
spa_debug_dict(0, info->props);
pw_properties_update(device->props, info->props);
}
static int update_device_props(struct device *device)
{
struct pw_properties *p = device->props;
const char *s, *d;
char temp[32];
if ((s = pw_properties_get(p, SPA_KEY_DEVICE_NAME)) == NULL) {
if ((s = pw_properties_get(p, SPA_KEY_DEVICE_BUS_ID)) == NULL) {
if ((s = pw_properties_get(p, SPA_KEY_DEVICE_BUS_PATH)) == NULL) {
snprintf(temp, sizeof(temp), "%d", device->id);
s = temp;
}
}
}
pw_properties_setf(p, PW_KEY_DEVICE_NAME, "alsa_card.%s", s);
if (pw_properties_get(p, PW_KEY_DEVICE_DESCRIPTION) == NULL) {
d = NULL;
if ((s = pw_properties_get(p, PW_KEY_DEVICE_FORM_FACTOR)))
if (strcmp(s, "internal") == 0)
d = "Built-in Audio";
if (!d)
if ((s = pw_properties_get(p, PW_KEY_DEVICE_CLASS)))
if (strcmp(s, "modem") == 0)
d = "Modem";
if (!d)
d = pw_properties_get(p, PW_KEY_DEVICE_PRODUCT_NAME);
if (!d)
d = "Unknown device";
pw_properties_set(p, PW_KEY_DEVICE_DESCRIPTION, d);
}
if (pw_properties_get(p, PW_KEY_DEVICE_ICON_NAME) == NULL) {
d = NULL;
if ((s = pw_properties_get(p, PW_KEY_DEVICE_FORM_FACTOR))) {
if (strcmp(s, "microphone") == 0)
d = "audio-input-microphone";
else if (strcmp(s, "webcam") == 0)
d = "camera-web";
else if (strcmp(s, "computer") == 0)
d = "computer";
else if (strcmp(s, "handset") == 0)
d = "phone";
else if (strcmp(s, "portable") == 0)
d = "multimedia-player";
else if (strcmp(s, "tv") == 0)
d = "video-display";
else if (strcmp(s, "headset") == 0)
d = "audio-headset";
else if (strcmp(s, "headphone") == 0)
d = "audio-headphones";
else if (strcmp(s, "speaker") == 0)
d = "audio-speakers";
else if (strcmp(s, "hands-free") == 0)
d = "audio-handsfree";
}
if (!d)
if ((s = pw_properties_get(p, PW_KEY_DEVICE_CLASS)))
if (strcmp(s, "modem") == 0)
d = "modem";
if (!d)
d = "audio-card";
s = pw_properties_get(p, PW_KEY_DEVICE_BUS);
pw_properties_setf(p, PW_KEY_DEVICE_ICON_NAME,
"%s-analog%s%s", d, s ? "-" : "", s);
}
return 1;
}
static void set_profile(struct device *device, int index)
{
struct impl *impl = device->impl;
char buf[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf));
if (impl->use_acp)
return;
pw_log_debug("%p: set profile %d id:%d", device, index, device->device_id);
if (device->device_id != 0) {
device->profile = index;
spa_device_set_param(device->device,
SPA_PARAM_Profile, 0,
spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamProfile, SPA_PARAM_Profile,
SPA_PARAM_PROFILE_index, SPA_POD_Int(index)));
}
}
static void set_jack_profile(struct impl *impl, int index)
{
char buf[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf));
if (impl->jack_device == NULL)
return;
pw_device_set_param((struct pw_device*)impl->jack_device,
SPA_PARAM_Profile, 0,
spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamProfile, SPA_PARAM_Profile,
SPA_PARAM_PROFILE_index, SPA_POD_Int(index)));
}
static void remove_jack_timeout(struct impl *impl)
{
struct pw_loop *main_loop = impl->session->loop;
if (impl->jack_timeout) {
pw_loop_destroy_source(main_loop, impl->jack_timeout);
impl->jack_timeout = NULL;
}
}
static void jack_timeout(void *data, uint64_t expirations)
{
struct impl *impl = data;
remove_jack_timeout(impl);
set_jack_profile(impl, 1);
}
static void add_jack_timeout(struct impl *impl)
{
struct timespec value;
struct pw_loop *main_loop = impl->session->loop;
if (impl->jack_timeout == NULL)
impl->jack_timeout = pw_loop_add_timer(main_loop, jack_timeout, impl);
value.tv_sec = DEFAULT_JACK_SECONDS;
value.tv_nsec = 0;
pw_loop_update_timer(main_loop, impl->jack_timeout, &value, NULL, false);
}
static void reserve_acquired(void *data, struct rd_device *d)
{
struct device *device = data;
pw_log_debug("%p: reserve acquired %d", device, device->n_acquired);
device->sdevice->locked = false;
if (device->n_acquired == 0)
rd_device_release(device->reserve);
}
static void sync_complete_done(void *data, int seq)
{
struct device *device = data;
pw_log_debug("%d %d", device->seq, seq);
if (seq != device->seq)
return;
spa_hook_remove(&device->sync_listener);
device->seq = 0;
if (device->reserve)
rd_device_complete_release(device->reserve, true);
}
static void sync_destroy(void *data)
{
struct device *device = data;
if (device->seq != 0)
sync_complete_done(data, device->seq);
}
static const struct pw_proxy_events sync_complete_release = {
PW_VERSION_PROXY_EVENTS,
.destroy = sync_destroy,
.done = sync_complete_done
};
static void reserve_release(void *data, struct rd_device *d, int forced)
{
struct device *device = data;
pw_log_debug("%p: reserve release", device);
set_profile(device, 0);
if (device->seq == 0)
pw_proxy_add_listener(device->sdevice->obj.proxy,
&device->sync_listener,
&sync_complete_release, device);
device->seq = pw_proxy_sync(device->sdevice->obj.proxy, 0);
}
static void reserve_busy(void *data, struct rd_device *d, const char *name, int32_t prio)
{
struct device *device = data;
struct impl *impl = device->impl;
pw_log_debug("%p: reserve busy %s", device, name);
device->sdevice->locked = true;
if (strcmp(name, "jack") == 0) {
add_jack_timeout(impl);
} else {
remove_jack_timeout(impl);
}
}
static void reserve_available(void *data, struct rd_device *d, const char *name)
{
struct device *device = data;
struct impl *impl = device->impl;
pw_log_debug("%p: reserve available %s", device, name);
device->sdevice->locked = false;
remove_jack_timeout(impl);
if (strcmp(name, "jack") == 0) {
set_jack_profile(impl, 0);
}
}
static const struct rd_device_callbacks reserve_callbacks = {
.acquired = reserve_acquired,
.release = reserve_release,
.busy = reserve_busy,
.available = reserve_available,
};
static void device_destroy(void *data)
{
struct device *device = data;
struct node *node;
pw_log_debug("device %p destroy", device);
spa_list_remove(&device->link);
spa_list_consume(node, &device->node_list, link)
alsa_remove_node(device, node);
if (device->appeared)
spa_hook_remove(&device->device_listener);
if (device->seq != 0)
spa_hook_remove(&device->sync_listener);
if (device->reserve)
rd_device_destroy(device->reserve);
}
static void device_free(void *data)
{
struct device *device = data;
pw_log_debug("device %p free", device);
spa_hook_remove(&device->listener);
pw_unload_spa_handle(device->handle);
pw_properties_free(device->props);
free(device);
}
static void device_update(void *data)
{
struct device *device = data;
pw_log_debug("device %p appeared %d %d", device, device->appeared, device->profile);
if (!device->appeared) {
device->device_id = device->sdevice->obj.id;
device->appeared = true;
spa_device_add_listener(device->device,
&device->device_listener,
&alsa_device_events, device);
sm_object_sync_update(&device->sdevice->obj);
}
if (device->pending_profile != device->profile && !device->sdevice->locked)
set_profile(device, device->pending_profile);
}
static const struct sm_object_events device_events = {
SM_VERSION_OBJECT_EVENTS,
.destroy = device_destroy,
.free = device_free,
.update = device_update,
};
static struct device *alsa_create_device(struct impl *impl, uint32_t id,
const struct spa_device_object_info *info)
{
struct pw_context *context = impl->session->context;
struct device *device;
struct spa_handle *handle;
int res;
void *iface;
const char *card, *factory_name;
pw_log_debug("new device %u", id);
if (strcmp(info->type, SPA_TYPE_INTERFACE_Device) != 0) {
errno = EINVAL;
return NULL;
}
if (impl->use_acp)
factory_name = SPA_NAME_API_ALSA_ACP_DEVICE;
else
factory_name = info->factory_name;
handle = pw_context_load_spa_handle(context,
factory_name, info->props);
if (handle == NULL) {
res = -errno;
pw_log_error("can't make factory instance: %m");
goto exit;
}
if ((res = spa_handle_get_interface(handle, info->type, &iface)) < 0) {
pw_log_error("can't get %s interface: %s", info->type, spa_strerror(res));
goto unload_handle;
}
device = calloc(1, sizeof(*device));
if (device == NULL) {
res = -errno;
goto unload_handle;
}
device->impl = impl;
device->id = id;
device->handle = handle;
device->device = iface;
device->props = pw_properties_new_dict(info->props);
device->priority = 1000;
update_device_props(device);
device->sdevice = sm_media_session_export_device(impl->session,
&device->props->dict, device->device);
if (device->sdevice == NULL) {
res = -errno;
goto clean_device;
}
sm_object_add_listener(&device->sdevice->obj,
&device->listener,
&device_events, device);
if (impl->conn &&
(card = spa_dict_lookup(info->props, SPA_KEY_API_ALSA_CARD)) != NULL) {
const char *reserve;
pw_properties_setf(device->props, "api.dbus.ReserveDevice1", "Audio%s", card);
reserve = pw_properties_get(device->props, "api.dbus.ReserveDevice1");
device->reserve = rd_device_new(impl->conn, reserve,
"PipeWire", 10,
&reserve_callbacks, device);
if (device->reserve == NULL) {
pw_log_warn("can't create device reserve for %s: %m", reserve);
} else {
rd_device_set_application_device_name(device->reserve,
spa_dict_lookup(info->props, SPA_KEY_API_ALSA_PATH));
rd_device_acquire(device->reserve);
}
device->priority -= atol(card) * 64;
}
device->pending_profile = 1;
device->first = true;
spa_list_init(&device->node_list);
spa_list_append(&impl->device_list, &device->link);
return device;
clean_device:
free(device);
unload_handle:
pw_unload_spa_handle(handle);
exit:
errno = -res;
return NULL;
}
static void alsa_remove_device(struct impl *impl, struct device *device)
{
pw_log_debug("%p: remove device %u", device, device->id);
if (device->sdevice)
sm_object_destroy(&device->sdevice->obj);
}
static void alsa_udev_object_info(void *data, uint32_t id,
const struct spa_device_object_info *info)
{
struct impl *impl = data;
struct device *device;
device = alsa_find_device(impl, id);
if (info == NULL) {
if (device == NULL)
return;
alsa_remove_device(impl, device);
} else if (device == NULL) {
if ((device = alsa_create_device(impl, id, info)) == NULL)
return;
} else {
alsa_update_device(impl, device, info);
}
}
static const struct spa_device_events alsa_udev_events =
{
SPA_VERSION_DEVICE_EVENTS,
.object_info = alsa_udev_object_info,
};
static int alsa_start_jack_device(struct impl *impl)
{
struct pw_properties *props;
int res = 0;
props = pw_properties_new(
SPA_KEY_FACTORY_NAME, SPA_NAME_API_JACK_DEVICE,
SPA_KEY_NODE_NAME, "JACK-Device",
NULL);
impl->jack_device = sm_media_session_create_object(impl->session,
"spa-device-factory",
PW_TYPE_INTERFACE_Device,
PW_VERSION_DEVICE,
&props->dict,
0);
if (impl->jack_device == NULL)
res = -errno;
pw_properties_free(props);
return res;
}
static void session_destroy(void *data)
{
struct impl *impl = data;
remove_jack_timeout(impl);
spa_hook_remove(&impl->session_listener);
spa_hook_remove(&impl->listener);
pw_proxy_destroy(impl->jack_device);
pw_unload_spa_handle(impl->handle);
free(impl);
}
static const struct sm_media_session_events session_events = {
SM_VERSION_MEDIA_SESSION_EVENTS,
.destroy = session_destroy,
};
int sm_alsa_monitor_start(struct sm_media_session *session)
{
struct pw_context *context = session->context;
struct impl *impl;
void *iface;
const char *str;
int res;
impl = calloc(1, sizeof(struct impl));
if (impl == NULL)
return -errno;
impl->session = session;
if ((str = pw_properties_get(session->props, "alsa.use-acp")) != NULL)
impl->use_acp = pw_properties_parse_bool(str);
if (session->dbus_connection)
impl->conn = spa_dbus_connection_get(session->dbus_connection);
if (impl->conn == NULL)
pw_log_warn("no dbus connection, device reservation disabled");
else
pw_log_debug("got dbus connection %p", impl->conn);
impl->handle = pw_context_load_spa_handle(context, SPA_NAME_API_ALSA_ENUM_UDEV, NULL);
if (impl->handle == NULL) {
res = -errno;
goto out_free;
}
if ((res = spa_handle_get_interface(impl->handle, SPA_TYPE_INTERFACE_Device, &iface)) < 0) {
pw_log_error("can't get udev Device interface: %d", res);
goto out_unload;
}
impl->monitor = iface;
spa_list_init(&impl->device_list);
spa_device_add_listener(impl->monitor, &impl->listener, &alsa_udev_events, impl);
if ((res = alsa_start_jack_device(impl)) < 0)
goto out_unload;
sm_media_session_add_listener(session, &impl->session_listener, &session_events, impl);
return 0;
out_unload:
pw_unload_spa_handle(impl->handle);
out_free:
free(impl);
return res;
}
|