1 /*****************************************************************************
2 * input.c: input thread
3 * Read a stream, demultiplex and parse it before sending it to
5 *****************************************************************************
6 * Copyright (C) 1998-2002 VideoLAN
7 * $Id: input.c,v 1.269 2003/11/30 16:00:24 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"
46 #include "modules/demux/util/sub.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 struct input_thread_sys_t
55 subtitle_demux_t **sub;
58 static int RunThread ( input_thread_t *p_input );
59 static int InitThread ( input_thread_t *p_input );
60 static void ErrorThread ( input_thread_t *p_input );
61 static void EndThread ( input_thread_t *p_input );
63 static void ParseOption ( input_thread_t *p_input,
64 const char *psz_option );
66 /*****************************************************************************
68 *****************************************************************************/
69 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
70 vlc_value_t oldval, vlc_value_t newval, void *p_data );
71 static int TimeCallback ( vlc_object_t *p_this, char const *psz_cmd,
72 vlc_value_t oldval, vlc_value_t newval, void *p_data );
73 static int StateCallback ( vlc_object_t *p_this, char const *psz_cmd,
74 vlc_value_t oldval, vlc_value_t newval, void *p_data );
75 static int RateCallback ( vlc_object_t *p_this, char const *psz_cmd,
76 vlc_value_t oldval, vlc_value_t newval, void *p_data );
78 /*****************************************************************************
79 * input_CreateThread: creates a new input thread
80 *****************************************************************************
81 * This function creates a new input, and returns a pointer
82 * to its description. On error, it returns NULL.
83 *****************************************************************************/
84 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
85 playlist_item_t *p_item )
87 input_thread_t * p_input; /* thread descriptor */
88 input_info_category_t * p_info;
92 /* Allocate descriptor */
93 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
96 msg_Err( p_parent, "out of memory" );
100 /* Parse input options */
101 for( i = 0; i < p_item->i_options; i++ )
103 ParseOption( p_input, p_item->ppsz_options[i] );
106 /* Create a few object variables we'll need later on */
107 var_Create( p_input, "video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
108 var_Create( p_input, "audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
109 var_Create( p_input, "audio-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
110 var_Create( p_input, "spu-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
111 var_Create( p_input, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
112 var_Create( p_input, "sub-autodetect-file", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
113 var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
115 var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
116 var_Create( p_input, "sout-all", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
117 var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
118 var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
119 var_Create( p_input, "sout-keep", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
122 var_Create( p_input, "minimize-threads", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
126 /* position variable */
127 var_Create( p_input, "position", VLC_VAR_FLOAT ); /* position 0.0->1.0 */
128 var_Create( p_input, "position-offset", VLC_VAR_FLOAT ); /* relative */
130 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
131 var_AddCallback( p_input, "position", PositionCallback, NULL );
132 var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
135 var_Create( p_input, "time", VLC_VAR_TIME );
136 var_Create( p_input, "time-offset", VLC_VAR_TIME ); /* relative */
138 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
139 var_AddCallback( p_input, "time", TimeCallback, NULL );
140 var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
142 /* length variable */
143 var_Create( p_input, "length", VLC_VAR_TIME );
145 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
148 var_Create( p_input, "rate", VLC_VAR_INTEGER );
149 var_Create( p_input, "rate-slower", VLC_VAR_VOID );
150 var_Create( p_input, "rate-faster", VLC_VAR_VOID );
151 val.i_int = DEFAULT_RATE;
152 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
153 var_AddCallback( p_input, "rate", RateCallback, NULL );
154 var_AddCallback( p_input, "rate-slower", RateCallback, NULL );
155 var_AddCallback( p_input, "rate-faster", RateCallback, NULL );
158 var_Create( p_input, "state", VLC_VAR_INTEGER );
160 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
161 var_AddCallback( p_input, "state", StateCallback, NULL );
164 var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
165 val.b_bool = VLC_FALSE;
166 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
168 /* Initialize thread properties */
170 p_input->p_sys = NULL;
173 p_input->psz_source = strdup( p_item->psz_uri );
179 p_input->p_es_out = NULL;
182 p_input->p_demux = NULL;
183 p_input->pf_demux = NULL;
184 p_input->pf_rewind = NULL;
185 p_input->pf_demux_control = NULL;
186 p_input->i_cr_average = config_GetInt( p_input, "cr-average" );
189 p_input->p_access = NULL;
191 p_input->i_bufsize = 0;
193 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
195 /* Initialize statistics */
196 p_input->c_loops = 0;
197 p_input->stream.c_packets_read = 0;
198 p_input->stream.c_packets_trashed = 0;
201 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
202 vlc_cond_init( p_input, &p_input->stream.stream_wait );
203 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
205 /* Initialize stream description */
206 p_input->stream.b_changed = 0;
207 p_input->stream.i_es_number = 0;
208 p_input->stream.i_selected_es_number = 0;
209 p_input->stream.i_pgrm_number = 0;
210 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
211 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
212 p_input->stream.i_mux_rate = 0;
213 p_input->stream.b_seekable = 0;
214 p_input->stream.p_sout = NULL;
216 /* no stream, no program, no area, no es */
217 p_input->stream.p_new_program = NULL;
219 p_input->stream.i_area_nb = 0;
220 p_input->stream.pp_areas = NULL;
221 p_input->stream.p_selected_area = NULL;
222 p_input->stream.p_new_area = NULL;
224 p_input->stream.pp_selected_es = NULL;
225 p_input->stream.p_removed_es = NULL;
226 p_input->stream.p_newly_selected_es = NULL;
228 /* By default there is one area in a stream */
229 input_AddArea( p_input, 0, 1 );
230 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
232 /* Initialize stream control properties. */
233 p_input->stream.control.i_status = INIT_S;
234 p_input->stream.control.i_rate = DEFAULT_RATE;
235 p_input->stream.control.b_mute = 0;
236 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
238 /* Initialize input info */
239 p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
240 if( !p_input->stream.p_info )
242 msg_Err( p_input, "No memory!" );
245 p_input->stream.p_info->psz_name = strdup("General") ;
246 p_input->stream.p_info->p_info = NULL;
247 p_input->stream.p_info->p_next = NULL;
249 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
251 p_info = input_InfoCategory( p_input, _("General") );
252 input_AddInfo( p_info, _("Playlist Item"), p_input->psz_source );
253 vlc_object_attach( p_input, p_parent );
255 /* Create thread and wait for its readiness. */
256 if( vlc_thread_create( p_input, "input", RunThread,
257 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
259 msg_Err( p_input, "cannot create input thread" );
267 /*****************************************************************************
268 * input_StopThread: mark an input thread as zombie
269 *****************************************************************************
270 * This function should not return until the thread is effectively cancelled.
271 *****************************************************************************/
272 void input_StopThread( input_thread_t *p_input )
274 /* Make the thread exit from a possible vlc_cond_wait() */
275 vlc_mutex_lock( &p_input->stream.stream_lock );
276 /* Request thread destruction */
279 vlc_cond_signal( &p_input->stream.stream_wait );
280 vlc_mutex_unlock( &p_input->stream.stream_lock );
283 /*****************************************************************************
284 * input_DestroyThread: mark an input thread as zombie
285 *****************************************************************************
286 * This function should not return until the thread is effectively cancelled.
287 *****************************************************************************/
288 void input_DestroyThread( input_thread_t *p_input )
290 /* Join the thread */
291 vlc_thread_join( p_input );
293 /* Destroy Mutex locks */
294 vlc_mutex_destroy( &p_input->stream.control.control_lock );
295 vlc_cond_destroy( &p_input->stream.stream_wait );
296 vlc_mutex_destroy( &p_input->stream.stream_lock );
299 /*****************************************************************************
300 * RunThread: main thread loop
301 *****************************************************************************
302 * Thread in charge of processing the network packets and demultiplexing.
303 *****************************************************************************/
304 static int RunThread( input_thread_t *p_input )
307 mtime_t i_update_next = -1;
309 /* Signal right now, otherwise we'll get stuck in a peek */
310 vlc_thread_ready( p_input );
312 if( InitThread( p_input ) )
314 /* If we failed, wait before we are killed, and exit */
315 p_input->b_error = 1;
317 ErrorThread( p_input );
319 /* Tell we're dead */
325 /* initialization is complete */
326 vlc_mutex_lock( &p_input->stream.stream_lock );
327 p_input->stream.b_changed = 1;
328 p_input->stream.control.i_status = PLAYING_S;
329 vlc_mutex_unlock( &p_input->stream.stream_lock );
331 val.i_int = PLAYING_S;
332 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
334 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
336 unsigned int i, i_count;
340 vlc_mutex_lock( &p_input->stream.stream_lock );
342 if( p_input->stream.p_new_program )
344 if( p_input->pf_set_program != NULL )
347 /* Reinitialize buffer manager. */
348 input_AccessReinit( p_input );
350 p_input->pf_set_program( p_input,
351 p_input->stream.p_new_program );
353 /* Escape all decoders for the stream discontinuity they
355 input_EscapeDiscontinuity( p_input );
357 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
359 pgrm_descriptor_t * p_pgrm
360 = p_input->stream.pp_programs[i];
362 /* Reinitialize synchro. */
363 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
366 p_input->stream.p_new_program = NULL;
369 if( p_input->stream.p_new_area )
371 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
373 input_AccessReinit( p_input );
375 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
377 /* Escape all decoders for the stream discontinuity they
379 input_EscapeDiscontinuity( p_input );
381 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
383 pgrm_descriptor_t * p_pgrm
384 = p_input->stream.pp_programs[i];
386 /* Reinitialize synchro. */
387 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
390 p_input->stream.p_new_area = NULL;
393 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
395 if( p_input->stream.p_selected_area->i_size > 0 )
399 double f = (double)p_input->stream.p_selected_area->i_seek /
400 (double)p_input->stream.p_selected_area->i_size;
402 vlc_mutex_unlock( &p_input->stream.stream_lock );
403 demux_Control( p_input, DEMUX_SET_POSITION, f );
404 vlc_mutex_lock( &p_input->stream.stream_lock );
406 /* Escape all decoders for the stream discontinuity they
408 input_EscapeDiscontinuity( p_input );
410 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
412 pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
414 /* Reinitialize synchro. */
415 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
418 vlc_mutex_unlock( &p_input->stream.stream_lock );
419 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
424 /* Help in bar display */
426 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
429 for( i = 0; i < p_input->p_sys->i_sub; i++ )
431 subtitle_Seek( p_input->p_sys->sub[i], i_time );
434 vlc_mutex_lock( &p_input->stream.stream_lock );
436 p_input->stream.p_selected_area->i_seek = NO_SEEK;
439 if( p_input->stream.p_removed_es )
441 input_UnselectES( p_input, p_input->stream.p_removed_es );
442 p_input->stream.p_removed_es = NULL;
445 if( p_input->stream.p_newly_selected_es )
447 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
448 p_input->stream.p_newly_selected_es = NULL;
451 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
453 if( p_input->stream.b_new_mute )
455 input_EscapeAudioDiscontinuity( p_input );
458 vlc_mutex_lock( &p_input->stream.control.control_lock );
459 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
460 vlc_mutex_unlock( &p_input->stream.control.control_lock );
462 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
465 vlc_mutex_unlock( &p_input->stream.stream_lock );
467 /* Read and demultiplex some data. */
468 i_count = p_input->pf_demux( p_input );
472 /* End of file - we do not set b_die because only the
473 * playlist is allowed to do so. */
474 msg_Info( p_input, "EOF reached" );
477 else if( i_count < 0 )
479 p_input->b_error = 1;
482 if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
489 /* update input status variables */
490 if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
492 val.f_float = (float)d_pos;
493 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
495 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
498 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
500 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
502 val.i_time = i_length;
503 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
507 for( i = 0; i < p_input->p_sys->i_sub; i++ )
509 subtitle_Demux( p_input->p_sys->sub[i], i_time );
512 i_update_next = mdate() + 150000LL;
516 if( p_input->b_error || p_input->b_eof )
518 ErrorThread( p_input );
521 EndThread( p_input );
526 /*****************************************************************************
527 * InitThread: init the input Thread
528 *****************************************************************************/
529 static int InitThread( input_thread_t * p_input )
532 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
533 char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
535 subtitle_demux_t *p_sub;
536 int64_t i_microsecondperframe;
538 /* Skip the plug-in names */
539 while( *psz_parser && *psz_parser != ':' )
543 #if defined( WIN32 ) || defined( UNDER_CE )
544 if( psz_parser - p_input->psz_dupsource == 1 )
546 msg_Warn( p_input, "drive letter %c: found in source string",
547 p_input->psz_dupsource[0] ) ;
554 p_input->psz_access = p_input->psz_demux = "";
555 p_input->psz_name = p_input->psz_source;
556 free( p_input->psz_dupsource );
557 p_input->psz_dupsource = NULL;
561 *psz_parser++ = '\0';
563 /* let's skip '//' */
564 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
569 p_input->psz_name = psz_parser ;
571 /* Come back to parse the access and demux plug-ins */
572 psz_parser = p_input->psz_dupsource;
577 p_input->psz_access = "";
579 else if( *psz_parser == '/' )
582 p_input->psz_access = "";
587 p_input->psz_access = psz_parser;
589 while( *psz_parser && *psz_parser != '/' )
594 if( *psz_parser == '/' )
596 *psz_parser++ = '\0';
603 p_input->psz_demux = "";
607 p_input->psz_demux = psz_parser;
611 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
612 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
614 if( input_AccessInit( p_input ) == -1 )
616 free( p_input->psz_source );
617 if( p_input->psz_dupsource != NULL )
619 free( p_input->psz_dupsource );
625 /* Initialize optional stream output. (before demuxer)*/
626 var_Get( p_input, "sout", &val );
627 if( val.psz_string != NULL )
629 if ( *val.psz_string && (p_input->stream.p_sout =
630 sout_NewInstance( p_input, val.psz_string )) == NULL )
632 msg_Err( p_input, "cannot start stream output instance, aborting" );
633 free( val.psz_string );
635 input_AccessEnd( p_input );
636 free( p_input->psz_source );
637 if( p_input->psz_dupsource != NULL )
639 free( p_input->psz_dupsource );
643 free( val.psz_string );
646 p_input->p_es_out = input_EsOutNew( p_input );
647 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
648 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
650 /* Find and open appropriate access module */
651 p_input->p_access = module_Need( p_input, "access",
652 p_input->psz_access );
654 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
655 * are forbidden in filenames on Win32. */
657 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
658 if ( p_input->p_access == NULL
659 && (*p_input->psz_demux || *p_input->psz_access) )
661 p_input->psz_access = p_input->psz_demux = "";
662 p_input->psz_name = p_input->psz_source;
663 free( p_input->psz_dupsource);
664 p_input->psz_dupsource = NULL;
666 p_input->p_access = module_Need( p_input, "access",
667 p_input->psz_access );
671 if( p_input->p_access == NULL )
673 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
674 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
675 if ( p_input->stream.p_sout != NULL )
677 sout_DeleteInstance( p_input->stream.p_sout );
680 input_AccessEnd( p_input );
681 free( p_input->psz_source );
682 if( p_input->psz_dupsource != NULL )
684 free( p_input->psz_dupsource );
689 /* Waiting for stream. */
692 p_input->i_bufsize = p_input->i_mtu;
696 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
699 /* If the desynchronisation requested by the user is < 0, we need to
700 * cache more data. */
701 var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
702 var_Get( p_input, "audio-desync", &val );
704 p_input->i_pts_delay -= (val.i_int * 1000);
706 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
708 while( !input_FillBuffer( p_input ) )
710 if( p_input->b_die || p_input->b_error || p_input->b_eof )
712 module_Unneed( p_input, p_input->p_access );
713 if ( p_input->stream.p_sout != NULL )
715 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 );
728 /* Create the stream_t facilities */
729 p_input->s = stream_OpenInput( p_input );
730 if( p_input->s == NULL )
732 /* should nver occur yet */
734 msg_Err( p_input, "cannot create stream_t !" );
736 module_Unneed( p_input, p_input->p_access );
737 if ( p_input->stream.p_sout != NULL )
739 sout_DeleteInstance( p_input->stream.p_sout );
741 input_AccessEnd( p_input );
742 free( p_input->psz_source );
743 if( p_input->psz_dupsource != NULL )
745 free( p_input->psz_dupsource );
750 /* Find and open appropriate demux module */
752 module_Need( p_input, "demux",
753 (p_input->psz_demux && *p_input->psz_demux) ?
754 p_input->psz_demux : "$demux" );
756 if( p_input->p_demux == NULL )
758 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
759 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
761 stream_Release( p_input->s );
762 module_Unneed( p_input, p_input->p_access );
763 if ( p_input->stream.p_sout != NULL )
765 sout_DeleteInstance( p_input->stream.p_sout );
767 input_AccessEnd( p_input );
768 free( p_input->psz_source );
769 if( p_input->psz_dupsource != NULL )
771 free( p_input->psz_dupsource );
776 /* Init input_thread_sys_t */
777 p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
778 p_input->p_sys->i_sub = 0;
779 p_input->p_sys->sub = NULL;
782 if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
784 i_microsecondperframe = 0;
788 i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
791 /* Look for and add subtitle files */
792 var_Get( p_input, "sub-file", &val );
793 if( val.psz_string && *val.psz_string )
795 if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string), i_microsecondperframe, 0 ) ) )
797 /* Select this ES by default */
798 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES_STATE, p_sub->p_es, VLC_TRUE );
800 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
803 if( val.psz_string ) free( val.psz_string );
805 var_Get( p_input, "sub-autodetect-file", &val );
809 char **tmp = subtitles_Detect( p_input, "", p_input->psz_source );
811 for( i = 0; *tmp2 != NULL; i++ )
813 if( ( p_sub = subtitle_New( p_input, strdup(*tmp2++), i_microsecondperframe, i ) ) )
815 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
821 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
822 val.b_bool = VLC_FALSE;
823 if( p_input->stream.p_sout )
825 var_Get( p_input, "sout-all", &val );
827 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
828 val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
833 /*****************************************************************************
834 * ErrorThread: RunThread() error loop
835 *****************************************************************************
836 * This function is called when an error occured during thread main's loop.
837 *****************************************************************************/
838 static void ErrorThread( input_thread_t *p_input )
840 while( !p_input->b_die )
843 msleep( INPUT_IDLE_SLEEP );
847 /*****************************************************************************
848 * EndThread: end the input thread
849 *****************************************************************************/
850 static void EndThread( input_thread_t * p_input )
853 #ifdef HAVE_SYS_TIMES_H
854 /* Display statistics */
855 struct tms cpu_usage;
858 msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
859 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
861 msg_Dbg( p_input, "%ld loops", p_input->c_loops );
864 input_DumpStream( p_input );
866 /* Free demultiplexer's data */
867 if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
869 /* Free all ES and destroy all decoder threads */
870 input_EndStream( p_input );
872 /* Close optional stream output instance */
873 if( p_input->stream.p_sout )
876 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
879 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
881 /* attach sout to the playlist */
882 msg_Warn( p_input, "keeping sout" );
883 vlc_object_detach( p_input->stream.p_sout );
884 vlc_object_attach( p_input->stream.p_sout, p_pl );
888 msg_Warn( p_input, "destroying sout" );
889 sout_DeleteInstance( p_input->stream.p_sout );
893 vlc_object_release( p_pl );
897 /* Destroy subtitles demuxers */
900 for( i = 0; i < p_input->p_sys->i_sub; i++ )
902 subtitle_Close( p_input->p_sys->sub[i] );
904 if( p_input->p_sys->i_sub > 0 )
906 free( p_input->p_sys->sub );
909 /* Free input_thread_sys_t */
910 free( p_input->p_sys );
913 /* Destroy the stream_t facilities */
914 if( p_input->s ) stream_Release( p_input->s );
917 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
919 /* Close the access plug-in */
920 if( p_input->p_access ) module_Unneed( p_input, p_input->p_access );
922 input_AccessEnd( p_input );
924 /* Free info structures XXX destroy es before 'cause vorbis */
925 msg_Dbg( p_input, "freeing info structures...");
926 input_DelInfo( p_input );
928 free( p_input->psz_source );
929 if( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
931 /* Tell we're dead */
935 /*****************************************************************************
936 * ParseOption: parses the options for the input
937 *****************************************************************************
938 * This function parses the input (config) options and creates their associated
940 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
941 * bar is the value of the option.
942 *****************************************************************************/
943 static void ParseOption( input_thread_t *p_input, const char *psz_option )
945 char *psz_name = (char *)psz_option;
946 char *psz_value = strchr( psz_option, '=' );
947 int i_name_len, i_type;
948 vlc_bool_t b_isno = VLC_FALSE;
951 if( psz_value ) i_name_len = psz_value - psz_option;
952 else i_name_len = strlen( psz_option );
954 /* It's too much of an hassle to remove the ':' when we parse
956 if( i_name_len && *psz_name == ':' )
962 if( i_name_len == 0 ) return;
964 psz_name = strndup( psz_name, i_name_len );
965 if( psz_value ) psz_value++;
967 i_type = config_GetType( p_input, psz_name );
969 if( !i_type && !psz_value )
971 /* check for "no-foo" or "nofoo" */
972 if( !strncmp( psz_name, "no-", 3 ) )
974 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
976 else if( !strncmp( psz_name, "no", 2 ) )
978 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
980 else goto cleanup; /* Option doesn't exist */
983 i_type = config_GetType( p_input, psz_name );
985 if( !i_type ) goto cleanup; /* Option doesn't exist */
987 else if( !i_type ) goto cleanup; /* Option doesn't exist */
989 if( ( i_type != VLC_VAR_BOOL ) &&
990 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
992 /* Create the variable in the input object.
993 * Children of the input object will be able to retreive this value
994 * thanks to the inheritance property of the object variables. */
995 var_Create( p_input, psz_name, i_type );
1000 val.b_bool = !b_isno;
1003 case VLC_VAR_INTEGER:
1004 val.i_int = atoi( psz_value );
1008 val.f_float = atof( psz_value );
1011 case VLC_VAR_STRING:
1012 case VLC_VAR_MODULE:
1014 case VLC_VAR_DIRECTORY:
1015 val.psz_string = psz_value;
1023 var_Set( p_input, psz_name, val );
1025 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
1028 if( psz_name ) free( psz_name );
1032 /*****************************************************************************
1033 * Callbacks (position, time, state, rate )
1034 *****************************************************************************/
1035 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1036 vlc_value_t oldval, vlc_value_t newval,
1039 input_thread_t *p_input = (input_thread_t *)p_this;
1041 msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
1042 oldval.f_float, newval.f_float );
1044 if( !strcmp( psz_cmd, "position-offset" ) )
1047 var_Get( p_input, "position", &val );
1049 newval.f_float += val.f_float;
1052 vlc_mutex_lock( &p_input->stream.stream_lock );
1053 p_input->stream.p_selected_area->i_seek =
1054 (int64_t)( newval.f_float *
1055 (double)p_input->stream.p_selected_area->i_size );
1057 if( p_input->stream.p_selected_area->i_seek < 0 )
1059 p_input->stream.p_selected_area->i_seek = 0;
1061 vlc_mutex_unlock( &p_input->stream.stream_lock );
1066 static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
1067 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1069 input_thread_t *p_input = (input_thread_t *)p_this;
1072 /* FIXME TODO FIXME */
1073 msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
1074 oldval.i_time, newval.i_time );
1076 var_Get( p_input, "length", &val );
1077 if( val.i_time > 0 )
1079 val.f_float = (double)newval.i_time / (double)val.i_time;
1080 if( !strcmp( psz_cmd, "time-offset" ) )
1082 var_Set( p_input, "position-offset", val );
1086 var_Set( p_input, "position", val );
1091 msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
1096 static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
1097 vlc_value_t oldval, vlc_value_t newval,
1100 input_thread_t *p_input = (input_thread_t *)p_this;
1102 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1103 psz_cmd, oldval.i_int, newval.i_int );
1105 switch( newval.i_int )
1108 input_SetStatus( p_input, INPUT_STATUS_PLAY );
1111 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1114 input_SetStatus( p_input, INPUT_STATUS_END );
1117 msg_Err( p_input, "cannot set new state (invalid)" );
1118 return VLC_EGENERIC;
1122 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
1123 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1125 input_thread_t *p_input = (input_thread_t *)p_this;
1127 if( !strcmp( psz_cmd, "rate-slower" ) )
1129 input_SetStatus( p_input, INPUT_STATUS_SLOWER );
1131 else if( !strcmp( psz_cmd, "rate-faster" ) )
1133 input_SetStatus( p_input, INPUT_STATUS_FASTER );
1137 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1138 psz_cmd, oldval.i_int, newval.i_int );
1139 input_SetRate( p_input, newval.i_int );