1 /*****************************************************************************
2 * input.c: input thread
3 * Read an MPEG2 stream, demultiplex and parse it before sending it to
5 *****************************************************************************
6 * Copyright (C) 1998-2001 VideoLAN
7 * $Id: input.c,v 1.168 2002/01/07 02:12:29 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 *****************************************************************************/
30 #include <sys/types.h>
34 #include <videolan/vlc.h>
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
45 #ifdef STRNCASECMP_IN_STRINGS_H
50 # include <winsock2.h>
51 # include <ws2tcpip.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 # ifdef HAVE_ARPA_INET_H
57 # include <arpa/inet.h> /* inet_ntoa(), inet_aton() */
61 #ifdef HAVE_SYS_TIMES_H
62 # include <sys/times.h>
67 #include "intf_playlist.h"
69 #include "stream_control.h"
70 #include "input_ext-intf.h"
71 #include "input_ext-dec.h"
72 #include "input_ext-plugins.h"
74 #include "interface.h"
76 /*****************************************************************************
78 *****************************************************************************/
79 static void RunThread ( input_thread_t *p_input );
80 static int InitThread ( input_thread_t *p_input );
81 static void ErrorThread ( input_thread_t *p_input );
82 static void CloseThread ( input_thread_t *p_input );
83 static void DestroyThread ( input_thread_t *p_input );
84 static void EndThread ( input_thread_t *p_input );
86 static void FileOpen ( input_thread_t *p_input );
87 static void StdOpen ( input_thread_t *p_input );
88 static void FileClose ( input_thread_t *p_input );
89 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
90 static void NetworkOpen ( input_thread_t *p_input );
91 static void HTTPOpen ( input_thread_t *p_input );
92 static void NetworkClose ( input_thread_t *p_input );
95 /*****************************************************************************
96 * input_InitBank: initialize the input bank.
97 *****************************************************************************/
98 void input_InitBank ( void )
100 p_input_bank->i_count = 0;
102 /* XXX: Workaround for old interface modules */
103 p_input_bank->pp_input[0] = NULL;
105 vlc_mutex_init( &p_input_bank->lock );
108 /*****************************************************************************
109 * input_EndBank: empty the input bank.
110 *****************************************************************************
111 * This function ends all unused inputs and empties the bank in
113 *****************************************************************************/
114 void input_EndBank ( void )
116 /* Ask all remaining video outputs to die */
117 while( p_input_bank->i_count )
120 p_input_bank->pp_input[ --p_input_bank->i_count ], NULL );
123 vlc_mutex_destroy( &p_input_bank->lock );
126 /*****************************************************************************
127 * input_CreateThread: creates a new input thread
128 *****************************************************************************
129 * This function creates a new input, and returns a pointer
130 * to its description. On error, it returns NULL.
131 * If pi_status is NULL, then the function will block until the thread is ready.
132 * If not, it will be updated using one of the THREAD_* constants.
133 *****************************************************************************/
134 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
136 input_thread_t * p_input; /* thread descriptor */
137 int i_status; /* thread status */
139 /* Allocate descriptor */
140 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
141 if( p_input == NULL )
143 intf_ErrMsg( "input error: can't allocate input thread (%s)",
148 /* Initialize thread properties */
150 p_input->b_error = 0;
154 p_input->p_source = p_item->psz_name;
156 /* I have never understood that stuff --Meuuh */
157 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
158 *p_input->pi_status = THREAD_CREATE;
160 /* Initialize stream description */
161 p_input->stream.i_es_number = 0;
162 p_input->stream.i_selected_es_number = 0;
163 p_input->stream.i_pgrm_number = 0;
164 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
165 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
166 p_input->stream.i_mux_rate = 0;
168 /* no stream, no area */
169 p_input->stream.i_area_nb = 0;
170 p_input->stream.pp_areas = NULL;
171 p_input->stream.p_selected_area = NULL;
172 p_input->stream.p_new_area = NULL;
174 /* By default there is one area in a stream */
175 input_AddArea( p_input );
176 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
178 /* Initialize stream control properties. */
179 p_input->stream.control.i_status = PLAYING_S;
180 p_input->stream.control.i_rate = DEFAULT_RATE;
181 p_input->stream.control.b_mute = 0;
182 p_input->stream.control.b_grayscale = main_GetIntVariable(
183 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
184 p_input->stream.control.i_smp = main_GetIntVariable(
185 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
187 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
190 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
193 intf_ErrMsg( "input error: can't create input thread (%s)",
199 /* If status is NULL, wait until the thread is created */
200 if( pi_status == NULL )
204 msleep( THREAD_SLEEP );
205 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
206 && (i_status != THREAD_FATAL) );
207 if( i_status != THREAD_READY )
215 /*****************************************************************************
216 * input_DestroyThread: mark an input thread as zombie
217 *****************************************************************************
218 * This function should not return until the thread is effectively cancelled.
219 *****************************************************************************/
220 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
222 int i_status; /* thread status */
225 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
226 *p_input->pi_status = THREAD_DESTROY;
228 /* Request thread destruction */
231 /* Make the thread exit from a possible vlc_cond_wait() */
232 vlc_mutex_lock( &p_input->stream.stream_lock );
233 vlc_cond_signal( &p_input->stream.stream_wait );
234 vlc_mutex_unlock( &p_input->stream.stream_lock );
236 /* If status is NULL, wait until thread has been destroyed */
237 if( pi_status == NULL )
241 msleep( THREAD_SLEEP );
242 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
243 && (i_status != THREAD_FATAL) );
247 /*****************************************************************************
248 * RunThread: main thread loop
249 *****************************************************************************
250 * Thread in charge of processing the network packets and demultiplexing.
251 *****************************************************************************/
252 static void RunThread( input_thread_t *p_input )
254 if( InitThread( p_input ) )
256 /* If we failed, wait before we are killed, and exit */
257 *p_input->pi_status = THREAD_ERROR;
258 p_input->b_error = 1;
259 ErrorThread( p_input );
260 DestroyThread( p_input );
264 /* initialization is complete */
265 vlc_mutex_lock( &p_input->stream.stream_lock );
266 p_input->stream.b_changed = 1;
267 vlc_mutex_unlock( &p_input->stream.stream_lock );
269 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
271 data_packet_t * p_data;
276 vlc_mutex_lock( &p_input->stream.stream_lock );
278 if( p_input->stream.p_new_program )
280 if( p_input->pf_set_program != NULL )
283 p_input->pf_set_program( p_input,
284 p_input->stream.p_new_program );
286 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
288 pgrm_descriptor_t * p_pgrm
289 = p_input->stream.pp_programs[i];
290 /* Escape all decoders for the stream discontinuity they
292 input_EscapeDiscontinuity( p_input, p_pgrm );
294 /* Reinitialize synchro. */
295 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
298 p_input->stream.p_new_program = NULL;
301 if( p_input->stream.p_new_area )
303 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
306 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
308 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
310 pgrm_descriptor_t * p_pgrm
311 = p_input->stream.pp_programs[i];
312 /* Escape all decoders for the stream discontinuity they
314 input_EscapeDiscontinuity( p_input, p_pgrm );
316 /* Reinitialize synchro. */
317 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
320 p_input->stream.p_new_area = NULL;
323 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
325 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
327 p_input->pf_seek( p_input,
328 p_input->stream.p_selected_area->i_seek );
330 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
332 pgrm_descriptor_t * p_pgrm
333 = p_input->stream.pp_programs[i];
334 /* Escape all decoders for the stream discontinuity they
336 input_EscapeDiscontinuity( p_input, p_pgrm );
338 /* Reinitialize synchro. */
339 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
342 p_input->stream.p_selected_area->i_seek = NO_SEEK;
345 if( p_input->stream.p_removed_es )
347 input_UnselectES( p_input, p_input->stream.p_removed_es );
348 p_input->stream.p_removed_es = NULL;
351 if( p_input->stream.p_newly_selected_es )
353 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
354 p_input->stream.p_newly_selected_es = NULL;
357 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
359 if( p_input->stream.b_new_mute )
361 input_EscapeAudioDiscontinuity( p_input );
364 vlc_mutex_lock( &p_input->stream.control.control_lock );
365 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
366 vlc_mutex_unlock( &p_input->stream.control.control_lock );
368 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
371 vlc_mutex_unlock( &p_input->stream.stream_lock );
373 i_count = p_input->pf_read( p_input, &p_data );
375 /* Demultiplex read packets. */
376 while( p_data != NULL )
378 data_packet_t * p_next = p_data->p_next;
379 p_data->p_next = NULL;
381 p_input->stream.c_packets_read++;
382 p_input->pf_demux( p_input, p_data );
387 if( i_count == 0 && p_input->stream.b_seekable )
389 /* End of file - we do not set b_die because only the
390 * interface is allowed to do so. */
391 intf_WarnMsg( 3, "input: EOF reached" );
394 else if( i_count < 0 )
396 p_input->b_error = 1;
400 if( p_input->b_error || p_input->b_eof )
402 ErrorThread( p_input );
405 EndThread( p_input );
407 DestroyThread( p_input );
409 intf_DbgMsg("input: Thread end");
412 /*****************************************************************************
413 * InitThread: init the input Thread
414 *****************************************************************************/
415 static int InitThread( input_thread_t * p_input )
417 /* Initialize statistics */
418 p_input->c_loops = 0;
419 p_input->stream.c_packets_read = 0;
420 p_input->stream.c_packets_trashed = 0;
421 p_input->p_stream = NULL;
424 vlc_mutex_init( &p_input->stream.stream_lock );
425 vlc_cond_init( &p_input->stream.stream_wait );
426 vlc_mutex_init( &p_input->stream.control.control_lock );
428 /* Find appropriate module. */
429 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
430 main_GetPszVariable( INPUT_METHOD_VAR, NULL ),
431 (probedata_t *)p_input );
433 if( p_input->p_input_module == NULL )
435 intf_ErrMsg( "input error: no suitable input module for `%s'",
440 #define f p_input->p_input_module->p_functions->input.functions.input
441 p_input->pf_init = f.pf_init;
442 p_input->pf_end = f.pf_end;
443 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
444 p_input->pf_read = f.pf_read;
445 p_input->pf_set_area = f.pf_set_area;
446 p_input->pf_set_program = f.pf_set_program;
447 p_input->pf_demux = f.pf_demux;
448 p_input->pf_new_packet = f.pf_new_packet;
449 p_input->pf_new_pes = f.pf_new_pes;
450 p_input->pf_delete_packet = f.pf_delete_packet;
451 p_input->pf_delete_pes = f.pf_delete_pes;
452 p_input->pf_rewind = f.pf_rewind;
453 p_input->pf_seek = f.pf_seek;
455 if( f.pf_open != NULL )
457 f.pf_open( p_input );
458 p_input->stream.i_method = INPUT_METHOD_DVD;
460 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
461 /* FIXME : this is waaaay too kludgy */
462 else if( ( strlen( p_input->p_source ) >= 10
463 && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
464 || ( strlen( p_input->p_source ) >= 4
465 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
468 NetworkOpen( p_input );
469 p_input->stream.i_method = INPUT_METHOD_NETWORK;
471 else if( ( strlen( p_input->p_source ) > 5 )
472 && !strncasecmp( p_input->p_source, "http:", 5 ) )
476 p_input->stream.i_method = INPUT_METHOD_NETWORK;
479 else if( ( strlen( p_input->p_source ) == 1 )
480 && *p_input->p_source == '-' )
489 p_input->stream.i_method = INPUT_METHOD_FILE;
493 if( p_input->b_error )
495 /* We barfed -- exit nicely */
496 module_Unneed( p_input->p_input_module );
500 p_input->pf_init( p_input );
502 if( p_input->b_error )
504 /* We barfed -- exit nicely */
505 CloseThread( p_input );
506 module_Unneed( p_input->p_input_module );
510 *p_input->pi_status = THREAD_READY;
515 /*****************************************************************************
516 * ErrorThread: RunThread() error loop
517 *****************************************************************************
518 * This function is called when an error occured during thread main's loop.
519 *****************************************************************************/
520 static void ErrorThread( input_thread_t *p_input )
522 while( !p_input->b_die )
525 msleep( INPUT_IDLE_SLEEP );
529 /*****************************************************************************
530 * EndThread: end the input thread
531 *****************************************************************************/
532 static void EndThread( input_thread_t * p_input )
534 int * pi_status; /* thread status */
537 pi_status = p_input->pi_status;
538 *pi_status = THREAD_END;
540 if( p_main->b_stats )
542 #ifdef HAVE_SYS_TIMES_H
543 /* Display statistics */
544 struct tms cpu_usage;
547 intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
549 cpu_usage.tms_utime, cpu_usage.tms_stime );
551 intf_StatMsg( "input stats: %d loops", p_input->c_loops );
554 input_DumpStream( p_input );
557 /* Free all ES and destroy all decoder threads */
558 input_EndStream( p_input );
560 /* Free demultiplexer's data */
561 p_input->pf_end( p_input );
563 /* Close the input method */
564 CloseThread( p_input );
566 /* Release modules */
567 module_Unneed( p_input->p_input_module );
570 /*****************************************************************************
571 * CloseThread: close the target
572 *****************************************************************************/
573 static void CloseThread( input_thread_t * p_input )
575 #define f p_input->p_input_module->p_functions->input.functions.input
577 if( f.pf_close != NULL )
579 f.pf_close( p_input );
581 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
583 else if( ( strlen( p_input->p_source ) > 10
584 && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
585 || ( strlen( p_input->p_source ) > 4
586 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
588 NetworkClose( p_input );
590 else if( ( strlen( p_input->p_source ) > 5 )
591 && !strncasecmp( p_input->p_source, "http:", 5 ) )
593 NetworkClose( p_input );
598 FileClose( p_input );
603 /*****************************************************************************
604 * DestroyThread: destroy the input thread
605 *****************************************************************************/
606 static void DestroyThread( input_thread_t * p_input )
608 int * pi_status; /* thread status */
611 pi_status = p_input->pi_status;
613 /* Destroy Mutex locks */
614 vlc_mutex_destroy( &p_input->stream.control.control_lock );
615 vlc_mutex_destroy( &p_input->stream.stream_lock );
617 /* Free input structure */
621 *pi_status = THREAD_OVER;
624 /*****************************************************************************
625 * StdOpen : open standard input
626 *****************************************************************************/
627 static void StdOpen( input_thread_t * p_input )
629 vlc_mutex_lock( &p_input->stream.stream_lock );
631 /* Suppose we can control the pace - this won't work in some cases ! */
632 p_input->stream.b_pace_control = 1;
634 p_input->stream.b_seekable = 0;
635 p_input->stream.p_selected_area->i_size = 0;
636 p_input->stream.p_selected_area->i_tell = 0;
637 vlc_mutex_unlock( &p_input->stream.stream_lock );
639 intf_WarnMsg( 2, "input: opening stdin" );
640 p_input->i_handle = 0;
643 /*****************************************************************************
644 * FileOpen : open a file descriptor
645 *****************************************************************************/
646 static void FileOpen( input_thread_t * p_input )
648 struct stat stat_info;
651 char *psz_name = p_input->p_source;
653 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
655 int i_size = strlen( psz_name );
658 && !strncasecmp( psz_name, "dvdread:", 8 ) )
660 /* get rid of the 'dvdread:' stuff and try again */
662 i_stat = stat( psz_name, &stat_info );
664 else if( ( i_size > 4 )
665 && !strncasecmp( psz_name, "dvd:", 4 ) )
667 /* get rid of the 'dvd:' stuff and try again */
669 i_stat = stat( psz_name, &stat_info );
671 else if( ( i_size > 4 )
672 && !strncasecmp( psz_name, "vcd:", 4 ) )
674 /* get rid of the 'vcd:' stuff and try again */
676 i_stat = stat( psz_name, &stat_info );
678 else if( ( i_size > 5 )
679 && !strncasecmp( psz_name, "file:", 5 ) )
681 /* get rid of the 'file:' stuff and try again */
683 i_stat = stat( psz_name, &stat_info );
688 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
689 psz_name, strerror(errno));
690 p_input->b_error = 1;
695 vlc_mutex_lock( &p_input->stream.stream_lock );
697 /* If we are here we can control the pace... */
698 p_input->stream.b_pace_control = 1;
700 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
701 || S_ISBLK(stat_info.st_mode) )
703 p_input->stream.b_seekable = 1;
704 p_input->stream.p_selected_area->i_size = stat_info.st_size;
706 else if( S_ISFIFO(stat_info.st_mode)
707 #if !defined( SYS_BEOS ) && !defined( WIN32 )
708 || S_ISSOCK(stat_info.st_mode)
712 p_input->stream.b_seekable = 0;
713 p_input->stream.p_selected_area->i_size = 0;
717 vlc_mutex_unlock( &p_input->stream.stream_lock );
718 intf_ErrMsg( "input error: unknown file type for `%s'",
720 p_input->b_error = 1;
724 p_input->stream.p_selected_area->i_tell = 0;
725 vlc_mutex_unlock( &p_input->stream.stream_lock );
727 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
728 if( (p_input->i_handle = open( psz_name,
729 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
731 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
732 p_input->b_error = 1;
738 /*****************************************************************************
739 * FileClose : close a file descriptor
740 *****************************************************************************/
741 static void FileClose( input_thread_t * p_input )
743 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
745 close( p_input->i_handle );
750 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
751 /*****************************************************************************
752 * NetworkOpen : open a network socket
753 *****************************************************************************/
754 static void NetworkOpen( input_thread_t * p_input )
756 char *psz_server = NULL;
757 char *psz_bind = NULL;
758 int i_server_port = 0;
762 struct sockaddr_in sock;
764 /* Get the remote server. Syntax is :
765 * udp[stream]:[/][/][serveraddr[:serverport]][@[bindaddr]:[bindport]] */
766 if( p_input->p_source != NULL )
768 char * psz_parser = p_input->p_source;
769 char * psz_server_port = NULL;
770 char * psz_bind_port = NULL;
772 /* Skip the protocol name */
773 while( *psz_parser && *psz_parser != ':' )
778 /* Skip the "://" part */
779 while( *psz_parser && (*psz_parser == ':' || *psz_parser == '/') )
784 if( *psz_parser && *psz_parser != '@' )
787 psz_server = psz_parser;
789 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
794 if( *psz_parser == ':' )
796 /* Found server port */
797 *psz_parser = '\0'; /* Terminate server name */
799 psz_server_port = psz_parser;
801 while( *psz_parser && *psz_parser != '@' )
808 if( *psz_parser == '@' )
810 /* Found bind address or bind port */
811 *psz_parser = '\0'; /* Terminate server port or name if necessary */
814 if( *psz_parser && *psz_parser != ':' )
816 /* Found bind address */
817 psz_bind = psz_parser;
819 while( *psz_parser && *psz_parser != ':' )
825 if( *psz_parser == ':' )
827 /* Found bind port */
828 *psz_parser = '\0'; /* Terminate bind address if necessary */
831 psz_bind_port = psz_parser;
835 /* Convert ports format */
836 if( psz_server_port != NULL )
838 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
841 intf_ErrMsg( "input error: cannot parse server port near %s",
843 p_input->b_error = 1;
848 if( psz_bind_port != NULL )
850 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
853 intf_ErrMsg( "input error: cannot parse bind port near %s",
855 p_input->b_error = 1;
862 /* This is required or NetworkClose will never be called */
863 p_input->p_source = "ts: network input";
866 /* Check that we got a valid port */
867 if( i_bind_port == 0 )
869 i_bind_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
872 intf_WarnMsg( 2, "input: server=%s:%d local=%s:%d",
873 psz_server, i_server_port, psz_bind, i_bind_port );
875 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
877 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
878 if( p_input->i_handle == -1 )
880 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
881 p_input->b_error = 1;
885 /* We may want to reuse an already used socket */
887 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
888 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
890 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
892 close( p_input->i_handle );
893 p_input->b_error = 1;
897 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
898 * packet loss caused by scheduling problems */
900 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
901 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
903 intf_WarnMsg( 1, "input warning: can't configure socket (SO_RCVBUF: %s)",
907 /* Check if we really got what we have asked for, because Linux, etc.
908 * will silently limit the max buffer size to net.core.rmem_max which
909 * is typically only 65535 bytes */
911 i_opt_size = sizeof( i_opt );
912 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
913 (void*) &i_opt, &i_opt_size ) == -1 )
915 intf_WarnMsg( 1, "input warning: can't query socket (SO_RCVBUF: %s)",
918 else if( i_opt < 0x80000 )
920 intf_WarnMsg( 1, "input warning: socket buffer size is 0x%x"
921 " instead of 0x%x", i_opt, 0x80000 );
924 /* Build the local socket */
925 if ( network_BuildAddr( &sock, psz_bind, i_bind_port ) == -1 )
927 intf_ErrMsg( "input error: can't build local address" );
928 close( p_input->i_handle );
929 p_input->b_error = 1;
934 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
935 sizeof( sock ) ) < 0 )
937 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
938 close( p_input->i_handle );
939 p_input->b_error = 1;
943 /* Allow broadcast reception if we bound on INADDR_ANY */
944 if( psz_bind == NULL )
947 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_BROADCAST,
948 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
950 intf_WarnMsg( 1, "input warning: can't configure socket (SO_BROADCAST: %s)",
955 /* Join the multicast group if the socket is a multicast address */
957 # define IN_MULTICAST(a) IN_CLASSD(a)
960 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
964 imr.imr_interface.s_addr = INADDR_ANY;
965 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
966 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
967 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
969 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
971 close( p_input->i_handle );
972 p_input->b_error = 1;
977 if( psz_server != NULL )
979 /* Build socket for remote connection */
980 if ( network_BuildAddr( &sock, psz_server, i_server_port ) == -1 )
982 intf_ErrMsg( "input error: can't build remote address" );
983 close( p_input->i_handle );
984 p_input->b_error = 1;
988 /* Connect the socket */
989 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
990 sizeof( sock ) ) == (-1) )
992 intf_ErrMsg( "input error: can't connect socket (%s)",
994 close( p_input->i_handle );
995 p_input->b_error = 1;
1000 p_input->stream.b_pace_control = 0;
1001 p_input->stream.b_seekable = 0;
1003 intf_WarnMsg( 3, "input: successfully opened network mode" );
1008 /*****************************************************************************
1009 * NetworkClose : close a network socket
1010 *****************************************************************************/
1011 static void NetworkClose( input_thread_t * p_input )
1013 intf_WarnMsg( 2, "input: closing network target `%s'", p_input->p_source );
1015 close( p_input->i_handle );
1018 /*****************************************************************************
1019 * HTTPOpen : make an HTTP request
1020 *****************************************************************************/
1021 static void HTTPOpen( input_thread_t * p_input )
1023 char *psz_server = NULL;
1024 char *psz_path = NULL;
1028 struct sockaddr_in sock;
1029 char psz_buffer[256];
1031 /* Get the remote server */
1032 if( p_input->p_source != NULL )
1034 psz_server = p_input->p_source;
1036 /* Skip the protocol name */
1037 while( *psz_server && *psz_server != ':' )
1042 /* Skip the "://" part */
1043 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1048 /* Found a server name */
1051 char *psz_port = psz_server;
1053 /* Skip the hostname part */
1054 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1059 /* Found a port name */
1062 if( *psz_port == ':' )
1064 /* Replace ':' with '\0' */
1069 psz_path = psz_port;
1070 while( *psz_path && *psz_path != '/' )
1085 if( *psz_port != '\0' )
1087 i_port = atoi( psz_port );
1097 /* Check that we got a valid server */
1098 if( psz_server == NULL )
1100 intf_ErrMsg( "input error: No server given" );
1101 p_input->b_error = 1;
1105 /* Check that we got a valid port */
1108 i_port = 80; /* FIXME */
1111 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1114 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1116 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1117 if( p_input->i_handle == -1 )
1119 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1123 /* We may want to reuse an already used socket */
1125 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1126 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1128 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1130 close( p_input->i_handle );
1131 p_input->b_error = 1;
1136 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1138 /* http://myproxy.mydomain:myport/ */
1139 int i_proxy_port = 0;
1141 /* Skip the protocol name */
1142 while( *psz_proxy && *psz_proxy != ':' )
1147 /* Skip the "://" part */
1148 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1153 /* Found a proxy name */
1156 char *psz_port = psz_proxy;
1158 /* Skip the hostname part */
1159 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1164 /* Found a port name */
1169 /* Replace ':' with '\0' */
1173 psz_junk = psz_port;
1174 while( *psz_junk && *psz_junk != '/' )
1184 if( *psz_port != '\0' )
1186 i_proxy_port = atoi( psz_port );
1192 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1193 close( p_input->i_handle );
1194 p_input->b_error = 1;
1198 /* Build socket for proxy connection */
1199 if ( network_BuildAddr( &sock, psz_proxy, i_proxy_port ) == -1 )
1201 intf_ErrMsg( "input error: can't build remote address" );
1202 close( p_input->i_handle );
1203 p_input->b_error = 1;
1209 /* No proxy, direct connection */
1210 if ( network_BuildAddr( &sock, psz_server, i_port ) == -1 )
1212 intf_ErrMsg( "input error: can't build remote address" );
1213 close( p_input->i_handle );
1214 p_input->b_error = 1;
1219 /* Connect the socket */
1220 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1221 sizeof( sock ) ) == (-1) )
1223 intf_ErrMsg( "input error: can't connect socket (%s)",
1225 close( p_input->i_handle );
1226 p_input->b_error = 1;
1230 p_input->stream.b_seekable = 0;
1231 p_input->stream.b_pace_control = 1; /* TCP/IP... */
1233 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1234 # define HTTP_END "\r\n"
1236 /* Prepare GET ... */
1237 if( psz_proxy != NULL )
1239 snprintf( psz_buffer, sizeof(psz_buffer),
1240 "GET http://%s:%d/%s HTTP/1.0\r\n"
1241 HTTP_USERAGENT HTTP_END,
1242 psz_server, i_port, psz_path );
1246 snprintf( psz_buffer, sizeof(psz_buffer),
1247 "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1248 HTTP_USERAGENT HTTP_END,
1249 psz_path, psz_server );
1251 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1254 if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1256 intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1257 close( p_input->i_handle );
1258 p_input->b_error = 1;
1262 /* Read HTTP header - this is gonna be fun with plug-ins which do not
1263 * use p_input->p_stream :-( */
1264 if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1266 intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1267 close( p_input->i_handle );
1268 p_input->b_error = 1;
1272 while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1274 if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1275 || *psz_buffer == '\r' || *psz_buffer == '\0' )
1279 /* FIXME : check Content-Type one day */
1282 intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1285 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */