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