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.76 2001/02/08 07:24:25 sam Exp $
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 void InitThread ( input_thread_t *p_input );
66 static void ErrorThread ( input_thread_t *p_input );
67 static void EndThread ( input_thread_t *p_input );
69 /*****************************************************************************
70 * input_CreateThread: creates a new input thread
71 *****************************************************************************
72 * This function creates a new input, and returns a pointer
73 * to its description. On error, it returns NULL.
74 * If pi_status is NULL, then the function will block until the thread is ready.
75 * If not, it will be updated using one of the THREAD_* constants.
76 *****************************************************************************/
77 input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
79 input_thread_t * p_input; /* thread descriptor */
80 int i_status; /* thread status */
82 /* Allocate descriptor */
83 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
86 intf_ErrMsg( "input error: can't allocate input thread (%s)",
91 /* Initialize thread properties */
97 p_input->p_source = p_item->psz_name;
99 /* I have never understood that stuff --Meuuh */
100 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
101 *p_input->pi_status = THREAD_CREATE;
103 /* Initialize stream description */
104 p_input->stream.i_es_number = 0;
105 p_input->stream.i_selected_es_number = 0;
106 p_input->stream.i_pgrm_number = 0;
107 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
108 p_input->stream.i_seek = 0;
110 /* Initialize stream control properties. */
111 p_input->stream.control.i_status = PLAYING_S;
112 p_input->stream.control.i_rate = DEFAULT_RATE;
113 p_input->stream.control.b_mute = 0;
114 p_input->stream.control.b_bw = 0;
116 /* Initialize default settings for spawned decoders */
117 p_input->p_default_aout = p_main->p_aout;
118 p_input->p_default_vout = p_main->p_intf->p_vout;
120 /* Create thread and set locks. */
121 vlc_mutex_init( &p_input->stream.stream_lock );
122 vlc_mutex_init( &p_input->stream.control.control_lock );
123 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
126 intf_ErrMsg( "input error: can't create input thread (%s)",
132 /* If status is NULL, wait until the thread is created */
133 if( pi_status == NULL )
137 msleep( THREAD_SLEEP );
138 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
139 && (i_status != THREAD_FATAL) );
140 if( i_status != THREAD_READY )
148 /*****************************************************************************
149 * input_DestroyThread: mark an input thread as zombie
150 *****************************************************************************
151 * This function should not return until the thread is effectively cancelled.
152 *****************************************************************************/
153 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
155 int i_status; /* thread status */
158 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
159 *p_input->pi_status = THREAD_DESTROY;
161 /* Request thread destruction */
164 /* If status is NULL, wait until thread has been destroyed */
165 if( pi_status == NULL )
169 msleep( THREAD_SLEEP );
170 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
171 && (i_status != THREAD_FATAL) );
175 /*****************************************************************************
176 * RunThread: main thread loop
177 *****************************************************************************
178 * Thread in charge of processing the network packets and demultiplexing.
179 *****************************************************************************/
180 static void RunThread( input_thread_t *p_input )
182 data_packet_t * pp_packets[INPUT_READ_ONCE];
185 InitThread( p_input );
187 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
194 vlc_mutex_lock( &p_input->stream.control.control_lock );
195 if( p_input->stream.control.i_status == BACKWARD_S
196 && p_input->pf_rewind != NULL )
198 p_input->pf_rewind( p_input );
199 /* FIXME: probably don't do it every loop, but when ? */
201 vlc_mutex_unlock( &p_input->stream.control.control_lock );
203 i_error = p_input->pf_read( p_input, pp_packets );
205 /* Demultiplex read packets. */
206 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
208 p_input->pf_demux( p_input, pp_packets[i] );
215 /* End of file - we do not set b_die because only the
216 * interface is allowed to do so. */
217 intf_WarnMsg( 1, "End of file reached" );
222 p_input->b_error = 1;
227 if( p_input->b_error || p_input->b_eof )
229 ErrorThread( p_input );
232 EndThread( p_input );
233 intf_DbgMsg("Thread end");
236 /*****************************************************************************
237 * InitThread: init the input Thread
238 *****************************************************************************/
239 static void InitThread( input_thread_t * p_input )
243 /* Initialize statistics */
244 p_input->c_loops = 0;
245 p_input->c_bytes = 0;
246 p_input->c_payload_bytes = 0;
247 p_input->c_packets_read = 0;
248 p_input->c_packets_trashed = 0;
251 p_input->p_input_module = module_Need( p_main->p_module_bank,
252 MODULE_CAPABILITY_INPUT, NULL );
254 if( p_input->p_input_module == NULL )
256 intf_ErrMsg( "input error: no suitable input module" );
257 p_input->b_error = 1;
261 #define f p_input->p_input_module->p_functions->input.functions.input
262 p_input->pf_init = f.pf_init;
263 p_input->pf_open = f.pf_open;
264 p_input->pf_close = f.pf_close;
265 p_input->pf_end = f.pf_end;
266 p_input->pf_read = f.pf_read;
267 p_input->pf_demux = f.pf_demux;
268 p_input->pf_new_packet = f.pf_new_packet;
269 p_input->pf_new_pes = f.pf_new_pes;
270 p_input->pf_delete_packet = f.pf_delete_packet;
271 p_input->pf_delete_pes = f.pf_delete_pes;
272 p_input->pf_rewind = f.pf_rewind;
273 p_input->pf_seek = f.pf_seek;
276 p_input->pf_open( p_input );
278 if( p_input->b_error )
280 module_Unneed( p_main->p_module_bank, p_input->p_input_module );
284 p_input->pf_init( p_input );
287 *p_input->pi_status = THREAD_READY;
290 /*****************************************************************************
291 * ErrorThread: RunThread() error loop
292 *****************************************************************************
293 * This function is called when an error occured during thread main's loop.
294 *****************************************************************************/
295 static void ErrorThread( input_thread_t *p_input )
297 while( !p_input->b_die )
300 msleep( INPUT_IDLE_SLEEP );
304 /*****************************************************************************
305 * EndThread: end the input thread
306 *****************************************************************************/
307 static void EndThread( input_thread_t * p_input )
309 int * pi_status; /* thread status */
312 pi_status = p_input->pi_status;
313 *pi_status = THREAD_END;
317 struct tms cpu_usage;
320 intf_Msg("input stats: cpu usage (user: %d, system: %d)",
321 cpu_usage.tms_utime, cpu_usage.tms_stime);
325 /* Free all ES and destroy all decoder threads */
326 input_EndStream( p_input );
329 p_input->pf_close( p_input );
331 /* Free demultiplexer's data */
332 p_input->pf_end( p_input );
334 /* Release modules */
335 module_Unneed( p_main->p_module_bank, p_input->p_input_module );
337 /* Destroy Mutex locks */
338 vlc_mutex_destroy( &p_input->stream.control.control_lock );
339 vlc_mutex_destroy( &p_input->stream.stream_lock );
341 /* Free input structure */
345 *pi_status = THREAD_OVER;
348 /*****************************************************************************
349 * input_FileOpen : open a file descriptor
350 *****************************************************************************/
351 void input_FileOpen( input_thread_t * p_input )
353 struct stat stat_info;
355 if( stat( p_input->p_source, &stat_info ) == (-1) )
357 intf_ErrMsg( "input error: cannot stat() file `%s' (%s)",
358 p_input->p_source, strerror(errno));
359 p_input->b_error = 1;
363 vlc_mutex_lock( &p_input->stream.stream_lock );
365 /* If we are here we can control the pace... */
366 p_input->stream.b_pace_control = 1;
368 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
369 || S_ISBLK(stat_info.st_mode) )
371 p_input->stream.b_seekable = 1;
372 p_input->stream.i_size = stat_info.st_size;
374 else if( S_ISFIFO(stat_info.st_mode) || S_ISSOCK(stat_info.st_mode) )
376 p_input->stream.b_seekable = 0;
377 p_input->stream.i_size = 0;
381 vlc_mutex_unlock( &p_input->stream.stream_lock );
382 intf_ErrMsg( "input error: unknown file type for `%s'",
384 p_input->b_error = 1;
388 p_input->stream.i_tell = 0;
389 vlc_mutex_unlock( &p_input->stream.stream_lock );
391 intf_Msg( "input: opening file %s", p_input->p_source );
392 if( (p_input->i_handle = open( p_input->p_source,
393 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
395 intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
396 p_input->b_error = 1;
402 /*****************************************************************************
403 * input_FileClose : close a file descriptor
404 *****************************************************************************/
405 void input_FileClose( input_thread_t * p_input )
407 close( p_input->i_handle );