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