]> git.sesse.net Git - vlc/blob - src/input/input.c
* ./include/vlc_common.h: defined the INSERT_ELEM and REMOVE_ELEM macros
[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-2002 VideoLAN
7  * $Id: input.c,v 1.213 2002/10/08 16:02:05 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,
162                            VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
163     {
164         msg_Err( p_input, "cannot create input thread (%s)", strerror(errno) );
165         free( p_input );
166         return NULL;
167     }
168
169     return p_input;
170 }
171
172 /*****************************************************************************
173  * input_StopThread: mark an input thread as zombie
174  *****************************************************************************
175  * This function should not return until the thread is effectively cancelled.
176  *****************************************************************************/
177 void input_StopThread( input_thread_t *p_input )
178 {
179     /* Make the thread exit from a possible vlc_cond_wait() */
180     vlc_mutex_lock( &p_input->stream.stream_lock );
181     /* Request thread destruction */
182     p_input->b_die = 1;
183
184     vlc_cond_signal( &p_input->stream.stream_wait );
185     vlc_mutex_unlock( &p_input->stream.stream_lock );
186 }
187
188 /*****************************************************************************
189  * input_DestroyThread: mark an input thread as zombie
190  *****************************************************************************
191  * This function should not return until the thread is effectively cancelled.
192  *****************************************************************************/
193 void input_DestroyThread( input_thread_t *p_input )
194 {
195     /* Join the thread */
196     vlc_thread_join( p_input );
197
198     /* Destroy Mutex locks */
199     vlc_mutex_destroy( &p_input->stream.control.control_lock );
200     vlc_cond_destroy( &p_input->stream.stream_wait );
201     vlc_mutex_destroy( &p_input->stream.stream_lock );
202 }
203
204 /*****************************************************************************
205  * RunThread: main thread loop
206  *****************************************************************************
207  * Thread in charge of processing the network packets and demultiplexing.
208  *****************************************************************************/
209 static int RunThread( input_thread_t *p_input )
210 {
211     /* Signal right now, otherwise we'll get stuck in a peek */
212     vlc_thread_ready( p_input );
213
214     if( InitThread( p_input ) )
215     {
216         /* If we failed, wait before we are killed, and exit */
217         p_input->b_error = 1;
218         ErrorThread( p_input );
219         p_input->b_dead = 1;
220         return 0;
221     }
222
223     /* initialization is complete */
224     vlc_mutex_lock( &p_input->stream.stream_lock );
225     p_input->stream.b_changed = 1;
226     vlc_mutex_unlock( &p_input->stream.stream_lock );
227
228     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
229     {
230         int i, i_count;
231
232         p_input->c_loops++;
233
234         vlc_mutex_lock( &p_input->stream.stream_lock );
235
236         if( p_input->stream.p_new_program )
237         {
238             if( p_input->pf_set_program != NULL )
239             {
240
241                 /* Reinitialize buffer manager. */
242                 input_AccessReinit( p_input );
243                 
244                 p_input->pf_set_program( p_input, 
245                                          p_input->stream.p_new_program );
246
247                 /* Escape all decoders for the stream discontinuity they
248                  * will encounter. */
249                 input_EscapeDiscontinuity( p_input );
250
251                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
252                 {
253                     pgrm_descriptor_t * p_pgrm
254                                             = p_input->stream.pp_programs[i];
255
256                     /* Reinitialize synchro. */
257                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
258                 }
259             }
260             p_input->stream.p_new_program = NULL;
261         }
262         
263         if( p_input->stream.p_new_area )
264         {
265             if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
266             {
267                 input_AccessReinit( p_input );
268
269                 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
270
271                 /* Escape all decoders for the stream discontinuity they
272                  * will encounter. */
273                 input_EscapeDiscontinuity( p_input );
274
275                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
276                 {
277                     pgrm_descriptor_t * p_pgrm
278                                             = p_input->stream.pp_programs[i];
279
280                     /* Reinitialize synchro. */
281                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
282                 }
283             }
284             p_input->stream.p_new_area = NULL;
285         }
286
287         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
288         {
289             if( p_input->stream.b_seekable
290                  && p_input->pf_seek != NULL )
291             {
292                 off_t i_new_pos;
293
294                 /* Reinitialize buffer manager. */
295                 input_AccessReinit( p_input );
296
297                 i_new_pos = p_input->stream.p_selected_area->i_seek;
298                 vlc_mutex_unlock( &p_input->stream.stream_lock );
299                 p_input->pf_seek( p_input, i_new_pos );
300                 vlc_mutex_lock( &p_input->stream.stream_lock );
301
302                 /* Escape all decoders for the stream discontinuity they
303                  * will encounter. */
304                 input_EscapeDiscontinuity( p_input );
305
306                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
307                 {
308                     pgrm_descriptor_t * p_pgrm
309                                             = p_input->stream.pp_programs[i];
310
311                     /* Reinitialize synchro. */
312                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
313                 }
314             }
315             p_input->stream.p_selected_area->i_seek = NO_SEEK;
316         }
317
318         if( p_input->stream.p_removed_es )
319         {
320             input_UnselectES( p_input, p_input->stream.p_removed_es );
321             p_input->stream.p_removed_es = NULL;
322         }
323
324         if( p_input->stream.p_newly_selected_es )
325         {
326             input_SelectES( p_input, p_input->stream.p_newly_selected_es );
327             p_input->stream.p_newly_selected_es = NULL;
328         }
329
330         if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
331         {
332             if( p_input->stream.b_new_mute )
333             {
334                 input_EscapeAudioDiscontinuity( p_input );
335             }
336
337             vlc_mutex_lock( &p_input->stream.control.control_lock );
338             p_input->stream.control.b_mute = p_input->stream.b_new_mute;
339             vlc_mutex_unlock( &p_input->stream.control.control_lock );
340
341             p_input->stream.b_new_mute = MUTE_NO_CHANGE;
342         }
343
344         vlc_mutex_unlock( &p_input->stream.stream_lock );
345
346         /* Read and demultiplex some data. */
347         i_count = p_input->pf_demux( p_input );
348
349         if( i_count == 0 && p_input->stream.b_seekable )
350         {
351             /* End of file - we do not set b_die because only the
352              * playlist is allowed to do so. */
353             msg_Info( p_input, "EOF reached" );
354             p_input->b_eof = 1;
355         }
356         else if( i_count < 0 )
357         {
358             p_input->b_error = 1;
359         }
360     }
361
362     if( p_input->b_error || p_input->b_eof )
363     {
364         ErrorThread( p_input );
365     }
366
367     EndThread( p_input );
368
369     return 0;
370 }
371
372 /*****************************************************************************
373  * InitThread: init the input Thread
374  *****************************************************************************/
375 static int InitThread( input_thread_t * p_input )
376 {
377     /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
378     char * psz_parser = p_input->psz_source;
379
380     /* Skip the plug-in names */
381     while( *psz_parser && *psz_parser != ':' )
382     {
383         psz_parser++;
384     }
385 #ifdef WIN32
386     if( psz_parser - p_input->psz_source == 1 )
387     {
388         msg_Warn( p_input, "drive letter %c: found in source string",
389                            p_input->psz_source ) ;
390         psz_parser = "";
391     }
392 #endif
393
394     if( !*psz_parser )
395     {
396         p_input->psz_access = p_input->psz_demux = "";
397         p_input->psz_name = p_input->psz_source;
398     }
399     else
400     {
401         *psz_parser++ = '\0';
402
403         /* let's skip '//' */
404         if( psz_parser[0] == '/' && psz_parser[1] == '/' )
405         {
406             psz_parser += 2 ;
407         } 
408
409         p_input->psz_name = psz_parser ;
410
411         /* Come back to parse the access and demux plug-ins */
412         psz_parser = p_input->psz_source;
413
414         if( !*psz_parser )
415         {
416             /* No access */
417             p_input->psz_access = "";
418         }
419         else if( *psz_parser == '/' )
420         {
421             /* No access */
422             p_input->psz_access = "";
423             psz_parser++;
424         }
425         else
426         {
427             p_input->psz_access = psz_parser;
428
429             while( *psz_parser && *psz_parser != '/' )
430             {
431                 psz_parser++;
432             }
433
434             if( *psz_parser == '/' )
435             {
436                 *psz_parser++ = '\0';
437             }
438         }
439
440         if( !*psz_parser )
441         {
442             /* No demux */
443             p_input->psz_demux = "";
444         }
445         else
446         {
447             p_input->psz_demux = psz_parser;
448         }
449     }
450
451     msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
452              p_input->psz_access, p_input->psz_demux, p_input->psz_name );
453
454     if( input_AccessInit( p_input ) == -1 )
455     {
456         return -1;
457     }
458
459     /* Find and open appropriate access module */
460     p_input->p_access = module_Need( p_input, "access",
461                                      p_input->psz_access );
462
463     if( p_input->p_access == NULL )
464     {
465         msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
466                  p_input->psz_access, p_input->psz_demux, p_input->psz_name );
467         return -1;
468     }
469
470     /* Waiting for stream. */
471     if( p_input->i_mtu )
472     {
473         p_input->i_bufsize = p_input->i_mtu;
474     }
475     else
476     {
477         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
478     }
479
480     if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
481     {
482         while( !input_FillBuffer( p_input ) )
483         {
484             if( p_input->b_die || p_input->b_error || p_input->b_eof )
485             {
486                 module_Unneed( p_input, p_input->p_access );
487                 return -1;
488             }
489         }
490     }
491
492     /* Find and open appropriate demux module */
493     p_input->p_demux = module_Need( p_input, "demux",
494                                     p_input->psz_demux );
495
496     if( p_input->p_demux== NULL )
497     {
498         msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
499                  p_input->psz_access, p_input->psz_demux, p_input->psz_name );
500         module_Unneed( p_input, p_input->p_access );
501         return -1;
502     }
503
504     /* Initialize optional stream output. */
505     psz_parser = config_GetPsz( p_input, "sout" );
506     if ( psz_parser != NULL )
507     {
508         if ( *psz_parser &&
509              (p_input->stream.p_sout = sout_NewInstance( p_input, psz_parser ))
510                == NULL )
511         {
512             msg_Err( p_input, "cannot start stream output instance, aborting" );
513             free( psz_parser );
514             module_Unneed( p_input, p_input->p_access );
515             module_Unneed( p_input, p_input->p_demux );
516             return -1;
517         }
518
519         free( psz_parser );
520     }
521
522     return 0;
523 }
524
525 /*****************************************************************************
526  * ErrorThread: RunThread() error loop
527  *****************************************************************************
528  * This function is called when an error occured during thread main's loop.
529  *****************************************************************************/
530 static void ErrorThread( input_thread_t *p_input )
531 {
532     while( !p_input->b_die )
533     {
534         /* Sleep a while */
535         msleep( INPUT_IDLE_SLEEP );
536     }
537 }
538
539 /*****************************************************************************
540  * EndThread: end the input thread
541  *****************************************************************************/
542 static void EndThread( input_thread_t * p_input )
543 {
544 #ifdef HAVE_SYS_TIMES_H
545     /* Display statistics */
546     struct tms  cpu_usage;
547     times( &cpu_usage );
548
549     msg_Dbg( p_input, "%d loops consuming user: %d, system: %d",
550              p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
551 #else
552     msg_Dbg( p_input, "%d loops", p_input->c_loops );
553 #endif
554
555     /* Free info structures */
556     msg_Dbg( p_input, "freeing info structures...");
557     input_DelInfo( p_input );
558     
559     input_DumpStream( p_input );
560
561     /* Free all ES and destroy all decoder threads */
562     input_EndStream( p_input );
563
564     /* Close optional stream output instance */
565     if ( p_input->stream.p_sout != NULL )
566     {
567         sout_DeleteInstance( p_input->stream.p_sout );
568     }
569
570     /* Free demultiplexer's data */
571     module_Unneed( p_input, p_input->p_demux );
572
573     /* Close the access plug-in */
574     module_Unneed( p_input, p_input->p_access );
575
576     input_AccessEnd( p_input );
577
578     free( p_input->psz_source );
579
580     /* Tell we're dead */
581     p_input->b_dead = 1;
582 }
583