]> git.sesse.net Git - vlc/blob - plugins/mpeg/input_ts.c
* Ported Glide and MGA plugins to the new module API. MGA never worked,
[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.5 2001/02/20 07:49:13 sam Exp $
6  *
7  * Authors: 
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 #include <sys/uio.h>
36
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #include <fcntl.h>
41
42 #include "config.h"
43 #include "common.h"
44 #include "threads.h"
45 #include "mtime.h"
46 #include "tests.h"
47 #include "modules.h"
48
49 #include "intf_msg.h"
50
51 #include "stream_control.h"
52 #include "input_ext-intf.h"
53 #include "input_ext-dec.h"
54
55 #include "input.h"
56 #include "input_ts.h"
57
58 #include "mpeg_system.h"
59 #include "input_netlist.h"
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static int  TSProbe     ( probedata_t * );
65 static int  TSRead      ( struct input_thread_s *,
66                           data_packet_t * p_packets[INPUT_READ_ONCE] );
67 static void TSInit      ( struct input_thread_s * );
68 static void TSEnd       ( struct input_thread_s * );
69
70 /*****************************************************************************
71  * Functions exported as capabilities. They are declared as static so that
72  * we don't pollute the namespace too much.
73  *****************************************************************************/
74 void _M( input_getfunctions )( function_list_t * p_function_list )
75 {
76 #define input p_function_list->functions.input
77     p_function_list->pf_probe = TSProbe;
78     input.pf_init             = TSInit;
79     input.pf_open             = input_FileOpen;
80     input.pf_close            = input_FileClose;
81     input.pf_end              = TSEnd;
82     input.pf_set_area         = NULL;
83     input.pf_read             = TSRead;
84     input.pf_demux            = input_DemuxTS;
85     input.pf_new_packet       = input_NetlistNewPacket;
86     input.pf_new_pes          = input_NetlistNewPES;
87     input.pf_delete_packet    = input_NetlistDeletePacket;
88     input.pf_delete_pes       = input_NetlistDeletePES;
89     input.pf_rewind           = NULL;
90     input.pf_seek             = NULL;
91 #undef input
92 }
93
94 /*****************************************************************************
95  * TSProbe: verifies that the stream is a TS stream
96  *****************************************************************************/
97 static int TSProbe( probedata_t * p_data )
98 {
99     input_thread_t * p_input = (input_thread_t *)p_data;
100
101     char * psz_name = p_input->p_source;
102     int i_handle;
103     int i_score = 1;
104
105     if( TestMethod( INPUT_METHOD_VAR, "ts" ) )
106     {
107         return( 999 );
108     }
109
110     if( ( strlen(psz_name) > 5 ) && !strncasecmp( psz_name, "file:", 5 ) )
111     {
112         /* If the user specified "file:" then it's probably a file */
113         psz_name += 5;
114     }
115
116     i_handle = open( psz_name, 0 );
117     if( i_handle == -1 )
118     {
119         return( 0 );
120     }
121     close( i_handle );
122
123     return( i_score );
124 }
125
126 /*****************************************************************************
127  * TSInit: initializes TS structures
128  *****************************************************************************/
129 static void TSInit( input_thread_t * p_input )
130 {
131     /* Initialize netlist and TS structures */
132     thread_ts_data_t    * p_method;
133     pgrm_ts_data_t      * p_pgrm_demux;
134     es_descriptor_t     * kludge1;
135
136     /* Initialise structure */
137     p_method = malloc( sizeof( thread_ts_data_t ) );
138     if( p_method == NULL )
139     {
140         intf_ErrMsg( "Out of memory" );
141         p_input->b_error = 1;
142         return;
143     }
144  
145     p_input->p_plugin_data = (void *)p_method;
146     p_input->p_method_data = NULL;
147  
148     
149     /* XXX : For the time being, only file, after, i'll do the network */ 
150     
151     /* Initialize netlist */
152     if( input_NetlistInit( p_input, NB_DATA, NB_PES, TS_PACKET_SIZE, 
153                 INPUT_READ_ONCE ) )
154     {
155         intf_ErrMsg( "Could not initialize netlist" );
156         return;
157     }
158    
159     /* Initialize the stream */
160     input_InitStream( p_input, sizeof( stream_ts_data_t ) );
161
162     /* FIXME : PSIDemux and PSIDecode */
163     /* Add audio and video programs */
164     /* p_input->stream.pp_programs[0] = */
165     input_AddProgram( p_input, 0, sizeof( pgrm_ts_data_t ) );
166     p_pgrm_demux = 
167         (pgrm_ts_data_t *)p_input->stream.pp_programs[0]->p_demux_data;
168     p_pgrm_demux->i_pcr_pid = 0x78;
169
170     kludge1 = input_AddES( p_input, p_input->stream.pp_programs[0], 
171                            0x78, sizeof( es_ts_data_t ) );
172
173     // kludge
174     kludge1->i_type = MPEG2_VIDEO_ES;
175
176     input_SelectES( p_input, kludge1 );
177
178     vlc_mutex_lock( &(p_input->stream.stream_lock) );
179     p_input->stream.pp_programs[0]->b_is_ok = 1;
180     vlc_mutex_unlock( &(p_input->stream.stream_lock) );
181
182 //debug
183 intf_ErrMsg("End of TSINIT");    
184 }
185
186 /*****************************************************************************
187  * TSEnd: frees unused data
188  *****************************************************************************/
189 static void TSEnd( input_thread_t * p_input )
190 {
191
192 }
193
194 /*****************************************************************************
195  * TSRead: reads data packets
196  *****************************************************************************
197  * Returns -1 in case of error, 0 if everything went well, and 1 in case of
198  * EOF.
199  *****************************************************************************/
200 static int TSRead( input_thread_t * p_input,
201                    data_packet_t * pp_packets[INPUT_READ_ONCE] )
202 {
203     unsigned int i_read, i_loop;
204     struct iovec * p_iovec;
205     
206     memset( pp_packets, 0, INPUT_READ_ONCE*sizeof(data_packet_t *) );
207     
208     p_iovec = input_NetlistGetiovec( p_input->p_method_data );
209     
210     if ( p_iovec == NULL )
211     {
212         return( -1 ); /* empty netlist */
213     } 
214
215     i_read = readv( p_input->i_handle, p_iovec, INPUT_READ_ONCE );
216     
217     if( i_read == -1 )
218     {
219         intf_ErrMsg( "Could not readv" );
220         return( -1 );
221     }
222
223     input_NetlistMviovec( p_input->p_method_data, 
224             (int)(i_read/TS_PACKET_SIZE) , pp_packets );
225
226     /* check correct TS header */
227     for( i_loop=0; i_loop < (int)(i_read/TS_PACKET_SIZE); i_loop++ )
228     {
229         if( pp_packets[i_loop]->p_buffer[0] != 0x47 )
230             intf_ErrMsg( "Bad TS Packet (starcode != 0x47)." );
231     }
232     return 0;
233 }