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