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.86 2001/02/20 08:47:25 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>
40 # include <sys/times.h>
50 #include "intf_plst.h"
52 #include "stream_control.h"
53 #include "input_ext-intf.h"
54 #include "input_ext-dec.h"
57 #include "interface.h"
61 /*****************************************************************************
63 *****************************************************************************/
64 static void RunThread ( input_thread_t *p_input );
65 static int InitThread ( input_thread_t *p_input );
66 static void ErrorThread ( input_thread_t *p_input );
67 static void DestroyThread ( input_thread_t *p_input );
68 static void EndThread ( input_thread_t *p_input );
70 /*****************************************************************************
71 * input_CreateThread: creates a new input thread
72 *****************************************************************************
73 * This function creates a new input, and returns a pointer
74 * to its description. On error, it returns NULL.
75 * If pi_status is NULL, then the function will block until the thread is ready.
76 * If not, it will be updated using one of the THREAD_* constants.
77 *****************************************************************************/
78 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
80 input_thread_t * p_input; /* thread descriptor */
81 int i_status; /* thread status */
83 /* Allocate descriptor */
84 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
87 intf_ErrMsg( "input error: can't allocate input thread (%s)",
92 /* Initialize thread properties */
98 p_input->p_source = p_item->psz_name;
100 /* I have never understood that stuff --Meuuh */
101 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
102 *p_input->pi_status = THREAD_CREATE;
104 /* Initialize stream description */
105 p_input->stream.i_es_number = 0;
106 p_input->stream.i_selected_es_number = 0;
107 p_input->stream.i_pgrm_number = 0;
108 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
109 p_input->stream.i_mux_rate = 0;
111 p_input->stream.i_area_nb = 0;
112 p_input->stream.pp_areas = NULL;
113 /* By default there is one areas in a stream */
114 input_AddArea( p_input );
115 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
116 p_input->stream.p_selected_area->i_seek = NO_SEEK;
118 /* Initialize stream control properties. */
119 p_input->stream.control.i_status = PLAYING_S;
120 p_input->stream.control.i_rate = DEFAULT_RATE;
121 p_input->stream.control.b_mute = 0;
122 p_input->stream.control.b_bw = 0;
124 /* Initialize default settings for spawned decoders */
125 p_input->p_default_aout = p_main->p_aout;
126 p_input->p_default_vout = p_main->p_vout;
128 /* Create thread and set locks. */
129 vlc_mutex_init( &p_input->stream.stream_lock );
130 vlc_cond_init( &p_input->stream.stream_wait );
131 vlc_mutex_init( &p_input->stream.control.control_lock );
132 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
135 intf_ErrMsg( "input error: can't create input thread (%s)",
141 /* If status is NULL, wait until the thread is created */
142 if( pi_status == NULL )
146 msleep( THREAD_SLEEP );
147 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
148 && (i_status != THREAD_FATAL) );
149 if( i_status != THREAD_READY )
157 /*****************************************************************************
158 * input_DestroyThread: mark an input thread as zombie
159 *****************************************************************************
160 * This function should not return until the thread is effectively cancelled.
161 *****************************************************************************/
162 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
164 int i_status; /* thread status */
167 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
168 *p_input->pi_status = THREAD_DESTROY;
170 /* Request thread destruction */
173 /* Make the thread exit of an eventual vlc_cond_wait() */
174 vlc_mutex_lock( &p_input->stream.stream_lock );
175 vlc_cond_signal( &p_input->stream.stream_wait );
176 vlc_mutex_unlock( &p_input->stream.stream_lock );
178 /* If status is NULL, wait until thread has been destroyed */
179 if( pi_status == NULL )
183 msleep( THREAD_SLEEP );
184 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
185 && (i_status != THREAD_FATAL) );
189 /*****************************************************************************
190 * RunThread: main thread loop
191 *****************************************************************************
192 * Thread in charge of processing the network packets and demultiplexing.
193 *****************************************************************************/
194 static void RunThread( input_thread_t *p_input )
196 data_packet_t * pp_packets[INPUT_READ_ONCE];
199 if( InitThread( p_input ) )
202 /* If we failed, wait before we are killed, and exit */
203 *p_input->pi_status = THREAD_ERROR;
204 p_input->b_error = 1;
205 ErrorThread( p_input );
206 DestroyThread( p_input );
210 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
217 vlc_mutex_lock( &p_input->stream.stream_lock );
218 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
220 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
222 p_input->pf_seek( p_input,
223 p_input->stream.p_selected_area->i_seek );
225 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
227 pgrm_descriptor_t * p_pgrm
228 = p_input->stream.pp_programs[i];
229 /* Escape all decoders for the stream discontinuity they
231 input_EscapeDiscontinuity( p_input, p_pgrm );
233 /* Reinitialize synchro. */
234 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
237 p_input->stream.p_selected_area->i_seek = NO_SEEK;
239 vlc_mutex_unlock( &p_input->stream.stream_lock );
241 i_error = p_input->pf_read( p_input, pp_packets );
243 /* Demultiplex read packets. */
244 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
246 p_input->pf_demux( p_input, pp_packets[i] );
253 /* End of file - we do not set b_die because only the
254 * interface is allowed to do so. */
255 intf_WarnMsg( 1, "End of file reached" );
260 p_input->b_error = 1;
265 if( p_input->b_error || p_input->b_eof )
267 ErrorThread( p_input );
270 EndThread( p_input );
272 DestroyThread( p_input );
274 intf_DbgMsg("Thread end");
277 /*****************************************************************************
278 * InitThread: init the input Thread
279 *****************************************************************************/
280 static int InitThread( input_thread_t * p_input )
284 /* Initialize statistics */
285 p_input->c_loops = 0;
286 p_input->c_bytes = 0;
287 p_input->c_payload_bytes = 0;
288 p_input->c_packets_read = 0;
289 p_input->c_packets_trashed = 0;
292 p_input->p_input_module = module_Need( p_main->p_bank,
293 MODULE_CAPABILITY_INPUT,
294 (probedata_t *)p_input );
296 if( p_input->p_input_module == NULL )
298 intf_ErrMsg( "input error: no suitable input module" );
299 module_Unneed( p_main->p_bank, p_input->p_input_module );
303 #define f p_input->p_input_module->p_functions->input.functions.input
304 p_input->pf_init = f.pf_init;
305 p_input->pf_open = f.pf_open;
306 p_input->pf_close = f.pf_close;
307 p_input->pf_end = f.pf_end;
308 p_input->pf_read = f.pf_read;
309 p_input->pf_demux = f.pf_demux;
310 p_input->pf_new_packet = f.pf_new_packet;
311 p_input->pf_new_pes = f.pf_new_pes;
312 p_input->pf_delete_packet = f.pf_delete_packet;
313 p_input->pf_delete_pes = f.pf_delete_pes;
314 p_input->pf_rewind = f.pf_rewind;
315 p_input->pf_seek = f.pf_seek;
318 p_input->pf_open( p_input );
320 if( p_input->b_error )
322 /* We barfed -- exit nicely */
323 p_input->pf_close( p_input );
324 module_Unneed( p_main->p_bank, p_input->p_input_module );
328 p_input->pf_init( p_input );
330 *p_input->pi_status = THREAD_READY;
335 /*****************************************************************************
336 * ErrorThread: RunThread() error loop
337 *****************************************************************************
338 * This function is called when an error occured during thread main's loop.
339 *****************************************************************************/
340 static void ErrorThread( input_thread_t *p_input )
342 while( !p_input->b_die )
345 msleep( INPUT_IDLE_SLEEP );
349 /*****************************************************************************
350 * EndThread: end the input thread
351 *****************************************************************************/
352 static void EndThread( input_thread_t * p_input )
354 int * pi_status; /* thread status */
357 pi_status = p_input->pi_status;
358 *pi_status = THREAD_END;
362 struct tms cpu_usage;
365 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
366 cpu_usage.tms_utime, cpu_usage.tms_stime);
370 /* Free all ES and destroy all decoder threads */
371 input_EndStream( p_input );
373 /* Free demultiplexer's data */
374 p_input->pf_end( p_input );
377 p_input->pf_close( p_input );
379 /* Release modules */
380 module_Unneed( p_main->p_bank, p_input->p_input_module );
384 /*****************************************************************************
385 * DestroyThread: destroy the input thread
386 *****************************************************************************/
387 static void DestroyThread( input_thread_t * p_input )
389 int * pi_status; /* thread status */
392 pi_status = p_input->pi_status;
394 /* Destroy Mutex locks */
395 vlc_mutex_destroy( &p_input->stream.control.control_lock );
396 vlc_mutex_destroy( &p_input->stream.stream_lock );
398 /* Free input structure */
402 *pi_status = THREAD_OVER;
405 /*****************************************************************************
406 * input_FileOpen : open a file descriptor
407 *****************************************************************************/
408 void input_FileOpen( input_thread_t * p_input )
410 struct stat stat_info;
413 char *psz_name = p_input->p_source;
415 /* FIXME: this code ought to be in the plugin so that code can
416 * be shared with the *_Probe function */
417 if( ( i_stat = stat( psz_name, &stat_info ) ) == (-1) )
419 int i_size = strlen( psz_name );
422 && !strncasecmp( psz_name, "dvd:", 4 ) )
424 /* get rid of the 'dvd:' stuff and try again */
426 i_stat = stat( psz_name, &stat_info );
428 else if( ( i_size > 5 )
429 && !strncasecmp( psz_name, "file:", 5 ) )
431 /* get rid of the 'file:' stuff and try again */
433 i_stat = stat( psz_name, &stat_info );
438 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
439 psz_name, strerror(errno));
440 p_input->b_error = 1;
445 vlc_mutex_lock( &p_input->stream.stream_lock );
447 /* If we are here we can control the pace... */
448 p_input->stream.b_pace_control = 1;
450 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
451 || S_ISBLK(stat_info.st_mode) )
453 p_input->stream.b_seekable = 1;
454 p_input->stream.p_selected_area->i_size = stat_info.st_size;
456 else if( S_ISFIFO(stat_info.st_mode)
458 || S_ISSOCK(stat_info.st_mode)
462 p_input->stream.b_seekable = 0;
463 p_input->stream.p_selected_area->i_size = 0;
467 vlc_mutex_unlock( &p_input->stream.stream_lock );
468 intf_ErrMsg( "input error: unknown file type for `%s'",
470 p_input->b_error = 1;
474 p_input->stream.p_selected_area->i_tell = 0;
475 vlc_mutex_unlock( &p_input->stream.stream_lock );
477 intf_Msg( "input: opening %s", p_input->p_source );
478 if( (p_input->i_handle = open( psz_name,
479 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
481 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
482 p_input->b_error = 1;
488 /*****************************************************************************
489 * input_FileClose : close a file descriptor
490 *****************************************************************************/
491 void input_FileClose( input_thread_t * p_input )
493 close( p_input->i_handle );