]> git.sesse.net Git - vlc/blob - plugins/mpeg_system/input_ts.c
* ./plugins/mpeg_vdec/vpar_headers.c: we no longer crash when the next
[vlc] / plugins / mpeg_system / input_ts.c
1 /*****************************************************************************
2  * input_ts.c: TS demux and netlist management
3  *****************************************************************************
4  * Copyright (C) 1998-2001 VideoLAN
5  * $Id: input_ts.c,v 1.13 2002/02/15 13:32:53 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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <videolan/vlc.h>
32
33 #ifdef STRNCASECMP_IN_STRINGS_H
34 #   include <strings.h>
35 #endif
36
37 #include <sys/types.h>
38
39 #if !defined( _MSC_VER )
40 #   include <sys/time.h>
41 #endif
42
43 #ifdef SYS_NTO
44 #   include <sys/select.h>
45 #endif
46
47 #include <sys/stat.h>
48
49 #ifdef HAVE_UNISTD_H
50 #   include <unistd.h>
51 #endif
52
53 #include <fcntl.h>
54
55 #if defined( WIN32 )
56 #   include <io.h>
57 #   include <winsock2.h>
58 #else
59 #   include <sys/uio.h>                                      /* struct iovec */
60 #endif
61
62 #if defined( WIN32 )
63 #   include "input_iovec.h"
64 #endif
65
66 #include "stream_control.h"
67 #include "input_ext-intf.h"
68 #include "input_ext-dec.h"
69 #include "input_ext-plugins.h"
70
71 #include "input_ts.h"
72
73 /*****************************************************************************
74  * Local prototypes
75  *****************************************************************************/
76 static int  TSProbe     ( struct input_thread_s * );
77 static void TSInit      ( struct input_thread_s * );
78 static void TSEnd       ( struct input_thread_s * );
79 static int  TSRead      ( struct input_thread_s *, data_packet_t ** );
80
81 /*****************************************************************************
82  * Declare a buffer manager
83  *****************************************************************************/
84 #define FLAGS           BUFFERS_UNIQUE_SIZE
85 #define NB_LIFO         1
86 DECLARE_BUFFERS_EMBEDDED( FLAGS, NB_LIFO );
87 DECLARE_BUFFERS_INIT( FLAGS, NB_LIFO );
88 DECLARE_BUFFERS_END( FLAGS, NB_LIFO );
89 DECLARE_BUFFERS_NEWPACKET( FLAGS, NB_LIFO );
90 DECLARE_BUFFERS_DELETEPACKET( FLAGS, NB_LIFO, 1000 );
91 DECLARE_BUFFERS_NEWPES( FLAGS, NB_LIFO );
92 DECLARE_BUFFERS_DELETEPES( FLAGS, NB_LIFO, 150 );
93 DECLARE_BUFFERS_TOIO( FLAGS, TS_PACKET_SIZE );
94
95 /*****************************************************************************
96  * Functions exported as capabilities. They are declared as static so that
97  * we don't pollute the namespace too much.
98  *****************************************************************************/
99 void _M( input_getfunctions )( function_list_t * p_function_list )
100 {
101 #define input p_function_list->functions.input
102     input.pf_probe            = TSProbe;
103     input.pf_init             = TSInit;
104     input.pf_open             = NULL;
105     input.pf_close            = NULL;
106     input.pf_end              = TSEnd;
107     input.pf_init_bit_stream  = InitBitstream;
108     input.pf_set_area         = NULL;
109     input.pf_set_program      = input_SetProgram;
110     input.pf_read             = TSRead;
111     input.pf_demux            = input_DemuxTS;
112     input.pf_new_packet       = input_NewPacket;
113     input.pf_new_pes          = input_NewPES;
114     input.pf_delete_packet    = input_DeletePacket;
115     input.pf_delete_pes       = input_DeletePES;
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( input_thread_t * p_input )
125 {
126     char * psz_name = p_input->p_source;
127
128     if( ( strlen(psz_name) >= 10 && !strncasecmp( psz_name, "udpstream:", 10 ) )
129             || ( strlen(psz_name) >= 4 && !strncasecmp( psz_name, "udp:", 4 ) ) )
130     {
131         /* If the user specified "udp:" then it's probably a network stream */
132         return 0;
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 0;
146     }
147
148     return -1;
149 }
150
151 /*****************************************************************************
152  * TSInit: initializes TS structures
153  *****************************************************************************/
154 static void TSInit( input_thread_t * p_input )
155 {
156     thread_ts_data_t    * p_method;
157     es_descriptor_t     * p_pat_es;
158     es_ts_data_t        * p_demux_data;
159     stream_ts_data_t    * p_stream_data;
160
161     /* Initialise structure */
162     p_method = malloc( sizeof( thread_ts_data_t ) );
163     if( p_method == NULL )
164     {
165         intf_ErrMsg( "TS input : Out of memory" );
166         p_input->b_error = 1;
167         return;
168     }
169
170 #if defined( WIN32 )
171     p_method->i_length = 0;
172     p_method->i_offset = 0;
173 #endif
174
175     p_input->p_plugin_data = (void *)p_method;
176     p_input->p_method_data = NULL;
177
178
179     if( (p_input->p_method_data = input_BuffersInit()) == NULL )
180     {
181         p_input->b_error = 1;
182         return;
183     }
184
185     /* Initialize the stream */
186     input_InitStream( p_input, sizeof( stream_ts_data_t ) );
187
188     p_input->stream.p_selected_area->i_tell = 0;
189
190     /* Init */
191     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
192     p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
193
194     /* We'll have to catch the PAT in order to continue
195      * Then the input will catch the PMT and then the others ES
196      * The PAT es is indepedent of any program. */
197     p_pat_es = input_AddES( p_input, NULL,
198                            0x00, sizeof( es_ts_data_t ) );
199     p_demux_data=(es_ts_data_t *)p_pat_es->p_demux_data;
200     p_demux_data->b_psi = 1;
201     p_demux_data->i_psi_type = PSI_IS_PAT;
202     p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
203     p_demux_data->p_psi_section->b_is_complete = 1;
204
205 }
206
207 /*****************************************************************************
208  * TSEnd: frees unused data
209  *****************************************************************************/
210 static void TSEnd( input_thread_t * p_input )
211 {
212     es_descriptor_t     * p_pat_es;
213
214     p_pat_es = input_FindES( p_input, 0x00 );
215
216     if( p_pat_es != NULL )
217         input_DelES( p_input, p_pat_es );
218
219     free(p_input->p_plugin_data);
220     input_BuffersEnd( p_input->p_method_data );
221 }
222
223 /*****************************************************************************
224  * TSRead: reads data packets
225  *****************************************************************************
226  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
227  * packets.
228  *****************************************************************************/
229 static int TSRead( input_thread_t * p_input,
230                    data_packet_t ** pp_data )
231 {
232     thread_ts_data_t    * p_method;
233     int             i_read = 0, i_loop;
234     int             i_data = 1;
235     struct iovec    p_iovec[TS_READ_ONCE];
236     data_packet_t * p_data;
237     struct timeval  timeout;
238
239     /* Init */
240     p_method = ( thread_ts_data_t * )p_input->p_plugin_data;
241
242     /* Initialize file descriptor set */
243     FD_ZERO( &(p_method->fds) );
244     FD_SET( p_input->i_handle, &(p_method->fds) );
245
246     /* We'll wait 0.5 second if nothing happens */
247     timeout.tv_sec = 0;
248     timeout.tv_usec = 500000;
249
250     /* Fill if some data is available */
251 #if defined( WIN32 )
252     if ( ! p_input->stream.b_pace_control ) 
253 #endif
254     {
255         i_data = select( p_input->i_handle + 1, &p_method->fds,
256                          NULL, NULL, &timeout );
257     }
258
259     if( i_data == -1 )
260     {
261         intf_ErrMsg( "input error: TS select error (%s)", strerror(errno) );
262         return( -1 );
263     }
264     
265     if( i_data )
266     {
267         /* Get iovecs */
268         *pp_data = p_data = input_BuffersToIO( p_input->p_method_data, p_iovec,
269                                                TS_READ_ONCE );
270
271         if ( p_data == NULL )
272         {
273             return( -1 );
274         }
275
276 #if defined( WIN32 )
277         if( p_input->stream.b_pace_control )
278         {
279             i_read = readv( p_input->i_handle, p_iovec, TS_READ_ONCE );
280         }
281         else
282         {
283             i_read = readv_network( p_input->i_handle, p_iovec,
284                                     TS_READ_ONCE, p_method );
285         }
286 #else
287         i_read = readv( p_input->i_handle, p_iovec, TS_READ_ONCE );
288
289         /* Shouldn't happen, but it does - at least under Linux */
290         if( (i_read == -1) && ( (errno == EAGAIN) || (errno = EWOULDBLOCK) ) )
291         {
292             /* just ignore that error */
293             intf_ErrMsg( "input error: 0 bytes read" );
294             i_read = 0;
295         }
296 #endif
297         /* Error */
298         if( i_read == -1 )
299         {
300             intf_ErrMsg( "input error: TS readv error" );
301             p_input->pf_delete_packet( p_input->p_method_data, p_data );
302             return( -1 );
303         }
304         p_input->stream.p_selected_area->i_tell += i_read;
305         i_read /= TS_PACKET_SIZE;
306
307         /* Check correct TS header */
308         for( i_loop = 0; i_loop < i_read; i_loop++ )
309         {
310             if( (*pp_data)->p_demux_start[0] != 0x47 )
311             {
312                 intf_ErrMsg( "input error: bad TS packet (starts with "
313                              "0x%.2x, should be 0x47)",
314                              p_data->p_demux_start[0] );
315             }
316             pp_data = &(*pp_data)->p_next;
317         }
318
319         if( i_read != TS_READ_ONCE )
320         {
321             /* Delete remaining packets */
322             p_input->pf_delete_packet( p_input->p_method_data, *pp_data );
323         }
324     }
325    
326     *pp_data = NULL;
327
328     return( i_read );
329 }
330