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.162 2001/12/07 16:47:47 jobi Exp $
9 * Authors: Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <sys/types.h>
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
45 #ifdef STRNCASECMP_IN_STRINGS_H
50 # include <winsock2.h>
51 # 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>
74 #include "intf_playlist.h"
76 #include "stream_control.h"
77 #include "input_ext-intf.h"
78 #include "input_ext-dec.h"
79 #include "input_ext-plugins.h"
81 #include "interface.h"
85 /*****************************************************************************
87 *****************************************************************************/
88 static void RunThread ( input_thread_t *p_input );
89 static int InitThread ( input_thread_t *p_input );
90 static void ErrorThread ( input_thread_t *p_input );
91 static void CloseThread ( input_thread_t *p_input );
92 static void DestroyThread ( input_thread_t *p_input );
93 static void EndThread ( input_thread_t *p_input );
95 static void FileOpen ( input_thread_t *p_input );
96 static void StdOpen ( input_thread_t *p_input );
97 static void FileClose ( input_thread_t *p_input );
98 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
99 static void NetworkOpen ( input_thread_t *p_input );
100 static void HTTPOpen ( input_thread_t *p_input );
101 static void NetworkClose ( input_thread_t *p_input );
104 /*****************************************************************************
105 * input_CreateThread: creates a new input thread
106 *****************************************************************************
107 * This function creates a new input, and returns a pointer
108 * to its description. On error, it returns NULL.
109 * If pi_status is NULL, then the function will block until the thread is ready.
110 * If not, it will be updated using one of the THREAD_* constants.
111 *****************************************************************************/
112 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
114 input_thread_t * p_input; /* thread descriptor */
115 int i_status; /* thread status */
117 /* Allocate descriptor */
118 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
119 if( p_input == NULL )
121 intf_ErrMsg( "input error: can't allocate input thread (%s)",
126 /* Packets read once */
127 p_input->i_read_once = INPUT_READ_ONCE;
129 /* Initialize thread properties */
131 p_input->b_error = 0;
135 p_input->p_source = p_item->psz_name;
137 /* I have never understood that stuff --Meuuh */
138 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
139 *p_input->pi_status = THREAD_CREATE;
141 /* Initialize stream description */
142 p_input->stream.i_es_number = 0;
143 p_input->stream.i_selected_es_number = 0;
144 p_input->stream.i_pgrm_number = 0;
145 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
146 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
147 p_input->stream.i_mux_rate = 0;
149 /* no stream, no area */
150 p_input->stream.i_area_nb = 0;
151 p_input->stream.pp_areas = NULL;
152 p_input->stream.p_selected_area = NULL;
153 p_input->stream.p_new_area = NULL;
155 /* By default there is one area in a stream */
156 input_AddArea( p_input );
157 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
159 /* Initialize stream control properties. */
160 p_input->stream.control.i_status = PLAYING_S;
161 p_input->stream.control.i_rate = DEFAULT_RATE;
162 p_input->stream.control.b_mute = 0;
163 p_input->stream.control.b_grayscale = main_GetIntVariable(
164 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
165 p_input->stream.control.i_smp = main_GetIntVariable(
166 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
168 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
171 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
174 intf_ErrMsg( "input error: can't create input thread (%s)",
180 /* If status is NULL, wait until the thread is created */
181 if( pi_status == NULL )
185 msleep( THREAD_SLEEP );
186 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
187 && (i_status != THREAD_FATAL) );
188 if( i_status != THREAD_READY )
196 /*****************************************************************************
197 * input_DestroyThread: mark an input thread as zombie
198 *****************************************************************************
199 * This function should not return until the thread is effectively cancelled.
200 *****************************************************************************/
201 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
203 int i_status; /* thread status */
206 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
207 *p_input->pi_status = THREAD_DESTROY;
209 /* Request thread destruction */
212 /* Make the thread exit of an eventual vlc_cond_wait() */
213 vlc_mutex_lock( &p_input->stream.stream_lock );
214 vlc_cond_signal( &p_input->stream.stream_wait );
215 vlc_mutex_unlock( &p_input->stream.stream_lock );
217 /* If status is NULL, wait until thread has been destroyed */
218 if( pi_status == NULL )
222 msleep( THREAD_SLEEP );
223 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
224 && (i_status != THREAD_FATAL) );
228 /*****************************************************************************
229 * RunThread: main thread loop
230 *****************************************************************************
231 * Thread in charge of processing the network packets and demultiplexing.
232 *****************************************************************************/
233 static void RunThread( input_thread_t *p_input )
236 data_packet_t ** pp_packets;
238 if( InitThread( p_input ) )
240 /* If we failed, wait before we are killed, and exit */
241 *p_input->pi_status = THREAD_ERROR;
242 p_input->b_error = 1;
243 ErrorThread( p_input );
244 DestroyThread( p_input );
248 /* initialization is completed */
249 vlc_mutex_lock( &p_input->stream.stream_lock );
250 p_input->stream.b_changed = 1;
251 vlc_mutex_unlock( &p_input->stream.stream_lock );
253 pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
254 sizeof( data_packet_t * ) );
255 if( pp_packets == NULL )
257 intf_ErrMsg( "input error: out of memory" );
259 p_input->b_error = 1;
262 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
266 vlc_mutex_lock( &p_input->stream.stream_lock );
268 if( p_input->stream.p_new_program )
270 if( p_input->pf_set_program != NULL )
273 p_input->pf_set_program( p_input,
274 p_input->stream.p_new_program );
276 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
278 pgrm_descriptor_t * p_pgrm
279 = p_input->stream.pp_programs[i];
280 /* Escape all decoders for the stream discontinuity they
282 input_EscapeDiscontinuity( p_input, p_pgrm );
284 /* Reinitialize synchro. */
285 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
288 p_input->stream.p_new_program = NULL;
291 if( p_input->stream.p_new_area )
293 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
296 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
298 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
300 pgrm_descriptor_t * p_pgrm
301 = p_input->stream.pp_programs[i];
302 /* Escape all decoders for the stream discontinuity they
304 input_EscapeDiscontinuity( p_input, p_pgrm );
306 /* Reinitialize synchro. */
307 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
310 p_input->stream.p_new_area = NULL;
313 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
315 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
317 p_input->pf_seek( p_input,
318 p_input->stream.p_selected_area->i_seek );
320 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
322 pgrm_descriptor_t * p_pgrm
323 = p_input->stream.pp_programs[i];
324 /* Escape all decoders for the stream discontinuity they
326 input_EscapeDiscontinuity( p_input, p_pgrm );
328 /* Reinitialize synchro. */
329 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
332 p_input->stream.p_selected_area->i_seek = NO_SEEK;
335 if( p_input->stream.p_removed_es )
337 input_UnselectES( p_input, p_input->stream.p_removed_es );
338 p_input->stream.p_removed_es = NULL;
341 if( p_input->stream.p_newly_selected_es )
343 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
344 p_input->stream.p_newly_selected_es = NULL;
347 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
349 if( p_input->stream.b_new_mute )
351 input_EscapeAudioDiscontinuity( p_input );
354 vlc_mutex_lock( &p_input->stream.control.control_lock );
355 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
356 vlc_mutex_unlock( &p_input->stream.control.control_lock );
358 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
361 vlc_mutex_unlock( &p_input->stream.stream_lock );
363 i_error = p_input->pf_read( p_input, pp_packets );
365 /* Demultiplex read packets. */
366 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
368 p_input->stream.c_packets_read++;
369 p_input->pf_demux( p_input, pp_packets[i] );
376 /* End of file - we do not set b_die because only the
377 * interface is allowed to do so. */
378 intf_WarnMsg( 3, "input: EOF reached" );
383 p_input->b_error = 1;
390 if( p_input->b_error || p_input->b_eof )
392 ErrorThread( p_input );
395 EndThread( p_input );
397 DestroyThread( p_input );
399 intf_DbgMsg("input: Thread end");
402 /*****************************************************************************
403 * InitThread: init the input Thread
404 *****************************************************************************/
405 static int InitThread( input_thread_t * p_input )
408 /* Initialize statistics */
409 p_input->c_loops = 0;
410 p_input->stream.c_packets_read = 0;
411 p_input->stream.c_packets_trashed = 0;
412 p_input->p_stream = NULL;
415 vlc_mutex_init( &p_input->stream.stream_lock );
416 vlc_cond_init( &p_input->stream.stream_wait );
417 vlc_mutex_init( &p_input->stream.control.control_lock );
419 /* Find appropriate module. */
420 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
421 (probedata_t *)p_input );
423 if( p_input->p_input_module == NULL )
425 intf_ErrMsg( "input error: no suitable input module for `%s'",
430 #define f p_input->p_input_module->p_functions->input.functions.input
431 p_input->pf_init = f.pf_init;
432 p_input->pf_end = f.pf_end;
433 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
434 p_input->pf_read = f.pf_read;
435 p_input->pf_set_area = f.pf_set_area;
436 p_input->pf_set_program = f.pf_set_program;
437 p_input->pf_demux = f.pf_demux;
438 p_input->pf_new_packet = f.pf_new_packet;
439 p_input->pf_new_pes = f.pf_new_pes;
440 p_input->pf_delete_packet = f.pf_delete_packet;
441 p_input->pf_delete_pes = f.pf_delete_pes;
442 p_input->pf_rewind = f.pf_rewind;
443 p_input->pf_seek = f.pf_seek;
445 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
446 /* FIXME : this is waaaay too kludgy */
447 if( ( strlen( p_input->p_source ) >= 10
448 && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
449 || ( strlen( p_input->p_source ) >= 4
450 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
453 NetworkOpen( p_input );
454 p_input->stream.i_method = INPUT_METHOD_NETWORK;
456 else if( ( strlen( p_input->p_source ) > 5 )
457 && !strncasecmp( p_input->p_source, "http:", 5 ) )
461 p_input->stream.i_method = INPUT_METHOD_NETWORK;
465 if( ( ( strlen( p_input->p_source ) > 4 )
466 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
467 || TestMethod( INPUT_METHOD_VAR, "dvd" ) )
469 /* DVD - this is THE kludge */
470 f.pf_open( p_input );
471 p_input->stream.i_method = INPUT_METHOD_DVD;
473 else if( ( ( strlen( p_input->p_source ) > 8 )
474 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
475 || TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
477 /* DVDRead - this is THE kludge */
478 f.pf_open( p_input );
479 p_input->stream.i_method = INPUT_METHOD_DVD;
481 else if( ( strlen( p_input->p_source ) > 4 )
482 && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
484 /* Dummy input - very kludgy */
485 f.pf_open( p_input );
487 else if( ( strlen( p_input->p_source ) == 1 )
488 && *p_input->p_source == '-' )
497 p_input->stream.i_method = INPUT_METHOD_FILE;
501 if( p_input->b_error )
503 /* We barfed -- exit nicely */
504 module_Unneed( p_input->p_input_module );
508 p_input->pf_init( p_input );
510 if( p_input->b_error )
512 /* We barfed -- exit nicely */
513 CloseThread( p_input );
514 module_Unneed( p_input->p_input_module );
518 *p_input->pi_status = THREAD_READY;
523 /*****************************************************************************
524 * ErrorThread: RunThread() error loop
525 *****************************************************************************
526 * This function is called when an error occured during thread main's loop.
527 *****************************************************************************/
528 static void ErrorThread( input_thread_t *p_input )
530 while( !p_input->b_die )
533 msleep( INPUT_IDLE_SLEEP );
537 /*****************************************************************************
538 * EndThread: end the input thread
539 *****************************************************************************/
540 static void EndThread( input_thread_t * p_input )
542 int * pi_status; /* thread status */
545 pi_status = p_input->pi_status;
546 *pi_status = THREAD_END;
548 if( p_main->b_stats )
550 #ifdef HAVE_SYS_TIMES_H
551 /* Display statistics */
552 struct tms cpu_usage;
555 intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
557 cpu_usage.tms_utime, cpu_usage.tms_stime );
559 intf_StatMsg( "input stats: %d loops", p_input->c_loops );
562 input_DumpStream( p_input );
565 /* Free all ES and destroy all decoder threads */
566 input_EndStream( p_input );
568 /* Free demultiplexer's data */
569 p_input->pf_end( p_input );
571 /* Close the input method */
572 CloseThread( p_input );
574 /* Release modules */
575 module_Unneed( p_input->p_input_module );
578 /*****************************************************************************
579 * CloseThread: close the target
580 *****************************************************************************/
581 static void CloseThread( input_thread_t * p_input )
583 #define f p_input->p_input_module->p_functions->input.functions.input
585 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
587 if( ( strlen( p_input->p_source ) > 10
588 && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
589 || ( strlen( p_input->p_source ) > 4
590 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
592 NetworkClose( p_input );
594 else if( ( strlen( p_input->p_source ) > 5 )
595 && !strncasecmp( p_input->p_source, "http:", 5 ) )
597 NetworkClose( p_input );
601 if( ( ( strlen( p_input->p_source ) > 4 )
602 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
603 || TestMethod( INPUT_METHOD_VAR, "dvd" ) )
605 f.pf_close( p_input );
607 else if( ( strlen( p_input->p_source ) > 4 )
608 && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
610 f.pf_close( p_input );
614 FileClose( p_input );
619 /*****************************************************************************
620 * DestroyThread: destroy the input thread
621 *****************************************************************************/
622 static void DestroyThread( input_thread_t * p_input )
624 int * pi_status; /* thread status */
627 pi_status = p_input->pi_status;
629 /* Destroy Mutex locks */
630 vlc_mutex_destroy( &p_input->stream.control.control_lock );
631 vlc_mutex_destroy( &p_input->stream.stream_lock );
633 /* Free input structure */
637 *pi_status = THREAD_OVER;
640 /*****************************************************************************
641 * StdOpen : open standard input
642 *****************************************************************************/
643 static void StdOpen( input_thread_t * p_input )
645 vlc_mutex_lock( &p_input->stream.stream_lock );
647 /* Suppose we can control the pace - this won't work in some cases ! */
648 p_input->stream.b_pace_control = 1;
650 p_input->stream.b_seekable = 0;
651 p_input->stream.p_selected_area->i_size = 0;
652 p_input->stream.p_selected_area->i_tell = 0;
653 vlc_mutex_unlock( &p_input->stream.stream_lock );
655 intf_WarnMsg( 2, "input: opening stdin" );
656 p_input->i_handle = 0;
659 /*****************************************************************************
660 * FileOpen : open a file descriptor
661 *****************************************************************************/
662 static void FileOpen( input_thread_t * p_input )
664 struct stat stat_info;
667 char *psz_name = p_input->p_source;
669 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
671 int i_size = strlen( psz_name );
674 && !strncasecmp( psz_name, "dvdread:", 8 ) )
676 /* get rid of the 'dvdread:' stuff and try again */
678 i_stat = stat( psz_name, &stat_info );
680 else if( ( i_size > 4 )
681 && !strncasecmp( psz_name, "dvd:", 4 ) )
683 /* get rid of the 'dvd:' stuff and try again */
685 i_stat = stat( psz_name, &stat_info );
687 else if( ( i_size > 4 )
688 && !strncasecmp( psz_name, "vcd:", 4 ) )
690 /* get rid of the 'vcd:' stuff and try again */
692 i_stat = stat( psz_name, &stat_info );
694 else if( ( i_size > 5 )
695 && !strncasecmp( psz_name, "file:", 5 ) )
697 /* get rid of the 'file:' stuff and try again */
699 i_stat = stat( psz_name, &stat_info );
704 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
705 psz_name, strerror(errno));
706 p_input->b_error = 1;
711 vlc_mutex_lock( &p_input->stream.stream_lock );
713 /* If we are here we can control the pace... */
714 p_input->stream.b_pace_control = 1;
716 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
717 || S_ISBLK(stat_info.st_mode) )
719 p_input->stream.b_seekable = 1;
720 p_input->stream.p_selected_area->i_size = stat_info.st_size;
722 else if( S_ISFIFO(stat_info.st_mode)
723 #if !defined( SYS_BEOS ) && !defined( WIN32 )
724 || S_ISSOCK(stat_info.st_mode)
728 p_input->stream.b_seekable = 0;
729 p_input->stream.p_selected_area->i_size = 0;
733 vlc_mutex_unlock( &p_input->stream.stream_lock );
734 intf_ErrMsg( "input error: unknown file type for `%s'",
736 p_input->b_error = 1;
740 p_input->stream.p_selected_area->i_tell = 0;
741 vlc_mutex_unlock( &p_input->stream.stream_lock );
743 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
744 if( (p_input->i_handle = open( psz_name,
745 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
747 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
748 p_input->b_error = 1;
754 /*****************************************************************************
755 * FileClose : close a file descriptor
756 *****************************************************************************/
757 static void FileClose( input_thread_t * p_input )
759 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
761 close( p_input->i_handle );
766 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
767 /*****************************************************************************
768 * NetworkOpen : open a network socket
769 *****************************************************************************/
770 static void NetworkOpen( input_thread_t * p_input )
772 char *psz_server = NULL;
773 char *psz_bind = NULL;
774 int i_server_port = 0;
778 struct sockaddr_in sock;
780 /* Get the remote server. Syntax is :
781 * udp[stream]:[/][/][serveraddr[:serverport]][@[bindaddr]:[bindport]] */
782 if( p_input->p_source != NULL )
784 char * psz_parser = p_input->p_source;
785 char * psz_server_port = NULL;
786 char * psz_bind_port = NULL;
788 /* Skip the protocol name */
789 while( *psz_parser && *psz_parser != ':' )
794 /* Skip the "://" part */
795 while( *psz_parser && (*psz_parser == ':' || *psz_parser == '/') )
800 if( *psz_parser && *psz_parser != '@' )
803 psz_server = psz_parser;
805 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
810 if( *psz_parser == ':' )
812 /* Found server port */
813 *psz_parser = '\0'; /* Terminate server name */
815 psz_server_port = psz_parser;
817 while( *psz_parser && *psz_parser != '@' )
824 if( *psz_parser == '@' )
826 /* Found bind address or bind port */
827 *psz_parser = '\0'; /* Terminate server port or name if necessary */
830 if( *psz_parser && *psz_parser != ':' )
832 /* Found bind address */
833 psz_bind = psz_parser;
835 while( *psz_parser && *psz_parser != ':' )
841 if( *psz_parser == ':' )
843 /* Found bind port */
844 *psz_parser = '\0'; /* Terminate bind address if necessary */
847 psz_bind_port = psz_parser;
851 /* Convert ports format */
852 if( psz_server_port != NULL )
854 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
857 intf_ErrMsg( "input error: cannot parse server port near %s",
859 p_input->b_error = 1;
864 if( psz_bind_port != NULL )
866 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
869 intf_ErrMsg( "input error: cannot parse bind port near %s",
871 p_input->b_error = 1;
878 /* This is required or NetworkClose will never be called */
879 p_input->p_source = "ts: network input";
882 /* Check that we got a valid port */
883 if( i_bind_port == 0 )
885 i_bind_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
888 intf_WarnMsg( 2, "input: server=%s:%d local=%s:%d",
889 psz_server, i_server_port, psz_bind, i_bind_port );
891 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
893 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
894 if( p_input->i_handle == -1 )
896 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
897 p_input->b_error = 1;
901 /* We may want to reuse an already used socket */
903 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
904 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
906 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
908 close( p_input->i_handle );
909 p_input->b_error = 1;
913 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
914 * packet loss caused by scheduling problems */
916 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
917 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
919 intf_WarnMsg( 1, "input warning: can't configure socket (SO_RCVBUF: %s)",
923 /* Check if we really got what we have asked for, because Linux, etc.
924 * will silently limit the max buffer size to net.core.rmem_max which
925 * is typically only 65535 bytes */
927 i_opt_size = sizeof( i_opt );
928 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
929 (void*) &i_opt, &i_opt_size ) == -1 )
931 intf_WarnMsg( 1, "input warning: can't query socket (SO_RCVBUF: %s)",
934 else if( i_opt < 0x80000 )
936 intf_WarnMsg( 1, "input warning: socket buffer size is 0x%x"
937 " instead of 0x%x", i_opt, 0x80000 );
940 /* Build the local socket */
941 if ( network_BuildAddr( &sock, psz_bind, i_bind_port ) == -1 )
943 intf_ErrMsg( "input error: can't build local address" );
944 close( p_input->i_handle );
945 p_input->b_error = 1;
950 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
951 sizeof( sock ) ) < 0 )
953 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
954 close( p_input->i_handle );
955 p_input->b_error = 1;
959 /* Allow broadcast reception if we bound on INADDR_ANY */
960 if( psz_bind == NULL )
963 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_BROADCAST,
964 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
966 intf_WarnMsg( 1, "input warning: can't configure socket (SO_BROADCAST: %s)",
971 /* Join the multicast group if the socket is a multicast address */
973 # define IN_MULTICAST(a) IN_CLASSD(a)
976 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
980 imr.imr_interface.s_addr = INADDR_ANY;
981 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
982 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
983 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
985 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
987 close( p_input->i_handle );
988 p_input->b_error = 1;
993 if( psz_server != NULL )
995 /* Build socket for remote connection */
996 if ( network_BuildAddr( &sock, psz_server, i_server_port ) == -1 )
998 intf_ErrMsg( "input error: can't build remote address" );
999 close( p_input->i_handle );
1000 p_input->b_error = 1;
1004 /* Connect the socket */
1005 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1006 sizeof( sock ) ) == (-1) )
1008 intf_ErrMsg( "input error: can't connect socket (%s)",
1010 close( p_input->i_handle );
1011 p_input->b_error = 1;
1016 p_input->stream.b_pace_control = 0;
1017 p_input->stream.b_seekable = 0;
1019 intf_WarnMsg( 3, "input: successfully opened network mode" );
1024 /*****************************************************************************
1025 * NetworkClose : close a network socket
1026 *****************************************************************************/
1027 static void NetworkClose( input_thread_t * p_input )
1029 intf_WarnMsg( 2, "input: closing network target `%s'", p_input->p_source );
1031 close( p_input->i_handle );
1034 /*****************************************************************************
1035 * HTTPOpen : make an HTTP request
1036 *****************************************************************************/
1037 static void HTTPOpen( input_thread_t * p_input )
1039 char *psz_server = NULL;
1040 char *psz_path = NULL;
1044 struct sockaddr_in sock;
1045 char psz_buffer[256];
1047 /* Get the remote server */
1048 if( p_input->p_source != NULL )
1050 psz_server = p_input->p_source;
1052 /* Skip the protocol name */
1053 while( *psz_server && *psz_server != ':' )
1058 /* Skip the "://" part */
1059 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1064 /* Found a server name */
1067 char *psz_port = psz_server;
1069 /* Skip the hostname part */
1070 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1075 /* Found a port name */
1078 if( *psz_port == ':' )
1080 /* Replace ':' with '\0' */
1085 psz_path = psz_port;
1086 while( *psz_path && *psz_path != '/' )
1101 if( *psz_port != '\0' )
1103 i_port = atoi( psz_port );
1113 /* Check that we got a valid server */
1114 if( psz_server == NULL )
1116 intf_ErrMsg( "input error: No server given" );
1117 p_input->b_error = 1;
1121 /* Check that we got a valid port */
1124 i_port = 80; /* FIXME */
1127 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1130 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1132 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1133 if( p_input->i_handle == -1 )
1135 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1139 /* We may want to reuse an already used socket */
1141 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1142 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1144 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1146 close( p_input->i_handle );
1147 p_input->b_error = 1;
1152 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1154 /* http://myproxy.mydomain:myport/ */
1155 int i_proxy_port = 0;
1157 /* Skip the protocol name */
1158 while( *psz_proxy && *psz_proxy != ':' )
1163 /* Skip the "://" part */
1164 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1169 /* Found a proxy name */
1172 char *psz_port = psz_proxy;
1174 /* Skip the hostname part */
1175 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1180 /* Found a port name */
1185 /* Replace ':' with '\0' */
1189 psz_junk = psz_port;
1190 while( *psz_junk && *psz_junk != '/' )
1200 if( *psz_port != '\0' )
1202 i_proxy_port = atoi( psz_port );
1208 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1209 close( p_input->i_handle );
1210 p_input->b_error = 1;
1214 /* Build socket for proxy connection */
1215 if ( network_BuildAddr( &sock, psz_proxy, i_proxy_port ) == -1 )
1217 intf_ErrMsg( "input error: can't build remote address" );
1218 close( p_input->i_handle );
1219 p_input->b_error = 1;
1225 /* No proxy, direct connection */
1226 if ( network_BuildAddr( &sock, psz_server, i_port ) == -1 )
1228 intf_ErrMsg( "input error: can't build remote address" );
1229 close( p_input->i_handle );
1230 p_input->b_error = 1;
1235 /* Connect the socket */
1236 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1237 sizeof( sock ) ) == (-1) )
1239 intf_ErrMsg( "input error: can't connect socket (%s)",
1241 close( p_input->i_handle );
1242 p_input->b_error = 1;
1246 p_input->stream.b_seekable = 0;
1247 p_input->stream.b_pace_control = 1; /* TCP/IP... */
1249 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1250 # define HTTP_END "\r\n"
1252 /* Prepare GET ... */
1253 if( psz_proxy != NULL )
1255 snprintf( psz_buffer, sizeof(psz_buffer),
1256 "GET http://%s:%d/%s HTTP/1.0\r\n"
1257 HTTP_USERAGENT HTTP_END,
1258 psz_server, i_port, psz_path );
1262 snprintf( psz_buffer, sizeof(psz_buffer),
1263 "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1264 HTTP_USERAGENT HTTP_END,
1265 psz_path, psz_server );
1267 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1270 if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1272 intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1273 close( p_input->i_handle );
1274 p_input->b_error = 1;
1278 /* Read HTTP header - this is gonna be fun with plug-ins which do not
1279 * use p_input->p_stream :-( */
1280 if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1282 intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1283 close( p_input->i_handle );
1284 p_input->b_error = 1;
1288 while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1290 if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1291 || *psz_buffer == '\r' || *psz_buffer == '\0' )
1295 /* FIXME : check Content-Type one day */
1298 intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1301 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */