1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004, 2008-2011, 2015-2017 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
;
89 PSI_TABLE_DECLARE(pp_eit_sections
);
92 mtime_t i_wallclock
= 0;
94 static ts_pid_t p_pids
[MAX_PIDS
];
95 static sid_t
**pp_sids
= NULL
;
96 static int i_nb_sids
= 0;
98 static PSI_TABLE_DECLARE(pp_current_pat_sections
);
99 static PSI_TABLE_DECLARE(pp_next_pat_sections
);
100 static PSI_TABLE_DECLARE(pp_current_cat_sections
);
101 static PSI_TABLE_DECLARE(pp_next_cat_sections
);
102 static PSI_TABLE_DECLARE(pp_current_nit_sections
);
103 static PSI_TABLE_DECLARE(pp_next_nit_sections
);
104 static PSI_TABLE_DECLARE(pp_current_sdt_sections
);
105 static PSI_TABLE_DECLARE(pp_next_sdt_sections
);
106 static mtime_t i_last_dts
= -1;
107 static int i_demux_fd
;
108 static uint64_t i_nb_packets
= 0;
109 static uint64_t i_nb_invalids
= 0;
110 static uint64_t i_nb_discontinuities
= 0;
111 static uint64_t i_nb_errors
= 0;
112 static int i_tuner_errors
= 0;
113 static mtime_t i_last_error
= 0;
114 static mtime_t i_last_reset
= 0;
115 static struct ev_timer print_watcher
;
118 static iconv_t iconv_handle
= (iconv_t
)-1;
121 /*****************************************************************************
123 *****************************************************************************/
124 static void demux_Handle( block_t
*p_ts
);
125 static void SetDTS( block_t
*p_list
);
126 static void SetPID( uint16_t i_pid
);
127 static void SetPID_EMM( uint16_t i_pid
);
128 static void UnsetPID( uint16_t i_pid
);
129 static void StartPID( output_t
*p_output
, uint16_t i_pid
);
130 static void StopPID( output_t
*p_output
, uint16_t i_pid
);
131 static void SelectPID( uint16_t i_sid
, uint16_t i_pid
, bool b_pcr
);
132 static void UnselectPID( uint16_t i_sid
, uint16_t i_pid
);
133 static void SelectPMT( uint16_t i_sid
, uint16_t i_pid
);
134 static void UnselectPMT( uint16_t i_sid
, uint16_t i_pid
);
135 static void GetPIDS( uint16_t **ppi_wanted_pids
, int *pi_nb_wanted_pids
,
136 uint16_t *pi_pcr_pid
, uint16_t i_sid
,
137 const uint16_t *pi_pids
, int i_nb_pids
);
138 static bool SIDIsSelected( uint16_t i_sid
);
139 static bool PIDWouldBeSelected( uint8_t *p_es
);
140 static bool PMTNeedsDescrambling( uint8_t *p_pmt
);
141 static void FlushEIT( output_t
*p_output
, mtime_t i_dts
);
142 static void SendTDT( block_t
*p_ts
);
143 static void SendEMM( block_t
*p_ts
);
144 static void NewPAT( output_t
*p_output
);
145 static void NewPMT( output_t
*p_output
);
146 static void NewNIT( output_t
*p_output
);
147 static void NewSDT( output_t
*p_output
);
148 static void HandlePSIPacket( uint8_t *p_ts
, mtime_t i_dts
);
149 static const char *get_pid_desc(uint16_t i_pid
, uint16_t *i_sid
);
152 * Remap an ES pid to a fixed value.
153 * Multiple streams of the same type use sequential pids
154 * Returns the new pid and updates the map tables
156 static uint16_t map_es_pid(output_t
* p_output
, uint8_t *p_es
, uint16_t i_pid
)
158 uint16_t i_newpid
= i_pid
;
159 uint16_t i_stream_type
= pmtn_get_streamtype(p_es
);
161 if ( !b_do_remap
&& !p_output
->config
.b_do_remap
)
164 msg_Dbg(NULL
, "REMAP: Found elementary stream type 0x%02x with original PID 0x%x (%u):", i_stream_type
, i_pid
, i_pid
);
166 switch ( i_stream_type
)
168 case 0x03: /* audio MPEG-1 */
169 case 0x04: /* audio MPEG-2 */
170 case 0x0f: /* audio AAC ADTS */
171 case 0x11: /* audio AAC LATM */
172 case 0x81: /* ATSC AC-3 */
173 case 0x87: /* ATSC Enhanced AC-3 */
175 i_newpid
= pi_newpids
[I_APID
];
177 i_newpid
= p_output
->config
.pi_confpids
[I_APID
];
179 case 0x01: /* video MPEG-1 */
180 case 0x02: /* video MPEG-2 */
181 case 0x10: /* video MPEG-4 */
182 case 0x1b: /* video H264 */
183 case 0x24: /* video H265 */
184 case 0x42: /* video AVS */
186 i_newpid
= pi_newpids
[I_VPID
];
188 i_newpid
= p_output
->config
.pi_confpids
[I_VPID
];
190 case 0x06: { /* PES Private Data - We must check the descriptors */
191 /* By default, nothing identified */
192 uint8_t SubStreamType
= N_MAP_PIDS
;
194 const uint8_t *p_desc
;
195 /* Loop over the descriptors */
196 while ( (p_desc
= descs_get_desc( pmtn_get_descs( p_es
), j
)) != NULL
)
198 /* Get the descriptor tag */
199 uint8_t i_tag
= desc_get_tag( p_desc
);
201 /* Check if the tag is: A/52, Enhanced A/52, DTS, AAC */
202 if (i_tag
== 0x6a || i_tag
== 0x7a || i_tag
== 0x7b || i_tag
== 0x7c)
203 SubStreamType
=I_APID
;
204 /* Check if the tag is: VBI + teletext, teletext, dvbsub */
205 if (i_tag
== 0x46 || i_tag
== 0x56 || i_tag
== 0x59)
206 SubStreamType
=I_SPUPID
;
209 if (SubStreamType
==I_APID
) {
210 msg_Dbg(NULL
, "REMAP: PES Private Data stream identified as [Audio]");
212 i_newpid
= pi_newpids
[I_APID
];
214 i_newpid
= p_output
->config
.pi_confpids
[I_APID
];
217 if (SubStreamType
==I_SPUPID
) {
218 msg_Dbg(NULL
, "REMAP: PES Private Data stream identified as [Subtitle]");
220 i_newpid
= pi_newpids
[I_SPUPID
];
222 i_newpid
= p_output
->config
.pi_confpids
[I_SPUPID
];
231 /* Got the new base for the mapped pid. Find the next free one
232 we do this to ensure that multiple audios get unique pids */
233 while (p_output
->pi_freepids
[i_newpid
] != UNUSED_PID
)
235 p_output
->pi_freepids
[i_newpid
] = i_pid
; /* Mark as in use */
236 p_output
->pi_newpids
[i_pid
] = i_newpid
; /* Save the new pid */
238 msg_Dbg(NULL
, "REMAP: => Elementary stream is remapped to PID 0x%x (%u)", i_newpid
, i_newpid
);
243 /*****************************************************************************
245 *****************************************************************************/
246 static inline sid_t
*FindSID( uint16_t i_sid
)
250 for ( i
= 0; i
< i_nb_sids
; i
++ )
252 sid_t
*p_sid
= pp_sids
[i
];
253 if ( p_sid
->i_sid
== i_sid
)
259 /*****************************************************************************
261 *****************************************************************************/
262 static void PrintCb( struct ev_loop
*loop
, struct ev_timer
*w
, int revents
)
264 uint64_t i_bitrate
= i_nb_packets
* TS_SIZE
* 8 * 1000000 / i_print_period
;
265 switch (i_print_type
)
269 "<STATUS type=\"bitrate\" status=\"%d\" value=\"%"PRIu64
"\" />\n",
270 i_bitrate
? 1 : 0, i_bitrate
);
273 fprintf(print_fh
, "bitrate: %"PRIu64
"\n", i_bitrate
);
282 switch (i_print_type
)
286 "<ERROR type=\"invalid_ts\" number=\"%"PRIu64
"\" />\n",
290 fprintf(print_fh
, "invalids: %"PRIu64
"\n", i_nb_invalids
);
298 if ( i_nb_discontinuities
)
300 switch (i_print_type
)
304 "<ERROR type=\"invalid_discontinuity\" number=\"%"PRIu64
"\" />\n",
305 i_nb_discontinuities
);
308 fprintf(print_fh
, "discontinuities: %"PRIu64
"\n",
309 i_nb_discontinuities
);
314 i_nb_discontinuities
= 0;
319 switch (i_print_type
)
323 "<ERROR type=\"transport_error\" number=\"%"PRIu64
"\" />\n",
327 fprintf(print_fh
, "errors: %"PRIu64
"\n", i_nb_errors
);
336 static void PrintESCb( struct ev_loop
*loop
, struct ev_timer
*w
, int revents
)
338 ts_pid_t
*p_pid
= container_of( w
, ts_pid_t
, timeout_watcher
);
339 uint16_t i_pid
= p_pid
- p_pids
;
341 switch (i_print_type
)
345 "<STATUS type=\"pid\" pid=\"%"PRIu16
"\" status=\"0\" />\n",
349 fprintf(print_fh
, "pid: %"PRIu16
" down\n", i_pid
);
355 ev_timer_stop( loop
, w
);
356 p_pid
->i_pes_status
= -1;
359 static void PrintES( uint16_t i_pid
)
361 const ts_pid_t
*p_pid
= &p_pids
[i_pid
];
363 switch (i_print_type
)
367 "<STATUS type=\"pid\" pid=\"%"PRIu16
"\" status=\"1\" pes=\"%d\" />\n",
368 i_pid
, p_pid
->i_pes_status
== 1 ? 1 : 0);
371 fprintf(print_fh
, "pid: %"PRIu16
" up%s\n",
372 i_pid
, p_pid
->i_pes_status
== 1 ? " pes" : "");
379 /*****************************************************************************
381 *****************************************************************************/
382 void demux_Open( void )
386 memset( p_pids
, 0, sizeof(p_pids
) );
390 for ( i
= 0; i
< MAX_PIDS
; i
++ )
392 p_pids
[i
].i_last_cc
= -1;
393 p_pids
[i
].i_demux_fd
= -1;
394 psi_assemble_init( &p_pids
[i
].p_psi_buffer
,
395 &p_pids
[i
].i_psi_buffer_used
);
396 p_pids
[i
].i_pes_status
= -1;
400 i_demux_fd
= pf_SetFilter(8192);
402 psi_table_init( pp_current_pat_sections
);
403 psi_table_init( pp_next_pat_sections
);
405 p_pids
[PAT_PID
].i_psi_refcount
++;
409 psi_table_init( pp_current_cat_sections
);
410 psi_table_init( pp_next_cat_sections
);
412 p_pids
[CAT_PID
].i_psi_refcount
++;
416 p_pids
[NIT_PID
].i_psi_refcount
++;
418 psi_table_init( pp_current_sdt_sections
);
419 psi_table_init( pp_next_sdt_sections
);
421 p_pids
[SDT_PID
].i_psi_refcount
++;
424 p_pids
[EIT_PID
].i_psi_refcount
++;
430 if ( i_print_period
)
432 ev_timer_init( &print_watcher
, PrintCb
,
433 i_print_period
/ 1000000., i_print_period
/ 1000000. );
434 ev_timer_start( event_loop
, &print_watcher
);
438 /*****************************************************************************
440 *****************************************************************************/
441 void demux_Close( void )
445 psi_table_free( pp_current_pat_sections
);
446 psi_table_free( pp_next_pat_sections
);
447 psi_table_free( pp_current_cat_sections
);
448 psi_table_free( pp_next_cat_sections
);
449 psi_table_free( pp_current_nit_sections
);
450 psi_table_free( pp_next_nit_sections
);
451 psi_table_free( pp_current_sdt_sections
);
452 psi_table_free( pp_next_sdt_sections
);
454 for ( i
= 0; i
< MAX_PIDS
; i
++ )
456 ev_timer_stop( event_loop
, &p_pids
[i
].timeout_watcher
);
457 free( p_pids
[i
].p_psi_buffer
);
458 free( p_pids
[i
].pp_outputs
);
461 for ( i
= 0; i
< i_nb_sids
; i
++ )
463 sid_t
*p_sid
= pp_sids
[i
];
464 psi_table_free(p_sid
->pp_eit_sections
);
465 free( p_sid
->p_current_pmt
);
471 if (iconv_handle
!= (iconv_t
)-1) {
472 iconv_close(iconv_handle
);
473 iconv_handle
= (iconv_t
)-1;
477 if ( i_print_period
)
478 ev_timer_stop( event_loop
, &print_watcher
);
481 /*****************************************************************************
483 *****************************************************************************/
484 void demux_Run( block_t
*p_ts
)
486 i_wallclock
= mdate();
490 while ( p_ts
!= NULL
)
492 block_t
*p_next
= p_ts
->p_next
;
494 demux_Handle( p_ts
);
499 /*****************************************************************************
501 *****************************************************************************/
502 static void demux_Handle( block_t
*p_ts
)
504 uint16_t i_pid
= ts_get_pid( p_ts
->p_ts
);
505 ts_pid_t
*p_pid
= &p_pids
[i_pid
];
506 uint8_t i_cc
= ts_get_cc( p_ts
->p_ts
);
511 if ( !ts_validate( p_ts
->p_ts
) )
513 msg_Warn( NULL
, "lost TS sync" );
514 block_Delete( p_ts
);
519 if ( i_pid
!= PADDING_PID
)
520 p_pid
->info
.i_scrambling
= ts_get_scrambling( p_ts
->p_ts
);
522 p_pid
->info
.i_last_packet_ts
= i_wallclock
;
523 p_pid
->info
.i_packets
++;
525 p_pid
->i_packets_passed
++;
527 /* Calculate bytes_per_sec */
528 if ( i_wallclock
> p_pid
->i_bytes_ts
+ 1000000 ) {
529 p_pid
->info
.i_bytes_per_sec
= p_pid
->i_packets_passed
* TS_SIZE
;
530 p_pid
->i_packets_passed
= 0;
531 p_pid
->i_bytes_ts
= i_wallclock
;
534 if ( p_pid
->info
.i_first_packet_ts
== 0 )
535 p_pid
->info
.i_first_packet_ts
= i_wallclock
;
537 if ( i_pid
!= PADDING_PID
&& p_pid
->i_last_cc
!= -1
538 && !ts_check_duplicate( i_cc
, p_pid
->i_last_cc
)
539 && ts_check_discontinuity( i_cc
, p_pid
->i_last_cc
) )
541 unsigned int expected_cc
= (p_pid
->i_last_cc
+ 1) & 0x0f;
543 const char *pid_desc
= get_pid_desc(i_pid
, &i_sid
);
545 p_pid
->info
.i_cc_errors
++;
546 i_nb_discontinuities
++;
548 msg_Warn( NULL
, "TS discontinuity on pid %4hu expected_cc %2u got %2u (%s, sid %d)",
549 i_pid
, expected_cc
, i_cc
, pid_desc
, i_sid
);
552 if ( ts_get_transporterror( p_ts
->p_ts
) )
555 const char *pid_desc
= get_pid_desc(i_pid
, &i_sid
);
557 p_pid
->info
.i_transport_errors
++;
559 msg_Warn( NULL
, "transport_error_indicator on pid %hu (%s, sid %u)",
560 i_pid
, pid_desc
, i_sid
);
564 i_last_error
= i_wallclock
;
566 else if ( i_wallclock
> i_last_error
+ WATCHDOG_WAIT
)
569 if ( i_tuner_errors
> MAX_ERRORS
)
573 "too many transport errors, tuning again" );
574 switch (i_print_type
) {
576 fprintf(print_fh
, "<EVENT type=\"reset\" cause=\"transport\" />\n");
579 fprintf(print_fh
, "reset cause: transport\n");
589 int i_pes_status
= -1;
590 if ( ts_get_scrambling( p_ts
->p_ts
) )
592 else if ( ts_get_unitstart( p_ts
->p_ts
) )
594 uint8_t *p_payload
= ts_payload( p_ts
->p_ts
);
595 if ( p_payload
+ 3 < p_ts
->p_ts
+ TS_SIZE
)
596 i_pes_status
= pes_validate( p_payload
) ? 1 : 0;
599 if ( i_pes_status
!= -1 )
601 if ( p_pid
->i_pes_status
== -1 )
603 p_pid
->i_pes_status
= i_pes_status
;
606 if ( i_pid
!= TDT_PID
)
608 ev_timer_init( &p_pid
->timeout_watcher
, PrintESCb
,
609 i_es_timeout
/ 1000000.,
610 i_es_timeout
/ 1000000. );
611 ev_timer_start( event_loop
, &p_pid
->timeout_watcher
);
615 ev_timer_init( &p_pid
->timeout_watcher
, PrintESCb
, 30, 30 );
616 ev_timer_start( event_loop
, &p_pid
->timeout_watcher
);
621 if ( p_pid
->i_pes_status
!= i_pes_status
)
623 p_pid
->i_pes_status
= i_pes_status
;
627 ev_timer_again( event_loop
, &p_pid
->timeout_watcher
);
632 if ( !ts_get_transporterror( p_ts
->p_ts
) )
635 if ( i_pid
== TDT_PID
|| i_pid
== RST_PID
)
637 else if ( p_pid
->i_psi_refcount
)
638 HandlePSIPacket( p_ts
->p_ts
, p_ts
->i_dts
);
640 if ( b_enable_emm
&& p_pid
->b_emm
)
644 p_pid
->i_last_cc
= i_cc
;
647 for ( i
= 0; i
< p_pid
->i_nb_outputs
; i
++ )
649 output_t
*p_output
= p_pid
->pp_outputs
[i
];
650 if ( p_output
!= NULL
)
652 if ( i_ca_handle
&& (p_output
->config
.i_config
& OUTPUT_WATCH
) &&
653 ts_get_unitstart( p_ts
->p_ts
) )
657 if ( ts_get_scrambling( p_ts
->p_ts
) ||
659 && (p_payload
= ts_payload( p_ts
->p_ts
)) + 3
660 < p_ts
->p_ts
+ TS_SIZE
661 && !pes_validate(p_payload
) ) )
664 i_last_reset
+ WATCHDOG_REFRACTORY_PERIOD
)
666 p_output
->i_nb_errors
++;
667 p_output
->i_last_error
= i_wallclock
;
670 else if ( i_wallclock
> p_output
->i_last_error
+ WATCHDOG_WAIT
)
671 p_output
->i_nb_errors
= 0;
673 if ( p_output
->i_nb_errors
> MAX_ERRORS
)
676 for ( j
= 0; j
< i_nb_outputs
; j
++ )
677 pp_outputs
[j
]->i_nb_errors
= 0;
680 "too many errors for stream %s, resetting",
681 p_output
->config
.psz_displayname
);
683 switch (i_print_type
) {
685 fprintf(print_fh
, "<EVENT type=\"reset\" cause=\"scrambling\" />\n");
688 fprintf(print_fh
, "reset cause: scrambling");
693 i_last_reset
= i_wallclock
;
698 if ( p_output
->i_pcr_pid
!= i_pid
699 || (ts_has_adaptation(p_ts
->p_ts
)
700 && ts_get_adaptation(p_ts
->p_ts
)
701 && tsaf_has_pcr(p_ts
->p_ts
)) )
702 output_Put( p_output
, p_ts
);
704 if ( p_output
->p_eit_ts_buffer
!= NULL
705 && p_ts
->i_dts
> p_output
->p_eit_ts_buffer
->i_dts
706 + MAX_EIT_RETENTION
)
707 FlushEIT( p_output
, p_ts
->i_dts
);
711 for ( i
= 0; i
< i_nb_outputs
; i
++ )
713 output_t
*p_output
= pp_outputs
[i
];
715 if ( !(p_output
->config
.i_config
& OUTPUT_VALID
) ||
716 !p_output
->config
.b_passthrough
)
719 output_Put( p_output
, p_ts
);
722 if ( output_dup
.config
.i_config
& OUTPUT_VALID
)
723 output_Put( &output_dup
, p_ts
);
726 if ( !p_ts
->i_refcount
)
727 block_Delete( p_ts
);
730 /*****************************************************************************
731 * demux_Change : called from main thread
732 *****************************************************************************/
733 static bool IsIn( const uint16_t *pi_pids
, int i_nb_pids
, uint16_t i_pid
)
736 for ( i
= 0; i
< i_nb_pids
; i
++ )
737 if ( i_pid
== pi_pids
[i
] ) break;
738 return ( i
!= i_nb_pids
);
741 void demux_Change( output_t
*p_output
, const output_config_t
*p_config
)
743 uint16_t *pi_wanted_pids
, *pi_current_pids
;
744 int i_nb_wanted_pids
, i_nb_current_pids
;
745 uint16_t i_wanted_pcr_pid
, i_current_pcr_pid
;
747 uint16_t i_old_sid
= p_output
->config
.i_sid
;
748 uint16_t i_sid
= p_config
->i_sid
;
749 uint16_t *pi_old_pids
= p_output
->config
.pi_pids
;
750 uint16_t *pi_pids
= p_config
->pi_pids
;
751 int i_old_nb_pids
= p_output
->config
.i_nb_pids
;
752 int i_nb_pids
= p_config
->i_nb_pids
;
754 bool b_sid_change
= i_sid
!= i_old_sid
;
755 bool b_pid_change
= false, b_tsid_change
= false;
756 bool b_dvb_change
= !!((p_output
->config
.i_config
^ p_config
->i_config
)
758 bool b_epg_change
= !!((p_output
->config
.i_config
^ p_config
->i_config
)
760 bool b_network_change
=
761 (dvb_string_cmp(&p_output
->config
.network_name
, &p_config
->network_name
) ||
762 p_output
->config
.i_network_id
!= p_config
->i_network_id
);
763 bool b_service_name_change
=
764 (dvb_string_cmp(&p_output
->config
.service_name
, &p_config
->service_name
) ||
765 dvb_string_cmp(&p_output
->config
.provider_name
, &p_config
->provider_name
));
766 bool b_remap_change
= p_output
->config
.i_new_sid
!= p_config
->i_new_sid
||
767 p_output
->config
.i_onid
!= p_config
->i_onid
||
768 p_output
->config
.b_do_remap
!= p_config
->b_do_remap
||
769 p_output
->config
.pi_confpids
[I_PMTPID
] != p_config
->pi_confpids
[I_PMTPID
] ||
770 p_output
->config
.pi_confpids
[I_APID
] != p_config
->pi_confpids
[I_APID
] ||
771 p_output
->config
.pi_confpids
[I_VPID
] != p_config
->pi_confpids
[I_VPID
] ||
772 p_output
->config
.pi_confpids
[I_SPUPID
] != p_config
->pi_confpids
[I_SPUPID
];
775 p_output
->config
.i_config
= p_config
->i_config
;
776 p_output
->config
.i_network_id
= p_config
->i_network_id
;
777 p_output
->config
.i_new_sid
= p_config
->i_new_sid
;
778 p_output
->config
.i_onid
= p_config
->i_onid
;
779 p_output
->config
.b_do_remap
= p_config
->b_do_remap
;
780 memcpy(p_output
->config
.pi_confpids
, p_config
->pi_confpids
,
781 sizeof(uint16_t) * N_MAP_PIDS
);
783 /* Change output settings related to names. */
784 dvb_string_clean( &p_output
->config
.network_name
);
785 dvb_string_clean( &p_output
->config
.service_name
);
786 dvb_string_clean( &p_output
->config
.provider_name
);
787 dvb_string_copy( &p_output
->config
.network_name
,
788 &p_config
->network_name
);
789 dvb_string_copy( &p_output
->config
.service_name
,
790 &p_config
->service_name
);
791 dvb_string_copy( &p_output
->config
.provider_name
,
792 &p_config
->provider_name
);
794 if ( p_config
->i_tsid
!= -1 && p_output
->config
.i_tsid
!= p_config
->i_tsid
)
796 p_output
->i_tsid
= p_output
->config
.i_tsid
= p_config
->i_tsid
;
797 b_tsid_change
= true;
799 if ( p_config
->i_tsid
== -1 && p_output
->config
.i_tsid
!= -1 )
801 p_output
->config
.i_tsid
= p_config
->i_tsid
;
802 if ( psi_table_validate(pp_current_pat_sections
) && !b_random_tsid
)
804 psi_table_get_tableidext(pp_current_pat_sections
);
806 p_output
->i_tsid
= rand() & 0xffff;
807 b_tsid_change
= true;
810 if ( p_config
->b_passthrough
== p_output
->config
.b_passthrough
&&
811 !b_sid_change
&& p_config
->i_nb_pids
== p_output
->config
.i_nb_pids
&&
812 (!p_config
->i_nb_pids
||
813 !memcmp( p_output
->config
.pi_pids
, p_config
->pi_pids
,
814 p_config
->i_nb_pids
* sizeof(uint16_t) )) )
817 GetPIDS( &pi_wanted_pids
, &i_nb_wanted_pids
, &i_wanted_pcr_pid
,
818 i_sid
, pi_pids
, i_nb_pids
);
819 GetPIDS( &pi_current_pids
, &i_nb_current_pids
, &i_current_pcr_pid
,
820 i_old_sid
, pi_old_pids
, i_old_nb_pids
);
822 if ( b_sid_change
&& i_old_sid
)
824 sid_t
*p_old_sid
= FindSID( i_old_sid
);
825 p_output
->config
.i_sid
= p_config
->i_sid
;
827 if ( p_old_sid
!= NULL
)
829 if ( i_sid
!= i_old_sid
)
830 UnselectPMT( i_old_sid
, p_old_sid
->i_pmt_pid
);
832 if ( i_ca_handle
&& !SIDIsSelected( i_old_sid
)
833 && p_old_sid
->p_current_pmt
!= NULL
834 && PMTNeedsDescrambling( p_old_sid
->p_current_pmt
) )
835 en50221_DeletePMT( p_old_sid
->p_current_pmt
);
839 for ( i
= 0; i
< i_nb_current_pids
; i
++ )
841 if ( !IsIn( pi_wanted_pids
, i_nb_wanted_pids
, pi_current_pids
[i
] ) )
843 StopPID( p_output
, pi_current_pids
[i
] );
848 if ( b_sid_change
&& i_ca_handle
&& i_old_sid
&&
849 SIDIsSelected( i_old_sid
) )
851 sid_t
*p_old_sid
= FindSID( i_old_sid
);
852 if ( p_old_sid
!= NULL
&& p_old_sid
->p_current_pmt
!= NULL
853 && PMTNeedsDescrambling( p_old_sid
->p_current_pmt
) )
854 en50221_UpdatePMT( p_old_sid
->p_current_pmt
);
857 for ( i
= 0; i
< i_nb_wanted_pids
; i
++ )
859 if ( !IsIn( pi_current_pids
, i_nb_current_pids
, pi_wanted_pids
[i
] ) )
861 StartPID( p_output
, pi_wanted_pids
[i
] );
866 free( pi_wanted_pids
);
867 free( pi_current_pids
);
868 p_output
->i_pcr_pid
= i_wanted_pcr_pid
;
870 if ( b_sid_change
&& i_sid
)
872 sid_t
*p_sid
= FindSID( i_sid
);
873 p_output
->config
.i_sid
= i_old_sid
;
877 if ( i_sid
!= i_old_sid
)
878 SelectPMT( i_sid
, p_sid
->i_pmt_pid
);
880 if ( i_ca_handle
&& !SIDIsSelected( i_sid
)
881 && p_sid
->p_current_pmt
!= NULL
882 && PMTNeedsDescrambling( p_sid
->p_current_pmt
) )
883 en50221_AddPMT( p_sid
->p_current_pmt
);
887 if ( i_ca_handle
&& i_sid
&& SIDIsSelected( i_sid
) )
889 sid_t
*p_sid
= FindSID( i_sid
);
890 if ( p_sid
!= NULL
&& p_sid
->p_current_pmt
!= NULL
891 && PMTNeedsDescrambling( p_sid
->p_current_pmt
) )
892 en50221_UpdatePMT( p_sid
->p_current_pmt
);
895 p_output
->config
.b_passthrough
= p_config
->b_passthrough
;
896 p_output
->config
.i_sid
= i_sid
;
897 free( p_output
->config
.pi_pids
);
898 p_output
->config
.pi_pids
= malloc( sizeof(uint16_t) * i_nb_pids
);
899 memcpy( p_output
->config
.pi_pids
, pi_pids
, sizeof(uint16_t) * i_nb_pids
);
900 p_output
->config
.i_nb_pids
= i_nb_pids
;
903 if ( b_sid_change
|| b_pid_change
|| b_tsid_change
|| b_dvb_change
||
904 b_network_change
|| b_service_name_change
|| b_remap_change
)
906 msg_Dbg( NULL
, "change %s%s%s%s%s%s%s",
907 b_sid_change
? "sid " : "",
908 b_pid_change
? "pid " : "",
909 b_tsid_change
? "tsid " : "",
910 b_dvb_change
? "dvb " : "",
911 b_network_change
? "network " : "",
912 b_service_name_change
? "service_name " : "",
913 b_remap_change
? "remap " : "" );
916 if ( b_sid_change
|| b_remap_change
)
931 else if ( b_dvb_change
)
936 else if ( b_network_change
)
939 if ( !b_tsid_change
&& (b_service_name_change
|| b_epg_change
) )
947 /*****************************************************************************
949 *****************************************************************************/
950 static void SetDTS( block_t
*p_list
)
954 block_t
*p_ts
= p_list
;
956 while ( p_ts
!= NULL
)
962 /* We suppose the stream is CBR, at least between two consecutive read().
963 * This is especially true in budget mode */
964 if ( i_last_dts
== -1 )
967 i_duration
= i_wallclock
- i_last_dts
;
971 while ( p_ts
!= NULL
)
973 p_ts
->i_dts
= i_wallclock
- i_duration
* i
/ i_nb_ts
;
978 i_last_dts
= i_wallclock
;
981 /*****************************************************************************
983 *****************************************************************************/
984 static void SetPID( uint16_t i_pid
)
986 p_pids
[i_pid
].i_refcount
++;
988 if ( !b_budget_mode
&& p_pids
[i_pid
].i_refcount
989 && p_pids
[i_pid
].i_demux_fd
== -1 )
990 p_pids
[i_pid
].i_demux_fd
= pf_SetFilter( i_pid
);
993 static void SetPID_EMM( uint16_t i_pid
)
996 p_pids
[i_pid
].b_emm
= true;
999 static void UnsetPID( uint16_t i_pid
)
1001 p_pids
[i_pid
].i_refcount
--;
1003 if ( !b_budget_mode
&& !p_pids
[i_pid
].i_refcount
1004 && p_pids
[i_pid
].i_demux_fd
!= -1 )
1006 pf_UnsetFilter( p_pids
[i_pid
].i_demux_fd
, i_pid
);
1007 p_pids
[i_pid
].i_demux_fd
= -1;
1008 p_pids
[i_pid
].b_emm
= false;
1012 /*****************************************************************************
1014 *****************************************************************************/
1015 static void StartPID( output_t
*p_output
, uint16_t i_pid
)
1019 for ( j
= 0; j
< p_pids
[i_pid
].i_nb_outputs
; j
++ )
1020 if ( p_pids
[i_pid
].pp_outputs
[j
] == p_output
)
1023 if ( j
== p_pids
[i_pid
].i_nb_outputs
)
1025 for ( j
= 0; j
< p_pids
[i_pid
].i_nb_outputs
; j
++ )
1026 if ( p_pids
[i_pid
].pp_outputs
[j
] == NULL
)
1029 if ( j
== p_pids
[i_pid
].i_nb_outputs
)
1031 p_pids
[i_pid
].i_nb_outputs
++;
1032 p_pids
[i_pid
].pp_outputs
= realloc( p_pids
[i_pid
].pp_outputs
,
1034 * p_pids
[i_pid
].i_nb_outputs
);
1037 p_pids
[i_pid
].pp_outputs
[j
] = p_output
;
1042 static void StopPID( output_t
*p_output
, uint16_t i_pid
)
1046 for ( j
= 0; j
< p_pids
[i_pid
].i_nb_outputs
; j
++ )
1048 if ( p_pids
[i_pid
].pp_outputs
[j
] != NULL
)
1050 if ( p_pids
[i_pid
].pp_outputs
[j
] == p_output
)
1055 if ( j
!= p_pids
[i_pid
].i_nb_outputs
)
1057 p_pids
[i_pid
].pp_outputs
[j
] = NULL
;
1062 /*****************************************************************************
1063 * SelectPID/UnselectPID
1064 *****************************************************************************/
1065 static void SelectPID( uint16_t i_sid
, uint16_t i_pid
, bool b_pcr
)
1069 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1071 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
1072 && pp_outputs
[i
]->config
.i_sid
== i_sid
)
1074 if ( pp_outputs
[i
]->config
.i_nb_pids
&&
1075 !IsIn( pp_outputs
[i
]->config
.pi_pids
,
1076 pp_outputs
[i
]->config
.i_nb_pids
, i_pid
) )
1079 pp_outputs
[i
]->i_pcr_pid
= i_pid
;
1083 StartPID( pp_outputs
[i
], i_pid
);
1088 static void UnselectPID( uint16_t i_sid
, uint16_t i_pid
)
1092 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1093 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
1094 && pp_outputs
[i
]->config
.i_sid
== i_sid
1095 && !pp_outputs
[i
]->config
.i_nb_pids
)
1096 StopPID( pp_outputs
[i
], i_pid
);
1099 /*****************************************************************************
1100 * SelectPMT/UnselectPMT
1101 *****************************************************************************/
1102 static void SelectPMT( uint16_t i_sid
, uint16_t i_pid
)
1106 p_pids
[i_pid
].i_psi_refcount
++;
1107 p_pids
[i_pid
].b_pes
= false;
1109 if ( b_select_pmts
)
1111 else for ( i
= 0; i
< i_nb_outputs
; i
++ )
1112 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
1113 && pp_outputs
[i
]->config
.i_sid
== i_sid
)
1117 static void UnselectPMT( uint16_t i_sid
, uint16_t i_pid
)
1121 p_pids
[i_pid
].i_psi_refcount
--;
1122 if ( !p_pids
[i_pid
].i_psi_refcount
)
1123 psi_assemble_reset( &p_pids
[i_pid
].p_psi_buffer
,
1124 &p_pids
[i_pid
].i_psi_buffer_used
);
1126 if ( b_select_pmts
)
1128 else for ( i
= 0; i
< i_nb_outputs
; i
++ )
1129 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
1130 && pp_outputs
[i
]->config
.i_sid
== i_sid
)
1134 /*****************************************************************************
1136 *****************************************************************************/
1137 static void GetPIDS( uint16_t **ppi_wanted_pids
, int *pi_nb_wanted_pids
,
1138 uint16_t *pi_wanted_pcr_pid
, uint16_t i_sid
,
1139 const uint16_t *pi_pids
, int i_nb_pids
)
1143 uint16_t i_pmt_pid
, i_pcr_pid
;
1146 const uint8_t *p_desc
;
1148 *pi_wanted_pcr_pid
= 0;
1150 if ( i_nb_pids
|| i_sid
== 0 )
1152 *pi_nb_wanted_pids
= i_nb_pids
;
1153 *ppi_wanted_pids
= malloc( sizeof(uint16_t) * i_nb_pids
);
1154 memcpy( *ppi_wanted_pids
, pi_pids
, sizeof(uint16_t) * i_nb_pids
);
1160 *pi_nb_wanted_pids
= 0;
1161 *ppi_wanted_pids
= NULL
;
1164 p_sid
= FindSID( i_sid
);
1165 if ( p_sid
== NULL
)
1168 p_pmt
= p_sid
->p_current_pmt
;
1169 i_pmt_pid
= p_sid
->i_pmt_pid
;
1170 if ( p_pmt
== NULL
) {
1171 msg_Dbg(NULL
, "no current PMT on sid %d\n", i_sid
);
1175 i_pcr_pid
= pmt_get_pcrpid( p_pmt
);
1177 while ( (p_es
= pmt_get_es( p_pmt
, j
)) != NULL
)
1181 uint16_t i_pid
= pmtn_get_pid( p_es
);
1184 b_select
= IsIn( pi_pids
, i_nb_pids
, i_pid
);
1187 b_select
= PIDWouldBeSelected( p_es
);
1190 *ppi_wanted_pids
= realloc( *ppi_wanted_pids
,
1191 (*pi_nb_wanted_pids
+ 1) * sizeof(uint16_t) );
1192 (*ppi_wanted_pids
)[(*pi_nb_wanted_pids
)++] = i_pid
;
1196 if ( b_select
&& b_enable_ecm
)
1200 while ((p_desc
= descs_get_desc( pmtn_get_descs( p_es
), k
++ )) != NULL
)
1202 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
1204 *ppi_wanted_pids
= realloc( *ppi_wanted_pids
,
1205 (*pi_nb_wanted_pids
+ 1) * sizeof(uint16_t) );
1206 (*ppi_wanted_pids
)[(*pi_nb_wanted_pids
)++] = desc09_get_pid( p_desc
);
1216 while ((p_desc
= descs_get_desc( pmt_get_descs( p_pmt
), j
++ )) != NULL
)
1218 if ( desc_get_tag( p_desc
) != 0x09 ||
1219 !desc09_validate( p_desc
) )
1221 *ppi_wanted_pids
= realloc( *ppi_wanted_pids
,
1222 (*pi_nb_wanted_pids
+ 1) * sizeof(uint16_t) );
1223 (*ppi_wanted_pids
)[(*pi_nb_wanted_pids
)++] = desc09_get_pid( p_desc
);
1227 if ( i_pcr_pid
!= PADDING_PID
&& i_pcr_pid
!= i_pmt_pid
1228 && !IsIn( *ppi_wanted_pids
, *pi_nb_wanted_pids
, i_pcr_pid
) )
1230 *ppi_wanted_pids
= realloc( *ppi_wanted_pids
,
1231 (*pi_nb_wanted_pids
+ 1) * sizeof(uint16_t) );
1232 (*ppi_wanted_pids
)[(*pi_nb_wanted_pids
)++] = i_pcr_pid
;
1233 /* We only need the PCR packets of this stream (incomplete) */
1234 *pi_wanted_pcr_pid
= i_pcr_pid
;
1235 msg_Dbg( NULL
, "Requesting partial PCR PID %"PRIu16
, i_pcr_pid
);
1239 /*****************************************************************************
1241 *****************************************************************************/
1242 static void OutputPSISection( output_t
*p_output
, uint8_t *p_section
,
1243 uint16_t i_pid
, uint8_t *pi_cc
, mtime_t i_dts
,
1244 block_t
**pp_ts_buffer
,
1245 uint8_t *pi_ts_buffer_offset
)
1247 uint16_t i_section_length
= psi_get_length(p_section
) + PSI_HEADER_SIZE
;
1248 uint16_t i_section_offset
= 0;
1254 uint8_t i_ts_offset
;
1255 bool b_append
= (pp_ts_buffer
!= NULL
&& *pp_ts_buffer
!= NULL
);
1259 p_block
= *pp_ts_buffer
;
1260 i_ts_offset
= *pi_ts_buffer_offset
;
1264 p_block
= block_New();
1265 p_block
->i_dts
= i_dts
;
1270 psi_split_section( p
, &i_ts_offset
, p_section
, &i_section_offset
);
1274 ts_set_pid( p
, i_pid
);
1275 ts_set_cc( p
, *pi_cc
);
1280 if ( i_section_offset
== i_section_length
)
1282 if ( i_ts_offset
< TS_SIZE
- MIN_SECTION_FRAGMENT
1283 && pp_ts_buffer
!= NULL
)
1285 *pp_ts_buffer
= p_block
;
1286 *pi_ts_buffer_offset
= i_ts_offset
;
1290 psi_split_end( p
, &i_ts_offset
);
1293 p_block
->i_dts
= i_dts
;
1294 p_block
->i_refcount
--;
1295 output_Put( p_output
, p_block
);
1296 if ( pp_ts_buffer
!= NULL
)
1298 *pp_ts_buffer
= NULL
;
1299 *pi_ts_buffer_offset
= 0;
1302 while ( i_section_offset
< i_section_length
);
1305 /*****************************************************************************
1307 *****************************************************************************/
1308 static void SendPAT( mtime_t i_dts
)
1312 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1314 output_t
*p_output
= pp_outputs
[i
];
1316 if ( !(p_output
->config
.i_config
& OUTPUT_VALID
) ||
1317 p_output
->config
.b_passthrough
)
1320 if ( p_output
->p_pat_section
== NULL
&&
1321 psi_table_validate(pp_current_pat_sections
) )
1323 /* SID doesn't exist - build an empty PAT. */
1325 p_output
->i_pat_version
++;
1327 p
= p_output
->p_pat_section
= psi_allocate();
1329 pat_set_length( p
, 0 );
1330 pat_set_tsid( p
, p_output
->i_tsid
);
1331 psi_set_version( p
, p_output
->i_pat_version
);
1332 psi_set_current( p
);
1333 psi_set_section( p
, 0 );
1334 psi_set_lastsection( p
, 0 );
1335 psi_set_crc( p_output
->p_pat_section
);
1339 if ( p_output
->p_pat_section
!= NULL
)
1340 OutputPSISection( p_output
, p_output
->p_pat_section
, PAT_PID
,
1341 &p_output
->i_pat_cc
, i_dts
, NULL
, NULL
);
1345 /*****************************************************************************
1347 *****************************************************************************/
1348 static void SendPMT( sid_t
*p_sid
, mtime_t i_dts
)
1351 int i_pmt_pid
= p_sid
->i_pmt_pid
;
1354 i_pmt_pid
= pi_newpids
[ I_PMTPID
];
1356 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1358 output_t
*p_output
= pp_outputs
[i
];
1360 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1361 && p_output
->config
.i_sid
== p_sid
->i_sid
1362 && p_output
->p_pmt_section
!= NULL
)
1364 if ( p_output
->config
.b_do_remap
&& p_output
->config
.pi_confpids
[I_PMTPID
] )
1365 i_pmt_pid
= p_output
->config
.pi_confpids
[I_PMTPID
];
1367 OutputPSISection( p_output
, p_output
->p_pmt_section
,
1368 i_pmt_pid
, &p_output
->i_pmt_cc
, i_dts
,
1374 /*****************************************************************************
1376 *****************************************************************************/
1377 static void SendNIT( mtime_t i_dts
)
1381 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1383 output_t
*p_output
= pp_outputs
[i
];
1385 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1386 && !p_output
->config
.b_passthrough
1387 && (p_output
->config
.i_config
& OUTPUT_DVB
)
1388 && p_output
->p_nit_section
!= NULL
)
1389 OutputPSISection( p_output
, p_output
->p_nit_section
, NIT_PID
,
1390 &p_output
->i_nit_cc
, i_dts
, NULL
, NULL
);
1394 /*****************************************************************************
1396 *****************************************************************************/
1397 static void SendSDT( mtime_t i_dts
)
1401 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1403 output_t
*p_output
= pp_outputs
[i
];
1405 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1406 && !p_output
->config
.b_passthrough
1407 && (p_output
->config
.i_config
& OUTPUT_DVB
)
1408 && p_output
->p_sdt_section
!= NULL
)
1409 OutputPSISection( p_output
, p_output
->p_sdt_section
, SDT_PID
,
1410 &p_output
->i_sdt_cc
, i_dts
, NULL
, NULL
);
1414 /*****************************************************************************
1416 *****************************************************************************/
1417 static void SendEIT( sid_t
*p_sid
, mtime_t i_dts
, uint8_t *p_eit
)
1419 uint8_t i_table_id
= psi_get_tableid( p_eit
);
1420 bool b_epg
= i_table_id
>= EIT_TABLE_ID_SCHED_ACTUAL_FIRST
&&
1421 i_table_id
<= EIT_TABLE_ID_SCHED_ACTUAL_LAST
;
1422 uint16_t i_onid
= eit_get_onid(p_eit
);
1425 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1427 output_t
*p_output
= pp_outputs
[i
];
1429 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1430 && !p_output
->config
.b_passthrough
1431 && (p_output
->config
.i_config
& OUTPUT_DVB
)
1432 && (!b_epg
|| (p_output
->config
.i_config
& OUTPUT_EPG
))
1433 && p_output
->config
.i_sid
== p_sid
->i_sid
)
1435 eit_set_tsid( p_eit
, p_output
->i_tsid
);
1437 if ( p_output
->config
.i_new_sid
)
1438 eit_set_sid( p_eit
, p_output
->config
.i_new_sid
);
1440 eit_set_sid( p_eit
, p_output
->config
.i_sid
);
1442 if ( p_output
->config
.i_onid
)
1443 eit_set_onid( p_eit
, p_output
->config
.i_onid
);
1445 psi_set_crc( p_eit
);
1447 OutputPSISection( p_output
, p_eit
, EIT_PID
, &p_output
->i_eit_cc
,
1448 i_dts
, &p_output
->p_eit_ts_buffer
,
1449 &p_output
->i_eit_ts_buffer_offset
);
1451 if ( p_output
->config
.i_onid
)
1452 eit_set_onid( p_eit
, i_onid
);
1457 /*****************************************************************************
1459 *****************************************************************************/
1460 static void FlushEIT( output_t
*p_output
, mtime_t i_dts
)
1462 block_t
*p_block
= p_output
->p_eit_ts_buffer
;
1464 psi_split_end( p_block
->p_ts
, &p_output
->i_eit_ts_buffer_offset
);
1465 p_block
->i_dts
= i_dts
;
1466 p_block
->i_refcount
--;
1467 output_Put( p_output
, p_block
);
1468 p_output
->p_eit_ts_buffer
= NULL
;
1469 p_output
->i_eit_ts_buffer_offset
= 0;
1472 /*****************************************************************************
1474 *****************************************************************************/
1475 static void SendTDT( block_t
*p_ts
)
1479 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1481 output_t
*p_output
= pp_outputs
[i
];
1483 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1484 && !p_output
->config
.b_passthrough
1485 && (p_output
->config
.i_config
& OUTPUT_DVB
)
1486 && p_output
->p_sdt_section
!= NULL
)
1487 output_Put( p_output
, p_ts
);
1490 /*****************************************************************************
1492 *****************************************************************************/
1493 static void SendEMM( block_t
*p_ts
)
1497 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1499 output_t
*p_output
= pp_outputs
[i
];
1501 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1502 && !p_output
->config
.b_passthrough
)
1503 output_Put( p_output
, p_ts
);
1507 /*****************************************************************************
1509 *****************************************************************************/
1510 static void NewPAT( output_t
*p_output
)
1512 const uint8_t *p_program
;
1516 free( p_output
->p_pat_section
);
1517 p_output
->p_pat_section
= NULL
;
1518 p_output
->i_pat_version
++;
1520 if ( !p_output
->config
.i_sid
) return;
1521 if ( !psi_table_validate(pp_current_pat_sections
) ) return;
1523 p_program
= pat_table_find_program( pp_current_pat_sections
,
1524 p_output
->config
.i_sid
);
1525 if ( p_program
== NULL
) return;
1527 p
= p_output
->p_pat_section
= psi_allocate();
1529 psi_set_length( p
, PSI_MAX_SIZE
);
1530 pat_set_tsid( p
, p_output
->i_tsid
);
1531 psi_set_version( p
, p_output
->i_pat_version
);
1532 psi_set_current( p
);
1533 psi_set_section( p
, 0 );
1534 psi_set_lastsection( p
, 0 );
1536 if ( p_output
->config
.i_config
& OUTPUT_DVB
)
1539 p
= pat_get_program( p_output
->p_pat_section
, k
++ );
1541 patn_set_program( p
, 0 );
1542 patn_set_pid( p
, NIT_PID
);
1545 p
= pat_get_program( p_output
->p_pat_section
, k
++ );
1547 if ( p_output
->config
.i_new_sid
)
1549 msg_Dbg( NULL
, "Mapping PAT SID %d to %d", p_output
->config
.i_sid
,
1550 p_output
->config
.i_new_sid
);
1551 patn_set_program( p
, p_output
->config
.i_new_sid
);
1554 patn_set_program( p
, p_output
->config
.i_sid
);
1558 msg_Dbg( NULL
, "Mapping PMT PID %d to %d", patn_get_pid( p_program
), pi_newpids
[I_PMTPID
] );
1559 patn_set_pid( p
, pi_newpids
[I_PMTPID
]);
1560 } else if ( p_output
->config
.b_do_remap
&& p_output
->config
.pi_confpids
[I_PMTPID
] ) {
1561 msg_Dbg( NULL
, "Mapping PMT PID %d to %d", patn_get_pid( p_program
), p_output
->config
.pi_confpids
[I_PMTPID
] );
1562 patn_set_pid( p
, p_output
->config
.pi_confpids
[I_PMTPID
]);
1564 patn_set_pid( p
, patn_get_pid( p_program
) );
1567 p
= pat_get_program( p_output
->p_pat_section
, k
);
1568 pat_set_length( p_output
->p_pat_section
,
1569 p
- p_output
->p_pat_section
- PAT_HEADER_SIZE
);
1570 psi_set_crc( p_output
->p_pat_section
);
1573 /*****************************************************************************
1575 *****************************************************************************/
1576 static void CopyDescriptors( uint8_t *p_descs
, uint8_t *p_current_descs
)
1579 const uint8_t *p_current_desc
;
1580 uint16_t j
= 0, k
= 0;
1582 descs_set_length( p_descs
, DESCS_MAX_SIZE
);
1584 while ( (p_current_desc
= descs_get_desc( p_current_descs
, j
)) != NULL
)
1586 uint8_t i_tag
= desc_get_tag( p_current_desc
);
1589 if ( !b_enable_ecm
&& i_tag
== 0x9 ) continue;
1591 p_desc
= descs_get_desc( p_descs
, k
);
1592 if ( p_desc
== NULL
) continue; /* This shouldn't happen */
1594 memcpy( p_desc
, p_current_desc
,
1595 DESC_HEADER_SIZE
+ desc_get_length( p_current_desc
) );
1598 p_desc
= descs_get_desc( p_descs
, k
);
1599 if ( p_desc
== NULL
)
1600 /* This shouldn't happen if the incoming PMT is valid */
1601 descs_set_length( p_descs
, 0 );
1603 descs_set_length( p_descs
, p_desc
- p_descs
- DESCS_HEADER_SIZE
);
1606 static void NewPMT( output_t
*p_output
)
1609 uint8_t *p_current_pmt
;
1610 uint8_t *p_es
, *p_current_es
;
1615 free( p_output
->p_pmt_section
);
1616 p_output
->p_pmt_section
= NULL
;
1617 p_output
->i_pmt_version
++;
1619 if ( !p_output
->config
.i_sid
) return;
1621 p_sid
= FindSID( p_output
->config
.i_sid
);
1622 if ( p_sid
== NULL
) return;
1624 if ( p_sid
->p_current_pmt
== NULL
) return;
1625 p_current_pmt
= p_sid
->p_current_pmt
;
1627 p
= p_output
->p_pmt_section
= psi_allocate();
1629 psi_set_length( p
, PSI_MAX_SIZE
);
1630 if ( p_output
->config
.i_new_sid
)
1632 msg_Dbg( NULL
, "Mapping PMT SID %d to %d", p_output
->config
.i_sid
,
1633 p_output
->config
.i_new_sid
);
1634 pmt_set_program( p
, p_output
->config
.i_new_sid
);
1637 pmt_set_program( p
, p_output
->config
.i_sid
);
1638 psi_set_version( p
, p_output
->i_pmt_version
);
1639 psi_set_current( p
);
1640 pmt_set_desclength( p
, 0 );
1641 init_pid_mapping( p_output
);
1644 CopyDescriptors( pmt_get_descs( p
), pmt_get_descs( p_current_pmt
) );
1647 while ( (p_current_es
= pmt_get_es( p_current_pmt
, j
)) != NULL
)
1649 uint16_t i_pid
= pmtn_get_pid( p_current_es
);
1652 if ( (p_output
->config
.i_nb_pids
|| !PIDWouldBeSelected( p_current_es
))
1653 && !IsIn( p_output
->config
.pi_pids
, p_output
->config
.i_nb_pids
,
1657 p_es
= pmt_get_es( p
, k
);
1658 if ( p_es
== NULL
) continue; /* This shouldn't happen */
1661 pmtn_set_streamtype( p_es
, pmtn_get_streamtype( p_current_es
) );
1662 pmtn_set_pid( p_es
, map_es_pid(p_output
, p_current_es
, i_pid
) );
1663 pmtn_set_desclength( p_es
, 0 );
1665 CopyDescriptors( pmtn_get_descs( p_es
),
1666 pmtn_get_descs( p_current_es
) );
1669 /* Do the pcr pid after everything else as it may have been remapped */
1670 i_pcrpid
= pmt_get_pcrpid( p_current_pmt
);
1671 if ( p_output
->pi_newpids
[i_pcrpid
] != UNUSED_PID
) {
1672 msg_Dbg( NULL
, "REMAP: The PCR PID was changed from 0x%x (%u) to 0x%x (%u)",
1673 i_pcrpid
, i_pcrpid
, p_output
->pi_newpids
[i_pcrpid
], p_output
->pi_newpids
[i_pcrpid
] );
1674 i_pcrpid
= p_output
->pi_newpids
[i_pcrpid
];
1676 msg_Dbg( NULL
, "The PCR PID has kept its original value of 0x%x (%u)", i_pcrpid
, i_pcrpid
);
1678 pmt_set_pcrpid( p
, i_pcrpid
);
1679 p_es
= pmt_get_es( p
, k
);
1681 /* This shouldn't happen if the incoming PMT is valid */
1682 pmt_set_length( p
, 0 );
1684 pmt_set_length( p
, p_es
- p
- PMT_HEADER_SIZE
);
1688 /*****************************************************************************
1690 *****************************************************************************/
1691 static void NewNIT( output_t
*p_output
)
1697 free( p_output
->p_nit_section
);
1698 p_output
->p_nit_section
= NULL
;
1699 p_output
->i_nit_version
++;
1701 p
= p_output
->p_nit_section
= psi_allocate();
1702 nit_init( p
, true );
1703 nit_set_length( p
, PSI_MAX_SIZE
);
1704 nit_set_nid( p
, p_output
->config
.i_network_id
);
1705 psi_set_version( p
, p_output
->i_nit_version
);
1706 psi_set_current( p
);
1707 psi_set_section( p
, 0 );
1708 psi_set_lastsection( p
, 0 );
1710 if ( p_output
->config
.network_name
.i
)
1714 nit_set_desclength( p
, DESCS_MAX_SIZE
);
1715 p_descs
= nit_get_descs( p
);
1716 p_desc
= descs_get_desc( p_descs
, 0 );
1717 desc40_init( p_desc
);
1718 desc40_set_networkname( p_desc
, p_output
->config
.network_name
.p
,
1719 p_output
->config
.network_name
.i
);
1720 p_desc
= descs_get_desc( p_descs
, 1 );
1721 descs_set_length( p_descs
, p_desc
- p_descs
- DESCS_HEADER_SIZE
);
1724 nit_set_desclength( p
, 0 );
1726 p_header2
= nit_get_header2( p
);
1727 nith_init( p_header2
);
1728 nith_set_tslength( p_header2
, NIT_TS_SIZE
);
1730 p_ts
= nit_get_ts( p
, 0 );
1732 nitn_set_tsid( p_ts
, p_output
->i_tsid
);
1733 if ( p_output
->config
.i_onid
)
1734 nitn_set_onid( p_ts
, p_output
->config
.i_onid
);
1736 nitn_set_onid( p_ts
, p_output
->config
.i_network_id
);
1737 nitn_set_desclength( p_ts
, 0 );
1739 p_ts
= nit_get_ts( p
, 1 );
1741 /* This shouldn't happen */
1742 nit_set_length( p
, 0 );
1744 nit_set_length( p
, p_ts
- p
- NIT_HEADER_SIZE
);
1745 psi_set_crc( p_output
->p_nit_section
);
1748 /*****************************************************************************
1750 *****************************************************************************/
1751 static void NewSDT( output_t
*p_output
)
1753 uint8_t *p_service
, *p_current_service
;
1756 free( p_output
->p_sdt_section
);
1757 p_output
->p_sdt_section
= NULL
;
1758 p_output
->i_sdt_version
++;
1760 if ( !p_output
->config
.i_sid
) return;
1761 if ( !psi_table_validate(pp_current_sdt_sections
) ) return;
1763 p_current_service
= sdt_table_find_service( pp_current_sdt_sections
,
1764 p_output
->config
.i_sid
);
1766 if ( p_current_service
== NULL
)
1768 if ( p_output
->p_pat_section
!= NULL
&&
1769 pat_get_program( p_output
->p_pat_section
, 0 ) == NULL
)
1771 /* Empty PAT and no SDT anymore */
1772 free( p_output
->p_pat_section
);
1773 p_output
->p_pat_section
= NULL
;
1774 p_output
->i_pat_version
++;
1779 p
= p_output
->p_sdt_section
= psi_allocate();
1780 sdt_init( p
, true );
1781 sdt_set_length( p
, PSI_MAX_SIZE
);
1782 sdt_set_tsid( p
, p_output
->i_tsid
);
1783 psi_set_version( p
, p_output
->i_sdt_version
);
1784 psi_set_current( p
);
1785 psi_set_section( p
, 0 );
1786 psi_set_lastsection( p
, 0 );
1787 if ( p_output
->config
.i_onid
)
1788 sdt_set_onid( p
, p_output
->config
.i_onid
);
1791 sdt_get_onid( psi_table_get_section( pp_current_sdt_sections
, 0 ) ) );
1793 p_service
= sdt_get_service( p
, 0 );
1794 sdtn_init( p_service
);
1795 if ( p_output
->config
.i_new_sid
)
1797 msg_Dbg( NULL
, "Mapping SDT SID %d to %d", p_output
->config
.i_sid
,
1798 p_output
->config
.i_new_sid
);
1799 sdtn_set_sid( p_service
, p_output
->config
.i_new_sid
);
1802 sdtn_set_sid( p_service
, p_output
->config
.i_sid
);
1804 /* We always forward EITp/f */
1805 if ( sdtn_get_eitpresent(p_current_service
) )
1806 sdtn_set_eitpresent(p_service
);
1808 if ( (p_output
->config
.i_config
& OUTPUT_EPG
) == OUTPUT_EPG
&&
1809 sdtn_get_eitschedule(p_current_service
) )
1810 sdtn_set_eitschedule(p_service
);
1812 sdtn_set_running( p_service
, sdtn_get_running(p_current_service
) );
1813 /* Do not set free_ca */
1814 sdtn_set_desclength( p_service
, sdtn_get_desclength(p_current_service
) );
1816 if ( !p_output
->config
.provider_name
.i
&&
1817 !p_output
->config
.service_name
.i
) {
1818 /* Copy all descriptors unchanged */
1819 memcpy( descs_get_desc( sdtn_get_descs(p_service
), 0 ),
1820 descs_get_desc( sdtn_get_descs(p_current_service
), 0 ),
1821 sdtn_get_desclength(p_current_service
) );
1823 int j
= 0, i_total_desc_len
= 0;
1825 uint8_t *p_new_desc
= descs_get_desc( sdtn_get_descs(p_service
), 0 );
1826 while ( (p_desc
= descs_get_desc( sdtn_get_descs( p_current_service
), j
++ )) != NULL
)
1828 /* Regenerate descriptor 48 (service name) */
1829 if ( desc_get_tag( p_desc
) == 0x48 && desc48_validate( p_desc
) )
1831 uint8_t i_old_provider_len
, i_old_service_len
;
1832 uint8_t i_new_desc_len
= 3; /* 1 byte - type, 1 byte provider_len, 1 byte service_len */
1833 const uint8_t *p_old_provider
= desc48_get_provider( p_desc
, &i_old_provider_len
);
1834 const uint8_t *p_old_service
= desc48_get_service( p_desc
, &i_old_service_len
);
1836 desc48_init( p_new_desc
);
1837 desc48_set_type( p_new_desc
, desc48_get_type( p_desc
) );
1839 if ( p_output
->config
.provider_name
.i
) {
1840 desc48_set_provider( p_new_desc
,
1841 p_output
->config
.provider_name
.p
,
1842 p_output
->config
.provider_name
.i
);
1843 i_new_desc_len
+= p_output
->config
.provider_name
.i
;
1845 desc48_set_provider( p_new_desc
, p_old_provider
,
1846 i_old_provider_len
);
1847 i_new_desc_len
+= i_old_provider_len
;
1850 if ( p_output
->config
.service_name
.i
) {
1851 desc48_set_service( p_new_desc
,
1852 p_output
->config
.service_name
.p
,
1853 p_output
->config
.service_name
.i
);
1854 i_new_desc_len
+= p_output
->config
.service_name
.i
;
1856 desc48_set_service( p_new_desc
, p_old_service
,
1857 i_old_service_len
);
1858 i_new_desc_len
+= i_old_service_len
;
1861 desc_set_length( p_new_desc
, i_new_desc_len
);
1862 i_total_desc_len
+= DESC_HEADER_SIZE
+ i_new_desc_len
;
1863 p_new_desc
+= DESC_HEADER_SIZE
+ i_new_desc_len
;
1865 /* Copy single descriptor */
1866 int i_desc_len
= DESC_HEADER_SIZE
+ desc_get_length( p_desc
);
1867 memcpy( p_new_desc
, p_desc
, i_desc_len
);
1868 p_new_desc
+= i_desc_len
;
1869 i_total_desc_len
+= i_desc_len
;
1872 sdtn_set_desclength( p_service
, i_total_desc_len
);
1875 p_service
= sdt_get_service( p
, 1 );
1876 if ( p_service
== NULL
)
1877 /* This shouldn't happen if the incoming SDT is valid */
1878 sdt_set_length( p
, 0 );
1880 sdt_set_length( p
, p_service
- p
- SDT_HEADER_SIZE
);
1881 psi_set_crc( p_output
->p_sdt_section
);
1884 /*****************************************************************************
1886 *****************************************************************************/
1887 #define DECLARE_UPDATE_FUNC( table ) \
1888 static void Update##table( uint16_t i_sid ) \
1892 for ( i = 0; i < i_nb_outputs; i++ ) \
1893 if ( ( pp_outputs[i]->config.i_config & OUTPUT_VALID ) \
1894 && pp_outputs[i]->config.i_sid == i_sid ) \
1895 New##table( pp_outputs[i] ); \
1898 DECLARE_UPDATE_FUNC(PAT
)
1899 DECLARE_UPDATE_FUNC(PMT
)
1900 DECLARE_UPDATE_FUNC(SDT
)
1902 /*****************************************************************************
1904 *****************************************************************************/
1905 static void UpdateTSID(void)
1907 uint16_t i_tsid
= psi_table_get_tableidext(pp_current_pat_sections
);
1910 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1912 output_t
*p_output
= pp_outputs
[i
];
1914 if ( (p_output
->config
.i_config
& OUTPUT_VALID
)
1915 && p_output
->config
.i_tsid
== -1 && !b_random_tsid
)
1917 p_output
->i_tsid
= i_tsid
;
1923 /*****************************************************************************
1925 *****************************************************************************/
1926 static bool SIDIsSelected( uint16_t i_sid
)
1930 for ( i
= 0; i
< i_nb_outputs
; i
++ )
1931 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
1932 && pp_outputs
[i
]->config
.i_sid
== i_sid
)
1938 /*****************************************************************************
1939 * demux_PIDIsSelected
1940 *****************************************************************************/
1941 bool demux_PIDIsSelected( uint16_t i_pid
)
1945 for ( i
= 0; i
< p_pids
[i_pid
].i_nb_outputs
; i
++ )
1946 if ( p_pids
[i_pid
].pp_outputs
[i
] != NULL
)
1952 /*****************************************************************************
1953 * PIDWouldBeSelected
1954 *****************************************************************************/
1955 static bool PIDWouldBeSelected( uint8_t *p_es
)
1957 if ( b_any_type
) return true;
1959 uint8_t i_type
= pmtn_get_streamtype( p_es
);
1963 case 0x1: /* video MPEG-1 */
1964 case 0x2: /* video */
1965 case 0x3: /* audio MPEG-1 */
1966 case 0x4: /* audio */
1967 case 0xf: /* audio AAC ADTS */
1968 case 0x10: /* video MPEG-4 */
1969 case 0x11: /* audio AAC LATM */
1970 case 0x1b: /* video H264 */
1971 case 0x24: /* video H265 */
1972 case 0x81: /* ATSC A/52 */
1973 case 0x87: /* ATSC Enhanced A/52 */
1980 const uint8_t *p_desc
;
1982 while ( (p_desc
= descs_get_desc( pmtn_get_descs( p_es
), j
)) != NULL
)
1984 uint8_t i_tag
= desc_get_tag( p_desc
);
1987 if( i_tag
== 0x46 /* VBI + teletext */
1988 || i_tag
== 0x56 /* teletext */
1989 || i_tag
== 0x59 /* dvbsub */
1990 || i_tag
== 0x6a /* A/52 */
1991 || i_tag
== 0x7a /* Enhanced A/52 */
1992 || i_tag
== 0x7b /* DCA */
1993 || i_tag
== 0x7c /* AAC */ )
2003 /* FIXME: also parse IOD */
2007 /*****************************************************************************
2009 *****************************************************************************/
2010 static bool PIDCarriesPES( const uint8_t *p_es
)
2012 uint8_t i_type
= pmtn_get_streamtype( p_es
);
2016 case 0x1: /* video MPEG-1 */
2017 case 0x2: /* video */
2018 case 0x3: /* audio MPEG-1 */
2019 case 0x4: /* audio */
2020 case 0x6: /* private PES data */
2021 case 0xf: /* audio AAC */
2022 case 0x10: /* video MPEG-4 */
2023 case 0x11: /* audio AAC LATM */
2024 case 0x1b: /* video H264 */
2025 case 0x24: /* video H265 */
2026 case 0x81: /* ATSC A/52 */
2027 case 0x87: /* ATSC Enhanced A/52 */
2037 /*****************************************************************************
2038 * PMTNeedsDescrambling
2039 *****************************************************************************/
2040 static bool PMTNeedsDescrambling( uint8_t *p_pmt
)
2045 const uint8_t *p_desc
;
2048 while ( (p_desc
= descs_get_desc( pmt_get_descs( p_pmt
), j
)) != NULL
)
2050 uint8_t i_tag
= desc_get_tag( p_desc
);
2053 if ( i_tag
== 0x9 ) return true;
2057 while ( (p_es
= pmt_get_es( p_pmt
, i
)) != NULL
)
2061 while ( (p_desc
= descs_get_desc( pmtn_get_descs( p_es
), j
)) != NULL
)
2063 uint8_t i_tag
= desc_get_tag( p_desc
);
2066 if ( i_tag
== 0x9 ) return true;
2073 /*****************************************************************************
2074 * demux_ResendCAPMTs
2075 *****************************************************************************/
2076 void demux_ResendCAPMTs( void )
2079 for ( i
= 0; i
< i_nb_sids
; i
++ )
2080 if ( pp_sids
[i
]->p_current_pmt
!= NULL
2081 && SIDIsSelected( pp_sids
[i
]->i_sid
)
2082 && PMTNeedsDescrambling( pp_sids
[i
]->p_current_pmt
) )
2083 en50221_AddPMT( pp_sids
[i
]->p_current_pmt
);
2086 /* Find CA descriptor that have PID i_ca_pid */
2087 static uint8_t *ca_desc_find( uint8_t *p_descl
, uint16_t i_length
,
2093 while ( (p_desc
= descl_get_desc( p_descl
, i_length
, j
++ )) != NULL
) {
2094 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2096 if ( desc09_get_pid( p_desc
) == i_ca_pid
)
2103 /*****************************************************************************
2105 *****************************************************************************/
2106 static void DeleteProgram( uint16_t i_sid
, uint16_t i_pid
)
2112 UnselectPMT( i_sid
, i_pid
);
2114 p_sid
= FindSID( i_sid
);
2115 if ( p_sid
== NULL
) return;
2117 p_pmt
= p_sid
->p_current_pmt
;
2119 if ( p_pmt
!= NULL
)
2121 uint16_t i_pcr_pid
= pmt_get_pcrpid( p_pmt
);
2125 if ( i_ca_handle
&& SIDIsSelected( i_sid
)
2126 && PMTNeedsDescrambling( p_pmt
) )
2127 en50221_DeletePMT( p_pmt
);
2129 if ( i_pcr_pid
!= PADDING_PID
2130 && i_pcr_pid
!= p_sid
->i_pmt_pid
)
2131 UnselectPID( i_sid
, i_pcr_pid
);
2137 while ((p_desc
= descs_get_desc( pmt_get_descs( p_pmt
), j
++ )) != NULL
)
2139 if ( desc_get_tag( p_desc
) != 0x09 ||
2140 !desc09_validate( p_desc
) )
2142 UnselectPID( i_sid
, desc09_get_pid( p_desc
) );
2147 while ( (p_es
= pmt_get_es( p_pmt
, j
)) != NULL
)
2149 uint16_t i_pid
= pmtn_get_pid( p_es
);
2152 if ( PIDWouldBeSelected( p_es
) )
2153 UnselectPID( i_sid
, i_pid
);
2159 while ((p_desc
= descs_get_desc( pmtn_get_descs( p_es
), k
++ )) != NULL
)
2161 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2163 UnselectPID( i_sid
, desc09_get_pid( p_desc
) );
2169 p_sid
->p_current_pmt
= NULL
;
2172 p_sid
->i_pmt_pid
= 0;
2173 psi_table_free(p_sid
->pp_eit_sections
);
2174 psi_table_init(p_sid
->pp_eit_sections
);
2177 /*****************************************************************************
2179 *****************************************************************************
2180 * This code is from biTStream's examples and is under the WTFPL (see
2182 *****************************************************************************/
2183 static char *iconv_append_null(const char *p_string
, size_t i_length
)
2185 char *psz_string
= malloc(i_length
+ 1);
2186 memcpy(psz_string
, p_string
, i_length
);
2187 psz_string
[i_length
] = '\0';
2191 char *demux_Iconv(void *_unused
, const char *psz_encoding
,
2192 char *p_string
, size_t i_length
)
2195 static const char *psz_current_encoding
= "";
2197 char *psz_string
, *p
;
2198 size_t i_out_length
;
2200 if (!strcmp(psz_encoding
, psz_native_charset
))
2201 return iconv_append_null(p_string
, i_length
);
2203 if (iconv_handle
!= (iconv_t
)-1 &&
2204 strcmp(psz_encoding
, psz_current_encoding
)) {
2205 iconv_close(iconv_handle
);
2206 iconv_handle
= (iconv_t
)-1;
2209 if (iconv_handle
== (iconv_t
)-1)
2210 iconv_handle
= iconv_open(psz_native_charset
, psz_encoding
);
2211 if (iconv_handle
== (iconv_t
)-1) {
2212 msg_Warn(NULL
, "couldn't open converter from %s to %s (%m)", psz_encoding
,
2213 psz_native_charset
);
2214 return iconv_append_null(p_string
, i_length
);
2216 psz_current_encoding
= psz_encoding
;
2218 /* converted strings can be up to six times larger */
2219 i_out_length
= i_length
* 6;
2220 p
= psz_string
= malloc(i_out_length
);
2221 if (iconv(iconv_handle
, &p_string
, &i_length
, &p
, &i_out_length
) == -1) {
2222 msg_Warn(NULL
, "couldn't convert from %s to %s (%m)", psz_encoding
,
2223 psz_native_charset
);
2225 return iconv_append_null(p_string
, i_length
);
2228 msg_Warn(NULL
, "partial conversion from %s to %s", psz_encoding
,
2229 psz_native_charset
);
2234 return iconv_append_null(p_string
, i_length
);
2238 /*****************************************************************************
2240 *****************************************************************************
2241 * This code is from biTStream's examples and is under the WTFPL (see
2243 *****************************************************************************/
2244 __attribute__ ((format(printf
, 2, 3)))
2245 static void demux_Print(void *_unused
, const char *psz_format
, ...)
2247 char psz_fmt
[strlen(psz_format
) + 2];
2249 va_start(args
, psz_format
);
2250 strcpy(psz_fmt
, psz_format
);
2251 if ( i_print_type
!= PRINT_XML
)
2252 strcat(psz_fmt
, "\n");
2253 vprintf(psz_fmt
, args
);
2257 /*****************************************************************************
2259 *****************************************************************************/
2260 static void HandlePAT( mtime_t i_dts
)
2262 bool b_change
= false;
2263 PSI_TABLE_DECLARE( pp_old_pat_sections
);
2264 uint8_t i_last_section
= psi_table_get_lastsection( pp_next_pat_sections
);
2267 if ( psi_table_validate( pp_current_pat_sections
) &&
2268 psi_table_compare( pp_current_pat_sections
, pp_next_pat_sections
) )
2270 /* Identical PAT. Shortcut. */
2271 psi_table_free( pp_next_pat_sections
);
2272 psi_table_init( pp_next_pat_sections
);
2276 if ( !pat_table_validate( pp_next_pat_sections
) )
2278 msg_Warn( NULL
, "invalid PAT received" );
2279 switch (i_print_type
) {
2281 fprintf(print_fh
, "<ERROR type=\"invalid_pat\"/>\n");
2284 fprintf(print_fh
, "error type: invalid_pat\n");
2289 psi_table_free( pp_next_pat_sections
);
2290 psi_table_init( pp_next_pat_sections
);
2294 /* Switch tables. */
2295 psi_table_copy( pp_old_pat_sections
, pp_current_pat_sections
);
2296 psi_table_copy( pp_current_pat_sections
, pp_next_pat_sections
);
2297 psi_table_init( pp_next_pat_sections
);
2299 if ( !psi_table_validate( pp_old_pat_sections
)
2300 || psi_table_get_tableidext( pp_current_pat_sections
)
2301 != psi_table_get_tableidext( pp_old_pat_sections
) )
2305 /* This will trigger a universal reset of everything. */
2308 for ( i
= 0; i
<= i_last_section
; i
++ )
2310 uint8_t *p_section
=
2311 psi_table_get_section( pp_current_pat_sections
, i
);
2312 const uint8_t *p_program
;
2315 while ( (p_program
= pat_get_program( p_section
, j
)) != NULL
)
2317 const uint8_t *p_old_program
= NULL
;
2318 uint16_t i_sid
= patn_get_program( p_program
);
2319 uint16_t i_pid
= patn_get_pid( p_program
);
2324 if ( i_pid
!= NIT_PID
)
2326 "NIT is carried on PID %hu which isn't DVB compliant",
2331 if ( !psi_table_validate( pp_old_pat_sections
)
2332 || (p_old_program
= pat_table_find_program(
2333 pp_old_pat_sections
, i_sid
)) == NULL
2334 || patn_get_pid( p_old_program
) != i_pid
2339 if ( p_old_program
!= NULL
)
2340 DeleteProgram( i_sid
, patn_get_pid( p_old_program
) );
2342 SelectPMT( i_sid
, i_pid
);
2344 p_sid
= FindSID( 0 );
2345 if ( p_sid
== NULL
)
2347 p_sid
= malloc( sizeof(sid_t
) );
2348 p_sid
->p_current_pmt
= NULL
;
2349 psi_table_init(p_sid
->pp_eit_sections
);
2351 pp_sids
= realloc( pp_sids
, sizeof(sid_t
*) * i_nb_sids
);
2352 pp_sids
[i_nb_sids
- 1] = p_sid
;
2355 p_sid
->i_sid
= i_sid
;
2356 p_sid
->i_pmt_pid
= i_pid
;
2363 if ( psi_table_validate( pp_old_pat_sections
) )
2365 i_last_section
= psi_table_get_lastsection( pp_old_pat_sections
);
2366 for ( i
= 0; i
<= i_last_section
; i
++ )
2368 uint8_t *p_section
=
2369 psi_table_get_section( pp_old_pat_sections
, i
);
2370 const uint8_t *p_program
;
2373 while ( (p_program
= pat_get_program( p_section
, j
)) != NULL
)
2375 uint16_t i_sid
= patn_get_program( p_program
);
2376 uint16_t i_pid
= patn_get_pid( p_program
);
2382 if ( pat_table_find_program( pp_current_pat_sections
, i_sid
)
2385 DeleteProgram( i_sid
, i_pid
);
2391 psi_table_free( pp_old_pat_sections
);
2394 pat_table_print( pp_current_pat_sections
, msg_Dbg
, NULL
, PRINT_TEXT
);
2395 if ( b_print_enabled
)
2397 pat_table_print( pp_current_pat_sections
, demux_Print
, NULL
,
2399 if ( i_print_type
== PRINT_XML
)
2400 fprintf(print_fh
, "\n");
2407 /*****************************************************************************
2409 *****************************************************************************/
2410 static void HandlePATSection( uint16_t i_pid
, uint8_t *p_section
,
2413 if ( i_pid
!= PAT_PID
|| !pat_validate( p_section
) )
2415 msg_Warn( NULL
, "invalid PAT section received on PID %hu", i_pid
);
2416 switch (i_print_type
) {
2418 fprintf(print_fh
, "<ERROR type=\"invalid_pat_section\"/>\n");
2421 fprintf(print_fh
, "error type: invalid_pat_section\n");
2430 if ( !psi_table_section( pp_next_pat_sections
, p_section
) )
2436 /*****************************************************************************
2438 *****************************************************************************/
2439 static void HandleCAT( mtime_t i_dts
)
2441 PSI_TABLE_DECLARE( pp_old_cat_sections
);
2442 uint8_t i_last_section
= psi_table_get_lastsection( pp_next_cat_sections
);
2443 uint8_t i_last_section2
;
2448 if ( psi_table_validate( pp_current_cat_sections
) &&
2449 psi_table_compare( pp_current_cat_sections
, pp_next_cat_sections
) )
2451 /* Identical CAT. Shortcut. */
2452 psi_table_free( pp_next_cat_sections
);
2453 psi_table_init( pp_next_cat_sections
);
2457 if ( !cat_table_validate( pp_next_cat_sections
) )
2459 msg_Warn( NULL
, "invalid CAT received" );
2460 switch (i_print_type
) {
2462 fprintf(print_fh
, "<ERROR type=\"invalid_cat\"/>\n");
2465 fprintf(print_fh
, "error type: invalid_cat\n");
2470 psi_table_free( pp_next_cat_sections
);
2471 psi_table_init( pp_next_cat_sections
);
2475 /* Switch tables. */
2476 psi_table_copy( pp_old_cat_sections
, pp_current_cat_sections
);
2477 psi_table_copy( pp_current_cat_sections
, pp_next_cat_sections
);
2478 psi_table_init( pp_next_cat_sections
);
2480 for ( i
= 0; i
<= i_last_section
; i
++ )
2482 uint8_t *p_section
= psi_table_get_section( pp_current_cat_sections
, i
);
2485 while ( (p_desc
= descl_get_desc( cat_get_descl(p_section
), cat_get_desclength(p_section
), j
++ )) != NULL
)
2487 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2490 SetPID_EMM( desc09_get_pid( p_desc
) );
2494 if ( psi_table_validate( pp_old_cat_sections
) )
2496 i_last_section
= psi_table_get_lastsection( pp_old_cat_sections
);
2497 for ( i
= 0; i
<= i_last_section
; i
++ )
2499 uint8_t *p_old_section
= psi_table_get_section( pp_old_cat_sections
, i
);
2501 while ( (p_desc
= descl_get_desc( cat_get_descl(p_old_section
), cat_get_desclength(p_old_section
), j
++ )) != NULL
)
2506 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2509 emm_pid
= desc09_get_pid( p_desc
);
2511 // Search in current sections if the pid exists
2512 i_last_section2
= psi_table_get_lastsection( pp_current_cat_sections
);
2513 for ( r
= 0; r
<= i_last_section2
; r
++ )
2515 uint8_t *p_section
= psi_table_get_section( pp_current_cat_sections
, r
);
2518 while ( (p_desc
= descl_get_desc( cat_get_descl(p_section
), cat_get_desclength(p_section
), k
++ )) != NULL
)
2520 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2522 if ( ca_desc_find( cat_get_descl(p_section
), cat_get_desclength(p_section
), emm_pid
) != NULL
)
2535 psi_table_free( pp_old_cat_sections
);
2538 cat_table_print( pp_current_cat_sections
, msg_Dbg
, NULL
, PRINT_TEXT
);
2539 if ( b_print_enabled
)
2541 cat_table_print( pp_current_cat_sections
, demux_Print
, NULL
,
2543 if ( i_print_type
== PRINT_XML
)
2544 fprintf(print_fh
, "\n");
2551 /*****************************************************************************
2553 *****************************************************************************/
2554 static void HandleCATSection( uint16_t i_pid
, uint8_t *p_section
,
2557 if ( i_pid
!= CAT_PID
|| !cat_validate( p_section
) )
2559 msg_Warn( NULL
, "invalid CAT section received on PID %hu", i_pid
);
2560 switch (i_print_type
) {
2562 fprintf(print_fh
, "<ERROR type=\"invalid_cat_section\"/>\n");
2565 fprintf(print_fh
, "error type: invalid_cat_section\n");
2574 if ( !psi_table_section( pp_next_cat_sections
, p_section
) )
2580 static void mark_pmt_pids( uint8_t *p_pmt
, uint8_t pid_map
[], uint8_t marker
)
2586 uint16_t i_pcr_pid
= pmt_get_pcrpid( p_pmt
);
2591 while ( (p_desc
= descs_get_desc( pmt_get_descs( p_pmt
), j
++ )) != NULL
)
2593 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2595 pid_map
[ desc09_get_pid( p_desc
) ] |= marker
;
2599 if ( i_pcr_pid
!= PADDING_PID
)
2600 pid_map
[ i_pcr_pid
] |= marker
;
2603 while ( (p_es
= pmt_get_es( p_pmt
, j
)) != NULL
)
2605 uint16_t i_pid
= pmtn_get_pid( p_es
);
2608 if ( PIDWouldBeSelected( p_es
) )
2609 pid_map
[ i_pid
] |= marker
;
2611 p_pids
[i_pid
].b_pes
= PIDCarriesPES( p_es
);
2616 while ( (p_desc
= descs_get_desc( pmtn_get_descs( p_es
), k
++ )) != NULL
)
2618 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
2620 pid_map
[ desc09_get_pid( p_desc
) ] |= marker
;
2626 /*****************************************************************************
2628 *****************************************************************************/
2629 static void HandlePMT( uint16_t i_pid
, uint8_t *p_pmt
, mtime_t i_dts
)
2631 uint16_t i_sid
= pmt_get_program( p_pmt
);
2633 bool b_needs_descrambling
, b_needed_descrambling
, b_is_selected
;
2634 uint8_t pid_map
[MAX_PIDS
];
2636 p_sid
= FindSID( i_sid
);
2637 if ( p_sid
== NULL
)
2639 /* Unwanted SID (happens when the same PMT PID is used for several
2645 if ( i_pid
!= p_sid
->i_pmt_pid
)
2647 msg_Warn( NULL
, "invalid PMT section received on PID %hu", i_pid
);
2648 switch (i_print_type
) {
2650 fprintf(print_fh
, "<ERROR type=\"ghost_pmt\" program=\"%hu\n pid=\"%hu\"/>\n",
2654 fprintf(print_fh
, "error type: ghost_pmt program: %hu pid: %hu\n",
2664 if ( p_sid
->p_current_pmt
!= NULL
&&
2665 psi_compare( p_sid
->p_current_pmt
, p_pmt
) )
2667 /* Identical PMT. Shortcut. */
2672 if ( !pmt_validate( p_pmt
) )
2674 msg_Warn( NULL
, "invalid PMT section received on PID %hu", i_pid
);
2675 switch (i_print_type
) {
2677 fprintf(print_fh
, "<ERROR type=\"invalid_pmt_section\" pid=\"%hu\"/>\n",
2681 fprintf(print_fh
, "error type: invalid_pmt_section pid: %hu\n",
2691 memset( pid_map
, 0, sizeof(pid_map
) );
2693 b_needs_descrambling
= PMTNeedsDescrambling( p_pmt
);
2694 b_needed_descrambling
= p_sid
->p_current_pmt
!= NULL
?
2695 PMTNeedsDescrambling( p_sid
->p_current_pmt
) :
2697 b_is_selected
= SIDIsSelected( i_sid
);
2699 if ( i_ca_handle
&& b_is_selected
&&
2700 !b_needs_descrambling
&& b_needed_descrambling
)
2701 en50221_DeletePMT( p_sid
->p_current_pmt
);
2703 if ( p_sid
->p_current_pmt
!= NULL
)
2705 mark_pmt_pids( p_sid
->p_current_pmt
, pid_map
, 0x02 );
2706 free( p_sid
->p_current_pmt
);
2709 mark_pmt_pids( p_pmt
, pid_map
, 0x01 );
2711 uint16_t i_pcr_pid
= pmt_get_pcrpid( p_pmt
);
2713 for ( i
= 0; i
< i_nb_outputs
; i
++ )
2714 if ( (pp_outputs
[i
]->config
.i_config
& OUTPUT_VALID
)
2715 && pp_outputs
[i
]->config
.i_sid
== i_sid
)
2716 pp_outputs
[i
]->i_pcr_pid
= 0;
2718 /* Start to stream PIDs */
2720 for ( pid
= 0; pid
< MAX_PIDS
; pid
++ )
2722 /* The pid does not exist in the old PMT and in the new PMT. Ignore this pid. */
2723 if ( !pid_map
[ pid
] )
2726 switch ( pid_map
[ pid
] & 0x03 ) {
2727 case 0x03: /* The pid exists in the old PMT and in the new PMT. The pid was already selected in case 0x01. */
2729 case 0x02: /* The pid does not exist in the new PMT but exists in the old PMT. Unselect it. */
2730 UnselectPID( i_sid
, pid
);
2732 case 0x01: /* The pid exists in new PMT. Select it. */
2733 SelectPID( i_sid
, pid
, pid
== i_pcr_pid
);
2738 p_sid
->p_current_pmt
= p_pmt
;
2740 if ( i_ca_handle
&& b_is_selected
)
2742 if ( b_needs_descrambling
&& !b_needed_descrambling
)
2743 en50221_AddPMT( p_pmt
);
2744 else if ( b_needs_descrambling
&& b_needed_descrambling
)
2745 en50221_UpdatePMT( p_pmt
);
2750 pmt_print( p_pmt
, msg_Dbg
, NULL
, demux_Iconv
, NULL
, PRINT_TEXT
);
2751 if ( b_print_enabled
)
2753 pmt_print( p_pmt
, demux_Print
, NULL
, demux_Iconv
, NULL
,
2755 if ( i_print_type
== PRINT_XML
)
2756 fprintf(print_fh
, "\n");
2760 SendPMT( p_sid
, i_dts
);
2763 /*****************************************************************************
2765 *****************************************************************************/
2766 static void HandleNIT( mtime_t i_dts
)
2768 if ( psi_table_validate( pp_current_nit_sections
) &&
2769 psi_table_compare( pp_current_nit_sections
, pp_next_nit_sections
) )
2771 /* Identical NIT. Shortcut. */
2772 psi_table_free( pp_next_nit_sections
);
2773 psi_table_init( pp_next_nit_sections
);
2777 if ( !nit_table_validate( pp_next_nit_sections
) )
2779 msg_Warn( NULL
, "invalid NIT received" );
2780 switch (i_print_type
) {
2782 fprintf(print_fh
, "<ERROR type=\"invalid_nit\"/>\n");
2785 fprintf(print_fh
, "error type: invalid_nit\n");
2790 psi_table_free( pp_next_nit_sections
);
2791 psi_table_init( pp_next_nit_sections
);
2795 /* Switch tables. */
2796 psi_table_free( pp_current_nit_sections
);
2797 psi_table_copy( pp_current_nit_sections
, pp_next_nit_sections
);
2798 psi_table_init( pp_next_nit_sections
);
2800 nit_table_print( pp_current_nit_sections
, msg_Dbg
, NULL
,
2801 demux_Iconv
, NULL
, PRINT_TEXT
);
2802 if ( b_print_enabled
)
2804 nit_table_print( pp_current_nit_sections
, demux_Print
, NULL
,
2805 demux_Iconv
, NULL
, i_print_type
);
2806 if ( i_print_type
== PRINT_XML
)
2807 fprintf(print_fh
, "\n");
2814 /*****************************************************************************
2816 *****************************************************************************/
2817 static void HandleNITSection( uint16_t i_pid
, uint8_t *p_section
,
2820 if ( i_pid
!= NIT_PID
|| !nit_validate( p_section
) )
2822 msg_Warn( NULL
, "invalid NIT section received on PID %hu", i_pid
);
2823 switch (i_print_type
) {
2825 fprintf(print_fh
, "<ERROR type=\"invalid_nit_section\" pid=\"%hu\"/>\n",
2829 fprintf(print_fh
, "error type: invalid_nit_section pid: %hu\n",
2839 if ( psi_table_section( pp_next_nit_sections
, p_section
) )
2842 /* This case is different because DVB specifies a minimum bitrate for
2843 * PID 0x10, even if we don't have any thing to send (for cheap
2844 * transport over network boundaries). */
2849 /*****************************************************************************
2851 *****************************************************************************/
2852 static void HandleSDT( mtime_t i_dts
)
2854 PSI_TABLE_DECLARE( pp_old_sdt_sections
);
2855 uint8_t i_last_section
= psi_table_get_lastsection( pp_next_sdt_sections
);
2859 if ( psi_table_validate( pp_current_sdt_sections
) &&
2860 psi_table_compare( pp_current_sdt_sections
, pp_next_sdt_sections
) )
2862 /* Identical SDT. Shortcut. */
2863 psi_table_free( pp_next_sdt_sections
);
2864 psi_table_init( pp_next_sdt_sections
);
2868 if ( !sdt_table_validate( pp_next_sdt_sections
) )
2870 msg_Warn( NULL
, "invalid SDT received" );
2871 switch (i_print_type
) {
2873 fprintf(print_fh
, "<ERROR type=\"invalid_sdt\"/>\n");
2876 fprintf(print_fh
, "error type: invalid_sdt\n");
2881 psi_table_free( pp_next_sdt_sections
);
2882 psi_table_init( pp_next_sdt_sections
);
2886 /* Switch tables. */
2887 psi_table_copy( pp_old_sdt_sections
, pp_current_sdt_sections
);
2888 psi_table_copy( pp_current_sdt_sections
, pp_next_sdt_sections
);
2889 psi_table_init( pp_next_sdt_sections
);
2891 for ( i
= 0; i
<= i_last_section
; i
++ )
2893 uint8_t *p_section
=
2894 psi_table_get_section( pp_current_sdt_sections
, i
);
2898 while ( (p_service
= sdt_get_service( p_section
, j
)) != NULL
)
2900 uint16_t i_sid
= sdtn_get_sid( p_service
);
2907 if ( psi_table_validate( pp_old_sdt_sections
) )
2909 i_last_section
= psi_table_get_lastsection( pp_old_sdt_sections
);
2910 for ( i
= 0; i
<= i_last_section
; i
++ )
2912 uint8_t *p_section
=
2913 psi_table_get_section( pp_old_sdt_sections
, i
);
2914 const uint8_t *p_service
;
2917 while ( (p_service
= sdt_get_service( p_section
, j
)) != NULL
)
2919 uint16_t i_sid
= sdtn_get_sid( p_service
);
2922 if ( sdt_table_find_service( pp_current_sdt_sections
, i_sid
)
2928 psi_table_free( pp_old_sdt_sections
);
2931 sdt_table_print( pp_current_sdt_sections
, msg_Dbg
, NULL
,
2932 demux_Iconv
, NULL
, PRINT_TEXT
);
2933 if ( b_print_enabled
)
2935 sdt_table_print( pp_current_sdt_sections
, demux_Print
, NULL
,
2936 demux_Iconv
, NULL
, i_print_type
);
2937 if ( i_print_type
== PRINT_XML
)
2938 fprintf(print_fh
, "\n");
2945 /*****************************************************************************
2947 *****************************************************************************/
2948 static void HandleSDTSection( uint16_t i_pid
, uint8_t *p_section
,
2951 if ( i_pid
!= SDT_PID
|| !sdt_validate( p_section
) )
2953 msg_Warn( NULL
, "invalid SDT section received on PID %hu", i_pid
);
2954 switch (i_print_type
) {
2956 fprintf(print_fh
, "<ERROR type=\"invalid_sdt_section\" pid=\"%hu\"/>\n",
2960 fprintf(print_fh
, "error type: invalid_sdt_section pid: %hu\n",
2970 if ( !psi_table_section( pp_next_sdt_sections
, p_section
) )
2976 /*****************************************************************************
2978 *****************************************************************************/
2979 static void HandleEIT( uint16_t i_pid
, uint8_t *p_eit
, mtime_t i_dts
)
2981 uint8_t i_table_id
= psi_get_tableid( p_eit
);
2982 uint16_t i_sid
= eit_get_sid( p_eit
);
2985 p_sid
= FindSID( i_sid
);
2986 if ( p_sid
== NULL
)
2988 /* Not a selected program. */
2993 if ( i_pid
!= EIT_PID
|| !eit_validate( p_eit
) )
2995 msg_Warn( NULL
, "invalid EIT section received on PID %hu", i_pid
);
2996 switch (i_print_type
) {
2998 fprintf(print_fh
, "<ERROR type=\"invalid_eit_section\" pid=\"%hu\"/>\n",
3002 fprintf(print_fh
, "error type: invalid_eit_section pid: %hu\n",
3012 if ( i_table_id
!= EIT_TABLE_ID_PF_ACTUAL
)
3015 /* We do not use psi_table_* primitives as the spec allows for holes in
3016 * section numbering, and there is no sure way to know whether you have
3017 * gathered all sections. */
3018 uint8_t i_section
= psi_get_section(p_eit
);
3019 if (p_sid
->pp_eit_sections
[i_section
] != NULL
&&
3020 psi_compare(p_sid
->pp_eit_sections
[i_section
], p_eit
)) {
3021 /* Identical section. Shortcut. */
3022 free(p_sid
->pp_eit_sections
[i_section
]);
3023 p_sid
->pp_eit_sections
[i_section
] = p_eit
;
3027 free(p_sid
->pp_eit_sections
[i_section
]);
3028 p_sid
->pp_eit_sections
[i_section
] = p_eit
;
3030 eit_print( p_eit
, msg_Dbg
, NULL
, demux_Iconv
, NULL
, PRINT_TEXT
);
3031 if ( b_print_enabled
)
3033 eit_print( p_eit
, demux_Print
, NULL
,
3034 demux_Iconv
, NULL
, i_print_type
);
3035 if ( i_print_type
== PRINT_XML
)
3036 fprintf(print_fh
, "\n");
3040 SendEIT( p_sid
, i_dts
, p_eit
);
3041 if ( i_table_id
!= EIT_TABLE_ID_PF_ACTUAL
)
3045 /*****************************************************************************
3047 *****************************************************************************/
3048 static void HandleSection( uint16_t i_pid
, uint8_t *p_section
, mtime_t i_dts
)
3050 uint8_t i_table_id
= psi_get_tableid( p_section
);
3052 if ( !psi_validate( p_section
) )
3054 msg_Warn( NULL
, "invalid section on PID %hu", i_pid
);
3055 switch (i_print_type
) {
3057 fprintf(print_fh
, "<ERROR type=\"invalid_section\" pid=\"%hu\"/>\n", i_pid
);
3060 fprintf(print_fh
, "error type: invalid_section pid: %hu\n", i_pid
);
3069 if ( !psi_get_current( p_section
) )
3071 /* Ignore sections which are not in use yet. */
3076 switch ( i_table_id
)
3079 HandlePATSection( i_pid
, p_section
, i_dts
);
3084 HandleCATSection( i_pid
, p_section
, i_dts
);
3088 HandlePMT( i_pid
, p_section
, i_dts
);
3091 case NIT_TABLE_ID_ACTUAL
:
3092 HandleNITSection( i_pid
, p_section
, i_dts
);
3095 case SDT_TABLE_ID_ACTUAL
:
3096 HandleSDTSection( i_pid
, p_section
, i_dts
);
3100 if ( i_table_id
== EIT_TABLE_ID_PF_ACTUAL
||
3101 (i_table_id
>= EIT_TABLE_ID_SCHED_ACTUAL_FIRST
&&
3102 i_table_id
<= EIT_TABLE_ID_SCHED_ACTUAL_LAST
) )
3104 HandleEIT( i_pid
, p_section
, i_dts
);
3112 /*****************************************************************************
3114 *****************************************************************************/
3115 static void HandlePSIPacket( uint8_t *p_ts
, mtime_t i_dts
)
3117 uint16_t i_pid
= ts_get_pid( p_ts
);
3118 ts_pid_t
*p_pid
= &p_pids
[i_pid
];
3119 uint8_t i_cc
= ts_get_cc( p_ts
);
3120 const uint8_t *p_payload
;
3123 if ( ts_check_duplicate( i_cc
, p_pid
->i_last_cc
)
3124 || !ts_has_payload( p_ts
) )
3127 if ( p_pid
->i_last_cc
!= -1
3128 && ts_check_discontinuity( i_cc
, p_pid
->i_last_cc
) )
3129 psi_assemble_reset( &p_pid
->p_psi_buffer
, &p_pid
->i_psi_buffer_used
);
3131 p_payload
= ts_section( p_ts
);
3132 i_length
= p_ts
+ TS_SIZE
- p_payload
;
3134 if ( !psi_assemble_empty( &p_pid
->p_psi_buffer
,
3135 &p_pid
->i_psi_buffer_used
) )
3137 uint8_t *p_section
= psi_assemble_payload( &p_pid
->p_psi_buffer
,
3138 &p_pid
->i_psi_buffer_used
,
3139 &p_payload
, &i_length
);
3140 if ( p_section
!= NULL
)
3141 HandleSection( i_pid
, p_section
, i_dts
);
3144 p_payload
= ts_next_section( p_ts
);
3145 i_length
= p_ts
+ TS_SIZE
- p_payload
;
3149 uint8_t *p_section
= psi_assemble_payload( &p_pid
->p_psi_buffer
,
3150 &p_pid
->i_psi_buffer_used
,
3151 &p_payload
, &i_length
);
3152 if ( p_section
!= NULL
)
3153 HandleSection( i_pid
, p_section
, i_dts
);
3157 /*****************************************************************************
3158 * PID info functions
3159 *****************************************************************************/
3160 static const char *h222_stream_type_desc(uint8_t i_stream_type
) {
3161 /* See ISO/IEC 13818-1 : 2000 (E) | Table 2-29 - Stream type assignments, Page 66 (48) */
3162 if (i_stream_type
== 0)
3163 return "Reserved stream";
3164 switch (i_stream_type
) {
3165 case 0x01: return "11172-2 video (MPEG-1)";
3166 case 0x02: return "H.262/13818-2 video (MPEG-2) or 11172-2 constrained video";
3167 case 0x03: return "11172-3 audio (MPEG-1)";
3168 case 0x04: return "13818-3 audio (MPEG-2)";
3169 case 0x05: return "H.222.0/13818-1 private sections";
3170 case 0x06: return "H.222.0/13818-1 PES private data";
3171 case 0x07: return "13522 MHEG";
3172 case 0x08: return "H.222.0/13818-1 Annex A - DSM CC";
3173 case 0x09: return "H.222.1";
3174 case 0x0A: return "13818-6 type A";
3175 case 0x0B: return "13818-6 type B";
3176 case 0x0C: return "13818-6 type C";
3177 case 0x0D: return "13818-6 type D";
3178 case 0x0E: return "H.222.0/13818-1 auxiliary";
3179 case 0x0F: return "13818-7 Audio with ADTS transport syntax";
3180 case 0x10: return "14496-2 Visual (MPEG-4 part 2 video)";
3181 case 0x11: return "14496-3 Audio with LATM transport syntax (14496-3/AMD 1)";
3182 case 0x12: return "14496-1 SL-packetized or FlexMux stream in PES packets";
3183 case 0x13: return "14496-1 SL-packetized or FlexMux stream in 14496 sections";
3184 case 0x14: return "ISO/IEC 13818-6 Synchronized Download Protocol";
3185 case 0x15: return "Metadata in PES packets";
3186 case 0x16: return "Metadata in metadata_sections";
3187 case 0x17: return "Metadata in 13818-6 Data Carousel";
3188 case 0x18: return "Metadata in 13818-6 Object Carousel";
3189 case 0x19: return "Metadata in 13818-6 Synchronized Download Protocol";
3190 case 0x1A: return "13818-11 MPEG-2 IPMP stream";
3191 case 0x1B: return "H.264/14496-10 video (MPEG-4/AVC)";
3192 case 0x24: return "H.265/23008-2 video (HEVC)";
3193 case 0x42: return "AVS Video";
3194 case 0x7F: return "IPMP stream";
3195 default : return "Unknown stream";
3199 static const char *get_pid_desc(uint16_t i_pid
, uint16_t *i_sid
) {
3201 uint8_t i_last_section
;
3203 uint16_t i_nit_pid
= NIT_PID
, i_pcr_pid
= 0;
3208 case 0x00: return "PAT";
3209 case 0x01: return "CAT";
3210 case 0x11: return "SDT";
3211 case 0x12: return "EPG";
3212 case 0x14: return "TDT/TOT";
3215 /* Detect NIT pid */
3216 if ( psi_table_validate( pp_current_pat_sections
) )
3218 i_last_section
= psi_table_get_lastsection( pp_current_pat_sections
);
3219 for ( i
= 0; i
<= i_last_section
; i
++ )
3221 uint8_t *p_section
= psi_table_get_section( pp_current_pat_sections
, i
);
3225 while ( (p_program
= pat_get_program( p_section
, j
++ )) != NULL
)
3227 /* Programs with PID == 0 are actually NIT */
3228 if ( patn_get_program( p_program
) == 0 )
3230 i_nit_pid
= patn_get_pid( p_program
);
3237 /* Detect EMM pids */
3238 if ( b_enable_emm
&& psi_table_validate( pp_current_cat_sections
) )
3240 i_last_section
= psi_table_get_lastsection( pp_current_cat_sections
);
3241 for ( i
= 0; i
<= i_last_section
; i
++ )
3243 uint8_t *p_section
= psi_table_get_section( pp_current_cat_sections
, i
);
3246 while ( (p_desc
= descl_get_desc( cat_get_descl(p_section
), cat_get_desclength(p_section
), j
++ )) != NULL
)
3248 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
3251 if ( desc09_get_pid( p_desc
) == i_pid
) {
3258 /* Detect streams in PMT */
3259 for ( k
= 0; k
< i_nb_sids
; k
++ )
3261 sid_t
*p_sid
= pp_sids
[k
];
3262 if ( p_sid
->i_pmt_pid
== i_pid
)
3265 *i_sid
= p_sid
->i_sid
;
3269 if ( p_sid
->i_sid
&& p_sid
->p_current_pmt
!= NULL
)
3271 uint8_t *p_current_pmt
= p_sid
->p_current_pmt
;
3272 uint8_t *p_current_es
;
3274 /* The PCR PID can be alone or PCR can be carried in some other PIDs (mostly video)
3275 so just remember the pid and if it is alone it will be reported as PCR, otherwise
3276 stream type of the PID will be reported */
3277 if ( i_pid
== pmt_get_pcrpid( p_current_pmt
) ) {
3279 *i_sid
= p_sid
->i_sid
;
3280 i_pcr_pid
= pmt_get_pcrpid( p_current_pmt
);
3285 while ((p_desc
= descs_get_desc( pmt_get_descs( p_current_pmt
), j
++ )) != NULL
)
3287 if ( desc_get_tag( p_desc
) != 0x09 || !desc09_validate( p_desc
) )
3290 if ( desc09_get_pid( p_desc
) == i_pid
) {
3292 *i_sid
= p_sid
->i_sid
;
3297 /* Detect stream types */
3299 while ( (p_current_es
= pmt_get_es( p_current_pmt
, j
++ )) != NULL
)
3301 if ( pmtn_get_pid( p_current_es
) == i_pid
)
3304 *i_sid
= p_sid
->i_sid
;
3305 return h222_stream_type_desc( pmtn_get_streamtype( p_current_es
) );
3311 /* Are there any other PIDs? */
3312 if (i_pid
== i_nit_pid
)
3315 if (i_pid
== i_pcr_pid
)
3321 /*****************************************************************************
3322 * Functions that return packed sections
3323 *****************************************************************************/
3324 uint8_t *demux_get_current_packed_PAT( unsigned int *pi_pack_size
) {
3325 return psi_pack_sections( pp_current_pat_sections
, pi_pack_size
);
3328 uint8_t *demux_get_current_packed_CAT( unsigned int *pi_pack_size
) {
3329 return psi_pack_sections( pp_current_cat_sections
, pi_pack_size
);
3332 uint8_t *demux_get_current_packed_NIT( unsigned int *pi_pack_size
) {
3333 return psi_pack_sections( pp_current_nit_sections
, pi_pack_size
);
3336 uint8_t *demux_get_current_packed_SDT( unsigned int *pi_pack_size
) {
3337 return psi_pack_sections( pp_current_sdt_sections
, pi_pack_size
);
3340 uint8_t *demux_get_packed_PMT( uint16_t i_sid
, unsigned int *pi_pack_size
) {
3341 sid_t
*p_sid
= FindSID( i_sid
);
3342 if ( p_sid
!= NULL
&& p_sid
->p_current_pmt
&& pmt_validate( p_sid
->p_current_pmt
) )
3343 return psi_pack_section( p_sid
->p_current_pmt
, pi_pack_size
);
3347 inline void demux_get_PID_info( uint16_t i_pid
, uint8_t *p_data
) {
3348 ts_pid_info_t
*p_info
= (ts_pid_info_t
*)p_data
;
3349 *p_info
= p_pids
[i_pid
].info
;
3352 inline void demux_get_PIDS_info( uint8_t *p_data
) {
3354 for (i_pid
= 0; i_pid
< MAX_PIDS
; i_pid
++ )
3355 demux_get_PID_info( i_pid
, p_data
+ ( i_pid
* sizeof(ts_pid_info_t
) ) );