1 /*****************************************************************************
2 * mpeg_ts.c : Transport Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: mpeg_ts.c,v 1.6 2002/03/15 17:17:35 sam Exp $
7 * Authors: Henri Fallon <henri@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <videolan/vlc.h>
33 #include "stream_control.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-dec.h"
36 #include "input_ext-plugins.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 #define TS_READ_ONCE 200
43 /*****************************************************************************
45 *****************************************************************************/
46 static void input_getfunctions( function_list_t * p_function_list );
47 static int TSInit ( struct input_thread_s * );
48 static void TSEnd ( struct input_thread_s * );
49 static int TSDemux ( struct input_thread_s * );
51 /*****************************************************************************
52 * Build configuration tree.
53 *****************************************************************************/
58 SET_DESCRIPTION( "ISO 13818-1 MPEG Transport Stream input" )
59 ADD_CAPABILITY( DEMUX, 160 )
64 input_getfunctions( &p_module->p_functions->demux );
67 MODULE_DEACTIVATE_START
68 MODULE_DEACTIVATE_STOP
70 /*****************************************************************************
71 * Functions exported as capabilities. They are declared as static so that
72 * we don't pollute the namespace too much.
73 *****************************************************************************/
74 static void input_getfunctions( function_list_t * p_function_list )
76 #define input p_function_list->functions.demux
77 input.pf_init = TSInit;
79 input.pf_demux = TSDemux;
80 input.pf_rewind = NULL;
84 /*****************************************************************************
85 * TSInit: initializes TS structures
86 *****************************************************************************/
87 static int TSInit( input_thread_t * p_input )
89 es_descriptor_t * p_pat_es;
90 es_ts_data_t * p_demux_data;
91 stream_ts_data_t * p_stream_data;
94 /* Initialize access plug-in structures. */
95 if( p_input->i_mtu == 0 )
98 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
101 /* Have a peep at the show. */
102 if( input_Peek( p_input, &p_peek, 1 ) < 1 )
104 intf_ErrMsg( "input error: cannot peek() (mpeg_ts)" );
108 if( *p_peek != TS_SYNC_CODE )
110 if( *p_input->psz_demux && strncmp( p_input->psz_demux, "ts", 3 ) )
113 intf_ErrMsg( "input error: this doesn't seem like a TS stream, continuing" );
117 intf_WarnMsg( 2, "input: TS plug-in discarded (no sync)" );
122 /* Adapt the bufsize for our only use. */
123 if( p_input->i_mtu != 0 )
125 /* Have minimum granularity to avoid bottlenecks at the input level. */
126 p_input->i_bufsize = (p_input->i_mtu / TS_PACKET_SIZE) * TS_PACKET_SIZE;
129 if( input_InitStream( p_input, sizeof( stream_ts_data_t ) ) == -1 )
134 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
135 p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
137 /* We'll have to catch the PAT in order to continue
138 * Then the input will catch the PMT and then the others ES
139 * The PAT es is indepedent of any program. */
140 p_pat_es = input_AddES( p_input, NULL,
141 0x00, sizeof( es_ts_data_t ) );
142 p_demux_data = (es_ts_data_t *)p_pat_es->p_demux_data;
143 p_demux_data->b_psi = 1;
144 p_demux_data->i_psi_type = PSI_IS_PAT;
145 p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
146 p_demux_data->p_psi_section->b_is_complete = 1;
151 /*****************************************************************************
152 * TSEnd: frees unused data
153 *****************************************************************************/
154 static void TSEnd( input_thread_t * p_input )
158 /*****************************************************************************
159 * TSDemux: reads and demuxes data packets
160 *****************************************************************************
161 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
163 *****************************************************************************/
164 static int TSDemux( input_thread_t * p_input )
166 int i_read_once = (p_input->i_mtu ?
167 p_input->i_bufsize / TS_PACKET_SIZE :
171 for( i = 0; i < i_read_once; i++ )
173 data_packet_t * p_data;
176 i_result = input_ReadTS( p_input, &p_data );
183 input_DemuxTS( p_input, p_data );
186 return( i_read_once );