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>
45 #include "stream_control.h"
46 #include "input_ext-intf.h"
47 #include "input_ext-dec.h"
51 /*****************************************************************************
53 *****************************************************************************/
54 static void RunThread ( input_thread_t *p_input );
55 static void InitThread ( input_thread_t *p_input );
56 static void ErrorThread ( input_thread_t *p_input );
57 static void EndThread ( input_thread_t *p_input );
58 static void NetworkOpen ( input_thread_t *p_input );
59 static void FileOpen ( input_thread_t *p_input );
61 /*****************************************************************************
62 * input_CreateThread: creates a new input thread
63 *****************************************************************************
64 * This function creates a new input, and returns a pointer
65 * to its description. On error, it returns NULL.
66 * If pi_status is NULL, then the function will block until the thread is ready.
67 * If not, it will be updated using one of the THREAD_* constants.
68 *****************************************************************************/
69 input_thread_t *input_CreateThread ( input_config_t * p_config, int *pi_status )
71 input_thread_t * p_input; /* thread descriptor */
72 int i_status; /* thread status */
75 /* Allocate descriptor */
77 p_input = (input_thread_t *)malloc( sizeof(input_thread_t) );
80 intf_ErrMsg("error: %s\n", strerror(errno));
85 /* Initialize thread properties */
88 /* I have never understood that stuff --Meuuh */
89 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
90 *p_input->pi_status = THREAD_CREATE;
91 p_input->p_config = p_config;
93 /* Initialize stream description */
94 for( i = 0; i < INPUT_MAX_SELECTED_ES; i++ )
96 p_input->pp_selected_es[i] = NULL;
98 for( i= 0; i < INPUT_MAX_ES; i++ )
100 p_input->p_es[i].i_id = EMPTY_ID;
102 p_input->stream.i_pgrm_number = 0;
104 /* Initialize stream control properties. */
105 p_input->stream.control.i_status = PLAYING_S;
106 p_input->stream.control.i_rate = DEFAULT_RATE;
107 p_input->stream.control.i_ref_sysdate = 0;
108 p_input->stream.control.i_ref_clock = 0;
109 p_input->stream.control.b_mute = 0;
110 p_input->stream.control.b_bw = 0;
112 /* Create thread and set locks. */
113 vlc_mutex_init( &p_input->stream.stream_lock );
114 vlc_mutex_init( &p_input->stream.control.control_lock );
115 if( vlc_thread_create( &p_input->thread_id, "input", (void *) RunThread,
118 intf_ErrMsg("error: %s\n", strerror(errno) );
124 /* If status is NULL, wait until the thread is created */
125 if( pi_status == NULL )
129 msleep( THREAD_SLEEP );
130 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
131 && (i_status != THREAD_FATAL) );
132 if( i_status != THREAD_READY )
140 /*****************************************************************************
141 * input_DestroyThread: mark an input thread as zombie
142 *****************************************************************************
143 * This function should not return until the thread is effectively cancelled.
144 *****************************************************************************/
145 void input_DestroyThread( input_thread_t *p_input, int *pi_status )
147 int i_status; /* thread status */
150 p_input->pi_status = (pi_status != NULL) ? pi_status : &i_status;
151 *p_input->pi_status = THREAD_DESTROY;
153 /* Request thread destruction */
156 /* If status is NULL, wait until thread has been destroyed */
157 if( pi_status == NULL )
161 msleep( THREAD_SLEEP );
162 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
163 && (i_status != THREAD_FATAL) );
167 /*****************************************************************************
168 * RunThread: main thread loop
169 *****************************************************************************
170 * Thread in charge of processing the network packets and demultiplexing.
171 *****************************************************************************/
172 static void RunThread( input_thread_t *p_input )
174 data_packet_t * pp_packets[INPUT_READ_ONCE];
176 InitThread( p_input );
178 while( !p_input->b_die && !p_input->b_error )
184 vlc_mutex_lock( &p_input->stream.control.control_lock );
185 if( p_input->stream.control.i_status == BACKWARD_S
186 && p_input->p_plugin->pf_rewind != NULL )
188 p_input->p_plugin->pf_rewind( p_input );
189 /* FIXME: probably don't do it every loop, but when ? */
191 vlc_mutex_unlock( &p_input->stream.control.control_lock );
193 p_input->p_plugin->pf_read( p_input, pp_packets );
194 if( !p_input->b_error )
198 for( i = 0; i < INPUT_READ_ONCE && pp_packets[i] != NULL; i++ )
200 p_input->p_plugin->pf_demux( p_input, pp_packets[i] );
205 if( p_input->b_error )
207 ErrorThread( p_input );
210 EndThread( p_input );
211 intf_DbgMsg("Thread end");
214 /*****************************************************************************
215 * InitThread: init the input thread
216 *****************************************************************************/
217 input_capabilities_t * PSKludge( void );
218 static void InitThread( input_thread_t * p_input )
220 /* Initialize default settings for spawned decoders */
221 p_input->p_default_aout = p_input->p_config->p_default_aout;
222 p_input->p_default_vout = p_input->p_config->p_default_vout;
225 /* Initialize statistics */
226 p_input->c_loops = 0;
227 p_input->c_bytes = 0;
228 p_input->c_payload_bytes = 0;
229 p_input->c_packets_read = 0;
230 p_input->c_packets_trashed = 0;
233 /* Use the appropriate input method */
234 switch( p_input->p_config->i_method )
236 case INPUT_METHOD_FILE: /* file methods */
239 case INPUT_METHOD_VLAN_BCAST: /* vlan network method */
240 /* if( !p_main->b_vlans )
242 intf_ErrMsg("error: vlans are not activated\n");
246 /* ... pass through */
247 case INPUT_METHOD_UCAST: /* network methods */
248 case INPUT_METHOD_MCAST:
249 case INPUT_METHOD_BCAST:
250 NetworkOpen( p_input );
254 intf_ErrMsg("Unknow input method");
255 free( p_input->p_config );
256 p_input->b_error = 1;
261 free( p_input->p_config );
263 /* Probe plugin (FIXME: load plugins before & write this) */
264 p_input->p_plugin = PSKludge();
265 p_input->p_plugin->pf_init( p_input );
267 *p_input->pi_status = THREAD_READY;
270 /*****************************************************************************
271 * ErrorThread: RunThread() error loop
272 *****************************************************************************
273 * This function is called when an error occured during thread main's loop.
274 *****************************************************************************/
275 static void ErrorThread( input_thread_t *p_input )
277 while( !p_input->b_die )
280 msleep( INPUT_IDLE_SLEEP );
284 /*****************************************************************************
285 * EndThread: end the input thread
286 *****************************************************************************/
287 static void EndThread( input_thread_t * p_input )
289 int * pi_status; /* thread status */
290 int i_es_loop; /* es index */
293 pi_status = p_input->pi_status;
294 *pi_status = THREAD_END;
296 /* Destroy all decoder threads */
298 (i_es_loop < INPUT_MAX_ES)
299 && (p_input->pp_selected_es[i_es_loop] != NULL) ;
302 p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->b_die = 1;
303 /* Make sure the thread leaves the GetByte() function */
304 vlc_mutex_lock( &p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->data_lock);
305 vlc_cond_signal( &p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->data_wait );
306 vlc_mutex_unlock( &p_input->pp_selected_es[i_es_loop]->p_decoder_fifo->data_lock );
308 /* Waiting for the thread to exit */
309 vlc_thread_join( p_input->pp_selected_es[i_es_loop]->thread_id );
310 free( p_input->pp_selected_es[i_es_loop]->p_decoder_fifo );
313 /* Free demultiplexer's data */
316 *pi_status = THREAD_OVER;
319 /*****************************************************************************
320 * NetworkOpen : open a network socket descriptor
321 *****************************************************************************/
322 static void NetworkOpen( input_thread_t * p_input )
324 /* straight copy & paste of input_network.c of input-I */
326 /* We cannot rewind nor lseek() */
327 p_input->stream.b_seekable = 0;
328 /* We cannot control the pace */
329 p_input->stream.b_pace_control = 0;
332 /*****************************************************************************
333 * FileOpen : open a file descriptor
334 *****************************************************************************/
335 static void FileOpen( input_thread_t * p_input )
337 struct stat stat_info;
339 #define p_config p_input->p_config
341 if( stat( p_config->p_source, &stat_info ) == (-1) )
343 intf_ErrMsg("Cannot stat() file %s (%s)", p_config->p_source,
345 p_input->b_error = 1;
349 vlc_mutex_lock( &p_input->stream.stream_lock );
351 /* If we are here we can control the pace... */
352 p_input->stream.b_pace_control = 1;
354 if( S_ISREG(stat_info.st_mode) || S_ISCHR(stat_info.st_mode)
355 || S_ISBLK(stat_info.st_mode) )
357 p_input->stream.b_seekable = 1;
358 p_input->stream.i_size = stat_info.st_size;
360 else if( S_ISFIFO(stat_info.st_mode) || S_ISSOCK(stat_info.st_mode) )
362 p_input->stream.b_seekable = 0;
363 p_input->stream.i_size = 0;
367 vlc_mutex_unlock( &p_input->stream.stream_lock );
368 intf_ErrMsg("Unknown file type");
369 p_input->b_error = 1;
373 p_input->stream.i_tell = 0;
374 vlc_mutex_unlock( &p_input->stream.stream_lock );
376 intf_Msg( "Opening file %s", p_config->p_source );
377 if( (p_input->i_handle = open( p_config->p_source,
378 /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
380 intf_ErrMsg("Cannot open file (%s)", strerror(errno));
381 p_input->b_error = 1;