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