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 "stream_output.h"
42 #include "vlc_interface.h"
45 #include "../../modules/demux/util/sub.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 struct input_thread_sys_t
54 subtitle_demux_t **sub;
59 static int RunThread ( input_thread_t *p_input );
60 static int InitThread ( input_thread_t *p_input );
61 static void ErrorThread( input_thread_t *p_input );
62 static void EndThread ( input_thread_t *p_input );
64 static void ParseOption( input_thread_t *p_input, const char *psz_option );
66 static void DecodeUrl ( char * );
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 );
79 static int BookmarkCallback( vlc_object_t *p_this, char const *psz_cmd,
80 vlc_value_t oldval, vlc_value_t newval, void *p_data );
82 /*****************************************************************************
83 * input_CreateThread: creates a new input thread
84 *****************************************************************************
85 * This function creates a new input, and returns a pointer
86 * to its description. On error, it returns NULL.
87 *****************************************************************************/
88 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
89 input_item_t *p_item )
92 input_thread_t *p_input; /* thread descriptor */
96 /* Allocate descriptor */
97 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
100 msg_Err( p_parent, "out of memory" );
104 /* Store pointer to input item descriptor */
105 p_input->p_item = p_item;
107 /* Parse input options */
108 vlc_mutex_lock( &p_item->lock );
109 for( i = 0; i < p_item->i_options; i++ )
111 msg_Dbg( p_input, "option: %s", p_item->ppsz_options[i] );
112 ParseOption( p_input, p_item->ppsz_options[i] );
114 vlc_mutex_unlock( &p_item->lock );
116 /* Create a few object variables we'll need later on */
117 var_Create( p_input, "video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
118 var_Create( p_input, "audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
119 var_Create( p_input, "audio-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
120 var_Create( p_input, "spu-channel", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
121 var_Create( p_input, "sub-file", VLC_VAR_FILE | VLC_VAR_DOINHERIT );
122 var_Create( p_input, "sub-autodetect-file", VLC_VAR_BOOL |
124 var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER |
127 var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
128 var_Create( p_input, "sout-all", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
129 var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
130 var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
131 var_Create( p_input, "sout-keep", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
133 /* repeat variable */
134 var_Create( p_input, "input-repeat", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
136 /* start/stop time */
137 var_Create( p_input, "start-time", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
138 var_Create( p_input, "stop-time", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
141 var_Create( p_input, "minimize-threads", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
145 /* position variable */
146 var_Create( p_input, "position", VLC_VAR_FLOAT ); /* position 0.0->1.0 */
147 var_Create( p_input, "position-offset", VLC_VAR_FLOAT ); /* relative */
149 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
150 var_AddCallback( p_input, "position", PositionCallback, NULL );
151 var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
154 var_Create( p_input, "time", VLC_VAR_TIME );
155 var_Create( p_input, "time-offset", VLC_VAR_TIME ); /* relative */
157 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
158 var_AddCallback( p_input, "time", TimeCallback, NULL );
159 var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
161 /* length variable */
162 var_Create( p_input, "length", VLC_VAR_TIME );
164 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
167 var_Create( p_input, "rate", VLC_VAR_INTEGER );
168 var_Create( p_input, "rate-slower", VLC_VAR_VOID );
169 var_Create( p_input, "rate-faster", VLC_VAR_VOID );
170 val.i_int = DEFAULT_RATE;
171 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
172 var_AddCallback( p_input, "rate", RateCallback, NULL );
173 var_AddCallback( p_input, "rate-slower", RateCallback, NULL );
174 var_AddCallback( p_input, "rate-faster", RateCallback, NULL );
177 var_Create( p_input, "state", VLC_VAR_INTEGER );
179 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
180 var_AddCallback( p_input, "state", StateCallback, NULL );
183 var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
184 val.b_bool = VLC_FALSE;
185 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
187 /* Initialize thread properties */
189 p_input->b_out_pace_control = VLC_FALSE;
190 p_input->p_sys = NULL;
193 vlc_mutex_lock( &p_item->lock );
194 p_input->psz_source = strdup( p_item->psz_uri );
195 vlc_mutex_unlock( &p_item->lock );
201 p_input->p_es_out = NULL;
204 p_input->p_demux = NULL;
205 p_input->pf_demux = NULL;
206 p_input->pf_rewind = NULL;
207 p_input->pf_demux_control = demux_vaControlDefault;
208 p_input->i_cr_average = config_GetInt( p_input, "cr-average" );
211 p_input->p_access = NULL;
213 p_input->i_bufsize = 0;
215 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
217 /* Initialize statistics */
218 p_input->c_loops = 0;
219 p_input->stream.c_packets_read = 0;
220 p_input->stream.c_packets_trashed = 0;
223 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
224 vlc_cond_init( p_input, &p_input->stream.stream_wait );
225 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
227 /* Initialize stream description */
228 p_input->stream.b_changed = 0;
229 p_input->stream.i_es_number = 0;
230 p_input->stream.i_selected_es_number = 0;
231 p_input->stream.i_pgrm_number = 0;
232 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
233 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
234 p_input->stream.i_mux_rate = 0;
235 p_input->stream.b_seekable = 0;
236 p_input->stream.p_sout = NULL;
238 /* no stream, no program, no area, no es */
239 p_input->stream.p_new_program = NULL;
241 p_input->stream.i_area_nb = 0;
242 p_input->stream.pp_areas = NULL;
243 p_input->stream.p_selected_area = NULL;
244 p_input->stream.p_new_area = NULL;
246 p_input->stream.pp_selected_es = NULL;
247 p_input->stream.p_removed_es = NULL;
248 p_input->stream.p_newly_selected_es = NULL;
250 /* By default there is one area in a stream */
251 input_AddArea( p_input, 0, 1 );
252 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
254 /* Initialize stream control properties. */
255 p_input->stream.control.i_status = INIT_S;
256 p_input->stream.control.i_rate = DEFAULT_RATE;
257 p_input->stream.control.b_mute = 0;
258 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale");
260 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
263 var_Create( p_input, "bookmarks", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
264 var_Create( p_input, "bookmark", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE |
266 val.psz_string = _("Bookmark");
267 var_Change( p_input, "bookmark", VLC_VAR_SETTEXT, &val, NULL );
268 var_AddCallback( p_input, "bookmark", BookmarkCallback, NULL );
270 p_input->i_bookmarks = 0;
271 p_input->pp_bookmarks = NULL;
273 var_Get( p_input, "bookmarks", &val );
276 /* FIXME: have a common cfg parsing routine used by sout and others */
277 char *psz_parser, *psz_start, *psz_end;
278 psz_parser = val.psz_string;
279 while( (psz_start = strchr( psz_parser, '{' ) ) )
281 seekpoint_t seekpoint;
284 psz_end = strchr( psz_start, '}' );
285 if( !psz_end ) break;
286 psz_parser = psz_end + 1;
287 backup = *psz_parser;
291 seekpoint.psz_name = 0;
292 seekpoint.i_byte_offset = 0;
293 seekpoint.i_time_offset = 0;
294 while( (psz_end = strchr( psz_start, ',' ) ) )
297 if( !strncmp( psz_start, "name=", 5 ) )
299 seekpoint.psz_name = psz_start + 5;
301 else if( !strncmp( psz_start, "bytes=", 6 ) )
303 seekpoint.i_byte_offset = atoll(psz_start + 6);
305 else if( !strncmp( psz_start, "time=", 5 ) )
307 seekpoint.i_time_offset = atoll(psz_start + 5) * 1000000;
309 psz_start = psz_end + 1;
311 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
312 seekpoint.psz_name, seekpoint.i_byte_offset,
313 seekpoint.i_time_offset );
314 input_Control( p_input, INPUT_ADD_BOOKMARK, &seekpoint );
315 *psz_parser = backup;
317 free( val.psz_string );
320 vlc_object_attach( p_input, p_parent );
322 /* Create thread and wait for its readiness. */
323 if( vlc_thread_create( p_input, "input", RunThread,
324 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
326 msg_Err( p_input, "cannot create input thread" );
334 /*****************************************************************************
335 * input_StopThread: mark an input thread as zombie
336 *****************************************************************************
337 * This function should not return until the thread is effectively cancelled.
338 *****************************************************************************/
339 void input_StopThread( input_thread_t *p_input )
341 /* Make the thread exit from a possible vlc_cond_wait() */
342 vlc_mutex_lock( &p_input->stream.stream_lock );
343 /* Request thread destruction */
346 vlc_cond_signal( &p_input->stream.stream_wait );
347 vlc_mutex_unlock( &p_input->stream.stream_lock );
350 /*****************************************************************************
351 * input_DestroyThread: mark an input thread as zombie
352 *****************************************************************************
353 * This function should not return until the thread is effectively cancelled.
354 *****************************************************************************/
355 void input_DestroyThread( input_thread_t *p_input )
357 /* Join the thread */
358 vlc_thread_join( p_input );
360 /* Destroy Mutex locks */
361 vlc_mutex_destroy( &p_input->stream.control.control_lock );
362 vlc_cond_destroy( &p_input->stream.stream_wait );
363 vlc_mutex_destroy( &p_input->stream.stream_lock );
366 /*****************************************************************************
367 * RunThread: main thread loop
368 *****************************************************************************
369 * Thread in charge of processing the network packets and demultiplexing.
370 *****************************************************************************/
371 static int RunThread( input_thread_t *p_input )
374 mtime_t i_update_next = -1;
376 /* Signal right now, otherwise we'll get stuck in a peek */
377 vlc_thread_ready( p_input );
379 if( InitThread( p_input ) )
381 /* If we failed, wait before we are killed, and exit */
382 p_input->b_error = 1;
384 ErrorThread( p_input );
386 /* Tell we're dead */
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 if( !demux_Control( p_input, DEMUX_GET_POSITION, &f ) )
503 val.f_float = (float)f;
504 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
506 vlc_mutex_lock( &p_input->stream.stream_lock );
508 p_input->stream.p_selected_area->i_seek = NO_SEEK;
511 if( p_input->stream.p_removed_es )
513 input_UnselectES( p_input, p_input->stream.p_removed_es );
514 p_input->stream.p_removed_es = NULL;
517 if( p_input->stream.p_newly_selected_es )
519 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
520 p_input->stream.p_newly_selected_es = NULL;
523 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
525 if( p_input->stream.b_new_mute )
527 input_EscapeAudioDiscontinuity( p_input );
530 vlc_mutex_lock( &p_input->stream.control.control_lock );
531 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
532 vlc_mutex_unlock( &p_input->stream.control.control_lock );
534 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
537 vlc_mutex_unlock( &p_input->stream.stream_lock );
539 /* Read and demultiplex some data. */
540 i_count = p_input->pf_demux( p_input );
546 var_Get( p_input, "input-repeat", &repeat );
547 if( repeat.i_int == 0 || p_input->stream.i_area_nb <= 0 )
549 /* End of file - we do not set b_die because only the
550 * playlist is allowed to do so. */
551 msg_Info( p_input, "EOF reached" );
556 msg_Dbg( p_input, "repeating the same input (%d)", repeat.i_int );
557 if( repeat.i_int > 0 )
560 var_Set( p_input, "input-repeat", repeat );
563 p_input->stream.p_new_area = p_input->stream.pp_areas[0];
564 p_input->stream.p_new_area->i_seek = 0;
567 else if( i_count < 0 )
569 p_input->b_error = 1;
572 if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
579 /* update input status variables */
580 if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
582 val.f_float = (float)d_pos;
583 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
585 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
588 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
590 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
592 val.i_time = i_length;
593 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
596 /* Check stop-time */
597 if( p_input->p_sys->i_stop_time > 0 && p_input->p_sys->i_stop_time < i_time )
599 msg_Warn( p_input, "EOF reached because of stop-time" );
604 for( i = 0; i < p_input->p_sys->i_sub; i++ )
606 subtitle_Demux( p_input->p_sys->sub[i], i_time );
609 i_update_next = mdate() + I64C(150000);
613 if( p_input->b_error || p_input->b_eof )
615 ErrorThread( p_input );
618 EndThread( p_input );
623 /*****************************************************************************
624 * InitThread: init the input Thread
625 *****************************************************************************/
626 static int InitThread( input_thread_t * p_input )
628 vlc_meta_t *p_meta = NULL, *p_meta_user = NULL;
633 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
634 char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
636 int64_t i_microsecondperframe;
638 subtitle_demux_t *p_sub_toselect = NULL;
639 char *psz_sub_file = NULL;
641 /* Skip the plug-in names */
642 while( *psz_parser && *psz_parser != ':' )
646 #if defined( WIN32 ) || defined( UNDER_CE )
647 if( psz_parser - p_input->psz_dupsource == 1 )
649 msg_Warn( p_input, "drive letter %c: found in source string",
650 p_input->psz_dupsource[0] ) ;
657 p_input->psz_access = p_input->psz_demux = "";
658 p_input->psz_name = p_input->psz_source;
659 free( p_input->psz_dupsource );
660 p_input->psz_dupsource = NULL;
664 *psz_parser++ = '\0';
666 /* let's skip '//' */
667 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
672 p_input->psz_name = psz_parser ;
674 /* Come back to parse the access and demux plug-ins */
675 psz_parser = p_input->psz_dupsource;
680 p_input->psz_access = "";
682 else if( *psz_parser == '/' )
685 p_input->psz_access = "";
690 p_input->psz_access = psz_parser;
692 while( *psz_parser && *psz_parser != '/' )
697 if( *psz_parser == '/' )
699 *psz_parser++ = '\0';
706 p_input->psz_demux = "";
710 p_input->psz_demux = psz_parser;
714 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
715 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
717 if( input_AccessInit( p_input ) == -1 )
719 free( p_input->psz_source );
720 if( p_input->psz_dupsource != NULL )
722 free( p_input->psz_dupsource );
728 /* Initialize optional stream output. (before demuxer)*/
729 var_Get( p_input, "sout", &val );
730 if( val.psz_string != NULL )
732 if ( *val.psz_string && (p_input->stream.p_sout =
733 sout_NewInstance( p_input, val.psz_string )) == NULL )
735 msg_Err( p_input, "cannot start stream output instance, aborting" );
736 free( val.psz_string );
738 input_AccessEnd( p_input );
739 free( p_input->psz_source );
740 if( p_input->psz_dupsource != NULL )
742 free( p_input->psz_dupsource );
746 free( val.psz_string );
749 p_input->p_es_out = input_EsOutNew( p_input );
750 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
751 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
753 /* Find and open appropriate access module */
754 p_input->p_access = module_Need( p_input, "access",
755 p_input->psz_access, VLC_TRUE );
757 /* Maybe we had an encoded url */
758 if( !p_input->p_access && strchr( p_input->psz_name, '%' ) )
760 DecodeUrl( p_input->psz_name );
762 msg_Dbg( p_input, "retying with %s", p_input->psz_name );
763 p_input->p_access = module_Need( p_input, "access",
764 p_input->psz_access, VLC_TRUE );
766 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
767 * are forbidden in filenames on Win32. */
769 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
770 if ( p_input->p_access == NULL
771 && (*p_input->psz_demux || *p_input->psz_access) )
773 p_input->psz_access = p_input->psz_demux = "";
774 p_input->psz_name = p_input->psz_source;
775 free( p_input->psz_dupsource);
776 p_input->psz_dupsource = NULL;
778 p_input->p_access = module_Need( p_input, "access",
779 p_input->psz_access, VLC_TRUE );
782 if( p_input->p_access == NULL )
784 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
785 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
786 if ( p_input->stream.p_sout != NULL )
788 sout_DeleteInstance( p_input->stream.p_sout );
791 input_AccessEnd( p_input );
792 free( p_input->psz_source );
793 if( p_input->psz_dupsource != NULL )
795 free( p_input->psz_dupsource );
797 input_EsOutDelete( p_input->p_es_out );
801 /* Waiting for stream. */
804 p_input->i_bufsize = p_input->i_mtu;
808 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
811 /* If the desynchronisation requested by the user is < 0, we need to
812 * cache more data. */
813 var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
814 var_Get( p_input, "audio-desync", &val );
816 p_input->i_pts_delay -= (val.i_int * 1000);
818 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
820 while( !input_FillBuffer( p_input ) )
822 if( p_input->b_die || p_input->b_error || p_input->b_eof )
824 module_Unneed( p_input, p_input->p_access );
825 if ( p_input->stream.p_sout != NULL )
827 sout_DeleteInstance( p_input->stream.p_sout );
829 input_AccessEnd( p_input );
830 free( p_input->psz_source );
831 if( p_input->psz_dupsource != NULL )
833 free( p_input->psz_dupsource );
835 input_EsOutDelete( p_input->p_es_out );
841 /* Create the stream_t facilities */
842 p_input->s = input_StreamNew( p_input );
843 if( p_input->s == NULL )
845 /* should never occur yet */
847 msg_Err( p_input, "cannot create stream_t" );
849 module_Unneed( p_input, p_input->p_access );
850 if ( p_input->stream.p_sout != NULL )
852 sout_DeleteInstance( p_input->stream.p_sout );
854 input_AccessEnd( p_input );
855 free( p_input->psz_source );
856 if( p_input->psz_dupsource != NULL )
858 free( p_input->psz_dupsource );
860 input_EsOutDelete( p_input->p_es_out );
864 /* Find and open appropriate demux module */
866 module_Need( p_input, "demux",
867 (p_input->psz_demux && *p_input->psz_demux) ?
868 p_input->psz_demux : "$demux",
869 (p_input->psz_demux && *p_input->psz_demux) ?
870 VLC_TRUE : VLC_FALSE );
872 if( p_input->p_demux == NULL )
874 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
875 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
877 input_StreamDelete( p_input->s );
878 module_Unneed( p_input, p_input->p_access );
879 if ( p_input->stream.p_sout != NULL )
881 sout_DeleteInstance( p_input->stream.p_sout );
883 input_AccessEnd( p_input );
884 free( p_input->psz_source );
885 if( p_input->psz_dupsource != NULL )
887 free( p_input->psz_dupsource );
889 input_EsOutDelete( p_input->p_es_out );
893 /* Init input_thread_sys_t */
894 p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
895 p_input->p_sys->i_sub = 0;
896 p_input->p_sys->sub = NULL;
898 p_input->p_sys->i_stop_time = 0;
900 /* Get meta information from user */
901 var_Create( p_input, "meta-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
902 var_Create( p_input, "meta-author", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
903 var_Create( p_input, "meta-artist", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
904 var_Create( p_input, "meta-genre", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
905 var_Create( p_input, "meta-copyright", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
906 var_Create( p_input, "meta-description", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
907 var_Create( p_input, "meta-date", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
908 var_Create( p_input, "meta-url", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
909 if( (p_meta_user = vlc_meta_New()) )
913 var_Get( p_input, "meta-title", &val );
914 if( val.psz_string && *val.psz_string )
915 vlc_meta_Add( p_meta_user, VLC_META_TITLE, val.psz_string );
916 if( val.psz_string ) free( val.psz_string );
917 var_Get( p_input, "meta-author", &val );
918 if( val.psz_string && *val.psz_string )
919 vlc_meta_Add( p_meta_user, VLC_META_AUTHOR, val.psz_string );
920 if( val.psz_string ) free( val.psz_string );
921 var_Get( p_input, "meta-artist", &val );
922 if( val.psz_string && *val.psz_string )
923 vlc_meta_Add( p_meta_user, VLC_META_ARTIST, val.psz_string );
924 if( val.psz_string ) free( val.psz_string );
925 var_Get( p_input, "meta-genre", &val );
926 if( val.psz_string && *val.psz_string )
927 vlc_meta_Add( p_meta_user, VLC_META_GENRE, val.psz_string );
928 if( val.psz_string ) free( val.psz_string );
929 var_Get( p_input, "meta-copyright", &val );
930 if( val.psz_string && *val.psz_string )
931 vlc_meta_Add( p_meta_user, VLC_META_COPYRIGHT, val.psz_string );
932 if( val.psz_string ) free( val.psz_string );
933 var_Get( p_input, "meta-description", &val );
934 if( val.psz_string && *val.psz_string )
935 vlc_meta_Add( p_meta_user, VLC_META_DESCRIPTION, val.psz_string );
936 if( val.psz_string ) free( val.psz_string );
937 var_Get( p_input, "meta-date", &val );
938 if( val.psz_string && *val.psz_string )
939 vlc_meta_Add( p_meta_user, VLC_META_DATE, val.psz_string );
940 if( val.psz_string ) free( val.psz_string );
941 var_Get( p_input, "meta-url", &val );
942 if( val.psz_string && *val.psz_string )
943 vlc_meta_Add( p_meta_user, VLC_META_URL, val.psz_string );
944 if( val.psz_string ) free( val.psz_string );
947 /* Get meta informations from demuxer */
948 if( !demux_Control( p_input, DEMUX_GET_META, &p_meta ) ||
949 ( p_meta_user && p_meta_user->i_meta ) )
953 /* Merge demux and user metadata */
954 if( !p_meta ){ p_meta = p_meta_user; p_meta_user = NULL; }
955 else if( p_meta && p_meta_user ) vlc_meta_Merge( p_meta, p_meta_user );
957 msg_Dbg( p_input, "meta informations:" );
958 if( p_meta->i_meta > 0 )
960 for( i = 0; i < p_meta->i_meta; i++ )
962 msg_Dbg( p_input, " - '%s' = '%s'", _(p_meta->name[i]),
964 if( !strcmp( p_meta->name[i], VLC_META_TITLE ) &&
966 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
968 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
969 input_Control( p_input, INPUT_ADD_INFO, _("General"),
970 _("Author"), p_meta->value[i] );
972 input_Control( p_input, INPUT_ADD_INFO, _("File"),
973 _(p_meta->name[i]), "%s", p_meta->value[i] );
976 for( i = 0; i < p_meta->i_track; i++ )
978 vlc_meta_t *tk = p_meta->track[i];
981 msg_Dbg( p_input, " - track[%d]:", i );
984 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
985 sprintf( psz_cat, "%s %d", _("Stream"), i );
987 for( j = 0; j < tk->i_meta; j++ )
989 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
992 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
993 _(tk->name[j]), "%s", tk->value[j] );
998 if( p_input->stream.p_sout && p_input->stream.p_sout->p_meta == NULL )
1000 p_input->stream.p_sout->p_meta = p_meta;
1004 vlc_meta_Delete( p_meta );
1007 if( p_meta_user ) vlc_meta_Delete( p_meta_user );
1010 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) &&
1013 char psz_buffer[MSTRTIME_MAX_SIZE];
1015 vlc_mutex_lock( &p_input->p_item->lock );
1016 p_input->p_item->i_duration = i_length;
1017 vlc_mutex_unlock( &p_input->p_item->lock );
1019 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1020 msecstotimestr( psz_buffer, i_length / 1000 ) );
1022 /* Set start time */
1023 var_Get( p_input, "start-time", &val );
1026 double f_pos = val.i_int * I64C(1000000) / (double)i_length;
1030 msg_Warn( p_input, "invalid start-time, ignored (start-time "
1031 ">= media length)" );
1035 p_input->stream.p_selected_area->i_seek =
1036 (int64_t)( f_pos * (double)p_input->stream.p_selected_area->i_size );
1038 msg_Dbg( p_input, "start-time %ds (%2.2f)", val.i_int, f_pos );
1043 /* Set stop-time and check validity */
1044 var_Get( p_input, "stop-time", &val );
1049 var_Get( p_input, "start-time", &start );
1050 if( start.i_int >= val.i_int )
1052 msg_Warn( p_input, "invalid stop-time, ignored (stop-time < "
1057 p_input->p_sys->i_stop_time = (int64_t)val.i_int * I64C(1000000);
1058 msg_Dbg( p_input, "stop-time %ds", val.i_int );
1063 if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
1065 i_microsecondperframe = 0;
1069 i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
1072 /* Look for and add subtitle files */
1073 var_Get( p_input, "sub-file", &val );
1074 if( val.psz_string && *val.psz_string )
1076 subtitle_demux_t *p_sub;
1078 msg_Dbg( p_input, "force subtitle: %s", val.psz_string );
1079 if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string),
1080 i_microsecondperframe ) ) )
1082 p_sub_toselect = p_sub;
1083 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
1086 psz_sub_file = val.psz_string;
1088 var_Get( p_input, "sub-autodetect-file", &val );
1091 subtitle_demux_t *p_sub;
1093 char **tmp = subtitles_Detect( p_input, "", p_input->psz_name );
1095 for( i = 0; *tmp2 != NULL; i++ )
1097 if( psz_sub_file == NULL || strcmp( psz_sub_file, *tmp2 ) )
1099 if( ( p_sub = subtitle_New( p_input, *tmp2,
1100 i_microsecondperframe ) ) )
1102 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub,
1110 if( psz_sub_file ) free( psz_sub_file );
1112 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1113 val.b_bool = VLC_FALSE;
1114 if( p_input->stream.p_sout )
1116 var_Get( p_input, "sout-all", &val );
1118 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
1119 val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
1120 if( p_sub_toselect )
1122 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1123 p_sub_toselect->p_es, VLC_TRUE );
1126 if( p_input->stream.p_sout )
1128 if( p_input->stream.p_sout->i_out_pace_nocontrol > 0 )
1130 p_input->b_out_pace_control = VLC_FALSE;
1134 p_input->b_out_pace_control = VLC_TRUE;
1136 msg_Dbg( p_input, "starting in %s mode",
1137 p_input->b_out_pace_control ? "asynch" : "synch" );
1143 /*****************************************************************************
1144 * ErrorThread: RunThread() error loop
1145 *****************************************************************************
1146 * This function is called when an error occured during thread main's loop.
1147 *****************************************************************************/
1148 static void ErrorThread( input_thread_t *p_input )
1150 while( !p_input->b_die )
1153 msleep( INPUT_IDLE_SLEEP );
1157 /*****************************************************************************
1158 * EndThread: end the input thread
1159 *****************************************************************************/
1160 static void EndThread( input_thread_t * p_input )
1163 #ifdef HAVE_SYS_TIMES_H
1164 /* Display statistics */
1165 struct tms cpu_usage;
1166 times( &cpu_usage );
1168 msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
1169 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
1171 msg_Dbg( p_input, "%ld loops", p_input->c_loops );
1174 input_DumpStream( p_input );
1176 /* Free demultiplexer's data */
1177 if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
1179 /* Free all ES and destroy all decoder threads */
1180 input_EndStream( p_input );
1182 /* Close optional stream output instance */
1183 if( p_input->stream.p_sout )
1185 vlc_object_t *p_pl =
1186 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1189 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1191 /* attach sout to the playlist */
1192 msg_Warn( p_input, "keeping sout" );
1193 vlc_object_detach( p_input->stream.p_sout );
1194 vlc_object_attach( p_input->stream.p_sout, p_pl );
1198 msg_Warn( p_input, "destroying sout" );
1199 sout_DeleteInstance( p_input->stream.p_sout );
1203 vlc_object_release( p_pl );
1207 /* Destroy subtitles demuxers */
1208 if( p_input->p_sys )
1210 for( i = 0; i < p_input->p_sys->i_sub; i++ )
1212 subtitle_Close( p_input->p_sys->sub[i] );
1214 if( p_input->p_sys->i_sub > 0 )
1216 free( p_input->p_sys->sub );
1219 /* Free input_thread_sys_t */
1220 free( p_input->p_sys );
1223 /* Destroy the stream_t facilities */
1224 if( p_input->s ) input_StreamDelete( p_input->s );
1226 /* Destroy es out */
1227 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
1229 /* Close the access plug-in */
1230 if( p_input->p_access ) module_Unneed( p_input, p_input->p_access );
1232 input_AccessEnd( p_input );
1234 /* Free info structures XXX destroy es before 'cause vorbis */
1235 msg_Dbg( p_input, "freeing info structures...");
1237 free( p_input->psz_source );
1238 if( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
1240 /* Tell we're dead */
1241 p_input->b_dead = 1;
1243 /*****************************************************************************
1244 * DecodeUrl: decode a given encoded url
1245 *****************************************************************************/
1246 static void DecodeUrl( char *psz )
1248 char *dup = strdup( psz );
1266 *psz++ = strtol( val, NULL, 16 );
1268 else if( *p == '+' )
1282 /*****************************************************************************
1283 * ParseOption: parses the options for the input
1284 *****************************************************************************
1285 * This function parses the input (config) options and creates their associated
1287 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
1288 * bar is the value of the option.
1289 *****************************************************************************/
1290 static void ParseOption( input_thread_t *p_input, const char *psz_option )
1292 char *psz_name = (char *)psz_option;
1293 char *psz_value = strchr( psz_option, '=' );
1294 int i_name_len, i_type;
1295 vlc_bool_t b_isno = VLC_FALSE;
1298 if( psz_value ) i_name_len = psz_value - psz_option;
1299 else i_name_len = strlen( psz_option );
1301 /* It's too much of an hassle to remove the ':' when we parse
1302 * the cmd line :) */
1303 if( i_name_len && *psz_name == ':' )
1309 if( i_name_len == 0 ) return;
1311 psz_name = strndup( psz_name, i_name_len );
1312 if( psz_value ) psz_value++;
1314 i_type = config_GetType( p_input, psz_name );
1316 if( !i_type && !psz_value )
1318 /* check for "no-foo" or "nofoo" */
1319 if( !strncmp( psz_name, "no-", 3 ) )
1321 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1323 else if( !strncmp( psz_name, "no", 2 ) )
1325 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1327 else goto cleanup; /* Option doesn't exist */
1330 i_type = config_GetType( p_input, psz_name );
1332 if( !i_type ) goto cleanup; /* Option doesn't exist */
1334 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1336 if( ( i_type != VLC_VAR_BOOL ) &&
1337 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1339 /* Create the variable in the input object.
1340 * Children of the input object will be able to retreive this value
1341 * thanks to the inheritance property of the object variables. */
1342 var_Create( p_input, psz_name, i_type );
1347 val.b_bool = !b_isno;
1350 case VLC_VAR_INTEGER:
1351 val.i_int = atoi( psz_value );
1355 val.f_float = atof( psz_value );
1358 case VLC_VAR_STRING:
1359 case VLC_VAR_MODULE:
1361 case VLC_VAR_DIRECTORY:
1362 val.psz_string = psz_value;
1370 var_Set( p_input, psz_name, val );
1372 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
1375 if( psz_name ) free( psz_name );
1379 /*****************************************************************************
1380 * Callbacks (position, time, state, rate )
1381 *****************************************************************************/
1382 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1383 vlc_value_t oldval, vlc_value_t newval,
1386 input_thread_t *p_input = (input_thread_t *)p_this;
1388 msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
1389 oldval.f_float, newval.f_float );
1391 if( !strcmp( psz_cmd, "position-offset" ) )
1394 var_Get( p_input, "position", &val );
1396 newval.f_float += val.f_float;
1399 vlc_mutex_lock( &p_input->stream.stream_lock );
1400 p_input->stream.p_selected_area->i_seek =
1401 (int64_t)( newval.f_float *
1402 (double)p_input->stream.p_selected_area->i_size );
1404 if( p_input->stream.p_selected_area->i_seek < 0 )
1406 p_input->stream.p_selected_area->i_seek = 0;
1408 vlc_mutex_unlock( &p_input->stream.stream_lock );
1413 static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
1414 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1416 input_thread_t *p_input = (input_thread_t *)p_this;
1419 /* FIXME TODO FIXME */
1420 msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
1421 oldval.i_time, newval.i_time );
1423 var_Get( p_input, "length", &val );
1424 if( val.i_time > 0 )
1426 val.f_float = (double)newval.i_time / (double)val.i_time;
1427 if( !strcmp( psz_cmd, "time-offset" ) )
1429 var_Set( p_input, "position-offset", val );
1433 var_Set( p_input, "position", val );
1438 msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
1443 static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
1444 vlc_value_t oldval, vlc_value_t newval,
1447 input_thread_t *p_input = (input_thread_t *)p_this;
1449 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1450 psz_cmd, oldval.i_int, newval.i_int );
1452 switch( newval.i_int )
1455 input_SetStatus( p_input, INPUT_STATUS_PLAY );
1458 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1461 input_SetStatus( p_input, INPUT_STATUS_END );
1464 msg_Err( p_input, "cannot set new state (invalid)" );
1465 return VLC_EGENERIC;
1469 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
1470 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1472 input_thread_t *p_input = (input_thread_t *)p_this;
1474 if( !strcmp( psz_cmd, "rate-slower" ) )
1476 input_SetStatus( p_input, INPUT_STATUS_SLOWER );
1478 else if( !strcmp( psz_cmd, "rate-faster" ) )
1480 input_SetStatus( p_input, INPUT_STATUS_FASTER );
1484 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1485 psz_cmd, oldval.i_int, newval.i_int );
1486 input_SetRate( p_input, newval.i_int );
1491 static int BookmarkCallback( vlc_object_t *p_this, char const *psz_cmd,
1492 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1494 input_thread_t *p_input = (input_thread_t *)p_this;
1495 return input_Control( p_input, INPUT_SET_BOOKMARK, newval );