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