]> git.sesse.net Git - vlc/blob - src/input/input.c
* skip "//" if we find "://" in a resource we have to play : this allows us to...
[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.191 2002/03/18 21:04:01 xav Exp $
8  *
9  * Authors: Christophe Massiot <massiot@via.ecp.fr>
10  *          Alexis Guillard <alexis.guillard@bt.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34
35 #include <videolan/vlc.h>
36
37 #include <string.h>
38 #include <errno.h>
39
40 #ifdef HAVE_SYS_TIMES_H
41 #   include <sys/times.h>
42 #endif
43
44 #include "netutils.h"
45
46 #include "intf_playlist.h"
47
48 #include "stream_control.h"
49 #include "input_ext-intf.h"
50 #include "input_ext-dec.h"
51 #include "input_ext-plugins.h"
52
53 #include "interface.h"
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static  int RunThread       ( input_thread_t *p_input );
59 static  int InitThread      ( input_thread_t *p_input );
60 static void ErrorThread     ( input_thread_t *p_input );
61 static void CloseThread     ( input_thread_t *p_input );
62 static void DestroyThread   ( input_thread_t *p_input );
63 static void EndThread       ( input_thread_t *p_input );
64
65 /*****************************************************************************
66  * input_InitBank: initialize the input bank.
67  *****************************************************************************/
68 void input_InitBank ( void )
69 {
70     p_input_bank->i_count = 0;
71
72     /* XXX: Workaround for old interface modules */
73     p_input_bank->pp_input[0] = NULL;
74
75     vlc_mutex_init( &p_input_bank->lock );
76 }
77
78 /*****************************************************************************
79  * input_EndBank: empty the input bank.
80  *****************************************************************************
81  * This function ends all unused inputs and empties the bank in
82  * case of success.
83  *****************************************************************************/
84 void input_EndBank ( void )
85 {
86     int i_input;
87
88     /* Ask all remaining video outputs to die */
89     for( i_input = 0; i_input < p_input_bank->i_count; i_input++ )
90     {
91         input_StopThread(
92                 p_input_bank->pp_input[ i_input ], NULL );
93         input_DestroyThread(
94                 p_input_bank->pp_input[ i_input ] );
95     }
96
97     vlc_mutex_destroy( &p_input_bank->lock );
98 }
99
100 /*****************************************************************************
101  * input_CreateThread: creates a new input thread
102  *****************************************************************************
103  * This function creates a new input, and returns a pointer
104  * to its description. On error, it returns NULL.
105  * If pi_status is NULL, then the function will block until the thread is ready.
106  * If not, it will be updated using one of the THREAD_* constants.
107  *****************************************************************************/
108 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
109 {
110     input_thread_t *    p_input;                        /* thread descriptor */
111
112     /* Allocate descriptor */
113     p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
114     if( p_input == NULL )
115     {
116         intf_ErrMsg( "input error: can't allocate input thread (%s)",
117                      strerror(errno) );
118         return( NULL );
119     }
120
121     /* Initialize thread properties */
122     p_input->b_die      = 0;
123     p_input->b_error    = 0;
124     p_input->b_eof      = 0;
125
126     /* Set target */
127     p_input->psz_source = strdup( p_item->psz_name );
128
129     /* Set status */
130     p_input->i_status   = THREAD_CREATE;
131
132     /* Demux */
133     p_input->p_demux_module = NULL;
134     p_input->pf_init    = NULL;
135     p_input->pf_end     = NULL;
136     p_input->pf_demux   = NULL;
137     p_input->pf_rewind  = NULL;
138
139     /* Access */
140     p_input->p_access_module = NULL;
141     p_input->pf_open        = NULL;
142     p_input->pf_close       = NULL;
143     p_input->pf_read        = NULL;
144     p_input->pf_seek        = NULL;
145     p_input->pf_set_area    = NULL;
146     p_input->pf_set_program = NULL;
147     
148     p_input->i_bufsize = 0;
149     p_input->i_mtu = 0;
150
151     /* Initialize statistics */
152     p_input->c_loops                    = 0;
153     p_input->stream.c_packets_read      = 0;
154     p_input->stream.c_packets_trashed   = 0;
155
156     /* Set locks. */
157     vlc_mutex_init( &p_input->stream.stream_lock );
158     vlc_cond_init( &p_input->stream.stream_wait );
159     vlc_mutex_init( &p_input->stream.control.control_lock );
160
161     /* Initialize stream description */
162     p_input->stream.b_changed = 0;
163     p_input->stream.i_es_number = 0;
164     p_input->stream.i_selected_es_number = 0;
165     p_input->stream.i_pgrm_number = 0;
166     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
167     p_input->stream.b_new_mute = MUTE_NO_CHANGE;
168     p_input->stream.i_mux_rate = 0;
169
170     /* no stream, no program, no area, no es */
171     p_input->stream.p_new_program = NULL;
172
173     p_input->stream.i_area_nb = 0;
174     p_input->stream.pp_areas = NULL;
175     p_input->stream.p_selected_area = NULL;
176     p_input->stream.p_new_area = NULL;
177
178     p_input->stream.pp_selected_es = NULL;
179     p_input->stream.p_removed_es = NULL;
180     p_input->stream.p_newly_selected_es = NULL;
181
182     /* By default there is one area in a stream */
183     input_AddArea( p_input );
184     p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
185
186     /* Initialize stream control properties. */
187     p_input->stream.control.i_status = PLAYING_S;
188     p_input->stream.control.i_rate = DEFAULT_RATE;
189     p_input->stream.control.b_mute = 0;
190     p_input->stream.control.b_grayscale = config_GetIntVariable( "grayscale" );
191     p_input->stream.control.i_smp = config_GetIntVariable( "vdec_smp" );
192
193     intf_WarnMsg( 1, "input: playlist item `%s'", p_input->psz_source );
194
195     /* Create thread. */
196     if( vlc_thread_create( &p_input->thread_id, "input",
197                            (vlc_thread_func_t)RunThread, (void *) p_input ) )
198     {
199         intf_ErrMsg( "input error: can't create input thread (%s)",
200                      strerror(errno) );
201         free( p_input );
202         return( NULL );
203     }
204
205 #if 0
206     /* If status is NULL, wait until the thread is created */
207     if( pi_status == NULL )
208     {
209         do
210         {
211             msleep( THREAD_SLEEP );
212         } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
213                 && (i_status != THREAD_FATAL) );
214     }
215 #endif
216
217     return( p_input );
218 }
219
220 /*****************************************************************************
221  * input_StopThread: mark an input thread as zombie
222  *****************************************************************************
223  * This function should not return until the thread is effectively cancelled.
224  *****************************************************************************/
225 void input_StopThread( input_thread_t *p_input, int *pi_status )
226 {
227     /* Make the thread exit from a possible vlc_cond_wait() */
228     vlc_mutex_lock( &p_input->stream.stream_lock );
229
230     /* Request thread destruction */
231     p_input->b_die = 1;
232
233     vlc_cond_signal( &p_input->stream.stream_wait );
234     vlc_mutex_unlock( &p_input->stream.stream_lock );
235
236     /* If status is NULL, wait until thread has been destroyed */
237 #if 0
238     if( pi_status == NULL )
239     {
240         do
241         {
242             msleep( THREAD_SLEEP );
243         } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
244                   && (i_status != THREAD_FATAL) );
245     }
246 #endif
247 }
248
249 /*****************************************************************************
250  * input_DestroyThread: mark an input thread as zombie
251  *****************************************************************************
252  * This function should not return until the thread is effectively cancelled.
253  *****************************************************************************/
254 void input_DestroyThread( input_thread_t *p_input )
255 {
256     /* Join the thread */
257     vlc_thread_join( p_input->thread_id );
258
259     /* Destroy Mutex locks */
260     vlc_mutex_destroy( &p_input->stream.control.control_lock );
261     vlc_cond_destroy( &p_input->stream.stream_wait );
262     vlc_mutex_destroy( &p_input->stream.stream_lock );
263     
264     /* Free input structure */
265     free( p_input );
266 }
267
268 /*****************************************************************************
269  * RunThread: main thread loop
270  *****************************************************************************
271  * Thread in charge of processing the network packets and demultiplexing.
272  *****************************************************************************/
273 static int RunThread( input_thread_t *p_input )
274 {
275     if( InitThread( p_input ) )
276     {
277         /* If we failed, wait before we are killed, and exit */
278         p_input->i_status = THREAD_ERROR;
279         p_input->b_error = 1;
280         ErrorThread( p_input );
281         DestroyThread( p_input );
282         return 0;
283     }
284
285     p_input->i_status = THREAD_READY;
286
287     /* initialization is complete */
288     vlc_mutex_lock( &p_input->stream.stream_lock );
289     p_input->stream.b_changed = 1;
290     vlc_mutex_unlock( &p_input->stream.stream_lock );
291
292     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
293     {
294         int i, i_count;
295
296         p_input->c_loops++;
297
298         vlc_mutex_lock( &p_input->stream.stream_lock );
299
300         if( p_input->stream.p_new_program )
301         {
302             if( p_input->pf_set_program != NULL )
303             {
304
305                 p_input->pf_set_program( p_input, 
306                         p_input->stream.p_new_program );
307
308                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
309                 {
310                     pgrm_descriptor_t * p_pgrm
311                                             = p_input->stream.pp_programs[i];
312                     /* Escape all decoders for the stream discontinuity they
313                      * will encounter. */
314                     input_EscapeDiscontinuity( p_input, p_pgrm );
315
316                     /* Reinitialize synchro. */
317                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
318                 }
319             }
320             p_input->stream.p_new_program = NULL;
321         }
322         
323         if( p_input->stream.p_new_area )
324         {
325             if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
326             {
327
328                 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
329
330                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
331                 {
332                     pgrm_descriptor_t * p_pgrm
333                                             = p_input->stream.pp_programs[i];
334                     /* Escape all decoders for the stream discontinuity they
335                      * will encounter. */
336                     input_EscapeDiscontinuity( p_input, p_pgrm );
337
338                     /* Reinitialize synchro. */
339                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
340                 }
341
342                 input_AccessReinit( p_input );
343             }
344             p_input->stream.p_new_area = NULL;
345         }
346
347         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
348         {
349             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
350             {
351                 off_t i_new_pos = p_input->stream.p_selected_area->i_seek;
352                 vlc_mutex_unlock( &p_input->stream.stream_lock );
353                 p_input->pf_seek( p_input, i_new_pos );
354                 vlc_mutex_lock( &p_input->stream.stream_lock );
355
356                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
357                 {
358                     pgrm_descriptor_t * p_pgrm
359                                             = p_input->stream.pp_programs[i];
360                     /* Escape all decoders for the stream discontinuity they
361                      * will encounter. */
362                     input_EscapeDiscontinuity( p_input, p_pgrm );
363
364                     /* Reinitialize synchro. */
365                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
366                 }
367
368                 /* Reinitialize buffer manager. */
369                 input_AccessReinit( p_input );
370             }
371             p_input->stream.p_selected_area->i_seek = NO_SEEK;
372         }
373
374         if( p_input->stream.p_removed_es )
375         {
376             input_UnselectES( p_input, p_input->stream.p_removed_es );
377             p_input->stream.p_removed_es = NULL;
378         }
379
380         if( p_input->stream.p_newly_selected_es )
381         {
382             input_SelectES( p_input, p_input->stream.p_newly_selected_es );
383             p_input->stream.p_newly_selected_es = NULL;
384         }
385
386         if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
387         {
388             if( p_input->stream.b_new_mute )
389             {
390                 input_EscapeAudioDiscontinuity( p_input );
391             }
392
393             vlc_mutex_lock( &p_input->stream.control.control_lock );
394             p_input->stream.control.b_mute = p_input->stream.b_new_mute;
395             vlc_mutex_unlock( &p_input->stream.control.control_lock );
396
397             p_input->stream.b_new_mute = MUTE_NO_CHANGE;
398         }
399
400         vlc_mutex_unlock( &p_input->stream.stream_lock );
401
402         /* Read and demultiplex some data. */
403         i_count = p_input->pf_demux( p_input );
404
405         if( i_count == 0 && p_input->stream.b_seekable )
406         {
407             /* End of file - we do not set b_die because only the
408              * interface is allowed to do so. */
409             intf_WarnMsg( 3, "input: EOF reached" );
410             p_input->b_eof = 1;
411         }
412         else if( i_count < 0 )
413         {
414             p_input->b_error = 1;
415         }
416     }
417
418     if( p_input->b_error || p_input->b_eof )
419     {
420         ErrorThread( p_input );
421     }
422
423     EndThread( p_input );
424
425     DestroyThread( p_input );
426
427     return 0;
428 }
429
430 /*****************************************************************************
431  * InitThread: init the input Thread
432  *****************************************************************************/
433 static int InitThread( input_thread_t * p_input )
434 {
435     /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
436     char * psz_parser = p_input->psz_source;
437
438     /* Skip the plug-in names */
439     while( *psz_parser && *psz_parser != ':' )
440     {
441         psz_parser++;
442     }
443 #ifdef WIN32
444     if( psz_parser - p_input->psz_source == 1 )
445     {
446         intf_WarnMsg( 2, "Drive letter %c: specified in source string",
447                       p_input->psz_source ) ;
448         psz_parser = "";
449     }
450 #endif
451
452     if( !*psz_parser )
453     {
454         p_input->psz_access = p_input->psz_demux = "";
455         p_input->psz_name = p_input->psz_source;
456     }
457     else
458     {
459         *psz_parser++ = '\0';
460
461         /* let's skip '//' */
462         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
463         {
464             psz_parser += 2 ;
465         } 
466
467         p_input->psz_name = psz_parser ;
468
469         /* Come back to parse the access and demux plug-ins */
470         psz_parser = p_input->psz_source;
471
472         if( !*psz_parser )
473         {
474             /* No access */
475             p_input->psz_access = "";
476         }
477         else if( *psz_parser == '/' )
478         {
479             /* No access */
480             p_input->psz_access = "";
481             psz_parser++;
482         }
483         else
484         {
485             p_input->psz_access = psz_parser;
486
487             while( *psz_parser && *psz_parser != '/' )
488             {
489                 psz_parser++;
490             }
491
492             if( *psz_parser == '/' )
493             {
494                 *psz_parser++ = '\0';
495             }
496         }
497
498         if( !*psz_parser )
499         {
500             /* No demux */
501             p_input->psz_demux = "";
502         }
503         else
504         {
505             p_input->psz_demux = psz_parser;
506         }
507     }
508
509     intf_WarnMsg( 2, "input: access `%s', demux `%s', name `%s'",
510                   p_input->psz_access, p_input->psz_demux,
511                   p_input->psz_name );
512
513     if( input_AccessInit( p_input ) == -1 )
514     {
515         return( -1 );
516     }
517
518     /* Find and open appropriate access plug-in. */
519     p_input->p_access_module = module_Need( MODULE_CAPABILITY_ACCESS,
520                                  p_input->psz_access,
521                                  (void *)p_input );
522
523     if( p_input->p_access_module == NULL )
524     {
525         intf_ErrMsg( "input error: no suitable access plug-in for `%s/%s:%s'",
526                      p_input->psz_access, p_input->psz_demux,
527                      p_input->psz_name );
528         return( -1 );
529     }
530
531 #define f p_input->p_access_module->p_functions->access.functions.access
532     p_input->pf_open          = f.pf_open;
533     p_input->pf_close         = f.pf_close;
534     p_input->pf_read          = f.pf_read;
535     p_input->pf_set_area      = f.pf_set_area;
536     p_input->pf_set_program   = f.pf_set_program;
537     p_input->pf_seek          = f.pf_seek;
538 #undef f
539
540     /* Waiting for stream. */
541     if( p_input->i_mtu )
542     {
543         p_input->i_bufsize = p_input->i_mtu;
544     }
545     else
546     {
547         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
548     }
549
550     if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
551     {
552         while( !input_FillBuffer( p_input ) )
553         {
554             if( p_input->b_die || p_input->b_error )
555             {
556                 module_Unneed( p_input->p_access_module );
557                 return( -1 );
558             }
559         }
560     }
561
562     /* Find and open appropriate demux plug-in. */
563     p_input->p_demux_module = module_Need( MODULE_CAPABILITY_DEMUX,
564                                  p_input->psz_demux,
565                                  (void *)p_input );
566
567     if( p_input->p_demux_module == NULL )
568     {
569         intf_ErrMsg( "input error: no suitable demux plug-in for `%s/%s:%s'",
570                      p_input->psz_access, p_input->psz_demux,
571                      p_input->psz_name );
572         module_Unneed( p_input->p_access_module );
573         return( -1 );
574     }
575
576 #define f p_input->p_demux_module->p_functions->demux.functions.demux
577     p_input->pf_init          = f.pf_init;
578     p_input->pf_end           = f.pf_end;
579     p_input->pf_demux         = f.pf_demux;
580     p_input->pf_rewind        = f.pf_rewind;
581 #undef f
582
583     return( 0 );
584 }
585
586 /*****************************************************************************
587  * ErrorThread: RunThread() error loop
588  *****************************************************************************
589  * This function is called when an error occured during thread main's loop.
590  *****************************************************************************/
591 static void ErrorThread( input_thread_t *p_input )
592 {
593     while( !p_input->b_die )
594     {
595         /* Sleep a while */
596         msleep( INPUT_IDLE_SLEEP );
597     }
598 }
599
600 /*****************************************************************************
601  * EndThread: end the input thread
602  *****************************************************************************/
603 static void EndThread( input_thread_t * p_input )
604 {
605     /* Store status */
606     p_input->i_status = THREAD_END;
607
608     if( p_main->b_stats )
609     {
610 #ifdef HAVE_SYS_TIMES_H
611         /* Display statistics */
612         struct tms  cpu_usage;
613         times( &cpu_usage );
614
615         intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
616                       p_input->c_loops,
617                       cpu_usage.tms_utime, cpu_usage.tms_stime );
618 #else
619         intf_StatMsg( "input stats: %d loops", p_input->c_loops );
620 #endif
621
622         input_DumpStream( p_input );
623     }
624
625     /* Free all ES and destroy all decoder threads */
626     input_EndStream( p_input );
627
628     /* Free demultiplexer's data */
629     p_input->pf_end( p_input );
630     module_Unneed( p_input->p_demux_module );
631
632     /* Close the access plug-in */
633     CloseThread( p_input );
634 }
635
636 /*****************************************************************************
637  * CloseThread: close the target
638  *****************************************************************************/
639 static void CloseThread( input_thread_t * p_input )
640 {
641     p_input->pf_close( p_input );
642     module_Unneed( p_input->p_access_module );
643
644     input_AccessEnd( p_input );
645
646     free( p_input->psz_source );
647 }
648
649 /*****************************************************************************
650  * DestroyThread: destroy the input thread
651  *****************************************************************************/
652 static void DestroyThread( input_thread_t * p_input )
653 {
654     /* Update status */
655     p_input->i_status = THREAD_OVER;
656 }
657