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.253 2003/11/13 12:28:34 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>
35 #ifdef HAVE_SYS_TIMES_H
36 # include <sys/times.h>
39 #include "vlc_playlist.h"
41 #include "stream_output.h"
43 #include "vlc_interface.h"
45 #include "modules/demux/util/sub.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 struct input_thread_sys_t
54 subtitle_demux_t **sub;
57 static int RunThread ( input_thread_t *p_input );
58 static int InitThread ( input_thread_t *p_input );
59 static void ErrorThread ( input_thread_t *p_input );
60 static void EndThread ( input_thread_t *p_input );
62 static void ParseOption ( input_thread_t *p_input,
63 const char *psz_option );
65 static es_out_t *EsOutCreate ( input_thread_t * );
66 static void EsOutRelease( es_out_t * );
68 /*****************************************************************************
70 *****************************************************************************/
71 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
72 vlc_value_t oldval, vlc_value_t newval, void *p_data );
73 static int TimeCallback ( vlc_object_t *p_this, char const *psz_cmd,
74 vlc_value_t oldval, vlc_value_t newval, void *p_data );
75 static int StateCallback ( vlc_object_t *p_this, char const *psz_cmd,
76 vlc_value_t oldval, vlc_value_t newval, void *p_data );
77 static int RateCallback ( vlc_object_t *p_this, char const *psz_cmd,
78 vlc_value_t oldval, vlc_value_t newval, void *p_data );
80 /*****************************************************************************
81 * input_CreateThread: creates a new input thread
82 *****************************************************************************
83 * This function creates a new input, and returns a pointer
84 * to its description. On error, it returns NULL.
85 *****************************************************************************/
86 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
87 playlist_item_t *p_item )
89 input_thread_t * p_input; /* thread descriptor */
90 input_info_category_t * p_info;
94 /* Allocate descriptor */
95 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
98 msg_Err( p_parent, "out of memory" );
102 /* Parse input options */
103 for( i = 0; i < p_item->i_options; i++ )
105 ParseOption( p_input, p_item->ppsz_options[i] );
108 /* Create a few object variables we'll need later on */
109 var_Create( p_input, "video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
110 var_Create( p_input, "audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
111 var_Create( p_input, "audio-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
112 var_Create( p_input, "spu-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
113 var_Create( p_input, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
114 var_Create( p_input, "sub-autodetect-file", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
115 var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
117 var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
118 var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
119 var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
120 var_Create( p_input, "sout-keep", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
124 /* position variable */
125 var_Create( p_input, "position", VLC_VAR_FLOAT ); /* position 0.0->1.0 */
126 var_Create( p_input, "position-offset", VLC_VAR_FLOAT ); /* relative */
128 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
129 var_AddCallback( p_input, "position", PositionCallback, NULL );
130 var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
133 var_Create( p_input, "time", VLC_VAR_TIME );
134 var_Create( p_input, "time-offset", VLC_VAR_TIME ); /* relative */
136 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
137 var_AddCallback( p_input, "time", TimeCallback, NULL );
138 var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
140 /* length variable */
141 var_Create( p_input, "length", VLC_VAR_TIME );
143 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
146 var_Create( p_input, "rate", VLC_VAR_INTEGER );
147 var_Create( p_input, "rate-slower", VLC_VAR_VOID );
148 var_Create( p_input, "rate-faster", VLC_VAR_VOID );
149 val.i_int = DEFAULT_RATE;
150 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
151 var_AddCallback( p_input, "rate", RateCallback, NULL );
152 var_AddCallback( p_input, "rate-slower", RateCallback, NULL );
153 var_AddCallback( p_input, "rate-faster", RateCallback, NULL );
156 var_Create( p_input, "state", VLC_VAR_INTEGER );
158 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
159 var_AddCallback( p_input, "state", StateCallback, NULL );
162 var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
163 val.b_bool = VLC_FALSE;
164 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
166 /* Initialize thread properties */
168 p_input->p_sys = NULL;
171 p_input->psz_source = strdup( p_item->psz_uri );
177 p_input->p_es_out = NULL;
180 p_input->p_demux = NULL;
181 p_input->pf_demux = NULL;
182 p_input->pf_rewind = NULL;
183 p_input->pf_demux_control = NULL;
186 p_input->p_access = NULL;
188 p_input->i_bufsize = 0;
190 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
192 /* Initialize statistics */
193 p_input->c_loops = 0;
194 p_input->stream.c_packets_read = 0;
195 p_input->stream.c_packets_trashed = 0;
198 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
199 vlc_cond_init( p_input, &p_input->stream.stream_wait );
200 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
202 /* Initialize stream description */
203 p_input->stream.b_changed = 0;
204 p_input->stream.i_es_number = 0;
205 p_input->stream.i_selected_es_number = 0;
206 p_input->stream.i_pgrm_number = 0;
207 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
208 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
209 p_input->stream.i_mux_rate = 0;
210 p_input->stream.b_seekable = 0;
211 p_input->stream.p_sout = NULL;
213 /* no stream, no program, no area, no es */
214 p_input->stream.p_new_program = NULL;
216 p_input->stream.i_area_nb = 0;
217 p_input->stream.pp_areas = NULL;
218 p_input->stream.p_selected_area = NULL;
219 p_input->stream.p_new_area = NULL;
221 p_input->stream.pp_selected_es = NULL;
222 p_input->stream.p_removed_es = NULL;
223 p_input->stream.p_newly_selected_es = NULL;
225 /* By default there is one area in a stream */
226 input_AddArea( p_input, 0, 1 );
227 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
229 /* Initialize stream control properties. */
230 p_input->stream.control.i_status = INIT_S;
231 p_input->stream.control.i_rate = DEFAULT_RATE;
232 p_input->stream.control.b_mute = 0;
233 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale" );
235 /* Initialize input info */
236 p_input->stream.p_info = malloc( sizeof( input_info_category_t ) );
237 if( !p_input->stream.p_info )
239 msg_Err( p_input, "No memory!" );
242 p_input->stream.p_info->psz_name = strdup("General") ;
243 p_input->stream.p_info->p_info = NULL;
244 p_input->stream.p_info->p_next = NULL;
246 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
248 p_info = input_InfoCategory( p_input, _("General") );
249 input_AddInfo( p_info, _("Playlist Item"), p_input->psz_source );
250 vlc_object_attach( p_input, p_parent );
252 /* Create thread and wait for its readiness. */
253 if( vlc_thread_create( p_input, "input", RunThread,
254 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
256 msg_Err( p_input, "cannot create input thread" );
264 /*****************************************************************************
265 * input_StopThread: mark an input thread as zombie
266 *****************************************************************************
267 * This function should not return until the thread is effectively cancelled.
268 *****************************************************************************/
269 void input_StopThread( input_thread_t *p_input )
271 /* Make the thread exit from a possible vlc_cond_wait() */
272 vlc_mutex_lock( &p_input->stream.stream_lock );
273 /* Request thread destruction */
276 vlc_cond_signal( &p_input->stream.stream_wait );
277 vlc_mutex_unlock( &p_input->stream.stream_lock );
280 /*****************************************************************************
281 * input_DestroyThread: mark an input thread as zombie
282 *****************************************************************************
283 * This function should not return until the thread is effectively cancelled.
284 *****************************************************************************/
285 void input_DestroyThread( input_thread_t *p_input )
287 /* Join the thread */
288 vlc_thread_join( p_input );
290 /* Destroy Mutex locks */
291 vlc_mutex_destroy( &p_input->stream.control.control_lock );
292 vlc_cond_destroy( &p_input->stream.stream_wait );
293 vlc_mutex_destroy( &p_input->stream.stream_lock );
296 /*****************************************************************************
297 * RunThread: main thread loop
298 *****************************************************************************
299 * Thread in charge of processing the network packets and demultiplexing.
300 *****************************************************************************/
301 static int RunThread( input_thread_t *p_input )
304 mtime_t i_update_next = -1;
306 /* Signal right now, otherwise we'll get stuck in a peek */
307 vlc_thread_ready( p_input );
309 if( InitThread( p_input ) )
311 /* If we failed, wait before we are killed, and exit */
312 p_input->b_error = 1;
313 ErrorThread( p_input );
318 /* initialization is complete */
319 vlc_mutex_lock( &p_input->stream.stream_lock );
320 p_input->stream.b_changed = 1;
321 p_input->stream.control.i_status = PLAYING_S;
322 vlc_mutex_unlock( &p_input->stream.stream_lock );
324 val.i_int = PLAYING_S;
325 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
327 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
329 unsigned int i, i_count;
333 vlc_mutex_lock( &p_input->stream.stream_lock );
335 if( p_input->stream.p_new_program )
337 if( p_input->pf_set_program != NULL )
340 /* Reinitialize buffer manager. */
341 input_AccessReinit( p_input );
343 p_input->pf_set_program( p_input,
344 p_input->stream.p_new_program );
346 /* Escape all decoders for the stream discontinuity they
348 input_EscapeDiscontinuity( p_input );
350 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
352 pgrm_descriptor_t * p_pgrm
353 = p_input->stream.pp_programs[i];
355 /* Reinitialize synchro. */
356 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
359 p_input->stream.p_new_program = NULL;
362 if( p_input->stream.p_new_area )
364 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
366 input_AccessReinit( p_input );
368 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
370 /* Escape all decoders for the stream discontinuity they
372 input_EscapeDiscontinuity( p_input );
374 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
376 pgrm_descriptor_t * p_pgrm
377 = p_input->stream.pp_programs[i];
379 /* Reinitialize synchro. */
380 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
383 p_input->stream.p_new_area = NULL;
386 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
388 if( p_input->stream.p_selected_area->i_size > 0 )
392 double f = (double)p_input->stream.p_selected_area->i_seek /
393 (double)p_input->stream.p_selected_area->i_size;
395 vlc_mutex_unlock( &p_input->stream.stream_lock );
396 demux_Control( p_input, DEMUX_SET_POSITION, f );
397 vlc_mutex_lock( &p_input->stream.stream_lock );
399 /* Escape all decoders for the stream discontinuity they
401 input_EscapeDiscontinuity( p_input );
403 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
405 pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
407 /* Reinitialize synchro. */
408 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
411 vlc_mutex_unlock( &p_input->stream.stream_lock );
412 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
417 /* Help in bar display */
419 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
422 for( i = 0; i < p_input->p_sys->i_sub; i++ )
424 subtitle_Seek( p_input->p_sys->sub[i], i_time );
427 vlc_mutex_lock( &p_input->stream.stream_lock );
429 p_input->stream.p_selected_area->i_seek = NO_SEEK;
432 if( p_input->stream.p_removed_es )
434 input_UnselectES( p_input, p_input->stream.p_removed_es );
435 p_input->stream.p_removed_es = NULL;
438 if( p_input->stream.p_newly_selected_es )
440 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
441 p_input->stream.p_newly_selected_es = NULL;
444 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
446 if( p_input->stream.b_new_mute )
448 input_EscapeAudioDiscontinuity( p_input );
451 vlc_mutex_lock( &p_input->stream.control.control_lock );
452 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
453 vlc_mutex_unlock( &p_input->stream.control.control_lock );
455 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
458 vlc_mutex_unlock( &p_input->stream.stream_lock );
460 /* Read and demultiplex some data. */
461 i_count = p_input->pf_demux( p_input );
465 /* End of file - we do not set b_die because only the
466 * playlist is allowed to do so. */
467 msg_Info( p_input, "EOF reached" );
470 else if( i_count < 0 )
472 p_input->b_error = 1;
475 if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
482 /* update input status variables */
483 if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
485 val.f_float = (float)d_pos;
486 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
488 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
491 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
493 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
495 val.i_time = i_length;
496 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
500 for( i = 0; i < p_input->p_sys->i_sub; i++ )
502 subtitle_Demux( p_input->p_sys->sub[i], i_time );
505 i_update_next = mdate() + 150000LL;
509 if( p_input->b_error || p_input->b_eof )
511 ErrorThread( p_input );
514 EndThread( p_input );
519 /*****************************************************************************
520 * InitThread: init the input Thread
521 *****************************************************************************/
522 static int InitThread( input_thread_t * p_input )
525 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
526 char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
528 subtitle_demux_t *p_sub;
529 int64_t i_microsecondperframe;
531 /* Skip the plug-in names */
532 while( *psz_parser && *psz_parser != ':' )
536 #if defined( WIN32 ) || defined( UNDER_CE )
537 if( psz_parser - p_input->psz_dupsource == 1 )
539 msg_Warn( p_input, "drive letter %c: found in source string",
540 p_input->psz_dupsource[0] ) ;
547 p_input->psz_access = p_input->psz_demux = "";
548 p_input->psz_name = p_input->psz_source;
549 free( p_input->psz_dupsource );
550 p_input->psz_dupsource = NULL;
554 *psz_parser++ = '\0';
556 /* let's skip '//' */
557 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
562 p_input->psz_name = psz_parser ;
564 /* Come back to parse the access and demux plug-ins */
565 psz_parser = p_input->psz_dupsource;
570 p_input->psz_access = "";
572 else if( *psz_parser == '/' )
575 p_input->psz_access = "";
580 p_input->psz_access = psz_parser;
582 while( *psz_parser && *psz_parser != '/' )
587 if( *psz_parser == '/' )
589 *psz_parser++ = '\0';
596 p_input->psz_demux = "";
600 p_input->psz_demux = psz_parser;
604 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
605 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
607 if( input_AccessInit( p_input ) == -1 )
612 /* Initialize optional stream output. (before demuxer)*/
613 var_Get( p_input, "sout", &val );
614 if( val.psz_string != NULL )
616 if ( *val.psz_string && (p_input->stream.p_sout =
617 sout_NewInstance( p_input, val.psz_string )) == NULL )
619 msg_Err( p_input, "cannot start stream output instance, aborting" );
620 free( val.psz_string );
623 free( val.psz_string );
626 p_input->p_es_out = EsOutCreate( p_input );
628 /* Find and open appropriate access module */
629 p_input->p_access = module_Need( p_input, "access",
630 p_input->psz_access );
632 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
633 * are forbidden in filenames on Win32. */
635 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
636 if ( p_input->p_access == NULL
637 && (*p_input->psz_demux || *p_input->psz_access) )
639 p_input->psz_access = p_input->psz_demux = "";
640 p_input->psz_name = p_input->psz_source;
641 free( p_input->psz_dupsource);
642 p_input->psz_dupsource = NULL;
644 p_input->p_access = module_Need( p_input, "access",
645 p_input->psz_access );
649 if( p_input->p_access == NULL )
651 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
652 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
653 if ( p_input->stream.p_sout != NULL )
655 sout_DeleteInstance( p_input->stream.p_sout );
660 /* Waiting for stream. */
663 p_input->i_bufsize = p_input->i_mtu;
667 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
670 /* If the desynchronisation requested by the user is < 0, we need to
671 * cache more data. */
672 var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
673 var_Get( p_input, "audio-desync", &val );
675 p_input->i_pts_delay -= (val.i_int * 1000);
677 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
679 while( !input_FillBuffer( p_input ) )
681 if( p_input->b_die || p_input->b_error || p_input->b_eof )
683 module_Unneed( p_input, p_input->p_access );
684 if ( p_input->stream.p_sout != NULL )
686 sout_DeleteInstance( p_input->stream.p_sout );
693 /* Create the stream_t facilities */
694 p_input->s = stream_OpenInput( p_input );
695 if( p_input->s == NULL )
697 /* should nver occur yet */
699 msg_Err( p_input, "cannot create stream_t !" );
700 module_Unneed( p_input, p_input->p_access );
701 if ( p_input->stream.p_sout != NULL )
703 sout_DeleteInstance( p_input->stream.p_sout );
708 /* Find and open appropriate demux module */
710 module_Need( p_input, "demux",
711 (p_input->psz_demux && *p_input->psz_demux) ?
712 p_input->psz_demux : "$demux" );
714 if( p_input->p_demux == NULL )
716 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
717 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
718 stream_Release( p_input->s );
719 module_Unneed( p_input, p_input->p_access );
720 if ( p_input->stream.p_sout != NULL )
722 sout_DeleteInstance( p_input->stream.p_sout );
727 /* Init input_thread_sys_t */
728 p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
729 p_input->p_sys->i_sub = 0;
730 p_input->p_sys->sub = NULL;
733 if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
735 i_microsecondperframe = 0;
739 i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
742 /* Look for and add subtitle files */
743 var_Get( p_input, "sub-file", &val );
744 if( val.psz_string && *val.psz_string )
746 if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string), i_microsecondperframe, 0 ) ) )
748 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
749 subtitle_Select( 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 all ES and destroy all decoder threads */
807 input_EndStream( p_input );
809 /* Close optional stream output instance */
810 if ( p_input->stream.p_sout != NULL )
813 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
816 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
818 /* attach sout to the playlist */
819 msg_Warn( p_input, "keeping sout" );
820 vlc_object_detach( p_input->stream.p_sout );
821 vlc_object_attach( p_input->stream.p_sout, p_pl );
825 msg_Warn( p_input, "destroying sout" );
826 sout_DeleteInstance( p_input->stream.p_sout );
830 vlc_object_release( p_pl );
834 /* Destroy subtitles demuxers */
835 for( i = 0; i < p_input->p_sys->i_sub; i++ )
837 subtitle_Close( p_input->p_sys->sub[i] );
839 if( p_input->p_sys->i_sub > 0 )
841 free( p_input->p_sys->sub );
844 /* Free input_thread_sys_t */
845 free( p_input->p_sys );
847 /* Free demultiplexer's data */
848 module_Unneed( p_input, p_input->p_demux );
850 /* Destroy the stream_t facilities */
851 stream_Release( p_input->s );
854 EsOutRelease( 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 * es_out_t input handler
971 *****************************************************************************/
974 input_thread_t *p_input;
984 es_descriptor_t *p_es;
987 static es_out_id_t *EsOutAdd ( es_out_t *, es_format_t * );
988 static int EsOutSend ( es_out_t *, es_out_id_t *, pes_packet_t * );
989 static void EsOutDel ( es_out_t *, es_out_id_t * );
990 static int EsOutControl( es_out_t *, int i_query, va_list );
992 static es_out_t *EsOutCreate( input_thread_t *p_input )
994 es_out_t *out = malloc( sizeof( es_out_t ) );
996 out->pf_add = EsOutAdd;
997 out->pf_send = EsOutSend;
998 out->pf_del = EsOutDel;
999 out->pf_control = EsOutControl;
1001 out->p_sys = malloc( sizeof( es_out_sys_t ) );
1002 out->p_sys->p_input = p_input;
1003 out->p_sys->i_id = 0;
1004 out->p_sys->id = NULL;
1005 out->p_sys->i_audio = -1;
1006 out->p_sys->i_video = -1;
1009 static void EsOutRelease( es_out_t *out )
1011 es_out_sys_t *p_sys = out->p_sys;
1014 for( i = 0; i < p_sys->i_id; i++ )
1016 free( p_sys->id[i] );
1026 static es_out_id_t *EsOutAdd( es_out_t *out, es_format_t *fmt )
1028 es_out_sys_t *p_sys = out->p_sys;
1029 input_thread_t *p_input = p_sys->p_input;
1030 es_out_id_t *id = malloc( sizeof( es_out_id_t ) );
1031 pgrm_descriptor_t *p_prgm = NULL;
1032 char psz_cat[strlen( "Stream " ) + 10];
1033 input_info_category_t *p_cat;
1035 vlc_mutex_lock( &p_input->stream.stream_lock );
1036 if( fmt->i_group >= 0 )
1038 /* search program */
1039 p_prgm = input_FindProgram( p_input, fmt->i_group );
1041 if( p_prgm == NULL )
1044 p_prgm = input_AddProgram( p_input, fmt->i_group, 0 );
1046 /* Select the first by default */
1047 if( p_input->stream.p_selected_program == NULL )
1049 p_input->stream.p_selected_program = p_prgm;
1054 id->p_es = input_AddES( p_input,
1056 1 + out->p_sys->i_id,
1058 fmt->psz_description, 0 );
1059 id->p_es->i_stream_id = 1 + out->p_sys->i_id;
1060 id->p_es->i_fourcc = fmt->i_codec;
1062 switch( fmt->i_cat )
1066 WAVEFORMATEX *p_wf = malloc( sizeof( WAVEFORMATEX ) + fmt->i_extra);
1068 p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
1069 p_wf->nChannels = fmt->audio.i_channels;
1070 p_wf->nSamplesPerSec = fmt->audio.i_samplerate;
1071 p_wf->nAvgBytesPerSec = fmt->audio.i_bitrate / 8;
1072 p_wf->nBlockAlign = fmt->audio.i_blockalign;
1073 p_wf->wBitsPerSample = fmt->audio.i_bitspersample;
1074 p_wf->cbSize = fmt->i_extra;
1075 if( fmt->i_extra > 0 )
1077 if( fmt->i_extra_type != ES_EXTRA_TYPE_WAVEFORMATEX )
1079 msg_Warn( p_input, "extra type != WAVEFORMATEX for audio" );
1081 memcpy( &p_wf[1], fmt->p_extra, fmt->i_extra );
1083 id->p_es->p_waveformatex = p_wf;
1088 BITMAPINFOHEADER *p_bih = malloc( sizeof( BITMAPINFOHEADER ) +
1090 p_bih->biSize = sizeof(BITMAPINFOHEADER) + fmt->i_extra;
1091 p_bih->biWidth = fmt->video.i_width;
1092 p_bih->biHeight = fmt->video.i_height;
1093 p_bih->biPlanes = 1;
1094 p_bih->biBitCount = 24;
1095 p_bih->biCompression = fmt->i_codec;
1096 p_bih->biSizeImage = fmt->video.i_width * fmt->video.i_height;
1097 p_bih->biXPelsPerMeter = 0;
1098 p_bih->biYPelsPerMeter = 0;
1099 p_bih->biClrUsed = 0;
1100 p_bih->biClrImportant = 0;
1102 if( fmt->i_extra > 0 )
1104 if( fmt->i_extra_type != ES_EXTRA_TYPE_BITMAPINFOHEADER )
1107 "extra type != BITMAPINFOHEADER for video" );
1109 memcpy( &p_bih[1], fmt->p_extra, fmt->i_extra );
1111 id->p_es->p_bitmapinfoheader = p_bih;
1118 if( fmt->i_cat == AUDIO_ES && fmt->i_priority > out->p_sys->i_audio )
1120 if( out->p_sys->i_audio >= 0 )
1122 msg_Err( p_input, "FIXME unselect es in es_out_Add" );
1124 input_SelectES( p_input, id->p_es );
1125 if( id->p_es->p_decoder_fifo )
1127 out->p_sys->i_audio = fmt->i_priority;
1130 else if( fmt->i_cat == VIDEO_ES && fmt->i_priority > out->p_sys->i_video )
1132 if( out->p_sys->i_video >= 0 )
1134 msg_Err( p_input, "FIXME unselect es in es_out_Add" );
1136 input_SelectES( p_input, id->p_es );
1137 if( id->p_es->p_decoder_fifo )
1139 out->p_sys->i_video = fmt->i_priority;
1143 sprintf( psz_cat, _("Stream %d"), out->p_sys->i_id );
1144 if( ( p_cat = input_InfoCategory( p_input, psz_cat ) ) )
1146 /* Add information */
1147 switch( fmt->i_cat )
1150 input_AddInfo( p_cat, _("Type"), _("Audio") );
1151 input_AddInfo( p_cat, _("Codec"), "%.4s", (char*)&fmt->i_codec );
1152 if( fmt->audio.i_channels > 0 )
1154 input_AddInfo( p_cat, _("Channels"), "%d", fmt->audio.i_channels );
1156 if( fmt->audio.i_samplerate > 0 )
1158 input_AddInfo( p_cat, _("Sample Rate"), "%d", fmt->audio.i_samplerate );
1160 if( fmt->audio.i_bitrate > 0 )
1162 input_AddInfo( p_cat, _("Bitrate"), "%d", fmt->audio.i_bitrate );
1164 if( fmt->audio.i_bitspersample )
1166 input_AddInfo( p_cat, _("Bits Per Sample"), "%d", fmt->audio.i_bitspersample );
1170 input_AddInfo( p_cat, _("Type"), _("Video") );
1171 input_AddInfo( p_cat, _("Codec"), "%.4s", (char*)&fmt->i_codec );
1172 if( fmt->video.i_width > 0 && fmt->video.i_height > 0 )
1174 input_AddInfo( p_cat, _("Resolution"), "%dx%d", fmt->video.i_width, fmt->video.i_height );
1176 if( fmt->video.i_display_width > 0 && fmt->video.i_display_height > 0 )
1178 input_AddInfo( p_cat, _("Display Resolution"), "%dx%d", fmt->video.i_display_width, fmt->video.i_display_height);
1182 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
1183 input_AddInfo( p_cat, _("Codec"), "%.4s", (char*)&fmt->i_codec );
1190 vlc_mutex_unlock( &p_input->stream.stream_lock );
1192 TAB_APPEND( out->p_sys->i_id, out->p_sys->id, id );
1195 static int EsOutSend( es_out_t *out, es_out_id_t *id, pes_packet_t *p_pes )
1197 if( id->p_es->p_decoder_fifo )
1199 input_DecodePES( id->p_es->p_decoder_fifo, p_pes );
1203 input_DeletePES( out->p_sys->p_input->p_method_data, p_pes );
1207 static void EsOutDel( es_out_t *out, es_out_id_t *id )
1209 es_out_sys_t *p_sys = out->p_sys;
1211 TAB_REMOVE( p_sys->i_id, p_sys->id, id );
1213 vlc_mutex_lock( &p_sys->p_input->stream.stream_lock );
1214 if( id->p_es->p_decoder_fifo )
1216 input_UnselectES( p_sys->p_input, id->p_es );
1218 if( id->p_es->p_waveformatex )
1220 free( id->p_es->p_waveformatex );
1221 id->p_es->p_waveformatex = NULL;
1223 if( id->p_es->p_bitmapinfoheader )
1225 free( id->p_es->p_bitmapinfoheader );
1226 id->p_es->p_bitmapinfoheader = NULL;
1228 input_DelES( p_sys->p_input, id->p_es );
1229 vlc_mutex_unlock( &p_sys->p_input->stream.stream_lock );
1233 static int EsOutControl( es_out_t *out, int i_query, va_list args )
1235 es_out_sys_t *p_sys = out->p_sys;
1240 case ES_OUT_SET_SELECT:
1241 id = (es_out_id_t*) va_arg( args, es_out_id_t * );
1242 b = (vlc_bool_t) va_arg( args, vlc_bool_t );
1243 if( b && id->p_es->p_decoder_fifo == NULL )
1245 input_SelectES( p_sys->p_input, id->p_es );
1246 return id->p_es->p_decoder_fifo ? VLC_SUCCESS : VLC_EGENERIC;
1248 else if( !b && id->p_es->p_decoder_fifo )
1250 input_UnselectES( p_sys->p_input, id->p_es );
1254 case ES_OUT_GET_SELECT:
1255 id = (es_out_id_t*) va_arg( args, es_out_id_t * );
1256 pb = (vlc_bool_t*) va_arg( args, vlc_bool_t * );
1258 *pb = id->p_es->p_decoder_fifo ? VLC_TRUE : VLC_FALSE;
1262 msg_Err( p_sys->p_input, "unknown query in es_out_Control" );
1263 return VLC_EGENERIC;
1267 /*****************************************************************************
1268 * Callbacks (position, time, state, rate )
1269 *****************************************************************************/
1270 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1271 vlc_value_t oldval, vlc_value_t newval,
1274 input_thread_t *p_input = (input_thread_t *)p_this;
1276 msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
1277 oldval.f_float, newval.f_float );
1279 if( !strcmp( psz_cmd, "position-offset" ) )
1282 var_Get( p_input, "position", &val );
1284 newval.f_float += val.f_float;
1287 vlc_mutex_lock( &p_input->stream.stream_lock );
1288 p_input->stream.p_selected_area->i_seek =
1289 (int64_t)( newval.f_float *
1290 (double)p_input->stream.p_selected_area->i_size );
1292 if( p_input->stream.p_selected_area->i_seek < 0 )
1294 p_input->stream.p_selected_area->i_seek = 0;
1296 vlc_mutex_unlock( &p_input->stream.stream_lock );
1301 static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
1302 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1304 input_thread_t *p_input = (input_thread_t *)p_this;
1307 /* FIXME TODO FIXME */
1308 msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
1309 oldval.i_time, newval.i_time );
1311 var_Get( p_input, "length", &val );
1312 if( val.i_time > 0 )
1314 val.f_float = (double)newval.i_time / (double)val.i_time;
1315 if( !strcmp( psz_cmd, "time-offset" ) )
1317 var_Set( p_input, "position-offset", val );
1321 var_Set( p_input, "position", val );
1326 msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
1331 static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
1332 vlc_value_t oldval, vlc_value_t newval,
1335 input_thread_t *p_input = (input_thread_t *)p_this;
1337 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1338 psz_cmd, oldval.i_int, newval.i_int );
1340 switch( newval.i_int )
1343 input_SetStatus( p_input, INPUT_STATUS_PLAY );
1346 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1349 input_SetStatus( p_input, INPUT_STATUS_END );
1352 msg_Err( p_input, "cannot set new state (invalid)" );
1353 return VLC_EGENERIC;
1357 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
1358 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1360 input_thread_t *p_input = (input_thread_t *)p_this;
1362 if( !strcmp( psz_cmd, "rate-slower" ) )
1364 input_SetStatus( p_input, INPUT_STATUS_SLOWER );
1366 else if( !strcmp( psz_cmd, "rate-faster" ) )
1368 input_SetStatus( p_input, INPUT_STATUS_FASTER );
1372 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1373 psz_cmd, oldval.i_int, newval.i_int );
1374 input_SetRate( p_input, newval.i_int );