]> git.sesse.net Git - vlc/blob - src/input/input.c
c59cf469370e2101660fa6f04b216027ebc4d728
[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.208 2002/07/25 21:53:53 sigmunau 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 "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 EndThread       ( input_thread_t *p_input );
60
61 /*****************************************************************************
62  * input_CreateThread: creates a new input thread
63  *****************************************************************************
64  * This function creates a new input, and returns a pointer
65  * to its description. On error, it returns NULL.
66  * If pi_status is NULL, then the function will block until the thread is ready.
67  * If not, it will be updated using one of the THREAD_* constants.
68  *****************************************************************************/
69 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
70                                       playlist_item_t *p_item, int *pi_status )
71 {
72     input_thread_t *    p_input;                        /* thread descriptor */
73     input_info_category_t * p_info;
74
75     /* Allocate descriptor */
76     p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
77     if( p_input == NULL )
78     {
79         msg_Err( p_parent, "out of memory" );
80         return NULL;
81     }
82
83     /* Initialize thread properties */
84     p_input->b_eof      = 0;
85
86     /* Set target */
87     p_input->psz_source = strdup( p_item->psz_name );
88
89     /* Demux */
90     p_input->p_demux_module = NULL;
91     p_input->pf_init    = NULL;
92     p_input->pf_end     = NULL;
93     p_input->pf_demux   = NULL;
94     p_input->pf_rewind  = NULL;
95
96     /* Access */
97     p_input->p_access_module = NULL;
98     p_input->pf_open        = NULL;
99     p_input->pf_close       = NULL;
100     p_input->pf_read        = NULL;
101     p_input->pf_seek        = NULL;
102     p_input->pf_set_area    = NULL;
103     p_input->pf_set_program = NULL;
104     
105     p_input->i_bufsize = 0;
106     p_input->i_mtu = 0;
107
108     /* Initialize statistics */
109     p_input->c_loops                    = 0;
110     p_input->stream.c_packets_read      = 0;
111     p_input->stream.c_packets_trashed   = 0;
112
113     /* Set locks. */
114     vlc_mutex_init( p_input, &p_input->stream.stream_lock );
115     vlc_cond_init( p_input, &p_input->stream.stream_wait );
116     vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
117
118     /* Initialize stream description */
119     p_input->stream.b_changed = 0;
120     p_input->stream.i_es_number = 0;
121     p_input->stream.i_selected_es_number = 0;
122     p_input->stream.i_pgrm_number = 0;
123     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
124     p_input->stream.b_new_mute = MUTE_NO_CHANGE;
125     p_input->stream.i_mux_rate = 0;
126     p_input->stream.b_seekable = 0;
127
128     /* no stream, no program, no area, no es */
129     p_input->stream.p_new_program = NULL;
130
131     p_input->stream.i_area_nb = 0;
132     p_input->stream.pp_areas = NULL;
133     p_input->stream.p_selected_area = NULL;
134     p_input->stream.p_new_area = NULL;
135
136     p_input->stream.pp_selected_es = NULL;
137     p_input->stream.p_removed_es = NULL;
138     p_input->stream.p_newly_selected_es = NULL;
139
140     /* By default there is one area in a stream */
141     input_AddArea( p_input );
142     p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
143
144     /* Initialize stream control properties. */
145     p_input->stream.control.i_status = PLAYING_S;
146     p_input->stream.control.i_rate = DEFAULT_RATE;
147     p_input->stream.control.b_mute = 0;
148     p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
149     p_input->stream.control.i_smp = config_GetInt( p_input, "vdec-smp" );
150
151     /* Initialize input info */
152     p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
153     if( !p_input->stream.p_info )
154     {
155         msg_Err( p_input, "No memory!" );
156         return NULL;
157     }
158     p_input->stream.p_info->psz_name = strdup("General") ;
159     p_input->stream.p_info->p_info = NULL;
160     p_input->stream.p_info->p_next = NULL;
161
162     msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
163
164     p_info = input_InfoCategory( p_input, "General");
165     input_AddInfo( p_info, "Playlist item", p_input->psz_source );
166     vlc_object_attach( p_input, p_parent );
167
168     /* Create thread and wait for its readiness. */
169     if( vlc_thread_create( p_input, "input", RunThread, VLC_TRUE ) )
170     {
171         msg_Err( p_input, "cannot create input thread (%s)", strerror(errno) );
172         free( p_input );
173         return NULL;
174     }
175
176     return p_input;
177 }
178
179 /*****************************************************************************
180  * input_StopThread: mark an input thread as zombie
181  *****************************************************************************
182  * This function should not return until the thread is effectively cancelled.
183  *****************************************************************************/
184 void input_StopThread( input_thread_t *p_input )
185 {
186     /* Make the thread exit from a possible vlc_cond_wait() */
187     vlc_mutex_lock( &p_input->stream.stream_lock );
188     /* Request thread destruction */
189     p_input->b_die = 1;
190
191     vlc_cond_signal( &p_input->stream.stream_wait );
192     vlc_mutex_unlock( &p_input->stream.stream_lock );
193 }
194
195 /*****************************************************************************
196  * input_DestroyThread: mark an input thread as zombie
197  *****************************************************************************
198  * This function should not return until the thread is effectively cancelled.
199  *****************************************************************************/
200 void input_DestroyThread( input_thread_t *p_input )
201 {
202     /* Join the thread */
203     vlc_thread_join( p_input );
204
205     /* Destroy Mutex locks */
206     vlc_mutex_destroy( &p_input->stream.control.control_lock );
207     vlc_cond_destroy( &p_input->stream.stream_wait );
208     vlc_mutex_destroy( &p_input->stream.stream_lock );
209 }
210
211 /*****************************************************************************
212  * RunThread: main thread loop
213  *****************************************************************************
214  * Thread in charge of processing the network packets and demultiplexing.
215  *****************************************************************************/
216 static int RunThread( input_thread_t *p_input )
217 {
218     /* Signal right now, otherwise we'll get stuck in a peek */
219     vlc_thread_ready( p_input );
220
221     if( InitThread( p_input ) )
222     {
223         /* If we failed, wait before we are killed, and exit */
224         p_input->b_error = 1;
225         ErrorThread( p_input );
226         p_input->b_dead = 1;
227         return 0;
228     }
229
230     /* initialization is complete */
231     vlc_mutex_lock( &p_input->stream.stream_lock );
232     p_input->stream.b_changed = 1;
233     vlc_mutex_unlock( &p_input->stream.stream_lock );
234
235     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
236     {
237         int i, i_count;
238
239         p_input->c_loops++;
240
241         vlc_mutex_lock( &p_input->stream.stream_lock );
242
243         if( p_input->stream.p_new_program )
244         {
245             if( p_input->pf_set_program != NULL )
246             {
247
248                 /* Reinitialize buffer manager. */
249                 input_AccessReinit( p_input );
250                 
251                 p_input->pf_set_program( p_input, 
252                         p_input->stream.p_new_program );
253
254                 /* Escape all decoders for the stream discontinuity they
255                  * will encounter. */
256                 input_EscapeDiscontinuity( p_input );
257
258                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
259                 {
260                     pgrm_descriptor_t * p_pgrm
261                                             = p_input->stream.pp_programs[i];
262
263                     /* Reinitialize synchro. */
264                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
265                 }
266             }
267             p_input->stream.p_new_program = NULL;
268         }
269         
270         if( p_input->stream.p_new_area )
271         {
272             if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
273             {
274                 input_AccessReinit( p_input );
275
276                 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
277
278                 /* Escape all decoders for the stream discontinuity they
279                  * will encounter. */
280                 input_EscapeDiscontinuity( p_input );
281
282                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
283                 {
284                     pgrm_descriptor_t * p_pgrm
285                                             = p_input->stream.pp_programs[i];
286
287                     /* Reinitialize synchro. */
288                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
289                 }
290             }
291             p_input->stream.p_new_area = NULL;
292         }
293
294         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
295         {
296             if( p_input->stream.b_seekable && 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              * interface 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 =
467         module_Need( p_input, MODULE_CAPABILITY_ACCESS,
468                      p_input->psz_access, (void *)p_input );
469
470     if( p_input->p_access_module == NULL )
471     {
472         msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
473                  p_input->psz_access, p_input->psz_demux, p_input->psz_name );
474         return -1;
475     }
476
477 #define f p_input->p_access_module->p_functions->access.functions.access
478     p_input->pf_open          = f.pf_open;
479     p_input->pf_close         = f.pf_close;
480     p_input->pf_read          = f.pf_read;
481     p_input->pf_set_area      = f.pf_set_area;
482     p_input->pf_set_program   = f.pf_set_program;
483     p_input->pf_seek          = f.pf_seek;
484 #undef f
485
486     /* Waiting for stream. */
487     if( p_input->i_mtu )
488     {
489         p_input->i_bufsize = p_input->i_mtu;
490     }
491     else
492     {
493         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
494     }
495
496     if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
497     {
498         while( !input_FillBuffer( p_input ) )
499         {
500             if( p_input->b_die || p_input->b_error || p_input->b_eof )
501             {
502                 module_Unneed( p_input->p_access_module );
503                 return -1;
504             }
505         }
506     }
507
508     /* Find and open appropriate demux module */
509     p_input->p_demux_module =
510         module_Need( p_input, MODULE_CAPABILITY_DEMUX,
511                      p_input->psz_demux, (void *)p_input );
512
513     if( p_input->p_demux_module == NULL )
514     {
515         msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
516                  p_input->psz_access, p_input->psz_demux, p_input->psz_name );
517         module_Unneed( p_input->p_access_module );
518         return -1;
519     }
520
521 #define f p_input->p_demux_module->p_functions->demux.functions.demux
522     p_input->pf_init          = f.pf_init;
523     p_input->pf_end           = f.pf_end;
524     p_input->pf_demux         = f.pf_demux;
525     p_input->pf_rewind        = f.pf_rewind;
526 #undef f
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     /* Free demultiplexer's data */
571     p_input->pf_end( p_input );
572     module_Unneed( p_input->p_demux_module );
573
574     /* Close the access plug-in */
575     p_input->pf_close( p_input );
576     module_Unneed( p_input->p_access_module );
577
578     input_AccessEnd( p_input );
579
580     free( p_input->psz_source );
581
582     /* Tell we're dead */
583     p_input->b_dead = 1;
584 }
585