1 /*****************************************************************************
2 * input_ts.c: TS demux and netlist management
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: input_ts.c,v 1.13 2002/02/15 13:32:53 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 ( struct input_thread_s * );
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 input.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( input_thread_t * p_input )
126 char * psz_name = p_input->p_source;
128 if( ( strlen(psz_name) >= 10 && !strncasecmp( psz_name, "udpstream:", 10 ) )
129 || ( strlen(psz_name) >= 4 && !strncasecmp( psz_name, "udp:", 4 ) ) )
131 /* If the user specified "udp:" then it's probably a network stream */
135 if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
137 /* If the user specified "file:" then it's probably a file */
141 if( ( strlen(psz_name) > 3 ) &&
142 !strncasecmp( psz_name+strlen(psz_name)-3, ".ts", 3) )
144 /* If it is a ".ts" file it's probably a TS file ... */
151 /*****************************************************************************
152 * TSInit: initializes TS structures
153 *****************************************************************************/
154 static void TSInit( input_thread_t * p_input )
156 thread_ts_data_t * p_method;
157 es_descriptor_t * p_pat_es;
158 es_ts_data_t * p_demux_data;
159 stream_ts_data_t * p_stream_data;
161 /* Initialise structure */
162 p_method = malloc( sizeof( thread_ts_data_t ) );
163 if( p_method == NULL )
165 intf_ErrMsg( "TS input : Out of memory" );
166 p_input->b_error = 1;
171 p_method->i_length = 0;
172 p_method->i_offset = 0;
175 p_input->p_plugin_data = (void *)p_method;
176 p_input->p_method_data = NULL;
179 if( (p_input->p_method_data = input_BuffersInit()) == NULL )
181 p_input->b_error = 1;
185 /* Initialize the stream */
186 input_InitStream( p_input, sizeof( stream_ts_data_t ) );
188 p_input->stream.p_selected_area->i_tell = 0;
191 p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
192 p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
194 /* We'll have to catch the PAT in order to continue
195 * Then the input will catch the PMT and then the others ES
196 * The PAT es is indepedent of any program. */
197 p_pat_es = input_AddES( p_input, NULL,
198 0x00, sizeof( es_ts_data_t ) );
199 p_demux_data=(es_ts_data_t *)p_pat_es->p_demux_data;
200 p_demux_data->b_psi = 1;
201 p_demux_data->i_psi_type = PSI_IS_PAT;
202 p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
203 p_demux_data->p_psi_section->b_is_complete = 1;
207 /*****************************************************************************
208 * TSEnd: frees unused data
209 *****************************************************************************/
210 static void TSEnd( input_thread_t * p_input )
212 es_descriptor_t * p_pat_es;
214 p_pat_es = input_FindES( p_input, 0x00 );
216 if( p_pat_es != NULL )
217 input_DelES( p_input, p_pat_es );
219 free(p_input->p_plugin_data);
220 input_BuffersEnd( p_input->p_method_data );
223 /*****************************************************************************
224 * TSRead: reads data packets
225 *****************************************************************************
226 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
228 *****************************************************************************/
229 static int TSRead( input_thread_t * p_input,
230 data_packet_t ** pp_data )
232 thread_ts_data_t * p_method;
233 int i_read = 0, i_loop;
235 struct iovec p_iovec[TS_READ_ONCE];
236 data_packet_t * p_data;
237 struct timeval timeout;
240 p_method = ( thread_ts_data_t * )p_input->p_plugin_data;
242 /* Initialize file descriptor set */
243 FD_ZERO( &(p_method->fds) );
244 FD_SET( p_input->i_handle, &(p_method->fds) );
246 /* We'll wait 0.5 second if nothing happens */
248 timeout.tv_usec = 500000;
250 /* Fill if some data is available */
252 if ( ! p_input->stream.b_pace_control )
255 i_data = select( p_input->i_handle + 1, &p_method->fds,
256 NULL, NULL, &timeout );
261 intf_ErrMsg( "input error: TS select error (%s)", strerror(errno) );
268 *pp_data = p_data = input_BuffersToIO( p_input->p_method_data, p_iovec,
271 if ( p_data == NULL )
277 if( p_input->stream.b_pace_control )
279 i_read = readv( p_input->i_handle, p_iovec, TS_READ_ONCE );
283 i_read = readv_network( p_input->i_handle, p_iovec,
284 TS_READ_ONCE, p_method );
287 i_read = readv( p_input->i_handle, p_iovec, TS_READ_ONCE );
289 /* Shouldn't happen, but it does - at least under Linux */
290 if( (i_read == -1) && ( (errno == EAGAIN) || (errno = EWOULDBLOCK) ) )
292 /* just ignore that error */
293 intf_ErrMsg( "input error: 0 bytes read" );
300 intf_ErrMsg( "input error: TS readv error" );
301 p_input->pf_delete_packet( p_input->p_method_data, p_data );
304 p_input->stream.p_selected_area->i_tell += i_read;
305 i_read /= TS_PACKET_SIZE;
307 /* Check correct TS header */
308 for( i_loop = 0; i_loop < i_read; i_loop++ )
310 if( (*pp_data)->p_demux_start[0] != 0x47 )
312 intf_ErrMsg( "input error: bad TS packet (starts with "
313 "0x%.2x, should be 0x47)",
314 p_data->p_demux_start[0] );
316 pp_data = &(*pp_data)->p_next;
319 if( i_read != TS_READ_ONCE )
321 /* Delete remaining packets */
322 p_input->pf_delete_packet( p_input->p_method_data, *pp_data );