1 /*****************************************************************************
2 * input.c: input thread
3 * Read a stream, demultiplex and parse it before sending it to
5 *****************************************************************************
6 * Copyright (C) 1998-2004 VideoLAN
7 * $Id: input.c,v 1.279 2004/01/26 20:26:54 gbazin 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 <vlc/input.h>
33 #include <vlc/decoder.h>
36 #ifdef HAVE_SYS_TIMES_H
37 # include <sys/times.h>
40 #include "vlc_playlist.h"
42 #include "stream_output.h"
44 #include "vlc_interface.h"
46 #include "../../modules/demux/util/sub.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 struct input_thread_sys_t
55 subtitle_demux_t **sub;
58 static int RunThread ( input_thread_t *p_input );
59 static int InitThread ( input_thread_t *p_input );
60 static void ErrorThread ( input_thread_t *p_input );
61 static void EndThread ( input_thread_t *p_input );
63 static void ParseOption ( input_thread_t *p_input,
64 const char *psz_option );
66 /*****************************************************************************
68 *****************************************************************************/
69 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
70 vlc_value_t oldval, vlc_value_t newval, void *p_data );
71 static int TimeCallback ( vlc_object_t *p_this, char const *psz_cmd,
72 vlc_value_t oldval, vlc_value_t newval, void *p_data );
73 static int StateCallback ( vlc_object_t *p_this, char const *psz_cmd,
74 vlc_value_t oldval, vlc_value_t newval, void *p_data );
75 static int RateCallback ( vlc_object_t *p_this, char const *psz_cmd,
76 vlc_value_t oldval, vlc_value_t newval, void *p_data );
78 /*****************************************************************************
79 * input_CreateThread: creates a new input thread
80 *****************************************************************************
81 * This function creates a new input, and returns a pointer
82 * to its description. On error, it returns NULL.
83 *****************************************************************************/
84 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
85 playlist_item_t *p_item )
87 input_thread_t * p_input; /* thread descriptor */
91 /* Allocate descriptor */
92 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
95 msg_Err( p_parent, "out of memory" );
99 /* Parse input options */
100 for( i = 0 ; i < p_item->i_categories ; i++ )
102 if( !strncmp( p_item->pp_categories[i]->psz_name, _("Options"), 7 ) )
104 msg_Dbg( p_input,"Parsing %i options for item",
105 p_item->pp_categories[i]->i_infos );
106 for( j = 0; j< p_item->pp_categories[i]->i_infos ; j++ )
108 msg_Dbg( p_input,"Option : %s",
109 p_item->pp_categories[i]->pp_infos[j]->psz_name);
110 ParseOption( p_input,
111 p_item->pp_categories[i]->pp_infos[j]->psz_value);
117 /* Create a few object variables we'll need later on */
118 var_Create( p_input, "video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
119 var_Create( p_input, "audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
120 var_Create( p_input, "audio-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
121 var_Create( p_input, "spu-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
122 var_Create( p_input, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
123 var_Create( p_input, "sub-autodetect-file", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
124 var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
126 var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
127 var_Create( p_input, "sout-all", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
128 var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
129 var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
130 var_Create( p_input, "sout-keep", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
133 var_Create( p_input, "minimize-threads", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
137 /* position variable */
138 var_Create( p_input, "position", VLC_VAR_FLOAT ); /* position 0.0->1.0 */
139 var_Create( p_input, "position-offset", VLC_VAR_FLOAT ); /* relative */
141 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
142 var_AddCallback( p_input, "position", PositionCallback, NULL );
143 var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
146 var_Create( p_input, "time", VLC_VAR_TIME );
147 var_Create( p_input, "time-offset", VLC_VAR_TIME ); /* relative */
149 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
150 var_AddCallback( p_input, "time", TimeCallback, NULL );
151 var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
153 /* length variable */
154 var_Create( p_input, "length", VLC_VAR_TIME );
156 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
159 var_Create( p_input, "rate", VLC_VAR_INTEGER );
160 var_Create( p_input, "rate-slower", VLC_VAR_VOID );
161 var_Create( p_input, "rate-faster", VLC_VAR_VOID );
162 val.i_int = DEFAULT_RATE;
163 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
164 var_AddCallback( p_input, "rate", RateCallback, NULL );
165 var_AddCallback( p_input, "rate-slower", RateCallback, NULL );
166 var_AddCallback( p_input, "rate-faster", RateCallback, NULL );
169 var_Create( p_input, "state", VLC_VAR_INTEGER );
171 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
172 var_AddCallback( p_input, "state", StateCallback, NULL );
175 var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
176 val.b_bool = VLC_FALSE;
177 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
179 /* Initialize thread properties */
181 p_input->p_sys = NULL;
184 p_input->psz_source = strdup( p_item->psz_uri );
190 p_input->p_es_out = NULL;
193 p_input->p_demux = NULL;
194 p_input->pf_demux = NULL;
195 p_input->pf_rewind = NULL;
196 p_input->pf_demux_control = NULL;
197 p_input->i_cr_average = config_GetInt( p_input, "cr-average" );
200 p_input->p_access = NULL;
202 p_input->i_bufsize = 0;
204 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
206 /* Initialize statistics */
207 p_input->c_loops = 0;
208 p_input->stream.c_packets_read = 0;
209 p_input->stream.c_packets_trashed = 0;
212 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
213 vlc_cond_init( p_input, &p_input->stream.stream_wait );
214 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
216 /* Initialize stream description */
217 p_input->stream.b_changed = 0;
218 p_input->stream.i_es_number = 0;
219 p_input->stream.i_selected_es_number = 0;
220 p_input->stream.i_pgrm_number = 0;
221 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
222 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
223 p_input->stream.i_mux_rate = 0;
224 p_input->stream.b_seekable = 0;
225 p_input->stream.p_sout = NULL;
227 /* no stream, no program, no area, no es */
228 p_input->stream.p_new_program = NULL;
230 p_input->stream.i_area_nb = 0;
231 p_input->stream.pp_areas = NULL;
232 p_input->stream.p_selected_area = NULL;
233 p_input->stream.p_new_area = NULL;
235 p_input->stream.pp_selected_es = NULL;
236 p_input->stream.p_removed_es = NULL;
237 p_input->stream.p_newly_selected_es = NULL;
239 /* By default there is one area in a stream */
240 input_AddArea( p_input, 0, 1 );
241 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
243 /* Initialize stream control properties. */
244 p_input->stream.control.i_status = INIT_S;
245 p_input->stream.control.i_rate = DEFAULT_RATE;
246 p_input->stream.control.b_mute = 0;
247 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale");
249 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
251 /* Initialize input info */
252 p_input->stream.p_info = NULL;
253 p_input->stream.p_info = input_InfoCategory( p_input, _("General") );
254 input_AddInfo( p_input->stream.p_info, _("Playlist Item"),
255 p_input->psz_source );
256 vlc_object_attach( p_input, p_parent );
258 /* Create thread and wait for its readiness. */
259 if( vlc_thread_create( p_input, "input", RunThread,
260 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
262 msg_Err( p_input, "cannot create input thread" );
263 input_DelInfo( p_input );
271 /*****************************************************************************
272 * input_StopThread: mark an input thread as zombie
273 *****************************************************************************
274 * This function should not return until the thread is effectively cancelled.
275 *****************************************************************************/
276 void input_StopThread( input_thread_t *p_input )
278 /* Make the thread exit from a possible vlc_cond_wait() */
279 vlc_mutex_lock( &p_input->stream.stream_lock );
280 /* Request thread destruction */
283 vlc_cond_signal( &p_input->stream.stream_wait );
284 vlc_mutex_unlock( &p_input->stream.stream_lock );
287 /*****************************************************************************
288 * input_DestroyThread: mark an input thread as zombie
289 *****************************************************************************
290 * This function should not return until the thread is effectively cancelled.
291 *****************************************************************************/
292 void input_DestroyThread( input_thread_t *p_input )
294 /* Join the thread */
295 vlc_thread_join( p_input );
297 /* Destroy Mutex locks */
298 vlc_mutex_destroy( &p_input->stream.control.control_lock );
299 vlc_cond_destroy( &p_input->stream.stream_wait );
300 vlc_mutex_destroy( &p_input->stream.stream_lock );
303 /*****************************************************************************
304 * RunThread: main thread loop
305 *****************************************************************************
306 * Thread in charge of processing the network packets and demultiplexing.
307 *****************************************************************************/
308 static int RunThread( input_thread_t *p_input )
311 mtime_t i_update_next = -1;
313 /* Signal right now, otherwise we'll get stuck in a peek */
314 vlc_thread_ready( p_input );
316 if( InitThread( p_input ) )
318 /* If we failed, wait before we are killed, and exit */
319 p_input->b_error = 1;
321 ErrorThread( p_input );
323 /* Tell we're dead */
326 input_DelInfo( p_input );
331 /* initialization is complete */
332 vlc_mutex_lock( &p_input->stream.stream_lock );
333 p_input->stream.b_changed = 1;
334 p_input->stream.control.i_status = PLAYING_S;
335 vlc_mutex_unlock( &p_input->stream.stream_lock );
337 val.i_int = PLAYING_S;
338 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
340 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
342 unsigned int i, i_count;
346 vlc_mutex_lock( &p_input->stream.stream_lock );
348 if( p_input->stream.p_new_program )
350 if( p_input->pf_set_program != NULL )
353 /* Reinitialize buffer manager. */
354 input_AccessReinit( p_input );
356 p_input->pf_set_program( p_input,
357 p_input->stream.p_new_program );
359 /* Escape all decoders for the stream discontinuity they
361 input_EscapeDiscontinuity( p_input );
363 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
365 pgrm_descriptor_t * p_pgrm
366 = p_input->stream.pp_programs[i];
368 /* Reinitialize synchro. */
369 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
372 p_input->stream.p_new_program = NULL;
375 if( p_input->stream.p_new_area )
377 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
379 input_AccessReinit( p_input );
381 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
383 /* Escape all decoders for the stream discontinuity they
385 input_EscapeDiscontinuity( p_input );
387 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
389 pgrm_descriptor_t * p_pgrm
390 = p_input->stream.pp_programs[i];
392 /* Reinitialize synchro. */
393 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
396 p_input->stream.p_new_area = NULL;
399 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
401 if( p_input->stream.p_selected_area->i_size > 0 )
405 double f = (double)p_input->stream.p_selected_area->i_seek /
406 (double)p_input->stream.p_selected_area->i_size;
408 vlc_mutex_unlock( &p_input->stream.stream_lock );
409 demux_Control( p_input, DEMUX_SET_POSITION, f );
410 vlc_mutex_lock( &p_input->stream.stream_lock );
412 /* Escape all decoders for the stream discontinuity they
414 input_EscapeDiscontinuity( p_input );
416 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
418 pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
420 /* Reinitialize synchro. */
421 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
424 vlc_mutex_unlock( &p_input->stream.stream_lock );
425 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
430 /* Help in bar display */
432 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
435 for( i = 0; i < p_input->p_sys->i_sub; i++ )
437 subtitle_Seek( p_input->p_sys->sub[i], i_time );
440 vlc_mutex_lock( &p_input->stream.stream_lock );
442 p_input->stream.p_selected_area->i_seek = NO_SEEK;
445 if( p_input->stream.p_removed_es )
447 input_UnselectES( p_input, p_input->stream.p_removed_es );
448 p_input->stream.p_removed_es = NULL;
451 if( p_input->stream.p_newly_selected_es )
453 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
454 p_input->stream.p_newly_selected_es = NULL;
457 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
459 if( p_input->stream.b_new_mute )
461 input_EscapeAudioDiscontinuity( p_input );
464 vlc_mutex_lock( &p_input->stream.control.control_lock );
465 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
466 vlc_mutex_unlock( &p_input->stream.control.control_lock );
468 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
471 vlc_mutex_unlock( &p_input->stream.stream_lock );
473 /* Read and demultiplex some data. */
474 i_count = p_input->pf_demux( p_input );
478 /* End of file - we do not set b_die because only the
479 * playlist is allowed to do so. */
480 msg_Info( p_input, "EOF reached" );
483 else if( i_count < 0 )
485 p_input->b_error = 1;
488 if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
495 /* update input status variables */
496 if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
498 val.f_float = (float)d_pos;
499 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
501 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
504 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
506 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
508 val.i_time = i_length;
509 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
513 for( i = 0; i < p_input->p_sys->i_sub; i++ )
515 subtitle_Demux( p_input->p_sys->sub[i], i_time );
518 i_update_next = mdate() + I64C(150000);
522 if( p_input->b_error || p_input->b_eof )
524 ErrorThread( p_input );
527 EndThread( p_input );
532 /*****************************************************************************
533 * InitThread: init the input Thread
534 *****************************************************************************/
535 static int InitThread( input_thread_t * p_input )
538 playlist_t *p_playlist;
541 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
542 char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
544 int64_t i_microsecondperframe;
546 subtitle_demux_t *p_sub_toselect = NULL;
548 /* Skip the plug-in names */
549 while( *psz_parser && *psz_parser != ':' )
553 #if defined( WIN32 ) || defined( UNDER_CE )
554 if( psz_parser - p_input->psz_dupsource == 1 )
556 msg_Warn( p_input, "drive letter %c: found in source string",
557 p_input->psz_dupsource[0] ) ;
564 p_input->psz_access = p_input->psz_demux = "";
565 p_input->psz_name = p_input->psz_source;
566 free( p_input->psz_dupsource );
567 p_input->psz_dupsource = NULL;
571 *psz_parser++ = '\0';
573 /* let's skip '//' */
574 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
579 p_input->psz_name = psz_parser ;
581 /* Come back to parse the access and demux plug-ins */
582 psz_parser = p_input->psz_dupsource;
587 p_input->psz_access = "";
589 else if( *psz_parser == '/' )
592 p_input->psz_access = "";
597 p_input->psz_access = psz_parser;
599 while( *psz_parser && *psz_parser != '/' )
604 if( *psz_parser == '/' )
606 *psz_parser++ = '\0';
613 p_input->psz_demux = "";
617 p_input->psz_demux = psz_parser;
621 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
622 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
624 if( input_AccessInit( p_input ) == -1 )
626 free( p_input->psz_source );
627 if( p_input->psz_dupsource != NULL )
629 free( p_input->psz_dupsource );
635 /* Initialize optional stream output. (before demuxer)*/
636 var_Get( p_input, "sout", &val );
637 if( val.psz_string != NULL )
639 if ( *val.psz_string && (p_input->stream.p_sout =
640 sout_NewInstance( p_input, val.psz_string )) == NULL )
642 msg_Err( p_input, "cannot start stream output instance, aborting" );
643 free( val.psz_string );
645 input_AccessEnd( p_input );
646 free( p_input->psz_source );
647 if( p_input->psz_dupsource != NULL )
649 free( p_input->psz_dupsource );
653 free( val.psz_string );
656 p_input->p_es_out = input_EsOutNew( p_input );
657 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
658 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
660 /* Find and open appropriate access module */
661 p_input->p_access = module_Need( p_input, "access",
662 p_input->psz_access );
664 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
665 * are forbidden in filenames on Win32. */
667 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
668 if ( p_input->p_access == NULL
669 && (*p_input->psz_demux || *p_input->psz_access) )
671 p_input->psz_access = p_input->psz_demux = "";
672 p_input->psz_name = p_input->psz_source;
673 free( p_input->psz_dupsource);
674 p_input->psz_dupsource = NULL;
676 p_input->p_access = module_Need( p_input, "access",
677 p_input->psz_access );
681 if( p_input->p_access == NULL )
683 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
684 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
685 if ( p_input->stream.p_sout != NULL )
687 sout_DeleteInstance( p_input->stream.p_sout );
690 input_AccessEnd( p_input );
691 free( p_input->psz_source );
692 if( p_input->psz_dupsource != NULL )
694 free( p_input->psz_dupsource );
699 /* Waiting for stream. */
702 p_input->i_bufsize = p_input->i_mtu;
706 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
709 /* If the desynchronisation requested by the user is < 0, we need to
710 * cache more data. */
711 var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
712 var_Get( p_input, "audio-desync", &val );
714 p_input->i_pts_delay -= (val.i_int * 1000);
716 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
718 while( !input_FillBuffer( p_input ) )
720 if( p_input->b_die || p_input->b_error || p_input->b_eof )
722 module_Unneed( p_input, p_input->p_access );
723 if ( p_input->stream.p_sout != NULL )
725 sout_DeleteInstance( p_input->stream.p_sout );
727 input_AccessEnd( p_input );
728 free( p_input->psz_source );
729 if( p_input->psz_dupsource != NULL )
731 free( p_input->psz_dupsource );
738 /* Create the stream_t facilities */
739 p_input->s = stream_OpenInput( p_input );
740 if( p_input->s == NULL )
742 /* should never occur yet */
744 msg_Err( p_input, "cannot create stream_t" );
746 module_Unneed( p_input, p_input->p_access );
747 if ( p_input->stream.p_sout != NULL )
749 sout_DeleteInstance( p_input->stream.p_sout );
751 input_AccessEnd( p_input );
752 free( p_input->psz_source );
753 if( p_input->psz_dupsource != NULL )
755 free( p_input->psz_dupsource );
760 /* Find and open appropriate demux module */
762 module_Need( p_input, "demux",
763 (p_input->psz_demux && *p_input->psz_demux) ?
764 p_input->psz_demux : "$demux" );
766 if( p_input->p_demux == NULL )
768 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
769 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
771 stream_Release( p_input->s );
772 module_Unneed( p_input, p_input->p_access );
773 if ( p_input->stream.p_sout != NULL )
775 sout_DeleteInstance( p_input->stream.p_sout );
777 input_AccessEnd( p_input );
778 free( p_input->psz_source );
779 if( p_input->psz_dupsource != NULL )
781 free( p_input->psz_dupsource );
786 /* Init input_thread_sys_t */
787 p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
788 p_input->p_sys->i_sub = 0;
789 p_input->p_sys->sub = NULL;
792 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
794 p_playlist = (playlist_t*)vlc_object_find( p_input,
799 playlist_SetDuration( p_playlist, -1 , i_length );
800 val.b_bool = VLC_TRUE;
801 var_Set( p_playlist, "item-change", val );
802 vlc_object_release( p_playlist );
808 if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
810 i_microsecondperframe = 0;
814 i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
817 /* Look for and add subtitle files */
818 var_Get( p_input, "sub-file", &val );
819 if( val.psz_string && *val.psz_string )
821 subtitle_demux_t *p_sub;
822 if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string),
823 i_microsecondperframe, 0 ) ) )
825 p_sub_toselect = p_sub;
826 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
829 if( val.psz_string ) free( val.psz_string );
831 var_Get( p_input, "sub-autodetect-file", &val );
834 subtitle_demux_t *p_sub;
836 char **tmp = subtitles_Detect( p_input, "", p_input->psz_source );
838 for( i = 0; *tmp2 != NULL; i++ )
840 if( ( p_sub = subtitle_New( p_input, *tmp2,
841 i_microsecondperframe, i ) ) )
843 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
850 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
851 val.b_bool = VLC_FALSE;
852 if( p_input->stream.p_sout )
854 var_Get( p_input, "sout-all", &val );
856 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
857 val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
860 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
861 p_sub_toselect->p_es, VLC_TRUE );
867 /*****************************************************************************
868 * ErrorThread: RunThread() error loop
869 *****************************************************************************
870 * This function is called when an error occured during thread main's loop.
871 *****************************************************************************/
872 static void ErrorThread( input_thread_t *p_input )
874 while( !p_input->b_die )
877 msleep( INPUT_IDLE_SLEEP );
881 /*****************************************************************************
882 * EndThread: end the input thread
883 *****************************************************************************/
884 static void EndThread( input_thread_t * p_input )
887 #ifdef HAVE_SYS_TIMES_H
888 /* Display statistics */
889 struct tms cpu_usage;
892 msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
893 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
895 msg_Dbg( p_input, "%ld loops", p_input->c_loops );
898 input_DumpStream( p_input );
900 /* Free demultiplexer's data */
901 if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
903 /* Free all ES and destroy all decoder threads */
904 input_EndStream( p_input );
906 /* Close optional stream output instance */
907 if( p_input->stream.p_sout )
910 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
913 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
915 /* attach sout to the playlist */
916 msg_Warn( p_input, "keeping sout" );
917 vlc_object_detach( p_input->stream.p_sout );
918 vlc_object_attach( p_input->stream.p_sout, p_pl );
922 msg_Warn( p_input, "destroying sout" );
923 sout_DeleteInstance( p_input->stream.p_sout );
927 vlc_object_release( p_pl );
931 /* Destroy subtitles demuxers */
934 for( i = 0; i < p_input->p_sys->i_sub; i++ )
936 subtitle_Close( p_input->p_sys->sub[i] );
938 if( p_input->p_sys->i_sub > 0 )
940 free( p_input->p_sys->sub );
943 /* Free input_thread_sys_t */
944 free( p_input->p_sys );
947 /* Destroy the stream_t facilities */
948 if( p_input->s ) stream_Release( p_input->s );
951 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
953 /* Close the access plug-in */
954 if( p_input->p_access ) module_Unneed( p_input, p_input->p_access );
956 input_AccessEnd( p_input );
958 /* Free info structures XXX destroy es before 'cause vorbis */
959 msg_Dbg( p_input, "freeing info structures...");
960 input_DelInfo( p_input );
962 free( p_input->psz_source );
963 if( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
965 /* Tell we're dead */
969 /*****************************************************************************
970 * ParseOption: parses the options for the input
971 *****************************************************************************
972 * This function parses the input (config) options and creates their associated
974 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
975 * bar is the value of the option.
976 *****************************************************************************/
977 static void ParseOption( input_thread_t *p_input, const char *psz_option )
979 char *psz_name = (char *)psz_option;
980 char *psz_value = strchr( psz_option, '=' );
981 int i_name_len, i_type;
982 vlc_bool_t b_isno = VLC_FALSE;
985 if( psz_value ) i_name_len = psz_value - psz_option;
986 else i_name_len = strlen( psz_option );
988 /* It's too much of an hassle to remove the ':' when we parse
990 if( i_name_len && *psz_name == ':' )
996 if( i_name_len == 0 ) return;
998 psz_name = strndup( psz_name, i_name_len );
999 if( psz_value ) psz_value++;
1001 i_type = config_GetType( p_input, psz_name );
1003 if( !i_type && !psz_value )
1005 /* check for "no-foo" or "nofoo" */
1006 if( !strncmp( psz_name, "no-", 3 ) )
1008 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1010 else if( !strncmp( psz_name, "no", 2 ) )
1012 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1014 else goto cleanup; /* Option doesn't exist */
1017 i_type = config_GetType( p_input, psz_name );
1019 if( !i_type ) goto cleanup; /* Option doesn't exist */
1021 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1023 if( ( i_type != VLC_VAR_BOOL ) &&
1024 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1026 /* Create the variable in the input object.
1027 * Children of the input object will be able to retreive this value
1028 * thanks to the inheritance property of the object variables. */
1029 var_Create( p_input, psz_name, i_type );
1034 val.b_bool = !b_isno;
1037 case VLC_VAR_INTEGER:
1038 val.i_int = atoi( psz_value );
1042 val.f_float = atof( psz_value );
1045 case VLC_VAR_STRING:
1046 case VLC_VAR_MODULE:
1048 case VLC_VAR_DIRECTORY:
1049 val.psz_string = psz_value;
1057 var_Set( p_input, psz_name, val );
1059 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
1062 if( psz_name ) free( psz_name );
1066 /*****************************************************************************
1067 * Callbacks (position, time, state, rate )
1068 *****************************************************************************/
1069 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1070 vlc_value_t oldval, vlc_value_t newval,
1073 input_thread_t *p_input = (input_thread_t *)p_this;
1075 msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
1076 oldval.f_float, newval.f_float );
1078 if( !strcmp( psz_cmd, "position-offset" ) )
1081 var_Get( p_input, "position", &val );
1083 newval.f_float += val.f_float;
1086 vlc_mutex_lock( &p_input->stream.stream_lock );
1087 p_input->stream.p_selected_area->i_seek =
1088 (int64_t)( newval.f_float *
1089 (double)p_input->stream.p_selected_area->i_size );
1091 if( p_input->stream.p_selected_area->i_seek < 0 )
1093 p_input->stream.p_selected_area->i_seek = 0;
1095 vlc_mutex_unlock( &p_input->stream.stream_lock );
1100 static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
1101 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1103 input_thread_t *p_input = (input_thread_t *)p_this;
1106 /* FIXME TODO FIXME */
1107 msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
1108 oldval.i_time, newval.i_time );
1110 var_Get( p_input, "length", &val );
1111 if( val.i_time > 0 )
1113 val.f_float = (double)newval.i_time / (double)val.i_time;
1114 if( !strcmp( psz_cmd, "time-offset" ) )
1116 var_Set( p_input, "position-offset", val );
1120 var_Set( p_input, "position", val );
1125 msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
1130 static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
1131 vlc_value_t oldval, vlc_value_t newval,
1134 input_thread_t *p_input = (input_thread_t *)p_this;
1136 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1137 psz_cmd, oldval.i_int, newval.i_int );
1139 switch( newval.i_int )
1142 input_SetStatus( p_input, INPUT_STATUS_PLAY );
1145 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1148 input_SetStatus( p_input, INPUT_STATUS_END );
1151 msg_Err( p_input, "cannot set new state (invalid)" );
1152 return VLC_EGENERIC;
1156 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
1157 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1159 input_thread_t *p_input = (input_thread_t *)p_this;
1161 if( !strcmp( psz_cmd, "rate-slower" ) )
1163 input_SetStatus( p_input, INPUT_STATUS_SLOWER );
1165 else if( !strcmp( psz_cmd, "rate-faster" ) )
1167 input_SetStatus( p_input, INPUT_STATUS_FASTER );
1171 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1172 psz_cmd, oldval.i_int, newval.i_int );
1173 input_SetRate( p_input, newval.i_int );