fix EIT schedule signaling in SDT
[dvblast.git] / demux.c
blobaa3dabc9ca5707236f43940abe7ccb0214c7fa3c
1 /*****************************************************************************
2 * demux.c
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 *****************************************************************************/
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <stdbool.h>
29 #include <stddef.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <inttypes.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <ev.h>
38 #include "dvblast.h"
39 #include "en50221.h"
40 #include "mrtg-cnt.h"
42 #ifdef HAVE_ICONV
43 #include <iconv.h>
44 #endif
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 /*****************************************************************************
54 * Local declarations
55 *****************************************************************************/
56 #define MIN_SECTION_FRAGMENT PSI_HEADER_SIZE_SYNTAX1
58 typedef struct ts_pid_t
60 int i_refcount;
61 int i_psi_refcount;
62 bool b_pes;
63 int8_t i_last_cc;
64 int i_demux_fd;
65 /* b_emm is set to true when PID carries EMM packet
66 and should be outputed in all services */
67 bool b_emm;
69 /* PID info and stats */
70 mtime_t i_bytes_ts;
71 unsigned long i_packets_passed;
72 ts_pid_info_t info;
74 /* biTStream PSI section gathering */
75 uint8_t *p_psi_buffer;
76 uint16_t i_psi_buffer_used;
78 output_t **pp_outputs;
79 int i_nb_outputs;
81 int i_pes_status; /* pes + unscrambled */
82 struct ev_timer timeout_watcher;
83 } ts_pid_t;
85 typedef struct sid_t
87 uint16_t i_sid, i_pmt_pid;
88 uint8_t *p_current_pmt;
89 PSI_TABLE_DECLARE(pp_eit_sections);
90 } sid_t;
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;
117 #ifdef HAVE_ICONV
118 static iconv_t iconv_handle = (iconv_t)-1;
119 #endif
121 /*****************************************************************************
122 * Local prototypes
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 )
162 return i_pid;
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 */
174 if ( b_do_remap )
175 i_newpid = pi_newpids[I_APID];
176 else
177 i_newpid = p_output->config.pi_confpids[I_APID];
178 break;
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 */
185 if ( b_do_remap )
186 i_newpid = pi_newpids[I_VPID];
187 else
188 i_newpid = p_output->config.pi_confpids[I_VPID];
189 break;
190 case 0x06: { /* PES Private Data - We must check the descriptors */
191 /* By default, nothing identified */
192 uint8_t SubStreamType = N_MAP_PIDS;
193 uint16_t j = 0;
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 );
200 j++;
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;
208 /* Audio found */
209 if (SubStreamType==I_APID) {
210 msg_Dbg(NULL, "REMAP: PES Private Data stream identified as [Audio]");
211 if ( b_do_remap )
212 i_newpid = pi_newpids[I_APID];
213 else
214 i_newpid = p_output->config.pi_confpids[I_APID];
216 /* Subtitle found */
217 if (SubStreamType==I_SPUPID) {
218 msg_Dbg(NULL, "REMAP: PES Private Data stream identified as [Subtitle]");
219 if ( b_do_remap )
220 i_newpid = pi_newpids[I_SPUPID];
221 else
222 i_newpid = p_output->config.pi_confpids[I_SPUPID];
224 break;
228 if (!i_newpid)
229 return i_pid;
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)
234 i_newpid++;
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);
240 return i_newpid;
243 /*****************************************************************************
244 * FindSID
245 *****************************************************************************/
246 static inline sid_t *FindSID( uint16_t i_sid )
248 int i;
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 )
254 return p_sid;
256 return NULL;
259 /*****************************************************************************
260 * Print info
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)
267 case PRINT_XML:
268 fprintf(print_fh,
269 "<STATUS type=\"bitrate\" status=\"%d\" value=\"%"PRIu64"\" />\n",
270 i_bitrate ? 1 : 0, i_bitrate);
271 break;
272 case PRINT_TEXT:
273 fprintf(print_fh, "bitrate: %"PRIu64"\n", i_bitrate);
274 break;
275 default:
276 break;
278 i_nb_packets = 0;
280 if ( i_nb_invalids )
282 switch (i_print_type)
284 case PRINT_XML:
285 fprintf(print_fh,
286 "<ERROR type=\"invalid_ts\" number=\"%"PRIu64"\" />\n",
287 i_nb_invalids);
288 break;
289 case PRINT_TEXT:
290 fprintf(print_fh, "invalids: %"PRIu64"\n", i_nb_invalids);
291 break;
292 default:
293 break;
295 i_nb_invalids = 0;
298 if ( i_nb_discontinuities )
300 switch (i_print_type)
302 case PRINT_XML:
303 fprintf(print_fh,
304 "<ERROR type=\"invalid_discontinuity\" number=\"%"PRIu64"\" />\n",
305 i_nb_discontinuities);
306 break;
307 case PRINT_TEXT:
308 fprintf(print_fh, "discontinuities: %"PRIu64"\n",
309 i_nb_discontinuities);
310 break;
311 default:
312 break;
314 i_nb_discontinuities = 0;
317 if ( i_nb_errors )
319 switch (i_print_type)
321 case PRINT_XML:
322 fprintf(print_fh,
323 "<ERROR type=\"transport_error\" number=\"%"PRIu64"\" />\n",
324 i_nb_errors);
325 break;
326 case PRINT_TEXT:
327 fprintf(print_fh, "errors: %"PRIu64"\n", i_nb_errors);
328 break;
329 default:
330 break;
332 i_nb_errors = 0;
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)
343 case PRINT_XML:
344 fprintf(print_fh,
345 "<STATUS type=\"pid\" pid=\"%"PRIu16"\" status=\"0\" />\n",
346 i_pid);
347 break;
348 case PRINT_TEXT:
349 fprintf(print_fh, "pid: %"PRIu16" down\n", i_pid);
350 break;
351 default:
352 break;
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)
365 case PRINT_XML:
366 fprintf(print_fh,
367 "<STATUS type=\"pid\" pid=\"%"PRIu16"\" status=\"1\" pes=\"%d\" />\n",
368 i_pid, p_pid->i_pes_status == 1 ? 1 : 0);
369 break;
370 case PRINT_TEXT:
371 fprintf(print_fh, "pid: %"PRIu16" up%s\n",
372 i_pid, p_pid->i_pes_status == 1 ? " pes" : "");
373 break;
374 default:
375 break;
379 /*****************************************************************************
380 * demux_Open
381 *****************************************************************************/
382 void demux_Open( void )
384 int i;
386 memset( p_pids, 0, sizeof(p_pids) );
388 pf_Open();
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;
399 if ( b_budget_mode )
400 i_demux_fd = pf_SetFilter(8192);
402 psi_table_init( pp_current_pat_sections );
403 psi_table_init( pp_next_pat_sections );
404 SetPID(PAT_PID);
405 p_pids[PAT_PID].i_psi_refcount++;
407 if ( b_enable_emm )
409 psi_table_init( pp_current_cat_sections );
410 psi_table_init( pp_next_cat_sections );
411 SetPID_EMM(CAT_PID);
412 p_pids[CAT_PID].i_psi_refcount++;
415 SetPID(NIT_PID);
416 p_pids[NIT_PID].i_psi_refcount++;
418 psi_table_init( pp_current_sdt_sections );
419 psi_table_init( pp_next_sdt_sections );
420 SetPID(SDT_PID);
421 p_pids[SDT_PID].i_psi_refcount++;
423 SetPID(EIT_PID);
424 p_pids[EIT_PID].i_psi_refcount++;
426 SetPID(RST_PID);
428 SetPID(TDT_PID);
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 /*****************************************************************************
439 * demux_Close
440 *****************************************************************************/
441 void demux_Close( void )
443 int i;
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 );
466 free( p_sid );
468 free( pp_sids );
470 #ifdef HAVE_ICONV
471 if (iconv_handle != (iconv_t)-1) {
472 iconv_close(iconv_handle);
473 iconv_handle = (iconv_t)-1;
475 #endif
477 if ( i_print_period )
478 ev_timer_stop( event_loop, &print_watcher );
481 /*****************************************************************************
482 * demux_Run
483 *****************************************************************************/
484 void demux_Run( block_t *p_ts )
486 i_wallclock = mdate();
487 mrtgAnalyse( p_ts );
488 SetDTS( p_ts );
490 while ( p_ts != NULL )
492 block_t *p_next = p_ts->p_next;
493 p_ts->p_next = NULL;
494 demux_Handle( p_ts );
495 p_ts = p_next;
499 /*****************************************************************************
500 * demux_Handle
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 );
507 int i;
509 i_nb_packets++;
511 if ( !ts_validate( p_ts->p_ts ) )
513 msg_Warn( NULL, "lost TS sync" );
514 block_Delete( p_ts );
515 i_nb_invalids++;
516 return;
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;
542 uint16_t i_sid = 0;
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 ) )
554 uint16_t i_sid = 0;
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 );
562 i_nb_errors++;
563 i_tuner_errors++;
564 i_last_error = i_wallclock;
566 else if ( i_wallclock > i_last_error + WATCHDOG_WAIT )
567 i_tuner_errors = 0;
569 if ( i_tuner_errors > MAX_ERRORS )
571 i_tuner_errors = 0;
572 msg_Warn( NULL,
573 "too many transport errors, tuning again" );
574 switch (i_print_type) {
575 case PRINT_XML:
576 fprintf(print_fh, "<EVENT type=\"reset\" cause=\"transport\" />\n");
577 break;
578 case PRINT_TEXT:
579 fprintf(print_fh, "reset cause: transport\n");
580 break;
581 default:
582 break;
584 pf_Reset();
587 if ( i_es_timeout )
589 int i_pes_status = -1;
590 if ( ts_get_scrambling( p_ts->p_ts ) )
591 i_pes_status = 0;
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;
604 PrintES( i_pid );
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 );
613 else
615 ev_timer_init( &p_pid->timeout_watcher, PrintESCb, 30, 30 );
616 ev_timer_start( event_loop, &p_pid->timeout_watcher );
619 else
621 if ( p_pid->i_pes_status != i_pes_status )
623 p_pid->i_pes_status = i_pes_status;
624 PrintES( i_pid );
627 ev_timer_again( event_loop, &p_pid->timeout_watcher );
632 if ( !ts_get_transporterror( p_ts->p_ts ) )
634 /* PSI parsing */
635 if ( i_pid == TDT_PID || i_pid == RST_PID )
636 SendTDT( p_ts );
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 )
641 SendEMM( p_ts );
644 p_pid->i_last_cc = i_cc;
646 /* Output */
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 ) )
655 uint8_t *p_payload;
657 if ( ts_get_scrambling( p_ts->p_ts ) ||
658 ( p_pid->b_pes
659 && (p_payload = ts_payload( p_ts->p_ts )) + 3
660 < p_ts->p_ts + TS_SIZE
661 && !pes_validate(p_payload) ) )
663 if ( i_wallclock >
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 )
675 int j;
676 for ( j = 0; j < i_nb_outputs; j++ )
677 pp_outputs[j]->i_nb_errors = 0;
679 msg_Warn( NULL,
680 "too many errors for stream %s, resetting",
681 p_output->config.psz_displayname );
683 switch (i_print_type) {
684 case PRINT_XML:
685 fprintf(print_fh, "<EVENT type=\"reset\" cause=\"scrambling\" />\n");
686 break;
687 case PRINT_TEXT:
688 fprintf(print_fh, "reset cause: scrambling");
689 break;
690 default:
691 break;
693 i_last_reset = i_wallclock;
694 en50221_Reset();
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 )
717 continue;
719 output_Put( p_output, p_ts );
722 if ( output_dup.config.i_config & OUTPUT_VALID )
723 output_Put( &output_dup, p_ts );
725 p_ts->i_refcount--;
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 )
735 int i;
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)
757 & OUTPUT_DVB);
758 bool b_epg_change = !!((p_output->config.i_config ^ p_config->i_config)
759 & OUTPUT_EPG);
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];
773 int i;
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 )
803 p_output->i_tsid =
804 psi_table_get_tableidext(pp_current_pat_sections);
805 else
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) )) )
815 goto out_change;
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] );
844 b_pid_change = true;
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] );
862 b_pid_change = true;
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;
875 if ( p_sid != NULL )
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;
902 out_change:
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 )
918 NewSDT( p_output );
919 NewNIT( p_output );
920 NewPAT( p_output );
921 NewPMT( p_output );
923 else
925 if ( b_tsid_change )
927 NewSDT( p_output );
928 NewNIT( p_output );
929 NewPAT( p_output );
931 else if ( b_dvb_change )
933 NewNIT( p_output );
934 NewPAT( p_output );
936 else if ( b_network_change )
937 NewNIT( p_output );
939 if ( !b_tsid_change && (b_service_name_change || b_epg_change) )
940 NewSDT( p_output );
942 if ( b_pid_change )
943 NewPMT( p_output );
947 /*****************************************************************************
948 * SetDTS
949 *****************************************************************************/
950 static void SetDTS( block_t *p_list )
952 int i_nb_ts = 0, i;
953 mtime_t i_duration;
954 block_t *p_ts = p_list;
956 while ( p_ts != NULL )
958 i_nb_ts++;
959 p_ts = p_ts->p_next;
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 )
965 i_duration = 0;
966 else
967 i_duration = i_wallclock - i_last_dts;
969 p_ts = p_list;
970 i = i_nb_ts - 1;
971 while ( p_ts != NULL )
973 p_ts->i_dts = i_wallclock - i_duration * i / i_nb_ts;
974 i--;
975 p_ts = p_ts->p_next;
978 i_last_dts = i_wallclock;
981 /*****************************************************************************
982 * SetPID/UnsetPID
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 )
995 SetPID( 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 /*****************************************************************************
1013 * StartPID/StopPID
1014 *****************************************************************************/
1015 static void StartPID( output_t *p_output, uint16_t i_pid )
1017 int j;
1019 for ( j = 0; j < p_pids[i_pid].i_nb_outputs; j++ )
1020 if ( p_pids[i_pid].pp_outputs[j] == p_output )
1021 break;
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 )
1027 break;
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,
1033 sizeof(output_t *)
1034 * p_pids[i_pid].i_nb_outputs );
1037 p_pids[i_pid].pp_outputs[j] = p_output;
1038 SetPID( i_pid );
1042 static void StopPID( output_t *p_output, uint16_t i_pid )
1044 int j;
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 )
1051 break;
1055 if ( j != p_pids[i_pid].i_nb_outputs )
1057 p_pids[i_pid].pp_outputs[j] = NULL;
1058 UnsetPID( i_pid );
1062 /*****************************************************************************
1063 * SelectPID/UnselectPID
1064 *****************************************************************************/
1065 static void SelectPID( uint16_t i_sid, uint16_t i_pid, bool b_pcr )
1067 int i;
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 ) )
1078 if ( b_pcr )
1079 pp_outputs[i]->i_pcr_pid = i_pid;
1080 else
1081 continue;
1083 StartPID( pp_outputs[i], i_pid );
1088 static void UnselectPID( uint16_t i_sid, uint16_t i_pid )
1090 int i;
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 )
1104 int i;
1106 p_pids[i_pid].i_psi_refcount++;
1107 p_pids[i_pid].b_pes = false;
1109 if ( b_select_pmts )
1110 SetPID( i_pid );
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 )
1114 SetPID( i_pid );
1117 static void UnselectPMT( uint16_t i_sid, uint16_t i_pid )
1119 int i;
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 )
1127 UnsetPID( i_pid );
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 )
1131 UnsetPID( i_pid );
1134 /*****************************************************************************
1135 * GetPIDS
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 )
1141 sid_t *p_sid;
1142 uint8_t *p_pmt;
1143 uint16_t i_pmt_pid, i_pcr_pid;
1144 uint8_t *p_es;
1145 uint8_t j;
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 );
1155 if ( i_sid == 0 )
1156 return;
1158 else
1160 *pi_nb_wanted_pids = 0;
1161 *ppi_wanted_pids = NULL;
1164 p_sid = FindSID( i_sid );
1165 if ( p_sid == NULL )
1166 return;
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);
1172 return;
1175 i_pcr_pid = pmt_get_pcrpid( p_pmt );
1176 j = 0;
1177 while ( (p_es = pmt_get_es( p_pmt, j )) != NULL )
1179 j++;
1181 uint16_t i_pid = pmtn_get_pid( p_es );
1182 bool b_select;
1183 if ( i_nb_pids )
1184 b_select = IsIn( pi_pids, i_nb_pids, i_pid );
1185 else
1187 b_select = PIDWouldBeSelected( p_es );
1188 if ( b_select )
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 )
1198 uint8_t k = 0;
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 ) )
1203 continue;
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 );
1212 if ( b_enable_ecm )
1214 j = 0;
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 ) )
1220 continue;
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 /*****************************************************************************
1240 * OutputPSISection
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;
1252 block_t *p_block;
1253 uint8_t *p;
1254 uint8_t i_ts_offset;
1255 bool b_append = (pp_ts_buffer != NULL && *pp_ts_buffer != NULL);
1257 if ( b_append )
1259 p_block = *pp_ts_buffer;
1260 i_ts_offset = *pi_ts_buffer_offset;
1262 else
1264 p_block = block_New();
1265 p_block->i_dts = i_dts;
1266 i_ts_offset = 0;
1268 p = p_block->p_ts;
1270 psi_split_section( p, &i_ts_offset, p_section, &i_section_offset );
1272 if ( !b_append )
1274 ts_set_pid( p, i_pid );
1275 ts_set_cc( p, *pi_cc );
1276 (*pi_cc)++;
1277 *pi_cc &= 0xf;
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;
1287 break;
1289 else
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 /*****************************************************************************
1306 * SendPAT
1307 *****************************************************************************/
1308 static void SendPAT( mtime_t i_dts )
1310 int i;
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 )
1318 continue;
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. */
1324 uint8_t *p;
1325 p_output->i_pat_version++;
1327 p = p_output->p_pat_section = psi_allocate();
1328 pat_init( p );
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 /*****************************************************************************
1346 * SendPMT
1347 *****************************************************************************/
1348 static void SendPMT( sid_t *p_sid, mtime_t i_dts )
1350 int i;
1351 int i_pmt_pid = p_sid->i_pmt_pid;
1353 if ( b_do_remap )
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,
1369 NULL, NULL );
1374 /*****************************************************************************
1375 * SendNIT
1376 *****************************************************************************/
1377 static void SendNIT( mtime_t i_dts )
1379 int i;
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 /*****************************************************************************
1395 * SendSDT
1396 *****************************************************************************/
1397 static void SendSDT( mtime_t i_dts )
1399 int i;
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 /*****************************************************************************
1415 * SendEIT
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);
1423 int i;
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 );
1439 else
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 /*****************************************************************************
1458 * FlushEIT
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 /*****************************************************************************
1473 * SendTDT
1474 *****************************************************************************/
1475 static void SendTDT( block_t *p_ts )
1477 int i;
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 /*****************************************************************************
1491 * SendEMM
1492 *****************************************************************************/
1493 static void SendEMM( block_t *p_ts )
1495 int i;
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 /*****************************************************************************
1508 * NewPAT
1509 *****************************************************************************/
1510 static void NewPAT( output_t *p_output )
1512 const uint8_t *p_program;
1513 uint8_t *p;
1514 uint8_t k = 0;
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();
1528 pat_init( p );
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 )
1538 /* NIT */
1539 p = pat_get_program( p_output->p_pat_section, k++ );
1540 patn_init( p );
1541 patn_set_program( p, 0 );
1542 patn_set_pid( p, NIT_PID );
1545 p = pat_get_program( p_output->p_pat_section, k++ );
1546 patn_init( p );
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 );
1553 else
1554 patn_set_program( p, p_output->config.i_sid );
1556 if ( b_do_remap )
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]);
1563 } else {
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 /*****************************************************************************
1574 * NewPMT
1575 *****************************************************************************/
1576 static void CopyDescriptors( uint8_t *p_descs, uint8_t *p_current_descs )
1578 uint8_t *p_desc;
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 );
1588 j++;
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 */
1593 k++;
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 );
1602 else
1603 descs_set_length( p_descs, p_desc - p_descs - DESCS_HEADER_SIZE );
1606 static void NewPMT( output_t *p_output )
1608 sid_t *p_sid;
1609 uint8_t *p_current_pmt;
1610 uint8_t *p_es, *p_current_es;
1611 uint8_t *p;
1612 uint16_t j, k;
1613 uint16_t i_pcrpid;
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();
1628 pmt_init( p );
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 );
1636 else
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 ) );
1646 j = 0; k = 0;
1647 while ( (p_current_es = pmt_get_es( p_current_pmt, j )) != NULL )
1649 uint16_t i_pid = pmtn_get_pid( p_current_es );
1651 j++;
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,
1654 i_pid ) )
1655 continue;
1657 p_es = pmt_get_es( p, k );
1658 if ( p_es == NULL ) continue; /* This shouldn't happen */
1659 k++;
1660 pmtn_init( p_es );
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];
1675 } else {
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 );
1680 if ( p_es == NULL )
1681 /* This shouldn't happen if the incoming PMT is valid */
1682 pmt_set_length( p, 0 );
1683 else
1684 pmt_set_length( p, p_es - p - PMT_HEADER_SIZE );
1685 psi_set_crc( p );
1688 /*****************************************************************************
1689 * NewNIT
1690 *****************************************************************************/
1691 static void NewNIT( output_t *p_output )
1693 uint8_t *p_ts;
1694 uint8_t *p_header2;
1695 uint8_t *p;
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 )
1712 uint8_t *p_descs;
1713 uint8_t *p_desc;
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 );
1723 else
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 );
1731 nitn_init( p_ts );
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 );
1735 else
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 );
1740 if ( p_ts == NULL )
1741 /* This shouldn't happen */
1742 nit_set_length( p, 0 );
1743 else
1744 nit_set_length( p, p_ts - p - NIT_HEADER_SIZE );
1745 psi_set_crc( p_output->p_nit_section );
1748 /*****************************************************************************
1749 * NewSDT
1750 *****************************************************************************/
1751 static void NewSDT( output_t *p_output )
1753 uint8_t *p_service, *p_current_service;
1754 uint8_t *p;
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++;
1776 return;
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 );
1789 else
1790 sdt_set_onid( p,
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 );
1801 else
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) );
1822 } else {
1823 int j = 0, i_total_desc_len = 0;
1824 uint8_t *p_desc;
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;
1844 } else {
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;
1855 } else {
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;
1864 } else {
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 );
1879 else
1880 sdt_set_length( p, p_service - p - SDT_HEADER_SIZE );
1881 psi_set_crc( p_output->p_sdt_section );
1884 /*****************************************************************************
1885 * UpdatePAT/PMT/SDT
1886 *****************************************************************************/
1887 #define DECLARE_UPDATE_FUNC( table ) \
1888 static void Update##table( uint16_t i_sid ) \
1890 int i; \
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 /*****************************************************************************
1903 * UpdateTSID
1904 *****************************************************************************/
1905 static void UpdateTSID(void)
1907 uint16_t i_tsid = psi_table_get_tableidext(pp_current_pat_sections);
1908 int i;
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;
1918 NewNIT( p_output );
1923 /*****************************************************************************
1924 * SIDIsSelected
1925 *****************************************************************************/
1926 static bool SIDIsSelected( uint16_t i_sid )
1928 int i;
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 )
1933 return true;
1935 return false;
1938 /*****************************************************************************
1939 * demux_PIDIsSelected
1940 *****************************************************************************/
1941 bool demux_PIDIsSelected( uint16_t i_pid )
1943 int i;
1945 for ( i = 0; i < p_pids[i_pid].i_nb_outputs; i++ )
1946 if ( p_pids[i_pid].pp_outputs[i] != NULL )
1947 return true;
1949 return false;
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 );
1961 switch ( i_type )
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 */
1974 return true;
1975 break;
1977 case 0x6:
1979 uint16_t j = 0;
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 );
1985 j++;
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 */ )
1994 return true;
1996 break;
1999 default:
2000 break;
2003 /* FIXME: also parse IOD */
2004 return false;
2007 /*****************************************************************************
2008 * PIDCarriesPES
2009 *****************************************************************************/
2010 static bool PIDCarriesPES( const uint8_t *p_es )
2012 uint8_t i_type = pmtn_get_streamtype( p_es );
2014 switch ( i_type )
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 */
2028 return true;
2029 break;
2031 default:
2032 return false;
2033 break;
2037 /*****************************************************************************
2038 * PMTNeedsDescrambling
2039 *****************************************************************************/
2040 static bool PMTNeedsDescrambling( uint8_t *p_pmt )
2042 uint8_t i;
2043 uint16_t j;
2044 uint8_t *p_es;
2045 const uint8_t *p_desc;
2047 j = 0;
2048 while ( (p_desc = descs_get_desc( pmt_get_descs( p_pmt ), j )) != NULL )
2050 uint8_t i_tag = desc_get_tag( p_desc );
2051 j++;
2053 if ( i_tag == 0x9 ) return true;
2056 i = 0;
2057 while ( (p_es = pmt_get_es( p_pmt, i )) != NULL )
2059 i++;
2060 j = 0;
2061 while ( (p_desc = descs_get_desc( pmtn_get_descs( p_es ), j )) != NULL )
2063 uint8_t i_tag = desc_get_tag( p_desc );
2064 j++;
2066 if ( i_tag == 0x9 ) return true;
2070 return false;
2073 /*****************************************************************************
2074 * demux_ResendCAPMTs
2075 *****************************************************************************/
2076 void demux_ResendCAPMTs( void )
2078 int i;
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,
2088 uint16_t i_ca_pid )
2090 int j = 0;
2091 uint8_t *p_desc;
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 ) )
2095 continue;
2096 if ( desc09_get_pid( p_desc ) == i_ca_pid )
2097 return p_desc;
2100 return NULL;
2103 /*****************************************************************************
2104 * DeleteProgram
2105 *****************************************************************************/
2106 static void DeleteProgram( uint16_t i_sid, uint16_t i_pid )
2108 sid_t *p_sid;
2109 uint8_t *p_pmt;
2110 uint8_t *p_desc;
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 );
2122 uint8_t *p_es;
2123 uint8_t j;
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 );
2133 if ( b_enable_ecm )
2135 j = 0;
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 ) )
2141 continue;
2142 UnselectPID( i_sid, desc09_get_pid( p_desc ) );
2146 j = 0;
2147 while ( (p_es = pmt_get_es( p_pmt, j )) != NULL )
2149 uint16_t i_pid = pmtn_get_pid( p_es );
2150 j++;
2152 if ( PIDWouldBeSelected( p_es ) )
2153 UnselectPID( i_sid, i_pid );
2155 if ( b_enable_ecm )
2157 uint8_t k = 0;
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 ) )
2162 continue;
2163 UnselectPID( i_sid, desc09_get_pid( p_desc ) );
2168 free( p_pmt );
2169 p_sid->p_current_pmt = NULL;
2171 p_sid->i_sid = 0;
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 /*****************************************************************************
2178 * demux_Iconv
2179 *****************************************************************************
2180 * This code is from biTStream's examples and is under the WTFPL (see
2181 * LICENSE.WTFPL).
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';
2188 return psz_string;
2191 char *demux_Iconv(void *_unused, const char *psz_encoding,
2192 char *p_string, size_t i_length)
2194 #ifdef HAVE_ICONV
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);
2224 free(psz_string);
2225 return iconv_append_null(p_string, i_length);
2227 if (i_length)
2228 msg_Warn(NULL, "partial conversion from %s to %s", psz_encoding,
2229 psz_native_charset);
2231 *p = '\0';
2232 return psz_string;
2233 #else
2234 return iconv_append_null(p_string, i_length);
2235 #endif
2238 /*****************************************************************************
2239 * demux_Print
2240 *****************************************************************************
2241 * This code is from biTStream's examples and is under the WTFPL (see
2242 * LICENSE.WTFPL).
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];
2248 va_list args;
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);
2254 va_end(args);
2257 /*****************************************************************************
2258 * HandlePAT
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 );
2265 uint8_t i;
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 );
2273 goto out_pat;
2276 if ( !pat_table_validate( pp_next_pat_sections ) )
2278 msg_Warn( NULL, "invalid PAT received" );
2279 switch (i_print_type) {
2280 case PRINT_XML:
2281 fprintf(print_fh, "<ERROR type=\"invalid_pat\"/>\n");
2282 break;
2283 case PRINT_TEXT:
2284 fprintf(print_fh, "error type: invalid_pat\n");
2285 break;
2286 default:
2287 break;
2289 psi_table_free( pp_next_pat_sections );
2290 psi_table_init( pp_next_pat_sections );
2291 goto out_pat;
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 ) )
2303 b_change = true;
2304 UpdateTSID();
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;
2313 int j = 0;
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 );
2320 j++;
2322 if ( i_sid == 0 )
2324 if ( i_pid != NIT_PID )
2325 msg_Warn( NULL,
2326 "NIT is carried on PID %hu which isn't DVB compliant",
2327 i_pid );
2328 continue; /* NIT */
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
2335 || b_change )
2337 sid_t *p_sid;
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);
2350 i_nb_sids++;
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;
2358 UpdatePAT( i_sid );
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;
2371 int j = 0;
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 );
2377 j++;
2379 if ( i_sid == 0 )
2380 continue; /* NIT */
2382 if ( pat_table_find_program( pp_current_pat_sections, i_sid )
2383 == NULL )
2385 DeleteProgram( i_sid, i_pid );
2386 UpdatePAT( i_sid );
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,
2398 i_print_type );
2399 if ( i_print_type == PRINT_XML )
2400 fprintf(print_fh, "\n");
2403 out_pat:
2404 SendPAT( i_dts );
2407 /*****************************************************************************
2408 * HandlePATSection
2409 *****************************************************************************/
2410 static void HandlePATSection( uint16_t i_pid, uint8_t *p_section,
2411 mtime_t i_dts )
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) {
2417 case PRINT_XML:
2418 fprintf(print_fh, "<ERROR type=\"invalid_pat_section\"/>\n");
2419 break;
2420 case PRINT_TEXT:
2421 fprintf(print_fh, "error type: invalid_pat_section\n");
2422 break;
2423 default:
2424 break;
2426 free( p_section );
2427 return;
2430 if ( !psi_table_section( pp_next_pat_sections, p_section ) )
2431 return;
2433 HandlePAT( i_dts );
2436 /*****************************************************************************
2437 * HandleCAT
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;
2444 uint8_t i, r;
2445 uint8_t *p_desc;
2446 int j, k;
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 );
2454 goto out_cat;
2457 if ( !cat_table_validate( pp_next_cat_sections ) )
2459 msg_Warn( NULL, "invalid CAT received" );
2460 switch (i_print_type) {
2461 case PRINT_XML:
2462 fprintf(print_fh, "<ERROR type=\"invalid_cat\"/>\n");
2463 break;
2464 case PRINT_TEXT:
2465 fprintf(print_fh, "error type: invalid_cat\n");
2466 break;
2467 default:
2468 break;
2470 psi_table_free( pp_next_cat_sections );
2471 psi_table_init( pp_next_cat_sections );
2472 goto out_cat;
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 );
2484 j = 0;
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 ) )
2488 continue;
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 );
2500 j = 0;
2501 while ( (p_desc = descl_get_desc( cat_get_descl(p_old_section), cat_get_desclength(p_old_section), j++ )) != NULL )
2503 uint16_t emm_pid;
2504 int pid_found = 0;
2506 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
2507 continue;
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 );
2517 k = 0;
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 ) )
2521 continue;
2522 if ( ca_desc_find( cat_get_descl(p_section), cat_get_desclength(p_section), emm_pid ) != NULL )
2524 pid_found = 1;
2525 break;
2530 if ( !pid_found )
2531 UnsetPID(emm_pid);
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,
2542 i_print_type );
2543 if ( i_print_type == PRINT_XML )
2544 fprintf(print_fh, "\n");
2547 out_cat:
2548 return;
2551 /*****************************************************************************
2552 * HandleCATSection
2553 *****************************************************************************/
2554 static void HandleCATSection( uint16_t i_pid, uint8_t *p_section,
2555 mtime_t i_dts )
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) {
2561 case PRINT_XML:
2562 fprintf(print_fh, "<ERROR type=\"invalid_cat_section\"/>\n");
2563 break;
2564 case PRINT_TEXT:
2565 fprintf(print_fh, "error type: invalid_cat_section\n");
2566 break;
2567 default:
2568 break;
2570 free( p_section );
2571 return;
2574 if ( !psi_table_section( pp_next_cat_sections, p_section ) )
2575 return;
2577 HandleCAT( i_dts );
2580 static void mark_pmt_pids( uint8_t *p_pmt, uint8_t pid_map[], uint8_t marker )
2582 uint16_t j, k;
2583 uint8_t *p_es;
2584 uint8_t *p_desc;
2586 uint16_t i_pcr_pid = pmt_get_pcrpid( p_pmt );
2588 if ( b_enable_ecm )
2590 j = 0;
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 ) )
2594 continue;
2595 pid_map[ desc09_get_pid( p_desc ) ] |= marker;
2599 if ( i_pcr_pid != PADDING_PID )
2600 pid_map[ i_pcr_pid ] |= marker;
2602 j = 0;
2603 while ( (p_es = pmt_get_es( p_pmt, j )) != NULL )
2605 uint16_t i_pid = pmtn_get_pid( p_es );
2606 j++;
2608 if ( PIDWouldBeSelected( p_es ) )
2609 pid_map[ i_pid ] |= marker;
2611 p_pids[i_pid].b_pes = PIDCarriesPES( p_es );
2613 if ( b_enable_ecm )
2615 k = 0;
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 ) )
2619 continue;
2620 pid_map[ desc09_get_pid( p_desc ) ] |= marker;
2626 /*****************************************************************************
2627 * HandlePMT
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 );
2632 sid_t *p_sid;
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
2640 * programs). */
2641 free( p_pmt );
2642 return;
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) {
2649 case PRINT_XML:
2650 fprintf(print_fh, "<ERROR type=\"ghost_pmt\" program=\"%hu\n pid=\"%hu\"/>\n",
2651 i_sid, i_pid);
2652 break;
2653 case PRINT_TEXT:
2654 fprintf(print_fh, "error type: ghost_pmt program: %hu pid: %hu\n",
2655 i_sid, i_pid);
2656 break;
2657 default:
2658 break;
2660 free( p_pmt );
2661 return;
2664 if ( p_sid->p_current_pmt != NULL &&
2665 psi_compare( p_sid->p_current_pmt, p_pmt ) )
2667 /* Identical PMT. Shortcut. */
2668 free( p_pmt );
2669 goto out_pmt;
2672 if ( !pmt_validate( p_pmt ) )
2674 msg_Warn( NULL, "invalid PMT section received on PID %hu", i_pid );
2675 switch (i_print_type) {
2676 case PRINT_XML:
2677 fprintf(print_fh, "<ERROR type=\"invalid_pmt_section\" pid=\"%hu\"/>\n",
2678 i_pid);
2679 break;
2680 case PRINT_TEXT:
2681 fprintf(print_fh, "error type: invalid_pmt_section pid: %hu\n",
2682 i_pid);
2683 break;
2684 default:
2685 break;
2687 free( p_pmt );
2688 goto out_pmt;
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 ) :
2696 false;
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 );
2712 int i;
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 */
2719 int pid;
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 ] )
2724 continue;
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. */
2728 continue;
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 );
2731 break;
2732 case 0x01: /* The pid exists in new PMT. Select it. */
2733 SelectPID( i_sid, pid, pid == i_pcr_pid );
2734 break;
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 );
2748 UpdatePMT( i_sid );
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,
2754 i_print_type );
2755 if ( i_print_type == PRINT_XML )
2756 fprintf(print_fh, "\n");
2759 out_pmt:
2760 SendPMT( p_sid, i_dts );
2763 /*****************************************************************************
2764 * HandleNIT
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 );
2774 goto out_nit;
2777 if ( !nit_table_validate( pp_next_nit_sections ) )
2779 msg_Warn( NULL, "invalid NIT received" );
2780 switch (i_print_type) {
2781 case PRINT_XML:
2782 fprintf(print_fh, "<ERROR type=\"invalid_nit\"/>\n");
2783 break;
2784 case PRINT_TEXT:
2785 fprintf(print_fh, "error type: invalid_nit\n");
2786 break;
2787 default:
2788 break;
2790 psi_table_free( pp_next_nit_sections );
2791 psi_table_init( pp_next_nit_sections );
2792 goto out_nit;
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");
2810 out_nit:
2814 /*****************************************************************************
2815 * HandleNITSection
2816 *****************************************************************************/
2817 static void HandleNITSection( uint16_t i_pid, uint8_t *p_section,
2818 mtime_t i_dts )
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) {
2824 case PRINT_XML:
2825 fprintf(print_fh, "<ERROR type=\"invalid_nit_section\" pid=\"%hu\"/>\n",
2826 i_pid);
2827 break;
2828 case PRINT_TEXT:
2829 fprintf(print_fh, "error type: invalid_nit_section pid: %hu\n",
2830 i_pid);
2831 break;
2832 default:
2833 break;
2835 free( p_section );
2836 return;
2839 if ( psi_table_section( pp_next_nit_sections, p_section ) )
2840 HandleNIT( i_dts );
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). */
2845 SendNIT( i_dts );
2849 /*****************************************************************************
2850 * HandleSDT
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 );
2856 uint8_t i;
2857 int j;
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 );
2865 goto out_sdt;
2868 if ( !sdt_table_validate( pp_next_sdt_sections ) )
2870 msg_Warn( NULL, "invalid SDT received" );
2871 switch (i_print_type) {
2872 case PRINT_XML:
2873 fprintf(print_fh, "<ERROR type=\"invalid_sdt\"/>\n");
2874 break;
2875 case PRINT_TEXT:
2876 fprintf(print_fh, "error type: invalid_sdt\n");
2877 break;
2878 default:
2879 break;
2881 psi_table_free( pp_next_sdt_sections );
2882 psi_table_init( pp_next_sdt_sections );
2883 goto out_sdt;
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 );
2895 uint8_t *p_service;
2896 j = 0;
2898 while ( (p_service = sdt_get_service( p_section, j )) != NULL )
2900 uint16_t i_sid = sdtn_get_sid( p_service );
2901 j++;
2903 UpdateSDT( i_sid );
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;
2915 int j = 0;
2917 while ( (p_service = sdt_get_service( p_section, j )) != NULL )
2919 uint16_t i_sid = sdtn_get_sid( p_service );
2920 j++;
2922 if ( sdt_table_find_service( pp_current_sdt_sections, i_sid )
2923 == NULL )
2924 UpdateSDT( 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");
2941 out_sdt:
2942 SendSDT( i_dts );
2945 /*****************************************************************************
2946 * HandleSDTSection
2947 *****************************************************************************/
2948 static void HandleSDTSection( uint16_t i_pid, uint8_t *p_section,
2949 mtime_t i_dts )
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) {
2955 case PRINT_XML:
2956 fprintf(print_fh, "<ERROR type=\"invalid_sdt_section\" pid=\"%hu\"/>\n",
2957 i_pid);
2958 break;
2959 case PRINT_TEXT:
2960 fprintf(print_fh, "error type: invalid_sdt_section pid: %hu\n",
2961 i_pid);
2962 break;
2963 default:
2964 break;
2966 free( p_section );
2967 return;
2970 if ( !psi_table_section( pp_next_sdt_sections, p_section ) )
2971 return;
2973 HandleSDT( i_dts );
2976 /*****************************************************************************
2977 * HandleEITSection
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 );
2983 sid_t *p_sid;
2985 p_sid = FindSID( i_sid );
2986 if ( p_sid == NULL )
2988 /* Not a selected program. */
2989 free( p_eit );
2990 return;
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) {
2997 case PRINT_XML:
2998 fprintf(print_fh, "<ERROR type=\"invalid_eit_section\" pid=\"%hu\"/>\n",
2999 i_pid);
3000 break;
3001 case PRINT_TEXT:
3002 fprintf(print_fh, "error type: invalid_eit_section pid: %hu\n",
3003 i_pid);
3004 break;
3005 default:
3006 break;
3008 free( p_eit );
3009 return;
3012 if ( i_table_id != EIT_TABLE_ID_PF_ACTUAL )
3013 goto out_eit;
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;
3024 goto out_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");
3039 out_eit:
3040 SendEIT( p_sid, i_dts, p_eit );
3041 if ( i_table_id != EIT_TABLE_ID_PF_ACTUAL )
3042 free( p_eit );
3045 /*****************************************************************************
3046 * HandleSection
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) {
3056 case PRINT_XML:
3057 fprintf(print_fh, "<ERROR type=\"invalid_section\" pid=\"%hu\"/>\n", i_pid);
3058 break;
3059 case PRINT_TEXT:
3060 fprintf(print_fh, "error type: invalid_section pid: %hu\n", i_pid);
3061 break;
3062 default:
3063 break;
3065 free( p_section );
3066 return;
3069 if ( !psi_get_current( p_section ) )
3071 /* Ignore sections which are not in use yet. */
3072 free( p_section );
3073 return;
3076 switch ( i_table_id )
3078 case PAT_TABLE_ID:
3079 HandlePATSection( i_pid, p_section, i_dts );
3080 break;
3082 case CAT_TABLE_ID:
3083 if ( b_enable_emm )
3084 HandleCATSection( i_pid, p_section, i_dts );
3085 break;
3087 case PMT_TABLE_ID:
3088 HandlePMT( i_pid, p_section, i_dts );
3089 break;
3091 case NIT_TABLE_ID_ACTUAL:
3092 HandleNITSection( i_pid, p_section, i_dts );
3093 break;
3095 case SDT_TABLE_ID_ACTUAL:
3096 HandleSDTSection( i_pid, p_section, i_dts );
3097 break;
3099 default:
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 );
3105 break;
3107 free( p_section );
3108 break;
3112 /*****************************************************************************
3113 * HandlePSIPacket
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;
3121 uint8_t i_length;
3123 if ( ts_check_duplicate( i_cc, p_pid->i_last_cc )
3124 || !ts_has_payload( p_ts ) )
3125 return;
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;
3147 while ( i_length )
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) {
3200 int i, j, k;
3201 uint8_t i_last_section;
3202 uint8_t *p_desc;
3203 uint16_t i_nit_pid = NIT_PID, i_pcr_pid = 0;
3205 /* Simple cases */
3206 switch (i_pid)
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 );
3222 uint8_t *p_program;
3224 j = 0;
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 );
3231 break;
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 );
3245 j = 0;
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 ) )
3249 continue;
3251 if ( desc09_get_pid( p_desc ) == i_pid ) {
3252 return "EMM";
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 )
3264 if ( i_sid )
3265 *i_sid = p_sid->i_sid;
3266 return "PMT";
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 ) ) {
3278 if ( i_sid )
3279 *i_sid = p_sid->i_sid;
3280 i_pcr_pid = pmt_get_pcrpid( p_current_pmt );
3283 /* Look for ECMs */
3284 j = 0;
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 ) )
3288 continue;
3290 if ( desc09_get_pid( p_desc ) == i_pid ) {
3291 if ( i_sid )
3292 *i_sid = p_sid->i_sid;
3293 return "ECM";
3297 /* Detect stream types */
3298 j = 0;
3299 while ( (p_current_es = pmt_get_es( p_current_pmt, j++ )) != NULL )
3301 if ( pmtn_get_pid( p_current_es ) == i_pid )
3303 if ( i_sid )
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)
3313 return "NIT";
3315 if (i_pid == i_pcr_pid)
3316 return "PCR";
3318 return "...";
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 );
3344 return NULL;
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 ) {
3353 int i_pid;
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) ) );