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