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.91 2001/03/07 10:31:10 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>
39 /* Network functions */
42 #include <netdb.h> /* hostent ... */
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
51 # include <sys/times.h>
61 #include "intf_plst.h"
63 #include "stream_control.h"
64 #include "input_ext-intf.h"
65 #include "input_ext-dec.h"
68 #include "interface.h"
72 /*****************************************************************************
74 *****************************************************************************/
75 static void RunThread ( input_thread_t *p_input );
76 static int InitThread ( input_thread_t *p_input );
77 static void ErrorThread ( input_thread_t *p_input );
78 static void DestroyThread ( input_thread_t *p_input );
79 static void EndThread ( input_thread_t *p_input );
81 /*****************************************************************************
82 * input_CreateThread: creates a new input thread
83 *****************************************************************************
84 * This function creates a new input, and returns a pointer
85 * to its description. On error, it returns NULL.
86 * If pi_status is NULL, then the function will block until the thread is ready.
87 * If not, it will be updated using one of the THREAD_* constants.
88 *****************************************************************************/
89 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
91 input_thread_t * p_input; /* thread descriptor */
92 int i_status; /* thread status */
94 /* Allocate descriptor */
95 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
98 intf_ErrMsg( "input error: can't allocate input thread (%s)",
103 /* Packets read once */
104 p_input->i_read_once = INPUT_READ_ONCE;
106 /* Initialize thread properties */
108 p_input->b_error = 0;
112 p_input->p_source = p_item->psz_name;
114 /* I have never understood that stuff --Meuuh */
115 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
116 *p_input->pi_status = THREAD_CREATE;
118 /* Initialize stream description */
119 p_input->stream.i_es_number = 0;
120 p_input->stream.i_selected_es_number = 0;
121 p_input->stream.i_pgrm_number = 0;
122 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
123 p_input->stream.i_mux_rate = 0;
125 p_input->stream.i_area_nb = 0;
126 p_input->stream.pp_areas = NULL;
127 /* By default there is one areas in a stream */
128 input_AddArea( p_input );
129 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
130 p_input->stream.p_selected_area->i_seek = NO_SEEK;
132 /* Initialize stream control properties. */
133 p_input->stream.control.i_status = PLAYING_S;
134 p_input->stream.control.i_rate = DEFAULT_RATE;
135 p_input->stream.control.b_mute = 0;
136 p_input->stream.control.b_bw = 0;
138 /* Initialize default settings for spawned decoders */
139 p_input->p_default_aout = p_main->p_aout;
140 p_input->p_default_vout = p_main->p_vout;
142 /* Create thread and set locks. */
143 vlc_mutex_init( &p_input->stream.stream_lock );
144 vlc_cond_init( &p_input->stream.stream_wait );
145 vlc_mutex_init( &p_input->stream.control.control_lock );
146 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
149 intf_ErrMsg( "input error: can't create input thread (%s)",
155 /* If status is NULL, wait until the thread is created */
156 if( pi_status == NULL )
160 msleep( THREAD_SLEEP );
161 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
162 && (i_status != THREAD_FATAL) );
163 if( i_status != THREAD_READY )
171 /*****************************************************************************
172 * input_DestroyThread: mark an input thread as zombie
173 *****************************************************************************
174 * This function should not return until the thread is effectively cancelled.
175 *****************************************************************************/
176 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
178 int i_status; /* thread status */
181 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
182 *p_input->pi_status = THREAD_DESTROY;
184 /* Request thread destruction */
187 /* Make the thread exit of an eventual vlc_cond_wait() */
188 vlc_mutex_lock( &p_input->stream.stream_lock );
189 vlc_cond_signal( &p_input->stream.stream_wait );
190 vlc_mutex_unlock( &p_input->stream.stream_lock );
192 /* If status is NULL, wait until thread has been destroyed */
193 if( pi_status == NULL )
197 msleep( THREAD_SLEEP );
198 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
199 && (i_status != THREAD_FATAL) );
203 /*****************************************************************************
204 * RunThread: main thread loop
205 *****************************************************************************
206 * Thread in charge of processing the network packets and demultiplexing.
207 *****************************************************************************/
208 static void RunThread( input_thread_t *p_input )
212 if( InitThread( p_input ) )
215 /* If we failed, wait before we are killed, and exit */
216 *p_input->pi_status = THREAD_ERROR;
217 p_input->b_error = 1;
218 ErrorThread( p_input );
219 DestroyThread( p_input );
223 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
225 data_packet_t * pp_packets[p_input->i_read_once];
231 vlc_mutex_lock( &p_input->stream.stream_lock );
232 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
234 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
236 p_input->pf_seek( p_input,
237 p_input->stream.p_selected_area->i_seek );
239 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
241 pgrm_descriptor_t * p_pgrm
242 = p_input->stream.pp_programs[i];
243 /* Escape all decoders for the stream discontinuity they
245 input_EscapeDiscontinuity( p_input, p_pgrm );
247 /* Reinitialize synchro. */
248 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
251 p_input->stream.p_selected_area->i_seek = NO_SEEK;
253 vlc_mutex_unlock( &p_input->stream.stream_lock );
255 i_error = p_input->pf_read( p_input, pp_packets );
257 /* Demultiplex read packets. */
258 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
260 p_input->pf_demux( p_input, pp_packets[i] );
267 /* End of file - we do not set b_die because only the
268 * interface is allowed to do so. */
269 intf_WarnMsg( 1, "End of file reached" );
274 p_input->b_error = 1;
279 if( p_input->b_error || p_input->b_eof )
281 ErrorThread( p_input );
284 EndThread( p_input );
286 DestroyThread( p_input );
288 intf_DbgMsg("Thread end");
291 /*****************************************************************************
292 * InitThread: init the input Thread
293 *****************************************************************************/
294 static int InitThread( input_thread_t * p_input )
298 /* Initialize statistics */
299 p_input->c_loops = 0;
300 p_input->c_bytes = 0;
301 p_input->c_payload_bytes = 0;
302 p_input->c_packets_read = 0;
303 p_input->c_packets_trashed = 0;
306 p_input->p_input_module = module_Need( p_main->p_bank,
307 MODULE_CAPABILITY_INPUT,
308 (probedata_t *)p_input );
310 if( p_input->p_input_module == NULL )
312 intf_ErrMsg( "input error: no suitable input module" );
316 #define f p_input->p_input_module->p_functions->input.functions.input
317 p_input->pf_init = f.pf_init;
318 p_input->pf_open = f.pf_open;
319 p_input->pf_close = f.pf_close;
320 p_input->pf_end = f.pf_end;
321 p_input->pf_read = f.pf_read;
322 p_input->pf_set_area = f.pf_set_area;
323 p_input->pf_demux = f.pf_demux;
324 p_input->pf_new_packet = f.pf_new_packet;
325 p_input->pf_new_pes = f.pf_new_pes;
326 p_input->pf_delete_packet = f.pf_delete_packet;
327 p_input->pf_delete_pes = f.pf_delete_pes;
328 p_input->pf_rewind = f.pf_rewind;
329 p_input->pf_seek = f.pf_seek;
331 p_input->pf_open( p_input );
333 if( p_input->b_error )
335 /* We barfed -- exit nicely */
336 p_input->pf_close( p_input );
337 module_Unneed( p_main->p_bank, p_input->p_input_module );
341 p_input->pf_init( p_input );
343 *p_input->pi_status = THREAD_READY;
348 /*****************************************************************************
349 * ErrorThread: RunThread() error loop
350 *****************************************************************************
351 * This function is called when an error occured during thread main's loop.
352 *****************************************************************************/
353 static void ErrorThread( input_thread_t *p_input )
355 while( !p_input->b_die )
358 msleep( INPUT_IDLE_SLEEP );
362 /*****************************************************************************
363 * EndThread: end the input thread
364 *****************************************************************************/
365 static void EndThread( input_thread_t * p_input )
367 int * pi_status; /* thread status */
370 pi_status = p_input->pi_status;
371 *pi_status = THREAD_END;
375 struct tms cpu_usage;
378 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
379 cpu_usage.tms_utime, cpu_usage.tms_stime);
383 /* Free all ES and destroy all decoder threads */
384 input_EndStream( p_input );
386 /* Free demultiplexer's data */
387 p_input->pf_end( p_input );
390 p_input->pf_close( p_input );
392 /* Release modules */
393 module_Unneed( p_main->p_bank, p_input->p_input_module );
397 /*****************************************************************************
398 * DestroyThread: destroy the input thread
399 *****************************************************************************/
400 static void DestroyThread( input_thread_t * p_input )
402 int * pi_status; /* thread status */
405 pi_status = p_input->pi_status;
407 /* Destroy Mutex locks */
408 vlc_mutex_destroy( &p_input->stream.control.control_lock );
409 vlc_mutex_destroy( &p_input->stream.stream_lock );
411 /* Free input structure */
415 *pi_status = THREAD_OVER;
418 /*****************************************************************************
419 * input_FileOpen : open a file descriptor
420 *****************************************************************************/
421 void input_FileOpen( input_thread_t * p_input )
423 struct stat stat_info;
426 char *psz_name = p_input->p_source;
428 /* FIXME: this code ought to be in the plugin so that code can
429 * be shared with the *_Probe function */
430 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
432 int i_size = strlen( psz_name );
435 && !strncasecmp( psz_name, "dvd:", 4 ) )
437 /* get rid of the 'dvd:' stuff and try again */
439 i_stat = stat( psz_name, &stat_info );
441 else if( ( i_size > 5 )
442 && !strncasecmp( psz_name, "file:", 5 ) )
444 /* get rid of the 'file:' stuff and try again */
446 i_stat = stat( psz_name, &stat_info );
451 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
452 psz_name, strerror(errno));
453 p_input->b_error = 1;
458 vlc_mutex_lock( &p_input->stream.stream_lock );
460 /* If we are here we can control the pace... */
461 p_input->stream.b_pace_control = 1;
463 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
464 || S_ISBLK(stat_info.st_mode) )
466 p_input->stream.b_seekable = 1;
467 p_input->stream.p_selected_area->i_size = stat_info.st_size;
469 else if( S_ISFIFO(stat_info.st_mode)
471 || S_ISSOCK(stat_info.st_mode)
475 p_input->stream.b_seekable = 0;
476 p_input->stream.p_selected_area->i_size = 0;
480 vlc_mutex_unlock( &p_input->stream.stream_lock );
481 intf_ErrMsg( "input error: unknown file type for `%s'",
483 p_input->b_error = 1;
487 p_input->stream.p_selected_area->i_tell = 0;
488 vlc_mutex_unlock( &p_input->stream.stream_lock );
490 intf_Msg( "input: opening %s", p_input->p_source );
491 if( (p_input->i_handle = open( psz_name,
492 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
494 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
495 p_input->b_error = 1;
501 /*****************************************************************************
502 * input_FileClose : close a file descriptor
503 *****************************************************************************/
504 void input_FileClose( input_thread_t * p_input )
506 close( p_input->i_handle );
512 /*****************************************************************************
513 * input_BuildLocalAddr : fill a sockaddr_in structure for local binding
514 *****************************************************************************/
515 int input_BuildLocalAddr( struct sockaddr_in * p_socket, int i_port,
516 boolean_t b_broadcast )
518 char psz_hostname[INPUT_MAX_SOURCE_LENGTH];
519 struct hostent * p_hostent;
522 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
523 p_socket->sin_family = AF_INET; /* family */
524 p_socket->sin_port = htons( i_port );
528 /* Try to get our own IP */
529 if( gethostname( psz_hostname, sizeof(psz_hostname) ) )
531 intf_ErrMsg( "BuildLocalAddr : unable to resolve local name : %s",
539 /* Using broadcast address. There are many ways of doing it, one of
540 * the simpliest being a #define ...
541 * FIXME : this is ugly */
542 strncpy( psz_hostname, INPUT_BCAST_ADDR,INPUT_MAX_SOURCE_LENGTH );
545 /* Try to convert address directly from in_addr - this will work if
546 * psz_in_addr is dotted decimal. */
548 #ifdef HAVE_ARPA_INET_H
549 if( !inet_aton( psz_hostname, &p_socket->sin_addr) )
551 if( (p_socket->sin_addr.s_addr = inet_addr( psz_hostname )) == -1 )
554 /* We have a fqdn, try to find its address */
555 if ( (p_hostent = gethostbyname( psz_hostname )) == NULL )
557 intf_ErrMsg( "BuildLocalAddr: unknown host %s", psz_hostname );
561 /* Copy the first address of the host in the socket address */
562 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
563 p_hostent->h_length );
569 /*****************************************************************************
570 * input_BuildRemoteAddr : fill a sockaddr_in structure for remote host
571 *****************************************************************************/
572 int input_BuildRemoteAddr( input_thread_t * p_input,
573 struct sockaddr_in * p_socket )
575 struct hostent * p_hostent;
577 /* Reset structure */
578 memset( p_socket, 0, sizeof( struct sockaddr_in ) );
579 p_socket->sin_family = AF_INET; /* family */
580 p_socket->sin_port = htons( 0 ); /* This is for remote end */
582 /* Get the remote server */
583 if( p_input->p_source == NULL )
585 p_input->p_source = main_GetPszVariable( INPUT_SERVER_VAR,
586 INPUT_SERVER_DEFAULT );
589 /* Try to convert address directly from in_addr - this will work if
590 * psz_in_addr is dotted decimal. */
591 #ifdef HAVE_ARPA_INET_H
592 if( !inet_aton( p_input->p_source, &p_socket->sin_addr) )
594 if( (p_socket->sin_addr.s_addr = inet_addr( p_input->p_source )) == -1 )
597 /* We have a fqdn, try to find its address */
598 if ( (p_hostent = gethostbyname(p_input->p_source)) == NULL )
600 intf_ErrMsg( "BuildRemoteAddr: unknown host %s",
605 /* Copy the first address of the host in the socket address */
606 memcpy( &p_socket->sin_addr, p_hostent->h_addr_list[0],
607 p_hostent->h_length );
613 /*****************************************************************************
614 * input_NetworkOpen : open a network socket
615 *****************************************************************************/
616 void input_NetworkOpen( input_thread_t * p_input )
619 int i_option_value, i_port;
620 struct sockaddr_in s_socket;
621 boolean_t b_broadcast;
623 /* FIXME : we don't handle channels for the moment */
625 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
627 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
628 if( p_input->i_handle == -1 )
630 intf_ErrMsg("NetworkOpen : can't create socket : %s", strerror(errno));
631 p_input->b_error = 1;
635 /* We may want to reuse an already used socket */
637 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
638 &i_option_value,sizeof( i_option_value ) ) == -1 )
640 intf_ErrMsg("NetworkOpen : can't configure socket (SO_REUSEADDR: %s)",
642 close( p_input->i_handle );
643 p_input->b_error = 1;
648 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
649 * packet loss caused by scheduling problems */
650 i_option_value = 524288;
651 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF, &i_option_value,
652 sizeof( i_option_value ) ) == -1 )
654 intf_ErrMsg("NetworkOpen : can't configure socket (SO_RCVBUF: %s)",
656 close( p_input->i_handle );
657 p_input->b_error = 1;
660 #endif /* SYS_BEOS */
662 /* Get details about what we are supposed to do */
663 b_broadcast = (boolean_t)main_GetIntVariable( INPUT_BROADCAST_VAR, 0 );
664 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
666 /* TODO : here deal with channel stufs */
668 /* Build the local socket */
669 if ( input_BuildLocalAddr( &s_socket, i_port, b_broadcast )
672 close( p_input->i_handle );
673 p_input->b_error = 1;
678 if( bind( p_input->i_handle, (struct sockaddr *)&s_socket,
679 sizeof( s_socket ) ) < 0 )
681 intf_ErrMsg("NetworkOpen: can't bind socket (%s)", strerror(errno));
682 close( p_input->i_handle );
683 p_input->b_error = 1;
687 /* Build socket for remote connection */
688 if ( input_BuildRemoteAddr( p_input, &s_socket )
691 close( p_input->i_handle );
692 p_input->b_error = 1;
696 /* And connect it ... should we really connect ? */
697 if( connect( p_input->i_handle, (struct sockaddr *) &s_socket,
698 sizeof( s_socket ) ) == (-1) )
700 intf_ErrMsg("NetworkOpen: can't connect socket" );
701 close( p_input->i_handle );
702 p_input->b_error = 1;
706 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
707 * with the server. */
708 p_input->stream.b_pace_control = 1;
713 /*****************************************************************************
714 * input_NetworkClose : close a network socket
715 *****************************************************************************/
716 void input_NetworkClose( input_thread_t * p_input )
718 close( p_input->i_handle );
719 /* FIXME: deal with channels */