1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004, 2008-2011, 2015 VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
7 * Andy Gatward <a.j.gatward@reading.ac.uk>
8 * Marian Ďurkovič <md@bts.sk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
46 #include <bitstream/mpeg/ts.h>
47 #include <bitstream/mpeg/pes.h>
48 #include <bitstream/mpeg/psi.h>
49 #include <bitstream/dvb/si.h>
50 #include <bitstream/dvb/si_print.h>
51 #include <bitstream/mpeg/psi_print.h>
53 /*****************************************************************************
55 *****************************************************************************/
56 #define MIN_SECTION_FRAGMENT PSI_HEADER_SIZE_SYNTAX1
58 typedef struct ts_pid_t
65 /* b_emm is set to true when PID carries EMM packet
66 and should be outputed in all services */
69 /* PID info and stats */
71 unsigned long i_packets_passed
;
74 /* biTStream PSI section gathering */
75 uint8_t *p_psi_buffer
;
76 uint16_t i_psi_buffer_used
;
78 output_t
**pp_outputs
;
81 int i_pes_status
; /* pes + unscrambled */
82 struct ev_timer timeout_watcher
;
87 uint16_t i_sid
, i_pmt_pid
;
88 uint8_t *p_current_pmt
;
91 mtime_t i_wallclock
= 0;
93 static ts_pid_t p_pids
[MAX_PIDS
];
94 static sid_t
**pp_sids
= NULL
;
95 static int i_nb_sids
= 0;
97 static PSI_TABLE_DECLARE(pp_current_pat_sections
);
98 static PSI_TABLE_DECLARE(pp_next_pat_sections
);
99 static PSI_TABLE_DECLARE(pp_current_cat_sections
);
100 static PSI_TABLE_DECLARE(pp_next_cat_sections
);
101 static PSI_TABLE_DECLARE(pp_current_nit_sections
);
102 static PSI_TABLE_DECLARE(pp_next_nit_sections
);
103 static PSI_TABLE_DECLARE(pp_current_sdt_sections
);
104 static PSI_TABLE_DECLARE(pp_next_sdt_sections
);
105 static mtime_t i_last_dts
= -1;
106 static int i_demux_fd
;
107 static uint64_t i_nb_packets
= 0;
108 static uint64_t i_nb_invalids
= 0;
109 static uint64_t i_nb_discontinuities
= 0;
110 static uint64_t i_nb_errors
= 0;
111 static int i_tuner_errors
= 0;
112 static mtime_t i_last_error
= 0;
113 static mtime_t i_last_reset
= 0;
114 static struct ev_timer print_watcher
;
117 static iconv_t iconv_handle
= (iconv_t
)-1;
120 /*****************************************************************************
122 *****************************************************************************/
123 static void demux_Handle( block_t
*p_ts
);
124 static void SetDTS( block_t
*p_list
);
125 static void SetPID( uint16_t i_pid
);
126 static void SetPID_EMM( uint16_t i_pid
);
127 static void UnsetPID( uint16_t i_pid
);
128 static void StartPID( output_t
*p_output
, uint16_t i_pid
);
129 static void StopPID( output_t
*p_output
, uint16_t i_pid
);
130 static void SelectPID( uint16_t i_sid
, uint16_t i_pid
);
131 static void UnselectPID( uint16_t i_sid
, uint16_t i_pid
);
132 static void SelectPMT( uint16_t i_sid
, uint16_t i_pid
);
133 static void UnselectPMT( uint16_t i_sid
, uint16_t i_pid
);
134 static void GetPIDS( uint16_t **ppi_wanted_pids
, int *pi_nb_wanted_pids
,
136 const uint16_t *pi_pids
, int i_nb_pids
);
137 static bool SIDIsSelected( uint16_t i_sid
);
138 static bool PIDWouldBeSelected( uint8_t *p_es
);
139 static bool PMTNeedsDescrambling( uint8_t *p_pmt
);
140 static void FlushEIT( output_t
*p_output
, mtime_t i_dts
);
141 static void SendTDT( block_t
*p_ts
);
142 static void SendEMM( block_t
*p_ts
);
143 static void NewPAT( output_t
*p_output
);
144 static void NewPMT( output_t
*p_output
);
145 static void NewNIT( output_t
*p_output
);
146 static void NewSDT( output_t
*p_output
);
147 static void HandlePSIPacket( uint8_t *p_ts
, mtime_t i_dts
);
148 static const char *get_pid_desc(uint16_t i_pid
, uint16_t *i_sid
);
151 * Remap an ES pid to a fixed value.
152 * Multiple streams of the same type use sequential pids
153 * Returns the new pid and updates the map tables
155 static uint16_t map_es_pid(output_t
* p_output
, uint8_t *p_es
, uint16_t i_pid
)
157 uint16_t i_newpid
= i_pid
;
158 uint16_t i_stream_type
= pmtn_get_streamtype(p_es
);
160 if ( !b_do_remap
&& !p_output
->config
.b_do_remap
)
163 msg_Dbg(NULL
, "REMAP: Found elementary stream type 0x%02x with original PID 0x%x (%u):", i_stream_type
, i_pid
, i_pid
);
165 switch ( i_stream_type
)
167 case 0x03: /* audio MPEG-1 */
168 case 0x04: /* audio MPEG-2 */
169 case 0x0f: /* audio AAC ADTS */
170 case 0x11: /* audio AAC LATM */
171 case 0x81: /* ATSC AC-3 */
172 case 0x87: /* ATSC Enhanced AC-3 */
174 i_newpid
= pi_newpids
[I_APID
];
176 i_newpid
= p_output
->config
.pi_confpids
[I_APID
];
178 case 0x01: /* video MPEG-1 */
179 case 0x02: /* video MPEG-2 */
180 case 0x10: /* video MPEG-4 */
181 case 0x1b: /* video H264 */
182 case 0x24: /* video H265 */
183 case 0x42: /* video AVS */
185 i_newpid
= pi_newpids
[I_VPID
];
187 i_newpid
= p_output
->config
.pi_confpids
[I_VPID
];
189 case 0x06: { /* PES Private Data - We must check the descriptors */
190 /* By default, nothing identified */
191 uint8_t SubStreamType
= N_MAP_PIDS
;
193 const uint8_t *p_desc
;
194 /* Loop over the descriptors */
195 while ( (p_desc
= descs_get_desc( pmtn_get_descs( p_es
), j
)) != NULL
)
197 /* Get the descriptor tag */
198 uint8_t i_tag
= desc_get_tag( p_desc
);
200 /* Check if the tag is: A/52, Enhanced A/52, DTS, AAC */
201 if (i_tag
== 0x6a || i_tag
== 0x7a || i_tag
== 0x7b || i_tag
== 0x7c)
202 SubStreamType
=I_APID
;
203 /* Check if the tag is: VBI + teletext, teletext, dvbsub */
204 if (i_tag
== 0x46 || i_tag
== 0x56 || i_tag
== 0x59)
205 SubStreamType
=I_SPUPID
;
208 if (SubStreamType
==I_APID
) {
209 msg_Dbg(NULL
, "REMAP: PES Private Data stream identified as [Audio]");
211 i_newpid
= pi_newpids
[I_APID
];
213 i_newpid
= p_output
->config
.pi_confpids
[I_APID
];
216 if (SubStreamType
==I_SPUPID
) {
217 msg_Dbg(NULL
, "REMAP: PES Private Data stream identified as [Subtitle]");
219 i_newpid
= pi_newpids
[I_SPUPID
];
221 i_newpid
= p_output
->config
.pi_confpids
[I_SPUPID
];
230 /* Got the new base for the mapped pid. Find the next free one
231 we do this to ensure that multiple audios get unique pids */
232 while (p_output
->pi_freepids
[i_newpid
] != UNUSED_PID
)
234 p_output
->pi_freepids
[i_newpid
] = i_pid
; /* Mark as in use */
235 p_output
->pi_newpids
[i_pid
] = i_newpid
; /* Save the new pid */
237 msg_Dbg(NULL
, "REMAP: => Elementary stream is remapped to PID 0x%x (%u)", i_newpid
, i_newpid
);
242 /*****************************************************************************
244 *****************************************************************************/
245 static inline sid_t
*FindSID( uint16_t i_sid
)
249 for ( i
= 0; i
< i_nb_sids
; i
++ )
251 sid_t
*p_sid
= pp_sids
[i
];
252 if ( p_sid
->i_sid
== i_sid
)
258 /*****************************************************************************
260 *****************************************************************************/
261 static void PrintCb( struct ev_loop
*loop
, struct ev_timer
*w
, int revents
)
263 uint64_t i_bitrate
= i_nb_packets
* TS_SIZE
* 8 * 1000000 / i_print_period
;
264 switch (i_print_type
)
268 "<STATUS type=\"bitrate\" status=\"%d\" value=\"%"PRIu64
"\" />\n",
269 i_bitrate
? 1 : 0, i_bitrate
);
272 fprintf(print_fh
, "bitrate: %"PRIu64
"\n", i_bitrate
);
281 switch (i_print_type
)
285 "<ERROR type=\"invalid_ts\" number=\"%"PRIu64
"\" />\n",
289 fprintf(print_fh
, "invalids: %"PRIu64
"\n", i_nb_invalids
);
297 if ( i_nb_discontinuities
)
299 switch (i_print_type
)
303 "<ERROR type=\"invalid_discontinuity\" number=\"%"PRIu64
"\" />\n",
304 i_nb_discontinuities
);
307 fprintf(print_fh
, "discontinuities: %"PRIu64
"\n",
308 i_nb_discontinuities
);
313 i_nb_discontinuities
= 0;
318 switch (i_print_type
)
322 "<ERROR type=\"transport_error\" number=\"%"PRIu64
"\" />\n",
326 fprintf(print_fh
, "errors: %"PRIu64
"\n", i_nb_errors
);
335 static void PrintESCb( struct ev_loop
*loop
, struct ev_timer
*w
, int revents
)
337 ts_pid_t
*p_pid
= container_of( w
, ts_pid_t
, timeout_watcher
);
338 uint16_t i_pid
= p_pid
- p_pids
;
340 switch (i_print_type
)
344 "<STATUS type=\"pid\" pid=\"%"PRIu16
"\" status=\"0\" />\n",
348 fprintf(print_fh
, "pid: %"PRIu16
" down\n", i_pid
);
354 ev_timer_stop( loop
, w
);
355 p_pid
->i_pes_status
= -1;
358 static void PrintES( uint16_t i_pid
)
360 const ts_pid_t
*p_pid
= &p_pids
[i_pid
];
362 switch (i_print_type
)
366 "<STATUS type=\"pid\" pid=\"%"PRIu16
"\" status=\"1\" pes=\"%d\" />\n",
367 i_pid
, p_pid
->i_pes_status
== 1 ? 1 : 0);
370 fprintf(print_fh
, "pid: %"PRIu16
" up%s\n",
371 i_pid
, p_pid
->i_pes_status
== 1 ? " pes" : "");
378 /*****************************************************************************
380 *****************************************************************************/
381 void demux_Open( void )
385 memset( p_pids
, 0, sizeof(p_pids
) );
389 for ( i
= 0; i
< MAX_PIDS
; i
++ )
391 p_pids
[i
].i_last_cc
= -1;
392 p_pids
[i
].i_demux_fd
= -1;
393 psi_assemble_init( &p_pids
[i
].p_psi_buffer
,
394 &p_pids
[i
].i_psi_buffer_used
);
395 p_pids
[i
].i_pes_status
= -1;
399 i_demux_fd
= pf_SetFilter(8192);
401 psi_table_init( pp_current_pat_sections
);
402 psi_table_init( pp_next_pat_sections
);
404 p_pids
[PAT_PID
].i_psi_refcount
++;
408 psi_table_init( pp_current_cat_sections
);
409 psi_table_init( pp_next_cat_sections
);
411 p_pids
[CAT_PID
].i_psi_refcount
++;
415 p_pids
[NIT_PID
].i_psi_refcount
++;
417 psi_table_init( pp_current_sdt_sections
);
418 psi_table_init( pp_next_sdt_sections
);
420 p_pids
[SDT_PID
].i_psi_refcount
++;
423 p_pids
[EIT_PID
].i_psi_refcount
++;
429 if ( i_print_period
)
431 ev_timer_init( &print_watcher
, PrintCb
,
432 i_print_period
/ 1000000., i_print_period
/ 1000000. );
433 ev_timer_start( event_loop
, &print_watcher
);
437 /*****************************************************************************
439 *****************************************************************************/
440 void demux_Close( void )
444 psi_table_free( pp_current_pat_sections
);
445 psi_table_free( pp_next_pat_sections
);
446 psi_table_free( pp_current_cat_sections
);
447 psi_table_free( pp_next_cat_sections
);
448 psi_table_free( pp_current_nit_sections
);
449 psi_table_free( pp_next_nit_sections
);
450 psi_table_free( pp_current_sdt_sections
);
451 psi_table_free( pp_next_sdt_sections
);
453 for ( i
= 0; i
< MAX_PIDS
; i
++ )
455 ev_timer_stop( event_loop
, &p_pids
[i
].timeout_watcher
);
456 free( p_pids
[i
].p_psi_buffer
);
457 free( p_pids
[i
].pp_outputs
);
460 for ( i
= 0; i
< i_nb_sids
; i
++ )
462 sid_t
*p_sid
= pp_sids
[i
];
463 free( p_sid
->p_current_pmt
);
469 if (iconv_handle
!= (iconv_t
)-1) {
470 iconv_close(iconv_handle
);
471 iconv_handle
= (iconv_t
)-1;
475 if ( i_print_period
)
476 ev_timer_stop( event_loop
, &print_watcher
);
479 /*****************************************************************************
481 *****************************************************************************/
482 void demux_Run( block_t
*p_ts
)
484 i_wallclock
= mdate();
488 while ( p_ts
!= NULL
)
490 block_t
*p_next
= p_ts
->p_next
;
492 demux_Handle( p_ts
);
497 /*****************************************************************************
499 *****************************************************************************/
500 static void demux_Handle( block_t
*p_ts
)
502 uint16_t i_pid
= ts_get_pid( p_ts
->p_ts
);
503 ts_pid_t
*p_pid
= &p_pids
[i_pid
];
504 uint8_t i_cc
= ts_get_cc( p_ts
->p_ts
);
509 if ( !ts_validate( p_ts
->p_ts
) )
511 msg_Warn( NULL
, "lost TS sync" );
512 block_Delete( p_ts
);
517 if ( i_pid
!= PADDING_PID
)
518 p_pid
->info
.i_scrambling
= ts_get_scrambling( p_ts
->p_ts
);
520 p_pid
->info
.i_last_packet_ts
= i_wallclock
;
521 p_pid
->info
.i_packets
++;
523 p_pid
->i_packets_passed
++;
525 /* Calculate bytes_per_sec */
526 if ( i_wallclock
> p_pid
->i_bytes_ts
+ 1000000 ) {
527 p_pid
->info
.i_bytes_per_sec
= p_pid
->i_packets_passed
* TS_SIZE
;
528 p_pid
->i_packets_passed
= 0;
529 p_pid
->i_bytes_ts
= i_wallclock
;
532 if ( p_pid
->info
.i_first_packet_ts
== 0 )
533 p_pid
->info
.i_first_packet_ts
= i_wallclock
;
535 if ( i_pid
!= PADDING_PID
&& p_pid
->i_last_cc
!= -1
536 && !ts_check_duplicate( i_cc
, p_pid
->i_last_cc
)
537 && ts_check_discontinuity( i_cc
, p_pid
->i_last_cc
) )
539 unsigned int expected_cc
= (p_pid
->i_last_cc
+ 1) & 0x0f;
541 const char *pid_desc
= get_pid_desc(i_pid
, &i_sid
);
543 p_pid
->info
.i_cc_errors
++;
544 i_nb_discontinuities
++;
546 msg_Warn( NULL
, "TS discontinuity on pid %4hu expected_cc %2u got %2u (%s, sid %d)",
547 i_pid
, expected_cc
, i_cc
, pid_desc
, i_sid
);
550 if ( ts_get_transporterror( p_ts
->p_ts
) )
553 const char *pid_desc
= get_pid_desc(i_pid
, &i_sid
);
555 p_pid
->info
.i_transport_errors
++;
557 msg_Warn( NULL
, "transport_error_indicator on pid %hu (%s, sid %u)",
558 i_pid
, pid_desc
, i_sid
);
562 i_last_error
= i_wallclock
;
564 else if ( i_wallclock
> i_last_error
+ WATCHDOG_WAIT
)
567 if ( i_tuner_errors
> MAX_ERRORS
)
571 "too many transport errors, tuning again" );
572 switch (i_print_type
) {
574 fprintf(print_fh
, "<EVENT type=\"reset\" cause=\"transport\" />\n");
577 fprintf(print_fh
, "reset cause: transport\n");
587 int i_pes_status
= -1;
588 if ( ts_get_scrambling( p_ts
->p_ts
) )
590 else if ( ts_get_unitstart( p_ts
->p_ts
) )
592 uint8_t *p_payload
= ts_payload( p_ts
->p_ts
);
593 if ( p_payload
+ 3 < p_ts
->p_ts
+ TS_SIZE
)
594 i_pes_status
= pes_validate( p_payload
) ? 1 : 0;
597 if ( i_pes_status
!= -1 )
599 if ( p_pid
->i_pes_status
== -1 )
601 p_pid
->i_pes_status
= i_pes_status
;
604 if ( i_pid
!= TDT_PID
)
606 ev_timer_init( &p_pid
->timeout_watcher
, PrintESCb
,
607 i_es_timeout
/ 1000000.,
608 i_es_timeout
/ 1000000. );
609 ev_timer_start( event_loop
, &p_pid
->timeout_watcher
);
613 ev_timer_init( &p_pid
->timeout_watcher
, PrintESCb
, 30, 30 );
614 ev_timer_start( event_loop
, &p_pid
->timeout_watcher
);
619 if ( p_pid
->i_pes_status
!= i_pes_status
)
621 p_pid
->i_pes_status
= i_pes_status
;
625 ev_timer_again( event_loop
, &p_pid
->timeout_watcher
);
630 if ( !ts_get_transporterror( p_ts
->p_ts
) )
633 if ( i_pid
== TDT_PID
|| i_pid
== RST_PID
)
635 else if ( p_pid
->i_psi_refcount
)
636 HandlePSIPacket( p_ts
->p_ts
, p_ts
->i_dts
);
638 if ( b_enable_emm
&& p_pid
->b_emm
)
642 p_pid
->i_last_cc
= i_cc
;
645 for ( i
= 0; i
< p_pid
->i_nb_outputs
; i
++ )
647 output_t
*p_output
= p_pid
->pp_outputs
[i
];
648 if ( p_output
!= NULL
)
650 if ( i_ca_handle
&& (p_output
->config
.i_config
& OUTPUT_WATCH
) &&
651 ts_get_unitstart( p_ts
->p_ts
) )
655 if ( ts_get_scrambling( p_ts
->p_ts
) ||
657 && (p_payload
= ts_payload( p_ts
->p_ts
)) + 3
658 < p_ts
->p_ts
+ TS_SIZE
659 && !pes_validate(p_payload
) ) )
662 i_last_reset
+ WATCHDOG_REFRACTORY_PERIOD
)
664 p_output
->i_nb_errors
++;
665 p_output
->i_last_error
= i_wallclock
;
668 else if ( i_wallclock
> p_output
->i_last_error
+ WATCHDOG_WAIT
)
669 p_output
->i_nb_errors
= 0;
671 if ( p_output
->i_nb_errors
> MAX_ERRORS
)
674 for ( j
= 0; j
< i_nb_outputs
; j
++ )
675 pp_outputs
[j
]->i_nb_errors
= 0;
678 "too many errors for stream %s, resetting",
679 p_output
->config
.psz_displayname
);
681 switch (i_print_type
) {
683 fprintf(print_fh
, "<EVENT type=\"reset\" cause=\"scrambling\" />\n");
686 fprintf(print_fh
, "reset cause: scrambling");
691 i_last_reset
= i_wallclock
;
696 output_Put( p_output
, p_ts
);
698 if ( p_output
->p_eit_ts_buffer
!= NULL
699 && p_ts
->i_dts
> p_output
->p_eit_ts_buffer
->i_dts
700 + MAX_EIT_RETENTION
)
701 FlushEIT( p_output
, p_ts
->i_dts
);
705 for ( i
= 0; i
< i_nb_outputs
; i
++ )
707 output_t
*p_output
= pp_outputs
[i
];
709 if ( !(p_output
->config
.i_config
& OUTPUT_VALID
) ||
710 !p_output
->config
.b_passthrough
)
713 output_Put( p_output
, p_ts
);
716 if ( output_dup
.config
.i_config
& OUTPUT_VALID
)
717 output_Put( &output_dup
, p_ts
);
720 if ( !p_ts
->i_refcount
)
721 block_Delete( p_ts
);
724 /*****************************************************************************
725 * demux_Change : called from main thread
726 *****************************************************************************/
727 static int IsIn( uint16_t *pi_pids
, int i_nb_pids
, uint16_t i_pid
)
730 for ( i
= 0; i
< i_nb_pids
; i
++ )
731 if ( i_pid
== pi_pids
[i
] ) break;
732 return ( i
!= i_nb_pids
);
735 void demux_Change( output_t
*p_output
, const output_config_t
*p_config
)
737 uint16_t *pi_wanted_pids
, *pi_current_pids
;
738 int i_nb_wanted_pids
, i_nb_current_pids
;
740 uint16_t i_old_sid
= p_output
->config
.i_sid
;
741 uint16_t i_sid
= p_config
->i_sid
;
742 uint16_t *pi_old_pids
= p_output
->config
.pi_pids
;
743 uint16_t *pi_pids
= p_config
->pi_pids
;
744 int i_old_nb_pids
= p_output
->config
.i_nb_pids
;
745 int i_nb_pids
= p_config
->i_nb_pids
;
747 bool b_sid_change
= i_sid
!= i_old_sid
;
748 bool b_pid_change
= false, b_tsid_change
= false;
749 bool b_dvb_change
= !!((p_output
->config
.i_config
^ p_config
->i_config
)
751 bool b_network_change
=
752 (dvb_string_cmp(&p_output
->config
.network_name
, &p_config
->network_name
) ||
753 p_output
->config
.i_network_id
!= p_config
->i_network_id
);
754 bool b_service_name_change
=
755 (dvb_string_cmp(&p_output
->config
.service_name
, &p_config
->service_name
) ||
756 dvb_string_cmp(&p_output
->config
.provider_name
, &p_config
->provider_name
));
757 bool b_remap_change
= p_output
->config
.i_new_sid
!= p_config
->i_new_sid
||
758 p_output
->config
.i_onid
!= p_config
->i_onid
||
759 p_output
->config
.b_do_remap
!= p_config
->b_do_remap
||
760 p_output
->config
.pi_confpids
[I_PMTPID
] != p_config
->pi_confpids
[I_PMTPID
] ||
761 p_output
->config
.pi_confpids
[I_APID
] != p_config
->pi_confpids
[I_APID
] ||
762 p_output
->config
.pi_confpids
[I_VPID
] != p_config
->pi_confpids
[I_VPID
] ||
763 p_output
->config
.pi_confpids
[I_SPUPID
] != p_config
->pi_confpids
[I_SPUPID
];
766 p_output
->config
.i_config
= p_config
->i_config
;
767 p_output
->config
.i_network_id
= p_config
->i_network_id
;
768 p_output
->config
.i_new_sid
= p_config
->i_new_sid
;
769 p_output
->config
.i_onid
= p_config
->i_onid
;
770 p_output
->config
.b_do_remap
= p_config
->b_do_remap
;
771 memcpy(p_output
->config
.pi_confpids
, p_config
->pi_confpids
,
772 sizeof(uint16_t) * N_MAP_PIDS
);
774 /* Change output settings related to names. */
775 dvb_string_clean( &p_output
->config
.network_name
);
776 dvb_string_clean( &p_output
->config
.service_name
);
777 dvb_string_clean( &p_output
->config
.provider_name
);
778 dvb_string_copy( &p_output
->config
.network_name
,
779 &p_config
->network_name
);
780 dvb_string_copy( &p_output
->config
.service_name
,
781 &p_config
->service_name
);
782 dvb_string_copy( &p_output
->config
.provider_name
,
783 &p_config
->provider_name
);
785 if ( p_config
->i_tsid
!= -1 && p_output
->config
.i_tsid
!= p_config
->i_tsid
)
787 p_output
->i_tsid
= p_config
->i_tsid
;
788 b_tsid_change
= true;
790 if ( p_config
->i_tsid
== -1 && p_output
->config
.i_tsid
!= -1 )
792 if ( psi_table_validate(pp_current_pat_sections
) && !b_random_tsid
)
794 psi_table_get_tableidext(pp_current_pat_sections
);
796 p_output
->i_tsid
= rand() & 0xffff;
797 b_tsid_change
= true;
800 if ( p_config
->b_passthrough
== p_output
->config
.b_passthrough
&&
801 !b_sid_change
&& p_config
->i_nb_pids
== p_output
->config
.i_nb_pids
&&
802 (!p_config
->i_nb_pids
||
803 !memcmp( p_output
->config
.pi_pids
, p_config
->pi_pids
,
804 p_config
->i_nb_pids
* sizeof(uint16_t) )) )
807 GetPIDS( &pi_wanted_pids
, &i_nb_wanted_pids
, i_sid
, pi_pids
, i_nb_pids
);
808 GetPIDS( &pi_current_pids
, &i_nb_current_pids
, i_old_sid
, pi_old_pids
,
811 if ( b_sid_change
&& i_old_sid
)
813 sid_t
*p_old_sid
= FindSID( i_old_sid
);
814 p_output
->config
.i_sid
= p_config
->i_sid
;
816 if ( p_old_sid
!= NULL
)
818 if ( i_sid
!= i_old_sid
)
819 UnselectPMT( i_old_sid
, p_old_sid
->i_pmt_pid
);
821 if ( i_ca_handle
&& !SIDIsSelected( i_old_sid
)
822 && p_old_sid
->p_current_pmt
!= NULL
823 && PMTNeedsDescrambling( p_old_sid
->p_current_pmt
) )
824 en50221_DeletePMT( p_old_sid
->p_current_pmt
);
828 for ( i
= 0; i
< i_nb_current_pids
; i
++ )
830 if ( !IsIn( pi_wanted_pids
, i_nb_wanted_pids
, pi_current_pids
[i
] ) )
832 StopPID( p_output
, pi_current_pids
[i
] );
837 if ( b_sid_change
&& i_ca_handle
&& i_old_sid
&&
838 SIDIsSelected( i_old_sid
) )
840 sid_t
*p_old_sid
= FindSID( i_old_sid
);
841 if ( p_old_sid
!= NULL
&& p_old_sid
->p_current_pmt
!= NULL
842 && PMTNeedsDescrambling( p_old_sid
->p_current_pmt
) )
843 en50221_UpdatePMT( p_old_sid
->p_current_pmt
);
846 for ( i
= 0; i
< i_nb_wanted_pids
; i
++ )
848 if ( !IsIn( pi_current_pids
, i_nb_current_pids
, pi_wanted_pids
[i
] ) )
850 StartPID( p_output
, pi_wanted_pids
[i
] );
855 free( pi_wanted_pids
);
856 free( pi_current_pids
);
858 if ( b_sid_change
&& i_sid
)
860 sid_t
*p_sid
= FindSID( i_sid
);
861 p_output
->config
.i_sid
= i_old_sid
;
865 if ( i_sid
!= i_old_sid
)
866 SelectPMT( i_sid
, p_sid
->i_pmt_pid
);
868 if ( i_ca_handle
&& !SIDIsSelected( i_sid
)
869 && p_sid
->p_current_pmt
!= NULL
870 && PMTNeedsDescrambling( p_sid
->p_current_pmt
) )
871 en50221_AddPMT( p_sid
->p_current_pmt
);
875 if ( i_ca_handle
&& i_sid
&& SIDIsSelected( i_sid
) )
877 sid_t
*p_sid
= FindSID( i_sid
);
878 if ( p_sid
!= NULL
&& p_sid
->p_current_pmt
!= NULL
879 && PMTNeedsDescrambling( p_sid
->p_current_pmt
) )
880 en50221_UpdatePMT( p_sid
->p_current_pmt
);
883 p_output
->config
.b_passthrough
= p_config
->b_passthrough
;
884 p_output
->config
.i_sid
= i_sid
;
885 free( p_output
->config
.pi_pids
);
886 p_output
->config
.pi_pids
= malloc( sizeof(uint16_t) * i_nb_pids
);
887 memcpy( p_output
->config
.pi_pids
, pi_pids
, sizeof(uint16_t) * i_nb_pids
);
888 p_output
->config
.i_nb_pids
= i_nb_pids
;
891 if ( b_sid_change
|| b_remap_change
)
906 else if ( b_dvb_change
)
911 else if ( b_network_change
)
914 if ( !b_tsid_change
&& b_service_name_change
)
922 /*****************************************************************************
924 *****************************************************************************/
925 static void SetDTS( block_t
*p_list
)
929 block_t
*p_ts
= p_list
;
931 while ( p_ts
!= NULL
)
937 /* We suppose the stream is CBR, at least between two consecutive read().
938 * This is especially true in budget mode */
939 if ( i_last_dts
== -1 )
942 i_duration
= i_wallclock
- i_last_dts
;
946 while ( p_ts
!= NULL
)
948 p_ts
->i_dts
= i_wallclock
- i_duration
* i
/ i_nb_ts
;
953 i_last_dts
= i_wallclock
;
956 /*****************************************************************************
958 *****************************************************************************/
959 static void SetPID( uint16_t i_pid
)
961 p_pids
[i_pid
].i_refcount
++;
963 if ( !b_budget_mode
&& p_pids
[i_pid
].i_refcount
964 && p_pids
[i_pid
].i_demux_fd
== -1 )
965 p_pids
[i_pid
].i_demux_fd
= pf_SetFilter( i_pid
);
968 static void SetPID_EMM( uint16_t i_pid
)
971 p_pids
[i_pid
].b_emm
= true;
974 static void UnsetPID( uint16_t i_pid
)
976 p_pids
[i_pid
].i_refcount
--;
978 if ( !b_budget_mode
&& !p_pids
[i_pid
].i_refcount
979 && p_pids
[i_pid
].i_demux_fd
!= -1 )
981 pf_UnsetFilter( p_pids
[i_pid
].i_demux_fd
, i_pid
);
982 p_pids
[i_pid
].i_demux_fd
= -1;
983 p_pids
[i_pid
].b_emm
= false;
987 /*****************************************************************************
989 *****************************************************************************/
990 static void StartPID( output_t
*p_output
, uint16_t i_pid
)
994 for ( j
= 0; j
< p_pids
[i_pid
].i_nb_outputs
; j
++ )
995 if ( p_pids
[i_pid
].pp_outputs
[j
] == p_output
)
998 if ( j
== p_pids
[i_pid
].i_nb_outputs
)
1000 for ( j
= 0; j
< p_pids
[i_pid
].i_nb_outputs
; j
++ )
1001 if ( p_pids
[i_pid
].pp_outputs
[j
] == NULL
)
1004 if ( j
== p_pids
[i_pid
].i_nb_outputs
)
1006 p_pids
[i_pid
].i_nb_outputs
++;
1007 p_pids
[i_pid
].pp_outputs
= realloc( p_pids
[i_pid
].pp_outputs
,
1009 * p_pids
[i_pid
].i_nb_outputs
);
1012 p_pids
[i_pid
].pp_outputs
[j
] = p_output
;
1017 static void StopPID( output_t
*p_output
, uint16_t i_pid
)
1021 for ( j
= 0; j
< p_pids
[i_pid
].i_nb_outputs
; j
++ )
1023 if ( p_pids
[i_pid
].pp_outputs
[j
] != NULL
)
1025 if ( p_pids
[i_pid
].pp_outputs
[j
] == p_output
)
1030 if ( j
!= p_pids
[i_pid
].i_nb_outputs
)
1032 p_pids
[i_pid
].pp_outputs
[j
] = NULL
;
1037 /*****************************************************************************
1038 * SelectPID/UnselectPID
1039 *****************************************************************************/
1040 static void SelectPID( uint16_t i_sid
, uint16_t i_pid
)
1044 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1045 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
1046 && pp_outputs
[i
]->config
.i_sid
== i_sid
1047 && !pp_outputs
[i
]->config
.i_nb_pids
)
1048 StartPID( pp_outputs
[i
], i_pid
);
1051 static void UnselectPID( uint16_t i_sid
, uint16_t i_pid
)
1055 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1056 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
1057 && pp_outputs
[i
]->config
.i_sid
== i_sid
1058 && !pp_outputs
[i
]->config
.i_nb_pids
)
1059 StopPID( pp_outputs
[i
], i_pid
);
1062 /*****************************************************************************
1063 * SelectPMT/UnselectPMT
1064 *****************************************************************************/
1065 static void SelectPMT( uint16_t i_sid
, uint16_t i_pid
)
1069 p_pids
[i_pid
].i_psi_refcount
++;
1070 p_pids
[i_pid
].b_pes
= false;
1072 if ( b_select_pmts
)
1074 else for ( i
= 0; i
< i_nb_outputs
; i
++ )
1075 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
1076 && pp_outputs
[i
]->config
.i_sid
== i_sid
)
1080 static void UnselectPMT( uint16_t i_sid
, uint16_t i_pid
)
1084 p_pids
[i_pid
].i_psi_refcount
--;
1085 if ( !p_pids
[i_pid
].i_psi_refcount
)
1086 psi_assemble_reset( &p_pids
[i_pid
].p_psi_buffer
,
1087 &p_pids
[i_pid
].i_psi_buffer_used
);
1089 if ( b_select_pmts
)
1091 else for ( i
= 0; i
< i_nb_outputs
; i
++ )
1092 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
1093 && pp_outputs
[i
]->config
.i_sid
== i_sid
)
1097 /*****************************************************************************
1099 *****************************************************************************/
1100 static void GetPIDS( uint16_t **ppi_wanted_pids
, int *pi_nb_wanted_pids
,
1102 const uint16_t *pi_pids
, int i_nb_pids
)
1106 uint16_t i_pmt_pid
, i_pcr_pid
;
1109 const uint8_t *p_desc
;
1111 if ( i_nb_pids
|| i_sid
== 0 )
1113 *pi_nb_wanted_pids
= i_nb_pids
;
1114 *ppi_wanted_pids
= malloc( sizeof(uint16_t) * i_nb_pids
);
1115 memcpy( *ppi_wanted_pids
, pi_pids
, sizeof(uint16_t) * i_nb_pids
);
1119 *pi_nb_wanted_pids
= 0;
1120 *ppi_wanted_pids
= NULL
;
1122 p_sid
= FindSID( i_sid
);
1123 if ( p_sid
== NULL
)
1126 p_pmt
= p_sid
->p_current_pmt
;
1127 i_pmt_pid
= p_sid
->i_pmt_pid
;
1128 if ( p_pmt
== NULL
) {
1129 msg_Dbg(NULL
, "no current PMT on sid %d\n", i_sid
);
1133 i_pcr_pid
= pmt_get_pcrpid( p_pmt
);
1135 while ( (p_es
= pmt_get_es( p_pmt
, j
)) != NULL
)
1138 if ( PIDWouldBeSelected( p_es
) )
1140 *ppi_wanted_pids
= realloc( *ppi_wanted_pids
,
1141 (*pi_nb_wanted_pids
+ 1) * sizeof(uint16_t) );
1142 (*ppi_wanted_pids
)[(*pi_nb_wanted_pids
)++] = pmtn_get_pid( p_es
);
1149 while ((p_desc
= descs_get_desc( pmtn_get_descs( p_es
), k
++ )) != NULL
)
1151 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
1153 *ppi_wanted_pids
= realloc( *ppi_wanted_pids
,
1154 (*pi_nb_wanted_pids
+ 1) * sizeof(uint16_t) );
1155 (*ppi_wanted_pids
)[(*pi_nb_wanted_pids
)++] = desc09_get_pid( p_desc
);
1165 while ((p_desc
= descs_get_desc( pmt_get_descs( p_pmt
), j
++ )) != NULL
)
1167 if ( desc_get_tag( p_desc
) != 0x09 ||
1168 !desc09_validate( p_desc
) )
1170 *ppi_wanted_pids
= realloc( *ppi_wanted_pids
,
1171 (*pi_nb_wanted_pids
+ 1) * sizeof(uint16_t) );
1172 (*ppi_wanted_pids
)[(*pi_nb_wanted_pids
)++] = desc09_get_pid( p_desc
);
1176 if ( i_pcr_pid
!= PADDING_PID
&& i_pcr_pid
!= i_pmt_pid
1177 && !IsIn( *ppi_wanted_pids
, *pi_nb_wanted_pids
, i_pcr_pid
) )
1179 *ppi_wanted_pids
= realloc( *ppi_wanted_pids
,
1180 (*pi_nb_wanted_pids
+ 1) * sizeof(uint16_t) );
1181 (*ppi_wanted_pids
)[(*pi_nb_wanted_pids
)++] = i_pcr_pid
;
1185 /*****************************************************************************
1187 *****************************************************************************/
1188 static void OutputPSISection( output_t
*p_output
, uint8_t *p_section
,
1189 uint16_t i_pid
, uint8_t *pi_cc
, mtime_t i_dts
,
1190 block_t
**pp_ts_buffer
,
1191 uint8_t *pi_ts_buffer_offset
)
1193 uint16_t i_section_length
= psi_get_length(p_section
) + PSI_HEADER_SIZE
;
1194 uint16_t i_section_offset
= 0;
1200 uint8_t i_ts_offset
;
1201 bool b_append
= (pp_ts_buffer
!= NULL
&& *pp_ts_buffer
!= NULL
);
1205 p_block
= *pp_ts_buffer
;
1206 i_ts_offset
= *pi_ts_buffer_offset
;
1210 p_block
= block_New();
1211 p_block
->i_dts
= i_dts
;
1216 psi_split_section( p
, &i_ts_offset
, p_section
, &i_section_offset
);
1220 ts_set_pid( p
, i_pid
);
1221 ts_set_cc( p
, *pi_cc
);
1226 if ( i_section_offset
== i_section_length
)
1228 if ( i_ts_offset
< TS_SIZE
- MIN_SECTION_FRAGMENT
1229 && pp_ts_buffer
!= NULL
)
1231 *pp_ts_buffer
= p_block
;
1232 *pi_ts_buffer_offset
= i_ts_offset
;
1236 psi_split_end( p
, &i_ts_offset
);
1239 p_block
->i_dts
= i_dts
;
1240 p_block
->i_refcount
--;
1241 output_Put( p_output
, p_block
);
1242 if ( pp_ts_buffer
!= NULL
)
1244 *pp_ts_buffer
= NULL
;
1245 *pi_ts_buffer_offset
= 0;
1248 while ( i_section_offset
< i_section_length
);
1251 /*****************************************************************************
1253 *****************************************************************************/
1254 static void SendPAT( mtime_t i_dts
)
1258 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1260 output_t
*p_output
= pp_outputs
[i
];
1262 if ( !(p_output
->config
.i_config
& OUTPUT_VALID
) ||
1263 p_output
->config
.b_passthrough
)
1266 if ( p_output
->p_pat_section
== NULL
&&
1267 psi_table_validate(pp_current_pat_sections
) )
1269 /* SID doesn't exist - build an empty PAT. */
1271 p_output
->i_pat_version
++;
1273 p
= p_output
->p_pat_section
= psi_allocate();
1275 pat_set_length( p
, 0 );
1276 pat_set_tsid( p
, p_output
->i_tsid
);
1277 psi_set_version( p
, p_output
->i_pat_version
);
1278 psi_set_current( p
);
1279 psi_set_section( p
, 0 );
1280 psi_set_lastsection( p
, 0 );
1281 psi_set_crc( p_output
->p_pat_section
);
1285 if ( p_output
->p_pat_section
!= NULL
)
1286 OutputPSISection( p_output
, p_output
->p_pat_section
, PAT_PID
,
1287 &p_output
->i_pat_cc
, i_dts
, NULL
, NULL
);
1291 /*****************************************************************************
1293 *****************************************************************************/
1294 static void SendPMT( sid_t
*p_sid
, mtime_t i_dts
)
1297 int i_pmt_pid
= p_sid
->i_pmt_pid
;
1300 i_pmt_pid
= pi_newpids
[ I_PMTPID
];
1302 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1304 output_t
*p_output
= pp_outputs
[i
];
1306 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1307 && p_output
->config
.i_sid
== p_sid
->i_sid
1308 && p_output
->p_pmt_section
!= NULL
)
1310 if ( p_output
->config
.b_do_remap
&& p_output
->config
.pi_confpids
[I_PMTPID
] )
1311 i_pmt_pid
= p_output
->config
.pi_confpids
[I_PMTPID
];
1313 OutputPSISection( p_output
, p_output
->p_pmt_section
,
1314 i_pmt_pid
, &p_output
->i_pmt_cc
, i_dts
,
1320 /*****************************************************************************
1322 *****************************************************************************/
1323 static void SendNIT( mtime_t i_dts
)
1327 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1329 output_t
*p_output
= pp_outputs
[i
];
1331 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1332 && !p_output
->config
.b_passthrough
1333 && (p_output
->config
.i_config
& OUTPUT_DVB
)
1334 && p_output
->p_nit_section
!= NULL
)
1335 OutputPSISection( p_output
, p_output
->p_nit_section
, NIT_PID
,
1336 &p_output
->i_nit_cc
, i_dts
, NULL
, NULL
);
1340 /*****************************************************************************
1342 *****************************************************************************/
1343 static void SendSDT( mtime_t i_dts
)
1347 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1349 output_t
*p_output
= pp_outputs
[i
];
1351 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1352 && !p_output
->config
.b_passthrough
1353 && (p_output
->config
.i_config
& OUTPUT_DVB
)
1354 && p_output
->p_sdt_section
!= NULL
)
1355 OutputPSISection( p_output
, p_output
->p_sdt_section
, SDT_PID
,
1356 &p_output
->i_sdt_cc
, i_dts
, NULL
, NULL
);
1360 /*****************************************************************************
1362 *****************************************************************************/
1363 static void SendEIT( sid_t
*p_sid
, mtime_t i_dts
, uint8_t *p_eit
)
1365 uint8_t i_table_id
= psi_get_tableid( p_eit
);
1366 bool b_epg
= i_table_id
>= EIT_TABLE_ID_SCHED_ACTUAL_FIRST
&&
1367 i_table_id
<= EIT_TABLE_ID_SCHED_ACTUAL_LAST
;
1370 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1372 output_t
*p_output
= pp_outputs
[i
];
1374 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1375 && !p_output
->config
.b_passthrough
1376 && (p_output
->config
.i_config
& OUTPUT_DVB
)
1377 && (!b_epg
|| (p_output
->config
.i_config
& OUTPUT_EPG
))
1378 && p_output
->config
.i_sid
== p_sid
->i_sid
)
1380 eit_set_tsid( p_eit
, p_output
->i_tsid
);
1382 if ( p_output
->config
.i_new_sid
)
1383 eit_set_sid( p_eit
, p_output
->config
.i_new_sid
);
1385 eit_set_sid( p_eit
, p_output
->config
.i_sid
);
1387 psi_set_crc( p_eit
);
1389 OutputPSISection( p_output
, p_eit
, EIT_PID
, &p_output
->i_eit_cc
,
1390 i_dts
, &p_output
->p_eit_ts_buffer
,
1391 &p_output
->i_eit_ts_buffer_offset
);
1396 /*****************************************************************************
1398 *****************************************************************************/
1399 static void FlushEIT( output_t
*p_output
, mtime_t i_dts
)
1401 block_t
*p_block
= p_output
->p_eit_ts_buffer
;
1403 psi_split_end( p_block
->p_ts
, &p_output
->i_eit_ts_buffer_offset
);
1404 p_block
->i_dts
= i_dts
;
1405 p_block
->i_refcount
--;
1406 output_Put( p_output
, p_block
);
1407 p_output
->p_eit_ts_buffer
= NULL
;
1408 p_output
->i_eit_ts_buffer_offset
= 0;
1411 /*****************************************************************************
1413 *****************************************************************************/
1414 static void SendTDT( block_t
*p_ts
)
1418 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1420 output_t
*p_output
= pp_outputs
[i
];
1422 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1423 && !p_output
->config
.b_passthrough
1424 && (p_output
->config
.i_config
& OUTPUT_DVB
)
1425 && p_output
->p_sdt_section
!= NULL
)
1426 output_Put( p_output
, p_ts
);
1429 /*****************************************************************************
1431 *****************************************************************************/
1432 static void SendEMM( block_t
*p_ts
)
1436 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1438 output_t
*p_output
= pp_outputs
[i
];
1440 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1441 && !p_output
->config
.b_passthrough
)
1442 output_Put( p_output
, p_ts
);
1446 /*****************************************************************************
1448 *****************************************************************************/
1449 static void NewPAT( output_t
*p_output
)
1451 const uint8_t *p_program
;
1455 free( p_output
->p_pat_section
);
1456 p_output
->p_pat_section
= NULL
;
1457 p_output
->i_pat_version
++;
1459 if ( !p_output
->config
.i_sid
) return;
1460 if ( !psi_table_validate(pp_current_pat_sections
) ) return;
1462 p_program
= pat_table_find_program( pp_current_pat_sections
,
1463 p_output
->config
.i_sid
);
1464 if ( p_program
== NULL
) return;
1466 p
= p_output
->p_pat_section
= psi_allocate();
1468 psi_set_length( p
, PSI_MAX_SIZE
);
1469 pat_set_tsid( p
, p_output
->i_tsid
);
1470 psi_set_version( p
, p_output
->i_pat_version
);
1471 psi_set_current( p
);
1472 psi_set_section( p
, 0 );
1473 psi_set_lastsection( p
, 0 );
1475 if ( p_output
->config
.i_config
& OUTPUT_DVB
)
1478 p
= pat_get_program( p_output
->p_pat_section
, k
++ );
1480 patn_set_program( p
, 0 );
1481 patn_set_pid( p
, NIT_PID
);
1484 p
= pat_get_program( p_output
->p_pat_section
, k
++ );
1486 if ( p_output
->config
.i_new_sid
)
1488 msg_Dbg( NULL
, "Mapping PAT SID %d to %d", p_output
->config
.i_sid
,
1489 p_output
->config
.i_new_sid
);
1490 patn_set_program( p
, p_output
->config
.i_new_sid
);
1493 patn_set_program( p
, p_output
->config
.i_sid
);
1497 msg_Dbg( NULL
, "Mapping PMT PID %d to %d", patn_get_pid( p_program
), pi_newpids
[I_PMTPID
] );
1498 patn_set_pid( p
, pi_newpids
[I_PMTPID
]);
1499 } else if ( p_output
->config
.b_do_remap
&& p_output
->config
.pi_confpids
[I_PMTPID
] ) {
1500 msg_Dbg( NULL
, "Mapping PMT PID %d to %d", patn_get_pid( p_program
), p_output
->config
.pi_confpids
[I_PMTPID
] );
1501 patn_set_pid( p
, p_output
->config
.pi_confpids
[I_PMTPID
]);
1503 patn_set_pid( p
, patn_get_pid( p_program
) );
1506 p
= pat_get_program( p_output
->p_pat_section
, k
);
1507 pat_set_length( p_output
->p_pat_section
,
1508 p
- p_output
->p_pat_section
- PAT_HEADER_SIZE
);
1509 psi_set_crc( p_output
->p_pat_section
);
1512 /*****************************************************************************
1514 *****************************************************************************/
1515 static void CopyDescriptors( uint8_t *p_descs
, uint8_t *p_current_descs
)
1518 const uint8_t *p_current_desc
;
1519 uint16_t j
= 0, k
= 0;
1521 descs_set_length( p_descs
, DESCS_MAX_SIZE
);
1523 while ( (p_current_desc
= descs_get_desc( p_current_descs
, j
)) != NULL
)
1525 uint8_t i_tag
= desc_get_tag( p_current_desc
);
1528 if ( !b_enable_ecm
&& i_tag
== 0x9 ) continue;
1530 p_desc
= descs_get_desc( p_descs
, k
);
1531 if ( p_desc
== NULL
) continue; /* This shouldn't happen */
1533 memcpy( p_desc
, p_current_desc
,
1534 DESC_HEADER_SIZE
+ desc_get_length( p_current_desc
) );
1537 p_desc
= descs_get_desc( p_descs
, k
);
1538 if ( p_desc
== NULL
)
1539 /* This shouldn't happen if the incoming PMT is valid */
1540 descs_set_length( p_descs
, 0 );
1542 descs_set_length( p_descs
, p_desc
- p_descs
- DESCS_HEADER_SIZE
);
1545 static void NewPMT( output_t
*p_output
)
1548 uint8_t *p_current_pmt
;
1549 uint8_t *p_es
, *p_current_es
;
1554 free( p_output
->p_pmt_section
);
1555 p_output
->p_pmt_section
= NULL
;
1556 p_output
->i_pmt_version
++;
1558 if ( !p_output
->config
.i_sid
) return;
1560 p_sid
= FindSID( p_output
->config
.i_sid
);
1561 if ( p_sid
== NULL
) return;
1563 if ( p_sid
->p_current_pmt
== NULL
) return;
1564 p_current_pmt
= p_sid
->p_current_pmt
;
1566 p
= p_output
->p_pmt_section
= psi_allocate();
1568 psi_set_length( p
, PSI_MAX_SIZE
);
1569 if ( p_output
->config
.i_new_sid
)
1571 msg_Dbg( NULL
, "Mapping PMT SID %d to %d", p_output
->config
.i_sid
,
1572 p_output
->config
.i_new_sid
);
1573 pmt_set_program( p
, p_output
->config
.i_new_sid
);
1576 pmt_set_program( p
, p_output
->config
.i_sid
);
1577 psi_set_version( p
, p_output
->i_pmt_version
);
1578 psi_set_current( p
);
1579 pmt_set_desclength( p
, 0 );
1580 init_pid_mapping( p_output
);
1583 CopyDescriptors( pmt_get_descs( p
), pmt_get_descs( p_current_pmt
) );
1586 while ( (p_current_es
= pmt_get_es( p_current_pmt
, j
)) != NULL
)
1588 uint16_t i_pid
= pmtn_get_pid( p_current_es
);
1591 if ( (p_output
->config
.i_nb_pids
|| !PIDWouldBeSelected( p_current_es
))
1592 && !IsIn( p_output
->config
.pi_pids
, p_output
->config
.i_nb_pids
,
1596 p_es
= pmt_get_es( p
, k
);
1597 if ( p_es
== NULL
) continue; /* This shouldn't happen */
1600 pmtn_set_streamtype( p_es
, pmtn_get_streamtype( p_current_es
) );
1601 pmtn_set_pid( p_es
, map_es_pid(p_output
, p_current_es
, i_pid
) );
1602 pmtn_set_desclength( p_es
, 0 );
1604 CopyDescriptors( pmtn_get_descs( p_es
),
1605 pmtn_get_descs( p_current_es
) );
1608 /* Do the pcr pid after everything else as it may have been remapped */
1609 i_pcrpid
= pmt_get_pcrpid( p_current_pmt
);
1610 if ( p_output
->pi_newpids
[i_pcrpid
] != UNUSED_PID
) {
1611 msg_Dbg( NULL
, "REMAP: The PCR PID was changed from 0x%x (%u) to 0x%x (%u)",
1612 i_pcrpid
, i_pcrpid
, p_output
->pi_newpids
[i_pcrpid
], p_output
->pi_newpids
[i_pcrpid
] );
1613 i_pcrpid
= p_output
->pi_newpids
[i_pcrpid
];
1615 msg_Dbg( NULL
, "The PCR PID has kept its original value of 0x%x (%u)", i_pcrpid
, i_pcrpid
);
1617 pmt_set_pcrpid( p
, i_pcrpid
);
1618 p_es
= pmt_get_es( p
, k
);
1620 /* This shouldn't happen if the incoming PMT is valid */
1621 pmt_set_length( p
, 0 );
1623 pmt_set_length( p
, p_es
- p
- PMT_HEADER_SIZE
);
1627 /*****************************************************************************
1629 *****************************************************************************/
1630 static void NewNIT( output_t
*p_output
)
1636 free( p_output
->p_nit_section
);
1637 p_output
->p_nit_section
= NULL
;
1638 p_output
->i_nit_version
++;
1640 p
= p_output
->p_nit_section
= psi_allocate();
1641 nit_init( p
, true );
1642 nit_set_length( p
, PSI_MAX_SIZE
);
1643 nit_set_nid( p
, p_output
->config
.i_network_id
);
1644 psi_set_version( p
, p_output
->i_nit_version
);
1645 psi_set_current( p
);
1646 psi_set_section( p
, 0 );
1647 psi_set_lastsection( p
, 0 );
1649 if ( p_output
->config
.network_name
.i
)
1653 nit_set_desclength( p
, DESCS_MAX_SIZE
);
1654 p_descs
= nit_get_descs( p
);
1655 p_desc
= descs_get_desc( p_descs
, 0 );
1656 desc40_init( p_desc
);
1657 desc40_set_networkname( p_desc
, p_output
->config
.network_name
.p
,
1658 p_output
->config
.network_name
.i
);
1659 p_desc
= descs_get_desc( p_descs
, 1 );
1660 descs_set_length( p_descs
, p_desc
- p_descs
- DESCS_HEADER_SIZE
);
1663 nit_set_desclength( p
, 0 );
1665 p_header2
= nit_get_header2( p
);
1666 nith_init( p_header2
);
1667 nith_set_tslength( p_header2
, NIT_TS_SIZE
);
1669 p_ts
= nit_get_ts( p
, 0 );
1671 nitn_set_tsid( p_ts
, p_output
->i_tsid
);
1672 if ( p_output
->config
.i_onid
)
1673 nitn_set_onid( p_ts
, p_output
->config
.i_onid
);
1675 nitn_set_onid( p_ts
, p_output
->config
.i_network_id
);
1676 nitn_set_desclength( p_ts
, 0 );
1678 p_ts
= nit_get_ts( p
, 1 );
1680 /* This shouldn't happen */
1681 nit_set_length( p
, 0 );
1683 nit_set_length( p
, p_ts
- p
- NIT_HEADER_SIZE
);
1684 psi_set_crc( p_output
->p_nit_section
);
1687 /*****************************************************************************
1689 *****************************************************************************/
1690 static void NewSDT( output_t
*p_output
)
1692 uint8_t *p_service
, *p_current_service
;
1695 free( p_output
->p_sdt_section
);
1696 p_output
->p_sdt_section
= NULL
;
1697 p_output
->i_sdt_version
++;
1699 if ( !p_output
->config
.i_sid
) return;
1700 if ( !psi_table_validate(pp_current_sdt_sections
) ) return;
1702 p_current_service
= sdt_table_find_service( pp_current_sdt_sections
,
1703 p_output
->config
.i_sid
);
1705 if ( p_current_service
== NULL
)
1707 if ( p_output
->p_pat_section
!= NULL
&&
1708 pat_get_program( p_output
->p_pat_section
, 0 ) == NULL
)
1710 /* Empty PAT and no SDT anymore */
1711 free( p_output
->p_pat_section
);
1712 p_output
->p_pat_section
= NULL
;
1713 p_output
->i_pat_version
++;
1718 p
= p_output
->p_sdt_section
= psi_allocate();
1719 sdt_init( p
, true );
1720 sdt_set_length( p
, PSI_MAX_SIZE
);
1721 sdt_set_tsid( p
, p_output
->i_tsid
);
1722 psi_set_version( p
, p_output
->i_sdt_version
);
1723 psi_set_current( p
);
1724 psi_set_section( p
, 0 );
1725 psi_set_lastsection( p
, 0 );
1726 if ( p_output
->config
.i_onid
)
1727 sdt_set_onid( p
, p_output
->config
.i_onid
);
1730 sdt_get_onid( psi_table_get_section( pp_current_sdt_sections
, 0 ) ) );
1732 p_service
= sdt_get_service( p
, 0 );
1733 sdtn_init( p_service
);
1734 if ( p_output
->config
.i_new_sid
)
1736 msg_Dbg( NULL
, "Mapping SDT SID %d to %d", p_output
->config
.i_sid
,
1737 p_output
->config
.i_new_sid
);
1738 sdtn_set_sid( p_service
, p_output
->config
.i_new_sid
);
1741 sdtn_set_sid( p_service
, p_output
->config
.i_sid
);
1743 if ( (p_output
->config
.i_config
& OUTPUT_EPG
) == OUTPUT_EPG
)
1745 sdtn_set_eitschedule(p_service
);
1746 sdtn_set_eitpresent(p_service
);
1748 if ( sdtn_get_eitschedule(p_current_service
) )
1749 sdtn_set_eitschedule(p_service
);
1750 if ( sdtn_get_eitpresent(p_current_service
) )
1751 sdtn_set_eitpresent(p_service
);
1754 sdtn_set_running( p_service
, sdtn_get_running(p_current_service
) );
1755 /* Do not set free_ca */
1756 sdtn_set_desclength( p_service
, sdtn_get_desclength(p_current_service
) );
1758 if ( !p_output
->config
.provider_name
.i
&&
1759 !p_output
->config
.service_name
.i
) {
1760 /* Copy all descriptors unchanged */
1761 memcpy( descs_get_desc( sdtn_get_descs(p_service
), 0 ),
1762 descs_get_desc( sdtn_get_descs(p_current_service
), 0 ),
1763 sdtn_get_desclength(p_current_service
) );
1765 int j
= 0, i_total_desc_len
= 0;
1767 uint8_t *p_new_desc
= descs_get_desc( sdtn_get_descs(p_service
), 0 );
1768 while ( (p_desc
= descs_get_desc( sdtn_get_descs( p_current_service
), j
++ )) != NULL
)
1770 /* Regenerate descriptor 48 (service name) */
1771 if ( desc_get_tag( p_desc
) == 0x48 && desc48_validate( p_desc
) )
1773 uint8_t i_old_provider_len
, i_old_service_len
;
1774 uint8_t i_new_desc_len
= 3; /* 1 byte - type, 1 byte provider_len, 1 byte service_len */
1775 const uint8_t *p_old_provider
= desc48_get_provider( p_desc
, &i_old_provider_len
);
1776 const uint8_t *p_old_service
= desc48_get_service( p_desc
, &i_old_service_len
);
1778 desc48_init( p_new_desc
);
1779 desc48_set_type( p_new_desc
, desc48_get_type( p_desc
) );
1781 if ( p_output
->config
.provider_name
.i
) {
1782 desc48_set_provider( p_new_desc
,
1783 p_output
->config
.provider_name
.p
,
1784 p_output
->config
.provider_name
.i
);
1785 i_new_desc_len
+= p_output
->config
.provider_name
.i
;
1787 desc48_set_provider( p_new_desc
, p_old_provider
,
1788 i_old_provider_len
);
1789 i_new_desc_len
+= i_old_provider_len
;
1792 if ( p_output
->config
.service_name
.i
) {
1793 desc48_set_service( p_new_desc
,
1794 p_output
->config
.service_name
.p
,
1795 p_output
->config
.service_name
.i
);
1796 i_new_desc_len
+= p_output
->config
.service_name
.i
;
1798 desc48_set_service( p_new_desc
, p_old_service
,
1799 i_old_service_len
);
1800 i_new_desc_len
+= i_old_service_len
;
1803 desc_set_length( p_new_desc
, i_new_desc_len
);
1804 i_total_desc_len
+= DESC_HEADER_SIZE
+ i_new_desc_len
;
1805 p_new_desc
+= DESC_HEADER_SIZE
+ i_new_desc_len
;
1807 /* Copy single descriptor */
1808 int i_desc_len
= DESC_HEADER_SIZE
+ desc_get_length( p_desc
);
1809 memcpy( p_new_desc
, p_desc
, i_desc_len
);
1810 p_new_desc
+= i_desc_len
;
1811 i_total_desc_len
+= i_desc_len
;
1814 sdtn_set_desclength( p_service
, i_total_desc_len
);
1817 p_service
= sdt_get_service( p
, 1 );
1818 if ( p_service
== NULL
)
1819 /* This shouldn't happen if the incoming SDT is valid */
1820 sdt_set_length( p
, 0 );
1822 sdt_set_length( p
, p_service
- p
- SDT_HEADER_SIZE
);
1823 psi_set_crc( p_output
->p_sdt_section
);
1826 /*****************************************************************************
1828 *****************************************************************************/
1829 #define DECLARE_UPDATE_FUNC( table ) \
1830 static void Update##table( uint16_t i_sid ) \
1834 for ( i = 0; i < i_nb_outputs; i++ ) \
1835 if ( ( pp_outputs[i]->config.i_config & OUTPUT_VALID ) \
1836 && pp_outputs[i]->config.i_sid == i_sid ) \
1837 New##table( pp_outputs[i] ); \
1840 DECLARE_UPDATE_FUNC(PAT
)
1841 DECLARE_UPDATE_FUNC(PMT
)
1842 DECLARE_UPDATE_FUNC(SDT
)
1844 /*****************************************************************************
1846 *****************************************************************************/
1847 static void UpdateTSID(void)
1849 uint16_t i_tsid
= psi_table_get_tableidext(pp_current_pat_sections
);
1852 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1854 output_t
*p_output
= pp_outputs
[i
];
1856 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1857 && p_output
->config
.i_tsid
== -1 && !b_random_tsid
)
1859 p_output
->i_tsid
= i_tsid
;
1865 /*****************************************************************************
1867 *****************************************************************************/
1868 static bool SIDIsSelected( uint16_t i_sid
)
1872 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1873 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
1874 && pp_outputs
[i
]->config
.i_sid
== i_sid
)
1880 /*****************************************************************************
1881 * demux_PIDIsSelected
1882 *****************************************************************************/
1883 bool demux_PIDIsSelected( uint16_t i_pid
)
1887 for ( i
= 0; i
< p_pids
[i_pid
].i_nb_outputs
; i
++ )
1888 if ( p_pids
[i_pid
].pp_outputs
[i
] != NULL
)
1894 /*****************************************************************************
1895 * PIDWouldBeSelected
1896 *****************************************************************************/
1897 static bool PIDWouldBeSelected( uint8_t *p_es
)
1899 if ( b_any_type
) return true;
1901 uint8_t i_type
= pmtn_get_streamtype( p_es
);
1905 case 0x1: /* video MPEG-1 */
1906 case 0x2: /* video */
1907 case 0x3: /* audio MPEG-1 */
1908 case 0x4: /* audio */
1909 case 0xf: /* audio AAC ADTS */
1910 case 0x10: /* video MPEG-4 */
1911 case 0x11: /* audio AAC LATM */
1912 case 0x1b: /* video H264 */
1913 case 0x81: /* ATSC A/52 */
1914 case 0x87: /* ATSC Enhanced A/52 */
1921 const uint8_t *p_desc
;
1923 while ( (p_desc
= descs_get_desc( pmtn_get_descs( p_es
), j
)) != NULL
)
1925 uint8_t i_tag
= desc_get_tag( p_desc
);
1928 if( i_tag
== 0x46 /* VBI + teletext */
1929 || i_tag
== 0x56 /* teletext */
1930 || i_tag
== 0x59 /* dvbsub */
1931 || i_tag
== 0x6a /* A/52 */
1932 || i_tag
== 0x7a /* Enhanced A/52 */
1933 || i_tag
== 0x7b /* DCA */
1934 || i_tag
== 0x7c /* AAC */ )
1944 /* FIXME: also parse IOD */
1948 /*****************************************************************************
1950 *****************************************************************************/
1951 static bool PIDCarriesPES( const uint8_t *p_es
)
1953 uint8_t i_type
= pmtn_get_streamtype( p_es
);
1957 case 0x1: /* video MPEG-1 */
1958 case 0x2: /* video */
1959 case 0x3: /* audio MPEG-1 */
1960 case 0x4: /* audio */
1961 case 0x6: /* private PES data */
1962 case 0xf: /* audio AAC */
1963 case 0x10: /* video MPEG-4 */
1964 case 0x11: /* audio AAC LATM */
1965 case 0x1b: /* video H264 */
1966 case 0x81: /* ATSC A/52 */
1967 case 0x87: /* ATSC Enhanced A/52 */
1977 /*****************************************************************************
1978 * PMTNeedsDescrambling
1979 *****************************************************************************/
1980 static bool PMTNeedsDescrambling( uint8_t *p_pmt
)
1985 const uint8_t *p_desc
;
1988 while ( (p_desc
= descs_get_desc( pmt_get_descs( p_pmt
), j
)) != NULL
)
1990 uint8_t i_tag
= desc_get_tag( p_desc
);
1993 if ( i_tag
== 0x9 ) return true;
1997 while ( (p_es
= pmt_get_es( p_pmt
, i
)) != NULL
)
2001 while ( (p_desc
= descs_get_desc( pmtn_get_descs( p_es
), j
)) != NULL
)
2003 uint8_t i_tag
= desc_get_tag( p_desc
);
2006 if ( i_tag
== 0x9 ) return true;
2013 /*****************************************************************************
2014 * demux_ResendCAPMTs
2015 *****************************************************************************/
2016 void demux_ResendCAPMTs( void )
2019 for ( i
= 0; i
< i_nb_sids
; i
++ )
2020 if ( pp_sids
[i
]->p_current_pmt
!= NULL
2021 && SIDIsSelected( pp_sids
[i
]->i_sid
)
2022 && PMTNeedsDescrambling( pp_sids
[i
]->p_current_pmt
) )
2023 en50221_AddPMT( pp_sids
[i
]->p_current_pmt
);
2026 /* Find CA descriptor that have PID i_ca_pid */
2027 static uint8_t *ca_desc_find( uint8_t *p_descl
, uint16_t i_length
,
2033 while ( (p_desc
= descl_get_desc( p_descl
, i_length
, j
++ )) != NULL
) {
2034 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2036 if ( desc09_get_pid( p_desc
) == i_ca_pid
)
2043 /*****************************************************************************
2045 *****************************************************************************/
2046 static void DeleteProgram( uint16_t i_sid
, uint16_t i_pid
)
2052 UnselectPMT( i_sid
, i_pid
);
2054 p_sid
= FindSID( i_sid
);
2055 if ( p_sid
== NULL
) return;
2057 p_pmt
= p_sid
->p_current_pmt
;
2059 if ( p_pmt
!= NULL
)
2061 uint16_t i_pcr_pid
= pmt_get_pcrpid( p_pmt
);
2065 if ( i_ca_handle
&& SIDIsSelected( i_sid
)
2066 && PMTNeedsDescrambling( p_pmt
) )
2067 en50221_DeletePMT( p_pmt
);
2069 if ( i_pcr_pid
!= PADDING_PID
2070 && i_pcr_pid
!= p_sid
->i_pmt_pid
)
2071 UnselectPID( i_sid
, i_pcr_pid
);
2077 while ((p_desc
= descs_get_desc( pmt_get_descs( p_pmt
), j
++ )) != NULL
)
2079 if ( desc_get_tag( p_desc
) != 0x09 ||
2080 !desc09_validate( p_desc
) )
2082 UnselectPID( i_sid
, desc09_get_pid( p_desc
) );
2087 while ( (p_es
= pmt_get_es( p_pmt
, j
)) != NULL
)
2089 uint16_t i_pid
= pmtn_get_pid( p_es
);
2092 if ( PIDWouldBeSelected( p_es
) )
2093 UnselectPID( i_sid
, i_pid
);
2099 while ((p_desc
= descs_get_desc( pmtn_get_descs( p_es
), k
++ )) != NULL
)
2101 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2103 UnselectPID( i_sid
, desc09_get_pid( p_desc
) );
2109 p_sid
->p_current_pmt
= NULL
;
2112 p_sid
->i_pmt_pid
= 0;
2115 /*****************************************************************************
2117 *****************************************************************************
2118 * This code is from biTStream's examples and is under the WTFPL (see
2120 *****************************************************************************/
2121 static char *iconv_append_null(const char *p_string
, size_t i_length
)
2123 char *psz_string
= malloc(i_length
+ 1);
2124 memcpy(psz_string
, p_string
, i_length
);
2125 psz_string
[i_length
] = '\0';
2129 char *demux_Iconv(void *_unused
, const char *psz_encoding
,
2130 char *p_string
, size_t i_length
)
2133 static const char *psz_current_encoding
= "";
2135 char *psz_string
, *p
;
2136 size_t i_out_length
;
2138 if (!strcmp(psz_encoding
, psz_native_charset
))
2139 return iconv_append_null(p_string
, i_length
);
2141 if (iconv_handle
!= (iconv_t
)-1 &&
2142 strcmp(psz_encoding
, psz_current_encoding
)) {
2143 iconv_close(iconv_handle
);
2144 iconv_handle
= (iconv_t
)-1;
2147 if (iconv_handle
== (iconv_t
)-1)
2148 iconv_handle
= iconv_open(psz_native_charset
, psz_encoding
);
2149 if (iconv_handle
== (iconv_t
)-1) {
2150 msg_Warn(NULL
, "couldn't open converter from %s to %s (%m)", psz_encoding
,
2151 psz_native_charset
);
2152 return iconv_append_null(p_string
, i_length
);
2154 psz_current_encoding
= psz_encoding
;
2156 /* converted strings can be up to six times larger */
2157 i_out_length
= i_length
* 6;
2158 p
= psz_string
= malloc(i_out_length
);
2159 if (iconv(iconv_handle
, &p_string
, &i_length
, &p
, &i_out_length
) == -1) {
2160 msg_Warn(NULL
, "couldn't convert from %s to %s (%m)", psz_encoding
,
2161 psz_native_charset
);
2163 return iconv_append_null(p_string
, i_length
);
2166 msg_Warn(NULL
, "partial conversion from %s to %s", psz_encoding
,
2167 psz_native_charset
);
2172 return iconv_append_null(p_string
, i_length
);
2176 /*****************************************************************************
2178 *****************************************************************************
2179 * This code is from biTStream's examples and is under the WTFPL (see
2181 *****************************************************************************/
2182 __attribute__ ((format(printf
, 2, 3)))
2183 static void demux_Print(void *_unused
, const char *psz_format
, ...)
2185 char psz_fmt
[strlen(psz_format
) + 2];
2187 va_start(args
, psz_format
);
2188 strcpy(psz_fmt
, psz_format
);
2189 if ( i_print_type
!= PRINT_XML
)
2190 strcat(psz_fmt
, "\n");
2191 vprintf(psz_fmt
, args
);
2195 /*****************************************************************************
2197 *****************************************************************************/
2198 static void HandlePAT( mtime_t i_dts
)
2200 bool b_change
= false;
2201 PSI_TABLE_DECLARE( pp_old_pat_sections
);
2202 uint8_t i_last_section
= psi_table_get_lastsection( pp_next_pat_sections
);
2205 if ( psi_table_validate( pp_current_pat_sections
) &&
2206 psi_table_compare( pp_current_pat_sections
, pp_next_pat_sections
) )
2208 /* Identical PAT. Shortcut. */
2209 psi_table_free( pp_next_pat_sections
);
2210 psi_table_init( pp_next_pat_sections
);
2214 if ( !pat_table_validate( pp_next_pat_sections
) )
2216 msg_Warn( NULL
, "invalid PAT received" );
2217 switch (i_print_type
) {
2219 fprintf(print_fh
, "<ERROR type=\"invalid_pat\"/>\n");
2222 fprintf(print_fh
, "error type: invalid_pat\n");
2227 psi_table_free( pp_next_pat_sections
);
2228 psi_table_init( pp_next_pat_sections
);
2232 /* Switch tables. */
2233 psi_table_copy( pp_old_pat_sections
, pp_current_pat_sections
);
2234 psi_table_copy( pp_current_pat_sections
, pp_next_pat_sections
);
2235 psi_table_init( pp_next_pat_sections
);
2237 if ( !psi_table_validate( pp_old_pat_sections
)
2238 || psi_table_get_tableidext( pp_current_pat_sections
)
2239 != psi_table_get_tableidext( pp_old_pat_sections
) )
2243 /* This will trigger a universal reset of everything. */
2246 for ( i
= 0; i
<= i_last_section
; i
++ )
2248 uint8_t *p_section
=
2249 psi_table_get_section( pp_current_pat_sections
, i
);
2250 const uint8_t *p_program
;
2253 while ( (p_program
= pat_get_program( p_section
, j
)) != NULL
)
2255 const uint8_t *p_old_program
= NULL
;
2256 uint16_t i_sid
= patn_get_program( p_program
);
2257 uint16_t i_pid
= patn_get_pid( p_program
);
2262 if ( i_pid
!= NIT_PID
)
2264 "NIT is carried on PID %hu which isn't DVB compliant",
2269 if ( !psi_table_validate( pp_old_pat_sections
)
2270 || (p_old_program
= pat_table_find_program(
2271 pp_old_pat_sections
, i_sid
)) == NULL
2272 || patn_get_pid( p_old_program
) != i_pid
2277 if ( p_old_program
!= NULL
)
2278 DeleteProgram( i_sid
, patn_get_pid( p_old_program
) );
2280 SelectPMT( i_sid
, i_pid
);
2282 p_sid
= FindSID( 0 );
2283 if ( p_sid
== NULL
)
2285 p_sid
= malloc( sizeof(sid_t
) );
2286 p_sid
->p_current_pmt
= NULL
;
2288 pp_sids
= realloc( pp_sids
, sizeof(sid_t
*) * i_nb_sids
);
2289 pp_sids
[i_nb_sids
- 1] = p_sid
;
2292 p_sid
->i_sid
= i_sid
;
2293 p_sid
->i_pmt_pid
= i_pid
;
2300 if ( psi_table_validate( pp_old_pat_sections
) )
2302 i_last_section
= psi_table_get_lastsection( pp_old_pat_sections
);
2303 for ( i
= 0; i
<= i_last_section
; i
++ )
2305 uint8_t *p_section
=
2306 psi_table_get_section( pp_old_pat_sections
, i
);
2307 const uint8_t *p_program
;
2310 while ( (p_program
= pat_get_program( p_section
, j
)) != NULL
)
2312 uint16_t i_sid
= patn_get_program( p_program
);
2313 uint16_t i_pid
= patn_get_pid( p_program
);
2319 if ( pat_table_find_program( pp_current_pat_sections
, i_sid
)
2322 DeleteProgram( i_sid
, i_pid
);
2328 psi_table_free( pp_old_pat_sections
);
2331 pat_table_print( pp_current_pat_sections
, msg_Dbg
, NULL
, PRINT_TEXT
);
2332 if ( b_print_enabled
)
2334 pat_table_print( pp_current_pat_sections
, demux_Print
, NULL
,
2336 if ( i_print_type
== PRINT_XML
)
2337 fprintf(print_fh
, "\n");
2344 /*****************************************************************************
2346 *****************************************************************************/
2347 static void HandlePATSection( uint16_t i_pid
, uint8_t *p_section
,
2350 if ( i_pid
!= PAT_PID
|| !pat_validate( p_section
) )
2352 msg_Warn( NULL
, "invalid PAT section received on PID %hu", i_pid
);
2353 switch (i_print_type
) {
2355 fprintf(print_fh
, "<ERROR type=\"invalid_pat_section\"/>\n");
2358 fprintf(print_fh
, "error type: invalid_pat_section\n");
2367 if ( !psi_table_section( pp_next_pat_sections
, p_section
) )
2373 /*****************************************************************************
2375 *****************************************************************************/
2376 static void HandleCAT( mtime_t i_dts
)
2378 PSI_TABLE_DECLARE( pp_old_cat_sections
);
2379 uint8_t i_last_section
= psi_table_get_lastsection( pp_next_cat_sections
);
2380 uint8_t i_last_section2
;
2385 if ( psi_table_validate( pp_current_cat_sections
) &&
2386 psi_table_compare( pp_current_cat_sections
, pp_next_cat_sections
) )
2388 /* Identical CAT. Shortcut. */
2389 psi_table_free( pp_next_cat_sections
);
2390 psi_table_init( pp_next_cat_sections
);
2394 if ( !cat_table_validate( pp_next_cat_sections
) )
2396 msg_Warn( NULL
, "invalid CAT received" );
2397 switch (i_print_type
) {
2399 fprintf(print_fh
, "<ERROR type=\"invalid_cat\"/>\n");
2402 fprintf(print_fh
, "error type: invalid_cat\n");
2407 psi_table_free( pp_next_cat_sections
);
2408 psi_table_init( pp_next_cat_sections
);
2412 /* Switch tables. */
2413 psi_table_copy( pp_old_cat_sections
, pp_current_cat_sections
);
2414 psi_table_copy( pp_current_cat_sections
, pp_next_cat_sections
);
2415 psi_table_init( pp_next_cat_sections
);
2417 for ( i
= 0; i
<= i_last_section
; i
++ )
2419 uint8_t *p_section
= psi_table_get_section( pp_current_cat_sections
, i
);
2422 while ( (p_desc
= descl_get_desc( cat_get_descl(p_section
), cat_get_desclength(p_section
), j
++ )) != NULL
)
2424 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2427 SetPID_EMM( desc09_get_pid( p_desc
) );
2431 if ( psi_table_validate( pp_old_cat_sections
) )
2433 i_last_section
= psi_table_get_lastsection( pp_old_cat_sections
);
2434 for ( i
= 0; i
<= i_last_section
; i
++ )
2436 uint8_t *p_old_section
= psi_table_get_section( pp_old_cat_sections
, i
);
2438 while ( (p_desc
= descl_get_desc( cat_get_descl(p_old_section
), cat_get_desclength(p_old_section
), j
++ )) != NULL
)
2443 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2446 emm_pid
= desc09_get_pid( p_desc
);
2448 // Search in current sections if the pid exists
2449 i_last_section2
= psi_table_get_lastsection( pp_current_cat_sections
);
2450 for ( r
= 0; r
<= i_last_section2
; r
++ )
2452 uint8_t *p_section
= psi_table_get_section( pp_current_cat_sections
, r
);
2455 while ( (p_desc
= descl_get_desc( cat_get_descl(p_section
), cat_get_desclength(p_section
), k
++ )) != NULL
)
2457 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2459 if ( ca_desc_find( cat_get_descl(p_section
), cat_get_desclength(p_section
), emm_pid
) != NULL
)
2472 psi_table_free( pp_old_cat_sections
);
2475 cat_table_print( pp_current_cat_sections
, msg_Dbg
, NULL
, PRINT_TEXT
);
2476 if ( b_print_enabled
)
2478 cat_table_print( pp_current_cat_sections
, demux_Print
, NULL
,
2480 if ( i_print_type
== PRINT_XML
)
2481 fprintf(print_fh
, "\n");
2488 /*****************************************************************************
2490 *****************************************************************************/
2491 static void HandleCATSection( uint16_t i_pid
, uint8_t *p_section
,
2494 if ( i_pid
!= CAT_PID
|| !cat_validate( p_section
) )
2496 msg_Warn( NULL
, "invalid CAT section received on PID %hu", i_pid
);
2497 switch (i_print_type
) {
2499 fprintf(print_fh
, "<ERROR type=\"invalid_cat_section\"/>\n");
2502 fprintf(print_fh
, "error type: invalid_cat_section\n");
2511 if ( !psi_table_section( pp_next_cat_sections
, p_section
) )
2517 static void mark_pmt_pids( uint8_t *p_pmt
, uint8_t pid_map
[], uint8_t marker
)
2523 uint16_t i_pcr_pid
= pmt_get_pcrpid( p_pmt
);
2528 while ( (p_desc
= descs_get_desc( pmt_get_descs( p_pmt
), j
++ )) != NULL
)
2530 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2532 pid_map
[ desc09_get_pid( p_desc
) ] |= marker
;
2536 if ( i_pcr_pid
!= PADDING_PID
)
2537 pid_map
[ i_pcr_pid
] |= marker
;
2540 while ( (p_es
= pmt_get_es( p_pmt
, j
)) != NULL
)
2542 uint16_t i_pid
= pmtn_get_pid( p_es
);
2545 if ( PIDWouldBeSelected( p_es
) )
2546 pid_map
[ i_pid
] |= marker
;
2548 p_pids
[i_pid
].b_pes
= PIDCarriesPES( p_es
);
2553 while ( (p_desc
= descs_get_desc( pmtn_get_descs( p_es
), k
++ )) != NULL
)
2555 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2557 pid_map
[ desc09_get_pid( p_desc
) ] |= marker
;
2563 /*****************************************************************************
2565 *****************************************************************************/
2566 static void HandlePMT( uint16_t i_pid
, uint8_t *p_pmt
, mtime_t i_dts
)
2568 uint16_t i_sid
= pmt_get_program( p_pmt
);
2570 bool b_needs_descrambling
, b_needed_descrambling
, b_is_selected
;
2571 uint8_t pid_map
[MAX_PIDS
];
2573 p_sid
= FindSID( i_sid
);
2574 if ( p_sid
== NULL
)
2576 /* Unwanted SID (happens when the same PMT PID is used for several
2582 if ( i_pid
!= p_sid
->i_pmt_pid
)
2584 msg_Warn( NULL
, "invalid PMT section received on PID %hu", i_pid
);
2585 switch (i_print_type
) {
2587 fprintf(print_fh
, "<ERROR type=\"ghost_pmt\" program=\"%hu\n pid=\"%hu\"/>\n",
2591 fprintf(print_fh
, "error type: ghost_pmt program: %hu pid: %hu\n",
2601 if ( p_sid
->p_current_pmt
!= NULL
&&
2602 psi_compare( p_sid
->p_current_pmt
, p_pmt
) )
2604 /* Identical PMT. Shortcut. */
2609 if ( !pmt_validate( p_pmt
) )
2611 msg_Warn( NULL
, "invalid PMT section received on PID %hu", i_pid
);
2612 switch (i_print_type
) {
2614 fprintf(print_fh
, "<ERROR type=\"invalid_pmt_section\" pid=\"%hu\"/>\n",
2618 fprintf(print_fh
, "error type: invalid_pmt_section pid: %hu\n",
2628 memset( pid_map
, 0, sizeof(pid_map
) );
2630 b_needs_descrambling
= PMTNeedsDescrambling( p_pmt
);
2631 b_needed_descrambling
= p_sid
->p_current_pmt
!= NULL
?
2632 PMTNeedsDescrambling( p_sid
->p_current_pmt
) :
2634 b_is_selected
= SIDIsSelected( i_sid
);
2636 if ( i_ca_handle
&& b_is_selected
&&
2637 !b_needs_descrambling
&& b_needed_descrambling
)
2638 en50221_DeletePMT( p_sid
->p_current_pmt
);
2640 if ( p_sid
->p_current_pmt
!= NULL
)
2642 mark_pmt_pids( p_sid
->p_current_pmt
, pid_map
, 0x02 );
2643 free( p_sid
->p_current_pmt
);
2646 mark_pmt_pids( p_pmt
, pid_map
, 0x01 );
2648 /* Start to stream PIDs */
2650 for ( pid
= 0; pid
< MAX_PIDS
; pid
++ )
2652 /* The pid does not exist in the old PMT and in the new PMT. Ignore this pid. */
2653 if ( !pid_map
[ pid
] )
2656 switch ( pid_map
[ pid
] & 0x03 ) {
2657 case 0x03: /* The pid exists in the old PMT and in the new PMT. The pid was already selected in case 0x01. */
2659 case 0x02: /* The pid does not exist in the new PMT but exists in the old PMT. Unselect it. */
2660 UnselectPID( i_sid
, pid
);
2662 case 0x01: /* The pid exists in new PMT. Select it. */
2663 SelectPID( i_sid
, pid
);
2668 p_sid
->p_current_pmt
= p_pmt
;
2670 if ( i_ca_handle
&& b_is_selected
)
2672 if ( b_needs_descrambling
&& !b_needed_descrambling
)
2673 en50221_AddPMT( p_pmt
);
2674 else if ( b_needs_descrambling
&& b_needed_descrambling
)
2675 en50221_UpdatePMT( p_pmt
);
2680 pmt_print( p_pmt
, msg_Dbg
, NULL
, demux_Iconv
, NULL
, PRINT_TEXT
);
2681 if ( b_print_enabled
)
2683 pmt_print( p_pmt
, demux_Print
, NULL
, demux_Iconv
, NULL
,
2685 if ( i_print_type
== PRINT_XML
)
2686 fprintf(print_fh
, "\n");
2690 SendPMT( p_sid
, i_dts
);
2693 /*****************************************************************************
2695 *****************************************************************************/
2696 static void HandleNIT( mtime_t i_dts
)
2698 if ( psi_table_validate( pp_current_nit_sections
) &&
2699 psi_table_compare( pp_current_nit_sections
, pp_next_nit_sections
) )
2701 /* Identical NIT. Shortcut. */
2702 psi_table_free( pp_next_nit_sections
);
2703 psi_table_init( pp_next_nit_sections
);
2707 if ( !nit_table_validate( pp_next_nit_sections
) )
2709 msg_Warn( NULL
, "invalid NIT received" );
2710 switch (i_print_type
) {
2712 fprintf(print_fh
, "<ERROR type=\"invalid_nit\"/>\n");
2715 fprintf(print_fh
, "error type: invalid_nit\n");
2720 psi_table_free( pp_next_nit_sections
);
2721 psi_table_init( pp_next_nit_sections
);
2725 /* Switch tables. */
2726 psi_table_free( pp_current_nit_sections
);
2727 psi_table_copy( pp_current_nit_sections
, pp_next_nit_sections
);
2728 psi_table_init( pp_next_nit_sections
);
2730 nit_table_print( pp_current_nit_sections
, msg_Dbg
, NULL
,
2731 demux_Iconv
, NULL
, PRINT_TEXT
);
2732 if ( b_print_enabled
)
2734 nit_table_print( pp_current_nit_sections
, demux_Print
, NULL
,
2735 demux_Iconv
, NULL
, i_print_type
);
2736 if ( i_print_type
== PRINT_XML
)
2737 fprintf(print_fh
, "\n");
2744 /*****************************************************************************
2746 *****************************************************************************/
2747 static void HandleNITSection( uint16_t i_pid
, uint8_t *p_section
,
2750 if ( i_pid
!= NIT_PID
|| !nit_validate( p_section
) )
2752 msg_Warn( NULL
, "invalid NIT section received on PID %hu", i_pid
);
2753 switch (i_print_type
) {
2755 fprintf(print_fh
, "<ERROR type=\"invalid_nit_section\" pid=\"%hu\"/>\n",
2759 fprintf(print_fh
, "error type: invalid_nit_section pid: %hu\n",
2769 if ( psi_table_section( pp_next_nit_sections
, p_section
) )
2772 /* This case is different because DVB specifies a minimum bitrate for
2773 * PID 0x10, even if we don't have any thing to send (for cheap
2774 * transport over network boundaries). */
2779 /*****************************************************************************
2781 *****************************************************************************/
2782 static void HandleSDT( mtime_t i_dts
)
2784 PSI_TABLE_DECLARE( pp_old_sdt_sections
);
2785 uint8_t i_last_section
= psi_table_get_lastsection( pp_next_sdt_sections
);
2789 if ( psi_table_validate( pp_current_sdt_sections
) &&
2790 psi_table_compare( pp_current_sdt_sections
, pp_next_sdt_sections
) )
2792 /* Identical SDT. Shortcut. */
2793 psi_table_free( pp_next_sdt_sections
);
2794 psi_table_init( pp_next_sdt_sections
);
2798 if ( !sdt_table_validate( pp_next_sdt_sections
) )
2800 msg_Warn( NULL
, "invalid SDT received" );
2801 switch (i_print_type
) {
2803 fprintf(print_fh
, "<ERROR type=\"invalid_sdt\"/>\n");
2806 fprintf(print_fh
, "error type: invalid_sdt\n");
2811 psi_table_free( pp_next_sdt_sections
);
2812 psi_table_init( pp_next_sdt_sections
);
2816 /* Switch tables. */
2817 psi_table_copy( pp_old_sdt_sections
, pp_current_sdt_sections
);
2818 psi_table_copy( pp_current_sdt_sections
, pp_next_sdt_sections
);
2819 psi_table_init( pp_next_sdt_sections
);
2821 for ( i
= 0; i
<= i_last_section
; i
++ )
2823 uint8_t *p_section
=
2824 psi_table_get_section( pp_current_sdt_sections
, i
);
2828 while ( (p_service
= sdt_get_service( p_section
, j
)) != NULL
)
2830 uint16_t i_sid
= sdtn_get_sid( p_service
);
2837 if ( psi_table_validate( pp_old_sdt_sections
) )
2839 i_last_section
= psi_table_get_lastsection( pp_old_sdt_sections
);
2840 for ( i
= 0; i
<= i_last_section
; i
++ )
2842 uint8_t *p_section
=
2843 psi_table_get_section( pp_old_sdt_sections
, i
);
2844 const uint8_t *p_service
;
2847 while ( (p_service
= sdt_get_service( p_section
, j
)) != NULL
)
2849 uint16_t i_sid
= sdtn_get_sid( p_service
);
2852 if ( sdt_table_find_service( pp_current_sdt_sections
, i_sid
)
2858 psi_table_free( pp_old_sdt_sections
);
2861 sdt_table_print( pp_current_sdt_sections
, msg_Dbg
, NULL
,
2862 demux_Iconv
, NULL
, PRINT_TEXT
);
2863 if ( b_print_enabled
)
2865 sdt_table_print( pp_current_sdt_sections
, demux_Print
, NULL
,
2866 demux_Iconv
, NULL
, i_print_type
);
2867 if ( i_print_type
== PRINT_XML
)
2868 fprintf(print_fh
, "\n");
2875 /*****************************************************************************
2877 *****************************************************************************/
2878 static void HandleSDTSection( uint16_t i_pid
, uint8_t *p_section
,
2881 if ( i_pid
!= SDT_PID
|| !sdt_validate( p_section
) )
2883 msg_Warn( NULL
, "invalid SDT section received on PID %hu", i_pid
);
2884 switch (i_print_type
) {
2886 fprintf(print_fh
, "<ERROR type=\"invalid_sdt_section\" pid=\"%hu\"/>\n",
2890 fprintf(print_fh
, "error type: invalid_sdt_section pid: %hu\n",
2900 if ( !psi_table_section( pp_next_sdt_sections
, p_section
) )
2906 /*****************************************************************************
2908 *****************************************************************************/
2909 static void HandleEIT( uint16_t i_pid
, uint8_t *p_eit
, mtime_t i_dts
)
2911 uint16_t i_sid
= eit_get_sid( p_eit
);
2914 p_sid
= FindSID( i_sid
);
2915 if ( p_sid
== NULL
)
2917 /* Not a selected program. */
2922 if ( i_pid
!= EIT_PID
|| !eit_validate( p_eit
) )
2924 msg_Warn( NULL
, "invalid EIT section received on PID %hu", i_pid
);
2925 switch (i_print_type
) {
2927 fprintf(print_fh
, "<ERROR type=\"invalid_eit_section\" pid=\"%hu\"/>\n",
2931 fprintf(print_fh
, "error type: invalid_eit_section pid: %hu\n",
2941 SendEIT( p_sid
, i_dts
, p_eit
);
2945 /*****************************************************************************
2947 *****************************************************************************/
2948 static void HandleSection( uint16_t i_pid
, uint8_t *p_section
, mtime_t i_dts
)
2950 uint8_t i_table_id
= psi_get_tableid( p_section
);
2952 if ( !psi_validate( p_section
) )
2954 msg_Warn( NULL
, "invalid section on PID %hu", i_pid
);
2955 switch (i_print_type
) {
2957 fprintf(print_fh
, "<ERROR type=\"invalid_section\" pid=\"%hu\"/>\n", i_pid
);
2960 fprintf(print_fh
, "error type: invalid_section pid: %hu\n", i_pid
);
2969 if ( !psi_get_current( p_section
) )
2971 /* Ignore sections which are not in use yet. */
2976 switch ( i_table_id
)
2979 HandlePATSection( i_pid
, p_section
, i_dts
);
2984 HandleCATSection( i_pid
, p_section
, i_dts
);
2988 HandlePMT( i_pid
, p_section
, i_dts
);
2991 case NIT_TABLE_ID_ACTUAL
:
2992 HandleNITSection( i_pid
, p_section
, i_dts
);
2995 case SDT_TABLE_ID_ACTUAL
:
2996 HandleSDTSection( i_pid
, p_section
, i_dts
);
3000 if ( i_table_id
== EIT_TABLE_ID_PF_ACTUAL
||
3001 (i_table_id
>= EIT_TABLE_ID_SCHED_ACTUAL_FIRST
&&
3002 i_table_id
<= EIT_TABLE_ID_SCHED_ACTUAL_LAST
) )
3004 HandleEIT( i_pid
, p_section
, i_dts
);
3012 /*****************************************************************************
3014 *****************************************************************************/
3015 static void HandlePSIPacket( uint8_t *p_ts
, mtime_t i_dts
)
3017 uint16_t i_pid
= ts_get_pid( p_ts
);
3018 ts_pid_t
*p_pid
= &p_pids
[i_pid
];
3019 uint8_t i_cc
= ts_get_cc( p_ts
);
3020 const uint8_t *p_payload
;
3023 if ( ts_check_duplicate( i_cc
, p_pid
->i_last_cc
)
3024 || !ts_has_payload( p_ts
) )
3027 if ( p_pid
->i_last_cc
!= -1
3028 && ts_check_discontinuity( i_cc
, p_pid
->i_last_cc
) )
3029 psi_assemble_reset( &p_pid
->p_psi_buffer
, &p_pid
->i_psi_buffer_used
);
3031 p_payload
= ts_section( p_ts
);
3032 i_length
= p_ts
+ TS_SIZE
- p_payload
;
3034 if ( !psi_assemble_empty( &p_pid
->p_psi_buffer
,
3035 &p_pid
->i_psi_buffer_used
) )
3037 uint8_t *p_section
= psi_assemble_payload( &p_pid
->p_psi_buffer
,
3038 &p_pid
->i_psi_buffer_used
,
3039 &p_payload
, &i_length
);
3040 if ( p_section
!= NULL
)
3041 HandleSection( i_pid
, p_section
, i_dts
);
3044 p_payload
= ts_next_section( p_ts
);
3045 i_length
= p_ts
+ TS_SIZE
- p_payload
;
3049 uint8_t *p_section
= psi_assemble_payload( &p_pid
->p_psi_buffer
,
3050 &p_pid
->i_psi_buffer_used
,
3051 &p_payload
, &i_length
);
3052 if ( p_section
!= NULL
)
3053 HandleSection( i_pid
, p_section
, i_dts
);
3057 /*****************************************************************************
3058 * PID info functions
3059 *****************************************************************************/
3060 static const char *h222_stream_type_desc(uint8_t i_stream_type
) {
3061 /* See ISO/IEC 13818-1 : 2000 (E) | Table 2-29 - Stream type assignments, Page 66 (48) */
3062 if (i_stream_type
== 0)
3063 return "Reserved stream";
3064 switch (i_stream_type
) {
3065 case 0x01: return "11172-2 video (MPEG-1)";
3066 case 0x02: return "H.262/13818-2 video (MPEG-2) or 11172-2 constrained video";
3067 case 0x03: return "11172-3 audio (MPEG-1)";
3068 case 0x04: return "13818-3 audio (MPEG-2)";
3069 case 0x05: return "H.222.0/13818-1 private sections";
3070 case 0x06: return "H.222.0/13818-1 PES private data";
3071 case 0x07: return "13522 MHEG";
3072 case 0x08: return "H.222.0/13818-1 Annex A - DSM CC";
3073 case 0x09: return "H.222.1";
3074 case 0x0A: return "13818-6 type A";
3075 case 0x0B: return "13818-6 type B";
3076 case 0x0C: return "13818-6 type C";
3077 case 0x0D: return "13818-6 type D";
3078 case 0x0E: return "H.222.0/13818-1 auxiliary";
3079 case 0x0F: return "13818-7 Audio with ADTS transport syntax";
3080 case 0x10: return "14496-2 Visual (MPEG-4 part 2 video)";
3081 case 0x11: return "14496-3 Audio with LATM transport syntax (14496-3/AMD 1)";
3082 case 0x12: return "14496-1 SL-packetized or FlexMux stream in PES packets";
3083 case 0x13: return "14496-1 SL-packetized or FlexMux stream in 14496 sections";
3084 case 0x14: return "ISO/IEC 13818-6 Synchronized Download Protocol";
3085 case 0x15: return "Metadata in PES packets";
3086 case 0x16: return "Metadata in metadata_sections";
3087 case 0x17: return "Metadata in 13818-6 Data Carousel";
3088 case 0x18: return "Metadata in 13818-6 Object Carousel";
3089 case 0x19: return "Metadata in 13818-6 Synchronized Download Protocol";
3090 case 0x1A: return "13818-11 MPEG-2 IPMP stream";
3091 case 0x1B: return "H.264/14496-10 video (MPEG-4/AVC)";
3092 case 0x42: return "AVS Video";
3093 case 0x7F: return "IPMP stream";
3094 default : return "Unknown stream";
3098 static const char *get_pid_desc(uint16_t i_pid
, uint16_t *i_sid
) {
3100 uint8_t i_last_section
;
3102 uint16_t i_nit_pid
= NIT_PID
, i_pcr_pid
= 0;
3107 case 0x00: return "PAT";
3108 case 0x01: return "CAT";
3109 case 0x11: return "SDT";
3110 case 0x12: return "EPG";
3111 case 0x14: return "TDT/TOT";
3114 /* Detect NIT pid */
3115 if ( psi_table_validate( pp_current_pat_sections
) )
3117 i_last_section
= psi_table_get_lastsection( pp_current_pat_sections
);
3118 for ( i
= 0; i
<= i_last_section
; i
++ )
3120 uint8_t *p_section
= psi_table_get_section( pp_current_pat_sections
, i
);
3124 while ( (p_program
= pat_get_program( p_section
, j
++ )) != NULL
)
3126 /* Programs with PID == 0 are actually NIT */
3127 if ( patn_get_program( p_program
) == 0 )
3129 i_nit_pid
= patn_get_pid( p_program
);
3136 /* Detect EMM pids */
3137 if ( b_enable_emm
&& psi_table_validate( pp_current_cat_sections
) )
3139 i_last_section
= psi_table_get_lastsection( pp_current_cat_sections
);
3140 for ( i
= 0; i
<= i_last_section
; i
++ )
3142 uint8_t *p_section
= psi_table_get_section( pp_current_cat_sections
, i
);
3145 while ( (p_desc
= descl_get_desc( cat_get_descl(p_section
), cat_get_desclength(p_section
), j
++ )) != NULL
)
3147 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
3150 if ( desc09_get_pid( p_desc
) == i_pid
) {
3157 /* Detect streams in PMT */
3158 for ( k
= 0; k
< i_nb_sids
; k
++ )
3160 sid_t
*p_sid
= pp_sids
[k
];
3161 if ( p_sid
->i_pmt_pid
== i_pid
)
3164 *i_sid
= p_sid
->i_sid
;
3168 if ( p_sid
->i_sid
&& p_sid
->p_current_pmt
!= NULL
)
3170 uint8_t *p_current_pmt
= p_sid
->p_current_pmt
;
3171 uint8_t *p_current_es
;
3173 /* The PCR PID can be alone or PCR can be carried in some other PIDs (mostly video)
3174 so just remember the pid and if it is alone it will be reported as PCR, otherwise
3175 stream type of the PID will be reported */
3176 if ( i_pid
== pmt_get_pcrpid( p_current_pmt
) ) {
3178 *i_sid
= p_sid
->i_sid
;
3179 i_pcr_pid
= pmt_get_pcrpid( p_current_pmt
);
3184 while ((p_desc
= descs_get_desc( pmt_get_descs( p_current_pmt
), j
++ )) != NULL
)
3186 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
3189 if ( desc09_get_pid( p_desc
) == i_pid
) {
3191 *i_sid
= p_sid
->i_sid
;
3196 /* Detect stream types */
3198 while ( (p_current_es
= pmt_get_es( p_current_pmt
, j
++ )) != NULL
)
3200 if ( pmtn_get_pid( p_current_es
) == i_pid
)
3203 *i_sid
= p_sid
->i_sid
;
3204 return h222_stream_type_desc( pmtn_get_streamtype( p_current_es
) );
3210 /* Are there any other PIDs? */
3211 if (i_pid
== i_nit_pid
)
3214 if (i_pid
== i_pcr_pid
)
3220 /*****************************************************************************
3221 * Functions that return packed sections
3222 *****************************************************************************/
3223 uint8_t *demux_get_current_packed_PAT( unsigned int *pi_pack_size
) {
3224 return psi_pack_sections( pp_current_pat_sections
, pi_pack_size
);
3227 uint8_t *demux_get_current_packed_CAT( unsigned int *pi_pack_size
) {
3228 return psi_pack_sections( pp_current_cat_sections
, pi_pack_size
);
3231 uint8_t *demux_get_current_packed_NIT( unsigned int *pi_pack_size
) {
3232 return psi_pack_sections( pp_current_nit_sections
, pi_pack_size
);
3235 uint8_t *demux_get_current_packed_SDT( unsigned int *pi_pack_size
) {
3236 return psi_pack_sections( pp_current_sdt_sections
, pi_pack_size
);
3239 uint8_t *demux_get_packed_PMT( uint16_t i_sid
, unsigned int *pi_pack_size
) {
3240 sid_t
*p_sid
= FindSID( i_sid
);
3241 if ( p_sid
!= NULL
&& p_sid
->p_current_pmt
&& pmt_validate( p_sid
->p_current_pmt
) )
3242 return psi_pack_section( p_sid
->p_current_pmt
, pi_pack_size
);
3246 inline void demux_get_PID_info( uint16_t i_pid
, uint8_t *p_data
) {
3247 ts_pid_info_t
*p_info
= (ts_pid_info_t
*)p_data
;
3248 *p_info
= p_pids
[i_pid
].info
;
3251 inline void demux_get_PIDS_info( uint8_t *p_data
) {
3253 for (i_pid
= 0; i_pid
< MAX_PIDS
; i_pid
++ )
3254 demux_get_PID_info( i_pid
, p_data
+ ( i_pid
* sizeof(ts_pid_info_t
) ) );