forked from ralph-irving/squeezelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.c
978 lines (830 loc) · 25 KB
/
stream.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
/*
* Squeezelite - lightweight headless squeezebox emulator
*
* (c) Adrian Smith 2012-2015, triode1@btinternet.com
* Ralph Irving 2015-2024, ralph_irving@hotmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// stream thread
#define _GNU_SOURCE
#include "squeezelite.h"
#include <fcntl.h>
#if USE_SSL
#include "openssl/ssl.h"
#include "openssl/err.h"
#endif
#if SUN
#include <signal.h>
#endif
#if USE_LIBOGG
#include "ogg/ogg.h"
#endif
struct {
bool flac;
u64_t serial;
#if USE_LIBOGG
bool active;
ogg_stream_state state;
ogg_packet packet;
ogg_sync_state sync;
ogg_page page;
#if !LINKALL
struct {
void* handle;
int (*ogg_stream_init)(ogg_stream_state* os, int serialno);
int (*ogg_stream_clear)(ogg_stream_state* os);
int (*ogg_stream_reset_serialno)(ogg_stream_state* os, int serialno);
int (*ogg_stream_pagein)(ogg_stream_state* os, ogg_page* og);
int (*ogg_stream_packetout)(ogg_stream_state* os, ogg_packet* op);
int (*ogg_sync_clear)(ogg_sync_state* oy);
char* (*ogg_sync_buffer)(ogg_sync_state* oy, long size);
int (*ogg_sync_wrote)(ogg_sync_state* oy, long bytes);
ogg_int64_t(*ogg_page_granulepos)(const ogg_page* og);
int (*ogg_page_serialno)(const ogg_page* og);
int (*ogg_page_bos)(const ogg_page* og);
int (*ogg_sync_pageout)(ogg_sync_state* oy, ogg_page* og);
} dl;
#endif
#else
enum { STREAM_OGG_OFF, STREAM_OGG_SYNC, STREAM_OGG_HEADER, STREAM_OGG_SEGMENTS, STREAM_OGG_PAGE } state;
size_t want, miss, match;
u8_t* data, segments[255];
#pragma pack(push, 1)
struct {
char pattern[4];
u8_t version, type;
u64_t granule;
u32_t serial, page, checksum;
u8_t count;
} header;
#pragma pack(pop)
#endif
} ogg;
static log_level loglevel;
static struct buffer buf;
struct buffer *streambuf = &buf;
#define LOCK mutex_lock(streambuf->mutex)
#define UNLOCK mutex_unlock(streambuf->mutex)
#define PTR_U32(p) ((u32_t) (*(u32_t*)p))
static sockfd fd;
static struct sockaddr_in addr;
static char host[256];
static int header_mlen;
struct streamstate stream;
#if USE_LIBOGG
#if LINKALL
#define OG(h, fn, ...) (ogg_ ## fn)(__VA_ARGS__)
#else
#define OG(h, fn, ...) (h)->ogg_##fn(__VA_ARGS__)
#endif
#endif
#if USE_SSL
static SSL_CTX *SSLctx;
static SSL *ssl;
static bool ssl_error;
static int _last_error(void) {
if (!ssl) return last_error();
return ssl_error ? ECONNABORTED : ERROR_WOULDBLOCK;
}
static int _recv(int fd, void *buffer, size_t bytes, int options) {
int n;
if (!ssl) return recv(fd, buffer, bytes, options);
n = SSL_read(ssl, (u8_t*) buffer, bytes);
if (n <= 0) {
int err = SSL_get_error(ssl, n);
if (err == SSL_ERROR_ZERO_RETURN) return 0;
ssl_error = (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE);
}
return n;
}
static int _send(int fd, void *buffer, size_t bytes, int options) {
int n = 0;
int err;
if (!ssl) return send(fd, buffer, bytes, options);
do {
ERR_clear_error();
if ((n = SSL_write(ssl, (u8_t*) buffer, bytes)) >= 0) break;
err = SSL_get_error(ssl, n);
ssl_error = (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE);
} while (!ssl_error);
return n;
}
/*
can't mimic exactly poll as SSL is a real pain. Even if SSL_pending returns
0, there might be bytes to read but when select (poll) return > 0, there might
be no frame available. As well select (poll) < 0 does not mean that there is
no data pending
*/
static int _poll(struct pollfd *pollinfo, int timeout) {
if (!ssl) return poll(pollinfo, 1, timeout);
if (pollinfo->events & POLLIN && SSL_pending(ssl)) {
if (pollinfo->events & POLLOUT) poll(pollinfo, 1, 0);
pollinfo->revents = POLLIN;
return 1;
}
return poll(pollinfo, 1, timeout);
}
#else
#define _recv(fd, buf, n, opt) recv(fd, buf, n, opt)
#define _send(fd, buf, n, opt) send(fd, buf, n, opt)
#define _poll(pollinfo, timeout) poll(pollinfo, 1, timeout)
#define _last_error() last_error()
#endif // USE_SSL
static bool send_header(void) {
char *ptr = stream.header;
int len = stream.header_len;
unsigned try = 0;
ssize_t n;
int error;
while (len) {
n = _send(fd, ptr, len, MSG_NOSIGNAL);
if (n <= 0) {
error = _last_error();
#if WIN
if (n < 0 && (error == ERROR_WOULDBLOCK || error == WSAENOTCONN) && try < 10) {
#else
if (n < 0 && error == ERROR_WOULDBLOCK && try < 10) {
#endif
LOG_DEBUG("retrying (%d) writing to socket", ++try);
usleep(1000);
continue;
}
LOG_WARN("failed writing to socket: %s", strerror(last_error()));
stream.disconnect = LOCAL_DISCONNECT;
stream.state = DISCONNECT;
wake_controller();
return false;
}
LOG_SDEBUG("wrote %d bytes to socket", n);
ptr += n;
len -= n;
}
LOG_SDEBUG("wrote header");
return true;
}
static bool running = true;
static void _disconnect(stream_state state, disconnect_code disconnect) {
stream.state = state;
stream.disconnect = disconnect;
#if USE_LIBOGG
if (ogg.active) {
OG(&ogg.dl, stream_clear, &ogg.state);
OG(&ogg.dl, sync_clear, &ogg.sync);
}
#else
if (ogg.state == STREAM_OGG_PAGE && ogg.data) free(ogg.data);
ogg.data = NULL;
#endif
#if USE_SSL
if (ssl) {
SSL_shutdown(ssl);
SSL_free(ssl);
ssl = NULL;
}
#endif
closesocket(fd);
fd = -1;
wake_controller();
}
static int connect_socket(bool use_ssl) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
LOG_ERROR("failed to create socket");
return -1;
}
LOG_INFO("connecting to %s:%d", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
set_nonblock(sock);
set_nosigpipe(sock);
set_recvbufsize(sock);
if (connect_timeout(sock, (struct sockaddr *) &addr, sizeof(addr), 10) < 0) {
LOG_INFO("unable to connect to server");
closesocket(sock);
return -1;
}
#if USE_SSL
if (use_ssl) {
ssl = SSL_new(SSLctx);
SSL_set_fd(ssl, sock);
// add SNI
if (*host) SSL_set_tlsext_host_name(ssl, host);
while (1) {
int status, err = 0;
ERR_clear_error();
status = SSL_connect(ssl);
// successful negotiation
if (status == 1) break;
// error or non-blocking requires more time
if (status < 0) {
err = SSL_get_error(ssl, status);
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) {
usleep(1000);
continue;
}
}
LOG_WARN("unable to open SSL socket %d (%d)", status, err);
closesocket(sock);
SSL_free(ssl);
ssl = NULL;
return -1;
}
}
#endif
return sock;
}
static u32_t inline itohl(u32_t littlelong) {
#if SL_LITTLE_ENDIAN
return littlelong;
#else
return __builtin_bswap32(littlelong);
#endif
}
/* https://xiph.org/ogg/doc/framing.html
* https://xiph.org/flac/ogg_mapping.html
* https://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-610004.2 */
#if !USE_LIBOGG
static size_t memfind(const u8_t* haystack, size_t n, const char* needle, size_t len, size_t* offset) {
size_t i;
for (i = 0; i < n && *offset != len; i++) *offset = (haystack[i] == needle[*offset]) ? *offset + 1 : 0;
return i;
}
/* this mode is made to save memory and CPU by not calling ogg decoding function and never having
* full packets (as a vorbis_comment can have a very large artwork. It works only at the page
* level, which means there is a risk of missing the searched comment if they are not on the
* first page of the vorbis_comment packet... nothing is perfect */
static void stream_ogg(size_t n) {
if (ogg.state == STREAM_OGG_OFF) return;
u8_t* p = streambuf->writep;
while (n) {
size_t consumed = min(ogg.miss, n);
// copy as many bytes as possible and come back later if we don't have enough
if (ogg.data) {
memcpy(ogg.data + ogg.want - ogg.miss, p, consumed);
ogg.miss -= consumed;
if (ogg.miss) return;
}
// we have what we want, let's parse
switch (ogg.state) {
case STREAM_OGG_SYNC:
ogg.miss -= consumed;
if (consumed) break;
// we have to memorize position in case any of last 3 bytes match...
size_t pos = memfind(p, n, "OggS", 4, &ogg.match);
if (ogg.match == 4) {
consumed = pos - ogg.match;
ogg.state = STREAM_OGG_HEADER;
ogg.miss = ogg.want = sizeof(ogg.header);
ogg.data = (u8_t*) &ogg.header;
ogg.match = 0;
} else {
if (!ogg.match) LOG_INFO("no OggS at expected position %zu/%zu", pos, n);
return;
}
break;
case STREAM_OGG_HEADER:
if (!memcmp(ogg.header.pattern, "OggS", 4)) {
ogg.miss = ogg.want = ogg.header.count;
ogg.data = ogg.segments;
ogg.state = STREAM_OGG_SEGMENTS;
// granule and page are also in little endian but that does not matter
ogg.header.serial = itohl(ogg.header.serial);
} else {
ogg.state = STREAM_OGG_SYNC;
ogg.data = NULL;
}
break;
case STREAM_OGG_SEGMENTS:
// calculate size of page using lacing values
for (size_t i = 0; i < ogg.want; i++) ogg.miss += ogg.data[i];
ogg.want = ogg.miss;
// acquire serial number when we are looking for headers and hit a bos
if (ogg.serial == ULLONG_MAX && (ogg.header.type & 0x02)) ogg.serial = ogg.header.serial;
// we have overshot and missed header, reset serial number to restart search (O and -1 are le/be)
if (ogg.header.serial == ogg.serial && ogg.header.granule && ogg.header.granule != -1) ogg.serial = ULLONG_MAX;
// not our serial (the above protected us from granule > 0)
if (ogg.header.serial != ogg.serial) {
// otherwise, jump over data
ogg.state = STREAM_OGG_SYNC;
ogg.data = NULL;
} else {
ogg.state = STREAM_OGG_PAGE;
ogg.data = malloc(ogg.want);
}
break;
case STREAM_OGG_PAGE: {
char** tag = (char* []){ "\x3vorbis", "OpusTags", NULL };
size_t ofs = 0;
/* with OggFlac, we need the next page (packet) - VorbisComment is wrapped into a FLAC_METADATA
* and except with vorbis, comment packet starts a new page but even in vorbis, it won't span
* accross multiple pages */
if (ogg.flac) ofs = 4;
else if (!memcmp(ogg.data, "\x7f""FLAC", 5)) ogg.flac = true;
else for (size_t n = 0; *tag; tag++, ofs = 0) if ((ofs = memfind(ogg.data, ogg.want, *tag, strlen(*tag), &n)) && n == strlen(*tag)) break;
if (ofs) {
// u32:len,char[]:vendorId, u32:N, N x (u32:len,char[]:comment)
char* p = (char*) ogg.data + ofs;
p += itohl(PTR_U32(p)) + 4;
u32_t count = itohl(PTR_U32(p));
p += 4;
// LMS metadata format for Ogg is "Ogg", N x (u16:len,char[]:comment)
memcpy(stream.header, "Ogg", 3);
stream.header_len = 3;
for (u32_t len; count--; p += len) {
len = itohl(PTR_U32(p));
p += 4;
// only report what we use and don't overflow (network byte order)
if (!strncasecmp(p, "TITLE=", 6) || !strncasecmp(p, "ARTIST=", 7) || !strncasecmp(p, "ALBUM=", 6)) {
if (stream.header_len + len > MAX_HEADER) break;
stream.header[stream.header_len++] = len >> 8;
stream.header[stream.header_len++] = len;
memcpy(stream.header + stream.header_len, p, len);
stream.header_len += len;
LOG_INFO("metadata: %.*s", len, p);
}
}
ogg.flac = false;
ogg.serial = ULLONG_MAX;
stream.meta_send = true;
wake_controller();
LOG_INFO("metadata length: %u", stream.header_len - 3);
}
free(ogg.data);
ogg.data = NULL;
ogg.state = STREAM_OGG_SYNC;
break;
}
default:
break;
}
p += consumed;
n -= consumed;
}
}
#else
static void stream_ogg(size_t n) {
if (!ogg.active) return;
// fill sync buffer with all what we have
char* buffer = OG(&ogg.dl, sync_buffer, &ogg.sync, n);
memcpy(buffer, streambuf->writep, n);
OG(&ogg.dl, sync_wrote, &ogg.sync, n);
// extract a page from sync buffer
while (OG(&ogg.dl, sync_pageout, &ogg.sync, &ogg.page) > 0) {
uint32_t serial = OG(&ogg.dl, page_serialno, &ogg.page);
// set stream serialno if we wait for a new one (no multiplexed streams)
if (ogg.serial == ULLONG_MAX && OG(&ogg.dl, page_bos, &ogg.page)) {
ogg.serial = serial;
OG(&ogg.dl, stream_reset_serialno, &ogg.state, serial);
}
// if we overshot, restart searching for headers
int64_t granule = OG(&ogg.dl, page_granulepos, &ogg.page);
if (ogg.serial == serial && granule && granule != -1) ogg.serial = ULLONG_MAX;
// if we don't have a serial number or it's not us, don't bring page in to avoid build-up
if (ogg.serial != serial) continue;
// bring new page in (there should be one but multiplexed streams are not supported)
if (OG(&ogg.dl, stream_pagein, &ogg.state, &ogg.page)) continue;
// get a packet (there might be more than one in a page)
while (OG(&ogg.dl, stream_packetout, &ogg.state, &ogg.packet) > 0) {
size_t ofs = 0;
// if case of OggFlac, VorbisComment is a flac METADATA_BLOC as 2nd packet (4 bytes in)
if (ogg.flac) ofs = 4;
else if (!memcmp(ogg.packet.packet, "\x7f""FLAC", 5)) ogg.flac = true;
else for (char** tag = (char* []){ "\x3vorbis", "OpusTags", NULL }; *tag && !ofs; tag++) if (!memcmp(ogg.packet.packet, *tag, strlen(*tag))) ofs = strlen(*tag);
if (!ofs) continue;
// u32:len,char[]:vendorId, u32:N, N x (u32:len,char[]:comment)
char* p = (char*)ogg.packet.packet + ofs;
p += itohl(PTR_U32(p)) + 4;
u32_t count = itohl(PTR_U32(p));
p += 4;
// LMS metadata format for Ogg is "Ogg", N x (u16:len,char[]:comment)
memcpy(stream.header, "Ogg", 3);
stream.header_len = 3;
for (u32_t len; count--; p += len) {
len = itohl(PTR_U32(p));
p += 4;
// only report what we use and don't overflow (network byte order)
if (!strncasecmp(p, "TITLE=", 6) || !strncasecmp(p, "ARTIST=", 7) || !strncasecmp(p, "ALBUM=", 6)) {
if (stream.header_len + len > MAX_HEADER) break;
stream.header[stream.header_len++] = len >> 8;
stream.header[stream.header_len++] = len;
memcpy(stream.header + stream.header_len, p, len);
stream.header_len += len;
LOG_INFO("metadata: %.*s", len, p);
}
}
// ogg_packet_clear does not need to be called as metadata packets terminate a page
ogg.flac = false;
ogg.serial = ULLONG_MAX;
stream.meta_send = true;
wake_controller();
LOG_INFO("metadata length: %u", stream.header_len - 3);
// return as we might have more than one metadata set but we want the first one
return;
}
}
}
#endif
static void *stream_thread() {
while (running) {
struct pollfd pollinfo;
size_t space;
LOCK;
space = min(_buf_space(streambuf), _buf_cont_write(streambuf));
if (fd < 0 || !space || stream.state <= STREAMING_WAIT) {
UNLOCK;
usleep(100000);
continue;
}
if (stream.state == STREAMING_FILE) {
int n = read(fd, streambuf->writep, space);
if (n == 0) {
LOG_INFO("end of stream");
_disconnect(DISCONNECT, DISCONNECT_OK);
}
if (n > 0) {
_buf_inc_writep(streambuf, n);
stream.bytes += n;
LOG_SDEBUG("streambuf read %d bytes", n);
}
if (n < 0) {
LOG_WARN("error reading: %s", strerror(last_error()));
_disconnect(DISCONNECT, REMOTE_DISCONNECT);
}
UNLOCK;
continue;
} else {
pollinfo.fd = fd;
pollinfo.events = POLLIN;
if (stream.state == SEND_HEADERS) {
pollinfo.events |= POLLOUT;
}
}
UNLOCK;
if (_poll(&pollinfo, 100)) {
LOCK;
// check socket has not been closed while in poll
if (fd < 0) {
UNLOCK;
continue;
}
if ((pollinfo.revents & POLLOUT) && stream.state == SEND_HEADERS) {
if (send_header()) stream.state = RECV_HEADERS;
header_mlen = stream.header_len;
stream.header_len = 0;
UNLOCK;
continue;
}
if (pollinfo.revents & (POLLIN | POLLHUP)) {
// get response headers
if (stream.state == RECV_HEADERS) {
// read one byte at a time to catch end of header
char c;
static int endtok;
int n = _recv(fd, &c, 1, 0);
if (n <= 0) {
if (n < 0 && _last_error() == ERROR_WOULDBLOCK) {
UNLOCK;
continue;
}
LOG_INFO("error reading headers: %s", n ? strerror(last_error()) : "closed");
#if USE_SSL
if (!ssl && !stream.header_len) {
int sock;
closesocket(fd);
fd = -1;
stream.header_len = header_mlen;
LOG_INFO("now attempting with SSL");
// must be performed locked in case slimproto sends a disconnects
sock = connect_socket(true);
if (sock >= 0) {
fd = sock;
stream.state = SEND_HEADERS;
UNLOCK;
continue;
}
}
#endif
_disconnect(STOPPED, LOCAL_DISCONNECT);
UNLOCK;
continue;
}
*(stream.header + stream.header_len) = c;
stream.header_len++;
if (stream.header_len > MAX_HEADER - 1) {
LOG_ERROR("received headers too long: %u", stream.header_len);
_disconnect(DISCONNECT, LOCAL_DISCONNECT);
}
if (stream.header_len > 1 && (c == '\r' || c == '\n')) {
endtok++;
if (endtok == 4) {
*(stream.header + stream.header_len) = '\0';
LOG_INFO("headers: len: %d\n%s", stream.header_len, stream.header);
stream.state = stream.cont_wait ? STREAMING_WAIT : STREAMING_BUFFERING;
wake_controller();
}
} else {
endtok = 0;
}
UNLOCK;
continue;
}
// receive icy meta data
if (stream.meta_interval && stream.meta_next == 0) {
if (stream.meta_left == 0) {
// read meta length
u8_t c;
int n = _recv(fd, &c, 1, 0);
if (n <= 0) {
if (n < 0 && _last_error() == ERROR_WOULDBLOCK) {
UNLOCK;
continue;
}
LOG_INFO("error reading icy meta: %s", n ? strerror(last_error()) : "closed");
_disconnect(STOPPED, LOCAL_DISCONNECT);
UNLOCK;
continue;
}
stream.meta_left = 16 * c;
stream.header_len = 0; // amount of received meta data
// MAX_HEADER must be more than meta max of 16 * 255
}
if (stream.meta_left) {
int n = _recv(fd, stream.header + stream.header_len, stream.meta_left, 0);
if (n <= 0) {
if (n < 0 && _last_error() == ERROR_WOULDBLOCK) {
UNLOCK;
continue;
}
LOG_INFO("error reading icy meta: %s", n ? strerror(last_error()) : "closed");
_disconnect(STOPPED, LOCAL_DISCONNECT);
UNLOCK;
continue;
}
stream.meta_left -= n;
stream.header_len += n;
}
if (stream.meta_left == 0) {
if (stream.header_len) {
*(stream.header + stream.header_len) = '\0';
LOG_INFO("icy meta: len: %u\n%s", stream.header_len, stream.header);
stream.meta_send = true;
wake_controller();
}
stream.meta_next = stream.meta_interval;
UNLOCK;
continue;
}
// stream body into streambuf
} else {
int n;
int error;
space = min(_buf_space(streambuf), _buf_cont_write(streambuf));
if (stream.meta_interval) {
space = min(space, stream.meta_next);
}
n = _recv(fd, streambuf->writep, space, 0);
if (n == 0) {
LOG_INFO("end of stream (%u bytes)", stream.bytes);
_disconnect(DISCONNECT, DISCONNECT_OK);
}
if (n < 0) {
error = _last_error();
if (error != ERROR_WOULDBLOCK) {
LOG_INFO("error reading: %s (%d)", strerror(error), error);
_disconnect(DISCONNECT, REMOTE_DISCONNECT);
}
}
if (n > 0) {
stream_ogg(n);
_buf_inc_writep(streambuf, n);
stream.bytes += n;
if (stream.meta_interval) {
stream.meta_next -= n;
}
} else {
UNLOCK;
continue;
}
if (stream.state == STREAMING_BUFFERING && stream.bytes > stream.threshold) {
stream.state = STREAMING_HTTP;
wake_controller();
}
LOG_SDEBUG("streambuf read %d bytes", n);
}
}
UNLOCK;
} else {
LOG_SDEBUG("poll timeout");
}
}
#if USE_SSL
if (SSLctx) {
SSL_CTX_free(SSLctx);
}
#endif
return 0;
}
static thread_type thread;
void stream_init(log_level level, unsigned stream_buf_size) {
loglevel = level;
LOG_INFO("init stream");
LOG_DEBUG("streambuf size: %u", stream_buf_size);
buf_init(streambuf, stream_buf_size);
if (streambuf->buf == NULL) {
LOG_ERROR("unable to malloc buffer");
exit(1);
}
#if USE_LIBOGG && !LINKALL
ogg.dl.handle = dlopen(LIBOGG, RTLD_NOW);
if (!ogg.dl.handle) {
LOG_INFO("ogg dlerror: %s", dlerror());
}
ogg.dl.ogg_stream_init = dlsym(ogg.dl.handle, "ogg_stream_init");
ogg.dl.ogg_stream_clear = dlsym(ogg.dl.handle, "ogg_stream_clear");
ogg.dl.ogg_stream_reset_serialno = dlsym(ogg.dl.handle, "ogg_stream_reset_serialno");
ogg.dl.ogg_stream_pagein = dlsym(ogg.dl.handle, "ogg_stream_pagein");
ogg.dl.ogg_stream_packetout = dlsym(ogg.dl.handle, "ogg_stream_packetout");
ogg.dl.ogg_sync_clear = dlsym(ogg.dl.handle, "ogg_sync_clear");
ogg.dl.ogg_sync_buffer = dlsym(ogg.dl.handle, "ogg_sync_buffer");
ogg.dl.ogg_sync_wrote = dlsym(ogg.dl.handle, "ogg_sync_wrote");
ogg.dl.ogg_sync_pageout = dlsym(ogg.dl.handle, "ogg_sync_pageout");
ogg.dl.ogg_page_bos = dlsym(ogg.dl.handle, "ogg_page_bos");
ogg.dl.ogg_page_serialno = dlsym(ogg.dl.handle, "ogg_page_serialno");
ogg.dl.ogg_page_granulepos = dlsym(ogg.dl.handle, "ogg_page_granulepos");
#endif
#if USE_SSL
#if !LINKALL && !NO_SSLSYM
if (ssl_loaded) {
#endif
SSL_library_init();
SSLctx = SSL_CTX_new(SSLv23_client_method());
if (SSLctx == NULL) {
LOG_ERROR("unable to allocate SSL context");
exit(1);
}
SSL_CTX_set_options(SSLctx, SSL_OP_NO_SSLv2);
#if !LINKALL && !NO_SSLSYM
}
#endif
ssl = NULL;
#endif
#if SUN
signal(SIGPIPE, SIG_IGN); /* Force sockets to return -1 with EPIPE on pipe signal */
#endif
stream.state = STOPPED;
stream.header = malloc(MAX_HEADER);
*stream.header = '\0';
fd = -1;
#if LINUX || FREEBSD
touch_memory(streambuf->buf, streambuf->size);
#endif
#if LINUX || OSX || FREEBSD
pthread_attr_t attr;
pthread_attr_init(&attr);
#ifdef PTHREAD_STACK_MIN
pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + STREAM_THREAD_STACK_SIZE);
#endif
pthread_create(&thread, &attr, stream_thread, NULL);
pthread_attr_destroy(&attr);
#endif
#if WIN
thread = CreateThread(NULL, STREAM_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&stream_thread, NULL, 0, NULL);
#endif
}
void stream_close(void) {
LOG_INFO("close stream");
LOCK;
running = false;
UNLOCK;
#if LINUX || OSX || FREEBSD
pthread_join(thread, NULL);
#endif
free(stream.header);
buf_destroy(streambuf);
}
void stream_file(const char *header, size_t header_len, unsigned threshold) {
buf_flush(streambuf);
LOCK;
stream.header_len = header_len;
memcpy(stream.header, header, header_len);
*(stream.header+header_len) = '\0';
LOG_INFO("opening local file: %s", stream.header);
#if WIN
fd = open(stream.header, O_RDONLY | O_BINARY);
#else
fd = open(stream.header, O_RDONLY);
#endif
stream.state = STREAMING_FILE;
if (fd < 0) {
LOG_INFO("can't open file: %s", stream.header);
stream.state = DISCONNECT;
}
wake_controller();
stream.cont_wait = false;
stream.meta_interval = 0;
stream.meta_next = 0;
stream.meta_left = 0;
stream.meta_send = false;
stream.sent_headers = false;
stream.bytes = 0;
stream.threshold = threshold;
UNLOCK;
}
void stream_sock(u32_t ip, u16_t port, bool use_ssl, bool use_ogg, const char* header, size_t header_len, unsigned threshold, bool cont_wait) {
char* p;
int sock;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = ip;
addr.sin_port = port;
*host = '\0';
p = strcasestr(header, "Host:");
if (p) {
sscanf(p, "Host:%255s", host);
if ((p = strchr(host, ':')) != NULL) *p = '\0';
}
port = ntohs(port);
sock = connect_socket(use_ssl || port == 443);
// try one more time with plain socket
if (sock < 0 && port == 443 && !use_ssl) sock = connect_socket(false);
if (sock < 0) {
LOCK;
stream.state = DISCONNECT;
stream.disconnect = UNREACHABLE;
UNLOCK;
return;
}
buf_flush(streambuf);
LOCK;
fd = sock;
stream.state = SEND_HEADERS;
stream.cont_wait = cont_wait;
stream.meta_interval = 0;
stream.meta_next = 0;
stream.meta_left = 0;
stream.meta_send = false;
stream.header_len = header_len;
memcpy(stream.header, header, header_len);
*(stream.header + header_len) = '\0';
LOG_INFO("header: %s", stream.header);
stream.sent_headers = false;
stream.bytes = 0;
stream.threshold = threshold;
#if USE_LIBOGG
#if !LINKALL
ogg.active = use_ogg && ogg.dl.handle;
#else
ogg.active = use_ogg;
#endif
if (use_ogg) {
OG(&ogg.dl, stream_clear, &ogg.state);
OG(&ogg.dl, sync_clear, &ogg.sync);
OG(&ogg.dl, stream_init, &ogg.state, -1);
}
#else
ogg.miss = ogg.match = 0;
ogg.state = use_ogg ? STREAM_OGG_SYNC : STREAM_OGG_OFF;
#endif
ogg.flac = false;
ogg.serial = ULLONG_MAX;
UNLOCK;
}
bool stream_disconnect(void) {
bool disc = false;
LOCK;
#if USE_SSL
if (ssl) {
SSL_shutdown(ssl);
SSL_free(ssl);
ssl = NULL;
}
#endif
if (fd != -1) {
closesocket(fd);
fd = -1;
disc = true;
}
stream.state = STOPPED;
#if USE_LIBOGG
if (ogg.active) {
OG(&ogg.dl, stream_clear, &ogg.state);
OG(&ogg.dl, sync_clear, &ogg.sync);
}
#else
if (ogg.state == STREAM_OGG_PAGE && ogg.data) free(ogg.data);
ogg.data = NULL;
#endif
UNLOCK;
return disc;
}