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.145 2001/10/23 03:06:50 jobi 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 FileClose ( input_thread_t *p_input );
95 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
96 static void NetworkOpen ( input_thread_t *p_input );
97 static void HTTPOpen ( input_thread_t *p_input );
98 static void NetworkClose ( input_thread_t *p_input );
101 /*****************************************************************************
102 * input_CreateThread: creates a new input thread
103 *****************************************************************************
104 * This function creates a new input, and returns a pointer
105 * to its description. On error, it returns NULL.
106 * If pi_status is NULL, then the function will block until the thread is ready.
107 * If not, it will be updated using one of the THREAD_* constants.
108 *****************************************************************************/
109 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
111 input_thread_t * p_input; /* thread descriptor */
112 int i_status; /* thread status */
114 /* Allocate descriptor */
115 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
116 if( p_input == NULL )
118 intf_ErrMsg( "input error: can't allocate input thread (%s)",
123 /* Packets read once */
124 p_input->i_read_once = INPUT_READ_ONCE;
126 /* Initialize thread properties */
128 p_input->b_error = 0;
132 p_input->p_source = p_item->psz_name;
134 /* I have never understood that stuff --Meuuh */
135 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
136 *p_input->pi_status = THREAD_CREATE;
138 /* Initialize stream description */
139 p_input->stream.i_es_number = 0;
140 p_input->stream.i_selected_es_number = 0;
141 p_input->stream.i_pgrm_number = 0;
142 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
143 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
144 p_input->stream.i_mux_rate = 0;
146 /* no stream, no area */
147 p_input->stream.i_area_nb = 0;
148 p_input->stream.pp_areas = NULL;
149 p_input->stream.p_selected_area = NULL;
150 p_input->stream.p_new_area = NULL;
152 /* By default there is one area in a stream */
153 input_AddArea( p_input );
154 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
156 /* Initialize stream control properties. */
157 p_input->stream.control.i_status = PLAYING_S;
158 p_input->stream.control.i_rate = DEFAULT_RATE;
159 p_input->stream.control.b_mute = 0;
160 p_input->stream.control.b_grayscale = main_GetIntVariable(
161 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
162 p_input->stream.control.i_smp = main_GetIntVariable(
163 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
165 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
168 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
171 intf_ErrMsg( "input error: can't create input thread (%s)",
177 /* If status is NULL, wait until the thread is created */
178 if( pi_status == NULL )
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 )
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 )
200 int i_status; /* thread status */
203 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
204 *p_input->pi_status = THREAD_DESTROY;
206 /* Request thread destruction */
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 );
214 /* If status is NULL, wait until thread has been destroyed */
215 if( pi_status == NULL )
219 msleep( THREAD_SLEEP );
220 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
221 && (i_status != THREAD_FATAL) );
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 )
233 data_packet_t ** pp_packets;
235 if( InitThread( p_input ) )
237 /* If we failed, wait before we are killed, and exit */
238 *p_input->pi_status = THREAD_ERROR;
239 p_input->b_error = 1;
240 ErrorThread( p_input );
241 DestroyThread( p_input );
245 /* initialization is completed */
246 vlc_mutex_lock( &p_input->stream.stream_lock );
247 p_input->stream.b_changed = 1;
248 vlc_mutex_unlock( &p_input->stream.stream_lock );
250 pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
251 sizeof( data_packet_t * ) );
252 if( pp_packets == NULL )
254 intf_ErrMsg( "input error: out of memory" );
256 p_input->b_error = 1;
259 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
263 vlc_mutex_lock( &p_input->stream.stream_lock );
265 if( p_input->stream.p_new_area )
267 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
270 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
272 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
274 pgrm_descriptor_t * p_pgrm
275 = p_input->stream.pp_programs[i];
276 /* Escape all decoders for the stream discontinuity they
278 input_EscapeDiscontinuity( p_input, p_pgrm );
280 /* Reinitialize synchro. */
281 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
284 p_input->stream.p_new_area = NULL;
287 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
289 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
291 p_input->pf_seek( p_input,
292 p_input->stream.p_selected_area->i_seek );
294 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
296 pgrm_descriptor_t * p_pgrm
297 = p_input->stream.pp_programs[i];
298 /* Escape all decoders for the stream discontinuity they
300 input_EscapeDiscontinuity( p_input, p_pgrm );
302 /* Reinitialize synchro. */
303 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
306 p_input->stream.p_selected_area->i_seek = NO_SEEK;
309 if( p_input->stream.p_removed_es )
311 input_UnselectES( p_input, p_input->stream.p_removed_es );
312 p_input->stream.p_removed_es = NULL;
315 if( p_input->stream.p_newly_selected_es )
317 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
318 p_input->stream.p_newly_selected_es = NULL;
321 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
323 if( p_input->stream.b_new_mute )
325 input_EscapeAudioDiscontinuity( p_input );
328 vlc_mutex_lock( &p_input->stream.control.control_lock );
329 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
330 vlc_mutex_unlock( &p_input->stream.control.control_lock );
332 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
335 vlc_mutex_unlock( &p_input->stream.stream_lock );
337 i_error = p_input->pf_read( p_input, pp_packets );
339 /* Demultiplex read packets. */
340 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
342 p_input->stream.c_packets_read++;
343 p_input->pf_demux( p_input, pp_packets[i] );
350 /* End of file - we do not set b_die because only the
351 * interface is allowed to do so. */
352 intf_WarnMsg( 3, "input: EOF reached" );
357 p_input->b_error = 1;
364 if( p_input->b_error || p_input->b_eof )
366 ErrorThread( p_input );
369 EndThread( p_input );
371 DestroyThread( p_input );
373 intf_DbgMsg("input: Thread end");
376 /*****************************************************************************
377 * InitThread: init the input Thread
378 *****************************************************************************/
379 static int InitThread( input_thread_t * p_input )
382 /* Initialize statistics */
383 p_input->c_loops = 0;
384 p_input->stream.c_packets_read = 0;
385 p_input->stream.c_packets_trashed = 0;
386 p_input->p_stream = NULL;
389 vlc_mutex_init( &p_input->stream.stream_lock );
390 vlc_cond_init( &p_input->stream.stream_wait );
391 vlc_mutex_init( &p_input->stream.control.control_lock );
393 /* Find appropriate module. */
394 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
395 (probedata_t *)p_input );
397 if( p_input->p_input_module == NULL )
399 intf_ErrMsg( "input error: no suitable input module for `%s'",
404 #define f p_input->p_input_module->p_functions->input.functions.input
405 p_input->pf_init = f.pf_init;
406 p_input->pf_end = f.pf_end;
407 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
408 p_input->pf_read = f.pf_read;
409 p_input->pf_set_area = f.pf_set_area;
410 p_input->pf_demux = f.pf_demux;
411 p_input->pf_new_packet = f.pf_new_packet;
412 p_input->pf_new_pes = f.pf_new_pes;
413 p_input->pf_delete_packet = f.pf_delete_packet;
414 p_input->pf_delete_pes = f.pf_delete_pes;
415 p_input->pf_rewind = f.pf_rewind;
416 p_input->pf_seek = f.pf_seek;
419 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
420 /* FIXME : this is waaaay too kludgy */
421 if( (strlen( p_input->p_source ) > 3) && !strncasecmp( p_input->p_source, "ts:", 3 ) )
424 NetworkOpen( p_input );
425 p_input->stream.i_method = INPUT_METHOD_NETWORK;
427 else if( ( strlen( p_input->p_source ) > 5 ) && !strncasecmp( p_input->p_source, "http:", 5 ) )
431 p_input->stream.i_method = INPUT_METHOD_NETWORK;
435 if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
437 /* DVD - this is THE kludge */
438 p_input->p_input_module->p_functions->input.functions.input.pf_open( p_input );
439 p_input->stream.i_method = INPUT_METHOD_DVD;
441 else if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
443 /* Dummy input - very kludgy */
444 p_input->p_input_module->p_functions->input.functions.input.pf_open( p_input );
450 p_input->stream.i_method = INPUT_METHOD_FILE;
453 if( p_input->b_error )
455 /* We barfed -- exit nicely */
456 module_Unneed( p_input->p_input_module );
460 p_input->pf_init( p_input );
462 if( p_input->b_error )
464 /* We barfed -- exit nicely */
465 p_input->pf_close( p_input );
466 module_Unneed( p_input->p_input_module );
470 *p_input->pi_status = THREAD_READY;
475 /*****************************************************************************
476 * ErrorThread: RunThread() error loop
477 *****************************************************************************
478 * This function is called when an error occured during thread main's loop.
479 *****************************************************************************/
480 static void ErrorThread( input_thread_t *p_input )
482 while( !p_input->b_die )
485 msleep( INPUT_IDLE_SLEEP );
489 /*****************************************************************************
490 * EndThread: end the input thread
491 *****************************************************************************/
492 static void EndThread( input_thread_t * p_input )
494 int * pi_status; /* thread status */
497 pi_status = p_input->pi_status;
498 *pi_status = THREAD_END;
500 if( p_main->b_stats )
502 #ifdef HAVE_SYS_TIMES_H
503 /* Display statistics */
504 struct tms cpu_usage;
507 intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
509 cpu_usage.tms_utime, cpu_usage.tms_stime );
511 intf_StatMsg( "input stats: %d loops", p_input->c_loops );
514 input_DumpStream( p_input );
517 /* Free all ES and destroy all decoder threads */
518 input_EndStream( p_input );
520 /* Free demultiplexer's data */
521 p_input->pf_end( p_input );
523 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
525 if( (strlen( p_input->p_source ) > 3) && !strncasecmp( p_input->p_source, "ts:", 3 ) )
527 NetworkClose( p_input );
529 else if( ( strlen( p_input->p_source ) > 5 ) && !strncasecmp( p_input->p_source, "http:", 5 ) )
531 NetworkClose( p_input );
535 if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
537 p_input->p_input_module->p_functions->input.functions.input.pf_close( p_input );
539 else if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
541 p_input->p_input_module->p_functions->input.functions.input.pf_close( p_input );
545 FileClose( p_input );
548 /* Release modules */
549 module_Unneed( p_input->p_input_module );
553 /*****************************************************************************
554 * DestroyThread: destroy the input thread
555 *****************************************************************************/
556 static void DestroyThread( input_thread_t * p_input )
558 int * pi_status; /* thread status */
561 pi_status = p_input->pi_status;
563 /* Destroy Mutex locks */
564 vlc_mutex_destroy( &p_input->stream.control.control_lock );
565 vlc_mutex_destroy( &p_input->stream.stream_lock );
567 /* Free input structure */
571 *pi_status = THREAD_OVER;
574 /*****************************************************************************
575 * FileOpen : open a file descriptor
576 *****************************************************************************/
577 static void FileOpen( input_thread_t * p_input )
579 struct stat stat_info;
582 char *psz_name = p_input->p_source;
584 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
586 int i_size = strlen( psz_name );
589 && !strncasecmp( psz_name, "dvd:", 4 ) )
591 /* get rid of the 'dvd:' stuff and try again */
593 i_stat = stat( psz_name, &stat_info );
595 else if( ( i_size > 4 )
596 && !strncasecmp( psz_name, "vcd:", 4 ) )
598 /* get rid of the 'vcd:' stuff and try again */
600 i_stat = stat( psz_name, &stat_info );
602 else if( ( i_size > 5 )
603 && !strncasecmp( psz_name, "file:", 5 ) )
605 /* get rid of the 'file:' stuff and try again */
607 i_stat = stat( psz_name, &stat_info );
612 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
613 psz_name, strerror(errno));
614 p_input->b_error = 1;
619 vlc_mutex_lock( &p_input->stream.stream_lock );
621 /* If we are here we can control the pace... */
622 p_input->stream.b_pace_control = 1;
624 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
625 || S_ISBLK(stat_info.st_mode) )
627 p_input->stream.b_seekable = 1;
628 p_input->stream.p_selected_area->i_size = stat_info.st_size;
630 else if( S_ISFIFO(stat_info.st_mode)
631 #if !defined( SYS_BEOS ) && !defined( WIN32 )
632 || S_ISSOCK(stat_info.st_mode)
636 p_input->stream.b_seekable = 0;
637 p_input->stream.p_selected_area->i_size = 0;
641 vlc_mutex_unlock( &p_input->stream.stream_lock );
642 intf_ErrMsg( "input error: unknown file type for `%s'",
644 p_input->b_error = 1;
648 p_input->stream.p_selected_area->i_tell = 0;
649 vlc_mutex_unlock( &p_input->stream.stream_lock );
651 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
652 if( (p_input->i_handle = open( psz_name,
653 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
655 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
656 p_input->b_error = 1;
662 /*****************************************************************************
663 * FileClose : close a file descriptor
664 *****************************************************************************/
665 static void FileClose( input_thread_t * p_input )
667 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
669 close( p_input->i_handle );
674 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
675 /*****************************************************************************
676 * NetworkOpen : open a network socket
677 *****************************************************************************/
678 static void NetworkOpen( input_thread_t * p_input )
680 char *psz_server = NULL;
681 char *psz_broadcast = NULL;
685 struct sockaddr_in sock;
686 unsigned int i_mc_group;
694 /* WinSock Library Init. */
695 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
699 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
704 /* Get the remote server */
705 if( p_input->p_source != NULL )
707 psz_server = p_input->p_source;
709 /* Skip the protocol name */
710 while( *psz_server && *psz_server != ':' )
715 /* Skip the "://" part */
716 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
721 /* Found a server name */
724 char *psz_port = psz_server;
726 /* Skip the hostname part */
727 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
732 /* Found a port name or a broadcast addres */
735 /* That's a port name */
736 if( *psz_port == ':' )
740 i_port = atoi( psz_port );
743 /* Search for '/' just after the port in case
744 * we also have a broadcast address */
745 psz_broadcast = psz_port;
746 while( *psz_broadcast && *psz_broadcast != '/' )
753 *psz_broadcast = '\0';
755 while( *psz_broadcast && *psz_broadcast == '/' )
762 psz_broadcast = NULL;
772 /* Check that we got a valid server */
773 if( psz_server == NULL )
775 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
776 INPUT_SERVER_DEFAULT );
779 /* Check that we got a valid port */
782 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
785 if( psz_broadcast == NULL )
787 /* Are we broadcasting ? */
788 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
789 INPUT_BROADCAST_DEFAULT ) )
791 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
792 INPUT_BCAST_ADDR_DEFAULT );
796 psz_broadcast = NULL;
800 intf_WarnMsg( 2, "input: server=%s port=%d broadcast=%s",
801 psz_server, i_port, psz_broadcast );
803 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
805 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
806 if( p_input->i_handle == -1 )
808 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
809 p_input->b_error = 1;
813 /* We may want to reuse an already used socket */
815 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
816 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
818 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
820 close( p_input->i_handle );
821 p_input->b_error = 1;
825 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
826 * packet loss caused by scheduling problems */
828 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
829 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
831 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
833 close( p_input->i_handle );
834 p_input->b_error = 1;
838 /* Check if we really got what we have asked for, because Linux, etc.
839 * will silently limit the max buffer size to net.core.rmem_max which
840 * is typically only 65535 bytes */
842 i_opt_size = sizeof( i_opt );
843 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
844 (void*) &i_opt, &i_opt_size ) == -1 )
846 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
848 close( p_input->i_handle );
849 p_input->b_error = 1;
853 if( i_opt < 0x80000 )
855 intf_WarnMsg( 1, "input warning: socket receive buffer size just 0x%x"
856 " instead of 0x%x bytes.", i_opt, 0x80000 );
859 /* Build the local socket */
860 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
862 intf_ErrMsg( "input error: can't build local address" );
863 close( p_input->i_handle );
864 p_input->b_error = 1;
868 /* Required for IP_ADD_MEMBERSHIP */
869 i_mc_group = sock.sin_addr.s_addr;
872 if ( psz_broadcast != NULL )
874 sock.sin_addr.s_addr = INADDR_ANY;
877 #define IN_MULTICAST(a) IN_CLASSD(a)
881 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
882 sizeof( sock ) ) < 0 )
884 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
885 close( p_input->i_handle );
886 p_input->b_error = 1;
890 /* Join the multicast group if the socket is a multicast address */
893 if( IN_MULTICAST( ntohl(i_mc_group) ) )
897 imr.imr_interface.s_addr = htonl(INADDR_ANY);
898 imr.imr_multiaddr.s_addr = i_mc_group;
899 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
900 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
902 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
904 close( p_input->i_handle);
905 p_input->b_error = 1;
911 /* Build socket for remote connection */
912 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
914 intf_ErrMsg( "input error: can't build remote address" );
915 close( p_input->i_handle );
916 p_input->b_error = 1;
920 /* Only connect if the user has passed a valid host */
921 if( sock.sin_addr.s_addr != INADDR_ANY )
923 /* Connect the socket */
924 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
925 sizeof( sock ) ) == (-1) )
927 intf_ErrMsg( "input error: can't connect socket (%s)",
929 close( p_input->i_handle );
930 p_input->b_error = 1;
935 p_input->stream.b_pace_control = 0;
936 p_input->stream.b_seekable = 0;
938 intf_WarnMsg( 3, "input: successfully opened network mode" );
943 /*****************************************************************************
944 * NetworkClose : close a network socket
945 *****************************************************************************/
946 static void NetworkClose( input_thread_t * p_input )
948 close( p_input->i_handle );
955 /*****************************************************************************
956 * HTTPOpen : make an HTTP request
957 *****************************************************************************/
958 static void HTTPOpen( input_thread_t * p_input )
960 char *psz_server = NULL;
961 char *psz_path = NULL;
965 struct sockaddr_in sock;
966 char psz_buffer[256];
974 /* WinSock Library Init. */
975 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
979 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
984 /* Get the remote server */
985 if( p_input->p_source != NULL )
987 psz_server = p_input->p_source;
989 /* Skip the protocol name */
990 while( *psz_server && *psz_server != ':' )
995 /* Skip the "://" part */
996 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1001 /* Found a server name */
1004 char *psz_port = psz_server;
1006 /* Skip the hostname part */
1007 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1012 /* Found a port name */
1015 if( *psz_port == ':' )
1017 /* Replace ':' with '\0' */
1022 psz_path = psz_port;
1023 while( *psz_path && *psz_path != '/' )
1038 if( *psz_port != '\0' )
1040 i_port = atoi( psz_port );
1050 /* Check that we got a valid server */
1051 if( psz_server == NULL )
1053 intf_ErrMsg( "input error: No server given" );
1054 p_input->b_error = 1;
1058 /* Check that we got a valid port */
1061 i_port = 80; /* FIXME */
1064 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1067 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1069 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1070 if( p_input->i_handle == -1 )
1072 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1076 /* We may want to reuse an already used socket */
1078 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1079 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1081 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1083 close( p_input->i_handle );
1084 p_input->b_error = 1;
1089 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1091 /* http://myproxy.mydomain:myport/ */
1092 int i_proxy_port = 0;
1094 /* Skip the protocol name */
1095 while( *psz_proxy && *psz_proxy != ':' )
1100 /* Skip the "://" part */
1101 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1106 /* Found a proxy name */
1109 char *psz_port = psz_proxy;
1111 /* Skip the hostname part */
1112 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1117 /* Found a port name */
1122 /* Replace ':' with '\0' */
1126 psz_junk = psz_port;
1127 while( *psz_junk && *psz_junk != '/' )
1137 if( *psz_port != '\0' )
1139 i_proxy_port = atoi( psz_port );
1145 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1146 close( p_input->i_handle );
1147 p_input->b_error = 1;
1151 /* Build socket for proxy connection */
1152 if ( network_BuildRemoteAddr( &sock, psz_proxy ) == -1 )
1154 intf_ErrMsg( "input error: can't build remote address" );
1155 close( p_input->i_handle );
1156 p_input->b_error = 1;
1159 sock.sin_port = htons( i_proxy_port );
1163 /* No proxy, direct connection */
1164 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
1166 intf_ErrMsg( "input error: can't build remote address" );
1167 close( p_input->i_handle );
1168 p_input->b_error = 1;
1171 sock.sin_port = htons( i_port );
1174 /* Connect the socket */
1175 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1176 sizeof( sock ) ) == (-1) )
1178 intf_ErrMsg( "input error: can't connect socket (%s)",
1180 close( p_input->i_handle );
1181 p_input->b_error = 1;
1185 p_input->stream.b_seekable = 0;
1186 p_input->stream.b_pace_control = 1; /* TCP/IP... */
1188 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1189 # define HTTP_END "\r\n"
1191 /* Prepare GET ... */
1192 if( psz_proxy != NULL )
1194 snprintf( psz_buffer, sizeof(psz_buffer),
1195 "GET http://%s:%d/%s HTTP/1.0\r\n"
1196 HTTP_USERAGENT HTTP_END,
1197 psz_server, i_port, psz_path );
1201 snprintf( psz_buffer, sizeof(psz_buffer),
1202 "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1203 HTTP_USERAGENT HTTP_END,
1204 psz_path, psz_server );
1206 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1209 if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1211 intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1212 close( p_input->i_handle );
1213 p_input->b_error = 1;
1217 /* Read HTTP header - this is gonna be fun with plug-ins which do not
1218 * use p_input->p_stream :-( */
1219 if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1221 intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1222 close( p_input->i_handle );
1223 p_input->b_error = 1;
1227 while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1229 if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1230 || *psz_buffer == '\r' || *psz_buffer == '\0' )
1234 /* FIXME : check Content-Type one day */
1237 intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1240 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */