]> git.sesse.net Git - vlc/blob - src/input/input.c
* Pause function implemented ('p' key).
[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.77 2001/02/08 13:08:02 massiot Exp $
8  *
9  * Authors: 
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 void InitThread  ( input_thread_t *p_input );
66 static void ErrorThread ( input_thread_t *p_input );
67 static void EndThread   ( input_thread_t *p_input );
68
69 /*****************************************************************************
70  * input_CreateThread: creates a new input thread
71  *****************************************************************************
72  * This function creates a new input, and returns a pointer
73  * to its description. On error, it returns NULL.
74  * If pi_status is NULL, then the function will block until the thread is ready.
75  * If not, it will be updated using one of the THREAD_* constants.
76  *****************************************************************************/
77 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
78 {
79     input_thread_t *    p_input;                        /* thread descriptor */
80     int                 i_status;                           /* thread status */
81
82     /* Allocate descriptor */
83     p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
84     if( p_input == NULL )
85     {
86         intf_ErrMsg( "input error: can't allocate input thread (%s)",
87                      strerror(errno) );
88         return( NULL );
89     }
90
91     /* Initialize thread properties */
92     p_input->b_die              = 0;
93     p_input->b_error            = 0;
94     p_input->b_eof              = 0;
95
96     /* Set target */
97     p_input->p_source           = p_item->psz_name;
98
99     /* I have never understood that stuff --Meuuh */
100     p_input->pi_status          = (pi_status != NULL) ? pi_status : &i_status;
101     *p_input->pi_status         = THREAD_CREATE;
102
103     /* Initialize stream description */
104     p_input->stream.i_es_number = 0;
105     p_input->stream.i_selected_es_number = 0;
106     p_input->stream.i_pgrm_number = 0;
107     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
108     p_input->stream.i_seek = 0;
109
110     /* Initialize stream control properties. */
111     p_input->stream.control.i_status = PLAYING_S;
112     p_input->stream.control.i_rate = DEFAULT_RATE;
113     p_input->stream.control.b_mute = 0;
114     p_input->stream.control.b_bw = 0;
115
116     /* Initialize default settings for spawned decoders */
117     p_input->p_default_aout = p_main->p_aout;
118     p_input->p_default_vout = p_main->p_intf->p_vout;
119
120     /* Create thread and set locks. */
121     vlc_mutex_init( &p_input->stream.stream_lock );
122     vlc_cond_init( &p_input->stream.stream_wait );
123     vlc_mutex_init( &p_input->stream.control.control_lock );
124     if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
125                            (void *) p_input ) )
126     {
127         intf_ErrMsg( "input error: can't create input thread (%s)",
128                      strerror(errno) );
129         free( p_input );
130         return( NULL );
131     }
132
133     /* If status is NULL, wait until the thread is created */
134     if( pi_status == NULL )
135     {
136         do
137         {
138             msleep( THREAD_SLEEP );
139         } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
140                 && (i_status != THREAD_FATAL) );
141         if( i_status != THREAD_READY )
142         {
143             return( NULL );
144         }
145     }
146     return( p_input );
147 }
148
149 /*****************************************************************************
150  * input_DestroyThread: mark an input thread as zombie
151  *****************************************************************************
152  * This function should not return until the thread is effectively cancelled.
153  *****************************************************************************/
154 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
155 {
156     int         i_status;                                   /* thread status */
157
158     /* Set status */
159     p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
160     *p_input->pi_status = THREAD_DESTROY;
161
162     /* Request thread destruction */
163     p_input->b_die = 1;
164
165     /* Make the thread exit of an eventual vlc_cond_wait() */
166     vlc_mutex_lock( &p_input->stream.stream_lock );
167     vlc_cond_signal( &p_input->stream.stream_wait );
168     vlc_mutex_unlock( &p_input->stream.stream_lock );
169
170     /* If status is NULL, wait until thread has been destroyed */
171     if( pi_status == NULL )
172     {
173         do
174         {
175             msleep( THREAD_SLEEP );
176         } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
177                   && (i_status != THREAD_FATAL) );
178     }
179 }
180
181 /*****************************************************************************
182  * RunThread: main thread loop
183  *****************************************************************************
184  * Thread in charge of processing the network packets and demultiplexing.
185  *****************************************************************************/
186 static void RunThread( input_thread_t *p_input )
187 {
188     data_packet_t *         pp_packets[INPUT_READ_ONCE];
189     int                     i_error, i;
190
191     InitThread( p_input );
192
193     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
194     {
195
196 #ifdef STATS
197         p_input->c_loops++;
198 #endif
199
200         vlc_mutex_lock( &p_input->stream.control.control_lock );
201         if( p_input->stream.control.i_status == BACKWARD_S
202              && p_input->pf_rewind != NULL )
203         {
204             p_input->pf_rewind( p_input );
205             /* FIXME: probably don't do it every loop, but when ? */
206         }
207         vlc_mutex_unlock( &p_input->stream.control.control_lock );
208
209         i_error = p_input->pf_read( p_input, pp_packets );
210
211         /* Demultiplex read packets. */
212         for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
213         {
214             p_input->pf_demux( p_input, pp_packets[i] );
215         }
216
217         if( i_error )
218         {
219             if( i_error == 1 )
220             {
221                 /* End of file - we do not set b_die because only the
222                  * interface is allowed to do so. */
223                 intf_WarnMsg( 1, "End of file reached" );
224                 p_input->b_eof = 1;
225             }
226             else
227             {
228                 p_input->b_error = 1;
229             }
230         }
231     }
232
233     if( p_input->b_error || p_input->b_eof )
234     {
235         ErrorThread( p_input );
236     }
237
238     EndThread( p_input );
239     intf_DbgMsg("Thread end");
240 }
241
242 /*****************************************************************************
243  * InitThread: init the input Thread
244  *****************************************************************************/
245 static void InitThread( input_thread_t * p_input )
246 {
247
248 #ifdef STATS
249     /* Initialize statistics */
250     p_input->c_loops                    = 0;
251     p_input->c_bytes                    = 0;
252     p_input->c_payload_bytes            = 0;
253     p_input->c_packets_read             = 0;
254     p_input->c_packets_trashed          = 0;
255 #endif
256
257     p_input->p_input_module = module_Need( p_main->p_module_bank,
258                                            MODULE_CAPABILITY_INPUT, NULL );
259
260     if( p_input->p_input_module == NULL )
261     {
262         intf_ErrMsg( "input error: no suitable input module" );
263         p_input->b_error = 1;
264         return;
265     }
266
267 #define f p_input->p_input_module->p_functions->input.functions.input
268     p_input->pf_init          = f.pf_init;
269     p_input->pf_open          = f.pf_open;
270     p_input->pf_close         = f.pf_close;
271     p_input->pf_end           = f.pf_end;
272     p_input->pf_read          = f.pf_read;
273     p_input->pf_demux         = f.pf_demux;
274     p_input->pf_new_packet    = f.pf_new_packet;
275     p_input->pf_new_pes       = f.pf_new_pes;
276     p_input->pf_delete_packet = f.pf_delete_packet;
277     p_input->pf_delete_pes    = f.pf_delete_pes;
278     p_input->pf_rewind        = f.pf_rewind;
279     p_input->pf_seek          = f.pf_seek;
280 #undef f
281
282     p_input->pf_open( p_input );
283
284     if( p_input->b_error )
285     {
286         module_Unneed( p_main->p_module_bank, p_input->p_input_module );
287     }
288     else
289     {
290         p_input->pf_init( p_input );
291     }
292
293     *p_input->pi_status = THREAD_READY;
294 }
295
296 /*****************************************************************************
297  * ErrorThread: RunThread() error loop
298  *****************************************************************************
299  * This function is called when an error occured during thread main's loop.
300  *****************************************************************************/
301 static void ErrorThread( input_thread_t *p_input )
302 {
303     while( !p_input->b_die )
304     {
305         /* Sleep a while */
306         msleep( INPUT_IDLE_SLEEP );
307     }
308 }
309
310 /*****************************************************************************
311  * EndThread: end the input thread
312  *****************************************************************************/
313 static void EndThread( input_thread_t * p_input )
314 {
315     int *       pi_status;                                  /* thread status */
316
317     /* Store status */
318     pi_status = p_input->pi_status;
319     *pi_status = THREAD_END;
320
321 #ifdef STATS
322     {
323         struct tms cpu_usage;
324         times( &cpu_usage );
325
326         intf_Msg("input stats: cpu usage (user: %d, system: %d)",
327                  cpu_usage.tms_utime, cpu_usage.tms_stime);
328     }
329 #endif
330
331     /* Free all ES and destroy all decoder threads */
332     input_EndStream( p_input );
333
334     /* Close stream */
335     p_input->pf_close( p_input );
336
337     /* Free demultiplexer's data */
338     p_input->pf_end( p_input );
339
340     /* Release modules */
341     module_Unneed( p_main->p_module_bank, p_input->p_input_module );
342
343     /* Destroy Mutex locks */
344     vlc_mutex_destroy( &p_input->stream.control.control_lock );
345     vlc_mutex_destroy( &p_input->stream.stream_lock );
346     
347     /* Free input structure */
348     free( p_input );
349
350     /* Update status */
351     *pi_status = THREAD_OVER;
352 }
353
354 /*****************************************************************************
355  * input_FileOpen : open a file descriptor
356  *****************************************************************************/
357 void input_FileOpen( input_thread_t * p_input )
358 {
359     struct stat         stat_info;
360
361     if( stat( p_input->p_source, &stat_info ) == (-1) )
362     {
363         intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
364                      p_input->p_source, strerror(errno));
365         p_input->b_error = 1;
366         return;
367     }
368
369     vlc_mutex_lock( &p_input->stream.stream_lock );
370
371     /* If we are here we can control the pace... */
372     p_input->stream.b_pace_control = 1;
373
374     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
375          || S_ISBLK(stat_info.st_mode) )
376     {
377         p_input->stream.b_seekable = 1;
378         p_input->stream.i_size = stat_info.st_size;
379     }
380     else if( S_ISFIFO(stat_info.st_mode) || S_ISSOCK(stat_info.st_mode) )
381     {
382         p_input->stream.b_seekable = 0;
383         p_input->stream.i_size = 0;
384     }
385     else
386     {
387         vlc_mutex_unlock( &p_input->stream.stream_lock );
388         intf_ErrMsg( "input error: unknown file type for `%s'",
389                      p_input->p_source );
390         p_input->b_error = 1;
391         return;
392     }
393
394     p_input->stream.i_tell = 0;
395     vlc_mutex_unlock( &p_input->stream.stream_lock );
396
397     intf_Msg( "input: opening file %s", p_input->p_source );
398     if( (p_input->i_handle = open( p_input->p_source,
399                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
400     {
401         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
402         p_input->b_error = 1;
403         return;
404     }
405
406 }
407
408 /*****************************************************************************
409  * input_FileClose : close a file descriptor
410  *****************************************************************************/
411 void input_FileClose( input_thread_t * p_input )
412 {
413     close( p_input->i_handle );
414
415     return;
416 }
417