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