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.288 2004/02/08 17:21:50 fenrir 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"
47 #include "../../modules/demux/util/sub.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 struct input_thread_sys_t
56 subtitle_demux_t **sub;
59 static int RunThread ( input_thread_t *p_input );
60 static int InitThread ( input_thread_t *p_input );
61 static void ErrorThread ( input_thread_t *p_input );
62 static void EndThread ( input_thread_t *p_input );
64 static void ParseOption ( input_thread_t *p_input,
65 const char *psz_option );
67 /*****************************************************************************
69 *****************************************************************************/
70 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
71 vlc_value_t oldval, vlc_value_t newval, void *p_data );
72 static int TimeCallback ( vlc_object_t *p_this, char const *psz_cmd,
73 vlc_value_t oldval, vlc_value_t newval, void *p_data );
74 static int StateCallback ( vlc_object_t *p_this, char const *psz_cmd,
75 vlc_value_t oldval, vlc_value_t newval, void *p_data );
76 static int RateCallback ( vlc_object_t *p_this, char const *psz_cmd,
77 vlc_value_t oldval, vlc_value_t newval, void *p_data );
79 /*****************************************************************************
80 * input_CreateThread: creates a new input thread
81 *****************************************************************************
82 * This function creates a new input, and returns a pointer
83 * to its description. On error, it returns NULL.
84 *****************************************************************************/
85 input_thread_t *__input_CreateThread( vlc_object_t *p_parent, char *psz_uri,
86 char **ppsz_options, int i_options )
89 input_thread_t * p_input; /* thread descriptor */
93 /* Allocate descriptor */
94 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
97 msg_Err( p_parent, "out of memory" );
101 /* Parse input options */
102 for( i = 0; i < i_options; i++ )
104 msg_Dbg( p_input, "option: %s", ppsz_options[i] );
105 ParseOption( p_input, ppsz_options[i] );
108 /* Create a few object variables we'll need later on */
109 var_Create( p_input, "video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
110 var_Create( p_input, "audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
111 var_Create( p_input, "audio-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
112 var_Create( p_input, "spu-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
113 var_Create( p_input, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
114 var_Create( p_input, "sub-autodetect-file", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
115 var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
117 var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
118 var_Create( p_input, "sout-all", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
119 var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
120 var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
121 var_Create( p_input, "sout-keep", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
124 var_Create( p_input, "minimize-threads", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
128 /* position variable */
129 var_Create( p_input, "position", VLC_VAR_FLOAT ); /* position 0.0->1.0 */
130 var_Create( p_input, "position-offset", VLC_VAR_FLOAT ); /* relative */
132 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
133 var_AddCallback( p_input, "position", PositionCallback, NULL );
134 var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
137 var_Create( p_input, "time", VLC_VAR_TIME );
138 var_Create( p_input, "time-offset", VLC_VAR_TIME ); /* relative */
140 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
141 var_AddCallback( p_input, "time", TimeCallback, NULL );
142 var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
144 /* length variable */
145 var_Create( p_input, "length", VLC_VAR_TIME );
147 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
150 var_Create( p_input, "rate", VLC_VAR_INTEGER );
151 var_Create( p_input, "rate-slower", VLC_VAR_VOID );
152 var_Create( p_input, "rate-faster", VLC_VAR_VOID );
153 val.i_int = DEFAULT_RATE;
154 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
155 var_AddCallback( p_input, "rate", RateCallback, NULL );
156 var_AddCallback( p_input, "rate-slower", RateCallback, NULL );
157 var_AddCallback( p_input, "rate-faster", RateCallback, NULL );
160 var_Create( p_input, "state", VLC_VAR_INTEGER );
162 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
163 var_AddCallback( p_input, "state", StateCallback, NULL );
166 var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
167 val.b_bool = VLC_FALSE;
168 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
170 /* Initialize thread properties */
172 p_input->p_sys = NULL;
175 p_input->psz_source = strdup( psz_uri );
181 p_input->p_es_out = NULL;
184 p_input->p_demux = NULL;
185 p_input->pf_demux = NULL;
186 p_input->pf_rewind = NULL;
187 p_input->pf_demux_control = NULL;
188 p_input->i_cr_average = config_GetInt( p_input, "cr-average" );
191 p_input->p_access = NULL;
193 p_input->i_bufsize = 0;
195 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
197 /* Initialize statistics */
198 p_input->c_loops = 0;
199 p_input->stream.c_packets_read = 0;
200 p_input->stream.c_packets_trashed = 0;
203 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
204 vlc_cond_init( p_input, &p_input->stream.stream_wait );
205 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
207 /* Initialize stream description */
208 p_input->stream.b_changed = 0;
209 p_input->stream.i_es_number = 0;
210 p_input->stream.i_selected_es_number = 0;
211 p_input->stream.i_pgrm_number = 0;
212 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
213 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
214 p_input->stream.i_mux_rate = 0;
215 p_input->stream.b_seekable = 0;
216 p_input->stream.p_sout = NULL;
218 /* no stream, no program, no area, no es */
219 p_input->stream.p_new_program = NULL;
221 p_input->stream.i_area_nb = 0;
222 p_input->stream.pp_areas = NULL;
223 p_input->stream.p_selected_area = NULL;
224 p_input->stream.p_new_area = NULL;
226 p_input->stream.pp_selected_es = NULL;
227 p_input->stream.p_removed_es = NULL;
228 p_input->stream.p_newly_selected_es = NULL;
230 /* By default there is one area in a stream */
231 input_AddArea( p_input, 0, 1 );
232 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
234 /* Initialize stream control properties. */
235 p_input->stream.control.i_status = INIT_S;
236 p_input->stream.control.i_rate = DEFAULT_RATE;
237 p_input->stream.control.b_mute = 0;
238 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale");
240 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
242 /* Initialize input info */
243 p_input->stream.p_info = NULL;
244 p_input->stream.p_info = input_InfoCategory( p_input, _("General") );
245 input_AddInfo( p_input->stream.p_info, _("Playlist Item"),
246 p_input->psz_source );
247 vlc_object_attach( p_input, p_parent );
249 /* Create thread and wait for its readiness. */
250 if( vlc_thread_create( p_input, "input", RunThread,
251 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
253 msg_Err( p_input, "cannot create input thread" );
254 input_DelInfo( p_input );
262 /*****************************************************************************
263 * input_StopThread: mark an input thread as zombie
264 *****************************************************************************
265 * This function should not return until the thread is effectively cancelled.
266 *****************************************************************************/
267 void input_StopThread( input_thread_t *p_input )
269 /* Make the thread exit from a possible vlc_cond_wait() */
270 vlc_mutex_lock( &p_input->stream.stream_lock );
271 /* Request thread destruction */
274 vlc_cond_signal( &p_input->stream.stream_wait );
275 vlc_mutex_unlock( &p_input->stream.stream_lock );
278 /*****************************************************************************
279 * input_DestroyThread: mark an input thread as zombie
280 *****************************************************************************
281 * This function should not return until the thread is effectively cancelled.
282 *****************************************************************************/
283 void input_DestroyThread( input_thread_t *p_input )
285 /* Join the thread */
286 vlc_thread_join( p_input );
288 /* Destroy Mutex locks */
289 vlc_mutex_destroy( &p_input->stream.control.control_lock );
290 vlc_cond_destroy( &p_input->stream.stream_wait );
291 vlc_mutex_destroy( &p_input->stream.stream_lock );
294 /*****************************************************************************
295 * RunThread: main thread loop
296 *****************************************************************************
297 * Thread in charge of processing the network packets and demultiplexing.
298 *****************************************************************************/
299 static int RunThread( input_thread_t *p_input )
302 mtime_t i_update_next = -1;
304 /* Signal right now, otherwise we'll get stuck in a peek */
305 vlc_thread_ready( p_input );
307 if( InitThread( p_input ) )
309 /* If we failed, wait before we are killed, and exit */
310 p_input->b_error = 1;
312 ErrorThread( p_input );
314 /* Tell we're dead */
317 input_DelInfo( p_input );
322 /* initialization is complete */
323 vlc_mutex_lock( &p_input->stream.stream_lock );
324 p_input->stream.b_changed = 1;
325 p_input->stream.control.i_status = PLAYING_S;
326 vlc_mutex_unlock( &p_input->stream.stream_lock );
328 val.i_int = PLAYING_S;
329 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
331 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
333 unsigned int i, i_count;
337 vlc_mutex_lock( &p_input->stream.stream_lock );
339 if( p_input->stream.p_new_program )
341 if( p_input->pf_set_program != NULL )
344 /* Reinitialize buffer manager. */
345 input_AccessReinit( p_input );
347 p_input->pf_set_program( p_input,
348 p_input->stream.p_new_program );
350 /* Escape all decoders for the stream discontinuity they
352 input_EscapeDiscontinuity( p_input );
354 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
356 pgrm_descriptor_t * p_pgrm
357 = p_input->stream.pp_programs[i];
359 /* Reinitialize synchro. */
360 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
363 p_input->stream.p_new_program = NULL;
366 if( p_input->stream.p_new_area )
368 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
370 input_AccessReinit( p_input );
372 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
374 /* Escape all decoders for the stream discontinuity they
376 input_EscapeDiscontinuity( p_input );
378 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
380 pgrm_descriptor_t * p_pgrm
381 = p_input->stream.pp_programs[i];
383 /* Reinitialize synchro. */
384 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
387 p_input->stream.p_new_area = NULL;
390 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
392 if( p_input->stream.p_selected_area->i_size > 0 )
396 double f = (double)p_input->stream.p_selected_area->i_seek /
397 (double)p_input->stream.p_selected_area->i_size;
399 vlc_mutex_unlock( &p_input->stream.stream_lock );
400 demux_Control( p_input, DEMUX_SET_POSITION, f );
401 vlc_mutex_lock( &p_input->stream.stream_lock );
403 /* Escape all decoders for the stream discontinuity they
405 input_EscapeDiscontinuity( p_input );
407 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
409 pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
411 /* Reinitialize synchro. */
412 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
415 vlc_mutex_unlock( &p_input->stream.stream_lock );
416 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
421 /* Help in bar display */
423 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
426 for( i = 0; i < p_input->p_sys->i_sub; i++ )
428 subtitle_Seek( p_input->p_sys->sub[i], i_time );
431 vlc_mutex_lock( &p_input->stream.stream_lock );
433 p_input->stream.p_selected_area->i_seek = NO_SEEK;
436 if( p_input->stream.p_removed_es )
438 input_UnselectES( p_input, p_input->stream.p_removed_es );
439 p_input->stream.p_removed_es = NULL;
442 if( p_input->stream.p_newly_selected_es )
444 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
445 p_input->stream.p_newly_selected_es = NULL;
448 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
450 if( p_input->stream.b_new_mute )
452 input_EscapeAudioDiscontinuity( p_input );
455 vlc_mutex_lock( &p_input->stream.control.control_lock );
456 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
457 vlc_mutex_unlock( &p_input->stream.control.control_lock );
459 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
462 vlc_mutex_unlock( &p_input->stream.stream_lock );
464 /* Read and demultiplex some data. */
465 i_count = p_input->pf_demux( p_input );
469 /* End of file - we do not set b_die because only the
470 * playlist is allowed to do so. */
471 msg_Info( p_input, "EOF reached" );
474 else if( i_count < 0 )
476 p_input->b_error = 1;
479 if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
486 /* update input status variables */
487 if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
489 val.f_float = (float)d_pos;
490 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
492 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
495 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
497 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
499 val.i_time = i_length;
500 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
504 for( i = 0; i < p_input->p_sys->i_sub; i++ )
506 subtitle_Demux( p_input->p_sys->sub[i], i_time );
509 i_update_next = mdate() + I64C(150000);
513 if( p_input->b_error || p_input->b_eof )
515 ErrorThread( p_input );
518 EndThread( p_input );
523 /*****************************************************************************
524 * InitThread: init the input Thread
525 *****************************************************************************/
526 static int InitThread( input_thread_t * p_input )
530 playlist_t *p_playlist;
533 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
534 char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
536 int64_t i_microsecondperframe;
538 subtitle_demux_t *p_sub_toselect = NULL;
539 char *psz_sub_file = NULL;
541 /* Skip the plug-in names */
542 while( *psz_parser && *psz_parser != ':' )
546 #if defined( WIN32 ) || defined( UNDER_CE )
547 if( psz_parser - p_input->psz_dupsource == 1 )
549 msg_Warn( p_input, "drive letter %c: found in source string",
550 p_input->psz_dupsource[0] ) ;
557 p_input->psz_access = p_input->psz_demux = "";
558 p_input->psz_name = p_input->psz_source;
559 free( p_input->psz_dupsource );
560 p_input->psz_dupsource = NULL;
564 *psz_parser++ = '\0';
566 /* let's skip '//' */
567 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
572 p_input->psz_name = psz_parser ;
574 /* Come back to parse the access and demux plug-ins */
575 psz_parser = p_input->psz_dupsource;
580 p_input->psz_access = "";
582 else if( *psz_parser == '/' )
585 p_input->psz_access = "";
590 p_input->psz_access = psz_parser;
592 while( *psz_parser && *psz_parser != '/' )
597 if( *psz_parser == '/' )
599 *psz_parser++ = '\0';
606 p_input->psz_demux = "";
610 p_input->psz_demux = psz_parser;
614 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
615 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
617 if( input_AccessInit( p_input ) == -1 )
619 free( p_input->psz_source );
620 if( p_input->psz_dupsource != NULL )
622 free( p_input->psz_dupsource );
628 /* Initialize optional stream output. (before demuxer)*/
629 var_Get( p_input, "sout", &val );
630 if( val.psz_string != NULL )
632 if ( *val.psz_string && (p_input->stream.p_sout =
633 sout_NewInstance( p_input, val.psz_string )) == NULL )
635 msg_Err( p_input, "cannot start stream output instance, aborting" );
636 free( val.psz_string );
638 input_AccessEnd( p_input );
639 free( p_input->psz_source );
640 if( p_input->psz_dupsource != NULL )
642 free( p_input->psz_dupsource );
646 free( val.psz_string );
649 p_input->p_es_out = input_EsOutNew( p_input );
650 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
651 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
653 /* Find and open appropriate access module */
654 p_input->p_access = module_Need( p_input, "access",
655 p_input->psz_access );
657 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
658 * are forbidden in filenames on Win32. */
660 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
661 if ( p_input->p_access == NULL
662 && (*p_input->psz_demux || *p_input->psz_access) )
664 p_input->psz_access = p_input->psz_demux = "";
665 p_input->psz_name = p_input->psz_source;
666 free( p_input->psz_dupsource);
667 p_input->psz_dupsource = NULL;
669 p_input->p_access = module_Need( p_input, "access",
670 p_input->psz_access );
674 if( p_input->p_access == NULL )
676 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
677 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
678 if ( p_input->stream.p_sout != NULL )
680 sout_DeleteInstance( p_input->stream.p_sout );
683 input_AccessEnd( p_input );
684 free( p_input->psz_source );
685 if( p_input->psz_dupsource != NULL )
687 free( p_input->psz_dupsource );
689 input_EsOutDelete( p_input->p_es_out );
693 /* Waiting for stream. */
696 p_input->i_bufsize = p_input->i_mtu;
700 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
703 /* If the desynchronisation requested by the user is < 0, we need to
704 * cache more data. */
705 var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
706 var_Get( p_input, "audio-desync", &val );
708 p_input->i_pts_delay -= (val.i_int * 1000);
710 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
712 while( !input_FillBuffer( p_input ) )
714 if( p_input->b_die || p_input->b_error || p_input->b_eof )
716 module_Unneed( p_input, p_input->p_access );
717 if ( p_input->stream.p_sout != NULL )
719 sout_DeleteInstance( p_input->stream.p_sout );
721 input_AccessEnd( p_input );
722 free( p_input->psz_source );
723 if( p_input->psz_dupsource != NULL )
725 free( p_input->psz_dupsource );
727 input_EsOutDelete( p_input->p_es_out );
733 /* Create the stream_t facilities */
734 p_input->s = input_StreamNew( p_input );
735 if( p_input->s == NULL )
737 /* should never occur yet */
739 msg_Err( p_input, "cannot create stream_t" );
741 module_Unneed( p_input, p_input->p_access );
742 if ( p_input->stream.p_sout != NULL )
744 sout_DeleteInstance( p_input->stream.p_sout );
746 input_AccessEnd( p_input );
747 free( p_input->psz_source );
748 if( p_input->psz_dupsource != NULL )
750 free( p_input->psz_dupsource );
752 input_EsOutDelete( p_input->p_es_out );
756 /* Find and open appropriate demux module */
758 module_Need( p_input, "demux",
759 (p_input->psz_demux && *p_input->psz_demux) ?
760 p_input->psz_demux : "$demux" );
762 if( p_input->p_demux == NULL )
764 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
765 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
767 input_StreamDelete( p_input->s );
768 module_Unneed( p_input, p_input->p_access );
769 if ( p_input->stream.p_sout != NULL )
771 sout_DeleteInstance( p_input->stream.p_sout );
773 input_AccessEnd( p_input );
774 free( p_input->psz_source );
775 if( p_input->psz_dupsource != NULL )
777 free( p_input->psz_dupsource );
779 input_EsOutDelete( p_input->p_es_out );
783 /* Init input_thread_sys_t */
784 p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
785 p_input->p_sys->i_sub = 0;
786 p_input->p_sys->sub = NULL;
788 /* get meta informations */
789 if( !demux_Control( p_input, DEMUX_GET_META, &meta ) )
791 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_input,
792 VLC_OBJECT_PLAYLIST, FIND_PARENT);
793 playlist_item_t *p_item = NULL;
794 input_info_category_t *p_cat;
799 vlc_mutex_lock( &p_playlist->object_lock );
800 p_item = playlist_ItemGetByPos( p_playlist, -1 );
803 vlc_mutex_lock( &p_item->lock );
805 vlc_mutex_unlock( &p_playlist->object_lock );
808 msg_Dbg( p_input, "meta informations:" );
809 if( meta->i_meta > 0 )
811 p_cat = input_InfoCategory( p_input, _("File") );
812 for( i = 0; i < meta->i_meta; i++ )
814 msg_Dbg( p_input, " - '%s' = '%s'", _(meta->name[i]), meta->value[i] );
815 input_AddInfo( p_cat, _(meta->name[i]), "%s", meta->value[i] );
818 playlist_ItemAddInfo( p_item, _("File"),
819 _(meta->name[i]), "%s", meta->value[i] );
823 for( i = 0; i < meta->i_track; i++ )
825 vlc_meta_t *tk = meta->track[i];
828 msg_Dbg( p_input, " - track[%d]:", i );
831 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
832 sprintf( psz_cat, "%s %d", _("Stream"), i );
833 p_cat = input_InfoCategory( p_input, psz_cat );
835 for( j = 0; j < tk->i_meta; j++ )
837 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]), tk->value[j] );
838 input_AddInfo( p_cat, _(tk->name[j]), "%s", tk->value[j] );
841 playlist_ItemAddInfo( p_item, psz_cat,
842 _(tk->name[j]), "%s", tk->value[j] );
850 vlc_mutex_unlock( &p_item->lock );
852 if( p_playlist ) vlc_object_release( p_playlist );
854 vlc_meta_Delete( meta );
858 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
860 p_playlist = (playlist_t*)vlc_object_find( p_input,
865 playlist_SetDuration( p_playlist, -1 , i_length );
866 val.b_bool = p_playlist->i_index;
867 var_Set( p_playlist, "item-change", val );
868 vlc_object_release( p_playlist );
874 if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
876 i_microsecondperframe = 0;
880 i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
883 /* Look for and add subtitle files */
884 var_Get( p_input, "sub-file", &val );
885 if( val.psz_string && *val.psz_string )
887 subtitle_demux_t *p_sub;
889 msg_Dbg( p_input, "force subtitle: %s", val.psz_string );
890 if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string),
891 i_microsecondperframe ) ) )
893 p_sub_toselect = p_sub;
894 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
897 psz_sub_file = val.psz_string;
899 var_Get( p_input, "sub-autodetect-file", &val );
902 subtitle_demux_t *p_sub;
904 char **tmp = subtitles_Detect( p_input, "", p_input->psz_name );
906 for( i = 0; *tmp2 != NULL; i++ )
908 if( psz_sub_file == NULL || strcmp( psz_sub_file, *tmp2 ) )
910 if( ( p_sub = subtitle_New( p_input, *tmp2,
911 i_microsecondperframe ) ) )
913 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
920 if( psz_sub_file ) free( psz_sub_file );
922 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
923 val.b_bool = VLC_FALSE;
924 if( p_input->stream.p_sout )
926 var_Get( p_input, "sout-all", &val );
928 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
929 val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
932 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
933 p_sub_toselect->p_es, VLC_TRUE );
939 /*****************************************************************************
940 * ErrorThread: RunThread() error loop
941 *****************************************************************************
942 * This function is called when an error occured during thread main's loop.
943 *****************************************************************************/
944 static void ErrorThread( input_thread_t *p_input )
946 while( !p_input->b_die )
949 msleep( INPUT_IDLE_SLEEP );
953 /*****************************************************************************
954 * EndThread: end the input thread
955 *****************************************************************************/
956 static void EndThread( input_thread_t * p_input )
959 #ifdef HAVE_SYS_TIMES_H
960 /* Display statistics */
961 struct tms cpu_usage;
964 msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
965 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
967 msg_Dbg( p_input, "%ld loops", p_input->c_loops );
970 input_DumpStream( p_input );
972 /* Free demultiplexer's data */
973 if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
975 /* Free all ES and destroy all decoder threads */
976 input_EndStream( p_input );
978 /* Close optional stream output instance */
979 if( p_input->stream.p_sout )
982 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
985 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
987 /* attach sout to the playlist */
988 msg_Warn( p_input, "keeping sout" );
989 vlc_object_detach( p_input->stream.p_sout );
990 vlc_object_attach( p_input->stream.p_sout, p_pl );
994 msg_Warn( p_input, "destroying sout" );
995 sout_DeleteInstance( p_input->stream.p_sout );
999 vlc_object_release( p_pl );
1003 /* Destroy subtitles demuxers */
1004 if( p_input->p_sys )
1006 for( i = 0; i < p_input->p_sys->i_sub; i++ )
1008 subtitle_Close( p_input->p_sys->sub[i] );
1010 if( p_input->p_sys->i_sub > 0 )
1012 free( p_input->p_sys->sub );
1015 /* Free input_thread_sys_t */
1016 free( p_input->p_sys );
1019 /* Destroy the stream_t facilities */
1020 if( p_input->s ) input_StreamDelete( p_input->s );
1022 /* Destroy es out */
1023 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
1025 /* Close the access plug-in */
1026 if( p_input->p_access ) module_Unneed( p_input, p_input->p_access );
1028 input_AccessEnd( p_input );
1030 /* Free info structures XXX destroy es before 'cause vorbis */
1031 msg_Dbg( p_input, "freeing info structures...");
1032 input_DelInfo( p_input );
1034 free( p_input->psz_source );
1035 if( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
1037 /* Tell we're dead */
1038 p_input->b_dead = 1;
1041 /*****************************************************************************
1042 * ParseOption: parses the options for the input
1043 *****************************************************************************
1044 * This function parses the input (config) options and creates their associated
1046 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
1047 * bar is the value of the option.
1048 *****************************************************************************/
1049 static void ParseOption( input_thread_t *p_input, const char *psz_option )
1051 char *psz_name = (char *)psz_option;
1052 char *psz_value = strchr( psz_option, '=' );
1053 int i_name_len, i_type;
1054 vlc_bool_t b_isno = VLC_FALSE;
1057 if( psz_value ) i_name_len = psz_value - psz_option;
1058 else i_name_len = strlen( psz_option );
1060 /* It's too much of an hassle to remove the ':' when we parse
1061 * the cmd line :) */
1062 if( i_name_len && *psz_name == ':' )
1068 if( i_name_len == 0 ) return;
1070 psz_name = strndup( psz_name, i_name_len );
1071 if( psz_value ) psz_value++;
1073 i_type = config_GetType( p_input, psz_name );
1075 if( !i_type && !psz_value )
1077 /* check for "no-foo" or "nofoo" */
1078 if( !strncmp( psz_name, "no-", 3 ) )
1080 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1082 else if( !strncmp( psz_name, "no", 2 ) )
1084 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1086 else goto cleanup; /* Option doesn't exist */
1089 i_type = config_GetType( p_input, psz_name );
1091 if( !i_type ) goto cleanup; /* Option doesn't exist */
1093 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1095 if( ( i_type != VLC_VAR_BOOL ) &&
1096 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1098 /* Create the variable in the input object.
1099 * Children of the input object will be able to retreive this value
1100 * thanks to the inheritance property of the object variables. */
1101 var_Create( p_input, psz_name, i_type );
1106 val.b_bool = !b_isno;
1109 case VLC_VAR_INTEGER:
1110 val.i_int = atoi( psz_value );
1114 val.f_float = atof( psz_value );
1117 case VLC_VAR_STRING:
1118 case VLC_VAR_MODULE:
1120 case VLC_VAR_DIRECTORY:
1121 val.psz_string = psz_value;
1129 var_Set( p_input, psz_name, val );
1131 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
1134 if( psz_name ) free( psz_name );
1138 /*****************************************************************************
1139 * Callbacks (position, time, state, rate )
1140 *****************************************************************************/
1141 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1142 vlc_value_t oldval, vlc_value_t newval,
1145 input_thread_t *p_input = (input_thread_t *)p_this;
1147 msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
1148 oldval.f_float, newval.f_float );
1150 if( !strcmp( psz_cmd, "position-offset" ) )
1153 var_Get( p_input, "position", &val );
1155 newval.f_float += val.f_float;
1158 vlc_mutex_lock( &p_input->stream.stream_lock );
1159 p_input->stream.p_selected_area->i_seek =
1160 (int64_t)( newval.f_float *
1161 (double)p_input->stream.p_selected_area->i_size );
1163 if( p_input->stream.p_selected_area->i_seek < 0 )
1165 p_input->stream.p_selected_area->i_seek = 0;
1167 vlc_mutex_unlock( &p_input->stream.stream_lock );
1172 static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
1173 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1175 input_thread_t *p_input = (input_thread_t *)p_this;
1178 /* FIXME TODO FIXME */
1179 msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
1180 oldval.i_time, newval.i_time );
1182 var_Get( p_input, "length", &val );
1183 if( val.i_time > 0 )
1185 val.f_float = (double)newval.i_time / (double)val.i_time;
1186 if( !strcmp( psz_cmd, "time-offset" ) )
1188 var_Set( p_input, "position-offset", val );
1192 var_Set( p_input, "position", val );
1197 msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
1202 static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
1203 vlc_value_t oldval, vlc_value_t newval,
1206 input_thread_t *p_input = (input_thread_t *)p_this;
1208 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1209 psz_cmd, oldval.i_int, newval.i_int );
1211 switch( newval.i_int )
1214 input_SetStatus( p_input, INPUT_STATUS_PLAY );
1217 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1220 input_SetStatus( p_input, INPUT_STATUS_END );
1223 msg_Err( p_input, "cannot set new state (invalid)" );
1224 return VLC_EGENERIC;
1228 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
1229 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1231 input_thread_t *p_input = (input_thread_t *)p_this;
1233 if( !strcmp( psz_cmd, "rate-slower" ) )
1235 input_SetStatus( p_input, INPUT_STATUS_SLOWER );
1237 else if( !strcmp( psz_cmd, "rate-faster" ) )
1239 input_SetStatus( p_input, INPUT_STATUS_FASTER );
1243 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1244 psz_cmd, oldval.i_int, newval.i_int );
1245 input_SetRate( p_input, newval.i_int );