forked from arkq/bluez-alsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-io.c
1254 lines (1042 loc) · 37.2 KB
/
test-io.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
/*
* test-io.c
* Copyright (c) 2016-2023 Arkadiusz Bokowy
*
* This file is a part of bluez-alsa.
*
* This project is licensed under the terms of the MIT license.
*
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <poll.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <unistd.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <check.h>
#include <glib.h>
#if ENABLE_LDAC
# include <ldacBT.h>
#endif
#if HAVE_SNDFILE
# include <sndfile.h>
#endif
#include "a2dp.h"
#if ENABLE_AAC
# include "a2dp-aac.h"
#endif
#if ENABLE_APTX
# include "a2dp-aptx.h"
#endif
#if ENABLE_APTX_HD
# include "a2dp-aptx-hd.h"
#endif
#if ENABLE_FASTSTREAM
# include "a2dp-faststream.h"
#endif
#if ENABLE_LC3PLUS
# include "a2dp-lc3plus.h"
#endif
#if ENABLE_LDAC
# include "a2dp-ldac.h"
#endif
#if ENABLE_MPEG
# include "a2dp-mpeg.h"
#endif
#include "a2dp-sbc.h"
#include "ba-adapter.h"
#include "ba-device.h"
#include "ba-rfcomm.h"
#include "ba-transport.h"
#include "bluealsa-config.h"
#include "bluealsa-dbus.h"
#include "bluez.h"
#include "hfp.h"
#include "io.h"
#if ENABLE_OFONO
# include "ofono.h"
#endif
#if ENABLE_LC3PLUS || ENABLE_LDAC
# include "rtp.h"
#endif
#include "storage.h"
#include "shared/a2dp-codecs.h"
#include "shared/defs.h"
#include "shared/log.h"
#include "../src/a2dp.c"
#include "../src/ba-transport.c"
#include "inc/btd.inc"
#include "inc/check.inc"
#include "inc/sine.inc"
#define CHECK_VERSION ( \
(CHECK_MAJOR_VERSION << 16 & 0xff0000) | \
(CHECK_MINOR_VERSION << 8 & 0x00ff00) | \
(CHECK_MICRO_VERSION << 0 & 0x0000ff))
void *a2dp_aac_dec_thread(struct ba_transport_thread *th);
void *a2dp_aac_enc_thread(struct ba_transport_thread *th);
void *a2dp_aptx_dec_thread(struct ba_transport_thread *th);
void *a2dp_aptx_enc_thread(struct ba_transport_thread *th);
void *a2dp_aptx_hd_dec_thread(struct ba_transport_thread *th);
void *a2dp_aptx_hd_enc_thread(struct ba_transport_thread *th);
void *a2dp_faststream_dec_thread(struct ba_transport_thread *th);
void *a2dp_faststream_enc_thread(struct ba_transport_thread *th);
void *a2dp_lc3plus_dec_thread(struct ba_transport_thread *th);
void *a2dp_lc3plus_enc_thread(struct ba_transport_thread *th);
void *a2dp_ldac_dec_thread(struct ba_transport_thread *th);
void *a2dp_ldac_enc_thread(struct ba_transport_thread *th);
void *a2dp_mp3_enc_thread(struct ba_transport_thread *th);
void *a2dp_mpeg_dec_thread(struct ba_transport_thread *th);
void *a2dp_sbc_dec_thread(struct ba_transport_thread *th);
void *a2dp_sbc_enc_thread(struct ba_transport_thread *th);
void *sco_dec_thread(struct ba_transport_thread *th);
void *sco_enc_thread(struct ba_transport_thread *th);
int bluealsa_dbus_pcm_register(struct ba_transport_pcm *pcm) {
debug("%s: %p", __func__, (void *)pcm); (void)pcm; return 0; }
void bluealsa_dbus_pcm_update(struct ba_transport_pcm *pcm, unsigned int mask) {
debug("%s: %p %#x", __func__, (void *)pcm, mask); (void)pcm; (void)mask; }
void bluealsa_dbus_pcm_unregister(struct ba_transport_pcm *pcm) {
debug("%s: %p", __func__, (void *)pcm); (void)pcm; }
struct ba_rfcomm *ba_rfcomm_new(struct ba_transport *sco, int fd) {
debug("%s: %p", __func__, (void *)sco); (void)sco; (void)fd; return NULL; }
void ba_rfcomm_destroy(struct ba_rfcomm *r) {
debug("%s: %p", __func__, (void *)r); (void)r; }
int ba_rfcomm_send_signal(struct ba_rfcomm *r, enum ba_rfcomm_signal sig) {
debug("%s: %p: %#x", __func__, (void *)r, sig); (void)r; (void)sig; return 0; }
bool bluez_a2dp_set_configuration(const char *current_dbus_sep_path,
const struct a2dp_sep *sep, GError **error) {
debug("%s: %s: %p", __func__, current_dbus_sep_path, sep);
(void)current_dbus_sep_path; (void)sep; (void)error; return false; }
int ofono_call_volume_update(struct ba_transport *t) {
debug("%s: %p", __func__, t); (void)t; return 0; }
int storage_device_load(const struct ba_device *d) { (void)d; return 0; }
int storage_device_save(const struct ba_device *d) { (void)d; return 0; }
int storage_pcm_data_sync(struct ba_transport_pcm *pcm) { (void)pcm; return 0; }
int storage_pcm_data_update(const struct ba_transport_pcm *pcm) { (void)pcm; return 0; }
static const a2dp_sbc_t config_sbc_44100_stereo = {
.frequency = SBC_SAMPLING_FREQ_44100,
.channel_mode = SBC_CHANNEL_MODE_STEREO,
.block_length = SBC_BLOCK_LENGTH_16,
.subbands = SBC_SUBBANDS_8,
.allocation_method = SBC_ALLOCATION_LOUDNESS,
.min_bitpool = SBC_MIN_BITPOOL,
.max_bitpool = SBC_MAX_BITPOOL,
};
__attribute__ ((unused))
static a2dp_mpeg_t config_mp3_44100_stereo = {
.layer = MPEG_LAYER_MP3,
.channel_mode = MPEG_CHANNEL_MODE_STEREO,
.frequency = MPEG_SAMPLING_FREQ_44100,
MPEG_INIT_BITRATE(0xFFFF)
};
__attribute__ ((unused))
static a2dp_aac_t config_aac_44100_stereo = {
.object_type = AAC_OBJECT_TYPE_MPEG2_AAC_LC,
AAC_INIT_FREQUENCY(AAC_SAMPLING_FREQ_44100)
.channels = AAC_CHANNELS_2,
AAC_INIT_BITRATE(0xFFFF)
};
__attribute__ ((unused))
static const a2dp_aptx_t config_aptx_44100_stereo = {
.info = A2DP_SET_VENDOR_ID_CODEC_ID(APTX_VENDOR_ID, APTX_CODEC_ID),
.frequency = APTX_SAMPLING_FREQ_44100,
.channel_mode = APTX_CHANNEL_MODE_STEREO,
};
__attribute__ ((unused))
static const a2dp_aptx_hd_t config_aptx_hd_44100_stereo = {
.aptx.info = A2DP_SET_VENDOR_ID_CODEC_ID(APTX_HD_VENDOR_ID, APTX_HD_CODEC_ID),
.aptx.frequency = APTX_SAMPLING_FREQ_44100,
.aptx.channel_mode = APTX_CHANNEL_MODE_STEREO,
};
__attribute__ ((unused))
static const a2dp_faststream_t config_faststream_44100_16000 = {
.info = A2DP_SET_VENDOR_ID_CODEC_ID(FASTSTREAM_VENDOR_ID, FASTSTREAM_CODEC_ID),
.direction = FASTSTREAM_DIRECTION_MUSIC | FASTSTREAM_DIRECTION_VOICE,
.frequency_music = FASTSTREAM_SAMPLING_FREQ_MUSIC_44100,
.frequency_voice = FASTSTREAM_SAMPLING_FREQ_VOICE_16000,
};
__attribute__ ((unused))
static const a2dp_lc3plus_t config_lc3plus_48000_stereo = {
.info = A2DP_SET_VENDOR_ID_CODEC_ID(LC3PLUS_VENDOR_ID, LC3PLUS_CODEC_ID),
.frame_duration = LC3PLUS_FRAME_DURATION_050,
.channels = LC3PLUS_CHANNELS_2,
LC3PLUS_INIT_FREQUENCY(LC3PLUS_SAMPLING_FREQ_48000)
};
__attribute__ ((unused))
static const a2dp_ldac_t config_ldac_44100_stereo = {
.info = A2DP_SET_VENDOR_ID_CODEC_ID(LDAC_VENDOR_ID, LDAC_CODEC_ID),
.frequency = LDAC_SAMPLING_FREQ_44100,
.channel_mode = LDAC_CHANNEL_MODE_STEREO,
};
static struct ba_adapter *adapter = NULL;
static struct ba_device *device1 = NULL;
static struct ba_device *device2 = NULL;
static const char *input_bt_file = NULL;
static const char *input_pcm_file = NULL;
static unsigned int aging_duration = 0;
static bool enable_vbr_mode = false;
static bool dump_data = false;
static bool packet_loss = false;
/* input BT dump file */
static struct bt_dump *btdin = NULL;
#if HAVE_SNDFILE
static void *pcm_write_frames_sndfile_async(void *userdata) {
struct ba_transport_pcm *pcm = userdata;
struct pollfd pfds[] = {{ pcm->fd, POLLOUT, 0 }};
SNDFILE *sf = NULL;
SF_INFO sf_info = {};
if ((sf = sf_open(input_pcm_file, SFM_READ, &sf_info)) == NULL) {
error("Couldn't load input audio file: %s", sf_strerror(NULL));
ck_assert_ptr_ne(sf, NULL);
}
for (;;) {
union {
int16_t s16[1024];
int32_t s32[1024];
} buffer;
sf_count_t samples = 0;
ssize_t len = 0;
ck_assert_int_ne(poll(pfds, ARRAYSIZE(pfds), -1), -1);
switch (pcm->format) {
case BA_TRANSPORT_PCM_FORMAT_S16_2LE:
samples = sf_read_short(sf, buffer.s16, ARRAYSIZE(buffer.s16));
len = samples * sizeof(int16_t);
break;
case BA_TRANSPORT_PCM_FORMAT_S24_4LE:
samples = sf_read_int(sf, buffer.s32, ARRAYSIZE(buffer.s32));
for (sf_count_t i = 0; i < samples; i++)
buffer.s32[i] >>= 8;
len = samples * sizeof(int32_t);
break;
case BA_TRANSPORT_PCM_FORMAT_S32_4LE:
samples = sf_read_int(sf, buffer.s32, ARRAYSIZE(buffer.s32));
len = samples * sizeof(int32_t);
break;
default:
g_assert_not_reached();
}
if (samples == 0)
break;
ck_assert_int_eq(write(pfds[0].fd, &buffer, len), len);
};
if (aging_duration == 0) {
/* If we are not performign aging test, close the PCM right
* away. The reading loop will not wait for timeout. */
pthread_mutex_lock(&pcm->mutex);
ba_transport_pcm_release(pcm);
pthread_mutex_unlock(&pcm->mutex);
}
sf_close(sf);
return NULL;
}
#endif
/**
* Write test PCM signal to the file descriptor. */
static void pcm_write_frames(struct ba_transport_pcm *pcm, size_t frames) {
if (input_pcm_file != NULL) {
#if HAVE_SNDFILE
pthread_t thread;
pthread_create(&thread, NULL, pcm_write_frames_sndfile_async, pcm);
pthread_detach(thread);
#else
error("Loading audio files requires sndfile library!");
#endif
return;
}
union {
int16_t s16[2 * 1024];
int32_t s32[2 * 1024];
} pcm_sine_buffer;
size_t pcm_sine_bytes = 0;
switch (pcm->format) {
case BA_TRANSPORT_PCM_FORMAT_S16_2LE:
snd_pcm_sine_s16_2le(pcm_sine_buffer.s16, 1024, pcm->channels, 0, 1.0 / 128);
pcm_sine_bytes = 1024 * pcm->channels * sizeof(int16_t);
break;
case BA_TRANSPORT_PCM_FORMAT_S24_4LE:
snd_pcm_sine_s24_4le(pcm_sine_buffer.s32, 1024, pcm->channels, 0, 1.0 / 128);
pcm_sine_bytes = 1024 * pcm->channels * sizeof(int32_t);
break;
case BA_TRANSPORT_PCM_FORMAT_S32_4LE:
snd_pcm_sine_s32_4le(pcm_sine_buffer.s32, 1024, pcm->channels, 0, 1.0 / 128);
pcm_sine_bytes = 1024 * pcm->channels * sizeof(int32_t);
break;
default:
g_assert_not_reached();
}
size_t samples = pcm->channels * frames;
size_t bytes = BA_TRANSPORT_PCM_FORMAT_BYTES(pcm->format) * samples;
debug("PCM write samples: %zu (%zu bytes)", samples, bytes);
if (dump_data) {
FILE *f;
ck_assert_ptr_ne(f = fopen("sample-sine.pcm", "w"), NULL);
ck_assert_int_eq(fwrite(&pcm_sine_buffer, pcm_sine_bytes, 1, f), 1);
ck_assert_int_eq(fclose(f), 0);
}
while (bytes > 0) {
size_t len = pcm_sine_bytes <= bytes ? pcm_sine_bytes : bytes;
ck_assert_int_eq(write(pcm->fd, &pcm_sine_buffer, len), len);
bytes -= len;
}
if (aging_duration == 0) {
/* If we are not performign aging test, close the PCM right
* away. The reading loop will not wait for timeout. */
pthread_mutex_lock(&pcm->mutex);
ba_transport_pcm_release(pcm);
pthread_mutex_unlock(&pcm->mutex);
}
}
struct bt_data {
struct bt_data *next;
uint8_t data[1024];
size_t len;
};
/**
* Linked list with generated BT data. */
static struct bt_data bt_data = { 0 };
static struct bt_data *bt_data_end;
static void bt_data_init(void) {
bt_data_end = &bt_data;
}
static void bt_data_push(uint8_t *data, size_t len) {
memcpy(bt_data_end->data, data, bt_data_end->len = len);
if (bt_data_end->next == NULL) {
bt_data_end->next = malloc(sizeof(*bt_data_end->next));
bt_data_end->next->next = NULL;
}
bt_data_end = bt_data_end->next;
}
static void bt_data_write(struct ba_transport *t) {
struct pollfd fds[] = {{ t->bt_fd, POLLOUT, 0 }};
struct bt_data *bt_data_head = &bt_data;
bool first_packet = true;
char buffer[2024];
ssize_t len;
if (input_bt_file != NULL) {
while ((len = bt_dump_read(btdin, buffer, sizeof(buffer))) != -1) {
if (packet_loss && random() < INT32_MAX / 3 && !first_packet) {
debug("Simulating packet loss: Dropping BT packet!");
continue;
}
ck_assert_int_ne(poll(fds, ARRAYSIZE(fds), -1), -1);
ck_assert_int_eq(write(fds[0].fd, buffer, len), len);
first_packet = false;
}
}
else {
for (; bt_data_head != bt_data_end; bt_data_head = bt_data_head->next) {
len = bt_data_head->len;
if (packet_loss && random() < INT32_MAX / 3 && !first_packet) {
debug("Simulating packet loss: Dropping BT packet!");
continue;
}
ck_assert_int_ne(poll(fds, ARRAYSIZE(fds), -1), -1);
ck_assert_int_eq(write(fds[0].fd, bt_data_head->data, len), len);
first_packet = false;
}
}
}
static pthread_cond_t test_terminate = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t test_mutex = PTHREAD_MUTEX_INITIALIZER;
static void *test_terminate_timer(void *arg) {
sleep((uintptr_t)arg);
pthread_cond_signal(&test_terminate);
return NULL;
}
static void test_start_terminate_timer(unsigned int delay) {
pthread_t thread;
pthread_create(&thread, NULL, test_terminate_timer, (void *)(uintptr_t)delay);
pthread_detach(thread);
}
static void *test_io_thread_dump_bt(struct ba_transport_thread *th) {
pthread_cleanup_push(PTHREAD_CLEANUP(ba_transport_thread_cleanup), th);
struct ba_transport *t = th->t;
struct pollfd pfds[] = {{ th->bt_fd, POLLIN, 0 }};
struct bt_dump *btd = NULL;
uint8_t buffer[1024];
ssize_t len;
if (dump_data) {
char fname[64];
sprintf(fname, "encoded-%s.btd", transport_to_fname(t));
ck_assert_ptr_ne(btd = bt_dump_create(fname, t), NULL);
}
debug_transport_thread_loop(th, "START");
ba_transport_thread_state_set_running(th);
while (poll(pfds, ARRAYSIZE(pfds), 500) > 0) {
if ((len = io_bt_read(th, buffer, sizeof(buffer))) <= 0) {
if (len == -1)
error("BT read error: %s", strerror(errno));
break;
}
bt_data_push(buffer, len);
hexdump("BT data", buffer, len, false);
if (btd != NULL)
bt_dump_write(btd, buffer, len);
}
if (btd != NULL)
bt_dump_close(btd);
/* signal termination and wait for cancellation */
test_start_terminate_timer(0);
sleep(3600);
pthread_cleanup_pop(1);
return NULL;
}
static void *test_io_thread_dump_pcm(struct ba_transport_thread *th) {
pthread_cleanup_push(PTHREAD_CLEANUP(ba_transport_thread_cleanup), th);
struct ba_transport_pcm *t_pcm = th->pcm;
size_t decoded_samples_total = 0;
#if HAVE_SNDFILE
SNDFILE *sf = NULL;
SF_INFO sf_info = {
.format = SF_FORMAT_WAV,
.channels = t_pcm->channels,
.samplerate = t_pcm->sampling,
};
switch (BA_TRANSPORT_PCM_FORMAT_WIDTH(t_pcm->format)) {
case 8:
sf_info.format |= SF_FORMAT_PCM_S8;
break;
case 16:
sf_info.format |= SF_FORMAT_PCM_16;
break;
case 24:
sf_info.format |= SF_FORMAT_PCM_24;
break;
case 32:
sf_info.format |= SF_FORMAT_PCM_32;
break;
default:
g_assert_not_reached();
}
#endif
if (dump_data) {
#if HAVE_SNDFILE
char fname[64];
sprintf(fname, "decoded-%s.wav", transport_to_fname(th->t));
ck_assert_ptr_ne(sf = sf_open(fname, SFM_WRITE, &sf_info), NULL);
#else
error("Dumping audio files requires sndfile library!");
#endif
}
debug_transport_thread_loop(th, "START");
for (ba_transport_thread_state_set_running(th);;) {
struct pollfd pfds[] = {{ -1, POLLIN, 0 }};
uint8_t buffer[2048];
ssize_t len;
pthread_mutex_lock(&t_pcm->mutex);
pfds[0].fd = t_pcm->fd;
pthread_mutex_unlock(&t_pcm->mutex);
if (poll(pfds, ARRAYSIZE(pfds), 500) <= 0)
break;
if ((len = read(pfds[0].fd, buffer, sizeof(buffer))) == -1) {
debug("PCM read error: %s", strerror(errno));
continue;
}
size_t sample_size = BA_TRANSPORT_PCM_FORMAT_BYTES(t_pcm->format);
size_t samples = len / sample_size;
debug("Decoded samples: %zd", samples);
decoded_samples_total += samples;
#if HAVE_SNDFILE
if (sf != NULL) {
switch (sample_size) {
case 2:
sf_write_short(sf, (short *)buffer, samples);
break;
case 4:
if (sf_info.format & SF_FORMAT_PCM_24)
for (size_t i = 0; i < samples; i++)
((int *)buffer)[i] <<= 8;
sf_write_int(sf, (int *)buffer, samples);
break;
default:
g_assert_not_reached();
}
}
#endif
}
debug("Decoded samples total [%zu frames]: %zu",
decoded_samples_total / t_pcm->channels, decoded_samples_total);
ck_assert_int_gt(decoded_samples_total, 0);
#if HAVE_SNDFILE
if (sf != NULL)
sf_close(sf);
#endif
/* signal termination and wait for cancellation */
test_start_terminate_timer(0);
sleep(3600);
pthread_cleanup_pop(1);
return NULL;
}
/**
* Drive PCM signal through source/sink loop. */
static void test_io(struct ba_transport *t_src, struct ba_transport *t_snk,
void *(*enc)(struct ba_transport_thread *), void *(*dec)(struct ba_transport_thread *),
size_t pcm_write_frames_count) {
const char *enc_name = enc == test_io_thread_dump_pcm ? "dump-pcm" : "encode";
const char *dec_name = dec == test_io_thread_dump_bt ? "dump-bt" : "decode";
if (enc == test_io_thread_dump_pcm && input_pcm_file != NULL)
return;
if (dec == test_io_thread_dump_bt && input_bt_file != NULL)
return;
struct ba_transport_pcm *t_src_pcm = t_src->thread_enc.pcm;
struct ba_transport_pcm *t_snk_pcm = t_snk->thread_dec.pcm;
int bt_fds[2];
ck_assert_int_eq(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0, bt_fds), 0);
debug("Created BT socket pair: %d, %d", bt_fds[0], bt_fds[1]);
t_src->bt_fd = bt_fds[1];
t_snk->bt_fd = bt_fds[0];
int pcm_fds[2];
ck_assert_int_eq(socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, pcm_fds), 0);
debug("Created PCM socket pair: %d, %d", pcm_fds[0], pcm_fds[1]);
t_src_pcm->fd = pcm_fds[1];
t_snk_pcm->fd = pcm_fds[0];
if (dec == test_io_thread_dump_bt)
bt_data_init();
if (aging_duration)
test_start_terminate_timer(aging_duration);
if (enc == test_io_thread_dump_pcm) {
ck_assert_int_eq(ba_transport_thread_create(&t_snk->thread_dec, dec, dec_name, true), 0);
ck_assert_int_eq(ba_transport_thread_create(&t_src->thread_enc, enc, enc_name, true), 0);
bt_data_write(t_src);
}
else {
ck_assert_int_eq(ba_transport_thread_create(&t_src->thread_enc, enc, enc_name, true), 0);
ck_assert_int_eq(ba_transport_thread_create(&t_snk->thread_dec, dec, dec_name, true), 0);
pcm_write_frames(t_snk_pcm, pcm_write_frames_count);
}
pthread_mutex_lock(&test_mutex);
pthread_cond_wait(&test_terminate, &test_mutex);
pthread_mutex_unlock(&test_mutex);
pthread_mutex_lock(&t_src_pcm->mutex);
ba_transport_pcm_release(t_src_pcm);
pthread_mutex_unlock(&t_src_pcm->mutex);
ba_transport_stop(t_src);
pthread_mutex_lock(&t_snk_pcm->mutex);
ba_transport_pcm_release(t_snk_pcm);
pthread_mutex_unlock(&t_snk_pcm->mutex);
ba_transport_stop(t_snk);
}
static int test_transport_acquire(struct ba_transport *t) {
debug("Acquire transport: %d", t->bt_fd); (void)t;
return 0;
}
static int test_transport_release_bt_a2dp(struct ba_transport *t) {
free(t->bluez_dbus_owner); t->bluez_dbus_owner = NULL;
return transport_release_bt_a2dp(t);
}
static struct ba_transport *test_transport_new_a2dp(
struct ba_device *device,
enum ba_transport_profile profile,
const char *dbus_path,
const struct a2dp_codec *codec,
const void *configuration) {
#if DEBUG
if (input_bt_file != NULL)
configuration = &btdin->a2dp_configuration;
#endif
struct ba_transport *t = ba_transport_new_a2dp(device, profile, ":test",
dbus_path, codec, configuration);
t->acquire = test_transport_acquire;
t->release = test_transport_release_bt_a2dp;
return t;
}
static struct ba_transport *test_transport_new_sco(
struct ba_device *device,
enum ba_transport_profile profile,
const char *dbus_path) {
struct ba_transport *t = ba_transport_new_sco(device, profile, ":test",
dbus_path, -1);
t->acquire = test_transport_acquire;
return t;
}
CK_START_TEST(test_a2dp_sbc) {
struct ba_transport *t1 = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/sbc", &a2dp_sbc_source,
&config_sbc_44100_stereo);
struct ba_transport *t2 = test_transport_new_a2dp(device2,
BA_TRANSPORT_PROFILE_A2DP_SINK, "/path/sbc", &a2dp_sbc_sink,
&config_sbc_44100_stereo);
if (aging_duration) {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 153 * 3;
test_io(t1, t2, a2dp_sbc_enc_thread, a2dp_sbc_dec_thread, 4 * 1024);
}
else {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 153 * 3;
test_io(t1, t2, a2dp_sbc_enc_thread, test_io_thread_dump_bt, 2 * 1024);
test_io(t1, t2, test_io_thread_dump_pcm, a2dp_sbc_dec_thread, 2 * 1024);
}
ba_transport_destroy(t1);
ba_transport_destroy(t2);
} CK_END_TEST
CK_START_TEST(test_a2dp_sbc_invalid_config) {
const a2dp_sbc_t config_sbc_invalid = { 0 };
struct ba_transport *t = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/sbc", &a2dp_sbc_source,
&config_sbc_invalid);
int bt_fds[2];
ck_assert_int_eq(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0, bt_fds), 0);
debug("Created BT socket pair: %d, %d", bt_fds[0], bt_fds[1]);
t->mtu_read = t->mtu_write = 153 * 3;
t->bt_fd = bt_fds[1];
struct ba_transport_thread *th = &t->thread_enc;
ck_assert_int_eq(ba_transport_thread_create(th, a2dp_sbc_enc_thread, "sbc", true), 0);
ck_assert_int_eq(ba_transport_thread_state_wait_running(th), -1);
ba_transport_destroy(t);
close(bt_fds[0]);
} CK_END_TEST
CK_START_TEST(test_a2dp_sbc_pcm_drop) {
int16_t pcm_zero[90] = { 0 };
int16_t pcm_rand[ARRAYSIZE(pcm_zero)];
for (size_t i = 0; i < ARRAYSIZE(pcm_rand); i++)
pcm_rand[i] = rand();
struct ba_transport *t1 = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/sbc", &a2dp_sbc_source,
&config_sbc_44100_stereo);
struct ba_transport *t2 = test_transport_new_a2dp(device2,
BA_TRANSPORT_PROFILE_A2DP_SINK, "/path/sbc", &a2dp_sbc_sink,
&config_sbc_44100_stereo);
struct ba_transport_thread *th1 = &t1->thread_enc;
struct ba_transport_thread *th2 = &t2->thread_dec;
int bt_fds[2];
ck_assert_int_eq(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK, 0, bt_fds), 0);
debug("Created BT socket pair: %d, %d", bt_fds[0], bt_fds[1]);
t1->mtu_read = t1->mtu_write = 256;
t2->mtu_read = t2->mtu_write = 256;
t1->bt_fd = bt_fds[1];
t2->bt_fd = bt_fds[0];
int pcm_snk_fds[2];
ck_assert_int_eq(pipe2(pcm_snk_fds, O_NONBLOCK), 0);
debug("Created PCM pipe pair: %d, %d", pcm_snk_fds[0], pcm_snk_fds[1]);
th1->pcm->fd = pcm_snk_fds[0];
th1->pcm->active = true;
int pcm_src_fds[2];
ck_assert_int_eq(pipe2(pcm_src_fds, O_NONBLOCK), 0);
debug("Created PCM pipe pair: %d, %d", pcm_src_fds[0], pcm_src_fds[1]);
th2->pcm->fd = pcm_src_fds[1];
th2->pcm->active = true;
/* sink PCM */
struct ba_transport_pcm *pcm = th1->pcm;
/* write zero samples to PCM FIFO until it is full */
while (write(pcm_snk_fds[1], pcm_zero, sizeof(pcm_zero)) > 0)
continue;
/* drop PCM samples before IO thread was started */
ck_assert_int_eq(ba_transport_pcm_drop(pcm), 0);
/* start IO thread and make sure it is running */
ck_assert_int_eq(ba_transport_thread_create(th1, a2dp_sbc_enc_thread, "sbc", true), 0);
ck_assert_int_eq(ba_transport_thread_state_wait_running(th1), 0);
/* wait for 50 ms - let the thread to run for a while */
usleep(50000);
uint8_t bt_buffer[1024];
/* try to read data from BT socket - it should be empty because
* the PCM has been dropped before the thread was started */
ck_assert_int_eq(read(bt_fds[0], bt_buffer, sizeof(bt_buffer)), -1);
ck_assert_int_eq(errno, EAGAIN);
/* write non-zero samples to PCM FIFO and process some of it */
while (write(pcm_snk_fds[1], pcm_rand, sizeof(pcm_rand)) > 0)
continue;
usleep(50000);
/* drop PCM samples */
ck_assert_int_eq(ba_transport_pcm_drop(pcm), 0);
/* write a little bit of non-zero samples and wait for processing; the
* number of samples shall be small enough to not produce a single codec
* frame, but enough to fill-in internal buffers */
ck_assert_int_gt(write(pcm_snk_fds[1], pcm_rand, sizeof(pcm_rand)), 0);
usleep(10000);
/* again drop PCM samples */
ck_assert_int_eq(ba_transport_pcm_drop(pcm), 0);
/* flush already processed data */
while (read(bt_fds[0], bt_buffer, sizeof(bt_buffer)) > 0)
continue;
/* After PCM has been dropped, IO thread should not process any more
* non-zero samples. We will check this by writing zero samples and
* checking if decoded data is all silence. */
ck_assert_int_eq(ba_transport_thread_create(th2, a2dp_sbc_dec_thread, "sbc", true), 0);
ck_assert_int_eq(ba_transport_thread_state_wait_running(th2), 0);
/* write some zero samples to PCM FIFO and process them */
for (size_t i = 0; i < 100; i++)
if (write(pcm_snk_fds[1], pcm_zero, sizeof(pcm_zero)) <= 0)
break;
usleep(250000);
ssize_t rv;
int16_t pcm_buffer[1024];
/* read decoded data and check if it is all silence */
while ((rv = read(pcm_src_fds[0], pcm_buffer, sizeof(pcm_buffer))) > 0) {
const size_t samples = rv / sizeof(pcm_buffer[0]);
for (size_t i = 0; i < samples; i++)
ck_assert_int_eq(pcm_buffer[i], 0);
}
ba_transport_destroy(t1);
ba_transport_destroy(t2);
close(pcm_snk_fds[0]);
close(pcm_src_fds[1]);
close(bt_fds[0]);
} CK_END_TEST
#if ENABLE_MP3LAME
CK_START_TEST(test_a2dp_mp3) {
config_mp3_44100_stereo.vbr = enable_vbr_mode ? 1 : 0;
struct ba_transport *t1 = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/mp3", &a2dp_mpeg_source,
&config_mp3_44100_stereo);
struct ba_transport *t2 = test_transport_new_a2dp(device2,
BA_TRANSPORT_PROFILE_A2DP_SINK, "/path/mp3", &a2dp_mpeg_sink,
&config_mp3_44100_stereo);
if (aging_duration) {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 1024;
test_io(t1, t2, a2dp_mp3_enc_thread, a2dp_mpeg_dec_thread, 4 * 1024);
}
else {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 512;
test_io(t1, t2, a2dp_mp3_enc_thread, test_io_thread_dump_bt, 3 * 1024);
test_io(t1, t2, test_io_thread_dump_pcm, a2dp_mpeg_dec_thread, 3 * 1024);
}
ba_transport_destroy(t1);
ba_transport_destroy(t2);
} CK_END_TEST
#endif
#if ENABLE_AAC
CK_START_TEST(test_a2dp_aac) {
config.aac_afterburner = true;
config.aac_prefer_vbr = enable_vbr_mode;
config_aac_44100_stereo.vbr = enable_vbr_mode ? 1 : 0;
struct ba_transport *t1 = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/aac", &a2dp_aac_source,
&config_aac_44100_stereo);
struct ba_transport *t2 = test_transport_new_a2dp(device2,
BA_TRANSPORT_PROFILE_A2DP_SINK, "/path/aac", &a2dp_aac_sink,
&config_aac_44100_stereo);
if (aging_duration) {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 450;
test_io(t1, t2, a2dp_aac_enc_thread, a2dp_aac_dec_thread, 4 * 1024);
}
else {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 190;
test_io(t1, t2, a2dp_aac_enc_thread, test_io_thread_dump_bt, 2 * 1024);
test_io(t1, t2, test_io_thread_dump_pcm, a2dp_aac_dec_thread, 2 * 1024);
}
ba_transport_destroy(t1);
ba_transport_destroy(t2);
} CK_END_TEST
#endif
#if ENABLE_APTX
CK_START_TEST(test_a2dp_aptx) {
struct ba_transport *t1 = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/aptx", &a2dp_aptx_source,
&config_aptx_44100_stereo);
struct ba_transport *t2 = test_transport_new_a2dp(device2,
BA_TRANSPORT_PROFILE_A2DP_SINK, "/path/aptx", &a2dp_aptx_sink,
&config_aptx_44100_stereo);
if (aging_duration) {
#if HAVE_APTX_DECODE
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 400;
test_io(t1, t2, a2dp_aptx_enc_thread, a2dp_aptx_dec_thread, 4 * 1024);
#endif
}
else {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 40;
test_io(t1, t2, a2dp_aptx_enc_thread, test_io_thread_dump_bt, 2 * 1024);
#if HAVE_APTX_DECODE
test_io(t1, t2, test_io_thread_dump_pcm, a2dp_aptx_dec_thread, 2 * 1024);
#endif
};
ba_transport_destroy(t1);
ba_transport_destroy(t2);
} CK_END_TEST
#endif
#if ENABLE_APTX_HD
CK_START_TEST(test_a2dp_aptx_hd) {
struct ba_transport *t1 = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/aptxhd", &a2dp_aptx_hd_source,
&config_aptx_hd_44100_stereo);
struct ba_transport *t2 = test_transport_new_a2dp(device2,
BA_TRANSPORT_PROFILE_A2DP_SINK, "/path/aptxhd", &a2dp_aptx_hd_sink,
&config_aptx_hd_44100_stereo);
if (aging_duration) {
#if HAVE_APTX_HD_DECODE
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 600;
test_io(t1, t2, a2dp_aptx_hd_enc_thread, a2dp_aptx_hd_dec_thread, 4 * 1024);
#endif
}
else {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 60;
test_io(t1, t2, a2dp_aptx_hd_enc_thread, test_io_thread_dump_bt, 2 * 1024);
#if HAVE_APTX_HD_DECODE
test_io(t1, t2, test_io_thread_dump_pcm, a2dp_aptx_hd_dec_thread, 2 * 1024);
#endif
};
ba_transport_destroy(t1);
ba_transport_destroy(t2);
} CK_END_TEST
#endif
#if ENABLE_FASTSTREAM
CK_START_TEST(test_a2dp_faststream_music) {
struct ba_transport *t1 = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/faststream", &a2dp_faststream_source,
&config_faststream_44100_16000);
struct ba_transport *t2 = test_transport_new_a2dp(device2,
BA_TRANSPORT_PROFILE_A2DP_SINK, "/path/faststream", &a2dp_faststream_sink,
&config_faststream_44100_16000);
if (aging_duration) {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 72 * 3;
test_io(t1, t2, a2dp_faststream_enc_thread, a2dp_faststream_dec_thread, 4 * 1024);
}
else {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 72 * 3;
test_io(t1, t2, a2dp_faststream_enc_thread, test_io_thread_dump_bt, 2 * 1024);
test_io(t1, t2, test_io_thread_dump_pcm, a2dp_faststream_dec_thread, 2 * 1024);
}
ba_transport_destroy(t1);
ba_transport_destroy(t2);
} CK_END_TEST
#endif
#if ENABLE_FASTSTREAM
CK_START_TEST(test_a2dp_faststream_voice) {
struct ba_transport *t1 = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/faststream", &a2dp_faststream_source,
&config_faststream_44100_16000);
struct ba_transport *t2 = test_transport_new_a2dp(device2,
BA_TRANSPORT_PROFILE_A2DP_SINK, "/path/faststream", &a2dp_faststream_sink,
&config_faststream_44100_16000);
if (aging_duration) {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 72 * 3;
test_io(t2, t1, a2dp_faststream_enc_thread, a2dp_faststream_dec_thread, 4 * 1024);
}
else {
t1->mtu_read = t1->mtu_write = t2->mtu_read = t2->mtu_write = 72 * 3;
test_io(t2, t1, a2dp_faststream_enc_thread, test_io_thread_dump_bt, 2 * 1024);
test_io(t2, t1, test_io_thread_dump_pcm, a2dp_faststream_dec_thread, 2 * 1024);
}
ba_transport_destroy(t1);
ba_transport_destroy(t2);
} CK_END_TEST
#endif
#if ENABLE_LC3PLUS
CK_START_TEST(test_a2dp_lc3plus) {
struct ba_transport *t1 = test_transport_new_a2dp(device1,
BA_TRANSPORT_PROFILE_A2DP_SOURCE, "/path/lc3plus", &a2dp_lc3plus_source,
&config_lc3plus_48000_stereo);
struct ba_transport *t2 = test_transport_new_a2dp(device2,