]> git.sesse.net Git - vlc/blob - src/input/input.c
The input-II. (more info by mail in about an hour)
[vlc] / src / input / input.c
1 /*****************************************************************************
2  * input.c: input thread
3  * Read an MPEG2 stream, demultiplex and parse it before sending it to
4  * decoders.
5  *****************************************************************************
6  * Copyright (C) 1998, 1999, 2000 VideoLAN
7  *
8  * Authors: 
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37
38 #include "config.h"
39 #include "common.h"
40 #include "threads.h"
41 #include "mtime.h"
42
43 #include "intf_msg.h"
44
45 #include "stream_control.h"
46 #include "input_ext-intf.h"
47 #include "input_ext-dec.h"
48
49 #include "input.h"
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static void RunThread   ( input_thread_t *p_input );
55 static void InitThread  ( input_thread_t *p_input );
56 static void ErrorThread ( input_thread_t *p_input );
57 static void EndThread   ( input_thread_t *p_input );
58 static void NetworkOpen ( input_thread_t *p_input );
59 static void FileOpen    ( input_thread_t *p_input );
60
61 /*****************************************************************************
62  * input_CreateThread: creates a new input thread
63  *****************************************************************************
64  * This function creates a new input, and returns a pointer
65  * to its description. On error, it returns NULL.
66  * If pi_status is NULL, then the function will block until the thread is ready.
67  * If not, it will be updated using one of the THREAD_* constants.
68  *****************************************************************************/
69 input_thread_t *input_CreateThread ( input_config_t * p_config, int *pi_status )
70 {
71     input_thread_t *    p_input;                        /* thread descriptor */
72     int                 i_status;                           /* thread status */
73     int                 i;
74
75     /* Allocate descriptor */
76     intf_DbgMsg("\n");
77     p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
78     if( p_input == NULL )
79     {
80         intf_ErrMsg("error: %s\n", strerror(errno));
81         free( p_config );
82         return( NULL );
83     }
84
85     /* Initialize thread properties */
86     p_input->b_die              = 0;
87     p_input->b_error            = 0;
88     /* I have never understood that stuff --Meuuh */
89     p_input->pi_status          = (pi_status != NULL) ? pi_status : &i_status;
90     *p_input->pi_status         = THREAD_CREATE;
91     p_input->p_config = p_config;
92
93     /* Initialize stream description */
94     for( i = 0; i < INPUT_MAX_SELECTED_ES; i++ )
95     {
96         p_input->pp_selected_es[i] = NULL;
97     }
98     for( i= 0; i < INPUT_MAX_ES; i++ )
99     {
100         p_input->p_es[i].i_id = EMPTY_ID;
101     }
102     p_input->stream.i_pgrm_number = 0;
103
104     /* Initialize stream control properties. */
105     p_input->stream.control.i_status = PLAYING_S;
106     p_input->stream.control.i_rate = DEFAULT_RATE;
107     p_input->stream.control.i_ref_sysdate = 0;
108     p_input->stream.control.i_ref_clock = 0;
109     p_input->stream.control.b_mute = 0;
110     p_input->stream.control.b_bw = 0;
111
112     /* Create thread and set locks. */
113     vlc_mutex_init( &p_input->stream.stream_lock );
114     vlc_mutex_init( &p_input->stream.control.control_lock );
115     if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
116                            (void *) p_input ) )
117     {
118         intf_ErrMsg("error: %s\n", strerror(errno) );
119         free( p_input );
120         free( p_config );
121         return( NULL );
122     }
123
124     /* If status is NULL, wait until the thread is created */
125     if( pi_status == NULL )
126     {
127         do
128         {
129             msleep( THREAD_SLEEP );
130         } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
131                 && (i_status != THREAD_FATAL) );
132         if( i_status != THREAD_READY )
133         {
134             return( NULL );
135         }
136     }
137     return( p_input );
138 }
139
140 /*****************************************************************************
141  * input_DestroyThread: mark an input thread as zombie
142  *****************************************************************************
143  * This function should not return until the thread is effectively cancelled.
144  *****************************************************************************/
145 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
146 {
147     int         i_status;                                   /* thread status */
148
149     /* Set status */
150     p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
151     *p_input->pi_status = THREAD_DESTROY;
152
153     /* Request thread destruction */
154     p_input->b_die = 1;
155
156     /* If status is NULL, wait until thread has been destroyed */
157     if( pi_status == NULL )
158     {
159         do
160         {
161             msleep( THREAD_SLEEP );
162         } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
163                   && (i_status != THREAD_FATAL) );
164     }
165 }
166
167 /*****************************************************************************
168  * RunThread: main thread loop
169  *****************************************************************************
170  * Thread in charge of processing the network packets and demultiplexing.
171  *****************************************************************************/
172 static void RunThread( input_thread_t *p_input )
173 {
174     data_packet_t *      pp_packets[INPUT_READ_ONCE];
175
176     InitThread( p_input );
177
178     while( !p_input->b_die && !p_input->b_error )
179     {
180 #ifdef STATS
181         p_input->c_loops++;
182 #endif
183
184         vlc_mutex_lock( &p_input->stream.control.control_lock );
185         if( p_input->stream.control.i_status == BACKWARD_S
186              && p_input->p_plugin->pf_rewind != NULL )
187         {
188             p_input->p_plugin->pf_rewind( p_input );
189             /* FIXME: probably don't do it every loop, but when ? */
190         }
191         vlc_mutex_unlock( &p_input->stream.control.control_lock );
192
193         p_input->p_plugin->pf_read( p_input, pp_packets );
194         if( !p_input->b_error )
195         {
196             int     i;
197
198             for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
199             {
200                 p_input->p_plugin->pf_demux( p_input, pp_packets[i] );
201             }
202         }
203     }
204
205     if( p_input->b_error )
206     {
207         ErrorThread( p_input );
208     }
209
210     EndThread( p_input );
211     intf_DbgMsg("Thread end");
212 }
213
214 /*****************************************************************************
215  * InitThread: init the input thread
216  *****************************************************************************/
217 input_capabilities_t * PSKludge( void );
218 static void InitThread( input_thread_t * p_input )
219 {
220     /* Initialize default settings for spawned decoders */
221     p_input->p_default_aout     = p_input->p_config->p_default_aout;
222     p_input->p_default_vout     = p_input->p_config->p_default_vout;
223
224 #ifdef STATS
225     /* Initialize statistics */
226     p_input->c_loops                    = 0;
227     p_input->c_bytes                    = 0;
228     p_input->c_payload_bytes            = 0;
229     p_input->c_packets_read             = 0;
230     p_input->c_packets_trashed          = 0;
231 #endif
232
233     /* Use the appropriate input method */
234     switch( p_input->p_config->i_method )
235     {
236     case INPUT_METHOD_FILE:                                  /* file methods */
237         FileOpen( p_input );
238         break;
239     case INPUT_METHOD_VLAN_BCAST:                     /* vlan network method */
240 /*        if( !p_main->b_vlans )
241         {
242             intf_ErrMsg("error: vlans are not activated\n");
243             free( p_input );
244             return( NULL );
245         } */ /* la-lala */
246         /* ... pass through */
247     case INPUT_METHOD_UCAST:                              /* network methods */
248     case INPUT_METHOD_MCAST:
249     case INPUT_METHOD_BCAST:
250         NetworkOpen( p_input );
251         break;
252 #ifdef DEBUG
253     default:
254         intf_ErrMsg("Unknow input method");
255         free( p_input->p_config );
256         p_input->b_error = 1;
257         break;
258 #endif
259     }
260
261     free( p_input->p_config );
262
263     /* Probe plugin (FIXME: load plugins before & write this) */
264     p_input->p_plugin = PSKludge();
265     p_input->p_plugin->pf_init( p_input );
266
267     *p_input->pi_status = THREAD_READY;
268 }
269
270 /*****************************************************************************
271  * ErrorThread: RunThread() error loop
272  *****************************************************************************
273  * This function is called when an error occured during thread main's loop.
274  *****************************************************************************/
275 static void ErrorThread( input_thread_t *p_input )
276 {
277     while( !p_input->b_die )
278     {
279         /* Sleep a while */
280         msleep( INPUT_IDLE_SLEEP );
281     }
282 }
283
284 /*****************************************************************************
285  * EndThread: end the input thread
286  *****************************************************************************/
287 static void EndThread( input_thread_t * p_input )
288 {
289     int *       pi_status;                                  /* thread status */
290     int         i_es_loop;                                       /* es index */
291
292     /* Store status */
293     pi_status = p_input->pi_status;
294     *pi_status = THREAD_END;
295
296     /* Destroy all decoder threads */
297     for( i_es_loop = 0;
298          (i_es_loop < INPUT_MAX_ES)
299             && (p_input->pp_selected_es[i_es_loop] != NULL) ;
300          i_es_loop++ )
301     {
302         p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->b_die = 1;
303         /* Make sure the thread leaves the GetByte() function */
304         vlc_mutex_lock( &p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->data_lock);
305         vlc_cond_signal( &p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->data_wait );
306         vlc_mutex_unlock( &p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->data_lock );
307
308         /* Waiting for the thread to exit */
309         vlc_thread_join( p_input->pp_selected_es[i_es_loop]->thread_id );
310         free( p_input->pp_selected_es[i_es_loop]->p_decoder_fifo );
311     }
312
313     /* Free demultiplexer's data */
314
315     /* Update status */
316     *pi_status = THREAD_OVER;
317 }
318
319 /*****************************************************************************
320  * NetworkOpen : open a network socket descriptor
321  *****************************************************************************/
322 static void NetworkOpen( input_thread_t * p_input )
323 {
324     /* straight copy & paste of input_network.c of input-I */
325
326     /* We cannot rewind nor lseek() */
327     p_input->stream.b_seekable = 0;
328     /* We cannot control the pace */
329     p_input->stream.b_pace_control = 0;
330 }
331
332 /*****************************************************************************
333  * FileOpen : open a file descriptor
334  *****************************************************************************/
335 static void FileOpen( input_thread_t * p_input )
336 {
337     struct stat         stat_info;
338
339 #define p_config    p_input->p_config
340
341     if( stat( p_config->p_source, &stat_info ) == (-1) )
342     {
343         intf_ErrMsg("Cannot stat() file %s (%s)", p_config->p_source,
344                     strerror(errno));
345         p_input->b_error = 1;
346         return;
347     }
348
349     vlc_mutex_lock( &p_input->stream.stream_lock );
350
351     /* If we are here we can control the pace... */
352     p_input->stream.b_pace_control = 1;
353
354     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
355          || S_ISBLK(stat_info.st_mode) )
356     {
357         p_input->stream.b_seekable = 1;
358         p_input->stream.i_size = stat_info.st_size;
359     }
360     else if( S_ISFIFO(stat_info.st_mode) || S_ISSOCK(stat_info.st_mode) )
361     {
362         p_input->stream.b_seekable = 0;
363         p_input->stream.i_size = 0;
364     }
365     else
366     {
367         vlc_mutex_unlock( &p_input->stream.stream_lock );
368         intf_ErrMsg("Unknown file type");
369         p_input->b_error = 1;
370         return;
371     }
372
373     p_input->stream.i_tell = 0;
374     vlc_mutex_unlock( &p_input->stream.stream_lock );
375
376     intf_Msg( "Opening file %s", p_config->p_source );
377     if( (p_input->i_handle = open( p_config->p_source,
378                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
379     {
380         intf_ErrMsg("Cannot open file (%s)", strerror(errno));
381         p_input->b_error = 1;
382         return;
383     }
384
385 #undef p_config
386 }