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