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