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.115 2001/05/31 01:37:08 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 )
43 #ifdef STRNCASECMP_IN_STRINGS_H
48 /* WinSock Includes */
55 /* Network functions */
57 #if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
58 #include <netdb.h> /* hostent ... */
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
62 #include <sys/types.h>
63 #include <sys/socket.h>
67 # include <sys/times.h>
78 #include "intf_playlist.h"
80 #include "stream_control.h"
81 #include "input_ext-intf.h"
82 #include "input_ext-dec.h"
85 #include "interface.h"
90 /*****************************************************************************
92 *****************************************************************************/
93 static void RunThread ( input_thread_t *p_input );
94 static int InitThread ( input_thread_t *p_input );
95 static void ErrorThread ( input_thread_t *p_input );
96 static void DestroyThread ( input_thread_t *p_input );
97 static void EndThread ( input_thread_t *p_input );
99 static void FileOpen ( input_thread_t *p_input );
100 static void FileClose ( input_thread_t *p_input );
101 static void NetworkOpen ( input_thread_t *p_input );
102 static void NetworkClose ( input_thread_t *p_input );
104 /*****************************************************************************
105 * input_CreateThread: creates a new input thread
106 *****************************************************************************
107 * This function creates a new input, and returns a pointer
108 * to its description. On error, it returns NULL.
109 * If pi_status is NULL, then the function will block until the thread is ready.
110 * If not, it will be updated using one of the THREAD_* constants.
111 *****************************************************************************/
112 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
114 input_thread_t * p_input; /* thread descriptor */
115 int i_status; /* thread status */
117 /* Allocate descriptor */
118 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
119 if( p_input == NULL )
121 intf_ErrMsg( "input error: can't allocate input thread (%s)",
126 /* Packets read once */
127 p_input->i_read_once = INPUT_READ_ONCE;
129 /* Initialize thread properties */
131 p_input->b_error = 0;
135 p_input->p_source = p_item->psz_name;
137 /* I have never understood that stuff --Meuuh */
138 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
139 *p_input->pi_status = THREAD_CREATE;
141 /* Initialize stream description */
142 p_input->stream.i_es_number = 0;
143 p_input->stream.i_selected_es_number = 0;
144 p_input->stream.i_pgrm_number = 0;
145 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
146 p_input->stream.i_mux_rate = 0;
148 /* no stream, no area */
149 p_input->stream.i_area_nb = 0;
150 p_input->stream.pp_areas = NULL;
151 p_input->stream.p_selected_area = NULL;
152 p_input->stream.p_new_area = NULL;
153 /* By default there is one areas in a stream */
154 input_AddArea( p_input );
155 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
157 /* Initialize stream control properties. */
158 p_input->stream.control.i_status = PLAYING_S;
159 p_input->stream.control.i_rate = DEFAULT_RATE;
160 p_input->stream.control.b_mute = 0;
161 p_input->stream.control.b_bw = 0;
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 /* Create thread and set locks. */
172 vlc_mutex_init( &p_input->stream.stream_lock );
173 vlc_cond_init( &p_input->stream.stream_wait );
174 vlc_mutex_init( &p_input->stream.control.control_lock );
175 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
178 intf_ErrMsg( "input error: can't create input thread (%s)",
184 /* If status is NULL, wait until the thread is created */
185 if( pi_status == NULL )
189 msleep( THREAD_SLEEP );
190 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
191 && (i_status != THREAD_FATAL) );
192 if( i_status != THREAD_READY )
200 /*****************************************************************************
201 * input_DestroyThread: mark an input thread as zombie
202 *****************************************************************************
203 * This function should not return until the thread is effectively cancelled.
204 *****************************************************************************/
205 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
207 int i_status; /* thread status */
210 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
211 *p_input->pi_status = THREAD_DESTROY;
213 /* Request thread destruction */
216 /* Make the thread exit of an eventual vlc_cond_wait() */
217 vlc_mutex_lock( &p_input->stream.stream_lock );
218 vlc_cond_signal( &p_input->stream.stream_wait );
219 vlc_mutex_unlock( &p_input->stream.stream_lock );
221 /* If status is NULL, wait until thread has been destroyed */
222 if( pi_status == NULL )
226 msleep( THREAD_SLEEP );
227 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
228 && (i_status != THREAD_FATAL) );
232 /*****************************************************************************
233 * RunThread: main thread loop
234 *****************************************************************************
235 * Thread in charge of processing the network packets and demultiplexing.
236 *****************************************************************************/
237 static void RunThread( input_thread_t *p_input )
240 data_packet_t ** pp_packets;
242 if( InitThread( p_input ) )
245 /* If we failed, wait before we are killed, and exit */
246 *p_input->pi_status = THREAD_ERROR;
247 p_input->b_error = 1;
248 ErrorThread( p_input );
249 DestroyThread( p_input );
253 /* initialization is completed */
254 vlc_mutex_lock( &p_input->stream.stream_lock );
255 p_input->stream.b_changed = 1;
256 vlc_mutex_unlock( &p_input->stream.stream_lock );
258 pp_packets = (data_packet_t **) malloc( p_input->i_read_once *
259 sizeof( data_packet_t * ) );
260 if( pp_packets == NULL )
262 intf_ErrMsg( "input error: out of memory" );
263 p_input->b_error = 1;
266 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
272 vlc_mutex_lock( &p_input->stream.stream_lock );
274 if( p_input->stream.p_new_area )
276 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
277 p_input->stream.p_new_area = NULL;
280 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
282 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
284 p_input->pf_seek( p_input,
285 p_input->stream.p_selected_area->i_seek );
287 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
289 pgrm_descriptor_t * p_pgrm
290 = p_input->stream.pp_programs[i];
291 /* Escape all decoders for the stream discontinuity they
293 input_EscapeDiscontinuity( p_input, p_pgrm );
295 /* Reinitialize synchro. */
296 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
299 p_input->stream.p_selected_area->i_seek = NO_SEEK;
302 if( p_input->stream.p_removed_es )
304 input_UnselectES( p_input, p_input->stream.p_removed_es );
305 p_input->stream.p_removed_es = NULL;
308 if( p_input->stream.p_newly_selected_es )
310 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
311 p_input->stream.p_newly_selected_es = NULL;
314 vlc_mutex_unlock( &p_input->stream.stream_lock );
316 i_error = p_input->pf_read( p_input, pp_packets );
318 /* Demultiplex read packets. */
319 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
321 p_input->pf_demux( p_input, pp_packets[i] );
328 /* End of file - we do not set b_die because only the
329 * interface is allowed to do so. */
330 intf_WarnMsg( 3, "input: EOF reached" );
335 p_input->b_error = 1;
342 if( p_input->b_error || p_input->b_eof )
344 ErrorThread( p_input );
347 EndThread( p_input );
349 DestroyThread( p_input );
351 intf_DbgMsg("input: Thread end");
354 /*****************************************************************************
355 * InitThread: init the input Thread
356 *****************************************************************************/
357 static int InitThread( input_thread_t * p_input )
361 /* Initialize statistics */
362 p_input->c_loops = 0;
363 p_input->c_bytes = 0;
364 p_input->c_payload_bytes = 0;
365 p_input->c_packets_read = 0;
366 p_input->c_packets_trashed = 0;
369 /* Default, might get overwritten */
370 p_input->pf_open = p_input->pf_file_open;
371 p_input->pf_close = p_input->pf_file_close;
373 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
374 (probedata_t *)p_input );
376 if( p_input->p_input_module == NULL )
378 intf_ErrMsg( "input error: no suitable input module for `%s'",
383 #define f p_input->p_input_module->p_functions->input.functions.input
384 p_input->pf_init = f.pf_init;
385 if( f.pf_open != NULL )
387 p_input->pf_open = f.pf_open;
389 if( f.pf_close != NULL )
391 p_input->pf_close = f.pf_close;
393 p_input->pf_end = f.pf_end;
394 p_input->pf_read = f.pf_read;
395 p_input->pf_set_area = f.pf_set_area;
396 p_input->pf_demux = f.pf_demux;
397 p_input->pf_new_packet = f.pf_new_packet;
398 p_input->pf_new_pes = f.pf_new_pes;
399 p_input->pf_delete_packet = f.pf_delete_packet;
400 p_input->pf_delete_pes = f.pf_delete_pes;
401 p_input->pf_rewind = f.pf_rewind;
402 p_input->pf_seek = f.pf_seek;
404 p_input->pf_open( p_input );
406 if( p_input->b_error )
408 /* We barfed -- exit nicely */
409 p_input->pf_close( p_input );
410 module_Unneed( p_input->p_input_module );
414 p_input->pf_init( p_input );
416 if( p_input->b_error )
418 /* We barfed -- exit nicely */
419 p_input->pf_close( p_input );
420 module_Unneed( p_input->p_input_module );
424 *p_input->pi_status = THREAD_READY;
429 /*****************************************************************************
430 * ErrorThread: RunThread() error loop
431 *****************************************************************************
432 * This function is called when an error occured during thread main's loop.
433 *****************************************************************************/
434 static void ErrorThread( input_thread_t *p_input )
436 while( !p_input->b_die )
439 msleep( INPUT_IDLE_SLEEP );
443 /*****************************************************************************
444 * EndThread: end the input thread
445 *****************************************************************************/
446 static void EndThread( input_thread_t * p_input )
448 int * pi_status; /* thread status */
451 pi_status = p_input->pi_status;
452 *pi_status = THREAD_END;
456 struct tms cpu_usage;
459 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
460 cpu_usage.tms_utime, cpu_usage.tms_stime);
464 /* Free all ES and destroy all decoder threads */
465 input_EndStream( p_input );
467 /* Free demultiplexer's data */
468 p_input->pf_end( p_input );
471 p_input->pf_close( p_input );
473 /* Release modules */
474 module_Unneed( p_input->p_input_module );
478 /*****************************************************************************
479 * DestroyThread: destroy the input thread
480 *****************************************************************************/
481 static void DestroyThread( input_thread_t * p_input )
483 int * pi_status; /* thread status */
486 pi_status = p_input->pi_status;
488 /* Destroy Mutex locks */
489 vlc_mutex_destroy( &p_input->stream.control.control_lock );
490 vlc_mutex_destroy( &p_input->stream.stream_lock );
492 /* Free input structure */
496 *pi_status = THREAD_OVER;
499 /*****************************************************************************
500 * FileOpen : open a file descriptor
501 *****************************************************************************/
502 static void FileOpen( input_thread_t * p_input )
504 struct stat stat_info;
507 char *psz_name = p_input->p_source;
509 /* FIXME: this code ought to be in the plugin so that code can
510 * be shared with the *_Probe function */
511 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
513 int i_size = strlen( psz_name );
516 && !strncasecmp( psz_name, "dvd:", 4 ) )
518 /* get rid of the 'dvd:' stuff and try again */
520 i_stat = stat( psz_name, &stat_info );
522 else if( ( i_size > 5 )
523 && !strncasecmp( psz_name, "file:", 5 ) )
525 /* get rid of the 'file:' stuff and try again */
527 i_stat = stat( psz_name, &stat_info );
532 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
533 psz_name, strerror(errno));
534 p_input->b_error = 1;
539 vlc_mutex_lock( &p_input->stream.stream_lock );
541 /* If we are here we can control the pace... */
542 p_input->stream.b_pace_control = 1;
544 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
545 || S_ISBLK(stat_info.st_mode) )
547 p_input->stream.b_seekable = 1;
548 p_input->stream.p_selected_area->i_size = stat_info.st_size;
550 else if( S_ISFIFO(stat_info.st_mode)
551 #if !defined( SYS_BEOS ) && !defined( WIN32 )
552 || S_ISSOCK(stat_info.st_mode)
556 p_input->stream.b_seekable = 0;
557 p_input->stream.p_selected_area->i_size = 0;
561 vlc_mutex_unlock( &p_input->stream.stream_lock );
562 intf_ErrMsg( "input error: unknown file type for `%s'",
564 p_input->b_error = 1;
568 p_input->stream.p_selected_area->i_tell = 0;
569 vlc_mutex_unlock( &p_input->stream.stream_lock );
571 intf_WarnMsg( 1, "input: opening file `%s'", p_input->p_source );
573 if( (p_input->i_handle = open( psz_name,
574 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
576 if( (p_input->i_handle = open( psz_name, O_BINARY
577 /*O_NONBLOCK | O_LARGEFILE*/ )) == (-1) )
580 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
581 p_input->b_error = 1;
587 /*****************************************************************************
588 * FileClose : close a file descriptor
589 *****************************************************************************/
590 static void FileClose( input_thread_t * p_input )
592 intf_WarnMsg( 1, "input: closing file `%s'", p_input->p_source );
593 close( p_input->i_handle );
599 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
600 /*****************************************************************************
601 * NetworkOpen : open a network socket
602 *****************************************************************************/
603 static void NetworkOpen( input_thread_t * p_input )
605 char *psz_server = NULL;
606 char *psz_broadcast = NULL;
609 struct sockaddr_in sock;
612 /* WinSock Library Init. */
614 int i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
618 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
623 /* Get the remote server */
624 if( p_input->p_source != NULL )
626 psz_server = p_input->p_source;
628 /* Skip the protocol name */
629 while( *psz_server && *psz_server != ':' )
634 /* Skip the "://" part */
635 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
640 /* Found a server name */
643 char *psz_port = psz_server;
645 /* Skip the hostname part */
646 while( *psz_port && *psz_port != ':' )
651 /* Found a port name */
654 /* Replace ':' with '\0' */
658 psz_broadcast = psz_port;
659 while( *psz_broadcast && *psz_broadcast != ':' )
666 *psz_broadcast = '\0';
668 while( *psz_broadcast && *psz_broadcast == ':' )
675 psz_broadcast = NULL;
678 /* port before broadcast address */
679 if( *psz_port != ':' )
681 i_port = atoi( psz_port );
691 /* Check that we got a valid server */
692 if( psz_server == NULL )
694 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
695 INPUT_SERVER_DEFAULT );
698 /* Check that we got a valid port */
701 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
704 if( psz_broadcast == NULL )
706 /* Are we broadcasting ? */
707 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
708 INPUT_BROADCAST_DEFAULT ) )
710 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
711 INPUT_BCAST_ADDR_DEFAULT );
715 psz_broadcast = NULL;
719 intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
720 psz_server, i_port, psz_broadcast );
722 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
724 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
725 if( p_input->i_handle == -1 )
727 intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
728 p_input->b_error = 1;
732 /* We may want to reuse an already used socket */
734 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
735 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
737 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
739 close( p_input->i_handle );
740 p_input->b_error = 1;
744 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
745 * packet loss caused by scheduling problems */
747 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
748 (void*) &i_opt, sizeof( i_opt ) ) == -1 )
750 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
752 close( p_input->i_handle );
753 p_input->b_error = 1;
757 /* Build the local socket */
758 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
760 intf_ErrMsg( "input error: can't build local address" );
761 close( p_input->i_handle );
762 p_input->b_error = 1;
767 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
768 sizeof( sock ) ) < 0 )
770 intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
771 close( p_input->i_handle );
772 p_input->b_error = 1;
776 /* Build socket for remote connection */
777 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
779 intf_ErrMsg( "input error: can't build remote address" );
780 close( p_input->i_handle );
781 p_input->b_error = 1;
785 /* And connect it ... should we really connect ? */
786 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
787 sizeof( sock ) ) == (-1) )
789 intf_ErrMsg( "input error: can't connect socket, %s",
791 close( p_input->i_handle );
792 p_input->b_error = 1;
796 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
797 * with the server. */
798 p_input->stream.b_pace_control = 0;
799 p_input->stream.b_seekable = 0;
801 intf_WarnMsg( 3, "input: successfully opened network mode" );
806 /*****************************************************************************
807 * NetworkClose : close a network socket
808 *****************************************************************************/
809 static void NetworkClose( input_thread_t * p_input )
811 close( p_input->i_handle );