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.169 2002/01/09 02:01:14 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 )
118 /* Ask all remaining video outputs to die */
119 for( i_input = 0; i_input < p_input_bank->i_count; i_input++ )
122 p_input_bank->pp_input[ i_input ], NULL );
124 p_input_bank->pp_input[ i_input ] );
127 vlc_mutex_destroy( &p_input_bank->lock );
130 /*****************************************************************************
131 * input_CreateThread: creates a new input thread
132 *****************************************************************************
133 * This function creates a new input, and returns a pointer
134 * to its description. On error, it returns NULL.
135 * If pi_status is NULL, then the function will block until the thread is ready.
136 * If not, it will be updated using one of the THREAD_* constants.
137 *****************************************************************************/
138 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
140 input_thread_t * p_input; /* thread descriptor */
142 /* Allocate descriptor */
143 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
144 if( p_input == NULL )
146 intf_ErrMsg( "input error: can't allocate input thread (%s)",
151 /* Initialize thread properties */
153 p_input->b_error = 0;
157 p_input->p_source = p_item->psz_name;
160 p_input->i_status = THREAD_CREATE;
162 /* Initialize stream description */
163 p_input->stream.i_es_number = 0;
164 p_input->stream.i_selected_es_number = 0;
165 p_input->stream.i_pgrm_number = 0;
166 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
167 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
168 p_input->stream.i_mux_rate = 0;
170 /* no stream, no area */
171 p_input->stream.i_area_nb = 0;
172 p_input->stream.pp_areas = NULL;
173 p_input->stream.p_selected_area = NULL;
174 p_input->stream.p_new_area = NULL;
176 /* By default there is one area in a stream */
177 input_AddArea( p_input );
178 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
180 /* Initialize stream control properties. */
181 p_input->stream.control.i_status = PLAYING_S;
182 p_input->stream.control.i_rate = DEFAULT_RATE;
183 p_input->stream.control.b_mute = 0;
184 p_input->stream.control.b_grayscale = main_GetIntVariable(
185 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
186 p_input->stream.control.i_smp = main_GetIntVariable(
187 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
189 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
192 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
195 intf_ErrMsg( "input error: can't create input thread (%s)",
202 /* If status is NULL, wait until the thread is created */
203 if( pi_status == NULL )
207 msleep( THREAD_SLEEP );
208 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
209 && (i_status != THREAD_FATAL) );
216 /*****************************************************************************
217 * input_StopThread: mark an input thread as zombie
218 *****************************************************************************
219 * This function should not return until the thread is effectively cancelled.
220 *****************************************************************************/
221 void input_StopThread( input_thread_t *p_input, int *pi_status )
223 /* Make the thread exit from a possible vlc_cond_wait() */
224 vlc_mutex_lock( &p_input->stream.stream_lock );
226 /* Request thread destruction */
229 vlc_cond_signal( &p_input->stream.stream_wait );
230 vlc_mutex_unlock( &p_input->stream.stream_lock );
232 /* If status is NULL, wait until thread has been destroyed */
234 if( pi_status == NULL )
238 msleep( THREAD_SLEEP );
239 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
240 && (i_status != THREAD_FATAL) );
245 /*****************************************************************************
246 * input_DestroyThread: mark an input thread as zombie
247 *****************************************************************************
248 * This function should not return until the thread is effectively cancelled.
249 *****************************************************************************/
250 void input_DestroyThread( input_thread_t *p_input )
252 /* Join the thread */
253 vlc_thread_join( p_input->thread_id );
255 /* Destroy Mutex locks */
256 vlc_mutex_destroy( &p_input->stream.control.control_lock );
257 vlc_mutex_destroy( &p_input->stream.stream_lock );
259 /* Free input structure */
263 /*****************************************************************************
264 * RunThread: main thread loop
265 *****************************************************************************
266 * Thread in charge of processing the network packets and demultiplexing.
267 *****************************************************************************/
268 static void RunThread( input_thread_t *p_input )
270 if( InitThread( p_input ) )
272 /* If we failed, wait before we are killed, and exit */
273 p_input->i_status = THREAD_ERROR;
274 p_input->b_error = 1;
275 ErrorThread( p_input );
276 DestroyThread( p_input );
280 p_input->i_status = THREAD_READY;
282 /* initialization is complete */
283 vlc_mutex_lock( &p_input->stream.stream_lock );
284 p_input->stream.b_changed = 1;
285 vlc_mutex_unlock( &p_input->stream.stream_lock );
287 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
289 data_packet_t * p_data;
294 vlc_mutex_lock( &p_input->stream.stream_lock );
296 if( p_input->stream.p_new_program )
298 if( p_input->pf_set_program != NULL )
301 p_input->pf_set_program( p_input,
302 p_input->stream.p_new_program );
304 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
306 pgrm_descriptor_t * p_pgrm
307 = p_input->stream.pp_programs[i];
308 /* Escape all decoders for the stream discontinuity they
310 input_EscapeDiscontinuity( p_input, p_pgrm );
312 /* Reinitialize synchro. */
313 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
316 p_input->stream.p_new_program = NULL;
319 if( p_input->stream.p_new_area )
321 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
324 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
326 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
328 pgrm_descriptor_t * p_pgrm
329 = p_input->stream.pp_programs[i];
330 /* Escape all decoders for the stream discontinuity they
332 input_EscapeDiscontinuity( p_input, p_pgrm );
334 /* Reinitialize synchro. */
335 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
338 p_input->stream.p_new_area = NULL;
341 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
343 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
345 p_input->pf_seek( p_input,
346 p_input->stream.p_selected_area->i_seek );
348 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
350 pgrm_descriptor_t * p_pgrm
351 = p_input->stream.pp_programs[i];
352 /* Escape all decoders for the stream discontinuity they
354 input_EscapeDiscontinuity( p_input, p_pgrm );
356 /* Reinitialize synchro. */
357 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
360 p_input->stream.p_selected_area->i_seek = NO_SEEK;
363 if( p_input->stream.p_removed_es )
365 input_UnselectES( p_input, p_input->stream.p_removed_es );
366 p_input->stream.p_removed_es = NULL;
369 if( p_input->stream.p_newly_selected_es )
371 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
372 p_input->stream.p_newly_selected_es = NULL;
375 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
377 if( p_input->stream.b_new_mute )
379 input_EscapeAudioDiscontinuity( p_input );
382 vlc_mutex_lock( &p_input->stream.control.control_lock );
383 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
384 vlc_mutex_unlock( &p_input->stream.control.control_lock );
386 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
389 vlc_mutex_unlock( &p_input->stream.stream_lock );
391 i_count = p_input->pf_read( p_input, &p_data );
393 /* Demultiplex read packets. */
394 while( p_data != NULL )
396 data_packet_t * p_next = p_data->p_next;
397 p_data->p_next = NULL;
399 p_input->stream.c_packets_read++;
400 p_input->pf_demux( p_input, p_data );
405 if( i_count == 0 && p_input->stream.b_seekable )
407 /* End of file - we do not set b_die because only the
408 * interface is allowed to do so. */
409 intf_WarnMsg( 3, "input: EOF reached" );
412 else if( i_count < 0 )
414 p_input->b_error = 1;
418 if( p_input->b_error || p_input->b_eof )
420 ErrorThread( p_input );
423 EndThread( p_input );
425 DestroyThread( p_input );
427 intf_DbgMsg("input: Thread end");
430 /*****************************************************************************
431 * InitThread: init the input Thread
432 *****************************************************************************/
433 static int InitThread( input_thread_t * p_input )
435 /* Initialize statistics */
436 p_input->c_loops = 0;
437 p_input->stream.c_packets_read = 0;
438 p_input->stream.c_packets_trashed = 0;
439 p_input->p_stream = NULL;
442 vlc_mutex_init( &p_input->stream.stream_lock );
443 vlc_cond_init( &p_input->stream.stream_wait );
444 vlc_mutex_init( &p_input->stream.control.control_lock );
446 /* Find appropriate module. */
447 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
448 main_GetPszVariable( INPUT_METHOD_VAR, NULL ),
449 (probedata_t *)p_input );
451 if( p_input->p_input_module == NULL )
453 intf_ErrMsg( "input error: no suitable input module for `%s'",
458 #define f p_input->p_input_module->p_functions->input.functions.input
459 p_input->pf_init = f.pf_init;
460 p_input->pf_end = f.pf_end;
461 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
462 p_input->pf_read = f.pf_read;
463 p_input->pf_set_area = f.pf_set_area;
464 p_input->pf_set_program = f.pf_set_program;
465 p_input->pf_demux = f.pf_demux;
466 p_input->pf_new_packet = f.pf_new_packet;
467 p_input->pf_new_pes = f.pf_new_pes;
468 p_input->pf_delete_packet = f.pf_delete_packet;
469 p_input->pf_delete_pes = f.pf_delete_pes;
470 p_input->pf_rewind = f.pf_rewind;
471 p_input->pf_seek = f.pf_seek;
473 if( f.pf_open != NULL )
475 f.pf_open( p_input );
476 p_input->stream.i_method = INPUT_METHOD_DVD;
478 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
479 /* FIXME : this is waaaay too kludgy */
480 else if( ( strlen( p_input->p_source ) >= 10
481 && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
482 || ( strlen( p_input->p_source ) >= 4
483 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
486 NetworkOpen( p_input );
487 p_input->stream.i_method = INPUT_METHOD_NETWORK;
489 else if( ( strlen( p_input->p_source ) > 5 )
490 && !strncasecmp( p_input->p_source, "http:", 5 ) )
494 p_input->stream.i_method = INPUT_METHOD_NETWORK;
497 else if( ( strlen( p_input->p_source ) == 1 )
498 && *p_input->p_source == '-' )
507 p_input->stream.i_method = INPUT_METHOD_FILE;
511 if( p_input->b_error )
513 /* We barfed -- exit nicely */
514 module_Unneed( p_input->p_input_module );
518 p_input->pf_init( p_input );
520 if( p_input->b_error )
522 /* We barfed -- exit nicely */
523 CloseThread( p_input );
524 module_Unneed( p_input->p_input_module );
531 /*****************************************************************************
532 * ErrorThread: RunThread() error loop
533 *****************************************************************************
534 * This function is called when an error occured during thread main's loop.
535 *****************************************************************************/
536 static void ErrorThread( input_thread_t *p_input )
538 while( !p_input->b_die )
541 msleep( INPUT_IDLE_SLEEP );
545 /*****************************************************************************
546 * EndThread: end the input thread
547 *****************************************************************************/
548 static void EndThread( input_thread_t * p_input )
551 p_input->i_status = THREAD_END;
553 if( p_main->b_stats )
555 #ifdef HAVE_SYS_TIMES_H
556 /* Display statistics */
557 struct tms cpu_usage;
560 intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
562 cpu_usage.tms_utime, cpu_usage.tms_stime );
564 intf_StatMsg( "input stats: %d loops", p_input->c_loops );
567 input_DumpStream( p_input );
570 /* Free all ES and destroy all decoder threads */
571 input_EndStream( p_input );
573 /* Free demultiplexer's data */
574 p_input->pf_end( p_input );
576 /* Close the input method */
577 CloseThread( p_input );
579 /* Release modules */
580 module_Unneed( p_input->p_input_module );
583 /*****************************************************************************
584 * CloseThread: close the target
585 *****************************************************************************/
586 static void CloseThread( input_thread_t * p_input )
588 #define f p_input->p_input_module->p_functions->input.functions.input
590 if( f.pf_close != NULL )
592 f.pf_close( p_input );
594 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
596 else if( ( strlen( p_input->p_source ) > 10
597 && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
598 || ( strlen( p_input->p_source ) > 4
599 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
601 NetworkClose( p_input );
603 else if( ( strlen( p_input->p_source ) > 5 )
604 && !strncasecmp( p_input->p_source, "http:", 5 ) )
606 NetworkClose( p_input );
611 FileClose( p_input );
616 /*****************************************************************************
617 * DestroyThread: destroy the input thread
618 *****************************************************************************/
619 static void DestroyThread( input_thread_t * p_input )
622 p_input->i_status = THREAD_OVER;
625 /*****************************************************************************
626 * StdOpen : open standard input
627 *****************************************************************************/
628 static void StdOpen( input_thread_t * p_input )
630 vlc_mutex_lock( &p_input->stream.stream_lock );
632 /* Suppose we can control the pace - this won't work in some cases ! */
633 p_input->stream.b_pace_control = 1;
635 p_input->stream.b_seekable = 0;
636 p_input->stream.p_selected_area->i_size = 0;
637 p_input->stream.p_selected_area->i_tell = 0;
638 vlc_mutex_unlock( &p_input->stream.stream_lock );
640 intf_WarnMsg( 2, "input: opening stdin" );
641 p_input->i_handle = 0;
644 /*****************************************************************************
645 * FileOpen : open a file descriptor
646 *****************************************************************************/
647 static void FileOpen( input_thread_t * p_input )
649 struct stat stat_info;
652 char *psz_name = p_input->p_source;
654 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
656 int i_size = strlen( psz_name );
659 && !strncasecmp( psz_name, "dvdread:", 8 ) )
661 /* get rid of the 'dvdread:' stuff and try again */
663 i_stat = stat( psz_name, &stat_info );
665 else if( ( i_size > 4 )
666 && !strncasecmp( psz_name, "dvd:", 4 ) )
668 /* get rid of the 'dvd:' stuff and try again */
670 i_stat = stat( psz_name, &stat_info );
672 else if( ( i_size > 4 )
673 && !strncasecmp( psz_name, "vcd:", 4 ) )
675 /* get rid of the 'vcd:' stuff and try again */
677 i_stat = stat( psz_name, &stat_info );
679 else if( ( i_size > 5 )
680 && !strncasecmp( psz_name, "file:", 5 ) )
682 /* get rid of the 'file:' stuff and try again */
684 i_stat = stat( psz_name, &stat_info );
689 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
690 psz_name, strerror(errno));
691 p_input->b_error = 1;
696 vlc_mutex_lock( &p_input->stream.stream_lock );
698 /* If we are here we can control the pace... */
699 p_input->stream.b_pace_control = 1;
701 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
702 || S_ISBLK(stat_info.st_mode) )
704 p_input->stream.b_seekable = 1;
705 p_input->stream.p_selected_area->i_size = stat_info.st_size;
707 else if( S_ISFIFO(stat_info.st_mode)
708 #if !defined( SYS_BEOS ) && !defined( WIN32 )
709 || S_ISSOCK(stat_info.st_mode)
713 p_input->stream.b_seekable = 0;
714 p_input->stream.p_selected_area->i_size = 0;
718 vlc_mutex_unlock( &p_input->stream.stream_lock );
719 intf_ErrMsg( "input error: unknown file type for `%s'",
721 p_input->b_error = 1;
725 p_input->stream.p_selected_area->i_tell = 0;
726 vlc_mutex_unlock( &p_input->stream.stream_lock );
728 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
729 if( (p_input->i_handle = open( psz_name,
730 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
732 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
733 p_input->b_error = 1;
739 /*****************************************************************************
740 * FileClose : close a file descriptor
741 *****************************************************************************/
742 static void FileClose( input_thread_t * p_input )
744 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
746 close( p_input->i_handle );
751 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
752 /*****************************************************************************
753 * NetworkOpen : open a network socket
754 *****************************************************************************/
755 static void NetworkOpen( input_thread_t * p_input )
757 char *psz_server = NULL;
758 char *psz_bind = NULL;
759 int i_server_port = 0;
763 struct sockaddr_in sock;
765 /* Get the remote server. Syntax is :
766 * udp[stream]:[/][/][serveraddr[:serverport]][@[bindaddr]:[bindport]] */
767 if( p_input->p_source != NULL )
769 char * psz_parser = p_input->p_source;
770 char * psz_server_port = NULL;
771 char * psz_bind_port = NULL;
773 /* Skip the protocol name */
774 while( *psz_parser && *psz_parser != ':' )
779 /* Skip the "://" part */
780 while( *psz_parser && (*psz_parser == ':' || *psz_parser == '/') )
785 if( *psz_parser && *psz_parser != '@' )
788 psz_server = psz_parser;
790 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
795 if( *psz_parser == ':' )
797 /* Found server port */
798 *psz_parser = '\0'; /* Terminate server name */
800 psz_server_port = psz_parser;
802 while( *psz_parser && *psz_parser != '@' )
809 if( *psz_parser == '@' )
811 /* Found bind address or bind port */
812 *psz_parser = '\0'; /* Terminate server port or name if necessary */
815 if( *psz_parser && *psz_parser != ':' )
817 /* Found bind address */
818 psz_bind = psz_parser;
820 while( *psz_parser && *psz_parser != ':' )
826 if( *psz_parser == ':' )
828 /* Found bind port */
829 *psz_parser = '\0'; /* Terminate bind address if necessary */
832 psz_bind_port = psz_parser;
836 /* Convert ports format */
837 if( psz_server_port != NULL )
839 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
842 intf_ErrMsg( "input error: cannot parse server port near %s",
844 p_input->b_error = 1;
849 if( psz_bind_port != NULL )
851 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
854 intf_ErrMsg( "input error: cannot parse bind port near %s",
856 p_input->b_error = 1;
863 /* This is required or NetworkClose will never be called */
864 p_input->p_source = "ts: network input";
867 /* Check that we got a valid port */
868 if( i_bind_port == 0 )
870 i_bind_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
873 intf_WarnMsg( 2, "input: server=%s:%d local=%s:%d",
874 psz_server, i_server_port, psz_bind, i_bind_port );
876 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
878 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
879 if( p_input->i_handle == -1 )
881 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
882 p_input->b_error = 1;
886 /* We may want to reuse an already used socket */
888 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
889 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
891 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
893 close( p_input->i_handle );
894 p_input->b_error = 1;
898 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
899 * packet loss caused by scheduling problems */
901 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
902 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
904 intf_WarnMsg( 1, "input warning: can't configure socket (SO_RCVBUF: %s)",
908 /* Check if we really got what we have asked for, because Linux, etc.
909 * will silently limit the max buffer size to net.core.rmem_max which
910 * is typically only 65535 bytes */
912 i_opt_size = sizeof( i_opt );
913 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
914 (void*) &i_opt, &i_opt_size ) == -1 )
916 intf_WarnMsg( 1, "input warning: can't query socket (SO_RCVBUF: %s)",
919 else if( i_opt < 0x80000 )
921 intf_WarnMsg( 1, "input warning: socket buffer size is 0x%x"
922 " instead of 0x%x", i_opt, 0x80000 );
925 /* Build the local socket */
926 if ( network_BuildAddr( &sock, psz_bind, i_bind_port ) == -1 )
928 intf_ErrMsg( "input error: can't build local address" );
929 close( p_input->i_handle );
930 p_input->b_error = 1;
935 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
936 sizeof( sock ) ) < 0 )
938 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
939 close( p_input->i_handle );
940 p_input->b_error = 1;
944 /* Allow broadcast reception if we bound on INADDR_ANY */
945 if( psz_bind == NULL )
948 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_BROADCAST,
949 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
951 intf_WarnMsg( 1, "input warning: can't configure socket (SO_BROADCAST: %s)",
956 /* Join the multicast group if the socket is a multicast address */
958 # define IN_MULTICAST(a) IN_CLASSD(a)
961 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
965 imr.imr_interface.s_addr = INADDR_ANY;
966 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
967 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
968 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
970 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
972 close( p_input->i_handle );
973 p_input->b_error = 1;
978 if( psz_server != NULL )
980 /* Build socket for remote connection */
981 if ( network_BuildAddr( &sock, psz_server, i_server_port ) == -1 )
983 intf_ErrMsg( "input error: can't build remote address" );
984 close( p_input->i_handle );
985 p_input->b_error = 1;
989 /* Connect the socket */
990 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
991 sizeof( sock ) ) == (-1) )
993 intf_ErrMsg( "input error: can't connect socket (%s)",
995 close( p_input->i_handle );
996 p_input->b_error = 1;
1001 p_input->stream.b_pace_control = 0;
1002 p_input->stream.b_seekable = 0;
1004 intf_WarnMsg( 3, "input: successfully opened network mode" );
1009 /*****************************************************************************
1010 * NetworkClose : close a network socket
1011 *****************************************************************************/
1012 static void NetworkClose( input_thread_t * p_input )
1014 intf_WarnMsg( 2, "input: closing network target `%s'", p_input->p_source );
1016 close( p_input->i_handle );
1019 /*****************************************************************************
1020 * HTTPOpen : make an HTTP request
1021 *****************************************************************************/
1022 static void HTTPOpen( input_thread_t * p_input )
1024 char *psz_server = NULL;
1025 char *psz_path = NULL;
1029 struct sockaddr_in sock;
1030 char psz_buffer[256];
1032 /* Get the remote server */
1033 if( p_input->p_source != NULL )
1035 psz_server = p_input->p_source;
1037 /* Skip the protocol name */
1038 while( *psz_server && *psz_server != ':' )
1043 /* Skip the "://" part */
1044 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1049 /* Found a server name */
1052 char *psz_port = psz_server;
1054 /* Skip the hostname part */
1055 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1060 /* Found a port name */
1063 if( *psz_port == ':' )
1065 /* Replace ':' with '\0' */
1070 psz_path = psz_port;
1071 while( *psz_path && *psz_path != '/' )
1086 if( *psz_port != '\0' )
1088 i_port = atoi( psz_port );
1098 /* Check that we got a valid server */
1099 if( psz_server == NULL )
1101 intf_ErrMsg( "input error: No server given" );
1102 p_input->b_error = 1;
1106 /* Check that we got a valid port */
1109 i_port = 80; /* FIXME */
1112 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1115 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1117 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1118 if( p_input->i_handle == -1 )
1120 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1124 /* We may want to reuse an already used socket */
1126 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1127 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1129 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1131 close( p_input->i_handle );
1132 p_input->b_error = 1;
1137 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1139 /* http://myproxy.mydomain:myport/ */
1140 int i_proxy_port = 0;
1142 /* Skip the protocol name */
1143 while( *psz_proxy && *psz_proxy != ':' )
1148 /* Skip the "://" part */
1149 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1154 /* Found a proxy name */
1157 char *psz_port = psz_proxy;
1159 /* Skip the hostname part */
1160 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1165 /* Found a port name */
1170 /* Replace ':' with '\0' */
1174 psz_junk = psz_port;
1175 while( *psz_junk && *psz_junk != '/' )
1185 if( *psz_port != '\0' )
1187 i_proxy_port = atoi( psz_port );
1193 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1194 close( p_input->i_handle );
1195 p_input->b_error = 1;
1199 /* Build socket for proxy connection */
1200 if ( network_BuildAddr( &sock, psz_proxy, i_proxy_port ) == -1 )
1202 intf_ErrMsg( "input error: can't build remote address" );
1203 close( p_input->i_handle );
1204 p_input->b_error = 1;
1210 /* No proxy, direct connection */
1211 if ( network_BuildAddr( &sock, psz_server, i_port ) == -1 )
1213 intf_ErrMsg( "input error: can't build remote address" );
1214 close( p_input->i_handle );
1215 p_input->b_error = 1;
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 ) */