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