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.103 2001/04/28 23:19:19 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>
37 #ifdef STRNCASECMP_IN_STRINGS_H
42 /* Network functions */
44 #if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
45 #include <netdb.h> /* hostent ... */
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
54 # include <sys/times.h>
65 #include "intf_playlist.h"
67 #include "stream_control.h"
68 #include "input_ext-intf.h"
69 #include "input_ext-dec.h"
72 #include "interface.h"
76 /* #include <netutils.h> */
79 /*****************************************************************************
81 *****************************************************************************/
82 static void RunThread ( input_thread_t *p_input );
83 static int InitThread ( input_thread_t *p_input );
84 static void ErrorThread ( input_thread_t *p_input );
85 static void DestroyThread ( input_thread_t *p_input );
86 static void EndThread ( input_thread_t *p_input );
88 /*****************************************************************************
89 * input_CreateThread: creates a new input thread
90 *****************************************************************************
91 * This function creates a new input, and returns a pointer
92 * to its description. On error, it returns NULL.
93 * If pi_status is NULL, then the function will block until the thread is ready.
94 * If not, it will be updated using one of the THREAD_* constants.
95 *****************************************************************************/
96 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
98 input_thread_t * p_input; /* thread descriptor */
99 int i_status; /* thread status */
101 /* Allocate descriptor */
102 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
103 if( p_input == NULL )
105 intf_ErrMsg( "input error: can't allocate input thread (%s)",
110 /* Packets read once */
111 p_input->i_read_once = INPUT_READ_ONCE;
113 /* Initialize thread properties */
115 p_input->b_error = 0;
119 p_input->p_source = p_item->psz_name;
121 /* I have never understood that stuff --Meuuh */
122 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
123 *p_input->pi_status = THREAD_CREATE;
125 /* Initialize stream description */
126 p_input->stream.i_es_number = 0;
127 p_input->stream.i_selected_es_number = 0;
128 p_input->stream.i_pgrm_number = 0;
129 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
130 p_input->stream.i_mux_rate = 0;
132 /* no stream, no area */
133 p_input->stream.i_area_nb = 0;
134 p_input->stream.pp_areas = NULL;
135 p_input->stream.p_selected_area = NULL;
136 /* By default there is one areas in a stream */
137 input_AddArea( p_input );
138 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
140 /* Initialize stream control properties. */
141 p_input->stream.control.i_status = PLAYING_S;
142 p_input->stream.control.i_rate = DEFAULT_RATE;
143 p_input->stream.control.b_mute = 0;
144 p_input->stream.control.b_bw = 0;
146 /* Initialize default settings for spawned decoders */
147 p_input->p_default_aout = p_main->p_aout;
148 p_input->p_default_vout = p_main->p_vout;
150 /* Create thread and set locks. */
151 vlc_mutex_init( &p_input->stream.stream_lock );
152 vlc_cond_init( &p_input->stream.stream_wait );
153 vlc_mutex_init( &p_input->stream.control.control_lock );
154 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
157 intf_ErrMsg( "input error: can't create input thread (%s)",
163 /* If status is NULL, wait until the thread is created */
164 if( pi_status == NULL )
168 msleep( THREAD_SLEEP );
169 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
170 && (i_status != THREAD_FATAL) );
171 if( i_status != THREAD_READY )
179 /*****************************************************************************
180 * input_DestroyThread: mark an input thread as zombie
181 *****************************************************************************
182 * This function should not return until the thread is effectively cancelled.
183 *****************************************************************************/
184 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
186 int i_status; /* thread status */
189 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
190 *p_input->pi_status = THREAD_DESTROY;
192 /* Request thread destruction */
195 /* Make the thread exit of an eventual vlc_cond_wait() */
196 vlc_mutex_lock( &p_input->stream.stream_lock );
197 vlc_cond_signal( &p_input->stream.stream_wait );
198 vlc_mutex_unlock( &p_input->stream.stream_lock );
200 /* If status is NULL, wait until thread has been destroyed */
201 if( pi_status == NULL )
205 msleep( THREAD_SLEEP );
206 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
207 && (i_status != THREAD_FATAL) );
211 /*****************************************************************************
212 * RunThread: main thread loop
213 *****************************************************************************
214 * Thread in charge of processing the network packets and demultiplexing.
215 *****************************************************************************/
216 static void RunThread( input_thread_t *p_input )
220 if( InitThread( p_input ) )
223 /* If we failed, wait before we are killed, and exit */
224 *p_input->pi_status = THREAD_ERROR;
225 p_input->b_error = 1;
226 ErrorThread( p_input );
227 DestroyThread( p_input );
231 /* initialization is completed */
232 vlc_mutex_lock( &p_input->stream.stream_lock );
233 p_input->stream.b_changed = 1;
234 vlc_mutex_unlock( &p_input->stream.stream_lock );
236 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
238 data_packet_t * pp_packets[p_input->i_read_once];
244 vlc_mutex_lock( &p_input->stream.stream_lock );
246 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
248 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
250 p_input->pf_seek( p_input,
251 p_input->stream.p_selected_area->i_seek );
253 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
255 pgrm_descriptor_t * p_pgrm
256 = p_input->stream.pp_programs[i];
257 /* Escape all decoders for the stream discontinuity they
259 input_EscapeDiscontinuity( p_input, p_pgrm );
261 /* Reinitialize synchro. */
262 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
265 p_input->stream.p_selected_area->i_seek = NO_SEEK;
268 vlc_mutex_unlock( &p_input->stream.stream_lock );
270 i_error = p_input->pf_read( p_input, pp_packets );
272 /* Demultiplex read packets. */
273 for( i = 0; i < p_input->i_read_once && pp_packets[i] != NULL; i++ )
275 p_input->pf_demux( p_input, pp_packets[i] );
282 /* End of file - we do not set b_die because only the
283 * interface is allowed to do so. */
284 intf_WarnMsg( 1, "input: EOF reached" );
289 p_input->b_error = 1;
294 if( p_input->b_error || p_input->b_eof )
296 ErrorThread( p_input );
299 EndThread( p_input );
301 DestroyThread( p_input );
303 intf_DbgMsg("input: Thread end");
306 /*****************************************************************************
307 * InitThread: init the input Thread
308 *****************************************************************************/
309 static int InitThread( input_thread_t * p_input )
313 /* Initialize statistics */
314 p_input->c_loops = 0;
315 p_input->c_bytes = 0;
316 p_input->c_payload_bytes = 0;
317 p_input->c_packets_read = 0;
318 p_input->c_packets_trashed = 0;
321 p_input->p_input_module = module_Need( p_main->p_bank,
322 MODULE_CAPABILITY_INPUT,
323 (probedata_t *)p_input );
325 if( p_input->p_input_module == NULL )
327 intf_ErrMsg( "input error: no suitable input module for `%s'",
332 #define f p_input->p_input_module->p_functions->input.functions.input
333 p_input->pf_init = f.pf_init;
334 p_input->pf_open = f.pf_open;
335 p_input->pf_close = f.pf_close;
336 p_input->pf_end = f.pf_end;
337 p_input->pf_read = f.pf_read;
338 p_input->pf_set_area = f.pf_set_area;
339 p_input->pf_demux = f.pf_demux;
340 p_input->pf_new_packet = f.pf_new_packet;
341 p_input->pf_new_pes = f.pf_new_pes;
342 p_input->pf_delete_packet = f.pf_delete_packet;
343 p_input->pf_delete_pes = f.pf_delete_pes;
344 p_input->pf_rewind = f.pf_rewind;
345 p_input->pf_seek = f.pf_seek;
347 p_input->pf_open( p_input );
349 if( p_input->b_error )
351 /* We barfed -- exit nicely */
352 p_input->pf_close( p_input );
353 module_Unneed( p_main->p_bank, p_input->p_input_module );
357 p_input->pf_init( p_input );
359 if( p_input->b_error )
361 /* We barfed -- exit nicely */
362 p_input->pf_close( p_input );
363 module_Unneed( p_main->p_bank, p_input->p_input_module );
367 *p_input->pi_status = THREAD_READY;
372 /*****************************************************************************
373 * ErrorThread: RunThread() error loop
374 *****************************************************************************
375 * This function is called when an error occured during thread main's loop.
376 *****************************************************************************/
377 static void ErrorThread( input_thread_t *p_input )
379 while( !p_input->b_die )
382 msleep( INPUT_IDLE_SLEEP );
386 /*****************************************************************************
387 * EndThread: end the input thread
388 *****************************************************************************/
389 static void EndThread( input_thread_t * p_input )
391 int * pi_status; /* thread status */
394 pi_status = p_input->pi_status;
395 *pi_status = THREAD_END;
399 struct tms cpu_usage;
402 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
403 cpu_usage.tms_utime, cpu_usage.tms_stime);
407 /* Free all ES and destroy all decoder threads */
408 input_EndStream( p_input );
410 /* Free demultiplexer's data */
411 p_input->pf_end( p_input );
414 p_input->pf_close( p_input );
416 /* Release modules */
417 module_Unneed( p_main->p_bank, p_input->p_input_module );
421 /*****************************************************************************
422 * DestroyThread: destroy the input thread
423 *****************************************************************************/
424 static void DestroyThread( input_thread_t * p_input )
426 int * pi_status; /* thread status */
429 pi_status = p_input->pi_status;
431 /* Destroy Mutex locks */
432 vlc_mutex_destroy( &p_input->stream.control.control_lock );
433 vlc_mutex_destroy( &p_input->stream.stream_lock );
435 /* Free input structure */
439 *pi_status = THREAD_OVER;
442 /*****************************************************************************
443 * input_FileOpen : open a file descriptor
444 *****************************************************************************/
445 void input_FileOpen( input_thread_t * p_input )
447 struct stat stat_info;
450 char *psz_name = p_input->p_source;
452 /* FIXME: this code ought to be in the plugin so that code can
453 * be shared with the *_Probe function */
454 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
456 int i_size = strlen( psz_name );
459 && !strncasecmp( psz_name, "dvd:", 4 ) )
461 /* get rid of the 'dvd:' stuff and try again */
463 i_stat = stat( psz_name, &stat_info );
465 else if( ( i_size > 5 )
466 && !strncasecmp( psz_name, "file:", 5 ) )
468 /* get rid of the 'file:' stuff and try again */
470 i_stat = stat( psz_name, &stat_info );
475 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
476 psz_name, strerror(errno));
477 p_input->b_error = 1;
482 vlc_mutex_lock( &p_input->stream.stream_lock );
484 /* If we are here we can control the pace... */
485 p_input->stream.b_pace_control = 1;
487 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
488 || S_ISBLK(stat_info.st_mode) )
490 p_input->stream.b_seekable = 1;
491 p_input->stream.p_selected_area->i_size = stat_info.st_size;
493 else if( S_ISFIFO(stat_info.st_mode)
494 #if !defined( SYS_BEOS ) && !defined( WIN32 )
495 || S_ISSOCK(stat_info.st_mode)
499 p_input->stream.b_seekable = 0;
500 p_input->stream.p_selected_area->i_size = 0;
504 vlc_mutex_unlock( &p_input->stream.stream_lock );
505 intf_ErrMsg( "input error: unknown file type for `%s'",
507 p_input->b_error = 1;
511 p_input->stream.p_selected_area->i_tell = 0;
512 vlc_mutex_unlock( &p_input->stream.stream_lock );
514 intf_Msg( "input: opening file `%s'", p_input->p_source );
516 if( (p_input->i_handle = open( psz_name,
517 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
519 if( (p_input->i_handle = open( psz_name, O_BINARY
520 /*O_NONBLOCK | O_LARGEFILE*/ )) == (-1) )
523 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
524 p_input->b_error = 1;
530 /*****************************************************************************
531 * input_FileClose : close a file descriptor
532 *****************************************************************************/
533 void input_FileClose( input_thread_t * p_input )
535 intf_Msg( "input: closing file `%s'", p_input->p_source );
536 close( p_input->i_handle );
542 #if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
543 /*****************************************************************************
544 * input_NetworkOpen : open a network socket
545 *****************************************************************************/
546 void input_NetworkOpen( input_thread_t * p_input )
548 char *psz_server = NULL;
551 struct sockaddr_in sock;
552 char * psz_broadcast;
554 /* Are we broadcasting ? */
555 psz_broadcast = main_GetPszVariable( INPUT_BROADCAST_VAR, NULL );
557 /* Get the remote server */
558 if( p_input->p_source != NULL )
560 psz_server = p_input->p_source;
562 /* Skip the protocol name */
563 while( *psz_server && *psz_server != ':' )
568 /* Skip the "://" part */
569 while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
574 /* Found a server name */
577 char *psz_port = psz_server;
579 /* Skip the hostname part */
580 while( *psz_port && *psz_port != ':' )
585 /* Found a port name */
588 /* Replace ':' with '\0' */
592 i_port = atoi( psz_port );
601 /* Check that we got a valid server */
602 if( psz_server == NULL )
604 psz_server = main_GetPszVariable( INPUT_SERVER_VAR,
605 INPUT_SERVER_DEFAULT );
608 /* Check that we got a valid port */
611 i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
614 /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
616 p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
617 if( p_input->i_handle == -1 )
619 intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
620 p_input->b_error = 1;
624 /* We may want to reuse an already used socket */
626 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
627 &i_opt, sizeof( i_opt ) ) == -1 )
629 intf_ErrMsg("input error: can't configure socket (SO_REUSEADDR: %s)",
631 close( p_input->i_handle );
632 p_input->b_error = 1;
636 /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
637 * packet loss caused by scheduling problems */
639 if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
640 &i_opt, sizeof( i_opt ) ) == -1 )
642 intf_ErrMsg("input error: can't configure socket (SO_RCVBUF: %s)",
644 close( p_input->i_handle );
645 p_input->b_error = 1;
649 /* Build the local socket */
650 if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast )
653 close( p_input->i_handle );
654 p_input->b_error = 1;
659 if( bind( p_input->i_handle, (struct sockaddr *)&sock,
660 sizeof( sock ) ) < 0 )
662 intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
663 close( p_input->i_handle );
664 p_input->b_error = 1;
668 /* Build socket for remote connection */
669 if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
671 close( p_input->i_handle );
672 p_input->b_error = 1;
676 /* And connect it ... should we really connect ? */
677 if( connect( p_input->i_handle, (struct sockaddr *) &sock,
678 sizeof( sock ) ) == (-1) )
680 intf_ErrMsg( "NetworkOpen: can't connect socket : %s",
682 close( p_input->i_handle );
683 p_input->b_error = 1;
687 /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
688 * with the server. */
689 p_input->stream.b_pace_control = 0;
690 p_input->stream.b_seekable = 0;
695 /*****************************************************************************
696 * input_NetworkClose : close a network socket
697 *****************************************************************************/
698 void input_NetworkClose( input_thread_t * p_input )
700 close( p_input->i_handle );