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.124 2001/06/21 07:22:03 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
;
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_bw
= 0;
159 /* Setup callbacks */
160 p_input
->pf_file_open
= FileOpen
;
161 p_input
->pf_file_close
= FileClose
;
162 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
163 p_input
->pf_network_open
= NetworkOpen
;
164 p_input
->pf_network_close
= NetworkClose
;
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
) )
238 /* If we failed, wait before we are killed, and exit */
239 *p_input
->pi_status
= THREAD_ERROR
;
240 p_input
->b_error
= 1;
241 ErrorThread( p_input
);
242 DestroyThread( p_input
);
247 /* initialization is completed */
248 vlc_mutex_lock( &p_input
->stream
.stream_lock
);
249 p_input
->stream
.b_changed
= 1;
250 vlc_mutex_unlock( &p_input
->stream
.stream_lock
);
252 pp_packets
= (data_packet_t
**) malloc( p_input
->i_read_once
*
253 sizeof( data_packet_t
* ) );
254 if( pp_packets
== NULL
)
256 intf_ErrMsg( "input error: out of memory" );
258 p_input
->b_error
= 1;
261 while( !p_input
->b_die
&& !p_input
->b_error
&& !p_input
->b_eof
)
267 vlc_mutex_lock( &p_input
->stream
.stream_lock
);
269 if( p_input
->stream
.p_new_area
)
271 if( p_input
->stream
.b_seekable
&& p_input
->pf_set_area
!= NULL
)
274 p_input
->pf_set_area( p_input
, p_input
->stream
.p_new_area
);
276 for( i
= 0; i
< p_input
->stream
.i_pgrm_number
; i
++ )
278 pgrm_descriptor_t
* p_pgrm
279 = p_input
->stream
.pp_programs
[i
];
280 /* Escape all decoders for the stream discontinuity they
282 input_EscapeDiscontinuity( p_input
, p_pgrm
);
284 /* Reinitialize synchro. */
285 p_pgrm
->i_synchro_state
= SYNCHRO_REINIT
;
288 p_input
->stream
.p_new_area
= NULL
;
291 if( p_input
->stream
.p_selected_area
->i_seek
!= NO_SEEK
)
293 if( p_input
->stream
.b_seekable
&& p_input
->pf_seek
!= NULL
)
295 p_input
->pf_seek( p_input
,
296 p_input
->stream
.p_selected_area
->i_seek
);
298 for( i
= 0; i
< p_input
->stream
.i_pgrm_number
; i
++ )
300 pgrm_descriptor_t
* p_pgrm
301 = p_input
->stream
.pp_programs
[i
];
302 /* Escape all decoders for the stream discontinuity they
304 input_EscapeDiscontinuity( p_input
, p_pgrm
);
306 /* Reinitialize synchro. */
307 p_pgrm
->i_synchro_state
= SYNCHRO_REINIT
;
310 p_input
->stream
.p_selected_area
->i_seek
= NO_SEEK
;
313 if( p_input
->stream
.p_removed_es
)
315 input_UnselectES( p_input
, p_input
->stream
.p_removed_es
);
316 p_input
->stream
.p_removed_es
= NULL
;
319 if( p_input
->stream
.p_newly_selected_es
)
321 input_SelectES( p_input
, p_input
->stream
.p_newly_selected_es
);
322 p_input
->stream
.p_newly_selected_es
= NULL
;
325 vlc_mutex_unlock( &p_input
->stream
.stream_lock
);
327 i_error
= p_input
->pf_read( p_input
, pp_packets
);
329 /* Demultiplex read packets. */
330 for( i
= 0; i
< p_input
->i_read_once
&& pp_packets
[i
] != NULL
; i
++ )
332 p_input
->pf_demux( p_input
, pp_packets
[i
] );
339 /* End of file - we do not set b_die because only the
340 * interface is allowed to do so. */
341 intf_WarnMsg( 3, "input: EOF reached" );
346 p_input
->b_error
= 1;
353 if( p_input
->b_error
|| p_input
->b_eof
)
355 ErrorThread( p_input
);
358 EndThread( p_input
);
360 DestroyThread( p_input
);
362 intf_DbgMsg("input: Thread end");
365 /*****************************************************************************
366 * InitThread: init the input Thread
367 *****************************************************************************/
368 static int InitThread( input_thread_t
* p_input
)
372 /* Initialize statistics */
373 p_input
->c_loops
= 0;
374 p_input
->c_bytes
= 0;
375 p_input
->c_payload_bytes
= 0;
376 p_input
->c_packets_read
= 0;
377 p_input
->c_packets_trashed
= 0;
381 vlc_mutex_init( &p_input
->stream
.stream_lock
);
382 vlc_cond_init( &p_input
->stream
.stream_wait
);
383 vlc_mutex_init( &p_input
->stream
.control
.control_lock
);
385 /* Default, might get overwritten */
386 p_input
->pf_open
= p_input
->pf_file_open
;
387 p_input
->pf_close
= p_input
->pf_file_close
;
389 p_input
->p_input_module
= module_Need( MODULE_CAPABILITY_INPUT
,
390 (probedata_t
*)p_input
);
392 if( p_input
->p_input_module
== NULL
)
394 intf_ErrMsg( "input error: no suitable input module for `%s'",
399 #define f p_input->p_input_module->p_functions->input.functions.input
400 p_input
->pf_init
= f
.pf_init
;
401 if( f
.pf_open
!= NULL
)
403 p_input
->pf_open
= f
.pf_open
;
405 if( f
.pf_close
!= NULL
)
407 p_input
->pf_close
= f
.pf_close
;
409 p_input
->pf_end
= f
.pf_end
;
410 p_input
->pf_read
= f
.pf_read
;
411 p_input
->pf_set_area
= f
.pf_set_area
;
412 p_input
->pf_demux
= f
.pf_demux
;
413 p_input
->pf_new_packet
= f
.pf_new_packet
;
414 p_input
->pf_new_pes
= f
.pf_new_pes
;
415 p_input
->pf_delete_packet
= f
.pf_delete_packet
;
416 p_input
->pf_delete_pes
= f
.pf_delete_pes
;
417 p_input
->pf_rewind
= f
.pf_rewind
;
418 p_input
->pf_seek
= f
.pf_seek
;
421 /* We found the appropriate plugin, open the target */
422 p_input
->pf_open( p_input
);
424 if( p_input
->b_error
)
426 /* We barfed -- exit nicely */
427 module_Unneed( p_input
->p_input_module
);
431 p_input
->pf_init( p_input
);
433 if( p_input
->b_error
)
435 /* We barfed -- exit nicely */
436 p_input
->pf_close( p_input
);
437 module_Unneed( p_input
->p_input_module
);
441 *p_input
->pi_status
= THREAD_READY
;
446 /*****************************************************************************
447 * ErrorThread: RunThread() error loop
448 *****************************************************************************
449 * This function is called when an error occured during thread main's loop.
450 *****************************************************************************/
451 static void ErrorThread( input_thread_t
*p_input
)
453 while( !p_input
->b_die
)
456 msleep( INPUT_IDLE_SLEEP
);
460 /*****************************************************************************
461 * EndThread: end the input thread
462 *****************************************************************************/
463 static void EndThread( input_thread_t
* p_input
)
465 int * pi_status
; /* thread status */
468 pi_status
= p_input
->pi_status
;
469 *pi_status
= THREAD_END
;
473 struct tms cpu_usage
;
476 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
477 cpu_usage
.tms_utime
, cpu_usage
.tms_stime
);
481 /* Free all ES and destroy all decoder threads */
482 input_EndStream( p_input
);
484 /* Free demultiplexer's data */
485 p_input
->pf_end( p_input
);
488 p_input
->pf_close( p_input
);
490 /* Release modules */
491 module_Unneed( p_input
->p_input_module
);
495 /*****************************************************************************
496 * DestroyThread: destroy the input thread
497 *****************************************************************************/
498 static void DestroyThread( input_thread_t
* p_input
)
500 int * pi_status
; /* thread status */
503 pi_status
= p_input
->pi_status
;
505 /* Destroy Mutex locks */
506 vlc_mutex_destroy( &p_input
->stream
.control
.control_lock
);
507 vlc_mutex_destroy( &p_input
->stream
.stream_lock
);
509 /* Free input structure */
513 *pi_status
= THREAD_OVER
;
516 /*****************************************************************************
517 * FileOpen : open a file descriptor
518 *****************************************************************************/
519 static void FileOpen( input_thread_t
* p_input
)
521 struct stat stat_info
;
524 char *psz_name
= p_input
->p_source
;
526 /* FIXME: this code ought to be in the plugin so that code can
527 * be shared with the *_Probe function */
528 if( ( i_stat
= stat( psz_name
, &stat_info
) ) == (-1) )
530 int i_size
= strlen( psz_name
);
533 && !strncasecmp( psz_name
, "dvd:", 4 ) )
535 /* get rid of the 'dvd:' stuff and try again */
537 i_stat
= stat( psz_name
, &stat_info
);
539 else if( ( i_size
> 5 )
540 && !strncasecmp( psz_name
, "file:", 5 ) )
542 /* get rid of the 'file:' stuff and try again */
544 i_stat
= stat( psz_name
, &stat_info
);
549 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
550 psz_name
, strerror(errno
));
551 p_input
->b_error
= 1;
556 vlc_mutex_lock( &p_input
->stream
.stream_lock
);
558 /* If we are here we can control the pace... */
559 p_input
->stream
.b_pace_control
= 1;
561 if( S_ISREG(stat_info
.st_mode
) || S_ISCHR(stat_info
.st_mode
)
562 || S_ISBLK(stat_info
.st_mode
) )
564 p_input
->stream
.b_seekable
= 1;
565 p_input
->stream
.p_selected_area
->i_size
= stat_info
.st_size
;
567 else if( S_ISFIFO(stat_info
.st_mode
)
568 #if !defined( SYS_BEOS ) && !defined( WIN32 )
569 || S_ISSOCK(stat_info
.st_mode
)
573 p_input
->stream
.b_seekable
= 0;
574 p_input
->stream
.p_selected_area
->i_size
= 0;
578 vlc_mutex_unlock( &p_input
->stream
.stream_lock
);
579 intf_ErrMsg( "input error: unknown file type for `%s'",
581 p_input
->b_error
= 1;
585 p_input
->stream
.p_selected_area
->i_tell
= 0;
586 vlc_mutex_unlock( &p_input
->stream
.stream_lock
);
588 intf_WarnMsg( 1, "input: opening file `%s'", p_input
->p_source
);
590 if( (p_input
->i_handle
= open( psz_name
, O_BINARY
) ) == (-1) )
592 if( (p_input
->i_handle
= open( psz_name
,
593 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
596 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno
) );
597 p_input
->b_error
= 1;
603 /*****************************************************************************
604 * FileClose : close a file descriptor
605 *****************************************************************************/
606 static void FileClose( input_thread_t
* p_input
)
608 intf_WarnMsg( 1, "input: closing file `%s'", p_input
->p_source
);
610 close( p_input
->i_handle
);
615 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
616 /*****************************************************************************
617 * NetworkOpen : open a network socket
618 *****************************************************************************/
619 static void NetworkOpen( input_thread_t
* p_input
)
621 char *psz_server
= NULL
;
622 char *psz_broadcast
= NULL
;
625 struct sockaddr_in sock
;
632 /* WinSock Library Init. */
633 i_err
= WSAStartup( MAKEWORD( 1, 1 ), &Data
);
637 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err
);
642 /* Get the remote server */
643 if( p_input
->p_source
!= NULL
)
645 psz_server
= p_input
->p_source
;
647 /* Skip the protocol name */
648 while( *psz_server
&& *psz_server
!= ':' )
653 /* Skip the "://" part */
654 while( *psz_server
&& (*psz_server
== ':' || *psz_server
== '/') )
659 /* Found a server name */
662 char *psz_port
= psz_server
;
664 /* Skip the hostname part */
665 while( *psz_port
&& *psz_port
!= ':' )
670 /* Found a port name */
673 /* Replace ':' with '\0' */
677 psz_broadcast
= psz_port
;
678 while( *psz_broadcast
&& *psz_broadcast
!= ':' )
685 *psz_broadcast
= '\0';
687 while( *psz_broadcast
&& *psz_broadcast
== ':' )
694 psz_broadcast
= NULL
;
697 /* port before broadcast address */
698 if( *psz_port
!= ':' )
700 i_port
= atoi( psz_port
);
710 /* Check that we got a valid server */
711 if( psz_server
== NULL
)
713 psz_server
= main_GetPszVariable( INPUT_SERVER_VAR
,
714 INPUT_SERVER_DEFAULT
);
717 /* Check that we got a valid port */
720 i_port
= main_GetIntVariable( INPUT_PORT_VAR
, INPUT_PORT_DEFAULT
);
723 if( psz_broadcast
== NULL
)
725 /* Are we broadcasting ? */
726 if( main_GetIntVariable( INPUT_BROADCAST_VAR
,
727 INPUT_BROADCAST_DEFAULT
) )
729 psz_broadcast
= main_GetPszVariable( INPUT_BCAST_ADDR_VAR
,
730 INPUT_BCAST_ADDR_DEFAULT
);
734 psz_broadcast
= NULL
;
738 intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
739 psz_server
, i_port
, psz_broadcast
);
741 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
743 p_input
->i_handle
= socket( AF_INET
, SOCK_DGRAM
, 0 );
744 if( p_input
->i_handle
== -1 )
746 intf_ErrMsg("input error: can't create socket : %s", strerror(errno
));
747 p_input
->b_error
= 1;
751 /* We may want to reuse an already used socket */
753 if( setsockopt( p_input
->i_handle
, SOL_SOCKET
, SO_REUSEADDR
,
754 (void*) &i_opt
, sizeof( i_opt
) ) == -1 )
756 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
758 close( p_input
->i_handle
);
759 p_input
->b_error
= 1;
763 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
764 * packet loss caused by scheduling problems */
766 if( setsockopt( p_input
->i_handle
, SOL_SOCKET
, SO_RCVBUF
,
767 (void*) &i_opt
, sizeof( i_opt
) ) == -1 )
769 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
771 close( p_input
->i_handle
);
772 p_input
->b_error
= 1;
776 /* Build the local socket */
777 if ( network_BuildLocalAddr( &sock
, i_port
, psz_broadcast
) == -1 )
779 intf_ErrMsg( "input error: can't build local address" );
780 close( p_input
->i_handle
);
781 p_input
->b_error
= 1;
786 if ( psz_broadcast
!= NULL
)
788 sock
.sin_addr
.s_addr
= INADDR_ANY
;
793 if( bind( p_input
->i_handle
, (struct sockaddr
*)&sock
,
794 sizeof( sock
) ) < 0 )
796 intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno
));
797 close( p_input
->i_handle
);
798 p_input
->b_error
= 1;
802 /* Build socket for remote connection */
803 if ( network_BuildRemoteAddr( &sock
, psz_server
) == -1 )
805 intf_ErrMsg( "input error: can't build remote address" );
806 close( p_input
->i_handle
);
807 p_input
->b_error
= 1;
811 /* And connect it ... should we really connect ? */
812 if( connect( p_input
->i_handle
, (struct sockaddr
*) &sock
,
813 sizeof( sock
) ) == (-1) )
815 intf_ErrMsg( "input error: can't connect socket, %s",
817 close( p_input
->i_handle
);
818 p_input
->b_error
= 1;
822 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
823 * with the server. */
824 p_input
->stream
.b_pace_control
= 0;
825 p_input
->stream
.b_seekable
= 0;
827 intf_WarnMsg( 3, "input: successfully opened network mode" );
832 /*****************************************************************************
833 * NetworkClose : close a network socket
834 *****************************************************************************/
835 static void NetworkClose( input_thread_t
* p_input
)
837 close( p_input
->i_handle
);