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.137 2001/10/03 03:32:05 xav 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>
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;
445 p_input->stream.i_method = INPUT_METHOD_FILE;
448 if( p_input->b_error )
450 /* We barfed -- exit nicely */
451 module_Unneed( p_input->p_input_module );
455 p_input->pf_init( p_input );
457 if( p_input->b_error )
459 /* We barfed -- exit nicely */
460 p_input->pf_close( p_input );
461 module_Unneed( p_input->p_input_module );
465 *p_input->pi_status = THREAD_READY;
470 /*****************************************************************************
471 * ErrorThread: RunThread() error loop
472 *****************************************************************************
473 * This function is called when an error occured during thread main's loop.
474 *****************************************************************************/
475 static void ErrorThread( input_thread_t *p_input )
477 while( !p_input->b_die )
480 msleep( INPUT_IDLE_SLEEP );
484 /*****************************************************************************
485 * EndThread: end the input thread
486 *****************************************************************************/
487 static void EndThread( input_thread_t * p_input )
489 int * pi_status; /* thread status */
492 pi_status = p_input->pi_status;
493 *pi_status = THREAD_END;
495 if( p_main->b_stats )
497 /* Display statistics */
499 struct tms cpu_usage;
502 intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
504 cpu_usage.tms_utime, cpu_usage.tms_stime );
506 input_DumpStream( p_input );
510 /* Free all ES and destroy all decoder threads */
511 input_EndStream( p_input );
513 /* Free demultiplexer's data */
514 p_input->pf_end( p_input );
516 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
518 if( (strlen( p_input->p_source ) > 3) && !strncasecmp( p_input->p_source, "ts:", 3 ) )
520 NetworkClose( p_input );
522 else if( ( strlen( p_input->p_source ) > 5 ) && !strncasecmp( p_input->p_source, "http:", 5 ) )
524 NetworkClose( p_input );
528 if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
530 p_input->p_input_module->p_functions->input.functions.input.pf_close( p_input );
534 FileClose( p_input );
537 /* Release modules */
538 module_Unneed( p_input->p_input_module );
542 /*****************************************************************************
543 * DestroyThread: destroy the input thread
544 *****************************************************************************/
545 static void DestroyThread( input_thread_t * p_input )
547 int * pi_status; /* thread status */
550 pi_status = p_input->pi_status;
552 /* Destroy Mutex locks */
553 vlc_mutex_destroy( &p_input->stream.control.control_lock );
554 vlc_mutex_destroy( &p_input->stream.stream_lock );
556 /* Free input structure */
560 *pi_status = THREAD_OVER;
563 /*****************************************************************************
564 * FileOpen : open a file descriptor
565 *****************************************************************************/
566 static void FileOpen( input_thread_t * p_input )
568 struct stat stat_info;
571 char *psz_name = p_input->p_source;
573 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
575 int i_size = strlen( psz_name );
578 && !strncasecmp( psz_name, "dvd:", 4 ) )
580 /* get rid of the 'dvd:' stuff and try again */
582 i_stat = stat( psz_name, &stat_info );
584 else if( ( i_size > 5 )
585 && !strncasecmp( psz_name, "file:", 5 ) )
587 /* get rid of the 'file:' stuff and try again */
589 i_stat = stat( psz_name, &stat_info );
594 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
595 psz_name, strerror(errno));
596 p_input->b_error = 1;
601 vlc_mutex_lock( &p_input->stream.stream_lock );
603 /* If we are here we can control the pace... */
604 p_input->stream.b_pace_control = 1;
606 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
607 || S_ISBLK(stat_info.st_mode) )
609 p_input->stream.b_seekable = 1;
610 p_input->stream.p_selected_area->i_size = stat_info.st_size;
612 else if( S_ISFIFO(stat_info.st_mode)
613 #if !defined( SYS_BEOS ) && !defined( WIN32 )
614 || S_ISSOCK(stat_info.st_mode)
618 p_input->stream.b_seekable = 0;
619 p_input->stream.p_selected_area->i_size = 0;
623 vlc_mutex_unlock( &p_input->stream.stream_lock );
624 intf_ErrMsg( "input error: unknown file type for `%s'",
626 p_input->b_error = 1;
630 p_input->stream.p_selected_area->i_tell = 0;
631 vlc_mutex_unlock( &p_input->stream.stream_lock );
633 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
634 if( (p_input->i_handle = open( psz_name,
635 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
637 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
638 p_input->b_error = 1;
644 /*****************************************************************************
645 * FileClose : close a file descriptor
646 *****************************************************************************/
647 static void FileClose( input_thread_t * p_input )
649 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
651 close( p_input->i_handle );
656 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
657 /*****************************************************************************
658 * NetworkOpen : open a network socket
659 *****************************************************************************/
660 static void NetworkOpen( input_thread_t * p_input )
662 char *psz_server = NULL;
663 char *psz_broadcast = NULL;
667 struct sockaddr_in sock;
668 unsigned int i_mc_group;
676 /* WinSock Library Init. */
677 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
681 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
686 /* Get the remote server */
687 if( p_input->p_source != NULL )
689 psz_server = p_input->p_source;
691 /* Skip the protocol name */
692 while( *psz_server && *psz_server != ':' )
697 /* Skip the "://" part */
698 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
703 /* Found a server name */
706 char *psz_port = psz_server;
708 /* Skip the hostname part */
709 while( *psz_port && *psz_port != ':' )
714 /* Found a port name */
717 /* Replace ':' with '\0' */
721 psz_broadcast = psz_port;
722 while( *psz_broadcast && *psz_broadcast != ':' )
729 *psz_broadcast = '\0';
731 while( *psz_broadcast && *psz_broadcast == ':' )
738 psz_broadcast = NULL;
741 /* port before broadcast address */
742 if( *psz_port != '\0' )
744 i_port = atoi( psz_port );
754 /* Check that we got a valid server */
755 if( psz_server == NULL )
757 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
758 INPUT_SERVER_DEFAULT );
761 /* Check that we got a valid port */
764 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
767 if( psz_broadcast == NULL )
769 /* Are we broadcasting ? */
770 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
771 INPUT_BROADCAST_DEFAULT ) )
773 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
774 INPUT_BCAST_ADDR_DEFAULT );
778 psz_broadcast = NULL;
782 intf_WarnMsg( 2, "input: server=%s port=%d broadcast=%s",
783 psz_server, i_port, psz_broadcast );
785 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
787 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
788 if( p_input->i_handle == -1 )
790 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
791 p_input->b_error = 1;
795 /* We may want to reuse an already used socket */
797 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
798 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
800 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
802 close( p_input->i_handle );
803 p_input->b_error = 1;
807 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
808 * packet loss caused by scheduling problems */
810 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
811 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
813 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
815 close( p_input->i_handle );
816 p_input->b_error = 1;
820 /* Check if we really got what we have asked for, because Linux, etc.
821 * will silently limit the max buffer size to net.core.rmem_max which
822 * is typically only 65535 bytes */
824 i_opt_size = sizeof( i_opt );
825 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
826 (void*) &i_opt, &i_opt_size ) == -1 )
828 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
830 close( p_input->i_handle );
831 p_input->b_error = 1;
835 if( i_opt < 0x80000 )
837 intf_WarnMsg( 1, "input warning: socket receive buffer size just %d instead of %d bytes.",
841 /* Build the local socket */
842 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
844 intf_ErrMsg( "input error: can't build local address" );
845 close( p_input->i_handle );
846 p_input->b_error = 1;
850 /* Required for IP_ADD_MEMBERSHIP */
851 i_mc_group = sock.sin_addr.s_addr;
854 if ( psz_broadcast != NULL )
856 sock.sin_addr.s_addr = INADDR_ANY;
858 #define IN_MULTICAST(a) IN_CLASSD(a)
862 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
863 sizeof( sock ) ) < 0 )
865 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
866 close( p_input->i_handle );
867 p_input->b_error = 1;
871 /* Join the multicast group if the socket is a multicast address */
872 if( IN_MULTICAST( ntohl(i_mc_group) ) )
876 imr.imr_interface.s_addr = htonl(INADDR_ANY);
877 imr.imr_multiaddr.s_addr = i_mc_group;
878 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
879 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
881 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
883 close( p_input->i_handle);
884 p_input->b_error = 1;
889 /* Build socket for remote connection */
890 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
892 intf_ErrMsg( "input error: can't build remote address" );
893 close( p_input->i_handle );
894 p_input->b_error = 1;
898 /* Only connect if the user has passed a valid host */
899 if( sock.sin_addr.s_addr != INADDR_ANY )
901 /* Connect the socket */
902 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
903 sizeof( sock ) ) == (-1) )
905 intf_ErrMsg( "input error: can't connect socket (%s)",
907 close( p_input->i_handle );
908 p_input->b_error = 1;
913 p_input->stream.b_pace_control = 0;
914 p_input->stream.b_seekable = 0;
916 intf_WarnMsg( 3, "input: successfully opened network mode" );
921 /*****************************************************************************
922 * NetworkClose : close a network socket
923 *****************************************************************************/
924 static void NetworkClose( input_thread_t * p_input )
926 close( p_input->i_handle );
933 /*****************************************************************************
934 * HTTPOpen : make an HTTP request
935 *****************************************************************************/
936 static void HTTPOpen( input_thread_t * p_input )
938 char *psz_server = NULL;
939 char *psz_path = NULL;
943 struct sockaddr_in sock;
944 char psz_buffer[256];
952 /* WinSock Library Init. */
953 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
957 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
962 /* Get the remote server */
963 if( p_input->p_source != NULL )
965 psz_server = p_input->p_source;
967 /* Skip the protocol name */
968 while( *psz_server && *psz_server != ':' )
973 /* Skip the "://" part */
974 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
979 /* Found a server name */
982 char *psz_port = psz_server;
984 /* Skip the hostname part */
985 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
990 /* Found a port name */
993 if( *psz_port == ':' )
995 /* Replace ':' with '\0' */
1000 psz_path = psz_port;
1001 while( *psz_path && *psz_path != '/' )
1016 if( *psz_port != '\0' )
1018 i_port = atoi( psz_port );
1028 /* Check that we got a valid server */
1029 if( psz_server == NULL )
1031 intf_ErrMsg( "input error: No server given" );
1032 p_input->b_error = 1;
1036 /* Check that we got a valid port */
1039 i_port = 80; /* FIXME */
1042 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1045 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1047 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1048 if( p_input->i_handle == -1 )
1050 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1054 /* We may want to reuse an already used socket */
1056 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1057 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1059 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1061 close( p_input->i_handle );
1062 p_input->b_error = 1;
1067 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1069 /* http://myproxy.mydomain:myport/ */
1070 int i_proxy_port = 0;
1072 /* Skip the protocol name */
1073 while( *psz_proxy && *psz_proxy != ':' )
1078 /* Skip the "://" part */
1079 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1084 /* Found a proxy name */
1087 char *psz_port = psz_proxy;
1089 /* Skip the hostname part */
1090 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1095 /* Found a port name */
1100 /* Replace ':' with '\0' */
1104 psz_junk = psz_port;
1105 while( *psz_junk && *psz_junk != '/' )
1115 if( *psz_port != '\0' )
1117 i_proxy_port = atoi( psz_port );
1123 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1124 close( p_input->i_handle );
1125 p_input->b_error = 1;
1129 /* Build socket for proxy connection */
1130 if ( network_BuildRemoteAddr( &sock, psz_proxy ) == -1 )
1132 intf_ErrMsg( "input error: can't build remote address" );
1133 close( p_input->i_handle );
1134 p_input->b_error = 1;
1137 sock.sin_port = htons( i_proxy_port );
1141 /* No proxy, direct connection */
1142 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
1144 intf_ErrMsg( "input error: can't build remote address" );
1145 close( p_input->i_handle );
1146 p_input->b_error = 1;
1149 sock.sin_port = htons( i_port );
1152 /* Connect the socket */
1153 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1154 sizeof( sock ) ) == (-1) )
1156 intf_ErrMsg( "input error: can't connect socket (%s)",
1158 close( p_input->i_handle );
1159 p_input->b_error = 1;
1163 p_input->stream.b_seekable = 0;
1164 p_input->stream.b_pace_control = 1; /* TCP/IP... */
1166 /* Prepare GET ... */
1167 if( psz_proxy != NULL )
1169 snprintf( psz_buffer, sizeof(psz_buffer),
1170 "GET http://%s:%d/%s HTTP/1.0\r\n\r\n", psz_server,
1175 snprintf( psz_buffer, sizeof(psz_buffer),
1176 "GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n",
1177 psz_path, psz_server );
1179 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1182 if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1184 intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1185 close( p_input->i_handle );
1186 p_input->b_error = 1;
1190 /* Read HTTP header - this is gonna be fun with plug-ins which do not
1191 * use p_input->p_stream :-( */
1192 if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1194 intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1195 close( p_input->i_handle );
1196 p_input->b_error = 1;
1200 while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1202 if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1203 || *psz_buffer == '\r' || *psz_buffer == '\0' )
1207 /* FIXME : check Content-Type one day */
1210 intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1213 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */