]> git.sesse.net Git - vlc/blob - src/input/input.c
* Modified the pf_close kludge in input.c so that we don't segfault when
[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.148 2001/11/01 15:30:50 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 "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
419 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
420     /* FIXME : this is waaaay too kludgy */
421     if( ( strlen( p_input->p_source ) > 3)
422           && !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 )
429                && !strncasecmp( p_input->p_source, "http:", 5 ) )
430     {
431         /* HTTP stream */
432         HTTPOpen( p_input );
433         p_input->stream.i_method = INPUT_METHOD_NETWORK;
434     }
435     else 
436 #endif
437         if( ( strlen( p_input->p_source ) > 4 )
438               && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
439     {
440         /* DVD - this is THE kludge */
441         f.pf_open( p_input );
442         p_input->stream.i_method = INPUT_METHOD_DVD;
443     }
444     else if( ( strlen( p_input->p_source ) > 4 )
445                && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
446     {
447         /* Dummy input - very kludgy */
448         f.pf_open( p_input );
449     }
450     else if( ( strlen( p_input->p_source ) == 1 )
451                && *p_input->p_source == '-' )
452     {
453         /* Stdin */
454         StdOpen( p_input );
455     }
456     else
457     {
458         /* File input */
459         FileOpen( p_input );
460         p_input->stream.i_method = INPUT_METHOD_FILE;
461     }
462 #undef f
463
464     if( p_input->b_error )
465     {
466         /* We barfed -- exit nicely */
467         module_Unneed( p_input->p_input_module );
468         return( -1 );
469     }
470
471     p_input->pf_init( p_input );
472
473     if( p_input->b_error )
474     {
475         /* We barfed -- exit nicely */
476         CloseThread( p_input );
477         module_Unneed( p_input->p_input_module );
478         return( -1 );
479     }
480
481     *p_input->pi_status = THREAD_READY;
482
483     return( 0 );
484 }
485
486 /*****************************************************************************
487  * ErrorThread: RunThread() error loop
488  *****************************************************************************
489  * This function is called when an error occured during thread main's loop.
490  *****************************************************************************/
491 static void ErrorThread( input_thread_t *p_input )
492 {
493     while( !p_input->b_die )
494     {
495         /* Sleep a while */
496         msleep( INPUT_IDLE_SLEEP );
497     }
498 }
499
500 /*****************************************************************************
501  * EndThread: end the input thread
502  *****************************************************************************/
503 static void EndThread( input_thread_t * p_input )
504 {
505     int *       pi_status;                                  /* thread status */
506
507     /* Store status */
508     pi_status = p_input->pi_status;
509     *pi_status = THREAD_END;
510
511     if( p_main->b_stats )
512     {
513 #ifdef HAVE_SYS_TIMES_H
514         /* Display statistics */
515         struct tms  cpu_usage;
516         times( &cpu_usage );
517
518         intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
519                       p_input->c_loops,
520                       cpu_usage.tms_utime, cpu_usage.tms_stime );
521 #else
522         intf_StatMsg( "input stats: %d loops", p_input->c_loops );
523 #endif
524
525         input_DumpStream( p_input );
526     }
527
528     /* Free all ES and destroy all decoder threads */
529     input_EndStream( p_input );
530
531     /* Free demultiplexer's data */
532     p_input->pf_end( p_input );
533
534     /* Close the input method */
535     CloseThread( p_input );
536
537     /* Release modules */
538     module_Unneed( p_input->p_input_module );
539
540 }
541
542 /*****************************************************************************
543  * CloseThread: close the target
544  *****************************************************************************/
545 static void CloseThread( input_thread_t * p_input )
546 {
547 #define f p_input->p_input_module->p_functions->input.functions.input
548
549 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
550     /* Close stream */
551     if( ( strlen( p_input->p_source ) > 3)
552           && !strncasecmp( p_input->p_source, "ts:", 3 ) )
553     {
554         NetworkClose( p_input );
555     }
556     else if( ( strlen( p_input->p_source ) > 5 )
557                && !strncasecmp( p_input->p_source, "http:", 5 ) )
558     {
559         NetworkClose( p_input );
560     }
561     else 
562 #endif
563     if( ( strlen( p_input->p_source ) > 4 )
564           && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
565     {
566         f.pf_close( p_input );
567     }
568     else if( ( strlen( p_input->p_source ) > 4 )
569                && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
570     {
571         f.pf_close( p_input );
572     }
573     else
574     {
575         FileClose( p_input );
576     }
577 #undef f
578 }
579
580 /*****************************************************************************
581  * DestroyThread: destroy the input thread
582  *****************************************************************************/
583 static void DestroyThread( input_thread_t * p_input )
584 {
585     int *       pi_status;                                  /* thread status */
586
587     /* Store status */
588     pi_status = p_input->pi_status;
589
590     /* Destroy Mutex locks */
591     vlc_mutex_destroy( &p_input->stream.control.control_lock );
592     vlc_mutex_destroy( &p_input->stream.stream_lock );
593     
594     /* Free input structure */
595     free( p_input );
596
597     /* Update status */
598     *pi_status = THREAD_OVER;
599 }
600
601 /*****************************************************************************
602  * StdOpen : open standard input
603  *****************************************************************************/
604 static void StdOpen( input_thread_t * p_input )
605 {
606     vlc_mutex_lock( &p_input->stream.stream_lock );
607
608     /* Suppose we can control the pace - this won't work in some cases ! */
609     p_input->stream.b_pace_control = 1;
610
611     p_input->stream.b_seekable = 0;
612     p_input->stream.p_selected_area->i_size = 0;
613     p_input->stream.p_selected_area->i_tell = 0;
614     vlc_mutex_unlock( &p_input->stream.stream_lock );
615
616     intf_WarnMsg( 2, "input: opening stdin" );
617     p_input->i_handle = 0;
618 }
619
620 /*****************************************************************************
621  * FileOpen : open a file descriptor
622  *****************************************************************************/
623 static void FileOpen( input_thread_t * p_input )
624 {
625     struct stat         stat_info;
626     int                 i_stat;
627
628     char *psz_name = p_input->p_source;
629
630     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
631     {
632         int i_size = strlen( psz_name );
633
634         if( ( i_size > 4 )
635             && !strncasecmp( psz_name, "dvd:", 4 ) )
636         {
637             /* get rid of the 'dvd:' stuff and try again */
638             psz_name += 4;
639             i_stat = stat( psz_name, &stat_info );
640         }
641         else if( ( i_size > 4 )
642                  && !strncasecmp( psz_name, "vcd:", 4 ) )
643         {
644             /* get rid of the 'vcd:' stuff and try again */
645             psz_name += 4;
646             i_stat = stat( psz_name, &stat_info );
647         }
648         else if( ( i_size > 5 )
649                  && !strncasecmp( psz_name, "file:", 5 ) )
650         {
651             /* get rid of the 'file:' stuff and try again */
652             psz_name += 5;
653             i_stat = stat( psz_name, &stat_info );
654         }
655
656         if( i_stat == (-1) )
657         {
658             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
659                          psz_name, strerror(errno));
660             p_input->b_error = 1;
661             return;
662         }
663     }
664
665     vlc_mutex_lock( &p_input->stream.stream_lock );
666
667     /* If we are here we can control the pace... */
668     p_input->stream.b_pace_control = 1;
669
670     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
671          || S_ISBLK(stat_info.st_mode) )
672     {
673         p_input->stream.b_seekable = 1;
674         p_input->stream.p_selected_area->i_size = stat_info.st_size;
675     }
676     else if( S_ISFIFO(stat_info.st_mode)
677 #if !defined( SYS_BEOS ) && !defined( WIN32 )
678              || S_ISSOCK(stat_info.st_mode)
679 #endif
680              )
681     {
682         p_input->stream.b_seekable = 0;
683         p_input->stream.p_selected_area->i_size = 0;
684     }
685     else
686     {
687         vlc_mutex_unlock( &p_input->stream.stream_lock );
688         intf_ErrMsg( "input error: unknown file type for `%s'",
689                      psz_name );
690         p_input->b_error = 1;
691         return;
692     }
693
694     p_input->stream.p_selected_area->i_tell = 0;
695     vlc_mutex_unlock( &p_input->stream.stream_lock );
696
697     intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
698     if( (p_input->i_handle = open( psz_name,
699                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
700     {
701         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
702         p_input->b_error = 1;
703         return;
704     }
705
706 }
707
708 /*****************************************************************************
709  * FileClose : close a file descriptor
710  *****************************************************************************/
711 static void FileClose( input_thread_t * p_input )
712 {
713     intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
714
715     close( p_input->i_handle );
716
717     return;
718 }
719
720 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
721 /*****************************************************************************
722  * NetworkOpen : open a network socket 
723  *****************************************************************************/
724 static void NetworkOpen( input_thread_t * p_input )
725 {
726     char                *psz_server = NULL;
727     char                *psz_broadcast = NULL;
728     int                 i_port = 0;
729     int                 i_opt;
730     int                 i_opt_size;
731     struct sockaddr_in  sock;
732     unsigned int        i_mc_group;
733
734 #ifdef WIN32
735     WSADATA Data;
736     int i_err;
737 #endif
738     
739 #ifdef WIN32
740     /* WinSock Library Init. */
741     i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
742
743     if( i_err )
744     {
745         intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
746         return ;
747     }
748 #endif
749     
750     /* Get the remote server */
751     if( p_input->p_source != NULL )
752     {
753         psz_server = p_input->p_source;
754
755         /* Skip the protocol name */
756         while( *psz_server && *psz_server != ':' )
757         {
758             psz_server++;
759         }
760
761         /* Skip the "://" part */
762         while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
763         {
764             psz_server++;
765         }
766
767         /* Found a server name */
768         if( *psz_server )
769         {
770             char *psz_port = psz_server;
771
772             /* Skip the hostname part */
773             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
774             {
775                 psz_port++;
776             }
777
778             /* Found a port name or a broadcast addres */
779             if( *psz_port )
780             {
781                 /* That's a port name */
782                 if( *psz_port == ':' )
783                 {
784                     *psz_port = '\0';
785                     psz_port++;
786                     i_port = atoi( psz_port );
787                 }
788
789                 /* Search for '/' just after the port in case
790                  * we also have a broadcast address */
791                 psz_broadcast = psz_port;
792                 while( *psz_broadcast && *psz_broadcast != '/' )
793                 {
794                     psz_broadcast++;
795                 }
796
797                 if( *psz_broadcast )
798                 {
799                     *psz_broadcast = '\0';
800                     psz_broadcast++;
801                     while( *psz_broadcast && *psz_broadcast == '/' )
802                     {
803                         psz_broadcast++;
804                     }
805                 }
806                 else
807                 {
808                     psz_broadcast = NULL;
809                 }
810             }
811         }
812         else
813         {
814             psz_server = NULL;
815         }
816     }
817
818     /* Check that we got a valid server */
819     if( psz_server == NULL )
820     {
821         psz_server = main_GetPszVariable( INPUT_SERVER_VAR, 
822                                           INPUT_SERVER_DEFAULT );
823     }
824
825     /* Check that we got a valid port */
826     if( i_port == 0 )
827     {
828         i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
829     }
830
831     if( psz_broadcast == NULL )
832     {
833         /* Are we broadcasting ? */
834         if( main_GetIntVariable( INPUT_BROADCAST_VAR,
835                                  INPUT_BROADCAST_DEFAULT ) )
836         {
837             psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
838                                                  INPUT_BCAST_ADDR_DEFAULT );
839         }
840         else
841         {
842            psz_broadcast = NULL; 
843         }
844     }
845
846     intf_WarnMsg( 2, "input: server=%s port=%d broadcast=%s",
847                      psz_server, i_port, psz_broadcast );
848
849     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
850      * protocol */
851     p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
852     if( p_input->i_handle == -1 )
853     {
854         intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
855         p_input->b_error = 1;
856         return;
857     }
858
859     /* We may want to reuse an already used socket */
860     i_opt = 1;
861     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
862                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
863     {
864         intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
865                      strerror(errno));
866         close( p_input->i_handle );
867         p_input->b_error = 1;
868         return;
869     }
870
871     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
872      * packet loss caused by scheduling problems */
873     i_opt = 0x80000;
874     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
875                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
876     {
877         intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)", 
878                      strerror(errno));
879         close( p_input->i_handle );
880         p_input->b_error = 1;
881         return;
882     }
883
884     /* Check if we really got what we have asked for, because Linux, etc.
885      * will silently limit the max buffer size to net.core.rmem_max which
886      * is typically only 65535 bytes */
887     i_opt = 0;
888     i_opt_size = sizeof( i_opt );
889     if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
890                     (void*) &i_opt, &i_opt_size ) == -1 )
891     {
892         intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)", 
893                      strerror(errno));
894         close( p_input->i_handle );
895         p_input->b_error = 1;
896         return;
897     }
898     
899     if( i_opt < 0x80000 )
900     {
901         intf_WarnMsg( 1, "input warning: socket receive buffer size just 0x%x"
902                          " instead of 0x%x bytes.", i_opt, 0x80000 );
903     }
904
905     /* Build the local socket */
906     if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
907     {
908         intf_ErrMsg( "input error: can't build local address" );
909         close( p_input->i_handle );
910         p_input->b_error = 1;
911         return;
912     }
913
914     /* Required for IP_ADD_MEMBERSHIP */
915     i_mc_group = sock.sin_addr.s_addr;
916
917 #if defined( WIN32 )
918     if ( psz_broadcast != NULL )
919     {
920         sock.sin_addr.s_addr = INADDR_ANY;
921     }
922     
923 #define IN_MULTICAST(a)         IN_CLASSD(a)
924 #endif
925
926     /* Bind it */
927     if( bind( p_input->i_handle, (struct sockaddr *)&sock, 
928               sizeof( sock ) ) < 0 )
929     {
930         intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
931         close( p_input->i_handle );
932         p_input->b_error = 1;
933         return;
934     }
935
936     /* Join the multicast group if the socket is a multicast address */
937
938 #ifndef WIN32    
939     if( IN_MULTICAST( ntohl(i_mc_group) ) )
940     {
941         struct ip_mreq imr;
942
943         imr.imr_interface.s_addr = htonl(INADDR_ANY);
944         imr.imr_multiaddr.s_addr = i_mc_group;
945         if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
946                         (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
947         {
948             intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
949                          strerror(errno) );
950             close( p_input->i_handle);
951             p_input->b_error = 1;
952             return;
953         }
954     }
955 #endif
956     
957     /* Build socket for remote connection */
958     if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
959     {
960         intf_ErrMsg( "input error: can't build remote address" );
961         close( p_input->i_handle );
962         p_input->b_error = 1;
963         return;
964     }
965
966     /* Only connect if the user has passed a valid host */
967     if( sock.sin_addr.s_addr != INADDR_ANY )
968     {
969         /* Connect the socket */
970         if( connect( p_input->i_handle, (struct sockaddr *) &sock,
971                      sizeof( sock ) ) == (-1) )
972         {
973             intf_ErrMsg( "input error: can't connect socket (%s)", 
974                          strerror(errno) );
975             close( p_input->i_handle );
976             p_input->b_error = 1;
977             return;
978         }
979     }
980
981     p_input->stream.b_pace_control = 0;
982     p_input->stream.b_seekable = 0;
983
984     intf_WarnMsg( 3, "input: successfully opened network mode" );
985     
986     return;
987 }
988
989 /*****************************************************************************
990  * NetworkClose : close a network socket
991  *****************************************************************************/
992 static void NetworkClose( input_thread_t * p_input )
993 {
994     close( p_input->i_handle );
995
996 #ifdef WIN32 
997     WSACleanup();
998 #endif
999 }
1000
1001 /*****************************************************************************
1002  * HTTPOpen : make an HTTP request
1003  *****************************************************************************/
1004 static void HTTPOpen( input_thread_t * p_input )
1005 {
1006     char                *psz_server = NULL;
1007     char                *psz_path = NULL;
1008     char                *psz_proxy;
1009     int                 i_port = 0;
1010     int                 i_opt;
1011     struct sockaddr_in  sock;
1012     char                psz_buffer[256];
1013
1014 #ifdef WIN32
1015     WSADATA Data;
1016     int i_err;
1017 #endif
1018     
1019 #ifdef WIN32
1020     /* WinSock Library Init. */
1021     i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
1022
1023     if( i_err )
1024     {
1025         intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
1026         return ;
1027     }
1028 #endif
1029     
1030     /* Get the remote server */
1031     if( p_input->p_source != NULL )
1032     {
1033         psz_server = p_input->p_source;
1034
1035         /* Skip the protocol name */
1036         while( *psz_server && *psz_server != ':' )
1037         {
1038             psz_server++;
1039         }
1040
1041         /* Skip the "://" part */
1042         while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1043         {
1044             psz_server++;
1045         }
1046
1047         /* Found a server name */
1048         if( *psz_server )
1049         {
1050             char *psz_port = psz_server;
1051
1052             /* Skip the hostname part */
1053             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1054             {
1055                 psz_port++;
1056             }
1057
1058             /* Found a port name */
1059             if( *psz_port )
1060             {
1061                 if( *psz_port == ':' )
1062                 {
1063                     /* Replace ':' with '\0' */
1064                     *psz_port = '\0';
1065                     psz_port++;
1066                 }
1067
1068                 psz_path = psz_port;
1069                 while( *psz_path && *psz_path != '/' )
1070                 {
1071                     psz_path++;
1072                 }
1073
1074                 if( *psz_path )
1075                 {
1076                     *psz_path = '\0';
1077                     psz_path++;
1078                 }
1079                 else
1080                 {
1081                     psz_path = NULL;
1082                 }
1083
1084                 if( *psz_port != '\0' )
1085                 {
1086                     i_port = atoi( psz_port );
1087                 }
1088             }
1089         }
1090         else
1091         {
1092             psz_server = NULL;
1093         }
1094     }
1095
1096     /* Check that we got a valid server */
1097     if( psz_server == NULL )
1098     {
1099         intf_ErrMsg( "input error: No server given" );
1100         p_input->b_error = 1;
1101         return;
1102     }
1103
1104     /* Check that we got a valid port */
1105     if( i_port == 0 )
1106     {
1107         i_port = 80; /* FIXME */
1108     }
1109
1110     intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1111                   i_port, psz_path );
1112
1113     /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1114      *      * protocol */
1115     p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1116     if( p_input->i_handle == -1 )
1117     {
1118         intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );        p_input->b_error = 1;
1119         return;
1120     }
1121
1122     /* We may want to reuse an already used socket */
1123     i_opt = 1;
1124     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1125                     (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1126     {
1127         intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1128                      strerror(errno));
1129         close( p_input->i_handle );
1130         p_input->b_error = 1;
1131         return;
1132     }
1133
1134     /* Check proxy */
1135     if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1136     {
1137         /* http://myproxy.mydomain:myport/ */
1138         int                 i_proxy_port = 0;
1139
1140         /* Skip the protocol name */
1141         while( *psz_proxy && *psz_proxy != ':' )
1142         {
1143             psz_proxy++;
1144         }
1145
1146         /* Skip the "://" part */
1147         while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1148         {
1149             psz_proxy++;
1150         }
1151
1152         /* Found a proxy name */
1153         if( *psz_proxy )
1154         {
1155             char *psz_port = psz_proxy;
1156
1157             /* Skip the hostname part */
1158             while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1159             {
1160                 psz_port++;
1161             }
1162
1163             /* Found a port name */
1164             if( *psz_port )
1165             {
1166                 char * psz_junk;
1167
1168                 /* Replace ':' with '\0' */
1169                 *psz_port = '\0';
1170                 psz_port++;
1171
1172                 psz_junk = psz_port;
1173                 while( *psz_junk && *psz_junk != '/' )
1174                 {
1175                     psz_junk++;
1176                 }
1177
1178                 if( *psz_junk )
1179                 {
1180                     *psz_junk = '\0';
1181                 }
1182
1183                 if( *psz_port != '\0' )
1184                 {
1185                     i_proxy_port = atoi( psz_port );
1186                 }
1187             }
1188         }
1189         else
1190         {
1191             intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1192             close( p_input->i_handle );
1193             p_input->b_error = 1;
1194             return;
1195         }
1196
1197         /* Build socket for proxy connection */
1198         if ( network_BuildRemoteAddr( &sock, psz_proxy ) == -1 )
1199         {
1200             intf_ErrMsg( "input error: can't build remote address" );
1201             close( p_input->i_handle );
1202             p_input->b_error = 1;
1203             return;
1204         }
1205         sock.sin_port = htons( i_proxy_port );
1206     }
1207     else
1208     {
1209         /* No proxy, direct connection */
1210         if ( network_BuildRemoteAddr( &sock, psz_server ) == -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         sock.sin_port = htons( i_port );
1218     }
1219
1220     /* Connect the socket */
1221     if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1222                  sizeof( sock ) ) == (-1) )
1223     {
1224         intf_ErrMsg( "input error: can't connect socket (%s)",
1225                      strerror(errno) );
1226         close( p_input->i_handle );
1227         p_input->b_error = 1;
1228         return;
1229     }
1230
1231     p_input->stream.b_seekable = 0;
1232     p_input->stream.b_pace_control = 1; /* TCP/IP... */
1233
1234 #   define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1235 #   define HTTP_END       "\r\n"
1236
1237     /* Prepare GET ... */
1238     if( psz_proxy != NULL )
1239     {
1240         snprintf( psz_buffer, sizeof(psz_buffer),
1241                   "GET http://%s:%d/%s HTTP/1.0\r\n"
1242                   HTTP_USERAGENT HTTP_END,
1243                   psz_server, i_port, psz_path );
1244     }
1245     else
1246     {
1247         snprintf( psz_buffer, sizeof(psz_buffer),
1248                   "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1249                   HTTP_USERAGENT HTTP_END,
1250                   psz_path, psz_server );
1251     }
1252     psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1253
1254     /* Send GET ... */
1255     if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1256     {
1257         intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1258         close( p_input->i_handle );
1259         p_input->b_error = 1;
1260         return;
1261     }
1262
1263     /* Read HTTP header - this is gonna be fun with plug-ins which do not
1264      * use p_input->p_stream :-( */
1265     if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1266     {
1267         intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1268         close( p_input->i_handle );
1269         p_input->b_error = 1;
1270         return;
1271     }
1272
1273     while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1274     {
1275         if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1276              || *psz_buffer == '\r' || *psz_buffer == '\0' )
1277         {
1278             break;
1279         }
1280         /* FIXME : check Content-Type one day */
1281     }
1282
1283     intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1284 }
1285
1286 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */
1287