1 /*****************************************************************************
2 * input.c: input thread
3 * Read an MPEG2 stream, demultiplex and parse it before sending it to
5 *****************************************************************************
6 * Copyright (C) 1998-2001 VideoLAN
7 * $Id: input.c,v 1.199 2002/06/01 18:04:49 sam 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 *****************************************************************************/
30 #include <sys/types.h>
39 #ifdef HAVE_SYS_TIMES_H
40 # include <sys/times.h>
46 #include "stream_control.h"
47 #include "input_ext-intf.h"
48 #include "input_ext-dec.h"
49 #include "input_ext-plugins.h"
51 #include "interface.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 static int RunThread ( input_thread_t *p_input );
57 static int InitThread ( input_thread_t *p_input );
58 static void ErrorThread ( input_thread_t *p_input );
59 static void CloseThread ( input_thread_t *p_input );
60 static void DestroyThread ( input_thread_t *p_input );
61 static void EndThread ( input_thread_t *p_input );
63 /*****************************************************************************
64 * input_CreateThread: creates a new input thread
65 *****************************************************************************
66 * This function creates a new input, and returns a pointer
67 * to its description. On error, it returns NULL.
68 * If pi_status is NULL, then the function will block until the thread is ready.
69 * If not, it will be updated using one of the THREAD_* constants.
70 *****************************************************************************/
71 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
72 playlist_item_t *p_item, int *pi_status )
74 input_thread_t * p_input; /* thread descriptor */
76 /* Allocate descriptor */
77 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
80 msg_Err( p_parent, "out of memory" );
84 /* Initialize thread properties */
88 p_input->psz_source = strdup( p_item->psz_name );
91 p_input->i_status = THREAD_CREATE;
94 p_input->p_demux_module = NULL;
95 p_input->pf_init = NULL;
96 p_input->pf_end = NULL;
97 p_input->pf_demux = NULL;
98 p_input->pf_rewind = NULL;
101 p_input->p_access_module = NULL;
102 p_input->pf_open = NULL;
103 p_input->pf_close = NULL;
104 p_input->pf_read = NULL;
105 p_input->pf_seek = NULL;
106 p_input->pf_set_area = NULL;
107 p_input->pf_set_program = NULL;
109 p_input->i_bufsize = 0;
112 /* Initialize statistics */
113 p_input->c_loops = 0;
114 p_input->stream.c_packets_read = 0;
115 p_input->stream.c_packets_trashed = 0;
118 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
119 vlc_cond_init( &p_input->stream.stream_wait );
120 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
122 /* Initialize stream description */
123 p_input->stream.b_changed = 0;
124 p_input->stream.i_es_number = 0;
125 p_input->stream.i_selected_es_number = 0;
126 p_input->stream.i_pgrm_number = 0;
127 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
128 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
129 p_input->stream.i_mux_rate = 0;
130 p_input->stream.b_seekable = 0;
132 /* no stream, no program, no area, no es */
133 p_input->stream.p_new_program = NULL;
135 p_input->stream.i_area_nb = 0;
136 p_input->stream.pp_areas = NULL;
137 p_input->stream.p_selected_area = NULL;
138 p_input->stream.p_new_area = NULL;
140 p_input->stream.pp_selected_es = NULL;
141 p_input->stream.p_removed_es = NULL;
142 p_input->stream.p_newly_selected_es = NULL;
144 /* By default there is one area in a stream */
145 input_AddArea( p_input );
146 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
148 /* Initialize stream control properties. */
149 p_input->stream.control.i_status = PLAYING_S;
150 p_input->stream.control.i_rate = DEFAULT_RATE;
151 p_input->stream.control.b_mute = 0;
152 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
153 p_input->stream.control.i_smp = config_GetInt( p_input, "vdec-smp" );
155 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
157 vlc_object_attach( p_input, p_parent );
160 if( vlc_thread_create( p_input, "input", RunThread, 1 ) )
162 msg_Err( p_input, "cannot create input thread (%s)", strerror(errno) );
168 /* If status is NULL, wait until the thread is created */
169 if( pi_status == NULL )
173 msleep( THREAD_SLEEP );
174 } while( (i_status != THREAD_READY) && (i_status != THREAD_ERROR)
175 && (i_status != THREAD_FATAL) );
182 /*****************************************************************************
183 * input_StopThread: mark an input thread as zombie
184 *****************************************************************************
185 * This function should not return until the thread is effectively cancelled.
186 *****************************************************************************/
187 void input_StopThread( input_thread_t *p_input, int *pi_status )
189 /* Make the thread exit from a possible vlc_cond_wait() */
190 vlc_mutex_lock( &p_input->stream.stream_lock );
191 /* Request thread destruction */
194 vlc_cond_signal( &p_input->stream.stream_wait );
195 vlc_mutex_unlock( &p_input->stream.stream_lock );
197 /* If status is NULL, wait until thread has been destroyed */
199 if( pi_status == NULL )
203 msleep( THREAD_SLEEP );
204 } while ( (i_status != THREAD_OVER) && (i_status != THREAD_ERROR)
205 && (i_status != THREAD_FATAL) );
210 /*****************************************************************************
211 * input_DestroyThread: mark an input thread as zombie
212 *****************************************************************************
213 * This function should not return until the thread is effectively cancelled.
214 *****************************************************************************/
215 void input_DestroyThread( input_thread_t *p_input )
217 /* Join the thread */
218 vlc_thread_join( p_input );
220 /* Destroy Mutex locks */
221 vlc_mutex_destroy( &p_input->stream.control.control_lock );
222 vlc_cond_destroy( &p_input->stream.stream_wait );
223 vlc_mutex_destroy( &p_input->stream.stream_lock );
225 /* Free input structure */
229 /*****************************************************************************
230 * RunThread: main thread loop
231 *****************************************************************************
232 * Thread in charge of processing the network packets and demultiplexing.
233 *****************************************************************************/
234 static int RunThread( input_thread_t *p_input )
236 if( InitThread( p_input ) )
238 /* If we failed, wait before we are killed, and exit */
239 p_input->i_status = THREAD_ERROR;
240 p_input->b_error = 1;
241 vlc_thread_ready( p_input );
242 ErrorThread( p_input );
243 DestroyThread( p_input );
247 vlc_thread_ready( p_input );
249 p_input->i_status = THREAD_READY;
251 /* initialization is complete */
252 vlc_mutex_lock( &p_input->stream.stream_lock );
253 p_input->stream.b_changed = 1;
254 vlc_mutex_unlock( &p_input->stream.stream_lock );
256 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
262 vlc_mutex_lock( &p_input->stream.stream_lock );
264 if( p_input->stream.p_new_program )
266 if( p_input->pf_set_program != NULL )
269 /* Reinitialize buffer manager. */
270 input_AccessReinit( p_input );
272 p_input->pf_set_program( p_input,
273 p_input->stream.p_new_program );
275 /* Escape all decoders for the stream discontinuity they
277 input_EscapeDiscontinuity( p_input );
279 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
281 pgrm_descriptor_t * p_pgrm
282 = p_input->stream.pp_programs[i];
284 /* Reinitialize synchro. */
285 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
288 p_input->stream.p_new_program = NULL;
291 if( p_input->stream.p_new_area )
293 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
295 input_AccessReinit( p_input );
297 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
299 /* Escape all decoders for the stream discontinuity they
301 input_EscapeDiscontinuity( p_input );
303 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
305 pgrm_descriptor_t * p_pgrm
306 = p_input->stream.pp_programs[i];
308 /* Reinitialize synchro. */
309 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
312 p_input->stream.p_new_area = NULL;
315 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
317 if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
321 /* Reinitialize buffer manager. */
322 input_AccessReinit( p_input );
324 i_new_pos = p_input->stream.p_selected_area->i_seek;
325 vlc_mutex_unlock( &p_input->stream.stream_lock );
326 p_input->pf_seek( p_input, i_new_pos );
327 vlc_mutex_lock( &p_input->stream.stream_lock );
329 /* Escape all decoders for the stream discontinuity they
331 input_EscapeDiscontinuity( p_input );
333 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
335 pgrm_descriptor_t * p_pgrm
336 = p_input->stream.pp_programs[i];
338 /* Reinitialize synchro. */
339 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
342 p_input->stream.p_selected_area->i_seek = NO_SEEK;
345 if( p_input->stream.p_removed_es )
347 input_UnselectES( p_input, p_input->stream.p_removed_es );
348 p_input->stream.p_removed_es = NULL;
351 if( p_input->stream.p_newly_selected_es )
353 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
354 p_input->stream.p_newly_selected_es = NULL;
357 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
359 if( p_input->stream.b_new_mute )
361 input_EscapeAudioDiscontinuity( p_input );
364 vlc_mutex_lock( &p_input->stream.control.control_lock );
365 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
366 vlc_mutex_unlock( &p_input->stream.control.control_lock );
368 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
371 vlc_mutex_unlock( &p_input->stream.stream_lock );
373 /* Read and demultiplex some data. */
374 i_count = p_input->pf_demux( p_input );
376 if( i_count == 0 && p_input->stream.b_seekable )
378 /* End of file - we do not set b_die because only the
379 * interface is allowed to do so. */
380 msg_Info( p_input, "EOF reached" );
383 else if( i_count < 0 )
385 p_input->b_error = 1;
389 if( p_input->b_error || p_input->b_eof )
391 ErrorThread( p_input );
394 EndThread( p_input );
396 DestroyThread( p_input );
401 /*****************************************************************************
402 * InitThread: init the input Thread
403 *****************************************************************************/
404 static int InitThread( input_thread_t * p_input )
406 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
407 char * psz_parser = p_input->psz_source;
409 /* Skip the plug-in names */
410 while( *psz_parser && *psz_parser != ':' )
415 if( psz_parser - p_input->psz_source == 1 )
417 msg_Warn( p_input, "drive letter %c: found in source string",
418 p_input->psz_source ) ;
425 p_input->psz_access = p_input->psz_demux = "";
426 p_input->psz_name = p_input->psz_source;
430 *psz_parser++ = '\0';
432 /* let's skip '//' */
433 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
438 p_input->psz_name = psz_parser ;
440 /* Come back to parse the access and demux plug-ins */
441 psz_parser = p_input->psz_source;
446 p_input->psz_access = "";
448 else if( *psz_parser == '/' )
451 p_input->psz_access = "";
456 p_input->psz_access = psz_parser;
458 while( *psz_parser && *psz_parser != '/' )
463 if( *psz_parser == '/' )
465 *psz_parser++ = '\0';
472 p_input->psz_demux = "";
476 p_input->psz_demux = psz_parser;
480 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
481 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
483 if( input_AccessInit( p_input ) == -1 )
488 /* Find and open appropriate access module */
489 p_input->p_access_module =
490 module_Need( p_input, MODULE_CAPABILITY_ACCESS,
491 p_input->psz_access, (void *)p_input );
493 if( p_input->p_access_module == NULL )
495 msg_Err( p_input, "no suitable access module for `%s/%s:%s'",
496 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
500 #define f p_input->p_access_module->p_functions->access.functions.access
501 p_input->pf_open = f.pf_open;
502 p_input->pf_close = f.pf_close;
503 p_input->pf_read = f.pf_read;
504 p_input->pf_set_area = f.pf_set_area;
505 p_input->pf_set_program = f.pf_set_program;
506 p_input->pf_seek = f.pf_seek;
509 /* Waiting for stream. */
512 p_input->i_bufsize = p_input->i_mtu;
516 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
519 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
521 while( !input_FillBuffer( p_input ) )
523 if( p_input->b_die || p_input->b_error || p_input->b_eof )
525 module_Unneed( p_input->p_access_module );
531 /* Find and open appropriate demux module */
532 p_input->p_demux_module =
533 module_Need( p_input, MODULE_CAPABILITY_DEMUX,
534 p_input->psz_demux, (void *)p_input );
536 if( p_input->p_demux_module == NULL )
538 msg_Err( p_input, "no suitable demux module for `%s/%s:%s'",
539 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
540 module_Unneed( p_input->p_access_module );
544 #define f p_input->p_demux_module->p_functions->demux.functions.demux
545 p_input->pf_init = f.pf_init;
546 p_input->pf_end = f.pf_end;
547 p_input->pf_demux = f.pf_demux;
548 p_input->pf_rewind = f.pf_rewind;
554 /*****************************************************************************
555 * ErrorThread: RunThread() error loop
556 *****************************************************************************
557 * This function is called when an error occured during thread main's loop.
558 *****************************************************************************/
559 static void ErrorThread( input_thread_t *p_input )
561 while( !p_input->b_die )
564 msleep( INPUT_IDLE_SLEEP );
568 /*****************************************************************************
569 * EndThread: end the input thread
570 *****************************************************************************/
571 static void EndThread( input_thread_t * p_input )
573 #ifdef HAVE_SYS_TIMES_H
574 /* Display statistics */
575 struct tms cpu_usage;
578 msg_Dbg( p_input, "%d loops consuming user: %d, system: %d",
579 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
581 msg_Dbg( p_input, "%d loops", p_input->c_loops );
584 input_DumpStream( p_input );
587 p_input->i_status = THREAD_END;
589 /* Free all ES and destroy all decoder threads */
590 input_EndStream( p_input );
592 /* Free demultiplexer's data */
593 p_input->pf_end( p_input );
594 module_Unneed( p_input->p_demux_module );
596 /* Close the access plug-in */
597 CloseThread( p_input );
600 /*****************************************************************************
601 * CloseThread: close the target
602 *****************************************************************************/
603 static void CloseThread( input_thread_t * p_input )
605 p_input->pf_close( p_input );
606 module_Unneed( p_input->p_access_module );
608 input_AccessEnd( p_input );
610 free( p_input->psz_source );
613 /*****************************************************************************
614 * DestroyThread: destroy the input thread
615 *****************************************************************************/
616 static void DestroyThread( input_thread_t * p_input )
619 p_input->i_status = THREAD_OVER;