forked from mikebrady/shairport-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
3668 lines (3267 loc) · 151 KB
/
player.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
/*
* Slave-clocked ALAC stream player. This file is part of Shairport.
* Copyright (c) James Laird 2011, 2013
* All rights reserved.
*
* Modifications for audio synchronisation, AirPlay 2
* and related work, copyright (c) Mike Brady 2014 -- 2023
* All rights reserved.
*
* 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 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 <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <sys/types.h>
#include <unistd.h>
#include "config.h"
#ifdef CONFIG_MBEDTLS
#include <mbedtls/aes.h>
#endif
#ifdef CONFIG_POLARSSL
#include <polarssl/aes.h>
#include <polarssl/havege.h>
#endif
#ifdef CONFIG_OPENSSL
#include <openssl/aes.h> // needed for older AES stuff
#include <openssl/bio.h> // needed for BIO_new_mem_buf
#include <openssl/err.h> // needed for ERR_error_string, ERR_get_error
#include <openssl/evp.h> // needed for EVP_PKEY_CTX_new, EVP_PKEY_sign_init, EVP_PKEY_sign
#include <openssl/pem.h> // needed for PEM_read_bio_RSAPrivateKey, EVP_PKEY_CTX_set_rsa_padding
#include <openssl/rsa.h> // needed for EVP_PKEY_CTX_set_rsa_padding
#endif
#ifdef CONFIG_SOXR
#include <soxr.h>
#endif
#ifdef CONFIG_CONVOLUTION
#include <FFTConvolver/convolver.h>
#endif
#ifdef CONFIG_METADATA_HUB
#include "metadata_hub.h"
#endif
#ifdef CONFIG_DACP_CLIENT
#include "dacp.h"
#endif
#include "common.h"
#include "mdns.h"
#include "player.h"
#include "rtp.h"
#include "rtsp.h"
#include "alac.h"
#ifdef CONFIG_APPLE_ALAC
#include "apple_alac.h"
#endif
#ifdef CONFIG_AIRPLAY_2
#include "ptp-utilities.h"
#endif
#include "loudness.h"
#include "activity_monitor.h"
// make the first audio packet deliberately early to bias the sync error of
// the very first packet, making the error more likely to be too early
// rather than too late. It it's too early,
// a delay exactly compensating for it can be sent just before the
// first packet. This should exactly compensate for the error.
int64_t first_frame_early_bias = 8;
// default buffer size
// needs to be a power of 2 because of the way BUFIDX(seqno) works
// #define BUFFER_FRAMES 512
#define MAX_PACKET 2048
// DAC buffer occupancy stuff
#define DAC_BUFFER_QUEUE_MINIMUM_LENGTH 2500
// static abuf_t audio_buffer[BUFFER_FRAMES];
#define BUFIDX(seqno) ((seq_t)(seqno) % BUFFER_FRAMES)
int32_t modulo_32_offset(uint32_t from, uint32_t to) { return to - from; }
void do_flush(uint32_t timestamp, rtsp_conn_info *conn);
void ab_resync(rtsp_conn_info *conn) {
int i;
for (i = 0; i < BUFFER_FRAMES; i++) {
conn->audio_buffer[i].ready = 0;
conn->audio_buffer[i].resend_request_number = 0;
conn->audio_buffer[i].resend_time =
0; // this is either zero or the time the last resend was requested.
conn->audio_buffer[i].initialisation_time =
0; // this is either the time the packet was received or the time it was noticed the packet
// was missing.
conn->audio_buffer[i].sequence_number = 0;
}
conn->ab_synced = 0;
conn->last_seqno_read = -1;
conn->ab_buffering = 1;
}
// the sequence numbers will wrap pretty often.
// this returns true if the second arg is strictly after the first
static inline int is_after(seq_t a, seq_t b) {
int16_t d = b - a;
return d > 0;
}
void reset_input_flow_metrics(rtsp_conn_info *conn) {
conn->play_number_after_flush = 0;
conn->packet_count_since_flush = 0;
conn->input_frame_rate_starting_point_is_valid = 0;
conn->initial_reference_time = 0;
conn->initial_reference_timestamp = 0;
}
void unencrypted_packet_decode(unsigned char *packet, int length, short *dest, int *outsize,
int size_limit, rtsp_conn_info *conn) {
if (conn->stream.type == ast_apple_lossless) {
#ifdef CONFIG_APPLE_ALAC
if (config.use_apple_decoder) {
if (conn->decoder_in_use != 1 << decoder_apple_alac) {
debug(2, "Apple ALAC Decoder used on encrypted audio.");
conn->decoder_in_use = 1 << decoder_apple_alac;
}
apple_alac_decode_frame(packet, length, (unsigned char *)dest, outsize);
*outsize = *outsize * 4; // bring the size to bytes
} else
#endif
{
if (conn->decoder_in_use != 1 << decoder_hammerton) {
debug(2, "Hammerton Decoder used on encrypted audio.");
conn->decoder_in_use = 1 << decoder_hammerton;
}
alac_decode_frame(conn->decoder_info, packet, (unsigned char *)dest, outsize);
}
} else if (conn->stream.type == ast_uncompressed) {
int length_to_use = length;
if (length_to_use > size_limit) {
warn("unencrypted_packet_decode: uncompressed audio packet too long (size: %d bytes) to "
"process -- truncated",
length);
length_to_use = size_limit;
}
int i;
short *source = (short *)packet;
for (i = 0; i < (length_to_use / 2); i++) {
*dest = ntohs(*source);
dest++;
source++;
}
*outsize = length_to_use;
}
}
#ifdef CONFIG_OPENSSL
// Thanks to
// https://stackoverflow.com/questions/27558625/how-do-i-use-aes-cbc-encrypt-128-openssl-properly-in-ubuntu
// for inspiration. Changed to a 128-bit key and no padding.
int openssl_aes_decrypt_cbc(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
unsigned char *iv, unsigned char *plaintext) {
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len = 0;
ctx = EVP_CIPHER_CTX_new();
if (ctx != NULL) {
if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1) {
EVP_CIPHER_CTX_set_padding(ctx, 0); // no padding -- always returns 1
// no need to allow space for padding in the output, as padding is disabled
if (EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len) == 1) {
plaintext_len = len;
if (EVP_DecryptFinal_ex(ctx, plaintext + len, &len) == 1) {
plaintext_len += len;
} else {
debug(1, "EVP_DecryptFinal_ex error \"%s\".", ERR_error_string(ERR_get_error(), NULL));
}
} else {
debug(1, "EVP_DecryptUpdate error \"%s\".", ERR_error_string(ERR_get_error(), NULL));
}
} else {
debug(1, "EVP_DecryptInit_ex error \"%s\".", ERR_error_string(ERR_get_error(), NULL));
}
EVP_CIPHER_CTX_free(ctx);
} else {
debug(1, "EVP_CIPHER_CTX_new error \"%s\".", ERR_error_string(ERR_get_error(), NULL));
}
return plaintext_len;
}
#endif
int audio_packet_decode(short *dest, int *destlen, uint8_t *buf, int len, rtsp_conn_info *conn) {
// parameters: where the decoded stuff goes, its length in samples,
// the incoming packet, the length of the incoming packet in bytes
// destlen should contain the allowed max number of samples on entry
if (len > MAX_PACKET) {
warn("Incoming audio packet size is too large at %d; it should not exceed %d.", len,
MAX_PACKET);
return -1;
}
unsigned char packet[MAX_PACKET];
// unsigned char packetp[MAX_PACKET];
assert(len <= MAX_PACKET);
int reply = 0; // everything okay
int outsize = conn->input_bytes_per_frame * (*destlen); // the size the output should be, in bytes
int maximum_possible_outsize = outsize;
if (conn->stream.encrypted) {
unsigned char iv[16];
int aeslen = len & ~0xf;
memcpy(iv, conn->stream.aesiv, sizeof(iv));
#ifdef CONFIG_MBEDTLS
mbedtls_aes_crypt_cbc(&conn->dctx, MBEDTLS_AES_DECRYPT, aeslen, iv, buf, packet);
#endif
#ifdef CONFIG_POLARSSL
aes_crypt_cbc(&conn->dctx, AES_DECRYPT, aeslen, iv, buf, packet);
#endif
#ifdef CONFIG_OPENSSL
openssl_aes_decrypt_cbc(buf, aeslen, conn->stream.aeskey, iv, packet);
#endif
memcpy(packet + aeslen, buf + aeslen, len - aeslen);
unencrypted_packet_decode(packet, len, dest, &outsize, maximum_possible_outsize, conn);
} else {
// not encrypted
unencrypted_packet_decode(buf, len, dest, &outsize, maximum_possible_outsize, conn);
}
if (outsize > maximum_possible_outsize) {
debug(2,
"Output from alac_decode larger (%d bytes, not frames) than expected (%d bytes) -- "
"truncated, but buffer overflow possible! Encrypted = %d.",
outsize, maximum_possible_outsize, conn->stream.encrypted);
reply = -1; // output packet is the wrong size
}
if (conn->input_bytes_per_frame != 0)
*destlen = outsize / conn->input_bytes_per_frame;
else
die("Unexpectedly, conn->input_bytes_per_frame is zero.");
if ((outsize % conn->input_bytes_per_frame) != 0)
debug(1,
"Number of audio frames (%d) does not correspond exactly to the number of bytes (%d) "
"and the audio frame size (%d).",
*destlen, outsize, conn->input_bytes_per_frame);
return reply;
}
static int init_alac_decoder(int32_t fmtp[12], rtsp_conn_info *conn) {
// clang-format off
// This is a guess, but the format of the fmtp looks identical to the format of an
// ALACSpecificCOnfig which is detailed in the file ALACMagicCookieDescription.txt
// in the Apple ALAC sample implementation
// Here it is:
/*
* ALAC Specific Info (24 bytes) (mandatory)
__________________________________________________________________________________________________________________________________
The Apple Lossless codec stores specific information about the encoded stream in the ALACSpecificConfig. This
info is vended by the encoder and is used to setup the decoder for a given encoded bitstream.
When read from and written to a file, the fields of this struct must be in big-endian order.
When vended by the encoder (and received by the decoder) the struct values will be in big-endian order.
struct ALACSpecificConfig (defined in ALACAudioTypes.h)
abstract This struct is used to describe codec provided information about the encoded Apple Lossless bitstream.
It must accompany the encoded stream in the containing audio file and be provided to the decoder.
field frameLength uint32_t indicating the frames per packet when no explicit frames per packet setting is
present in the packet header. The encoder frames per packet can be explicitly set
but for maximum compatibility, the default encoder setting of 4096 should be used.
field compatibleVersion uint8_t indicating compatible version,
value must be set to 0
field bitDepth uint8_t describes the bit depth of the source PCM data (maximum value = 32)
field pb uint8_t currently unused tuning parameter.
value should be set to 40
field mb uint8_t currently unused tuning parameter.
value should be set to 10
field kb uint8_t currently unused tuning parameter.
value should be set to 14
field numChannels uint8_t describes the channel count (1 = mono, 2 = stereo, etc...)
when channel layout info is not provided in the 'magic cookie', a channel count > 2
describes a set of discreet channels with no specific ordering
field maxRun uint16_t currently unused.
value should be set to 255
field maxFrameBytes uint32_t the maximum size of an Apple Lossless packet within the encoded stream.
value of 0 indicates unknown
field avgBitRate uint32_t the average bit rate in bits per second of the Apple Lossless stream.
value of 0 indicates unknown
field sampleRate uint32_t sample rate of the encoded stream
typedef struct ALACSpecificConfig
{
uint32_t frameLength;
uint8_t compatibleVersion;
uint8_t bitDepth;
uint8_t pb;
uint8_t mb;
uint8_t kb;
uint8_t numChannels;
uint16_t maxRun;
uint32_t maxFrameBytes;
uint32_t avgBitRate;
uint32_t sampleRate;
} ALACSpecificConfig;
*/
// We are going to go on that basis
// clang-format on
alac_file *alac;
alac = alac_create(conn->input_bit_depth,
conn->input_num_channels); // no pthread cancellation point in here
if (!alac)
return 1;
conn->decoder_info = alac;
alac->setinfo_max_samples_per_frame = conn->max_frames_per_packet;
alac->setinfo_7a = fmtp[2];
alac->setinfo_sample_size = conn->input_bit_depth;
alac->setinfo_rice_historymult = fmtp[4];
alac->setinfo_rice_initialhistory = fmtp[5];
alac->setinfo_rice_kmodifier = fmtp[6];
alac->setinfo_7f = fmtp[7];
alac->setinfo_80 = fmtp[8];
alac->setinfo_82 = fmtp[9];
alac->setinfo_86 = fmtp[10];
alac->setinfo_8a_rate = fmtp[11];
alac_allocate_buffers(alac); // no pthread cancellation point in here
#ifdef CONFIG_APPLE_ALAC
apple_alac_init(fmtp); // no pthread cancellation point in here
#endif
return 0;
}
static void terminate_decoders(rtsp_conn_info *conn) {
alac_free(conn->decoder_info);
#ifdef CONFIG_APPLE_ALAC
apple_alac_terminate();
#endif
}
uint64_t buffers_allocated = 0;
uint64_t buffers_released = 0;
static void init_buffer(rtsp_conn_info *conn) {
// debug(1,"input_bytes_per_frame: %d.", conn->input_bytes_per_frame);
// debug(1,"input_bit_depth: %d.", conn->input_bit_depth);
int i;
for (i = 0; i < BUFFER_FRAMES; i++) {
// conn->audio_buffer[i].data = malloc(conn->input_bytes_per_frame *
// conn->max_frames_per_packet);
void *allocation = malloc(8 * conn->max_frames_per_packet);
if (allocation == NULL) {
die("could not allocate memory for audio buffers. %" PRId64 " buffers allocated, %" PRId64
" buffers released.",
buffers_allocated, buffers_released);
} else {
conn->audio_buffer[i].data = allocation;
buffers_allocated++;
}
}
}
static void free_audio_buffers(rtsp_conn_info *conn) {
int i;
for (i = 0; i < BUFFER_FRAMES; i++) {
free(conn->audio_buffer[i].data);
buffers_released++;
}
debug(2, "%" PRId64 " buffers allocated, %" PRId64 " buffers released.", buffers_allocated,
buffers_released);
}
int first_possibly_missing_frame = -1;
void reset_buffer(rtsp_conn_info *conn) {
debug_mutex_lock(&conn->ab_mutex, 30000, 0);
ab_resync(conn);
debug_mutex_unlock(&conn->ab_mutex, 0);
if (config.output->flush) {
config.output->flush(); // no cancellation points
// debug(1, "reset_buffer: flush output device.");
}
}
void get_audio_buffer_size_and_occupancy(unsigned int *size, unsigned int *occupancy,
rtsp_conn_info *conn) {
debug_mutex_lock(&conn->ab_mutex, 30000, 0);
*size = BUFFER_FRAMES;
if (conn->ab_synced) {
int16_t occ =
conn->ab_write - conn->ab_read; // will be zero or positive if read and write are within
// 2^15 of each other and write is at or after read
*occupancy = occ;
} else {
*occupancy = 0;
}
debug_mutex_unlock(&conn->ab_mutex, 0);
}
void player_put_packet(int original_format, seq_t seqno, uint32_t actual_timestamp, uint8_t *data,
int len, rtsp_conn_info *conn) {
// if it's original format, it has a valid seqno and must be decoded
// otherwise, it can take the next seqno and doesn't need decoding.
// ignore a request to flush that has been made before the first packet...
if (conn->packet_count == 0) {
debug_mutex_lock(&conn->flush_mutex, 1000, 1);
conn->flush_requested = 0;
conn->flush_rtp_timestamp = 0;
debug_mutex_unlock(&conn->flush_mutex, 3);
}
debug_mutex_lock(&conn->ab_mutex, 30000, 0);
uint64_t time_now = get_absolute_time_in_ns();
conn->packet_count++;
conn->packet_count_since_flush++;
conn->time_of_last_audio_packet = time_now;
if (conn->connection_state_to_output) { // if we are supposed to be processing these packets
abuf_t *abuf = 0;
if (!conn->ab_synced) {
conn->ab_write = seqno;
conn->ab_read = seqno;
conn->ab_synced = 1;
conn->first_packet_timestamp = 0;
debug(2, "Connection %d: synced by first packet, seqno %u.", conn->connection_number, seqno);
} else if (original_format == 0) {
// if the packet is coming in original format, the sequence number is important
// otherwise, ignore is by setting it equal to the expected sequence number in ab_write
seqno = conn->ab_write;
}
if (conn->ab_write ==
seqno) { // if this is the expected packet (which could be the first packet...)
if (conn->input_frame_rate_starting_point_is_valid == 0) {
if ((conn->packet_count_since_flush >= 500) && (conn->packet_count_since_flush <= 510)) {
conn->frames_inward_measurement_start_time = time_now;
conn->frames_inward_frames_received_at_measurement_start_time = actual_timestamp;
conn->input_frame_rate_starting_point_is_valid = 1; // valid now
}
}
conn->frames_inward_measurement_time = time_now;
conn->frames_inward_frames_received_at_measurement_time = actual_timestamp;
abuf = conn->audio_buffer + BUFIDX(seqno);
conn->ab_write = seqno + 1; // move the write pointer to the next free space
} else if (is_after(conn->ab_write, seqno)) { // newer than expected
int32_t gap = seqno - conn->ab_write;
if (gap <= 0)
debug(1, "Unexpected gap size: %d.", gap);
int i;
for (i = 0; i < gap; i++) {
abuf = conn->audio_buffer + BUFIDX(conn->ab_write + i);
abuf->ready = 0; // to be sure, to be sure
abuf->resend_request_number = 0;
abuf->initialisation_time =
time_now; // this represents when the packet was noticed to be missing
abuf->status = 1 << 0; // signifying missing
abuf->resend_time = 0;
abuf->given_timestamp = 0;
abuf->sequence_number = 0;
}
abuf = conn->audio_buffer + BUFIDX(seqno);
// rtp_request_resend(ab_write, gap);
// resend_requests++;
conn->ab_write = seqno + 1;
} else if (is_after(conn->ab_read, seqno)) { // older than expected but not too late
conn->late_packets++;
abuf = conn->audio_buffer + BUFIDX(seqno);
} else { // too late.
conn->too_late_packets++;
}
if (abuf) {
int datalen = conn->max_frames_per_packet;
abuf->initialisation_time = time_now;
abuf->resend_time = 0;
if ((original_format != 0) &&
(audio_packet_decode(abuf->data, &datalen, data, len, conn) == 0)) {
abuf->ready = 1;
abuf->status = 0; // signifying that it was received
abuf->length = datalen;
abuf->given_timestamp = actual_timestamp;
abuf->sequence_number = seqno;
} else if (original_format == 0) {
memcpy(abuf->data, data, len * conn->input_bytes_per_frame);
abuf->ready = 1;
abuf->status = 0; // signifying that it was received
abuf->length = len;
abuf->given_timestamp = actual_timestamp;
abuf->sequence_number = seqno;
} else {
debug(1, "Bad audio packet detected and discarded.");
abuf->ready = 0;
abuf->status = 1 << 1; // bad packet, discarded
abuf->resend_request_number = 0;
abuf->given_timestamp = 0;
abuf->sequence_number = 0;
}
}
int rc = pthread_cond_signal(&conn->flowcontrol);
if (rc)
debug(1, "Error signalling flowcontrol.");
// resend checks
{
uint64_t minimum_wait_time =
(uint64_t)(config.resend_control_first_check_time * (uint64_t)1000000000);
uint64_t resend_repeat_interval =
(uint64_t)(config.resend_control_check_interval_time * (uint64_t)1000000000);
uint64_t minimum_remaining_time = (uint64_t)(
(config.resend_control_last_check_time + config.audio_backend_buffer_desired_length) *
(uint64_t)1000000000);
uint64_t latency_time = (uint64_t)(conn->latency * (uint64_t)1000000000);
latency_time = latency_time / (uint64_t)conn->input_rate;
// find the first frame that is missing, if known
int x = conn->ab_read;
if (first_possibly_missing_frame >= 0) {
// if it's within the range
int16_t buffer_size = conn->ab_write - conn->ab_read; // must be positive
if (buffer_size >= 0) {
int16_t position_in_buffer = first_possibly_missing_frame - conn->ab_read;
if ((position_in_buffer >= 0) && (position_in_buffer < buffer_size))
x = first_possibly_missing_frame;
}
}
first_possibly_missing_frame = -1; // has not been set
int missing_frame_run_count = 0;
int start_of_missing_frame_run = -1;
int number_of_missing_frames = 0;
while (x != conn->ab_write) {
abuf_t *check_buf = conn->audio_buffer + BUFIDX(x);
if (!check_buf->ready) {
if (first_possibly_missing_frame < 0)
first_possibly_missing_frame = x;
number_of_missing_frames++;
// debug(1, "frame %u's initialisation_time is 0x%" PRIx64 ", latency_time is 0x%"
// PRIx64 ", time_now is 0x%" PRIx64 ", minimum_remaining_time is 0x%" PRIx64 ".", x,
// check_buf->initialisation_time, latency_time, time_now, minimum_remaining_time);
int too_late = ((check_buf->initialisation_time < (time_now - latency_time)) ||
((check_buf->initialisation_time - (time_now - latency_time)) <
minimum_remaining_time));
int too_early = ((time_now - check_buf->initialisation_time) < minimum_wait_time);
int too_soon_after_last_request =
((check_buf->resend_time != 0) &&
((time_now - check_buf->resend_time) <
resend_repeat_interval)); // time_now can never be less than the time_tag
if (too_late)
check_buf->status |= 1 << 2; // too late
else
check_buf->status &= 0xFF - (1 << 2); // not too late
if (too_early)
check_buf->status |= 1 << 3; // too early
else
check_buf->status &= 0xFF - (1 << 3); // not too early
if (too_soon_after_last_request)
check_buf->status |= 1 << 4; // too soon after last request
else
check_buf->status &= 0xFF - (1 << 4); // not too soon after last request
if ((!too_soon_after_last_request) && (!too_late) && (!too_early)) {
if (start_of_missing_frame_run == -1) {
start_of_missing_frame_run = x;
missing_frame_run_count = 1;
} else {
missing_frame_run_count++;
}
check_buf->resend_time = time_now; // setting the time to now because we are
// definitely going to take action
check_buf->resend_request_number++;
debug(3, "Frame %d is missing with ab_read of %u and ab_write of %u.", x, conn->ab_read,
conn->ab_write);
}
// if (too_late) {
// debug(1,"too late to get missing frame %u.", x);
// }
}
// if (number_of_missing_frames != 0)
// debug(1,"check with x = %u, ab_read = %u, ab_write = %u, first_possibly_missing_frame
// = %d.", x, conn->ab_read, conn->ab_write, first_possibly_missing_frame);
x = (x + 1) & 0xffff;
if (((check_buf->ready) || (x == conn->ab_write)) && (missing_frame_run_count > 0)) {
// send a resend request
if (missing_frame_run_count > 1)
debug(3, "request resend of %d packets starting at seqno %u.", missing_frame_run_count,
start_of_missing_frame_run);
if (config.disable_resend_requests == 0) {
debug_mutex_unlock(&conn->ab_mutex, 3);
rtp_request_resend(start_of_missing_frame_run, missing_frame_run_count, conn);
debug_mutex_lock(&conn->ab_mutex, 20000, 1);
conn->resend_requests++;
}
start_of_missing_frame_run = -1;
missing_frame_run_count = 0;
}
}
if (number_of_missing_frames == 0)
first_possibly_missing_frame = conn->ab_write;
}
}
debug_mutex_unlock(&conn->ab_mutex, 0);
}
int32_t rand_in_range(int32_t exclusive_range_limit) {
static uint32_t lcg_prev = 12345;
// returns a pseudo random integer in the range 0 to (exclusive_range_limit-1) inclusive
int64_t sp = lcg_prev;
int64_t rl = exclusive_range_limit;
lcg_prev = lcg_prev * 69069 + 3; // crappy psrg
sp = sp * rl; // 64 bit calculation. Interesting part is above the 32 rightmost bits;
return sp >> 32;
}
static inline void process_sample(int32_t sample, char **outp, sps_format_t format, int volume,
int dither, rtsp_conn_info *conn) {
/*
{
static int old_volume = 0;
if (volume != old_volume) {
debug(1,"Volume is now %d.",volume);
old_volume = volume;
}
}
*/
int64_t hyper_sample = sample;
int result = 0;
if (config.loudness) {
hyper_sample <<=
32; // Do not apply volume as it has already been done with the Loudness DSP filter
} else {
int64_t hyper_volume = (int64_t)volume << 16;
hyper_sample = hyper_sample * hyper_volume; // this is 64 bit bit multiplication -- we may need
// to dither it down to its target resolution
}
// next, do dither, if necessary
if (dither) {
// add a TPDF dither -- see
// http://educypedia.karadimov.info/library/DitherExplained.pdf
// and the discussion around https://www.hydrogenaud.io/forums/index.php?showtopic=16963&st=25
// I think, for a 32 --> 16 bits, the range of
// random numbers needs to be from -2^16 to 2^16, i.e. from -65536 to 65536 inclusive, not from
// -32768 to +32767
// Actually, what would be generated here is from -65535 to 65535, i.e. one less on the limits.
// See the original paper at
// http://www.ece.rochester.edu/courses/ECE472/resources/Papers/Lipshitz_1992.pdf
// by Lipshitz, Wannamaker and Vanderkooy, 1992.
int64_t dither_mask = 0;
switch (format) {
case SPS_FORMAT_S32:
case SPS_FORMAT_S32_LE:
case SPS_FORMAT_S32_BE:
dither_mask = (int64_t)1 << (64 - 32);
break;
case SPS_FORMAT_S24:
case SPS_FORMAT_S24_LE:
case SPS_FORMAT_S24_BE:
case SPS_FORMAT_S24_3LE:
case SPS_FORMAT_S24_3BE:
dither_mask = (int64_t)1 << (64 - 24);
break;
case SPS_FORMAT_S16:
case SPS_FORMAT_S16_LE:
case SPS_FORMAT_S16_BE:
dither_mask = (int64_t)1 << (64 - 16);
break;
case SPS_FORMAT_S8:
case SPS_FORMAT_U8:
dither_mask = (int64_t)1 << (64 - 8);
break;
case SPS_FORMAT_UNKNOWN:
die("Unexpected SPS_FORMAT_UNKNOWN while calculating dither mask.");
break;
case SPS_FORMAT_AUTO:
die("Unexpected SPS_FORMAT_AUTO while calculating dither mask.");
break;
case SPS_FORMAT_INVALID:
die("Unexpected SPS_FORMAT_INVALID while calculating dither mask.");
break;
}
dither_mask -= 1;
int64_t r = r64i();
int64_t tpdf = (r & dither_mask) - (conn->previous_random_number & dither_mask);
conn->previous_random_number = r;
// add dither, allowing for clipping
if (tpdf >= 0) {
if (INT64_MAX - tpdf >= hyper_sample)
hyper_sample += tpdf;
else
hyper_sample = INT64_MAX;
} else {
if (INT64_MIN - tpdf <= hyper_sample)
hyper_sample += tpdf;
else
hyper_sample = INT64_MIN;
}
// dither is complete here
}
// move the result to the desired position in the int64_t
char *op = *outp;
uint8_t byt;
switch (format) {
case SPS_FORMAT_S32_LE:
hyper_sample >>= (64 - 32);
byt = (uint8_t)hyper_sample;
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 8);
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 16);
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 24);
*op++ = byt;
result = 4;
break;
case SPS_FORMAT_S32_BE:
hyper_sample >>= (64 - 32);
byt = (uint8_t)(hyper_sample >> 24);
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 16);
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 8);
*op++ = byt;
byt = (uint8_t)hyper_sample;
*op++ = byt;
result = 4;
break;
case SPS_FORMAT_S32:
hyper_sample >>= (64 - 32);
*(int32_t *)op = hyper_sample;
result = 4;
break;
case SPS_FORMAT_S24_3LE:
hyper_sample >>= (64 - 24);
byt = (uint8_t)hyper_sample;
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 8);
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 16);
*op++ = byt;
result = 3;
break;
case SPS_FORMAT_S24_3BE:
hyper_sample >>= (64 - 24);
byt = (uint8_t)(hyper_sample >> 16);
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 8);
*op++ = byt;
byt = (uint8_t)hyper_sample;
*op++ = byt;
result = 3;
break;
case SPS_FORMAT_S24_LE:
hyper_sample >>= (64 - 24);
byt = (uint8_t)hyper_sample;
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 8);
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 16);
*op++ = byt;
*op++ = 0;
result = 4;
break;
case SPS_FORMAT_S24_BE:
hyper_sample >>= (64 - 24);
*op++ = 0;
byt = (uint8_t)(hyper_sample >> 16);
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 8);
*op++ = byt;
byt = (uint8_t)hyper_sample;
*op++ = byt;
result = 4;
break;
case SPS_FORMAT_S24:
hyper_sample >>= (64 - 24);
*(int32_t *)op = hyper_sample;
result = 4;
break;
case SPS_FORMAT_S16_LE:
hyper_sample >>= (64 - 16);
byt = (uint8_t)hyper_sample;
*op++ = byt;
byt = (uint8_t)(hyper_sample >> 8);
*op++ = byt;
result = 2;
break;
case SPS_FORMAT_S16_BE:
hyper_sample >>= (64 - 16);
byt = (uint8_t)(hyper_sample >> 8);
*op++ = byt;
byt = (uint8_t)hyper_sample;
*op++ = byt;
result = 2;
break;
case SPS_FORMAT_S16:
hyper_sample >>= (64 - 16);
*(int16_t *)op = (int16_t)hyper_sample;
result = 2;
break;
case SPS_FORMAT_S8:
hyper_sample >>= (int8_t)(64 - 8);
*op = hyper_sample;
result = 1;
break;
case SPS_FORMAT_U8:
hyper_sample >>= (uint8_t)(64 - 8);
hyper_sample += 128;
*op = hyper_sample;
result = 1;
break;
case SPS_FORMAT_UNKNOWN:
die("Unexpected SPS_FORMAT_UNKNOWN while outputting samples");
break;
case SPS_FORMAT_AUTO:
die("Unexpected SPS_FORMAT_AUTO while outputting samples");
break;
case SPS_FORMAT_INVALID:
die("Unexpected SPS_FORMAT_INVALID while outputting samples");
break;
}
*outp += result;
}
void buffer_get_frame_cleanup_handler(void *arg) {
rtsp_conn_info *conn = (rtsp_conn_info *)arg;
debug_mutex_unlock(&conn->ab_mutex, 0);
}
// get the next frame, when available. return 0 if underrun/stream reset.
static abuf_t *buffer_get_frame(rtsp_conn_info *conn) {
// int16_t buf_fill;
uint64_t local_time_now;
// struct timespec tn;
abuf_t *curframe = NULL;
int notified_buffer_empty = 0; // diagnostic only
debug_mutex_lock(&conn->ab_mutex, 30000, 0);
int wait;
long dac_delay = 0; // long because alsa returns a long
int have_sent_prefiller_silence =
0; // set to true when we have sent at least one silent frame to the DAC
pthread_cleanup_push(buffer_get_frame_cleanup_handler,
(void *)conn); // undo what's been done so far
do {
pthread_testcancel(); // even if no packets are coming in...
// get the time
local_time_now = get_absolute_time_in_ns(); // type okay
// debug(3, "buffer_get_frame is iterating");
// we must have timing information before we can do anything here
if (have_timestamp_timing_information(conn)) {
int rco = get_requested_connection_state_to_output();
if (conn->connection_state_to_output != rco) {
conn->connection_state_to_output = rco;
// change happening
if (conn->connection_state_to_output == 0) { // going off
debug(2, "request flush because connection_state_to_output is off");
debug_mutex_lock(&conn->flush_mutex, 1000, 1);
conn->flush_requested = 1;
conn->flush_rtp_timestamp = 0;
debug_mutex_unlock(&conn->flush_mutex, 3);
}
}
if (config.output->is_running)
if (config.output->is_running() != 0) { // if the back end isn't running for any reason
debug(2, "request flush because back end is not running");
debug_mutex_lock(&conn->flush_mutex, 1000, 0);
conn->flush_requested = 1;
conn->flush_rtp_timestamp = 0;
debug_mutex_unlock(&conn->flush_mutex, 0);
}
debug_mutex_lock(&conn->flush_mutex, 1000, 0);
pthread_cleanup_push(mutex_unlock, &conn->flush_mutex);
if (conn->flush_requested == 1) {
if (conn->flush_output_flushed == 0)
if (config.output->flush) {
config.output->flush(); // no cancellation points
debug(2, "flush request: flush output device.");
}
conn->flush_output_flushed = 1;
}
// now check to see it the flush request is for frames in the buffer or not
// if the first_packet_timestamp is zero, don't check
int flush_needed = 0;
int drop_request = 0;
if (conn->flush_requested == 1) {
if (conn->flush_rtp_timestamp == 0) {
debug(1, "flush request: flush frame 0 -- flush assumed to be needed.");
flush_needed = 1;
drop_request = 1;
} else {
if ((conn->ab_synced) && ((conn->ab_write - conn->ab_read) > 0)) {
abuf_t *firstPacket = conn->audio_buffer + BUFIDX(conn->ab_read);
abuf_t *lastPacket = conn->audio_buffer + BUFIDX(conn->ab_write - 1);
if ((firstPacket != NULL) && (firstPacket->ready)) {
uint32_t first_frame_in_buffer = firstPacket->given_timestamp;
int32_t offset_from_first_frame = conn->flush_rtp_timestamp - first_frame_in_buffer;
if ((lastPacket != NULL) && (lastPacket->ready)) {
// we have enough information to check if the flush is needed or can be discarded
uint32_t last_frame_in_buffer =
lastPacket->given_timestamp + lastPacket->length - 1;
// clang-format off
// Now we have to work out if the flush frame is in the buffer.
// If it is later than the end of the buffer, flush everything and keep the
// request active.
// If it is in the buffer, we need to flush part of the buffer.
// (Actually we flush the entire buffer and drop the request.)
// If it is before the buffer, no flush is needed. Drop the request.
// clang-format on
if (offset_from_first_frame > 0) {
int32_t offset_to_last_frame = last_frame_in_buffer - conn->flush_rtp_timestamp;
if (offset_to_last_frame >= 0) {