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.263 2003/11/24 20:50:45 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-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
117 var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
118 var_Create( p_input, "sout-keep", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
122 /* position variable */
123 var_Create( p_input, "position", VLC_VAR_FLOAT ); /* position 0.0->1.0 */
124 var_Create( p_input, "position-offset", VLC_VAR_FLOAT ); /* relative */
126 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
127 var_AddCallback( p_input, "position", PositionCallback, NULL );
128 var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
131 var_Create( p_input, "time", VLC_VAR_TIME );
132 var_Create( p_input, "time-offset", VLC_VAR_TIME ); /* relative */
134 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
135 var_AddCallback( p_input, "time", TimeCallback, NULL );
136 var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
138 /* length variable */
139 var_Create( p_input, "length", VLC_VAR_TIME );
141 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
144 var_Create( p_input, "rate", VLC_VAR_INTEGER );
145 var_Create( p_input, "rate-slower", VLC_VAR_VOID );
146 var_Create( p_input, "rate-faster", VLC_VAR_VOID );
147 val.i_int = DEFAULT_RATE;
148 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
149 var_AddCallback( p_input, "rate", RateCallback, NULL );
150 var_AddCallback( p_input, "rate-slower", RateCallback, NULL );
151 var_AddCallback( p_input, "rate-faster", RateCallback, NULL );
154 var_Create( p_input, "state", VLC_VAR_INTEGER );
156 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
157 var_AddCallback( p_input, "state", StateCallback, NULL );
160 var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
161 val.b_bool = VLC_FALSE;
162 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
164 /* Initialize thread properties */
166 p_input->p_sys = NULL;
169 p_input->psz_source = strdup( p_item->psz_uri );
175 p_input->p_es_out = NULL;
178 p_input->p_demux = NULL;
179 p_input->pf_demux = NULL;
180 p_input->pf_rewind = NULL;
181 p_input->pf_demux_control = NULL;
184 p_input->p_access = NULL;
186 p_input->i_bufsize = 0;
188 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
190 /* Initialize statistics */
191 p_input->c_loops = 0;
192 p_input->stream.c_packets_read = 0;
193 p_input->stream.c_packets_trashed = 0;
196 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
197 vlc_cond_init( p_input, &p_input->stream.stream_wait );
198 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
200 /* Initialize stream description */
201 p_input->stream.b_changed = 0;
202 p_input->stream.i_es_number = 0;
203 p_input->stream.i_selected_es_number = 0;
204 p_input->stream.i_pgrm_number = 0;
205 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
206 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
207 p_input->stream.i_mux_rate = 0;
208 p_input->stream.b_seekable = 0;
209 p_input->stream.p_sout = NULL;
211 /* no stream, no program, no area, no es */
212 p_input->stream.p_new_program = NULL;
214 p_input->stream.i_area_nb = 0;
215 p_input->stream.pp_areas = NULL;
216 p_input->stream.p_selected_area = NULL;
217 p_input->stream.p_new_area = NULL;
219 p_input->stream.pp_selected_es = NULL;
220 p_input->stream.p_removed_es = NULL;
221 p_input->stream.p_newly_selected_es = NULL;
223 /* By default there is one area in a stream */
224 input_AddArea( p_input, 0, 1 );
225 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
227 /* Initialize stream control properties. */
228 p_input->stream.control.i_status = INIT_S;
229 p_input->stream.control.i_rate = DEFAULT_RATE;
230 p_input->stream.control.b_mute = 0;
231 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
233 /* Initialize input info */
234 p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
235 if( !p_input->stream.p_info )
237 msg_Err( p_input, "No memory!" );
240 p_input->stream.p_info->psz_name = strdup("General") ;
241 p_input->stream.p_info->p_info = NULL;
242 p_input->stream.p_info->p_next = NULL;
244 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
246 p_info = input_InfoCategory( p_input, _("General") );
247 input_AddInfo( p_info, _("Playlist Item"), p_input->psz_source );
248 vlc_object_attach( p_input, p_parent );
250 /* Create thread and wait for its readiness. */
251 if( vlc_thread_create( p_input, "input", RunThread,
252 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
254 msg_Err( p_input, "cannot create input thread" );
262 /*****************************************************************************
263 * input_StopThread: mark an input thread as zombie
264 *****************************************************************************
265 * This function should not return until the thread is effectively cancelled.
266 *****************************************************************************/
267 void input_StopThread( input_thread_t *p_input )
269 /* Make the thread exit from a possible vlc_cond_wait() */
270 vlc_mutex_lock( &p_input->stream.stream_lock );
271 /* Request thread destruction */
274 vlc_cond_signal( &p_input->stream.stream_wait );
275 vlc_mutex_unlock( &p_input->stream.stream_lock );
278 /*****************************************************************************
279 * input_DestroyThread: mark an input thread as zombie
280 *****************************************************************************
281 * This function should not return until the thread is effectively cancelled.
282 *****************************************************************************/
283 void input_DestroyThread( input_thread_t *p_input )
285 /* Join the thread */
286 vlc_thread_join( p_input );
288 /* Destroy Mutex locks */
289 vlc_mutex_destroy( &p_input->stream.control.control_lock );
290 vlc_cond_destroy( &p_input->stream.stream_wait );
291 vlc_mutex_destroy( &p_input->stream.stream_lock );
294 /*****************************************************************************
295 * RunThread: main thread loop
296 *****************************************************************************
297 * Thread in charge of processing the network packets and demultiplexing.
298 *****************************************************************************/
299 static int RunThread( input_thread_t *p_input )
302 mtime_t i_update_next = -1;
304 /* Signal right now, otherwise we'll get stuck in a peek */
305 vlc_thread_ready( p_input );
307 if( InitThread( p_input ) )
309 /* If we failed, wait before we are killed, and exit */
310 p_input->b_error = 1;
311 ErrorThread( p_input );
316 /* initialization is complete */
317 vlc_mutex_lock( &p_input->stream.stream_lock );
318 p_input->stream.b_changed = 1;
319 p_input->stream.control.i_status = PLAYING_S;
320 vlc_mutex_unlock( &p_input->stream.stream_lock );
322 val.i_int = PLAYING_S;
323 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
325 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
327 unsigned int i, i_count;
331 vlc_mutex_lock( &p_input->stream.stream_lock );
333 if( p_input->stream.p_new_program )
335 if( p_input->pf_set_program != NULL )
338 /* Reinitialize buffer manager. */
339 input_AccessReinit( p_input );
341 p_input->pf_set_program( p_input,
342 p_input->stream.p_new_program );
344 /* Escape all decoders for the stream discontinuity they
346 input_EscapeDiscontinuity( p_input );
348 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
350 pgrm_descriptor_t * p_pgrm
351 = p_input->stream.pp_programs[i];
353 /* Reinitialize synchro. */
354 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
357 p_input->stream.p_new_program = NULL;
360 if( p_input->stream.p_new_area )
362 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
364 input_AccessReinit( p_input );
366 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
368 /* Escape all decoders for the stream discontinuity they
370 input_EscapeDiscontinuity( p_input );
372 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
374 pgrm_descriptor_t * p_pgrm
375 = p_input->stream.pp_programs[i];
377 /* Reinitialize synchro. */
378 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
381 p_input->stream.p_new_area = NULL;
384 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
386 if( p_input->stream.p_selected_area->i_size > 0 )
390 double f = (double)p_input->stream.p_selected_area->i_seek /
391 (double)p_input->stream.p_selected_area->i_size;
393 vlc_mutex_unlock( &p_input->stream.stream_lock );
394 demux_Control( p_input, DEMUX_SET_POSITION, f );
395 vlc_mutex_lock( &p_input->stream.stream_lock );
397 /* Escape all decoders for the stream discontinuity they
399 input_EscapeDiscontinuity( p_input );
401 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
403 pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
405 /* Reinitialize synchro. */
406 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
409 vlc_mutex_unlock( &p_input->stream.stream_lock );
410 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
415 /* Help in bar display */
417 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
420 for( i = 0; i < p_input->p_sys->i_sub; i++ )
422 subtitle_Seek( p_input->p_sys->sub[i], i_time );
425 vlc_mutex_lock( &p_input->stream.stream_lock );
427 p_input->stream.p_selected_area->i_seek = NO_SEEK;
430 if( p_input->stream.p_removed_es )
432 input_UnselectES( p_input, p_input->stream.p_removed_es );
433 p_input->stream.p_removed_es = NULL;
436 if( p_input->stream.p_newly_selected_es )
438 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
439 p_input->stream.p_newly_selected_es = NULL;
442 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
444 if( p_input->stream.b_new_mute )
446 input_EscapeAudioDiscontinuity( p_input );
449 vlc_mutex_lock( &p_input->stream.control.control_lock );
450 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
451 vlc_mutex_unlock( &p_input->stream.control.control_lock );
453 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
456 vlc_mutex_unlock( &p_input->stream.stream_lock );
458 /* Read and demultiplex some data. */
459 i_count = p_input->pf_demux( p_input );
463 /* End of file - we do not set b_die because only the
464 * playlist is allowed to do so. */
465 msg_Info( p_input, "EOF reached" );
468 else if( i_count < 0 )
470 p_input->b_error = 1;
473 if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
480 /* update input status variables */
481 if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
483 val.f_float = (float)d_pos;
484 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
486 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
489 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
491 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
493 val.i_time = i_length;
494 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
498 for( i = 0; i < p_input->p_sys->i_sub; i++ )
500 subtitle_Demux( p_input->p_sys->sub[i], i_time );
503 i_update_next = mdate() + 150000LL;
507 if( p_input->b_error || p_input->b_eof )
509 ErrorThread( p_input );
512 EndThread( p_input );
517 /*****************************************************************************
518 * InitThread: init the input Thread
519 *****************************************************************************/
520 static int InitThread( input_thread_t * p_input )
523 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
524 char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
526 subtitle_demux_t *p_sub;
527 int64_t i_microsecondperframe;
529 /* Skip the plug-in names */
530 while( *psz_parser && *psz_parser != ':' )
534 #if defined( WIN32 ) || defined( UNDER_CE )
535 if( psz_parser - p_input->psz_dupsource == 1 )
537 msg_Warn( p_input, "drive letter %c: found in source string",
538 p_input->psz_dupsource[0] ) ;
545 p_input->psz_access = p_input->psz_demux = "";
546 p_input->psz_name = p_input->psz_source;
547 free( p_input->psz_dupsource );
548 p_input->psz_dupsource = NULL;
552 *psz_parser++ = '\0';
554 /* let's skip '//' */
555 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
560 p_input->psz_name = psz_parser ;
562 /* Come back to parse the access and demux plug-ins */
563 psz_parser = p_input->psz_dupsource;
568 p_input->psz_access = "";
570 else if( *psz_parser == '/' )
573 p_input->psz_access = "";
578 p_input->psz_access = psz_parser;
580 while( *psz_parser && *psz_parser != '/' )
585 if( *psz_parser == '/' )
587 *psz_parser++ = '\0';
594 p_input->psz_demux = "";
598 p_input->psz_demux = psz_parser;
602 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
603 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
605 if( input_AccessInit( p_input ) == -1 )
610 /* Initialize optional stream output. (before demuxer)*/
611 var_Get( p_input, "sout", &val );
612 if( val.psz_string != NULL )
614 if ( *val.psz_string && (p_input->stream.p_sout =
615 sout_NewInstance( p_input, val.psz_string )) == NULL )
617 msg_Err( p_input, "cannot start stream output instance, aborting" );
618 free( val.psz_string );
621 free( val.psz_string );
624 p_input->p_es_out = input_EsOutNew( p_input );
626 /* Find and open appropriate access module */
627 p_input->p_access = module_Need( p_input, "access",
628 p_input->psz_access );
630 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
631 * are forbidden in filenames on Win32. */
633 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
634 if ( p_input->p_access == NULL
635 && (*p_input->psz_demux || *p_input->psz_access) )
637 p_input->psz_access = p_input->psz_demux = "";
638 p_input->psz_name = p_input->psz_source;
639 free( p_input->psz_dupsource);
640 p_input->psz_dupsource = NULL;
642 p_input->p_access = module_Need( p_input, "access",
643 p_input->psz_access );
647 if( p_input->p_access == NULL )
649 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
650 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
651 if ( p_input->stream.p_sout != NULL )
653 sout_DeleteInstance( p_input->stream.p_sout );
658 /* Waiting for stream. */
661 p_input->i_bufsize = p_input->i_mtu;
665 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
668 /* If the desynchronisation requested by the user is < 0, we need to
669 * cache more data. */
670 var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
671 var_Get( p_input, "audio-desync", &val );
673 p_input->i_pts_delay -= (val.i_int * 1000);
675 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
677 while( !input_FillBuffer( p_input ) )
679 if( p_input->b_die || p_input->b_error || p_input->b_eof )
681 module_Unneed( p_input, p_input->p_access );
682 if ( p_input->stream.p_sout != NULL )
684 sout_DeleteInstance( p_input->stream.p_sout );
691 /* Create the stream_t facilities */
692 p_input->s = stream_OpenInput( p_input );
693 if( p_input->s == NULL )
695 /* should nver occur yet */
697 msg_Err( p_input, "cannot create stream_t !" );
698 module_Unneed( p_input, p_input->p_access );
699 if ( p_input->stream.p_sout != NULL )
701 sout_DeleteInstance( p_input->stream.p_sout );
706 /* Find and open appropriate demux module */
708 module_Need( p_input, "demux",
709 (p_input->psz_demux && *p_input->psz_demux) ?
710 p_input->psz_demux : "$demux" );
712 if( p_input->p_demux == NULL )
714 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
715 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
716 stream_Release( p_input->s );
717 module_Unneed( p_input, p_input->p_access );
718 if ( p_input->stream.p_sout != NULL )
720 sout_DeleteInstance( p_input->stream.p_sout );
725 /* Init input_thread_sys_t */
726 p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
727 p_input->p_sys->i_sub = 0;
728 p_input->p_sys->sub = NULL;
731 if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
733 i_microsecondperframe = 0;
737 i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
740 /* Look for and add subtitle files */
741 var_Get( p_input, "sub-file", &val );
742 if( val.psz_string && *val.psz_string )
744 if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string), i_microsecondperframe, 0 ) ) )
746 /* Select this ES by default */
747 es_out_Control( p_input->p_es_out, ES_OUT_SET_SELECT, p_sub->p_es, VLC_TRUE );
749 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
752 if( val.psz_string ) free( val.psz_string );
754 var_Get( p_input, "sub-autodetect-file", &val );
758 char **tmp = subtitles_Detect( p_input, "", p_input->psz_source );
760 for( i = 0; *tmp2 != NULL; i++ )
762 if( ( p_sub = subtitle_New( p_input, strdup(*tmp2++), i_microsecondperframe, i ) ) )
764 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
773 /*****************************************************************************
774 * ErrorThread: RunThread() error loop
775 *****************************************************************************
776 * This function is called when an error occured during thread main's loop.
777 *****************************************************************************/
778 static void ErrorThread( input_thread_t *p_input )
780 while( !p_input->b_die )
783 msleep( INPUT_IDLE_SLEEP );
787 /*****************************************************************************
788 * EndThread: end the input thread
789 *****************************************************************************/
790 static void EndThread( input_thread_t * p_input )
793 #ifdef HAVE_SYS_TIMES_H
794 /* Display statistics */
795 struct tms cpu_usage;
798 msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
799 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
801 msg_Dbg( p_input, "%ld loops", p_input->c_loops );
804 input_DumpStream( p_input );
806 /* Free demultiplexer's data */
807 module_Unneed( p_input, p_input->p_demux );
809 /* Free all ES and destroy all decoder threads */
810 input_EndStream( p_input );
812 /* Close optional stream output instance */
813 if ( p_input->stream.p_sout != NULL )
816 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
819 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
821 /* attach sout to the playlist */
822 msg_Warn( p_input, "keeping sout" );
823 vlc_object_detach( p_input->stream.p_sout );
824 vlc_object_attach( p_input->stream.p_sout, p_pl );
828 msg_Warn( p_input, "destroying sout" );
829 sout_DeleteInstance( p_input->stream.p_sout );
833 vlc_object_release( p_pl );
837 /* Destroy subtitles demuxers */
838 for( i = 0; i < p_input->p_sys->i_sub; i++ )
840 subtitle_Close( p_input->p_sys->sub[i] );
842 if( p_input->p_sys->i_sub > 0 )
844 free( p_input->p_sys->sub );
847 /* Free input_thread_sys_t */
848 free( p_input->p_sys );
850 /* Destroy the stream_t facilities */
851 stream_Release( p_input->s );
854 input_EsOutDelete( p_input->p_es_out );
856 /* Close the access plug-in */
857 module_Unneed( p_input, p_input->p_access );
859 input_AccessEnd( p_input );
861 /* Free info structures XXX destroy es before 'cause vorbis */
862 msg_Dbg( p_input, "freeing info structures...");
863 input_DelInfo( p_input );
865 free( p_input->psz_source );
866 if ( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
868 /* Tell we're dead */
872 /*****************************************************************************
873 * ParseOption: parses the options for the input
874 *****************************************************************************
875 * This function parses the input (config) options and creates their associated
877 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
878 * bar is the value of the option.
879 *****************************************************************************/
880 static void ParseOption( input_thread_t *p_input, const char *psz_option )
882 char *psz_name = (char *)psz_option;
883 char *psz_value = strchr( psz_option, '=' );
884 int i_name_len, i_type;
885 vlc_bool_t b_isno = VLC_FALSE;
888 if( psz_value ) i_name_len = psz_value - psz_option;
889 else i_name_len = strlen( psz_option );
891 /* It's too much of an hassle to remove the ':' when we parse
893 if( i_name_len && *psz_name == ':' )
899 if( i_name_len == 0 ) return;
901 psz_name = strndup( psz_name, i_name_len );
902 if( psz_value ) psz_value++;
904 i_type = config_GetType( p_input, psz_name );
906 if( !i_type && !psz_value )
908 /* check for "no-foo" or "nofoo" */
909 if( !strncmp( psz_name, "no-", 3 ) )
911 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
913 else if( !strncmp( psz_name, "no", 2 ) )
915 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
917 else goto cleanup; /* Option doesn't exist */
920 i_type = config_GetType( p_input, psz_name );
922 if( !i_type ) goto cleanup; /* Option doesn't exist */
924 else if( !i_type ) goto cleanup; /* Option doesn't exist */
926 if( ( i_type != VLC_VAR_BOOL ) &&
927 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
929 /* Create the variable in the input object.
930 * Children of the input object will be able to retreive this value
931 * thanks to the inheritance property of the object variables. */
932 var_Create( p_input, psz_name, i_type );
937 val.b_bool = !b_isno;
940 case VLC_VAR_INTEGER:
941 val.i_int = atoi( psz_value );
945 val.f_float = atof( psz_value );
951 case VLC_VAR_DIRECTORY:
952 val.psz_string = psz_value;
960 var_Set( p_input, psz_name, val );
962 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
965 if( psz_name ) free( psz_name );
969 /*****************************************************************************
970 * Callbacks (position, time, state, rate )
971 *****************************************************************************/
972 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
973 vlc_value_t oldval, vlc_value_t newval,
976 input_thread_t *p_input = (input_thread_t *)p_this;
978 msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
979 oldval.f_float, newval.f_float );
981 if( !strcmp( psz_cmd, "position-offset" ) )
984 var_Get( p_input, "position", &val );
986 newval.f_float += val.f_float;
989 vlc_mutex_lock( &p_input->stream.stream_lock );
990 p_input->stream.p_selected_area->i_seek =
991 (int64_t)( newval.f_float *
992 (double)p_input->stream.p_selected_area->i_size );
994 if( p_input->stream.p_selected_area->i_seek < 0 )
996 p_input->stream.p_selected_area->i_seek = 0;
998 vlc_mutex_unlock( &p_input->stream.stream_lock );
1003 static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
1004 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1006 input_thread_t *p_input = (input_thread_t *)p_this;
1009 /* FIXME TODO FIXME */
1010 msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
1011 oldval.i_time, newval.i_time );
1013 var_Get( p_input, "length", &val );
1014 if( val.i_time > 0 )
1016 val.f_float = (double)newval.i_time / (double)val.i_time;
1017 if( !strcmp( psz_cmd, "time-offset" ) )
1019 var_Set( p_input, "position-offset", val );
1023 var_Set( p_input, "position", val );
1028 msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
1033 static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
1034 vlc_value_t oldval, vlc_value_t newval,
1037 input_thread_t *p_input = (input_thread_t *)p_this;
1039 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1040 psz_cmd, oldval.i_int, newval.i_int );
1042 switch( newval.i_int )
1045 input_SetStatus( p_input, INPUT_STATUS_PLAY );
1048 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1051 input_SetStatus( p_input, INPUT_STATUS_END );
1054 msg_Err( p_input, "cannot set new state (invalid)" );
1055 return VLC_EGENERIC;
1059 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
1060 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1062 input_thread_t *p_input = (input_thread_t *)p_this;
1064 if( !strcmp( psz_cmd, "rate-slower" ) )
1066 input_SetStatus( p_input, INPUT_STATUS_SLOWER );
1068 else if( !strcmp( psz_cmd, "rate-faster" ) )
1070 input_SetStatus( p_input, INPUT_STATUS_FASTER );
1074 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1075 psz_cmd, oldval.i_int, newval.i_int );
1076 input_SetRate( p_input, newval.i_int );