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.89 2001/03/07 00:18:46 henri 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>
39 /* Network functions */
41 #include <netdb.h> /* hostent ... */
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
49 # include <sys/times.h>
59 #include "intf_plst.h"
61 #include "stream_control.h"
62 #include "input_ext-intf.h"
63 #include "input_ext-dec.h"
66 #include "interface.h"
70 /*****************************************************************************
72 *****************************************************************************/
73 static void RunThread ( input_thread_t *p_input );
74 static int InitThread ( input_thread_t *p_input );
75 static void ErrorThread ( input_thread_t *p_input );
76 static void DestroyThread ( input_thread_t *p_input );
77 static void EndThread ( input_thread_t *p_input );
79 /*****************************************************************************
80 * input_CreateThread: creates a new input thread
81 *****************************************************************************
82 * This function creates a new input, and returns a pointer
83 * to its description. On error, it returns NULL.
84 * If pi_status is NULL, then the function will block until the thread is ready.
85 * If not, it will be updated using one of the THREAD_* constants.
86 *****************************************************************************/
87 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
89 input_thread_t * p_input; /* thread descriptor */
90 int i_status; /* thread status */
92 /* Allocate descriptor */
93 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
96 intf_ErrMsg( "input error: can't allocate input thread (%s)",
101 /* Packets read once */
102 p_input->i_read_once = INPUT_READ_ONCE;
104 /* Initialize thread properties */
106 p_input->b_error = 0;
110 p_input->p_source = p_item->psz_name;
112 /* I have never understood that stuff --Meuuh */
113 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
114 *p_input->pi_status = THREAD_CREATE;
116 /* Initialize stream description */
117 p_input->stream.i_es_number = 0;
118 p_input->stream.i_selected_es_number = 0;
119 p_input->stream.i_pgrm_number = 0;
120 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
121 p_input->stream.i_mux_rate = 0;
123 p_input->stream.i_area_nb = 0;
124 p_input->stream.pp_areas = NULL;
125 /* By default there is one areas in a stream */
126 input_AddArea( p_input );
127 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
128 p_input->stream.p_selected_area->i_seek = NO_SEEK;
130 /* Initialize stream control properties. */
131 p_input->stream.control.i_status = PLAYING_S;
132 p_input->stream.control.i_rate = DEFAULT_RATE;
133 p_input->stream.control.b_mute = 0;
134 p_input->stream.control.b_bw = 0;
136 /* Initialize default settings for spawned decoders */
137 p_input->p_default_aout = p_main->p_aout;
138 p_input->p_default_vout = p_main->p_vout;
140 /* Create thread and set locks. */
141 vlc_mutex_init( &p_input->stream.stream_lock );
142 vlc_cond_init( &p_input->stream.stream_wait );
143 vlc_mutex_init( &p_input->stream.control.control_lock );
144 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
147 intf_ErrMsg( "input error: can't create input thread (%s)",
153 /* If status is NULL, wait until the thread is created */
154 if( pi_status == NULL )
158 msleep( THREAD_SLEEP );
159 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
160 && (i_status != THREAD_FATAL) );
161 if( i_status != THREAD_READY )
169 /*****************************************************************************
170 * input_DestroyThread: mark an input thread as zombie
171 *****************************************************************************
172 * This function should not return until the thread is effectively cancelled.
173 *****************************************************************************/
174 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
176 int i_status; /* thread status */
179 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
180 *p_input->pi_status = THREAD_DESTROY;
182 /* Request thread destruction */
185 /* Make the thread exit of an eventual vlc_cond_wait() */
186 vlc_mutex_lock( &p_input->stream.stream_lock );
187 vlc_cond_signal( &p_input->stream.stream_wait );
188 vlc_mutex_unlock( &p_input->stream.stream_lock );
190 /* If status is NULL, wait until thread has been destroyed */
191 if( pi_status == NULL )
195 msleep( THREAD_SLEEP );
196 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
197 && (i_status != THREAD_FATAL) );
201 /*****************************************************************************
202 * RunThread: main thread loop
203 *****************************************************************************
204 * Thread in charge of processing the network packets and demultiplexing.
205 *****************************************************************************/
206 static void RunThread( input_thread_t *p_input )
210 if( InitThread( p_input ) )
213 /* If we failed, wait before we are killed, and exit */
214 *p_input->pi_status = THREAD_ERROR;
215 p_input->b_error = 1;
216 ErrorThread( p_input );
217 DestroyThread( p_input );
221 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
223 data_packet_t * pp_packets[p_input->i_read_once];
229 vlc_mutex_lock( &p_input->stream.stream_lock );
230 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
232 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
234 p_input->pf_seek( p_input,
235 p_input->stream.p_selected_area->i_seek );
237 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
239 pgrm_descriptor_t * p_pgrm
240 = p_input->stream.pp_programs[i];
241 /* Escape all decoders for the stream discontinuity they
243 input_EscapeDiscontinuity( p_input, p_pgrm );
245 /* Reinitialize synchro. */
246 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
249 p_input->stream.p_selected_area->i_seek = NO_SEEK;
251 vlc_mutex_unlock( &p_input->stream.stream_lock );
253 i_error = p_input->pf_read( p_input, pp_packets );
255 /* Demultiplex read packets. */
256 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
258 p_input->pf_demux( p_input, pp_packets[i] );
265 /* End of file - we do not set b_die because only the
266 * interface is allowed to do so. */
267 intf_WarnMsg( 1, "End of file reached" );
272 p_input->b_error = 1;
277 if( p_input->b_error || p_input->b_eof )
279 ErrorThread( p_input );
282 EndThread( p_input );
284 DestroyThread( p_input );
286 intf_DbgMsg("Thread end");
289 /*****************************************************************************
290 * InitThread: init the input Thread
291 *****************************************************************************/
292 static int InitThread( input_thread_t * p_input )
296 /* Initialize statistics */
297 p_input->c_loops = 0;
298 p_input->c_bytes = 0;
299 p_input->c_payload_bytes = 0;
300 p_input->c_packets_read = 0;
301 p_input->c_packets_trashed = 0;
304 p_input->p_input_module = module_Need( p_main->p_bank,
305 MODULE_CAPABILITY_INPUT,
306 (probedata_t *)p_input );
308 if( p_input->p_input_module == NULL )
310 intf_ErrMsg( "input error: no suitable input module" );
314 #define f p_input->p_input_module->p_functions->input.functions.input
315 p_input->pf_init = f.pf_init;
316 p_input->pf_open = f.pf_open;
317 p_input->pf_close = f.pf_close;
318 p_input->pf_end = f.pf_end;
319 p_input->pf_read = f.pf_read;
320 p_input->pf_demux = f.pf_demux;
321 p_input->pf_new_packet = f.pf_new_packet;
322 p_input->pf_new_pes = f.pf_new_pes;
323 p_input->pf_delete_packet = f.pf_delete_packet;
324 p_input->pf_delete_pes = f.pf_delete_pes;
325 p_input->pf_rewind = f.pf_rewind;
326 p_input->pf_seek = f.pf_seek;
328 p_input->pf_open( p_input );
330 if( p_input->b_error )
332 /* We barfed -- exit nicely */
333 p_input->pf_close( p_input );
334 module_Unneed( p_main->p_bank, p_input->p_input_module );
338 p_input->pf_init( p_input );
340 *p_input->pi_status = THREAD_READY;
345 /*****************************************************************************
346 * ErrorThread: RunThread() error loop
347 *****************************************************************************
348 * This function is called when an error occured during thread main's loop.
349 *****************************************************************************/
350 static void ErrorThread( input_thread_t *p_input )
352 while( !p_input->b_die )
355 msleep( INPUT_IDLE_SLEEP );
359 /*****************************************************************************
360 * EndThread: end the input thread
361 *****************************************************************************/
362 static void EndThread( input_thread_t * p_input )
364 int * pi_status; /* thread status */
367 pi_status = p_input->pi_status;
368 *pi_status = THREAD_END;
372 struct tms cpu_usage;
375 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
376 cpu_usage.tms_utime, cpu_usage.tms_stime);
380 /* Free all ES and destroy all decoder threads */
381 input_EndStream( p_input );
383 /* Free demultiplexer's data */
384 p_input->pf_end( p_input );
387 p_input->pf_close( p_input );
389 /* Release modules */
390 module_Unneed( p_main->p_bank, p_input->p_input_module );
394 /*****************************************************************************
395 * DestroyThread: destroy the input thread
396 *****************************************************************************/
397 static void DestroyThread( input_thread_t * p_input )
399 int * pi_status; /* thread status */
402 pi_status = p_input->pi_status;
404 /* Destroy Mutex locks */
405 vlc_mutex_destroy( &p_input->stream.control.control_lock );
406 vlc_mutex_destroy( &p_input->stream.stream_lock );
408 /* Free input structure */
412 *pi_status = THREAD_OVER;
415 /*****************************************************************************
416 * input_FileOpen : open a file descriptor
417 *****************************************************************************/
418 void input_FileOpen( input_thread_t * p_input )
420 struct stat stat_info;
423 char *psz_name = p_input->p_source;
425 /* FIXME: this code ought to be in the plugin so that code can
426 * be shared with the *_Probe function */
427 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
429 int i_size = strlen( psz_name );
432 && !strncasecmp( psz_name, "dvd:", 4 ) )
434 /* get rid of the 'dvd:' stuff and try again */
436 i_stat = stat( psz_name, &stat_info );
438 else if( ( i_size > 5 )
439 && !strncasecmp( psz_name, "file:", 5 ) )
441 /* get rid of the 'file:' stuff and try again */
443 i_stat = stat( psz_name, &stat_info );
448 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
449 psz_name, strerror(errno));
450 p_input->b_error = 1;
455 vlc_mutex_lock( &p_input->stream.stream_lock );
457 /* If we are here we can control the pace... */
458 p_input->stream.b_pace_control = 1;
460 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
461 || S_ISBLK(stat_info.st_mode) )
463 p_input->stream.b_seekable = 1;
464 p_input->stream.p_selected_area->i_size = stat_info.st_size;
466 else if( S_ISFIFO(stat_info.st_mode)
468 || S_ISSOCK(stat_info.st_mode)
472 p_input->stream.b_seekable = 0;
473 p_input->stream.p_selected_area->i_size = 0;
477 vlc_mutex_unlock( &p_input->stream.stream_lock );
478 intf_ErrMsg( "input error: unknown file type for `%s'",
480 p_input->b_error = 1;
484 p_input->stream.p_selected_area->i_tell = 0;
485 vlc_mutex_unlock( &p_input->stream.stream_lock );
487 intf_Msg( "input: opening %s", p_input->p_source );
488 if( (p_input->i_handle = open( psz_name,
489 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
491 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
492 p_input->b_error = 1;
498 /*****************************************************************************
499 * input_FileClose : close a file descriptor
500 *****************************************************************************/
501 void input_FileClose( input_thread_t * p_input )
503 close( p_input->i_handle );
508 /*****************************************************************************
509 * input_BuildLocalAddr : fill a sockaddr_in structure for local binding
510 *****************************************************************************/
511 int input_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
512 boolean_t b_broadcast )
514 char psz_hostname[INPUT_MAX_SOURCE_LENGTH];
515 struct hostent * p_hostent;
518 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
519 p_socket->sin_family = AF_INET; /* family */
520 p_socket->sin_port = htons( i_port );
524 /* Try to get our own IP */
525 if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
527 intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
535 /* Using broadcast address. There are many ways of doing it, one of
536 * the simpliest being a #define ...
537 * FIXME : this is ugly */
538 strncpy( psz_hostname, INPUT_BCAST_ADDR,INPUT_MAX_SOURCE_LENGTH );
541 /* Try to convert address directly from in_addr - this will work if
542 * psz_in_addr is dotted decimal. */
544 #ifdef HAVE_ARPA_INET_H
545 if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
547 if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
550 /* We have a fqdn, try to find its address */
551 if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
553 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
557 /* Copy the first address of the host in the socket address */
558 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
559 p_hostent->h_length );
565 /*****************************************************************************
566 * input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
567 *****************************************************************************/
568 int input_BuildRemoteAddr( input_thread_t * p_input,
569 struct sockaddr_in * p_socket )
571 struct hostent * p_hostent;
573 /* Reset structure */
574 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
575 p_socket->sin_family = AF_INET; /* family */
576 p_socket->sin_port = htons( 0 ); /* This is for remote end */
578 /* Get the remote server */
579 if( p_input->p_source == NULL )
581 p_input->p_source = main_GetPszVariable( INPUT_SERVER_VAR,
582 INPUT_SERVER_DEFAULT );
585 /* Try to convert address directly from in_addr - this will work if
586 * psz_in_addr is dotted decimal. */
587 #ifdef HAVE_ARPA_INET_H
588 if( !inet_aton( p_input->p_source, &p_socket->sin_addr) )
590 if( (p_socket->sin_addr.s_addr = inet_addr( p_input->p_source )) == -1 )
593 /* We have a fqdn, try to find its address */
594 if ( (p_hostent = gethostbyname(p_input->p_source)) == NULL )
596 intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
601 /* Copy the first address of the host in the socket address */
602 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
603 p_hostent->h_length );
609 /*****************************************************************************
610 * input_NetworkOpen : open a network socket
611 *****************************************************************************/
612 void input_NetworkOpen( input_thread_t * p_input )
615 int i_option_value, i_port;
616 struct sockaddr_in s_socket;
617 boolean_t b_broadcast;
619 /* FIXME : we don't handle channels for the moment */
621 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
623 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
624 if( p_input->i_handle == -1 )
626 intf_ErrMsg("NetworkOpen : can't create socket : %s", strerror(errno));
627 p_input->b_error = 1;
631 /* We may want to reuse an already used socket */
633 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
634 &i_option_value,sizeof( i_option_value ) ) == -1 )
636 intf_ErrMsg("NetworkOpen : can't configure socket (SO_REUSEADDR: %s)",
638 close( p_input->i_handle );
639 p_input->b_error = 1;
644 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
645 * packet loss caused by scheduling problems */
646 i_option_value = 524288;
647 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF, &i_option_value,
648 sizeof( i_option_value ) ) == -1 )
650 intf_ErrMsg("NetworkOpen : can't configure socket (SO_RCVBUF: %s)",
652 close( p_input->i_handle );
653 p_input->b_error = 1;
656 #endif /* SYS_BEOS */
658 /* Get details about what we are supposed to do */
659 b_broadcast = (boolean_t)main_GetIntVariable( INPUT_BROADCAST_VAR, 0 );
660 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
662 /* TODO : here deal with channel stufs */
664 /* Build the local socket */
665 if ( input_BuildLocalAddr( &s_socket, i_port, b_broadcast )
668 close( p_input->i_handle );
669 p_input->b_error = 1;
674 if( bind( p_input->i_handle, (struct sockaddr *)&s_socket,
675 sizeof( s_socket ) ) < 0 )
677 intf_ErrMsg("NetworkOpen: can't bind socket (%s)", strerror(errno));
678 close( p_input->i_handle );
679 p_input->b_error = 1;
683 /* Build socket for remote connection */
684 if ( input_BuildRemoteAddr( p_input, &s_socket )
687 close( p_input->i_handle );
688 p_input->b_error = 1;
692 /* And connect it ... should we really connect ? */
693 if( connect( p_input->i_handle, (struct sockaddr *) &s_socket,
694 sizeof( s_socket ) ) == (-1) )
696 intf_ErrMsg("NetworkOpen: can't connect socket" );
697 close( p_input->i_handle );
698 p_input->b_error = 1;
702 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
703 * with the server. */
704 p_input->stream.b_pace_control = 1;
709 /*****************************************************************************
710 * input_NetworkClose : close a network socket
711 *****************************************************************************/
712 void input_NetworkClose( input_thread_t * p_input )
714 close( p_input->i_handle );
715 /* FIXME: deal with channels */