]> git.sesse.net Git - vlc/blob - plugins/mpeg_system/input_ts.c
Some heavy changes today:
[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.10 2001/12/30 07:09:55 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     ( probedata_t * );
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     p_function_list->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( 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( ( strlen(psz_name) >= 10 && !strncasecmp( psz_name, "udpstream:", 10 ) )
132             || ( strlen(psz_name) >= 4 && !strncasecmp( psz_name, "udp:", 4 ) ) )
133     {
134         /* If the user specified "udp:" then it's probably a network stream */
135         return( 999 );
136     }
137
138     if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
139     {
140         /* If the user specified "file:" then it's probably a file */
141         psz_name += 5;
142     }
143
144     if( ( strlen(psz_name) > 3 ) &&
145                     !strncasecmp( psz_name+strlen(psz_name)-3, ".ts", 3) )
146     {
147         /* If it is a ".ts" file it's probably a TS file ... */
148         return( 900 );
149     }
150
151     return( i_score );
152 }
153
154 /*****************************************************************************
155  * TSInit: initializes TS structures
156  *****************************************************************************/
157 static void TSInit( input_thread_t * p_input )
158 {
159     thread_ts_data_t    * p_method;
160     es_descriptor_t     * p_pat_es;
161     es_ts_data_t        * p_demux_data;
162     stream_ts_data_t    * p_stream_data;
163
164     /* Initialise structure */
165     p_method = malloc( sizeof( thread_ts_data_t ) );
166     if( p_method == NULL )
167     {
168         intf_ErrMsg( "TS input : Out of memory" );
169         p_input->b_error = 1;
170         return;
171     }
172
173 #if defined( WIN32 )
174     p_method->i_length = 0;
175     p_method->i_offset = 0;
176 #endif
177
178     p_input->p_plugin_data = (void *)p_method;
179     p_input->p_method_data = NULL;
180
181
182     if( (p_input->p_method_data = input_BuffersInit()) == NULL )
183     {
184         p_input->b_error = 1;
185         return;
186     }
187
188     /* Initialize the stream */
189     input_InitStream( p_input, sizeof( stream_ts_data_t ) );
190
191     p_input->stream.p_selected_area->i_tell = 0;
192
193     /* Init */
194     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
195     p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
196
197     /* We'll have to catch the PAT in order to continue
198      * Then the input will catch the PMT and then the others ES
199      * The PAT es is indepedent of any program. */
200     p_pat_es = input_AddES( p_input, NULL,
201                            0x00, sizeof( es_ts_data_t ) );
202     p_demux_data=(es_ts_data_t *)p_pat_es->p_demux_data;
203     p_demux_data->b_psi = 1;
204     p_demux_data->i_psi_type = PSI_IS_PAT;
205     p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
206     p_demux_data->p_psi_section->b_is_complete = 1;
207
208 }
209
210 /*****************************************************************************
211  * TSEnd: frees unused data
212  *****************************************************************************/
213 static void TSEnd( input_thread_t * p_input )
214 {
215     es_descriptor_t     * p_pat_es;
216
217     p_pat_es = input_FindES( p_input, 0x00 );
218
219     if( p_pat_es != NULL )
220         input_DelES( p_input, p_pat_es );
221
222     free(p_input->p_plugin_data);
223     input_BuffersEnd( p_input->p_method_data );
224 }
225
226 /*****************************************************************************
227  * TSRead: reads data packets
228  *****************************************************************************
229  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
230  * packets.
231  *****************************************************************************/
232 static int TSRead( input_thread_t * p_input,
233                    data_packet_t ** pp_data )
234 {
235     thread_ts_data_t    * p_method;
236     int             i_read = 0, i_loop;
237     int             i_data = 1;
238     struct iovec    p_iovec[TS_READ_ONCE];
239     data_packet_t * p_data;
240     struct timeval  timeout;
241
242     /* Init */
243     p_method = ( thread_ts_data_t * )p_input->p_plugin_data;
244
245     /* Initialize file descriptor set */
246     FD_ZERO( &(p_method->fds) );
247     FD_SET( p_input->i_handle, &(p_method->fds) );
248
249     /* We'll wait 0.5 second if nothing happens */
250     timeout.tv_sec = 0;
251     timeout.tv_usec = 500000;
252
253     /* Fill if some data is available */
254 #if defined( WIN32 )
255     if ( ! p_input->stream.b_pace_control ) 
256 #endif
257     {
258         i_data = select( p_input->i_handle + 1, &p_method->fds,
259                          NULL, NULL, &timeout );
260     }
261
262     if( i_data == -1 )
263     {
264         intf_ErrMsg( "input error: TS select error (%s)", strerror(errno) );
265         return( -1 );
266     }
267     
268     if( i_data )
269     {
270         /* Get iovecs */
271         *pp_data = p_data = input_BuffersToIO( p_input->p_method_data, p_iovec,
272                                                TS_READ_ONCE );
273
274         if ( p_data == NULL )
275         {
276             return( -1 );
277         }
278
279 #if defined( WIN32 )
280         if( p_input->stream.b_pace_control )
281         {
282             i_read = readv( p_input->i_handle, p_iovec, TS_READ_ONCE );
283         }
284         else
285         {
286             i_read = readv_network( p_input->i_handle, p_iovec,
287                                     TS_READ_ONCE, p_method );
288         }
289 #else
290         i_read = readv( p_input->i_handle, p_iovec, TS_READ_ONCE );
291
292         /* Shouldn't happen, but it does - at least under Linux */
293         if( (i_read == -1) && ( (errno == EAGAIN) || (errno = EWOULDBLOCK) ) )
294         {
295             /* just ignore that error */
296             intf_ErrMsg( "input error: 0 bytes read" );
297             i_read = 0;
298         }
299 #endif
300         /* Error */
301         if( i_read == -1 )
302         {
303             intf_ErrMsg( "input error: TS readv error" );
304             p_input->pf_delete_packet( p_input->p_method_data, p_data );
305             return( -1 );
306         }
307         p_input->stream.p_selected_area->i_tell += i_read;
308         i_read /= TS_PACKET_SIZE;
309
310         /* Check correct TS header */
311         for( i_loop = 0; i_loop < i_read; i_loop++ )
312         {
313             if( (*pp_data)->p_demux_start[0] != 0x47 )
314             {
315                 intf_ErrMsg( "input error: bad TS packet (starts with "
316                              "0x%.2x, should be 0x47)",
317                              p_data->p_demux_start[0] );
318             }
319             pp_data = &(*pp_data)->p_next;
320         }
321
322         if( i_read != TS_READ_ONCE )
323         {
324             /* Delete remaining packets */
325             p_input->pf_delete_packet( p_input->p_method_data, *pp_data );
326             *pp_data = NULL;
327         }
328     }
329     return( i_read );
330 }
331