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.143 2001/10/15 14:59:56 sam 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 #ifdef HAVE_SYS_TIMES_H
61 # include <sys/times.h>
72 #include "intf_playlist.h"
74 #include "stream_control.h"
75 #include "input_ext-intf.h"
76 #include "input_ext-dec.h"
77 #include "input_ext-plugins.h"
79 #include "interface.h"
83 /*****************************************************************************
85 *****************************************************************************/
86 static void RunThread ( input_thread_t *p_input );
87 static int InitThread ( input_thread_t *p_input );
88 static void ErrorThread ( input_thread_t *p_input );
89 static void DestroyThread ( input_thread_t *p_input );
90 static void EndThread ( input_thread_t *p_input );
92 static void FileOpen ( input_thread_t *p_input );
93 static void FileClose ( input_thread_t *p_input );
94 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
95 static void NetworkOpen ( input_thread_t *p_input );
96 static void HTTPOpen ( input_thread_t *p_input );
97 static void NetworkClose ( input_thread_t *p_input );
100 /*****************************************************************************
101 * input_CreateThread: creates a new input thread
102 *****************************************************************************
103 * This function creates a new input, and returns a pointer
104 * to its description. On error, it returns NULL.
105 * If pi_status is NULL, then the function will block until the thread is ready.
106 * If not, it will be updated using one of the THREAD_* constants.
107 *****************************************************************************/
108 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
110 input_thread_t * p_input; /* thread descriptor */
111 int i_status; /* thread status */
113 /* Allocate descriptor */
114 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
115 if( p_input == NULL )
117 intf_ErrMsg( "input error: can't allocate input thread (%s)",
122 /* Packets read once */
123 p_input->i_read_once = INPUT_READ_ONCE;
125 /* Initialize thread properties */
127 p_input->b_error = 0;
131 p_input->p_source = p_item->psz_name;
133 /* I have never understood that stuff --Meuuh */
134 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
135 *p_input->pi_status = THREAD_CREATE;
137 /* Initialize stream description */
138 p_input->stream.i_es_number = 0;
139 p_input->stream.i_selected_es_number = 0;
140 p_input->stream.i_pgrm_number = 0;
141 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
142 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
143 p_input->stream.i_mux_rate = 0;
145 /* no stream, no area */
146 p_input->stream.i_area_nb = 0;
147 p_input->stream.pp_areas = NULL;
148 p_input->stream.p_selected_area = NULL;
149 p_input->stream.p_new_area = NULL;
151 /* By default there is one area in a stream */
152 input_AddArea( p_input );
153 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
155 /* Initialize stream control properties. */
156 p_input->stream.control.i_status = PLAYING_S;
157 p_input->stream.control.i_rate = DEFAULT_RATE;
158 p_input->stream.control.b_mute = 0;
159 p_input->stream.control.b_grayscale = main_GetIntVariable(
160 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
161 p_input->stream.control.i_smp = main_GetIntVariable(
162 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
164 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
167 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
170 intf_ErrMsg( "input error: can't create input thread (%s)",
176 /* If status is NULL, wait until the thread is created */
177 if( pi_status == NULL )
181 msleep( THREAD_SLEEP );
182 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
183 && (i_status != THREAD_FATAL) );
184 if( i_status != THREAD_READY )
192 /*****************************************************************************
193 * input_DestroyThread: mark an input thread as zombie
194 *****************************************************************************
195 * This function should not return until the thread is effectively cancelled.
196 *****************************************************************************/
197 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
199 int i_status; /* thread status */
202 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
203 *p_input->pi_status = THREAD_DESTROY;
205 /* Request thread destruction */
208 /* Make the thread exit of an eventual vlc_cond_wait() */
209 vlc_mutex_lock( &p_input->stream.stream_lock );
210 vlc_cond_signal( &p_input->stream.stream_wait );
211 vlc_mutex_unlock( &p_input->stream.stream_lock );
213 /* If status is NULL, wait until thread has been destroyed */
214 if( pi_status == NULL )
218 msleep( THREAD_SLEEP );
219 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
220 && (i_status != THREAD_FATAL) );
224 /*****************************************************************************
225 * RunThread: main thread loop
226 *****************************************************************************
227 * Thread in charge of processing the network packets and demultiplexing.
228 *****************************************************************************/
229 static void RunThread( input_thread_t *p_input )
232 data_packet_t ** pp_packets;
234 if( InitThread( p_input ) )
236 /* If we failed, wait before we are killed, and exit */
237 *p_input->pi_status = THREAD_ERROR;
238 p_input->b_error = 1;
239 ErrorThread( p_input );
240 DestroyThread( p_input );
244 /* initialization is completed */
245 vlc_mutex_lock( &p_input->stream.stream_lock );
246 p_input->stream.b_changed = 1;
247 vlc_mutex_unlock( &p_input->stream.stream_lock );
249 pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
250 sizeof( data_packet_t * ) );
251 if( pp_packets == NULL )
253 intf_ErrMsg( "input error: out of memory" );
255 p_input->b_error = 1;
258 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
262 vlc_mutex_lock( &p_input->stream.stream_lock );
264 if( p_input->stream.p_new_area )
266 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
269 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
271 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
273 pgrm_descriptor_t * p_pgrm
274 = p_input->stream.pp_programs[i];
275 /* Escape all decoders for the stream discontinuity they
277 input_EscapeDiscontinuity( p_input, p_pgrm );
279 /* Reinitialize synchro. */
280 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
283 p_input->stream.p_new_area = NULL;
286 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
288 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
290 p_input->pf_seek( p_input,
291 p_input->stream.p_selected_area->i_seek );
293 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
295 pgrm_descriptor_t * p_pgrm
296 = p_input->stream.pp_programs[i];
297 /* Escape all decoders for the stream discontinuity they
299 input_EscapeDiscontinuity( p_input, p_pgrm );
301 /* Reinitialize synchro. */
302 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
305 p_input->stream.p_selected_area->i_seek = NO_SEEK;
308 if( p_input->stream.p_removed_es )
310 input_UnselectES( p_input, p_input->stream.p_removed_es );
311 p_input->stream.p_removed_es = NULL;
314 if( p_input->stream.p_newly_selected_es )
316 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
317 p_input->stream.p_newly_selected_es = NULL;
320 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
322 if( p_input->stream.b_new_mute )
324 input_EscapeAudioDiscontinuity( p_input );
327 vlc_mutex_lock( &p_input->stream.control.control_lock );
328 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
329 vlc_mutex_unlock( &p_input->stream.control.control_lock );
331 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
334 vlc_mutex_unlock( &p_input->stream.stream_lock );
336 i_error = p_input->pf_read( p_input, pp_packets );
338 /* Demultiplex read packets. */
339 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
341 p_input->stream.c_packets_read++;
342 p_input->pf_demux( p_input, pp_packets[i] );
349 /* End of file - we do not set b_die because only the
350 * interface is allowed to do so. */
351 intf_WarnMsg( 3, "input: EOF reached" );
356 p_input->b_error = 1;
363 if( p_input->b_error || p_input->b_eof )
365 ErrorThread( p_input );
368 EndThread( p_input );
370 DestroyThread( p_input );
372 intf_DbgMsg("input: Thread end");
375 /*****************************************************************************
376 * InitThread: init the input Thread
377 *****************************************************************************/
378 static int InitThread( input_thread_t * p_input )
381 /* Initialize statistics */
382 p_input->c_loops = 0;
383 p_input->stream.c_packets_read = 0;
384 p_input->stream.c_packets_trashed = 0;
385 p_input->p_stream = NULL;
388 vlc_mutex_init( &p_input->stream.stream_lock );
389 vlc_cond_init( &p_input->stream.stream_wait );
390 vlc_mutex_init( &p_input->stream.control.control_lock );
392 /* Find appropriate module. */
393 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
394 (probedata_t *)p_input );
396 if( p_input->p_input_module == NULL )
398 intf_ErrMsg( "input error: no suitable input module for `%s'",
403 #define f p_input->p_input_module->p_functions->input.functions.input
404 p_input->pf_init = f.pf_init;
405 p_input->pf_end = f.pf_end;
406 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
407 p_input->pf_read = f.pf_read;
408 p_input->pf_set_area = f.pf_set_area;
409 p_input->pf_demux = f.pf_demux;
410 p_input->pf_new_packet = f.pf_new_packet;
411 p_input->pf_new_pes = f.pf_new_pes;
412 p_input->pf_delete_packet = f.pf_delete_packet;
413 p_input->pf_delete_pes = f.pf_delete_pes;
414 p_input->pf_rewind = f.pf_rewind;
415 p_input->pf_seek = f.pf_seek;
418 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
419 /* FIXME : this is waaaay too kludgy */
420 if( (strlen( p_input->p_source ) > 3) && !strncasecmp( p_input->p_source, "ts:", 3 ) )
423 NetworkOpen( p_input );
424 p_input->stream.i_method = INPUT_METHOD_NETWORK;
426 else if( ( strlen( p_input->p_source ) > 5 ) && !strncasecmp( p_input->p_source, "http:", 5 ) )
430 p_input->stream.i_method = INPUT_METHOD_NETWORK;
434 if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
436 /* DVD - this is THE kludge */
437 p_input->p_input_module->p_functions->input.functions.input.pf_open( p_input );
438 p_input->stream.i_method = INPUT_METHOD_DVD;
440 else if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
442 /* Dummy input - very kludgy */
443 p_input->p_input_module->p_functions->input.functions.input.pf_open( p_input );
449 p_input->stream.i_method = INPUT_METHOD_FILE;
452 if( p_input->b_error )
454 /* We barfed -- exit nicely */
455 module_Unneed( p_input->p_input_module );
459 p_input->pf_init( p_input );
461 if( p_input->b_error )
463 /* We barfed -- exit nicely */
464 p_input->pf_close( p_input );
465 module_Unneed( p_input->p_input_module );
469 *p_input->pi_status = THREAD_READY;
474 /*****************************************************************************
475 * ErrorThread: RunThread() error loop
476 *****************************************************************************
477 * This function is called when an error occured during thread main's loop.
478 *****************************************************************************/
479 static void ErrorThread( input_thread_t *p_input )
481 while( !p_input->b_die )
484 msleep( INPUT_IDLE_SLEEP );
488 /*****************************************************************************
489 * EndThread: end the input thread
490 *****************************************************************************/
491 static void EndThread( input_thread_t * p_input )
493 int * pi_status; /* thread status */
496 pi_status = p_input->pi_status;
497 *pi_status = THREAD_END;
499 if( p_main->b_stats )
501 #ifdef HAVE_SYS_TIMES_H
502 /* Display statistics */
503 struct tms cpu_usage;
506 intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
508 cpu_usage.tms_utime, cpu_usage.tms_stime );
510 intf_StatMsg( "input stats: %d loops", p_input->c_loops );
513 input_DumpStream( p_input );
516 /* Free all ES and destroy all decoder threads */
517 input_EndStream( p_input );
519 /* Free demultiplexer's data */
520 p_input->pf_end( p_input );
522 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
524 if( (strlen( p_input->p_source ) > 3) && !strncasecmp( p_input->p_source, "ts:", 3 ) )
526 NetworkClose( p_input );
528 else if( ( strlen( p_input->p_source ) > 5 ) && !strncasecmp( p_input->p_source, "http:", 5 ) )
530 NetworkClose( p_input );
534 if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
536 p_input->p_input_module->p_functions->input.functions.input.pf_close( p_input );
538 else if( ( strlen( p_input->p_source ) > 4 ) && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
540 p_input->p_input_module->p_functions->input.functions.input.pf_close( p_input );
544 FileClose( p_input );
547 /* Release modules */
548 module_Unneed( p_input->p_input_module );
552 /*****************************************************************************
553 * DestroyThread: destroy the input thread
554 *****************************************************************************/
555 static void DestroyThread( input_thread_t * p_input )
557 int * pi_status; /* thread status */
560 pi_status = p_input->pi_status;
562 /* Destroy Mutex locks */
563 vlc_mutex_destroy( &p_input->stream.control.control_lock );
564 vlc_mutex_destroy( &p_input->stream.stream_lock );
566 /* Free input structure */
570 *pi_status = THREAD_OVER;
573 /*****************************************************************************
574 * FileOpen : open a file descriptor
575 *****************************************************************************/
576 static void FileOpen( input_thread_t * p_input )
578 struct stat stat_info;
581 char *psz_name = p_input->p_source;
583 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
585 int i_size = strlen( psz_name );
588 && !strncasecmp( psz_name, "dvd:", 4 ) )
590 /* get rid of the 'dvd:' stuff and try again */
592 i_stat = stat( psz_name, &stat_info );
594 else if( ( i_size > 5 )
595 && !strncasecmp( psz_name, "file:", 5 ) )
597 /* get rid of the 'file:' stuff and try again */
599 i_stat = stat( psz_name, &stat_info );
604 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
605 psz_name, strerror(errno));
606 p_input->b_error = 1;
611 vlc_mutex_lock( &p_input->stream.stream_lock );
613 /* If we are here we can control the pace... */
614 p_input->stream.b_pace_control = 1;
616 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
617 || S_ISBLK(stat_info.st_mode) )
619 p_input->stream.b_seekable = 1;
620 p_input->stream.p_selected_area->i_size = stat_info.st_size;
622 else if( S_ISFIFO(stat_info.st_mode)
623 #if !defined( SYS_BEOS ) && !defined( WIN32 )
624 || S_ISSOCK(stat_info.st_mode)
628 p_input->stream.b_seekable = 0;
629 p_input->stream.p_selected_area->i_size = 0;
633 vlc_mutex_unlock( &p_input->stream.stream_lock );
634 intf_ErrMsg( "input error: unknown file type for `%s'",
636 p_input->b_error = 1;
640 p_input->stream.p_selected_area->i_tell = 0;
641 vlc_mutex_unlock( &p_input->stream.stream_lock );
643 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
644 if( (p_input->i_handle = open( psz_name,
645 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
647 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
648 p_input->b_error = 1;
654 /*****************************************************************************
655 * FileClose : close a file descriptor
656 *****************************************************************************/
657 static void FileClose( input_thread_t * p_input )
659 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
661 close( p_input->i_handle );
666 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
667 /*****************************************************************************
668 * NetworkOpen : open a network socket
669 *****************************************************************************/
670 static void NetworkOpen( input_thread_t * p_input )
672 char *psz_server = NULL;
673 char *psz_broadcast = NULL;
677 struct sockaddr_in sock;
678 unsigned int i_mc_group;
686 /* WinSock Library Init. */
687 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
691 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
696 /* Get the remote server */
697 if( p_input->p_source != NULL )
699 psz_server = p_input->p_source;
701 /* Skip the protocol name */
702 while( *psz_server && *psz_server != ':' )
707 /* Skip the "://" part */
708 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
713 /* Found a server name */
716 char *psz_port = psz_server;
718 /* Skip the hostname part */
719 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
724 /* Found a port name or a broadcast addres */
727 /* That's a port name */
728 if( *psz_port == ':' )
732 i_port = atoi( psz_port );
735 /* Search for '/' just after the port in case
736 * we also have a broadcast address */
737 psz_broadcast = psz_port;
738 while( *psz_broadcast && *psz_broadcast != '/' )
745 *psz_broadcast = '\0';
747 while( *psz_broadcast && *psz_broadcast == '/' )
754 psz_broadcast = NULL;
764 /* Check that we got a valid server */
765 if( psz_server == NULL )
767 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
768 INPUT_SERVER_DEFAULT );
771 /* Check that we got a valid port */
774 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
777 if( psz_broadcast == NULL )
779 /* Are we broadcasting ? */
780 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
781 INPUT_BROADCAST_DEFAULT ) )
783 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
784 INPUT_BCAST_ADDR_DEFAULT );
788 psz_broadcast = NULL;
792 intf_WarnMsg( 2, "input: server=%s port=%d broadcast=%s",
793 psz_server, i_port, psz_broadcast );
795 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
797 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
798 if( p_input->i_handle == -1 )
800 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
801 p_input->b_error = 1;
805 /* We may want to reuse an already used socket */
807 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
808 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
810 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
812 close( p_input->i_handle );
813 p_input->b_error = 1;
817 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
818 * packet loss caused by scheduling problems */
820 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
821 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
823 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
825 close( p_input->i_handle );
826 p_input->b_error = 1;
830 /* Check if we really got what we have asked for, because Linux, etc.
831 * will silently limit the max buffer size to net.core.rmem_max which
832 * is typically only 65535 bytes */
834 i_opt_size = sizeof( i_opt );
835 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
836 (void*) &i_opt, &i_opt_size ) == -1 )
838 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
840 close( p_input->i_handle );
841 p_input->b_error = 1;
845 if( i_opt < 0x80000 )
847 intf_WarnMsg( 1, "input warning: socket receive buffer size just 0x%x"
848 " instead of 0x%x bytes.", i_opt, 0x80000 );
851 /* Build the local socket */
852 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
854 intf_ErrMsg( "input error: can't build local address" );
855 close( p_input->i_handle );
856 p_input->b_error = 1;
860 /* Required for IP_ADD_MEMBERSHIP */
861 i_mc_group = sock.sin_addr.s_addr;
864 if ( psz_broadcast != NULL )
866 sock.sin_addr.s_addr = INADDR_ANY;
868 #define IN_MULTICAST(a) IN_CLASSD(a)
872 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
873 sizeof( sock ) ) < 0 )
875 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
876 close( p_input->i_handle );
877 p_input->b_error = 1;
881 /* Join the multicast group if the socket is a multicast address */
882 if( IN_MULTICAST( ntohl(i_mc_group) ) )
886 imr.imr_interface.s_addr = htonl(INADDR_ANY);
887 imr.imr_multiaddr.s_addr = i_mc_group;
888 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
889 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
891 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
893 close( p_input->i_handle);
894 p_input->b_error = 1;
899 /* Build socket for remote connection */
900 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
902 intf_ErrMsg( "input error: can't build remote address" );
903 close( p_input->i_handle );
904 p_input->b_error = 1;
908 /* Only connect if the user has passed a valid host */
909 if( sock.sin_addr.s_addr != INADDR_ANY )
911 /* Connect the socket */
912 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
913 sizeof( sock ) ) == (-1) )
915 intf_ErrMsg( "input error: can't connect socket (%s)",
917 close( p_input->i_handle );
918 p_input->b_error = 1;
923 p_input->stream.b_pace_control = 0;
924 p_input->stream.b_seekable = 0;
926 intf_WarnMsg( 3, "input: successfully opened network mode" );
931 /*****************************************************************************
932 * NetworkClose : close a network socket
933 *****************************************************************************/
934 static void NetworkClose( input_thread_t * p_input )
936 close( p_input->i_handle );
943 /*****************************************************************************
944 * HTTPOpen : make an HTTP request
945 *****************************************************************************/
946 static void HTTPOpen( input_thread_t * p_input )
948 char *psz_server = NULL;
949 char *psz_path = NULL;
953 struct sockaddr_in sock;
954 char psz_buffer[256];
962 /* WinSock Library Init. */
963 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
967 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
972 /* Get the remote server */
973 if( p_input->p_source != NULL )
975 psz_server = p_input->p_source;
977 /* Skip the protocol name */
978 while( *psz_server && *psz_server != ':' )
983 /* Skip the "://" part */
984 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
989 /* Found a server name */
992 char *psz_port = psz_server;
994 /* Skip the hostname part */
995 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1000 /* Found a port name */
1003 if( *psz_port == ':' )
1005 /* Replace ':' with '\0' */
1010 psz_path = psz_port;
1011 while( *psz_path && *psz_path != '/' )
1026 if( *psz_port != '\0' )
1028 i_port = atoi( psz_port );
1038 /* Check that we got a valid server */
1039 if( psz_server == NULL )
1041 intf_ErrMsg( "input error: No server given" );
1042 p_input->b_error = 1;
1046 /* Check that we got a valid port */
1049 i_port = 80; /* FIXME */
1052 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1055 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1057 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1058 if( p_input->i_handle == -1 )
1060 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1064 /* We may want to reuse an already used socket */
1066 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1067 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1069 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1071 close( p_input->i_handle );
1072 p_input->b_error = 1;
1077 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1079 /* http://myproxy.mydomain:myport/ */
1080 int i_proxy_port = 0;
1082 /* Skip the protocol name */
1083 while( *psz_proxy && *psz_proxy != ':' )
1088 /* Skip the "://" part */
1089 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1094 /* Found a proxy name */
1097 char *psz_port = psz_proxy;
1099 /* Skip the hostname part */
1100 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1105 /* Found a port name */
1110 /* Replace ':' with '\0' */
1114 psz_junk = psz_port;
1115 while( *psz_junk && *psz_junk != '/' )
1125 if( *psz_port != '\0' )
1127 i_proxy_port = atoi( psz_port );
1133 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1134 close( p_input->i_handle );
1135 p_input->b_error = 1;
1139 /* Build socket for proxy connection */
1140 if ( network_BuildRemoteAddr( &sock, psz_proxy ) == -1 )
1142 intf_ErrMsg( "input error: can't build remote address" );
1143 close( p_input->i_handle );
1144 p_input->b_error = 1;
1147 sock.sin_port = htons( i_proxy_port );
1151 /* No proxy, direct connection */
1152 if ( network_BuildRemoteAddr( &sock, psz_server ) == -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_port );
1162 /* Connect the socket */
1163 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1164 sizeof( sock ) ) == (-1) )
1166 intf_ErrMsg( "input error: can't connect socket (%s)",
1168 close( p_input->i_handle );
1169 p_input->b_error = 1;
1173 p_input->stream.b_seekable = 0;
1174 p_input->stream.b_pace_control = 1; /* TCP/IP... */
1176 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1177 # define HTTP_END "\r\n"
1179 /* Prepare GET ... */
1180 if( psz_proxy != NULL )
1182 snprintf( psz_buffer, sizeof(psz_buffer),
1183 "GET http://%s:%d/%s HTTP/1.0\r\n"
1184 HTTP_USERAGENT HTTP_END,
1185 psz_server, i_port, psz_path );
1189 snprintf( psz_buffer, sizeof(psz_buffer),
1190 "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1191 HTTP_USERAGENT HTTP_END,
1192 psz_path, psz_server );
1194 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1197 if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1199 intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1200 close( p_input->i_handle );
1201 p_input->b_error = 1;
1205 /* Read HTTP header - this is gonna be fun with plug-ins which do not
1206 * use p_input->p_stream :-( */
1207 if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1209 intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1210 close( p_input->i_handle );
1211 p_input->b_error = 1;
1215 while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1217 if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1218 || *psz_buffer == '\r' || *psz_buffer == '\0' )
1222 /* FIXME : check Content-Type one day */
1225 intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1228 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */