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