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