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.134 2001/10/02 17:04:43 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
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>
60 #include <sys/times.h>
70 #include "intf_playlist.h"
72 #include "stream_control.h"
73 #include "input_ext-intf.h"
74 #include "input_ext-dec.h"
75 #include "input_ext-plugins.h"
77 #include "interface.h"
81 /*****************************************************************************
83 *****************************************************************************/
84 static void RunThread ( input_thread_t *p_input );
85 static int InitThread ( input_thread_t *p_input );
86 static void ErrorThread ( input_thread_t *p_input );
87 static void DestroyThread ( input_thread_t *p_input );
88 static void EndThread ( input_thread_t *p_input );
90 static void FileOpen ( input_thread_t *p_input );
91 static void FileClose ( input_thread_t *p_input );
92 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
93 static void NetworkOpen ( input_thread_t *p_input );
94 static void HTTPOpen ( input_thread_t *p_input );
95 static void NetworkClose ( input_thread_t *p_input );
98 /*****************************************************************************
99 * input_CreateThread: creates a new input thread
100 *****************************************************************************
101 * This function creates a new input, and returns a pointer
102 * to its description. On error, it returns NULL.
103 * If pi_status is NULL, then the function will block until the thread is ready.
104 * If not, it will be updated using one of the THREAD_* constants.
105 *****************************************************************************/
106 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
108 input_thread_t * p_input; /* thread descriptor */
109 int i_status; /* thread status */
111 /* Allocate descriptor */
112 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
113 if( p_input == NULL )
115 intf_ErrMsg( "input error: can't allocate input thread (%s)",
120 /* Packets read once */
121 p_input->i_read_once = INPUT_READ_ONCE;
123 /* Initialize thread properties */
125 p_input->b_error = 0;
129 p_input->p_source = p_item->psz_name;
131 /* I have never understood that stuff --Meuuh */
132 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
133 *p_input->pi_status = THREAD_CREATE;
135 /* Initialize stream description */
136 p_input->stream.i_es_number = 0;
137 p_input->stream.i_selected_es_number = 0;
138 p_input->stream.i_pgrm_number = 0;
139 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
140 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
141 p_input->stream.i_mux_rate = 0;
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;
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];
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_grayscale = main_GetIntVariable(
158 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
159 p_input->stream.control.i_smp = main_GetIntVariable(
160 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
162 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
165 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
168 intf_ErrMsg( "input error: can't create input thread (%s)",
174 /* If status is NULL, wait until the thread is created */
175 if( pi_status == NULL )
179 msleep( THREAD_SLEEP );
180 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
181 && (i_status != THREAD_FATAL) );
182 if( i_status != THREAD_READY )
190 /*****************************************************************************
191 * input_DestroyThread: mark an input thread as zombie
192 *****************************************************************************
193 * This function should not return until the thread is effectively cancelled.
194 *****************************************************************************/
195 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
197 int i_status; /* thread status */
200 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
201 *p_input->pi_status = THREAD_DESTROY;
203 /* Request thread destruction */
206 /* Make the thread exit of an eventual vlc_cond_wait() */
207 vlc_mutex_lock( &p_input->stream.stream_lock );
208 vlc_cond_signal( &p_input->stream.stream_wait );
209 vlc_mutex_unlock( &p_input->stream.stream_lock );
211 /* If status is NULL, wait until thread has been destroyed */
212 if( pi_status == NULL )
216 msleep( THREAD_SLEEP );
217 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
218 && (i_status != THREAD_FATAL) );
222 /*****************************************************************************
223 * RunThread: main thread loop
224 *****************************************************************************
225 * Thread in charge of processing the network packets and demultiplexing.
226 *****************************************************************************/
227 static void RunThread( input_thread_t *p_input )
230 data_packet_t ** pp_packets;
232 if( InitThread( p_input ) )
234 /* If we failed, wait before we are killed, and exit */
235 *p_input->pi_status = THREAD_ERROR;
236 p_input->b_error = 1;
237 ErrorThread( p_input );
238 DestroyThread( p_input );
242 /* initialization is completed */
243 vlc_mutex_lock( &p_input->stream.stream_lock );
244 p_input->stream.b_changed = 1;
245 vlc_mutex_unlock( &p_input->stream.stream_lock );
247 pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
248 sizeof( data_packet_t * ) );
249 if( pp_packets == NULL )
251 intf_ErrMsg( "input error: out of memory" );
253 p_input->b_error = 1;
256 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
260 vlc_mutex_lock( &p_input->stream.stream_lock );
262 if( p_input->stream.p_new_area )
264 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
267 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
269 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
271 pgrm_descriptor_t * p_pgrm
272 = p_input->stream.pp_programs[i];
273 /* Escape all decoders for the stream discontinuity they
275 input_EscapeDiscontinuity( p_input, p_pgrm );
277 /* Reinitialize synchro. */
278 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
281 p_input->stream.p_new_area = NULL;
284 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
286 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
288 p_input->pf_seek( p_input,
289 p_input->stream.p_selected_area->i_seek );
291 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
293 pgrm_descriptor_t * p_pgrm
294 = p_input->stream.pp_programs[i];
295 /* Escape all decoders for the stream discontinuity they
297 input_EscapeDiscontinuity( p_input, p_pgrm );
299 /* Reinitialize synchro. */
300 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
303 p_input->stream.p_selected_area->i_seek = NO_SEEK;
306 if( p_input->stream.p_removed_es )
308 input_UnselectES( p_input, p_input->stream.p_removed_es );
309 p_input->stream.p_removed_es = NULL;
312 if( p_input->stream.p_newly_selected_es )
314 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
315 p_input->stream.p_newly_selected_es = NULL;
318 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
320 if( p_input->stream.b_new_mute )
322 input_EscapeAudioDiscontinuity( p_input );
325 vlc_mutex_lock( &p_input->stream.control.control_lock );
326 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
327 vlc_mutex_unlock( &p_input->stream.control.control_lock );
329 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
332 vlc_mutex_unlock( &p_input->stream.stream_lock );
334 i_error = p_input->pf_read( p_input, pp_packets );
336 /* Demultiplex read packets. */
337 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
339 p_input->stream.c_packets_read++;
340 p_input->pf_demux( p_input, pp_packets[i] );
347 /* End of file - we do not set b_die because only the
348 * interface is allowed to do so. */
349 intf_WarnMsg( 3, "input: EOF reached" );
354 p_input->b_error = 1;
361 if( p_input->b_error || p_input->b_eof )
363 ErrorThread( p_input );
366 EndThread( p_input );
368 DestroyThread( p_input );
370 intf_DbgMsg("input: Thread end");
373 /*****************************************************************************
374 * InitThread: init the input Thread
375 *****************************************************************************/
376 static int InitThread( input_thread_t * p_input )
379 /* Initialize statistics */
380 p_input->c_loops = 0;
381 p_input->stream.c_packets_read = 0;
382 p_input->stream.c_packets_trashed = 0;
383 p_input->p_stream = NULL;
386 vlc_mutex_init( &p_input->stream.stream_lock );
387 vlc_cond_init( &p_input->stream.stream_wait );
388 vlc_mutex_init( &p_input->stream.control.control_lock );
390 /* Find appropriate module. */
391 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
392 (probedata_t *)p_input );
394 if( p_input->p_input_module == NULL )
396 intf_ErrMsg( "input error: no suitable input module for `%s'",
401 #define f p_input->p_input_module->p_functions->input.functions.input
402 p_input->pf_init = f.pf_init;
403 p_input->pf_end = f.pf_end;
404 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
405 p_input->pf_read = f.pf_read;
406 p_input->pf_set_area = f.pf_set_area;
407 p_input->pf_demux = f.pf_demux;
408 p_input->pf_new_packet = f.pf_new_packet;
409 p_input->pf_new_pes = f.pf_new_pes;
410 p_input->pf_delete_packet = f.pf_delete_packet;
411 p_input->pf_delete_pes = f.pf_delete_pes;
412 p_input->pf_rewind = f.pf_rewind;
413 p_input->pf_seek = f.pf_seek;
416 /* FIXME : this is waaaay too kludgy */
417 if( (strlen( p_input->p_source ) > 3) && !strncasecmp( p_input->p_source, "ts:", 3 ) )
420 NetworkOpen( p_input );
421 p_input->stream.i_method = INPUT_METHOD_NETWORK;
423 else if( ( strlen( p_input->p_source ) > 5 ) && !strncasecmp( p_input->p_source, "http:", 5 ) )
427 p_input->stream.i_method = INPUT_METHOD_NETWORK;
429 else if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
431 /* DVD - this is THE kludge */
432 p_input->p_input_module->p_functions->input.functions.input.pf_open( p_input );
433 p_input->stream.i_method = INPUT_METHOD_DVD;
439 p_input->stream.i_method = INPUT_METHOD_FILE;
442 if( p_input->b_error )
444 /* We barfed -- exit nicely */
445 module_Unneed( p_input->p_input_module );
449 p_input->pf_init( p_input );
451 if( p_input->b_error )
453 /* We barfed -- exit nicely */
454 p_input->pf_close( p_input );
455 module_Unneed( p_input->p_input_module );
459 *p_input->pi_status = THREAD_READY;
464 /*****************************************************************************
465 * ErrorThread: RunThread() error loop
466 *****************************************************************************
467 * This function is called when an error occured during thread main's loop.
468 *****************************************************************************/
469 static void ErrorThread( input_thread_t *p_input )
471 while( !p_input->b_die )
474 msleep( INPUT_IDLE_SLEEP );
478 /*****************************************************************************
479 * EndThread: end the input thread
480 *****************************************************************************/
481 static void EndThread( input_thread_t * p_input )
483 int * pi_status; /* thread status */
486 pi_status = p_input->pi_status;
487 *pi_status = THREAD_END;
489 if( p_main->b_stats )
491 /* Display statistics */
492 struct tms cpu_usage;
495 intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
497 cpu_usage.tms_utime, cpu_usage.tms_stime );
499 input_DumpStream( p_input );
502 /* Free all ES and destroy all decoder threads */
503 input_EndStream( p_input );
505 /* Free demultiplexer's data */
506 p_input->pf_end( p_input );
509 if( (strlen( p_input->p_source ) > 3) && !strncasecmp( p_input->p_source, "ts:", 3 ) )
511 NetworkClose( p_input );
513 else if( ( strlen( p_input->p_source ) > 5 ) && !strncasecmp( p_input->p_source, "http:", 5 ) )
515 NetworkClose( p_input );
517 else if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
519 p_input->p_input_module->p_functions->input.functions.input.pf_close( p_input );
523 FileClose( p_input );
526 /* Release modules */
527 module_Unneed( p_input->p_input_module );
531 /*****************************************************************************
532 * DestroyThread: destroy the input thread
533 *****************************************************************************/
534 static void DestroyThread( input_thread_t * p_input )
536 int * pi_status; /* thread status */
539 pi_status = p_input->pi_status;
541 /* Destroy Mutex locks */
542 vlc_mutex_destroy( &p_input->stream.control.control_lock );
543 vlc_mutex_destroy( &p_input->stream.stream_lock );
545 /* Free input structure */
549 *pi_status = THREAD_OVER;
552 /*****************************************************************************
553 * FileOpen : open a file descriptor
554 *****************************************************************************/
555 static void FileOpen( input_thread_t * p_input )
557 struct stat stat_info;
560 char *psz_name = p_input->p_source;
562 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
564 int i_size = strlen( psz_name );
567 && !strncasecmp( psz_name, "dvd:", 4 ) )
569 /* get rid of the 'dvd:' stuff and try again */
571 i_stat = stat( psz_name, &stat_info );
573 else if( ( i_size > 5 )
574 && !strncasecmp( psz_name, "file:", 5 ) )
576 /* get rid of the 'file:' stuff and try again */
578 i_stat = stat( psz_name, &stat_info );
583 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
584 psz_name, strerror(errno));
585 p_input->b_error = 1;
590 vlc_mutex_lock( &p_input->stream.stream_lock );
592 /* If we are here we can control the pace... */
593 p_input->stream.b_pace_control = 1;
595 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
596 || S_ISBLK(stat_info.st_mode) )
598 p_input->stream.b_seekable = 1;
599 p_input->stream.p_selected_area->i_size = stat_info.st_size;
601 else if( S_ISFIFO(stat_info.st_mode)
602 #if !defined( SYS_BEOS ) && !defined( WIN32 )
603 || S_ISSOCK(stat_info.st_mode)
607 p_input->stream.b_seekable = 0;
608 p_input->stream.p_selected_area->i_size = 0;
612 vlc_mutex_unlock( &p_input->stream.stream_lock );
613 intf_ErrMsg( "input error: unknown file type for `%s'",
615 p_input->b_error = 1;
619 p_input->stream.p_selected_area->i_tell = 0;
620 vlc_mutex_unlock( &p_input->stream.stream_lock );
622 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
623 if( (p_input->i_handle = open( psz_name,
624 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
626 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
627 p_input->b_error = 1;
633 /*****************************************************************************
634 * FileClose : close a file descriptor
635 *****************************************************************************/
636 static void FileClose( input_thread_t * p_input )
638 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
640 close( p_input->i_handle );
645 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
646 /*****************************************************************************
647 * NetworkOpen : open a network socket
648 *****************************************************************************/
649 static void NetworkOpen( input_thread_t * p_input )
651 char *psz_server = NULL;
652 char *psz_broadcast = NULL;
656 struct sockaddr_in sock;
657 unsigned int i_mc_group;
665 /* WinSock Library Init. */
666 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
670 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
675 /* Get the remote server */
676 if( p_input->p_source != NULL )
678 psz_server = p_input->p_source;
680 /* Skip the protocol name */
681 while( *psz_server && *psz_server != ':' )
686 /* Skip the "://" part */
687 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
692 /* Found a server name */
695 char *psz_port = psz_server;
697 /* Skip the hostname part */
698 while( *psz_port && *psz_port != ':' )
703 /* Found a port name */
706 /* Replace ':' with '\0' */
710 psz_broadcast = psz_port;
711 while( *psz_broadcast && *psz_broadcast != ':' )
718 *psz_broadcast = '\0';
720 while( *psz_broadcast && *psz_broadcast == ':' )
727 psz_broadcast = NULL;
730 /* port before broadcast address */
731 if( *psz_port != '\0' )
733 i_port = atoi( psz_port );
743 /* Check that we got a valid server */
744 if( psz_server == NULL )
746 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
747 INPUT_SERVER_DEFAULT );
750 /* Check that we got a valid port */
753 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
756 if( psz_broadcast == NULL )
758 /* Are we broadcasting ? */
759 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
760 INPUT_BROADCAST_DEFAULT ) )
762 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
763 INPUT_BCAST_ADDR_DEFAULT );
767 psz_broadcast = NULL;
771 intf_WarnMsg( 2, "input: server=%s port=%d broadcast=%s",
772 psz_server, i_port, psz_broadcast );
774 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
776 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
777 if( p_input->i_handle == -1 )
779 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
780 p_input->b_error = 1;
784 /* We may want to reuse an already used socket */
786 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
787 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
789 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
791 close( p_input->i_handle );
792 p_input->b_error = 1;
796 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
797 * packet loss caused by scheduling problems */
799 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
800 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
802 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
804 close( p_input->i_handle );
805 p_input->b_error = 1;
809 /* Check if we really got what we have asked for, because Linux, etc.
810 * will silently limit the max buffer size to net.core.rmem_max which
811 * is typically only 65535 bytes */
813 i_opt_size = sizeof( i_opt );
814 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
815 (void*) &i_opt, &i_opt_size ) == -1 )
817 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
819 close( p_input->i_handle );
820 p_input->b_error = 1;
824 if( i_opt < 0x80000 )
826 intf_WarnMsg( 1, "input warning: socket receive buffer size just %d instead of %d bytes.",
830 /* Build the local socket */
831 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
833 intf_ErrMsg( "input error: can't build local address" );
834 close( p_input->i_handle );
835 p_input->b_error = 1;
839 /* Required for IP_ADD_MEMBERSHIP */
840 i_mc_group = sock.sin_addr.s_addr;
843 if ( psz_broadcast != NULL )
845 sock.sin_addr.s_addr = INADDR_ANY;
847 #define IN_MULTICAST(a) IN_CLASSD(a)
851 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
852 sizeof( sock ) ) < 0 )
854 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
855 close( p_input->i_handle );
856 p_input->b_error = 1;
860 /* Join the multicast group if the socket is a multicast address */
861 if( IN_MULTICAST( ntohl(i_mc_group) ) )
865 imr.imr_interface.s_addr = htonl(INADDR_ANY);
866 imr.imr_multiaddr.s_addr = i_mc_group;
867 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
868 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
870 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
872 close( p_input->i_handle);
873 p_input->b_error = 1;
878 /* Build socket for remote connection */
879 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
881 intf_ErrMsg( "input error: can't build remote address" );
882 close( p_input->i_handle );
883 p_input->b_error = 1;
887 /* Only connect if the user has passed a valid host */
888 if( sock.sin_addr.s_addr != INADDR_ANY )
890 /* Connect the socket */
891 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
892 sizeof( sock ) ) == (-1) )
894 intf_ErrMsg( "input error: can't connect socket (%s)",
896 close( p_input->i_handle );
897 p_input->b_error = 1;
902 p_input->stream.b_pace_control = 0;
903 p_input->stream.b_seekable = 0;
905 intf_WarnMsg( 3, "input: successfully opened network mode" );
910 /*****************************************************************************
911 * NetworkClose : close a network socket
912 *****************************************************************************/
913 static void NetworkClose( input_thread_t * p_input )
915 close( p_input->i_handle );
922 /*****************************************************************************
923 * HTTPOpen : make an HTTP request
924 *****************************************************************************/
925 static void HTTPOpen( input_thread_t * p_input )
927 char *psz_server = NULL;
928 char *psz_path = NULL;
932 struct sockaddr_in sock;
933 char psz_buffer[256];
941 /* WinSock Library Init. */
942 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
946 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
951 /* Get the remote server */
952 if( p_input->p_source != NULL )
954 psz_server = p_input->p_source;
956 /* Skip the protocol name */
957 while( *psz_server && *psz_server != ':' )
962 /* Skip the "://" part */
963 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
968 /* Found a server name */
971 char *psz_port = psz_server;
973 /* Skip the hostname part */
974 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
979 /* Found a port name */
982 if( *psz_port == ':' )
984 /* Replace ':' with '\0' */
990 while( *psz_path && *psz_path != '/' )
1005 if( *psz_port != '\0' )
1007 i_port = atoi( psz_port );
1017 /* Check that we got a valid server */
1018 if( psz_server == NULL )
1020 intf_ErrMsg( "input error: No server given" );
1021 p_input->b_error = 1;
1025 /* Check that we got a valid port */
1028 i_port = 80; /* FIXME */
1031 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1034 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1036 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1037 if( p_input->i_handle == -1 )
1039 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1043 /* We may want to reuse an already used socket */
1045 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1046 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1048 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1050 close( p_input->i_handle );
1051 p_input->b_error = 1;
1056 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1058 /* http://myproxy.mydomain:myport/ */
1059 int i_proxy_port = 0;
1061 /* Skip the protocol name */
1062 while( *psz_proxy && *psz_proxy != ':' )
1067 /* Skip the "://" part */
1068 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1073 /* Found a proxy name */
1076 char *psz_port = psz_proxy;
1078 /* Skip the hostname part */
1079 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1084 /* Found a port name */
1089 /* Replace ':' with '\0' */
1093 psz_junk = psz_port;
1094 while( *psz_junk && *psz_junk != '/' )
1104 if( *psz_port != '\0' )
1106 i_proxy_port = atoi( psz_port );
1112 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1113 close( p_input->i_handle );
1114 p_input->b_error = 1;
1118 /* Build socket for proxy connection */
1119 if ( network_BuildRemoteAddr( &sock, psz_proxy ) == -1 )
1121 intf_ErrMsg( "input error: can't build remote address" );
1122 close( p_input->i_handle );
1123 p_input->b_error = 1;
1126 sock.sin_port = htons( i_proxy_port );
1130 /* No proxy, direct connection */
1131 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
1133 intf_ErrMsg( "input error: can't build remote address" );
1134 close( p_input->i_handle );
1135 p_input->b_error = 1;
1138 sock.sin_port = htons( i_port );
1141 /* Connect the socket */
1142 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1143 sizeof( sock ) ) == (-1) )
1145 intf_ErrMsg( "input error: can't connect socket (%s)",
1147 close( p_input->i_handle );
1148 p_input->b_error = 1;
1152 p_input->stream.b_seekable = 0;
1153 p_input->stream.b_pace_control = 1; /* TCP/IP... */
1155 /* Prepare GET ... */
1156 if( psz_proxy != NULL )
1158 snprintf( psz_buffer, sizeof(psz_buffer),
1159 "GET http://%s:%d/%s HTTP/1.0\r\n\r\n", psz_server,
1164 snprintf( psz_buffer, sizeof(psz_buffer), "GET /%s HTTP/1.0\r\n\r\n",
1167 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1170 if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1172 intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1173 close( p_input->i_handle );
1174 p_input->b_error = 1;
1178 /* Read HTTP header - this is gonna be fun with plug-ins which do not
1179 * use p_input->p_stream :-( */
1180 if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1182 intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1183 close( p_input->i_handle );
1184 p_input->b_error = 1;
1188 while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1190 if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1191 || *psz_buffer == '\r' || *psz_buffer == '\0' )
1195 /* FIXME : check Content-Type one day */
1198 intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1201 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */