1 /*****************************************************************************
2 * input_ts.c: TS demux and netlist management
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
5 * $Id: input_ts.c,v 1.14 2001/04/13 05:36:12 stef Exp $
7 * Authors: Henri Fallon <henri@videolan.org>
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 #define MODULE_NAME ts
25 #include "modules_inner.h"
27 /*****************************************************************************
29 *****************************************************************************/
36 #include <sys/types.h>
39 #include <sys/select.h>
55 #include "stream_control.h"
56 #include "input_ext-intf.h"
57 #include "input_ext-dec.h"
62 #include "mpeg_system.h"
63 #include "input_netlist.h"
65 /*****************************************************************************
67 *****************************************************************************/
68 static int TSProbe ( probedata_t * );
69 static int TSRead ( struct input_thread_s *,
70 data_packet_t * p_packets[INPUT_READ_ONCE] );
71 static void TSInit ( struct input_thread_s * );
72 static void TSEnd ( struct input_thread_s * );
74 /*****************************************************************************
75 * Functions exported as capabilities. They are declared as static so that
76 * we don't pollute the namespace too much.
77 *****************************************************************************/
78 void _M( input_getfunctions )( function_list_t * p_function_list )
80 #define input p_function_list->functions.input
81 p_function_list->pf_probe = TSProbe;
82 input.pf_init = TSInit;
83 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
84 input.pf_open = input_NetworkOpen;
85 input.pf_close = input_NetworkClose;
87 input.pf_open = input_FileOpen;
88 input.pf_close = input_FileClose;
91 input.pf_set_area = NULL;
92 input.pf_read = TSRead;
93 input.pf_demux = input_DemuxTS;
94 input.pf_new_packet = input_NetlistNewPacket;
95 input.pf_new_pes = input_NetlistNewPES;
96 input.pf_delete_packet = input_NetlistDeletePacket;
97 input.pf_delete_pes = input_NetlistDeletePES;
98 input.pf_rewind = NULL;
103 /*****************************************************************************
104 * TSProbe: verifies that the stream is a TS stream
105 *****************************************************************************/
106 static int TSProbe( probedata_t * p_data )
108 input_thread_t * p_input = (input_thread_t *)p_data;
110 char * psz_name = p_input->p_source;
114 if( TestMethod( INPUT_METHOD_VAR, "ts" ) )
119 if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
121 /* If the user specified "file:" then it's probably a file */
125 i_handle = open( psz_name, 0 );
135 /*****************************************************************************
136 * TSInit: initializes TS structures
137 *****************************************************************************/
138 static void TSInit( input_thread_t * p_input )
140 /* Initialize netlist and TS structures */
141 thread_ts_data_t * p_method;
142 es_descriptor_t * p_pat_es;
143 es_ts_data_t * p_demux_data;
144 stream_ts_data_t * p_stream_data;
146 /* Initialise structure */
147 p_method = malloc( sizeof( thread_ts_data_t ) );
148 if( p_method == NULL )
150 intf_ErrMsg( "TS input : Out of memory" );
151 p_input->b_error = 1;
155 p_input->p_plugin_data = (void *)p_method;
156 p_input->p_method_data = NULL;
159 /* Initialize netlist */
160 if( input_NetlistInit( p_input, NB_DATA, NB_PES, TS_PACKET_SIZE,
163 intf_ErrMsg( "TS input : Could not initialize netlist" );
167 /* Initialize the stream */
168 input_InitStream( p_input, sizeof( stream_ts_data_t ) );
170 /* input method type */
171 /* FIXME: should test if you have network or file here */
172 p_input->stream.i_method = INPUT_METHOD_NETWORK;
175 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
176 p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
178 /* We'll have to catch the PAT in order to continue
179 * Then the input will catch the PMT and then the others ES
180 * The PAT es is indepedent of any program. */
181 p_pat_es = input_AddES( p_input, NULL,
182 0x00, sizeof( es_ts_data_t ) );
183 p_demux_data=(es_ts_data_t *)p_pat_es->p_demux_data;
184 p_demux_data->b_psi = 1;
185 p_demux_data->i_psi_type = PSI_IS_PAT;
186 p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
187 p_demux_data->p_psi_section->b_is_complete = 1;
191 /*****************************************************************************
192 * TSEnd: frees unused data
193 *****************************************************************************/
194 static void TSEnd( input_thread_t * p_input )
196 es_descriptor_t * p_pat_es;
198 p_pat_es = input_FindES( p_input, 0x00 );
200 if( p_pat_es != NULL )
201 input_DelES( p_input, p_pat_es );
202 free(p_input->p_plugin_data);
205 /*****************************************************************************
206 * TSRead: reads data packets
207 *****************************************************************************
208 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
210 *****************************************************************************/
211 static int TSRead( input_thread_t * p_input,
212 data_packet_t * pp_packets[INPUT_READ_ONCE] )
214 thread_ts_data_t * p_method;
215 unsigned int i_read, i_loop;
217 struct iovec * p_iovec;
218 struct timeval s_wait;
222 p_method = ( thread_ts_data_t * )p_input->p_plugin_data;
224 /* Initialize file descriptor set */
225 FD_ZERO( &(p_method->s_fdset) );
226 FD_SET( p_input->i_handle, &(p_method->s_fdset) );
229 /* We'll wait 0.5 second if nothing happens */
233 /* Reset pointer table */
234 memset( pp_packets, 0, INPUT_READ_ONCE*sizeof(data_packet_t *) );
237 p_iovec = input_NetlistGetiovec( p_input->p_method_data );
239 if ( p_iovec == NULL )
241 return( -1 ); /* empty netlist */
244 /* Fill if some data is available */
245 i_data = select(p_input->i_handle + 1, &(p_method->s_fdset), NULL, NULL,
250 intf_ErrMsg( "TS input : Error in select : %s", strerror(errno) );
256 i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
260 intf_ErrMsg( "TS input : Could not readv" );
264 input_NetlistMviovec( p_input->p_method_data,
265 (int)(i_read/TS_PACKET_SIZE) , pp_packets );
267 /* check correct TS header */
268 for( i_loop=0; i_loop * TS_PACKET_SIZE < i_read; i_loop++ )
270 if( pp_packets[i_loop]->p_buffer[0] != 0x47 )
271 intf_ErrMsg( "TS input : Bad TS Packet (starcode != 0x47)." );