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