forked from carsten-gross/ts2shout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts2shout.c
1336 lines (1255 loc) · 54.6 KB
/
ts2shout.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
/*
ts2shout.c
(C) Carsten Gross <carsten@siski.de> 2018, 2019
reworked from dvbshout.c written by
(C) Dave Chapman <dave@dchapman.com> 2001, 2002.
(C) Nicholas J Humfrey <njh@aelius.com> 2006.
Copyright notice:
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <poll.h>
#include <errno.h>
#include <stdarg.h>
#include <time.h>
#include <curl/curl.h>
#include "ts2shout.h"
#include "rds.h"
#define XSTR(s) STR(s)
#define STR(s) #s
int Interrupted=0; /* Playing interrupted by signal? */
int channel_count=0; /* Current listen channel count */
uint8_t shoutcast=1; /* Send shoutcast headers? */
uint8_t logformat=1; /* Apache compatible output format */
uint8_t cgi_mode=0; /* Are we running as CGI programme? This is set if there is QUERY_STRING set in the environment */
static const long int mb_conversion = 1024 * 1024;
ts2shout_channel_t *channel_map[MAX_PID_COUNT];
ts2shout_channel_t *channels[MAX_CHANNEL_COUNT];
/* Structure that keeps track of connected EIT frames */
section_aggregate_t *eit_table;
section_aggregate_t *sdt_table;
/* Global application structure */
programm_info_t *global_state;
static void signal_handler(int signum)
{
if (signum != SIGPIPE) {
Interrupted=signum;
}
signal(signum,signal_handler);
}
static void parse_args(int argc, char **argv)
{
/* TODO ... improve command line handling */
int i = 0;
for (i = 0; i < argc; i++) {
if (strcmp("noshout", argv[i]) == 0) {
shoutcast = 0;
}
if (strcmp("ac3", argv[i]) == 0) {
global_state->want_ac3 = 1;
}
if (strcmp("rds", argv[i]) == 0) {
global_state->prefer_rds = 1;
}
}
}
void cleanup_mpeg_string(char *string) {
uint32_t i = 0;
if (string == NULL)
return;
for (i = 0; i < strlen(string); i++) {
/* replace control character "LF/CR" by Space */
if ((unsigned char)string[i] == 0x8a) {
string[i] = 0x20;
}
}
return;
}
/* Output logmessage in apache errorlog compatible format */
void output_logmessage(const char *fmt, ... ) {
char s[STR_BUF_SIZE];
char current_time_fmt[STR_BUF_SIZE];
char current_time[STR_BUF_SIZE];
va_list argp;
struct timespec t;
clock_gettime(CLOCK_REALTIME, &t);
strftime(current_time_fmt, STR_BUF_SIZE, "%a %b %d %H:%M:%S.%%6.6d %Y", localtime(&t.tv_sec));
snprintf(current_time, STR_BUF_SIZE, current_time_fmt, (t.tv_nsec / 1000));
va_start(argp, fmt);
vsnprintf(s, STR_BUF_SIZE, fmt, argp);
va_end(argp);
if ( 1 == logformat ) {
fprintf(stderr, "[%s] [ts2shout:info] [pid %d] %s",
current_time, getpid(), s);
} else {
fprintf(stderr, "%s", s);
}
return;
}
static void ts_continuity_check( ts2shout_channel_t *chan, int ts_cc )
{
if (chan->continuity_count != ts_cc) {
// Only display an error after we gain sync
if (chan->synced) {
output_logmessage("ts_continuity_check: TS continuity error (pid: %d)\n", chan->pid );
chan->synced=0;
}
chan->continuity_count = ts_cc;
}
chan->continuity_count++;
if (chan->continuity_count >= 16)
chan->continuity_count = 0;
}
static void extract_pat_payload(unsigned char *pes_ptr, size_t pes_len, ts2shout_channel_t *chan, int start_of_pes ) {
unsigned char* start = NULL;
start = pes_ptr + start_of_pes;
#ifdef DEBUG
fprintf (stderr, "PAT: Found data, table 0x%2.2d (Section length %d), transport_stream_id %d, section %d, last section %d\n",
PAT_TABLE_ID(start),
PAT_SECTION_LENGTH(start),
PAT_TRANSPORT_STREAM_ID(start),
PAT_SECTION_NUMBER(start),
PAT_LAST_SECTION_NUMBER(start));
#endif
unsigned char* programmes = PAT_PROGRAMME_START(start);
/* Add PMT if not already there */
if (! channel_map[PAT_PROGRAMME_PMT(programmes)] ) {
if (crc32(start, PAT_SECTION_LENGTH(start) + 3) == 0) {
global_state->transport_stream_id = PAT_TRANSPORT_STREAM_ID(start);
output_logmessage("extract_pat_payload(): programme has transport_stream_id: %d\n", global_state->transport_stream_id );
add_channel(CHANNEL_TYPE_PMT, PAT_PROGRAMME_PMT(programmes));
// global_state->programm_id; PAT_TRANSPORT_STREAM_ID(start);
} else {
// fprintf (stderr, "PAT: crc32 does not match: calculated %d, expected 0\n", crc32(start, PAT_SECTION_LENGTH(start) + 3));
}
}
#ifdef DEBUG
fprintf (stderr, "PAT: Programme 1, MAP-PID %d\n", PAT_PROGRAMME_PMT(programmes));
#endif
}
/* Get info about an available media stream (we want mp1/mp2 or AC-3) */
static void add_payload_from_pmt(unsigned char *pmt_stream_info_offset, unsigned char *start) {
if (!global_state->want_ac3) {
if (PMT_STREAM_TYPE(pmt_stream_info_offset) == 0x03 /* MPEG 1 audio */
|| PMT_STREAM_TYPE(pmt_stream_info_offset) == 0x04 /* MPEG 2 audio */
|| PMT_STREAM_TYPE(pmt_stream_info_offset) == 0x0f /* MPEG 2 audio */) {
/* We found a mp1/mp2 media stream */
global_state->service_id = PMT_PROGRAM_NUMBER(start);
output_logmessage("add_payload_from_pmt(): Found mp1/mp2 audio stream in PID %d (service_id %d)\n",
PMT_PID(pmt_stream_info_offset),
global_state->service_id );
add_channel(CHANNEL_TYPE_PAYLOAD, PMT_PID(pmt_stream_info_offset));
global_state->payload_added = 1;
}
} else {
/* 0x06 private data (very likely AC-3), scan a maximum of 2 stream descriptors to try to get the AC-3 descriptor */
if ( PMT_STREAM_TYPE(pmt_stream_info_offset) == 0x06 ) {
unsigned char *first_stream_descriptor = PMT_FIRST_STREAM_DESCRIPTORP(pmt_stream_info_offset);
/* 0x6a AC-3 descriptor found? */
#ifdef DEBUG
fprintf(stderr, "private-stream found, first stream descriptor 0x%x\n", PMT_STREAM_DESCRIPTOR_TAG(first_stream_descriptor));
#endif
if (PMT_STREAM_DESCRIPTOR_TAG(first_stream_descriptor) != 0x6a) {
/* no, next */
unsigned char *second_stream_descriptor = first_stream_descriptor + PMT_NEXT_STREAM_DESCRIPTOROFF(first_stream_descriptor);
#ifdef DEBUG
fprintf(stderr, "private-stream found, second stream descriptor 0x%x, using offset %d\n", PMT_STREAM_DESCRIPTOR_TAG(second_stream_descriptor), PMT_NEXT_STREAM_DESCRIPTOROFF(first_stream_descriptor));
#endif
if (PMT_STREAM_DESCRIPTOR_TAG(second_stream_descriptor) == 0x6a) {
output_logmessage("add_payload_from_pmt(): Found AC-3 audio stream in PID %d\n", PMT_PID(pmt_stream_info_offset));
add_channel(CHANNEL_TYPE_PAYLOAD, PMT_PID(pmt_stream_info_offset));
global_state->payload_added = 1;
}
} else {
#ifdef DEBUG
fprintf(stderr, "private-stream found, first stream descriptor 0x%x\n", PMT_STREAM_DESCRIPTOR_TAG(first_stream_descriptor));
#endif
output_logmessage("add_payload_from_pmt(): Found AC-3 audio stream in PID %d\n", PMT_PID(pmt_stream_info_offset));
add_channel(CHANNEL_TYPE_PAYLOAD, PMT_PID(pmt_stream_info_offset));
global_state->payload_added = 1;
}
}
}
}
/* Get stream info out of the PMT (program map table). We are only interested for radio mp1/mp2 streams or
* alternativly for AC-3 (not done yet). */
static void extract_pmt_payload(unsigned char *pes_ptr, size_t pes_len, ts2shout_channel_t *chan, int start_of_pes ) {
unsigned char* start = NULL;
unsigned int found_streams_counter = 0;
/* Only check for possible streaming payload in PMT if not one is added yet */
if ( global_state->payload_added) {
return;
}
start = pes_ptr + start_of_pes;
#ifdef DEBUG
fprintf (stderr, "PMT: Found data, table 0x%2.2x (Section length %d), program number %d, section %d, last section %d, INFO Length %d\n",
PMT_TABLE_ID(start),
PMT_SECTION_LENGTH(start),
PMT_PROGRAM_NUMBER(start),
PMT_SECTION_NUMBER(start),
PMT_LAST_SECTION_NUMBER(start),
PMT_PROGRAMME_INFO_LENGTH(start));
#endif
/* Look into table 0x02 containing the PID to be read */
if ( PMT_TABLE_ID(start) == 2) {
/* Check crc32 to avoid checking it later on */
if (crc32(start, PAT_SECTION_LENGTH(start) + 3) != 0) {
output_logmessage("extract_pmt_payload: crc32 does not match %d found, 0 expected\n", crc32(start, PAT_SECTION_LENGTH(start) + 3));
return;
}
if (PMT_SECTION_NUMBER(start) == 0 && PMT_LAST_SECTION_NUMBER(start) == 0) {
unsigned char* pmt_stream_info_offset = PMT_DESCRIPTOR(start);
/* Search for stream description */
while ((pmt_stream_info_offset != NULL) && found_streams_counter < 3) {
if (! channel_map[PMT_PID(pmt_stream_info_offset)]) {
/* right transport_id? */
add_payload_from_pmt(pmt_stream_info_offset, start);
found_streams_counter += 1;
if (global_state->payload_added > 0) {
pmt_stream_info_offset = NULL;
global_state->service_id = PMT_PROGRAM_NUMBER(start);
pmt_stream_info_offset = NULL;
} else {
pmt_stream_info_offset = PMT_FIRST_STREAM_DESCRIPTORP(pmt_stream_info_offset) + PMT_INFO_LENGTH(pmt_stream_info_offset);
#ifdef DEBUG
fprintf(stderr, "PMT: Found other stream with PID %d, stream type %d\n", PMT_PID(pmt_stream_info_offset), PMT_STREAM_TYPE(pmt_stream_info_offset));
#endif
}
} else {
/* check next */
pmt_stream_info_offset = PMT_FIRST_STREAM_DESCRIPTORP(pmt_stream_info_offset) + PMT_INFO_LENGTH(pmt_stream_info_offset);
#ifdef DEBUG
fprintf(stderr, "PMT: Found other stream with PID %d, stream type %d\n", PMT_PID(pmt_stream_info_offset), PMT_STREAM_TYPE(pmt_stream_info_offset));
#endif
found_streams_counter += 1;
}
}
}
}
}
/* Sometimes more than one small table has already been collected in our
* aggregation buffer. This function simply grabs the next table in the buffer,
* returns it, and moves the rest of the buffer contents to the beginning of
* the buffer. This function can be called as often as needed, it returns 0 if
* there is nothing and 1 if the data was successfully extracted from the
* buffer. */
uint8_t fetch_next(section_aggregate_t* aggregation, uint8_t *buffer, uint8_t* size) {
uint16_t section_length = EIT_SECTION_LENGTH(aggregation->buffer);
if (section_length == 0
|| section_length + 3 > aggregation->offset
|| section_length + 3 > EIT_BUF_SIZE) {
/* not enough data in buffer */
*size = 0;
return 0;
}
if (crc32(aggregation->buffer, section_length + 3) != 0) {
/* not valid */
#ifdef DEBUG
fprintf(stderr, "fetch_next(): Found data, but crc32 is wrong %d!=0", crc32(aggregation->buffer, section_length + 3) );
#endif
*size = 0;
return 0;
}
#ifdef DEBUG
fprintf(stderr, "fetch_next(): Found valid data with size %d\n", section_length);
#endif
/* Copy into return buffer. If it's not given the function can be used to check for validity of buffer! */
if (buffer) {
memcpy(buffer, aggregation->buffer, section_length + 3);
*size = section_length;
/* move the rest around */
memmove(aggregation->buffer, aggregation->buffer + section_length + 3, EIT_BUF_SIZE - (section_length + 3));
aggregation->offset = aggregation->offset - (section_length + 3);
}
return 1;
}
/* * * * Collect a table (SDT, EIT) out of many MPEG frames * * *
* If an information block doesn't fit into an mpeg-ts frame it is continued in a next frame. The information is
* directly attached after the PID (TS_HEADER_SIZE = 4 Byte offset). It is possible that there is a multi-ts-frame continuation.
* Even worse: EIT frames are joined directly together. Even inbetween the MPEG TS frame. No stuffing bytes
* If mpeg packet says "payload start" the pointer in 4th byte tells where the table start is. This is quite burried in
* the documentation.
* For the time being we search for stuffing at the end of a mpeg table: stuffing bytes, then we assume the next mpeg packet starts over.
* But we respect the pointer anyway (because our access macro is quite failsafe).
* No stuffing bytes? We check the CRC32, if it is valid, the table is valid: The next one is attached directly.
* TODO Currently the implementation doesn't fetch all information reliably.
* This is an issue with mpeg ts stream provided by vdr.
* tvheadend prepares the mpeg ts stream in another way and we have no issues with it. */
uint8_t collect_continuation(section_aggregate_t* aggregation, unsigned char *pes_ptr, size_t pes_len, int start_of_pes, unsigned char* ts_full_frame, enum_channel_type type) {
/* start of the part of the packet we are interested in */
unsigned char* start = pes_ptr + start_of_pes;
/* We are currently aggregating mpeg packets for a continuated frame? */
if (aggregation->buffer_valid == 0 && aggregation->continuation > 0 ) { // && (TS_PACKET_PAYLOAD_START(ts_full_frame) == 0) ) {
/* yes */
#ifdef DEBUG
fprintf(stderr, "%s: continued frame: offset: %d, counter: %d, section_length: %d, payload_start: %d (Pointer: %d)\n",
channel_name(type), aggregation->offset, aggregation->counter, aggregation->section_length,
TS_PACKET_PAYLOAD_START(ts_full_frame), TS_PACKET_POINTER(ts_full_frame));
DumpHex(start, TS_PACKET_SIZE - TS_HEADER_SIZE - start_of_pes);
#endif
memcpy(aggregation->buffer + aggregation->offset, start, TS_PACKET_SIZE - TS_HEADER_SIZE - start_of_pes);
aggregation->offset += TS_PACKET_SIZE - TS_HEADER_SIZE - start_of_pes; /* Offset for next TS packet */
aggregation->counter += 1;
/* 4 = size of crc32 (not included in section length) */
if ((aggregation->offset - TS_HEADER_SIZE - start_of_pes - 4) >= aggregation->section_length ) {
/* Section is finished, finally */
aggregation->buffer_valid = 1;
aggregation->continuation = 0;
#ifdef DEBUG
fprintf(stderr, "%s: finished multi-frame table after offset %d, counter: %d\n",
channel_name(type), aggregation->offset, aggregation->counter);
#endif
/* Check wether there is some leftover */
aggregation->ob_used = aggregation->offset - aggregation->section_length;
/* Stuffing bytes, or size wrong? */
if ((start[TS_PACKET_SIZE - aggregation->ob_used + 3] == 0xff && start[TS_PACKET_SIZE - aggregation->ob_used + 4] == 0xff)
|| (aggregation->ob_used > TS_PACKET_SIZE )
) {
/* nothing */
aggregation->ob_used = 0;
} else {
/* The 4 is the crc32 that is not mentioned in section length */
memcpy(aggregation->offset_buffer, start + 3 + TS_PACKET_SIZE - TS_HEADER_SIZE - start_of_pes - aggregation->ob_used, aggregation->ob_used);
if (aggregation->ob_used < 4) {
aggregation->ob_used = 0;
} else {
aggregation->ob_used -= 3;
}
#ifdef DEBUG
fprintf(stderr, "Leftover in continued frame ....\n");
DumpHex(aggregation->offset_buffer, aggregation->ob_used);
#endif
}
return 1;
} else {
if ( TS_PACKET_PAYLOAD_START(ts_full_frame) == 1) {
#ifdef DEBUG
fprintf(stderr, "%s: I'm at offset %d, and a new payload start is there... interrupting\n", channel_name(type), aggregation->offset);
#endif
aggregation->buffer_valid = 0;
aggregation->continuation = 1;
aggregation->counter = 1;
aggregation->section_length = EIT_SECTION_LENGTH((start + TS_PACKET_POINTER(ts_full_frame)));
aggregation->offset = TS_PACKET_SIZE - start_of_pes - 4 - TS_PACKET_POINTER(ts_full_frame) ; /* Offset for next TS packet */
/* Copy first frame directly into buffer */
memcpy(aggregation->buffer, pes_ptr + start_of_pes + TS_PACKET_POINTER(ts_full_frame), TS_PACKET_SIZE - start_of_pes - TS_PACKET_POINTER(ts_full_frame) );
/* Packet is not finished yet, it cannot be handled now */
#ifdef DEBUG
fprintf (stderr, "%s: Found multi-frame-table 0x%2.2x (last table 0x%2.2x), Section-Length: %d, Section: %d, payload_start: %d (Pointer %d)\n",
channel_name(type), EIT_PACKET_TABLEID((start + TS_PACKET_POINTER(ts_full_frame))), EIT_PACKET_LAST_TABLEID((start + TS_PACKET_POINTER(ts_full_frame))),
EIT_SECTION_LENGTH((start + TS_PACKET_POINTER(ts_full_frame))), EIT_SECTION_NUMBER((start + TS_PACKET_POINTER(ts_full_frame))),
TS_PACKET_PAYLOAD_START(ts_full_frame), TS_PACKET_POINTER(ts_full_frame));
DumpHex(start, 183);
#endif
return 0;
}
// This should not happen, because the standard recommends a maximum size of ~4K */
if (aggregation->offset + TS_PACKET_SIZE - TS_HEADER_SIZE > EIT_BUF_SIZE - TS_PACKET_SIZE ) {
output_logmessage("collect_continuation: Maximum Data-Chunk Size of %d characters " \
"exceeded by MPEG-Transport-Stream. Did read %d continued packets (%d bytes)\n",
EIT_BUF_SIZE - TS_PACKET_SIZE, aggregation->counter, aggregation->offset);
// reset internal buffer
memset(aggregation, 0, sizeof(section_aggregate_t));
}
return 0;
}
/* Move start of packet to helper buffer for long frames */
}
if ( aggregation->buffer_valid == 0) {
/* Check wether we have a *valid* leftover from last TS packet */
if ( aggregation->continuation == 0 ) {
if (aggregation->ob_used > 5) {
uint8_t size = 0;
/* special operation */
/* Just assume that this frame is longer then an mpeg frame ... */
aggregation->continuation = 1;
aggregation->counter = 1;
aggregation->section_length = EIT_SECTION_LENGTH(aggregation->offset_buffer);
aggregation->offset = aggregation->ob_used;
memcpy(aggregation->buffer, aggregation->offset_buffer, aggregation->ob_used);
aggregation->ob_used = 0;
/* Copy the rest of the mpeg frame */
memcpy(aggregation->buffer + aggregation->offset, pes_ptr + start_of_pes, TS_PACKET_SIZE - TS_HEADER_SIZE - start_of_pes );
aggregation->offset += TS_PACKET_SIZE - TS_HEADER_SIZE - start_of_pes;
#ifdef DEBUG
fprintf (stderr, "%s: Found continued multi-frame-table from last frame 0x%2.2x (data %d byte), Section-Length: %d, Section: %d\n",
channel_name(type), EIT_PACKET_TABLEID(start), aggregation->offset, EIT_SECTION_LENGTH(aggregation->buffer),
EIT_SECTION_NUMBER(aggregation->buffer));
DumpHex(aggregation->buffer, aggregation->offset);
#endif
/* Returns 0 on valid frame, 1 on success */
return fetch_next(aggregation, NULL, &size);
}
}
/* A short EIT frame < TS_PACKET_SIZE length, take care of the crc32 (not included in section_length)! */
if ( aggregation->continuation == 0 && EIT_SECTION_LENGTH(start) < (TS_PACKET_SIZE - TS_HEADER_SIZE - 4 ) ) {
aggregation->buffer_valid = 1;
aggregation->continuation = 0;
aggregation->counter = 1;
aggregation->section_length = EIT_SECTION_LENGTH(start);
aggregation->offset = 0;
memcpy(aggregation->buffer, start, TS_PACKET_SIZE - start_of_pes );
#ifdef DEBUG
fprintf (stderr, "%s: Single frame-table 0x%2.2x (last table 0x%2.2x), Section-Length: %d (%d), Section: %d, payload_start: %d (Pointer: %d)\n",
channel_name(type), EIT_PACKET_TABLEID(start), EIT_PACKET_LAST_TABLEID(start), EIT_SECTION_LENGTH(start), TS_PACKET_SIZE - start_of_pes, EIT_SECTION_NUMBER(start),
TS_PACKET_PAYLOAD_START(ts_full_frame), TS_PACKET_POINTER(ts_full_frame));
#endif
} else if ( ( EIT_SECTION_LENGTH((start + TS_PACKET_POINTER(ts_full_frame))) >= (TS_PACKET_SIZE - TS_HEADER_SIZE - 4))
&& ( 0 == aggregation->continuation )
&& (TS_PACKET_PAYLOAD_START(ts_full_frame) == 1) ) {
/* Start of a long frame, will be continued in next frame */
aggregation->buffer_valid = 0; /* It's not valid @ the moment */
aggregation->continuation = 1;
aggregation->counter = 1;
aggregation->section_length = EIT_SECTION_LENGTH((start + TS_PACKET_POINTER(ts_full_frame)));
aggregation->offset = TS_PACKET_SIZE - TS_HEADER_SIZE - start_of_pes - TS_PACKET_POINTER(ts_full_frame); /* Offset for next TS packet */
/* Copy first frame directly into buffer */
memcpy(aggregation->buffer, pes_ptr + start_of_pes + TS_PACKET_POINTER(ts_full_frame), TS_PACKET_SIZE - TS_HEADER_SIZE - start_of_pes - TS_PACKET_POINTER(ts_full_frame));
/* Packet is not finished yet, it cannot be handled now */
#ifdef DEBUG
fprintf (stderr, "%s: Found multi-frame-table 0x%2.2x (data in first packet %d), Section-Length: %d, Section: %d, payload_start: %d (Pointer: %d)\n",
channel_name(type), EIT_PACKET_TABLEID((start + TS_PACKET_POINTER(ts_full_frame))),
TS_PACKET_SIZE - TS_HEADER_SIZE - start_of_pes - TS_PACKET_POINTER(ts_full_frame),
EIT_SECTION_LENGTH((start + TS_PACKET_POINTER(ts_full_frame))), EIT_SECTION_NUMBER((start+ TS_PACKET_POINTER(ts_full_frame))),
TS_PACKET_PAYLOAD_START(ts_full_frame), TS_PACKET_POINTER(ts_full_frame));
DumpHex(start + TS_PACKET_POINTER(ts_full_frame), TS_PACKET_SIZE - TS_HEADER_SIZE - start_of_pes - TS_PACKET_POINTER(ts_full_frame));
#endif
return 0;
}
return 1;
} else {
return 1;
}
return 0;
}
static void extract_sdt_payload(unsigned char *pes_ptr, size_t pes_len, ts2shout_channel_t *chan, int start_of_pes, unsigned char* ts_full_frame) {
unsigned char* start = NULL;
start = pes_ptr + start_of_pes;
/* collect up continuation frames */
if (! collect_continuation(sdt_table, pes_ptr, pes_len, start_of_pes, ts_full_frame, CHANNEL_TYPE_SDT)) {
return;
}
/* SDT can use most of the PMT stuff */
if (! sdt_table->buffer_valid) {
return;
}
start = sdt_table->buffer;
#ifdef DEBUG
fprintf (stderr, "SDT: Found data, table 0x%2.2x (Section length %d), program number %d, section %d, last section %d\n",
PMT_TABLE_ID(start),
PMT_SECTION_LENGTH(start),
PMT_PROGRAM_NUMBER(start),
PMT_SECTION_NUMBER(start),
PMT_LAST_SECTION_NUMBER(start));
DumpHex(start, 183);
#endif
unsigned char * description_offset = SDT_FIRST_DESCRIPTOR(start);
#ifdef DEBUG
if (1) {
#else
if (global_state->sdt_fromstream == 0) {
#endif
if (crc32(start, sdt_table->section_length + 3) != 0) {
#ifdef DEBUG
output_logmessage("SDT: crc32 does not match, calculated %d, expected 0\n", crc32(start, sdt_table->section_length + 3));
#endif
} else {
if ( PMT_TABLE_ID(start) == 0x42 ) {
/* Table 0x42 contains information about current stream, we only want programm "running" (see mpeg standard for this hardcoded stuff) */
while (description_offset < (SDT_FIRST_DESCRIPTOR(start) + PMT_SECTION_LENGTH(start))) {
if(SDT_DESCRIPTOR_SERVICE_ID(description_offset) != global_state->service_id) {
#ifdef DEBUG
fprintf(stderr, "SDT: SDT service %d doesn't match global service id (%d)\n", SDT_DESCRIPTOR_SERVICE_ID(description_offset), global_state->service_id);
// DumpHex(description_offset, 188);
// fprintf(stderr, "----------------\n");
#endif
} else {
if (SDT_DESCRIPTOR_RUNNING(description_offset) == 0x4) {
unsigned char* description_content = SDT_DESCRIPTOR_CONTENT(description_offset);
char provider_name[STR_BUF_SIZE];
char service_name[STR_BUF_SIZE];
uint8_t service_name_length = description_content[SDT_DC_PROVIDER_NAME_LENGTH(description_content) + 4];
unsigned char * tmp = description_content + SDT_DC_PROVIDER_NAME_LENGTH(description_content) + 5;
/* Service 0x02, 0x0A, 0x07: (Digital) Radio */
if (SDT_DC_SERVICE_TYPE(description_content) == 0x2
|| SDT_DC_SERVICE_TYPE(description_content) == 0x0a
|| SDT_DC_SERVICE_TYPE(description_content) == 0x07 ) {
if (SDT_DC_PROVIDER_NAME_LENGTH(description_content) < STR_BUF_SIZE) {
if (SDT_DC_PROVIDER_NAME(description_content)[0] < 0x20) {
/* MPEG Standard has very sophisticated charset encoding, therefore a simple Hack for my setup */
snprintf(provider_name, SDT_DC_PROVIDER_NAME_LENGTH(description_content), "%s", SDT_DC_PROVIDER_NAME(description_content + 1) );
} else {
snprintf(provider_name, SDT_DC_PROVIDER_NAME_LENGTH(description_content) + 1, "%s", SDT_DC_PROVIDER_NAME(description_content));
}
}
/* like above, but written compacted, if first character is smaller 0x20 it's the charset encoding */
snprintf(service_name, service_name_length + (tmp[0]< 0x20?0:1), "%s", tmp + (tmp[0]< 0x20?1:0));
/* Sometime we get garbage only store if we have a service_name with length > 0 */
if (strlen(service_name) > 0) {
/* service name is latin1 in most cases */
unsigned char utf8_service_name[STR_BUF_SIZE];
utf8((unsigned char*)service_name, utf8_service_name);
/* Yes, we want to get information about the programme */
output_logmessage("SDT: Stream is station %s from network %s.\n", utf8_service_name, provider_name);
strncpy(global_state->station_name, service_name, STR_BUF_SIZE);
global_state->sdt_fromstream = 1;
break; /* leave while loop */
}
} else {
/* If service type is 0xff it's very likely just a stuffing frame without any content */
if (SDT_DC_SERVICE_TYPE(description_content) != 0xff) {
output_logmessage("SDT: Warning: Stream (also) contains unkown service with id 0x%2x\n", SDT_DC_SERVICE_TYPE(description_content));
}
}
}
}
description_offset = description_offset + SDT_DESCRIPTOR_LOOP_LENGTH(description_offset) + 5;
} /* end while */
}
}
}
sdt_table->buffer_valid = 0;
return;
}
static void extract_eit_payload(unsigned char *pes_ptr, size_t pes_len, ts2shout_channel_t *chan, int start_of_pes, unsigned char* ts_full_frame )
{
if (global_state->found_rds > 0) {
return;
}
unsigned char* start = NULL;
//uint8_t buffer[EIT_BUF_SIZE];
//uint8_t size = 0;
char short_description[STR_BUF_SIZE];
char text_description[STR_BUF_SIZE];
memset(short_description, 0, STR_BUF_SIZE);
memset(text_description, 0, STR_BUF_SIZE);
start = pes_ptr + start_of_pes;
/* collect up continuation frames */
if (! collect_continuation(eit_table, pes_ptr, pes_len, start_of_pes, ts_full_frame, CHANNEL_TYPE_EIT)) {
return;
}
start = eit_table->buffer;
if (eit_table->buffer_valid) {
#ifdef DEBUG
fprintf(stderr, "EIT: crc32 %s (%d, l: %d)\n",( crc32(start, EIT_SECTION_LENGTH(start)+3)== 0?"OK":"FAIL"), crc32(start, EIT_SECTION_LENGTH(start)+3), EIT_SECTION_LENGTH(start)+3);
DumpHex(start, EIT_SECTION_LENGTH(start));
#endif
}
if (crc32(start, EIT_SECTION_LENGTH(start)+3)!= 0) {
/* crc32 not valid, throw away continued data */
eit_table->ob_used = 0;
eit_table->buffer_valid = 0;
return;
}
/* 0x4e current_event table */
if (eit_table->buffer_valid == 1 && 0x4e == EIT_PACKET_TABLEID(start)) {
/* Current programme found */
unsigned char* event_start = EIT_PACKET_EVENTSP(start);
unsigned char* description_start = EIT_EVENT_DESCRIPTORP(event_start);
unsigned int service_id = EIT_SERVICE_ID(start);
if ( service_id != global_state->service_id ) {
#ifdef DEBUG
fprintf(stderr, "EIT: service_id %d is wrong (%d)\n", service_id, global_state->service_id);
#endif
eit_table->buffer_valid = 0;
return;
}
#ifdef DEBUG
unsigned char* first_description_start = EIT_EVENT_DESCRIPTORP(event_start);
fprintf(stderr, "EIT: Found event with id %d, currently in status %d, starttime %6.6x, duration %6.6x, Section: %d (V:%d) from %d (Length: %d).\n",
EIT_EVENT_EVENTID(event_start),
EIT_EVENT_RUNNING_STATUS(event_start),
EIT_EVENT_STARTTIME_TIME(event_start),
EIT_EVENT_DURATION(event_start),
EIT_SECTION_NUMBER(start),
EIT_VERSION_NUMBER(start),
EIT_LAST_SECTION_NUMBER(start),
EIT_SECTION_LENGTH(start));
#endif
/* 0x4d = Short event descriptor found, transport_stream matches */
if (EIT_DESCRIPTOR_TAG(description_start) == 0x4d && EIT_TRANSPORT_STREAM_ID(start) == global_state->transport_stream_id ) {
/* Now calculate crc32, because we want to do something with the data */
if (crc32(start, EIT_SECTION_LENGTH(start)+3) != 0) {
#ifdef DEBUG
fprintf(stderr, "EIT: crc32 does not match, calculated %d, expected 0, using section length: %d\n", crc32(start, EIT_SECTION_LENGTH(start)+3), EIT_SECTION_LENGTH(start)+3);
#endif
eit_table->buffer_valid = 0;
return;
}
//fprintf(stderr, "EIT: Dumping full buffer .. \n");
// write(2, start, eit_table->section_length);
// fprintf(stderr, "\n");
/* Step through the event descriptions */
uint16_t current_in_position = 0;
uint16_t current_out_position = 0;
uint16_t max_size = EIT_EVENT_LOOPLENGTH(event_start);
unsigned int stringlen = 0;
unsigned char* text1_start = NULL;
int text1_len = 0;
uint8_t * tmp = EIT_NAME_CONTENT(description_start);
/* The same charset issue as with the SDT */
stringlen = EIT_NAME_LENGTH(description_start) + (tmp[0] < 0x20? 0 : 1);
snprintf(short_description, stringlen, "%s", EIT_NAME_CONTENT(description_start) + (tmp[0] < 0x20? 1 : 0) );
while (current_in_position + 60 <= max_size && current_in_position < eit_table->section_length) {
stringlen = EIT_NAME_LENGTH(description_start);
text1_start = description_start + EIT_SIZE_DESCRIPTOR_HEADER + stringlen;
text1_len = text1_start[0];
if (text1_len == 0 || text1_len > max_size) break;
/* Avoid the character code marker 0x05 if it's a latin1 text */
memcpy(text_description + current_out_position, text1_start + (text1_start[1] < 0x20 ? 2 : 1), text1_len - ((text1_start[1] < 0x20) ? 1 : 1));
/* First round? */
if (current_out_position == 0 && ( 70 < max_size) ) {
/* Hack, if the overall text is very short, no "~" */
strcpy(text_description + current_out_position + text1_len - 1, " ~ ");
memset(text_description + current_out_position + text1_len + 3 - 1, 0, 1);
current_out_position += text1_len + 3 - 1;
} else {
memset(text_description + current_out_position + text1_len - 1, 0, 1);
current_out_position += text1_len - 1;
}
#ifdef DEBUG
fprintf(stderr, "DEBUG: text1: %s, in_pos: %d (%d), text1_len: %d, global_len: %d\n", text_description, current_in_position,
current_in_position + text1_len + EIT_SIZE_DESCRIPTOR_HEADER + stringlen, text1_len, max_size);
#endif
description_start = description_start + EIT_SIZE_DESCRIPTOR_HEADER + stringlen + 1 + text1_len + 1;
current_in_position += EIT_SIZE_DESCRIPTOR_HEADER + stringlen + 1 + text1_len + 1;
}
/* Replace control characters */
cleanup_mpeg_string(text_description);
if ( EIT_EVENT_RUNNING_STATUS(event_start) == 4) {
char tmp_title[STR_BUF_SIZE];
#ifdef DEBUG
fprintf(stderr, "EIT: RUNNING EVENT: %s (%s) in language %3.3s found.\n", short_description, text_description, EIT_DESCRIPTOR_LANG(first_description_start));
#endif /* Write full info into channel title, but only if there is a difference between text and short description */
if (text_description != NULL && strlen(text_description) > 0 ) {
snprintf(tmp_title, STR_BUF_SIZE - 1, "%s - %s", short_description, text_description);
} else {
/* Sonst nur short description */
snprintf(tmp_title, STR_BUF_SIZE - 1, "%s", short_description);
}
if (0 != strcmp(tmp_title, global_state->stream_title)) {
// It's needed in iso8859-1 for StreamTitle, but in UTF-8 for logging
unsigned char utf8_message[STR_BUF_SIZE];
strcpy(global_state->stream_title, tmp_title);
output_logmessage("EIT: Current transmission `%s'\n", utf8((unsigned char*)short_description, utf8_message));
}
} else {
#ifdef DEBUG
fprintf(stderr, "EIT: Event %s in Language %3.3s found.\n", short_description, EIT_DESCRIPTOR_LANG(first_description_start));
#endif
}
}
}
eit_table->buffer_valid = 0;
return;
}
int32_t extract_pes_payload( unsigned char *pes_ptr, size_t pes_len, ts2shout_channel_t *chan, int start_of_pes )
{
unsigned char* es_ptr=NULL;
size_t es_len=0;
int32_t bytes_written = 0;
// Start of a PES header?
if ( start_of_pes ) {
// Parse the PES header
es_ptr = parse_pes( pes_ptr, pes_len, &es_len, chan );
} else if (chan->pes_stream_id) {
// Don't output any data until we have seen a PES header
es_ptr = pes_ptr;
es_len = pes_len;
// Are we are the end of the PES packet?
if (es_len>chan->pes_remaining) {
es_len=chan->pes_remaining;
}
}
// Subtract the amount remaining in current PES packet
chan->pes_remaining -= es_len;
// Got some data to write out?
if (es_ptr) {
// Scan through Elementary Stream (ES)
// and try and find MPEG audio stream header
while (!chan->synced && es_len>= 6) {
// Valid header?
// MPEG1/2
if (chan->pes_stream_id >= 0xc0) {
if (mpa_header_parse(es_ptr, &chan->mpah)) {
// Now we know bitrate etc, set things up
// output_logmessage("Synced to MP1/MP2 audio in PID %d stream: 0x%x\n", chan->pid, chan->pes_stream_id );
mpa_header_print( &chan->mpah );
chan->synced = 1;
chan->payload_size = 2048;
}
} else {
if (ac3_header_parse(es_ptr, &chan->mpah)) {
// output_logmessage("Synced to AC-3 audio in PID %d stream: 0x%x\n", chan->pid, chan->pes_stream_id );
ac3_header_print( &chan->mpah );
chan->synced = 1;
chan->payload_size = 2048;
}
}
if (chan->synced) {
// Allocate buffer to store packet in
chan->buf_size = chan->payload_size + TS_PACKET_SIZE;
chan->buf = realloc( chan->buf, chan->buf_size + 4 );
if (chan->buf==NULL) {
output_logmessage("Error: Failed to allocate memory for MPEG Audio buffer\n");
exit(-1);
}
chan->buf_ptr = chan->buf;
// Initialise the RTP TS to the PES TS
} else {
// Skip byte
es_len--;
es_ptr++;
}
}
// If stream is synced then put data info buffer
if (chan->synced && global_state->output_payload) {
// Check that there is space
if (chan->buf_used + es_len > chan->buf_size) {
output_logmessage("Error: MPEG Audio buffer overflow\n" );
exit(-1);
}
// Copy data into the buffer
memcpy( chan->buf_ptr + chan->buf_used, es_ptr, es_len);
chan->buf_used += es_len;
}
}
/* Okay, actually this doesn't fit very well here, but we want to update the
* cache only if the payload channel is in sync, and we have the SDT ready.
* we have easy access to the sync data here, therefore we access it
* here - Perhaps we can move this elsewhere TODO */
if ((!global_state->cache_written) &&
cgi_mode &&
global_state->sdt_fromstream &&
chan->synced) {
add_cache(global_state);
global_state->cache_written = 1;
}
// every time the buffer is full scan for RDS data. Hopefully we get the RDS data
if (chan->buf_used > chan->payload_size && global_state->output_payload) {
rds_data_scan(chan);
}
// Got enough to send packet and we are allowed to output data
if (chan->buf_used > chan->payload_size && global_state->output_payload ) {
#ifndef DEBUG
/* If Icy-MetaData is set to 1 a shoutcast StreamTitle is required all 8192 */
/* (SHOUTCAST_METAINT) Bytes */
/* see documentation: https://cast.readme.io/docs/icy */
if (shoutcast) {
if (chan->payload_size + chan->bytes_written_nt <= SHOUTCAST_METAINT) {
if (chan->payload_size > 0) {
if (! fwrite((char*)chan->buf, chan->payload_size, 1, stdout) ) {
if ( ferror(stdout)) {
output_logmessage("write_streamdata: Error during write: %s, Exiting.\n", strerror(errno));
/* Not ready */
return -1;
} else {
output_logmessage("write_streamdata: EOF on STDOUT(?) during write.\n");
return -1;
}
}
chan->bytes_written_nt += chan->payload_size;
bytes_written += chan->payload_size;
}
} else {
uint32_t first_write = SHOUTCAST_METAINT - chan->bytes_written_nt;
uint32_t second_write = chan->payload_size - first_write;
uint16_t bytes = 0;
uint16_t written = 0;
char streamtitle[STR_BUF_SIZE];
/* Only output StreamTitle if it's different or for the first time */
if (strcmp(global_state->stream_title, global_state->old_stream_title) != 0) {
/* Maximum of 2000 characters! */
snprintf(streamtitle, STR_BUF_SIZE - 1, "StreamTitle='%.2000s';", global_state->stream_title);
strcpy(global_state->old_stream_title, global_state->stream_title);
} else {
memset(streamtitle, 0, strlen(global_state->stream_title) + 1);
}
bytes = ((strlen(streamtitle))>>4) + (strlen(streamtitle) > 0?1:0);
/* Shift right by 4 bit => Divide by 16, and add 1 to get minimum possible length.
* Add 1 only, if size is > 0, otherwise there is no metadata and therefore nothing to send */
if (first_write > 0) {
if (! fwrite((char*)chan->buf, first_write, 1, stdout)) {
if (ferror(stdout)) {
output_logmessage("write_streamdata: Error during write: %s, Exiting.\n", strerror(errno));
} else {
output_logmessage("write_streamdata: Error or EOF on STDOUT(?) during write.\n");
}
return -1;
}
bytes_written += first_write;
chan->bytes_written_nt += first_write;
}
fwrite(&bytes, 1, 1, stdout);
fwrite(streamtitle, bytes<<4, 1, stdout);
bytes_written += 1;
bytes_written += bytes<<4;
if (second_write > 0) {
if (! fwrite((char*)(chan->buf + first_write), second_write, 1, stdout) ) {
if (ferror(stdout)) {
output_logmessage("write_streamdata: Error during write: %s, Exiting.\n", strerror(errno));
} else {
output_logmessage("write_streamdata: Error or EOF on STDOUT(?) during write.\n");
}
return -1;
}
written = second_write;
bytes_written += written;
/* Reset the Shoutcastcounter */
chan->bytes_written_nt = second_write;
}
fflush(stdout);
}
} else {
if (chan->payload_size > 0 && ! fwrite((char*)chan->buf, chan->payload_size, 1, stdout) ) {
output_logmessage("write_streamdata: Error or EOF on STDOUT(?) during write.\n");
return -1;
}
bytes_written += chan->payload_size;
chan->bytes_written_nt += chan->payload_size;
}
#endif
// Move any remaining memory to the start of the buffer
chan->buf_used -= chan->payload_size;
memmove( chan->buf_ptr, chan->buf_ptr+chan->payload_size, chan->buf_used );
}
return bytes_written;
}
/* In FILTER mode (non-cgi-mode) we simply start with a file descriptor. This is a
* leftover from the original code, because in our case it is always stdin
* This is the main processing loop for filter mode that runs until we have no
* longer data on stdin (read returns 0) or we caught a signal (Interrupted > 0) */
void filter_global_loop(int fd_dvr) {
unsigned char buf[TS_PACKET_SIZE];
int bytes_read;
const uint16_t max_sync_errors = 5;
while (! Interrupted ) {
bytes_read = read(fd_dvr, buf, TS_PACKET_SIZE);
global_state->bytes_streamed_read += bytes_read;
if (bytes_read == 0) {
output_logmessage("filter_global_loop: read from stream %.2f MB, wrote %.2f MB, no bytes left to read - EOF. Exiting.\n",
(float)global_state->bytes_streamed_read/mb_conversion, (float)global_state->bytes_streamed_write/mb_conversion);
break;
}
if (bytes_read > 0 && bytes_read < TS_PACKET_SIZE) {
output_logmessage("filter_global_loop: short read, only got %d bytes, instead of %d, trying to resync\n", bytes_read, TS_PACKET_SIZE);
// trying to start over, to get a full read by waiting a little bit (450 ms)
poll(NULL, 0, 450);
/* Throw rest of the not read bytes away by trying to read it in the buffer and jump back to read full mpeg frames */
bytes_read = read(fd_dvr,buf,TS_PACKET_SIZE - bytes_read);
global_state->ts_sync_error += 1;
if (global_state->ts_sync_error > max_sync_errors) {
break;
}
continue;
} else if (bytes_read < 0) {
output_logmessage("filter_global_loop: streamed %ld bytes, read returned an error: %s, exiting.\n", global_state->bytes_streamed_read, strerror(errno));
break;
}
// Check the sync-byte
if (TS_PACKET_SYNC_BYTE(buf) != 0x47) {
int i = 1;
int did_read = 0;
/* Check whether we are completly lost
* (got no synchronisation on transport-stream start after max_sync_errors tries) */
global_state->ts_sync_error += 1;
if (global_state->ts_sync_error > max_sync_errors) {
output_logmessage("filter_global_loop: After reading %.2f MB and writing %.2f, " \
"Lost synchronisation (sync loss counter of %d exceeded) - Exiting\n",
(float)global_state->bytes_streamed_read/mb_conversion, (float)global_state->bytes_streamed_write/mb_conversion, max_sync_errors);
return;
}
for (i = 1; i < TS_PACKET_SIZE; i++) {
if (0x47 == buf[i]) {
/* throw rest of buffer away */
bytes_read = read(fd_dvr, buf, i);
did_read = i;
continue;
}
}
/* No 0x47 found, most likly we are lost */
if (0 == did_read) {
output_logmessage("filter_global_loop: After reading %.2f MB and writing %.2f, " \
"Lost synchronisation - skipping full block (Lost counter %d, aborting at %d) \n",
(float)global_state->bytes_streamed_read/mb_conversion, (float)global_state->bytes_streamed_write/mb_conversion,
global_state->ts_sync_error, max_sync_errors);
} else {
output_logmessage("filter_global_loop: After reading %.2f MB and writing %.2f, " \
" Lost synchronisation - skipping %d bytes (Lost counter %d, aborting at %d) \n",
(float)global_state->bytes_streamed_read/mb_conversion, (float)global_state->bytes_streamed_write/mb_conversion,
did_read, global_state->ts_sync_error, max_sync_errors);
}
continue; /* read next block */
}
/* Bail out on hard errors */
if (process_ts_packet(buf) == TS_HARD_ERROR) {
break;
}
}
return;
}
/* In CGI mode we are called by libcurl using the libcurl CURLOPT_WRITEFUNCTION callback function
* we don't know how much data we get at once, but we've to handle it completly before going back.
* This is no big deal because this code is much faster then needed for processing */
struct memory_struct {
unsigned char *memory;
size_t size;
};
/* libcurls write callback, set with CURLOPT_WRITEFUNCTION if in CGI mode */
/* the code is inspired by an example implementation given on libcurls webpage */
static size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
size_t already_processed = 0;
char header[STR_BUF_SIZE];
struct memory_struct *mem = (struct memory_struct *)userp;
/* process the data we've stored from the last run? */
unsigned char * buf = contents;
/* Do we have to output the HTTP Header? */
if (!global_state->output_payload) {
/* not all data items were available, check wether they are available now.
* output the header and START playing audio */
if (global_state->station_name
&& strlen(global_state->station_name) > 0
&& global_state->br > 0
&& global_state->sr > 0) {
char *content_type = NULL;
if (global_state->want_ac3) {
content_type = "Content-Type: audio/ac3";
} else {
content_type = "Content-Type: audio/mpeg";
}
if (shoutcast) {
/* Strlen: of all the static stuff: 114 Byte */