]> git.sesse.net Git - vlc/blob - src/input/input.c
3bdee5cb1df023de85df02430af1371678dd8bbb
[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.128 2001/08/05 15:32:46 gbazin 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 STATS
61 #   include <sys/times.h>
62 #endif
63
64 #include "config.h"
65 #include "common.h"
66 #include "threads.h"
67 #include "mtime.h"
68 #include "netutils.h"
69 #include "modules.h"
70
71 #include "intf_msg.h"
72 #include "intf_playlist.h"
73
74 #include "stream_control.h"
75 #include "input_ext-intf.h"
76 #include "input_ext-dec.h"
77 #include "input_ext-plugins.h"
78
79 #include "interface.h"
80
81 #include "main.h"
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86 static void RunThread       ( input_thread_t *p_input );
87 static  int InitThread      ( input_thread_t *p_input );
88 static void ErrorThread     ( input_thread_t *p_input );
89 static void DestroyThread   ( input_thread_t *p_input );
90 static void EndThread       ( input_thread_t *p_input );
91
92 static void FileOpen        ( 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 NetworkClose    ( input_thread_t *p_input );
97 #endif
98
99 /*****************************************************************************
100  * input_CreateThread: creates a new input thread
101  *****************************************************************************
102  * This function creates a new input, and returns a pointer
103  * to its description. On error, it returns NULL.
104  * If pi_status is NULL, then the function will block until the thread is ready.
105  * If not, it will be updated using one of the THREAD_* constants.
106  *****************************************************************************/
107 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
108 {
109     input_thread_t *    p_input;                        /* thread descriptor */
110     int                 i_status;                           /* thread status */
111
112     /* Allocate descriptor */
113     p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
114     if( p_input == NULL )
115     {
116         intf_ErrMsg( "input error: can't allocate input thread (%s)",
117                      strerror(errno) );
118         return( NULL );
119     }
120
121     /* Packets read once */
122     p_input->i_read_once = INPUT_READ_ONCE;
123
124     /* Initialize thread properties */
125     p_input->b_die              = 0;
126     p_input->b_error            = 0;
127     p_input->b_eof              = 0;
128
129     /* Set target */
130     p_input->p_source           = p_item->psz_name;
131
132     /* I have never understood that stuff --Meuuh */
133     p_input->pi_status          = (pi_status != NULL) ? pi_status : &i_status;
134     *p_input->pi_status         = THREAD_CREATE;
135
136     /* Initialize stream description */
137     p_input->stream.i_es_number = 0;
138     p_input->stream.i_selected_es_number = 0;
139     p_input->stream.i_pgrm_number = 0;
140     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
141     p_input->stream.b_new_mute = MUTE_NO_CHANGE;
142     p_input->stream.i_mux_rate = 0;
143
144     /* no stream, no area */
145     p_input->stream.i_area_nb = 0;
146     p_input->stream.pp_areas = NULL;
147     p_input->stream.p_selected_area = NULL;
148     p_input->stream.p_new_area = NULL;
149
150     /* By default there is one area in a stream */
151     input_AddArea( p_input );
152     p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
153
154     /* Initialize stream control properties. */
155     p_input->stream.control.i_status = PLAYING_S;
156     p_input->stream.control.i_rate = DEFAULT_RATE;
157     p_input->stream.control.b_mute = 0;
158     p_input->stream.control.b_grayscale = main_GetIntVariable(
159                             VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
160     p_input->stream.control.i_smp = main_GetIntVariable(
161                             VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
162
163     /* Setup callbacks */
164     p_input->pf_file_open     = FileOpen;
165     p_input->pf_file_close    = FileClose;
166 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
167     p_input->pf_network_open  = NetworkOpen;
168     p_input->pf_network_close = NetworkClose;
169 #endif
170
171     /* Create thread. */
172     if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
173                            (void *) p_input ) )
174     {
175         intf_ErrMsg( "input error: can't create input thread (%s)",
176                      strerror(errno) );
177         free( p_input );
178         return( NULL );
179     }
180
181     /* If status is NULL, wait until the thread is created */
182     if( pi_status == NULL )
183     {
184         do
185         {
186             msleep( THREAD_SLEEP );
187         } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
188                 && (i_status != THREAD_FATAL) );
189         if( i_status != THREAD_READY )
190         {
191             return( NULL );
192         }
193     }
194     return( p_input );
195 }
196
197 /*****************************************************************************
198  * input_DestroyThread: mark an input thread as zombie
199  *****************************************************************************
200  * This function should not return until the thread is effectively cancelled.
201  *****************************************************************************/
202 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
203 {
204     int         i_status;                                   /* thread status */
205
206     /* Set status */
207     p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
208     *p_input->pi_status = THREAD_DESTROY;
209
210     /* Request thread destruction */
211     p_input->b_die = 1;
212
213     /* Make the thread exit of an eventual vlc_cond_wait() */
214     vlc_mutex_lock( &p_input->stream.stream_lock );
215     vlc_cond_signal( &p_input->stream.stream_wait );
216     vlc_mutex_unlock( &p_input->stream.stream_lock );
217
218     /* If status is NULL, wait until thread has been destroyed */
219     if( pi_status == NULL )
220     {
221         do
222         {
223             msleep( THREAD_SLEEP );
224         } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
225                   && (i_status != THREAD_FATAL) );
226     }
227 }
228
229 /*****************************************************************************
230  * RunThread: main thread loop
231  *****************************************************************************
232  * Thread in charge of processing the network packets and demultiplexing.
233  *****************************************************************************/
234 static void RunThread( input_thread_t *p_input )
235 {
236     int                     i_error, i;
237     data_packet_t **        pp_packets;
238
239     if( InitThread( p_input ) )
240     {
241
242         /* If we failed, wait before we are killed, and exit */
243         *p_input->pi_status = THREAD_ERROR;
244         p_input->b_error = 1;
245         ErrorThread( p_input );
246         DestroyThread( p_input );
247         free( p_input );
248         return;
249     }
250
251     /* initialization is completed */
252     vlc_mutex_lock( &p_input->stream.stream_lock );
253     p_input->stream.b_changed = 1;
254     vlc_mutex_unlock( &p_input->stream.stream_lock );
255
256     pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
257                                         sizeof( data_packet_t * ) );
258     if( pp_packets == NULL )
259     {
260         intf_ErrMsg( "input error: out of memory" );
261         free( pp_packets );
262         p_input->b_error = 1;
263     }
264
265     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
266     {
267 #ifdef STATS
268         p_input->c_loops++;
269 #endif
270
271         vlc_mutex_lock( &p_input->stream.stream_lock );
272
273         if( p_input->stream.p_new_area )
274         {
275             if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
276             {
277
278                 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
279
280                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
281                 {
282                     pgrm_descriptor_t * p_pgrm
283                                             = p_input->stream.pp_programs[i];
284                     /* Escape all decoders for the stream discontinuity they
285                      * will encounter. */
286                     input_EscapeDiscontinuity( p_input, p_pgrm );
287
288                     /* Reinitialize synchro. */
289                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
290                 }
291             }
292             p_input->stream.p_new_area = NULL;
293         }
294
295         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
296         {
297             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
298             {
299                 p_input->pf_seek( p_input,
300                                   p_input->stream.p_selected_area->i_seek );
301
302                 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
303                 {
304                     pgrm_descriptor_t * p_pgrm
305                                             = p_input->stream.pp_programs[i];
306                     /* Escape all decoders for the stream discontinuity they
307                      * will encounter. */
308                     input_EscapeDiscontinuity( p_input, p_pgrm );
309
310                     /* Reinitialize synchro. */
311                     p_pgrm->i_synchro_state = SYNCHRO_REINIT;
312                 }
313             }
314             p_input->stream.p_selected_area->i_seek = NO_SEEK;
315         }
316
317         if( p_input->stream.p_removed_es )
318         {
319             input_UnselectES( p_input, p_input->stream.p_removed_es );
320             p_input->stream.p_removed_es = NULL;
321         }
322
323         if( p_input->stream.p_newly_selected_es )
324         {
325             input_SelectES( p_input, p_input->stream.p_newly_selected_es );
326             p_input->stream.p_newly_selected_es = NULL;
327         }
328
329         if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
330         {
331             if( p_input->stream.b_new_mute )
332             {
333                 input_EscapeAudioDiscontinuity( p_input );
334             }
335
336             vlc_mutex_lock( &p_input->stream.control.control_lock );
337             p_input->stream.control.b_mute = p_input->stream.b_new_mute;
338             vlc_mutex_unlock( &p_input->stream.control.control_lock );
339
340             p_input->stream.b_new_mute = MUTE_NO_CHANGE;
341         }
342
343         vlc_mutex_unlock( &p_input->stream.stream_lock );
344
345         i_error = p_input->pf_read( p_input, pp_packets );
346
347         /* Demultiplex read packets. */
348         for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
349         {
350             p_input->pf_demux( p_input, pp_packets[i] );
351         }
352
353         if( i_error )
354         {
355             if( i_error == 1 )
356             {
357                 /* End of file - we do not set b_die because only the
358                  * interface is allowed to do so. */
359                 intf_WarnMsg( 3, "input: EOF reached" );
360                 p_input->b_eof = 1;
361             }
362             else
363             {
364                 p_input->b_error = 1;
365             }
366         }
367     }
368
369     free( pp_packets );
370
371     if( p_input->b_error || p_input->b_eof )
372     {
373         ErrorThread( p_input );
374     }
375
376     EndThread( p_input );
377
378     DestroyThread( p_input );
379
380     intf_DbgMsg("input: Thread end");
381 }
382
383 /*****************************************************************************
384  * InitThread: init the input Thread
385  *****************************************************************************/
386 static int InitThread( input_thread_t * p_input )
387 {
388
389 #ifdef STATS
390     /* Initialize statistics */
391     p_input->c_loops                    = 0;
392     p_input->c_bytes                    = 0;
393     p_input->c_payload_bytes            = 0;
394     p_input->c_packets_read             = 0;
395     p_input->c_packets_trashed          = 0;
396 #endif
397
398     /* Set locks. */
399     vlc_mutex_init( &p_input->stream.stream_lock );
400     vlc_cond_init( &p_input->stream.stream_wait );
401     vlc_mutex_init( &p_input->stream.control.control_lock );
402
403     /* Default, might get overwritten */
404     p_input->pf_open = p_input->pf_file_open;
405     p_input->pf_close = p_input->pf_file_close;
406
407     p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
408                                            (probedata_t *)p_input );
409
410     if( p_input->p_input_module == NULL )
411     {
412         intf_ErrMsg( "input error: no suitable input module for `%s'",
413                      p_input->p_source );
414         return( -1 );
415     }
416
417 #define f p_input->p_input_module->p_functions->input.functions.input
418     p_input->pf_init          = f.pf_init;
419     if( f.pf_open != NULL )
420     {
421         p_input->pf_open          = f.pf_open;
422     }
423     if( f.pf_close != NULL )
424     {
425         p_input->pf_close         = f.pf_close;
426     }
427     p_input->pf_end           = f.pf_end;
428     p_input->pf_init_bit_stream= f.pf_init_bit_stream;
429     p_input->pf_read          = f.pf_read;
430     p_input->pf_set_area      = f.pf_set_area;
431     p_input->pf_demux         = f.pf_demux;
432     p_input->pf_new_packet    = f.pf_new_packet;
433     p_input->pf_new_pes       = f.pf_new_pes;
434     p_input->pf_delete_packet = f.pf_delete_packet;
435     p_input->pf_delete_pes    = f.pf_delete_pes;
436     p_input->pf_rewind        = f.pf_rewind;
437     p_input->pf_seek          = f.pf_seek;
438 #undef f
439
440     /* We found the appropriate plugin, open the target */
441     p_input->pf_open( p_input );
442
443     if( p_input->b_error )
444     {
445         /* We barfed -- exit nicely */
446         module_Unneed( p_input->p_input_module );
447         return( -1 );
448     }
449
450     p_input->pf_init( p_input );
451
452     if( p_input->b_error )
453     {
454         /* We barfed -- exit nicely */
455         p_input->pf_close( p_input );
456         module_Unneed( p_input->p_input_module );
457         return( -1 );
458     }
459
460     *p_input->pi_status = THREAD_READY;
461
462     return( 0 );
463 }
464
465 /*****************************************************************************
466  * ErrorThread: RunThread() error loop
467  *****************************************************************************
468  * This function is called when an error occured during thread main's loop.
469  *****************************************************************************/
470 static void ErrorThread( input_thread_t *p_input )
471 {
472     while( !p_input->b_die )
473     {
474         /* Sleep a while */
475         msleep( INPUT_IDLE_SLEEP );
476     }
477 }
478
479 /*****************************************************************************
480  * EndThread: end the input thread
481  *****************************************************************************/
482 static void EndThread( input_thread_t * p_input )
483 {
484     int *       pi_status;                                  /* thread status */
485
486     /* Store status */
487     pi_status = p_input->pi_status;
488     *pi_status = THREAD_END;
489
490 #ifdef STATS
491     {
492         struct tms cpu_usage;
493         times( &cpu_usage );
494
495         intf_Msg("input stats: cpu usage (user: %d, system: %d)",
496                  cpu_usage.tms_utime, cpu_usage.tms_stime);
497     }
498 #endif
499
500     /* Free all ES and destroy all decoder threads */
501     input_EndStream( p_input );
502
503     /* Free demultiplexer's data */
504     p_input->pf_end( p_input );
505
506     /* Close stream */
507     p_input->pf_close( p_input );
508
509     /* Release modules */
510     module_Unneed( p_input->p_input_module );
511
512 }
513
514 /*****************************************************************************
515  * DestroyThread: destroy the input thread
516  *****************************************************************************/
517 static void DestroyThread( input_thread_t * p_input )
518 {
519     int *       pi_status;                                  /* thread status */
520
521     /* Store status */
522     pi_status = p_input->pi_status;
523
524     /* Destroy Mutex locks */
525     vlc_mutex_destroy( &p_input->stream.control.control_lock );
526     vlc_mutex_destroy( &p_input->stream.stream_lock );
527     
528     /* Free input structure */
529     free( p_input );
530
531     /* Update status */
532     *pi_status = THREAD_OVER;
533 }
534
535 /*****************************************************************************
536  * FileOpen : open a file descriptor
537  *****************************************************************************/
538 static void FileOpen( input_thread_t * p_input )
539 {
540     struct stat         stat_info;
541     int                 i_stat;
542
543     char *psz_name = p_input->p_source;
544
545     /* FIXME: this code ought to be in the plugin so that code can
546      * be shared with the *_Probe function */
547     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
548     {
549         int i_size = strlen( psz_name );
550
551         if( ( i_size > 4 )
552             && !strncasecmp( psz_name, "dvd:", 4 ) )
553         {
554             /* get rid of the 'dvd:' stuff and try again */
555             psz_name += 4;
556             i_stat = stat( psz_name, &stat_info );
557         }
558         else if( ( i_size > 5 )
559                  && !strncasecmp( psz_name, "file:", 5 ) )
560         {
561             /* get rid of the 'file:' stuff and try again */
562             psz_name += 5;
563             i_stat = stat( psz_name, &stat_info );
564         }
565
566         if( i_stat == (-1) )
567         {
568             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
569                          psz_name, strerror(errno));
570             p_input->b_error = 1;
571             return;
572         }
573     }
574
575     vlc_mutex_lock( &p_input->stream.stream_lock );
576
577     /* If we are here we can control the pace... */
578     p_input->stream.b_pace_control = 1;
579
580     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
581          || S_ISBLK(stat_info.st_mode) )
582     {
583         p_input->stream.b_seekable = 1;
584         p_input->stream.p_selected_area->i_size = stat_info.st_size;
585     }
586     else if( S_ISFIFO(stat_info.st_mode)
587 #if !defined( SYS_BEOS ) && !defined( WIN32 )
588              || S_ISSOCK(stat_info.st_mode)
589 #endif
590              )
591     {
592         p_input->stream.b_seekable = 0;
593         p_input->stream.p_selected_area->i_size = 0;
594     }
595     else
596     {
597         vlc_mutex_unlock( &p_input->stream.stream_lock );
598         intf_ErrMsg( "input error: unknown file type for `%s'",
599                      psz_name );
600         p_input->b_error = 1;
601         return;
602     }
603
604     p_input->stream.p_selected_area->i_tell = 0;
605     vlc_mutex_unlock( &p_input->stream.stream_lock );
606
607     intf_WarnMsg( 1, "input: opening file `%s'", p_input->p_source );
608     if( (p_input->i_handle = open( psz_name,
609                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
610     {
611         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
612         p_input->b_error = 1;
613         return;
614     }
615
616 }
617
618 /*****************************************************************************
619  * FileClose : close a file descriptor
620  *****************************************************************************/
621 static void FileClose( input_thread_t * p_input )
622 {
623     intf_WarnMsg( 1, "input: closing file `%s'", p_input->p_source );
624
625     close( p_input->i_handle );
626
627     return;
628 }
629
630 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
631 /*****************************************************************************
632  * NetworkOpen : open a network socket 
633  *****************************************************************************/
634 static void NetworkOpen( input_thread_t * p_input )
635 {
636     char                *psz_server = NULL;
637     char                *psz_broadcast = NULL;
638     int                 i_port = 0;
639     int                 i_opt;
640     struct sockaddr_in  sock;
641 #ifdef WIN32
642     WSADATA Data;
643     int i_err;
644 #endif
645     
646 #ifdef WIN32
647     /* WinSock Library Init. */
648     i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
649
650     if( i_err )
651     {
652         intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
653         return ;
654     }
655 #endif
656     
657     /* Get the remote server */
658     if( p_input->p_source != NULL )
659     {
660         psz_server = p_input->p_source;
661
662         /* Skip the protocol name */
663         while( *psz_server && *psz_server != ':' )
664         {
665             psz_server++;
666         }
667
668         /* Skip the "://" part */
669         while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
670         {
671             psz_server++;
672         }
673
674         /* Found a server name */
675         if( *psz_server )
676         {
677             char *psz_port = psz_server;
678
679             /* Skip the hostname part */
680             while( *psz_port && *psz_port != ':' )
681             {
682                 psz_port++;
683             }
684
685             /* Found a port name */
686             if( *psz_port )
687             {
688                 /* Replace ':' with '\0' */
689                 *psz_port = '\0';
690                 psz_port++;
691
692                 psz_broadcast = psz_port;
693                 while( *psz_broadcast && *psz_broadcast != ':' )
694                 {
695                     psz_broadcast++;
696                 }
697
698                 if( *psz_broadcast )
699                 {
700                     *psz_broadcast = '\0';
701                     psz_broadcast++;
702                     while( *psz_broadcast && *psz_broadcast == ':' )
703                     {
704                         psz_broadcast++;
705                     }
706                 }
707                 else
708                 {
709                     psz_broadcast = NULL;
710                 }
711
712                 /* port before broadcast address */
713                 if( *psz_port != ':' )
714                 {
715                     i_port = atoi( psz_port );
716                 }
717             }
718         }
719         else
720         {
721             psz_server = NULL;
722         }
723     }
724
725     /* Check that we got a valid server */
726     if( psz_server == NULL )
727     {
728         psz_server = main_GetPszVariable( INPUT_SERVER_VAR, 
729                                           INPUT_SERVER_DEFAULT );
730     }
731
732     /* Check that we got a valid port */
733     if( i_port == 0 )
734     {
735         i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
736     }
737
738     if( psz_broadcast == NULL )
739     {
740         /* Are we broadcasting ? */
741         if( main_GetIntVariable( INPUT_BROADCAST_VAR,
742                                  INPUT_BROADCAST_DEFAULT ) )
743         {
744             psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
745                                                  INPUT_BCAST_ADDR_DEFAULT );
746         }
747         else
748         {
749            psz_broadcast = NULL; 
750         }
751     }
752
753     intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
754                      psz_server, i_port, psz_broadcast );
755
756     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
757      * protocol */
758     p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
759     if( p_input->i_handle == -1 )
760     {
761         intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
762         p_input->b_error = 1;
763         return;
764     }
765
766     /* We may want to reuse an already used socket */
767     i_opt = 1;
768     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
769                     (void*) &i_opt, sizeof( i_opt ) ) == -1 )
770     {
771         intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
772                      strerror(errno));
773         close( p_input->i_handle );
774         p_input->b_error = 1;
775         return;
776     }
777
778     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
779      * packet loss caused by scheduling problems */
780     i_opt = 0x80000;
781     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
782                     (void*) &i_opt, sizeof( i_opt ) ) == -1 )
783     {
784         intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)", 
785                      strerror(errno));
786         close( p_input->i_handle );
787         p_input->b_error = 1;
788         return;
789     }
790
791     /* Build the local socket */
792     if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
793     {
794         intf_ErrMsg( "input error: can't build local address" );
795         close( p_input->i_handle );
796         p_input->b_error = 1;
797         return;
798     }
799
800 #if defined( WIN32 )
801     if ( psz_broadcast != NULL )
802     {
803         sock.sin_addr.s_addr = INADDR_ANY;
804     }
805 #endif
806     
807     /* Bind it */
808     if( bind( p_input->i_handle, (struct sockaddr *)&sock, 
809               sizeof( sock ) ) < 0 )
810     {
811         intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
812         close( p_input->i_handle );
813         p_input->b_error = 1;
814         return;
815     }
816
817     /* Build socket for remote connection */
818     if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
819     {
820         intf_ErrMsg( "input error: can't build remote address" );
821         close( p_input->i_handle );
822         p_input->b_error = 1;
823         return;
824     }
825
826     /* And connect it ... should we really connect ? */
827     if( connect( p_input->i_handle, (struct sockaddr *) &sock,
828                  sizeof( sock ) ) == (-1) )
829     {
830         intf_ErrMsg( "input error: can't connect socket, %s", 
831                      strerror(errno) );
832         close( p_input->i_handle );
833         p_input->b_error = 1;
834         return;
835     }
836
837     /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
838      * with the server. */
839     p_input->stream.b_pace_control = 0;
840     p_input->stream.b_seekable = 0;
841
842     intf_WarnMsg( 3, "input: successfully opened network mode" );
843     
844     return;
845 }
846
847 /*****************************************************************************
848  * NetworkClose : close a network socket
849  *****************************************************************************/
850 static void NetworkClose( input_thread_t * p_input )
851 {
852     close( p_input->i_handle );
853
854 #ifdef WIN32 
855     WSACleanup();
856 #endif
857
858 }
859 #endif
860