]> git.sesse.net Git - vlc/blob - src/input/input.c
* Fixed a segfault in input.c when no input plugin was found for
[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.87 2001/02/20 09:10:36 sam 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.p_selected_area = p_input->stream.pp_areas[0];
116     p_input->stream.p_selected_area->i_seek = NO_SEEK;
117
118     /* Initialize stream control properties. */
119     p_input->stream.control.i_status = PLAYING_S;
120     p_input->stream.control.i_rate = DEFAULT_RATE;
121     p_input->stream.control.b_mute = 0;
122     p_input->stream.control.b_bw = 0;
123
124     /* Initialize default settings for spawned decoders */
125     p_input->p_default_aout = p_main->p_aout;
126     p_input->p_default_vout = p_main->p_vout;
127
128     /* Create thread and set locks. */
129     vlc_mutex_init( &p_input->stream.stream_lock );
130     vlc_cond_init( &p_input->stream.stream_wait );
131     vlc_mutex_init( &p_input->stream.control.control_lock );
132     if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
133                            (void *) p_input ) )
134     {
135         intf_ErrMsg( "input error: can't create input thread (%s)",
136                      strerror(errno) );
137         free( p_input );
138         return( NULL );
139     }
140
141     /* If status is NULL, wait until the thread is created */
142     if( pi_status == NULL )
143     {
144         do
145         {
146             msleep( THREAD_SLEEP );
147         } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
148                 && (i_status != THREAD_FATAL) );
149         if( i_status != THREAD_READY )
150         {
151             return( NULL );
152         }
153     }
154     return( p_input );
155 }
156
157 /*****************************************************************************
158  * input_DestroyThread: mark an input thread as zombie
159  *****************************************************************************
160  * This function should not return until the thread is effectively cancelled.
161  *****************************************************************************/
162 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
163 {
164     int         i_status;                                   /* thread status */
165
166     /* Set status */
167     p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
168     *p_input->pi_status = THREAD_DESTROY;
169
170     /* Request thread destruction */
171     p_input->b_die = 1;
172
173     /* Make the thread exit of an eventual vlc_cond_wait() */
174     vlc_mutex_lock( &p_input->stream.stream_lock );
175     vlc_cond_signal( &p_input->stream.stream_wait );
176     vlc_mutex_unlock( &p_input->stream.stream_lock );
177
178     /* If status is NULL, wait until thread has been destroyed */
179     if( pi_status == NULL )
180     {
181         do
182         {
183             msleep( THREAD_SLEEP );
184         } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
185                   && (i_status != THREAD_FATAL) );
186     }
187 }
188
189 /*****************************************************************************
190  * RunThread: main thread loop
191  *****************************************************************************
192  * Thread in charge of processing the network packets and demultiplexing.
193  *****************************************************************************/
194 static void RunThread( input_thread_t *p_input )
195 {
196     data_packet_t *         pp_packets[INPUT_READ_ONCE];
197     int                     i_error, i;
198
199     if( InitThread( p_input ) )
200     {
201
202         /* If we failed, wait before we are killed, and exit */
203         *p_input->pi_status = THREAD_ERROR;
204         p_input->b_error = 1;
205         ErrorThread( p_input );
206         DestroyThread( p_input );
207         return;
208     }
209
210     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
211     {
212
213 #ifdef STATS
214         p_input->c_loops++;
215 #endif
216
217         vlc_mutex_lock( &p_input->stream.stream_lock );
218         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
219         {
220             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
221             {
222                 p_input->pf_seek( p_input,
223                                   p_input->stream.p_selected_area->i_seek );
224
225                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
226                 {
227                     pgrm_descriptor_t * p_pgrm
228                                             = p_input->stream.pp_programs[i];
229                     /* Escape all decoders for the stream discontinuity they
230                      * will encounter. */
231                     input_EscapeDiscontinuity( p_input, p_pgrm );
232
233                     /* Reinitialize synchro. */
234                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
235                 }
236             }
237             p_input->stream.p_selected_area->i_seek = NO_SEEK;
238         }
239         vlc_mutex_unlock( &p_input->stream.stream_lock );
240
241         i_error = p_input->pf_read( p_input, pp_packets );
242
243         /* Demultiplex read packets. */
244         for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
245         {
246             p_input->pf_demux( p_input, pp_packets[i] );
247         }
248
249         if( i_error )
250         {
251             if( i_error == 1 )
252             {
253                 /* End of file - we do not set b_die because only the
254                  * interface is allowed to do so. */
255                 intf_WarnMsg( 1, "End of file reached" );
256                 p_input->b_eof = 1;
257             }
258             else
259             {
260                 p_input->b_error = 1;
261             }
262         }
263     }
264
265     if( p_input->b_error || p_input->b_eof )
266     {
267         ErrorThread( p_input );
268     }
269
270     EndThread( p_input );
271
272     DestroyThread( p_input );
273
274     intf_DbgMsg("Thread end");
275 }
276
277 /*****************************************************************************
278  * InitThread: init the input Thread
279  *****************************************************************************/
280 static int InitThread( input_thread_t * p_input )
281 {
282
283 #ifdef STATS
284     /* Initialize statistics */
285     p_input->c_loops                    = 0;
286     p_input->c_bytes                    = 0;
287     p_input->c_payload_bytes            = 0;
288     p_input->c_packets_read             = 0;
289     p_input->c_packets_trashed          = 0;
290 #endif
291
292     p_input->p_input_module = module_Need( p_main->p_bank,
293                                            MODULE_CAPABILITY_INPUT,
294                                            (probedata_t *)p_input );
295
296     if( p_input->p_input_module == NULL )
297     {
298         intf_ErrMsg( "input error: no suitable input module" );
299         return( -1 );
300     }
301
302 #define f p_input->p_input_module->p_functions->input.functions.input
303     p_input->pf_init          = f.pf_init;
304     p_input->pf_open          = f.pf_open;
305     p_input->pf_close         = f.pf_close;
306     p_input->pf_end           = f.pf_end;
307     p_input->pf_read          = f.pf_read;
308     p_input->pf_demux         = f.pf_demux;
309     p_input->pf_new_packet    = f.pf_new_packet;
310     p_input->pf_new_pes       = f.pf_new_pes;
311     p_input->pf_delete_packet = f.pf_delete_packet;
312     p_input->pf_delete_pes    = f.pf_delete_pes;
313     p_input->pf_rewind        = f.pf_rewind;
314     p_input->pf_seek          = f.pf_seek;
315 #undef f
316
317     p_input->pf_open( p_input );
318
319     if( p_input->b_error )
320     {
321         /* We barfed -- exit nicely */
322         p_input->pf_close( p_input );
323         module_Unneed( p_main->p_bank, p_input->p_input_module );
324         return( -1 );
325     }
326
327     p_input->pf_init( p_input );
328
329     *p_input->pi_status = THREAD_READY;
330
331     return( 0 );
332 }
333
334 /*****************************************************************************
335  * ErrorThread: RunThread() error loop
336  *****************************************************************************
337  * This function is called when an error occured during thread main's loop.
338  *****************************************************************************/
339 static void ErrorThread( input_thread_t *p_input )
340 {
341     while( !p_input->b_die )
342     {
343         /* Sleep a while */
344         msleep( INPUT_IDLE_SLEEP );
345     }
346 }
347
348 /*****************************************************************************
349  * EndThread: end the input thread
350  *****************************************************************************/
351 static void EndThread( input_thread_t * p_input )
352 {
353     int *       pi_status;                                  /* thread status */
354
355     /* Store status */
356     pi_status = p_input->pi_status;
357     *pi_status = THREAD_END;
358
359 #ifdef STATS
360     {
361         struct tms cpu_usage;
362         times( &cpu_usage );
363
364         intf_Msg("input stats: cpu usage (user: %d, system: %d)",
365                  cpu_usage.tms_utime, cpu_usage.tms_stime);
366     }
367 #endif
368
369     /* Free all ES and destroy all decoder threads */
370     input_EndStream( p_input );
371
372     /* Free demultiplexer's data */
373     p_input->pf_end( p_input );
374
375     /* Close stream */
376     p_input->pf_close( p_input );
377
378     /* Release modules */
379     module_Unneed( p_main->p_bank, p_input->p_input_module );
380
381 }
382
383 /*****************************************************************************
384  * DestroyThread: destroy the input thread
385  *****************************************************************************/
386 static void DestroyThread( input_thread_t * p_input )
387 {
388     int *       pi_status;                                  /* thread status */
389
390     /* Store status */
391     pi_status = p_input->pi_status;
392
393     /* Destroy Mutex locks */
394     vlc_mutex_destroy( &p_input->stream.control.control_lock );
395     vlc_mutex_destroy( &p_input->stream.stream_lock );
396     
397     /* Free input structure */
398     free( p_input );
399
400     /* Update status */
401     *pi_status = THREAD_OVER;
402 }
403
404 /*****************************************************************************
405  * input_FileOpen : open a file descriptor
406  *****************************************************************************/
407 void input_FileOpen( input_thread_t * p_input )
408 {
409     struct stat         stat_info;
410     int                 i_stat;
411
412     char *psz_name = p_input->p_source;
413
414     /* FIXME: this code ought to be in the plugin so that code can
415      * be shared with the *_Probe function */
416     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
417     {
418         int i_size = strlen( psz_name );
419
420         if( ( i_size > 4 )
421             && !strncasecmp( psz_name, "dvd:", 4 ) )
422         {
423             /* get rid of the 'dvd:' stuff and try again */
424             psz_name += 4;
425             i_stat = stat( psz_name, &stat_info );
426         }
427         else if( ( i_size > 5 )
428                  && !strncasecmp( psz_name, "file:", 5 ) )
429         {
430             /* get rid of the 'file:' stuff and try again */
431             psz_name += 5;
432             i_stat = stat( psz_name, &stat_info );
433         }
434
435         if( i_stat == (-1) )
436         {
437             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
438                          psz_name, strerror(errno));
439             p_input->b_error = 1;
440             return;
441         }
442     }
443
444     vlc_mutex_lock( &p_input->stream.stream_lock );
445
446     /* If we are here we can control the pace... */
447     p_input->stream.b_pace_control = 1;
448
449     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
450          || S_ISBLK(stat_info.st_mode) )
451     {
452         p_input->stream.b_seekable = 1;
453         p_input->stream.p_selected_area->i_size = stat_info.st_size;
454     }
455     else if( S_ISFIFO(stat_info.st_mode)
456 #ifndef SYS_BEOS
457              || S_ISSOCK(stat_info.st_mode)
458 #endif
459              )
460     {
461         p_input->stream.b_seekable = 0;
462         p_input->stream.p_selected_area->i_size = 0;
463     }
464     else
465     {
466         vlc_mutex_unlock( &p_input->stream.stream_lock );
467         intf_ErrMsg( "input error: unknown file type for `%s'",
468                      psz_name );
469         p_input->b_error = 1;
470         return;
471     }
472
473     p_input->stream.p_selected_area->i_tell = 0;
474     vlc_mutex_unlock( &p_input->stream.stream_lock );
475
476     intf_Msg( "input: opening %s", p_input->p_source );
477     if( (p_input->i_handle = open( psz_name,
478                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
479     {
480         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
481         p_input->b_error = 1;
482         return;
483     }
484
485 }
486
487 /*****************************************************************************
488  * input_FileClose : close a file descriptor
489  *****************************************************************************/
490 void input_FileClose( input_thread_t * p_input )
491 {
492     close( p_input->i_handle );
493
494     return;
495 }
496