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