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