-
Notifications
You must be signed in to change notification settings - Fork 2
/
ts2shout.c
1917 lines (1829 loc) · 77.1 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-2021
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 <assert.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=0; /* Send shoutcast headers? Default no */
uint8_t logformat=1; /* Apache compatible output format */
uint64_t frame_count=0; /* ts-Frame number (used for debugging) */
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;
section_aggregate_t *dsmcc_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("shoutcast", argv[i]) == 0) {
shoutcast = 1;
}
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;
unsigned int possible_pmt = 0;
#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* one_program = PAT_PROGRAMME_START(start);
/* tvheadend and vdr rewrite PAT, but some mp2t serving systems only remove PMT pids and leave PAT as it is.
* Therefore we have to scan PAT for alle PMT pids. If there are also more than one PMT in the stream a
* "random" (the first seen PMT) program is selected. */
// if (! channel_map[PAT_PROGRAMME_PMT(one_program)] ) { // This was here before, but does not work anymore, because we allow more than one PMT result
/* Add PMT if we don't have a valid transport_stream_id already */
if ( global_state->transport_stream_id != PAT_TRANSPORT_STREAM_ID(start)) {
if (dvb_crc32(start, PAT_SECTION_LENGTH(start) + 3) == 0) {
unsigned int i = 0;
#ifdef DEBUG
// DumpHex(one_program, PAT_SECTION_LENGTH(start) - 8);
#endif
/* Scan for possible, valid PMTs */
global_state->transport_stream_id = PAT_TRANSPORT_STREAM_ID(start);
for (i = 0; i < ( PAT_SECTION_LENGTH(start) - 8 - 2 /* CRC */ ); i += 4) {
if (PAT_PROGRAMME_PMT( (one_program + i) ) > 0x11) { // Only add normal tables, not NIT et.al.
add_channel(CHANNEL_TYPE_PMT, PAT_PROGRAMME_PMT( (one_program + i) ));
possible_pmt += 1;
}
}
}
output_logmessage("extract_pat_payload(): Added %d possible PMT id(s) with transport_stream_id: %d.\n", possible_pmt, global_state->transport_stream_id);
}
return;
}
/* Let's hate software patents. This table is guessed out of real world radio DVB-S reception
* It's not correct, but if someone wants this fixed, please provide me a description of how
* the streaming parameters are read out of the LATM stream or mpeg-ts stream PMT.
*/
static void set_latm_parameters(uint8_t aac_profile) {
if (aac_profile == 0x51 ) {
global_state->sr = 48000;
global_state->br = 128;
} else if (aac_profile == 0x52 ) {
global_state->sr = 48000;
global_state->br = 256;
} else if (aac_profile == 0x60 ) {
global_state->sr = 48000;
if (global_state->br == 0) {
global_state->br = 48;
}
} else {
global_state->sr = 48000;
if (global_state->br == 0) {
global_state->br = 128;
}
output_logmessage("Sorry, no configuration for AAC profile (id=0x%x) found. All values guessed.\n");
}
return;
}
/* Get the info about one stream back out of the PMT
* Make a quality estimation to select the best stream afterwards */
audio_quality_t * analyze_stream_from_pmt(unsigned char *pmt_stream_info_offset, unsigned char *start) {
unsigned int stream_type;
audio_quality_t * stream_quality = NULL;
enum_audio_checks audio_all_checks = NO_AUDIO_STREAM;
stream_type = PMT_STREAM_TYPE(pmt_stream_info_offset);
stream_quality = calloc(1, sizeof(audio_quality_t));
memset(stream_quality, 0, sizeof(audio_quality_t));
/* Search for audio streams and make an assumption about preference
* We want MP1/2, AAC-LATM, AAC, AC-3 (in this order)
* Additional preference work is done for AAC-LATM at the end of this function */
switch ( stream_type ) {
case 0x03: /* MPEG 1 audio */
audio_all_checks = AUDIO_STREAM;
stream_quality->stream_type_name = "MPEG 1";
stream_quality->stream_type = STREAM_MODE_MPEG;
stream_quality->audio_preference = AUDIO_PREFERENCE_BEST;
break;
case 0x04: /* MPEG 2 audio */
audio_all_checks = AUDIO_STREAM;
stream_quality->stream_type_name = "MPEG 2";
stream_quality->stream_type = STREAM_MODE_MPEG;
stream_quality->audio_preference = AUDIO_PREFERENCE_BEST;
break;
case 0x06: /* AC-3 */
audio_all_checks = AC3_CHECK_DESCRIPTOR;
stream_quality->stream_type_name = "AC-3";
stream_quality->stream_type = STREAM_MODE_AC3;
/* If option AC3 is set, the preference is updated to "BEST" below */
stream_quality->audio_preference = AUDIO_PREFERENCE_LOW;
break;
case 0x0b: /* DSM-CC (it's not audio, it's a data carousel) */
audio_all_checks = DSMCC_STREAM;
stream_quality->stream_type_name = "DSM-CC";
stream_quality->stream_type = STREAM_MODE_DSMCC;
break;
case 0x0f: /* MPEG 2 AAC */
audio_all_checks = AUDIO_STREAM;
stream_quality->stream_type_name = "AAC";
stream_quality->stream_type = STREAM_MODE_AAC;
stream_quality->audio_preference = AUDIO_PREFERENCE_MEDIUM;
break;
case 0x11: /* MPEG 4 AAC LATM */
audio_all_checks = AUDIO_STREAM;
stream_quality->stream_type_name = "LATM";
stream_quality->stream_type = STREAM_MODE_AACP;
stream_quality->audio_preference = AUDIO_PREFERENCE_BETTER;
break;
case 0x89: /* If MPEG 2/4 AAC (LATM) RDS data is in a separate PID */
audio_all_checks = RDS_STREAM;
stream_quality->stream_type_name = "RDS";
stream_quality->stream_type = STREAM_MODE_NONE;
break;
default:
/* No useful stream found */
return stream_quality;
break;
}
/* We found a supported media or data stream */
stream_quality->ptr = pmt_stream_info_offset;
/* We have to scan the descriptors for AC-3, AAC and RDS informations */
uint8_t rds_ok = 0;
uint8_t dsmcc_ok = 0;
{
int32_t offset = 0;
#ifdef DEBUG
fprintf(stderr, "Searching PMT descriptors: Length of descriptors %d\n", PMT_INFO_LENGTH(pmt_stream_info_offset));
#endif
while (offset < PMT_INFO_LENGTH(pmt_stream_info_offset)) {
unsigned char * descriptor_pointer = PMT_FIRST_STREAM_DESCRIPTORP(pmt_stream_info_offset) + offset;
offset = offset + DESCRIPTOR_LENGTH (descriptor_pointer) + 2; /* The 2 is the static size of the offset and the tag byte itself */
#ifdef DEBUG
fprintf(stderr, "Searching PMT descriptors, current type 0x%x: next descriptor @%d\n", DESCRIPTOR_TAG(descriptor_pointer), offset);
#endif
/* AC-3 Descriptor */
if ( DESCRIPTOR_TAG(descriptor_pointer) == 0x6a ) {
#ifdef DEBUG
output_logmessage("add_payload_from_pmt(): Found AC-3 audio descriptor for PID %d\n", PMT_PID(pmt_stream_info_offset));
#endif
if ( audio_all_checks == AC3_CHECK_DESCRIPTOR && stream_quality->stream_type == STREAM_MODE_AC3) {
audio_all_checks = AUDIO_STREAM;
}
}
/* AAC descriptor */
if ( DESCRIPTOR_TAG(descriptor_pointer) == 0x7c ) {
stream_quality->aac_profile = descriptor_pointer[2];
}
/* Ancillary data descriptor */
if ( DESCRIPTOR_TAG(descriptor_pointer) == 0x6b ) {
if (descriptor_pointer[2] == 0x40) {
rds_ok |= 1;
}
/* RDS = length = 1, type 0x40 */
}
/* Stream identifier descriptor, we don't care about it in rds context */
if ( DESCRIPTOR_TAG(descriptor_pointer) == 0x52 ) {
if ( descriptor_pointer[2] == 0x32) {
rds_ok |= 2;
}
/* length = 1, content = 0x32 */
}
if ( DESCRIPTOR_TAG(descriptor_pointer) == 0x66) {
if (descriptor_pointer[1] == 2) {
uint16_t data_broadcast_descriptor = descriptor_pointer[2]<<8 | descriptor_pointer[3];
if ( data_broadcast_descriptor == 0x123) {
/* HBBTV carousel */
dsmcc_ok = 1;
}
}
}
}
}
/* Correct the entries in the created struct */
/* Check for RDS OK */
if ( audio_all_checks == RDS_STREAM ) {
if ((rds_ok & 1) == 1) {
#ifdef DEBUG
fprintf(stderr, "analyze_stream_from_pmt(): RDS Stream found\n");
#endif
stream_quality->stream_type = STREAM_MODE_RDS;
} else {
stream_quality->stream_type = STREAM_MODE_NONE;
}
}
if ( audio_all_checks == DSMCC_STREAM ) {
if (dsmcc_ok == 1) {
#ifdef DEBUG
fprintf(stderr, "analyze_stream_from_pmt(): HBBTV DSMCC Stream found\n");
#endif
stream_quality->stream_type = STREAM_MODE_DSMCC;
} else {
stream_quality->stream_type = STREAM_MODE_NONE;
}
}
/* Check for AC-3 descriptors, if they weren't there, its not AC-3 */
if ( audio_all_checks == AC3_CHECK_DESCRIPTOR ) {
stream_quality->stream_type = STREAM_MODE_NONE;
}
/* If the user wants AC-3 we maximum prefer it */
if (global_state->want_ac3) {
if ( stream_quality->stream_type == STREAM_MODE_AC3) {
stream_quality->audio_preference = AUDIO_PREFERENCE_BEST;
stream_quality->audio_preference += 1;
}
}
/* Adjust preference of the different AAC profiles */
switch (stream_quality->aac_profile) {
case 0x52: /* AAC profile, Level 4, BR 256 kBit/s */
stream_quality->audio_preference += 0x80;
break;
case 0x51: /* AAC profile, Level 2, BR 128 kBit/s */
stream_quality->audio_preference += 0x70;
break;
default:
break;
}
return stream_quality;
}
/* Get info about an available media stream (we want mp1/mp2/mp4 or AC-3) */
static void add_payload_from_pmt(audio_quality_t * stream_quality, unsigned char *start) {
enum_audio_checks audio_all_checks = NO_AUDIO_STREAM;
switch ( stream_quality->stream_type ) {
case STREAM_MODE_MPEG:
case STREAM_MODE_AAC:
case STREAM_MODE_AACP:
case STREAM_MODE_AC3:
audio_all_checks = AUDIO_STREAM;
global_state->stream_type = stream_quality->stream_type;
break;
case STREAM_MODE_RDS:
audio_all_checks = RDS_STREAM;
break;
case STREAM_MODE_DSMCC:
audio_all_checks = DSMCC_STREAM;
break;
default:
output_logmessage("add_payload_from_pmt(): Programm error, no valid stream-type given (%d)\n", stream_quality->stream_type);
return;
break;
}
/* Scan descriptors */
{
unsigned int offset = 0;
#ifdef DEBUG
fprintf(stderr, "Searching PMT descriptors: Length of descriptors %d\n", PMT_INFO_LENGTH(stream_quality->ptr));
#endif
while (offset < PMT_INFO_LENGTH(stream_quality->ptr)) {
unsigned char * descriptor_pointer = PMT_FIRST_STREAM_DESCRIPTORP(stream_quality->ptr) + offset;
offset = offset + DESCRIPTOR_LENGTH (descriptor_pointer) + 2; /* The 2 is the static size of the offset and the tag byte itself */
#ifdef DEBUG
fprintf(stderr, "Searching PMT descriptors, current type 0x%x: next descriptor @%d\n", DESCRIPTOR_TAG(descriptor_pointer), offset);
#endif
/* Maximum Bitrate descriptor, found description in wireshark and on the internet */
if ( DESCRIPTOR_TAG(descriptor_pointer) == 0x0e ) {
int bitrate;
bitrate = ((descriptor_pointer[2] & 0x3f)<<16) + (descriptor_pointer[3]<<8) + descriptor_pointer[4];
bitrate = bitrate * 50;
global_state->br = ( bitrate * 8 ) / 1024;
output_logmessage("add_payload_from_pmt(): %s maximum bitrate %.1f KByte/s (%.1f KBit/s)\n", stream_quality->stream_type_name, (float)bitrate/1024, ((float)bitrate * 8) /1024);
}
/* AC-3 Descriptor */
if ( stream_quality->stream_type == STREAM_MODE_AC3 && DESCRIPTOR_TAG(descriptor_pointer) == 0x6a ) {
output_logmessage("add_payload_from_pmt(): Found AC-3 audio descriptor for PID %d\n", PMT_PID(stream_quality->ptr));
/* TODO parse AC-3 parameters out of fields */
}
/* AAC descriptor */
if ( DESCRIPTOR_TAG(descriptor_pointer) == 0x7c ) {
uint8_t aac_profile;
aac_profile = descriptor_pointer[2];
set_latm_parameters(aac_profile);
output_logmessage("add_payload_from_pmt(): Audio `%s'\n", aac_profile_name(aac_profile));
}
if ( DESCRIPTOR_TAG(descriptor_pointer) == 0x0a ) {
unsigned char language[4];
memcpy(language, descriptor_pointer + 2, 3);
language[3] = 0;
output_logmessage("add_payload_from_pmt(): stream language `%s'\n", language);
/* ger, deu, FRA, fre... oh, this is quite a mess even if you look only on german and french */
}
}
}
/* If all parameters are ok, add the payload stream */
if ( audio_all_checks == AUDIO_STREAM ) {
global_state->service_id = PMT_PROGRAM_NUMBER(start);
global_state->mime_type = mime_type(global_state->stream_type);
global_state->payload_added = 1;
output_logmessage("add_payload_from_pmt(): Found %s audio stream in PID %d (service_id %d)\n", stream_quality->stream_type_name, PMT_PID(stream_quality->ptr), global_state->service_id);
add_channel(CHANNEL_TYPE_PAYLOAD, PMT_PID(stream_quality->ptr));
} else if ( audio_all_checks == RDS_STREAM ) {
if ( global_state->prefer_rds > 0) {
output_logmessage("add_payload_from_pmt(): Found RDS data stream in PID %d\n", PMT_PID(stream_quality->ptr));
add_channel(CHANNEL_TYPE_RDS, PMT_PID(stream_quality->ptr));
} else {
output_logmessage("add_payload_from_pmt(): Ignoring RDS data stream in PID %d, RDS disabled by configuration\n", PMT_PID(stream_quality->ptr));
}
} else if ( audio_all_checks == DSMCC_STREAM ) {
output_logmessage("add_payload_from_pmt(): Found DSM-CC data stream in PID %d\n", PMT_PID(stream_quality->ptr));
add_channel(CHANNEL_TYPE_DSMCC, PMT_PID(stream_quality->ptr));
}
return;
}
/* Get stream info out of the PMT (program map table). We are only interested in mp1/mp2/aac and ac-3 streams */
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;
uint8_t found_streams_counter = 0;
uint8_t i = 0;
uint32_t section_length = 0;
uint32_t current_offset = 9; /* Size of PMT "header" */
uint32_t best_quality = 0;
audio_quality_t* quality[10]; /* Maximum of 10 streams possible */
char aac_info_message[STR_BUF_SIZE] = "";
/* 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
section_length = PMT_SECTION_LENGTH(start);
/* 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 (dvb_crc32(start, PAT_SECTION_LENGTH(start) + 3) != 0) {
output_logmessage("extract_pmt_payload(): crc32 does not match %d found, 0 expected\n", dvb_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 < 10 && current_offset < section_length) {
quality[found_streams_counter] = analyze_stream_from_pmt(pmt_stream_info_offset, start);
found_streams_counter += 1;
current_offset += PMT_INFO_LENGTH(pmt_stream_info_offset);
pmt_stream_info_offset = PMT_FIRST_STREAM_DESCRIPTORP(pmt_stream_info_offset) + PMT_INFO_LENGTH(pmt_stream_info_offset);
}
}
if ( found_streams_counter > 0) {
found_streams_counter -= 1;
}
/* Search for best audio */
for (i = 0; i < found_streams_counter; i++) {
#ifdef DEBUG
fprintf(stderr, "Found Stream in Mode %d with Preference %d as number %d\n", quality[i]->stream_type, quality[i]->audio_preference, i);
#endif
if (quality[i]->audio_preference > best_quality) {
best_quality = quality[i]->audio_preference ;
}
}
/* Search for Audio in best quality */
for (i = 0; i < found_streams_counter; i++) {
if (quality[i]->stream_type != STREAM_MODE_NONE
&& quality[i]->stream_type != STREAM_MODE_RDS
&& quality[i]->stream_type != STREAM_MODE_DSMCC
&& quality[i]->audio_preference == best_quality) {
/* Add audio */
if (! channel_map[PMT_PID(quality[i]->ptr)]) {
add_payload_from_pmt(quality[i], start);
}
if (quality[i]->stream_type == STREAM_MODE_AACP) {
#ifdef FFMPEG
/* Prepare ffmpeg */
global_state->ffmpeg.pkt = av_packet_alloc();
/* find the MPEG audio decoder */
global_state->ffmpeg.codec = avcodec_find_decoder(AV_CODEC_ID_AAC_LATM);
if (! global_state->ffmpeg.codec) {
output_logmessage("avcodec_find_decoder(): Codec AV_CODEC_ID_AAC_LATM not found.\n");
goto end;
}
global_state->ffmpeg.parser = av_parser_init(global_state->ffmpeg.codec->id);
if (! global_state->ffmpeg.parser) {
output_logmessage("av_parser_init(): Parser for Codec not possible.\n");
goto end;
}
global_state->ffmpeg.c = avcodec_alloc_context3(global_state->ffmpeg.codec);
if (! global_state->ffmpeg.c) {
output_logmessage("avcodec_alloc_context3(): Could not allocate audio codec context.\n");
goto end;
}
/* open it */
if (avcodec_open2(global_state->ffmpeg.c, global_state->ffmpeg.codec, NULL) < 0) {
output_logmessage("avcodec_open2(): Could not open codec.\n");
goto end;
}
global_state->ffmpeg.decoded_frame = av_frame_alloc();
if (! global_state->ffmpeg.decoded_frame) {
output_logmessage("av_frame_alloc(): Could not allocate decoded frame space.\n");
goto end;
}
#endif
/* No FFMPEG or Parser successfully initialized, let's go! */
global_state->aac_inline_rds = 1;
#ifdef FFMPEG
end:
#endif
(void)0; // NOP
}
}
}
/* Search RDS */
for (i = 0; i < found_streams_counter; i++) {
if (quality[i]->stream_type == STREAM_MODE_RDS) {
if (! channel_map[PMT_PID(quality[i]->ptr)]) {
global_state->aac_inline_rds = 0;
sprintf(aac_info_message, " (Separate RDS PID %d available)", PMT_PID(quality[i]->ptr) );
add_payload_from_pmt(quality[i], start);
}
}
}
/* Search DSMCC */
for (i = 0; i < found_streams_counter; i++) {
if (quality[i]->stream_type == STREAM_MODE_DSMCC) {
if (! channel_map[PMT_PID(quality[i]->ptr)]) {
add_payload_from_pmt(quality[i], start);
}
}
}
for (i = 0; i < found_streams_counter; i++) {
free(quality[i]);
}
}
#ifdef FFMPEG
output_logmessage("AAC inline RDS messages are %s%s\n", ((global_state->aac_inline_rds > 0)? "enabled" : "disabled"), aac_info_message);
#endif
}
/* 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 (dvb_crc32(aggregation->buffer, section_length + 3) != 0) {
/* not valid */
#ifdef DEBUG
fprintf(stderr, "fetch_next(): Found data, but crc32 is wrong %d!=0", dvb_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 data out of many MPEG frames (EIT, SDT, DSM-CC etc.) * * *
* 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: 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.
* In most cases all information is fetched correctly. Sometimes concated, very small EIT tables are misinterpreted and give a wrong CRC32.
* This is only 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).
header size must be not substracted, because it was substracted above */
if ((aggregation->offset - 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
/* New, better code: Check whether there is some leftover */
if ( start_of_pes) {
if ( TS_PACKET_POINTER(ts_full_frame) < ( TS_PACKET_SIZE - 5 ) ) {
aggregation->ob_used = TS_PACKET_SIZE - TS_PACKET_POINTER(ts_full_frame) - 5;
memcpy(aggregation->offset_buffer, start + TS_PACKET_POINTER(ts_full_frame), TS_PACKET_SIZE - TS_PACKET_POINTER(ts_full_frame) - 5);
#ifdef DEBUG
fprintf(stderr, "Leftover in continued frame ....\n");
DumpHex(aggregation->offset_buffer, aggregation->ob_used);
#endif
}
}
return 1;
} else {
if ( start_of_pes ) {
#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 > 0) {
uint8_t size = 0;
/* special operation */
if (! start_of_pes ) {
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);
} else {
/* This is a *short* frame again with leftover */
aggregation->continuation = 0;
aggregation->buffer_valid = 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);
/* 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, "collect_continuation(): FIXED BUG we are using leftover from last frame and have a leftover again\n");
#endif
if ( TS_PACKET_POINTER(ts_full_frame) < ( TS_PACKET_SIZE - 5 ) ) {
aggregation->ob_used = TS_PACKET_SIZE - TS_PACKET_POINTER(ts_full_frame) - 5;
memcpy(aggregation->offset_buffer, start + TS_PACKET_POINTER(ts_full_frame), TS_PACKET_SIZE - TS_PACKET_POINTER(ts_full_frame) - 5);
#ifdef DEBUG
fprintf(stderr, "Leftover in continued frame ....\n");
DumpHex(aggregation->offset_buffer, aggregation->ob_used);
#endif
}
return 1;
}
}
}
/* 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;
}
/* For normal people SDT contains the Service description, i.e. the Station name. Some satellite stations are transmitting garbage
* and remove the data from SDT and use PMT or whatever for it.
* Sometimes it is just missing or removed by broken MPEG software */
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 (dvb_crc32(start, sdt_table->section_length + 3) != 0) {
output_logmessage("SDT: crc32 does not match, calculated %d, expected 0\n", dvb_crc32(start, sdt_table->section_length + 3));
} 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(PMT_PROGRAM_NUMBER(start) != global_state->transport_stream_id) {
#ifdef DEBUG
fprintf(stderr, "SDT: SDT programm-number %d doesn't match global transport_stream_id %d\n", PMT_PROGRAM_NUMBER(start), global_state->transport_stream_id);
#endif
#if 0
// I thought this is correct, but it doesn't work for the new french tranponder 12285V on Astra 19.2
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), transport_stream_id %d, programm_id %d\n",
SDT_DESCRIPTOR_SERVICE_ID(description_offset), global_state->service_id,
global_state->transport_stream_id, -1);
// DumpHex(description_offset, 188);
// fprintf(stderr, "----------------\n");
#endif
#endif
} else {
if ( SDT_DESCRIPTOR_SERVICE_ID(description_offset) != global_state->service_id ) {
#ifdef DEBUG
fprintf(stderr, "SDT: SDT skipping service %d, because not matching for wanted sevice_id %d\n",
SDT_DESCRIPTOR_SERVICE_ID(description_offset), global_state->service_id );
#endif
description_offset = description_offset + SDT_DESCRIPTOR_LOOP_LENGTH(description_offset) + 5;
continue;
}
if (SDT_DESCRIPTOR_RUNNING(description_offset) == 0x4 ||
SDT_DESCRIPTOR_RUNNING(description_offset) == 0x1 /* FRENCH transponder?! */ ) {
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;
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));
/* 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
|| SDT_DC_SERVICE_TYPE(description_content) == 0x01 /* broken station has SD-TV set */ ) {
/* 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 */
unsigned char utf8_service_name[STR_BUF_SIZE];
if (strlen(service_name) > 0) {
unsigned char utf8_service_name[STR_BUF_SIZE];
utf8((unsigned char*)service_name, utf8_service_name);
}
if (SDT_DC_SERVICE_TYPE(description_content) == 0x1f) {
output_logmessage("SDT: Warning: Stream (also) contains service HEVC HDTV (%s)\n", utf8_service_name);
} else if (SDT_DC_SERVICE_TYPE(description_content) != 0xff) {
output_logmessage("SDT: Warning: Stream (also) contains unkown service with id 0x%2x (%s)\n", SDT_DC_SERVICE_TYPE(description_content), utf8_service_name);
}
}
}
}
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;
#ifdef DEBUG
fprintf(stderr, "EIT: crc32 %s (%d, l: %d)\n",( dvb_crc32(start, EIT_SECTION_LENGTH(start)+3)== 0?"OK":"FAIL"), dvb_crc32(start, EIT_SECTION_LENGTH(start)+3), EIT_SECTION_LENGTH(start)+3);
#endif
if (dvb_crc32(start, EIT_SECTION_LENGTH(start)+3)!= 0) {
/* crc32 not valid, throw away buffer */
// 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 (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 (dvb_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", dvb_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);