-
Notifications
You must be signed in to change notification settings - Fork 189
/
transport.c
1070 lines (884 loc) · 27 KB
/
transport.c
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BlueALSA - transport.c
* Copyright (c) 2016-2018 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
* This project is licensed under the terms of the MIT license.
*
*/
#define _GNU_SOURCE
#include "transport.h"
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <gio/gunixfdlist.h>
#include "a2dp-codecs.h"
#include "bluealsa.h"
#include "ctl.h"
#include "hfp.h"
#include "io.h"
#include "rfcomm.h"
#include "utils.h"
#include "shared/log.h"
static const char *transport_type_to_string(enum ba_transport_type type) {
switch (type) {
case TRANSPORT_TYPE_A2DP:
return "A2DP";
case TRANSPORT_TYPE_RFCOMM:
return "RFCOMM";
case TRANSPORT_TYPE_SCO:
return "SCO";
}
return "N/A";
}
static int io_thread_create(struct ba_transport *t) {
void *(*routine)(void *) = NULL;
int ret;
switch (t->type) {
case TRANSPORT_TYPE_A2DP:
if (t->profile == BLUETOOTH_PROFILE_A2DP_SOURCE)
switch (t->codec) {
case A2DP_CODEC_SBC:
routine = io_thread_a2dp_source_sbc;
break;
#if ENABLE_MPEG
case A2DP_CODEC_MPEG12:
break;
#endif
#if ENABLE_AAC
case A2DP_CODEC_MPEG24:
routine = io_thread_a2dp_source_aac;
break;
#endif
#if ENABLE_APTX
case A2DP_CODEC_VENDOR_APTX:
routine = io_thread_a2dp_source_aptx;
break;
#endif
#if ENABLE_LDAC
case A2DP_CODEC_VENDOR_LDAC:
routine = io_thread_a2dp_source_ldac;
break;
#endif
default:
warn("Codec not supported: %u", t->codec);
}
if (t->profile == BLUETOOTH_PROFILE_A2DP_SINK)
switch (t->codec) {
case A2DP_CODEC_SBC:
routine = io_thread_a2dp_sink_sbc;
break;
#if ENABLE_MPEG
case A2DP_CODEC_MPEG12:
break;
#endif
#if ENABLE_AAC
case A2DP_CODEC_MPEG24:
routine = io_thread_a2dp_sink_aac;
break;
#endif
default:
warn("Codec not supported: %u", t->codec);
}
break;
case TRANSPORT_TYPE_RFCOMM:
routine = rfcomm_thread;
break;
case TRANSPORT_TYPE_SCO:
routine = io_thread_sco;
break;
}
if (routine == NULL)
return -1;
if ((ret = pthread_create(&t->thread, NULL, routine, t)) != 0) {
error("Couldn't create IO thread: %s", strerror(ret));
t->thread = config.main_thread;
return -1;
}
pthread_setname_np(t->thread, "baio");
debug("Created new IO thread: %s: %s",
transport_type_to_string(t->type),
bluetooth_profile_to_string(t->profile));
return 0;
}
struct ba_device *device_new(int hci_dev_id, const bdaddr_t *addr, const char *name) {
struct ba_device *d;
if ((d = calloc(1, sizeof(*d))) == NULL)
return NULL;
d->hci_dev_id = hci_dev_id;
bacpy(&d->addr, addr);
strncpy(d->name, name, sizeof(d->name));
d->name[sizeof(d->name) - 1] = '\0';
d->transports = g_hash_table_new_full(g_str_hash, g_str_equal,
NULL, (GDestroyNotify)transport_free);
return d;
}
void device_free(struct ba_device *d) {
if (d == NULL)
return;
/* XXX: Modification-safe remove-all loop.
*
* By the usage of a standard g_hash_table_remove_all() function, one
* has to comply to the license warranty, which states that anything
* can happen. In our case it is true to the letter - SIGSEGV is 100%
* guaranteed.
*
* Our transport structure holds reference to some other transport
* structure within the same hash-table. Unfortunately, such a usage
* is not supported. Almost every GLib-2.0 function facilitates cache,
* which backfires at us if we modify hash-table from the inside of
* the destroy function. However, it is possible to "iterate" over
* a hash-table in a pop-like matter - reinitialize iterator after
* every modification. And voila - modification-safe remove loop. */
for (;;) {
GHashTableIter iter;
struct ba_transport *t;
g_hash_table_iter_init(&iter, d->transports);
if (!g_hash_table_iter_next(&iter, NULL, (gpointer)&t))
break;
transport_free(t);
}
g_hash_table_unref(d->transports);
free(d);
}
struct ba_device *device_get(GHashTable *devices, const char *key) {
struct ba_device *d;
char name[sizeof(d->name)];
GVariant *property;
bdaddr_t addr;
if ((d = g_hash_table_lookup(devices, key)) != NULL)
return d;
g_dbus_device_path_to_bdaddr(key, &addr);
ba2str(&addr, name);
/* get local (user editable) Bluetooth device name */
if ((property = g_dbus_get_property(config.dbus, "org.bluez", key,
"org.bluez.Device1", "Alias")) != NULL) {
strncpy(name, g_variant_get_string(property, NULL), sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
g_variant_unref(property);
}
d = device_new(config.hci_dev.dev_id, &addr, name);
g_hash_table_insert(devices, g_strdup(key), d);
return d;
}
struct ba_device *device_lookup(GHashTable *devices, const char *key) {
return g_hash_table_lookup(devices, key);
}
bool device_remove(GHashTable *devices, const char *key) {
return g_hash_table_remove(devices, key);
}
void device_set_battery_level(struct ba_device *d, uint8_t value) {
d->battery.enabled = true;
d->battery.level = value;
bluealsa_ctl_event(BA_EVENT_UPDATE_BATTERY);
}
/**
* Create new transport.
*
* @param device Pointer to the device structure.
* @param type Transport type.
* @param dbus_owner D-Bus service, which owns this transport.
* @param dbus_path D-Bus service path for this transport.
* @param profile Bluetooth profile.
* @param codec Used audio codec.
* @return On success, the pointer to the newly allocated transport structure
* is returned. If error occurs, NULL is returned and the errno variable is
* set to indicated the cause of the error. */
struct ba_transport *transport_new(
struct ba_device *device,
enum ba_transport_type type,
const char *dbus_owner,
const char *dbus_path,
enum bluetooth_profile profile,
uint16_t codec) {
struct ba_transport *t;
int err;
if ((t = calloc(1, sizeof(*t))) == NULL)
goto fail;
t->device = device;
t->type = type;
t->profile = profile;
t->codec = codec;
/* HSP supports CVSD only */
if (profile == BLUETOOTH_PROFILE_HSP_HS || profile == BLUETOOTH_PROFILE_HSP_AG)
t->codec = HFP_CODEC_CVSD;
t->state = TRANSPORT_IDLE;
t->thread = config.main_thread;
t->bt_fd = -1;
t->sig_fd[0] = -1;
t->sig_fd[1] = -1;
if ((t->dbus_owner = strdup(dbus_owner)) == NULL)
goto fail;
if ((t->dbus_path = strdup(dbus_path)) == NULL)
goto fail;
if (pipe(t->sig_fd) == -1)
goto fail;
g_hash_table_insert(device->transports, t->dbus_path, t);
return t;
fail:
err = errno;
transport_free(t);
errno = err;
return NULL;
}
struct ba_transport *transport_new_a2dp(
struct ba_device *device,
const char *dbus_owner,
const char *dbus_path,
enum bluetooth_profile profile,
uint16_t codec,
const uint8_t *config,
size_t config_size) {
struct ba_transport *t;
if ((t = transport_new(device, TRANSPORT_TYPE_A2DP,
dbus_owner, dbus_path, profile, codec)) == NULL)
return NULL;
t->a2dp.ch1_volume = 127;
t->a2dp.ch2_volume = 127;
if (config_size > 0) {
t->a2dp.cconfig = malloc(config_size);
t->a2dp.cconfig_size = config_size;
memcpy(t->a2dp.cconfig, config, config_size);
}
t->a2dp.pcm.fd = -1;
t->a2dp.pcm.client = -1;
pthread_cond_init(&t->a2dp.pcm.drained, NULL);
pthread_mutex_init(&t->a2dp.pcm.drained_mn, NULL);
bluealsa_ctl_event(BA_EVENT_TRANSPORT_ADDED);
return t;
}
struct ba_transport *transport_new_rfcomm(
struct ba_device *device,
const char *dbus_owner,
const char *dbus_path,
enum bluetooth_profile profile) {
gchar *dbus_path_sco = NULL;
struct ba_transport *t, *t_sco;
if ((t = transport_new(device, TRANSPORT_TYPE_RFCOMM,
dbus_owner, dbus_path, profile, -1)) == NULL)
goto fail;
dbus_path_sco = g_strdup_printf("%s/sco", dbus_path);
if ((t_sco = transport_new(device, TRANSPORT_TYPE_SCO,
dbus_owner, dbus_path_sco, profile, HFP_CODEC_UNDEFINED)) == NULL)
goto fail;
t->rfcomm.sco = t_sco;
t_sco->sco.rfcomm = t;
t_sco->sco.spk_gain = 15;
t_sco->sco.mic_gain = 15;
t_sco->sco.spk_pcm.fd = -1;
t_sco->sco.spk_pcm.client = -1;
pthread_cond_init(&t_sco->sco.spk_pcm.drained, NULL);
pthread_mutex_init(&t_sco->sco.spk_pcm.drained_mn, NULL);
t_sco->sco.mic_pcm.fd = -1;
t_sco->sco.mic_pcm.client = -1;
pthread_cond_init(&t_sco->sco.mic_pcm.drained, NULL);
pthread_mutex_init(&t_sco->sco.mic_pcm.drained_mn, NULL);
bluealsa_ctl_event(BA_EVENT_TRANSPORT_ADDED);
return t;
fail:
if (dbus_path_sco != NULL)
g_free(dbus_path_sco);
transport_free(t);
return NULL;
}
void transport_free(struct ba_transport *t) {
if (t == NULL || t->state == TRANSPORT_LIMBO)
return;
t->state = TRANSPORT_LIMBO;
debug("Freeing transport: %s: %s (%s)",
transport_type_to_string(t->type),
bluetooth_profile_to_string(t->profile),
bluetooth_a2dp_codec_to_string(t->codec));
/* If the transport is active, prior to releasing resources, we have to
* terminate the IO thread (or at least make sure it is not running any
* more). Not doing so might result in an undefined behavior or even a
* race condition (closed and reused file descriptor). */
transport_pthread_cancel(t->thread);
/* if possible, try to release resources gracefully */
if (t->release != NULL)
t->release(t);
if (t->bt_fd != -1)
close(t->bt_fd);
if (t->sig_fd[0] != -1)
close(t->sig_fd[0]);
if (t->sig_fd[1] != -1)
close(t->sig_fd[1]);
/* free type-specific resources */
switch (t->type) {
case TRANSPORT_TYPE_A2DP:
transport_release_pcm(&t->a2dp.pcm);
pthread_cond_destroy(&t->a2dp.pcm.drained);
pthread_mutex_destroy(&t->a2dp.pcm.drained_mn);
free(t->a2dp.cconfig);
break;
case TRANSPORT_TYPE_RFCOMM:
memset(&t->device->battery, 0, sizeof(t->device->battery));
memset(&t->device->xapl, 0, sizeof(t->device->xapl));
transport_free(t->rfcomm.sco);
break;
case TRANSPORT_TYPE_SCO:
transport_release_pcm(&t->sco.spk_pcm);
pthread_cond_destroy(&t->sco.spk_pcm.drained);
pthread_mutex_destroy(&t->sco.spk_pcm.drained_mn);
transport_release_pcm(&t->sco.mic_pcm);
pthread_cond_destroy(&t->sco.mic_pcm.drained);
pthread_mutex_destroy(&t->sco.mic_pcm.drained_mn);
t->sco.rfcomm->rfcomm.sco = NULL;
break;
}
/* If the free action was called on the behalf of the destroy notification,
* removing a value from the hash-table shouldn't hurt - it would have been
* removed anyway. */
g_hash_table_steal(t->device->transports, t->dbus_path);
bluealsa_ctl_event(BA_EVENT_TRANSPORT_REMOVED);
free(t->dbus_owner);
free(t->dbus_path);
free(t);
}
struct ba_transport *transport_lookup(GHashTable *devices, const char *dbus_path) {
GHashTableIter iter;
struct ba_device *d;
struct ba_transport *t;
g_hash_table_iter_init(&iter, devices);
while (g_hash_table_iter_next(&iter, NULL, (gpointer)&d)) {
if ((t = g_hash_table_lookup(d->transports, dbus_path)) != NULL)
return t;
}
return NULL;
}
struct ba_transport *transport_lookup_pcm_client(GHashTable *devices, int client) {
GHashTableIter iter_d, iter_t;
struct ba_device *d;
struct ba_transport *t;
g_hash_table_iter_init(&iter_d, devices);
while (g_hash_table_iter_next(&iter_d, NULL, (gpointer)&d)) {
g_hash_table_iter_init(&iter_t, d->transports);
while (g_hash_table_iter_next(&iter_t, NULL, (gpointer)&t)) {
switch (t->type) {
case TRANSPORT_TYPE_A2DP:
if (t->a2dp.pcm.client == client)
return t;
break;
case TRANSPORT_TYPE_RFCOMM:
break;
case TRANSPORT_TYPE_SCO:
if (t->sco.spk_pcm.client == client)
return t;
if (t->sco.mic_pcm.client == client)
return t;
break;
}
}
}
return NULL;
}
bool transport_remove(GHashTable *devices, const char *dbus_path) {
GHashTableIter iter;
struct ba_device *d;
struct ba_transport *t;
g_hash_table_iter_init(&iter, devices);
while (g_hash_table_iter_next(&iter, NULL, (gpointer)&d)) {
/* Disassociate D-Bus owner before further actions. This will ensure,
* that we will not generate errors by using non-existent interface. */
if ((t = g_hash_table_lookup(d->transports, dbus_path)) != NULL) {
free(t->dbus_owner);
t->dbus_owner = NULL;
}
if (g_hash_table_remove(d->transports, dbus_path)) {
if (g_hash_table_size(d->transports) == 0)
g_hash_table_iter_remove(&iter);
return true;
}
}
return false;
}
int transport_send_signal(struct ba_transport *t, enum ba_transport_signal sig) {
return write(t->sig_fd[1], &sig, sizeof(sig));
}
int transport_send_rfcomm(struct ba_transport *t, const char command[32]) {
char msg[sizeof(enum ba_transport_signal) + 32];
((enum ba_transport_signal *)msg)[0] = TRANSPORT_SEND_RFCOMM;
memcpy(&msg[sizeof(enum ba_transport_signal)], command, 32);
return write(t->sig_fd[1], msg, sizeof(msg));
}
unsigned int transport_get_channels(const struct ba_transport *t) {
switch (t->type) {
case TRANSPORT_TYPE_A2DP:
switch (t->codec) {
case A2DP_CODEC_SBC:
switch (((a2dp_sbc_t *)t->a2dp.cconfig)->channel_mode) {
case SBC_CHANNEL_MODE_MONO:
return 1;
case SBC_CHANNEL_MODE_STEREO:
case SBC_CHANNEL_MODE_JOINT_STEREO:
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
return 2;
}
break;
#if ENABLE_MPEG
case A2DP_CODEC_MPEG12:
switch (((a2dp_mpeg_t *)t->a2dp.cconfig)->channel_mode) {
case MPEG_CHANNEL_MODE_MONO:
return 1;
case MPEG_CHANNEL_MODE_STEREO:
case MPEG_CHANNEL_MODE_JOINT_STEREO:
case MPEG_CHANNEL_MODE_DUAL_CHANNEL:
return 2;
}
break;
#endif
#if ENABLE_AAC
case A2DP_CODEC_MPEG24:
switch (((a2dp_aac_t *)t->a2dp.cconfig)->channels) {
case AAC_CHANNELS_1:
return 1;
case AAC_CHANNELS_2:
return 2;
}
break;
#endif
#if ENABLE_APTX
case A2DP_CODEC_VENDOR_APTX:
switch (((a2dp_aptx_t *)t->a2dp.cconfig)->channel_mode) {
case APTX_CHANNEL_MODE_MONO:
return 1;
case APTX_CHANNEL_MODE_STEREO:
return 2;
}
break;
#endif
#if ENABLE_LDAC
case A2DP_CODEC_VENDOR_LDAC:
switch (((a2dp_ldac_t *)t->a2dp.cconfig)->channel_mode) {
case LDAC_CHANNEL_MODE_MONO:
return 1;
case LDAC_CHANNEL_MODE_STEREO:
case LDAC_CHANNEL_MODE_DUAL_CHANNEL:
return 2;
}
break;
#endif
}
break;
case TRANSPORT_TYPE_RFCOMM:
break;
case TRANSPORT_TYPE_SCO:
return 1;
}
/* the number of channels is unspecified */
return 0;
}
unsigned int transport_get_sampling(const struct ba_transport *t) {
switch (t->type) {
case TRANSPORT_TYPE_A2DP:
switch (t->codec) {
case A2DP_CODEC_SBC:
switch (((a2dp_sbc_t *)t->a2dp.cconfig)->frequency) {
case SBC_SAMPLING_FREQ_16000:
return 16000;
case SBC_SAMPLING_FREQ_32000:
return 32000;
case SBC_SAMPLING_FREQ_44100:
return 44100;
case SBC_SAMPLING_FREQ_48000:
return 48000;
}
break;
#if ENABLE_MPEG
case A2DP_CODEC_MPEG12:
switch (((a2dp_mpeg_t *)t->a2dp.cconfig)->frequency) {
case MPEG_SAMPLING_FREQ_16000:
return 16000;
case MPEG_SAMPLING_FREQ_22050:
return 22050;
case MPEG_SAMPLING_FREQ_24000:
return 24000;
case MPEG_SAMPLING_FREQ_32000:
return 32000;
case MPEG_SAMPLING_FREQ_44100:
return 44100;
case MPEG_SAMPLING_FREQ_48000:
return 48000;
}
break;
#endif
#if ENABLE_AAC
case A2DP_CODEC_MPEG24:
switch (AAC_GET_FREQUENCY(*(a2dp_aac_t *)t->a2dp.cconfig)) {
case AAC_SAMPLING_FREQ_8000:
return 8000;
case AAC_SAMPLING_FREQ_11025:
return 11025;
case AAC_SAMPLING_FREQ_12000:
return 12000;
case AAC_SAMPLING_FREQ_16000:
return 16000;
case AAC_SAMPLING_FREQ_22050:
return 22050;
case AAC_SAMPLING_FREQ_24000:
return 24000;
case AAC_SAMPLING_FREQ_32000:
return 32000;
case AAC_SAMPLING_FREQ_44100:
return 44100;
case AAC_SAMPLING_FREQ_48000:
return 48000;
case AAC_SAMPLING_FREQ_64000:
return 64000;
case AAC_SAMPLING_FREQ_88200:
return 88200;
case AAC_SAMPLING_FREQ_96000:
return 96000;
}
break;
#endif
#if ENABLE_APTX
case A2DP_CODEC_VENDOR_APTX:
switch (((a2dp_aptx_t *)t->a2dp.cconfig)->frequency) {
case APTX_SAMPLING_FREQ_16000:
return 16000;
case APTX_SAMPLING_FREQ_32000:
return 32000;
case APTX_SAMPLING_FREQ_44100:
return 44100;
case APTX_SAMPLING_FREQ_48000:
return 48000;
}
break;
#endif
#if ENABLE_LDAC
case A2DP_CODEC_VENDOR_LDAC:
switch (((a2dp_ldac_t *)t->a2dp.cconfig)->frequency) {
case LDAC_SAMPLING_FREQ_44100:
return 44100;
case LDAC_SAMPLING_FREQ_48000:
return 48000;
case LDAC_SAMPLING_FREQ_88200:
return 88200;
case LDAC_SAMPLING_FREQ_96000:
return 96000;
case LDAC_SAMPLING_FREQ_176400:
return 176400;
case LDAC_SAMPLING_FREQ_192000:
return 192000;
}
break;
#endif
}
break;
case TRANSPORT_TYPE_RFCOMM:
break;
case TRANSPORT_TYPE_SCO:
switch (t->codec) {
case HFP_CODEC_CVSD:
return 8000;
case HFP_CODEC_MSBC:
return 16000;
default:
debug("Unsupported SCO codec: %#x", t->codec);
}
}
/* the sampling frequency is unspecified */
return 0;
}
int transport_set_volume(struct ba_transport *t, uint8_t ch1_muted, uint8_t ch2_muted,
uint8_t ch1_volume, uint8_t ch2_volume) {
debug("Setting volume for %s profile %d: %d<>%d [%c%c]", batostr_(&t->device->addr),
t->profile, ch1_volume, ch2_volume, ch1_muted ? 'M' : 'O', ch2_muted ? 'M' : 'O');
switch (t->type) {
case TRANSPORT_TYPE_A2DP:
t->a2dp.ch1_muted = ch1_muted;
t->a2dp.ch2_muted = ch2_muted;
t->a2dp.ch1_volume = ch1_volume;
t->a2dp.ch2_volume = ch2_volume;
if (config.a2dp.volume) {
uint16_t volume = (ch1_muted | ch2_muted) ? 0 : MIN(ch1_volume, ch2_volume);
g_dbus_set_property(config.dbus, t->dbus_owner, t->dbus_path,
"org.bluez.MediaTransport1", "Volume", g_variant_new_uint16(volume));
}
break;
case TRANSPORT_TYPE_RFCOMM:
break;
case TRANSPORT_TYPE_SCO:
t->sco.spk_muted = ch1_muted;
t->sco.mic_muted = ch2_muted;
t->sco.spk_gain = ch1_volume;
t->sco.mic_gain = ch2_volume;
/* notify associated RFCOMM transport */
transport_send_signal(t->sco.rfcomm, TRANSPORT_SET_VOLUME);
break;
}
return 0;
}
int transport_set_state(struct ba_transport *t, enum ba_transport_state state) {
debug("State transition: %d -> %d", t->state, state);
if (t->state == state)
return 0;
/* For the A2DP sink profile, the IO thread can not be created until the
* BT transport is acquired, otherwise thread initialized will fail. */
if (t->profile == BLUETOOTH_PROFILE_A2DP_SINK &&
t->state == TRANSPORT_IDLE && state != TRANSPORT_PENDING)
return 0;
int ret = 0;
t->state = state;
switch (state) {
case TRANSPORT_IDLE:
transport_pthread_cancel(t->thread);
break;
case TRANSPORT_PENDING:
/* When transport is marked as pending, try to acquire transport, but only
* if we are handing A2DP sink profile. For source profile, transport has
* to be acquired by our controller (during the PCM open request). */
if (t->profile == BLUETOOTH_PROFILE_A2DP_SINK)
ret = transport_acquire_bt_a2dp(t);
break;
case TRANSPORT_ACTIVE:
case TRANSPORT_PAUSED:
if (pthread_equal(t->thread, config.main_thread))
ret = io_thread_create(t);
break;
case TRANSPORT_LIMBO:
break;
}
/* something went wrong, so go back to idle */
if (ret == -1)
return transport_set_state(t, TRANSPORT_IDLE);
return ret;
}
int transport_set_state_from_string(struct ba_transport *t, const char *state) {
if (strcmp(state, "idle") == 0)
transport_set_state(t, TRANSPORT_IDLE);
else if (strcmp(state, "pending") == 0)
transport_set_state(t, TRANSPORT_PENDING);
else if (strcmp(state, "active") == 0)
transport_set_state(t, TRANSPORT_ACTIVE);
else {
warn("Invalid state: %s", state);
return -1;
}
return 0;
}
int transport_drain_pcm(struct ba_transport *t) {
struct ba_pcm *pcm = NULL;
switch (t->profile) {
case BLUETOOTH_PROFILE_NULL:
case BLUETOOTH_PROFILE_A2DP_SINK:
break;
case BLUETOOTH_PROFILE_A2DP_SOURCE:
pcm = &t->a2dp.pcm;
break;
case BLUETOOTH_PROFILE_HSP_AG:
case BLUETOOTH_PROFILE_HFP_AG:
pcm = &t->sco.spk_pcm;
break;
case BLUETOOTH_PROFILE_HSP_HS:
case BLUETOOTH_PROFILE_HFP_HF:
break;
}
if (pcm == NULL || t->state != TRANSPORT_ACTIVE)
return 0;
pthread_mutex_lock(&pcm->drained_mn);
transport_send_signal(t, TRANSPORT_PCM_SYNC);
pthread_cond_wait(&pcm->drained, &pcm->drained_mn);
pthread_mutex_unlock(&pcm->drained_mn);
/* TODO: Asynchronous transport release.
*
* Unfortunately, BlueZ does not provide API for internal buffer drain.
* Also, there is no specification for Bluetooth playback drain. In order
* to make sure, that all samples are played out, we have to wait some
* arbitrary time before releasing transport. In order to make it right,
* there is a requirement for an asynchronous release mechanism, which
* is not implemented - it requires a little bit of refactoring. */
usleep(200000);
debug("PCM drained");
return 0;
}
int transport_acquire_bt_a2dp(struct ba_transport *t) {
GDBusMessage *msg, *rep;
GUnixFDList *fd_list;
GError *err = NULL;
if (t->bt_fd != -1) {
warn("Closing dangling BT socket: %d", t->bt_fd);
close(t->bt_fd);
t->bt_fd = -1;
}
msg = g_dbus_message_new_method_call(t->dbus_owner, t->dbus_path, "org.bluez.MediaTransport1",
t->state == TRANSPORT_PENDING ? "TryAcquire" : "Acquire");
if ((rep = g_dbus_connection_send_message_with_reply_sync(config.dbus, msg,
G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err)) == NULL)
goto fail;
if (g_dbus_message_get_message_type(rep) == G_DBUS_MESSAGE_TYPE_ERROR) {
g_dbus_message_to_gerror(rep, &err);
goto fail;
}
g_variant_get(g_dbus_message_get_body(rep), "(hqq)", (int32_t *)&t->bt_fd,
(uint16_t *)&t->mtu_read, (uint16_t *)&t->mtu_write);
fd_list = g_dbus_message_get_unix_fd_list(rep);
t->bt_fd = g_unix_fd_list_get(fd_list, 0, &err);
t->release = transport_release_bt_a2dp;
/* Minimize audio delay and increase responsiveness (seeking, stopping) by
* decreasing the BT socket output buffer. We will use a tripled write MTU
* value, in order to prevent tearing due to temporal heavy load. */
size_t size = t->mtu_write * 3;
if (setsockopt(t->bt_fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) == -1)
warn("Couldn't set socket output buffer size: %s", strerror(errno));
debug("New transport: %d (MTU: R:%zu W:%zu)", t->bt_fd, t->mtu_read, t->mtu_write);
fail:
g_object_unref(msg);
if (rep != NULL)
g_object_unref(rep);
if (err != NULL) {
error("Couldn't acquire transport: %s", err->message);
g_error_free(err);
}
return t->bt_fd;
}
int transport_release_bt_a2dp(struct ba_transport *t) {
GDBusMessage *msg = NULL, *rep = NULL;
GError *err = NULL;
int ret = -1;
/* If the transport has not been acquired, or it has been released already,
* there is no need to release it again. In fact, trying to release already
* closed transport will result in returning error message. */
if (t->bt_fd == -1)
return 0;
debug("Releasing transport: %s (%s)",
bluetooth_profile_to_string(t->profile),
bluetooth_a2dp_codec_to_string(t->codec));
/* If the state is idle, it means that either transport was not acquired, or
* was released by the BlueZ. In both cases there is no point in a explicit
* release request. It might even return error (e.g. not authorized). */
if (t->state != TRANSPORT_IDLE && t->dbus_owner != NULL) {
msg = g_dbus_message_new_method_call(t->dbus_owner, t->dbus_path,
"org.bluez.MediaTransport1", "Release");
if ((rep = g_dbus_connection_send_message_with_reply_sync(config.dbus, msg,
G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err)) == NULL)
goto fail;
if (g_dbus_message_get_message_type(rep) == G_DBUS_MESSAGE_TYPE_ERROR) {
g_dbus_message_to_gerror(rep, &err);
if (err->code == G_DBUS_ERROR_NO_REPLY) {
/* If BlueZ is already terminated (or is terminating), we won't receive
* any response. Do not treat such a case as an error - omit logging. */
g_error_free(err);
err = NULL;
}
else
goto fail;
}
}
debug("Closing BT: %d", t->bt_fd);
ret = 0;
t->release = NULL;
close(t->bt_fd);
t->bt_fd = -1;
fail:
if (msg != NULL)
g_object_unref(msg);
if (rep != NULL)
g_object_unref(rep);
if (err != NULL) {
error("Couldn't release transport: %s", err->message);
g_error_free(err);
}
return ret;
}
int transport_release_bt_rfcomm(struct ba_transport *t) {
if (t->bt_fd == -1)
return 0;
debug("Closing RFCOMM: %d", t->bt_fd);
t->release = NULL;
shutdown(t->bt_fd, SHUT_RDWR);
close(t->bt_fd);
t->bt_fd = -1;
/* BlueZ does not trigger profile disconnection signal when the Bluetooth
* link has been lost (e.g. device power down). However, it is required to
* remove transport from the transport pool before reconnecting. */
transport_free(t);
return 0;
}
int transport_acquire_bt_sco(struct ba_transport *t) {
struct hci_dev_info di;
if (t->bt_fd != -1)
return t->bt_fd;
if (hci_devinfo(t->device->hci_dev_id, &di) == -1) {
error("Couldn't get HCI device info: %s", strerror(errno));
return -1;
}
if ((t->bt_fd = hci_open_sco(&di, &t->device->addr, t->codec != HFP_CODEC_CVSD)) == -1) {
error("Couldn't open SCO link: %s", strerror(errno));
return -1;
}
t->mtu_read = di.sco_mtu;
t->mtu_write = di.sco_mtu;
t->release = transport_release_bt_sco;
/* XXX: It seems, that the MTU values returned by the HCI interface
* are incorrect (or our interpretation of them is incorrect). */
t->mtu_read = 48;
t->mtu_write = 48;
debug("New SCO link: %d (MTU: R:%zu W:%zu)", t->bt_fd, t->mtu_read, t->mtu_write);
return t->bt_fd;
}
int transport_release_bt_sco(struct ba_transport *t) {