1 /*****************************************************************************
2 * input_ts.c: TS demux and netlist management
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: input_ts.c,v 1.10 2001/12/30 07:09:55 sam 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 /*****************************************************************************
26 *****************************************************************************/
31 #include <videolan/vlc.h>
33 #ifdef STRNCASECMP_IN_STRINGS_H
37 #include <sys/types.h>
39 #if !defined( _MSC_VER )
40 # include <sys/time.h>
44 # include <sys/select.h>
57 # include <winsock2.h>
59 # include <sys/uio.h> /* struct iovec */
63 # include "input_iovec.h"
66 #include "stream_control.h"
67 #include "input_ext-intf.h"
68 #include "input_ext-dec.h"
69 #include "input_ext-plugins.h"
73 /*****************************************************************************
75 *****************************************************************************/
76 static int TSProbe ( probedata_t * );
77 static void TSInit ( struct input_thread_s * );
78 static void TSEnd ( struct input_thread_s * );
79 static int TSRead ( struct input_thread_s *, data_packet_t ** );
81 /*****************************************************************************
82 * Declare a buffer manager
83 *****************************************************************************/
84 #define FLAGS BUFFERS_UNIQUE_SIZE
86 DECLARE_BUFFERS_EMBEDDED( FLAGS, NB_LIFO );
87 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
88 DECLARE_BUFFERS_END( FLAGS, NB_LIFO );
89 DECLARE_BUFFERS_NEWPACKET( FLAGS, NB_LIFO );
90 DECLARE_BUFFERS_DELETEPACKET( FLAGS, NB_LIFO, 1000 );
91 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
92 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 150 );
93 DECLARE_BUFFERS_TOIO( FLAGS, TS_PACKET_SIZE );
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_program = input_SetProgram;
110 input.pf_read = TSRead;
111 input.pf_demux = input_DemuxTS;
112 input.pf_new_packet = input_NewPacket;
113 input.pf_new_pes = input_NewPES;
114 input.pf_delete_packet = input_DeletePacket;
115 input.pf_delete_pes = input_DeletePES;
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( ( strlen(psz_name) >= 10 && !strncasecmp( psz_name, "udpstream:", 10 ) )
132 || ( strlen(psz_name) >= 4 && !strncasecmp( psz_name, "udp:", 4 ) ) )
134 /* If the user specified "udp:" then it's probably a network stream */
138 if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
140 /* If the user specified "file:" then it's probably a file */
144 if( ( strlen(psz_name) > 3 ) &&
145 !strncasecmp( psz_name+strlen(psz_name)-3, ".ts", 3) )
147 /* If it is a ".ts" file it's probably a TS file ... */
154 /*****************************************************************************
155 * TSInit: initializes TS structures
156 *****************************************************************************/
157 static void TSInit( input_thread_t * p_input )
159 thread_ts_data_t * p_method;
160 es_descriptor_t * p_pat_es;
161 es_ts_data_t * p_demux_data;
162 stream_ts_data_t * p_stream_data;
164 /* Initialise structure */
165 p_method = malloc( sizeof( thread_ts_data_t ) );
166 if( p_method == NULL )
168 intf_ErrMsg( "TS input : Out of memory" );
169 p_input->b_error = 1;
174 p_method->i_length = 0;
175 p_method->i_offset = 0;
178 p_input->p_plugin_data = (void *)p_method;
179 p_input->p_method_data = NULL;
182 if( (p_input->p_method_data = input_BuffersInit()) == NULL )
184 p_input->b_error = 1;
188 /* Initialize the stream */
189 input_InitStream( p_input, sizeof( stream_ts_data_t ) );
191 p_input->stream.p_selected_area->i_tell = 0;
194 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
195 p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
197 /* We'll have to catch the PAT in order to continue
198 * Then the input will catch the PMT and then the others ES
199 * The PAT es is indepedent of any program. */
200 p_pat_es = input_AddES( p_input, NULL,
201 0x00, sizeof( es_ts_data_t ) );
202 p_demux_data=(es_ts_data_t *)p_pat_es->p_demux_data;
203 p_demux_data->b_psi = 1;
204 p_demux_data->i_psi_type = PSI_IS_PAT;
205 p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
206 p_demux_data->p_psi_section->b_is_complete = 1;
210 /*****************************************************************************
211 * TSEnd: frees unused data
212 *****************************************************************************/
213 static void TSEnd( input_thread_t * p_input )
215 es_descriptor_t * p_pat_es;
217 p_pat_es = input_FindES( p_input, 0x00 );
219 if( p_pat_es != NULL )
220 input_DelES( p_input, p_pat_es );
222 free(p_input->p_plugin_data);
223 input_BuffersEnd( p_input->p_method_data );
226 /*****************************************************************************
227 * TSRead: reads data packets
228 *****************************************************************************
229 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
231 *****************************************************************************/
232 static int TSRead( input_thread_t * p_input,
233 data_packet_t ** pp_data )
235 thread_ts_data_t * p_method;
236 int i_read = 0, i_loop;
238 struct iovec p_iovec[TS_READ_ONCE];
239 data_packet_t * p_data;
240 struct timeval timeout;
243 p_method = ( thread_ts_data_t * )p_input->p_plugin_data;
245 /* Initialize file descriptor set */
246 FD_ZERO( &(p_method->fds) );
247 FD_SET( p_input->i_handle, &(p_method->fds) );
249 /* We'll wait 0.5 second if nothing happens */
251 timeout.tv_usec = 500000;
253 /* Fill if some data is available */
255 if ( ! p_input->stream.b_pace_control )
258 i_data = select( p_input->i_handle + 1, &p_method->fds,
259 NULL, NULL, &timeout );
264 intf_ErrMsg( "input error: TS select error (%s)", strerror(errno) );
271 *pp_data = p_data = input_BuffersToIO( p_input->p_method_data, p_iovec,
274 if ( p_data == NULL )
280 if( p_input->stream.b_pace_control )
282 i_read = readv( p_input->i_handle, p_iovec, TS_READ_ONCE );
286 i_read = readv_network( p_input->i_handle, p_iovec,
287 TS_READ_ONCE, p_method );
290 i_read = readv( p_input->i_handle, p_iovec, TS_READ_ONCE );
292 /* Shouldn't happen, but it does - at least under Linux */
293 if( (i_read == -1) && ( (errno == EAGAIN) || (errno = EWOULDBLOCK) ) )
295 /* just ignore that error */
296 intf_ErrMsg( "input error: 0 bytes read" );
303 intf_ErrMsg( "input error: TS readv error" );
304 p_input->pf_delete_packet( p_input->p_method_data, p_data );
307 p_input->stream.p_selected_area->i_tell += i_read;
308 i_read /= TS_PACKET_SIZE;
310 /* Check correct TS header */
311 for( i_loop = 0; i_loop < i_read; i_loop++ )
313 if( (*pp_data)->p_demux_start[0] != 0x47 )
315 intf_ErrMsg( "input error: bad TS packet (starts with "
316 "0x%.2x, should be 0x47)",
317 p_data->p_demux_start[0] );
319 pp_data = &(*pp_data)->p_next;
322 if( i_read != TS_READ_ONCE )
324 /* Delete remaining packets */
325 p_input->pf_delete_packet( p_input->p_method_data, *pp_data );