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