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