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