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.119 2001/06/07 01:10:33 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"
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.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;
148 /* By default there is one areas in a stream */
149 input_AddArea( p_input );
150 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
152 /* Initialize stream control properties. */
153 p_input->stream.control.i_status = PLAYING_S;
154 p_input->stream.control.i_rate = DEFAULT_RATE;
155 p_input->stream.control.b_mute = 0;
156 p_input->stream.control.b_bw = 0;
158 /* Setup callbacks */
159 p_input->pf_file_open = FileOpen;
160 p_input->pf_file_close = FileClose;
161 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
162 p_input->pf_network_open = NetworkOpen;
163 p_input->pf_network_close = NetworkClose;
166 /* Create thread and set locks. */
167 vlc_mutex_init( &p_input->stream.stream_lock );
168 vlc_cond_init( &p_input->stream.stream_wait );
169 vlc_mutex_init( &p_input->stream.control.control_lock );
170 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
173 intf_ErrMsg( "input error: can't create input thread (%s)",
179 /* If status is NULL, wait until the thread is created */
180 if( pi_status == NULL )
184 msleep( THREAD_SLEEP );
185 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
186 && (i_status != THREAD_FATAL) );
187 if( i_status != THREAD_READY )
195 /*****************************************************************************
196 * input_DestroyThread: mark an input thread as zombie
197 *****************************************************************************
198 * This function should not return until the thread is effectively cancelled.
199 *****************************************************************************/
200 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
202 int i_status; /* thread status */
205 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
206 *p_input->pi_status = THREAD_DESTROY;
208 /* Request thread destruction */
211 /* Make the thread exit of an eventual vlc_cond_wait() */
212 vlc_mutex_lock( &p_input->stream.stream_lock );
213 vlc_cond_signal( &p_input->stream.stream_wait );
214 vlc_mutex_unlock( &p_input->stream.stream_lock );
216 /* If status is NULL, wait until thread has been destroyed */
217 if( pi_status == NULL )
221 msleep( THREAD_SLEEP );
222 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
223 && (i_status != THREAD_FATAL) );
227 /*****************************************************************************
228 * RunThread: main thread loop
229 *****************************************************************************
230 * Thread in charge of processing the network packets and demultiplexing.
231 *****************************************************************************/
232 static void RunThread( input_thread_t *p_input )
235 data_packet_t ** pp_packets;
237 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 )
268 vlc_mutex_lock( &p_input->stream.stream_lock );
270 if( p_input->stream.p_new_area )
272 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
273 p_input->stream.p_new_area = NULL;
276 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
278 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
280 p_input->pf_seek( p_input,
281 p_input->stream.p_selected_area->i_seek );
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_selected_area->i_seek = NO_SEEK;
298 if( p_input->stream.p_removed_es )
300 input_UnselectES( p_input, p_input->stream.p_removed_es );
301 p_input->stream.p_removed_es = NULL;
304 if( p_input->stream.p_newly_selected_es )
306 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
307 p_input->stream.p_newly_selected_es = NULL;
310 vlc_mutex_unlock( &p_input->stream.stream_lock );
312 i_error = p_input->pf_read( p_input, pp_packets );
314 /* Demultiplex read packets. */
315 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
317 p_input->pf_demux( p_input, pp_packets[i] );
324 /* End of file - we do not set b_die because only the
325 * interface is allowed to do so. */
326 intf_WarnMsg( 3, "input: EOF reached" );
331 p_input->b_error = 1;
338 if( p_input->b_error || p_input->b_eof )
340 ErrorThread( p_input );
343 EndThread( p_input );
345 DestroyThread( p_input );
347 intf_DbgMsg("input: Thread end");
350 /*****************************************************************************
351 * InitThread: init the input Thread
352 *****************************************************************************/
353 static int InitThread( input_thread_t * p_input )
357 /* Initialize statistics */
358 p_input->c_loops = 0;
359 p_input->c_bytes = 0;
360 p_input->c_payload_bytes = 0;
361 p_input->c_packets_read = 0;
362 p_input->c_packets_trashed = 0;
365 /* Default, might get overwritten */
366 p_input->pf_open = p_input->pf_file_open;
367 p_input->pf_close = p_input->pf_file_close;
369 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
370 (probedata_t *)p_input );
372 if( p_input->p_input_module == NULL )
374 intf_ErrMsg( "input error: no suitable input module for `%s'",
379 #define f p_input->p_input_module->p_functions->input.functions.input
380 p_input->pf_init = f.pf_init;
381 if( f.pf_open != NULL )
383 p_input->pf_open = f.pf_open;
385 if( f.pf_close != NULL )
387 p_input->pf_close = f.pf_close;
389 p_input->pf_end = f.pf_end;
390 p_input->pf_read = f.pf_read;
391 p_input->pf_set_area = f.pf_set_area;
392 p_input->pf_demux = f.pf_demux;
393 p_input->pf_new_packet = f.pf_new_packet;
394 p_input->pf_new_pes = f.pf_new_pes;
395 p_input->pf_delete_packet = f.pf_delete_packet;
396 p_input->pf_delete_pes = f.pf_delete_pes;
397 p_input->pf_rewind = f.pf_rewind;
398 p_input->pf_seek = f.pf_seek;
401 /* We found the appropriate plugin, open the target */
402 p_input->pf_open( p_input );
404 if( p_input->b_error )
406 /* We barfed -- exit nicely */
407 p_input->pf_close( p_input );
408 module_Unneed( p_input->p_input_module );
412 p_input->pf_init( p_input );
414 if( p_input->b_error )
416 /* We barfed -- exit nicely */
417 p_input->pf_close( p_input );
418 module_Unneed( p_input->p_input_module );
422 *p_input->pi_status = THREAD_READY;
427 /*****************************************************************************
428 * ErrorThread: RunThread() error loop
429 *****************************************************************************
430 * This function is called when an error occured during thread main's loop.
431 *****************************************************************************/
432 static void ErrorThread( input_thread_t *p_input )
434 while( !p_input->b_die )
437 msleep( INPUT_IDLE_SLEEP );
441 /*****************************************************************************
442 * EndThread: end the input thread
443 *****************************************************************************/
444 static void EndThread( input_thread_t * p_input )
446 int * pi_status; /* thread status */
449 pi_status = p_input->pi_status;
450 *pi_status = THREAD_END;
454 struct tms cpu_usage;
457 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
458 cpu_usage.tms_utime, cpu_usage.tms_stime);
462 /* Free all ES and destroy all decoder threads */
463 input_EndStream( p_input );
465 /* Free demultiplexer's data */
466 p_input->pf_end( p_input );
469 p_input->pf_close( p_input );
471 /* Release modules */
472 module_Unneed( p_input->p_input_module );
476 /*****************************************************************************
477 * DestroyThread: destroy the input thread
478 *****************************************************************************/
479 static void DestroyThread( input_thread_t * p_input )
481 int * pi_status; /* thread status */
484 pi_status = p_input->pi_status;
486 /* Destroy Mutex locks */
487 vlc_mutex_destroy( &p_input->stream.control.control_lock );
488 vlc_mutex_destroy( &p_input->stream.stream_lock );
490 /* Free input structure */
494 *pi_status = THREAD_OVER;
497 /*****************************************************************************
498 * FileOpen : open a file descriptor
499 *****************************************************************************/
500 static void FileOpen( input_thread_t * p_input )
502 struct stat stat_info;
509 char *psz_name = p_input->p_source;
511 /* FIXME: this code ought to be in the plugin so that code can
512 * be shared with the *_Probe function */
513 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
515 int i_size = strlen( psz_name );
518 && !strncasecmp( psz_name, "dvd:", 4 ) )
520 /* get rid of the 'dvd:' stuff and try again */
522 i_stat = stat( psz_name, &stat_info );
524 _snprintf( buf, 7, "\\\\.\\%c:", psz_name[0] );
527 else if( ( i_size > 5 )
528 && !strncasecmp( psz_name, "file:", 5 ) )
530 /* get rid of the 'file:' stuff and try again */
532 i_stat = stat( psz_name, &stat_info );
541 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
542 psz_name, strerror(errno));
543 p_input->b_error = 1;
548 vlc_mutex_lock( &p_input->stream.stream_lock );
550 /* If we are here we can control the pace... */
551 p_input->stream.b_pace_control = 1;
553 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
554 || S_ISBLK(stat_info.st_mode)
556 || ( buf[0] && ( ( stat_info.st_size = 0 ) == 0 ) )
560 p_input->stream.b_seekable = 1;
561 p_input->stream.p_selected_area->i_size = stat_info.st_size;
563 else if( S_ISFIFO(stat_info.st_mode)
564 #if !defined( SYS_BEOS ) && !defined( WIN32 )
565 || S_ISSOCK(stat_info.st_mode)
569 p_input->stream.b_seekable = 0;
570 p_input->stream.p_selected_area->i_size = 0;
574 vlc_mutex_unlock( &p_input->stream.stream_lock );
575 intf_ErrMsg( "input error: unknown file type for `%s'",
577 p_input->b_error = 1;
581 p_input->stream.p_selected_area->i_tell = 0;
582 vlc_mutex_unlock( &p_input->stream.stream_lock );
584 intf_WarnMsg( 1, "input: opening file `%s'", p_input->p_source );
586 if( (p_input->i_handle = open( psz_name,
587 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
589 if( ( buf[0] && ( (HANDLE) p_input->i_handle = CreateFile( buf,
590 GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
591 NULL, OPEN_EXISTING, 0, NULL ) ) == INVALID_HANDLE_VALUE ) ||
592 ( !buf[0] && (p_input->i_handle = open( psz_name, O_BINARY ) ) == (-1) ) )
595 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
596 p_input->b_error = 1;
602 /*****************************************************************************
603 * FileClose : close a file descriptor
604 *****************************************************************************/
605 static void FileClose( input_thread_t * p_input )
607 intf_WarnMsg( 1, "input: closing file `%s'", p_input->p_source );
609 if( ( strlen( p_input->p_source ) > 4 ) &&
610 !strncasecmp( p_input->p_source, "dvd:", 4 ) )
612 CloseHandle( (HANDLE) p_input->i_handle );
616 close( p_input->i_handle );
621 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
622 /*****************************************************************************
623 * NetworkOpen : open a network socket
624 *****************************************************************************/
625 static void NetworkOpen( input_thread_t * p_input )
627 char *psz_server = NULL;
628 char *psz_broadcast = NULL;
631 struct sockaddr_in sock;
634 /* WinSock Library Init. */
636 int i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
640 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
645 /* Get the remote server */
646 if( p_input->p_source != NULL )
648 psz_server = p_input->p_source;
650 /* Skip the protocol name */
651 while( *psz_server && *psz_server != ':' )
656 /* Skip the "://" part */
657 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
662 /* Found a server name */
665 char *psz_port = psz_server;
667 /* Skip the hostname part */
668 while( *psz_port && *psz_port != ':' )
673 /* Found a port name */
676 /* Replace ':' with '\0' */
680 psz_broadcast = psz_port;
681 while( *psz_broadcast && *psz_broadcast != ':' )
688 *psz_broadcast = '\0';
690 while( *psz_broadcast && *psz_broadcast == ':' )
697 psz_broadcast = NULL;
700 /* port before broadcast address */
701 if( *psz_port != ':' )
703 i_port = atoi( psz_port );
713 /* Check that we got a valid server */
714 if( psz_server == NULL )
716 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
717 INPUT_SERVER_DEFAULT );
720 /* Check that we got a valid port */
723 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
726 if( psz_broadcast == NULL )
728 /* Are we broadcasting ? */
729 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
730 INPUT_BROADCAST_DEFAULT ) )
732 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
733 INPUT_BCAST_ADDR_DEFAULT );
737 psz_broadcast = NULL;
741 intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
742 psz_server, i_port, psz_broadcast );
744 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
746 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
747 if( p_input->i_handle == -1 )
749 intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
750 p_input->b_error = 1;
754 /* We may want to reuse an already used socket */
756 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
757 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
759 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
761 close( p_input->i_handle );
762 p_input->b_error = 1;
766 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
767 * packet loss caused by scheduling problems */
769 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
770 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
772 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
774 close( p_input->i_handle );
775 p_input->b_error = 1;
779 /* Build the local socket */
780 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
782 intf_ErrMsg( "input error: can't build local address" );
783 close( p_input->i_handle );
784 p_input->b_error = 1;
789 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
790 sizeof( sock ) ) < 0 )
792 intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
793 close( p_input->i_handle );
794 p_input->b_error = 1;
798 /* Build socket for remote connection */
799 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
801 intf_ErrMsg( "input error: can't build remote address" );
802 close( p_input->i_handle );
803 p_input->b_error = 1;
807 /* And connect it ... should we really connect ? */
808 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
809 sizeof( sock ) ) == (-1) )
811 intf_ErrMsg( "input error: can't connect socket, %s",
813 close( p_input->i_handle );
814 p_input->b_error = 1;
818 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
819 * with the server. */
820 p_input->stream.b_pace_control = 0;
821 p_input->stream.b_seekable = 0;
823 intf_WarnMsg( 3, "input: successfully opened network mode" );
828 /*****************************************************************************
829 * NetworkClose : close a network socket
830 *****************************************************************************/
831 static void NetworkClose( input_thread_t * p_input )
833 close( p_input->i_handle );