]> git.sesse.net Git - vlc/blob - plugins/mpeg/input_ts.c
361a89ea8e79ea1482099cbf1b0c14bc77bc52d0
[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.35 2001/10/22 02:33:54 xav 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 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             = NULL;
106     input.pf_close            = NULL;
107     input.pf_end              = TSEnd;
108     input.pf_init_bit_stream  = InitBitstream;
109     input.pf_set_area         = NULL;
110     input.pf_read             = TSRead;
111     input.pf_demux            = input_DemuxTS;
112     input.pf_new_packet       = input_NetlistNewPacket;
113     input.pf_new_pes          = input_NetlistNewPES;
114     input.pf_delete_packet    = input_NetlistDeletePacket;
115     input.pf_delete_pes       = input_NetlistDeletePES;
116     input.pf_rewind           = NULL;
117     input.pf_seek             = NULL;
118 #undef input
119 }
120
121 /*****************************************************************************
122  * TSProbe: verifies that the stream is a TS stream
123  *****************************************************************************/
124 static int TSProbe( probedata_t * p_data )
125 {
126     input_thread_t * p_input = (input_thread_t *)p_data;
127
128     char * psz_name = p_input->p_source;
129     int i_score = 2;
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     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 #if defined( WIN32 )
179     p_method->i_length = 0;
180     p_method->i_offset = 0;
181 #endif
182
183     p_input->p_plugin_data = (void *)p_method;
184     p_input->p_method_data = NULL;
185
186
187     /* Initialize netlist */
188     if( input_NetlistInit( p_input, NB_DATA, NB_PES, TS_PACKET_SIZE,
189                 INPUT_READ_ONCE ) )
190     {
191         intf_ErrMsg( "TS input : Could not initialize netlist" );
192         return;
193     }
194
195     /* Initialize the stream */
196     input_InitStream( p_input, sizeof( stream_ts_data_t ) );
197
198     p_input->stream.p_selected_area->i_tell = 0;
199
200     /* Init */
201     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
202     p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
203
204     /* We'll have to catch the PAT in order to continue
205      * Then the input will catch the PMT and then the others ES
206      * The PAT es is indepedent of any program. */
207     p_pat_es = input_AddES( p_input, NULL,
208                            0x00, sizeof( es_ts_data_t ) );
209     p_demux_data=(es_ts_data_t *)p_pat_es->p_demux_data;
210     p_demux_data->b_psi = 1;
211     p_demux_data->i_psi_type = PSI_IS_PAT;
212     p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
213     p_demux_data->p_psi_section->b_is_complete = 1;
214
215 }
216
217 /*****************************************************************************
218  * TSEnd: frees unused data
219  *****************************************************************************/
220 static void TSEnd( input_thread_t * p_input )
221 {
222     es_descriptor_t     * p_pat_es;
223
224     p_pat_es = input_FindES( p_input, 0x00 );
225
226     if( p_pat_es != NULL )
227         input_DelES( p_input, p_pat_es );
228     free(p_input->p_plugin_data);
229 }
230
231 /*****************************************************************************
232  * TSRead: reads data packets
233  *****************************************************************************
234  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
235  * EOF.
236  *****************************************************************************/
237 static int TSRead( input_thread_t * p_input,
238                    data_packet_t * pp_packets[INPUT_READ_ONCE] )
239 {
240     thread_ts_data_t    * p_method;
241     unsigned int    i_loop;
242     int             i_read;
243     int             i_data = 1;
244     struct iovec  * p_iovec;
245     struct timeval  timeout;
246
247     /* Init */
248     p_method = ( thread_ts_data_t * )p_input->p_plugin_data;
249
250     /* Initialize file descriptor set */
251     FD_ZERO( &(p_method->fds) );
252     FD_SET( p_input->i_handle, &(p_method->fds) );
253
254     /* We'll wait 0.5 second if nothing happens */
255     timeout.tv_sec = 0;
256     timeout.tv_usec = 500000;
257
258     /* Reset pointer table */
259     memset( pp_packets, 0, INPUT_READ_ONCE * sizeof(data_packet_t *) );
260
261     /* Fill if some data is available */
262 #if defined( WIN32 )
263     if ( ! p_input->stream.b_pace_control ) 
264 #endif
265     {
266         i_data = select( p_input->i_handle + 1, &p_method->fds,
267                          NULL, NULL, &timeout );
268     }
269
270     if( i_data == -1 )
271     {
272         intf_ErrMsg( "input error: TS select error (%s)", strerror(errno) );
273         return( -1 );
274     }
275     
276     if( i_data )
277     {
278         /* Get iovecs */
279         p_iovec = input_NetlistGetiovec( p_input->p_method_data );
280
281         if ( p_iovec == NULL )
282         {
283             return( -1 ); /* empty netlist */
284         }
285
286 #if defined( WIN32 )
287         if( p_input->stream.b_pace_control )
288         {
289             i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
290         }
291         else
292         {
293             i_read = readv_network( p_input->i_handle, p_iovec,
294                                     INPUT_READ_ONCE, p_method );
295         }
296 #else
297         i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
298
299         /* Shouldn't happen, but it does - at least under Linux */
300         if( (i_read == -1) && ( (errno == EAGAIN) || (errno = EWOULDBLOCK) ) )
301         {
302             /* just ignore that error */
303             i_read = 0;
304         }
305 #endif
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