]> git.sesse.net Git - vlc/blob - src/input/input.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[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-2001 VideoLAN
7  * $Id: input.c,v 1.209 2002/07/31 20:56:52 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 <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33
34 #include <vlc/vlc.h>
35
36 #include <string.h>
37 #include <errno.h>
38
39 #ifdef HAVE_SYS_TIMES_H
40 #   include <sys/times.h>
41 #endif
42
43 #include "netutils.h"
44 #include "vlc_playlist.h"
45
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
48 #include "input_ext-dec.h"
49 #include "input_ext-plugins.h"
50
51 #include "interface.h"
52
53 /*****************************************************************************
54  * Local prototypes
55  *****************************************************************************/
56 static  int RunThread       ( input_thread_t *p_input );
57 static  int InitThread      ( input_thread_t *p_input );
58 static void ErrorThread     ( input_thread_t *p_input );
59 static void EndThread       ( input_thread_t *p_input );
60
61 /*****************************************************************************
62  * input_CreateThread: creates a new input thread
63  *****************************************************************************
64  * This function creates a new input, and returns a pointer
65  * to its description. On error, it returns NULL.
66  * If pi_status is NULL, then the function will block until the thread is ready.
67  * If not, it will be updated using one of the THREAD_* constants.
68  *****************************************************************************/
69 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
70                                       playlist_item_t *p_item, int *pi_status )
71 {
72     input_thread_t *    p_input;                        /* thread descriptor */
73     input_info_category_t * p_info;
74
75     /* Allocate descriptor */
76     p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
77     if( p_input == NULL )
78     {
79         msg_Err( p_parent, "out of memory" );
80         return NULL;
81     }
82
83     /* Initialize thread properties */
84     p_input->b_eof      = 0;
85
86     /* Set target */
87     p_input->psz_source = strdup( p_item->psz_name );
88
89     /* Demux */
90     p_input->p_demux = NULL;
91
92     /* Access */
93     p_input->p_access = NULL;
94     
95     p_input->i_bufsize = 0;
96     p_input->i_mtu = 0;
97
98     /* Initialize statistics */
99     p_input->c_loops                    = 0;
100     p_input->stream.c_packets_read      = 0;
101     p_input->stream.c_packets_trashed   = 0;
102
103     /* Set locks. */
104     vlc_mutex_init( p_input, &p_input->stream.stream_lock );
105     vlc_cond_init( p_input, &p_input->stream.stream_wait );
106     vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
107
108     /* Initialize stream description */
109     p_input->stream.b_changed = 0;
110     p_input->stream.i_es_number = 0;
111     p_input->stream.i_selected_es_number = 0;
112     p_input->stream.i_pgrm_number = 0;
113     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
114     p_input->stream.b_new_mute = MUTE_NO_CHANGE;
115     p_input->stream.i_mux_rate = 0;
116     p_input->stream.b_seekable = 0;
117
118     /* no stream, no program, no area, no es */
119     p_input->stream.p_new_program = NULL;
120
121     p_input->stream.i_area_nb = 0;
122     p_input->stream.pp_areas = NULL;
123     p_input->stream.p_selected_area = NULL;
124     p_input->stream.p_new_area = NULL;
125
126     p_input->stream.pp_selected_es = NULL;
127     p_input->stream.p_removed_es = NULL;
128     p_input->stream.p_newly_selected_es = NULL;
129
130     /* By default there is one area in a stream */
131     input_AddArea( p_input );
132     p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
133
134     /* Initialize stream control properties. */
135     p_input->stream.control.i_status = PLAYING_S;
136     p_input->stream.control.i_rate = DEFAULT_RATE;
137     p_input->stream.control.b_mute = 0;
138     p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
139
140     /* Initialize input info */
141     p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
142     if( !p_input->stream.p_info )
143     {
144         msg_Err( p_input, "No memory!" );
145         return NULL;
146     }
147     p_input->stream.p_info->psz_name = strdup("General") ;
148     p_input->stream.p_info->p_info = NULL;
149     p_input->stream.p_info->p_next = NULL;
150
151     msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
152
153     p_info = input_InfoCategory( p_input, "General");
154     input_AddInfo( p_info, "Playlist item", p_input->psz_source );
155     vlc_object_attach( p_input, p_parent );
156
157     /* Create thread and wait for its readiness. */
158     if( vlc_thread_create( p_input, "input", RunThread, VLC_TRUE ) )
159     {
160         msg_Err( p_input, "cannot create input thread (%s)", strerror(errno) );
161         free( p_input );
162         return NULL;
163     }
164
165     return p_input;
166 }
167
168 /*****************************************************************************
169  * input_StopThread: mark an input thread as zombie
170  *****************************************************************************
171  * This function should not return until the thread is effectively cancelled.
172  *****************************************************************************/
173 void input_StopThread( input_thread_t *p_input )
174 {
175     /* Make the thread exit from a possible vlc_cond_wait() */
176     vlc_mutex_lock( &p_input->stream.stream_lock );
177     /* Request thread destruction */
178     p_input->b_die = 1;
179
180     vlc_cond_signal( &p_input->stream.stream_wait );
181     vlc_mutex_unlock( &p_input->stream.stream_lock );
182 }
183
184 /*****************************************************************************
185  * input_DestroyThread: mark an input thread as zombie
186  *****************************************************************************
187  * This function should not return until the thread is effectively cancelled.
188  *****************************************************************************/
189 void input_DestroyThread( input_thread_t *p_input )
190 {
191     /* Join the thread */
192     vlc_thread_join( p_input );
193
194     /* Destroy Mutex locks */
195     vlc_mutex_destroy( &p_input->stream.control.control_lock );
196     vlc_cond_destroy( &p_input->stream.stream_wait );
197     vlc_mutex_destroy( &p_input->stream.stream_lock );
198 }
199
200 /*****************************************************************************
201  * RunThread: main thread loop
202  *****************************************************************************
203  * Thread in charge of processing the network packets and demultiplexing.
204  *****************************************************************************/
205 static int RunThread( input_thread_t *p_input )
206 {
207     /* Signal right now, otherwise we'll get stuck in a peek */
208     vlc_thread_ready( p_input );
209
210     if( InitThread( p_input ) )
211     {
212         /* If we failed, wait before we are killed, and exit */
213         p_input->b_error = 1;
214         ErrorThread( p_input );
215         p_input->b_dead = 1;
216         return 0;
217     }
218
219     /* initialization is complete */
220     vlc_mutex_lock( &p_input->stream.stream_lock );
221     p_input->stream.b_changed = 1;
222     vlc_mutex_unlock( &p_input->stream.stream_lock );
223
224     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
225     {
226         int i, i_count;
227
228         p_input->c_loops++;
229
230         vlc_mutex_lock( &p_input->stream.stream_lock );
231
232         if( p_input->stream.p_new_program )
233         {
234             if( p_input->pf_set_program != NULL )
235             {
236
237                 /* Reinitialize buffer manager. */
238                 input_AccessReinit( p_input );
239                 
240                 p_input->pf_set_program( p_input, 
241                                          p_input->stream.p_new_program );
242
243                 /* Escape all decoders for the stream discontinuity they
244                  * will encounter. */
245                 input_EscapeDiscontinuity( p_input );
246
247                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
248                 {
249                     pgrm_descriptor_t * p_pgrm
250                                             = p_input->stream.pp_programs[i];
251
252                     /* Reinitialize synchro. */
253                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
254                 }
255             }
256             p_input->stream.p_new_program = NULL;
257         }
258         
259         if( p_input->stream.p_new_area )
260         {
261             if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
262             {
263                 input_AccessReinit( p_input );
264
265                 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
266
267                 /* Escape all decoders for the stream discontinuity they
268                  * will encounter. */
269                 input_EscapeDiscontinuity( p_input );
270
271                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
272                 {
273                     pgrm_descriptor_t * p_pgrm
274                                             = p_input->stream.pp_programs[i];
275
276                     /* Reinitialize synchro. */
277                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
278                 }
279             }
280             p_input->stream.p_new_area = NULL;
281         }
282
283         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
284         {
285             if( p_input->stream.b_seekable
286                  && p_input->pf_seek != NULL )
287             {
288                 off_t i_new_pos;
289
290                 /* Reinitialize buffer manager. */
291                 input_AccessReinit( p_input );
292
293                 i_new_pos = p_input->stream.p_selected_area->i_seek;
294                 vlc_mutex_unlock( &p_input->stream.stream_lock );
295                 p_input->pf_seek( p_input, i_new_pos );
296                 vlc_mutex_lock( &p_input->stream.stream_lock );
297
298                 /* Escape all decoders for the stream discontinuity they
299                  * will encounter. */
300                 input_EscapeDiscontinuity( p_input );
301
302                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
303                 {
304                     pgrm_descriptor_t * p_pgrm
305                                             = p_input->stream.pp_programs[i];
306
307                     /* Reinitialize synchro. */
308                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
309                 }
310             }
311             p_input->stream.p_selected_area->i_seek = NO_SEEK;
312         }
313
314         if( p_input->stream.p_removed_es )
315         {
316             input_UnselectES( p_input, p_input->stream.p_removed_es );
317             p_input->stream.p_removed_es = NULL;
318         }
319
320         if( p_input->stream.p_newly_selected_es )
321         {
322             input_SelectES( p_input, p_input->stream.p_newly_selected_es );
323             p_input->stream.p_newly_selected_es = NULL;
324         }
325
326         if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
327         {
328             if( p_input->stream.b_new_mute )
329             {
330                 input_EscapeAudioDiscontinuity( p_input );
331             }
332
333             vlc_mutex_lock( &p_input->stream.control.control_lock );
334             p_input->stream.control.b_mute = p_input->stream.b_new_mute;
335             vlc_mutex_unlock( &p_input->stream.control.control_lock );
336
337             p_input->stream.b_new_mute = MUTE_NO_CHANGE;
338         }
339
340         vlc_mutex_unlock( &p_input->stream.stream_lock );
341
342         /* Read and demultiplex some data. */
343         i_count = p_input->pf_demux( p_input );
344
345         if( i_count == 0 && p_input->stream.b_seekable )
346         {
347             /* End of file - we do not set b_die because only the
348              * playlist is allowed to do so. */
349             msg_Info( p_input, "EOF reached" );
350             p_input->b_eof = 1;
351         }
352         else if( i_count < 0 )
353         {
354             p_input->b_error = 1;
355         }
356     }
357
358     if( p_input->b_error || p_input->b_eof )
359     {
360         ErrorThread( p_input );
361     }
362
363     EndThread( p_input );
364
365     return 0;
366 }
367
368 /*****************************************************************************
369  * InitThread: init the input Thread
370  *****************************************************************************/
371 static int InitThread( input_thread_t * p_input )
372 {
373     /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
374     char * psz_parser = p_input->psz_source;
375
376     /* Skip the plug-in names */
377     while( *psz_parser && *psz_parser != ':' )
378     {
379         psz_parser++;
380     }
381 #ifdef WIN32
382     if( psz_parser - p_input->psz_source == 1 )
383     {
384         msg_Warn( p_input, "drive letter %c: found in source string",
385                            p_input->psz_source ) ;
386         psz_parser = "";
387     }
388 #endif
389
390     if( !*psz_parser )
391     {
392         p_input->psz_access = p_input->psz_demux = "";
393         p_input->psz_name = p_input->psz_source;
394     }
395     else
396     {
397         *psz_parser++ = '\0';
398
399         /* let's skip '//' */
400         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
401         {
402             psz_parser += 2 ;
403         } 
404
405         p_input->psz_name = psz_parser ;
406
407         /* Come back to parse the access and demux plug-ins */
408         psz_parser = p_input->psz_source;
409
410         if( !*psz_parser )
411         {
412             /* No access */
413             p_input->psz_access = "";
414         }
415         else if( *psz_parser == '/' )
416         {
417             /* No access */
418             p_input->psz_access = "";
419             psz_parser++;
420         }
421         else
422         {
423             p_input->psz_access = psz_parser;
424
425             while( *psz_parser && *psz_parser != '/' )
426             {
427                 psz_parser++;
428             }
429
430             if( *psz_parser == '/' )
431             {
432                 *psz_parser++ = '\0';
433             }
434         }
435
436         if( !*psz_parser )
437         {
438             /* No demux */
439             p_input->psz_demux = "";
440         }
441         else
442         {
443             p_input->psz_demux = psz_parser;
444         }
445     }
446
447     msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
448              p_input->psz_access, p_input->psz_demux, p_input->psz_name );
449
450     if( input_AccessInit( p_input ) == -1 )
451     {
452         return -1;
453     }
454
455     /* Find and open appropriate access module */
456     p_input->p_access = module_Need( p_input, "access",
457                                      p_input->psz_access );
458
459     if( p_input->p_access == NULL )
460     {
461         msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
462                  p_input->psz_access, p_input->psz_demux, p_input->psz_name );
463         return -1;
464     }
465
466     /* Waiting for stream. */
467     if( p_input->i_mtu )
468     {
469         p_input->i_bufsize = p_input->i_mtu;
470     }
471     else
472     {
473         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
474     }
475
476     if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
477     {
478         while( !input_FillBuffer( p_input ) )
479         {
480             if( p_input->b_die || p_input->b_error || p_input->b_eof )
481             {
482                 module_Unneed( p_input, p_input->p_access );
483                 return -1;
484             }
485         }
486     }
487
488     /* Find and open appropriate demux module */
489     p_input->p_demux = module_Need( p_input, "demux",
490                                     p_input->psz_demux );
491
492     if( p_input->p_demux== NULL )
493     {
494         msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
495                  p_input->psz_access, p_input->psz_demux, p_input->psz_name );
496         module_Unneed( p_input, p_input->p_access );
497         return -1;
498     }
499
500     return 0;
501 }
502
503 /*****************************************************************************
504  * ErrorThread: RunThread() error loop
505  *****************************************************************************
506  * This function is called when an error occured during thread main's loop.
507  *****************************************************************************/
508 static void ErrorThread( input_thread_t *p_input )
509 {
510     while( !p_input->b_die )
511     {
512         /* Sleep a while */
513         msleep( INPUT_IDLE_SLEEP );
514     }
515 }
516
517 /*****************************************************************************
518  * EndThread: end the input thread
519  *****************************************************************************/
520 static void EndThread( input_thread_t * p_input )
521 {
522 #ifdef HAVE_SYS_TIMES_H
523     /* Display statistics */
524     struct tms  cpu_usage;
525     times( &cpu_usage );
526
527     msg_Dbg( p_input, "%d loops consuming user: %d, system: %d",
528              p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
529 #else
530     msg_Dbg( p_input, "%d loops", p_input->c_loops );
531 #endif
532
533     /* Free info structures */
534     msg_Dbg( p_input, "freeing info structures...");
535     input_DelInfo( p_input );
536     
537     input_DumpStream( p_input );
538
539     /* Free all ES and destroy all decoder threads */
540     input_EndStream( p_input );
541
542     /* Free demultiplexer's data */
543     module_Unneed( p_input, p_input->p_demux );
544
545     /* Close the access plug-in */
546     module_Unneed( p_input, p_input->p_access );
547
548     input_AccessEnd( p_input );
549
550     free( p_input->psz_source );
551
552     /* Tell we're dead */
553     p_input->b_dead = 1;
554 }
555