consider HEVC like video
[dvblast.git] / demux.c
blob4c6353969305e6abf2300ed5874cbec1f8160c98
1 /*****************************************************************************
2 * demux.c
3 *****************************************************************************
4 * Copyright (C) 2004, 2008-2011, 2015 VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
7 * Andy Gatward <a.j.gatward@reading.ac.uk>
8 * Marian Ďurkovič <md@bts.sk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
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 } sid_t;
91 mtime_t i_wallclock = 0;
93 static ts_pid_t p_pids[MAX_PIDS];
94 static sid_t **pp_sids = NULL;
95 static int i_nb_sids = 0;
97 static PSI_TABLE_DECLARE(pp_current_pat_sections);
98 static PSI_TABLE_DECLARE(pp_next_pat_sections);
99 static PSI_TABLE_DECLARE(pp_current_cat_sections);
100 static PSI_TABLE_DECLARE(pp_next_cat_sections);
101 static PSI_TABLE_DECLARE(pp_current_nit_sections);
102 static PSI_TABLE_DECLARE(pp_next_nit_sections);
103 static PSI_TABLE_DECLARE(pp_current_sdt_sections);
104 static PSI_TABLE_DECLARE(pp_next_sdt_sections);
105 static mtime_t i_last_dts = -1;
106 static int i_demux_fd;
107 static uint64_t i_nb_packets = 0;
108 static uint64_t i_nb_invalids = 0;
109 static uint64_t i_nb_discontinuities = 0;
110 static uint64_t i_nb_errors = 0;
111 static int i_tuner_errors = 0;
112 static mtime_t i_last_error = 0;
113 static mtime_t i_last_reset = 0;
114 static struct ev_timer print_watcher;
116 #ifdef HAVE_ICONV
117 static iconv_t iconv_handle = (iconv_t)-1;
118 #endif
120 /*****************************************************************************
121 * Local prototypes
122 *****************************************************************************/
123 static void demux_Handle( block_t *p_ts );
124 static void SetDTS( block_t *p_list );
125 static void SetPID( uint16_t i_pid );
126 static void SetPID_EMM( uint16_t i_pid );
127 static void UnsetPID( uint16_t i_pid );
128 static void StartPID( output_t *p_output, uint16_t i_pid );
129 static void StopPID( output_t *p_output, uint16_t i_pid );
130 static void SelectPID( uint16_t i_sid, uint16_t i_pid );
131 static void UnselectPID( uint16_t i_sid, uint16_t i_pid );
132 static void SelectPMT( uint16_t i_sid, uint16_t i_pid );
133 static void UnselectPMT( uint16_t i_sid, uint16_t i_pid );
134 static void GetPIDS( uint16_t **ppi_wanted_pids, int *pi_nb_wanted_pids,
135 uint16_t i_sid,
136 const uint16_t *pi_pids, int i_nb_pids );
137 static bool SIDIsSelected( uint16_t i_sid );
138 static bool PIDWouldBeSelected( uint8_t *p_es );
139 static bool PMTNeedsDescrambling( uint8_t *p_pmt );
140 static void FlushEIT( output_t *p_output, mtime_t i_dts );
141 static void SendTDT( block_t *p_ts );
142 static void SendEMM( block_t *p_ts );
143 static void NewPAT( output_t *p_output );
144 static void NewPMT( output_t *p_output );
145 static void NewNIT( output_t *p_output );
146 static void NewSDT( output_t *p_output );
147 static void HandlePSIPacket( uint8_t *p_ts, mtime_t i_dts );
148 static const char *get_pid_desc(uint16_t i_pid, uint16_t *i_sid);
151 * Remap an ES pid to a fixed value.
152 * Multiple streams of the same type use sequential pids
153 * Returns the new pid and updates the map tables
155 static uint16_t map_es_pid(output_t * p_output, uint8_t *p_es, uint16_t i_pid)
157 uint16_t i_newpid = i_pid;
158 uint16_t i_stream_type = pmtn_get_streamtype(p_es);
160 if ( !b_do_remap && !p_output->config.b_do_remap )
161 return i_pid;
163 msg_Dbg(NULL, "REMAP: Found elementary stream type 0x%02x with original PID 0x%x (%u):", i_stream_type, i_pid, i_pid);
165 switch ( i_stream_type )
167 case 0x03: /* audio MPEG-1 */
168 case 0x04: /* audio MPEG-2 */
169 case 0x0f: /* audio AAC ADTS */
170 case 0x11: /* audio AAC LATM */
171 case 0x81: /* ATSC AC-3 */
172 case 0x87: /* ATSC Enhanced AC-3 */
173 if ( b_do_remap )
174 i_newpid = pi_newpids[I_APID];
175 else
176 i_newpid = p_output->config.pi_confpids[I_APID];
177 break;
178 case 0x01: /* video MPEG-1 */
179 case 0x02: /* video MPEG-2 */
180 case 0x10: /* video MPEG-4 */
181 case 0x1b: /* video H264 */
182 case 0x24: /* video H265 */
183 case 0x42: /* video AVS */
184 if ( b_do_remap )
185 i_newpid = pi_newpids[I_VPID];
186 else
187 i_newpid = p_output->config.pi_confpids[I_VPID];
188 break;
189 case 0x06: { /* PES Private Data - We must check the descriptors */
190 /* By default, nothing identified */
191 uint8_t SubStreamType = N_MAP_PIDS;
192 uint16_t j = 0;
193 const uint8_t *p_desc;
194 /* Loop over the descriptors */
195 while ( (p_desc = descs_get_desc( pmtn_get_descs( p_es ), j )) != NULL )
197 /* Get the descriptor tag */
198 uint8_t i_tag = desc_get_tag( p_desc );
199 j++;
200 /* Check if the tag is: A/52, Enhanced A/52, DTS, AAC */
201 if (i_tag == 0x6a || i_tag == 0x7a || i_tag == 0x7b || i_tag == 0x7c)
202 SubStreamType=I_APID;
203 /* Check if the tag is: VBI + teletext, teletext, dvbsub */
204 if (i_tag == 0x46 || i_tag == 0x56 || i_tag == 0x59)
205 SubStreamType=I_SPUPID;
207 /* Audio found */
208 if (SubStreamType==I_APID) {
209 msg_Dbg(NULL, "REMAP: PES Private Data stream identified as [Audio]");
210 if ( b_do_remap )
211 i_newpid = pi_newpids[I_APID];
212 else
213 i_newpid = p_output->config.pi_confpids[I_APID];
215 /* Subtitle found */
216 if (SubStreamType==I_SPUPID) {
217 msg_Dbg(NULL, "REMAP: PES Private Data stream identified as [Subtitle]");
218 if ( b_do_remap )
219 i_newpid = pi_newpids[I_SPUPID];
220 else
221 i_newpid = p_output->config.pi_confpids[I_SPUPID];
223 break;
227 if (!i_newpid)
228 return i_pid;
230 /* Got the new base for the mapped pid. Find the next free one
231 we do this to ensure that multiple audios get unique pids */
232 while (p_output->pi_freepids[i_newpid] != UNUSED_PID)
233 i_newpid++;
234 p_output->pi_freepids[i_newpid] = i_pid; /* Mark as in use */
235 p_output->pi_newpids[i_pid] = i_newpid; /* Save the new pid */
237 msg_Dbg(NULL, "REMAP: => Elementary stream is remapped to PID 0x%x (%u)", i_newpid, i_newpid);
239 return i_newpid;
242 /*****************************************************************************
243 * FindSID
244 *****************************************************************************/
245 static inline sid_t *FindSID( uint16_t i_sid )
247 int i;
249 for ( i = 0; i < i_nb_sids; i++ )
251 sid_t *p_sid = pp_sids[i];
252 if ( p_sid->i_sid == i_sid )
253 return p_sid;
255 return NULL;
258 /*****************************************************************************
259 * Print info
260 *****************************************************************************/
261 static void PrintCb( struct ev_loop *loop, struct ev_timer *w, int revents )
263 uint64_t i_bitrate = i_nb_packets * TS_SIZE * 8 * 1000000 / i_print_period;
264 switch (i_print_type)
266 case PRINT_XML:
267 fprintf(print_fh,
268 "<STATUS type=\"bitrate\" status=\"%d\" value=\"%"PRIu64"\" />\n",
269 i_bitrate ? 1 : 0, i_bitrate);
270 break;
271 case PRINT_TEXT:
272 fprintf(print_fh, "bitrate: %"PRIu64"\n", i_bitrate);
273 break;
274 default:
275 break;
277 i_nb_packets = 0;
279 if ( i_nb_invalids )
281 switch (i_print_type)
283 case PRINT_XML:
284 fprintf(print_fh,
285 "<ERROR type=\"invalid_ts\" number=\"%"PRIu64"\" />\n",
286 i_nb_invalids);
287 break;
288 case PRINT_TEXT:
289 fprintf(print_fh, "invalids: %"PRIu64"\n", i_nb_invalids);
290 break;
291 default:
292 break;
294 i_nb_invalids = 0;
297 if ( i_nb_discontinuities )
299 switch (i_print_type)
301 case PRINT_XML:
302 fprintf(print_fh,
303 "<ERROR type=\"invalid_discontinuity\" number=\"%"PRIu64"\" />\n",
304 i_nb_discontinuities);
305 break;
306 case PRINT_TEXT:
307 fprintf(print_fh, "discontinuities: %"PRIu64"\n",
308 i_nb_discontinuities);
309 break;
310 default:
311 break;
313 i_nb_discontinuities = 0;
316 if ( i_nb_errors )
318 switch (i_print_type)
320 case PRINT_XML:
321 fprintf(print_fh,
322 "<ERROR type=\"transport_error\" number=\"%"PRIu64"\" />\n",
323 i_nb_errors);
324 break;
325 case PRINT_TEXT:
326 fprintf(print_fh, "errors: %"PRIu64"\n", i_nb_errors);
327 break;
328 default:
329 break;
331 i_nb_errors = 0;
335 static void PrintESCb( struct ev_loop *loop, struct ev_timer *w, int revents )
337 ts_pid_t *p_pid = container_of( w, ts_pid_t, timeout_watcher );
338 uint16_t i_pid = p_pid - p_pids;
340 switch (i_print_type)
342 case PRINT_XML:
343 fprintf(print_fh,
344 "<STATUS type=\"pid\" pid=\"%"PRIu16"\" status=\"0\" />\n",
345 i_pid);
346 break;
347 case PRINT_TEXT:
348 fprintf(print_fh, "pid: %"PRIu16" down\n", i_pid);
349 break;
350 default:
351 break;
354 ev_timer_stop( loop, w );
355 p_pid->i_pes_status = -1;
358 static void PrintES( uint16_t i_pid )
360 const ts_pid_t *p_pid = &p_pids[i_pid];
362 switch (i_print_type)
364 case PRINT_XML:
365 fprintf(print_fh,
366 "<STATUS type=\"pid\" pid=\"%"PRIu16"\" status=\"1\" pes=\"%d\" />\n",
367 i_pid, p_pid->i_pes_status == 1 ? 1 : 0);
368 break;
369 case PRINT_TEXT:
370 fprintf(print_fh, "pid: %"PRIu16" up%s\n",
371 i_pid, p_pid->i_pes_status == 1 ? " pes" : "");
372 break;
373 default:
374 break;
378 /*****************************************************************************
379 * demux_Open
380 *****************************************************************************/
381 void demux_Open( void )
383 int i;
385 memset( p_pids, 0, sizeof(p_pids) );
387 pf_Open();
389 for ( i = 0; i < MAX_PIDS; i++ )
391 p_pids[i].i_last_cc = -1;
392 p_pids[i].i_demux_fd = -1;
393 psi_assemble_init( &p_pids[i].p_psi_buffer,
394 &p_pids[i].i_psi_buffer_used );
395 p_pids[i].i_pes_status = -1;
398 if ( b_budget_mode )
399 i_demux_fd = pf_SetFilter(8192);
401 psi_table_init( pp_current_pat_sections );
402 psi_table_init( pp_next_pat_sections );
403 SetPID(PAT_PID);
404 p_pids[PAT_PID].i_psi_refcount++;
406 if ( b_enable_emm )
408 psi_table_init( pp_current_cat_sections );
409 psi_table_init( pp_next_cat_sections );
410 SetPID_EMM(CAT_PID);
411 p_pids[CAT_PID].i_psi_refcount++;
414 SetPID(NIT_PID);
415 p_pids[NIT_PID].i_psi_refcount++;
417 psi_table_init( pp_current_sdt_sections );
418 psi_table_init( pp_next_sdt_sections );
419 SetPID(SDT_PID);
420 p_pids[SDT_PID].i_psi_refcount++;
422 SetPID(EIT_PID);
423 p_pids[EIT_PID].i_psi_refcount++;
425 SetPID(RST_PID);
427 SetPID(TDT_PID);
429 if ( i_print_period )
431 ev_timer_init( &print_watcher, PrintCb,
432 i_print_period / 1000000., i_print_period / 1000000. );
433 ev_timer_start( event_loop, &print_watcher );
437 /*****************************************************************************
438 * demux_Close
439 *****************************************************************************/
440 void demux_Close( void )
442 int i;
444 psi_table_free( pp_current_pat_sections );
445 psi_table_free( pp_next_pat_sections );
446 psi_table_free( pp_current_cat_sections );
447 psi_table_free( pp_next_cat_sections );
448 psi_table_free( pp_current_nit_sections );
449 psi_table_free( pp_next_nit_sections );
450 psi_table_free( pp_current_sdt_sections );
451 psi_table_free( pp_next_sdt_sections );
453 for ( i = 0; i < MAX_PIDS; i++ )
455 ev_timer_stop( event_loop, &p_pids[i].timeout_watcher );
456 free( p_pids[i].p_psi_buffer );
457 free( p_pids[i].pp_outputs );
460 for ( i = 0; i < i_nb_sids; i++ )
462 sid_t *p_sid = pp_sids[i];
463 free( p_sid->p_current_pmt );
464 free( p_sid );
466 free( pp_sids );
468 #ifdef HAVE_ICONV
469 if (iconv_handle != (iconv_t)-1) {
470 iconv_close(iconv_handle);
471 iconv_handle = (iconv_t)-1;
473 #endif
475 if ( i_print_period )
476 ev_timer_stop( event_loop, &print_watcher );
479 /*****************************************************************************
480 * demux_Run
481 *****************************************************************************/
482 void demux_Run( block_t *p_ts )
484 i_wallclock = mdate();
485 mrtgAnalyse( p_ts );
486 SetDTS( p_ts );
488 while ( p_ts != NULL )
490 block_t *p_next = p_ts->p_next;
491 p_ts->p_next = NULL;
492 demux_Handle( p_ts );
493 p_ts = p_next;
497 /*****************************************************************************
498 * demux_Handle
499 *****************************************************************************/
500 static void demux_Handle( block_t *p_ts )
502 uint16_t i_pid = ts_get_pid( p_ts->p_ts );
503 ts_pid_t *p_pid = &p_pids[i_pid];
504 uint8_t i_cc = ts_get_cc( p_ts->p_ts );
505 int i;
507 i_nb_packets++;
509 if ( !ts_validate( p_ts->p_ts ) )
511 msg_Warn( NULL, "lost TS sync" );
512 block_Delete( p_ts );
513 i_nb_invalids++;
514 return;
517 if ( i_pid != PADDING_PID )
518 p_pid->info.i_scrambling = ts_get_scrambling( p_ts->p_ts );
520 p_pid->info.i_last_packet_ts = i_wallclock;
521 p_pid->info.i_packets++;
523 p_pid->i_packets_passed++;
525 /* Calculate bytes_per_sec */
526 if ( i_wallclock > p_pid->i_bytes_ts + 1000000 ) {
527 p_pid->info.i_bytes_per_sec = p_pid->i_packets_passed * TS_SIZE;
528 p_pid->i_packets_passed = 0;
529 p_pid->i_bytes_ts = i_wallclock;
532 if ( p_pid->info.i_first_packet_ts == 0 )
533 p_pid->info.i_first_packet_ts = i_wallclock;
535 if ( i_pid != PADDING_PID && p_pid->i_last_cc != -1
536 && !ts_check_duplicate( i_cc, p_pid->i_last_cc )
537 && ts_check_discontinuity( i_cc, p_pid->i_last_cc ) )
539 unsigned int expected_cc = (p_pid->i_last_cc + 1) & 0x0f;
540 uint16_t i_sid = 0;
541 const char *pid_desc = get_pid_desc(i_pid, &i_sid);
543 p_pid->info.i_cc_errors++;
544 i_nb_discontinuities++;
546 msg_Warn( NULL, "TS discontinuity on pid %4hu expected_cc %2u got %2u (%s, sid %d)",
547 i_pid, expected_cc, i_cc, pid_desc, i_sid );
550 if ( ts_get_transporterror( p_ts->p_ts ) )
552 uint16_t i_sid = 0;
553 const char *pid_desc = get_pid_desc(i_pid, &i_sid);
555 p_pid->info.i_transport_errors++;
557 msg_Warn( NULL, "transport_error_indicator on pid %hu (%s, sid %u)",
558 i_pid, pid_desc, i_sid );
560 i_nb_errors++;
561 i_tuner_errors++;
562 i_last_error = i_wallclock;
564 else if ( i_wallclock > i_last_error + WATCHDOG_WAIT )
565 i_tuner_errors = 0;
567 if ( i_tuner_errors > MAX_ERRORS )
569 i_tuner_errors = 0;
570 msg_Warn( NULL,
571 "too many transport errors, tuning again" );
572 switch (i_print_type) {
573 case PRINT_XML:
574 fprintf(print_fh, "<EVENT type=\"reset\" cause=\"transport\" />\n");
575 break;
576 case PRINT_TEXT:
577 fprintf(print_fh, "reset cause: transport\n");
578 break;
579 default:
580 break;
582 pf_Reset();
585 if ( i_es_timeout )
587 int i_pes_status = -1;
588 if ( ts_get_scrambling( p_ts->p_ts ) )
589 i_pes_status = 0;
590 else if ( ts_get_unitstart( p_ts->p_ts ) )
592 uint8_t *p_payload = ts_payload( p_ts->p_ts );
593 if ( p_payload + 3 < p_ts->p_ts + TS_SIZE )
594 i_pes_status = pes_validate( p_payload ) ? 1 : 0;
597 if ( i_pes_status != -1 )
599 if ( p_pid->i_pes_status == -1 )
601 p_pid->i_pes_status = i_pes_status;
602 PrintES( i_pid );
604 if ( i_pid != TDT_PID )
606 ev_timer_init( &p_pid->timeout_watcher, PrintESCb,
607 i_es_timeout / 1000000.,
608 i_es_timeout / 1000000. );
609 ev_timer_start( event_loop, &p_pid->timeout_watcher );
611 else
613 ev_timer_init( &p_pid->timeout_watcher, PrintESCb, 30, 30 );
614 ev_timer_start( event_loop, &p_pid->timeout_watcher );
617 else
619 if ( p_pid->i_pes_status != i_pes_status )
621 p_pid->i_pes_status = i_pes_status;
622 PrintES( i_pid );
625 ev_timer_again( event_loop, &p_pid->timeout_watcher );
630 if ( !ts_get_transporterror( p_ts->p_ts ) )
632 /* PSI parsing */
633 if ( i_pid == TDT_PID || i_pid == RST_PID )
634 SendTDT( p_ts );
635 else if ( p_pid->i_psi_refcount )
636 HandlePSIPacket( p_ts->p_ts, p_ts->i_dts );
638 if ( b_enable_emm && p_pid->b_emm )
639 SendEMM( p_ts );
642 p_pid->i_last_cc = i_cc;
644 /* Output */
645 for ( i = 0; i < p_pid->i_nb_outputs; i++ )
647 output_t *p_output = p_pid->pp_outputs[i];
648 if ( p_output != NULL )
650 if ( i_ca_handle && (p_output->config.i_config & OUTPUT_WATCH) &&
651 ts_get_unitstart( p_ts->p_ts ) )
653 uint8_t *p_payload;
655 if ( ts_get_scrambling( p_ts->p_ts ) ||
656 ( p_pid->b_pes
657 && (p_payload = ts_payload( p_ts->p_ts )) + 3
658 < p_ts->p_ts + TS_SIZE
659 && !pes_validate(p_payload) ) )
661 if ( i_wallclock >
662 i_last_reset + WATCHDOG_REFRACTORY_PERIOD )
664 p_output->i_nb_errors++;
665 p_output->i_last_error = i_wallclock;
668 else if ( i_wallclock > p_output->i_last_error + WATCHDOG_WAIT )
669 p_output->i_nb_errors = 0;
671 if ( p_output->i_nb_errors > MAX_ERRORS )
673 int j;
674 for ( j = 0; j < i_nb_outputs; j++ )
675 pp_outputs[j]->i_nb_errors = 0;
677 msg_Warn( NULL,
678 "too many errors for stream %s, resetting",
679 p_output->config.psz_displayname );
681 switch (i_print_type) {
682 case PRINT_XML:
683 fprintf(print_fh, "<EVENT type=\"reset\" cause=\"scrambling\" />\n");
684 break;
685 case PRINT_TEXT:
686 fprintf(print_fh, "reset cause: scrambling");
687 break;
688 default:
689 break;
691 i_last_reset = i_wallclock;
692 en50221_Reset();
696 output_Put( p_output, p_ts );
698 if ( p_output->p_eit_ts_buffer != NULL
699 && p_ts->i_dts > p_output->p_eit_ts_buffer->i_dts
700 + MAX_EIT_RETENTION )
701 FlushEIT( p_output, p_ts->i_dts );
705 for ( i = 0; i < i_nb_outputs; i++ )
707 output_t *p_output = pp_outputs[i];
709 if ( !(p_output->config.i_config & OUTPUT_VALID) ||
710 !p_output->config.b_passthrough )
711 continue;
713 output_Put( p_output, p_ts );
716 if ( output_dup.config.i_config & OUTPUT_VALID )
717 output_Put( &output_dup, p_ts );
719 p_ts->i_refcount--;
720 if ( !p_ts->i_refcount )
721 block_Delete( p_ts );
724 /*****************************************************************************
725 * demux_Change : called from main thread
726 *****************************************************************************/
727 static int IsIn( uint16_t *pi_pids, int i_nb_pids, uint16_t i_pid )
729 int i;
730 for ( i = 0; i < i_nb_pids; i++ )
731 if ( i_pid == pi_pids[i] ) break;
732 return ( i != i_nb_pids );
735 void demux_Change( output_t *p_output, const output_config_t *p_config )
737 uint16_t *pi_wanted_pids, *pi_current_pids;
738 int i_nb_wanted_pids, i_nb_current_pids;
740 uint16_t i_old_sid = p_output->config.i_sid;
741 uint16_t i_sid = p_config->i_sid;
742 uint16_t *pi_old_pids = p_output->config.pi_pids;
743 uint16_t *pi_pids = p_config->pi_pids;
744 int i_old_nb_pids = p_output->config.i_nb_pids;
745 int i_nb_pids = p_config->i_nb_pids;
747 bool b_sid_change = i_sid != i_old_sid;
748 bool b_pid_change = false, b_tsid_change = false;
749 bool b_dvb_change = !!((p_output->config.i_config ^ p_config->i_config)
750 & OUTPUT_DVB);
751 bool b_network_change =
752 (dvb_string_cmp(&p_output->config.network_name, &p_config->network_name) ||
753 p_output->config.i_network_id != p_config->i_network_id);
754 bool b_service_name_change =
755 (dvb_string_cmp(&p_output->config.service_name, &p_config->service_name) ||
756 dvb_string_cmp(&p_output->config.provider_name, &p_config->provider_name));
757 bool b_remap_change = p_output->config.i_new_sid != p_config->i_new_sid ||
758 p_output->config.i_onid != p_config->i_onid ||
759 p_output->config.b_do_remap != p_config->b_do_remap ||
760 p_output->config.pi_confpids[I_PMTPID] != p_config->pi_confpids[I_PMTPID] ||
761 p_output->config.pi_confpids[I_APID] != p_config->pi_confpids[I_APID] ||
762 p_output->config.pi_confpids[I_VPID] != p_config->pi_confpids[I_VPID] ||
763 p_output->config.pi_confpids[I_SPUPID] != p_config->pi_confpids[I_SPUPID];
764 int i;
766 p_output->config.i_config = p_config->i_config;
767 p_output->config.i_network_id = p_config->i_network_id;
768 p_output->config.i_new_sid = p_config->i_new_sid;
769 p_output->config.i_onid = p_config->i_onid;
770 p_output->config.b_do_remap = p_config->b_do_remap;
771 memcpy(p_output->config.pi_confpids, p_config->pi_confpids,
772 sizeof(uint16_t) * N_MAP_PIDS);
774 /* Change output settings related to names. */
775 dvb_string_clean( &p_output->config.network_name );
776 dvb_string_clean( &p_output->config.service_name );
777 dvb_string_clean( &p_output->config.provider_name );
778 dvb_string_copy( &p_output->config.network_name,
779 &p_config->network_name );
780 dvb_string_copy( &p_output->config.service_name,
781 &p_config->service_name );
782 dvb_string_copy( &p_output->config.provider_name,
783 &p_config->provider_name );
785 if ( p_config->i_tsid != -1 && p_output->config.i_tsid != p_config->i_tsid )
787 p_output->i_tsid = p_config->i_tsid;
788 b_tsid_change = true;
790 if ( p_config->i_tsid == -1 && p_output->config.i_tsid != -1 )
792 if ( psi_table_validate(pp_current_pat_sections) && !b_random_tsid )
793 p_output->i_tsid =
794 psi_table_get_tableidext(pp_current_pat_sections);
795 else
796 p_output->i_tsid = rand() & 0xffff;
797 b_tsid_change = true;
800 if ( p_config->b_passthrough == p_output->config.b_passthrough &&
801 !b_sid_change && p_config->i_nb_pids == p_output->config.i_nb_pids &&
802 (!p_config->i_nb_pids ||
803 !memcmp( p_output->config.pi_pids, p_config->pi_pids,
804 p_config->i_nb_pids * sizeof(uint16_t) )) )
805 goto out_change;
807 GetPIDS( &pi_wanted_pids, &i_nb_wanted_pids, i_sid, pi_pids, i_nb_pids );
808 GetPIDS( &pi_current_pids, &i_nb_current_pids, i_old_sid, pi_old_pids,
809 i_old_nb_pids );
811 if ( b_sid_change && i_old_sid )
813 sid_t *p_old_sid = FindSID( i_old_sid );
814 p_output->config.i_sid = p_config->i_sid;
816 if ( p_old_sid != NULL )
818 if ( i_sid != i_old_sid )
819 UnselectPMT( i_old_sid, p_old_sid->i_pmt_pid );
821 if ( i_ca_handle && !SIDIsSelected( i_old_sid )
822 && p_old_sid->p_current_pmt != NULL
823 && PMTNeedsDescrambling( p_old_sid->p_current_pmt ) )
824 en50221_DeletePMT( p_old_sid->p_current_pmt );
828 for ( i = 0; i < i_nb_current_pids; i++ )
830 if ( !IsIn( pi_wanted_pids, i_nb_wanted_pids, pi_current_pids[i] ) )
832 StopPID( p_output, pi_current_pids[i] );
833 b_pid_change = true;
837 if ( b_sid_change && i_ca_handle && i_old_sid &&
838 SIDIsSelected( i_old_sid ) )
840 sid_t *p_old_sid = FindSID( i_old_sid );
841 if ( p_old_sid != NULL && p_old_sid->p_current_pmt != NULL
842 && PMTNeedsDescrambling( p_old_sid->p_current_pmt ) )
843 en50221_UpdatePMT( p_old_sid->p_current_pmt );
846 for ( i = 0; i < i_nb_wanted_pids; i++ )
848 if ( !IsIn( pi_current_pids, i_nb_current_pids, pi_wanted_pids[i] ) )
850 StartPID( p_output, pi_wanted_pids[i] );
851 b_pid_change = true;
855 free( pi_wanted_pids );
856 free( pi_current_pids );
858 if ( b_sid_change && i_sid )
860 sid_t *p_sid = FindSID( i_sid );
861 p_output->config.i_sid = i_old_sid;
863 if ( p_sid != NULL )
865 if ( i_sid != i_old_sid )
866 SelectPMT( i_sid, p_sid->i_pmt_pid );
868 if ( i_ca_handle && !SIDIsSelected( i_sid )
869 && p_sid->p_current_pmt != NULL
870 && PMTNeedsDescrambling( p_sid->p_current_pmt ) )
871 en50221_AddPMT( p_sid->p_current_pmt );
875 if ( i_ca_handle && i_sid && SIDIsSelected( i_sid ) )
877 sid_t *p_sid = FindSID( i_sid );
878 if ( p_sid != NULL && p_sid->p_current_pmt != NULL
879 && PMTNeedsDescrambling( p_sid->p_current_pmt ) )
880 en50221_UpdatePMT( p_sid->p_current_pmt );
883 p_output->config.b_passthrough = p_config->b_passthrough;
884 p_output->config.i_sid = i_sid;
885 free( p_output->config.pi_pids );
886 p_output->config.pi_pids = malloc( sizeof(uint16_t) * i_nb_pids );
887 memcpy( p_output->config.pi_pids, pi_pids, sizeof(uint16_t) * i_nb_pids );
888 p_output->config.i_nb_pids = i_nb_pids;
890 out_change:
891 if ( b_sid_change || b_remap_change )
893 NewSDT( p_output );
894 NewNIT( p_output );
895 NewPAT( p_output );
896 NewPMT( p_output );
898 else
900 if ( b_tsid_change )
902 NewSDT( p_output );
903 NewNIT( p_output );
904 NewPAT( p_output );
906 else if ( b_dvb_change )
908 NewNIT( p_output );
909 NewPAT( p_output );
911 else if ( b_network_change )
912 NewNIT( p_output );
914 if ( !b_tsid_change && b_service_name_change )
915 NewSDT( p_output );
917 if ( b_pid_change )
918 NewPMT( p_output );
922 /*****************************************************************************
923 * SetDTS
924 *****************************************************************************/
925 static void SetDTS( block_t *p_list )
927 int i_nb_ts = 0, i;
928 mtime_t i_duration;
929 block_t *p_ts = p_list;
931 while ( p_ts != NULL )
933 i_nb_ts++;
934 p_ts = p_ts->p_next;
937 /* We suppose the stream is CBR, at least between two consecutive read().
938 * This is especially true in budget mode */
939 if ( i_last_dts == -1 )
940 i_duration = 0;
941 else
942 i_duration = i_wallclock - i_last_dts;
944 p_ts = p_list;
945 i = i_nb_ts - 1;
946 while ( p_ts != NULL )
948 p_ts->i_dts = i_wallclock - i_duration * i / i_nb_ts;
949 i--;
950 p_ts = p_ts->p_next;
953 i_last_dts = i_wallclock;
956 /*****************************************************************************
957 * SetPID/UnsetPID
958 *****************************************************************************/
959 static void SetPID( uint16_t i_pid )
961 p_pids[i_pid].i_refcount++;
963 if ( !b_budget_mode && p_pids[i_pid].i_refcount
964 && p_pids[i_pid].i_demux_fd == -1 )
965 p_pids[i_pid].i_demux_fd = pf_SetFilter( i_pid );
968 static void SetPID_EMM( uint16_t i_pid )
970 SetPID( i_pid );
971 p_pids[i_pid].b_emm = true;
974 static void UnsetPID( uint16_t i_pid )
976 p_pids[i_pid].i_refcount--;
978 if ( !b_budget_mode && !p_pids[i_pid].i_refcount
979 && p_pids[i_pid].i_demux_fd != -1 )
981 pf_UnsetFilter( p_pids[i_pid].i_demux_fd, i_pid );
982 p_pids[i_pid].i_demux_fd = -1;
983 p_pids[i_pid].b_emm = false;
987 /*****************************************************************************
988 * StartPID/StopPID
989 *****************************************************************************/
990 static void StartPID( output_t *p_output, uint16_t i_pid )
992 int j;
994 for ( j = 0; j < p_pids[i_pid].i_nb_outputs; j++ )
995 if ( p_pids[i_pid].pp_outputs[j] == p_output )
996 break;
998 if ( j == p_pids[i_pid].i_nb_outputs )
1000 for ( j = 0; j < p_pids[i_pid].i_nb_outputs; j++ )
1001 if ( p_pids[i_pid].pp_outputs[j] == NULL )
1002 break;
1004 if ( j == p_pids[i_pid].i_nb_outputs )
1006 p_pids[i_pid].i_nb_outputs++;
1007 p_pids[i_pid].pp_outputs = realloc( p_pids[i_pid].pp_outputs,
1008 sizeof(output_t *)
1009 * p_pids[i_pid].i_nb_outputs );
1012 p_pids[i_pid].pp_outputs[j] = p_output;
1013 SetPID( i_pid );
1017 static void StopPID( output_t *p_output, uint16_t i_pid )
1019 int j;
1021 for ( j = 0; j < p_pids[i_pid].i_nb_outputs; j++ )
1023 if ( p_pids[i_pid].pp_outputs[j] != NULL )
1025 if ( p_pids[i_pid].pp_outputs[j] == p_output )
1026 break;
1030 if ( j != p_pids[i_pid].i_nb_outputs )
1032 p_pids[i_pid].pp_outputs[j] = NULL;
1033 UnsetPID( i_pid );
1037 /*****************************************************************************
1038 * SelectPID/UnselectPID
1039 *****************************************************************************/
1040 static void SelectPID( uint16_t i_sid, uint16_t i_pid )
1042 int i;
1044 for ( i = 0; i < i_nb_outputs; i++ )
1045 if ( (pp_outputs[i]->config.i_config & OUTPUT_VALID)
1046 && pp_outputs[i]->config.i_sid == i_sid
1047 && !pp_outputs[i]->config.i_nb_pids )
1048 StartPID( pp_outputs[i], i_pid );
1051 static void UnselectPID( uint16_t i_sid, uint16_t i_pid )
1053 int i;
1055 for ( i = 0; i < i_nb_outputs; i++ )
1056 if ( (pp_outputs[i]->config.i_config & OUTPUT_VALID)
1057 && pp_outputs[i]->config.i_sid == i_sid
1058 && !pp_outputs[i]->config.i_nb_pids )
1059 StopPID( pp_outputs[i], i_pid );
1062 /*****************************************************************************
1063 * SelectPMT/UnselectPMT
1064 *****************************************************************************/
1065 static void SelectPMT( uint16_t i_sid, uint16_t i_pid )
1067 int i;
1069 p_pids[i_pid].i_psi_refcount++;
1070 p_pids[i_pid].b_pes = false;
1072 if ( b_select_pmts )
1073 SetPID( i_pid );
1074 else for ( i = 0; i < i_nb_outputs; i++ )
1075 if ( (pp_outputs[i]->config.i_config & OUTPUT_VALID)
1076 && pp_outputs[i]->config.i_sid == i_sid )
1077 SetPID( i_pid );
1080 static void UnselectPMT( uint16_t i_sid, uint16_t i_pid )
1082 int i;
1084 p_pids[i_pid].i_psi_refcount--;
1085 if ( !p_pids[i_pid].i_psi_refcount )
1086 psi_assemble_reset( &p_pids[i_pid].p_psi_buffer,
1087 &p_pids[i_pid].i_psi_buffer_used );
1089 if ( b_select_pmts )
1090 UnsetPID( i_pid );
1091 else for ( i = 0; i < i_nb_outputs; i++ )
1092 if ( (pp_outputs[i]->config.i_config & OUTPUT_VALID)
1093 && pp_outputs[i]->config.i_sid == i_sid )
1094 UnsetPID( i_pid );
1097 /*****************************************************************************
1098 * GetPIDS
1099 *****************************************************************************/
1100 static void GetPIDS( uint16_t **ppi_wanted_pids, int *pi_nb_wanted_pids,
1101 uint16_t i_sid,
1102 const uint16_t *pi_pids, int i_nb_pids )
1104 sid_t *p_sid;
1105 uint8_t *p_pmt;
1106 uint16_t i_pmt_pid, i_pcr_pid;
1107 uint8_t *p_es;
1108 uint8_t j;
1109 const uint8_t *p_desc;
1111 if ( i_nb_pids || i_sid == 0 )
1113 *pi_nb_wanted_pids = i_nb_pids;
1114 *ppi_wanted_pids = malloc( sizeof(uint16_t) * i_nb_pids );
1115 memcpy( *ppi_wanted_pids, pi_pids, sizeof(uint16_t) * i_nb_pids );
1116 return;
1119 *pi_nb_wanted_pids = 0;
1120 *ppi_wanted_pids = NULL;
1122 p_sid = FindSID( i_sid );
1123 if ( p_sid == NULL )
1124 return;
1126 p_pmt = p_sid->p_current_pmt;
1127 i_pmt_pid = p_sid->i_pmt_pid;
1128 if ( p_pmt == NULL ) {
1129 msg_Dbg(NULL, "no current PMT on sid %d\n", i_sid);
1130 return;
1133 i_pcr_pid = pmt_get_pcrpid( p_pmt );
1134 j = 0;
1135 while ( (p_es = pmt_get_es( p_pmt, j )) != NULL )
1137 j++;
1138 if ( PIDWouldBeSelected( p_es ) )
1140 *ppi_wanted_pids = realloc( *ppi_wanted_pids,
1141 (*pi_nb_wanted_pids + 1) * sizeof(uint16_t) );
1142 (*ppi_wanted_pids)[(*pi_nb_wanted_pids)++] = pmtn_get_pid( p_es );
1145 if ( b_enable_ecm )
1147 uint8_t k = 0;
1149 while ((p_desc = descs_get_desc( pmtn_get_descs( p_es ), k++ )) != NULL)
1151 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
1152 continue;
1153 *ppi_wanted_pids = realloc( *ppi_wanted_pids,
1154 (*pi_nb_wanted_pids + 1) * sizeof(uint16_t) );
1155 (*ppi_wanted_pids)[(*pi_nb_wanted_pids)++] = desc09_get_pid( p_desc );
1161 if ( b_enable_ecm )
1163 j = 0;
1165 while ((p_desc = descs_get_desc( pmt_get_descs( p_pmt ), j++ )) != NULL)
1167 if ( desc_get_tag( p_desc ) != 0x09 ||
1168 !desc09_validate( p_desc ) )
1169 continue;
1170 *ppi_wanted_pids = realloc( *ppi_wanted_pids,
1171 (*pi_nb_wanted_pids + 1) * sizeof(uint16_t) );
1172 (*ppi_wanted_pids)[(*pi_nb_wanted_pids)++] = desc09_get_pid( p_desc );
1176 if ( i_pcr_pid != PADDING_PID && i_pcr_pid != i_pmt_pid
1177 && !IsIn( *ppi_wanted_pids, *pi_nb_wanted_pids, i_pcr_pid ) )
1179 *ppi_wanted_pids = realloc( *ppi_wanted_pids,
1180 (*pi_nb_wanted_pids + 1) * sizeof(uint16_t) );
1181 (*ppi_wanted_pids)[(*pi_nb_wanted_pids)++] = i_pcr_pid;
1185 /*****************************************************************************
1186 * OutputPSISection
1187 *****************************************************************************/
1188 static void OutputPSISection( output_t *p_output, uint8_t *p_section,
1189 uint16_t i_pid, uint8_t *pi_cc, mtime_t i_dts,
1190 block_t **pp_ts_buffer,
1191 uint8_t *pi_ts_buffer_offset )
1193 uint16_t i_section_length = psi_get_length(p_section) + PSI_HEADER_SIZE;
1194 uint16_t i_section_offset = 0;
1198 block_t *p_block;
1199 uint8_t *p;
1200 uint8_t i_ts_offset;
1201 bool b_append = (pp_ts_buffer != NULL && *pp_ts_buffer != NULL);
1203 if ( b_append )
1205 p_block = *pp_ts_buffer;
1206 i_ts_offset = *pi_ts_buffer_offset;
1208 else
1210 p_block = block_New();
1211 p_block->i_dts = i_dts;
1212 i_ts_offset = 0;
1214 p = p_block->p_ts;
1216 psi_split_section( p, &i_ts_offset, p_section, &i_section_offset );
1218 if ( !b_append )
1220 ts_set_pid( p, i_pid );
1221 ts_set_cc( p, *pi_cc );
1222 (*pi_cc)++;
1223 *pi_cc &= 0xf;
1226 if ( i_section_offset == i_section_length )
1228 if ( i_ts_offset < TS_SIZE - MIN_SECTION_FRAGMENT
1229 && pp_ts_buffer != NULL )
1231 *pp_ts_buffer = p_block;
1232 *pi_ts_buffer_offset = i_ts_offset;
1233 break;
1235 else
1236 psi_split_end( p, &i_ts_offset );
1239 p_block->i_dts = i_dts;
1240 p_block->i_refcount--;
1241 output_Put( p_output, p_block );
1242 if ( pp_ts_buffer != NULL )
1244 *pp_ts_buffer = NULL;
1245 *pi_ts_buffer_offset = 0;
1248 while ( i_section_offset < i_section_length );
1251 /*****************************************************************************
1252 * SendPAT
1253 *****************************************************************************/
1254 static void SendPAT( mtime_t i_dts )
1256 int i;
1258 for ( i = 0; i < i_nb_outputs; i++ )
1260 output_t *p_output = pp_outputs[i];
1262 if ( !(p_output->config.i_config & OUTPUT_VALID) ||
1263 p_output->config.b_passthrough )
1264 continue;
1266 if ( p_output->p_pat_section == NULL &&
1267 psi_table_validate(pp_current_pat_sections) )
1269 /* SID doesn't exist - build an empty PAT. */
1270 uint8_t *p;
1271 p_output->i_pat_version++;
1273 p = p_output->p_pat_section = psi_allocate();
1274 pat_init( p );
1275 pat_set_length( p, 0 );
1276 pat_set_tsid( p, p_output->i_tsid );
1277 psi_set_version( p, p_output->i_pat_version );
1278 psi_set_current( p );
1279 psi_set_section( p, 0 );
1280 psi_set_lastsection( p, 0 );
1281 psi_set_crc( p_output->p_pat_section );
1285 if ( p_output->p_pat_section != NULL )
1286 OutputPSISection( p_output, p_output->p_pat_section, PAT_PID,
1287 &p_output->i_pat_cc, i_dts, NULL, NULL );
1291 /*****************************************************************************
1292 * SendPMT
1293 *****************************************************************************/
1294 static void SendPMT( sid_t *p_sid, mtime_t i_dts )
1296 int i;
1297 int i_pmt_pid = p_sid->i_pmt_pid;
1299 if ( b_do_remap )
1300 i_pmt_pid = pi_newpids[ I_PMTPID ];
1302 for ( i = 0; i < i_nb_outputs; i++ )
1304 output_t *p_output = pp_outputs[i];
1306 if ( (p_output->config.i_config & OUTPUT_VALID)
1307 && p_output->config.i_sid == p_sid->i_sid
1308 && p_output->p_pmt_section != NULL )
1310 if ( p_output->config.b_do_remap && p_output->config.pi_confpids[I_PMTPID] )
1311 i_pmt_pid = p_output->config.pi_confpids[I_PMTPID];
1313 OutputPSISection( p_output, p_output->p_pmt_section,
1314 i_pmt_pid, &p_output->i_pmt_cc, i_dts,
1315 NULL, NULL );
1320 /*****************************************************************************
1321 * SendNIT
1322 *****************************************************************************/
1323 static void SendNIT( mtime_t i_dts )
1325 int i;
1327 for ( i = 0; i < i_nb_outputs; i++ )
1329 output_t *p_output = pp_outputs[i];
1331 if ( (p_output->config.i_config & OUTPUT_VALID)
1332 && !p_output->config.b_passthrough
1333 && (p_output->config.i_config & OUTPUT_DVB)
1334 && p_output->p_nit_section != NULL )
1335 OutputPSISection( p_output, p_output->p_nit_section, NIT_PID,
1336 &p_output->i_nit_cc, i_dts, NULL, NULL );
1340 /*****************************************************************************
1341 * SendSDT
1342 *****************************************************************************/
1343 static void SendSDT( mtime_t i_dts )
1345 int i;
1347 for ( i = 0; i < i_nb_outputs; i++ )
1349 output_t *p_output = pp_outputs[i];
1351 if ( (p_output->config.i_config & OUTPUT_VALID)
1352 && !p_output->config.b_passthrough
1353 && (p_output->config.i_config & OUTPUT_DVB)
1354 && p_output->p_sdt_section != NULL )
1355 OutputPSISection( p_output, p_output->p_sdt_section, SDT_PID,
1356 &p_output->i_sdt_cc, i_dts, NULL, NULL );
1360 /*****************************************************************************
1361 * SendEIT
1362 *****************************************************************************/
1363 static void SendEIT( sid_t *p_sid, mtime_t i_dts, uint8_t *p_eit )
1365 uint8_t i_table_id = psi_get_tableid( p_eit );
1366 bool b_epg = i_table_id >= EIT_TABLE_ID_SCHED_ACTUAL_FIRST &&
1367 i_table_id <= EIT_TABLE_ID_SCHED_ACTUAL_LAST;
1368 int i;
1370 for ( i = 0; i < i_nb_outputs; i++ )
1372 output_t *p_output = pp_outputs[i];
1374 if ( (p_output->config.i_config & OUTPUT_VALID)
1375 && !p_output->config.b_passthrough
1376 && (p_output->config.i_config & OUTPUT_DVB)
1377 && (!b_epg || (p_output->config.i_config & OUTPUT_EPG))
1378 && p_output->config.i_sid == p_sid->i_sid )
1380 eit_set_tsid( p_eit, p_output->i_tsid );
1382 if ( p_output->config.i_new_sid )
1383 eit_set_sid( p_eit, p_output->config.i_new_sid );
1384 else
1385 eit_set_sid( p_eit, p_output->config.i_sid );
1387 psi_set_crc( p_eit );
1389 OutputPSISection( p_output, p_eit, EIT_PID, &p_output->i_eit_cc,
1390 i_dts, &p_output->p_eit_ts_buffer,
1391 &p_output->i_eit_ts_buffer_offset );
1396 /*****************************************************************************
1397 * FlushEIT
1398 *****************************************************************************/
1399 static void FlushEIT( output_t *p_output, mtime_t i_dts )
1401 block_t *p_block = p_output->p_eit_ts_buffer;
1403 psi_split_end( p_block->p_ts, &p_output->i_eit_ts_buffer_offset );
1404 p_block->i_dts = i_dts;
1405 p_block->i_refcount--;
1406 output_Put( p_output, p_block );
1407 p_output->p_eit_ts_buffer = NULL;
1408 p_output->i_eit_ts_buffer_offset = 0;
1411 /*****************************************************************************
1412 * SendTDT
1413 *****************************************************************************/
1414 static void SendTDT( block_t *p_ts )
1416 int i;
1418 for ( i = 0; i < i_nb_outputs; i++ )
1420 output_t *p_output = pp_outputs[i];
1422 if ( (p_output->config.i_config & OUTPUT_VALID)
1423 && !p_output->config.b_passthrough
1424 && (p_output->config.i_config & OUTPUT_DVB)
1425 && p_output->p_sdt_section != NULL )
1426 output_Put( p_output, p_ts );
1429 /*****************************************************************************
1430 * SendEMM
1431 *****************************************************************************/
1432 static void SendEMM( block_t *p_ts )
1434 int i;
1436 for ( i = 0; i < i_nb_outputs; i++ )
1438 output_t *p_output = pp_outputs[i];
1440 if ( (p_output->config.i_config & OUTPUT_VALID)
1441 && !p_output->config.b_passthrough )
1442 output_Put( p_output, p_ts );
1446 /*****************************************************************************
1447 * NewPAT
1448 *****************************************************************************/
1449 static void NewPAT( output_t *p_output )
1451 const uint8_t *p_program;
1452 uint8_t *p;
1453 uint8_t k = 0;
1455 free( p_output->p_pat_section );
1456 p_output->p_pat_section = NULL;
1457 p_output->i_pat_version++;
1459 if ( !p_output->config.i_sid ) return;
1460 if ( !psi_table_validate(pp_current_pat_sections) ) return;
1462 p_program = pat_table_find_program( pp_current_pat_sections,
1463 p_output->config.i_sid );
1464 if ( p_program == NULL ) return;
1466 p = p_output->p_pat_section = psi_allocate();
1467 pat_init( p );
1468 psi_set_length( p, PSI_MAX_SIZE );
1469 pat_set_tsid( p, p_output->i_tsid );
1470 psi_set_version( p, p_output->i_pat_version );
1471 psi_set_current( p );
1472 psi_set_section( p, 0 );
1473 psi_set_lastsection( p, 0 );
1475 if ( p_output->config.i_config & OUTPUT_DVB )
1477 /* NIT */
1478 p = pat_get_program( p_output->p_pat_section, k++ );
1479 patn_init( p );
1480 patn_set_program( p, 0 );
1481 patn_set_pid( p, NIT_PID );
1484 p = pat_get_program( p_output->p_pat_section, k++ );
1485 patn_init( p );
1486 if ( p_output->config.i_new_sid )
1488 msg_Dbg( NULL, "Mapping PAT SID %d to %d", p_output->config.i_sid,
1489 p_output->config.i_new_sid );
1490 patn_set_program( p, p_output->config.i_new_sid );
1492 else
1493 patn_set_program( p, p_output->config.i_sid );
1495 if ( b_do_remap )
1497 msg_Dbg( NULL, "Mapping PMT PID %d to %d", patn_get_pid( p_program ), pi_newpids[I_PMTPID] );
1498 patn_set_pid( p, pi_newpids[I_PMTPID]);
1499 } else if ( p_output->config.b_do_remap && p_output->config.pi_confpids[I_PMTPID] ) {
1500 msg_Dbg( NULL, "Mapping PMT PID %d to %d", patn_get_pid( p_program ), p_output->config.pi_confpids[I_PMTPID] );
1501 patn_set_pid( p, p_output->config.pi_confpids[I_PMTPID]);
1502 } else {
1503 patn_set_pid( p, patn_get_pid( p_program ) );
1506 p = pat_get_program( p_output->p_pat_section, k );
1507 pat_set_length( p_output->p_pat_section,
1508 p - p_output->p_pat_section - PAT_HEADER_SIZE );
1509 psi_set_crc( p_output->p_pat_section );
1512 /*****************************************************************************
1513 * NewPMT
1514 *****************************************************************************/
1515 static void CopyDescriptors( uint8_t *p_descs, uint8_t *p_current_descs )
1517 uint8_t *p_desc;
1518 const uint8_t *p_current_desc;
1519 uint16_t j = 0, k = 0;
1521 descs_set_length( p_descs, DESCS_MAX_SIZE );
1523 while ( (p_current_desc = descs_get_desc( p_current_descs, j )) != NULL )
1525 uint8_t i_tag = desc_get_tag( p_current_desc );
1527 j++;
1528 if ( !b_enable_ecm && i_tag == 0x9 ) continue;
1530 p_desc = descs_get_desc( p_descs, k );
1531 if ( p_desc == NULL ) continue; /* This shouldn't happen */
1532 k++;
1533 memcpy( p_desc, p_current_desc,
1534 DESC_HEADER_SIZE + desc_get_length( p_current_desc ) );
1537 p_desc = descs_get_desc( p_descs, k );
1538 if ( p_desc == NULL )
1539 /* This shouldn't happen if the incoming PMT is valid */
1540 descs_set_length( p_descs, 0 );
1541 else
1542 descs_set_length( p_descs, p_desc - p_descs - DESCS_HEADER_SIZE );
1545 static void NewPMT( output_t *p_output )
1547 sid_t *p_sid;
1548 uint8_t *p_current_pmt;
1549 uint8_t *p_es, *p_current_es;
1550 uint8_t *p;
1551 uint16_t j, k;
1552 uint16_t i_pcrpid;
1554 free( p_output->p_pmt_section );
1555 p_output->p_pmt_section = NULL;
1556 p_output->i_pmt_version++;
1558 if ( !p_output->config.i_sid ) return;
1560 p_sid = FindSID( p_output->config.i_sid );
1561 if ( p_sid == NULL ) return;
1563 if ( p_sid->p_current_pmt == NULL ) return;
1564 p_current_pmt = p_sid->p_current_pmt;
1566 p = p_output->p_pmt_section = psi_allocate();
1567 pmt_init( p );
1568 psi_set_length( p, PSI_MAX_SIZE );
1569 if ( p_output->config.i_new_sid )
1571 msg_Dbg( NULL, "Mapping PMT SID %d to %d", p_output->config.i_sid,
1572 p_output->config.i_new_sid );
1573 pmt_set_program( p, p_output->config.i_new_sid );
1575 else
1576 pmt_set_program( p, p_output->config.i_sid );
1577 psi_set_version( p, p_output->i_pmt_version );
1578 psi_set_current( p );
1579 pmt_set_desclength( p, 0 );
1580 init_pid_mapping( p_output );
1583 CopyDescriptors( pmt_get_descs( p ), pmt_get_descs( p_current_pmt ) );
1585 j = 0; k = 0;
1586 while ( (p_current_es = pmt_get_es( p_current_pmt, j )) != NULL )
1588 uint16_t i_pid = pmtn_get_pid( p_current_es );
1590 j++;
1591 if ( (p_output->config.i_nb_pids || !PIDWouldBeSelected( p_current_es ))
1592 && !IsIn( p_output->config.pi_pids, p_output->config.i_nb_pids,
1593 i_pid ) )
1594 continue;
1596 p_es = pmt_get_es( p, k );
1597 if ( p_es == NULL ) continue; /* This shouldn't happen */
1598 k++;
1599 pmtn_init( p_es );
1600 pmtn_set_streamtype( p_es, pmtn_get_streamtype( p_current_es ) );
1601 pmtn_set_pid( p_es, map_es_pid(p_output, p_current_es, i_pid) );
1602 pmtn_set_desclength( p_es, 0 );
1604 CopyDescriptors( pmtn_get_descs( p_es ),
1605 pmtn_get_descs( p_current_es ) );
1608 /* Do the pcr pid after everything else as it may have been remapped */
1609 i_pcrpid = pmt_get_pcrpid( p_current_pmt );
1610 if ( p_output->pi_newpids[i_pcrpid] != UNUSED_PID ) {
1611 msg_Dbg( NULL, "REMAP: The PCR PID was changed from 0x%x (%u) to 0x%x (%u)",
1612 i_pcrpid, i_pcrpid, p_output->pi_newpids[i_pcrpid], p_output->pi_newpids[i_pcrpid] );
1613 i_pcrpid = p_output->pi_newpids[i_pcrpid];
1614 } else {
1615 msg_Dbg( NULL, "The PCR PID has kept its original value of 0x%x (%u)", i_pcrpid, i_pcrpid);
1617 pmt_set_pcrpid( p, i_pcrpid );
1618 p_es = pmt_get_es( p, k );
1619 if ( p_es == NULL )
1620 /* This shouldn't happen if the incoming PMT is valid */
1621 pmt_set_length( p, 0 );
1622 else
1623 pmt_set_length( p, p_es - p - PMT_HEADER_SIZE );
1624 psi_set_crc( p );
1627 /*****************************************************************************
1628 * NewNIT
1629 *****************************************************************************/
1630 static void NewNIT( output_t *p_output )
1632 uint8_t *p_ts;
1633 uint8_t *p_header2;
1634 uint8_t *p;
1636 free( p_output->p_nit_section );
1637 p_output->p_nit_section = NULL;
1638 p_output->i_nit_version++;
1640 p = p_output->p_nit_section = psi_allocate();
1641 nit_init( p, true );
1642 nit_set_length( p, PSI_MAX_SIZE );
1643 nit_set_nid( p, p_output->config.i_network_id );
1644 psi_set_version( p, p_output->i_nit_version );
1645 psi_set_current( p );
1646 psi_set_section( p, 0 );
1647 psi_set_lastsection( p, 0 );
1649 if ( p_output->config.network_name.i )
1651 uint8_t *p_descs;
1652 uint8_t *p_desc;
1653 nit_set_desclength( p, DESCS_MAX_SIZE );
1654 p_descs = nit_get_descs( p );
1655 p_desc = descs_get_desc( p_descs, 0 );
1656 desc40_init( p_desc );
1657 desc40_set_networkname( p_desc, p_output->config.network_name.p,
1658 p_output->config.network_name.i );
1659 p_desc = descs_get_desc( p_descs, 1 );
1660 descs_set_length( p_descs, p_desc - p_descs - DESCS_HEADER_SIZE );
1662 else
1663 nit_set_desclength( p, 0 );
1665 p_header2 = nit_get_header2( p );
1666 nith_init( p_header2 );
1667 nith_set_tslength( p_header2, NIT_TS_SIZE );
1669 p_ts = nit_get_ts( p, 0 );
1670 nitn_init( p_ts );
1671 nitn_set_tsid( p_ts, p_output->i_tsid );
1672 if ( p_output->config.i_onid )
1673 nitn_set_onid( p_ts, p_output->config.i_onid );
1674 else
1675 nitn_set_onid( p_ts, p_output->config.i_network_id );
1676 nitn_set_desclength( p_ts, 0 );
1678 p_ts = nit_get_ts( p, 1 );
1679 if ( p_ts == NULL )
1680 /* This shouldn't happen */
1681 nit_set_length( p, 0 );
1682 else
1683 nit_set_length( p, p_ts - p - NIT_HEADER_SIZE );
1684 psi_set_crc( p_output->p_nit_section );
1687 /*****************************************************************************
1688 * NewSDT
1689 *****************************************************************************/
1690 static void NewSDT( output_t *p_output )
1692 uint8_t *p_service, *p_current_service;
1693 uint8_t *p;
1695 free( p_output->p_sdt_section );
1696 p_output->p_sdt_section = NULL;
1697 p_output->i_sdt_version++;
1699 if ( !p_output->config.i_sid ) return;
1700 if ( !psi_table_validate(pp_current_sdt_sections) ) return;
1702 p_current_service = sdt_table_find_service( pp_current_sdt_sections,
1703 p_output->config.i_sid );
1705 if ( p_current_service == NULL )
1707 if ( p_output->p_pat_section != NULL &&
1708 pat_get_program( p_output->p_pat_section, 0 ) == NULL )
1710 /* Empty PAT and no SDT anymore */
1711 free( p_output->p_pat_section );
1712 p_output->p_pat_section = NULL;
1713 p_output->i_pat_version++;
1715 return;
1718 p = p_output->p_sdt_section = psi_allocate();
1719 sdt_init( p, true );
1720 sdt_set_length( p, PSI_MAX_SIZE );
1721 sdt_set_tsid( p, p_output->i_tsid );
1722 psi_set_version( p, p_output->i_sdt_version );
1723 psi_set_current( p );
1724 psi_set_section( p, 0 );
1725 psi_set_lastsection( p, 0 );
1726 if ( p_output->config.i_onid )
1727 sdt_set_onid( p, p_output->config.i_onid );
1728 else
1729 sdt_set_onid( p,
1730 sdt_get_onid( psi_table_get_section( pp_current_sdt_sections, 0 ) ) );
1732 p_service = sdt_get_service( p, 0 );
1733 sdtn_init( p_service );
1734 if ( p_output->config.i_new_sid )
1736 msg_Dbg( NULL, "Mapping SDT SID %d to %d", p_output->config.i_sid,
1737 p_output->config.i_new_sid );
1738 sdtn_set_sid( p_service, p_output->config.i_new_sid );
1740 else
1741 sdtn_set_sid( p_service, p_output->config.i_sid );
1743 if ( (p_output->config.i_config & OUTPUT_EPG) == OUTPUT_EPG )
1745 sdtn_set_eitschedule(p_service);
1746 sdtn_set_eitpresent(p_service);
1747 } else {
1748 if ( sdtn_get_eitschedule(p_current_service) )
1749 sdtn_set_eitschedule(p_service);
1750 if ( sdtn_get_eitpresent(p_current_service) )
1751 sdtn_set_eitpresent(p_service);
1754 sdtn_set_running( p_service, sdtn_get_running(p_current_service) );
1755 /* Do not set free_ca */
1756 sdtn_set_desclength( p_service, sdtn_get_desclength(p_current_service) );
1758 if ( !p_output->config.provider_name.i &&
1759 !p_output->config.service_name.i ) {
1760 /* Copy all descriptors unchanged */
1761 memcpy( descs_get_desc( sdtn_get_descs(p_service), 0 ),
1762 descs_get_desc( sdtn_get_descs(p_current_service), 0 ),
1763 sdtn_get_desclength(p_current_service) );
1764 } else {
1765 int j = 0, i_total_desc_len = 0;
1766 uint8_t *p_desc;
1767 uint8_t *p_new_desc = descs_get_desc( sdtn_get_descs(p_service), 0 );
1768 while ( (p_desc = descs_get_desc( sdtn_get_descs( p_current_service ), j++ )) != NULL )
1770 /* Regenerate descriptor 48 (service name) */
1771 if ( desc_get_tag( p_desc ) == 0x48 && desc48_validate( p_desc ) )
1773 uint8_t i_old_provider_len, i_old_service_len;
1774 uint8_t i_new_desc_len = 3; /* 1 byte - type, 1 byte provider_len, 1 byte service_len */
1775 const uint8_t *p_old_provider = desc48_get_provider( p_desc, &i_old_provider_len );
1776 const uint8_t *p_old_service = desc48_get_service( p_desc, &i_old_service_len );
1778 desc48_init( p_new_desc );
1779 desc48_set_type( p_new_desc, desc48_get_type( p_desc ) );
1781 if ( p_output->config.provider_name.i ) {
1782 desc48_set_provider( p_new_desc,
1783 p_output->config.provider_name.p,
1784 p_output->config.provider_name.i );
1785 i_new_desc_len += p_output->config.provider_name.i;
1786 } else {
1787 desc48_set_provider( p_new_desc, p_old_provider,
1788 i_old_provider_len );
1789 i_new_desc_len += i_old_provider_len;
1792 if ( p_output->config.service_name.i ) {
1793 desc48_set_service( p_new_desc,
1794 p_output->config.service_name.p,
1795 p_output->config.service_name.i );
1796 i_new_desc_len += p_output->config.service_name.i;
1797 } else {
1798 desc48_set_service( p_new_desc, p_old_service,
1799 i_old_service_len );
1800 i_new_desc_len += i_old_service_len;
1803 desc_set_length( p_new_desc, i_new_desc_len );
1804 i_total_desc_len += DESC_HEADER_SIZE + i_new_desc_len;
1805 p_new_desc += DESC_HEADER_SIZE + i_new_desc_len;
1806 } else {
1807 /* Copy single descriptor */
1808 int i_desc_len = DESC_HEADER_SIZE + desc_get_length( p_desc );
1809 memcpy( p_new_desc, p_desc, i_desc_len );
1810 p_new_desc += i_desc_len;
1811 i_total_desc_len += i_desc_len;
1814 sdtn_set_desclength( p_service, i_total_desc_len );
1817 p_service = sdt_get_service( p, 1 );
1818 if ( p_service == NULL )
1819 /* This shouldn't happen if the incoming SDT is valid */
1820 sdt_set_length( p, 0 );
1821 else
1822 sdt_set_length( p, p_service - p - SDT_HEADER_SIZE );
1823 psi_set_crc( p_output->p_sdt_section );
1826 /*****************************************************************************
1827 * UpdatePAT/PMT/SDT
1828 *****************************************************************************/
1829 #define DECLARE_UPDATE_FUNC( table ) \
1830 static void Update##table( uint16_t i_sid ) \
1832 int i; \
1834 for ( i = 0; i < i_nb_outputs; i++ ) \
1835 if ( ( pp_outputs[i]->config.i_config & OUTPUT_VALID ) \
1836 && pp_outputs[i]->config.i_sid == i_sid ) \
1837 New##table( pp_outputs[i] ); \
1840 DECLARE_UPDATE_FUNC(PAT)
1841 DECLARE_UPDATE_FUNC(PMT)
1842 DECLARE_UPDATE_FUNC(SDT)
1844 /*****************************************************************************
1845 * UpdateTSID
1846 *****************************************************************************/
1847 static void UpdateTSID(void)
1849 uint16_t i_tsid = psi_table_get_tableidext(pp_current_pat_sections);
1850 int i;
1852 for ( i = 0; i < i_nb_outputs; i++ )
1854 output_t *p_output = pp_outputs[i];
1856 if ( (p_output->config.i_config & OUTPUT_VALID)
1857 && p_output->config.i_tsid == -1 && !b_random_tsid )
1859 p_output->i_tsid = i_tsid;
1860 NewNIT( p_output );
1865 /*****************************************************************************
1866 * SIDIsSelected
1867 *****************************************************************************/
1868 static bool SIDIsSelected( uint16_t i_sid )
1870 int i;
1872 for ( i = 0; i < i_nb_outputs; i++ )
1873 if ( (pp_outputs[i]->config.i_config & OUTPUT_VALID)
1874 && pp_outputs[i]->config.i_sid == i_sid )
1875 return true;
1877 return false;
1880 /*****************************************************************************
1881 * demux_PIDIsSelected
1882 *****************************************************************************/
1883 bool demux_PIDIsSelected( uint16_t i_pid )
1885 int i;
1887 for ( i = 0; i < p_pids[i_pid].i_nb_outputs; i++ )
1888 if ( p_pids[i_pid].pp_outputs[i] != NULL )
1889 return true;
1891 return false;
1894 /*****************************************************************************
1895 * PIDWouldBeSelected
1896 *****************************************************************************/
1897 static bool PIDWouldBeSelected( uint8_t *p_es )
1899 if ( b_any_type ) return true;
1901 uint8_t i_type = pmtn_get_streamtype( p_es );
1903 switch ( i_type )
1905 case 0x1: /* video MPEG-1 */
1906 case 0x2: /* video */
1907 case 0x3: /* audio MPEG-1 */
1908 case 0x4: /* audio */
1909 case 0xf: /* audio AAC ADTS */
1910 case 0x10: /* video MPEG-4 */
1911 case 0x11: /* audio AAC LATM */
1912 case 0x1b: /* video H264 */
1913 case 0x81: /* ATSC A/52 */
1914 case 0x87: /* ATSC Enhanced A/52 */
1915 return true;
1916 break;
1918 case 0x6:
1920 uint16_t j = 0;
1921 const uint8_t *p_desc;
1923 while ( (p_desc = descs_get_desc( pmtn_get_descs( p_es ), j )) != NULL )
1925 uint8_t i_tag = desc_get_tag( p_desc );
1926 j++;
1928 if( i_tag == 0x46 /* VBI + teletext */
1929 || i_tag == 0x56 /* teletext */
1930 || i_tag == 0x59 /* dvbsub */
1931 || i_tag == 0x6a /* A/52 */
1932 || i_tag == 0x7a /* Enhanced A/52 */
1933 || i_tag == 0x7b /* DCA */
1934 || i_tag == 0x7c /* AAC */ )
1935 return true;
1937 break;
1940 default:
1941 break;
1944 /* FIXME: also parse IOD */
1945 return false;
1948 /*****************************************************************************
1949 * PIDCarriesPES
1950 *****************************************************************************/
1951 static bool PIDCarriesPES( const uint8_t *p_es )
1953 uint8_t i_type = pmtn_get_streamtype( p_es );
1955 switch ( i_type )
1957 case 0x1: /* video MPEG-1 */
1958 case 0x2: /* video */
1959 case 0x3: /* audio MPEG-1 */
1960 case 0x4: /* audio */
1961 case 0x6: /* private PES data */
1962 case 0xf: /* audio AAC */
1963 case 0x10: /* video MPEG-4 */
1964 case 0x11: /* audio AAC LATM */
1965 case 0x1b: /* video H264 */
1966 case 0x81: /* ATSC A/52 */
1967 case 0x87: /* ATSC Enhanced A/52 */
1968 return true;
1969 break;
1971 default:
1972 return false;
1973 break;
1977 /*****************************************************************************
1978 * PMTNeedsDescrambling
1979 *****************************************************************************/
1980 static bool PMTNeedsDescrambling( uint8_t *p_pmt )
1982 uint8_t i;
1983 uint16_t j;
1984 uint8_t *p_es;
1985 const uint8_t *p_desc;
1987 j = 0;
1988 while ( (p_desc = descs_get_desc( pmt_get_descs( p_pmt ), j )) != NULL )
1990 uint8_t i_tag = desc_get_tag( p_desc );
1991 j++;
1993 if ( i_tag == 0x9 ) return true;
1996 i = 0;
1997 while ( (p_es = pmt_get_es( p_pmt, i )) != NULL )
1999 i++;
2000 j = 0;
2001 while ( (p_desc = descs_get_desc( pmtn_get_descs( p_es ), j )) != NULL )
2003 uint8_t i_tag = desc_get_tag( p_desc );
2004 j++;
2006 if ( i_tag == 0x9 ) return true;
2010 return false;
2013 /*****************************************************************************
2014 * demux_ResendCAPMTs
2015 *****************************************************************************/
2016 void demux_ResendCAPMTs( void )
2018 int i;
2019 for ( i = 0; i < i_nb_sids; i++ )
2020 if ( pp_sids[i]->p_current_pmt != NULL
2021 && SIDIsSelected( pp_sids[i]->i_sid )
2022 && PMTNeedsDescrambling( pp_sids[i]->p_current_pmt ) )
2023 en50221_AddPMT( pp_sids[i]->p_current_pmt );
2026 /* Find CA descriptor that have PID i_ca_pid */
2027 static uint8_t *ca_desc_find( uint8_t *p_descl, uint16_t i_length,
2028 uint16_t i_ca_pid )
2030 int j = 0;
2031 uint8_t *p_desc;
2033 while ( (p_desc = descl_get_desc( p_descl, i_length, j++ )) != NULL ) {
2034 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
2035 continue;
2036 if ( desc09_get_pid( p_desc ) == i_ca_pid )
2037 return p_desc;
2040 return NULL;
2043 /*****************************************************************************
2044 * DeleteProgram
2045 *****************************************************************************/
2046 static void DeleteProgram( uint16_t i_sid, uint16_t i_pid )
2048 sid_t *p_sid;
2049 uint8_t *p_pmt;
2050 uint8_t *p_desc;
2052 UnselectPMT( i_sid, i_pid );
2054 p_sid = FindSID( i_sid );
2055 if ( p_sid == NULL ) return;
2057 p_pmt = p_sid->p_current_pmt;
2059 if ( p_pmt != NULL )
2061 uint16_t i_pcr_pid = pmt_get_pcrpid( p_pmt );
2062 uint8_t *p_es;
2063 uint8_t j;
2065 if ( i_ca_handle && SIDIsSelected( i_sid )
2066 && PMTNeedsDescrambling( p_pmt ) )
2067 en50221_DeletePMT( p_pmt );
2069 if ( i_pcr_pid != PADDING_PID
2070 && i_pcr_pid != p_sid->i_pmt_pid )
2071 UnselectPID( i_sid, i_pcr_pid );
2073 if ( b_enable_ecm )
2075 j = 0;
2077 while ((p_desc = descs_get_desc( pmt_get_descs( p_pmt ), j++ )) != NULL)
2079 if ( desc_get_tag( p_desc ) != 0x09 ||
2080 !desc09_validate( p_desc ) )
2081 continue;
2082 UnselectPID( i_sid, desc09_get_pid( p_desc ) );
2086 j = 0;
2087 while ( (p_es = pmt_get_es( p_pmt, j )) != NULL )
2089 uint16_t i_pid = pmtn_get_pid( p_es );
2090 j++;
2092 if ( PIDWouldBeSelected( p_es ) )
2093 UnselectPID( i_sid, i_pid );
2095 if ( b_enable_ecm )
2097 uint8_t k = 0;
2099 while ((p_desc = descs_get_desc( pmtn_get_descs( p_es ), k++ )) != NULL)
2101 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
2102 continue;
2103 UnselectPID( i_sid, desc09_get_pid( p_desc ) );
2108 free( p_pmt );
2109 p_sid->p_current_pmt = NULL;
2111 p_sid->i_sid = 0;
2112 p_sid->i_pmt_pid = 0;
2115 /*****************************************************************************
2116 * demux_Iconv
2117 *****************************************************************************
2118 * This code is from biTStream's examples and is under the WTFPL (see
2119 * LICENSE.WTFPL).
2120 *****************************************************************************/
2121 static char *iconv_append_null(const char *p_string, size_t i_length)
2123 char *psz_string = malloc(i_length + 1);
2124 memcpy(psz_string, p_string, i_length);
2125 psz_string[i_length] = '\0';
2126 return psz_string;
2129 char *demux_Iconv(void *_unused, const char *psz_encoding,
2130 char *p_string, size_t i_length)
2132 #ifdef HAVE_ICONV
2133 static const char *psz_current_encoding = "";
2135 char *psz_string, *p;
2136 size_t i_out_length;
2138 if (!strcmp(psz_encoding, psz_native_charset))
2139 return iconv_append_null(p_string, i_length);
2141 if (iconv_handle != (iconv_t)-1 &&
2142 strcmp(psz_encoding, psz_current_encoding)) {
2143 iconv_close(iconv_handle);
2144 iconv_handle = (iconv_t)-1;
2147 if (iconv_handle == (iconv_t)-1)
2148 iconv_handle = iconv_open(psz_native_charset, psz_encoding);
2149 if (iconv_handle == (iconv_t)-1) {
2150 msg_Warn(NULL, "couldn't open converter from %s to %s (%m)", psz_encoding,
2151 psz_native_charset);
2152 return iconv_append_null(p_string, i_length);
2154 psz_current_encoding = psz_encoding;
2156 /* converted strings can be up to six times larger */
2157 i_out_length = i_length * 6;
2158 p = psz_string = malloc(i_out_length);
2159 if (iconv(iconv_handle, &p_string, &i_length, &p, &i_out_length) == -1) {
2160 msg_Warn(NULL, "couldn't convert from %s to %s (%m)", psz_encoding,
2161 psz_native_charset);
2162 free(psz_string);
2163 return iconv_append_null(p_string, i_length);
2165 if (i_length)
2166 msg_Warn(NULL, "partial conversion from %s to %s", psz_encoding,
2167 psz_native_charset);
2169 *p = '\0';
2170 return psz_string;
2171 #else
2172 return iconv_append_null(p_string, i_length);
2173 #endif
2176 /*****************************************************************************
2177 * demux_Print
2178 *****************************************************************************
2179 * This code is from biTStream's examples and is under the WTFPL (see
2180 * LICENSE.WTFPL).
2181 *****************************************************************************/
2182 __attribute__ ((format(printf, 2, 3)))
2183 static void demux_Print(void *_unused, const char *psz_format, ...)
2185 char psz_fmt[strlen(psz_format) + 2];
2186 va_list args;
2187 va_start(args, psz_format);
2188 strcpy(psz_fmt, psz_format);
2189 if ( i_print_type != PRINT_XML )
2190 strcat(psz_fmt, "\n");
2191 vprintf(psz_fmt, args);
2192 va_end(args);
2195 /*****************************************************************************
2196 * HandlePAT
2197 *****************************************************************************/
2198 static void HandlePAT( mtime_t i_dts )
2200 bool b_change = false;
2201 PSI_TABLE_DECLARE( pp_old_pat_sections );
2202 uint8_t i_last_section = psi_table_get_lastsection( pp_next_pat_sections );
2203 uint8_t i;
2205 if ( psi_table_validate( pp_current_pat_sections ) &&
2206 psi_table_compare( pp_current_pat_sections, pp_next_pat_sections ) )
2208 /* Identical PAT. Shortcut. */
2209 psi_table_free( pp_next_pat_sections );
2210 psi_table_init( pp_next_pat_sections );
2211 goto out_pat;
2214 if ( !pat_table_validate( pp_next_pat_sections ) )
2216 msg_Warn( NULL, "invalid PAT received" );
2217 switch (i_print_type) {
2218 case PRINT_XML:
2219 fprintf(print_fh, "<ERROR type=\"invalid_pat\"/>\n");
2220 break;
2221 case PRINT_TEXT:
2222 fprintf(print_fh, "error type: invalid_pat\n");
2223 break;
2224 default:
2225 break;
2227 psi_table_free( pp_next_pat_sections );
2228 psi_table_init( pp_next_pat_sections );
2229 goto out_pat;
2232 /* Switch tables. */
2233 psi_table_copy( pp_old_pat_sections, pp_current_pat_sections );
2234 psi_table_copy( pp_current_pat_sections, pp_next_pat_sections );
2235 psi_table_init( pp_next_pat_sections );
2237 if ( !psi_table_validate( pp_old_pat_sections )
2238 || psi_table_get_tableidext( pp_current_pat_sections )
2239 != psi_table_get_tableidext( pp_old_pat_sections ) )
2241 b_change = true;
2242 UpdateTSID();
2243 /* This will trigger a universal reset of everything. */
2246 for ( i = 0; i <= i_last_section; i++ )
2248 uint8_t *p_section =
2249 psi_table_get_section( pp_current_pat_sections, i );
2250 const uint8_t *p_program;
2251 int j = 0;
2253 while ( (p_program = pat_get_program( p_section, j )) != NULL )
2255 const uint8_t *p_old_program = NULL;
2256 uint16_t i_sid = patn_get_program( p_program );
2257 uint16_t i_pid = patn_get_pid( p_program );
2258 j++;
2260 if ( i_sid == 0 )
2262 if ( i_pid != NIT_PID )
2263 msg_Warn( NULL,
2264 "NIT is carried on PID %hu which isn't DVB compliant",
2265 i_pid );
2266 continue; /* NIT */
2269 if ( !psi_table_validate( pp_old_pat_sections )
2270 || (p_old_program = pat_table_find_program(
2271 pp_old_pat_sections, i_sid )) == NULL
2272 || patn_get_pid( p_old_program ) != i_pid
2273 || b_change )
2275 sid_t *p_sid;
2277 if ( p_old_program != NULL )
2278 DeleteProgram( i_sid, patn_get_pid( p_old_program ) );
2280 SelectPMT( i_sid, i_pid );
2282 p_sid = FindSID( 0 );
2283 if ( p_sid == NULL )
2285 p_sid = malloc( sizeof(sid_t) );
2286 p_sid->p_current_pmt = NULL;
2287 i_nb_sids++;
2288 pp_sids = realloc( pp_sids, sizeof(sid_t *) * i_nb_sids );
2289 pp_sids[i_nb_sids - 1] = p_sid;
2292 p_sid->i_sid = i_sid;
2293 p_sid->i_pmt_pid = i_pid;
2295 UpdatePAT( i_sid );
2300 if ( psi_table_validate( pp_old_pat_sections ) )
2302 i_last_section = psi_table_get_lastsection( pp_old_pat_sections );
2303 for ( i = 0; i <= i_last_section; i++ )
2305 uint8_t *p_section =
2306 psi_table_get_section( pp_old_pat_sections, i );
2307 const uint8_t *p_program;
2308 int j = 0;
2310 while ( (p_program = pat_get_program( p_section, j )) != NULL )
2312 uint16_t i_sid = patn_get_program( p_program );
2313 uint16_t i_pid = patn_get_pid( p_program );
2314 j++;
2316 if ( i_sid == 0 )
2317 continue; /* NIT */
2319 if ( pat_table_find_program( pp_current_pat_sections, i_sid )
2320 == NULL )
2322 DeleteProgram( i_sid, i_pid );
2323 UpdatePAT( i_sid );
2328 psi_table_free( pp_old_pat_sections );
2331 pat_table_print( pp_current_pat_sections, msg_Dbg, NULL, PRINT_TEXT );
2332 if ( b_print_enabled )
2334 pat_table_print( pp_current_pat_sections, demux_Print, NULL,
2335 i_print_type );
2336 if ( i_print_type == PRINT_XML )
2337 fprintf(print_fh, "\n");
2340 out_pat:
2341 SendPAT( i_dts );
2344 /*****************************************************************************
2345 * HandlePATSection
2346 *****************************************************************************/
2347 static void HandlePATSection( uint16_t i_pid, uint8_t *p_section,
2348 mtime_t i_dts )
2350 if ( i_pid != PAT_PID || !pat_validate( p_section ) )
2352 msg_Warn( NULL, "invalid PAT section received on PID %hu", i_pid );
2353 switch (i_print_type) {
2354 case PRINT_XML:
2355 fprintf(print_fh, "<ERROR type=\"invalid_pat_section\"/>\n");
2356 break;
2357 case PRINT_TEXT:
2358 fprintf(print_fh, "error type: invalid_pat_section\n");
2359 break;
2360 default:
2361 break;
2363 free( p_section );
2364 return;
2367 if ( !psi_table_section( pp_next_pat_sections, p_section ) )
2368 return;
2370 HandlePAT( i_dts );
2373 /*****************************************************************************
2374 * HandleCAT
2375 *****************************************************************************/
2376 static void HandleCAT( mtime_t i_dts )
2378 PSI_TABLE_DECLARE( pp_old_cat_sections );
2379 uint8_t i_last_section = psi_table_get_lastsection( pp_next_cat_sections );
2380 uint8_t i_last_section2;
2381 uint8_t i, r;
2382 uint8_t *p_desc;
2383 int j, k;
2385 if ( psi_table_validate( pp_current_cat_sections ) &&
2386 psi_table_compare( pp_current_cat_sections, pp_next_cat_sections ) )
2388 /* Identical CAT. Shortcut. */
2389 psi_table_free( pp_next_cat_sections );
2390 psi_table_init( pp_next_cat_sections );
2391 goto out_cat;
2394 if ( !cat_table_validate( pp_next_cat_sections ) )
2396 msg_Warn( NULL, "invalid CAT received" );
2397 switch (i_print_type) {
2398 case PRINT_XML:
2399 fprintf(print_fh, "<ERROR type=\"invalid_cat\"/>\n");
2400 break;
2401 case PRINT_TEXT:
2402 fprintf(print_fh, "error type: invalid_cat\n");
2403 break;
2404 default:
2405 break;
2407 psi_table_free( pp_next_cat_sections );
2408 psi_table_init( pp_next_cat_sections );
2409 goto out_cat;
2412 /* Switch tables. */
2413 psi_table_copy( pp_old_cat_sections, pp_current_cat_sections );
2414 psi_table_copy( pp_current_cat_sections, pp_next_cat_sections );
2415 psi_table_init( pp_next_cat_sections );
2417 for ( i = 0; i <= i_last_section; i++ )
2419 uint8_t *p_section = psi_table_get_section( pp_current_cat_sections, i );
2421 j = 0;
2422 while ( (p_desc = descl_get_desc( cat_get_descl(p_section), cat_get_desclength(p_section), j++ )) != NULL )
2424 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
2425 continue;
2427 SetPID_EMM( desc09_get_pid( p_desc ) );
2431 if ( psi_table_validate( pp_old_cat_sections ) )
2433 i_last_section = psi_table_get_lastsection( pp_old_cat_sections );
2434 for ( i = 0; i <= i_last_section; i++ )
2436 uint8_t *p_old_section = psi_table_get_section( pp_old_cat_sections, i );
2437 j = 0;
2438 while ( (p_desc = descl_get_desc( cat_get_descl(p_old_section), cat_get_desclength(p_old_section), j++ )) != NULL )
2440 uint16_t emm_pid;
2441 int pid_found = 0;
2443 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
2444 continue;
2446 emm_pid = desc09_get_pid( p_desc );
2448 // Search in current sections if the pid exists
2449 i_last_section2 = psi_table_get_lastsection( pp_current_cat_sections );
2450 for ( r = 0; r <= i_last_section2; r++ )
2452 uint8_t *p_section = psi_table_get_section( pp_current_cat_sections, r );
2454 k = 0;
2455 while ( (p_desc = descl_get_desc( cat_get_descl(p_section), cat_get_desclength(p_section), k++ )) != NULL )
2457 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
2458 continue;
2459 if ( ca_desc_find( cat_get_descl(p_section), cat_get_desclength(p_section), emm_pid ) != NULL )
2461 pid_found = 1;
2462 break;
2467 if ( !pid_found )
2468 UnsetPID(emm_pid);
2472 psi_table_free( pp_old_cat_sections );
2475 cat_table_print( pp_current_cat_sections, msg_Dbg, NULL, PRINT_TEXT );
2476 if ( b_print_enabled )
2478 cat_table_print( pp_current_cat_sections, demux_Print, NULL,
2479 i_print_type );
2480 if ( i_print_type == PRINT_XML )
2481 fprintf(print_fh, "\n");
2484 out_cat:
2485 return;
2488 /*****************************************************************************
2489 * HandleCATSection
2490 *****************************************************************************/
2491 static void HandleCATSection( uint16_t i_pid, uint8_t *p_section,
2492 mtime_t i_dts )
2494 if ( i_pid != CAT_PID || !cat_validate( p_section ) )
2496 msg_Warn( NULL, "invalid CAT section received on PID %hu", i_pid );
2497 switch (i_print_type) {
2498 case PRINT_XML:
2499 fprintf(print_fh, "<ERROR type=\"invalid_cat_section\"/>\n");
2500 break;
2501 case PRINT_TEXT:
2502 fprintf(print_fh, "error type: invalid_cat_section\n");
2503 break;
2504 default:
2505 break;
2507 free( p_section );
2508 return;
2511 if ( !psi_table_section( pp_next_cat_sections, p_section ) )
2512 return;
2514 HandleCAT( i_dts );
2517 static void mark_pmt_pids( uint8_t *p_pmt, uint8_t pid_map[], uint8_t marker )
2519 uint16_t j, k;
2520 uint8_t *p_es;
2521 uint8_t *p_desc;
2523 uint16_t i_pcr_pid = pmt_get_pcrpid( p_pmt );
2525 if ( b_enable_ecm )
2527 j = 0;
2528 while ( (p_desc = descs_get_desc( pmt_get_descs( p_pmt ), j++ )) != NULL )
2530 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
2531 continue;
2532 pid_map[ desc09_get_pid( p_desc ) ] |= marker;
2536 if ( i_pcr_pid != PADDING_PID )
2537 pid_map[ i_pcr_pid ] |= marker;
2539 j = 0;
2540 while ( (p_es = pmt_get_es( p_pmt, j )) != NULL )
2542 uint16_t i_pid = pmtn_get_pid( p_es );
2543 j++;
2545 if ( PIDWouldBeSelected( p_es ) )
2546 pid_map[ i_pid ] |= marker;
2548 p_pids[i_pid].b_pes = PIDCarriesPES( p_es );
2550 if ( b_enable_ecm )
2552 k = 0;
2553 while ( (p_desc = descs_get_desc( pmtn_get_descs( p_es ), k++ )) != NULL )
2555 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
2556 continue;
2557 pid_map[ desc09_get_pid( p_desc ) ] |= marker;
2563 /*****************************************************************************
2564 * HandlePMT
2565 *****************************************************************************/
2566 static void HandlePMT( uint16_t i_pid, uint8_t *p_pmt, mtime_t i_dts )
2568 uint16_t i_sid = pmt_get_program( p_pmt );
2569 sid_t *p_sid;
2570 bool b_needs_descrambling, b_needed_descrambling, b_is_selected;
2571 uint8_t pid_map[MAX_PIDS];
2573 p_sid = FindSID( i_sid );
2574 if ( p_sid == NULL )
2576 /* Unwanted SID (happens when the same PMT PID is used for several
2577 * programs). */
2578 free( p_pmt );
2579 return;
2582 if ( i_pid != p_sid->i_pmt_pid )
2584 msg_Warn( NULL, "invalid PMT section received on PID %hu", i_pid );
2585 switch (i_print_type) {
2586 case PRINT_XML:
2587 fprintf(print_fh, "<ERROR type=\"ghost_pmt\" program=\"%hu\n pid=\"%hu\"/>\n",
2588 i_sid, i_pid);
2589 break;
2590 case PRINT_TEXT:
2591 fprintf(print_fh, "error type: ghost_pmt program: %hu pid: %hu\n",
2592 i_sid, i_pid);
2593 break;
2594 default:
2595 break;
2597 free( p_pmt );
2598 return;
2601 if ( p_sid->p_current_pmt != NULL &&
2602 psi_compare( p_sid->p_current_pmt, p_pmt ) )
2604 /* Identical PMT. Shortcut. */
2605 free( p_pmt );
2606 goto out_pmt;
2609 if ( !pmt_validate( p_pmt ) )
2611 msg_Warn( NULL, "invalid PMT section received on PID %hu", i_pid );
2612 switch (i_print_type) {
2613 case PRINT_XML:
2614 fprintf(print_fh, "<ERROR type=\"invalid_pmt_section\" pid=\"%hu\"/>\n",
2615 i_pid);
2616 break;
2617 case PRINT_TEXT:
2618 fprintf(print_fh, "error type: invalid_pmt_section pid: %hu\n",
2619 i_pid);
2620 break;
2621 default:
2622 break;
2624 free( p_pmt );
2625 goto out_pmt;
2628 memset( pid_map, 0, sizeof(pid_map) );
2630 b_needs_descrambling = PMTNeedsDescrambling( p_pmt );
2631 b_needed_descrambling = p_sid->p_current_pmt != NULL ?
2632 PMTNeedsDescrambling( p_sid->p_current_pmt ) :
2633 false;
2634 b_is_selected = SIDIsSelected( i_sid );
2636 if ( i_ca_handle && b_is_selected &&
2637 !b_needs_descrambling && b_needed_descrambling )
2638 en50221_DeletePMT( p_sid->p_current_pmt );
2640 if ( p_sid->p_current_pmt != NULL )
2642 mark_pmt_pids( p_sid->p_current_pmt, pid_map, 0x02 );
2643 free( p_sid->p_current_pmt );
2646 mark_pmt_pids( p_pmt, pid_map, 0x01 );
2648 /* Start to stream PIDs */
2649 int pid;
2650 for ( pid = 0; pid < MAX_PIDS; pid++ )
2652 /* The pid does not exist in the old PMT and in the new PMT. Ignore this pid. */
2653 if ( !pid_map[ pid ] )
2654 continue;
2656 switch ( pid_map[ pid ] & 0x03 ) {
2657 case 0x03: /* The pid exists in the old PMT and in the new PMT. The pid was already selected in case 0x01. */
2658 continue;
2659 case 0x02: /* The pid does not exist in the new PMT but exists in the old PMT. Unselect it. */
2660 UnselectPID( i_sid, pid );
2661 break;
2662 case 0x01: /* The pid exists in new PMT. Select it. */
2663 SelectPID( i_sid, pid );
2664 break;
2668 p_sid->p_current_pmt = p_pmt;
2670 if ( i_ca_handle && b_is_selected )
2672 if ( b_needs_descrambling && !b_needed_descrambling )
2673 en50221_AddPMT( p_pmt );
2674 else if ( b_needs_descrambling && b_needed_descrambling )
2675 en50221_UpdatePMT( p_pmt );
2678 UpdatePMT( i_sid );
2680 pmt_print( p_pmt, msg_Dbg, NULL, demux_Iconv, NULL, PRINT_TEXT );
2681 if ( b_print_enabled )
2683 pmt_print( p_pmt, demux_Print, NULL, demux_Iconv, NULL,
2684 i_print_type );
2685 if ( i_print_type == PRINT_XML )
2686 fprintf(print_fh, "\n");
2689 out_pmt:
2690 SendPMT( p_sid, i_dts );
2693 /*****************************************************************************
2694 * HandleNIT
2695 *****************************************************************************/
2696 static void HandleNIT( mtime_t i_dts )
2698 if ( psi_table_validate( pp_current_nit_sections ) &&
2699 psi_table_compare( pp_current_nit_sections, pp_next_nit_sections ) )
2701 /* Identical NIT. Shortcut. */
2702 psi_table_free( pp_next_nit_sections );
2703 psi_table_init( pp_next_nit_sections );
2704 goto out_nit;
2707 if ( !nit_table_validate( pp_next_nit_sections ) )
2709 msg_Warn( NULL, "invalid NIT received" );
2710 switch (i_print_type) {
2711 case PRINT_XML:
2712 fprintf(print_fh, "<ERROR type=\"invalid_nit\"/>\n");
2713 break;
2714 case PRINT_TEXT:
2715 fprintf(print_fh, "error type: invalid_nit\n");
2716 break;
2717 default:
2718 break;
2720 psi_table_free( pp_next_nit_sections );
2721 psi_table_init( pp_next_nit_sections );
2722 goto out_nit;
2725 /* Switch tables. */
2726 psi_table_free( pp_current_nit_sections );
2727 psi_table_copy( pp_current_nit_sections, pp_next_nit_sections );
2728 psi_table_init( pp_next_nit_sections );
2730 nit_table_print( pp_current_nit_sections, msg_Dbg, NULL,
2731 demux_Iconv, NULL, PRINT_TEXT );
2732 if ( b_print_enabled )
2734 nit_table_print( pp_current_nit_sections, demux_Print, NULL,
2735 demux_Iconv, NULL, i_print_type );
2736 if ( i_print_type == PRINT_XML )
2737 fprintf(print_fh, "\n");
2740 out_nit:
2744 /*****************************************************************************
2745 * HandleNITSection
2746 *****************************************************************************/
2747 static void HandleNITSection( uint16_t i_pid, uint8_t *p_section,
2748 mtime_t i_dts )
2750 if ( i_pid != NIT_PID || !nit_validate( p_section ) )
2752 msg_Warn( NULL, "invalid NIT section received on PID %hu", i_pid );
2753 switch (i_print_type) {
2754 case PRINT_XML:
2755 fprintf(print_fh, "<ERROR type=\"invalid_nit_section\" pid=\"%hu\"/>\n",
2756 i_pid);
2757 break;
2758 case PRINT_TEXT:
2759 fprintf(print_fh, "error type: invalid_nit_section pid: %hu\n",
2760 i_pid);
2761 break;
2762 default:
2763 break;
2765 free( p_section );
2766 return;
2769 if ( psi_table_section( pp_next_nit_sections, p_section ) )
2770 HandleNIT( i_dts );
2772 /* This case is different because DVB specifies a minimum bitrate for
2773 * PID 0x10, even if we don't have any thing to send (for cheap
2774 * transport over network boundaries). */
2775 SendNIT( i_dts );
2779 /*****************************************************************************
2780 * HandleSDT
2781 *****************************************************************************/
2782 static void HandleSDT( mtime_t i_dts )
2784 PSI_TABLE_DECLARE( pp_old_sdt_sections );
2785 uint8_t i_last_section = psi_table_get_lastsection( pp_next_sdt_sections );
2786 uint8_t i;
2787 int j;
2789 if ( psi_table_validate( pp_current_sdt_sections ) &&
2790 psi_table_compare( pp_current_sdt_sections, pp_next_sdt_sections ) )
2792 /* Identical SDT. Shortcut. */
2793 psi_table_free( pp_next_sdt_sections );
2794 psi_table_init( pp_next_sdt_sections );
2795 goto out_sdt;
2798 if ( !sdt_table_validate( pp_next_sdt_sections ) )
2800 msg_Warn( NULL, "invalid SDT received" );
2801 switch (i_print_type) {
2802 case PRINT_XML:
2803 fprintf(print_fh, "<ERROR type=\"invalid_sdt\"/>\n");
2804 break;
2805 case PRINT_TEXT:
2806 fprintf(print_fh, "error type: invalid_sdt\n");
2807 break;
2808 default:
2809 break;
2811 psi_table_free( pp_next_sdt_sections );
2812 psi_table_init( pp_next_sdt_sections );
2813 goto out_sdt;
2816 /* Switch tables. */
2817 psi_table_copy( pp_old_sdt_sections, pp_current_sdt_sections );
2818 psi_table_copy( pp_current_sdt_sections, pp_next_sdt_sections );
2819 psi_table_init( pp_next_sdt_sections );
2821 for ( i = 0; i <= i_last_section; i++ )
2823 uint8_t *p_section =
2824 psi_table_get_section( pp_current_sdt_sections, i );
2825 uint8_t *p_service;
2826 j = 0;
2828 while ( (p_service = sdt_get_service( p_section, j )) != NULL )
2830 uint16_t i_sid = sdtn_get_sid( p_service );
2831 j++;
2833 UpdateSDT( i_sid );
2837 if ( psi_table_validate( pp_old_sdt_sections ) )
2839 i_last_section = psi_table_get_lastsection( pp_old_sdt_sections );
2840 for ( i = 0; i <= i_last_section; i++ )
2842 uint8_t *p_section =
2843 psi_table_get_section( pp_old_sdt_sections, i );
2844 const uint8_t *p_service;
2845 int j = 0;
2847 while ( (p_service = sdt_get_service( p_section, j )) != NULL )
2849 uint16_t i_sid = sdtn_get_sid( p_service );
2850 j++;
2852 if ( sdt_table_find_service( pp_current_sdt_sections, i_sid )
2853 == NULL )
2854 UpdateSDT( i_sid );
2858 psi_table_free( pp_old_sdt_sections );
2861 sdt_table_print( pp_current_sdt_sections, msg_Dbg, NULL,
2862 demux_Iconv, NULL, PRINT_TEXT );
2863 if ( b_print_enabled )
2865 sdt_table_print( pp_current_sdt_sections, demux_Print, NULL,
2866 demux_Iconv, NULL, i_print_type );
2867 if ( i_print_type == PRINT_XML )
2868 fprintf(print_fh, "\n");
2871 out_sdt:
2872 SendSDT( i_dts );
2875 /*****************************************************************************
2876 * HandleSDTSection
2877 *****************************************************************************/
2878 static void HandleSDTSection( uint16_t i_pid, uint8_t *p_section,
2879 mtime_t i_dts )
2881 if ( i_pid != SDT_PID || !sdt_validate( p_section ) )
2883 msg_Warn( NULL, "invalid SDT section received on PID %hu", i_pid );
2884 switch (i_print_type) {
2885 case PRINT_XML:
2886 fprintf(print_fh, "<ERROR type=\"invalid_sdt_section\" pid=\"%hu\"/>\n",
2887 i_pid);
2888 break;
2889 case PRINT_TEXT:
2890 fprintf(print_fh, "error type: invalid_sdt_section pid: %hu\n",
2891 i_pid);
2892 break;
2893 default:
2894 break;
2896 free( p_section );
2897 return;
2900 if ( !psi_table_section( pp_next_sdt_sections, p_section ) )
2901 return;
2903 HandleSDT( i_dts );
2906 /*****************************************************************************
2907 * HandleEITSection
2908 *****************************************************************************/
2909 static void HandleEIT( uint16_t i_pid, uint8_t *p_eit, mtime_t i_dts )
2911 uint16_t i_sid = eit_get_sid( p_eit );
2912 sid_t *p_sid;
2914 p_sid = FindSID( i_sid );
2915 if ( p_sid == NULL )
2917 /* Not a selected program. */
2918 free( p_eit );
2919 return;
2922 if ( i_pid != EIT_PID || !eit_validate( p_eit ) )
2924 msg_Warn( NULL, "invalid EIT section received on PID %hu", i_pid );
2925 switch (i_print_type) {
2926 case PRINT_XML:
2927 fprintf(print_fh, "<ERROR type=\"invalid_eit_section\" pid=\"%hu\"/>\n",
2928 i_pid);
2929 break;
2930 case PRINT_TEXT:
2931 fprintf(print_fh, "error type: invalid_eit_section pid: %hu\n",
2932 i_pid);
2933 break;
2934 default:
2935 break;
2937 free( p_eit );
2938 return;
2941 SendEIT( p_sid, i_dts, p_eit );
2942 free( p_eit );
2945 /*****************************************************************************
2946 * HandleSection
2947 *****************************************************************************/
2948 static void HandleSection( uint16_t i_pid, uint8_t *p_section, mtime_t i_dts )
2950 uint8_t i_table_id = psi_get_tableid( p_section );
2952 if ( !psi_validate( p_section ) )
2954 msg_Warn( NULL, "invalid section on PID %hu", i_pid );
2955 switch (i_print_type) {
2956 case PRINT_XML:
2957 fprintf(print_fh, "<ERROR type=\"invalid_section\" pid=\"%hu\"/>\n", i_pid);
2958 break;
2959 case PRINT_TEXT:
2960 fprintf(print_fh, "error type: invalid_section pid: %hu\n", i_pid);
2961 break;
2962 default:
2963 break;
2965 free( p_section );
2966 return;
2969 if ( !psi_get_current( p_section ) )
2971 /* Ignore sections which are not in use yet. */
2972 free( p_section );
2973 return;
2976 switch ( i_table_id )
2978 case PAT_TABLE_ID:
2979 HandlePATSection( i_pid, p_section, i_dts );
2980 break;
2982 case CAT_TABLE_ID:
2983 if ( b_enable_emm )
2984 HandleCATSection( i_pid, p_section, i_dts );
2985 break;
2987 case PMT_TABLE_ID:
2988 HandlePMT( i_pid, p_section, i_dts );
2989 break;
2991 case NIT_TABLE_ID_ACTUAL:
2992 HandleNITSection( i_pid, p_section, i_dts );
2993 break;
2995 case SDT_TABLE_ID_ACTUAL:
2996 HandleSDTSection( i_pid, p_section, i_dts );
2997 break;
2999 default:
3000 if ( i_table_id == EIT_TABLE_ID_PF_ACTUAL ||
3001 (i_table_id >= EIT_TABLE_ID_SCHED_ACTUAL_FIRST &&
3002 i_table_id <= EIT_TABLE_ID_SCHED_ACTUAL_LAST) )
3004 HandleEIT( i_pid, p_section, i_dts );
3005 break;
3007 free( p_section );
3008 break;
3012 /*****************************************************************************
3013 * HandlePSIPacket
3014 *****************************************************************************/
3015 static void HandlePSIPacket( uint8_t *p_ts, mtime_t i_dts )
3017 uint16_t i_pid = ts_get_pid( p_ts );
3018 ts_pid_t *p_pid = &p_pids[i_pid];
3019 uint8_t i_cc = ts_get_cc( p_ts );
3020 const uint8_t *p_payload;
3021 uint8_t i_length;
3023 if ( ts_check_duplicate( i_cc, p_pid->i_last_cc )
3024 || !ts_has_payload( p_ts ) )
3025 return;
3027 if ( p_pid->i_last_cc != -1
3028 && ts_check_discontinuity( i_cc, p_pid->i_last_cc ) )
3029 psi_assemble_reset( &p_pid->p_psi_buffer, &p_pid->i_psi_buffer_used );
3031 p_payload = ts_section( p_ts );
3032 i_length = p_ts + TS_SIZE - p_payload;
3034 if ( !psi_assemble_empty( &p_pid->p_psi_buffer,
3035 &p_pid->i_psi_buffer_used ) )
3037 uint8_t *p_section = psi_assemble_payload( &p_pid->p_psi_buffer,
3038 &p_pid->i_psi_buffer_used,
3039 &p_payload, &i_length );
3040 if ( p_section != NULL )
3041 HandleSection( i_pid, p_section, i_dts );
3044 p_payload = ts_next_section( p_ts );
3045 i_length = p_ts + TS_SIZE - p_payload;
3047 while ( i_length )
3049 uint8_t *p_section = psi_assemble_payload( &p_pid->p_psi_buffer,
3050 &p_pid->i_psi_buffer_used,
3051 &p_payload, &i_length );
3052 if ( p_section != NULL )
3053 HandleSection( i_pid, p_section, i_dts );
3057 /*****************************************************************************
3058 * PID info functions
3059 *****************************************************************************/
3060 static const char *h222_stream_type_desc(uint8_t i_stream_type) {
3061 /* See ISO/IEC 13818-1 : 2000 (E) | Table 2-29 - Stream type assignments, Page 66 (48) */
3062 if (i_stream_type == 0)
3063 return "Reserved stream";
3064 switch (i_stream_type) {
3065 case 0x01: return "11172-2 video (MPEG-1)";
3066 case 0x02: return "H.262/13818-2 video (MPEG-2) or 11172-2 constrained video";
3067 case 0x03: return "11172-3 audio (MPEG-1)";
3068 case 0x04: return "13818-3 audio (MPEG-2)";
3069 case 0x05: return "H.222.0/13818-1 private sections";
3070 case 0x06: return "H.222.0/13818-1 PES private data";
3071 case 0x07: return "13522 MHEG";
3072 case 0x08: return "H.222.0/13818-1 Annex A - DSM CC";
3073 case 0x09: return "H.222.1";
3074 case 0x0A: return "13818-6 type A";
3075 case 0x0B: return "13818-6 type B";
3076 case 0x0C: return "13818-6 type C";
3077 case 0x0D: return "13818-6 type D";
3078 case 0x0E: return "H.222.0/13818-1 auxiliary";
3079 case 0x0F: return "13818-7 Audio with ADTS transport syntax";
3080 case 0x10: return "14496-2 Visual (MPEG-4 part 2 video)";
3081 case 0x11: return "14496-3 Audio with LATM transport syntax (14496-3/AMD 1)";
3082 case 0x12: return "14496-1 SL-packetized or FlexMux stream in PES packets";
3083 case 0x13: return "14496-1 SL-packetized or FlexMux stream in 14496 sections";
3084 case 0x14: return "ISO/IEC 13818-6 Synchronized Download Protocol";
3085 case 0x15: return "Metadata in PES packets";
3086 case 0x16: return "Metadata in metadata_sections";
3087 case 0x17: return "Metadata in 13818-6 Data Carousel";
3088 case 0x18: return "Metadata in 13818-6 Object Carousel";
3089 case 0x19: return "Metadata in 13818-6 Synchronized Download Protocol";
3090 case 0x1A: return "13818-11 MPEG-2 IPMP stream";
3091 case 0x1B: return "H.264/14496-10 video (MPEG-4/AVC)";
3092 case 0x42: return "AVS Video";
3093 case 0x7F: return "IPMP stream";
3094 default : return "Unknown stream";
3098 static const char *get_pid_desc(uint16_t i_pid, uint16_t *i_sid) {
3099 int i, j, k;
3100 uint8_t i_last_section;
3101 uint8_t *p_desc;
3102 uint16_t i_nit_pid = NIT_PID, i_pcr_pid = 0;
3104 /* Simple cases */
3105 switch (i_pid)
3107 case 0x00: return "PAT";
3108 case 0x01: return "CAT";
3109 case 0x11: return "SDT";
3110 case 0x12: return "EPG";
3111 case 0x14: return "TDT/TOT";
3114 /* Detect NIT pid */
3115 if ( psi_table_validate( pp_current_pat_sections ) )
3117 i_last_section = psi_table_get_lastsection( pp_current_pat_sections );
3118 for ( i = 0; i <= i_last_section; i++ )
3120 uint8_t *p_section = psi_table_get_section( pp_current_pat_sections, i );
3121 uint8_t *p_program;
3123 j = 0;
3124 while ( (p_program = pat_get_program( p_section, j++ )) != NULL )
3126 /* Programs with PID == 0 are actually NIT */
3127 if ( patn_get_program( p_program ) == 0 )
3129 i_nit_pid = patn_get_pid( p_program );
3130 break;
3136 /* Detect EMM pids */
3137 if ( b_enable_emm && psi_table_validate( pp_current_cat_sections ) )
3139 i_last_section = psi_table_get_lastsection( pp_current_cat_sections );
3140 for ( i = 0; i <= i_last_section; i++ )
3142 uint8_t *p_section = psi_table_get_section( pp_current_cat_sections, i );
3144 j = 0;
3145 while ( (p_desc = descl_get_desc( cat_get_descl(p_section), cat_get_desclength(p_section), j++ )) != NULL )
3147 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
3148 continue;
3150 if ( desc09_get_pid( p_desc ) == i_pid ) {
3151 return "EMM";
3157 /* Detect streams in PMT */
3158 for ( k = 0; k < i_nb_sids; k++ )
3160 sid_t *p_sid = pp_sids[k];
3161 if ( p_sid->i_pmt_pid == i_pid )
3163 if ( i_sid )
3164 *i_sid = p_sid->i_sid;
3165 return "PMT";
3168 if ( p_sid->i_sid && p_sid->p_current_pmt != NULL )
3170 uint8_t *p_current_pmt = p_sid->p_current_pmt;
3171 uint8_t *p_current_es;
3173 /* The PCR PID can be alone or PCR can be carried in some other PIDs (mostly video)
3174 so just remember the pid and if it is alone it will be reported as PCR, otherwise
3175 stream type of the PID will be reported */
3176 if ( i_pid == pmt_get_pcrpid( p_current_pmt ) ) {
3177 if ( i_sid )
3178 *i_sid = p_sid->i_sid;
3179 i_pcr_pid = pmt_get_pcrpid( p_current_pmt );
3182 /* Look for ECMs */
3183 j = 0;
3184 while ((p_desc = descs_get_desc( pmt_get_descs( p_current_pmt ), j++ )) != NULL)
3186 if ( desc_get_tag( p_desc ) != 0x09 || !desc09_validate( p_desc ) )
3187 continue;
3189 if ( desc09_get_pid( p_desc ) == i_pid ) {
3190 if ( i_sid )
3191 *i_sid = p_sid->i_sid;
3192 return "ECM";
3196 /* Detect stream types */
3197 j = 0;
3198 while ( (p_current_es = pmt_get_es( p_current_pmt, j++ )) != NULL )
3200 if ( pmtn_get_pid( p_current_es ) == i_pid )
3202 if ( i_sid )
3203 *i_sid = p_sid->i_sid;
3204 return h222_stream_type_desc( pmtn_get_streamtype( p_current_es ) );
3210 /* Are there any other PIDs? */
3211 if (i_pid == i_nit_pid)
3212 return "NIT";
3214 if (i_pid == i_pcr_pid)
3215 return "PCR";
3217 return "...";
3220 /*****************************************************************************
3221 * Functions that return packed sections
3222 *****************************************************************************/
3223 uint8_t *demux_get_current_packed_PAT( unsigned int *pi_pack_size ) {
3224 return psi_pack_sections( pp_current_pat_sections, pi_pack_size );
3227 uint8_t *demux_get_current_packed_CAT( unsigned int *pi_pack_size ) {
3228 return psi_pack_sections( pp_current_cat_sections, pi_pack_size );
3231 uint8_t *demux_get_current_packed_NIT( unsigned int *pi_pack_size ) {
3232 return psi_pack_sections( pp_current_nit_sections, pi_pack_size );
3235 uint8_t *demux_get_current_packed_SDT( unsigned int *pi_pack_size ) {
3236 return psi_pack_sections( pp_current_sdt_sections, pi_pack_size );
3239 uint8_t *demux_get_packed_PMT( uint16_t i_sid, unsigned int *pi_pack_size ) {
3240 sid_t *p_sid = FindSID( i_sid );
3241 if ( p_sid != NULL && p_sid->p_current_pmt && pmt_validate( p_sid->p_current_pmt ) )
3242 return psi_pack_section( p_sid->p_current_pmt, pi_pack_size );
3243 return NULL;
3246 inline void demux_get_PID_info( uint16_t i_pid, uint8_t *p_data ) {
3247 ts_pid_info_t *p_info = (ts_pid_info_t *)p_data;
3248 *p_info = p_pids[i_pid].info;
3251 inline void demux_get_PIDS_info( uint8_t *p_data ) {
3252 int i_pid;
3253 for (i_pid = 0; i_pid < MAX_PIDS; i_pid++ )
3254 demux_get_PID_info( i_pid, p_data + ( i_pid * sizeof(ts_pid_info_t) ) );