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-path", VLC_VAR_STRING |
126 var_Create( p_input, "sub-autodetect-fuzzy", VLC_VAR_INTEGER |
129 var_Create( p_input, "sout", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
130 var_Create( p_input, "sout-all", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
131 var_Create( p_input, "sout-audio", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
132 var_Create( p_input, "sout-video", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
133 var_Create( p_input, "sout-keep", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
135 /* repeat variable */
136 var_Create( p_input, "input-repeat", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
138 /* start/stop time */
139 var_Create( p_input, "start-time", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
140 var_Create( p_input, "stop-time", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
143 var_Create( p_input, "minimize-threads", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
147 /* position variable */
148 var_Create( p_input, "position", VLC_VAR_FLOAT ); /* position 0.0->1.0 */
149 var_Create( p_input, "position-offset", VLC_VAR_FLOAT ); /* relative */
151 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
152 var_AddCallback( p_input, "position", PositionCallback, NULL );
153 var_AddCallback( p_input, "position-offset", PositionCallback, NULL );
156 var_Create( p_input, "time", VLC_VAR_TIME );
157 var_Create( p_input, "time-offset", VLC_VAR_TIME ); /* relative */
159 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
160 var_AddCallback( p_input, "time", TimeCallback, NULL );
161 var_AddCallback( p_input, "time-offset", TimeCallback, NULL );
163 /* length variable */
164 var_Create( p_input, "length", VLC_VAR_TIME );
166 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
169 var_Create( p_input, "rate", VLC_VAR_INTEGER );
170 var_Create( p_input, "rate-slower", VLC_VAR_VOID );
171 var_Create( p_input, "rate-faster", VLC_VAR_VOID );
172 val.i_int = DEFAULT_RATE;
173 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
174 var_AddCallback( p_input, "rate", RateCallback, NULL );
175 var_AddCallback( p_input, "rate-slower", RateCallback, NULL );
176 var_AddCallback( p_input, "rate-faster", RateCallback, NULL );
179 var_Create( p_input, "state", VLC_VAR_INTEGER );
181 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
182 var_AddCallback( p_input, "state", StateCallback, NULL );
185 var_Create( p_input, "demuxed-id3", VLC_VAR_BOOL );
186 val.b_bool = VLC_FALSE;
187 var_Change( p_input, "demuxed-id3", VLC_VAR_SETVALUE, &val, NULL );
189 /* Initialize thread properties */
191 p_input->b_out_pace_control = VLC_FALSE;
192 p_input->p_sys = NULL;
195 vlc_mutex_lock( &p_item->lock );
196 p_input->psz_source = strdup( p_item->psz_uri );
197 vlc_mutex_unlock( &p_item->lock );
203 p_input->p_es_out = NULL;
206 p_input->p_demux = NULL;
207 p_input->pf_demux = NULL;
208 p_input->pf_rewind = NULL;
209 p_input->pf_demux_control = demux_vaControlDefault;
210 p_input->i_cr_average = config_GetInt( p_input, "cr-average" );
213 p_input->p_access = NULL;
215 p_input->i_bufsize = 0;
217 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
219 /* Initialize statistics */
220 p_input->c_loops = 0;
221 p_input->stream.c_packets_read = 0;
222 p_input->stream.c_packets_trashed = 0;
225 vlc_mutex_init( p_input, &p_input->stream.stream_lock );
226 vlc_cond_init( p_input, &p_input->stream.stream_wait );
227 vlc_mutex_init( p_input, &p_input->stream.control.control_lock );
229 /* Initialize stream description */
230 p_input->stream.b_changed = 0;
231 p_input->stream.i_es_number = 0;
232 p_input->stream.i_selected_es_number = 0;
233 p_input->stream.i_pgrm_number = 0;
234 p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
235 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
236 p_input->stream.i_mux_rate = 0;
237 p_input->stream.b_seekable = 0;
238 p_input->stream.p_sout = NULL;
240 /* no stream, no program, no area, no es */
241 p_input->stream.p_new_program = NULL;
243 p_input->stream.i_area_nb = 0;
244 p_input->stream.pp_areas = NULL;
245 p_input->stream.p_selected_area = NULL;
246 p_input->stream.p_new_area = NULL;
248 p_input->stream.pp_selected_es = NULL;
249 p_input->stream.p_removed_es = NULL;
250 p_input->stream.p_newly_selected_es = NULL;
252 /* By default there is one area in a stream */
253 input_AddArea( p_input, 0, 1 );
254 p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
256 /* Initialize stream control properties. */
257 p_input->stream.control.i_status = INIT_S;
258 p_input->stream.control.i_rate = DEFAULT_RATE;
259 p_input->stream.control.b_mute = 0;
260 p_input->stream.control.b_grayscale = config_GetInt( p_input, "grayscale");
262 msg_Info( p_input, "playlist item `%s'", p_input->psz_source );
265 var_Create( p_input, "bookmarks", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
266 var_Create( p_input, "bookmark", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE |
268 val.psz_string = _("Bookmark");
269 var_Change( p_input, "bookmark", VLC_VAR_SETTEXT, &val, NULL );
270 var_AddCallback( p_input, "bookmark", BookmarkCallback, NULL );
272 p_input->i_bookmarks = 0;
273 p_input->pp_bookmarks = NULL;
275 var_Get( p_input, "bookmarks", &val );
278 /* FIXME: have a common cfg parsing routine used by sout and others */
279 char *psz_parser, *psz_start, *psz_end;
280 psz_parser = val.psz_string;
281 while( (psz_start = strchr( psz_parser, '{' ) ) )
283 seekpoint_t seekpoint;
286 psz_end = strchr( psz_start, '}' );
287 if( !psz_end ) break;
288 psz_parser = psz_end + 1;
289 backup = *psz_parser;
293 seekpoint.psz_name = 0;
294 seekpoint.i_byte_offset = 0;
295 seekpoint.i_time_offset = 0;
296 while( (psz_end = strchr( psz_start, ',' ) ) )
299 if( !strncmp( psz_start, "name=", 5 ) )
301 seekpoint.psz_name = psz_start + 5;
303 else if( !strncmp( psz_start, "bytes=", 6 ) )
305 seekpoint.i_byte_offset = atoll(psz_start + 6);
307 else if( !strncmp( psz_start, "time=", 5 ) )
309 seekpoint.i_time_offset = atoll(psz_start + 5) * 1000000;
311 psz_start = psz_end + 1;
313 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
314 seekpoint.psz_name, seekpoint.i_byte_offset,
315 seekpoint.i_time_offset );
316 input_Control( p_input, INPUT_ADD_BOOKMARK, &seekpoint );
317 *psz_parser = backup;
319 free( val.psz_string );
322 vlc_object_attach( p_input, p_parent );
324 /* Create thread and wait for its readiness. */
325 if( vlc_thread_create( p_input, "input", RunThread,
326 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
328 msg_Err( p_input, "cannot create input thread" );
336 /*****************************************************************************
337 * input_StopThread: mark an input thread as zombie
338 *****************************************************************************
339 * This function should not return until the thread is effectively cancelled.
340 *****************************************************************************/
341 void input_StopThread( input_thread_t *p_input )
345 /* Make the thread exit from a possible vlc_cond_wait() */
346 vlc_mutex_lock( &p_input->stream.stream_lock );
348 /* Request thread destruction */
350 /* Temporary demux2 hack */
351 p_demux = (demux_t *)vlc_object_find( p_input, VLC_OBJECT_DEMUX,
356 vlc_object_release( p_demux );
361 vlc_cond_signal( &p_input->stream.stream_wait );
362 vlc_mutex_unlock( &p_input->stream.stream_lock );
365 /*****************************************************************************
366 * input_DestroyThread: mark an input thread as zombie
367 *****************************************************************************
368 * This function should not return until the thread is effectively cancelled.
369 *****************************************************************************/
370 void input_DestroyThread( input_thread_t *p_input )
372 /* Join the thread */
373 vlc_thread_join( p_input );
375 /* Destroy Mutex locks */
376 vlc_mutex_destroy( &p_input->stream.control.control_lock );
377 vlc_cond_destroy( &p_input->stream.stream_wait );
378 vlc_mutex_destroy( &p_input->stream.stream_lock );
381 /*****************************************************************************
382 * RunThread: main thread loop
383 *****************************************************************************
384 * Thread in charge of processing the network packets and demultiplexing.
385 *****************************************************************************/
386 static int RunThread( input_thread_t *p_input )
389 mtime_t i_update_next = -1;
391 /* Signal right now, otherwise we'll get stuck in a peek */
392 vlc_thread_ready( p_input );
394 if( InitThread( p_input ) )
396 /* If we failed, wait before we are killed, and exit */
397 p_input->b_error = 1;
399 ErrorThread( p_input );
401 /* Tell we're dead */
407 /* initialization is complete */
408 vlc_mutex_lock( &p_input->stream.stream_lock );
409 p_input->stream.b_changed = 1;
410 p_input->stream.control.i_status = PLAYING_S;
411 vlc_mutex_unlock( &p_input->stream.stream_lock );
413 val.i_int = PLAYING_S;
414 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
416 while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
418 unsigned int i, i_count;
422 vlc_mutex_lock( &p_input->stream.stream_lock );
424 if( p_input->stream.p_new_program )
426 if( p_input->pf_set_program != NULL )
429 /* Reinitialize buffer manager. */
430 input_AccessReinit( p_input );
432 p_input->pf_set_program( p_input,
433 p_input->stream.p_new_program );
435 /* Escape all decoders for the stream discontinuity they
437 input_EscapeDiscontinuity( p_input );
439 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
441 pgrm_descriptor_t * p_pgrm
442 = p_input->stream.pp_programs[i];
444 /* Reinitialize synchro. */
445 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
448 p_input->stream.p_new_program = NULL;
451 if( p_input->stream.p_new_area )
453 if( p_input->stream.b_seekable && p_input->pf_set_area != NULL )
455 input_AccessReinit( p_input );
457 p_input->pf_set_area( p_input, p_input->stream.p_new_area );
459 /* Escape all decoders for the stream discontinuity they
461 input_EscapeDiscontinuity( p_input );
463 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
465 pgrm_descriptor_t * p_pgrm
466 = p_input->stream.pp_programs[i];
468 /* Reinitialize synchro. */
469 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
472 p_input->stream.p_new_area = NULL;
475 if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
477 if( p_input->stream.p_selected_area->i_size > 0 )
481 double f = (double)p_input->stream.p_selected_area->i_seek /
482 (double)p_input->stream.p_selected_area->i_size;
484 vlc_mutex_unlock( &p_input->stream.stream_lock );
485 demux_Control( p_input, DEMUX_SET_POSITION, f );
486 vlc_mutex_lock( &p_input->stream.stream_lock );
488 /* Escape all decoders for the stream discontinuity they
490 input_EscapeDiscontinuity( p_input );
492 for( i = 0; i < p_input->stream.i_pgrm_number; i++ )
494 pgrm_descriptor_t * p_pgrm=p_input->stream.pp_programs[i];
496 /* Reinitialize synchro. */
497 p_pgrm->i_synchro_state = SYNCHRO_REINIT;
500 vlc_mutex_unlock( &p_input->stream.stream_lock );
501 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
506 /* Help in bar display */
508 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
511 for( i = 0; i < p_input->p_sys->i_sub; i++ )
513 subtitle_Seek( p_input->p_sys->sub[i], i_time );
516 if( !demux_Control( p_input, DEMUX_GET_POSITION, &f ) )
518 val.f_float = (float)f;
519 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
521 vlc_mutex_lock( &p_input->stream.stream_lock );
523 p_input->stream.p_selected_area->i_seek = NO_SEEK;
526 if( p_input->stream.p_removed_es )
528 input_UnselectES( p_input, p_input->stream.p_removed_es );
529 p_input->stream.p_removed_es = NULL;
532 if( p_input->stream.p_newly_selected_es )
534 input_SelectES( p_input, p_input->stream.p_newly_selected_es );
535 p_input->stream.p_newly_selected_es = NULL;
538 if( p_input->stream.b_new_mute != MUTE_NO_CHANGE )
540 if( p_input->stream.b_new_mute )
542 input_EscapeAudioDiscontinuity( p_input );
545 vlc_mutex_lock( &p_input->stream.control.control_lock );
546 p_input->stream.control.b_mute = p_input->stream.b_new_mute;
547 vlc_mutex_unlock( &p_input->stream.control.control_lock );
549 p_input->stream.b_new_mute = MUTE_NO_CHANGE;
552 vlc_mutex_unlock( &p_input->stream.stream_lock );
554 /* Read and demultiplex some data. */
555 i_count = p_input->pf_demux( p_input );
561 var_Get( p_input, "input-repeat", &repeat );
562 if( repeat.i_int == 0 || p_input->stream.i_area_nb <= 0 )
564 /* End of file - we do not set b_die because only the
565 * playlist is allowed to do so. */
566 msg_Info( p_input, "EOF reached" );
571 msg_Dbg( p_input, "repeating the same input (%d)", repeat.i_int );
572 if( repeat.i_int > 0 )
575 var_Set( p_input, "input-repeat", repeat );
578 p_input->stream.p_new_area = p_input->stream.pp_areas[0];
579 p_input->stream.p_new_area->i_seek = 0;
582 else if( i_count < 0 )
584 p_input->b_error = 1;
587 if( !p_input->b_error && !p_input->b_eof && i_update_next < mdate() )
594 /* update input status variables */
595 if( !demux_Control( p_input, DEMUX_GET_POSITION, &d_pos ) )
597 val.f_float = (float)d_pos;
598 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
600 if( !demux_Control( p_input, DEMUX_GET_TIME, &i_time ) )
603 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
605 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) )
607 val.i_time = i_length;
608 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
611 /* Check stop-time */
612 if( p_input->p_sys->i_stop_time > 0 && p_input->p_sys->i_stop_time < i_time )
614 msg_Warn( p_input, "EOF reached because of stop-time" );
619 for( i = 0; i < p_input->p_sys->i_sub; i++ )
621 subtitle_Demux( p_input->p_sys->sub[i], i_time );
624 i_update_next = mdate() + I64C(150000);
628 if( p_input->b_error || p_input->b_eof )
630 ErrorThread( p_input );
633 EndThread( p_input );
638 /*****************************************************************************
639 * InitThread: init the input Thread
640 *****************************************************************************/
641 static int InitThread( input_thread_t * p_input )
643 vlc_meta_t *p_meta = NULL, *p_meta_user = NULL;
648 /* Parse source string. Syntax : [[<access>][/<demux>]:][<source>] */
649 char * psz_parser = p_input->psz_dupsource = strdup(p_input->psz_source);
650 vlc_value_t val, val1;
651 int64_t i_microsecondperframe;
653 subtitle_demux_t *p_sub_toselect = NULL;
654 char *psz_sub_file = NULL;
656 /* Skip the plug-in names */
657 while( *psz_parser && *psz_parser != ':' )
661 #if defined( WIN32 ) || defined( UNDER_CE )
662 if( psz_parser - p_input->psz_dupsource == 1 )
664 msg_Warn( p_input, "drive letter %c: found in source string",
665 p_input->psz_dupsource[0] ) ;
672 p_input->psz_access = p_input->psz_demux = "";
673 p_input->psz_name = p_input->psz_source;
674 free( p_input->psz_dupsource );
675 p_input->psz_dupsource = NULL;
679 *psz_parser++ = '\0';
681 /* let's skip '//' */
682 if( psz_parser[0] == '/' && psz_parser[1] == '/' )
687 p_input->psz_name = psz_parser ;
689 /* Come back to parse the access and demux plug-ins */
690 psz_parser = p_input->psz_dupsource;
695 p_input->psz_access = "";
697 else if( *psz_parser == '/' )
700 p_input->psz_access = "";
705 p_input->psz_access = psz_parser;
707 while( *psz_parser && *psz_parser != '/' )
712 if( *psz_parser == '/' )
714 *psz_parser++ = '\0';
721 p_input->psz_demux = "";
725 p_input->psz_demux = psz_parser;
729 msg_Dbg( p_input, "access `%s', demux `%s', name `%s'",
730 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
732 if( input_AccessInit( p_input ) == -1 )
734 free( p_input->psz_source );
735 if( p_input->psz_dupsource != NULL )
737 free( p_input->psz_dupsource );
743 /* Initialize optional stream output. (before demuxer)*/
744 var_Get( p_input, "sout", &val );
745 if( val.psz_string != NULL )
747 if ( *val.psz_string && (p_input->stream.p_sout =
748 sout_NewInstance( p_input, val.psz_string )) == NULL )
750 msg_Err( p_input, "cannot start stream output instance, aborting" );
751 free( val.psz_string );
753 input_AccessEnd( p_input );
754 free( p_input->psz_source );
755 if( p_input->psz_dupsource != NULL )
757 free( p_input->psz_dupsource );
761 free( val.psz_string );
764 p_input->p_es_out = input_EsOutNew( p_input );
765 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
766 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
768 /* Find and open appropriate access module */
769 p_input->p_access = module_Need( p_input, "access",
770 p_input->psz_access, VLC_TRUE );
772 /* Maybe we had an encoded url */
773 if( !p_input->p_access && strchr( p_input->psz_name, '%' ) )
775 DecodeUrl( p_input->psz_name );
777 msg_Dbg( p_input, "retying with %s", p_input->psz_name );
778 p_input->p_access = module_Need( p_input, "access",
779 p_input->psz_access, VLC_TRUE );
781 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
782 * are forbidden in filenames on Win32. */
784 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
785 if ( p_input->p_access == NULL
786 && (*p_input->psz_demux || *p_input->psz_access) )
788 p_input->psz_access = p_input->psz_demux = "";
789 p_input->psz_name = p_input->psz_source;
790 free( p_input->psz_dupsource);
791 p_input->psz_dupsource = NULL;
793 p_input->p_access = module_Need( p_input, "access",
794 p_input->psz_access, VLC_TRUE );
797 if( p_input->p_access == NULL )
799 msg_Err( p_input, "no suitable access module for `%s/%s://%s'",
800 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
801 if ( p_input->stream.p_sout != NULL )
803 sout_DeleteInstance( p_input->stream.p_sout );
806 input_AccessEnd( p_input );
807 free( p_input->psz_source );
808 if( p_input->psz_dupsource != NULL )
810 free( p_input->psz_dupsource );
812 input_EsOutDelete( p_input->p_es_out );
816 /* Waiting for stream. */
819 p_input->i_bufsize = p_input->i_mtu;
823 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
826 /* If the desynchronisation requested by the user is < 0, we need to
827 * cache more data. */
828 var_Create( p_input, "audio-desync", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
829 var_Get( p_input, "audio-desync", &val );
831 p_input->i_pts_delay -= (val.i_int * 1000);
833 if( p_input->p_current_data == NULL && p_input->pf_read != NULL )
835 while( !input_FillBuffer( p_input ) )
837 if( p_input->b_die || p_input->b_error || p_input->b_eof )
839 module_Unneed( p_input, p_input->p_access );
840 if ( p_input->stream.p_sout != NULL )
842 sout_DeleteInstance( p_input->stream.p_sout );
844 input_AccessEnd( p_input );
845 free( p_input->psz_source );
846 if( p_input->psz_dupsource != NULL )
848 free( p_input->psz_dupsource );
850 input_EsOutDelete( p_input->p_es_out );
856 /* Create the stream_t facilities */
857 p_input->s = input_StreamNew( p_input );
858 if( p_input->s == NULL )
860 /* should never occur yet */
862 msg_Err( p_input, "cannot create stream_t" );
864 module_Unneed( p_input, p_input->p_access );
865 if ( p_input->stream.p_sout != NULL )
867 sout_DeleteInstance( p_input->stream.p_sout );
869 input_AccessEnd( p_input );
870 free( p_input->psz_source );
871 if( p_input->psz_dupsource != NULL )
873 free( p_input->psz_dupsource );
875 input_EsOutDelete( p_input->p_es_out );
879 /* Find and open appropriate demux module */
881 module_Need( p_input, "demux",
882 (p_input->psz_demux && *p_input->psz_demux) ?
883 p_input->psz_demux : "$demux",
884 (p_input->psz_demux && *p_input->psz_demux) ?
885 VLC_TRUE : VLC_FALSE );
887 if( p_input->p_demux == NULL )
889 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
890 p_input->psz_access, p_input->psz_demux, p_input->psz_name );
892 input_StreamDelete( p_input->s );
893 module_Unneed( p_input, p_input->p_access );
894 if ( p_input->stream.p_sout != NULL )
896 sout_DeleteInstance( p_input->stream.p_sout );
898 input_AccessEnd( p_input );
899 free( p_input->psz_source );
900 if( p_input->psz_dupsource != NULL )
902 free( p_input->psz_dupsource );
904 input_EsOutDelete( p_input->p_es_out );
908 /* Init input_thread_sys_t */
909 p_input->p_sys = malloc( sizeof( input_thread_sys_t ) );
910 p_input->p_sys->i_sub = 0;
911 p_input->p_sys->sub = NULL;
913 p_input->p_sys->i_stop_time = 0;
915 /* Get meta information from user */
916 var_Create( p_input, "meta-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
917 var_Create( p_input, "meta-author", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
918 var_Create( p_input, "meta-artist", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
919 var_Create( p_input, "meta-genre", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
920 var_Create( p_input, "meta-copyright", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
921 var_Create( p_input, "meta-description", VLC_VAR_STRING|VLC_VAR_DOINHERIT);
922 var_Create( p_input, "meta-date", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
923 var_Create( p_input, "meta-url", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
924 if( (p_meta_user = vlc_meta_New()) )
928 var_Get( p_input, "meta-title", &val );
929 if( val.psz_string && *val.psz_string )
930 vlc_meta_Add( p_meta_user, VLC_META_TITLE, val.psz_string );
931 if( val.psz_string ) free( val.psz_string );
932 var_Get( p_input, "meta-author", &val );
933 if( val.psz_string && *val.psz_string )
934 vlc_meta_Add( p_meta_user, VLC_META_AUTHOR, val.psz_string );
935 if( val.psz_string ) free( val.psz_string );
936 var_Get( p_input, "meta-artist", &val );
937 if( val.psz_string && *val.psz_string )
938 vlc_meta_Add( p_meta_user, VLC_META_ARTIST, val.psz_string );
939 if( val.psz_string ) free( val.psz_string );
940 var_Get( p_input, "meta-genre", &val );
941 if( val.psz_string && *val.psz_string )
942 vlc_meta_Add( p_meta_user, VLC_META_GENRE, val.psz_string );
943 if( val.psz_string ) free( val.psz_string );
944 var_Get( p_input, "meta-copyright", &val );
945 if( val.psz_string && *val.psz_string )
946 vlc_meta_Add( p_meta_user, VLC_META_COPYRIGHT, val.psz_string );
947 if( val.psz_string ) free( val.psz_string );
948 var_Get( p_input, "meta-description", &val );
949 if( val.psz_string && *val.psz_string )
950 vlc_meta_Add( p_meta_user, VLC_META_DESCRIPTION, val.psz_string );
951 if( val.psz_string ) free( val.psz_string );
952 var_Get( p_input, "meta-date", &val );
953 if( val.psz_string && *val.psz_string )
954 vlc_meta_Add( p_meta_user, VLC_META_DATE, val.psz_string );
955 if( val.psz_string ) free( val.psz_string );
956 var_Get( p_input, "meta-url", &val );
957 if( val.psz_string && *val.psz_string )
958 vlc_meta_Add( p_meta_user, VLC_META_URL, val.psz_string );
959 if( val.psz_string ) free( val.psz_string );
962 /* Get meta informations from demuxer */
963 if( !demux_Control( p_input, DEMUX_GET_META, &p_meta ) ||
964 ( p_meta_user && p_meta_user->i_meta ) )
968 /* Merge demux and user metadata */
969 if( !p_meta ){ p_meta = p_meta_user; p_meta_user = NULL; }
970 else if( p_meta && p_meta_user ) vlc_meta_Merge( p_meta, p_meta_user );
972 msg_Dbg( p_input, "meta informations:" );
973 if( p_meta->i_meta > 0 )
975 for( i = 0; i < p_meta->i_meta; i++ )
977 msg_Dbg( p_input, " - '%s' = '%s'", _(p_meta->name[i]),
979 if( !strcmp( p_meta->name[i], VLC_META_TITLE ) &&
981 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
983 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
984 input_Control( p_input, INPUT_ADD_INFO, _("General"),
985 _("Author"), p_meta->value[i] );
987 input_Control( p_input, INPUT_ADD_INFO, _("File"),
988 _(p_meta->name[i]), "%s", p_meta->value[i] );
991 for( i = 0; i < p_meta->i_track; i++ )
993 vlc_meta_t *tk = p_meta->track[i];
996 msg_Dbg( p_input, " - track[%d]:", i );
999 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1000 sprintf( psz_cat, "%s %d", _("Stream"), i );
1002 for( j = 0; j < tk->i_meta; j++ )
1004 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1007 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1008 _(tk->name[j]), "%s", tk->value[j] );
1013 if( p_input->stream.p_sout && p_input->stream.p_sout->p_meta == NULL )
1015 p_input->stream.p_sout->p_meta = p_meta;
1019 vlc_meta_Delete( p_meta );
1022 if( p_meta_user ) vlc_meta_Delete( p_meta_user );
1025 if( !demux_Control( p_input, DEMUX_GET_LENGTH, &i_length ) &&
1028 char psz_buffer[MSTRTIME_MAX_SIZE];
1030 vlc_mutex_lock( &p_input->p_item->lock );
1031 p_input->p_item->i_duration = i_length;
1032 vlc_mutex_unlock( &p_input->p_item->lock );
1034 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1035 msecstotimestr( psz_buffer, i_length / 1000 ) );
1037 /* Set start time */
1038 var_Get( p_input, "start-time", &val );
1041 double f_pos = val.i_int * I64C(1000000) / (double)i_length;
1045 msg_Warn( p_input, "invalid start-time, ignored (start-time "
1046 ">= media length)" );
1050 p_input->stream.p_selected_area->i_seek =
1051 (int64_t)( f_pos * (double)p_input->stream.p_selected_area->i_size );
1053 msg_Dbg( p_input, "start-time %ds (%2.2f)", val.i_int, f_pos );
1058 /* Set stop-time and check validity */
1059 var_Get( p_input, "stop-time", &val );
1064 var_Get( p_input, "start-time", &start );
1065 if( start.i_int >= val.i_int )
1067 msg_Warn( p_input, "invalid stop-time, ignored (stop-time < "
1072 p_input->p_sys->i_stop_time = (int64_t)val.i_int * I64C(1000000);
1073 msg_Dbg( p_input, "stop-time %ds", val.i_int );
1078 if( demux_Control( p_input, DEMUX_GET_FPS, &f_fps ) || f_fps < 0.1 )
1080 i_microsecondperframe = 0;
1084 i_microsecondperframe = (int64_t)( (double)1000000.0 / (double)f_fps );
1087 /* Look for and add subtitle files */
1088 var_Get( p_input, "sub-file", &val );
1089 if( val.psz_string && *val.psz_string )
1091 subtitle_demux_t *p_sub;
1093 msg_Dbg( p_input, "force subtitle: %s", val.psz_string );
1094 if( ( p_sub = subtitle_New( p_input, strdup(val.psz_string),
1095 i_microsecondperframe ) ) )
1097 p_sub_toselect = p_sub;
1098 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub, p_sub );
1101 psz_sub_file = val.psz_string;
1103 var_Get( p_input, "sub-autodetect-file", &val );
1104 var_Get( p_input, "sub-autodetect-path", &val1 );
1107 subtitle_demux_t *p_sub;
1109 char **tmp = subtitles_Detect( p_input, val1.psz_string, p_input->psz_name );
1111 for( i = 0; *tmp2 != NULL; i++ )
1113 if( psz_sub_file == NULL || strcmp( psz_sub_file, *tmp2 ) )
1115 if( ( p_sub = subtitle_New( p_input, *tmp2,
1116 i_microsecondperframe ) ) )
1118 TAB_APPEND( p_input->p_sys->i_sub, p_input->p_sys->sub,
1125 free( val1.psz_string );
1127 if( psz_sub_file ) free( psz_sub_file );
1129 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1130 val.b_bool = VLC_FALSE;
1131 if( p_input->stream.p_sout )
1133 var_Get( p_input, "sout-all", &val );
1135 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE,
1136 val.b_bool ? ES_OUT_MODE_ALL : ES_OUT_MODE_AUTO );
1137 if( p_sub_toselect )
1139 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1140 p_sub_toselect->p_es, VLC_TRUE );
1143 if( p_input->stream.p_sout )
1145 if( p_input->stream.p_sout->i_out_pace_nocontrol > 0 )
1147 p_input->b_out_pace_control = VLC_FALSE;
1151 p_input->b_out_pace_control = VLC_TRUE;
1153 msg_Dbg( p_input, "starting in %s mode",
1154 p_input->b_out_pace_control ? "asynch" : "synch" );
1160 /*****************************************************************************
1161 * ErrorThread: RunThread() error loop
1162 *****************************************************************************
1163 * This function is called when an error occured during thread main's loop.
1164 *****************************************************************************/
1165 static void ErrorThread( input_thread_t *p_input )
1167 while( !p_input->b_die )
1170 msleep( INPUT_IDLE_SLEEP );
1174 /*****************************************************************************
1175 * EndThread: end the input thread
1176 *****************************************************************************/
1177 static void EndThread( input_thread_t * p_input )
1180 #ifdef HAVE_SYS_TIMES_H
1181 /* Display statistics */
1182 struct tms cpu_usage;
1183 times( &cpu_usage );
1185 msg_Dbg( p_input, "%ld loops consuming user: %ld, system: %ld",
1186 p_input->c_loops, cpu_usage.tms_utime, cpu_usage.tms_stime );
1188 msg_Dbg( p_input, "%ld loops", p_input->c_loops );
1191 input_DumpStream( p_input );
1193 /* Free demultiplexer's data */
1194 if( p_input->p_demux ) module_Unneed( p_input, p_input->p_demux );
1196 /* Free all ES and destroy all decoder threads */
1197 input_EndStream( p_input );
1199 /* Close optional stream output instance */
1200 if( p_input->stream.p_sout )
1202 vlc_object_t *p_pl =
1203 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1206 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1208 /* attach sout to the playlist */
1209 msg_Warn( p_input, "keeping sout" );
1210 vlc_object_detach( p_input->stream.p_sout );
1211 vlc_object_attach( p_input->stream.p_sout, p_pl );
1215 msg_Warn( p_input, "destroying sout" );
1216 sout_DeleteInstance( p_input->stream.p_sout );
1220 vlc_object_release( p_pl );
1224 /* Destroy subtitles demuxers */
1225 if( p_input->p_sys )
1227 for( i = 0; i < p_input->p_sys->i_sub; i++ )
1229 subtitle_Close( p_input->p_sys->sub[i] );
1231 if( p_input->p_sys->i_sub > 0 )
1233 free( p_input->p_sys->sub );
1236 /* Free input_thread_sys_t */
1237 free( p_input->p_sys );
1240 /* Destroy the stream_t facilities */
1241 if( p_input->s ) input_StreamDelete( p_input->s );
1243 /* Destroy es out */
1244 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
1246 /* Close the access plug-in */
1247 if( p_input->p_access ) module_Unneed( p_input, p_input->p_access );
1249 input_AccessEnd( p_input );
1251 /* Free info structures XXX destroy es before 'cause vorbis */
1252 msg_Dbg( p_input, "freeing info structures...");
1254 free( p_input->psz_source );
1255 if( p_input->psz_dupsource != NULL ) free( p_input->psz_dupsource );
1257 /* Tell we're dead */
1258 p_input->b_dead = 1;
1260 /*****************************************************************************
1261 * DecodeUrl: decode a given encoded url
1262 *****************************************************************************/
1263 static void DecodeUrl( char *psz )
1265 char *dup = strdup( psz );
1283 *psz++ = strtol( val, NULL, 16 );
1285 else if( *p == '+' )
1299 /*****************************************************************************
1300 * ParseOption: parses the options for the input
1301 *****************************************************************************
1302 * This function parses the input (config) options and creates their associated
1304 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
1305 * bar is the value of the option.
1306 *****************************************************************************/
1307 static void ParseOption( input_thread_t *p_input, const char *psz_option )
1309 char *psz_name = (char *)psz_option;
1310 char *psz_value = strchr( psz_option, '=' );
1311 int i_name_len, i_type;
1312 vlc_bool_t b_isno = VLC_FALSE;
1315 if( psz_value ) i_name_len = psz_value - psz_option;
1316 else i_name_len = strlen( psz_option );
1318 /* It's too much of an hassle to remove the ':' when we parse
1319 * the cmd line :) */
1320 if( i_name_len && *psz_name == ':' )
1326 if( i_name_len == 0 ) return;
1328 psz_name = strndup( psz_name, i_name_len );
1329 if( psz_value ) psz_value++;
1331 i_type = config_GetType( p_input, psz_name );
1333 if( !i_type && !psz_value )
1335 /* check for "no-foo" or "nofoo" */
1336 if( !strncmp( psz_name, "no-", 3 ) )
1338 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
1340 else if( !strncmp( psz_name, "no", 2 ) )
1342 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
1344 else goto cleanup; /* Option doesn't exist */
1347 i_type = config_GetType( p_input, psz_name );
1349 if( !i_type ) goto cleanup; /* Option doesn't exist */
1351 else if( !i_type ) goto cleanup; /* Option doesn't exist */
1353 if( ( i_type != VLC_VAR_BOOL ) &&
1354 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
1356 /* Create the variable in the input object.
1357 * Children of the input object will be able to retreive this value
1358 * thanks to the inheritance property of the object variables. */
1359 var_Create( p_input, psz_name, i_type );
1364 val.b_bool = !b_isno;
1367 case VLC_VAR_INTEGER:
1368 val.i_int = atoi( psz_value );
1372 val.f_float = atof( psz_value );
1375 case VLC_VAR_STRING:
1376 case VLC_VAR_MODULE:
1378 case VLC_VAR_DIRECTORY:
1379 val.psz_string = psz_value;
1387 var_Set( p_input, psz_name, val );
1389 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value );
1392 if( psz_name ) free( psz_name );
1396 /*****************************************************************************
1397 * Callbacks (position, time, state, rate )
1398 *****************************************************************************/
1399 static int PositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1400 vlc_value_t oldval, vlc_value_t newval,
1403 input_thread_t *p_input = (input_thread_t *)p_this;
1405 msg_Dbg( p_input, "cmd=%s old=%f new=%f", psz_cmd,
1406 oldval.f_float, newval.f_float );
1408 if( !strcmp( psz_cmd, "position-offset" ) )
1411 var_Get( p_input, "position", &val );
1413 newval.f_float += val.f_float;
1416 vlc_mutex_lock( &p_input->stream.stream_lock );
1417 p_input->stream.p_selected_area->i_seek =
1418 (int64_t)( newval.f_float *
1419 (double)p_input->stream.p_selected_area->i_size );
1421 if( p_input->stream.p_selected_area->i_seek < 0 )
1423 p_input->stream.p_selected_area->i_seek = 0;
1425 vlc_mutex_unlock( &p_input->stream.stream_lock );
1430 static int TimeCallback( vlc_object_t *p_this, char const *psz_cmd,
1431 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1433 input_thread_t *p_input = (input_thread_t *)p_this;
1436 /* FIXME TODO FIXME */
1437 msg_Dbg( p_input, "cmd=%s old=%lld new=%lld", psz_cmd,
1438 oldval.i_time, newval.i_time );
1440 var_Get( p_input, "length", &val );
1441 if( val.i_time > 0 )
1443 val.f_float = (double)newval.i_time / (double)val.i_time;
1444 if( !strcmp( psz_cmd, "time-offset" ) )
1446 var_Set( p_input, "position-offset", val );
1450 var_Set( p_input, "position", val );
1455 msg_Warn( p_input, "TimeCallback: length <= 0 -> can't seek" );
1460 static int StateCallback( vlc_object_t *p_this, char const *psz_cmd,
1461 vlc_value_t oldval, vlc_value_t newval,
1464 input_thread_t *p_input = (input_thread_t *)p_this;
1466 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1467 psz_cmd, oldval.i_int, newval.i_int );
1469 switch( newval.i_int )
1472 input_SetStatus( p_input, INPUT_STATUS_PLAY );
1475 input_SetStatus( p_input, INPUT_STATUS_PAUSE );
1478 input_SetStatus( p_input, INPUT_STATUS_END );
1481 msg_Err( p_input, "cannot set new state (invalid)" );
1482 return VLC_EGENERIC;
1486 static int RateCallback( vlc_object_t *p_this, char const *psz_cmd,
1487 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1489 input_thread_t *p_input = (input_thread_t *)p_this;
1491 if( !strcmp( psz_cmd, "rate-slower" ) )
1493 input_SetStatus( p_input, INPUT_STATUS_SLOWER );
1495 else if( !strcmp( psz_cmd, "rate-faster" ) )
1497 input_SetStatus( p_input, INPUT_STATUS_FASTER );
1501 msg_Dbg( p_input, "cmd=%s old=%d new=%d",
1502 psz_cmd, oldval.i_int, newval.i_int );
1503 input_SetRate( p_input, newval.i_int );
1508 static int BookmarkCallback( vlc_object_t *p_this, char const *psz_cmd,
1509 vlc_value_t oldval, vlc_value_t newval, void *p_data )
1511 input_thread_t *p_input = (input_thread_t *)p_this;
1512 return input_Control( p_input, INPUT_SET_BOOKMARK, newval );