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