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