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.148 2001/11/01 15:30:50 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
51 # include <winsock2.h>
52 #elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
53 # include <netdb.h> /* hostent ... */
54 # include <sys/socket.h>
55 # include <netinet/in.h>
56 # include <arpa/inet.h>
57 # include <sys/types.h>
58 # include <sys/socket.h>
61 #ifdef HAVE_SYS_TIMES_H
62 # include <sys/times.h>
73 #include "intf_playlist.h"
75 #include "stream_control.h"
76 #include "input_ext-intf.h"
77 #include "input_ext-dec.h"
78 #include "input_ext-plugins.h"
80 #include "interface.h"
84 /*****************************************************************************
86 *****************************************************************************/
87 static void RunThread ( input_thread_t *p_input );
88 static int InitThread ( input_thread_t *p_input );
89 static void ErrorThread ( input_thread_t *p_input );
90 static void DestroyThread ( input_thread_t *p_input );
91 static void EndThread ( input_thread_t *p_input );
93 static void FileOpen ( input_thread_t *p_input );
94 static void StdOpen ( input_thread_t *p_input );
95 static void FileClose ( input_thread_t *p_input );
96 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
97 static void NetworkOpen ( input_thread_t *p_input );
98 static void HTTPOpen ( input_thread_t *p_input );
99 static void NetworkClose ( input_thread_t *p_input );
102 /*****************************************************************************
103 * input_CreateThread: creates a new input thread
104 *****************************************************************************
105 * This function creates a new input, and returns a pointer
106 * to its description. On error, it returns NULL.
107 * If pi_status is NULL, then the function will block until the thread is ready.
108 * If not, it will be updated using one of the THREAD_* constants.
109 *****************************************************************************/
110 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
112 input_thread_t * p_input; /* thread descriptor */
113 int i_status; /* thread status */
115 /* Allocate descriptor */
116 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
117 if( p_input == NULL )
119 intf_ErrMsg( "input error: can't allocate input thread (%s)",
124 /* Packets read once */
125 p_input->i_read_once = INPUT_READ_ONCE;
127 /* Initialize thread properties */
129 p_input->b_error = 0;
133 p_input->p_source = p_item->psz_name;
135 /* I have never understood that stuff --Meuuh */
136 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
137 *p_input->pi_status = THREAD_CREATE;
139 /* Initialize stream description */
140 p_input->stream.i_es_number = 0;
141 p_input->stream.i_selected_es_number = 0;
142 p_input->stream.i_pgrm_number = 0;
143 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
144 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
145 p_input->stream.i_mux_rate = 0;
147 /* no stream, no area */
148 p_input->stream.i_area_nb = 0;
149 p_input->stream.pp_areas = NULL;
150 p_input->stream.p_selected_area = NULL;
151 p_input->stream.p_new_area = NULL;
153 /* By default there is one area in a stream */
154 input_AddArea( p_input );
155 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
157 /* Initialize stream control properties. */
158 p_input->stream.control.i_status = PLAYING_S;
159 p_input->stream.control.i_rate = DEFAULT_RATE;
160 p_input->stream.control.b_mute = 0;
161 p_input->stream.control.b_grayscale = main_GetIntVariable(
162 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
163 p_input->stream.control.i_smp = main_GetIntVariable(
164 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
166 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
169 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
172 intf_ErrMsg( "input error: can't create input thread (%s)",
178 /* If status is NULL, wait until the thread is created */
179 if( pi_status == NULL )
183 msleep( THREAD_SLEEP );
184 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
185 && (i_status != THREAD_FATAL) );
186 if( i_status != THREAD_READY )
194 /*****************************************************************************
195 * input_DestroyThread: mark an input thread as zombie
196 *****************************************************************************
197 * This function should not return until the thread is effectively cancelled.
198 *****************************************************************************/
199 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
201 int i_status; /* thread status */
204 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
205 *p_input->pi_status = THREAD_DESTROY;
207 /* Request thread destruction */
210 /* Make the thread exit of an eventual vlc_cond_wait() */
211 vlc_mutex_lock( &p_input->stream.stream_lock );
212 vlc_cond_signal( &p_input->stream.stream_wait );
213 vlc_mutex_unlock( &p_input->stream.stream_lock );
215 /* If status is NULL, wait until thread has been destroyed */
216 if( pi_status == NULL )
220 msleep( THREAD_SLEEP );
221 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
222 && (i_status != THREAD_FATAL) );
226 /*****************************************************************************
227 * RunThread: main thread loop
228 *****************************************************************************
229 * Thread in charge of processing the network packets and demultiplexing.
230 *****************************************************************************/
231 static void RunThread( input_thread_t *p_input )
234 data_packet_t ** pp_packets;
236 if( InitThread( p_input ) )
238 /* If we failed, wait before we are killed, and exit */
239 *p_input->pi_status = THREAD_ERROR;
240 p_input->b_error = 1;
241 ErrorThread( p_input );
242 DestroyThread( p_input );
246 /* initialization is completed */
247 vlc_mutex_lock( &p_input->stream.stream_lock );
248 p_input->stream.b_changed = 1;
249 vlc_mutex_unlock( &p_input->stream.stream_lock );
251 pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
252 sizeof( data_packet_t * ) );
253 if( pp_packets == NULL )
255 intf_ErrMsg( "input error: out of memory" );
257 p_input->b_error = 1;
260 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
264 vlc_mutex_lock( &p_input->stream.stream_lock );
266 if( p_input->stream.p_new_area )
268 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
271 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
273 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
275 pgrm_descriptor_t * p_pgrm
276 = p_input->stream.pp_programs[i];
277 /* Escape all decoders for the stream discontinuity they
279 input_EscapeDiscontinuity( p_input, p_pgrm );
281 /* Reinitialize synchro. */
282 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
285 p_input->stream.p_new_area = NULL;
288 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
290 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
292 p_input->pf_seek( p_input,
293 p_input->stream.p_selected_area->i_seek );
295 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
297 pgrm_descriptor_t * p_pgrm
298 = p_input->stream.pp_programs[i];
299 /* Escape all decoders for the stream discontinuity they
301 input_EscapeDiscontinuity( p_input, p_pgrm );
303 /* Reinitialize synchro. */
304 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
307 p_input->stream.p_selected_area->i_seek = NO_SEEK;
310 if( p_input->stream.p_removed_es )
312 input_UnselectES( p_input, p_input->stream.p_removed_es );
313 p_input->stream.p_removed_es = NULL;
316 if( p_input->stream.p_newly_selected_es )
318 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
319 p_input->stream.p_newly_selected_es = NULL;
322 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
324 if( p_input->stream.b_new_mute )
326 input_EscapeAudioDiscontinuity( p_input );
329 vlc_mutex_lock( &p_input->stream.control.control_lock );
330 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
331 vlc_mutex_unlock( &p_input->stream.control.control_lock );
333 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
336 vlc_mutex_unlock( &p_input->stream.stream_lock );
338 i_error = p_input->pf_read( p_input, pp_packets );
340 /* Demultiplex read packets. */
341 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
343 p_input->stream.c_packets_read++;
344 p_input->pf_demux( p_input, pp_packets[i] );
351 /* End of file - we do not set b_die because only the
352 * interface is allowed to do so. */
353 intf_WarnMsg( 3, "input: EOF reached" );
358 p_input->b_error = 1;
365 if( p_input->b_error || p_input->b_eof )
367 ErrorThread( p_input );
370 EndThread( p_input );
372 DestroyThread( p_input );
374 intf_DbgMsg("input: Thread end");
377 /*****************************************************************************
378 * InitThread: init the input Thread
379 *****************************************************************************/
380 static int InitThread( input_thread_t * p_input )
383 /* Initialize statistics */
384 p_input->c_loops = 0;
385 p_input->stream.c_packets_read = 0;
386 p_input->stream.c_packets_trashed = 0;
387 p_input->p_stream = NULL;
390 vlc_mutex_init( &p_input->stream.stream_lock );
391 vlc_cond_init( &p_input->stream.stream_wait );
392 vlc_mutex_init( &p_input->stream.control.control_lock );
394 /* Find appropriate module. */
395 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
396 (probedata_t *)p_input );
398 if( p_input->p_input_module == NULL )
400 intf_ErrMsg( "input error: no suitable input module for `%s'",
405 #define f p_input->p_input_module->p_functions->input.functions.input
406 p_input->pf_init = f.pf_init;
407 p_input->pf_end = f.pf_end;
408 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
409 p_input->pf_read = f.pf_read;
410 p_input->pf_set_area = f.pf_set_area;
411 p_input->pf_demux = f.pf_demux;
412 p_input->pf_new_packet = f.pf_new_packet;
413 p_input->pf_new_pes = f.pf_new_pes;
414 p_input->pf_delete_packet = f.pf_delete_packet;
415 p_input->pf_delete_pes = f.pf_delete_pes;
416 p_input->pf_rewind = f.pf_rewind;
417 p_input->pf_seek = f.pf_seek;
419 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
420 /* FIXME : this is waaaay too kludgy */
421 if( ( strlen( p_input->p_source ) > 3)
422 && !strncasecmp( p_input->p_source, "ts:", 3 ) )
425 NetworkOpen( p_input );
426 p_input->stream.i_method = INPUT_METHOD_NETWORK;
428 else if( ( strlen( p_input->p_source ) > 5 )
429 && !strncasecmp( p_input->p_source, "http:", 5 ) )
433 p_input->stream.i_method = INPUT_METHOD_NETWORK;
437 if( ( strlen( p_input->p_source ) > 4 )
438 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
440 /* DVD - this is THE kludge */
441 f.pf_open( p_input );
442 p_input->stream.i_method = INPUT_METHOD_DVD;
444 else if( ( strlen( p_input->p_source ) > 4 )
445 && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
447 /* Dummy input - very kludgy */
448 f.pf_open( p_input );
450 else if( ( strlen( p_input->p_source ) == 1 )
451 && *p_input->p_source == '-' )
460 p_input->stream.i_method = INPUT_METHOD_FILE;
464 if( p_input->b_error )
466 /* We barfed -- exit nicely */
467 module_Unneed( p_input->p_input_module );
471 p_input->pf_init( p_input );
473 if( p_input->b_error )
475 /* We barfed -- exit nicely */
476 CloseThread( p_input );
477 module_Unneed( p_input->p_input_module );
481 *p_input->pi_status = THREAD_READY;
486 /*****************************************************************************
487 * ErrorThread: RunThread() error loop
488 *****************************************************************************
489 * This function is called when an error occured during thread main's loop.
490 *****************************************************************************/
491 static void ErrorThread( input_thread_t *p_input )
493 while( !p_input->b_die )
496 msleep( INPUT_IDLE_SLEEP );
500 /*****************************************************************************
501 * EndThread: end the input thread
502 *****************************************************************************/
503 static void EndThread( input_thread_t * p_input )
505 int * pi_status; /* thread status */
508 pi_status = p_input->pi_status;
509 *pi_status = THREAD_END;
511 if( p_main->b_stats )
513 #ifdef HAVE_SYS_TIMES_H
514 /* Display statistics */
515 struct tms cpu_usage;
518 intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
520 cpu_usage.tms_utime, cpu_usage.tms_stime );
522 intf_StatMsg( "input stats: %d loops", p_input->c_loops );
525 input_DumpStream( p_input );
528 /* Free all ES and destroy all decoder threads */
529 input_EndStream( p_input );
531 /* Free demultiplexer's data */
532 p_input->pf_end( p_input );
534 /* Close the input method */
535 CloseThread( p_input );
537 /* Release modules */
538 module_Unneed( p_input->p_input_module );
542 /*****************************************************************************
543 * CloseThread: close the target
544 *****************************************************************************/
545 static void CloseThread( input_thread_t * p_input )
547 #define f p_input->p_input_module->p_functions->input.functions.input
549 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
551 if( ( strlen( p_input->p_source ) > 3)
552 && !strncasecmp( p_input->p_source, "ts:", 3 ) )
554 NetworkClose( p_input );
556 else if( ( strlen( p_input->p_source ) > 5 )
557 && !strncasecmp( p_input->p_source, "http:", 5 ) )
559 NetworkClose( p_input );
563 if( ( strlen( p_input->p_source ) > 4 )
564 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
566 f.pf_close( p_input );
568 else if( ( strlen( p_input->p_source ) > 4 )
569 && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
571 f.pf_close( p_input );
575 FileClose( p_input );
580 /*****************************************************************************
581 * DestroyThread: destroy the input thread
582 *****************************************************************************/
583 static void DestroyThread( input_thread_t * p_input )
585 int * pi_status; /* thread status */
588 pi_status = p_input->pi_status;
590 /* Destroy Mutex locks */
591 vlc_mutex_destroy( &p_input->stream.control.control_lock );
592 vlc_mutex_destroy( &p_input->stream.stream_lock );
594 /* Free input structure */
598 *pi_status = THREAD_OVER;
601 /*****************************************************************************
602 * StdOpen : open standard input
603 *****************************************************************************/
604 static void StdOpen( input_thread_t * p_input )
606 vlc_mutex_lock( &p_input->stream.stream_lock );
608 /* Suppose we can control the pace - this won't work in some cases ! */
609 p_input->stream.b_pace_control = 1;
611 p_input->stream.b_seekable = 0;
612 p_input->stream.p_selected_area->i_size = 0;
613 p_input->stream.p_selected_area->i_tell = 0;
614 vlc_mutex_unlock( &p_input->stream.stream_lock );
616 intf_WarnMsg( 2, "input: opening stdin" );
617 p_input->i_handle = 0;
620 /*****************************************************************************
621 * FileOpen : open a file descriptor
622 *****************************************************************************/
623 static void FileOpen( input_thread_t * p_input )
625 struct stat stat_info;
628 char *psz_name = p_input->p_source;
630 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
632 int i_size = strlen( psz_name );
635 && !strncasecmp( psz_name, "dvd:", 4 ) )
637 /* get rid of the 'dvd:' stuff and try again */
639 i_stat = stat( psz_name, &stat_info );
641 else if( ( i_size > 4 )
642 && !strncasecmp( psz_name, "vcd:", 4 ) )
644 /* get rid of the 'vcd:' stuff and try again */
646 i_stat = stat( psz_name, &stat_info );
648 else if( ( i_size > 5 )
649 && !strncasecmp( psz_name, "file:", 5 ) )
651 /* get rid of the 'file:' stuff and try again */
653 i_stat = stat( psz_name, &stat_info );
658 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
659 psz_name, strerror(errno));
660 p_input->b_error = 1;
665 vlc_mutex_lock( &p_input->stream.stream_lock );
667 /* If we are here we can control the pace... */
668 p_input->stream.b_pace_control = 1;
670 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
671 || S_ISBLK(stat_info.st_mode) )
673 p_input->stream.b_seekable = 1;
674 p_input->stream.p_selected_area->i_size = stat_info.st_size;
676 else if( S_ISFIFO(stat_info.st_mode)
677 #if !defined( SYS_BEOS ) && !defined( WIN32 )
678 || S_ISSOCK(stat_info.st_mode)
682 p_input->stream.b_seekable = 0;
683 p_input->stream.p_selected_area->i_size = 0;
687 vlc_mutex_unlock( &p_input->stream.stream_lock );
688 intf_ErrMsg( "input error: unknown file type for `%s'",
690 p_input->b_error = 1;
694 p_input->stream.p_selected_area->i_tell = 0;
695 vlc_mutex_unlock( &p_input->stream.stream_lock );
697 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
698 if( (p_input->i_handle = open( psz_name,
699 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
701 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
702 p_input->b_error = 1;
708 /*****************************************************************************
709 * FileClose : close a file descriptor
710 *****************************************************************************/
711 static void FileClose( input_thread_t * p_input )
713 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
715 close( p_input->i_handle );
720 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
721 /*****************************************************************************
722 * NetworkOpen : open a network socket
723 *****************************************************************************/
724 static void NetworkOpen( input_thread_t * p_input )
726 char *psz_server = NULL;
727 char *psz_broadcast = NULL;
731 struct sockaddr_in sock;
732 unsigned int i_mc_group;
740 /* WinSock Library Init. */
741 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
745 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
750 /* Get the remote server */
751 if( p_input->p_source != NULL )
753 psz_server = p_input->p_source;
755 /* Skip the protocol name */
756 while( *psz_server && *psz_server != ':' )
761 /* Skip the "://" part */
762 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
767 /* Found a server name */
770 char *psz_port = psz_server;
772 /* Skip the hostname part */
773 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
778 /* Found a port name or a broadcast addres */
781 /* That's a port name */
782 if( *psz_port == ':' )
786 i_port = atoi( psz_port );
789 /* Search for '/' just after the port in case
790 * we also have a broadcast address */
791 psz_broadcast = psz_port;
792 while( *psz_broadcast && *psz_broadcast != '/' )
799 *psz_broadcast = '\0';
801 while( *psz_broadcast && *psz_broadcast == '/' )
808 psz_broadcast = NULL;
818 /* Check that we got a valid server */
819 if( psz_server == NULL )
821 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
822 INPUT_SERVER_DEFAULT );
825 /* Check that we got a valid port */
828 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
831 if( psz_broadcast == NULL )
833 /* Are we broadcasting ? */
834 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
835 INPUT_BROADCAST_DEFAULT ) )
837 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
838 INPUT_BCAST_ADDR_DEFAULT );
842 psz_broadcast = NULL;
846 intf_WarnMsg( 2, "input: server=%s port=%d broadcast=%s",
847 psz_server, i_port, psz_broadcast );
849 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
851 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
852 if( p_input->i_handle == -1 )
854 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
855 p_input->b_error = 1;
859 /* We may want to reuse an already used socket */
861 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
862 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
864 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
866 close( p_input->i_handle );
867 p_input->b_error = 1;
871 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
872 * packet loss caused by scheduling problems */
874 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
875 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
877 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
879 close( p_input->i_handle );
880 p_input->b_error = 1;
884 /* Check if we really got what we have asked for, because Linux, etc.
885 * will silently limit the max buffer size to net.core.rmem_max which
886 * is typically only 65535 bytes */
888 i_opt_size = sizeof( i_opt );
889 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
890 (void*) &i_opt, &i_opt_size ) == -1 )
892 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
894 close( p_input->i_handle );
895 p_input->b_error = 1;
899 if( i_opt < 0x80000 )
901 intf_WarnMsg( 1, "input warning: socket receive buffer size just 0x%x"
902 " instead of 0x%x bytes.", i_opt, 0x80000 );
905 /* Build the local socket */
906 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
908 intf_ErrMsg( "input error: can't build local address" );
909 close( p_input->i_handle );
910 p_input->b_error = 1;
914 /* Required for IP_ADD_MEMBERSHIP */
915 i_mc_group = sock.sin_addr.s_addr;
918 if ( psz_broadcast != NULL )
920 sock.sin_addr.s_addr = INADDR_ANY;
923 #define IN_MULTICAST(a) IN_CLASSD(a)
927 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
928 sizeof( sock ) ) < 0 )
930 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
931 close( p_input->i_handle );
932 p_input->b_error = 1;
936 /* Join the multicast group if the socket is a multicast address */
939 if( IN_MULTICAST( ntohl(i_mc_group) ) )
943 imr.imr_interface.s_addr = htonl(INADDR_ANY);
944 imr.imr_multiaddr.s_addr = i_mc_group;
945 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
946 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
948 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
950 close( p_input->i_handle);
951 p_input->b_error = 1;
957 /* Build socket for remote connection */
958 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
960 intf_ErrMsg( "input error: can't build remote address" );
961 close( p_input->i_handle );
962 p_input->b_error = 1;
966 /* Only connect if the user has passed a valid host */
967 if( sock.sin_addr.s_addr != INADDR_ANY )
969 /* Connect the socket */
970 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
971 sizeof( sock ) ) == (-1) )
973 intf_ErrMsg( "input error: can't connect socket (%s)",
975 close( p_input->i_handle );
976 p_input->b_error = 1;
981 p_input->stream.b_pace_control = 0;
982 p_input->stream.b_seekable = 0;
984 intf_WarnMsg( 3, "input: successfully opened network mode" );
989 /*****************************************************************************
990 * NetworkClose : close a network socket
991 *****************************************************************************/
992 static void NetworkClose( input_thread_t * p_input )
994 close( p_input->i_handle );
1001 /*****************************************************************************
1002 * HTTPOpen : make an HTTP request
1003 *****************************************************************************/
1004 static void HTTPOpen( input_thread_t * p_input )
1006 char *psz_server = NULL;
1007 char *psz_path = NULL;
1011 struct sockaddr_in sock;
1012 char psz_buffer[256];
1020 /* WinSock Library Init. */
1021 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
1025 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
1030 /* Get the remote server */
1031 if( p_input->p_source != NULL )
1033 psz_server = p_input->p_source;
1035 /* Skip the protocol name */
1036 while( *psz_server && *psz_server != ':' )
1041 /* Skip the "://" part */
1042 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1047 /* Found a server name */
1050 char *psz_port = psz_server;
1052 /* Skip the hostname part */
1053 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1058 /* Found a port name */
1061 if( *psz_port == ':' )
1063 /* Replace ':' with '\0' */
1068 psz_path = psz_port;
1069 while( *psz_path && *psz_path != '/' )
1084 if( *psz_port != '\0' )
1086 i_port = atoi( psz_port );
1096 /* Check that we got a valid server */
1097 if( psz_server == NULL )
1099 intf_ErrMsg( "input error: No server given" );
1100 p_input->b_error = 1;
1104 /* Check that we got a valid port */
1107 i_port = 80; /* FIXME */
1110 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1113 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1115 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1116 if( p_input->i_handle == -1 )
1118 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1122 /* We may want to reuse an already used socket */
1124 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1125 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1127 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1129 close( p_input->i_handle );
1130 p_input->b_error = 1;
1135 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1137 /* http://myproxy.mydomain:myport/ */
1138 int i_proxy_port = 0;
1140 /* Skip the protocol name */
1141 while( *psz_proxy && *psz_proxy != ':' )
1146 /* Skip the "://" part */
1147 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1152 /* Found a proxy name */
1155 char *psz_port = psz_proxy;
1157 /* Skip the hostname part */
1158 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1163 /* Found a port name */
1168 /* Replace ':' with '\0' */
1172 psz_junk = psz_port;
1173 while( *psz_junk && *psz_junk != '/' )
1183 if( *psz_port != '\0' )
1185 i_proxy_port = atoi( psz_port );
1191 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1192 close( p_input->i_handle );
1193 p_input->b_error = 1;
1197 /* Build socket for proxy connection */
1198 if ( network_BuildRemoteAddr( &sock, psz_proxy ) == -1 )
1200 intf_ErrMsg( "input error: can't build remote address" );
1201 close( p_input->i_handle );
1202 p_input->b_error = 1;
1205 sock.sin_port = htons( i_proxy_port );
1209 /* No proxy, direct connection */
1210 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
1212 intf_ErrMsg( "input error: can't build remote address" );
1213 close( p_input->i_handle );
1214 p_input->b_error = 1;
1217 sock.sin_port = htons( i_port );
1220 /* Connect the socket */
1221 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1222 sizeof( sock ) ) == (-1) )
1224 intf_ErrMsg( "input error: can't connect socket (%s)",
1226 close( p_input->i_handle );
1227 p_input->b_error = 1;
1231 p_input->stream.b_seekable = 0;
1232 p_input->stream.b_pace_control = 1; /* TCP/IP... */
1234 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1235 # define HTTP_END "\r\n"
1237 /* Prepare GET ... */
1238 if( psz_proxy != NULL )
1240 snprintf( psz_buffer, sizeof(psz_buffer),
1241 "GET http://%s:%d/%s HTTP/1.0\r\n"
1242 HTTP_USERAGENT HTTP_END,
1243 psz_server, i_port, psz_path );
1247 snprintf( psz_buffer, sizeof(psz_buffer),
1248 "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1249 HTTP_USERAGENT HTTP_END,
1250 psz_path, psz_server );
1252 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1255 if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1257 intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1258 close( p_input->i_handle );
1259 p_input->b_error = 1;
1263 /* Read HTTP header - this is gonna be fun with plug-ins which do not
1264 * use p_input->p_stream :-( */
1265 if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1267 intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1268 close( p_input->i_handle );
1269 p_input->b_error = 1;
1273 while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1275 if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1276 || *psz_buffer == '\r' || *psz_buffer == '\0' )
1280 /* FIXME : check Content-Type one day */
1283 intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1286 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */