1 /*****************************************************************************
2 * input.c: input thread
3 * Read a stream, demultiplex and parse it before sending it to
5 *****************************************************************************
6 * Copyright (C) 1998-2004 VideoLAN
9 * Authors: Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
32 #include <vlc/input.h>
33 #include <vlc/decoder.h>
36 #ifdef HAVE_SYS_TIMES_H
37 # include <sys/times.h>
40 #include "vlc_playlist.h"
42 #include "stream_output.h"
44 #include "vlc_interface.h"
47 #include "../../modules/demux/util/sub.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 struct input_thread_sys_t
56 subtitle_demux_t **sub;
61 static int RunThread ( input_thread_t *p_input );
62 static int InitThread ( input_thread_t *p_input );
63 static void ErrorThread ( input_thread_t *p_input );
64 static void EndThread ( input_thread_t *p_input );
66 static void ParseOption ( input_thread_t *p_input,
67 const char *psz_option );
69 /*****************************************************************************
71 *****************************************************************************/
72 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
73 vlc_value_t oldval, vlc_value_t newval, void *p_data );
74 static int TimeCallback ( vlc_object_t *p_this, char const *psz_cmd,
75 vlc_value_t oldval, vlc_value_t newval, void *p_data );
76 static int StateCallback ( vlc_object_t *p_this, char const *psz_cmd,
77 vlc_value_t oldval, vlc_value_t newval, void *p_data );
78 static int RateCallback ( vlc_object_t *p_this, char const *psz_cmd,
79 vlc_value_t oldval, vlc_value_t newval, void *p_data );
80 static int BookmarkCallback( vlc_object_t *p_this, char const *psz_cmd,
81 vlc_value_t oldval, vlc_value_t newval, void *p_data );
83 /*****************************************************************************
84 * input_CreateThread: creates a new input thread
85 *****************************************************************************
86 * This function creates a new input, and returns a pointer
87 * to its description. On error, it returns NULL.
88 *****************************************************************************/
89 input_thread_t *__input_CreateThread( vlc_object_t *p_parent, char *psz_uri,
90 char **ppsz_options, int i_options )
93 input_thread_t *p_input; /* thread descriptor */
97 /* Allocate descriptor */
98 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
101 msg_Err( p_parent, "out of memory" );
105 /* Parse input options */
106 for( i = 0; i < i_options; i++ )
108 msg_Dbg( p_input, "option: %s", ppsz_options[i] );
109 ParseOption( p_input, ppsz_options[i] );
112 /* Create a few object variables we'll need later on */
113 var_Create( p_input, "video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
114 var_Create( p_input, "audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
115 var_Create( p_input, "audio-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
116 var_Create( p_input, "spu-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
117 var_Create( p_input, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
118 var_Create( p_input, "sub-autodetect-file", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
119 var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
121 var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
122 var_Create( p_input, "sout-all", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
123 var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
124 var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
125 var_Create( p_input, "sout-keep", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
127 /* repeat variable */
128 var_Create( p_input, "input-repeat", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
130 /* start/stop time */
131 var_Create( p_input, "start-time", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
132 var_Create( p_input, "stop-time", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
135 var_Create( p_input, "minimize-threads", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
139 /* position variable */
140 var_Create( p_input, "position", VLC_VAR_FLOAT ); /* position 0.0->1.0 */
141 var_Create( p_input, "position-offset", VLC_VAR_FLOAT ); /* relative */
143 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
144 var_AddCallback( p_input, "position", PositionCallback, NULL );
145 var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
148 var_Create( p_input, "time", VLC_VAR_TIME );
149 var_Create( p_input, "time-offset", VLC_VAR_TIME ); /* relative */
151 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
152 var_AddCallback( p_input, "time", TimeCallback, NULL );
153 var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
155 /* length variable */
156 var_Create( p_input, "length", VLC_VAR_TIME );
158 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
161 var_Create( p_input, "rate", VLC_VAR_INTEGER );
162 var_Create( p_input, "rate-slower", VLC_VAR_VOID );
163 var_Create( p_input, "rate-faster", VLC_VAR_VOID );
164 val.i_int = DEFAULT_RATE;
165 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
166 var_AddCallback( p_input, "rate", RateCallback, NULL );
167 var_AddCallback( p_input, "rate-slower", RateCallback, NULL );
168 var_AddCallback( p_input, "rate-faster", RateCallback, NULL );
171 var_Create( p_input, "state", VLC_VAR_INTEGER );
173 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
174 var_AddCallback( p_input, "state", StateCallback, NULL );
177 var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
178 val.b_bool = VLC_FALSE;
179 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
181 /* Initialize thread properties */
183 p_input->b_out_pace_control = VLC_FALSE;
184 p_input->p_sys = NULL;
187 p_input->psz_source = strdup( psz_uri );
193 p_input->p_es_out = NULL;
196 p_input->p_demux = NULL;
197 p_input->pf_demux = NULL;
198 p_input->pf_rewind = NULL;
199 p_input->pf_demux_control = demux_vaControlDefault;
200 p_input->i_cr_average = config_GetInt( p_input, "cr-average" );
203 p_input->p_access = NULL;
205 p_input->i_bufsize = 0;
207 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
209 /* Initialize statistics */
210 p_input->c_loops = 0;
211 p_input->stream.c_packets_read = 0;
212 p_input->stream.c_packets_trashed = 0;
215 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
216 vlc_cond_init( p_input, &p_input->stream.stream_wait );
217 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
219 /* Initialize stream description */
220 p_input->stream.b_changed = 0;
221 p_input->stream.i_es_number = 0;
222 p_input->stream.i_selected_es_number = 0;
223 p_input->stream.i_pgrm_number = 0;
224 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
225 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
226 p_input->stream.i_mux_rate = 0;
227 p_input->stream.b_seekable = 0;
228 p_input->stream.p_sout = NULL;
230 /* no stream, no program, no area, no es */
231 p_input->stream.p_new_program = NULL;
233 p_input->stream.i_area_nb = 0;
234 p_input->stream.pp_areas = NULL;
235 p_input->stream.p_selected_area = NULL;
236 p_input->stream.p_new_area = NULL;
238 p_input->stream.pp_selected_es = NULL;
239 p_input->stream.p_removed_es = NULL;
240 p_input->stream.p_newly_selected_es = NULL;
242 /* By default there is one area in a stream */
243 input_AddArea( p_input, 0, 1 );
244 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
246 /* Initialize stream control properties. */
247 p_input->stream.control.i_status = INIT_S;
248 p_input->stream.control.i_rate = DEFAULT_RATE;
249 p_input->stream.control.b_mute = 0;
250 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale");
252 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
255 var_Create( p_input, "bookmarks", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
256 var_Create( p_input, "bookmark", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE |
258 val.psz_string = _("Bookmark");
259 var_Change( p_input, "bookmark", VLC_VAR_SETTEXT, &val, NULL );
260 var_AddCallback( p_input, "bookmark", BookmarkCallback, NULL );
262 p_input->i_bookmarks = 0;
263 p_input->pp_bookmarks = NULL;
265 var_Get( p_input, "bookmarks", &val );
268 /* FIXME: have a common cfg parsing routine used by sout and others */
269 char *psz_parser, *psz_start, *psz_end;
270 psz_parser = val.psz_string;
271 while( (psz_start = strchr( psz_parser, '{' ) ) )
273 seekpoint_t seekpoint;
276 psz_end = strchr( psz_start, '}' );
277 if( !psz_end ) break;
278 psz_parser = psz_end + 1;
279 backup = *psz_parser;
283 seekpoint.psz_name = 0;
284 seekpoint.i_byte_offset = 0;
285 seekpoint.i_time_offset = 0;
286 while( (psz_end = strchr( psz_start, ',' ) ) )
289 if( !strncmp( psz_start, "name=", 5 ) )
291 seekpoint.psz_name = psz_start + 5;
293 else if( !strncmp( psz_start, "bytes=", 6 ) )
295 seekpoint.i_byte_offset = atol(psz_start + 6);
297 else if( !strncmp( psz_start, "time=", 5 ) )
299 seekpoint.i_time_offset = atol(psz_start + 5) * 1000000;
301 psz_start = psz_end + 1;
303 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
304 seekpoint.psz_name, seekpoint.i_byte_offset,
305 seekpoint.i_time_offset );
306 input_Control( p_input, INPUT_ADD_BOOKMARK, &seekpoint );
307 *psz_parser = backup;
309 free( val.psz_string );
312 /* Initialize input info */
313 p_input->stream.p_info = NULL;
314 p_input->stream.p_info = input_InfoCategory( p_input, _("General") );
315 input_AddInfo( p_input->stream.p_info, _("Playlist Item"),
316 p_input->psz_source );
317 vlc_object_attach( p_input, p_parent );
319 /* Create thread and wait for its readiness. */
320 if( vlc_thread_create( p_input, "input", RunThread,
321 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
323 msg_Err( p_input, "cannot create input thread" );
324 input_DelInfo( p_input );
332 /*****************************************************************************
333 * input_StopThread: mark an input thread as zombie
334 *****************************************************************************
335 * This function should not return until the thread is effectively cancelled.
336 *****************************************************************************/
337 void input_StopThread( input_thread_t *p_input )
339 /* Make the thread exit from a possible vlc_cond_wait() */
340 vlc_mutex_lock( &p_input->stream.stream_lock );
341 /* Request thread destruction */
344 vlc_cond_signal( &p_input->stream.stream_wait );
345 vlc_mutex_unlock( &p_input->stream.stream_lock );
348 /*****************************************************************************
349 * input_DestroyThread: mark an input thread as zombie
350 *****************************************************************************
351 * This function should not return until the thread is effectively cancelled.
352 *****************************************************************************/
353 void input_DestroyThread( input_thread_t *p_input )
355 /* Join the thread */
356 vlc_thread_join( p_input );
358 /* Destroy Mutex locks */
359 vlc_mutex_destroy( &p_input->stream.control.control_lock );
360 vlc_cond_destroy( &p_input->stream.stream_wait );
361 vlc_mutex_destroy( &p_input->stream.stream_lock );
364 /*****************************************************************************
365 * RunThread: main thread loop
366 *****************************************************************************
367 * Thread in charge of processing the network packets and demultiplexing.
368 *****************************************************************************/
369 static int RunThread( input_thread_t *p_input )
372 mtime_t i_update_next = -1;
374 /* Signal right now, otherwise we'll get stuck in a peek */
375 vlc_thread_ready( p_input );
377 if( InitThread( p_input ) )
379 /* If we failed, wait before we are killed, and exit */
380 p_input->b_error = 1;
382 ErrorThread( p_input );
384 /* Tell we're dead */
387 input_DelInfo( p_input );
392 /* initialization is complete */
393 vlc_mutex_lock( &p_input->stream.stream_lock );
394 p_input->stream.b_changed = 1;
395 p_input->stream.control.i_status = PLAYING_S;
396 vlc_mutex_unlock( &p_input->stream.stream_lock );
398 val.i_int = PLAYING_S;
399 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
401 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
403 unsigned int i, i_count;
407 vlc_mutex_lock( &p_input->stream.stream_lock );
409 if( p_input->stream.p_new_program )
411 if( p_input->pf_set_program != NULL )
414 /* Reinitialize buffer manager. */
415 input_AccessReinit( p_input );
417 p_input->pf_set_program( p_input,
418 p_input->stream.p_new_program );
420 /* Escape all decoders for the stream discontinuity they
422 input_EscapeDiscontinuity( p_input );
424 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
426 pgrm_descriptor_t * p_pgrm
427 = p_input->stream.pp_programs[i];
429 /* Reinitialize synchro. */
430 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
433 p_input->stream.p_new_program = NULL;
436 if( p_input->stream.p_new_area )
438 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
440 input_AccessReinit( p_input );
442 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
444 /* Escape all decoders for the stream discontinuity they
446 input_EscapeDiscontinuity( p_input );
448 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
450 pgrm_descriptor_t * p_pgrm
451 = p_input->stream.pp_programs[i];
453 /* Reinitialize synchro. */
454 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
457 p_input->stream.p_new_area = NULL;
460 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
462 if( p_input->stream.p_selected_area->i_size > 0 )
466 double f = (double)p_input->stream.p_selected_area->i_seek /
467 (double)p_input->stream.p_selected_area->i_size;
469 vlc_mutex_unlock( &p_input->stream.stream_lock );
470 demux_Control( p_input, DEMUX_SET_POSITION, f );
471 vlc_mutex_lock( &p_input->stream.stream_lock );
473 /* Escape all decoders for the stream discontinuity they
475 input_EscapeDiscontinuity( p_input );
477 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
479 pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
481 /* Reinitialize synchro. */
482 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
485 vlc_mutex_unlock( &p_input->stream.stream_lock );
486 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
491 /* Help in bar display */
493 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
496 for( i = 0; i < p_input->p_sys->i_sub; i++ )
498 subtitle_Seek( p_input->p_sys->sub[i], i_time );
501 vlc_mutex_lock( &p_input->stream.stream_lock );
503 p_input->stream.p_selected_area->i_seek = NO_SEEK;
506 if( p_input->stream.p_removed_es )
508 input_UnselectES( p_input, p_input->stream.p_removed_es );
509 p_input->stream.p_removed_es = NULL;
512 if( p_input->stream.p_newly_selected_es )
514 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
515 p_input->stream.p_newly_selected_es = NULL;
518 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
520 if( p_input->stream.b_new_mute )
522 input_EscapeAudioDiscontinuity( p_input );
525 vlc_mutex_lock( &p_input->stream.control.control_lock );
526 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
527 vlc_mutex_unlock( &p_input->stream.control.control_lock );
529 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
532 vlc_mutex_unlock( &p_input->stream.stream_lock );
534 /* Read and demultiplex some data. */
535 i_count = p_input->pf_demux( p_input );
541 var_Get( p_input, "input-repeat", &repeat );
542 if( repeat.i_int == 0 || p_input->stream.i_area_nb <= 0 )
544 /* End of file - we do not set b_die because only the
545 * playlist is allowed to do so. */
546 msg_Info( p_input, "EOF reached" );
551 msg_Dbg( p_input, "repeating the same input (%d)", repeat.i_int );
552 if( repeat.i_int > 0 )
555 var_Set( p_input, "input-repeat", repeat );
558 p_input->stream.p_new_area = p_input->stream.pp_areas[0];
559 p_input->stream.p_new_area->i_seek = 0;
562 else if( i_count < 0 )
564 p_input->b_error = 1;
567 if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
574 /* update input status variables */
575 if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
577 val.f_float = (float)d_pos;
578 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
580 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
583 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
585 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
587 val.i_time = i_length;
588 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
591 /* Check stop-time */
592 if( p_input->p_sys->i_stop_time > 0 && p_input->p_sys->i_stop_time < i_time )
594 msg_Warn( p_input, "EOF reached because of stop-time" );
599 for( i = 0; i < p_input->p_sys->i_sub; i++ )
601 subtitle_Demux( p_input->p_sys->sub[i], i_time );
604 i_update_next = mdate() + I64C(150000);
608 if( p_input->b_error || p_input->b_eof )
610 ErrorThread( p_input );
613 EndThread( p_input );
618 /*****************************************************************************
619 * InitThread: init the input Thread
620 *****************************************************************************/
621 static int InitThread( input_thread_t * p_input )
623 vlc_meta_t *p_meta = NULL, *p_meta_user = NULL;
626 playlist_t *p_playlist;
629 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
630 char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
632 int64_t i_microsecondperframe;
634 subtitle_demux_t *p_sub_toselect = NULL;
635 char *psz_sub_file = NULL;
637 /* Skip the plug-in names */
638 while( *psz_parser && *psz_parser != ':' )
642 #if defined( WIN32 ) || defined( UNDER_CE )
643 if( psz_parser - p_input->psz_dupsource == 1 )
645 msg_Warn( p_input, "drive letter %c: found in source string",
646 p_input->psz_dupsource[0] ) ;
653 p_input->psz_access = p_input->psz_demux = "";
654 p_input->psz_name = p_input->psz_source;
655 free( p_input->psz_dupsource );
656 p_input->psz_dupsource = NULL;
660 *psz_parser++ = '\0';
662 /* let's skip '//' */
663 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
668 p_input->psz_name = psz_parser ;
670 /* Come back to parse the access and demux plug-ins */
671 psz_parser = p_input->psz_dupsource;
676 p_input->psz_access = "";
678 else if( *psz_parser == '/' )
681 p_input->psz_access = "";
686 p_input->psz_access = psz_parser;
688 while( *psz_parser && *psz_parser != '/' )
693 if( *psz_parser == '/' )
695 *psz_parser++ = '\0';
702 p_input->psz_demux = "";
706 p_input->psz_demux = psz_parser;
710 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
711 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
713 if( input_AccessInit( p_input ) == -1 )
715 free( p_input->psz_source );
716 if( p_input->psz_dupsource != NULL )
718 free( p_input->psz_dupsource );
724 /* Initialize optional stream output. (before demuxer)*/
725 var_Get( p_input, "sout", &val );
726 if( val.psz_string != NULL )
728 if ( *val.psz_string && (p_input->stream.p_sout =
729 sout_NewInstance( p_input, val.psz_string )) == NULL )
731 msg_Err( p_input, "cannot start stream output instance, aborting" );
732 free( val.psz_string );
734 input_AccessEnd( p_input );
735 free( p_input->psz_source );
736 if( p_input->psz_dupsource != NULL )
738 free( p_input->psz_dupsource );
742 free( val.psz_string );
745 p_input->p_es_out = input_EsOutNew( p_input );
746 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
747 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
749 /* Find and open appropriate access module */
750 p_input->p_access = module_Need( p_input, "access",
751 p_input->psz_access, VLC_TRUE );
753 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
754 * are forbidden in filenames on Win32. */
756 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
757 if ( p_input->p_access == NULL
758 && (*p_input->psz_demux || *p_input->psz_access) )
760 p_input->psz_access = p_input->psz_demux = "";
761 p_input->psz_name = p_input->psz_source;
762 free( p_input->psz_dupsource);
763 p_input->psz_dupsource = NULL;
765 p_input->p_access = module_Need( p_input, "access",
766 p_input->psz_access, VLC_TRUE );
770 if( p_input->p_access == NULL )
772 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
773 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
774 if ( p_input->stream.p_sout != NULL )
776 sout_DeleteInstance( p_input->stream.p_sout );
779 input_AccessEnd( p_input );
780 free( p_input->psz_source );
781 if( p_input->psz_dupsource != NULL )
783 free( p_input->psz_dupsource );
785 input_EsOutDelete( p_input->p_es_out );
789 /* Waiting for stream. */
792 p_input->i_bufsize = p_input->i_mtu;
796 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
799 /* If the desynchronisation requested by the user is < 0, we need to
800 * cache more data. */
801 var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
802 var_Get( p_input, "audio-desync", &val );
804 p_input->i_pts_delay -= (val.i_int * 1000);
806 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
808 while( !input_FillBuffer( p_input ) )
810 if( p_input->b_die || p_input->b_error || p_input->b_eof )
812 module_Unneed( p_input, p_input->p_access );
813 if ( p_input->stream.p_sout != NULL )
815 sout_DeleteInstance( p_input->stream.p_sout );
817 input_AccessEnd( p_input );
818 free( p_input->psz_source );
819 if( p_input->psz_dupsource != NULL )
821 free( p_input->psz_dupsource );
823 input_EsOutDelete( p_input->p_es_out );
829 /* Create the stream_t facilities */
830 p_input->s = input_StreamNew( p_input );
831 if( p_input->s == NULL )
833 /* should never occur yet */
835 msg_Err( p_input, "cannot create stream_t" );
837 module_Unneed( p_input, p_input->p_access );
838 if ( p_input->stream.p_sout != NULL )
840 sout_DeleteInstance( p_input->stream.p_sout );
842 input_AccessEnd( p_input );
843 free( p_input->psz_source );
844 if( p_input->psz_dupsource != NULL )
846 free( p_input->psz_dupsource );
848 input_EsOutDelete( p_input->p_es_out );
852 /* Find and open appropriate demux module */
854 module_Need( p_input, "demux",
855 (p_input->psz_demux && *p_input->psz_demux) ?
856 p_input->psz_demux : "$demux",
857 (p_input->psz_demux && *p_input->psz_demux) ?
858 VLC_TRUE : VLC_FALSE );
860 if( p_input->p_demux == NULL )
862 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
863 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
865 input_StreamDelete( p_input->s );
866 module_Unneed( p_input, p_input->p_access );
867 if ( p_input->stream.p_sout != NULL )
869 sout_DeleteInstance( p_input->stream.p_sout );
871 input_AccessEnd( p_input );
872 free( p_input->psz_source );
873 if( p_input->psz_dupsource != NULL )
875 free( p_input->psz_dupsource );
877 input_EsOutDelete( p_input->p_es_out );
881 /* Init input_thread_sys_t */
882 p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
883 p_input->p_sys->i_sub = 0;
884 p_input->p_sys->sub = NULL;
886 p_input->p_sys->i_stop_time = 0;
888 /* Get meta information from user */
889 var_Create( p_input, "meta-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
890 var_Create( p_input, "meta-author", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
891 var_Create( p_input, "meta-artist", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
892 var_Create( p_input, "meta-genre", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
893 var_Create( p_input, "meta-copyright", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
894 var_Create( p_input, "meta-description", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
895 var_Create( p_input, "meta-date", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
896 var_Create( p_input, "meta-url", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
897 if( (p_meta_user = vlc_meta_New()) )
901 var_Get( p_input, "meta-title", &val );
902 if( val.psz_string && *val.psz_string )
903 vlc_meta_Add( p_meta_user, VLC_META_TITLE, val.psz_string );
904 if( val.psz_string ) free( val.psz_string );
905 var_Get( p_input, "meta-author", &val );
906 if( val.psz_string && *val.psz_string )
907 vlc_meta_Add( p_meta_user, VLC_META_AUTHOR, val.psz_string );
908 if( val.psz_string ) free( val.psz_string );
909 var_Get( p_input, "meta-artist", &val );
910 if( val.psz_string && *val.psz_string )
911 vlc_meta_Add( p_meta_user, VLC_META_ARTIST, val.psz_string );
912 if( val.psz_string ) free( val.psz_string );
913 var_Get( p_input, "meta-genre", &val );
914 if( val.psz_string && *val.psz_string )
915 vlc_meta_Add( p_meta_user, VLC_META_GENRE, val.psz_string );
916 if( val.psz_string ) free( val.psz_string );
917 var_Get( p_input, "meta-copyright", &val );
918 if( val.psz_string && *val.psz_string )
919 vlc_meta_Add( p_meta_user, VLC_META_COPYRIGHT, val.psz_string );
920 if( val.psz_string ) free( val.psz_string );
921 var_Get( p_input, "meta-description", &val );
922 if( val.psz_string && *val.psz_string )
923 vlc_meta_Add( p_meta_user, VLC_META_DESCRIPTION, val.psz_string );
924 if( val.psz_string ) free( val.psz_string );
925 var_Get( p_input, "meta-date", &val );
926 if( val.psz_string && *val.psz_string )
927 vlc_meta_Add( p_meta_user, VLC_META_DATE, val.psz_string );
928 if( val.psz_string ) free( val.psz_string );
929 var_Get( p_input, "meta-url", &val );
930 if( val.psz_string && *val.psz_string )
931 vlc_meta_Add( p_meta_user, VLC_META_URL, val.psz_string );
932 if( val.psz_string ) free( val.psz_string );
935 /* Get meta informations from demuxer */
936 if( !demux_Control( p_input, DEMUX_GET_META, &p_meta ) ||
937 ( p_meta_user && p_meta_user->i_meta ) )
939 playlist_t *p_playlist = (playlist_t *)vlc_object_find( p_input,
940 VLC_OBJECT_PLAYLIST, FIND_PARENT);
941 playlist_item_t *p_item = NULL;
942 input_info_category_t *p_cat;
945 /* Merge demux and user metadata */
946 if( !p_meta ){ p_meta = p_meta_user; p_meta_user = NULL; }
947 else if( p_meta && p_meta_user ) vlc_meta_Merge( p_meta, p_meta_user );
951 vlc_mutex_lock( &p_playlist->object_lock );
952 p_item = playlist_ItemGetByPos( p_playlist, -1 );
955 vlc_mutex_lock( &p_item->lock );
957 vlc_mutex_unlock( &p_playlist->object_lock );
960 msg_Dbg( p_input, "meta informations:" );
961 if( p_meta->i_meta > 0 )
963 p_cat = input_InfoCategory( p_input, _("File") );
964 for( i = 0; i < p_meta->i_meta; i++ )
966 msg_Dbg( p_input, " - '%s' = '%s'", _(p_meta->name[i]),
968 if( !strcmp( p_meta->name[i], VLC_META_TITLE ) )
970 playlist_ItemSetName( p_item, p_meta->value[i] );
972 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
974 playlist_ItemAddInfo( p_item, _("General"), _("Author"),
977 input_AddInfo( p_cat, _(p_meta->name[i]), "%s",
981 playlist_ItemAddInfo( p_item, _("File"),
982 _(p_meta->name[i]), "%s",
987 for( i = 0; i < p_meta->i_track; i++ )
989 vlc_meta_t *tk = p_meta->track[i];
992 msg_Dbg( p_input, " - track[%d]:", i );
995 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
996 sprintf( psz_cat, "%s %d", _("Stream"), i );
997 p_cat = input_InfoCategory( p_input, psz_cat );
999 for( j = 0; j < tk->i_meta; j++ )
1001 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1003 input_AddInfo( p_cat, _(tk->name[j]), "%s", tk->value[j] );
1006 playlist_ItemAddInfo( p_item, psz_cat, _(tk->name[j]),
1007 "%s", tk->value[j] );
1015 vlc_mutex_unlock( &p_item->lock );
1017 if( p_playlist ) vlc_object_release( p_playlist );
1019 if( p_input->stream.p_sout && p_input->stream.p_sout->p_meta == NULL )
1021 p_input->stream.p_sout->p_meta = p_meta;
1025 vlc_meta_Delete( p_meta );
1028 if( p_meta_user ) vlc_meta_Delete( p_meta_user );
1031 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) &&
1034 input_info_category_t *p_cat =
1035 input_InfoCategory( p_input, _("File") );
1037 (playlist_t*)vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1041 playlist_SetDuration( p_playlist, -1 , i_length );
1042 val.b_bool = p_playlist->i_index;
1043 var_Set( p_playlist, "item-change", val );
1044 vlc_object_release( p_playlist );
1048 char psz_buffer[MSTRTIME_MAX_SIZE];
1049 input_AddInfo( p_cat, _("Duration"),
1050 msecstotimestr( psz_buffer, i_length / 1000 ) );
1053 /* Set start time */
1054 var_Get( p_input, "start-time", &val );
1057 double f_pos = val.i_int * I64C(1000000) / (double)i_length;
1061 msg_Warn( p_input, "invalid start-time, ignored (start-time "
1062 ">= media length)" );
1066 p_input->stream.p_selected_area->i_seek =
1067 (int64_t)( f_pos * (double)p_input->stream.p_selected_area->i_size );
1069 msg_Dbg( p_input, "start-time %ds (%2.2f)", val.i_int, f_pos );
1074 /* Set stop-time and check validity */
1075 var_Get( p_input, "stop-time", &val );
1080 var_Get( p_input, "start-time", &start );
1081 if( start.i_int >= val.i_int )
1083 msg_Warn( p_input, "invalid stop-time, ignored (stop-time < "
1088 p_input->p_sys->i_stop_time = (int64_t)val.i_int * I64C(1000000);
1089 msg_Dbg( p_input, "stop-time %ds", val.i_int );
1094 if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
1099 i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
1102 /* Look for and add subtitle files */
1103 var_Get( p_input, "sub-file", &val );
1104 if( val.psz_string && *val.psz_string )
1106 subtitle_demux_t *p_sub;
1108 msg_Dbg( p_input, "force subtitle: %s", val.psz_string );
1109 if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string),
1110 i_microsecondperframe ) ) )
1112 p_sub_toselect = p_sub;
1113 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
1116 psz_sub_file = val.psz_string;
1118 var_Get( p_input, "sub-autodetect-file", &val );
1121 subtitle_demux_t *p_sub;
1123 char **tmp = subtitles_Detect( p_input, "", p_input->psz_name );
1125 for( i = 0; *tmp2 != NULL; i++ )
1127 if( psz_sub_file == NULL || strcmp( psz_sub_file, *tmp2 ) )
1129 if( ( p_sub = subtitle_New( p_input, *tmp2,
1130 i_microsecondperframe ) ) )
1132 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub,
1140 if( psz_sub_file ) free( psz_sub_file );
1142 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1143 val.b_bool = VLC_FALSE;
1144 if( p_input->stream.p_sout )
1146 var_Get( p_input, "sout-all", &val );
1148 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
1149 val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
1150 if( p_sub_toselect )
1152 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1153 p_sub_toselect->p_es, VLC_TRUE );
1156 if( p_input->stream.p_sout )
1158 if( p_input->stream.p_sout->i_out_pace_nocontrol > 0 )
1160 p_input->b_out_pace_control = VLC_FALSE;
1164 p_input->b_out_pace_control = VLC_TRUE;
1166 msg_Dbg( p_input, "starting in %s mode",
1167 p_input->b_out_pace_control ? "asynch" : "synch" );
1173 /*****************************************************************************
1174 * ErrorThread: RunThread() error loop
1175 *****************************************************************************
1176 * This function is called when an error occured during thread main's loop.
1177 *****************************************************************************/
1178 static void ErrorThread( input_thread_t *p_input )
1180 while( !p_input->b_die )
1183 msleep( INPUT_IDLE_SLEEP );
1187 /*****************************************************************************
1188 * EndThread: end the input thread
1189 *****************************************************************************/
1190 static void EndThread( input_thread_t * p_input )
1193 #ifdef HAVE_SYS_TIMES_H
1194 /* Display statistics */
1195 struct tms cpu_usage;
1196 times( &cpu_usage );
1198 msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
1199 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
1201 msg_Dbg( p_input, "%ld loops", p_input->c_loops );
1204 input_DumpStream( p_input );
1206 /* Free demultiplexer's data */
1207 if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
1209 /* Free all ES and destroy all decoder threads */
1210 input_EndStream( p_input );
1212 /* Close optional stream output instance */
1213 if( p_input->stream.p_sout )
1215 vlc_object_t *p_pl =
1216 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1219 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1221 /* attach sout to the playlist */
1222 msg_Warn( p_input, "keeping sout" );
1223 vlc_object_detach( p_input->stream.p_sout );
1224 vlc_object_attach( p_input->stream.p_sout, p_pl );
1228 msg_Warn( p_input, "destroying sout" );
1229 sout_DeleteInstance( p_input->stream.p_sout );
1233 vlc_object_release( p_pl );
1237 /* Destroy subtitles demuxers */
1238 if( p_input->p_sys )
1240 for( i = 0; i < p_input->p_sys->i_sub; i++ )
1242 subtitle_Close( p_input->p_sys->sub[i] );
1244 if( p_input->p_sys->i_sub > 0 )
1246 free( p_input->p_sys->sub );
1249 /* Free input_thread_sys_t */
1250 free( p_input->p_sys );
1253 /* Destroy the stream_t facilities */
1254 if( p_input->s ) input_StreamDelete( p_input->s );
1256 /* Destroy es out */
1257 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
1259 /* Close the access plug-in */
1260 if( p_input->p_access ) module_Unneed( p_input, p_input->p_access );
1262 input_AccessEnd( p_input );
1264 /* Free info structures XXX destroy es before 'cause vorbis */
1265 msg_Dbg( p_input, "freeing info structures...");
1266 input_DelInfo( p_input );
1268 free( p_input->psz_source );
1269 if( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
1271 /* Tell we're dead */
1272 p_input->b_dead = 1;
1275 /*****************************************************************************
1276 * ParseOption: parses the options for the input
1277 *****************************************************************************
1278 * This function parses the input (config) options and creates their associated
1280 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
1281 * bar is the value of the option.
1282 *****************************************************************************/
1283 static void ParseOption( input_thread_t *p_input, const char *psz_option )
1285 char *psz_name = (char *)psz_option;
1286 char *psz_value = strchr( psz_option, '=' );
1287 int i_name_len, i_type;
1288 vlc_bool_t b_isno = VLC_FALSE;
1291 if( psz_value ) i_name_len = psz_value - psz_option;
1292 else i_name_len = strlen( psz_option );
1294 /* It's too much of an hassle to remove the ':' when we parse
1295 * the cmd line :) */
1296 if( i_name_len && *psz_name == ':' )
1302 if( i_name_len == 0 ) return;
1304 psz_name = strndup( psz_name, i_name_len );
1305 if( psz_value ) psz_value++;
1307 i_type = config_GetType( p_input, psz_name );
1309 if( !i_type && !psz_value )
1311 /* check for "no-foo" or "nofoo" */
1312 if( !strncmp( psz_name, "no-", 3 ) )
1314 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1316 else if( !strncmp( psz_name, "no", 2 ) )
1318 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1320 else goto cleanup; /* Option doesn't exist */
1323 i_type = config_GetType( p_input, psz_name );
1325 if( !i_type ) goto cleanup; /* Option doesn't exist */
1327 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1329 if( ( i_type != VLC_VAR_BOOL ) &&
1330 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1332 /* Create the variable in the input object.
1333 * Children of the input object will be able to retreive this value
1334 * thanks to the inheritance property of the object variables. */
1335 var_Create( p_input, psz_name, i_type );
1340 val.b_bool = !b_isno;
1343 case VLC_VAR_INTEGER:
1344 val.i_int = atoi( psz_value );
1348 val.f_float = atof( psz_value );
1351 case VLC_VAR_STRING:
1352 case VLC_VAR_MODULE:
1354 case VLC_VAR_DIRECTORY:
1355 val.psz_string = psz_value;
1363 var_Set( p_input, psz_name, val );
1365 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
1368 if( psz_name ) free( psz_name );
1372 /*****************************************************************************
1373 * Callbacks (position, time, state, rate )
1374 *****************************************************************************/
1375 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1376 vlc_value_t oldval, vlc_value_t newval,
1379 input_thread_t *p_input = (input_thread_t *)p_this;
1381 msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
1382 oldval.f_float, newval.f_float );
1384 if( !strcmp( psz_cmd, "position-offset" ) )
1387 var_Get( p_input, "position", &val );
1389 newval.f_float += val.f_float;
1392 vlc_mutex_lock( &p_input->stream.stream_lock );
1393 p_input->stream.p_selected_area->i_seek =
1394 (int64_t)( newval.f_float *
1395 (double)p_input->stream.p_selected_area->i_size );
1397 if( p_input->stream.p_selected_area->i_seek < 0 )
1399 p_input->stream.p_selected_area->i_seek = 0;
1401 vlc_mutex_unlock( &p_input->stream.stream_lock );
1406 static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
1407 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1409 input_thread_t *p_input = (input_thread_t *)p_this;
1412 /* FIXME TODO FIXME */
1413 msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
1414 oldval.i_time, newval.i_time );
1416 var_Get( p_input, "length", &val );
1417 if( val.i_time > 0 )
1419 val.f_float = (double)newval.i_time / (double)val.i_time;
1420 if( !strcmp( psz_cmd, "time-offset" ) )
1422 var_Set( p_input, "position-offset", val );
1426 var_Set( p_input, "position", val );
1431 msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
1436 static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
1437 vlc_value_t oldval, vlc_value_t newval,
1440 input_thread_t *p_input = (input_thread_t *)p_this;
1442 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1443 psz_cmd, oldval.i_int, newval.i_int );
1445 switch( newval.i_int )
1448 input_SetStatus( p_input, INPUT_STATUS_PLAY );
1451 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1454 input_SetStatus( p_input, INPUT_STATUS_END );
1457 msg_Err( p_input, "cannot set new state (invalid)" );
1458 return VLC_EGENERIC;
1462 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
1463 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1465 input_thread_t *p_input = (input_thread_t *)p_this;
1467 if( !strcmp( psz_cmd, "rate-slower" ) )
1469 input_SetStatus( p_input, INPUT_STATUS_SLOWER );
1471 else if( !strcmp( psz_cmd, "rate-faster" ) )
1473 input_SetStatus( p_input, INPUT_STATUS_FASTER );
1477 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1478 psz_cmd, oldval.i_int, newval.i_int );
1479 input_SetRate( p_input, newval.i_int );
1484 static int BookmarkCallback( vlc_object_t *p_this, char const *psz_cmd,
1485 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1487 input_thread_t *p_input = (input_thread_t *)p_this;
1488 return input_Control( p_input, INPUT_SET_BOOKMARK, newval );