1 /*****************************************************************************
2 * input.c: input thread
3 * Read an MPEG2 stream, demultiplex and parse it before sending it to
5 *****************************************************************************
6 * Copyright (C) 1998, 1999, 2000 VideoLAN
7 * $Id: input.c,v 1.147 2001/10/30 10:57:37 massiot Exp $
9 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <sys/types.h>
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
45 #ifdef STRNCASECMP_IN_STRINGS_H
51 # include <winsock2.h>
52 #elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
53 # include <netdb.h> /* hostent ... */
54 # include <sys/socket.h>
55 # include <netinet/in.h>
56 # include <arpa/inet.h>
57 # include <sys/types.h>
58 # include <sys/socket.h>
61 #ifdef HAVE_SYS_TIMES_H
62 # include <sys/times.h>
73 #include "intf_playlist.h"
75 #include "stream_control.h"
76 #include "input_ext-intf.h"
77 #include "input_ext-dec.h"
78 #include "input_ext-plugins.h"
80 #include "interface.h"
84 /*****************************************************************************
86 *****************************************************************************/
87 static void RunThread ( input_thread_t *p_input );
88 static int InitThread ( input_thread_t *p_input );
89 static void ErrorThread ( input_thread_t *p_input );
90 static void DestroyThread ( input_thread_t *p_input );
91 static void EndThread ( input_thread_t *p_input );
93 static void FileOpen ( input_thread_t *p_input );
94 static void StdOpen ( input_thread_t *p_input );
95 static void FileClose ( input_thread_t *p_input );
96 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
97 static void NetworkOpen ( input_thread_t *p_input );
98 static void HTTPOpen ( input_thread_t *p_input );
99 static void NetworkClose ( input_thread_t *p_input );
102 /*****************************************************************************
103 * input_CreateThread: creates a new input thread
104 *****************************************************************************
105 * This function creates a new input, and returns a pointer
106 * to its description. On error, it returns NULL.
107 * If pi_status is NULL, then the function will block until the thread is ready.
108 * If not, it will be updated using one of the THREAD_* constants.
109 *****************************************************************************/
110 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
112 input_thread_t * p_input; /* thread descriptor */
113 int i_status; /* thread status */
115 /* Allocate descriptor */
116 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
117 if( p_input == NULL )
119 intf_ErrMsg( "input error: can't allocate input thread (%s)",
124 /* Packets read once */
125 p_input->i_read_once = INPUT_READ_ONCE;
127 /* Initialize thread properties */
129 p_input->b_error = 0;
133 p_input->p_source = p_item->psz_name;
135 /* I have never understood that stuff --Meuuh */
136 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
137 *p_input->pi_status = THREAD_CREATE;
139 /* Initialize stream description */
140 p_input->stream.i_es_number = 0;
141 p_input->stream.i_selected_es_number = 0;
142 p_input->stream.i_pgrm_number = 0;
143 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
144 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
145 p_input->stream.i_mux_rate = 0;
147 /* no stream, no area */
148 p_input->stream.i_area_nb = 0;
149 p_input->stream.pp_areas = NULL;
150 p_input->stream.p_selected_area = NULL;
151 p_input->stream.p_new_area = NULL;
153 /* By default there is one area in a stream */
154 input_AddArea( p_input );
155 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
157 /* Initialize stream control properties. */
158 p_input->stream.control.i_status = PLAYING_S;
159 p_input->stream.control.i_rate = DEFAULT_RATE;
160 p_input->stream.control.b_mute = 0;
161 p_input->stream.control.b_grayscale = main_GetIntVariable(
162 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
163 p_input->stream.control.i_smp = main_GetIntVariable(
164 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
166 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
169 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
172 intf_ErrMsg( "input error: can't create input thread (%s)",
178 /* If status is NULL, wait until the thread is created */
179 if( pi_status == NULL )
183 msleep( THREAD_SLEEP );
184 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
185 && (i_status != THREAD_FATAL) );
186 if( i_status != THREAD_READY )
194 /*****************************************************************************
195 * input_DestroyThread: mark an input thread as zombie
196 *****************************************************************************
197 * This function should not return until the thread is effectively cancelled.
198 *****************************************************************************/
199 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
201 int i_status; /* thread status */
204 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
205 *p_input->pi_status = THREAD_DESTROY;
207 /* Request thread destruction */
210 /* Make the thread exit of an eventual vlc_cond_wait() */
211 vlc_mutex_lock( &p_input->stream.stream_lock );
212 vlc_cond_signal( &p_input->stream.stream_wait );
213 vlc_mutex_unlock( &p_input->stream.stream_lock );
215 /* If status is NULL, wait until thread has been destroyed */
216 if( pi_status == NULL )
220 msleep( THREAD_SLEEP );
221 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
222 && (i_status != THREAD_FATAL) );
226 /*****************************************************************************
227 * RunThread: main thread loop
228 *****************************************************************************
229 * Thread in charge of processing the network packets and demultiplexing.
230 *****************************************************************************/
231 static void RunThread( input_thread_t *p_input )
234 data_packet_t ** pp_packets;
236 if( InitThread( p_input ) )
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 );
246 /* initialization is completed */
247 vlc_mutex_lock( &p_input->stream.stream_lock );
248 p_input->stream.b_changed = 1;
249 vlc_mutex_unlock( &p_input->stream.stream_lock );
251 pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
252 sizeof( data_packet_t * ) );
253 if( pp_packets == NULL )
255 intf_ErrMsg( "input error: out of memory" );
257 p_input->b_error = 1;
260 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
264 vlc_mutex_lock( &p_input->stream.stream_lock );
266 if( p_input->stream.p_new_area )
268 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
271 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
273 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
275 pgrm_descriptor_t * p_pgrm
276 = p_input->stream.pp_programs[i];
277 /* Escape all decoders for the stream discontinuity they
279 input_EscapeDiscontinuity( p_input, p_pgrm );
281 /* Reinitialize synchro. */
282 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
285 p_input->stream.p_new_area = NULL;
288 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
290 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
292 p_input->pf_seek( p_input,
293 p_input->stream.p_selected_area->i_seek );
295 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
297 pgrm_descriptor_t * p_pgrm
298 = p_input->stream.pp_programs[i];
299 /* Escape all decoders for the stream discontinuity they
301 input_EscapeDiscontinuity( p_input, p_pgrm );
303 /* Reinitialize synchro. */
304 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
307 p_input->stream.p_selected_area->i_seek = NO_SEEK;
310 if( p_input->stream.p_removed_es )
312 input_UnselectES( p_input, p_input->stream.p_removed_es );
313 p_input->stream.p_removed_es = NULL;
316 if( p_input->stream.p_newly_selected_es )
318 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
319 p_input->stream.p_newly_selected_es = NULL;
322 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
324 if( p_input->stream.b_new_mute )
326 input_EscapeAudioDiscontinuity( p_input );
329 vlc_mutex_lock( &p_input->stream.control.control_lock );
330 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
331 vlc_mutex_unlock( &p_input->stream.control.control_lock );
333 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
336 vlc_mutex_unlock( &p_input->stream.stream_lock );
338 i_error = p_input->pf_read( p_input, pp_packets );
340 /* Demultiplex read packets. */
341 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
343 p_input->stream.c_packets_read++;
344 p_input->pf_demux( p_input, pp_packets[i] );
351 /* End of file - we do not set b_die because only the
352 * interface is allowed to do so. */
353 intf_WarnMsg( 3, "input: EOF reached" );
358 p_input->b_error = 1;
365 if( p_input->b_error || p_input->b_eof )
367 ErrorThread( p_input );
370 EndThread( p_input );
372 DestroyThread( p_input );
374 intf_DbgMsg("input: Thread end");
377 /*****************************************************************************
378 * InitThread: init the input Thread
379 *****************************************************************************/
380 static int InitThread( input_thread_t * p_input )
383 /* Initialize statistics */
384 p_input->c_loops = 0;
385 p_input->stream.c_packets_read = 0;
386 p_input->stream.c_packets_trashed = 0;
387 p_input->p_stream = NULL;
390 vlc_mutex_init( &p_input->stream.stream_lock );
391 vlc_cond_init( &p_input->stream.stream_wait );
392 vlc_mutex_init( &p_input->stream.control.control_lock );
394 /* Find appropriate module. */
395 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
396 (probedata_t *)p_input );
398 if( p_input->p_input_module == NULL )
400 intf_ErrMsg( "input error: no suitable input module for `%s'",
405 #define f p_input->p_input_module->p_functions->input.functions.input
406 p_input->pf_init = f.pf_init;
407 p_input->pf_end = f.pf_end;
408 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
409 p_input->pf_read = f.pf_read;
410 p_input->pf_set_area = f.pf_set_area;
411 p_input->pf_demux = f.pf_demux;
412 p_input->pf_new_packet = f.pf_new_packet;
413 p_input->pf_new_pes = f.pf_new_pes;
414 p_input->pf_delete_packet = f.pf_delete_packet;
415 p_input->pf_delete_pes = f.pf_delete_pes;
416 p_input->pf_rewind = f.pf_rewind;
417 p_input->pf_seek = f.pf_seek;
420 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
421 /* FIXME : this is waaaay too kludgy */
422 if( (strlen( p_input->p_source ) > 3) && !strncasecmp( p_input->p_source, "ts:", 3 ) )
425 NetworkOpen( p_input );
426 p_input->stream.i_method = INPUT_METHOD_NETWORK;
428 else if( ( strlen( p_input->p_source ) > 5 ) && !strncasecmp( p_input->p_source, "http:", 5 ) )
432 p_input->stream.i_method = INPUT_METHOD_NETWORK;
436 if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
438 /* DVD - this is THE kludge */
439 p_input->p_input_module->p_functions->input.functions.input.pf_open( p_input );
440 p_input->stream.i_method = INPUT_METHOD_DVD;
442 else if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
444 /* Dummy input - very kludgy */
445 p_input->p_input_module->p_functions->input.functions.input.pf_open( p_input );
447 else if( ( strlen( p_input->p_source ) == 1 ) && *p_input->p_source == '-' )
456 p_input->stream.i_method = INPUT_METHOD_FILE;
459 if( p_input->b_error )
461 /* We barfed -- exit nicely */
462 module_Unneed( p_input->p_input_module );
466 p_input->pf_init( p_input );
468 if( p_input->b_error )
470 /* We barfed -- exit nicely */
471 p_input->pf_close( p_input );
472 module_Unneed( p_input->p_input_module );
476 *p_input->pi_status = THREAD_READY;
481 /*****************************************************************************
482 * ErrorThread: RunThread() error loop
483 *****************************************************************************
484 * This function is called when an error occured during thread main's loop.
485 *****************************************************************************/
486 static void ErrorThread( input_thread_t *p_input )
488 while( !p_input->b_die )
491 msleep( INPUT_IDLE_SLEEP );
495 /*****************************************************************************
496 * EndThread: end the input thread
497 *****************************************************************************/
498 static void EndThread( input_thread_t * p_input )
500 int * pi_status; /* thread status */
503 pi_status = p_input->pi_status;
504 *pi_status = THREAD_END;
506 if( p_main->b_stats )
508 #ifdef HAVE_SYS_TIMES_H
509 /* Display statistics */
510 struct tms cpu_usage;
513 intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
515 cpu_usage.tms_utime, cpu_usage.tms_stime );
517 intf_StatMsg( "input stats: %d loops", p_input->c_loops );
520 input_DumpStream( p_input );
523 /* Free all ES and destroy all decoder threads */
524 input_EndStream( p_input );
526 /* Free demultiplexer's data */
527 p_input->pf_end( p_input );
529 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
531 if( (strlen( p_input->p_source ) > 3) && !strncasecmp( p_input->p_source, "ts:", 3 ) )
533 NetworkClose( p_input );
535 else if( ( strlen( p_input->p_source ) > 5 ) && !strncasecmp( p_input->p_source, "http:", 5 ) )
537 NetworkClose( p_input );
541 if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
543 p_input->p_input_module->p_functions->input.functions.input.pf_close( p_input );
545 else if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
547 p_input->p_input_module->p_functions->input.functions.input.pf_close( p_input );
551 FileClose( p_input );
554 /* Release modules */
555 module_Unneed( p_input->p_input_module );
559 /*****************************************************************************
560 * DestroyThread: destroy the input thread
561 *****************************************************************************/
562 static void DestroyThread( input_thread_t * p_input )
564 int * pi_status; /* thread status */
567 pi_status = p_input->pi_status;
569 /* Destroy Mutex locks */
570 vlc_mutex_destroy( &p_input->stream.control.control_lock );
571 vlc_mutex_destroy( &p_input->stream.stream_lock );
573 /* Free input structure */
577 *pi_status = THREAD_OVER;
580 /*****************************************************************************
581 * StdOpen : open standard input
582 *****************************************************************************/
583 static void StdOpen( input_thread_t * p_input )
585 vlc_mutex_lock( &p_input->stream.stream_lock );
587 /* Suppose we can control the pace - this won't work in some cases ! */
588 p_input->stream.b_pace_control = 1;
590 p_input->stream.b_seekable = 0;
591 p_input->stream.p_selected_area->i_size = 0;
592 p_input->stream.p_selected_area->i_tell = 0;
593 vlc_mutex_unlock( &p_input->stream.stream_lock );
595 intf_WarnMsg( 2, "input: opening stdin" );
596 p_input->i_handle = 0;
599 /*****************************************************************************
600 * FileOpen : open a file descriptor
601 *****************************************************************************/
602 static void FileOpen( input_thread_t * p_input )
604 struct stat stat_info;
607 char *psz_name = p_input->p_source;
609 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
611 int i_size = strlen( psz_name );
614 && !strncasecmp( psz_name, "dvd:", 4 ) )
616 /* get rid of the 'dvd:' stuff and try again */
618 i_stat = stat( psz_name, &stat_info );
620 else if( ( i_size > 4 )
621 && !strncasecmp( psz_name, "vcd:", 4 ) )
623 /* get rid of the 'vcd:' stuff and try again */
625 i_stat = stat( psz_name, &stat_info );
627 else if( ( i_size > 5 )
628 && !strncasecmp( psz_name, "file:", 5 ) )
630 /* get rid of the 'file:' stuff and try again */
632 i_stat = stat( psz_name, &stat_info );
637 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
638 psz_name, strerror(errno));
639 p_input->b_error = 1;
644 vlc_mutex_lock( &p_input->stream.stream_lock );
646 /* If we are here we can control the pace... */
647 p_input->stream.b_pace_control = 1;
649 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
650 || S_ISBLK(stat_info.st_mode) )
652 p_input->stream.b_seekable = 1;
653 p_input->stream.p_selected_area->i_size = stat_info.st_size;
655 else if( S_ISFIFO(stat_info.st_mode)
656 #if !defined( SYS_BEOS ) && !defined( WIN32 )
657 || S_ISSOCK(stat_info.st_mode)
661 p_input->stream.b_seekable = 0;
662 p_input->stream.p_selected_area->i_size = 0;
666 vlc_mutex_unlock( &p_input->stream.stream_lock );
667 intf_ErrMsg( "input error: unknown file type for `%s'",
669 p_input->b_error = 1;
673 p_input->stream.p_selected_area->i_tell = 0;
674 vlc_mutex_unlock( &p_input->stream.stream_lock );
676 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
677 if( (p_input->i_handle = open( psz_name,
678 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
680 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
681 p_input->b_error = 1;
687 /*****************************************************************************
688 * FileClose : close a file descriptor
689 *****************************************************************************/
690 static void FileClose( input_thread_t * p_input )
692 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
694 close( p_input->i_handle );
699 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
700 /*****************************************************************************
701 * NetworkOpen : open a network socket
702 *****************************************************************************/
703 static void NetworkOpen( input_thread_t * p_input )
705 char *psz_server = NULL;
706 char *psz_broadcast = NULL;
710 struct sockaddr_in sock;
711 unsigned int i_mc_group;
719 /* WinSock Library Init. */
720 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
724 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
729 /* Get the remote server */
730 if( p_input->p_source != NULL )
732 psz_server = p_input->p_source;
734 /* Skip the protocol name */
735 while( *psz_server && *psz_server != ':' )
740 /* Skip the "://" part */
741 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
746 /* Found a server name */
749 char *psz_port = psz_server;
751 /* Skip the hostname part */
752 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
757 /* Found a port name or a broadcast addres */
760 /* That's a port name */
761 if( *psz_port == ':' )
765 i_port = atoi( psz_port );
768 /* Search for '/' just after the port in case
769 * we also have a broadcast address */
770 psz_broadcast = psz_port;
771 while( *psz_broadcast && *psz_broadcast != '/' )
778 *psz_broadcast = '\0';
780 while( *psz_broadcast && *psz_broadcast == '/' )
787 psz_broadcast = NULL;
797 /* Check that we got a valid server */
798 if( psz_server == NULL )
800 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
801 INPUT_SERVER_DEFAULT );
804 /* Check that we got a valid port */
807 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
810 if( psz_broadcast == NULL )
812 /* Are we broadcasting ? */
813 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
814 INPUT_BROADCAST_DEFAULT ) )
816 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
817 INPUT_BCAST_ADDR_DEFAULT );
821 psz_broadcast = NULL;
825 intf_WarnMsg( 2, "input: server=%s port=%d broadcast=%s",
826 psz_server, i_port, psz_broadcast );
828 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
830 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
831 if( p_input->i_handle == -1 )
833 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
834 p_input->b_error = 1;
838 /* We may want to reuse an already used socket */
840 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
841 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
843 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
845 close( p_input->i_handle );
846 p_input->b_error = 1;
850 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
851 * packet loss caused by scheduling problems */
853 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
854 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
856 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
858 close( p_input->i_handle );
859 p_input->b_error = 1;
863 /* Check if we really got what we have asked for, because Linux, etc.
864 * will silently limit the max buffer size to net.core.rmem_max which
865 * is typically only 65535 bytes */
867 i_opt_size = sizeof( i_opt );
868 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
869 (void*) &i_opt, &i_opt_size ) == -1 )
871 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
873 close( p_input->i_handle );
874 p_input->b_error = 1;
878 if( i_opt < 0x80000 )
880 intf_WarnMsg( 1, "input warning: socket receive buffer size just 0x%x"
881 " instead of 0x%x bytes.", i_opt, 0x80000 );
884 /* Build the local socket */
885 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
887 intf_ErrMsg( "input error: can't build local address" );
888 close( p_input->i_handle );
889 p_input->b_error = 1;
893 /* Required for IP_ADD_MEMBERSHIP */
894 i_mc_group = sock.sin_addr.s_addr;
897 if ( psz_broadcast != NULL )
899 sock.sin_addr.s_addr = INADDR_ANY;
902 #define IN_MULTICAST(a) IN_CLASSD(a)
906 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
907 sizeof( sock ) ) < 0 )
909 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
910 close( p_input->i_handle );
911 p_input->b_error = 1;
915 /* Join the multicast group if the socket is a multicast address */
918 if( IN_MULTICAST( ntohl(i_mc_group) ) )
922 imr.imr_interface.s_addr = htonl(INADDR_ANY);
923 imr.imr_multiaddr.s_addr = i_mc_group;
924 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
925 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
927 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
929 close( p_input->i_handle);
930 p_input->b_error = 1;
936 /* Build socket for remote connection */
937 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
939 intf_ErrMsg( "input error: can't build remote address" );
940 close( p_input->i_handle );
941 p_input->b_error = 1;
945 /* Only connect if the user has passed a valid host */
946 if( sock.sin_addr.s_addr != INADDR_ANY )
948 /* Connect the socket */
949 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
950 sizeof( sock ) ) == (-1) )
952 intf_ErrMsg( "input error: can't connect socket (%s)",
954 close( p_input->i_handle );
955 p_input->b_error = 1;
960 p_input->stream.b_pace_control = 0;
961 p_input->stream.b_seekable = 0;
963 intf_WarnMsg( 3, "input: successfully opened network mode" );
968 /*****************************************************************************
969 * NetworkClose : close a network socket
970 *****************************************************************************/
971 static void NetworkClose( input_thread_t * p_input )
973 close( p_input->i_handle );
980 /*****************************************************************************
981 * HTTPOpen : make an HTTP request
982 *****************************************************************************/
983 static void HTTPOpen( input_thread_t * p_input )
985 char *psz_server = NULL;
986 char *psz_path = NULL;
990 struct sockaddr_in sock;
991 char psz_buffer[256];
999 /* WinSock Library Init. */
1000 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
1004 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
1009 /* Get the remote server */
1010 if( p_input->p_source != NULL )
1012 psz_server = p_input->p_source;
1014 /* Skip the protocol name */
1015 while( *psz_server && *psz_server != ':' )
1020 /* Skip the "://" part */
1021 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1026 /* Found a server name */
1029 char *psz_port = psz_server;
1031 /* Skip the hostname part */
1032 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1037 /* Found a port name */
1040 if( *psz_port == ':' )
1042 /* Replace ':' with '\0' */
1047 psz_path = psz_port;
1048 while( *psz_path && *psz_path != '/' )
1063 if( *psz_port != '\0' )
1065 i_port = atoi( psz_port );
1075 /* Check that we got a valid server */
1076 if( psz_server == NULL )
1078 intf_ErrMsg( "input error: No server given" );
1079 p_input->b_error = 1;
1083 /* Check that we got a valid port */
1086 i_port = 80; /* FIXME */
1089 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1092 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1094 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1095 if( p_input->i_handle == -1 )
1097 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1101 /* We may want to reuse an already used socket */
1103 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1104 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1106 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1108 close( p_input->i_handle );
1109 p_input->b_error = 1;
1114 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1116 /* http://myproxy.mydomain:myport/ */
1117 int i_proxy_port = 0;
1119 /* Skip the protocol name */
1120 while( *psz_proxy && *psz_proxy != ':' )
1125 /* Skip the "://" part */
1126 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1131 /* Found a proxy name */
1134 char *psz_port = psz_proxy;
1136 /* Skip the hostname part */
1137 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1142 /* Found a port name */
1147 /* Replace ':' with '\0' */
1151 psz_junk = psz_port;
1152 while( *psz_junk && *psz_junk != '/' )
1162 if( *psz_port != '\0' )
1164 i_proxy_port = atoi( psz_port );
1170 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1171 close( p_input->i_handle );
1172 p_input->b_error = 1;
1176 /* Build socket for proxy connection */
1177 if ( network_BuildRemoteAddr( &sock, psz_proxy ) == -1 )
1179 intf_ErrMsg( "input error: can't build remote address" );
1180 close( p_input->i_handle );
1181 p_input->b_error = 1;
1184 sock.sin_port = htons( i_proxy_port );
1188 /* No proxy, direct connection */
1189 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
1191 intf_ErrMsg( "input error: can't build remote address" );
1192 close( p_input->i_handle );
1193 p_input->b_error = 1;
1196 sock.sin_port = htons( i_port );
1199 /* Connect the socket */
1200 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1201 sizeof( sock ) ) == (-1) )
1203 intf_ErrMsg( "input error: can't connect socket (%s)",
1205 close( p_input->i_handle );
1206 p_input->b_error = 1;
1210 p_input->stream.b_seekable = 0;
1211 p_input->stream.b_pace_control = 1; /* TCP/IP... */
1213 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1214 # define HTTP_END "\r\n"
1216 /* Prepare GET ... */
1217 if( psz_proxy != NULL )
1219 snprintf( psz_buffer, sizeof(psz_buffer),
1220 "GET http://%s:%d/%s HTTP/1.0\r\n"
1221 HTTP_USERAGENT HTTP_END,
1222 psz_server, i_port, psz_path );
1226 snprintf( psz_buffer, sizeof(psz_buffer),
1227 "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1228 HTTP_USERAGENT HTTP_END,
1229 psz_path, psz_server );
1231 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1234 if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1236 intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1237 close( p_input->i_handle );
1238 p_input->b_error = 1;
1242 /* Read HTTP header - this is gonna be fun with plug-ins which do not
1243 * use p_input->p_stream :-( */
1244 if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1246 intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1247 close( p_input->i_handle );
1248 p_input->b_error = 1;
1252 while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1254 if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1255 || *psz_buffer == '\r' || *psz_buffer == '\0' )
1259 /* FIXME : check Content-Type one day */
1262 intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1265 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */