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