]> git.sesse.net Git - vlc/blob - src/input/input.c
* Changed the way decoder_fifo_t works ;
[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.165 2001/12/12 02:13:50 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 "defs.h"
30
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35
36 #ifdef HAVE_UNISTD_H
37 #   include <unistd.h>
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
39 #   include <io.h>
40 #endif
41
42 #include <string.h>
43 #include <errno.h>
44
45 #ifdef STRNCASECMP_IN_STRINGS_H
46 #   include <strings.h>
47 #endif
48
49 #ifdef WIN32
50 #   include <winsock2.h>
51 #   include <ws2tcpip.h>
52 #elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
53 #   include <netdb.h>                                         /* hostent ... */
54 #   include <sys/socket.h>
55 #   include <netinet/in.h>
56 #   ifdef HAVE_ARPA_INET_H
57 #       include <arpa/inet.h>                    /* inet_ntoa(), inet_aton() */
58 #   endif
59 #endif
60
61 #ifdef HAVE_SYS_TIMES_H
62 #   include <sys/times.h>
63 #endif
64
65 #include "common.h"
66 #include "intf_msg.h"
67 #include "threads.h"
68 #include "mtime.h"
69 #include "tests.h"
70 #include "netutils.h"
71 #include "modules.h"
72
73 #include "intf_playlist.h"
74
75 #include "stream_control.h"
76 #include "input_ext-intf.h"
77 #include "input_ext-dec.h"
78 #include "input_ext-plugins.h"
79
80 #include "interface.h"
81
82 /*****************************************************************************
83  * Local prototypes
84  *****************************************************************************/
85 static void RunThread       ( input_thread_t *p_input );
86 static  int InitThread      ( input_thread_t *p_input );
87 static void ErrorThread     ( input_thread_t *p_input );
88 static void CloseThread     ( input_thread_t *p_input );
89 static void DestroyThread   ( input_thread_t *p_input );
90 static void EndThread       ( input_thread_t *p_input );
91
92 static void FileOpen        ( input_thread_t *p_input );
93 static void StdOpen         ( input_thread_t *p_input );
94 static void FileClose       ( input_thread_t *p_input );
95 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
96 static void NetworkOpen     ( input_thread_t *p_input );
97 static void HTTPOpen        ( input_thread_t *p_input );
98 static void NetworkClose    ( input_thread_t *p_input );
99 #endif
100
101 /*****************************************************************************
102  * input_CreateThread: creates a new input thread
103  *****************************************************************************
104  * This function creates a new input, and returns a pointer
105  * to its description. On error, it returns NULL.
106  * If pi_status is NULL, then the function will block until the thread is ready.
107  * If not, it will be updated using one of the THREAD_* constants.
108  *****************************************************************************/
109 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
110 {
111     input_thread_t *    p_input;                        /* thread descriptor */
112     int                 i_status;                           /* thread status */
113
114     /* Allocate descriptor */
115     p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
116     if( p_input == NULL )
117     {
118         intf_ErrMsg( "input error: can't allocate input thread (%s)",
119                      strerror(errno) );
120         return( NULL );
121     }
122
123     /* Packets read once */
124     p_input->i_read_once = INPUT_READ_ONCE;
125
126     /* Initialize thread properties */
127     p_input->b_die              = 0;
128     p_input->b_error            = 0;
129     p_input->b_eof              = 0;
130
131     /* Set target */
132     p_input->p_source           = p_item->psz_name;
133
134     /* I have never understood that stuff --Meuuh */
135     p_input->pi_status          = (pi_status != NULL) ? pi_status : &i_status;
136     *p_input->pi_status         = THREAD_CREATE;
137
138     /* Initialize stream description */
139     p_input->stream.i_es_number = 0;
140     p_input->stream.i_selected_es_number = 0;
141     p_input->stream.i_pgrm_number = 0;
142     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
143     p_input->stream.b_new_mute = MUTE_NO_CHANGE;
144     p_input->stream.i_mux_rate = 0;
145
146     /* no stream, no area */
147     p_input->stream.i_area_nb = 0;
148     p_input->stream.pp_areas = NULL;
149     p_input->stream.p_selected_area = NULL;
150     p_input->stream.p_new_area = NULL;
151
152     /* By default there is one area in a stream */
153     input_AddArea( p_input );
154     p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
155
156     /* Initialize stream control properties. */
157     p_input->stream.control.i_status = PLAYING_S;
158     p_input->stream.control.i_rate = DEFAULT_RATE;
159     p_input->stream.control.b_mute = 0;
160     p_input->stream.control.b_grayscale = main_GetIntVariable(
161                             VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
162     p_input->stream.control.i_smp = main_GetIntVariable(
163                             VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
164
165     intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
166
167     /* Create thread. */
168     if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
169                            (void *) p_input ) )
170     {
171         intf_ErrMsg( "input error: can't create input thread (%s)",
172                      strerror(errno) );
173         free( p_input );
174         return( NULL );
175     }
176
177     /* If status is NULL, wait until the thread is created */
178     if( pi_status == NULL )
179     {
180         do
181         {
182             msleep( THREAD_SLEEP );
183         } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
184                 && (i_status != THREAD_FATAL) );
185         if( i_status != THREAD_READY )
186         {
187             return( NULL );
188         }
189     }
190     return( p_input );
191 }
192
193 /*****************************************************************************
194  * input_DestroyThread: mark an input thread as zombie
195  *****************************************************************************
196  * This function should not return until the thread is effectively cancelled.
197  *****************************************************************************/
198 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
199 {
200     int         i_status;                                   /* thread status */
201
202     /* Set status */
203     p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
204     *p_input->pi_status = THREAD_DESTROY;
205
206     /* Request thread destruction */
207     p_input->b_die = 1;
208
209     /* Make the thread exit of an eventual vlc_cond_wait() */
210     vlc_mutex_lock( &p_input->stream.stream_lock );
211     vlc_cond_signal( &p_input->stream.stream_wait );
212     vlc_mutex_unlock( &p_input->stream.stream_lock );
213
214     /* If status is NULL, wait until thread has been destroyed */
215     if( pi_status == NULL )
216     {
217         do
218         {
219             msleep( THREAD_SLEEP );
220         } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
221                   && (i_status != THREAD_FATAL) );
222     }
223 }
224
225 /*****************************************************************************
226  * RunThread: main thread loop
227  *****************************************************************************
228  * Thread in charge of processing the network packets and demultiplexing.
229  *****************************************************************************/
230 static void RunThread( input_thread_t *p_input )
231 {
232     int                     i_error, i;
233     data_packet_t **        pp_packets;
234
235     if( InitThread( p_input ) )
236     {
237         /* If we failed, wait before we are killed, and exit */
238         *p_input->pi_status = THREAD_ERROR;
239         p_input->b_error = 1;
240         ErrorThread( p_input );
241         DestroyThread( p_input );
242         return;
243     }
244
245     /* initialization is completed */
246     vlc_mutex_lock( &p_input->stream.stream_lock );
247     p_input->stream.b_changed = 1;
248     vlc_mutex_unlock( &p_input->stream.stream_lock );
249
250     pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
251                                         sizeof( data_packet_t * ) );
252     if( pp_packets == NULL )
253     {
254         intf_ErrMsg( "input error: out of memory" );
255         free( pp_packets );
256         p_input->b_error = 1;
257     }
258
259     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
260     {
261         p_input->c_loops++;
262
263         vlc_mutex_lock( &p_input->stream.stream_lock );
264
265         if( p_input->stream.p_new_program )
266         {
267             if( p_input->pf_set_program != NULL )
268             {
269
270                 p_input->pf_set_program( p_input, 
271                         p_input->stream.p_new_program );
272
273                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
274                 {
275                     pgrm_descriptor_t * p_pgrm
276                                             = p_input->stream.pp_programs[i];
277                     /* Escape all decoders for the stream discontinuity they
278                      * will encounter. */
279                     input_EscapeDiscontinuity( p_input, p_pgrm );
280
281                     /* Reinitialize synchro. */
282                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
283                 }
284             }
285             p_input->stream.p_new_program = NULL;
286         }
287         
288         if( p_input->stream.p_new_area )
289         {
290             if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
291             {
292
293                 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
294
295                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
296                 {
297                     pgrm_descriptor_t * p_pgrm
298                                             = p_input->stream.pp_programs[i];
299                     /* Escape all decoders for the stream discontinuity they
300                      * will encounter. */
301                     input_EscapeDiscontinuity( p_input, p_pgrm );
302
303                     /* Reinitialize synchro. */
304                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
305                 }
306             }
307             p_input->stream.p_new_area = NULL;
308         }
309
310         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
311         {
312             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
313             {
314                 p_input->pf_seek( p_input,
315                                   p_input->stream.p_selected_area->i_seek );
316
317                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
318                 {
319                     pgrm_descriptor_t * p_pgrm
320                                             = p_input->stream.pp_programs[i];
321                     /* Escape all decoders for the stream discontinuity they
322                      * will encounter. */
323                     input_EscapeDiscontinuity( p_input, p_pgrm );
324
325                     /* Reinitialize synchro. */
326                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
327                 }
328             }
329             p_input->stream.p_selected_area->i_seek = NO_SEEK;
330         }
331
332         if( p_input->stream.p_removed_es )
333         {
334             input_UnselectES( p_input, p_input->stream.p_removed_es );
335             p_input->stream.p_removed_es = NULL;
336         }
337
338         if( p_input->stream.p_newly_selected_es )
339         {
340             input_SelectES( p_input, p_input->stream.p_newly_selected_es );
341             p_input->stream.p_newly_selected_es = NULL;
342         }
343
344         if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
345         {
346             if( p_input->stream.b_new_mute )
347             {
348                 input_EscapeAudioDiscontinuity( p_input );
349             }
350
351             vlc_mutex_lock( &p_input->stream.control.control_lock );
352             p_input->stream.control.b_mute = p_input->stream.b_new_mute;
353             vlc_mutex_unlock( &p_input->stream.control.control_lock );
354
355             p_input->stream.b_new_mute = MUTE_NO_CHANGE;
356         }
357
358         vlc_mutex_unlock( &p_input->stream.stream_lock );
359
360         i_error = p_input->pf_read( p_input, pp_packets );
361
362         /* Demultiplex read packets. */
363         for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
364         {
365             p_input->stream.c_packets_read++;
366             p_input->pf_demux( p_input, pp_packets[i] );
367         }
368
369         if( i_error )
370         {
371             if( i_error == 1 )
372             {
373                 /* End of file - we do not set b_die because only the
374                  * interface is allowed to do so. */
375                 intf_WarnMsg( 3, "input: EOF reached" );
376                 p_input->b_eof = 1;
377             }
378             else
379             {
380                 p_input->b_error = 1;
381             }
382         }
383     }
384
385     free( pp_packets );
386
387     if( p_input->b_error || p_input->b_eof )
388     {
389         ErrorThread( p_input );
390     }
391
392     EndThread( p_input );
393
394     DestroyThread( p_input );
395
396     intf_DbgMsg("input: Thread end");
397 }
398
399 /*****************************************************************************
400  * InitThread: init the input Thread
401  *****************************************************************************/
402 static int InitThread( input_thread_t * p_input )
403 {
404
405     /* Initialize statistics */
406     p_input->c_loops                    = 0;
407     p_input->stream.c_packets_read      = 0;
408     p_input->stream.c_packets_trashed   = 0;
409     p_input->p_stream                   = NULL;
410
411     /* Set locks. */
412     vlc_mutex_init( &p_input->stream.stream_lock );
413     vlc_cond_init( &p_input->stream.stream_wait );
414     vlc_mutex_init( &p_input->stream.control.control_lock );
415
416     /* Find appropriate module. */
417     p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
418                                            (probedata_t *)p_input );
419
420     if( p_input->p_input_module == NULL )
421     {
422         intf_ErrMsg( "input error: no suitable input module for `%s'",
423                      p_input->p_source );
424         return( -1 );
425     }
426
427 #define f p_input->p_input_module->p_functions->input.functions.input
428     p_input->pf_init          = f.pf_init;
429     p_input->pf_end           = f.pf_end;
430     p_input->pf_init_bit_stream= f.pf_init_bit_stream;
431     p_input->pf_read          = f.pf_read;
432     p_input->pf_set_area      = f.pf_set_area;
433     p_input->pf_set_program   = f.pf_set_program;
434     p_input->pf_demux         = f.pf_demux;
435     p_input->pf_new_packet    = f.pf_new_packet;
436     p_input->pf_new_pes       = f.pf_new_pes;
437     p_input->pf_delete_packet = f.pf_delete_packet;
438     p_input->pf_delete_pes    = f.pf_delete_pes;
439     p_input->pf_rewind        = f.pf_rewind;
440     p_input->pf_seek          = f.pf_seek;
441
442 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
443     /* FIXME : this is waaaay too kludgy */
444     if( ( strlen( p_input->p_source ) >= 10
445           && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
446           || ( strlen( p_input->p_source ) >= 4
447                 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
448     {
449         /* Network stream */
450         NetworkOpen( p_input );
451         p_input->stream.i_method = INPUT_METHOD_NETWORK;
452     }
453     else if( ( strlen( p_input->p_source ) > 5 )
454                && !strncasecmp( p_input->p_source, "http:", 5 ) )
455     {
456         /* HTTP stream */
457         HTTPOpen( p_input );
458         p_input->stream.i_method = INPUT_METHOD_NETWORK;
459     }
460     else 
461 #endif
462         if( ( ( ( strlen( p_input->p_source ) > 4 )
463                   && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
464               || TestMethod( INPUT_METHOD_VAR, "dvd" ) ) 
465              && f.pf_open != NULL )
466     {
467         /* DVD - this is THE kludge */
468         f.pf_open( p_input );
469         p_input->stream.i_method = INPUT_METHOD_DVD;
470     }
471     else if( ( ( ( strlen( p_input->p_source ) > 8 )
472                    && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
473                 || TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
474              && f.pf_open != NULL )
475     {
476         /* DVDRead - this is THE kludge */
477         f.pf_open( p_input );
478         p_input->stream.i_method = INPUT_METHOD_DVD;
479     }
480     else if( ( strlen( p_input->p_source ) > 4 )
481                && !strncasecmp( p_input->p_source, "vlc:", 4 )
482                && f.pf_open != NULL )
483     {
484         /* Dummy input - very kludgy */
485         f.pf_open( p_input );
486     }
487     else if( ( strlen( p_input->p_source ) == 1 )
488                && *p_input->p_source == '-' )
489     {
490         /* Stdin */
491         StdOpen( p_input );
492     }
493     else
494     {
495         /* File input */
496         FileOpen( p_input );
497         p_input->stream.i_method = INPUT_METHOD_FILE;
498     }
499 #undef f
500
501     if( p_input->b_error )
502     {
503         /* We barfed -- exit nicely */
504         module_Unneed( p_input->p_input_module );
505         return( -1 );
506     }
507
508     p_input->pf_init( p_input );
509
510     if( p_input->b_error )
511     {
512         /* We barfed -- exit nicely */
513         CloseThread( p_input );
514         module_Unneed( p_input->p_input_module );
515         return( -1 );
516     }
517
518     *p_input->pi_status = THREAD_READY;
519
520     return( 0 );
521 }
522
523 /*****************************************************************************
524  * ErrorThread: RunThread() error loop
525  *****************************************************************************
526  * This function is called when an error occured during thread main's loop.
527  *****************************************************************************/
528 static void ErrorThread( input_thread_t *p_input )
529 {
530     while( !p_input->b_die )
531     {
532         /* Sleep a while */
533         msleep( INPUT_IDLE_SLEEP );
534     }
535 }
536
537 /*****************************************************************************
538  * EndThread: end the input thread
539  *****************************************************************************/
540 static void EndThread( input_thread_t * p_input )
541 {
542     int *       pi_status;                                  /* thread status */
543
544     /* Store status */
545     pi_status = p_input->pi_status;
546     *pi_status = THREAD_END;
547
548     if( p_main->b_stats )
549     {
550 #ifdef HAVE_SYS_TIMES_H
551         /* Display statistics */
552         struct tms  cpu_usage;
553         times( &cpu_usage );
554
555         intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
556                       p_input->c_loops,
557                       cpu_usage.tms_utime, cpu_usage.tms_stime );
558 #else
559         intf_StatMsg( "input stats: %d loops", p_input->c_loops );
560 #endif
561
562         input_DumpStream( p_input );
563     }
564
565     /* Free all ES and destroy all decoder threads */
566     input_EndStream( p_input );
567
568     /* Free demultiplexer's data */
569     p_input->pf_end( p_input );
570
571     /* Close the input method */
572     CloseThread( p_input );
573
574     /* Release modules */
575     module_Unneed( p_input->p_input_module );
576 }
577
578 /*****************************************************************************
579  * CloseThread: close the target
580  *****************************************************************************/
581 static void CloseThread( input_thread_t * p_input )
582 {
583 #define f p_input->p_input_module->p_functions->input.functions.input
584
585 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
586     /* Close stream */
587     if( ( strlen( p_input->p_source ) > 10
588           && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
589           || ( strlen( p_input->p_source ) > 4
590                 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
591     {
592         NetworkClose( p_input );
593     }
594     else if( ( strlen( p_input->p_source ) > 5 )
595                && !strncasecmp( p_input->p_source, "http:", 5 ) )
596     {
597         NetworkClose( p_input );
598     }
599     else 
600 #endif
601     if( ( ( strlen( p_input->p_source ) > 4 )
602             && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
603         || TestMethod( INPUT_METHOD_VAR, "dvd" ) )
604     {
605         f.pf_close( p_input );
606     }
607     else if( ( ( strlen( p_input->p_source ) > 8 )
608                  && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
609              || TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
610     {
611         f.pf_close( p_input );
612     }
613     else if( ( strlen( p_input->p_source ) > 4 )
614                && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
615     {
616         f.pf_close( p_input );
617     }
618     else
619     {
620         FileClose( p_input );
621     }
622 #undef f
623 }
624
625 /*****************************************************************************
626  * DestroyThread: destroy the input thread
627  *****************************************************************************/
628 static void DestroyThread( input_thread_t * p_input )
629 {
630     int *       pi_status;                                  /* thread status */
631
632     /* Store status */
633     pi_status = p_input->pi_status;
634
635     /* Destroy Mutex locks */
636     vlc_mutex_destroy( &p_input->stream.control.control_lock );
637     vlc_mutex_destroy( &p_input->stream.stream_lock );
638     
639     /* Free input structure */
640     free( p_input );
641
642     /* Update status */
643     *pi_status = THREAD_OVER;
644 }
645
646 /*****************************************************************************
647  * StdOpen : open standard input
648  *****************************************************************************/
649 static void StdOpen( input_thread_t * p_input )
650 {
651     vlc_mutex_lock( &p_input->stream.stream_lock );
652
653     /* Suppose we can control the pace - this won't work in some cases ! */
654     p_input->stream.b_pace_control = 1;
655
656     p_input->stream.b_seekable = 0;
657     p_input->stream.p_selected_area->i_size = 0;
658     p_input->stream.p_selected_area->i_tell = 0;
659     vlc_mutex_unlock( &p_input->stream.stream_lock );
660
661     intf_WarnMsg( 2, "input: opening stdin" );
662     p_input->i_handle = 0;
663 }
664
665 /*****************************************************************************
666  * FileOpen : open a file descriptor
667  *****************************************************************************/
668 static void FileOpen( input_thread_t * p_input )
669 {
670     struct stat         stat_info;
671     int                 i_stat;
672
673     char *psz_name = p_input->p_source;
674
675     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
676     {
677         int i_size = strlen( psz_name );
678
679         if( ( i_size > 8 )
680             && !strncasecmp( psz_name, "dvdread:", 8 ) )
681         {
682             /* get rid of the 'dvdread:' stuff and try again */
683             psz_name += 8;
684             i_stat = stat( psz_name, &stat_info );
685         }
686         else if( ( i_size > 4 )
687             && !strncasecmp( psz_name, "dvd:", 4 ) )
688         {
689             /* get rid of the 'dvd:' stuff and try again */
690             psz_name += 4;
691             i_stat = stat( psz_name, &stat_info );
692         }
693         else if( ( i_size > 4 )
694                  && !strncasecmp( psz_name, "vcd:", 4 ) )
695         {
696             /* get rid of the 'vcd:' stuff and try again */
697             psz_name += 4;
698             i_stat = stat( psz_name, &stat_info );
699         }
700         else if( ( i_size > 5 )
701                  && !strncasecmp( psz_name, "file:", 5 ) )
702         {
703             /* get rid of the 'file:' stuff and try again */
704             psz_name += 5;
705             i_stat = stat( psz_name, &stat_info );
706         }
707
708         if( i_stat == (-1) )
709         {
710             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
711                          psz_name, strerror(errno));
712             p_input->b_error = 1;
713             return;
714         }
715     }
716
717     vlc_mutex_lock( &p_input->stream.stream_lock );
718
719     /* If we are here we can control the pace... */
720     p_input->stream.b_pace_control = 1;
721
722     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
723          || S_ISBLK(stat_info.st_mode) )
724     {
725         p_input->stream.b_seekable = 1;
726         p_input->stream.p_selected_area->i_size = stat_info.st_size;
727     }
728     else if( S_ISFIFO(stat_info.st_mode)
729 #if !defined( SYS_BEOS ) && !defined( WIN32 )
730              || S_ISSOCK(stat_info.st_mode)
731 #endif
732              )
733     {
734         p_input->stream.b_seekable = 0;
735         p_input->stream.p_selected_area->i_size = 0;
736     }
737     else
738     {
739         vlc_mutex_unlock( &p_input->stream.stream_lock );
740         intf_ErrMsg( "input error: unknown file type for `%s'",
741                      psz_name );
742         p_input->b_error = 1;
743         return;
744     }
745
746     p_input->stream.p_selected_area->i_tell = 0;
747     vlc_mutex_unlock( &p_input->stream.stream_lock );
748
749     intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
750     if( (p_input->i_handle = open( psz_name,
751                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
752     {
753         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
754         p_input->b_error = 1;
755         return;
756     }
757
758 }
759
760 /*****************************************************************************
761  * FileClose : close a file descriptor
762  *****************************************************************************/
763 static void FileClose( input_thread_t * p_input )
764 {
765     intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
766
767     close( p_input->i_handle );
768
769     return;
770 }
771
772 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
773 /*****************************************************************************
774  * NetworkOpen : open a network socket 
775  *****************************************************************************/
776 static void NetworkOpen( input_thread_t * p_input )
777 {
778     char                *psz_server = NULL;
779     char                *psz_bind = NULL;
780     int                 i_server_port = 0;
781     int                 i_bind_port = 0;
782     int                 i_opt;
783     int                 i_opt_size;
784     struct sockaddr_in  sock;
785
786     /* Get the remote server. Syntax is :
787      * udp[stream]:[/][/][serveraddr[:serverport]][@[bindaddr]:[bindport]] */
788     if( p_input->p_source != NULL )
789     {
790         char * psz_parser = p_input->p_source;
791         char * psz_server_port = NULL;
792         char * psz_bind_port = NULL;
793
794         /* Skip the protocol name */
795         while( *psz_parser && *psz_parser != ':' )
796         {
797             psz_parser++;
798         }
799
800         /* Skip the "://" part */
801         while( *psz_parser && (*psz_parser == ':' || *psz_parser == '/') )
802         {
803             psz_parser++;
804         }
805
806         if( *psz_parser && *psz_parser != '@' )
807         {
808             /* Found server */
809             psz_server = psz_parser;
810
811             while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
812             {
813                 psz_parser++;
814             }
815
816             if( *psz_parser == ':' )
817             {
818                 /* Found server port */
819                 *psz_parser = '\0'; /* Terminate server name */
820                 psz_parser++;
821                 psz_server_port = psz_parser;
822
823                 while( *psz_parser && *psz_parser != '@' )
824                 {
825                     psz_parser++;
826                 }
827             }
828         }
829
830         if( *psz_parser == '@' )
831         {
832             /* Found bind address or bind port */
833             *psz_parser = '\0'; /* Terminate server port or name if necessary */
834             psz_parser++;
835
836             if( *psz_parser && *psz_parser != ':' )
837             {
838                 /* Found bind address */
839                 psz_bind = psz_parser;
840
841                 while( *psz_parser && *psz_parser != ':' )
842                 {
843                     psz_parser++;
844                 }
845             }
846
847             if( *psz_parser == ':' )
848             {
849                 /* Found bind port */
850                 *psz_parser = '\0'; /* Terminate bind address if necessary */
851                 psz_parser++;
852
853                 psz_bind_port = psz_parser;
854             }
855         }
856
857         /* Convert ports format */
858         if( psz_server_port != NULL )
859         {
860             i_server_port = strtol( psz_server_port, &psz_parser, 10 );
861             if( *psz_parser )
862             {
863                 intf_ErrMsg( "input error: cannot parse server port near %s",
864                              psz_parser );
865                 p_input->b_error = 1;
866                 return;
867             }
868         }
869
870         if( psz_bind_port != NULL )
871         {
872             i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
873             if( *psz_parser )
874             {
875                 intf_ErrMsg( "input error: cannot parse bind port near %s",
876                              psz_parser );
877                 p_input->b_error = 1;
878                 return;
879             }
880         }
881     }
882     else
883     {
884         /* This is required or NetworkClose will never be called */
885         p_input->p_source = "ts: network input";
886     }
887
888     /* Check that we got a valid port */
889     if( i_bind_port == 0 )
890     {
891         i_bind_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
892     }
893
894     intf_WarnMsg( 2, "input: server=%s:%d local=%s:%d",
895                      psz_server, i_server_port, psz_bind, i_bind_port );
896
897     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
898      * protocol */
899     p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
900     if( p_input->i_handle == -1 )
901     {
902         intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
903         p_input->b_error = 1;
904         return;
905     }
906
907     /* We may want to reuse an already used socket */
908     i_opt = 1;
909     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
910                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
911     {
912         intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
913                      strerror(errno));
914         close( p_input->i_handle );
915         p_input->b_error = 1;
916         return;
917     }
918
919     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
920      * packet loss caused by scheduling problems */
921     i_opt = 0x80000;
922     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
923                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
924     {
925         intf_WarnMsg( 1, "input warning: can't configure socket (SO_RCVBUF: %s)", 
926                          strerror(errno));
927     }
928
929     /* Check if we really got what we have asked for, because Linux, etc.
930      * will silently limit the max buffer size to net.core.rmem_max which
931      * is typically only 65535 bytes */
932     i_opt = 0;
933     i_opt_size = sizeof( i_opt );
934     if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
935                     (void*) &i_opt, &i_opt_size ) == -1 )
936     {
937         intf_WarnMsg( 1, "input warning: can't query socket (SO_RCVBUF: %s)", 
938                          strerror(errno));
939     }
940     else if( i_opt < 0x80000 )
941     {
942         intf_WarnMsg( 1, "input warning: socket buffer size is 0x%x"
943                          " instead of 0x%x", i_opt, 0x80000 );
944     }
945
946     /* Build the local socket */
947     if ( network_BuildAddr( &sock, psz_bind, i_bind_port ) == -1 )
948     {
949         intf_ErrMsg( "input error: can't build local address" );
950         close( p_input->i_handle );
951         p_input->b_error = 1;
952         return;
953     }
954
955     /* Bind it */
956     if( bind( p_input->i_handle, (struct sockaddr *)&sock, 
957               sizeof( sock ) ) < 0 )
958     {
959         intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
960         close( p_input->i_handle );
961         p_input->b_error = 1;
962         return;
963     }
964
965     /* Allow broadcast reception if we bound on INADDR_ANY */
966     if( psz_bind == NULL )
967     {
968         i_opt = 1;
969         if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_BROADCAST,
970                         (void*) &i_opt, sizeof( i_opt ) ) == -1 )
971         {
972             intf_WarnMsg( 1, "input warning: can't configure socket (SO_BROADCAST: %s)", 
973                              strerror(errno));
974         }
975     }
976
977     /* Join the multicast group if the socket is a multicast address */
978 #ifndef IN_MULTICAST
979 #   define IN_MULTICAST(a)         IN_CLASSD(a)
980 #endif
981
982     if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
983     {
984         struct ip_mreq imr;
985
986         imr.imr_interface.s_addr = INADDR_ANY;
987         imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
988         if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
989                         (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
990         {
991             intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
992                          strerror(errno) );
993             close( p_input->i_handle );
994             p_input->b_error = 1;
995             return;
996         }
997     }
998     
999     if( psz_server != NULL )
1000     {
1001         /* Build socket for remote connection */
1002         if ( network_BuildAddr( &sock, psz_server, i_server_port ) == -1 )
1003         {
1004             intf_ErrMsg( "input error: can't build remote address" );
1005             close( p_input->i_handle );
1006             p_input->b_error = 1;
1007             return;
1008         }
1009
1010         /* Connect the socket */
1011         if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1012                      sizeof( sock ) ) == (-1) )
1013         {
1014             intf_ErrMsg( "input error: can't connect socket (%s)", 
1015                          strerror(errno) );
1016             close( p_input->i_handle );
1017             p_input->b_error = 1;
1018             return;
1019         }
1020     }
1021
1022     p_input->stream.b_pace_control = 0;
1023     p_input->stream.b_seekable = 0;
1024
1025     intf_WarnMsg( 3, "input: successfully opened network mode" );
1026     
1027     return;
1028 }
1029
1030 /*****************************************************************************
1031  * NetworkClose : close a network socket
1032  *****************************************************************************/
1033 static void NetworkClose( input_thread_t * p_input )
1034 {
1035     intf_WarnMsg( 2, "input: closing network target `%s'", p_input->p_source );
1036
1037     close( p_input->i_handle );
1038 }
1039
1040 /*****************************************************************************
1041  * HTTPOpen : make an HTTP request
1042  *****************************************************************************/
1043 static void HTTPOpen( input_thread_t * p_input )
1044 {
1045     char                *psz_server = NULL;
1046     char                *psz_path = NULL;
1047     char                *psz_proxy;
1048     int                 i_port = 0;
1049     int                 i_opt;
1050     struct sockaddr_in  sock;
1051     char                psz_buffer[256];
1052
1053     /* Get the remote server */
1054     if( p_input->p_source != NULL )
1055     {
1056         psz_server = p_input->p_source;
1057
1058         /* Skip the protocol name */
1059         while( *psz_server && *psz_server != ':' )
1060         {
1061             psz_server++;
1062         }
1063
1064         /* Skip the "://" part */
1065         while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1066         {
1067             psz_server++;
1068         }
1069
1070         /* Found a server name */
1071         if( *psz_server )
1072         {
1073             char *psz_port = psz_server;
1074
1075             /* Skip the hostname part */
1076             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1077             {
1078                 psz_port++;
1079             }
1080
1081             /* Found a port name */
1082             if( *psz_port )
1083             {
1084                 if( *psz_port == ':' )
1085                 {
1086                     /* Replace ':' with '\0' */
1087                     *psz_port = '\0';
1088                     psz_port++;
1089                 }
1090
1091                 psz_path = psz_port;
1092                 while( *psz_path && *psz_path != '/' )
1093                 {
1094                     psz_path++;
1095                 }
1096
1097                 if( *psz_path )
1098                 {
1099                     *psz_path = '\0';
1100                     psz_path++;
1101                 }
1102                 else
1103                 {
1104                     psz_path = NULL;
1105                 }
1106
1107                 if( *psz_port != '\0' )
1108                 {
1109                     i_port = atoi( psz_port );
1110                 }
1111             }
1112         }
1113         else
1114         {
1115             psz_server = NULL;
1116         }
1117     }
1118
1119     /* Check that we got a valid server */
1120     if( psz_server == NULL )
1121     {
1122         intf_ErrMsg( "input error: No server given" );
1123         p_input->b_error = 1;
1124         return;
1125     }
1126
1127     /* Check that we got a valid port */
1128     if( i_port == 0 )
1129     {
1130         i_port = 80; /* FIXME */
1131     }
1132
1133     intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1134                   i_port, psz_path );
1135
1136     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1137      *      * protocol */
1138     p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1139     if( p_input->i_handle == -1 )
1140     {
1141         intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );        p_input->b_error = 1;
1142         return;
1143     }
1144
1145     /* We may want to reuse an already used socket */
1146     i_opt = 1;
1147     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1148                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1149     {
1150         intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1151                      strerror(errno));
1152         close( p_input->i_handle );
1153         p_input->b_error = 1;
1154         return;
1155     }
1156
1157     /* Check proxy */
1158     if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1159     {
1160         /* http://myproxy.mydomain:myport/ */
1161         int                 i_proxy_port = 0;
1162
1163         /* Skip the protocol name */
1164         while( *psz_proxy && *psz_proxy != ':' )
1165         {
1166             psz_proxy++;
1167         }
1168
1169         /* Skip the "://" part */
1170         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1171         {
1172             psz_proxy++;
1173         }
1174
1175         /* Found a proxy name */
1176         if( *psz_proxy )
1177         {
1178             char *psz_port = psz_proxy;
1179
1180             /* Skip the hostname part */
1181             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1182             {
1183                 psz_port++;
1184             }
1185
1186             /* Found a port name */
1187             if( *psz_port )
1188             {
1189                 char * psz_junk;
1190
1191                 /* Replace ':' with '\0' */
1192                 *psz_port = '\0';
1193                 psz_port++;
1194
1195                 psz_junk = psz_port;
1196                 while( *psz_junk && *psz_junk != '/' )
1197                 {
1198                     psz_junk++;
1199                 }
1200
1201                 if( *psz_junk )
1202                 {
1203                     *psz_junk = '\0';
1204                 }
1205
1206                 if( *psz_port != '\0' )
1207                 {
1208                     i_proxy_port = atoi( psz_port );
1209                 }
1210             }
1211         }
1212         else
1213         {
1214             intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1215             close( p_input->i_handle );
1216             p_input->b_error = 1;
1217             return;
1218         }
1219
1220         /* Build socket for proxy connection */
1221         if ( network_BuildAddr( &sock, psz_proxy, i_proxy_port ) == -1 )
1222         {
1223             intf_ErrMsg( "input error: can't build remote address" );
1224             close( p_input->i_handle );
1225             p_input->b_error = 1;
1226             return;
1227         }
1228     }
1229     else
1230     {
1231         /* No proxy, direct connection */
1232         if ( network_BuildAddr( &sock, psz_server, i_port ) == -1 )
1233         {
1234             intf_ErrMsg( "input error: can't build remote address" );
1235             close( p_input->i_handle );
1236             p_input->b_error = 1;
1237             return;
1238         }
1239     }
1240
1241     /* Connect the socket */
1242     if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1243                  sizeof( sock ) ) == (-1) )
1244     {
1245         intf_ErrMsg( "input error: can't connect socket (%s)",
1246                      strerror(errno) );
1247         close( p_input->i_handle );
1248         p_input->b_error = 1;
1249         return;
1250     }
1251
1252     p_input->stream.b_seekable = 0;
1253     p_input->stream.b_pace_control = 1; /* TCP/IP... */
1254
1255 #   define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1256 #   define HTTP_END       "\r\n"
1257
1258     /* Prepare GET ... */
1259     if( psz_proxy != NULL )
1260     {
1261         snprintf( psz_buffer, sizeof(psz_buffer),
1262                   "GET http://%s:%d/%s HTTP/1.0\r\n"
1263                   HTTP_USERAGENT HTTP_END,
1264                   psz_server, i_port, psz_path );
1265     }
1266     else
1267     {
1268         snprintf( psz_buffer, sizeof(psz_buffer),
1269                   "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1270                   HTTP_USERAGENT HTTP_END,
1271                   psz_path, psz_server );
1272     }
1273     psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1274
1275     /* Send GET ... */
1276     if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1277     {
1278         intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1279         close( p_input->i_handle );
1280         p_input->b_error = 1;
1281         return;
1282     }
1283
1284     /* Read HTTP header - this is gonna be fun with plug-ins which do not
1285      * use p_input->p_stream :-( */
1286     if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1287     {
1288         intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1289         close( p_input->i_handle );
1290         p_input->b_error = 1;
1291         return;
1292     }
1293
1294     while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1295     {
1296         if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1297              || *psz_buffer == '\r' || *psz_buffer == '\0' )
1298         {
1299             break;
1300         }
1301         /* FIXME : check Content-Type one day */
1302     }
1303
1304     intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1305 }
1306
1307 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */
1308