]> git.sesse.net Git - vlc/blob - src/input/input.c
48bb78eff967458b6e28a72b8dc5824ecd91fcda
[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.197 2002/05/21 01:40:17 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 <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     p_input->stream.b_seekable = 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                 /* Reinitialize buffer manager. */
306                 input_AccessReinit( p_input );
307                 
308                 p_input->pf_set_program( p_input, 
309                         p_input->stream.p_new_program );
310
311                 /* Escape all decoders for the stream discontinuity they
312                  * will encounter. */
313                 input_EscapeDiscontinuity( p_input );
314
315                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
316                 {
317                     pgrm_descriptor_t * p_pgrm
318                                             = p_input->stream.pp_programs[i];
319
320                     /* Reinitialize synchro. */
321                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
322                 }
323             }
324             p_input->stream.p_new_program = NULL;
325         }
326         
327         if( p_input->stream.p_new_area )
328         {
329             if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
330             {
331                 input_AccessReinit( p_input );
332
333                 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
334
335                 /* Escape all decoders for the stream discontinuity they
336                  * will encounter. */
337                 input_EscapeDiscontinuity( p_input );
338
339                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
340                 {
341                     pgrm_descriptor_t * p_pgrm
342                                             = p_input->stream.pp_programs[i];
343
344                     /* Reinitialize synchro. */
345                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
346                 }
347             }
348             p_input->stream.p_new_area = NULL;
349         }
350
351         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
352         {
353             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
354             {
355                 off_t i_new_pos;
356
357                 /* Reinitialize buffer manager. */
358                 input_AccessReinit( p_input );
359
360                 i_new_pos = p_input->stream.p_selected_area->i_seek;
361                 vlc_mutex_unlock( &p_input->stream.stream_lock );
362                 p_input->pf_seek( p_input, i_new_pos );
363                 vlc_mutex_lock( &p_input->stream.stream_lock );
364
365                 /* Escape all decoders for the stream discontinuity they
366                  * will encounter. */
367                 input_EscapeDiscontinuity( p_input );
368
369                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
370                 {
371                     pgrm_descriptor_t * p_pgrm
372                                             = p_input->stream.pp_programs[i];
373
374                     /* Reinitialize synchro. */
375                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
376                 }
377             }
378             p_input->stream.p_selected_area->i_seek = NO_SEEK;
379         }
380
381         if( p_input->stream.p_removed_es )
382         {
383             input_UnselectES( p_input, p_input->stream.p_removed_es );
384             p_input->stream.p_removed_es = NULL;
385         }
386
387         if( p_input->stream.p_newly_selected_es )
388         {
389             input_SelectES( p_input, p_input->stream.p_newly_selected_es );
390             p_input->stream.p_newly_selected_es = NULL;
391         }
392
393         if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
394         {
395             if( p_input->stream.b_new_mute )
396             {
397                 input_EscapeAudioDiscontinuity( p_input );
398             }
399
400             vlc_mutex_lock( &p_input->stream.control.control_lock );
401             p_input->stream.control.b_mute = p_input->stream.b_new_mute;
402             vlc_mutex_unlock( &p_input->stream.control.control_lock );
403
404             p_input->stream.b_new_mute = MUTE_NO_CHANGE;
405         }
406
407         vlc_mutex_unlock( &p_input->stream.stream_lock );
408
409         /* Read and demultiplex some data. */
410         i_count = p_input->pf_demux( p_input );
411
412         if( i_count == 0 && p_input->stream.b_seekable )
413         {
414             /* End of file - we do not set b_die because only the
415              * interface is allowed to do so. */
416             intf_WarnMsg( 3, "input: EOF reached" );
417             p_input->b_eof = 1;
418         }
419         else if( i_count < 0 )
420         {
421             p_input->b_error = 1;
422         }
423     }
424
425     if( p_input->b_error || p_input->b_eof )
426     {
427         ErrorThread( p_input );
428     }
429
430     EndThread( p_input );
431
432     DestroyThread( p_input );
433
434     return 0;
435 }
436
437 /*****************************************************************************
438  * InitThread: init the input Thread
439  *****************************************************************************/
440 static int InitThread( input_thread_t * p_input )
441 {
442     /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
443     char * psz_parser = p_input->psz_source;
444
445     /* Skip the plug-in names */
446     while( *psz_parser && *psz_parser != ':' )
447     {
448         psz_parser++;
449     }
450 #ifdef WIN32
451     if( psz_parser - p_input->psz_source == 1 )
452     {
453         intf_WarnMsg( 2, "Drive letter %c: specified in source string",
454                       p_input->psz_source ) ;
455         psz_parser = "";
456     }
457 #endif
458
459     if( !*psz_parser )
460     {
461         p_input->psz_access = p_input->psz_demux = "";
462         p_input->psz_name = p_input->psz_source;
463     }
464     else
465     {
466         *psz_parser++ = '\0';
467
468         /* let's skip '//' */
469         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
470         {
471             psz_parser += 2 ;
472         } 
473
474         p_input->psz_name = psz_parser ;
475
476         /* Come back to parse the access and demux plug-ins */
477         psz_parser = p_input->psz_source;
478
479         if( !*psz_parser )
480         {
481             /* No access */
482             p_input->psz_access = "";
483         }
484         else if( *psz_parser == '/' )
485         {
486             /* No access */
487             p_input->psz_access = "";
488             psz_parser++;
489         }
490         else
491         {
492             p_input->psz_access = psz_parser;
493
494             while( *psz_parser && *psz_parser != '/' )
495             {
496                 psz_parser++;
497             }
498
499             if( *psz_parser == '/' )
500             {
501                 *psz_parser++ = '\0';
502             }
503         }
504
505         if( !*psz_parser )
506         {
507             /* No demux */
508             p_input->psz_demux = "";
509         }
510         else
511         {
512             p_input->psz_demux = psz_parser;
513         }
514     }
515
516     intf_WarnMsg( 2, "input: access `%s', demux `%s', name `%s'",
517                   p_input->psz_access, p_input->psz_demux,
518                   p_input->psz_name );
519
520     if( input_AccessInit( p_input ) == -1 )
521     {
522         return( -1 );
523     }
524
525     /* Find and open appropriate access plug-in. */
526     p_input->p_access_module = module_Need( MODULE_CAPABILITY_ACCESS,
527                                  p_input->psz_access,
528                                  (void *)p_input );
529
530     if( p_input->p_access_module == NULL )
531     {
532         intf_ErrMsg( "input error: no suitable access plug-in for `%s/%s:%s'",
533                      p_input->psz_access, p_input->psz_demux,
534                      p_input->psz_name );
535         return( -1 );
536     }
537
538 #define f p_input->p_access_module->p_functions->access.functions.access
539     p_input->pf_open          = f.pf_open;
540     p_input->pf_close         = f.pf_close;
541     p_input->pf_read          = f.pf_read;
542     p_input->pf_set_area      = f.pf_set_area;
543     p_input->pf_set_program   = f.pf_set_program;
544     p_input->pf_seek          = f.pf_seek;
545 #undef f
546
547     /* Waiting for stream. */
548     if( p_input->i_mtu )
549     {
550         p_input->i_bufsize = p_input->i_mtu;
551     }
552     else
553     {
554         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
555     }
556
557     if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
558     {
559         while( !input_FillBuffer( p_input ) )
560         {
561             if( p_input->b_die || p_input->b_error || p_input->b_eof )
562             {
563                 module_Unneed( p_input->p_access_module );
564                 return( -1 );
565             }
566         }
567     }
568
569     /* Find and open appropriate demux plug-in. */
570     p_input->p_demux_module = module_Need( MODULE_CAPABILITY_DEMUX,
571                                  p_input->psz_demux,
572                                  (void *)p_input );
573
574     if( p_input->p_demux_module == NULL )
575     {
576         intf_ErrMsg( "input error: no suitable demux plug-in for `%s/%s:%s'",
577                      p_input->psz_access, p_input->psz_demux,
578                      p_input->psz_name );
579         module_Unneed( p_input->p_access_module );
580         return( -1 );
581     }
582
583 #define f p_input->p_demux_module->p_functions->demux.functions.demux
584     p_input->pf_init          = f.pf_init;
585     p_input->pf_end           = f.pf_end;
586     p_input->pf_demux         = f.pf_demux;
587     p_input->pf_rewind        = f.pf_rewind;
588 #undef f
589
590     return( 0 );
591 }
592
593 /*****************************************************************************
594  * ErrorThread: RunThread() error loop
595  *****************************************************************************
596  * This function is called when an error occured during thread main's loop.
597  *****************************************************************************/
598 static void ErrorThread( input_thread_t *p_input )
599 {
600     while( !p_input->b_die )
601     {
602         /* Sleep a while */
603         msleep( INPUT_IDLE_SLEEP );
604     }
605 }
606
607 /*****************************************************************************
608  * EndThread: end the input thread
609  *****************************************************************************/
610 static void EndThread( input_thread_t * p_input )
611 {
612     /* Store status */
613     p_input->i_status = THREAD_END;
614
615     if( p_main->b_stats )
616     {
617 #ifdef HAVE_SYS_TIMES_H
618         /* Display statistics */
619         struct tms  cpu_usage;
620         times( &cpu_usage );
621
622         intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
623                       p_input->c_loops,
624                       cpu_usage.tms_utime, cpu_usage.tms_stime );
625 #else
626         intf_StatMsg( "input stats: %d loops", p_input->c_loops );
627 #endif
628
629         input_DumpStream( p_input );
630     }
631
632     /* Free all ES and destroy all decoder threads */
633     input_EndStream( p_input );
634
635     /* Free demultiplexer's data */
636     p_input->pf_end( p_input );
637     module_Unneed( p_input->p_demux_module );
638
639     /* Close the access plug-in */
640     CloseThread( p_input );
641 }
642
643 /*****************************************************************************
644  * CloseThread: close the target
645  *****************************************************************************/
646 static void CloseThread( input_thread_t * p_input )
647 {
648     p_input->pf_close( p_input );
649     module_Unneed( p_input->p_access_module );
650
651     input_AccessEnd( p_input );
652
653     free( p_input->psz_source );
654 }
655
656 /*****************************************************************************
657  * DestroyThread: destroy the input thread
658  *****************************************************************************/
659 static void DestroyThread( input_thread_t * p_input )
660 {
661     /* Update status */
662     p_input->i_status = THREAD_OVER;
663 }
664