]> git.sesse.net Git - vlc/blob - plugins/mpeg/input_ts.c
IP Multicast support, courtesy of Mathias Kretschmer <mathias@research.att.com>.
[vlc] / plugins / mpeg / input_ts.c
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.32 2001/08/27 16:13:20 massiot Exp $
6  *
7  * Authors: Henri Fallon <henri@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #define MODULE_NAME ts
25 #include "modules_inner.h"
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <errno.h>
35
36 #ifdef STRNCASECMP_IN_STRINGS_H
37 #   include <strings.h>
38 #endif
39
40 #include <sys/types.h>
41
42 #if !defined( _MSC_VER )
43 #   include <sys/time.h>
44 #endif
45
46 #ifdef SYS_NTO
47 #   include <sys/select.h>
48 #endif
49
50 #include <sys/stat.h>
51
52 #ifdef HAVE_UNISTD_H
53 #   include <unistd.h>
54 #endif
55
56 #include <fcntl.h>
57
58 #if defined( WIN32 )
59 #   include <io.h>
60 #   include <winsock2.h>
61 #else
62 #   include <sys/uio.h>                                      /* struct iovec */
63 #endif
64
65 #include "config.h"
66 #include "common.h"
67 #include "threads.h"
68 #include "mtime.h"
69 #include "tests.h"
70
71 #if defined( WIN32 )
72 #   include "input_iovec.h"
73 #endif
74
75 #include "intf_msg.h"
76
77 #include "stream_control.h"
78 #include "input_ext-intf.h"
79 #include "input_ext-dec.h"
80 #include "input_ext-plugins.h"
81
82 #include "input_ts.h"
83
84 #include "modules.h"
85 #include "modules_export.h"
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90 static int  TSProbe     ( probedata_t * );
91 static void TSInit      ( struct input_thread_s * );
92 static void TSFakeOpen  ( struct input_thread_s * );
93 static void TSEnd       ( struct input_thread_s * );
94 static int  TSRead      ( struct input_thread_s *,
95                           data_packet_t * p_packets[INPUT_READ_ONCE] );
96
97 /*****************************************************************************
98  * Functions exported as capabilities. They are declared as static so that
99  * we don't pollute the namespace too much.
100  *****************************************************************************/
101 void _M( input_getfunctions )( function_list_t * p_function_list )
102 {
103 #define input p_function_list->functions.input
104     p_function_list->pf_probe = TSProbe;
105     input.pf_init             = TSInit;
106     input.pf_open             = TSFakeOpen;
107     input.pf_close            = NULL;              /* Will be set by pf_open */
108     input.pf_end              = TSEnd;
109     input.pf_init_bit_stream  = InitBitstream;
110     input.pf_set_area         = NULL;
111     input.pf_read             = TSRead;
112     input.pf_demux            = input_DemuxTS;
113     input.pf_new_packet       = input_NetlistNewPacket;
114     input.pf_new_pes          = input_NetlistNewPES;
115     input.pf_delete_packet    = input_NetlistDeletePacket;
116     input.pf_delete_pes       = input_NetlistDeletePES;
117     input.pf_rewind           = NULL;
118     input.pf_seek             = NULL;
119 #undef input
120 }
121
122 /*****************************************************************************
123  * TSProbe: verifies that the stream is a TS stream
124  *****************************************************************************/
125 static int TSProbe( probedata_t * p_data )
126 {
127     input_thread_t * p_input = (input_thread_t *)p_data;
128
129     char * psz_name = p_input->p_source;
130     int i_handle;
131     int i_score = 1;
132
133     if( TestMethod( INPUT_METHOD_VAR, "ts" ) )
134     {
135         return( 999 );
136     }
137
138     if( ( strlen(psz_name) > 3 ) && !strncasecmp( psz_name, "ts:", 3 ) )
139     {
140         /* If the user specified "ts:" then it's probably a network stream */
141         return( 999 );
142     }
143
144     if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
145     {
146         /* If the user specified "file:" then it's probably a file */
147         psz_name += 5;
148     }
149
150     if( ( strlen(psz_name) > 3 ) &&
151                     !strncasecmp( psz_name+strlen(psz_name)-3, ".ts", 3) )
152     {
153         /* If it is a ".ts" file it's probably a TS file ... */
154         return( 900 );
155     }
156
157     i_handle = open( psz_name, 0 );
158     if( i_handle == -1 )
159     {
160         return( 0 );
161     }
162     close( i_handle );
163
164     return( i_score );
165 }
166
167 /*****************************************************************************
168  * TSInit: initializes TS structures
169  *****************************************************************************/
170 static void TSInit( input_thread_t * p_input )
171 {
172     /* Initialize netlist and TS structures */
173     thread_ts_data_t    * p_method;
174     es_descriptor_t     * p_pat_es;
175     es_ts_data_t        * p_demux_data;
176     stream_ts_data_t    * p_stream_data;
177
178     /* Initialise structure */
179     p_method = malloc( sizeof( thread_ts_data_t ) );
180     if( p_method == NULL )
181     {
182         intf_ErrMsg( "TS input : Out of memory" );
183         p_input->b_error = 1;
184         return;
185     }
186
187 #if defined( WIN32 )
188     p_method->i_length = 0;
189     p_method->i_offset = 0;
190 #endif
191
192     p_input->p_plugin_data = (void *)p_method;
193     p_input->p_method_data = NULL;
194
195
196     /* Initialize netlist */
197     if( input_NetlistInit( p_input, NB_DATA, NB_PES, TS_PACKET_SIZE,
198                 INPUT_READ_ONCE ) )
199     {
200         intf_ErrMsg( "TS input : Could not initialize netlist" );
201         return;
202     }
203
204     /* Initialize the stream */
205     input_InitStream( p_input, sizeof( stream_ts_data_t ) );
206
207     /* input method type */
208     /* FIXME: should test if you have network or file here */
209     p_input->stream.i_method = INPUT_METHOD_NETWORK;
210     p_input->stream.p_selected_area->i_tell = 0;
211
212     /* Init */
213     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
214     p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
215
216     /* We'll have to catch the PAT in order to continue
217      * Then the input will catch the PMT and then the others ES
218      * The PAT es is indepedent of any program. */
219     p_pat_es = input_AddES( p_input, NULL,
220                            0x00, sizeof( es_ts_data_t ) );
221     p_demux_data=(es_ts_data_t *)p_pat_es->p_demux_data;
222     p_demux_data->b_psi = 1;
223     p_demux_data->i_psi_type = PSI_IS_PAT;
224     p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
225     p_demux_data->p_psi_section->b_is_complete = 1;
226
227 }
228
229 /*****************************************************************************
230  * TSFakeOpen: open the stream and set pf_close
231  *****************************************************************************/
232 void TSFakeOpen( input_thread_t * p_input )
233 {
234 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
235     char *psz_name = p_input->p_source;
236
237     if( ( strlen(psz_name) > 3 ) && !strncasecmp( psz_name, "ts:", 3 ) )
238     {
239         /* If the user specified "ts:" he wants a network stream */
240         p_input->pf_open  = p_input->pf_network_open;
241         p_input->pf_close = p_input->pf_network_close;
242     }
243     else
244 #endif
245     {
246         p_input->pf_open  = p_input->pf_file_open;
247         p_input->pf_close = p_input->pf_file_close;
248     }
249
250     p_input->pf_open( p_input );
251 }
252
253 /*****************************************************************************
254  * TSEnd: frees unused data
255  *****************************************************************************/
256 static void TSEnd( input_thread_t * p_input )
257 {
258     es_descriptor_t     * p_pat_es;
259
260     p_pat_es = input_FindES( p_input, 0x00 );
261
262     if( p_pat_es != NULL )
263         input_DelES( p_input, p_pat_es );
264     free(p_input->p_plugin_data);
265 }
266
267 /*****************************************************************************
268  * TSRead: reads data packets
269  *****************************************************************************
270  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
271  * EOF.
272  *****************************************************************************/
273 static int TSRead( input_thread_t * p_input,
274                    data_packet_t * pp_packets[INPUT_READ_ONCE] )
275 {
276     thread_ts_data_t    * p_method;
277     unsigned int    i_loop;
278     int             i_read;
279     int             i_data = 0;
280     struct iovec  * p_iovec;
281     struct timeval  timeout;
282
283     /* Get iovecs */
284     p_iovec = input_NetlistGetiovec( p_input->p_method_data );
285
286     if ( p_iovec == NULL )
287     {
288         return( -1 ); /* empty netlist */
289     }
290
291     /* Init */
292     p_method = ( thread_ts_data_t * )p_input->p_plugin_data;
293
294     /* Initialize file descriptor set */
295     FD_ZERO( &(p_method->fds) );
296     FD_SET( p_input->i_handle, &(p_method->fds) );
297
298     /* We'll wait 0.5 second if nothing happens */
299     timeout.tv_sec = 0;
300     timeout.tv_usec = 500000;
301
302     /* Reset pointer table */
303     memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
304
305     /* Fill if some data is available */
306 #if defined( WIN32 )
307     if ( ! p_input->stream.b_pace_control ) 
308 #endif
309     {
310         i_data = select( p_input->i_handle + 1, &p_method->fds,
311                          NULL, NULL, &timeout );
312     }
313
314     if( i_data == -1 )
315     {
316         intf_ErrMsg( "input error: TS select error (%s)", strerror(errno) );
317         return( -1 );
318     }
319
320     if( i_data )
321     {
322 #if defined( WIN32 )
323         if( p_input->stream.b_pace_control )
324         {
325             i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
326         }
327         else
328         {
329             i_read = readv_network( p_input->i_handle, p_iovec,
330                                     INPUT_READ_ONCE, p_method );
331         }
332 #else
333         i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
334
335         /* Shouldn't happen, but it does - at least under Linux */
336         if( (i_read == -1) && ( (errno == EAGAIN) || (errno = EWOULDBLOCK) ) )
337         {
338             /* just ignore that error */
339             i_read = 0;
340         }
341 #endif
342         if( i_read == -1 )
343         {
344             intf_ErrMsg( "input error: TS readv error" );
345             return( -1 );
346         }
347         
348         input_NetlistMviovec( p_input->p_method_data,
349                 (int)(i_read/TS_PACKET_SIZE) , pp_packets );
350
351         /* check correct TS header */
352         for( i_loop=0; i_loop * TS_PACKET_SIZE < i_read; i_loop++ )
353         {
354             if( pp_packets[i_loop]->p_buffer[0] != 0x47 )
355                 intf_ErrMsg( "input error: bad TS packet (starts with "
356                              "0x%.2x, should be 0x47)",
357                              pp_packets[i_loop]->p_buffer[0] );
358         }
359
360         p_input->stream.p_selected_area->i_tell += i_read;
361     }
362     return 0;
363 }
364