1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc/input.h>
33 #include <vlc/decoder.h>
36 #include "input_internal.h"
38 #include "stream_output.h"
39 #include "vlc_playlist.h"
40 #include "vlc_interface.h"
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Run ( input_thread_t *p_input );
47 static int Init ( input_thread_t *p_input, vlc_bool_t b_quick );
48 static void Error( input_thread_t *p_input );
49 static void End ( input_thread_t *p_input );
51 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
52 static void ControlReduce( input_thread_t * );
53 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
56 static int UpdateFromAccess( input_thread_t * );
57 static int UpdateFromDemux( input_thread_t * );
58 static int UpdateMeta( input_thread_t *, vlc_bool_t );
60 static void UpdateItemLength( input_thread_t *, int64_t i_length, vlc_bool_t );
62 static void ParseOption( input_thread_t *p_input, const char *psz_option );
64 static void DecodeUrl( char * );
65 static void MRLSplit( input_thread_t *, char *, char **, char **, char ** );
66 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
68 static input_source_t *InputSourceNew( input_thread_t *);
69 static int InputSourceInit( input_thread_t *, input_source_t *,
70 char *, char *psz_forced_demux,
72 static void InputSourceClean( input_thread_t *, input_source_t * );
74 static void SlaveDemux( input_thread_t *p_input );
75 static void SlaveSeek( input_thread_t *p_input );
77 static vlc_meta_t *InputMetaUser( input_thread_t *p_input );
79 /*****************************************************************************
80 * input_CreateThread: creates a new input thread
81 *****************************************************************************
82 * This function creates a new input, and returns a pointer
83 * to its description. On error, it returns NULL.
85 * Variables for _public_ use:
88 * - rate,rate-slower, rate-faster
89 * - position, position-offset
91 * - title,title-next,title-prev
92 * - chapter,chapter-next, chapter-prev
93 * - program, audio-es, video-es, spu-es
94 * - audio-delay, spu-delay
99 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
100 * * For intf callback upon changes
102 * TODO explain when Callback is called
103 * TODO complete this list (?)
104 *****************************************************************************/
105 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
106 input_item_t *p_item )
109 input_thread_t *p_input; /* thread descriptor */
113 /* Allocate descriptor */
114 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
115 if( p_input == NULL )
117 msg_Err( p_parent, "out of memory" );
121 /* Init Common fields */
122 p_input->b_eof = VLC_FALSE;
123 p_input->b_can_pace_control = VLC_TRUE;
124 p_input->i_start = 0;
127 p_input->i_title = 0;
128 p_input->title = NULL;
129 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
130 p_input->i_state = INIT_S;
131 p_input->i_rate = INPUT_RATE_DEFAULT;
132 p_input->i_bookmark = 0;
133 p_input->bookmark = NULL;
134 p_input->p_meta = NULL;
135 p_input->p_es_out = NULL;
136 p_input->p_sout = NULL;
137 p_input->b_out_pace_control = VLC_FALSE;
138 p_input->i_pts_delay = 0;
140 /* Init Input fields */
141 p_input->input.p_item = p_item;
142 p_input->input.p_access = NULL;
143 p_input->input.p_stream = NULL;
144 p_input->input.p_demux = NULL;
145 p_input->input.b_title_demux = VLC_FALSE;
146 p_input->input.i_title = 0;
147 p_input->input.title = NULL;
148 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
149 p_input->input.b_can_pace_control = VLC_TRUE;
150 p_input->input.b_eof = VLC_FALSE;
151 p_input->input.i_cr_average = 0;
154 p_input->i_slave = 0;
155 p_input->slave = NULL;
157 /* Init control buffer */
158 vlc_mutex_init( p_input, &p_input->lock_control );
159 p_input->i_control = 0;
161 /* Parse input options */
162 vlc_mutex_lock( &p_item->lock );
163 for( i = 0; i < p_item->i_options; i++ )
165 // msg_Dbg( p_input, "option: %s", p_item->ppsz_options[i] );
166 ParseOption( p_input, p_item->ppsz_options[i] );
168 vlc_mutex_unlock( &p_item->lock );
170 /* Create Object Variables for private use only */
171 input_ConfigVarInit( p_input );
173 /* Create Objects variables for public Get and Set */
174 input_ControlVarInit( p_input );
175 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
178 var_Get( p_input, "bookmarks", &val );
181 /* FIXME: have a common cfg parsing routine used by sout and others */
182 char *psz_parser, *psz_start, *psz_end;
183 psz_parser = val.psz_string;
184 while( (psz_start = strchr( psz_parser, '{' ) ) )
186 seekpoint_t seekpoint;
189 psz_end = strchr( psz_start, '}' );
190 if( !psz_end ) break;
191 psz_parser = psz_end + 1;
192 backup = *psz_parser;
196 seekpoint.psz_name = 0;
197 seekpoint.i_byte_offset = 0;
198 seekpoint.i_time_offset = 0;
199 while( (psz_end = strchr( psz_start, ',' ) ) )
202 if( !strncmp( psz_start, "name=", 5 ) )
204 seekpoint.psz_name = psz_start + 5;
206 else if( !strncmp( psz_start, "bytes=", 6 ) )
208 seekpoint.i_byte_offset = atoll(psz_start + 6);
210 else if( !strncmp( psz_start, "time=", 5 ) )
212 seekpoint.i_time_offset = atoll(psz_start + 5) * 1000000;
214 psz_start = psz_end + 1;
216 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
217 seekpoint.psz_name, seekpoint.i_byte_offset,
218 seekpoint.i_time_offset );
219 input_Control( p_input, INPUT_ADD_BOOKMARK, &seekpoint );
220 *psz_parser = backup;
222 free( val.psz_string );
225 /* Now we can attach our new input */
226 vlc_object_attach( p_input, p_parent );
228 /* Create thread and wait for its readiness. */
229 if( vlc_thread_create( p_input, "input", Run,
230 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
232 msg_Err( p_input, "cannot create input thread" );
233 vlc_object_detach( p_input );
234 vlc_object_destroy( p_input );
241 /*****************************************************************************
242 * input_PreParse: Lightweight input for playlist item preparsing
243 *****************************************************************************/
244 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
246 input_thread_t *p_input; /* thread descriptor */
249 /* Allocate descriptor */
250 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
251 if( p_input == NULL )
253 msg_Err( p_parent, "out of memory" );
257 /* Init Common fields */
258 p_input->b_eof = VLC_FALSE;
259 p_input->b_can_pace_control = VLC_TRUE;
260 p_input->i_start = 0;
263 p_input->i_title = 0;
264 p_input->title = NULL;
265 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
266 p_input->i_state = INIT_S;
267 p_input->i_rate = INPUT_RATE_DEFAULT;
268 p_input->i_bookmark = 0;
269 p_input->bookmark = NULL;
270 p_input->p_meta = NULL;
271 p_input->p_es_out = NULL;
272 p_input->p_sout = NULL;
273 p_input->b_out_pace_control = VLC_FALSE;
274 p_input->i_pts_delay = 0;
276 /* Init Input fields */
277 p_input->input.p_item = p_item;
278 p_input->input.p_access = NULL;
279 p_input->input.p_stream = NULL;
280 p_input->input.p_demux = NULL;
281 p_input->input.b_title_demux = VLC_FALSE;
282 p_input->input.i_title = 0;
283 p_input->input.title = NULL;
284 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
285 p_input->input.b_can_pace_control = VLC_TRUE;
286 p_input->input.b_eof = VLC_FALSE;
287 p_input->input.i_cr_average = 0;
290 p_input->i_slave = 0;
291 p_input->slave = NULL;
293 /* Init control buffer */
294 vlc_mutex_init( p_input, &p_input->lock_control );
295 p_input->i_control = 0;
297 /* Parse input options */
298 vlc_mutex_lock( &p_item->lock );
299 for( i = 0; i < p_item->i_options; i++ )
301 ParseOption( p_input, p_item->ppsz_options[i] );
303 vlc_mutex_unlock( &p_item->lock );
305 /* Create Object Variables for private use only */
306 input_ConfigVarInit( p_input );
307 input_ControlVarInit( p_input );
309 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
311 /* Now we can attach our new input */
312 vlc_object_attach( p_input, p_parent );
314 Init( p_input, VLC_TRUE );
316 /* Clean up master */
317 InputSourceClean( p_input, &p_input->input );
319 /* Kill access and demux */
320 if( p_input->input.p_access ) p_input->input.p_access->b_die = VLC_TRUE;
321 if( p_input->input.p_demux ) p_input->input.p_access->b_die = VLC_TRUE;
323 /* Unload all modules */
324 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
327 if( p_input->p_meta ) vlc_meta_Delete( p_input->p_meta );
329 vlc_object_detach( p_input );
330 vlc_object_destroy( p_input );
335 /*****************************************************************************
336 * input_StopThread: mark an input thread as zombie
337 *****************************************************************************
338 * This function should not return until the thread is effectively cancelled.
339 *****************************************************************************/
340 void input_StopThread( input_thread_t *p_input )
345 /* Set die for input */
346 p_input->b_die = VLC_TRUE;
348 /* We cannot touch p_input fields directly (we can from another thread),
349 * so use the vlc_object_find way, it's perfectly safe */
351 /* Set die for all access */
352 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
353 for( i = 0; i < p_list->i_count; i++ )
355 p_list->p_values[i].p_object->b_die = VLC_TRUE;
357 vlc_list_release( p_list );
359 /* Set die for all stream */
360 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
361 for( i = 0; i < p_list->i_count; i++ )
363 p_list->p_values[i].p_object->b_die = VLC_TRUE;
365 vlc_list_release( p_list );
367 /* Set die for all demux */
368 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
369 for( i = 0; i < p_list->i_count; i++ )
371 p_list->p_values[i].p_object->b_die = VLC_TRUE;
373 vlc_list_release( p_list );
375 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
378 /*****************************************************************************
379 * input_DestroyThread: mark an input thread as zombie
380 *****************************************************************************
381 * This function should not return until the thread is effectively cancelled.
382 *****************************************************************************/
383 void input_DestroyThread( input_thread_t *p_input )
385 /* Join the thread */
386 vlc_thread_join( p_input );
388 /* Delete input lock (only after thread joined) */
389 vlc_mutex_destroy( &p_input->lock_control );
391 /* TODO: maybe input_DestroyThread should also delete p_input instead
392 * of the playlist but I'm not sure if it's possible */
395 /*****************************************************************************
396 * Run: main thread loop
397 *****************************************************************************
398 * Thread in charge of processing the network packets and demultiplexing.
401 * read subtitle support (XXX take care of spu-delay in the right way).
402 * multi-input support (XXX may be done with subs)
403 *****************************************************************************/
404 static int Run( input_thread_t *p_input )
406 int64_t i_intf_update = 0;
408 /* Signal that the thread is launched */
409 vlc_thread_ready( p_input );
411 if( Init( p_input, VLC_FALSE ) )
413 /* If we failed, wait before we are killed, and exit */
414 p_input->b_error = VLC_TRUE;
418 /* Tell we're dead */
419 p_input->b_dead = VLC_TRUE;
425 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
427 vlc_bool_t b_force_update = VLC_FALSE;
433 if( p_input->i_state != PAUSE_S )
435 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
436 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
443 if( p_input->input.b_title_demux &&
444 p_input->input.p_demux->info.i_update )
446 i_ret = UpdateFromDemux( p_input );
447 b_force_update = VLC_TRUE;
449 else if( !p_input->input.b_title_demux &&
450 p_input->input.p_access &&
451 p_input->input.p_access->info.i_update )
453 i_ret = UpdateFromAccess( p_input );
454 b_force_update = VLC_TRUE;
458 if( i_ret == 0 ) /* EOF */
462 var_Get( p_input, "input-repeat", &repeat );
463 if( repeat.i_int == 0 )
465 /* End of file - we do not set b_die because only the
466 * playlist is allowed to do so. */
467 msg_Dbg( p_input, "EOF reached" );
468 p_input->input.b_eof = VLC_TRUE;
472 msg_Dbg( p_input, "repeating the same input (%d)",
474 if( repeat.i_int > 0 )
477 var_Set( p_input, "input-repeat", repeat );
480 /* Seek to start title/seekpoint */
481 val.i_int = p_input->input.i_title_start -
482 p_input->input.i_title_offset;
483 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
485 input_ControlPush( p_input,
486 INPUT_CONTROL_SET_TITLE, &val );
488 val.i_int = p_input->input.i_seekpoint_start -
489 p_input->input.i_seekpoint_offset;
490 if( val.i_int > 0 /* TODO: check upper boundary */ )
491 input_ControlPush( p_input,
492 INPUT_CONTROL_SET_SEEKPOINT, &val );
494 /* Seek to start position */
495 if( p_input->i_start > 0 )
497 val.i_time = p_input->i_start;
498 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
504 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
511 p_input->b_error = VLC_TRUE;
514 if( i_ret > 0 && p_input->i_slave > 0 )
516 SlaveDemux( p_input );
526 vlc_mutex_lock( &p_input->lock_control );
527 ControlReduce( p_input );
528 while( !ControlPopNoLock( p_input, &i_type, &val ) )
530 msg_Dbg( p_input, "control type=%d", i_type );
531 if( Control( p_input, i_type, val ) )
532 b_force_update = VLC_TRUE;
534 vlc_mutex_unlock( &p_input->lock_control );
536 if( b_force_update || i_intf_update < mdate() )
540 int64_t i_time, i_length;
541 /* update input status variables */
542 if( !demux2_Control( p_input->input.p_demux,
543 DEMUX_GET_POSITION, &f_pos ) )
545 val.f_float = (float)f_pos;
546 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
548 if( !demux2_Control( p_input->input.p_demux,
549 DEMUX_GET_TIME, &i_time ) )
551 p_input->i_time = i_time;
553 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
555 if( !demux2_Control( p_input->input.p_demux,
556 DEMUX_GET_LENGTH, &i_length ) )
559 var_Get( p_input, "length", &old_val );
560 val.i_time = i_length;
561 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
563 if( old_val.i_time != val.i_time )
565 UpdateItemLength( p_input, i_length, VLC_TRUE );
569 var_SetBool( p_input, "intf-change", VLC_TRUE );
570 i_intf_update = mdate() + I64C(150000);
574 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
576 /* We have finish to demux data but not to play them */
577 while( !p_input->b_die )
579 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
582 msg_Dbg( p_input, "waiting decoder fifos to empty" );
584 msleep( INPUT_IDLE_SLEEP );
587 /* We have finished */
588 p_input->b_eof = VLC_TRUE;
591 /* Wait we are asked to die */
592 if( !p_input->b_die )
604 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
610 vlc_meta_t *p_meta, *p_meta_tmp;
614 /* Initialize optional stream output. (before access/demuxer) */
617 psz = var_GetString( p_input, "sout" );
620 p_input->p_sout = sout_NewInstance( p_input, psz );
621 if( p_input->p_sout == NULL )
623 msg_Err( p_input, "cannot start stream output instance," \
633 p_input->p_es_out = input_EsOutNew( p_input );
634 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
635 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
637 if( InputSourceInit( p_input, &p_input->input,
638 p_input->input.p_item->psz_uri, NULL, b_quick ) )
643 /* Create global title (from master) */
646 p_input->i_title = p_input->input.i_title;
647 p_input->title = p_input->input.title;
648 p_input->i_title_offset = p_input->input.i_title_offset;
649 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
650 if( p_input->i_title > 0 )
652 /* Setup variables */
653 input_ControlVarNavigation( p_input );
654 input_ControlVarTitle( p_input, 0 );
658 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
659 p_input->b_can_pause = p_input->input.b_can_pause;
662 if( p_input->i_pts_delay <= 0 )
663 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
665 /* If the desynchronisation requested by the user is < 0, we need to
666 * cache more data. */
667 var_Get( p_input, "audio-desync", &val );
668 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
670 /* Update cr_average depending on the caching */
671 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
672 p_input->input.i_cr_average /= 10;
673 if( p_input->input.i_cr_average <= 0 ) p_input->input.i_cr_average = 1;
676 /* Load master infos */
678 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
679 &val.i_time ) && val.i_time > 0 )
681 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
682 UpdateItemLength( p_input, val.i_time, b_quick );
683 p_input->input.p_item->i_duration = val.i_time;
686 /* Start title/chapter */
689 val.i_int = p_input->input.i_title_start -
690 p_input->input.i_title_offset;
691 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
692 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
693 val.i_int = p_input->input.i_seekpoint_start -
694 p_input->input.i_seekpoint_offset;
695 if( val.i_int > 0 /* TODO: check upper boundary */ )
696 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
700 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
702 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
705 if( p_input->i_start > 0 )
707 if( p_input->i_start >= val.i_time )
709 msg_Warn( p_input, "invalid start-time ignored" );
715 msg_Dbg( p_input, "start-time: %ds",
716 (int)( p_input->i_start / I64C(1000000) ) );
718 s.i_time = p_input->i_start;
719 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
722 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
724 msg_Warn( p_input, "invalid stop-time ignored" );
730 /* Get fps and set it if not already set */
731 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
734 float f_requested_fps;
736 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
737 var_SetFloat( p_input, "sub-original-fps", f_fps );
739 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
740 if( f_requested_fps != f_fps )
742 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
744 var_SetFloat( p_input, "sub-fps", f_requested_fps );
748 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
751 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
755 /* Look for and add subtitle files */
756 psz_subtitle = var_GetString( p_input, "sub-file" );
763 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
765 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
768 sub = InputSourceNew( p_input );
769 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
772 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
775 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
778 if( count.i_int == 0 )
780 /* if it was first one, there is disable too */
782 if( count.i_int < list.p_list->i_count )
784 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
785 &list.p_list->p_values[count.i_int] );
787 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
793 var_Get( p_input, "sub-autodetect-file", &val );
796 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
797 char **subs = subtitles_Detect( p_input, psz_autopath,
798 p_input->input.p_item->psz_uri );
801 for( i = 0; subs[i] != NULL; i++ )
803 if( strcmp( psz_subtitle, subs[i] ) )
805 sub = InputSourceNew( p_input );
806 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
809 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
815 free( psz_autopath );
817 free( psz_subtitle );
820 psz = var_GetString( p_input, "input-slave" );
824 input_source_t *slave;
827 while( *psz == ' ' || *psz == '#' )
831 if( ( psz_delim = strchr( psz, '#' ) ) )
840 msg_Dbg( p_input, "adding slave '%s'", psz );
841 slave = InputSourceNew( p_input );
842 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
844 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
849 if( psz ) free( psz );
853 p_input->i_start = 0;
854 p_input->i_start = 0;
860 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
861 i_es_out_mode = ES_OUT_MODE_AUTO;
863 if( p_input->p_sout )
865 var_Get( p_input, "sout-all", &val );
868 i_es_out_mode = ES_OUT_MODE_ALL;
873 var_Get( p_input, "programs", &val );
874 if ( val.p_list && val.p_list->i_count )
876 i_es_out_mode = ES_OUT_MODE_PARTIAL;
877 /* Note : we should remove the "program" callback. */
880 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
884 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
886 /* Inform the demuxer about waited group (needed only for DVB) */
887 if( i_es_out_mode == ES_OUT_MODE_ALL )
889 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
891 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
893 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
898 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
899 (int) var_GetInteger( p_input, "program" ), NULL );
902 if( p_input->p_sout )
904 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
906 p_input->b_out_pace_control = VLC_FALSE;
910 p_input->b_out_pace_control = VLC_TRUE;
912 msg_Dbg( p_input, "starting in %s mode",
913 p_input->b_out_pace_control ? "asynch" : "synch" );
917 /* Get meta data from users */
918 p_meta_tmp = InputMetaUser( p_input );
920 /* Get meta data from master input */
921 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
929 else if( p_meta_tmp )
931 vlc_meta_Merge( p_meta, p_meta_tmp );
932 vlc_meta_Delete( p_meta_tmp );
935 /* Access_file does not give any meta, and there are no slave */
938 if( !p_input->input.p_access ||
939 access2_Control( p_input->input.p_access, ACCESS_GET_META,
947 else if( p_meta_tmp )
949 vlc_meta_Merge( p_meta, p_meta_tmp );
950 vlc_meta_Delete( p_meta_tmp );
953 /* Get meta data from slave input */
954 for( i = 0; i < p_input->i_slave; i++ )
956 vlc_meta_t *p_meta_slave;
958 if( !demux2_Control( p_input->slave[i]->p_demux,
959 DEMUX_GET_META, &p_meta_slave ) )
963 p_meta = p_meta_slave;
965 else if( p_meta_slave )
967 vlc_meta_Merge( p_meta, p_meta_slave );
968 vlc_meta_Delete( p_meta_slave );
972 if( p_input->slave[i]->p_access &&
973 !access2_Control( p_input->slave[i]->p_access,
974 ACCESS_GET_META, &p_meta_slave ) )
978 p_meta = p_meta_slave;
980 else if( p_meta_slave )
982 vlc_meta_Merge( p_meta, p_meta_slave );
983 vlc_meta_Delete( p_meta_slave );
989 p_input->p_meta = p_meta;
990 UpdateMeta( p_input, b_quick );
994 msg_Dbg( p_input, "`%s' sucessfully opened",
995 p_input->input.p_item->psz_uri );
999 /* Trigger intf update for this item */
1000 /* Playlist has a callback on this variable and will forward
1002 var_SetInteger( p_input, "item-change", p_input->input.p_item->i_id );
1004 /* initialization is complete */
1005 p_input->i_state = PLAYING_S;
1007 val.i_int = PLAYING_S;
1008 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1013 if( p_input->p_es_out )
1014 input_EsOutDelete( p_input->p_es_out );
1016 if( p_input->p_sout )
1017 sout_DeleteInstance( p_input->p_sout );
1019 /* Mark them deleted */
1020 p_input->input.p_demux = NULL;
1021 p_input->input.p_stream = NULL;
1022 p_input->input.p_access = NULL;
1023 p_input->p_es_out = NULL;
1024 p_input->p_sout = NULL;
1026 return VLC_EGENERIC;
1029 /*****************************************************************************
1030 * Error: RunThread() error loop
1031 *****************************************************************************
1032 * This function is called when an error occurred during thread main's loop.
1033 *****************************************************************************/
1034 static void Error( input_thread_t *p_input )
1036 while( !p_input->b_die )
1039 msleep( INPUT_IDLE_SLEEP );
1043 /*****************************************************************************
1044 * End: end the input thread
1045 *****************************************************************************/
1046 static void End( input_thread_t * p_input )
1051 msg_Dbg( p_input, "closing input" );
1053 /* We are at the end */
1054 p_input->i_state = END_S;
1057 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1059 /* Clean control variables */
1060 input_ControlVarClean( p_input );
1062 /* Clean up master */
1063 InputSourceClean( p_input, &p_input->input );
1066 for( i = 0; i < p_input->i_slave; i++ )
1068 InputSourceClean( p_input, p_input->slave[i] );
1069 free( p_input->slave[i] );
1071 if( p_input->slave ) free( p_input->slave );
1073 /* Unload all modules */
1074 if( p_input->p_es_out )
1075 input_EsOutDelete( p_input->p_es_out );
1077 /* Close optional stream output instance */
1078 if( p_input->p_sout )
1080 vlc_object_t *p_pl =
1081 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1084 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1086 /* attach sout to the playlist */
1087 msg_Warn( p_input, "keeping sout" );
1088 vlc_object_detach( p_input->p_sout );
1089 vlc_object_attach( p_input->p_sout, p_pl );
1093 msg_Warn( p_input, "destroying sout" );
1094 sout_DeleteInstance( p_input->p_sout );
1097 vlc_object_release( p_pl );
1101 if( p_input->p_meta )
1102 vlc_meta_Delete( p_input->p_meta );
1104 /* Tell we're dead */
1105 p_input->b_dead = VLC_TRUE;
1108 /*****************************************************************************
1110 *****************************************************************************/
1111 static inline int ControlPopNoLock( input_thread_t *p_input,
1112 int *pi_type, vlc_value_t *p_val )
1114 if( p_input->i_control <= 0 )
1116 return VLC_EGENERIC;
1119 *pi_type = p_input->control[0].i_type;
1120 *p_val = p_input->control[0].val;
1122 p_input->i_control--;
1123 if( p_input->i_control > 0 )
1127 for( i = 0; i < p_input->i_control; i++ )
1129 p_input->control[i].i_type = p_input->control[i+1].i_type;
1130 p_input->control[i].val = p_input->control[i+1].val;
1137 static void ControlReduce( input_thread_t *p_input )
1140 for( i = 1; i < p_input->i_control; i++ )
1142 const int i_lt = p_input->control[i-1].i_type;
1143 const int i_ct = p_input->control[i].i_type;
1145 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1146 msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1149 ( i_ct == INPUT_CONTROL_SET_STATE ||
1150 i_ct == INPUT_CONTROL_SET_RATE ||
1151 i_ct == INPUT_CONTROL_SET_POSITION ||
1152 i_ct == INPUT_CONTROL_SET_TIME ||
1153 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1154 i_ct == INPUT_CONTROL_SET_TITLE ||
1155 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1156 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1159 msg_Dbg( p_input, "merged at %d", i );
1160 /* Remove the i-1 */
1161 for( j = i; j < p_input->i_control; j++ )
1162 p_input->control[j-1] = p_input->control[j];
1163 p_input->i_control--;
1167 /* TODO but that's not that important
1168 - merge SET_X with SET_X_CMD
1169 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1170 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1177 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1180 vlc_bool_t b_force_update = VLC_FALSE;
1184 case INPUT_CONTROL_SET_DIE:
1185 msg_Dbg( p_input, "control: INPUT_CONTROL_SET_DIE proceed" );
1186 /* Mark all submodules to die */
1187 if( p_input->input.p_access )
1188 p_input->input.p_access->b_die = VLC_TRUE;
1189 if( p_input->input.p_stream )
1190 p_input->input.p_stream->b_die = VLC_TRUE;
1191 p_input->input.p_demux->b_die = VLC_TRUE;
1193 p_input->b_die = VLC_TRUE;
1196 case INPUT_CONTROL_SET_POSITION:
1197 case INPUT_CONTROL_SET_POSITION_OFFSET:
1200 if( i_type == INPUT_CONTROL_SET_POSITION )
1202 f_pos = val.f_float;
1206 /* Should not fail */
1207 demux2_Control( p_input->input.p_demux,
1208 DEMUX_GET_POSITION, &f_pos );
1209 f_pos += val.f_float;
1211 if( f_pos < 0.0 ) f_pos = 0.0;
1212 if( f_pos > 1.0 ) f_pos = 1.0;
1213 /* Reset the decoders states and clock synch (before calling the demuxer */
1214 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1215 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1216 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1219 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1220 "%2.1f%% failed", f_pos * 100 );
1224 if( p_input->i_slave > 0 )
1225 SlaveSeek( p_input );
1227 //input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1228 //es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1229 b_force_update = VLC_TRUE;
1234 case INPUT_CONTROL_SET_TIME:
1235 case INPUT_CONTROL_SET_TIME_OFFSET:
1240 if( i_type == INPUT_CONTROL_SET_TIME )
1242 i_time = val.i_time;
1246 /* Should not fail */
1247 demux2_Control( p_input->input.p_demux,
1248 DEMUX_GET_TIME, &i_time );
1249 i_time += val.i_time;
1251 if( i_time < 0 ) i_time = 0;
1253 /* Reset the decoders states and clock synch (before calling the demuxer */
1254 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1255 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1257 i_ret = demux2_Control( p_input->input.p_demux,
1258 DEMUX_SET_TIME, i_time );
1262 /* Emulate it with a SET_POS */
1264 demux2_Control( p_input->input.p_demux,
1265 DEMUX_GET_LENGTH, &i_length );
1268 double f_pos = (double)i_time / (double)i_length;
1269 i_ret = demux2_Control( p_input->input.p_demux,
1270 DEMUX_SET_POSITION, f_pos );
1275 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1276 " failed", i_time );
1280 if( p_input->i_slave > 0 )
1281 SlaveSeek( p_input );
1283 //input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1284 //es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1285 b_force_update = VLC_TRUE;
1290 case INPUT_CONTROL_SET_STATE:
1291 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1292 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1295 if( p_input->input.p_access )
1296 i_ret = access2_Control( p_input->input.p_access,
1297 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1299 i_ret = demux2_Control( p_input->input.p_demux,
1300 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1304 /* FIXME What to do ? */
1305 msg_Warn( p_input, "cannot unset pause -> EOF" );
1306 vlc_mutex_unlock( &p_input->lock_control );
1307 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1308 vlc_mutex_lock( &p_input->lock_control );
1311 b_force_update = VLC_TRUE;
1313 /* Switch to play */
1314 p_input->i_state = PLAYING_S;
1315 val.i_int = PLAYING_S;
1316 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1319 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1321 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1322 p_input->b_can_pause )
1325 if( p_input->input.p_access )
1326 i_ret = access2_Control( p_input->input.p_access,
1327 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1329 i_ret = demux2_Control( p_input->input.p_demux,
1330 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1332 b_force_update = VLC_TRUE;
1336 msg_Warn( p_input, "cannot set pause state" );
1337 val.i_int = p_input->i_state;
1341 val.i_int = PAUSE_S;
1344 /* Switch to new state */
1345 p_input->i_state = val.i_int;
1346 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1348 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1350 b_force_update = VLC_TRUE;
1352 /* Correct "state" value */
1353 val.i_int = p_input->i_state;
1354 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1356 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1358 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1362 case INPUT_CONTROL_SET_RATE:
1363 case INPUT_CONTROL_SET_RATE_SLOWER:
1364 case INPUT_CONTROL_SET_RATE_FASTER:
1368 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1369 i_rate = p_input->i_rate * 2;
1370 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1371 i_rate = p_input->i_rate / 2;
1375 if( i_rate < INPUT_RATE_MIN )
1377 msg_Dbg( p_input, "cannot set rate faster" );
1378 i_rate = INPUT_RATE_MIN;
1380 else if( i_rate > INPUT_RATE_MAX )
1382 msg_Dbg( p_input, "cannot set rate slower" );
1383 i_rate = INPUT_RATE_MAX;
1385 if( i_rate != INPUT_RATE_DEFAULT &&
1386 ( !p_input->b_can_pace_control ||
1387 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1389 msg_Dbg( p_input, "cannot change rate" );
1390 i_rate = INPUT_RATE_DEFAULT;
1392 if( i_rate != p_input->i_rate )
1394 p_input->i_rate = i_rate;
1396 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1398 /* We haven't send data to decoder when rate != default */
1399 if( i_rate == INPUT_RATE_DEFAULT )
1400 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1403 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1405 b_force_update = VLC_TRUE;
1410 case INPUT_CONTROL_SET_PROGRAM:
1411 /* No need to force update, es_out does it if needed */
1412 es_out_Control( p_input->p_es_out,
1413 ES_OUT_SET_GROUP, val.i_int );
1415 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1419 case INPUT_CONTROL_SET_ES:
1420 /* No need to force update, es_out does it if needed */
1421 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1422 input_EsOutGetFromID( p_input->p_es_out,
1426 case INPUT_CONTROL_SET_AUDIO_DELAY:
1427 input_EsOutSetDelay( p_input->p_es_out,
1428 AUDIO_ES, val.i_time );
1429 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1432 case INPUT_CONTROL_SET_SPU_DELAY:
1433 input_EsOutSetDelay( p_input->p_es_out,
1434 SPU_ES, val.i_time );
1435 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1438 case INPUT_CONTROL_SET_TITLE:
1439 case INPUT_CONTROL_SET_TITLE_NEXT:
1440 case INPUT_CONTROL_SET_TITLE_PREV:
1441 if( p_input->input.b_title_demux &&
1442 p_input->input.i_title > 0 )
1445 /* FIXME handle demux title */
1446 demux_t *p_demux = p_input->input.p_demux;
1449 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1450 i_title = p_demux->info.i_title - 1;
1451 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1452 i_title = p_demux->info.i_title + 1;
1454 i_title = val.i_int;
1456 if( i_title >= 0 && i_title < p_input->input.i_title )
1458 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1460 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1461 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1463 input_ControlVarTitle( p_input, i_title );
1466 else if( p_input->input.i_title > 0 )
1468 access_t *p_access = p_input->input.p_access;
1471 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1472 i_title = p_access->info.i_title - 1;
1473 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1474 i_title = p_access->info.i_title + 1;
1476 i_title = val.i_int;
1478 if( i_title >= 0 && i_title < p_input->input.i_title )
1480 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1481 stream_AccessReset( p_input->input.p_stream );
1483 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1484 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1488 case INPUT_CONTROL_SET_SEEKPOINT:
1489 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1490 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1491 if( p_input->input.b_title_demux &&
1492 p_input->input.i_title > 0 )
1494 demux_t *p_demux = p_input->input.p_demux;
1497 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1498 i_seekpoint = p_demux->info.i_seekpoint - 1;
1499 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1500 i_seekpoint = p_demux->info.i_seekpoint + 1;
1502 i_seekpoint = val.i_int;
1504 if( i_seekpoint >= 0 && i_seekpoint <
1505 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1507 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1509 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1510 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1513 else if( p_input->input.i_title > 0 )
1515 access_t *p_access = p_input->input.p_access;
1518 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1519 i_seekpoint = p_access->info.i_seekpoint - 1;
1520 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1521 i_seekpoint = p_access->info.i_seekpoint + 1;
1523 i_seekpoint = val.i_int;
1525 if( i_seekpoint >= 0 && i_seekpoint <
1526 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1528 access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1529 stream_AccessReset( p_input->input.p_stream );
1531 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1532 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1537 case INPUT_CONTROL_ADD_SLAVE:
1538 if( val.psz_string )
1540 input_source_t *slave = InputSourceNew( p_input );
1542 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1545 vlc_meta_t *p_meta_new = NULL;
1550 msg_Dbg( p_input, "adding %s as slave on the fly",
1554 if( demux2_Control( p_input->input.p_demux,
1555 DEMUX_GET_TIME, &i_time ) )
1557 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1558 InputSourceClean( p_input, slave );
1562 if( demux2_Control( slave->p_demux,
1563 DEMUX_SET_TIME, i_time ) )
1565 msg_Err( p_input, "seek failed for new slave" );
1566 InputSourceClean( p_input, slave );
1572 /* Get meta (access and demux) */
1573 if( access2_Control( slave->p_access,
1574 ACCESS_GET_META, &p_meta_new ) )
1576 if( !demux2_Control( slave->p_demux,
1577 DEMUX_GET_META, &p_meta ) )
1581 vlc_meta_Merge( p_meta_new, p_meta );
1582 vlc_meta_Delete( p_meta );
1586 p_meta_new = p_meta;
1592 if( p_input->p_meta )
1594 vlc_meta_Merge( p_input->p_meta, p_meta_new );
1595 vlc_meta_Delete( p_meta_new );
1599 p_input->p_meta = p_meta_new;
1601 UpdateMeta( p_input, VLC_FALSE );
1604 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1608 msg_Warn( p_input, "failed to add %s as slave",
1612 free( val.psz_string );
1616 case INPUT_CONTROL_SET_BOOKMARK:
1618 msg_Err( p_input, "not yet implemented" );
1622 return b_force_update;
1625 /*****************************************************************************
1627 *****************************************************************************/
1628 static int UpdateFromDemux( input_thread_t *p_input )
1630 demux_t *p_demux = p_input->input.p_demux;
1633 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1635 v.i_int = p_demux->info.i_title;
1636 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1638 input_ControlVarTitle( p_input, p_demux->info.i_title );
1640 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1642 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1644 v.i_int = p_demux->info.i_seekpoint;
1645 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1647 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1649 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1651 /* Hmmm only works with master input */
1652 if( p_input->input.p_demux == p_demux )
1654 int i_title_end = p_input->input.i_title_end -
1655 p_input->input.i_title_offset;
1656 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1657 p_input->input.i_seekpoint_offset;
1659 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1661 if( p_demux->info.i_title > i_title_end ||
1662 ( p_demux->info.i_title == i_title_end &&
1663 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1665 else if( i_seekpoint_end >=0 )
1667 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1669 else if( i_title_end >= 0 )
1671 if( p_demux->info.i_title > i_title_end ) return 0;
1678 /*****************************************************************************
1680 *****************************************************************************/
1681 static int UpdateFromAccess( input_thread_t *p_input )
1683 access_t *p_access = p_input->input.p_access;
1686 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1688 v.i_int = p_access->info.i_title;
1689 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1691 input_ControlVarTitle( p_input, p_access->info.i_title );
1693 stream_AccessUpdate( p_input->input.p_stream );
1695 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1697 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1699 v.i_int = p_access->info.i_seekpoint;
1700 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1702 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1704 if( p_access->info.i_update & INPUT_UPDATE_META )
1706 /* TODO maybe multi - access ? */
1708 if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1710 if( p_input->p_meta )
1712 vlc_meta_Merge( p_input->p_meta, p_meta );
1713 vlc_meta_Delete( p_meta );
1717 p_input->p_meta = p_meta;
1720 UpdateMeta( p_input, VLC_FALSE );
1721 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1723 p_access->info.i_update &= ~INPUT_UPDATE_META;
1726 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1728 /* Hmmm only works with master input */
1729 if( p_input->input.p_access == p_access )
1731 int i_title_end = p_input->input.i_title_end -
1732 p_input->input.i_title_offset;
1733 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1734 p_input->input.i_seekpoint_offset;
1736 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1738 if( p_access->info.i_title > i_title_end ||
1739 ( p_access->info.i_title == i_title_end &&
1740 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1742 else if( i_seekpoint_end >=0 )
1744 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1746 else if( i_title_end >= 0 )
1748 if( p_access->info.i_title > i_title_end ) return 0;
1755 /*****************************************************************************
1757 *****************************************************************************/
1758 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1760 vlc_meta_t *p_meta = p_input->p_meta;
1763 if( !p_meta || p_meta->i_meta == 0 )
1766 if( !b_quick ) msg_Dbg( p_input, "meta information:" );
1767 for( i = 0; i < p_meta->i_meta; i++ )
1770 msg_Dbg( p_input, " - '%s' = '%s'",
1771 _(p_meta->name[i]), p_meta->value[i] );
1773 if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] &&
1774 !p_input->input.p_item->b_fixed_name )
1775 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1777 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
1778 input_Control( p_input, INPUT_ADD_INFO, _("General"),
1779 _("Author"), p_meta->value[i] );
1781 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
1782 _(p_meta->name[i]), "%s", p_meta->value[i] );
1785 for( i = 0; i < p_meta->i_track; i++ )
1787 vlc_meta_t *tk = p_meta->track[i];
1790 if( tk->i_meta > 0 )
1792 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1794 msg_Dbg( p_input, " - track[%d]:", i );
1796 sprintf( psz_cat, "%s %d", _("Stream"), i );
1797 for( j = 0; j < tk->i_meta; j++ )
1799 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1802 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1803 _(tk->name[j]), "%s", tk->value[j] );
1808 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1810 p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1816 /*****************************************************************************
1818 *****************************************************************************/
1819 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1820 vlc_bool_t b_quick )
1822 playlist_t *p_playlist;
1823 char psz_buffer[MSTRTIME_MAX_SIZE];
1825 vlc_mutex_lock( &p_input->input.p_item->lock );
1826 p_input->input.p_item->i_duration = i_length;
1827 vlc_mutex_unlock( &p_input->input.p_item->lock );
1829 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1833 var_SetInteger( p_playlist, "item-change",
1834 p_input->input.p_item->i_id );
1835 vlc_object_release( p_playlist );
1838 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1839 msecstotimestr( psz_buffer, i_length / 1000 ) );
1842 /*****************************************************************************
1844 *****************************************************************************/
1845 static input_source_t *InputSourceNew( input_thread_t *p_input )
1847 input_source_t *in = malloc( sizeof( input_source_t ) );
1850 in->p_access = NULL;
1851 in->p_stream = NULL;
1853 in->b_title_demux = VLC_FALSE;
1856 in->b_can_pace_control = VLC_TRUE;
1857 in->b_eof = VLC_FALSE;
1858 in->i_cr_average = 0;
1863 /*****************************************************************************
1865 *****************************************************************************/
1866 static int InputSourceInit( input_thread_t *p_input,
1867 input_source_t *in, char *psz_mrl,
1868 char *psz_forced_demux, vlc_bool_t b_quick )
1870 char *psz_dup = strdup( psz_mrl );
1879 MRLSplit( p_input, psz_dup, &psz_access, &psz_demux, &psz_path );
1881 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1882 psz_mrl, psz_access, psz_demux, psz_path );
1884 /* Hack to allow udp://@:port syntax */
1886 (strncmp( psz_access, "udp", 3 ) &&
1887 strncmp( psz_access, "rtp", 3 )) )
1889 /* Find optional titles and seekpoints */
1890 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1891 &in->i_seekpoint_start, &in->i_seekpoint_end );
1893 if( psz_forced_demux && *psz_forced_demux )
1894 psz_demux = psz_forced_demux;
1896 /* Try access_demux if no demux given */
1897 if( *psz_demux == '\0' )
1899 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1900 NULL, p_input->p_es_out, VLC_FALSE );
1906 msg_Dbg( p_input, "trying to preparse %s", psz_path );
1907 psz_demux = strdup( "" );
1908 psz_access = strdup( "file" );
1913 int64_t i_pts_delay;
1915 /* Get infos from access_demux */
1916 demux2_Control( in->p_demux,
1917 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1918 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1920 in->b_title_demux = VLC_TRUE;
1921 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1922 &in->title, &in->i_title,
1923 &in->i_title_offset, &in->i_seekpoint_offset ) )
1928 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1929 &in->b_can_pace_control );
1930 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1934 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1940 int64_t i_pts_delay;
1942 /* Now try a real access */
1943 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
1946 /* Access failed, URL encoded ? */
1947 if( in->p_access == NULL && strchr( psz_path, '%' ) )
1949 DecodeUrl( psz_path );
1951 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
1952 psz_access, psz_demux, psz_path );
1954 in->p_access = access2_New( p_input,
1955 psz_access, psz_demux, psz_path,
1958 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
1959 * are forbidden in filenames on Win32. */
1961 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
1962 if( in->p_access == NULL &&
1963 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
1966 psz_dup = strdup( psz_mrl );
1971 in->p_access = access2_New( p_input,
1972 psz_access, psz_demux, psz_path,
1977 if( in->p_access == NULL )
1979 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
1983 /* Get infos from access */
1986 access2_Control( in->p_access,
1987 ACCESS_GET_PTS_DELAY, &i_pts_delay );
1988 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1990 in->b_title_demux = VLC_FALSE;
1991 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
1992 &in->title, &in->i_title,
1993 &in->i_title_offset, &in->i_seekpoint_offset ) )
1999 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2000 &in->b_can_pace_control );
2001 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2003 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2005 var_Set( p_input, "seekable", val );
2008 /* Create the stream_t */
2009 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2010 if( in->p_stream == NULL )
2012 msg_Warn( p_input, "cannot create a stream_t from access" );
2016 /* Open a demuxer */
2017 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2019 psz_demux = in->p_access->psz_demux;
2021 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2022 in->p_stream, p_input->p_es_out, b_quick );
2023 if( in->p_demux == NULL )
2025 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2026 psz_access, psz_demux, psz_path );
2030 /* TODO get title from demux */
2031 if( !b_quick && in->i_title <= 0 )
2033 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2034 &in->title, &in->i_title,
2035 &in->i_title_offset, &in->i_seekpoint_offset ))
2042 in->b_title_demux = VLC_TRUE;
2047 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2048 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2055 demux2_Delete( in->p_demux );
2058 stream_AccessDelete( in->p_stream );
2061 access2_Delete( in->p_access );
2064 return VLC_EGENERIC;
2067 /*****************************************************************************
2069 *****************************************************************************/
2070 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2073 demux2_Delete( in->p_demux );
2076 stream_AccessDelete( in->p_stream );
2079 access2_Delete( in->p_access );
2081 if( in->i_title > 0 )
2084 for( i = 0; i < in->i_title; i++ )
2086 vlc_input_title_Delete( in->title[i] );
2092 static void SlaveDemux( input_thread_t *p_input )
2096 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2098 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2102 for( i = 0; i < p_input->i_slave; i++ )
2104 input_source_t *in = p_input->slave[i];
2110 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2115 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2117 msg_Err( p_input, "slave[%d] doesn't like "
2118 "DEMUX_GET_TIME -> EOF", i );
2123 if( i_stime >= i_time )
2126 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2132 i_ret = in->p_demux->pf_demux( in->p_demux );
2137 msg_Dbg( p_input, "slave %d EOF", i );
2138 in->b_eof = VLC_TRUE;
2143 static void SlaveSeek( input_thread_t *p_input )
2148 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2150 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2154 for( i = 0; i < p_input->i_slave; i++ )
2156 input_source_t *in = p_input->slave[i];
2158 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2160 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2161 in->b_eof = VLC_TRUE;
2165 /*****************************************************************************
2167 *****************************************************************************/
2168 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
2173 if( ( p_meta = vlc_meta_New() ) == NULL )
2176 /* Get meta information from user */
2177 #define GET_META( c, s ) \
2178 var_Get( p_input, (s), &val ); \
2179 if( *val.psz_string ) \
2180 vlc_meta_Add( p_meta, c, val.psz_string ); \
2181 free( val.psz_string )
2183 GET_META( VLC_META_TITLE, "meta-title" );
2184 GET_META( VLC_META_AUTHOR, "meta-author" );
2185 GET_META( VLC_META_ARTIST, "meta-artist" );
2186 GET_META( VLC_META_GENRE, "meta-genre" );
2187 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
2188 GET_META( VLC_META_DESCRIPTION, "meta-description" );
2189 GET_META( VLC_META_DATE, "meta-date" );
2190 GET_META( VLC_META_URL, "meta-url" );
2196 /*****************************************************************************
2197 * DecodeUrl: decode a given encoded url
2198 *****************************************************************************/
2199 static void DecodeUrl( char *psz )
2201 char *dup = strdup( psz );
2219 *psz++ = strtol( val, NULL, 16 );
2221 else if( *p == '+' )
2235 /*****************************************************************************
2236 * ParseOption: parses the options for the input
2237 *****************************************************************************
2238 * This function parses the input (config) options and creates their associated
2240 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
2241 * bar is the value of the option.
2242 *****************************************************************************/
2243 static void ParseOption( input_thread_t *p_input, const char *psz_option )
2245 char *psz_name = (char *)psz_option;
2246 char *psz_value = strchr( psz_option, '=' );
2247 int i_name_len, i_type;
2248 vlc_bool_t b_isno = VLC_FALSE;
2251 if( psz_value ) i_name_len = psz_value - psz_option;
2252 else i_name_len = strlen( psz_option );
2254 /* It's too much of an hassle to remove the ':' when we parse
2255 * the cmd line :) */
2256 if( i_name_len && *psz_name == ':' )
2262 if( i_name_len == 0 ) return;
2264 psz_name = strndup( psz_name, i_name_len );
2265 if( psz_value ) psz_value++;
2267 i_type = config_GetType( p_input, psz_name );
2269 if( !i_type && !psz_value )
2271 /* check for "no-foo" or "nofoo" */
2272 if( !strncmp( psz_name, "no-", 3 ) )
2274 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
2276 else if( !strncmp( psz_name, "no", 2 ) )
2278 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
2280 else goto cleanup; /* Option doesn't exist */
2283 i_type = config_GetType( p_input, psz_name );
2285 if( !i_type ) goto cleanup; /* Option doesn't exist */
2287 else if( !i_type ) goto cleanup; /* Option doesn't exist */
2289 if( ( i_type != VLC_VAR_BOOL ) &&
2290 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
2292 /* Create the variable in the input object.
2293 * Children of the input object will be able to retreive this value
2294 * thanks to the inheritance property of the object variables. */
2295 var_Create( p_input, psz_name, i_type );
2300 val.b_bool = !b_isno;
2303 case VLC_VAR_INTEGER:
2304 val.i_int = atoi( psz_value );
2308 val.f_float = atof( psz_value );
2311 case VLC_VAR_STRING:
2312 case VLC_VAR_MODULE:
2314 case VLC_VAR_DIRECTORY:
2315 val.psz_string = psz_value;
2323 var_Set( p_input, psz_name, val );
2325 msg_Dbg( p_input, "set input option: %s to %s", psz_name,
2326 psz_value ? psz_value : ( val.b_bool ? "true" : "false") );
2329 if( psz_name ) free( psz_name );
2333 /*****************************************************************************
2334 * MRLSplit: parse the access, demux and url part of the
2335 * Media Resource Locator.
2336 *****************************************************************************/
2337 static void MRLSplit( input_thread_t *p_input, char *psz_dup,
2338 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2340 char *psz_access = NULL;
2341 char *psz_demux = NULL;
2342 char *psz_path = NULL;
2343 char *psz, *psz_check;
2345 psz = strchr( psz_dup, ':' );
2347 /* '@' not allowed in access/demux part */
2348 psz_check = strchr( psz_dup, '@' );
2349 if( psz_check && psz_check < psz ) psz = 0;
2351 #if defined( WIN32 ) || defined( UNDER_CE )
2352 if( psz - psz_dup == 1 )
2354 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2363 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2367 psz = strchr( psz_dup, '/' );
2374 psz_access = psz_dup;
2381 if( !psz_access ) *ppsz_access = "";
2382 else *ppsz_access = psz_access;
2384 if( !psz_demux ) *ppsz_demux = "";
2385 else *ppsz_demux = psz_demux;
2387 if( !psz_path ) *ppsz_path = "";
2388 else *ppsz_path = psz_path;
2391 /*****************************************************************************
2392 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2395 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2396 *****************************************************************************/
2397 static void MRLSections( input_thread_t *p_input, char *psz_source,
2398 int *pi_title_start, int *pi_title_end,
2399 int *pi_chapter_start, int *pi_chapter_end )
2401 char *psz, *psz_end, *psz_next, *psz_check;
2403 *pi_title_start = *pi_title_end = -1;
2404 *pi_chapter_start = *pi_chapter_end = -1;
2406 /* Start by parsing titles and chapters */
2407 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2409 /* Check we are really dealing with a title/chapter section */
2410 psz_check = psz + 1;
2411 if( !*psz_check ) return;
2412 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2413 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2414 if( *psz_check == ':' && ++psz_check )
2415 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2416 if( *psz_check != '-' && *psz_check ) return;
2417 if( *psz_check == '-' && ++psz_check )
2418 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2419 if( *psz_check != ':' && *psz_check ) return;
2420 if( *psz_check == ':' && ++psz_check )
2421 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2422 if( *psz_check ) return;
2424 /* Separate start and end */
2426 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2428 /* Look for the start title */
2429 *pi_title_start = strtol( psz, &psz_next, 0 );
2430 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2431 *pi_title_end = *pi_title_start;
2434 /* Look for the start chapter */
2436 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2437 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2438 *pi_chapter_end = *pi_chapter_start;
2442 /* Look for the end title */
2443 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2444 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2447 /* Look for the end chapter */
2448 if( *psz_end ) psz_end++;
2449 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2450 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2453 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2454 psz_source, *pi_title_start, *pi_chapter_start,
2455 *pi_title_end, *pi_chapter_end );
2459 /***********************************************************************
2460 * Info management functions
2461 ***********************************************************************/
2462 char *vlc_input_item_GetInfo( input_item_t *p_i,
2463 const char *psz_cat,
2464 const char *psz_name )
2468 vlc_mutex_lock( &p_i->lock );
2470 for( i = 0 ; i< p_i->i_categories ; i++ )
2472 info_category_t *p_cat = p_i->pp_categories[i];
2474 if( !psz_cat || strcmp( p_cat->psz_name, psz_cat ) )
2477 for( j = 0; j < p_cat->i_infos ; j++ )
2479 if( !strcmp( p_cat->pp_infos[j]->psz_name, psz_name ) )
2481 vlc_mutex_unlock( &p_i->lock );
2482 return strdup( p_cat->pp_infos[j]->psz_value );
2486 vlc_mutex_unlock( &p_i->lock );
2487 return strdup( "" );
2490 int vlc_input_item_AddInfo( input_item_t *p_i,
2491 const char *psz_cat,
2492 const char *psz_name,
2493 const char *psz_format, ... )
2497 info_t *p_info = NULL;
2498 info_category_t *p_cat = NULL ;
2500 vlc_mutex_lock( &p_i->lock );
2502 for( i = 0 ; i < p_i->i_categories ; i ++ )
2504 if( !strcmp( p_i->pp_categories[i]->psz_name, psz_cat ) )
2506 p_cat = p_i->pp_categories[i];
2512 if( ( p_cat = (info_category_t *)malloc(
2513 sizeof( info_category_t ) ) ) == NULL )
2515 vlc_mutex_unlock( &p_i->lock );
2516 return VLC_EGENERIC;
2518 p_cat->psz_name = strdup( psz_cat );
2520 p_cat->pp_infos = 0;
2521 INSERT_ELEM( p_i->pp_categories, p_i->i_categories, p_i->i_categories,
2525 for( i = 0; i< p_cat->i_infos; i++ )
2527 if( !strcmp( p_cat->pp_infos[i]->psz_name, psz_name ) )
2529 p_info = p_cat->pp_infos[i];
2536 if( ( p_info = (info_t *)malloc( sizeof( info_t ) ) ) == NULL )
2538 vlc_mutex_unlock( &p_i->lock );
2539 return VLC_EGENERIC;
2541 INSERT_ELEM( p_cat->pp_infos, p_cat->i_infos, p_cat->i_infos, p_info );
2542 p_info->psz_name = strdup( psz_name );
2546 if( p_info->psz_value ) free( p_info->psz_value );
2549 va_start( args, psz_format );
2550 vasprintf( &p_info->psz_value, psz_format, args);
2553 vlc_mutex_unlock( &p_i->lock );