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.166 2001/12/27 03:47:09 massiot 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 /* Initialize thread properties */
125 p_input->b_error = 0;
129 p_input->p_source = p_item->psz_name;
131 /* I have never understood that stuff --Meuuh */
132 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
133 *p_input->pi_status = THREAD_CREATE;
135 /* Initialize stream description */
136 p_input->stream.i_es_number = 0;
137 p_input->stream.i_selected_es_number = 0;
138 p_input->stream.i_pgrm_number = 0;
139 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
140 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
141 p_input->stream.i_mux_rate = 0;
143 /* no stream, no area */
144 p_input->stream.i_area_nb = 0;
145 p_input->stream.pp_areas = NULL;
146 p_input->stream.p_selected_area = NULL;
147 p_input->stream.p_new_area = NULL;
149 /* By default there is one area in a stream */
150 input_AddArea( p_input );
151 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
153 /* Initialize stream control properties. */
154 p_input->stream.control.i_status = PLAYING_S;
155 p_input->stream.control.i_rate = DEFAULT_RATE;
156 p_input->stream.control.b_mute = 0;
157 p_input->stream.control.b_grayscale = main_GetIntVariable(
158 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
159 p_input->stream.control.i_smp = main_GetIntVariable(
160 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
162 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
165 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
168 intf_ErrMsg( "input error: can't create input thread (%s)",
174 /* If status is NULL, wait until the thread is created */
175 if( pi_status == NULL )
179 msleep( THREAD_SLEEP );
180 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
181 && (i_status != THREAD_FATAL) );
182 if( i_status != THREAD_READY )
190 /*****************************************************************************
191 * input_DestroyThread: mark an input thread as zombie
192 *****************************************************************************
193 * This function should not return until the thread is effectively cancelled.
194 *****************************************************************************/
195 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
197 int i_status; /* thread status */
200 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
201 *p_input->pi_status = THREAD_DESTROY;
203 /* Request thread destruction */
206 /* Make the thread exit of an eventual vlc_cond_wait() */
207 vlc_mutex_lock( &p_input->stream.stream_lock );
208 vlc_cond_signal( &p_input->stream.stream_wait );
209 vlc_mutex_unlock( &p_input->stream.stream_lock );
211 /* If status is NULL, wait until thread has been destroyed */
212 if( pi_status == NULL )
216 msleep( THREAD_SLEEP );
217 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
218 && (i_status != THREAD_FATAL) );
222 /*****************************************************************************
223 * RunThread: main thread loop
224 *****************************************************************************
225 * Thread in charge of processing the network packets and demultiplexing.
226 *****************************************************************************/
227 static void RunThread( input_thread_t *p_input )
229 if( InitThread( p_input ) )
231 /* If we failed, wait before we are killed, and exit */
232 *p_input->pi_status = THREAD_ERROR;
233 p_input->b_error = 1;
234 ErrorThread( p_input );
235 DestroyThread( p_input );
239 /* initialization is complete */
240 vlc_mutex_lock( &p_input->stream.stream_lock );
241 p_input->stream.b_changed = 1;
242 vlc_mutex_unlock( &p_input->stream.stream_lock );
244 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
246 data_packet_t * p_data;
251 vlc_mutex_lock( &p_input->stream.stream_lock );
253 if( p_input->stream.p_new_program )
255 if( p_input->pf_set_program != NULL )
258 p_input->pf_set_program( p_input,
259 p_input->stream.p_new_program );
261 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
263 pgrm_descriptor_t * p_pgrm
264 = p_input->stream.pp_programs[i];
265 /* Escape all decoders for the stream discontinuity they
267 input_EscapeDiscontinuity( p_input, p_pgrm );
269 /* Reinitialize synchro. */
270 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
273 p_input->stream.p_new_program = NULL;
276 if( p_input->stream.p_new_area )
278 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
281 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
283 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
285 pgrm_descriptor_t * p_pgrm
286 = p_input->stream.pp_programs[i];
287 /* Escape all decoders for the stream discontinuity they
289 input_EscapeDiscontinuity( p_input, p_pgrm );
291 /* Reinitialize synchro. */
292 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
295 p_input->stream.p_new_area = NULL;
298 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
300 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
302 p_input->pf_seek( p_input,
303 p_input->stream.p_selected_area->i_seek );
305 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
307 pgrm_descriptor_t * p_pgrm
308 = p_input->stream.pp_programs[i];
309 /* Escape all decoders for the stream discontinuity they
311 input_EscapeDiscontinuity( p_input, p_pgrm );
313 /* Reinitialize synchro. */
314 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
317 p_input->stream.p_selected_area->i_seek = NO_SEEK;
320 if( p_input->stream.p_removed_es )
322 input_UnselectES( p_input, p_input->stream.p_removed_es );
323 p_input->stream.p_removed_es = NULL;
326 if( p_input->stream.p_newly_selected_es )
328 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
329 p_input->stream.p_newly_selected_es = NULL;
332 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
334 if( p_input->stream.b_new_mute )
336 input_EscapeAudioDiscontinuity( p_input );
339 vlc_mutex_lock( &p_input->stream.control.control_lock );
340 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
341 vlc_mutex_unlock( &p_input->stream.control.control_lock );
343 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
346 vlc_mutex_unlock( &p_input->stream.stream_lock );
348 i_count = p_input->pf_read( p_input, &p_data );
350 /* Demultiplex read packets. */
351 while( p_data != NULL )
353 data_packet_t * p_next = p_data->p_next;
354 p_data->p_next = NULL;
356 p_input->stream.c_packets_read++;
357 p_input->pf_demux( p_input, p_data );
362 if( i_count == 0 && p_input->stream.b_seekable )
364 /* End of file - we do not set b_die because only the
365 * interface is allowed to do so. */
366 intf_WarnMsg( 3, "input: EOF reached" );
369 else if( i_count < 0 )
371 p_input->b_error = 1;
375 if( p_input->b_error || p_input->b_eof )
377 ErrorThread( p_input );
380 EndThread( p_input );
382 DestroyThread( p_input );
384 intf_DbgMsg("input: Thread end");
387 /*****************************************************************************
388 * InitThread: init the input Thread
389 *****************************************************************************/
390 static int InitThread( input_thread_t * p_input )
393 /* Initialize statistics */
394 p_input->c_loops = 0;
395 p_input->stream.c_packets_read = 0;
396 p_input->stream.c_packets_trashed = 0;
397 p_input->p_stream = NULL;
400 vlc_mutex_init( &p_input->stream.stream_lock );
401 vlc_cond_init( &p_input->stream.stream_wait );
402 vlc_mutex_init( &p_input->stream.control.control_lock );
404 /* Find appropriate module. */
405 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
406 (probedata_t *)p_input );
408 if( p_input->p_input_module == NULL )
410 intf_ErrMsg( "input error: no suitable input module for `%s'",
415 #define f p_input->p_input_module->p_functions->input.functions.input
416 p_input->pf_init = f.pf_init;
417 p_input->pf_end = f.pf_end;
418 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
419 p_input->pf_read = f.pf_read;
420 p_input->pf_set_area = f.pf_set_area;
421 p_input->pf_set_program = f.pf_set_program;
422 p_input->pf_demux = f.pf_demux;
423 p_input->pf_new_packet = f.pf_new_packet;
424 p_input->pf_new_pes = f.pf_new_pes;
425 p_input->pf_delete_packet = f.pf_delete_packet;
426 p_input->pf_delete_pes = f.pf_delete_pes;
427 p_input->pf_rewind = f.pf_rewind;
428 p_input->pf_seek = f.pf_seek;
430 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
431 /* FIXME : this is waaaay too kludgy */
432 if( ( strlen( p_input->p_source ) >= 10
433 && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
434 || ( strlen( p_input->p_source ) >= 4
435 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
438 NetworkOpen( p_input );
439 p_input->stream.i_method = INPUT_METHOD_NETWORK;
441 else if( ( strlen( p_input->p_source ) > 5 )
442 && !strncasecmp( p_input->p_source, "http:", 5 ) )
446 p_input->stream.i_method = INPUT_METHOD_NETWORK;
450 if( ( ( ( strlen( p_input->p_source ) > 4 )
451 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
452 || TestMethod( INPUT_METHOD_VAR, "dvd" ) )
453 && f.pf_open != NULL )
455 /* DVD - this is THE kludge */
456 f.pf_open( p_input );
457 p_input->stream.i_method = INPUT_METHOD_DVD;
459 else if( ( ( ( strlen( p_input->p_source ) > 8 )
460 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
461 || TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
462 && f.pf_open != NULL )
464 /* DVDRead - this is THE kludge */
465 f.pf_open( p_input );
466 p_input->stream.i_method = INPUT_METHOD_DVD;
468 else if( ( strlen( p_input->p_source ) > 4 )
469 && !strncasecmp( p_input->p_source, "vlc:", 4 )
470 && f.pf_open != NULL )
472 /* Dummy input - very kludgy */
473 f.pf_open( p_input );
475 else if( ( strlen( p_input->p_source ) == 1 )
476 && *p_input->p_source == '-' )
485 p_input->stream.i_method = INPUT_METHOD_FILE;
489 if( p_input->b_error )
491 /* We barfed -- exit nicely */
492 module_Unneed( p_input->p_input_module );
496 p_input->pf_init( p_input );
498 if( p_input->b_error )
500 /* We barfed -- exit nicely */
501 CloseThread( p_input );
502 module_Unneed( p_input->p_input_module );
506 *p_input->pi_status = THREAD_READY;
511 /*****************************************************************************
512 * ErrorThread: RunThread() error loop
513 *****************************************************************************
514 * This function is called when an error occured during thread main's loop.
515 *****************************************************************************/
516 static void ErrorThread( input_thread_t *p_input )
518 while( !p_input->b_die )
521 msleep( INPUT_IDLE_SLEEP );
525 /*****************************************************************************
526 * EndThread: end the input thread
527 *****************************************************************************/
528 static void EndThread( input_thread_t * p_input )
530 int * pi_status; /* thread status */
533 pi_status = p_input->pi_status;
534 *pi_status = THREAD_END;
536 if( p_main->b_stats )
538 #ifdef HAVE_SYS_TIMES_H
539 /* Display statistics */
540 struct tms cpu_usage;
543 intf_StatMsg( "input stats: %d loops consuming user: %d, system: %d",
545 cpu_usage.tms_utime, cpu_usage.tms_stime );
547 intf_StatMsg( "input stats: %d loops", p_input->c_loops );
550 input_DumpStream( p_input );
553 /* Free all ES and destroy all decoder threads */
554 input_EndStream( p_input );
556 /* Free demultiplexer's data */
557 p_input->pf_end( p_input );
559 /* Close the input method */
560 CloseThread( p_input );
562 /* Release modules */
563 module_Unneed( p_input->p_input_module );
566 /*****************************************************************************
567 * CloseThread: close the target
568 *****************************************************************************/
569 static void CloseThread( input_thread_t * p_input )
571 #define f p_input->p_input_module->p_functions->input.functions.input
573 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
575 if( ( strlen( p_input->p_source ) > 10
576 && !strncasecmp( p_input->p_source, "udpstream:", 10 ) )
577 || ( strlen( p_input->p_source ) > 4
578 && !strncasecmp( p_input->p_source, "udp:", 4 ) ) )
580 NetworkClose( p_input );
582 else if( ( strlen( p_input->p_source ) > 5 )
583 && !strncasecmp( p_input->p_source, "http:", 5 ) )
585 NetworkClose( p_input );
589 if( ( ( strlen( p_input->p_source ) > 4 )
590 && !strncasecmp( p_input->p_source, "dvd:", 4 ) )
591 || TestMethod( INPUT_METHOD_VAR, "dvd" ) )
593 f.pf_close( p_input );
595 else if( ( ( strlen( p_input->p_source ) > 8 )
596 && !strncasecmp( p_input->p_source, "dvdread:", 8 ) )
597 || TestMethod( INPUT_METHOD_VAR, "dvdread" ) )
599 f.pf_close( p_input );
601 else if( ( strlen( p_input->p_source ) > 4 )
602 && !strncasecmp( p_input->p_source, "vlc:", 4 ) )
604 f.pf_close( p_input );
608 FileClose( p_input );
613 /*****************************************************************************
614 * DestroyThread: destroy the input thread
615 *****************************************************************************/
616 static void DestroyThread( input_thread_t * p_input )
618 int * pi_status; /* thread status */
621 pi_status = p_input->pi_status;
623 /* Destroy Mutex locks */
624 vlc_mutex_destroy( &p_input->stream.control.control_lock );
625 vlc_mutex_destroy( &p_input->stream.stream_lock );
627 /* Free input structure */
631 *pi_status = THREAD_OVER;
634 /*****************************************************************************
635 * StdOpen : open standard input
636 *****************************************************************************/
637 static void StdOpen( input_thread_t * p_input )
639 vlc_mutex_lock( &p_input->stream.stream_lock );
641 /* Suppose we can control the pace - this won't work in some cases ! */
642 p_input->stream.b_pace_control = 1;
644 p_input->stream.b_seekable = 0;
645 p_input->stream.p_selected_area->i_size = 0;
646 p_input->stream.p_selected_area->i_tell = 0;
647 vlc_mutex_unlock( &p_input->stream.stream_lock );
649 intf_WarnMsg( 2, "input: opening stdin" );
650 p_input->i_handle = 0;
653 /*****************************************************************************
654 * FileOpen : open a file descriptor
655 *****************************************************************************/
656 static void FileOpen( input_thread_t * p_input )
658 struct stat stat_info;
661 char *psz_name = p_input->p_source;
663 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
665 int i_size = strlen( psz_name );
668 && !strncasecmp( psz_name, "dvdread:", 8 ) )
670 /* get rid of the 'dvdread:' stuff and try again */
672 i_stat = stat( psz_name, &stat_info );
674 else if( ( i_size > 4 )
675 && !strncasecmp( psz_name, "dvd:", 4 ) )
677 /* get rid of the 'dvd:' stuff and try again */
679 i_stat = stat( psz_name, &stat_info );
681 else if( ( i_size > 4 )
682 && !strncasecmp( psz_name, "vcd:", 4 ) )
684 /* get rid of the 'vcd:' stuff and try again */
686 i_stat = stat( psz_name, &stat_info );
688 else if( ( i_size > 5 )
689 && !strncasecmp( psz_name, "file:", 5 ) )
691 /* get rid of the 'file:' stuff and try again */
693 i_stat = stat( psz_name, &stat_info );
698 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
699 psz_name, strerror(errno));
700 p_input->b_error = 1;
705 vlc_mutex_lock( &p_input->stream.stream_lock );
707 /* If we are here we can control the pace... */
708 p_input->stream.b_pace_control = 1;
710 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
711 || S_ISBLK(stat_info.st_mode) )
713 p_input->stream.b_seekable = 1;
714 p_input->stream.p_selected_area->i_size = stat_info.st_size;
716 else if( S_ISFIFO(stat_info.st_mode)
717 #if !defined( SYS_BEOS ) && !defined( WIN32 )
718 || S_ISSOCK(stat_info.st_mode)
722 p_input->stream.b_seekable = 0;
723 p_input->stream.p_selected_area->i_size = 0;
727 vlc_mutex_unlock( &p_input->stream.stream_lock );
728 intf_ErrMsg( "input error: unknown file type for `%s'",
730 p_input->b_error = 1;
734 p_input->stream.p_selected_area->i_tell = 0;
735 vlc_mutex_unlock( &p_input->stream.stream_lock );
737 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
738 if( (p_input->i_handle = open( psz_name,
739 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
741 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
742 p_input->b_error = 1;
748 /*****************************************************************************
749 * FileClose : close a file descriptor
750 *****************************************************************************/
751 static void FileClose( input_thread_t * p_input )
753 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
755 close( p_input->i_handle );
760 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
761 /*****************************************************************************
762 * NetworkOpen : open a network socket
763 *****************************************************************************/
764 static void NetworkOpen( input_thread_t * p_input )
766 char *psz_server = NULL;
767 char *psz_bind = NULL;
768 int i_server_port = 0;
772 struct sockaddr_in sock;
774 /* Get the remote server. Syntax is :
775 * udp[stream]:[/][/][serveraddr[:serverport]][@[bindaddr]:[bindport]] */
776 if( p_input->p_source != NULL )
778 char * psz_parser = p_input->p_source;
779 char * psz_server_port = NULL;
780 char * psz_bind_port = NULL;
782 /* Skip the protocol name */
783 while( *psz_parser && *psz_parser != ':' )
788 /* Skip the "://" part */
789 while( *psz_parser && (*psz_parser == ':' || *psz_parser == '/') )
794 if( *psz_parser && *psz_parser != '@' )
797 psz_server = psz_parser;
799 while( *psz_parser && *psz_parser != ':' && *psz_parser != '@' )
804 if( *psz_parser == ':' )
806 /* Found server port */
807 *psz_parser = '\0'; /* Terminate server name */
809 psz_server_port = psz_parser;
811 while( *psz_parser && *psz_parser != '@' )
818 if( *psz_parser == '@' )
820 /* Found bind address or bind port */
821 *psz_parser = '\0'; /* Terminate server port or name if necessary */
824 if( *psz_parser && *psz_parser != ':' )
826 /* Found bind address */
827 psz_bind = psz_parser;
829 while( *psz_parser && *psz_parser != ':' )
835 if( *psz_parser == ':' )
837 /* Found bind port */
838 *psz_parser = '\0'; /* Terminate bind address if necessary */
841 psz_bind_port = psz_parser;
845 /* Convert ports format */
846 if( psz_server_port != NULL )
848 i_server_port = strtol( psz_server_port, &psz_parser, 10 );
851 intf_ErrMsg( "input error: cannot parse server port near %s",
853 p_input->b_error = 1;
858 if( psz_bind_port != NULL )
860 i_bind_port = strtol( psz_bind_port, &psz_parser, 10 );
863 intf_ErrMsg( "input error: cannot parse bind port near %s",
865 p_input->b_error = 1;
872 /* This is required or NetworkClose will never be called */
873 p_input->p_source = "ts: network input";
876 /* Check that we got a valid port */
877 if( i_bind_port == 0 )
879 i_bind_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
882 intf_WarnMsg( 2, "input: server=%s:%d local=%s:%d",
883 psz_server, i_server_port, psz_bind, i_bind_port );
885 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
887 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
888 if( p_input->i_handle == -1 )
890 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
891 p_input->b_error = 1;
895 /* We may want to reuse an already used socket */
897 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
898 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
900 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
902 close( p_input->i_handle );
903 p_input->b_error = 1;
907 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
908 * packet loss caused by scheduling problems */
910 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
911 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
913 intf_WarnMsg( 1, "input warning: can't configure socket (SO_RCVBUF: %s)",
917 /* Check if we really got what we have asked for, because Linux, etc.
918 * will silently limit the max buffer size to net.core.rmem_max which
919 * is typically only 65535 bytes */
921 i_opt_size = sizeof( i_opt );
922 if( getsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
923 (void*) &i_opt, &i_opt_size ) == -1 )
925 intf_WarnMsg( 1, "input warning: can't query socket (SO_RCVBUF: %s)",
928 else if( i_opt < 0x80000 )
930 intf_WarnMsg( 1, "input warning: socket buffer size is 0x%x"
931 " instead of 0x%x", i_opt, 0x80000 );
934 /* Build the local socket */
935 if ( network_BuildAddr( &sock, psz_bind, i_bind_port ) == -1 )
937 intf_ErrMsg( "input error: can't build local address" );
938 close( p_input->i_handle );
939 p_input->b_error = 1;
944 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
945 sizeof( sock ) ) < 0 )
947 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
948 close( p_input->i_handle );
949 p_input->b_error = 1;
953 /* Allow broadcast reception if we bound on INADDR_ANY */
954 if( psz_bind == NULL )
957 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_BROADCAST,
958 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
960 intf_WarnMsg( 1, "input warning: can't configure socket (SO_BROADCAST: %s)",
965 /* Join the multicast group if the socket is a multicast address */
967 # define IN_MULTICAST(a) IN_CLASSD(a)
970 if( IN_MULTICAST( ntohl(sock.sin_addr.s_addr) ) )
974 imr.imr_interface.s_addr = INADDR_ANY;
975 imr.imr_multiaddr.s_addr = sock.sin_addr.s_addr;
976 if( setsockopt( p_input->i_handle, IPPROTO_IP, IP_ADD_MEMBERSHIP,
977 (char*)&imr, sizeof(struct ip_mreq) ) == -1 )
979 intf_ErrMsg( "input error: failed to join IP multicast group (%s)",
981 close( p_input->i_handle );
982 p_input->b_error = 1;
987 if( psz_server != NULL )
989 /* Build socket for remote connection */
990 if ( network_BuildAddr( &sock, psz_server, i_server_port ) == -1 )
992 intf_ErrMsg( "input error: can't build remote address" );
993 close( p_input->i_handle );
994 p_input->b_error = 1;
998 /* Connect the socket */
999 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1000 sizeof( sock ) ) == (-1) )
1002 intf_ErrMsg( "input error: can't connect socket (%s)",
1004 close( p_input->i_handle );
1005 p_input->b_error = 1;
1010 p_input->stream.b_pace_control = 0;
1011 p_input->stream.b_seekable = 0;
1013 intf_WarnMsg( 3, "input: successfully opened network mode" );
1018 /*****************************************************************************
1019 * NetworkClose : close a network socket
1020 *****************************************************************************/
1021 static void NetworkClose( input_thread_t * p_input )
1023 intf_WarnMsg( 2, "input: closing network target `%s'", p_input->p_source );
1025 close( p_input->i_handle );
1028 /*****************************************************************************
1029 * HTTPOpen : make an HTTP request
1030 *****************************************************************************/
1031 static void HTTPOpen( input_thread_t * p_input )
1033 char *psz_server = NULL;
1034 char *psz_path = NULL;
1038 struct sockaddr_in sock;
1039 char psz_buffer[256];
1041 /* Get the remote server */
1042 if( p_input->p_source != NULL )
1044 psz_server = p_input->p_source;
1046 /* Skip the protocol name */
1047 while( *psz_server && *psz_server != ':' )
1052 /* Skip the "://" part */
1053 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
1058 /* Found a server name */
1061 char *psz_port = psz_server;
1063 /* Skip the hostname part */
1064 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1069 /* Found a port name */
1072 if( *psz_port == ':' )
1074 /* Replace ':' with '\0' */
1079 psz_path = psz_port;
1080 while( *psz_path && *psz_path != '/' )
1095 if( *psz_port != '\0' )
1097 i_port = atoi( psz_port );
1107 /* Check that we got a valid server */
1108 if( psz_server == NULL )
1110 intf_ErrMsg( "input error: No server given" );
1111 p_input->b_error = 1;
1115 /* Check that we got a valid port */
1118 i_port = 80; /* FIXME */
1121 intf_WarnMsg( 2, "input: server=%s port=%d path=%s", psz_server,
1124 /* Open a SOCK_STREAM (TCP) socket, in the AF_INET domain, automatic (0)
1126 p_input->i_handle = socket( AF_INET, SOCK_STREAM, 0 );
1127 if( p_input->i_handle == -1 )
1129 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) ); p_input->b_error = 1;
1133 /* We may want to reuse an already used socket */
1135 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
1136 (void *) &i_opt, sizeof( i_opt ) ) == -1 )
1138 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
1140 close( p_input->i_handle );
1141 p_input->b_error = 1;
1146 if( (psz_proxy = main_GetPszVariable( "http_proxy", NULL )) != NULL )
1148 /* http://myproxy.mydomain:myport/ */
1149 int i_proxy_port = 0;
1151 /* Skip the protocol name */
1152 while( *psz_proxy && *psz_proxy != ':' )
1157 /* Skip the "://" part */
1158 while( *psz_proxy && (*psz_proxy == ':' || *psz_proxy == '/') )
1163 /* Found a proxy name */
1166 char *psz_port = psz_proxy;
1168 /* Skip the hostname part */
1169 while( *psz_port && *psz_port != ':' && *psz_port != '/' )
1174 /* Found a port name */
1179 /* Replace ':' with '\0' */
1183 psz_junk = psz_port;
1184 while( *psz_junk && *psz_junk != '/' )
1194 if( *psz_port != '\0' )
1196 i_proxy_port = atoi( psz_port );
1202 intf_ErrMsg( "input error: http_proxy environment variable is invalid !" );
1203 close( p_input->i_handle );
1204 p_input->b_error = 1;
1208 /* Build socket for proxy connection */
1209 if ( network_BuildAddr( &sock, psz_proxy, i_proxy_port ) == -1 )
1211 intf_ErrMsg( "input error: can't build remote address" );
1212 close( p_input->i_handle );
1213 p_input->b_error = 1;
1219 /* No proxy, direct connection */
1220 if ( network_BuildAddr( &sock, psz_server, i_port ) == -1 )
1222 intf_ErrMsg( "input error: can't build remote address" );
1223 close( p_input->i_handle );
1224 p_input->b_error = 1;
1229 /* Connect the socket */
1230 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
1231 sizeof( sock ) ) == (-1) )
1233 intf_ErrMsg( "input error: can't connect socket (%s)",
1235 close( p_input->i_handle );
1236 p_input->b_error = 1;
1240 p_input->stream.b_seekable = 0;
1241 p_input->stream.b_pace_control = 1; /* TCP/IP... */
1243 # define HTTP_USERAGENT "User-Agent: " COPYRIGHT_MESSAGE "\r\n"
1244 # define HTTP_END "\r\n"
1246 /* Prepare GET ... */
1247 if( psz_proxy != NULL )
1249 snprintf( psz_buffer, sizeof(psz_buffer),
1250 "GET http://%s:%d/%s HTTP/1.0\r\n"
1251 HTTP_USERAGENT HTTP_END,
1252 psz_server, i_port, psz_path );
1256 snprintf( psz_buffer, sizeof(psz_buffer),
1257 "GET /%s HTTP/1.0\r\nHost: %s\r\n"
1258 HTTP_USERAGENT HTTP_END,
1259 psz_path, psz_server );
1261 psz_buffer[sizeof(psz_buffer) - 1] = '\0';
1264 if( write( p_input->i_handle, psz_buffer, strlen( psz_buffer ) ) == (-1) )
1266 intf_ErrMsg( "input error: can't send request (%s)", strerror(errno) );
1267 close( p_input->i_handle );
1268 p_input->b_error = 1;
1272 /* Read HTTP header - this is gonna be fun with plug-ins which do not
1273 * use p_input->p_stream :-( */
1274 if( (p_input->p_stream = fdopen( p_input->i_handle, "r+" )) == NULL )
1276 intf_ErrMsg( "input error: can't reopen socket (%s)", strerror(errno) );
1277 close( p_input->i_handle );
1278 p_input->b_error = 1;
1282 while( !feof( p_input->p_stream ) && !ferror( p_input->p_stream ) )
1284 if( fgets( psz_buffer, sizeof(psz_buffer), p_input->p_stream ) == NULL
1285 || *psz_buffer == '\r' || *psz_buffer == '\0' )
1289 /* FIXME : check Content-Type one day */
1292 intf_WarnMsg( 3, "input: successfully opened HTTP mode" );
1295 #endif /* !defined( SYS_BEOS ) && !defined( SYS_NTO ) */