1 /*****************************************************************************
2 * input.c: input thread
3 * Read an MPEG2 stream, demultiplex and parse it before sending it to
5 *****************************************************************************
6 * Copyright (C) 1998, 1999, 2000 VideoLAN
7 * $Id: input.c,v 1.129 2001/08/09 08:20:26 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 #elif !defined( SYS_BEOS ) && !defined( SYS_NTO )
52 # include <netdb.h> /* hostent ... */
53 # include <sys/socket.h>
54 # include <netinet/in.h>
55 # include <arpa/inet.h>
56 # include <sys/types.h>
57 # include <sys/socket.h>
61 # include <sys/times.h>
72 #include "intf_playlist.h"
74 #include "stream_control.h"
75 #include "input_ext-intf.h"
76 #include "input_ext-dec.h"
77 #include "input_ext-plugins.h"
79 #include "interface.h"
83 /*****************************************************************************
85 *****************************************************************************/
86 static void RunThread ( input_thread_t *p_input );
87 static int InitThread ( input_thread_t *p_input );
88 static void ErrorThread ( 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 FileClose ( input_thread_t *p_input );
94 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
95 static void NetworkOpen ( input_thread_t *p_input );
96 static void NetworkClose ( input_thread_t *p_input );
99 /*****************************************************************************
100 * input_CreateThread: creates a new input thread
101 *****************************************************************************
102 * This function creates a new input, and returns a pointer
103 * to its description. On error, it returns NULL.
104 * If pi_status is NULL, then the function will block until the thread is ready.
105 * If not, it will be updated using one of the THREAD_* constants.
106 *****************************************************************************/
107 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
109 input_thread_t * p_input; /* thread descriptor */
110 int i_status; /* thread status */
112 /* Allocate descriptor */
113 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
114 if( p_input == NULL )
116 intf_ErrMsg( "input error: can't allocate input thread (%s)",
121 /* Packets read once */
122 p_input->i_read_once = INPUT_READ_ONCE;
124 /* Initialize thread properties */
126 p_input->b_error = 0;
130 p_input->p_source = p_item->psz_name;
132 /* I have never understood that stuff --Meuuh */
133 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
134 *p_input->pi_status = THREAD_CREATE;
136 /* Initialize stream description */
137 p_input->stream.i_es_number = 0;
138 p_input->stream.i_selected_es_number = 0;
139 p_input->stream.i_pgrm_number = 0;
140 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
141 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
142 p_input->stream.i_mux_rate = 0;
144 /* no stream, no area */
145 p_input->stream.i_area_nb = 0;
146 p_input->stream.pp_areas = NULL;
147 p_input->stream.p_selected_area = NULL;
148 p_input->stream.p_new_area = NULL;
150 /* By default there is one area in a stream */
151 input_AddArea( p_input );
152 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
154 /* Initialize stream control properties. */
155 p_input->stream.control.i_status = PLAYING_S;
156 p_input->stream.control.i_rate = DEFAULT_RATE;
157 p_input->stream.control.b_mute = 0;
158 p_input->stream.control.b_grayscale = main_GetIntVariable(
159 VOUT_GRAYSCALE_VAR, VOUT_GRAYSCALE_DEFAULT );
160 p_input->stream.control.i_smp = main_GetIntVariable(
161 VDEC_SMP_VAR, VDEC_SMP_DEFAULT );
163 /* Setup callbacks */
164 p_input->pf_file_open = FileOpen;
165 p_input->pf_file_close = FileClose;
166 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
167 p_input->pf_network_open = NetworkOpen;
168 p_input->pf_network_close = NetworkClose;
171 intf_WarnMsg( 1, "input: playlist item `%s'", p_input->p_source );
174 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
177 intf_ErrMsg( "input error: can't create input thread (%s)",
183 /* If status is NULL, wait until the thread is created */
184 if( pi_status == NULL )
188 msleep( THREAD_SLEEP );
189 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
190 && (i_status != THREAD_FATAL) );
191 if( i_status != THREAD_READY )
199 /*****************************************************************************
200 * input_DestroyThread: mark an input thread as zombie
201 *****************************************************************************
202 * This function should not return until the thread is effectively cancelled.
203 *****************************************************************************/
204 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
206 int i_status; /* thread status */
209 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
210 *p_input->pi_status = THREAD_DESTROY;
212 /* Request thread destruction */
215 /* Make the thread exit of an eventual vlc_cond_wait() */
216 vlc_mutex_lock( &p_input->stream.stream_lock );
217 vlc_cond_signal( &p_input->stream.stream_wait );
218 vlc_mutex_unlock( &p_input->stream.stream_lock );
220 /* If status is NULL, wait until thread has been destroyed */
221 if( pi_status == NULL )
225 msleep( THREAD_SLEEP );
226 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
227 && (i_status != THREAD_FATAL) );
231 /*****************************************************************************
232 * RunThread: main thread loop
233 *****************************************************************************
234 * Thread in charge of processing the network packets and demultiplexing.
235 *****************************************************************************/
236 static void RunThread( input_thread_t *p_input )
239 data_packet_t ** pp_packets;
241 if( InitThread( p_input ) )
243 /* If we failed, wait before we are killed, and exit */
244 *p_input->pi_status = THREAD_ERROR;
245 p_input->b_error = 1;
246 ErrorThread( p_input );
247 DestroyThread( p_input );
251 /* initialization is completed */
252 vlc_mutex_lock( &p_input->stream.stream_lock );
253 p_input->stream.b_changed = 1;
254 vlc_mutex_unlock( &p_input->stream.stream_lock );
256 pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
257 sizeof( data_packet_t * ) );
258 if( pp_packets == NULL )
260 intf_ErrMsg( "input error: out of memory" );
262 p_input->b_error = 1;
265 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
271 vlc_mutex_lock( &p_input->stream.stream_lock );
273 if( p_input->stream.p_new_area )
275 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
278 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
280 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
282 pgrm_descriptor_t * p_pgrm
283 = p_input->stream.pp_programs[i];
284 /* Escape all decoders for the stream discontinuity they
286 input_EscapeDiscontinuity( p_input, p_pgrm );
288 /* Reinitialize synchro. */
289 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
292 p_input->stream.p_new_area = NULL;
295 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
297 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
299 p_input->pf_seek( p_input,
300 p_input->stream.p_selected_area->i_seek );
302 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
304 pgrm_descriptor_t * p_pgrm
305 = p_input->stream.pp_programs[i];
306 /* Escape all decoders for the stream discontinuity they
308 input_EscapeDiscontinuity( p_input, p_pgrm );
310 /* Reinitialize synchro. */
311 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
314 p_input->stream.p_selected_area->i_seek = NO_SEEK;
317 if( p_input->stream.p_removed_es )
319 input_UnselectES( p_input, p_input->stream.p_removed_es );
320 p_input->stream.p_removed_es = NULL;
323 if( p_input->stream.p_newly_selected_es )
325 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
326 p_input->stream.p_newly_selected_es = NULL;
329 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
331 if( p_input->stream.b_new_mute )
333 input_EscapeAudioDiscontinuity( p_input );
336 vlc_mutex_lock( &p_input->stream.control.control_lock );
337 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
338 vlc_mutex_unlock( &p_input->stream.control.control_lock );
340 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
343 vlc_mutex_unlock( &p_input->stream.stream_lock );
345 i_error = p_input->pf_read( p_input, pp_packets );
347 /* Demultiplex read packets. */
348 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
350 p_input->pf_demux( p_input, pp_packets[i] );
357 /* End of file - we do not set b_die because only the
358 * interface is allowed to do so. */
359 intf_WarnMsg( 3, "input: EOF reached" );
364 p_input->b_error = 1;
371 if( p_input->b_error || p_input->b_eof )
373 ErrorThread( p_input );
376 EndThread( p_input );
378 DestroyThread( p_input );
380 intf_DbgMsg("input: Thread end");
383 /*****************************************************************************
384 * InitThread: init the input Thread
385 *****************************************************************************/
386 static int InitThread( input_thread_t * p_input )
390 /* Initialize statistics */
391 p_input->c_loops = 0;
392 p_input->c_bytes = 0;
393 p_input->c_payload_bytes = 0;
394 p_input->c_packets_read = 0;
395 p_input->c_packets_trashed = 0;
399 vlc_mutex_init( &p_input->stream.stream_lock );
400 vlc_cond_init( &p_input->stream.stream_wait );
401 vlc_mutex_init( &p_input->stream.control.control_lock );
403 /* Default, might get overwritten */
404 p_input->pf_open = p_input->pf_file_open;
405 p_input->pf_close = p_input->pf_file_close;
407 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
408 (probedata_t *)p_input );
410 if( p_input->p_input_module == NULL )
412 intf_ErrMsg( "input error: no suitable input module for `%s'",
417 #define f p_input->p_input_module->p_functions->input.functions.input
418 p_input->pf_init = f.pf_init;
419 if( f.pf_open != NULL )
421 p_input->pf_open = f.pf_open;
423 if( f.pf_close != NULL )
425 p_input->pf_close = f.pf_close;
427 p_input->pf_end = f.pf_end;
428 p_input->pf_init_bit_stream= f.pf_init_bit_stream;
429 p_input->pf_read = f.pf_read;
430 p_input->pf_set_area = f.pf_set_area;
431 p_input->pf_demux = f.pf_demux;
432 p_input->pf_new_packet = f.pf_new_packet;
433 p_input->pf_new_pes = f.pf_new_pes;
434 p_input->pf_delete_packet = f.pf_delete_packet;
435 p_input->pf_delete_pes = f.pf_delete_pes;
436 p_input->pf_rewind = f.pf_rewind;
437 p_input->pf_seek = f.pf_seek;
440 /* We found the appropriate plugin, open the target */
441 p_input->pf_open( p_input );
443 if( p_input->b_error )
445 /* We barfed -- exit nicely */
446 module_Unneed( p_input->p_input_module );
450 p_input->pf_init( p_input );
452 if( p_input->b_error )
454 /* We barfed -- exit nicely */
455 p_input->pf_close( p_input );
456 module_Unneed( p_input->p_input_module );
460 *p_input->pi_status = THREAD_READY;
465 /*****************************************************************************
466 * ErrorThread: RunThread() error loop
467 *****************************************************************************
468 * This function is called when an error occured during thread main's loop.
469 *****************************************************************************/
470 static void ErrorThread( input_thread_t *p_input )
472 while( !p_input->b_die )
475 msleep( INPUT_IDLE_SLEEP );
479 /*****************************************************************************
480 * EndThread: end the input thread
481 *****************************************************************************/
482 static void EndThread( input_thread_t * p_input )
484 int * pi_status; /* thread status */
487 pi_status = p_input->pi_status;
488 *pi_status = THREAD_END;
492 struct tms cpu_usage;
495 intf_Msg( "input stats: cpu usage (user: %d, system: %d)",
496 cpu_usage.tms_utime, cpu_usage.tms_stime );
500 /* Free all ES and destroy all decoder threads */
501 input_EndStream( p_input );
503 /* Free demultiplexer's data */
504 p_input->pf_end( p_input );
507 p_input->pf_close( p_input );
509 /* Release modules */
510 module_Unneed( p_input->p_input_module );
514 /*****************************************************************************
515 * DestroyThread: destroy the input thread
516 *****************************************************************************/
517 static void DestroyThread( input_thread_t * p_input )
519 int * pi_status; /* thread status */
522 pi_status = p_input->pi_status;
524 /* Destroy Mutex locks */
525 vlc_mutex_destroy( &p_input->stream.control.control_lock );
526 vlc_mutex_destroy( &p_input->stream.stream_lock );
528 /* Free input structure */
532 *pi_status = THREAD_OVER;
535 /*****************************************************************************
536 * FileOpen : open a file descriptor
537 *****************************************************************************/
538 static void FileOpen( input_thread_t * p_input )
540 struct stat stat_info;
543 char *psz_name = p_input->p_source;
545 /* FIXME: this code ought to be in the plugin so that code can
546 * be shared with the *_Probe function */
547 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
549 int i_size = strlen( psz_name );
552 && !strncasecmp( psz_name, "dvd:", 4 ) )
554 /* get rid of the 'dvd:' stuff and try again */
556 i_stat = stat( psz_name, &stat_info );
558 else if( ( i_size > 5 )
559 && !strncasecmp( psz_name, "file:", 5 ) )
561 /* get rid of the 'file:' stuff and try again */
563 i_stat = stat( psz_name, &stat_info );
568 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
569 psz_name, strerror(errno));
570 p_input->b_error = 1;
575 vlc_mutex_lock( &p_input->stream.stream_lock );
577 /* If we are here we can control the pace... */
578 p_input->stream.b_pace_control = 1;
580 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
581 || S_ISBLK(stat_info.st_mode) )
583 p_input->stream.b_seekable = 1;
584 p_input->stream.p_selected_area->i_size = stat_info.st_size;
586 else if( S_ISFIFO(stat_info.st_mode)
587 #if !defined( SYS_BEOS ) && !defined( WIN32 )
588 || S_ISSOCK(stat_info.st_mode)
592 p_input->stream.b_seekable = 0;
593 p_input->stream.p_selected_area->i_size = 0;
597 vlc_mutex_unlock( &p_input->stream.stream_lock );
598 intf_ErrMsg( "input error: unknown file type for `%s'",
600 p_input->b_error = 1;
604 p_input->stream.p_selected_area->i_tell = 0;
605 vlc_mutex_unlock( &p_input->stream.stream_lock );
607 intf_WarnMsg( 2, "input: opening file `%s'", p_input->p_source );
608 if( (p_input->i_handle = open( psz_name,
609 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
611 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
612 p_input->b_error = 1;
618 /*****************************************************************************
619 * FileClose : close a file descriptor
620 *****************************************************************************/
621 static void FileClose( input_thread_t * p_input )
623 intf_WarnMsg( 2, "input: closing file `%s'", p_input->p_source );
625 close( p_input->i_handle );
630 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
631 /*****************************************************************************
632 * NetworkOpen : open a network socket
633 *****************************************************************************/
634 static void NetworkOpen( input_thread_t * p_input )
636 char *psz_server = NULL;
637 char *psz_broadcast = NULL;
640 struct sockaddr_in sock;
647 /* WinSock Library Init. */
648 i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
652 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
657 /* Get the remote server */
658 if( p_input->p_source != NULL )
660 psz_server = p_input->p_source;
662 /* Skip the protocol name */
663 while( *psz_server && *psz_server != ':' )
668 /* Skip the "://" part */
669 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
674 /* Found a server name */
677 char *psz_port = psz_server;
679 /* Skip the hostname part */
680 while( *psz_port && *psz_port != ':' )
685 /* Found a port name */
688 /* Replace ':' with '\0' */
692 psz_broadcast = psz_port;
693 while( *psz_broadcast && *psz_broadcast != ':' )
700 *psz_broadcast = '\0';
702 while( *psz_broadcast && *psz_broadcast == ':' )
709 psz_broadcast = NULL;
712 /* port before broadcast address */
713 if( *psz_port != ':' )
715 i_port = atoi( psz_port );
725 /* Check that we got a valid server */
726 if( psz_server == NULL )
728 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
729 INPUT_SERVER_DEFAULT );
732 /* Check that we got a valid port */
735 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
738 if( psz_broadcast == NULL )
740 /* Are we broadcasting ? */
741 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
742 INPUT_BROADCAST_DEFAULT ) )
744 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
745 INPUT_BCAST_ADDR_DEFAULT );
749 psz_broadcast = NULL;
753 intf_WarnMsg( 2, "input: server=%s port=%d broadcast=%s",
754 psz_server, i_port, psz_broadcast );
756 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
758 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
759 if( p_input->i_handle == -1 )
761 intf_ErrMsg( "input error: can't create socket (%s)", strerror(errno) );
762 p_input->b_error = 1;
766 /* We may want to reuse an already used socket */
768 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
769 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
771 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
773 close( p_input->i_handle );
774 p_input->b_error = 1;
778 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
779 * packet loss caused by scheduling problems */
781 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
782 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
784 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
786 close( p_input->i_handle );
787 p_input->b_error = 1;
791 /* Build the local socket */
792 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
794 intf_ErrMsg( "input error: can't build local address" );
795 close( p_input->i_handle );
796 p_input->b_error = 1;
801 if ( psz_broadcast != NULL )
803 sock.sin_addr.s_addr = INADDR_ANY;
808 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
809 sizeof( sock ) ) < 0 )
811 intf_ErrMsg( "input error: can't bind socket (%s)", strerror(errno) );
812 close( p_input->i_handle );
813 p_input->b_error = 1;
817 /* Build socket for remote connection */
818 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
820 intf_ErrMsg( "input error: can't build remote address" );
821 close( p_input->i_handle );
822 p_input->b_error = 1;
826 /* And connect it ... should we really connect ? */
827 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
828 sizeof( sock ) ) == (-1) )
830 intf_ErrMsg( "input error: can't connect socket (%s)",
832 close( p_input->i_handle );
833 p_input->b_error = 1;
837 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
838 * with the server. */
839 p_input->stream.b_pace_control = 0;
840 p_input->stream.b_seekable = 0;
842 intf_WarnMsg( 3, "input: successfully opened network mode" );
847 /*****************************************************************************
848 * NetworkClose : close a network socket
849 *****************************************************************************/
850 static void NetworkClose( input_thread_t * p_input )
852 close( p_input->i_handle );