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