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.165 2001/12/12 02:13:50 sam Exp $
9 * Authors: Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <sys/types.h>
38 #elif defined( _MSC_VER ) && defined( _WIN32 )
45 #ifdef STRNCASECMP_IN_STRINGS_H
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>
73 #include "intf_playlist.h"
75 #include "stream_control.h"
76 #include "input_ext-intf.h"
77 #include "input_ext-dec.h"
78 #include "input_ext-plugins.h"
80 #include "interface.h"
82 /*****************************************************************************
84 *****************************************************************************/
85 static void RunThread ( input_thread_t *p_input );
86 static int InitThread ( input_thread_t *p_input );
87 static void ErrorThread ( input_thread_t *p_input );
88 static void CloseThread ( input_thread_t *p_input );
89 static void DestroyThread ( input_thread_t *p_input );
90 static void EndThread ( input_thread_t *p_input );
92 static void FileOpen ( input_thread_t *p_input );
93 static void StdOpen ( input_thread_t *p_input );
94 static void FileClose ( input_thread_t *p_input );
95 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
96 static void NetworkOpen ( input_thread_t *p_input );
97 static void HTTPOpen ( input_thread_t *p_input );
98 static void NetworkClose ( input_thread_t *p_input );
101 /*****************************************************************************
102 * input_CreateThread: creates a new input thread
103 *****************************************************************************
104 * This function creates a new input, and returns a pointer
105 * to its description. On error, it returns NULL.
106 * If pi_status is NULL, then the function will block until the thread is ready.
107 * If not, it will be updated using one of the THREAD_* constants.
108 *****************************************************************************/
109 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
111 input_thread_t * p_input; /* thread descriptor */
112 int i_status; /* thread status */
114 /* Allocate descriptor */
115 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
116 if( p_input == NULL )
118 intf_ErrMsg( "input error: can't allocate input thread (%s)",
123 /* Packets read once */
124 p_input->i_read_once = INPUT_READ_ONCE;
126 /* Initialize thread properties */
128 p_input->b_error = 0;
132 p_input->p_source = p_item->psz_name;
134 /* I have never understood that stuff --Meuuh */
135 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
136 *p_input->pi_status = THREAD_CREATE;
138 /* Initialize stream description */
139 p_input->stream.i_es_number = 0;
140 p_input->stream.i_selected_es_number = 0;
141 p_input->stream.i_pgrm_number = 0;
142 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
143 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
144 p_input->stream.i_mux_rate = 0;
146 /* no stream, no area */
147 p_input->stream.i_area_nb = 0;
148 p_input->stream.pp_areas = NULL;
149 p_input->stream.p_selected_area = NULL;
150 p_input->stream.p_new_area = NULL;
152 /* By default there is one area in a stream */
153 input_AddArea( p_input );
154 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
156 /* Initialize stream control properties. */
157 p_input->stream.control.i_status = PLAYING_S;
158 p_input->stream.control.i_rate = DEFAULT_RATE;
159 p_input->stream.control.b_mute = 0;
160 p_input->stream.control.b_grayscale = main_GetIntVariable(
161 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
162 p_input->stream.control.i_smp = main_GetIntVariable(
163 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
165 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
168 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
171 intf_ErrMsg( "input error: can't create input thread (%s)",
177 /* If status is NULL, wait until the thread is created */
178 if( pi_status == NULL )
182 msleep( THREAD_SLEEP );
183 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
184 && (i_status != THREAD_FATAL) );
185 if( i_status != THREAD_READY )
193 /*****************************************************************************
194 * input_DestroyThread: mark an input thread as zombie
195 *****************************************************************************
196 * This function should not return until the thread is effectively cancelled.
197 *****************************************************************************/
198 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
200 int i_status; /* thread status */
203 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
204 *p_input->pi_status = THREAD_DESTROY;
206 /* Request thread destruction */
209 /* Make the thread exit of an eventual vlc_cond_wait() */
210 vlc_mutex_lock( &p_input->stream.stream_lock );
211 vlc_cond_signal( &p_input->stream.stream_wait );
212 vlc_mutex_unlock( &p_input->stream.stream_lock );
214 /* If status is NULL, wait until thread has been destroyed */
215 if( pi_status == NULL )
219 msleep( THREAD_SLEEP );
220 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
221 && (i_status != THREAD_FATAL) );
225 /*****************************************************************************
226 * RunThread: main thread loop
227 *****************************************************************************
228 * Thread in charge of processing the network packets and demultiplexing.
229 *****************************************************************************/
230 static void RunThread( input_thread_t *p_input )
233 data_packet_t ** pp_packets;
235 if( InitThread( p_input ) )
237 /* If we failed, wait before we are killed, and exit */
238 *p_input->pi_status = THREAD_ERROR;
239 p_input->b_error = 1;
240 ErrorThread( p_input );
241 DestroyThread( p_input );
245 /* initialization is completed */
246 vlc_mutex_lock( &p_input->stream.stream_lock );
247 p_input->stream.b_changed = 1;
248 vlc_mutex_unlock( &p_input->stream.stream_lock );
250 pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
251 sizeof( data_packet_t * ) );
252 if( pp_packets == NULL )
254 intf_ErrMsg( "input error: out of memory" );
256 p_input->b_error = 1;
259 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
263 vlc_mutex_lock( &p_input->stream.stream_lock );
265 if( p_input->stream.p_new_program )
267 if( p_input->pf_set_program != NULL )
270 p_input->pf_set_program( p_input,
271 p_input->stream.p_new_program );
273 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
275 pgrm_descriptor_t * p_pgrm
276 = p_input->stream.pp_programs[i];
277 /* Escape all decoders for the stream discontinuity they
279 input_EscapeDiscontinuity( p_input, p_pgrm );
281 /* Reinitialize synchro. */
282 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
285 p_input->stream.p_new_program = NULL;
288 if( p_input->stream.p_new_area )
290 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
293 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
295 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
297 pgrm_descriptor_t * p_pgrm
298 = p_input->stream.pp_programs[i];
299 /* Escape all decoders for the stream discontinuity they
301 input_EscapeDiscontinuity( p_input, p_pgrm );
303 /* Reinitialize synchro. */
304 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
307 p_input->stream.p_new_area = NULL;
310 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
312 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
314 p_input->pf_seek( p_input,
315 p_input->stream.p_selected_area->i_seek );
317 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
319 pgrm_descriptor_t * p_pgrm
320 = p_input->stream.pp_programs[i];
321 /* Escape all decoders for the stream discontinuity they
323 input_EscapeDiscontinuity( p_input, p_pgrm );
325 /* Reinitialize synchro. */
326 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
329 p_input->stream.p_selected_area->i_seek = NO_SEEK;
332 if( p_input->stream.p_removed_es )
334 input_UnselectES( p_input, p_input->stream.p_removed_es );
335 p_input->stream.p_removed_es = NULL;
338 if( p_input->stream.p_newly_selected_es )
340 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
341 p_input->stream.p_newly_selected_es = NULL;
344 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
346 if( p_input->stream.b_new_mute )
348 input_EscapeAudioDiscontinuity( p_input );
351 vlc_mutex_lock( &p_input->stream.control.control_lock );
352 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
353 vlc_mutex_unlock( &p_input->stream.control.control_lock );
355 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
358 vlc_mutex_unlock( &p_input->stream.stream_lock );
360 i_error = p_input->pf_read( p_input, pp_packets );
362 /* Demultiplex read packets. */
363 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
365 p_input->stream.c_packets_read++;
366 p_input->pf_demux( p_input, pp_packets[i] );
373 /* End of file - we do not set b_die because only the
374 * interface is allowed to do so. */
375 intf_WarnMsg( 3, "input: EOF reached" );
380 p_input->b_error = 1;
387 if( p_input->b_error || p_input->b_eof )
389 ErrorThread( p_input );
392 EndThread( p_input );
394 DestroyThread( p_input );
396 intf_DbgMsg("input: Thread end");
399 /*****************************************************************************
400 * InitThread: init the input Thread
401 *****************************************************************************/
402 static int InitThread( input_thread_t * p_input )
405 /* Initialize statistics */
406 p_input->c_loops = 0;
407 p_input->stream.c_packets_read = 0;
408 p_input->stream.c_packets_trashed = 0;
409 p_input->p_stream = NULL;
412 vlc_mutex_init( &p_input->stream.stream_lock );
413 vlc_cond_init( &p_input->stream.stream_wait );
414 vlc_mutex_init( &p_input->stream.control.control_lock );
416 /* Find appropriate module. */
417 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
418 (probedata_t *)p_input );
420 if( p_input->p_input_module == NULL )
422 intf_ErrMsg( "input error: no suitable input module for `%s'",
427 #define f p_input->p_input_module->p_functions->input.functions.input
428 p_input->pf_init = f.pf_init;
429 p_input->pf_end = f.pf_end;
430 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
431 p_input->pf_read = f.pf_read;
432 p_input->pf_set_area = f.pf_set_area;
433 p_input->pf_set_program = f.pf_set_program;
434 p_input->pf_demux = f.pf_demux;
435 p_input->pf_new_packet = f.pf_new_packet;
436 p_input->pf_new_pes = f.pf_new_pes;
437 p_input->pf_delete_packet = f.pf_delete_packet;
438 p_input->pf_delete_pes = f.pf_delete_pes;
439 p_input->pf_rewind = f.pf_rewind;
440 p_input->pf_seek = f.pf_seek;
442 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
443 /* FIXME : this is waaaay too kludgy */
444 if( ( strlen( p_input->p_source ) >= 10
445 && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
446 || ( strlen( p_input->p_source ) >= 4
447 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
450 NetworkOpen( p_input );
451 p_input->stream.i_method = INPUT_METHOD_NETWORK;
453 else if( ( strlen( p_input->p_source ) > 5 )
454 && !strncasecmp( p_input->p_source, "http:", 5 ) )
458 p_input->stream.i_method = INPUT_METHOD_NETWORK;
462 if( ( ( ( strlen( p_input->p_source ) > 4 )
463 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
464 || TestMethod( INPUT_METHOD_VAR, "dvd" ) )
465 && f.pf_open != NULL )
467 /* DVD - this is THE kludge */
468 f.pf_open( p_input );
469 p_input->stream.i_method = INPUT_METHOD_DVD;
471 else if( ( ( ( strlen( p_input->p_source ) > 8 )
472 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
473 || TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
474 && f.pf_open != NULL )
476 /* DVDRead - this is THE kludge */
477 f.pf_open( p_input );
478 p_input->stream.i_method = INPUT_METHOD_DVD;
480 else if( ( strlen( p_input->p_source ) > 4 )
481 && !strncasecmp( p_input->p_source, "vlc:", 4 )
482 && f.pf_open != NULL )
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 ) > 8 )
608 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
609 || TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
611 f.pf_close( p_input );
613 else if( ( strlen( p_input->p_source ) > 4 )
614 && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
616 f.pf_close( p_input );
620 FileClose( p_input );
625 /*****************************************************************************
626 * DestroyThread: destroy the input thread
627 *****************************************************************************/
628 static void DestroyThread( input_thread_t * p_input )
630 int * pi_status; /* thread status */
633 pi_status = p_input->pi_status;
635 /* Destroy Mutex locks */
636 vlc_mutex_destroy( &p_input->stream.control.control_lock );
637 vlc_mutex_destroy( &p_input->stream.stream_lock );
639 /* Free input structure */
643 *pi_status = THREAD_OVER;
646 /*****************************************************************************
647 * StdOpen : open standard input
648 *****************************************************************************/
649 static void StdOpen( input_thread_t * p_input )
651 vlc_mutex_lock( &p_input->stream.stream_lock );
653 /* Suppose we can control the pace - this won't work in some cases ! */
654 p_input->stream.b_pace_control = 1;
656 p_input->stream.b_seekable = 0;
657 p_input->stream.p_selected_area->i_size = 0;
658 p_input->stream.p_selected_area->i_tell = 0;
659 vlc_mutex_unlock( &p_input->stream.stream_lock );
661 intf_WarnMsg( 2, "input: opening stdin" );
662 p_input->i_handle = 0;
665 /*****************************************************************************
666 * FileOpen : open a file descriptor
667 *****************************************************************************/
668 static void FileOpen( input_thread_t * p_input )
670 struct stat stat_info;
673 char *psz_name = p_input->p_source;
675 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
677 int i_size = strlen( psz_name );
680 && !strncasecmp( psz_name, "dvdread:", 8 ) )
682 /* get rid of the 'dvdread:' stuff and try again */
684 i_stat = stat( psz_name, &stat_info );
686 else if( ( i_size > 4 )
687 && !strncasecmp( psz_name, "dvd:", 4 ) )
689 /* get rid of the 'dvd:' stuff and try again */
691 i_stat = stat( psz_name, &stat_info );
693 else if( ( i_size > 4 )
694 && !strncasecmp( psz_name, "vcd:", 4 ) )
696 /* get rid of the 'vcd:' stuff and try again */
698 i_stat = stat( psz_name, &stat_info );
700 else if( ( i_size > 5 )
701 && !strncasecmp( psz_name, "file:", 5 ) )
703 /* get rid of the 'file:' stuff and try again */
705 i_stat = stat( psz_name, &stat_info );
710 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
711 psz_name, strerror(errno));
712 p_input->b_error = 1;
717 vlc_mutex_lock( &p_input->stream.stream_lock );
719 /* If we are here we can control the pace... */
720 p_input->stream.b_pace_control = 1;
722 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
723 || S_ISBLK(stat_info.st_mode) )
725 p_input->stream.b_seekable = 1;
726 p_input->stream.p_selected_area->i_size = stat_info.st_size;
728 else if( S_ISFIFO(stat_info.st_mode)
729 #if !defined( SYS_BEOS ) && !defined( WIN32 )
730 || S_ISSOCK(stat_info.st_mode)
734 p_input->stream.b_seekable = 0;
735 p_input->stream.p_selected_area->i_size = 0;
739 vlc_mutex_unlock( &p_input->stream.stream_lock );
740 intf_ErrMsg( "input error: unknown file type for `%s'",
742 p_input->b_error = 1;
746 p_input->stream.p_selected_area->i_tell = 0;
747 vlc_mutex_unlock( &p_input->stream.stream_lock );
749 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
750 if( (p_input->i_handle = open( psz_name,
751 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
753 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
754 p_input->b_error = 1;
760 /*****************************************************************************
761 * FileClose : close a file descriptor
762 *****************************************************************************/
763 static void FileClose( input_thread_t * p_input )
765 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
767 close( p_input->i_handle );
772 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
773 /*****************************************************************************
774 * NetworkOpen : open a network socket
775 *****************************************************************************/
776 static void NetworkOpen( input_thread_t * p_input )
778 char *psz_server = NULL;
779 char *psz_bind = NULL;
780 int i_server_port = 0;
784 struct sockaddr_in sock;
786 /* Get the remote server. Syntax is :
787 * udp[stream]:[/][/][serveraddr[:serverport]][@[bindaddr]:[bindport]] */
788 if( p_input->p_source != NULL )
790 char * psz_parser = p_input->p_source;
791 char * psz_server_port = NULL;
792 char * psz_bind_port = NULL;
794 /* Skip the protocol name */
795 while( *psz_parser && *psz_parser != ':' )
800 /* Skip the "://" part */
801 while( *psz_parser && (*psz_parser == ':' || *psz_parser == '/') )
806 if( *psz_parser && *psz_parser != '@' )
809 psz_server = psz_parser;
811 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
816 if( *psz_parser == ':' )
818 /* Found server port */
819 *psz_parser = '\0'; /* Terminate server name */
821 psz_server_port = psz_parser;
823 while( *psz_parser && *psz_parser != '@' )
830 if( *psz_parser == '@' )
832 /* Found bind address or bind port */
833 *psz_parser = '\0'; /* Terminate server port or name if necessary */
836 if( *psz_parser && *psz_parser != ':' )
838 /* Found bind address */
839 psz_bind = psz_parser;
841 while( *psz_parser && *psz_parser != ':' )
847 if( *psz_parser == ':' )
849 /* Found bind port */
850 *psz_parser = '\0'; /* Terminate bind address if necessary */
853 psz_bind_port = psz_parser;
857 /* Convert ports format */
858 if( psz_server_port != NULL )
860 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
863 intf_ErrMsg( "input error: cannot parse server port near %s",
865 p_input->b_error = 1;
870 if( psz_bind_port != NULL )
872 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
875 intf_ErrMsg( "input error: cannot parse bind port near %s",
877 p_input->b_error = 1;
884 /* This is required or NetworkClose will never be called */
885 p_input->p_source = "ts: network input";
888 /* Check that we got a valid port */
889 if( i_bind_port == 0 )
891 i_bind_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
894 intf_WarnMsg( 2, "input: server=%s:%d local=%s:%d",
895 psz_server, i_server_port, psz_bind, i_bind_port );
897 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
899 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
900 if( p_input->i_handle == -1 )
902 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
903 p_input->b_error = 1;
907 /* We may want to reuse an already used socket */
909 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
910 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
912 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
914 close( p_input->i_handle );
915 p_input->b_error = 1;
919 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
920 * packet loss caused by scheduling problems */
922 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
923 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
925 intf_WarnMsg( 1, "input warning: can't configure socket (SO_RCVBUF: %s)",
929 /* Check if we really got what we have asked for, because Linux, etc.
930 * will silently limit the max buffer size to net.core.rmem_max which
931 * is typically only 65535 bytes */
933 i_opt_size = sizeof( i_opt );
934 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
935 (void*) &i_opt, &i_opt_size ) == -1 )
937 intf_WarnMsg( 1, "input warning: can't query socket (SO_RCVBUF: %s)",
940 else if( i_opt < 0x80000 )
942 intf_WarnMsg( 1, "input warning: socket buffer size is 0x%x"
943 " instead of 0x%x", i_opt, 0x80000 );
946 /* Build the local socket */
947 if ( network_BuildAddr( &sock, psz_bind, i_bind_port ) == -1 )
949 intf_ErrMsg( "input error: can't build local address" );
950 close( p_input->i_handle );
951 p_input->b_error = 1;
956 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
957 sizeof( sock ) ) < 0 )
959 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
960 close( p_input->i_handle );
961 p_input->b_error = 1;
965 /* Allow broadcast reception if we bound on INADDR_ANY */
966 if( psz_bind == NULL )
969 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_BROADCAST,
970 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
972 intf_WarnMsg( 1, "input warning: can't configure socket (SO_BROADCAST: %s)",
977 /* Join the multicast group if the socket is a multicast address */
979 # define IN_MULTICAST(a) IN_CLASSD(a)
982 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
986 imr.imr_interface.s_addr = INADDR_ANY;
987 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
988 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
989 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
991 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
993 close( p_input->i_handle );
994 p_input->b_error = 1;
999 if( psz_server != NULL )
1001 /* Build socket for remote connection */
1002 if ( network_BuildAddr( &sock, psz_server, i_server_port ) == -1 )
1004 intf_ErrMsg( "input error: can't build remote address" );
1005 close( p_input->i_handle );
1006 p_input->b_error = 1;
1010 /* Connect the socket */
1011 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1012 sizeof( sock ) ) == (-1) )
1014 intf_ErrMsg( "input error: can't connect socket (%s)",
1016 close( p_input->i_handle );
1017 p_input->b_error = 1;
1022 p_input->stream.b_pace_control = 0;
1023 p_input->stream.b_seekable = 0;
1025 intf_WarnMsg( 3, "input: successfully opened network mode" );
1030 /*****************************************************************************
1031 * NetworkClose : close a network socket
1032 *****************************************************************************/
1033 static void NetworkClose( input_thread_t * p_input )
1035 intf_WarnMsg( 2, "input: closing network target `%s'", p_input->p_source );
1037 close( p_input->i_handle );
1040 /*****************************************************************************
1041 * HTTPOpen : make an HTTP request
1042 *****************************************************************************/
1043 static void HTTPOpen( input_thread_t * p_input )
1045 char *psz_server = NULL;
1046 char *psz_path = NULL;
1050 struct sockaddr_in sock;
1051 char psz_buffer[256];
1053 /* Get the remote server */
1054 if( p_input->p_source != NULL )
1056 psz_server = p_input->p_source;
1058 /* Skip the protocol name */
1059 while( *psz_server && *psz_server != ':' )
1064 /* Skip the "://" part */
1065 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1070 /* Found a server name */
1073 char *psz_port = psz_server;
1075 /* Skip the hostname part */
1076 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1081 /* Found a port name */
1084 if( *psz_port == ':' )
1086 /* Replace ':' with '\0' */
1091 psz_path = psz_port;
1092 while( *psz_path && *psz_path != '/' )
1107 if( *psz_port != '\0' )
1109 i_port = atoi( psz_port );
1119 /* Check that we got a valid server */
1120 if( psz_server == NULL )
1122 intf_ErrMsg( "input error: No server given" );
1123 p_input->b_error = 1;
1127 /* Check that we got a valid port */
1130 i_port = 80; /* FIXME */
1133 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1136 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1138 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1139 if( p_input->i_handle == -1 )
1141 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1145 /* We may want to reuse an already used socket */
1147 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1148 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1150 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1152 close( p_input->i_handle );
1153 p_input->b_error = 1;
1158 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1160 /* http://myproxy.mydomain:myport/ */
1161 int i_proxy_port = 0;
1163 /* Skip the protocol name */
1164 while( *psz_proxy && *psz_proxy != ':' )
1169 /* Skip the "://" part */
1170 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1175 /* Found a proxy name */
1178 char *psz_port = psz_proxy;
1180 /* Skip the hostname part */
1181 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1186 /* Found a port name */
1191 /* Replace ':' with '\0' */
1195 psz_junk = psz_port;
1196 while( *psz_junk && *psz_junk != '/' )
1206 if( *psz_port != '\0' )
1208 i_proxy_port = atoi( psz_port );
1214 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1215 close( p_input->i_handle );
1216 p_input->b_error = 1;
1220 /* Build socket for proxy connection */
1221 if ( network_BuildAddr( &sock, psz_proxy, i_proxy_port ) == -1 )
1223 intf_ErrMsg( "input error: can't build remote address" );
1224 close( p_input->i_handle );
1225 p_input->b_error = 1;
1231 /* No proxy, direct connection */
1232 if ( network_BuildAddr( &sock, psz_server, i_port ) == -1 )
1234 intf_ErrMsg( "input error: can't build remote address" );
1235 close( p_input->i_handle );
1236 p_input->b_error = 1;
1241 /* Connect the socket */
1242 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1243 sizeof( sock ) ) == (-1) )
1245 intf_ErrMsg( "input error: can't connect socket (%s)",
1247 close( p_input->i_handle );
1248 p_input->b_error = 1;
1252 p_input->stream.b_seekable = 0;
1253 p_input->stream.b_pace_control = 1; /* TCP/IP... */
1255 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1256 # define HTTP_END "\r\n"
1258 /* Prepare GET ... */
1259 if( psz_proxy != NULL )
1261 snprintf( psz_buffer, sizeof(psz_buffer),
1262 "GET http://%s:%d/%s HTTP/1.0\r\n"
1263 HTTP_USERAGENT HTTP_END,
1264 psz_server, i_port, psz_path );
1268 snprintf( psz_buffer, sizeof(psz_buffer),
1269 "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1270 HTTP_USERAGENT HTTP_END,
1271 psz_path, psz_server );
1273 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1276 if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1278 intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1279 close( p_input->i_handle );
1280 p_input->b_error = 1;
1284 /* Read HTTP header - this is gonna be fun with plug-ins which do not
1285 * use p_input->p_stream :-( */
1286 if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1288 intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1289 close( p_input->i_handle );
1290 p_input->b_error = 1;
1294 while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1296 if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1297 || *psz_buffer == '\r' || *psz_buffer == '\0' )
1301 /* FIXME : check Content-Type one day */
1304 intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1307 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */