]> git.sesse.net Git - vlc/blob - src/input/input.c
First serie of changes in DVD module for the forthcoming interface menus
[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  * $Id: input.c,v 1.85 2001/02/20 02:53:13 stef Exp $
8  *
9  * Authors: Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "defs.h"
30
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <errno.h>
38
39 #ifdef STATS
40 #   include <sys/times.h>
41 #endif
42
43 #include "config.h"
44 #include "common.h"
45 #include "threads.h"
46 #include "mtime.h"
47 #include "modules.h"
48
49 #include "intf_msg.h"
50 #include "intf_plst.h"
51
52 #include "stream_control.h"
53 #include "input_ext-intf.h"
54 #include "input_ext-dec.h"
55
56 #include "input.h"
57 #include "interface.h"
58
59 #include "main.h"
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static void RunThread       ( input_thread_t *p_input );
65 static  int InitThread      ( input_thread_t *p_input );
66 static void ErrorThread     ( input_thread_t *p_input );
67 static void DestroyThread   ( input_thread_t *p_input );
68 static void EndThread       ( input_thread_t *p_input );
69
70 /*****************************************************************************
71  * input_CreateThread: creates a new input thread
72  *****************************************************************************
73  * This function creates a new input, and returns a pointer
74  * to its description. On error, it returns NULL.
75  * If pi_status is NULL, then the function will block until the thread is ready.
76  * If not, it will be updated using one of the THREAD_* constants.
77  *****************************************************************************/
78 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
79 {
80     input_thread_t *    p_input;                        /* thread descriptor */
81     int                 i_status;                           /* thread status */
82
83     /* Allocate descriptor */
84     p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
85     if( p_input == NULL )
86     {
87         intf_ErrMsg( "input error: can't allocate input thread (%s)",
88                      strerror(errno) );
89         return( NULL );
90     }
91
92     /* Initialize thread properties */
93     p_input->b_die              = 0;
94     p_input->b_error            = 0;
95     p_input->b_eof              = 0;
96
97     /* Set target */
98     p_input->p_source           = p_item->psz_name;
99
100     /* I have never understood that stuff --Meuuh */
101     p_input->pi_status          = (pi_status != NULL) ? pi_status : &i_status;
102     *p_input->pi_status         = THREAD_CREATE;
103
104     /* Initialize stream description */
105     p_input->stream.i_es_number = 0;
106     p_input->stream.i_selected_es_number = 0;
107     p_input->stream.i_pgrm_number = 0;
108     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
109     p_input->stream.i_mux_rate = 0;
110
111     p_input->stream.i_area_nb = 0;
112     p_input->stream.pp_areas = NULL;
113     /* By default there is one areas in a stream */
114     input_AddArea( p_input );
115     p_input->stream.pp_areas[0]->i_seek = NO_SEEK;
116
117     /* Initialize stream control properties. */
118     p_input->stream.control.i_status = PLAYING_S;
119     p_input->stream.control.i_rate = DEFAULT_RATE;
120     p_input->stream.control.b_mute = 0;
121     p_input->stream.control.b_bw = 0;
122
123     /* Initialize default settings for spawned decoders */
124     p_input->p_default_aout = p_main->p_aout;
125     p_input->p_default_vout = p_main->p_vout;
126
127     /* Create thread and set locks. */
128     vlc_mutex_init( &p_input->stream.stream_lock );
129     vlc_cond_init( &p_input->stream.stream_wait );
130     vlc_mutex_init( &p_input->stream.control.control_lock );
131     if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
132                            (void *) p_input ) )
133     {
134         intf_ErrMsg( "input error: can't create input thread (%s)",
135                      strerror(errno) );
136         free( p_input );
137         return( NULL );
138     }
139
140     /* If status is NULL, wait until the thread is created */
141     if( pi_status == NULL )
142     {
143         do
144         {
145             msleep( THREAD_SLEEP );
146         } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
147                 && (i_status != THREAD_FATAL) );
148         if( i_status != THREAD_READY )
149         {
150             return( NULL );
151         }
152     }
153     return( p_input );
154 }
155
156 /*****************************************************************************
157  * input_DestroyThread: mark an input thread as zombie
158  *****************************************************************************
159  * This function should not return until the thread is effectively cancelled.
160  *****************************************************************************/
161 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
162 {
163     int         i_status;                                   /* thread status */
164
165     /* Set status */
166     p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
167     *p_input->pi_status = THREAD_DESTROY;
168
169     /* Request thread destruction */
170     p_input->b_die = 1;
171
172     /* Make the thread exit of an eventual vlc_cond_wait() */
173     vlc_mutex_lock( &p_input->stream.stream_lock );
174     vlc_cond_signal( &p_input->stream.stream_wait );
175     vlc_mutex_unlock( &p_input->stream.stream_lock );
176
177     /* If status is NULL, wait until thread has been destroyed */
178     if( pi_status == NULL )
179     {
180         do
181         {
182             msleep( THREAD_SLEEP );
183         } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
184                   && (i_status != THREAD_FATAL) );
185     }
186 }
187
188 /*****************************************************************************
189  * RunThread: main thread loop
190  *****************************************************************************
191  * Thread in charge of processing the network packets and demultiplexing.
192  *****************************************************************************/
193 static void RunThread( input_thread_t *p_input )
194 {
195     data_packet_t *         pp_packets[INPUT_READ_ONCE];
196     int                     i_error, i;
197
198     if( InitThread( p_input ) )
199     {
200
201         /* If we failed, wait before we are killed, and exit */
202         *p_input->pi_status = THREAD_ERROR;
203         p_input->b_error = 1;
204         ErrorThread( p_input );
205         DestroyThread( p_input );
206         return;
207     }
208
209     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
210     {
211
212 #ifdef STATS
213         p_input->c_loops++;
214 #endif
215
216         vlc_mutex_lock( &p_input->stream.stream_lock );
217         if( p_input->stream.pp_areas[0]->i_seek != NO_SEEK )
218         {
219             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
220             {
221                 p_input->pf_seek( p_input, p_input->stream.pp_areas[0]->i_seek );
222
223                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
224                 {
225                     pgrm_descriptor_t * p_pgrm
226                                             = p_input->stream.pp_programs[i];
227                     /* Escape all decoders for the stream discontinuity they
228                      * will encounter. */
229                     input_EscapeDiscontinuity( p_input, p_pgrm );
230
231                     /* Reinitialize synchro. */
232                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
233                 }
234             }
235             p_input->stream.pp_areas[0]->i_seek = NO_SEEK;
236         }
237         vlc_mutex_unlock( &p_input->stream.stream_lock );
238
239         i_error = p_input->pf_read( p_input, pp_packets );
240
241         /* Demultiplex read packets. */
242         for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
243         {
244             p_input->pf_demux( p_input, pp_packets[i] );
245         }
246
247         if( i_error )
248         {
249             if( i_error == 1 )
250             {
251                 /* End of file - we do not set b_die because only the
252                  * interface is allowed to do so. */
253                 intf_WarnMsg( 1, "End of file reached" );
254                 p_input->b_eof = 1;
255             }
256             else
257             {
258                 p_input->b_error = 1;
259             }
260         }
261     }
262
263     if( p_input->b_error || p_input->b_eof )
264     {
265         ErrorThread( p_input );
266     }
267
268     EndThread( p_input );
269
270     DestroyThread( p_input );
271
272     intf_DbgMsg("Thread end");
273 }
274
275 /*****************************************************************************
276  * InitThread: init the input Thread
277  *****************************************************************************/
278 static int InitThread( input_thread_t * p_input )
279 {
280
281 #ifdef STATS
282     /* Initialize statistics */
283     p_input->c_loops                    = 0;
284     p_input->c_bytes                    = 0;
285     p_input->c_payload_bytes            = 0;
286     p_input->c_packets_read             = 0;
287     p_input->c_packets_trashed          = 0;
288 #endif
289
290     p_input->p_input_module = module_Need( p_main->p_bank,
291                                            MODULE_CAPABILITY_INPUT,
292                                            (probedata_t *)p_input );
293
294     if( p_input->p_input_module == NULL )
295     {
296         intf_ErrMsg( "input error: no suitable input module" );
297         module_Unneed( p_main->p_bank, p_input->p_input_module );
298         return( -1 );
299     }
300
301 #define f p_input->p_input_module->p_functions->input.functions.input
302     p_input->pf_init          = f.pf_init;
303     p_input->pf_open          = f.pf_open;
304     p_input->pf_close         = f.pf_close;
305     p_input->pf_end           = f.pf_end;
306     p_input->pf_read          = f.pf_read;
307     p_input->pf_demux         = f.pf_demux;
308     p_input->pf_new_packet    = f.pf_new_packet;
309     p_input->pf_new_pes       = f.pf_new_pes;
310     p_input->pf_delete_packet = f.pf_delete_packet;
311     p_input->pf_delete_pes    = f.pf_delete_pes;
312     p_input->pf_rewind        = f.pf_rewind;
313     p_input->pf_seek          = f.pf_seek;
314 #undef f
315
316     p_input->pf_open( p_input );
317
318     if( p_input->b_error )
319     {
320         /* We barfed -- exit nicely */
321         p_input->pf_close( p_input );
322         module_Unneed( p_main->p_bank, p_input->p_input_module );
323         return( -1 );
324     }
325
326     p_input->pf_init( p_input );
327
328     *p_input->pi_status = THREAD_READY;
329
330     return( 0 );
331 }
332
333 /*****************************************************************************
334  * ErrorThread: RunThread() error loop
335  *****************************************************************************
336  * This function is called when an error occured during thread main's loop.
337  *****************************************************************************/
338 static void ErrorThread( input_thread_t *p_input )
339 {
340     while( !p_input->b_die )
341     {
342         /* Sleep a while */
343         msleep( INPUT_IDLE_SLEEP );
344     }
345 }
346
347 /*****************************************************************************
348  * EndThread: end the input thread
349  *****************************************************************************/
350 static void EndThread( input_thread_t * p_input )
351 {
352     int *       pi_status;                                  /* thread status */
353
354     /* Store status */
355     pi_status = p_input->pi_status;
356     *pi_status = THREAD_END;
357
358 #ifdef STATS
359     {
360         struct tms cpu_usage;
361         times( &cpu_usage );
362
363         intf_Msg("input stats: cpu usage (user: %d, system: %d)",
364                  cpu_usage.tms_utime, cpu_usage.tms_stime);
365     }
366 #endif
367
368     /* Free all ES and destroy all decoder threads */
369     input_EndStream( p_input );
370
371     /* Free demultiplexer's data */
372     p_input->pf_end( p_input );
373
374     /* Close stream */
375     p_input->pf_close( p_input );
376
377     /* Release modules */
378     module_Unneed( p_main->p_bank, p_input->p_input_module );
379
380 }
381
382 /*****************************************************************************
383  * DestroyThread: destroy the input thread
384  *****************************************************************************/
385 static void DestroyThread( input_thread_t * p_input )
386 {
387     int *       pi_status;                                  /* thread status */
388
389     /* Store status */
390     pi_status = p_input->pi_status;
391
392     /* Destroy Mutex locks */
393     vlc_mutex_destroy( &p_input->stream.control.control_lock );
394     vlc_mutex_destroy( &p_input->stream.stream_lock );
395     
396     /* Free input structure */
397     free( p_input );
398
399     /* Update status */
400     *pi_status = THREAD_OVER;
401 }
402
403 /*****************************************************************************
404  * input_FileOpen : open a file descriptor
405  *****************************************************************************/
406 void input_FileOpen( input_thread_t * p_input )
407 {
408     struct stat         stat_info;
409     int                 i_stat;
410
411     char *psz_name = p_input->p_source;
412
413     /* FIXME: this code ought to be in the plugin so that code can
414      * be shared with the *_Probe function */
415     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
416     {
417         int i_size = strlen( psz_name );
418
419         if( ( i_size > 4 )
420             && !strncasecmp( psz_name, "dvd:", 4 ) )
421         {
422             /* get rid of the 'dvd:' stuff and try again */
423             psz_name += 4;
424             i_stat = stat( psz_name, &stat_info );
425         }
426         else if( ( i_size > 5 )
427                  && !strncasecmp( psz_name, "file:", 5 ) )
428         {
429             /* get rid of the 'file:' stuff and try again */
430             psz_name += 5;
431             i_stat = stat( psz_name, &stat_info );
432         }
433
434         if( i_stat == (-1) )
435         {
436             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
437                          psz_name, strerror(errno));
438             p_input->b_error = 1;
439             return;
440         }
441     }
442
443     vlc_mutex_lock( &p_input->stream.stream_lock );
444
445     /* If we are here we can control the pace... */
446     p_input->stream.b_pace_control = 1;
447
448     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
449          || S_ISBLK(stat_info.st_mode) )
450     {
451         p_input->stream.b_seekable = 1;
452         p_input->stream.pp_areas[0]->i_size = stat_info.st_size;
453     }
454     else if( S_ISFIFO(stat_info.st_mode)
455 #ifndef SYS_BEOS
456              || S_ISSOCK(stat_info.st_mode)
457 #endif
458              )
459     {
460         p_input->stream.b_seekable = 0;
461         p_input->stream.pp_areas[0]->i_size = 0;
462     }
463     else
464     {
465         vlc_mutex_unlock( &p_input->stream.stream_lock );
466         intf_ErrMsg( "input error: unknown file type for `%s'",
467                      psz_name );
468         p_input->b_error = 1;
469         return;
470     }
471
472     p_input->stream.pp_areas[0]->i_tell = 0;
473     vlc_mutex_unlock( &p_input->stream.stream_lock );
474
475     intf_Msg( "input: opening %s", p_input->p_source );
476     if( (p_input->i_handle = open( psz_name,
477                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
478     {
479         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
480         p_input->b_error = 1;
481         return;
482     }
483
484 }
485
486 /*****************************************************************************
487  * input_FileClose : close a file descriptor
488  *****************************************************************************/
489 void input_FileClose( input_thread_t * p_input )
490 {
491     close( p_input->i_handle );
492
493     return;
494 }
495