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
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <sys/types.h>
39 # include <sys/times.h>
49 #include "stream_control.h"
50 #include "input_ext-intf.h"
51 #include "input_ext-dec.h"
55 /*****************************************************************************
57 *****************************************************************************/
58 static void RunThread ( input_thread_t *p_input );
59 static void InitThread ( input_thread_t *p_input );
60 static void ErrorThread ( input_thread_t *p_input );
61 static void EndThread ( input_thread_t *p_input );
62 static void NetworkOpen ( input_thread_t *p_input );
63 static void FileOpen ( input_thread_t *p_input );
65 /*****************************************************************************
66 * input_CreateThread: creates a new input thread
67 *****************************************************************************
68 * This function creates a new input, and returns a pointer
69 * to its description. On error, it returns NULL.
70 * If pi_status is NULL, then the function will block until the thread is ready.
71 * If not, it will be updated using one of the THREAD_* constants.
72 *****************************************************************************/
73 input_thread_t *input_CreateThread ( input_config_t * p_config, int *pi_status )
75 input_thread_t * p_input; /* thread descriptor */
76 int i_status; /* thread status */
79 /* Allocate descriptor */
81 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
84 intf_ErrMsg("error: %s\n", strerror(errno));
89 /* Initialize thread properties */
92 /* I have never understood that stuff --Meuuh */
93 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
94 *p_input->pi_status = THREAD_CREATE;
95 p_input->p_config = p_config;
97 /* Initialize stream description */
98 for( i = 0; i < INPUT_MAX_SELECTED_ES; i++ )
100 p_input->pp_selected_es[i] = NULL;
102 for( i= 0; i < INPUT_MAX_ES; i++ )
104 p_input->p_es[i].i_id = EMPTY_ID;
106 p_input->stream.i_pgrm_number = 0;
108 /* Initialize stream control properties. */
109 p_input->stream.control.i_status = PLAYING_S;
110 p_input->stream.control.i_rate = DEFAULT_RATE;
111 p_input->stream.control.i_ref_sysdate = 0;
112 p_input->stream.control.i_ref_clock = 0;
113 p_input->stream.control.b_mute = 0;
114 p_input->stream.control.b_bw = 0;
116 /* Create thread and set locks. */
117 vlc_mutex_init( &p_input->stream.stream_lock );
118 vlc_mutex_init( &p_input->stream.control.control_lock );
119 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
122 intf_ErrMsg("error: %s\n", strerror(errno) );
128 /* If status is NULL, wait until the thread is created */
129 if( pi_status == NULL )
133 msleep( THREAD_SLEEP );
134 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
135 && (i_status != THREAD_FATAL) );
136 if( i_status != THREAD_READY )
144 /*****************************************************************************
145 * input_DestroyThread: mark an input thread as zombie
146 *****************************************************************************
147 * This function should not return until the thread is effectively cancelled.
148 *****************************************************************************/
149 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
151 int i_status; /* thread status */
154 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
155 *p_input->pi_status = THREAD_DESTROY;
157 /* Request thread destruction */
160 /* If status is NULL, wait until thread has been destroyed */
161 if( pi_status == NULL )
165 msleep( THREAD_SLEEP );
166 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
167 && (i_status != THREAD_FATAL) );
171 /*****************************************************************************
172 * RunThread: main thread loop
173 *****************************************************************************
174 * Thread in charge of processing the network packets and demultiplexing.
175 *****************************************************************************/
176 static void RunThread( input_thread_t *p_input )
178 data_packet_t * pp_packets[INPUT_READ_ONCE];
180 InitThread( p_input );
182 while( !p_input->b_die && !p_input->b_error )
188 vlc_mutex_lock( &p_input->stream.control.control_lock );
189 if( p_input->stream.control.i_status == BACKWARD_S
190 && p_input->p_plugin->pf_rewind != NULL )
192 p_input->p_plugin->pf_rewind( p_input );
193 /* FIXME: probably don't do it every loop, but when ? */
195 vlc_mutex_unlock( &p_input->stream.control.control_lock );
197 p_input->p_plugin->pf_read( p_input, pp_packets );
198 if( !p_input->b_error )
202 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
204 p_input->p_plugin->pf_demux( p_input, pp_packets[i] );
209 if( p_input->b_error )
211 ErrorThread( p_input );
214 EndThread( p_input );
215 intf_DbgMsg("Thread end");
218 /*****************************************************************************
219 * InitThread: init the input thread
220 *****************************************************************************/
221 input_capabilities_t * PSKludge( void );
222 static void InitThread( input_thread_t * p_input )
224 /* Initialize default settings for spawned decoders */
225 p_input->p_default_aout = p_input->p_config->p_default_aout;
226 p_input->p_default_vout = p_input->p_config->p_default_vout;
229 /* Initialize statistics */
230 p_input->c_loops = 0;
231 p_input->c_bytes = 0;
232 p_input->c_payload_bytes = 0;
233 p_input->c_packets_read = 0;
234 p_input->c_packets_trashed = 0;
237 /* Use the appropriate input method */
238 switch( p_input->p_config->i_method )
240 case INPUT_METHOD_FILE: /* file methods */
243 case INPUT_METHOD_VLAN_BCAST: /* vlan network method */
244 /* if( !p_main->b_vlans )
246 intf_ErrMsg("error: vlans are not activated\n");
250 /* ... pass through */
251 case INPUT_METHOD_UCAST: /* network methods */
252 case INPUT_METHOD_MCAST:
253 case INPUT_METHOD_BCAST:
254 NetworkOpen( p_input );
258 intf_ErrMsg("Unknow input method");
259 free( p_input->p_config );
260 p_input->b_error = 1;
265 free( p_input->p_config );
267 /* Probe plugin (FIXME: load plugins before & write this) */
268 p_input->p_plugin = PSKludge();
269 p_input->p_plugin->pf_init( p_input );
271 *p_input->pi_status = THREAD_READY;
274 /*****************************************************************************
275 * ErrorThread: RunThread() error loop
276 *****************************************************************************
277 * This function is called when an error occured during thread main's loop.
278 *****************************************************************************/
279 static void ErrorThread( input_thread_t *p_input )
281 while( !p_input->b_die )
284 msleep( INPUT_IDLE_SLEEP );
288 /*****************************************************************************
289 * EndThread: end the input thread
290 *****************************************************************************/
291 static void EndThread( input_thread_t * p_input )
293 int * pi_status; /* thread status */
294 int i_es_loop; /* es index */
297 pi_status = p_input->pi_status;
298 *pi_status = THREAD_END;
302 struct tms cpu_usage;
305 intf_Msg("input stats: cpu usage (user: %d, system: %d)\n",
306 cpu_usage.tms_utime, cpu_usage.tms_stime);
310 /* Destroy all decoder threads */
312 (i_es_loop < INPUT_MAX_ES)
313 && (p_input->pp_selected_es[i_es_loop] != NULL) ;
316 p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->b_die = 1;
317 /* Make sure the thread leaves the GetByte() function */
318 vlc_mutex_lock( &p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->data_lock);
319 vlc_cond_signal( &p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->data_wait );
320 vlc_mutex_unlock( &p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->data_lock );
322 /* Waiting for the thread to exit */
323 vlc_thread_join( p_input->pp_selected_es[i_es_loop]->thread_id );
324 free( p_input->pp_selected_es[i_es_loop]->p_decoder_fifo );
327 /* Free demultiplexer's data */
330 *pi_status = THREAD_OVER;
333 /*****************************************************************************
334 * NetworkOpen : open a network socket descriptor
335 *****************************************************************************/
336 static void NetworkOpen( input_thread_t * p_input )
338 /* straight copy & paste of input_network.c of input-I */
340 /* We cannot rewind nor lseek() */
341 p_input->stream.b_seekable = 0;
342 /* We cannot control the pace */
343 p_input->stream.b_pace_control = 0;
346 /*****************************************************************************
347 * FileOpen : open a file descriptor
348 *****************************************************************************/
349 static void FileOpen( input_thread_t * p_input )
351 struct stat stat_info;
353 #define p_config p_input->p_config
355 if( !strncmp( p_config->p_source, "-", 1 ) )
358 p_input->i_handle = 0;
360 vlc_mutex_lock( &p_input->stream.stream_lock );
361 p_input->stream.b_pace_control = 1;
362 p_input->stream.b_seekable = 0;
363 p_input->stream.i_size = 0;
364 p_input->stream.i_tell = 0;
365 vlc_mutex_unlock( &p_input->stream.stream_lock );
369 if( stat( p_config->p_source, &stat_info ) == (-1) )
371 intf_ErrMsg("Cannot stat() file %s (%s)", p_config->p_source,
373 p_input->b_error = 1;
377 vlc_mutex_lock( &p_input->stream.stream_lock );
379 /* If we are here we can control the pace... */
380 p_input->stream.b_pace_control = 1;
382 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
383 || S_ISBLK(stat_info.st_mode) )
385 p_input->stream.b_seekable = 1;
386 p_input->stream.i_size = stat_info.st_size;
388 else if( S_ISFIFO(stat_info.st_mode) || S_ISSOCK(stat_info.st_mode) )
390 p_input->stream.b_seekable = 0;
391 p_input->stream.i_size = 0;
395 vlc_mutex_unlock( &p_input->stream.stream_lock );
396 intf_ErrMsg("Unknown file type");
397 p_input->b_error = 1;
401 p_input->stream.i_tell = 0;
402 vlc_mutex_unlock( &p_input->stream.stream_lock );
404 intf_Msg( "Opening file %s", p_config->p_source );
405 if( (p_input->i_handle = open( p_config->p_source,
406 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
408 intf_ErrMsg("Cannot open file (%s)", strerror(errno));
409 p_input->b_error = 1;