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.112 2001/05/30 05:19:03 stef 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>
37 #ifdef STRNCASECMP_IN_STRINGS_H
42 /* WinSock Includes */
49 /* Network functions */
51 #if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
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"
84 /*****************************************************************************
86 *****************************************************************************/
87 static void RunThread ( input_thread_t *p_input );
88 static int InitThread ( input_thread_t *p_input );
89 static void ErrorThread ( input_thread_t *p_input );
90 static void DestroyThread ( input_thread_t *p_input );
91 static void EndThread ( input_thread_t *p_input );
93 /*****************************************************************************
94 * input_CreateThread: creates a new input thread
95 *****************************************************************************
96 * This function creates a new input, and returns a pointer
97 * to its description. On error, it returns NULL.
98 * If pi_status is NULL, then the function will block until the thread is ready.
99 * If not, it will be updated using one of the THREAD_* constants.
100 *****************************************************************************/
101 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
103 input_thread_t * p_input; /* thread descriptor */
104 int i_status; /* thread status */
106 /* Allocate descriptor */
107 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
108 if( p_input == NULL )
110 intf_ErrMsg( "input error: can't allocate input thread (%s)",
115 /* Packets read once */
116 p_input->i_read_once = INPUT_READ_ONCE;
118 /* Initialize thread properties */
120 p_input->b_error = 0;
124 p_input->p_source = p_item->psz_name;
126 /* I have never understood that stuff --Meuuh */
127 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
128 *p_input->pi_status = THREAD_CREATE;
130 /* Initialize stream description */
131 p_input->stream.i_es_number = 0;
132 p_input->stream.i_selected_es_number = 0;
133 p_input->stream.i_pgrm_number = 0;
134 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
135 p_input->stream.i_mux_rate = 0;
137 /* no stream, no area */
138 p_input->stream.i_area_nb = 0;
139 p_input->stream.pp_areas = NULL;
140 p_input->stream.p_selected_area = NULL;
141 p_input->stream.p_new_area = NULL;
142 /* By default there is one areas in a stream */
143 input_AddArea( p_input );
144 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
146 /* Initialize stream control properties. */
147 p_input->stream.control.i_status = PLAYING_S;
148 p_input->stream.control.i_rate = DEFAULT_RATE;
149 p_input->stream.control.b_mute = 0;
150 p_input->stream.control.b_bw = 0;
152 /* Create thread and set locks. */
153 vlc_mutex_init( &p_input->stream.stream_lock );
154 vlc_cond_init( &p_input->stream.stream_wait );
155 vlc_mutex_init( &p_input->stream.control.control_lock );
156 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
159 intf_ErrMsg( "input error: can't create input thread (%s)",
165 /* If status is NULL, wait until the thread is created */
166 if( pi_status == NULL )
170 msleep( THREAD_SLEEP );
171 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
172 && (i_status != THREAD_FATAL) );
173 if( i_status != THREAD_READY )
181 /*****************************************************************************
182 * input_DestroyThread: mark an input thread as zombie
183 *****************************************************************************
184 * This function should not return until the thread is effectively cancelled.
185 *****************************************************************************/
186 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
188 int i_status; /* thread status */
191 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
192 *p_input->pi_status = THREAD_DESTROY;
194 /* Request thread destruction */
197 /* Make the thread exit of an eventual vlc_cond_wait() */
198 vlc_mutex_lock( &p_input->stream.stream_lock );
199 vlc_cond_signal( &p_input->stream.stream_wait );
200 vlc_mutex_unlock( &p_input->stream.stream_lock );
202 /* If status is NULL, wait until thread has been destroyed */
203 if( pi_status == NULL )
207 msleep( THREAD_SLEEP );
208 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
209 && (i_status != THREAD_FATAL) );
213 /*****************************************************************************
214 * RunThread: main thread loop
215 *****************************************************************************
216 * Thread in charge of processing the network packets and demultiplexing.
217 *****************************************************************************/
218 static void RunThread( input_thread_t *p_input )
222 if( InitThread( p_input ) )
225 /* If we failed, wait before we are killed, and exit */
226 *p_input->pi_status = THREAD_ERROR;
227 p_input->b_error = 1;
228 ErrorThread( p_input );
229 DestroyThread( p_input );
233 /* initialization is completed */
234 vlc_mutex_lock( &p_input->stream.stream_lock );
235 p_input->stream.b_changed = 1;
236 vlc_mutex_unlock( &p_input->stream.stream_lock );
238 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
240 data_packet_t * pp_packets[p_input->i_read_once];
246 vlc_mutex_lock( &p_input->stream.stream_lock );
248 if( p_input->stream.p_new_area )
250 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
251 p_input->stream.p_new_area = NULL;
254 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
256 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
258 p_input->pf_seek( p_input,
259 p_input->stream.p_selected_area->i_seek );
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_selected_area->i_seek = NO_SEEK;
276 if( p_input->stream.p_removed_es )
278 input_UnselectES( p_input, p_input->stream.p_removed_es );
279 p_input->stream.p_removed_es = NULL;
282 if( p_input->stream.p_newly_selected_es )
284 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
285 p_input->stream.p_newly_selected_es = NULL;
288 vlc_mutex_unlock( &p_input->stream.stream_lock );
290 i_error = p_input->pf_read( p_input, pp_packets );
292 /* Demultiplex read packets. */
293 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
295 p_input->pf_demux( p_input, pp_packets[i] );
302 /* End of file - we do not set b_die because only the
303 * interface is allowed to do so. */
304 intf_WarnMsg( 3, "input: EOF reached" );
309 p_input->b_error = 1;
314 if( p_input->b_error || p_input->b_eof )
316 ErrorThread( p_input );
319 EndThread( p_input );
321 DestroyThread( p_input );
323 intf_DbgMsg("input: Thread end");
326 /*****************************************************************************
327 * InitThread: init the input Thread
328 *****************************************************************************/
329 static int InitThread( input_thread_t * p_input )
333 /* Initialize statistics */
334 p_input->c_loops = 0;
335 p_input->c_bytes = 0;
336 p_input->c_payload_bytes = 0;
337 p_input->c_packets_read = 0;
338 p_input->c_packets_trashed = 0;
341 p_input->p_input_module = module_Need( MODULE_CAPABILITY_INPUT,
342 (probedata_t *)p_input );
344 if( p_input->p_input_module == NULL )
346 intf_ErrMsg( "input error: no suitable input module for `%s'",
351 #define f p_input->p_input_module->p_functions->input.functions.input
352 p_input->pf_init = f.pf_init;
353 p_input->pf_open = f.pf_open;
354 p_input->pf_close = f.pf_close;
355 p_input->pf_end = f.pf_end;
356 p_input->pf_read = f.pf_read;
357 p_input->pf_set_area = f.pf_set_area;
358 p_input->pf_demux = f.pf_demux;
359 p_input->pf_new_packet = f.pf_new_packet;
360 p_input->pf_new_pes = f.pf_new_pes;
361 p_input->pf_delete_packet = f.pf_delete_packet;
362 p_input->pf_delete_pes = f.pf_delete_pes;
363 p_input->pf_rewind = f.pf_rewind;
364 p_input->pf_seek = f.pf_seek;
366 p_input->pf_open( p_input );
368 if( p_input->b_error )
370 /* We barfed -- exit nicely */
371 p_input->pf_close( p_input );
372 module_Unneed( p_input->p_input_module );
376 p_input->pf_init( p_input );
378 if( p_input->b_error )
380 /* We barfed -- exit nicely */
381 p_input->pf_close( p_input );
382 module_Unneed( p_input->p_input_module );
386 *p_input->pi_status = THREAD_READY;
391 /*****************************************************************************
392 * ErrorThread: RunThread() error loop
393 *****************************************************************************
394 * This function is called when an error occured during thread main's loop.
395 *****************************************************************************/
396 static void ErrorThread( input_thread_t *p_input )
398 while( !p_input->b_die )
401 msleep( INPUT_IDLE_SLEEP );
405 /*****************************************************************************
406 * EndThread: end the input thread
407 *****************************************************************************/
408 static void EndThread( input_thread_t * p_input )
410 int * pi_status; /* thread status */
413 pi_status = p_input->pi_status;
414 *pi_status = THREAD_END;
418 struct tms cpu_usage;
421 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
422 cpu_usage.tms_utime, cpu_usage.tms_stime);
426 /* Free all ES and destroy all decoder threads */
427 input_EndStream( p_input );
429 /* Free demultiplexer's data */
430 p_input->pf_end( p_input );
433 p_input->pf_close( p_input );
435 /* Release modules */
436 module_Unneed( p_input->p_input_module );
440 /*****************************************************************************
441 * DestroyThread: destroy the input thread
442 *****************************************************************************/
443 static void DestroyThread( input_thread_t * p_input )
445 int * pi_status; /* thread status */
448 pi_status = p_input->pi_status;
450 /* Destroy Mutex locks */
451 vlc_mutex_destroy( &p_input->stream.control.control_lock );
452 vlc_mutex_destroy( &p_input->stream.stream_lock );
454 /* Free input structure */
458 *pi_status = THREAD_OVER;
461 /*****************************************************************************
462 * input_FileOpen : open a file descriptor
463 *****************************************************************************/
464 void input_FileOpen( input_thread_t * p_input )
466 struct stat stat_info;
469 char *psz_name = p_input->p_source;
471 /* FIXME: this code ought to be in the plugin so that code can
472 * be shared with the *_Probe function */
473 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
475 int i_size = strlen( psz_name );
478 && !strncasecmp( psz_name, "dvd:", 4 ) )
480 /* get rid of the 'dvd:' stuff and try again */
482 i_stat = stat( psz_name, &stat_info );
484 else if( ( i_size > 5 )
485 && !strncasecmp( psz_name, "file:", 5 ) )
487 /* get rid of the 'file:' stuff and try again */
489 i_stat = stat( psz_name, &stat_info );
494 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
495 psz_name, strerror(errno));
496 p_input->b_error = 1;
501 vlc_mutex_lock( &p_input->stream.stream_lock );
503 /* If we are here we can control the pace... */
504 p_input->stream.b_pace_control = 1;
506 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
507 || S_ISBLK(stat_info.st_mode) )
509 p_input->stream.b_seekable = 1;
510 p_input->stream.p_selected_area->i_size = stat_info.st_size;
512 else if( S_ISFIFO(stat_info.st_mode)
513 #if !defined( SYS_BEOS ) && !defined( WIN32 )
514 || S_ISSOCK(stat_info.st_mode)
518 p_input->stream.b_seekable = 0;
519 p_input->stream.p_selected_area->i_size = 0;
523 vlc_mutex_unlock( &p_input->stream.stream_lock );
524 intf_ErrMsg( "input error: unknown file type for `%s'",
526 p_input->b_error = 1;
530 p_input->stream.p_selected_area->i_tell = 0;
531 vlc_mutex_unlock( &p_input->stream.stream_lock );
533 intf_WarnMsg( 1, "input: opening file `%s'", p_input->p_source );
535 if( (p_input->i_handle = open( psz_name,
536 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
538 if( (p_input->i_handle = open( psz_name, O_BINARY
539 /*O_NONBLOCK | O_LARGEFILE*/ )) == (-1) )
542 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
543 p_input->b_error = 1;
549 /*****************************************************************************
550 * input_FileClose : close a file descriptor
551 *****************************************************************************/
552 void input_FileClose( input_thread_t * p_input )
554 intf_WarnMsg( 1, "input: closing file `%s'", p_input->p_source );
555 close( p_input->i_handle );
561 #if !defined( SYS_BEOS ) && !defined( SYS_NTO )
562 /*****************************************************************************
563 * input_NetworkOpen : open a network socket
564 *****************************************************************************/
565 void input_NetworkOpen( input_thread_t * p_input )
567 char *psz_server = NULL;
568 char *psz_broadcast = NULL;
571 struct sockaddr_in sock;
574 /* WinSock Library Init. */
576 int i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
580 intf_ErrMsg( "input: can't initiate WinSocks, error %i", i_err );
585 /* Get the remote server */
586 if( p_input->p_source != NULL )
588 psz_server = p_input->p_source;
590 /* Skip the protocol name */
591 while( *psz_server && *psz_server != ':' )
596 /* Skip the "://" part */
597 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
602 /* Found a server name */
605 char *psz_port = psz_server;
607 /* Skip the hostname part */
608 while( *psz_port && *psz_port != ':' )
613 /* Found a port name */
616 /* Replace ':' with '\0' */
620 psz_broadcast = psz_port;
621 while( *psz_broadcast && *psz_broadcast != ':' )
628 *psz_broadcast = '\0';
630 while( *psz_broadcast && *psz_broadcast == ':' )
637 psz_broadcast = NULL;
640 /* port before broadcast address */
641 if( *psz_port != ':' )
643 i_port = atoi( psz_port );
653 /* Check that we got a valid server */
654 if( psz_server == NULL )
656 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
657 INPUT_SERVER_DEFAULT );
660 /* Check that we got a valid port */
663 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
666 if( psz_broadcast == NULL )
668 /* Are we broadcasting ? */
669 if( main_GetIntVariable( INPUT_BROADCAST_VAR,
670 INPUT_BROADCAST_DEFAULT ) )
672 psz_broadcast = main_GetPszVariable( INPUT_BCAST_ADDR_VAR,
673 INPUT_BCAST_ADDR_DEFAULT );
677 psz_broadcast = NULL;
681 intf_WarnMsg( 2, "input: server: %s port: %d broadcast: %s",
682 psz_server, i_port, psz_broadcast );
684 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
686 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
687 if( p_input->i_handle == -1 )
689 intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
690 p_input->b_error = 1;
694 /* We may want to reuse an already used socket */
696 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
697 &i_opt, sizeof( i_opt ) ) == -1 )
699 intf_ErrMsg( "input error: can't configure socket (SO_REUSEADDR: %s)",
701 close( p_input->i_handle );
702 p_input->b_error = 1;
706 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
707 * packet loss caused by scheduling problems */
709 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
710 &i_opt, sizeof( i_opt ) ) == -1 )
712 intf_ErrMsg( "input error: can't configure socket (SO_RCVBUF: %s)",
714 close( p_input->i_handle );
715 p_input->b_error = 1;
719 /* Build the local socket */
720 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) == -1 )
722 intf_ErrMsg( "input error: can't build local address" );
723 close( p_input->i_handle );
724 p_input->b_error = 1;
729 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
730 sizeof( sock ) ) < 0 )
732 intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
733 close( p_input->i_handle );
734 p_input->b_error = 1;
738 /* Build socket for remote connection */
739 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
741 intf_ErrMsg( "input error: can't build remote address" );
742 close( p_input->i_handle );
743 p_input->b_error = 1;
747 /* And connect it ... should we really connect ? */
748 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
749 sizeof( sock ) ) == (-1) )
751 intf_ErrMsg( "input error: can't connect socket, %s",
753 close( p_input->i_handle );
754 p_input->b_error = 1;
758 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
759 * with the server. */
760 p_input->stream.b_pace_control = 0;
761 p_input->stream.b_seekable = 0;
763 intf_WarnMsg( 3, "input: successfully opened network mode" );
768 /*****************************************************************************
769 * input_NetworkClose : close a network socket
770 *****************************************************************************/
771 void input_NetworkClose( input_thread_t * p_input )
773 close( p_input->i_handle );