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