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->p_sys = NULL;
184 p_input->psz_source = strdup( 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 );
480 var_Get( p_input, "input-repeat", &repeat );
481 if( repeat.i_int == 0 || p_input->stream.i_area_nb <= 0 )
483 /* End of file - we do not set b_die because only the
484 * playlist is allowed to do so. */
485 msg_Info( p_input, "EOF reached" );
490 msg_Dbg( p_input, "repeating the same input (%d)", repeat.i_int );
491 if( repeat.i_int > 0 )
494 var_Set( p_input, "input-repeat", repeat );
497 p_input->stream.p_new_area = p_input->stream.pp_areas[0];
498 p_input->stream.p_new_area->i_seek = 0;
501 else if( i_count < 0 )
503 p_input->b_error = 1;
506 if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
513 /* update input status variables */
514 if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
516 val.f_float = (float)d_pos;
517 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
519 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
522 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
524 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
526 val.i_time = i_length;
527 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
530 /* Check stop-time */
531 if( p_input->p_sys->i_stop_time > 0 && p_input->p_sys->i_stop_time < i_time )
533 msg_Warn( p_input, "EOF reached because of stop-time" );
538 for( i = 0; i < p_input->p_sys->i_sub; i++ )
540 subtitle_Demux( p_input->p_sys->sub[i], i_time );
543 i_update_next = mdate() + I64C(150000);
547 if( p_input->b_error || p_input->b_eof )
549 ErrorThread( p_input );
552 EndThread( p_input );
557 /*****************************************************************************
558 * InitThread: init the input Thread
559 *****************************************************************************/
560 static int InitThread( input_thread_t * p_input )
564 playlist_t *p_playlist;
567 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
568 char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
570 int64_t i_microsecondperframe;
572 subtitle_demux_t *p_sub_toselect = NULL;
573 char *psz_sub_file = NULL;
575 /* Skip the plug-in names */
576 while( *psz_parser && *psz_parser != ':' )
580 #if defined( WIN32 ) || defined( UNDER_CE )
581 if( psz_parser - p_input->psz_dupsource == 1 )
583 msg_Warn( p_input, "drive letter %c: found in source string",
584 p_input->psz_dupsource[0] ) ;
591 p_input->psz_access = p_input->psz_demux = "";
592 p_input->psz_name = p_input->psz_source;
593 free( p_input->psz_dupsource );
594 p_input->psz_dupsource = NULL;
598 *psz_parser++ = '\0';
600 /* let's skip '//' */
601 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
606 p_input->psz_name = psz_parser ;
608 /* Come back to parse the access and demux plug-ins */
609 psz_parser = p_input->psz_dupsource;
614 p_input->psz_access = "";
616 else if( *psz_parser == '/' )
619 p_input->psz_access = "";
624 p_input->psz_access = psz_parser;
626 while( *psz_parser && *psz_parser != '/' )
631 if( *psz_parser == '/' )
633 *psz_parser++ = '\0';
640 p_input->psz_demux = "";
644 p_input->psz_demux = psz_parser;
648 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
649 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
651 if( input_AccessInit( p_input ) == -1 )
653 free( p_input->psz_source );
654 if( p_input->psz_dupsource != NULL )
656 free( p_input->psz_dupsource );
662 /* Initialize optional stream output. (before demuxer)*/
663 var_Get( p_input, "sout", &val );
664 if( val.psz_string != NULL )
666 if ( *val.psz_string && (p_input->stream.p_sout =
667 sout_NewInstance( p_input, val.psz_string )) == NULL )
669 msg_Err( p_input, "cannot start stream output instance, aborting" );
670 free( val.psz_string );
672 input_AccessEnd( p_input );
673 free( p_input->psz_source );
674 if( p_input->psz_dupsource != NULL )
676 free( p_input->psz_dupsource );
680 free( val.psz_string );
683 p_input->p_es_out = input_EsOutNew( p_input );
684 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
685 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
687 /* Find and open appropriate access module */
688 p_input->p_access = module_Need( p_input, "access",
689 p_input->psz_access, VLC_TRUE );
691 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
692 * are forbidden in filenames on Win32. */
694 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
695 if ( p_input->p_access == NULL
696 && (*p_input->psz_demux || *p_input->psz_access) )
698 p_input->psz_access = p_input->psz_demux = "";
699 p_input->psz_name = p_input->psz_source;
700 free( p_input->psz_dupsource);
701 p_input->psz_dupsource = NULL;
703 p_input->p_access = module_Need( p_input, "access",
704 p_input->psz_access, VLC_TRUE );
708 if( p_input->p_access == NULL )
710 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
711 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
712 if ( p_input->stream.p_sout != NULL )
714 sout_DeleteInstance( p_input->stream.p_sout );
717 input_AccessEnd( p_input );
718 free( p_input->psz_source );
719 if( p_input->psz_dupsource != NULL )
721 free( p_input->psz_dupsource );
723 input_EsOutDelete( p_input->p_es_out );
727 /* Waiting for stream. */
730 p_input->i_bufsize = p_input->i_mtu;
734 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
737 /* If the desynchronisation requested by the user is < 0, we need to
738 * cache more data. */
739 var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
740 var_Get( p_input, "audio-desync", &val );
742 p_input->i_pts_delay -= (val.i_int * 1000);
744 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
746 while( !input_FillBuffer( p_input ) )
748 if( p_input->b_die || p_input->b_error || p_input->b_eof )
750 module_Unneed( p_input, p_input->p_access );
751 if ( p_input->stream.p_sout != NULL )
753 sout_DeleteInstance( p_input->stream.p_sout );
755 input_AccessEnd( p_input );
756 free( p_input->psz_source );
757 if( p_input->psz_dupsource != NULL )
759 free( p_input->psz_dupsource );
761 input_EsOutDelete( p_input->p_es_out );
767 /* Create the stream_t facilities */
768 p_input->s = input_StreamNew( p_input );
769 if( p_input->s == NULL )
771 /* should never occur yet */
773 msg_Err( p_input, "cannot create stream_t" );
775 module_Unneed( p_input, p_input->p_access );
776 if ( p_input->stream.p_sout != NULL )
778 sout_DeleteInstance( p_input->stream.p_sout );
780 input_AccessEnd( p_input );
781 free( p_input->psz_source );
782 if( p_input->psz_dupsource != NULL )
784 free( p_input->psz_dupsource );
786 input_EsOutDelete( p_input->p_es_out );
790 /* Find and open appropriate demux module */
792 module_Need( p_input, "demux",
793 (p_input->psz_demux && *p_input->psz_demux) ?
794 p_input->psz_demux : "$demux",
795 (p_input->psz_demux && *p_input->psz_demux) ?
796 VLC_TRUE : VLC_FALSE );
798 if( p_input->p_demux == NULL )
800 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
801 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
803 input_StreamDelete( p_input->s );
804 module_Unneed( p_input, p_input->p_access );
805 if ( p_input->stream.p_sout != NULL )
807 sout_DeleteInstance( p_input->stream.p_sout );
809 input_AccessEnd( p_input );
810 free( p_input->psz_source );
811 if( p_input->psz_dupsource != NULL )
813 free( p_input->psz_dupsource );
815 input_EsOutDelete( p_input->p_es_out );
819 /* Init input_thread_sys_t */
820 p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
821 p_input->p_sys->i_sub = 0;
822 p_input->p_sys->sub = NULL;
824 p_input->p_sys->i_stop_time = 0;
826 /* get meta informations */
827 if( !demux_Control( p_input, DEMUX_GET_META, &meta ) )
829 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_input,
830 VLC_OBJECT_PLAYLIST, FIND_PARENT);
831 playlist_item_t *p_item = NULL;
832 input_info_category_t *p_cat;
837 vlc_mutex_lock( &p_playlist->object_lock );
838 p_item = playlist_ItemGetByPos( p_playlist, -1 );
841 vlc_mutex_lock( &p_item->lock );
843 vlc_mutex_unlock( &p_playlist->object_lock );
846 msg_Dbg( p_input, "meta informations:" );
847 if( meta->i_meta > 0 )
849 p_cat = input_InfoCategory( p_input, _("File") );
850 for( i = 0; i < meta->i_meta; i++ )
852 msg_Dbg( p_input, " - '%s' = '%s'", _(meta->name[i]), meta->value[i] );
853 if( !strcmp( meta->name[i], VLC_META_TITLE ) )
855 playlist_ItemSetName( p_item, meta->value[i] );
857 if( !strcmp( meta->name[i], VLC_META_AUTHOR ) )
859 playlist_ItemAddInfo( p_item, _("General"), _("Author"),
862 input_AddInfo( p_cat, _(meta->name[i]), "%s", meta->value[i] );
865 playlist_ItemAddInfo( p_item, _("File"),
866 _(meta->name[i]), "%s", meta->value[i] );
870 for( i = 0; i < meta->i_track; i++ )
872 vlc_meta_t *tk = meta->track[i];
875 msg_Dbg( p_input, " - track[%d]:", i );
878 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
879 sprintf( psz_cat, "%s %d", _("Stream"), i );
880 p_cat = input_InfoCategory( p_input, psz_cat );
882 for( j = 0; j < tk->i_meta; j++ )
884 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]), tk->value[j] );
885 input_AddInfo( p_cat, _(tk->name[j]), "%s", tk->value[j] );
888 playlist_ItemAddInfo( p_item, psz_cat,
889 _(tk->name[j]), "%s", tk->value[j] );
897 vlc_mutex_unlock( &p_item->lock );
899 if( p_playlist ) vlc_object_release( p_playlist );
901 vlc_meta_Delete( meta );
905 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) && i_length > 0 )
907 input_info_category_t *p_cat = input_InfoCategory( p_input, _("File") );
908 p_playlist = (playlist_t*)vlc_object_find( p_input,
913 playlist_SetDuration( p_playlist, -1 , i_length );
914 val.b_bool = p_playlist->i_index;
915 var_Set( p_playlist, "item-change", val );
916 vlc_object_release( p_playlist );
920 char psz_buffer[MSTRTIME_MAX_SIZE];
921 input_AddInfo( p_cat, _("Duration"),
922 msecstotimestr( psz_buffer, i_length / 1000 ) );
926 var_Get( p_input, "start-time", &val );
929 double f_pos = (double)( (int64_t)val.i_int * I64C(1000000) ) / (double)i_length;
933 msg_Warn( p_input, "invalid start-time, ignored (start-time >= media length)" );
937 p_input->stream.p_selected_area->i_seek =
938 (int64_t)( f_pos * (double)p_input->stream.p_selected_area->i_size );
940 msg_Dbg( p_input, "start-time %ds (%2.2f)", val.i_int, f_pos );
944 /* Set stop-time and check validity */
945 var_Get( p_input, "stop-time", &val );
950 var_Get( p_input, "start-time", &start );
951 if( start.i_int >= val.i_int )
953 msg_Warn( p_input, "invalid stop-time, ignored (stop-time < start-time)" );
957 p_input->p_sys->i_stop_time = (int64_t)val.i_int * I64C(1000000);
958 msg_Dbg( p_input, "stop-time %ds", val.i_int );
963 if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
965 i_microsecondperframe = 0;
969 i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
972 /* Look for and add subtitle files */
973 var_Get( p_input, "sub-file", &val );
974 if( val.psz_string && *val.psz_string )
976 subtitle_demux_t *p_sub;
978 msg_Dbg( p_input, "force subtitle: %s", val.psz_string );
979 if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string),
980 i_microsecondperframe ) ) )
982 p_sub_toselect = p_sub;
983 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
986 psz_sub_file = val.psz_string;
988 var_Get( p_input, "sub-autodetect-file", &val );
991 subtitle_demux_t *p_sub;
993 char **tmp = subtitles_Detect( p_input, "", p_input->psz_name );
995 for( i = 0; *tmp2 != NULL; i++ )
997 if( psz_sub_file == NULL || strcmp( psz_sub_file, *tmp2 ) )
999 if( ( p_sub = subtitle_New( p_input, *tmp2,
1000 i_microsecondperframe ) ) )
1002 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
1009 if( psz_sub_file ) free( psz_sub_file );
1011 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1012 val.b_bool = VLC_FALSE;
1013 if( p_input->stream.p_sout )
1015 var_Get( p_input, "sout-all", &val );
1017 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
1018 val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
1019 if( p_sub_toselect )
1021 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1022 p_sub_toselect->p_es, VLC_TRUE );
1028 /*****************************************************************************
1029 * ErrorThread: RunThread() error loop
1030 *****************************************************************************
1031 * This function is called when an error occured during thread main's loop.
1032 *****************************************************************************/
1033 static void ErrorThread( input_thread_t *p_input )
1035 while( !p_input->b_die )
1038 msleep( INPUT_IDLE_SLEEP );
1042 /*****************************************************************************
1043 * EndThread: end the input thread
1044 *****************************************************************************/
1045 static void EndThread( input_thread_t * p_input )
1048 #ifdef HAVE_SYS_TIMES_H
1049 /* Display statistics */
1050 struct tms cpu_usage;
1051 times( &cpu_usage );
1053 msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
1054 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
1056 msg_Dbg( p_input, "%ld loops", p_input->c_loops );
1059 input_DumpStream( p_input );
1061 /* Free demultiplexer's data */
1062 if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
1064 /* Free all ES and destroy all decoder threads */
1065 input_EndStream( p_input );
1067 /* Close optional stream output instance */
1068 if( p_input->stream.p_sout )
1070 vlc_object_t *p_pl =
1071 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1074 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1076 /* attach sout to the playlist */
1077 msg_Warn( p_input, "keeping sout" );
1078 vlc_object_detach( p_input->stream.p_sout );
1079 vlc_object_attach( p_input->stream.p_sout, p_pl );
1083 msg_Warn( p_input, "destroying sout" );
1084 sout_DeleteInstance( p_input->stream.p_sout );
1088 vlc_object_release( p_pl );
1092 /* Destroy subtitles demuxers */
1093 if( p_input->p_sys )
1095 for( i = 0; i < p_input->p_sys->i_sub; i++ )
1097 subtitle_Close( p_input->p_sys->sub[i] );
1099 if( p_input->p_sys->i_sub > 0 )
1101 free( p_input->p_sys->sub );
1104 /* Free input_thread_sys_t */
1105 free( p_input->p_sys );
1108 /* Destroy the stream_t facilities */
1109 if( p_input->s ) input_StreamDelete( p_input->s );
1111 /* Destroy es out */
1112 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
1114 /* Close the access plug-in */
1115 if( p_input->p_access ) module_Unneed( p_input, p_input->p_access );
1117 input_AccessEnd( p_input );
1119 /* Free info structures XXX destroy es before 'cause vorbis */
1120 msg_Dbg( p_input, "freeing info structures...");
1121 input_DelInfo( p_input );
1123 free( p_input->psz_source );
1124 if( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
1126 /* Tell we're dead */
1127 p_input->b_dead = 1;
1130 /*****************************************************************************
1131 * ParseOption: parses the options for the input
1132 *****************************************************************************
1133 * This function parses the input (config) options and creates their associated
1135 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
1136 * bar is the value of the option.
1137 *****************************************************************************/
1138 static void ParseOption( input_thread_t *p_input, const char *psz_option )
1140 char *psz_name = (char *)psz_option;
1141 char *psz_value = strchr( psz_option, '=' );
1142 int i_name_len, i_type;
1143 vlc_bool_t b_isno = VLC_FALSE;
1146 if( psz_value ) i_name_len = psz_value - psz_option;
1147 else i_name_len = strlen( psz_option );
1149 /* It's too much of an hassle to remove the ':' when we parse
1150 * the cmd line :) */
1151 if( i_name_len && *psz_name == ':' )
1157 if( i_name_len == 0 ) return;
1159 psz_name = strndup( psz_name, i_name_len );
1160 if( psz_value ) psz_value++;
1162 i_type = config_GetType( p_input, psz_name );
1164 if( !i_type && !psz_value )
1166 /* check for "no-foo" or "nofoo" */
1167 if( !strncmp( psz_name, "no-", 3 ) )
1169 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1171 else if( !strncmp( psz_name, "no", 2 ) )
1173 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1175 else goto cleanup; /* Option doesn't exist */
1178 i_type = config_GetType( p_input, psz_name );
1180 if( !i_type ) goto cleanup; /* Option doesn't exist */
1182 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1184 if( ( i_type != VLC_VAR_BOOL ) &&
1185 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1187 /* Create the variable in the input object.
1188 * Children of the input object will be able to retreive this value
1189 * thanks to the inheritance property of the object variables. */
1190 var_Create( p_input, psz_name, i_type );
1195 val.b_bool = !b_isno;
1198 case VLC_VAR_INTEGER:
1199 val.i_int = atoi( psz_value );
1203 val.f_float = atof( psz_value );
1206 case VLC_VAR_STRING:
1207 case VLC_VAR_MODULE:
1209 case VLC_VAR_DIRECTORY:
1210 val.psz_string = psz_value;
1218 var_Set( p_input, psz_name, val );
1220 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
1223 if( psz_name ) free( psz_name );
1227 /*****************************************************************************
1228 * Callbacks (position, time, state, rate )
1229 *****************************************************************************/
1230 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1231 vlc_value_t oldval, vlc_value_t newval,
1234 input_thread_t *p_input = (input_thread_t *)p_this;
1236 msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
1237 oldval.f_float, newval.f_float );
1239 if( !strcmp( psz_cmd, "position-offset" ) )
1242 var_Get( p_input, "position", &val );
1244 newval.f_float += val.f_float;
1247 vlc_mutex_lock( &p_input->stream.stream_lock );
1248 p_input->stream.p_selected_area->i_seek =
1249 (int64_t)( newval.f_float *
1250 (double)p_input->stream.p_selected_area->i_size );
1252 if( p_input->stream.p_selected_area->i_seek < 0 )
1254 p_input->stream.p_selected_area->i_seek = 0;
1256 vlc_mutex_unlock( &p_input->stream.stream_lock );
1261 static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
1262 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1264 input_thread_t *p_input = (input_thread_t *)p_this;
1267 /* FIXME TODO FIXME */
1268 msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
1269 oldval.i_time, newval.i_time );
1271 var_Get( p_input, "length", &val );
1272 if( val.i_time > 0 )
1274 val.f_float = (double)newval.i_time / (double)val.i_time;
1275 if( !strcmp( psz_cmd, "time-offset" ) )
1277 var_Set( p_input, "position-offset", val );
1281 var_Set( p_input, "position", val );
1286 msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
1291 static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
1292 vlc_value_t oldval, vlc_value_t newval,
1295 input_thread_t *p_input = (input_thread_t *)p_this;
1297 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1298 psz_cmd, oldval.i_int, newval.i_int );
1300 switch( newval.i_int )
1303 input_SetStatus( p_input, INPUT_STATUS_PLAY );
1306 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1309 input_SetStatus( p_input, INPUT_STATUS_END );
1312 msg_Err( p_input, "cannot set new state (invalid)" );
1313 return VLC_EGENERIC;
1317 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
1318 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1320 input_thread_t *p_input = (input_thread_t *)p_this;
1322 if( !strcmp( psz_cmd, "rate-slower" ) )
1324 input_SetStatus( p_input, INPUT_STATUS_SLOWER );
1326 else if( !strcmp( psz_cmd, "rate-faster" ) )
1328 input_SetStatus( p_input, INPUT_STATUS_FASTER );
1332 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1333 psz_cmd, oldval.i_int, newval.i_int );
1334 input_SetRate( p_input, newval.i_int );