forked from gfto/dvblast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demux.c
2953 lines (2544 loc) · 93.6 KB
/
demux.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
/*****************************************************************************
* demux.c
*****************************************************************************
* Copyright (C) 2004, 2008-2011 VideoLAN
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Andy Gatward <a.j.gatward@reading.ac.uk>
* Marian Ďurkovič <md@bts.sk>
*
* 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <stdarg.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "dvblast.h"
#include "en50221.h"
#ifdef HAVE_ICONV
#include <iconv.h>
#endif
#include <bitstream/mpeg/ts.h>
#include <bitstream/mpeg/pes.h>
#include <bitstream/mpeg/psi.h>
#include <bitstream/dvb/si.h>
#include <bitstream/dvb/si_print.h>
#include <bitstream/mpeg/psi_print.h>
extern bool b_enable_emm;
extern bool b_enable_ecm;
/*****************************************************************************
* Local declarations
*****************************************************************************/
#define MIN_SECTION_FRAGMENT PSI_HEADER_SIZE_SYNTAX1
typedef struct ts_pid_t
{
int i_refcount;
int i_psi_refcount;
bool b_pes;
int8_t i_last_cc;
int i_demux_fd;
/* b_emm is set to true when PID carries EMM packet
and should be outputed in all services */
bool b_emm;
/* PID info and stats */
mtime_t i_bytes_ts;
unsigned long i_packets_passed;
ts_pid_info_t info;
/* biTStream PSI section gathering */
uint8_t *p_psi_buffer;
uint16_t i_psi_buffer_used;
output_t **pp_outputs;
int i_nb_outputs;
} ts_pid_t;
typedef struct sid_t
{
uint16_t i_sid, i_pmt_pid;
uint8_t *p_current_pmt;
} sid_t;
ts_pid_t p_pids[MAX_PIDS];
static sid_t **pp_sids = NULL;
static int i_nb_sids = 0;
static PSI_TABLE_DECLARE(pp_current_pat_sections);
static PSI_TABLE_DECLARE(pp_next_pat_sections);
static PSI_TABLE_DECLARE(pp_current_cat_sections);
static PSI_TABLE_DECLARE(pp_next_cat_sections);
static PSI_TABLE_DECLARE(pp_current_nit_sections);
static PSI_TABLE_DECLARE(pp_next_nit_sections);
static PSI_TABLE_DECLARE(pp_current_sdt_sections);
static PSI_TABLE_DECLARE(pp_next_sdt_sections);
static mtime_t i_last_dts = -1;
static int i_demux_fd;
static int i_nb_errors = 0;
static mtime_t i_last_error = 0;
static mtime_t i_last_reset = 0;
#ifdef HAVE_ICONV
static iconv_t iconv_handle = (iconv_t)-1;
#endif
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static void demux_Handle( block_t *p_ts );
static void SetDTS( block_t *p_list );
static void SetPID( uint16_t i_pid );
static void SetPID_EMM( uint16_t i_pid );
static void UnsetPID( uint16_t i_pid );
static void StartPID( output_t *p_output, uint16_t i_pid );
static void StopPID( output_t *p_output, uint16_t i_pid );
static void SelectPID( uint16_t i_sid, uint16_t i_pid );
static void UnselectPID( uint16_t i_sid, uint16_t i_pid );
static void SelectPMT( uint16_t i_sid, uint16_t i_pid );
static void UnselectPMT( uint16_t i_sid, uint16_t i_pid );
static void GetPIDS( uint16_t **ppi_wanted_pids, int *pi_nb_wanted_pids,
uint16_t i_sid,
const uint16_t *pi_pids, int i_nb_pids );
static bool SIDIsSelected( uint16_t i_sid );
static bool PIDWouldBeSelected( uint8_t *p_es );
static bool PMTNeedsDescrambling( uint8_t *p_pmt );
static void FlushEIT( output_t *p_output, mtime_t i_dts );
static void SendTDT( block_t *p_ts );
static void SendEMM( block_t *p_ts );
static void NewPAT( output_t *p_output );
static void NewPMT( output_t *p_output );
static void NewNIT( output_t *p_output );
static void NewSDT( output_t *p_output );
static void HandlePSIPacket( uint8_t *p_ts, mtime_t i_dts );
static const char *get_pid_desc(uint16_t i_pid, uint16_t *i_sid);
/*
* Remap an ES pid to a fixed value.
* Multiple streams of the same type use sequential pids
* Returns the new pid and updates the map tables
*/
uint16_t map_es_pid(output_t * p_output, uint8_t *p_es, uint16_t i_pid)
{
uint16_t i_newpid = i_pid;
uint16_t i_stream_type = pmtn_get_streamtype(p_es);
if (! b_do_remap && !p_output->b_do_remap )
return i_pid;
msg_Dbg(NULL, "REMAP: Found elementary stream type 0x%02x with original PID 0x%x (%u):", i_stream_type, i_pid, i_pid);
switch ( i_stream_type )
{
case 0x03: /* audio MPEG-1 */
case 0x04: /* audio */
case 0x0f: /* audio AAC ADTS */
case 0x11: /* audio AAC LATM */
case 0x81: /* ATSC AC-3 */
case 0x87: /* ATSC Enhanced AC-3 */
if ( b_do_remap )
i_newpid = pi_newpids[I_APID];
else
i_newpid = p_output->pi_confpids[I_APID];
break;
case 0x01: /* video MPEG-1 */
case 0x02: /* video */
case 0x10: /* video MPEG-4 */
case 0x1b: /* video H264 */
if ( b_do_remap )
i_newpid = pi_newpids[I_VPID];
else
i_newpid = p_output->pi_confpids[I_VPID];
break;
case 0x06: { /* PES Private Data - We must check the descriptors */
/* By default, nothing identified */
uint8_t SubStreamType = N_MAP_PIDS;
uint16_t j = 0;
const uint8_t *p_desc;
/* Loop over the descriptors */
while ( (p_desc = descs_get_desc( pmtn_get_descs( p_es ), j )) != NULL )
{
/* Get the descriptor tag */
uint8_t i_tag = desc_get_tag( p_desc );
j++;
/* Check if the tag is: A/52, Enhanced A/52, DTS, AAC */
if (i_tag == 0x6a || i_tag == 0x7a || i_tag == 0x7b || i_tag == 0x7c)
SubStreamType=I_APID;
/* Check if the tag is: VBI + teletext, teletext, dvbsub */
if (i_tag == 0x46 || i_tag == 0x56 || i_tag == 0x59)
SubStreamType=I_SPUPID;
}
/* Audio found */
if (SubStreamType==I_APID) {
msg_Dbg(NULL, "REMAP: PES Private Data stream identified as [Audio]");
if ( b_do_remap )
i_newpid = pi_newpids[I_APID];
else
i_newpid = p_output->pi_confpids[I_APID];
}
/* Subtitle found */
if (SubStreamType==I_SPUPID) {
msg_Dbg(NULL, "REMAP: PES Private Data stream identified as [Subtitle]");
if ( b_do_remap )
i_newpid = pi_newpids[I_SPUPID];
else
i_newpid = p_output->pi_confpids[I_SPUPID];
}
break;
}
}
/* Got the new base for the mapped pid. Find the next free one
we do this to ensure that multiple audios get unique pids */
while (p_output->pi_freepids[i_newpid] != UNUSED_PID)
i_newpid++;
p_output->pi_freepids[i_newpid] = i_pid; /* Mark as in use */
p_output->pi_newpids[i_pid] = i_newpid; /* Save the new pid */
msg_Dbg(NULL, "REMAP: => Elementary stream is remapped to PID 0x%x (%u)", i_newpid, i_newpid);
return i_newpid;
}
/*****************************************************************************
* FindSID
*****************************************************************************/
static inline sid_t *FindSID( uint16_t i_sid )
{
int i;
for ( i = 0; i < i_nb_sids; i++ )
{
sid_t *p_sid = pp_sids[i];
if ( p_sid->i_sid == i_sid )
return p_sid;
}
return NULL;
}
/*****************************************************************************
* demux_Open
*****************************************************************************/
void demux_Open( void )
{
int i;
memset( p_pids, 0, sizeof(p_pids) );
pf_Open();
for ( i = 0; i < MAX_PIDS; i++ )
{
p_pids[i].i_last_cc = -1;
p_pids[i].i_demux_fd = -1;
psi_assemble_init( &p_pids[i].p_psi_buffer,
&p_pids[i].i_psi_buffer_used );
}
if ( b_budget_mode )
i_demux_fd = pf_SetFilter(8192);
psi_table_init( pp_current_pat_sections );
psi_table_init( pp_next_pat_sections );
SetPID(PAT_PID);
p_pids[PAT_PID].i_psi_refcount++;
if ( b_enable_emm )
{
psi_table_init( pp_current_cat_sections );
psi_table_init( pp_next_cat_sections );
SetPID_EMM(CAT_PID);
p_pids[CAT_PID].i_psi_refcount++;
}
SetPID(NIT_PID);
p_pids[NIT_PID].i_psi_refcount++;
psi_table_init( pp_current_sdt_sections );
psi_table_init( pp_next_sdt_sections );
SetPID(SDT_PID);
p_pids[SDT_PID].i_psi_refcount++;
SetPID(EIT_PID);
p_pids[EIT_PID].i_psi_refcount++;
SetPID(RST_PID);
SetPID(TDT_PID);
}
/*****************************************************************************
* demux_Close
*****************************************************************************/
void demux_Close( void )
{
int i;
psi_table_free( pp_current_pat_sections );
psi_table_free( pp_next_pat_sections );
psi_table_free( pp_current_cat_sections );
psi_table_free( pp_next_cat_sections );
psi_table_free( pp_current_nit_sections );
psi_table_free( pp_next_nit_sections );
psi_table_free( pp_current_sdt_sections );
psi_table_free( pp_next_sdt_sections );
for ( i = 0; i < MAX_PIDS; i++ )
{
free( p_pids[i].p_psi_buffer );
free( p_pids[i].pp_outputs );
}
for ( i = 0; i < i_nb_sids; i++ )
{
sid_t *p_sid = pp_sids[i];
free( p_sid->p_current_pmt );
free( p_sid );
}
free( pp_sids );
#ifdef HAVE_ICONV
if (iconv_handle != (iconv_t)-1) {
iconv_close(iconv_handle);
iconv_handle = (iconv_t)-1;
}
#endif
}
/*****************************************************************************
* demux_Run
*****************************************************************************/
void demux_Run( block_t *p_ts )
{
SetDTS( p_ts );
while ( p_ts != NULL )
{
block_t *p_next = p_ts->p_next;
p_ts->p_next = NULL;
demux_Handle( p_ts );
p_ts = p_next;
}
}
/*****************************************************************************
* demux_Handle
*****************************************************************************/
static void demux_Handle( block_t *p_ts )
{
uint16_t i_pid = ts_get_pid( p_ts->p_ts );
uint8_t i_cc = ts_get_cc( p_ts->p_ts );
int i;
if ( !ts_validate( p_ts->p_ts ) )
{
msg_Warn( NULL, "lost TS sync" );
switch ( i_print_type )
{
case PRINT_XML:
printf("<ERROR type=\"invalid_ts\"/>\n");
break;
case PRINT_TEXT:
printf("lost TS sync\n");
break;
default:
break;
}
block_Delete( p_ts );
return;
}
if ( i_pid != PADDING_PID )
p_pids[i_pid].info.i_scrambling = ts_get_scrambling( p_ts->p_ts );
p_pids[i_pid].info.i_last_packet_ts = i_wallclock;
p_pids[i_pid].info.i_packets++;
p_pids[i_pid].i_packets_passed++;
/* Calculate bytes_per_sec */
if ( i_wallclock > p_pids[i_pid].i_bytes_ts + 1000000 ) {
p_pids[i_pid].info.i_bytes_per_sec = p_pids[i_pid].i_packets_passed * TS_SIZE;
p_pids[i_pid].i_packets_passed = 0;
p_pids[i_pid].i_bytes_ts = i_wallclock;
}
if ( p_pids[i_pid].info.i_first_packet_ts == 0 )
p_pids[i_pid].info.i_first_packet_ts = i_wallclock;
if ( i_pid != PADDING_PID && p_pids[i_pid].i_last_cc != -1
&& !ts_check_duplicate( i_cc, p_pids[i_pid].i_last_cc )
&& ts_check_discontinuity( i_cc, p_pids[i_pid].i_last_cc ) )
{
unsigned int expected_cc = (p_pids[i_pid].i_last_cc + 1) & 0x0f;
uint16_t i_sid = 0;
const char *pid_desc = get_pid_desc(i_pid, &i_sid);
p_pids[i_pid].info.i_cc_errors++;
msg_Warn( NULL, "TS discontinuity on pid %4hu expected_cc %2u got %2u (%s, sid %d)",
i_pid, expected_cc, i_cc, pid_desc, i_sid );
switch ( i_print_type )
{
case PRINT_XML:
printf("<ERROR type=\"invalid_discontinuity\" pid=\"%hu\" expected_cc=\"%u\" got_cc=\"%u\" pid_carries=\"%s\" sid=\"%u\" />\n",
i_pid, expected_cc, i_cc, pid_desc, i_sid );
break;
case PRINT_TEXT:
printf("TS discontinuity (PID=%hu) (expected_cc=%u) (got_cc=%u) (PID_carries=%s) (sid=%d)\n",
i_pid, expected_cc, i_cc, pid_desc, i_sid );
break;
default:
break;
}
}
if ( ts_get_transporterror( p_ts->p_ts ) )
{
uint16_t i_sid = 0;
const char *pid_desc = get_pid_desc(i_pid, &i_sid);
p_pids[i_pid].info.i_transport_errors++;
msg_Warn( NULL, "transport_error_indicator on pid %hu (%s, sid %u)",
i_pid, pid_desc, i_sid );
switch ( i_print_type )
{
case PRINT_XML:
printf("<ERROR type=\"transport_error\" pid=\"%hu\" pid_carries=\"%s\" sid=\"%u\" />\n",
i_pid, pid_desc, i_sid );
break;
case PRINT_TEXT:
printf("transport_error_indicator (PID=%hu) (PID_carries=%s) (sid=%u)\n",
i_pid, pid_desc, i_sid );
break;
default:
break;
}
i_nb_errors++;
i_last_error = i_wallclock;
}
else if ( i_wallclock > i_last_error + WATCHDOG_WAIT )
i_nb_errors = 0;
if ( i_nb_errors > MAX_ERRORS )
{
i_nb_errors = 0;
msg_Warn( NULL,
"too many transport errors, tuning again" );
pf_Reset();
}
if ( !ts_get_transporterror( p_ts->p_ts ) )
{
/* PSI parsing */
if ( i_pid == TDT_PID || i_pid == RST_PID )
SendTDT( p_ts );
else if ( p_pids[i_pid].i_psi_refcount )
HandlePSIPacket( p_ts->p_ts, p_ts->i_dts );
if ( b_enable_emm && p_pids[i_pid].b_emm )
SendEMM( p_ts );
/* PCR handling */
if ( ts_has_adaptation( p_ts->p_ts )
&& ts_get_adaptation( p_ts->p_ts )
&& tsaf_has_pcr( p_ts->p_ts ) )
{
mtime_t i_timestamp = tsaf_get_pcr( p_ts->p_ts );
int j;
for ( j = 0; j < i_nb_sids; j++ )
{
sid_t *p_sid = pp_sids[j];
if ( p_sid->i_sid && p_sid->p_current_pmt != NULL
&& pmt_get_pcrpid( p_sid->p_current_pmt ) == i_pid )
{
for ( i = 0; i < i_nb_outputs; i++ )
{
output_t *p_output = pp_outputs[i];
if ( p_output->config.i_sid == p_sid->i_sid )
{
p_output->i_ref_timestamp = i_timestamp;
p_output->i_ref_wallclock = p_ts->i_dts;
}
}
}
}
}
}
p_pids[i_pid].i_last_cc = i_cc;
/* Output */
for ( i = 0; i < p_pids[i_pid].i_nb_outputs; i++ )
{
output_t *p_output = p_pids[i_pid].pp_outputs[i];
if ( p_output != NULL )
{
if ( i_ca_handle && (p_output->config.i_config & OUTPUT_WATCH) &&
ts_get_unitstart( p_ts->p_ts ) )
{
uint8_t *p_payload;
if ( ts_get_scrambling( p_ts->p_ts ) ||
( p_pids[i_pid].b_pes
&& (p_payload = ts_payload( p_ts->p_ts )) + 3
< p_ts->p_ts + TS_SIZE
&& !pes_validate(p_payload) ) )
{
if ( i_wallclock >
i_last_reset + WATCHDOG_REFRACTORY_PERIOD )
{
p_output->i_nb_errors++;
p_output->i_last_error = i_wallclock;
}
}
else if ( i_wallclock > p_output->i_last_error + WATCHDOG_WAIT )
p_output->i_nb_errors = 0;
if ( p_output->i_nb_errors > MAX_ERRORS )
{
int j;
for ( j = 0; j < i_nb_outputs; j++ )
pp_outputs[j]->i_nb_errors = 0;
msg_Warn( NULL,
"too many errors for stream %s, resetting",
p_output->config.psz_displayname );
i_last_reset = i_wallclock;
en50221_Reset();
}
}
output_Put( p_output, p_ts );
if ( p_output->p_eit_ts_buffer != NULL
&& p_ts->i_dts > p_output->p_eit_ts_buffer->i_dts
+ MAX_EIT_RETENTION )
FlushEIT( p_output, p_ts->i_dts );
}
}
if ( output_dup.config.i_config & OUTPUT_VALID )
output_Put( &output_dup, p_ts );
p_ts->i_refcount--;
if ( !p_ts->i_refcount )
block_Delete( p_ts );
}
/*****************************************************************************
* demux_Change : called from main thread
*****************************************************************************/
static int IsIn( uint16_t *pi_pids, int i_nb_pids, uint16_t i_pid )
{
int i;
for ( i = 0; i < i_nb_pids; i++ )
if ( i_pid == pi_pids[i] ) break;
return ( i != i_nb_pids );
}
void demux_Change( output_t *p_output, const output_config_t *p_config )
{
uint16_t *pi_wanted_pids, *pi_current_pids;
int i_nb_wanted_pids, i_nb_current_pids;
uint16_t i_old_sid = p_output->config.i_sid;
uint16_t i_sid = p_config->i_sid;
uint16_t *pi_old_pids = p_output->config.pi_pids;
uint16_t *pi_pids = p_config->pi_pids;
int i_old_nb_pids = p_output->config.i_nb_pids;
int i_nb_pids = p_config->i_nb_pids;
bool b_sid_change = i_sid != i_old_sid;
bool b_pid_change = false, b_tsid_change = false;
bool b_dvb_change = !!((p_output->config.i_config ^ p_config->i_config)
& OUTPUT_DVB);
bool b_service_name_change =
(!streq(p_output->config.psz_service_name, p_config->psz_service_name) ||
!streq(p_output->config.psz_service_provider, p_config->psz_service_provider));
int i;
p_output->config.i_config = p_config->i_config;
/* Change output settings related to service_name and service_provider . */
free( p_output->config.psz_service_name );
free( p_output->config.psz_service_provider );
p_output->config.psz_service_name = xstrdup( p_config->psz_service_name );
p_output->config.psz_service_provider = xstrdup( p_config->psz_service_provider );
if ( p_config->i_tsid != -1 && p_output->config.i_tsid != p_config->i_tsid )
{
p_output->i_tsid = p_config->i_tsid;
b_tsid_change = true;
}
if ( p_config->i_tsid == -1 && p_output->config.i_tsid != -1 )
{
if ( psi_table_validate(pp_current_pat_sections) && !b_random_tsid )
p_output->i_tsid =
psi_table_get_tableidext(pp_current_pat_sections);
else
p_output->i_tsid = rand() & 0xffff;
b_tsid_change = true;
}
p_output->config.i_tsid = p_config->i_tsid;
if ( !b_sid_change && p_config->i_nb_pids == p_output->config.i_nb_pids &&
(!p_config->i_nb_pids ||
!memcmp( p_output->config.pi_pids, p_config->pi_pids,
p_config->i_nb_pids * sizeof(uint16_t) )) )
goto out_change;
GetPIDS( &pi_wanted_pids, &i_nb_wanted_pids, i_sid, pi_pids, i_nb_pids );
GetPIDS( &pi_current_pids, &i_nb_current_pids, i_old_sid, pi_old_pids,
i_old_nb_pids );
if ( b_sid_change && i_old_sid )
{
sid_t *p_old_sid = FindSID( i_old_sid );
p_output->config.i_sid = p_config->i_sid;
if ( p_old_sid != NULL )
{
if ( i_sid != i_old_sid )
UnselectPMT( i_old_sid, p_old_sid->i_pmt_pid );
if ( i_ca_handle && !SIDIsSelected( i_old_sid )
&& p_old_sid->p_current_pmt != NULL
&& PMTNeedsDescrambling( p_old_sid->p_current_pmt ) )
en50221_DeletePMT( p_old_sid->p_current_pmt );
}
}
for ( i = 0; i < i_nb_current_pids; i++ )
{
if ( !IsIn( pi_wanted_pids, i_nb_wanted_pids, pi_current_pids[i] ) )
{
StopPID( p_output, pi_current_pids[i] );
b_pid_change = true;
}
}
if ( b_sid_change && i_ca_handle && i_old_sid &&
SIDIsSelected( i_old_sid ) )
{
sid_t *p_old_sid = FindSID( i_old_sid );
if ( p_old_sid != NULL && p_old_sid->p_current_pmt != NULL
&& PMTNeedsDescrambling( p_old_sid->p_current_pmt ) )
en50221_UpdatePMT( p_old_sid->p_current_pmt );
}
for ( i = 0; i < i_nb_wanted_pids; i++ )
{
if ( !IsIn( pi_current_pids, i_nb_current_pids, pi_wanted_pids[i] ) )
{
StartPID( p_output, pi_wanted_pids[i] );
b_pid_change = true;
}
}
free( pi_wanted_pids );
free( pi_current_pids );
if ( b_sid_change && i_sid )
{
sid_t *p_sid = FindSID( i_sid );
p_output->config.i_sid = i_old_sid;
if ( p_sid != NULL )
{
if ( i_sid != i_old_sid )
SelectPMT( i_sid, p_sid->i_pmt_pid );
if ( i_ca_handle && !SIDIsSelected( i_sid )
&& p_sid->p_current_pmt != NULL
&& PMTNeedsDescrambling( p_sid->p_current_pmt ) )
en50221_AddPMT( p_sid->p_current_pmt );
}
}
if ( i_ca_handle && i_sid && SIDIsSelected( i_sid ) )
{
sid_t *p_sid = FindSID( i_sid );
if ( p_sid != NULL && p_sid->p_current_pmt != NULL
&& PMTNeedsDescrambling( p_sid->p_current_pmt ) )
en50221_UpdatePMT( p_sid->p_current_pmt );
}
p_output->config.i_sid = i_sid;
free( p_output->config.pi_pids );
p_output->config.pi_pids = malloc( sizeof(uint16_t) * i_nb_pids );
memcpy( p_output->config.pi_pids, pi_pids, sizeof(uint16_t) * i_nb_pids );
p_output->config.i_nb_pids = i_nb_pids;
out_change:
if ( b_sid_change )
{
NewSDT( p_output );
NewNIT( p_output );
NewPAT( p_output );
NewPMT( p_output );
}
else
{
if ( b_tsid_change )
{
NewSDT( p_output );
NewNIT( p_output );
NewPAT( p_output );
}
else if ( b_dvb_change )
{
NewNIT( p_output );
NewPAT( p_output );
}
if ( b_pid_change )
NewPMT( p_output );
if ( !b_sid_change && b_service_name_change )
NewSDT( p_output );
}
}
/*****************************************************************************
* SetDTS
*****************************************************************************/
static void SetDTS( block_t *p_list )
{
int i_nb_ts = 0, i;
mtime_t i_duration;
block_t *p_ts = p_list;
while ( p_ts != NULL )
{
i_nb_ts++;
p_ts = p_ts->p_next;
}
/* We suppose the stream is CBR, at least between two consecutive read().
* This is especially true in budget mode */
if ( i_last_dts == -1 )
i_duration = 0;
else
i_duration = i_wallclock - i_last_dts;
p_ts = p_list;
i = i_nb_ts - 1;
while ( p_ts != NULL )
{
p_ts->i_dts = i_wallclock - i_duration * i / i_nb_ts;
i--;
p_ts = p_ts->p_next;
}
i_last_dts = i_wallclock;
}
/*****************************************************************************
* SetPID/UnsetPID
*****************************************************************************/
static void SetPID( uint16_t i_pid )
{
p_pids[i_pid].i_refcount++;
if ( !b_budget_mode && p_pids[i_pid].i_refcount
&& p_pids[i_pid].i_demux_fd == -1 )
p_pids[i_pid].i_demux_fd = pf_SetFilter( i_pid );
}
static void SetPID_EMM( uint16_t i_pid )
{
SetPID( i_pid );
p_pids[i_pid].b_emm = true;
}
static void UnsetPID( uint16_t i_pid )
{
p_pids[i_pid].i_refcount--;
if ( !b_budget_mode && !p_pids[i_pid].i_refcount
&& p_pids[i_pid].i_demux_fd != -1 )
{
pf_UnsetFilter( p_pids[i_pid].i_demux_fd, i_pid );
p_pids[i_pid].i_demux_fd = -1;
p_pids[i_pid].b_emm = false;
}
}
/*****************************************************************************
* StartPID/StopPID
*****************************************************************************/
static void StartPID( output_t *p_output, uint16_t i_pid )
{
int j;
for ( j = 0; j < p_pids[i_pid].i_nb_outputs; j++ )
if ( p_pids[i_pid].pp_outputs[j] == p_output )
break;
if ( j == p_pids[i_pid].i_nb_outputs )
{
for ( j = 0; j < p_pids[i_pid].i_nb_outputs; j++ )
if ( p_pids[i_pid].pp_outputs[j] == NULL )
break;
if ( j == p_pids[i_pid].i_nb_outputs )
{
p_pids[i_pid].i_nb_outputs++;
p_pids[i_pid].pp_outputs = realloc( p_pids[i_pid].pp_outputs,
sizeof(output_t *)
* p_pids[i_pid].i_nb_outputs );
}
p_pids[i_pid].pp_outputs[j] = p_output;
SetPID( i_pid );
}
}
static void StopPID( output_t *p_output, uint16_t i_pid )
{
int j;
for ( j = 0; j < p_pids[i_pid].i_nb_outputs; j++ )
{
if ( p_pids[i_pid].pp_outputs[j] != NULL )
{
if ( p_pids[i_pid].pp_outputs[j] == p_output )
break;
}
}
if ( j != p_pids[i_pid].i_nb_outputs )
{
p_pids[i_pid].pp_outputs[j] = NULL;
UnsetPID( i_pid );
}
}
/*****************************************************************************
* SelectPID/UnselectPID
*****************************************************************************/
static void SelectPID( uint16_t i_sid, uint16_t i_pid )
{
int i;
for ( i = 0; i < i_nb_outputs; i++ )
if ( (pp_outputs[i]->config.i_config & OUTPUT_VALID)
&& pp_outputs[i]->config.i_sid == i_sid
&& !pp_outputs[i]->config.i_nb_pids )
StartPID( pp_outputs[i], i_pid );
}
static void UnselectPID( uint16_t i_sid, uint16_t i_pid )
{
int i;
for ( i = 0; i < i_nb_outputs; i++ )
if ( (pp_outputs[i]->config.i_config & OUTPUT_VALID)
&& pp_outputs[i]->config.i_sid == i_sid
&& !pp_outputs[i]->config.i_nb_pids )
StopPID( pp_outputs[i], i_pid );
}
/*****************************************************************************
* SelectPMT/UnselectPMT
*****************************************************************************/
static void SelectPMT( uint16_t i_sid, uint16_t i_pid )
{
int i;
p_pids[i_pid].i_psi_refcount++;
p_pids[i_pid].b_pes = false;
if ( b_select_pmts )
SetPID( i_pid );
else for ( i = 0; i < i_nb_outputs; i++ )
if ( (pp_outputs[i]->config.i_config & OUTPUT_VALID)
&& pp_outputs[i]->config.i_sid == i_sid )
SetPID( i_pid );
}
static void UnselectPMT( uint16_t i_sid, uint16_t i_pid )
{
int i;
p_pids[i_pid].i_psi_refcount--;
if ( !p_pids[i_pid].i_psi_refcount )
psi_assemble_reset( &p_pids[i_pid].p_psi_buffer,
&p_pids[i_pid].i_psi_buffer_used );
if ( b_select_pmts )
UnsetPID( i_pid );
else for ( i = 0; i < i_nb_outputs; i++ )
if ( (pp_outputs[i]->config.i_config & OUTPUT_VALID)
&& pp_outputs[i]->config.i_sid == i_sid )
UnsetPID( i_pid );
}
/*****************************************************************************
* GetPIDS
*****************************************************************************/
static void GetPIDS( uint16_t **ppi_wanted_pids, int *pi_nb_wanted_pids,
uint16_t i_sid,
const uint16_t *pi_pids, int i_nb_pids )
{
sid_t *p_sid;
uint8_t *p_pmt;
uint16_t i_pmt_pid, i_pcr_pid;
uint8_t *p_es;
uint8_t j;
if ( i_nb_pids || i_sid == 0 )
{
*pi_nb_wanted_pids = i_nb_pids;
*ppi_wanted_pids = malloc( sizeof(uint16_t) * i_nb_pids );
memcpy( *ppi_wanted_pids, pi_pids, sizeof(uint16_t) * i_nb_pids );
return;
}
*pi_nb_wanted_pids = 0;
*ppi_wanted_pids = NULL;
p_sid = FindSID( i_sid );
if ( p_sid == NULL )
return;
p_pmt = p_sid->p_current_pmt;
i_pmt_pid = p_sid->i_pmt_pid;
if ( p_pmt == NULL ) {
msg_Dbg(NULL, "no current PMT on sid %d\n", i_sid);
return;
}
i_pcr_pid = pmt_get_pcrpid( p_pmt );
j = 0;
while ( (p_es = pmt_get_es( p_pmt, j )) != NULL )
{
j++;
if ( PIDWouldBeSelected( p_es ) )
{
*ppi_wanted_pids = realloc( *ppi_wanted_pids,
(*pi_nb_wanted_pids + 1) * sizeof(uint16_t) );
(*ppi_wanted_pids)[(*pi_nb_wanted_pids)++] = pmtn_get_pid( p_es );
}
}
if ( i_pcr_pid != PADDING_PID && i_pcr_pid != i_pmt_pid
&& !IsIn( *ppi_wanted_pids, *pi_nb_wanted_pids, i_pcr_pid ) )
{
*ppi_wanted_pids = realloc( *ppi_wanted_pids,
(*pi_nb_wanted_pids + 1) * sizeof(uint16_t) );
(*ppi_wanted_pids)[(*pi_nb_wanted_pids)++] = i_pcr_pid;
}
}
/*****************************************************************************
* OutputPSISection
*****************************************************************************/
static void OutputPSISection( output_t *p_output, uint8_t *p_section,
uint16_t i_pid, uint8_t *pi_cc, mtime_t i_dts,
block_t **pp_ts_buffer,
uint8_t *pi_ts_buffer_offset )
{
uint16_t i_section_length = psi_get_length(p_section) + PSI_HEADER_SIZE;
uint16_t i_section_offset = 0;
do
{
block_t *p_block;
uint8_t *p;
uint8_t i_ts_offset;
bool b_append = (pp_ts_buffer != NULL && *pp_ts_buffer != NULL);
if ( b_append )
{
p_block = *pp_ts_buffer;
i_ts_offset = *pi_ts_buffer_offset;
}
else
{
p_block = block_New();
p_block->i_dts = i_dts;
i_ts_offset = 0;
}
p = p_block->p_ts;
psi_split_section( p, &i_ts_offset, p_section, &i_section_offset );
if ( !b_append )
{
ts_set_pid( p, i_pid );