]> git.sesse.net Git - vlc/blob - plugins/mpeg/input_ts.c
* vlc core compiles under QNX RTOS. Now if anyone feels like tidying
[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.12 2001/03/19 13:26:59 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 #ifdef SYS_NTO
38 #include <sys/select.h>
39 #endif
40 #include <sys/uio.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44
45 #include "config.h"
46 #include "common.h"
47 #include "threads.h"
48 #include "mtime.h"
49 #include "tests.h"
50 #include "modules.h"
51
52 #include "intf_msg.h"
53
54 #include "stream_control.h"
55 #include "input_ext-intf.h"
56 #include "input_ext-dec.h"
57
58 #include "input.h"
59 #include "input_ts.h"
60
61 #include "mpeg_system.h"
62 #include "input_netlist.h"
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static int  TSProbe     ( probedata_t * );
68 static int  TSRead      ( struct input_thread_s *,
69                           data_packet_t * p_packets[INPUT_READ_ONCE] );
70 static void TSInit      ( struct input_thread_s * );
71 static void TSEnd       ( struct input_thread_s * );
72
73 /*****************************************************************************
74  * Functions exported as capabilities. They are declared as static so that
75  * we don't pollute the namespace too much.
76  *****************************************************************************/
77 void _M( input_getfunctions )( function_list_t * p_function_list )
78 {
79 #define input p_function_list->functions.input
80     p_function_list->pf_probe = TSProbe;
81     input.pf_init             = TSInit;
82 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
83     input.pf_open             = input_NetworkOpen;
84     input.pf_close            = input_NetworkClose;
85 #else
86     input.pf_open             = input_FileOpen;
87     input.pf_close            = input_FileClose;
88 #endif
89     input.pf_end              = TSEnd;
90     input.pf_set_area         = NULL;
91     input.pf_read             = TSRead;
92     input.pf_demux            = input_DemuxTS;
93     input.pf_new_packet       = input_NetlistNewPacket;
94     input.pf_new_pes          = input_NetlistNewPES;
95     input.pf_delete_packet    = input_NetlistDeletePacket;
96     input.pf_delete_pes       = input_NetlistDeletePES;
97     input.pf_rewind           = NULL;
98     input.pf_seek             = NULL;
99 #undef input
100 }
101
102 /*****************************************************************************
103  * TSProbe: verifies that the stream is a TS stream
104  *****************************************************************************/
105 static int TSProbe( probedata_t * p_data )
106 {
107     input_thread_t * p_input = (input_thread_t *)p_data;
108
109     char * psz_name = p_input->p_source;
110     int i_handle;
111     int i_score = 1;
112
113     if( TestMethod( INPUT_METHOD_VAR, "ts" ) )
114     {
115         return( 999 );
116     }
117
118     if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
119     {
120         /* If the user specified "file:" then it's probably a file */
121         psz_name += 5;
122     }
123
124     i_handle = open( psz_name, 0 );
125     if( i_handle == -1 )
126     {
127         return( 0 );
128     }
129     close( i_handle );
130
131     return( i_score );
132 }
133
134 /*****************************************************************************
135  * TSInit: initializes TS structures
136  *****************************************************************************/
137 static void TSInit( input_thread_t * p_input )
138 {
139     /* Initialize netlist and TS structures */
140     thread_ts_data_t    * p_method;
141     es_descriptor_t     * p_pat_es;
142     es_ts_data_t        * p_demux_data;
143     stream_ts_data_t    * p_stream_data;
144
145     /* Initialise structure */
146     p_method = malloc( sizeof( thread_ts_data_t ) );
147     if( p_method == NULL )
148     {
149         intf_ErrMsg( "TS input : Out of memory" );
150         p_input->b_error = 1;
151         return;
152     }
153
154     p_input->p_plugin_data = (void *)p_method;
155     p_input->p_method_data = NULL;
156  
157     
158     /* Initialize netlist */
159     if( input_NetlistInit( p_input, NB_DATA, NB_PES, TS_PACKET_SIZE, 
160                 INPUT_READ_ONCE ) )
161     {
162         intf_ErrMsg( "TS input : Could not initialize netlist" );
163         return;
164     }
165    
166     /* Initialize the stream */
167     input_InitStream( p_input, sizeof( stream_ts_data_t ) );
168
169     /* Init */
170     p_stream_data = (stream_ts_data_t *)p_input->stream.p_demux_data;
171     p_stream_data->i_pat_version = PAT_UNINITIALIZED ;
172
173     /* We'll have to catch the PAT in order to continue 
174      * Then the input will catch the PMT and then the others ES
175      * The PAT es is indepedent of any program. */
176     p_pat_es = input_AddES( p_input, NULL,
177                            0x00, sizeof( es_ts_data_t ) );
178     p_demux_data=(es_ts_data_t *)p_pat_es->p_demux_data;
179     p_demux_data->b_psi = 1;
180     p_demux_data->i_psi_type = PSI_IS_PAT;
181     p_demux_data->p_psi_section = malloc(sizeof(psi_section_t));
182     p_demux_data->p_psi_section->b_is_complete = 1;
183     
184 }
185
186 /*****************************************************************************
187  * TSEnd: frees unused data
188  *****************************************************************************/
189 static void TSEnd( input_thread_t * p_input )
190 {
191     es_descriptor_t     * p_pat_es;
192     
193     p_pat_es = input_FindES( p_input, 0x00 );
194
195     if( p_pat_es != NULL )
196         input_DelES( p_input, p_pat_es );
197     free(p_input->p_plugin_data);
198 }
199
200 /*****************************************************************************
201  * TSRead: reads data packets
202  *****************************************************************************
203  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
204  * EOF.
205  *****************************************************************************/
206 static int TSRead( input_thread_t * p_input,
207                    data_packet_t * pp_packets[INPUT_READ_ONCE] )
208 {
209     thread_ts_data_t    * p_method;
210     unsigned int    i_read, i_loop;
211     int             i_data;
212     struct iovec  * p_iovec;
213     struct timeval  s_wait;
214     
215
216     /* Init */
217     p_method = ( thread_ts_data_t * )p_input->p_plugin_data;
218    
219     /* Initialize file descriptor set */
220     FD_ZERO( &(p_method->s_fdset) );
221     FD_SET( p_input->i_handle, &(p_method->s_fdset) );
222
223     
224     /* We'll wait 0.5 second if nothing happens */
225     s_wait.tv_sec = 0.5;
226     s_wait.tv_usec = 0;
227     
228     /* Reset pointer table */
229     memset( pp_packets, 0, INPUT_READ_ONCE*sizeof(data_packet_t *) );
230     
231     /* Get iovecs */
232     p_iovec = input_NetlistGetiovec( p_input->p_method_data );
233     
234     if ( p_iovec == NULL )
235     {
236         return( -1 ); /* empty netlist */
237     } 
238
239     /* Fill if some data is available */
240     i_data = select(p_input->i_handle + 1, &(p_method->s_fdset), NULL, NULL, 
241                     &s_wait);
242     
243     if( i_data == -1 )
244     {
245         intf_ErrMsg( "TS input : Error in select : %s", strerror(errno) );
246         return( -1 );
247     }
248     
249     if( i_data )
250     {
251         i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
252         
253         if( i_read == -1 )
254         {
255             intf_ErrMsg( "TS input : Could not readv" );
256             return( -1 );
257         }
258     
259         input_NetlistMviovec( p_input->p_method_data, 
260                 (int)(i_read/TS_PACKET_SIZE) , pp_packets );
261     
262         /* check correct TS header */
263         for( i_loop=0; i_loop * TS_PACKET_SIZE < i_read; i_loop++ )
264         {
265             if( pp_packets[i_loop]->p_buffer[0] != 0x47 )
266                 intf_ErrMsg( "TS input : Bad TS Packet (starcode != 0x47)." );
267         }
268     }
269     return 0;
270 }