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