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