1 /*****************************************************************************
2 * input_ts.c: TS demux and netlist management
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: input_ts.c,v 1.41 2001/12/07 16:47:47 jobi 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 #ifdef STRNCASECMP_IN_STRINGS_H
40 #include <sys/types.h>
42 #if !defined( _MSC_VER )
43 # include <sys/time.h>
47 # include <sys/select.h>
60 # include <winsock2.h>
62 # include <sys/uio.h> /* struct iovec */
73 # include "input_iovec.h"
77 #include "modules_export.h"
79 #include "stream_control.h"
80 #include "input_ext-intf.h"
81 #include "input_ext-dec.h"
82 #include "input_ext-plugins.h"
86 /*****************************************************************************
88 *****************************************************************************/
89 static int TSProbe ( probedata_t * );
90 static void TSInit ( struct input_thread_s * );
91 static void TSEnd ( struct input_thread_s * );
92 static int TSRead ( struct input_thread_s *,
93 data_packet_t * p_packets[INPUT_READ_ONCE] );
95 /*****************************************************************************
96 * Functions exported as capabilities. They are declared as static so that
97 * we don't pollute the namespace too much.
98 *****************************************************************************/
99 void _M( input_getfunctions )( function_list_t * p_function_list )
101 #define input p_function_list->functions.input
102 p_function_list->pf_probe = TSProbe;
103 input.pf_init = TSInit;
104 input.pf_open = NULL;
105 input.pf_close = NULL;
106 input.pf_end = TSEnd;
107 input.pf_init_bit_stream = InitBitstream;
108 input.pf_set_area = NULL;
109 input.pf_set_area = input_SetProgram;
110 input.pf_read = TSRead;
111 input.pf_demux = input_DemuxTS;
112 input.pf_new_packet = input_NetlistNewPacket;
113 input.pf_new_pes = input_NetlistNewPES;
114 input.pf_delete_packet = input_NetlistDeletePacket;
115 input.pf_delete_pes = input_NetlistDeletePES;
116 input.pf_rewind = NULL;
117 input.pf_seek = NULL;
121 /*****************************************************************************
122 * TSProbe: verifies that the stream is a TS stream
123 *****************************************************************************/
124 static int TSProbe( probedata_t * p_data )
126 input_thread_t * p_input = (input_thread_t *)p_data;
128 char * psz_name = p_input->p_source;
131 if( TestMethod( INPUT_METHOD_VAR, "ts" ) )
136 if( ( strlen(psz_name) >= 10 && !strncasecmp( psz_name, "udpstream:", 10 ) )
137 || ( strlen(psz_name) >= 4 && !strncasecmp( psz_name, "udp:", 4 ) ) )
139 /* If the user specified "udp:" then it's probably a network stream */
143 if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
145 /* If the user specified "file:" then it's probably a file */
149 if( ( strlen(psz_name) > 3 ) &&
150 !strncasecmp( psz_name+strlen(psz_name)-3, ".ts", 3) )
152 /* If it is a ".ts" file it's probably a TS file ... */
159 /*****************************************************************************
160 * TSInit: initializes TS structures
161 *****************************************************************************/
162 static void TSInit( input_thread_t * p_input )
164 /* Initialize netlist and TS structures */
165 thread_ts_data_t * p_method;
166 es_descriptor_t * p_pat_es;
167 es_ts_data_t * p_demux_data;
168 stream_ts_data_t * p_stream_data;
170 /* Initialise structure */
171 p_method = malloc( sizeof( thread_ts_data_t ) );
172 if( p_method == NULL )
174 intf_ErrMsg( "TS input : Out of memory" );
175 p_input->b_error = 1;
180 p_method->i_length = 0;
181 p_method->i_offset = 0;
184 p_input->p_plugin_data = (void *)p_method;
185 p_input->p_method_data = NULL;
188 /* Initialize netlist */
189 if( input_NetlistInit( p_input, NB_DATA, NB_DATA, NB_PES, TS_PACKET_SIZE,
192 intf_ErrMsg( "TS input : Could not initialize netlist" );
196 /* Initialize the stream */
197 input_InitStream( p_input, sizeof( stream_ts_data_t ) );
199 p_input->stream.p_selected_area->i_tell = 0;
202 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
203 p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
205 /* We'll have to catch the PAT in order to continue
206 * Then the input will catch the PMT and then the others ES
207 * The PAT es is indepedent of any program. */
208 p_pat_es = input_AddES( p_input, NULL,
209 0x00, sizeof( es_ts_data_t ) );
210 p_demux_data=(es_ts_data_t *)p_pat_es->p_demux_data;
211 p_demux_data->b_psi = 1;
212 p_demux_data->i_psi_type = PSI_IS_PAT;
213 p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
214 p_demux_data->p_psi_section->b_is_complete = 1;
218 /*****************************************************************************
219 * TSEnd: frees unused data
220 *****************************************************************************/
221 static void TSEnd( input_thread_t * p_input )
223 es_descriptor_t * p_pat_es;
225 p_pat_es = input_FindES( p_input, 0x00 );
227 if( p_pat_es != NULL )
228 input_DelES( p_input, p_pat_es );
230 free(p_input->p_plugin_data);
231 input_NetlistEnd( p_input );
234 /*****************************************************************************
235 * TSRead: reads data packets
236 *****************************************************************************
237 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
239 *****************************************************************************/
240 static int TSRead( input_thread_t * p_input,
241 data_packet_t * pp_packets[INPUT_READ_ONCE] )
243 thread_ts_data_t * p_method;
247 struct iovec * p_iovec;
248 struct timeval timeout;
251 p_method = ( thread_ts_data_t * )p_input->p_plugin_data;
253 /* Initialize file descriptor set */
254 FD_ZERO( &(p_method->fds) );
255 FD_SET( p_input->i_handle, &(p_method->fds) );
257 /* We'll wait 0.5 second if nothing happens */
259 timeout.tv_usec = 500000;
261 /* Reset pointer table */
262 memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
264 /* Fill if some data is available */
266 if ( ! p_input->stream.b_pace_control )
269 i_data = select( p_input->i_handle + 1, &p_method->fds,
270 NULL, NULL, &timeout );
275 intf_ErrMsg( "input error: TS select error (%s)", strerror(errno) );
282 p_iovec = input_NetlistGetiovec( p_input->p_method_data );
284 if ( p_iovec == NULL )
286 return( -1 ); /* empty netlist */
290 if( p_input->stream.b_pace_control )
292 i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
296 i_read = readv_network( p_input->i_handle, p_iovec,
297 INPUT_READ_ONCE, p_method );
300 i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
302 /* Shouldn't happen, but it does - at least under Linux */
303 if( (i_read == -1) && ( (errno == EAGAIN) || (errno = EWOULDBLOCK) ) )
305 /* just ignore that error */
306 intf_ErrMsg( "input error: 0 bytes read" );
312 intf_ErrMsg( "input error: TS readv error" );
317 if( i_read == 0 && p_input->stream.b_seekable )
322 input_NetlistMviovec( p_input->p_method_data,
323 (int)(((i_read-1)/TS_PACKET_SIZE)+1) , pp_packets );
325 /* check correct TS header */
326 for( i_loop=0; i_loop * TS_PACKET_SIZE < i_read; i_loop++ )
328 if( pp_packets[i_loop]->p_buffer[0] != 0x47 )
329 intf_ErrMsg( "input error: bad TS packet (starts with "
330 "0x%.2x, should be 0x47)",
331 pp_packets[i_loop]->p_buffer[0] );
333 for( ; i_loop < INPUT_READ_ONCE ; i_loop++ )
335 pp_packets[i_loop] = NULL;
338 p_input->stream.p_selected_area->i_tell += i_read;