]> git.sesse.net Git - vlc/blob - src/input/input.c
* Win32 network support by Boris Dor�s <babal@via.ecp.fr>.
[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.124 2001/06/21 07:22:03 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
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_read          = f.pf_read;
411     p_input->pf_set_area      = f.pf_set_area;
412     p_input->pf_demux         = f.pf_demux;
413     p_input->pf_new_packet    = f.pf_new_packet;
414     p_input->pf_new_pes       = f.pf_new_pes;
415     p_input->pf_delete_packet = f.pf_delete_packet;
416     p_input->pf_delete_pes    = f.pf_delete_pes;
417     p_input->pf_rewind        = f.pf_rewind;
418     p_input->pf_seek          = f.pf_seek;
419 #undef f
420
421     /* We found the appropriate plugin, open the target */
422     p_input->pf_open( p_input );
423
424     if( p_input->b_error )
425     {
426         /* We barfed -- exit nicely */
427         module_Unneed( p_input->p_input_module );
428         return( -1 );
429     }
430
431     p_input->pf_init( p_input );
432
433     if( p_input->b_error )
434     {
435         /* We barfed -- exit nicely */
436         p_input->pf_close( p_input );
437         module_Unneed( p_input->p_input_module );
438         return( -1 );
439     }
440
441     *p_input->pi_status = THREAD_READY;
442
443     return( 0 );
444 }
445
446 /*****************************************************************************
447  * ErrorThread: RunThread() error loop
448  *****************************************************************************
449  * This function is called when an error occured during thread main's loop.
450  *****************************************************************************/
451 static void ErrorThread( input_thread_t *p_input )
452 {
453     while( !p_input->b_die )
454     {
455         /* Sleep a while */
456         msleep( INPUT_IDLE_SLEEP );
457     }
458 }
459
460 /*****************************************************************************
461  * EndThread: end the input thread
462  *****************************************************************************/
463 static void EndThread( input_thread_t * p_input )
464 {
465     int *       pi_status;                                  /* thread status */
466
467     /* Store status */
468     pi_status = p_input->pi_status;
469     *pi_status = THREAD_END;
470
471 #ifdef STATS
472     {
473         struct tms cpu_usage;
474         times( &cpu_usage );
475
476         intf_Msg("input stats: cpu usage (user: %d, system: %d)",
477                  cpu_usage.tms_utime, cpu_usage.tms_stime);
478     }
479 #endif
480
481     /* Free all ES and destroy all decoder threads */
482     input_EndStream( p_input );
483
484     /* Free demultiplexer's data */
485     p_input->pf_end( p_input );
486
487     /* Close stream */
488     p_input->pf_close( p_input );
489
490     /* Release modules */
491     module_Unneed( p_input->p_input_module );
492
493 }
494
495 /*****************************************************************************
496  * DestroyThread: destroy the input thread
497  *****************************************************************************/
498 static void DestroyThread( input_thread_t * p_input )
499 {
500     int *       pi_status;                                  /* thread status */
501
502     /* Store status */
503     pi_status = p_input->pi_status;
504
505     /* Destroy Mutex locks */
506     vlc_mutex_destroy( &p_input->stream.control.control_lock );
507     vlc_mutex_destroy( &p_input->stream.stream_lock );
508     
509     /* Free input structure */
510     free( p_input );
511
512     /* Update status */
513     *pi_status = THREAD_OVER;
514 }
515
516 /*****************************************************************************
517  * FileOpen : open a file descriptor
518  *****************************************************************************/
519 static void FileOpen( input_thread_t * p_input )
520 {
521     struct stat         stat_info;
522     int                 i_stat;
523
524     char *psz_name = p_input->p_source;
525
526     /* FIXME: this code ought to be in the plugin so that code can
527      * be shared with the *_Probe function */
528     if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
529     {
530         int i_size = strlen( psz_name );
531
532         if( ( i_size > 4 )
533             && !strncasecmp( psz_name, "dvd:", 4 ) )
534         {
535             /* get rid of the 'dvd:' stuff and try again */
536             psz_name += 4;
537             i_stat = stat( psz_name, &stat_info );
538         }
539         else if( ( i_size > 5 )
540                  && !strncasecmp( psz_name, "file:", 5 ) )
541         {
542             /* get rid of the 'file:' stuff and try again */
543             psz_name += 5;
544             i_stat = stat( psz_name, &stat_info );
545         }
546
547         if( i_stat == (-1) )
548         {
549             intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
550                          psz_name, strerror(errno));
551             p_input->b_error = 1;
552             return;
553         }
554     }
555
556     vlc_mutex_lock( &p_input->stream.stream_lock );
557
558     /* If we are here we can control the pace... */
559     p_input->stream.b_pace_control = 1;
560
561     if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
562          || S_ISBLK(stat_info.st_mode) )
563     {
564         p_input->stream.b_seekable = 1;
565         p_input->stream.p_selected_area->i_size = stat_info.st_size;
566     }
567     else if( S_ISFIFO(stat_info.st_mode)
568 #if !defined( SYS_BEOS ) && !defined( WIN32 )
569              || S_ISSOCK(stat_info.st_mode)
570 #endif
571              )
572     {
573         p_input->stream.b_seekable = 0;
574         p_input->stream.p_selected_area->i_size = 0;
575     }
576     else
577     {
578         vlc_mutex_unlock( &p_input->stream.stream_lock );
579         intf_ErrMsg( "input error: unknown file type for `%s'",
580                      psz_name );
581         p_input->b_error = 1;
582         return;
583     }
584
585     p_input->stream.p_selected_area->i_tell = 0;
586     vlc_mutex_unlock( &p_input->stream.stream_lock );
587
588     intf_WarnMsg( 1, "input: opening file `%s'", p_input->p_source );
589 #if defined( WIN32 )
590     if( (p_input->i_handle = open( psz_name, O_BINARY ) ) == (-1) )
591 #else
592     if( (p_input->i_handle = open( psz_name,
593                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
594 #endif
595     {
596         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
597         p_input->b_error = 1;
598         return;
599     }
600
601 }
602
603 /*****************************************************************************
604  * FileClose : close a file descriptor
605  *****************************************************************************/
606 static void FileClose( input_thread_t * p_input )
607 {
608     intf_WarnMsg( 1, "input: closing file `%s'", p_input->p_source );
609
610     close( p_input->i_handle );
611
612     return;
613 }
614
615 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
616 /*****************************************************************************
617  * NetworkOpen : open a network socket 
618  *****************************************************************************/
619 static void NetworkOpen( input_thread_t * p_input )
620 {
621     char                *psz_server = NULL;
622     char                *psz_broadcast = NULL;
623     int                 i_port = 0;
624     int                 i_opt;
625     struct sockaddr_in  sock;
626 #ifdef WIN32
627     WSADATA Data;
628     int i_err;
629 #endif
630     
631 #ifdef WIN32
632     /* WinSock Library Init. */
633     i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
634
635     if( i_err )
636     {
637         intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
638         return ;
639     }
640 #endif
641     
642     /* Get the remote server */
643     if( p_input->p_source != NULL )
644     {
645         psz_server = p_input->p_source;
646
647         /* Skip the protocol name */
648         while( *psz_server && *psz_server != ':' )
649         {
650             psz_server++;
651         }
652
653         /* Skip the "://" part */
654         while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
655         {
656             psz_server++;
657         }
658
659         /* Found a server name */
660         if( *psz_server )
661         {
662             char *psz_port = psz_server;
663
664             /* Skip the hostname part */
665             while( *psz_port && *psz_port != ':' )
666             {
667                 psz_port++;
668             }
669
670             /* Found a port name */
671             if( *psz_port )
672             {
673                 /* Replace ':' with '\0' */
674                 *psz_port = '\0';
675                 psz_port++;
676
677                 psz_broadcast = psz_port;
678                 while( *psz_broadcast && *psz_broadcast != ':' )
679                 {
680                     psz_broadcast++;
681                 }
682
683                 if( *psz_broadcast )
684                 {
685                     *psz_broadcast = '\0';
686                     psz_broadcast++;
687                     while( *psz_broadcast && *psz_broadcast == ':' )
688                     {
689                         psz_broadcast++;
690                     }
691                 }
692                 else
693                 {
694                     psz_broadcast = NULL;
695                 }
696
697                 /* port before broadcast address */
698                 if( *psz_port != ':' )
699                 {
700                     i_port = atoi( psz_port );
701                 }
702             }
703         }
704         else
705         {
706             psz_server = NULL;
707         }
708     }
709
710     /* Check that we got a valid server */
711     if( psz_server == NULL )
712     {
713         psz_server = main_GetPszVariable( INPUT_SERVER_VAR, 
714                                           INPUT_SERVER_DEFAULT );
715     }
716
717     /* Check that we got a valid port */
718     if( i_port == 0 )
719     {
720         i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
721     }
722
723     if( psz_broadcast == NULL )
724     {
725         /* Are we broadcasting ? */
726         if( main_GetIntVariable( INPUT_BROADCAST_VAR,
727                                  INPUT_BROADCAST_DEFAULT ) )
728         {
729             psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
730                                                  INPUT_BCAST_ADDR_DEFAULT );
731         }
732         else
733         {
734            psz_broadcast = NULL; 
735         }
736     }
737
738     intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
739                      psz_server, i_port, psz_broadcast );
740
741     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
742      * protocol */
743     p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
744     if( p_input->i_handle == -1 )
745     {
746         intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
747         p_input->b_error = 1;
748         return;
749     }
750
751     /* We may want to reuse an already used socket */
752     i_opt = 1;
753     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
754                     (void*) &i_opt, sizeof( i_opt ) ) == -1 )
755     {
756         intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
757                      strerror(errno));
758         close( p_input->i_handle );
759         p_input->b_error = 1;
760         return;
761     }
762
763     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
764      * packet loss caused by scheduling problems */
765     i_opt = 0x80000;
766     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
767                     (void*) &i_opt, sizeof( i_opt ) ) == -1 )
768     {
769         intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)", 
770                      strerror(errno));
771         close( p_input->i_handle );
772         p_input->b_error = 1;
773         return;
774     }
775
776     /* Build the local socket */
777     if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
778     {
779         intf_ErrMsg( "input error: can't build local address" );
780         close( p_input->i_handle );
781         p_input->b_error = 1;
782         return;
783     }
784
785 #if defined( WIN32 )
786     if ( psz_broadcast != NULL )
787     {
788         sock.sin_addr.s_addr = INADDR_ANY;
789     }
790 #endif
791     
792     /* Bind it */
793     if( bind( p_input->i_handle, (struct sockaddr *)&sock, 
794               sizeof( sock ) ) < 0 )
795     {
796         intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
797         close( p_input->i_handle );
798         p_input->b_error = 1;
799         return;
800     }
801
802     /* Build socket for remote connection */
803     if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
804     {
805         intf_ErrMsg( "input error: can't build remote address" );
806         close( p_input->i_handle );
807         p_input->b_error = 1;
808         return;
809     }
810
811     /* And connect it ... should we really connect ? */
812     if( connect( p_input->i_handle, (struct sockaddr *) &sock,
813                  sizeof( sock ) ) == (-1) )
814     {
815         intf_ErrMsg( "input error: can't connect socket, %s", 
816                      strerror(errno) );
817         close( p_input->i_handle );
818         p_input->b_error = 1;
819         return;
820     }
821
822     /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
823      * with the server. */
824     p_input->stream.b_pace_control = 0;
825     p_input->stream.b_seekable = 0;
826
827     intf_WarnMsg( 3, "input: successfully opened network mode" );
828     
829     return;
830 }
831
832 /*****************************************************************************
833  * NetworkClose : close a network socket
834  *****************************************************************************/
835 static void NetworkClose( input_thread_t * p_input )
836 {
837     close( p_input->i_handle );
838
839 #ifdef WIN32 
840     WSACleanup();
841 #endif
842
843 }
844 #endif
845