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
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;
61 static int RunThread ( input_thread_t *p_input );
62 static int InitThread ( input_thread_t *p_input );
63 static void ErrorThread ( input_thread_t *p_input );
64 static void EndThread ( input_thread_t *p_input );
66 static void ParseOption ( input_thread_t *p_input,
67 const char *psz_option );
69 /*****************************************************************************
71 *****************************************************************************/
72 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
73 vlc_value_t oldval, vlc_value_t newval, void *p_data );
74 static int TimeCallback ( vlc_object_t *p_this, char const *psz_cmd,
75 vlc_value_t oldval, vlc_value_t newval, void *p_data );
76 static int StateCallback ( vlc_object_t *p_this, char const *psz_cmd,
77 vlc_value_t oldval, vlc_value_t newval, void *p_data );
78 static int RateCallback ( vlc_object_t *p_this, char const *psz_cmd,
79 vlc_value_t oldval, vlc_value_t newval, void *p_data );
81 /*****************************************************************************
82 * input_CreateThread: creates a new input thread
83 *****************************************************************************
84 * This function creates a new input, and returns a pointer
85 * to its description. On error, it returns NULL.
86 *****************************************************************************/
87 input_thread_t *__input_CreateThread( vlc_object_t *p_parent, char *psz_uri,
88 char **ppsz_options, int i_options )
91 input_thread_t *p_input; /* thread descriptor */
95 /* Allocate descriptor */
96 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
99 msg_Err( p_parent, "out of memory" );
103 /* Parse input options */
104 for( i = 0; i < i_options; i++ )
106 msg_Dbg( p_input, "option: %s", ppsz_options[i] );
107 ParseOption( p_input, ppsz_options[i] );
110 /* Create a few object variables we'll need later on */
111 var_Create( p_input, "video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
112 var_Create( p_input, "audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
113 var_Create( p_input, "audio-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
114 var_Create( p_input, "spu-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
115 var_Create( p_input, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
116 var_Create( p_input, "sub-autodetect-file", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
117 var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
119 var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
120 var_Create( p_input, "sout-all", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
121 var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
122 var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
123 var_Create( p_input, "sout-keep", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
125 /* repeat variable */
126 var_Create( p_input, "input-repeat", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
128 /* start/stop time */
129 var_Create( p_input, "start-time", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
130 var_Create( p_input, "stop-time", VLC_VAR_INTEGER|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->b_out_pace_control = VLC_FALSE;
182 p_input->p_sys = NULL;
185 p_input->psz_source = strdup( psz_uri );
191 p_input->p_es_out = NULL;
194 p_input->p_demux = NULL;
195 p_input->pf_demux = NULL;
196 p_input->pf_rewind = NULL;
197 p_input->pf_demux_control = NULL;
198 p_input->i_cr_average = config_GetInt( p_input, "cr-average" );
201 p_input->p_access = NULL;
203 p_input->i_bufsize = 0;
205 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
207 /* Initialize statistics */
208 p_input->c_loops = 0;
209 p_input->stream.c_packets_read = 0;
210 p_input->stream.c_packets_trashed = 0;
213 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
214 vlc_cond_init( p_input, &p_input->stream.stream_wait );
215 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
217 /* Initialize stream description */
218 p_input->stream.b_changed = 0;
219 p_input->stream.i_es_number = 0;
220 p_input->stream.i_selected_es_number = 0;
221 p_input->stream.i_pgrm_number = 0;
222 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
223 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
224 p_input->stream.i_mux_rate = 0;
225 p_input->stream.b_seekable = 0;
226 p_input->stream.p_sout = NULL;
228 /* no stream, no program, no area, no es */
229 p_input->stream.p_new_program = NULL;
231 p_input->stream.i_area_nb = 0;
232 p_input->stream.pp_areas = NULL;
233 p_input->stream.p_selected_area = NULL;
234 p_input->stream.p_new_area = NULL;
236 p_input->stream.pp_selected_es = NULL;
237 p_input->stream.p_removed_es = NULL;
238 p_input->stream.p_newly_selected_es = NULL;
240 /* By default there is one area in a stream */
241 input_AddArea( p_input, 0, 1 );
242 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
244 /* Initialize stream control properties. */
245 p_input->stream.control.i_status = INIT_S;
246 p_input->stream.control.i_rate = DEFAULT_RATE;
247 p_input->stream.control.b_mute = 0;
248 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale");
250 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
252 /* Initialize input info */
253 p_input->stream.p_info = NULL;
254 p_input->stream.p_info = input_InfoCategory( p_input, _("General") );
255 input_AddInfo( p_input->stream.p_info, _("Playlist Item"),
256 p_input->psz_source );
257 vlc_object_attach( p_input, p_parent );
259 /* Create thread and wait for its readiness. */
260 if( vlc_thread_create( p_input, "input", RunThread,
261 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
263 msg_Err( p_input, "cannot create input thread" );
264 input_DelInfo( p_input );
272 /*****************************************************************************
273 * input_StopThread: mark an input thread as zombie
274 *****************************************************************************
275 * This function should not return until the thread is effectively cancelled.
276 *****************************************************************************/
277 void input_StopThread( input_thread_t *p_input )
279 /* Make the thread exit from a possible vlc_cond_wait() */
280 vlc_mutex_lock( &p_input->stream.stream_lock );
281 /* Request thread destruction */
284 vlc_cond_signal( &p_input->stream.stream_wait );
285 vlc_mutex_unlock( &p_input->stream.stream_lock );
288 /*****************************************************************************
289 * input_DestroyThread: mark an input thread as zombie
290 *****************************************************************************
291 * This function should not return until the thread is effectively cancelled.
292 *****************************************************************************/
293 void input_DestroyThread( input_thread_t *p_input )
295 /* Join the thread */
296 vlc_thread_join( p_input );
298 /* Destroy Mutex locks */
299 vlc_mutex_destroy( &p_input->stream.control.control_lock );
300 vlc_cond_destroy( &p_input->stream.stream_wait );
301 vlc_mutex_destroy( &p_input->stream.stream_lock );
304 /*****************************************************************************
305 * RunThread: main thread loop
306 *****************************************************************************
307 * Thread in charge of processing the network packets and demultiplexing.
308 *****************************************************************************/
309 static int RunThread( input_thread_t *p_input )
312 mtime_t i_update_next = -1;
314 /* Signal right now, otherwise we'll get stuck in a peek */
315 vlc_thread_ready( p_input );
317 if( InitThread( p_input ) )
319 /* If we failed, wait before we are killed, and exit */
320 p_input->b_error = 1;
322 ErrorThread( p_input );
324 /* Tell we're dead */
327 input_DelInfo( p_input );
332 /* initialization is complete */
333 vlc_mutex_lock( &p_input->stream.stream_lock );
334 p_input->stream.b_changed = 1;
335 p_input->stream.control.i_status = PLAYING_S;
336 vlc_mutex_unlock( &p_input->stream.stream_lock );
338 val.i_int = PLAYING_S;
339 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
341 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
343 unsigned int i, i_count;
347 vlc_mutex_lock( &p_input->stream.stream_lock );
349 if( p_input->stream.p_new_program )
351 if( p_input->pf_set_program != NULL )
354 /* Reinitialize buffer manager. */
355 input_AccessReinit( p_input );
357 p_input->pf_set_program( p_input,
358 p_input->stream.p_new_program );
360 /* Escape all decoders for the stream discontinuity they
362 input_EscapeDiscontinuity( p_input );
364 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
366 pgrm_descriptor_t * p_pgrm
367 = p_input->stream.pp_programs[i];
369 /* Reinitialize synchro. */
370 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
373 p_input->stream.p_new_program = NULL;
376 if( p_input->stream.p_new_area )
378 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
380 input_AccessReinit( p_input );
382 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
384 /* Escape all decoders for the stream discontinuity they
386 input_EscapeDiscontinuity( p_input );
388 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
390 pgrm_descriptor_t * p_pgrm
391 = p_input->stream.pp_programs[i];
393 /* Reinitialize synchro. */
394 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
397 p_input->stream.p_new_area = NULL;
400 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
402 if( p_input->stream.p_selected_area->i_size > 0 )
406 double f = (double)p_input->stream.p_selected_area->i_seek /
407 (double)p_input->stream.p_selected_area->i_size;
409 vlc_mutex_unlock( &p_input->stream.stream_lock );
410 demux_Control( p_input, DEMUX_SET_POSITION, f );
411 vlc_mutex_lock( &p_input->stream.stream_lock );
413 /* Escape all decoders for the stream discontinuity they
415 input_EscapeDiscontinuity( p_input );
417 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
419 pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
421 /* Reinitialize synchro. */
422 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
425 vlc_mutex_unlock( &p_input->stream.stream_lock );
426 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
431 /* Help in bar display */
433 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
436 for( i = 0; i < p_input->p_sys->i_sub; i++ )
438 subtitle_Seek( p_input->p_sys->sub[i], i_time );
441 vlc_mutex_lock( &p_input->stream.stream_lock );
443 p_input->stream.p_selected_area->i_seek = NO_SEEK;
446 if( p_input->stream.p_removed_es )
448 input_UnselectES( p_input, p_input->stream.p_removed_es );
449 p_input->stream.p_removed_es = NULL;
452 if( p_input->stream.p_newly_selected_es )
454 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
455 p_input->stream.p_newly_selected_es = NULL;
458 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
460 if( p_input->stream.b_new_mute )
462 input_EscapeAudioDiscontinuity( p_input );
465 vlc_mutex_lock( &p_input->stream.control.control_lock );
466 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
467 vlc_mutex_unlock( &p_input->stream.control.control_lock );
469 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
472 vlc_mutex_unlock( &p_input->stream.stream_lock );
474 /* Read and demultiplex some data. */
475 i_count = p_input->pf_demux( p_input );
481 var_Get( p_input, "input-repeat", &repeat );
482 if( repeat.i_int == 0 || p_input->stream.i_area_nb <= 0 )
484 /* End of file - we do not set b_die because only the
485 * playlist is allowed to do so. */
486 msg_Info( p_input, "EOF reached" );
491 msg_Dbg( p_input, "repeating the same input (%d)", repeat.i_int );
492 if( repeat.i_int > 0 )
495 var_Set( p_input, "input-repeat", repeat );
498 p_input->stream.p_new_area = p_input->stream.pp_areas[0];
499 p_input->stream.p_new_area->i_seek = 0;
502 else if( i_count < 0 )
504 p_input->b_error = 1;
507 if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
514 /* update input status variables */
515 if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
517 val.f_float = (float)d_pos;
518 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
520 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
523 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
525 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
527 val.i_time = i_length;
528 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
531 /* Check stop-time */
532 if( p_input->p_sys->i_stop_time > 0 && p_input->p_sys->i_stop_time < i_time )
534 msg_Warn( p_input, "EOF reached because of stop-time" );
539 for( i = 0; i < p_input->p_sys->i_sub; i++ )
541 subtitle_Demux( p_input->p_sys->sub[i], i_time );
544 i_update_next = mdate() + I64C(150000);
548 if( p_input->b_error || p_input->b_eof )
550 ErrorThread( p_input );
553 EndThread( p_input );
558 /*****************************************************************************
559 * InitThread: init the input Thread
560 *****************************************************************************/
561 static int InitThread( input_thread_t * p_input )
563 vlc_meta_t *p_meta = NULL, *p_meta_user = NULL;
565 playlist_t *p_playlist;
568 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
569 char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
571 int64_t i_microsecondperframe;
573 subtitle_demux_t *p_sub_toselect = NULL;
574 char *psz_sub_file = NULL;
576 /* Skip the plug-in names */
577 while( *psz_parser && *psz_parser != ':' )
581 #if defined( WIN32 ) || defined( UNDER_CE )
582 if( psz_parser - p_input->psz_dupsource == 1 )
584 msg_Warn( p_input, "drive letter %c: found in source string",
585 p_input->psz_dupsource[0] ) ;
592 p_input->psz_access = p_input->psz_demux = "";
593 p_input->psz_name = p_input->psz_source;
594 free( p_input->psz_dupsource );
595 p_input->psz_dupsource = NULL;
599 *psz_parser++ = '\0';
601 /* let's skip '//' */
602 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
607 p_input->psz_name = psz_parser ;
609 /* Come back to parse the access and demux plug-ins */
610 psz_parser = p_input->psz_dupsource;
615 p_input->psz_access = "";
617 else if( *psz_parser == '/' )
620 p_input->psz_access = "";
625 p_input->psz_access = psz_parser;
627 while( *psz_parser && *psz_parser != '/' )
632 if( *psz_parser == '/' )
634 *psz_parser++ = '\0';
641 p_input->psz_demux = "";
645 p_input->psz_demux = psz_parser;
649 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
650 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
652 if( input_AccessInit( p_input ) == -1 )
654 free( p_input->psz_source );
655 if( p_input->psz_dupsource != NULL )
657 free( p_input->psz_dupsource );
663 /* Initialize optional stream output. (before demuxer)*/
664 var_Get( p_input, "sout", &val );
665 if( val.psz_string != NULL )
667 if ( *val.psz_string && (p_input->stream.p_sout =
668 sout_NewInstance( p_input, val.psz_string )) == NULL )
670 msg_Err( p_input, "cannot start stream output instance, aborting" );
671 free( val.psz_string );
673 input_AccessEnd( p_input );
674 free( p_input->psz_source );
675 if( p_input->psz_dupsource != NULL )
677 free( p_input->psz_dupsource );
681 free( val.psz_string );
684 p_input->p_es_out = input_EsOutNew( p_input );
685 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
686 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
688 /* Find and open appropriate access module */
689 p_input->p_access = module_Need( p_input, "access",
690 p_input->psz_access, VLC_TRUE );
692 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
693 * are forbidden in filenames on Win32. */
695 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
696 if ( p_input->p_access == NULL
697 && (*p_input->psz_demux || *p_input->psz_access) )
699 p_input->psz_access = p_input->psz_demux = "";
700 p_input->psz_name = p_input->psz_source;
701 free( p_input->psz_dupsource);
702 p_input->psz_dupsource = NULL;
704 p_input->p_access = module_Need( p_input, "access",
705 p_input->psz_access, VLC_TRUE );
709 if( p_input->p_access == NULL )
711 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
712 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
713 if ( p_input->stream.p_sout != NULL )
715 sout_DeleteInstance( p_input->stream.p_sout );
718 input_AccessEnd( p_input );
719 free( p_input->psz_source );
720 if( p_input->psz_dupsource != NULL )
722 free( p_input->psz_dupsource );
724 input_EsOutDelete( p_input->p_es_out );
728 /* Waiting for stream. */
731 p_input->i_bufsize = p_input->i_mtu;
735 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
738 /* If the desynchronisation requested by the user is < 0, we need to
739 * cache more data. */
740 var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
741 var_Get( p_input, "audio-desync", &val );
743 p_input->i_pts_delay -= (val.i_int * 1000);
745 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
747 while( !input_FillBuffer( p_input ) )
749 if( p_input->b_die || p_input->b_error || p_input->b_eof )
751 module_Unneed( p_input, p_input->p_access );
752 if ( p_input->stream.p_sout != NULL )
754 sout_DeleteInstance( p_input->stream.p_sout );
756 input_AccessEnd( p_input );
757 free( p_input->psz_source );
758 if( p_input->psz_dupsource != NULL )
760 free( p_input->psz_dupsource );
762 input_EsOutDelete( p_input->p_es_out );
768 /* Create the stream_t facilities */
769 p_input->s = input_StreamNew( p_input );
770 if( p_input->s == NULL )
772 /* should never occur yet */
774 msg_Err( p_input, "cannot create stream_t" );
776 module_Unneed( p_input, p_input->p_access );
777 if ( p_input->stream.p_sout != NULL )
779 sout_DeleteInstance( p_input->stream.p_sout );
781 input_AccessEnd( p_input );
782 free( p_input->psz_source );
783 if( p_input->psz_dupsource != NULL )
785 free( p_input->psz_dupsource );
787 input_EsOutDelete( p_input->p_es_out );
791 /* Find and open appropriate demux module */
793 module_Need( p_input, "demux",
794 (p_input->psz_demux && *p_input->psz_demux) ?
795 p_input->psz_demux : "$demux",
796 (p_input->psz_demux && *p_input->psz_demux) ?
797 VLC_TRUE : VLC_FALSE );
799 if( p_input->p_demux == NULL )
801 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
802 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
804 input_StreamDelete( p_input->s );
805 module_Unneed( p_input, p_input->p_access );
806 if ( p_input->stream.p_sout != NULL )
808 sout_DeleteInstance( p_input->stream.p_sout );
810 input_AccessEnd( p_input );
811 free( p_input->psz_source );
812 if( p_input->psz_dupsource != NULL )
814 free( p_input->psz_dupsource );
816 input_EsOutDelete( p_input->p_es_out );
820 /* Init input_thread_sys_t */
821 p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
822 p_input->p_sys->i_sub = 0;
823 p_input->p_sys->sub = NULL;
825 p_input->p_sys->i_stop_time = 0;
827 /* Get meta information from user */
828 var_Create( p_input, "meta-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
829 var_Create( p_input, "meta-author", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
830 var_Create( p_input, "meta-artist", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
831 var_Create( p_input, "meta-genre", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
832 var_Create( p_input, "meta-copyright", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
833 var_Create( p_input, "meta-description", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
834 var_Create( p_input, "meta-date", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
835 var_Create( p_input, "meta-url", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
836 if( (p_meta_user = vlc_meta_New()) )
840 var_Get( p_input, "meta-title", &val );
841 if( val.psz_string && *val.psz_string )
842 vlc_meta_Add( p_meta_user, VLC_META_TITLE, val.psz_string );
843 if( val.psz_string ) free( val.psz_string );
844 var_Get( p_input, "meta-author", &val );
845 if( val.psz_string && *val.psz_string )
846 vlc_meta_Add( p_meta_user, VLC_META_AUTHOR, val.psz_string );
847 if( val.psz_string ) free( val.psz_string );
848 var_Get( p_input, "meta-artist", &val );
849 if( val.psz_string && *val.psz_string )
850 vlc_meta_Add( p_meta_user, VLC_META_ARTIST, val.psz_string );
851 if( val.psz_string ) free( val.psz_string );
852 var_Get( p_input, "meta-genre", &val );
853 if( val.psz_string && *val.psz_string )
854 vlc_meta_Add( p_meta_user, VLC_META_GENRE, val.psz_string );
855 if( val.psz_string ) free( val.psz_string );
856 var_Get( p_input, "meta-copyright", &val );
857 if( val.psz_string && *val.psz_string )
858 vlc_meta_Add( p_meta_user, VLC_META_COPYRIGHT, val.psz_string );
859 if( val.psz_string ) free( val.psz_string );
860 var_Get( p_input, "meta-description", &val );
861 if( val.psz_string && *val.psz_string )
862 vlc_meta_Add( p_meta_user, VLC_META_DESCRIPTION, val.psz_string );
863 if( val.psz_string ) free( val.psz_string );
864 var_Get( p_input, "meta-date", &val );
865 if( val.psz_string && *val.psz_string )
866 vlc_meta_Add( p_meta_user, VLC_META_DATE, val.psz_string );
867 if( val.psz_string ) free( val.psz_string );
868 var_Get( p_input, "meta-url", &val );
869 if( val.psz_string && *val.psz_string )
870 vlc_meta_Add( p_meta_user, VLC_META_URL, val.psz_string );
871 if( val.psz_string ) free( val.psz_string );
874 /* Get meta informations from demuxer */
875 if( !demux_Control( p_input, DEMUX_GET_META, &p_meta ) ||
876 ( p_meta_user && p_meta_user->i_meta ) )
878 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_input,
879 VLC_OBJECT_PLAYLIST, FIND_PARENT);
880 playlist_item_t *p_item = NULL;
881 input_info_category_t *p_cat;
884 /* Merge demux and user metadata */
885 if( !p_meta ){ p_meta = p_meta_user; p_meta_user = NULL; }
886 else if( p_meta && p_meta_user ) vlc_meta_Merge( p_meta, p_meta_user );
890 vlc_mutex_lock( &p_playlist->object_lock );
891 p_item = playlist_ItemGetByPos( p_playlist, -1 );
894 vlc_mutex_lock( &p_item->lock );
896 vlc_mutex_unlock( &p_playlist->object_lock );
899 msg_Dbg( p_input, "meta informations:" );
900 if( p_meta->i_meta > 0 )
902 p_cat = input_InfoCategory( p_input, _("File") );
903 for( i = 0; i < p_meta->i_meta; i++ )
905 msg_Dbg( p_input, " - '%s' = '%s'", _(p_meta->name[i]),
907 if( !strcmp( p_meta->name[i], VLC_META_TITLE ) )
909 playlist_ItemSetName( p_item, p_meta->value[i] );
911 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
913 playlist_ItemAddInfo( p_item, _("General"), _("Author"),
916 input_AddInfo( p_cat, _(p_meta->name[i]), "%s",
920 playlist_ItemAddInfo( p_item, _("File"),
921 _(p_meta->name[i]), "%s",
926 for( i = 0; i < p_meta->i_track; i++ )
928 vlc_meta_t *tk = p_meta->track[i];
931 msg_Dbg( p_input, " - track[%d]:", i );
934 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
935 sprintf( psz_cat, "%s %d", _("Stream"), i );
936 p_cat = input_InfoCategory( p_input, psz_cat );
938 for( j = 0; j < tk->i_meta; j++ )
940 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
942 input_AddInfo( p_cat, _(tk->name[j]), "%s", tk->value[j] );
945 playlist_ItemAddInfo( p_item, psz_cat, _(tk->name[j]),
946 "%s", tk->value[j] );
954 vlc_mutex_unlock( &p_item->lock );
956 if( p_playlist ) vlc_object_release( p_playlist );
958 if( p_input->stream.p_sout && p_input->stream.p_sout->p_meta == NULL )
960 p_input->stream.p_sout->p_meta = p_meta;
964 vlc_meta_Delete( p_meta );
967 if( p_meta_user ) vlc_meta_Delete( p_meta_user );
970 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) &&
973 input_info_category_t *p_cat =
974 input_InfoCategory( p_input, _("File") );
976 (playlist_t*)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
980 playlist_SetDuration( p_playlist, -1 , i_length );
981 val.b_bool = p_playlist->i_index;
982 var_Set( p_playlist, "item-change", val );
983 vlc_object_release( p_playlist );
987 char psz_buffer[MSTRTIME_MAX_SIZE];
988 input_AddInfo( p_cat, _("Duration"),
989 msecstotimestr( psz_buffer, i_length / 1000 ) );
993 var_Get( p_input, "start-time", &val );
996 double f_pos = val.i_int * I64C(1000000) / (double)i_length;
1000 msg_Warn( p_input, "invalid start-time, ignored (start-time "
1001 ">= media length)" );
1005 p_input->stream.p_selected_area->i_seek =
1006 (int64_t)( f_pos * (double)p_input->stream.p_selected_area->i_size );
1008 msg_Dbg( p_input, "start-time %ds (%2.2f)", val.i_int, f_pos );
1013 /* Set stop-time and check validity */
1014 var_Get( p_input, "stop-time", &val );
1019 var_Get( p_input, "start-time", &start );
1020 if( start.i_int >= val.i_int )
1022 msg_Warn( p_input, "invalid stop-time, ignored (stop-time < "
1027 p_input->p_sys->i_stop_time = (int64_t)val.i_int * I64C(1000000);
1028 msg_Dbg( p_input, "stop-time %ds", val.i_int );
1033 if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
1035 i_microsecondperframe = 0;
1039 i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
1042 /* Look for and add subtitle files */
1043 var_Get( p_input, "sub-file", &val );
1044 if( val.psz_string && *val.psz_string )
1046 subtitle_demux_t *p_sub;
1048 msg_Dbg( p_input, "force subtitle: %s", val.psz_string );
1049 if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string),
1050 i_microsecondperframe ) ) )
1052 p_sub_toselect = p_sub;
1053 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
1056 psz_sub_file = val.psz_string;
1058 var_Get( p_input, "sub-autodetect-file", &val );
1061 subtitle_demux_t *p_sub;
1063 char **tmp = subtitles_Detect( p_input, "", p_input->psz_name );
1065 for( i = 0; *tmp2 != NULL; i++ )
1067 if( psz_sub_file == NULL || strcmp( psz_sub_file, *tmp2 ) )
1069 if( ( p_sub = subtitle_New( p_input, *tmp2,
1070 i_microsecondperframe ) ) )
1072 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub,
1080 if( psz_sub_file ) free( psz_sub_file );
1082 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1083 val.b_bool = VLC_FALSE;
1084 if( p_input->stream.p_sout )
1086 var_Get( p_input, "sout-all", &val );
1088 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
1089 val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
1090 if( p_sub_toselect )
1092 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1093 p_sub_toselect->p_es, VLC_TRUE );
1096 if( p_input->stream.p_sout )
1098 if( p_input->stream.p_sout->i_out_pace_nocontrol > 0 )
1100 p_input->b_out_pace_control = VLC_FALSE;
1104 p_input->b_out_pace_control = VLC_TRUE;
1106 msg_Dbg( p_input, "starting in %s mode",
1107 p_input->b_out_pace_control ? "asynch" : "synch" );
1113 /*****************************************************************************
1114 * ErrorThread: RunThread() error loop
1115 *****************************************************************************
1116 * This function is called when an error occured during thread main's loop.
1117 *****************************************************************************/
1118 static void ErrorThread( input_thread_t *p_input )
1120 while( !p_input->b_die )
1123 msleep( INPUT_IDLE_SLEEP );
1127 /*****************************************************************************
1128 * EndThread: end the input thread
1129 *****************************************************************************/
1130 static void EndThread( input_thread_t * p_input )
1133 #ifdef HAVE_SYS_TIMES_H
1134 /* Display statistics */
1135 struct tms cpu_usage;
1136 times( &cpu_usage );
1138 msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
1139 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
1141 msg_Dbg( p_input, "%ld loops", p_input->c_loops );
1144 input_DumpStream( p_input );
1146 /* Free demultiplexer's data */
1147 if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
1149 /* Free all ES and destroy all decoder threads */
1150 input_EndStream( p_input );
1152 /* Close optional stream output instance */
1153 if( p_input->stream.p_sout )
1155 vlc_object_t *p_pl =
1156 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1159 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1161 /* attach sout to the playlist */
1162 msg_Warn( p_input, "keeping sout" );
1163 vlc_object_detach( p_input->stream.p_sout );
1164 vlc_object_attach( p_input->stream.p_sout, p_pl );
1168 msg_Warn( p_input, "destroying sout" );
1169 sout_DeleteInstance( p_input->stream.p_sout );
1173 vlc_object_release( p_pl );
1177 /* Destroy subtitles demuxers */
1178 if( p_input->p_sys )
1180 for( i = 0; i < p_input->p_sys->i_sub; i++ )
1182 subtitle_Close( p_input->p_sys->sub[i] );
1184 if( p_input->p_sys->i_sub > 0 )
1186 free( p_input->p_sys->sub );
1189 /* Free input_thread_sys_t */
1190 free( p_input->p_sys );
1193 /* Destroy the stream_t facilities */
1194 if( p_input->s ) input_StreamDelete( p_input->s );
1196 /* Destroy es out */
1197 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
1199 /* Close the access plug-in */
1200 if( p_input->p_access ) module_Unneed( p_input, p_input->p_access );
1202 input_AccessEnd( p_input );
1204 /* Free info structures XXX destroy es before 'cause vorbis */
1205 msg_Dbg( p_input, "freeing info structures...");
1206 input_DelInfo( p_input );
1208 free( p_input->psz_source );
1209 if( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
1211 /* Tell we're dead */
1212 p_input->b_dead = 1;
1215 /*****************************************************************************
1216 * ParseOption: parses the options for the input
1217 *****************************************************************************
1218 * This function parses the input (config) options and creates their associated
1220 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
1221 * bar is the value of the option.
1222 *****************************************************************************/
1223 static void ParseOption( input_thread_t *p_input, const char *psz_option )
1225 char *psz_name = (char *)psz_option;
1226 char *psz_value = strchr( psz_option, '=' );
1227 int i_name_len, i_type;
1228 vlc_bool_t b_isno = VLC_FALSE;
1231 if( psz_value ) i_name_len = psz_value - psz_option;
1232 else i_name_len = strlen( psz_option );
1234 /* It's too much of an hassle to remove the ':' when we parse
1235 * the cmd line :) */
1236 if( i_name_len && *psz_name == ':' )
1242 if( i_name_len == 0 ) return;
1244 psz_name = strndup( psz_name, i_name_len );
1245 if( psz_value ) psz_value++;
1247 i_type = config_GetType( p_input, psz_name );
1249 if( !i_type && !psz_value )
1251 /* check for "no-foo" or "nofoo" */
1252 if( !strncmp( psz_name, "no-", 3 ) )
1254 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1256 else if( !strncmp( psz_name, "no", 2 ) )
1258 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1260 else goto cleanup; /* Option doesn't exist */
1263 i_type = config_GetType( p_input, psz_name );
1265 if( !i_type ) goto cleanup; /* Option doesn't exist */
1267 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1269 if( ( i_type != VLC_VAR_BOOL ) &&
1270 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1272 /* Create the variable in the input object.
1273 * Children of the input object will be able to retreive this value
1274 * thanks to the inheritance property of the object variables. */
1275 var_Create( p_input, psz_name, i_type );
1280 val.b_bool = !b_isno;
1283 case VLC_VAR_INTEGER:
1284 val.i_int = atoi( psz_value );
1288 val.f_float = atof( psz_value );
1291 case VLC_VAR_STRING:
1292 case VLC_VAR_MODULE:
1294 case VLC_VAR_DIRECTORY:
1295 val.psz_string = psz_value;
1303 var_Set( p_input, psz_name, val );
1305 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
1308 if( psz_name ) free( psz_name );
1312 /*****************************************************************************
1313 * Callbacks (position, time, state, rate )
1314 *****************************************************************************/
1315 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1316 vlc_value_t oldval, vlc_value_t newval,
1319 input_thread_t *p_input = (input_thread_t *)p_this;
1321 msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
1322 oldval.f_float, newval.f_float );
1324 if( !strcmp( psz_cmd, "position-offset" ) )
1327 var_Get( p_input, "position", &val );
1329 newval.f_float += val.f_float;
1332 vlc_mutex_lock( &p_input->stream.stream_lock );
1333 p_input->stream.p_selected_area->i_seek =
1334 (int64_t)( newval.f_float *
1335 (double)p_input->stream.p_selected_area->i_size );
1337 if( p_input->stream.p_selected_area->i_seek < 0 )
1339 p_input->stream.p_selected_area->i_seek = 0;
1341 vlc_mutex_unlock( &p_input->stream.stream_lock );
1346 static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
1347 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1349 input_thread_t *p_input = (input_thread_t *)p_this;
1352 /* FIXME TODO FIXME */
1353 msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
1354 oldval.i_time, newval.i_time );
1356 var_Get( p_input, "length", &val );
1357 if( val.i_time > 0 )
1359 val.f_float = (double)newval.i_time / (double)val.i_time;
1360 if( !strcmp( psz_cmd, "time-offset" ) )
1362 var_Set( p_input, "position-offset", val );
1366 var_Set( p_input, "position", val );
1371 msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
1376 static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
1377 vlc_value_t oldval, vlc_value_t newval,
1380 input_thread_t *p_input = (input_thread_t *)p_this;
1382 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1383 psz_cmd, oldval.i_int, newval.i_int );
1385 switch( newval.i_int )
1388 input_SetStatus( p_input, INPUT_STATUS_PLAY );
1391 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1394 input_SetStatus( p_input, INPUT_STATUS_END );
1397 msg_Err( p_input, "cannot set new state (invalid)" );
1398 return VLC_EGENERIC;
1402 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
1403 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1405 input_thread_t *p_input = (input_thread_t *)p_this;
1407 if( !strcmp( psz_cmd, "rate-slower" ) )
1409 input_SetStatus( p_input, INPUT_STATUS_SLOWER );
1411 else if( !strcmp( psz_cmd, "rate-faster" ) )
1413 input_SetStatus( p_input, INPUT_STATUS_FASTER );
1417 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1418 psz_cmd, oldval.i_int, newval.i_int );
1419 input_SetRate( p_input, newval.i_int );