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