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 );
308 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
310 /* Now we can attach our new input */
311 vlc_object_attach( p_input, p_parent );
313 Init( p_input, VLC_TRUE );
315 /* Clean up master */
316 InputSourceClean( p_input, &p_input->input );
318 /* Kill access and demux */
319 if( p_input->input.p_access ) p_input->input.p_access->b_die = VLC_TRUE;
320 if( p_input->input.p_demux ) p_input->input.p_access->b_die = VLC_TRUE;
322 /* Unload all modules */
323 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
326 if( p_input->p_meta ) vlc_meta_Delete( p_input->p_meta );
328 vlc_object_detach( p_input );
329 vlc_object_destroy( p_input );
334 /*****************************************************************************
335 * input_StopThread: mark an input thread as zombie
336 *****************************************************************************
337 * This function should not return until the thread is effectively cancelled.
338 *****************************************************************************/
339 void input_StopThread( input_thread_t *p_input )
344 /* Set die for input */
345 p_input->b_die = VLC_TRUE;
347 /* We cannot touch p_input fields directly (we can from another thread),
348 * so use the vlc_object_find way, it's perfectly safe */
350 /* Set die for all access */
351 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
352 for( i = 0; i < p_list->i_count; i++ )
354 p_list->p_values[i].p_object->b_die = VLC_TRUE;
356 vlc_list_release( p_list );
358 /* Set die for all stream */
359 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
360 for( i = 0; i < p_list->i_count; i++ )
362 p_list->p_values[i].p_object->b_die = VLC_TRUE;
364 vlc_list_release( p_list );
366 /* Set die for all demux */
367 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
368 for( i = 0; i < p_list->i_count; i++ )
370 p_list->p_values[i].p_object->b_die = VLC_TRUE;
372 vlc_list_release( p_list );
374 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
377 /*****************************************************************************
378 * input_DestroyThread: mark an input thread as zombie
379 *****************************************************************************
380 * This function should not return until the thread is effectively cancelled.
381 *****************************************************************************/
382 void input_DestroyThread( input_thread_t *p_input )
384 /* Join the thread */
385 vlc_thread_join( p_input );
387 /* Delete input lock (only after thread joined) */
388 vlc_mutex_destroy( &p_input->lock_control );
390 /* TODO: maybe input_DestroyThread should also delete p_input instead
391 * of the playlist but I'm not sure if it's possible */
394 /*****************************************************************************
395 * Run: main thread loop
396 *****************************************************************************
397 * Thread in charge of processing the network packets and demultiplexing.
400 * read subtitle support (XXX take care of spu-delay in the right way).
401 * multi-input support (XXX may be done with subs)
402 *****************************************************************************/
403 static int Run( input_thread_t *p_input )
405 int64_t i_intf_update = 0;
407 /* Signal that the thread is launched */
408 vlc_thread_ready( p_input );
410 if( Init( p_input, VLC_FALSE ) )
412 /* If we failed, wait before we are killed, and exit */
413 p_input->b_error = VLC_TRUE;
417 /* Tell we're dead */
418 p_input->b_dead = VLC_TRUE;
424 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
426 vlc_bool_t b_force_update = VLC_FALSE;
432 if( p_input->i_state != PAUSE_S )
434 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
435 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
442 if( p_input->input.b_title_demux &&
443 p_input->input.p_demux->info.i_update )
445 i_ret = UpdateFromDemux( p_input );
446 b_force_update = VLC_TRUE;
448 else if( !p_input->input.b_title_demux &&
449 p_input->input.p_access &&
450 p_input->input.p_access->info.i_update )
452 i_ret = UpdateFromAccess( p_input );
453 b_force_update = VLC_TRUE;
457 if( i_ret == 0 ) /* EOF */
461 var_Get( p_input, "input-repeat", &repeat );
462 if( repeat.i_int == 0 )
464 /* End of file - we do not set b_die because only the
465 * playlist is allowed to do so. */
466 msg_Dbg( p_input, "EOF reached" );
467 p_input->input.b_eof = VLC_TRUE;
471 msg_Dbg( p_input, "repeating the same input (%d)",
473 if( repeat.i_int > 0 )
476 var_Set( p_input, "input-repeat", repeat );
479 /* Seek to start title/seekpoint */
480 val.i_int = p_input->input.i_title_start -
481 p_input->input.i_title_offset;
482 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
484 input_ControlPush( p_input,
485 INPUT_CONTROL_SET_TITLE, &val );
487 val.i_int = p_input->input.i_seekpoint_start -
488 p_input->input.i_seekpoint_offset;
489 if( val.i_int > 0 /* TODO: check upper boundary */ )
490 input_ControlPush( p_input,
491 INPUT_CONTROL_SET_SEEKPOINT, &val );
493 /* Seek to start position */
494 if( p_input->i_start > 0 )
496 val.i_time = p_input->i_start;
497 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
503 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
510 p_input->b_error = VLC_TRUE;
513 if( i_ret > 0 && p_input->i_slave > 0 )
515 SlaveDemux( p_input );
525 vlc_mutex_lock( &p_input->lock_control );
526 ControlReduce( p_input );
527 while( !ControlPopNoLock( p_input, &i_type, &val ) )
529 msg_Dbg( p_input, "control type=%d", i_type );
530 if( Control( p_input, i_type, val ) )
531 b_force_update = VLC_TRUE;
533 vlc_mutex_unlock( &p_input->lock_control );
535 if( b_force_update || i_intf_update < mdate() )
539 int64_t i_time, i_length;
540 /* update input status variables */
541 if( !demux2_Control( p_input->input.p_demux,
542 DEMUX_GET_POSITION, &f_pos ) )
544 val.f_float = (float)f_pos;
545 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
547 if( !demux2_Control( p_input->input.p_demux,
548 DEMUX_GET_TIME, &i_time ) )
550 p_input->i_time = i_time;
552 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
554 if( !demux2_Control( p_input->input.p_demux,
555 DEMUX_GET_LENGTH, &i_length ) )
558 var_Get( p_input, "length", &old_val );
559 val.i_time = i_length;
560 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
562 if( old_val.i_time != val.i_time )
564 UpdateItemLength( p_input, i_length, VLC_TRUE );
568 var_SetBool( p_input, "intf-change", VLC_TRUE );
569 i_intf_update = mdate() + I64C(150000);
573 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
575 /* We have finish to demux data but not to play them */
576 while( !p_input->b_die )
578 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
581 msg_Dbg( p_input, "waiting decoder fifos to empty" );
583 msleep( INPUT_IDLE_SLEEP );
586 /* We have finished */
587 p_input->b_eof = VLC_TRUE;
590 /* Wait we are asked to die */
591 if( !p_input->b_die )
603 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
609 vlc_meta_t *p_meta, *p_meta_tmp;
613 /* Initialize optional stream output. (before access/demuxer) */
616 psz = var_GetString( p_input, "sout" );
619 p_input->p_sout = sout_NewInstance( p_input, psz );
620 if( p_input->p_sout == NULL )
622 msg_Err( p_input, "cannot start stream output instance," \
632 p_input->p_es_out = input_EsOutNew( p_input );
633 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
634 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
636 if( InputSourceInit( p_input, &p_input->input,
637 p_input->input.p_item->psz_uri, NULL, b_quick ) )
642 /* Create global title (from master) */
645 p_input->i_title = p_input->input.i_title;
646 p_input->title = p_input->input.title;
647 p_input->i_title_offset = p_input->input.i_title_offset;
648 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
649 if( p_input->i_title > 0 )
651 /* Setup variables */
652 input_ControlVarNavigation( p_input );
653 input_ControlVarTitle( p_input, 0 );
657 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
658 p_input->b_can_pause = p_input->input.b_can_pause;
661 if( p_input->i_pts_delay <= 0 )
662 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
664 /* If the desynchronisation requested by the user is < 0, we need to
665 * cache more data. */
666 var_Get( p_input, "audio-desync", &val );
667 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
669 /* Update cr_average depending on the caching */
670 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
671 p_input->input.i_cr_average /= 10;
672 if( p_input->input.i_cr_average <= 0 ) p_input->input.i_cr_average = 1;
675 /* Load master infos */
677 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
678 &val.i_time ) && val.i_time > 0 )
680 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
681 UpdateItemLength( p_input, val.i_time, b_quick );
682 p_input->input.p_item->i_duration = val.i_time;
685 /* Start title/chapter */
688 val.i_int = p_input->input.i_title_start -
689 p_input->input.i_title_offset;
690 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
691 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
692 val.i_int = p_input->input.i_seekpoint_start -
693 p_input->input.i_seekpoint_offset;
694 if( val.i_int > 0 /* TODO: check upper boundary */ )
695 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
699 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
701 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
704 if( p_input->i_start > 0 )
706 if( p_input->i_start >= val.i_time )
708 msg_Warn( p_input, "invalid start-time ignored" );
714 msg_Dbg( p_input, "start-time: %ds",
715 (int)( p_input->i_start / I64C(1000000) ) );
717 s.i_time = p_input->i_start;
718 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
721 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
723 msg_Warn( p_input, "invalid stop-time ignored" );
729 /* Get fps and set it if not already set */
730 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
733 float f_requested_fps;
735 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
736 var_SetFloat( p_input, "sub-original-fps", f_fps );
738 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
739 if( f_requested_fps != f_fps )
741 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
743 var_SetFloat( p_input, "sub-fps", f_requested_fps );
747 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
750 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
754 /* Look for and add subtitle files */
755 psz_subtitle = var_GetString( p_input, "sub-file" );
762 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
764 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
767 sub = InputSourceNew( p_input );
768 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
771 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
774 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
777 if( count.i_int == 0 )
779 /* if it was first one, there is disable too */
781 if( count.i_int < list.p_list->i_count )
783 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
784 &list.p_list->p_values[count.i_int] );
786 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
792 var_Get( p_input, "sub-autodetect-file", &val );
795 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
796 char **subs = subtitles_Detect( p_input, psz_autopath,
797 p_input->input.p_item->psz_uri );
800 for( i = 0; subs[i] != NULL; i++ )
802 if( strcmp( psz_subtitle, subs[i] ) )
804 sub = InputSourceNew( p_input );
805 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
808 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
814 free( psz_autopath );
816 free( psz_subtitle );
819 psz = var_GetString( p_input, "input-slave" );
823 input_source_t *slave;
826 while( *psz == ' ' || *psz == '#' )
830 if( ( psz_delim = strchr( psz, '#' ) ) )
839 msg_Dbg( p_input, "adding slave '%s'", psz );
840 slave = InputSourceNew( p_input );
841 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
843 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
848 if( psz ) free( psz );
852 p_input->i_start = 0;
853 p_input->i_start = 0;
859 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
860 i_es_out_mode = ES_OUT_MODE_AUTO;
862 if( p_input->p_sout )
864 var_Get( p_input, "sout-all", &val );
867 i_es_out_mode = ES_OUT_MODE_ALL;
872 var_Get( p_input, "programs", &val );
873 if ( val.p_list && val.p_list->i_count )
875 i_es_out_mode = ES_OUT_MODE_PARTIAL;
876 /* Note : we should remove the "program" callback. */
879 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
883 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
885 /* Inform the demuxer about waited group (needed only for DVB) */
886 if( i_es_out_mode == ES_OUT_MODE_ALL )
888 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
890 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
892 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
897 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
898 (int) var_GetInteger( p_input, "program" ), NULL );
901 if( p_input->p_sout )
903 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
905 p_input->b_out_pace_control = VLC_FALSE;
909 p_input->b_out_pace_control = VLC_TRUE;
911 msg_Dbg( p_input, "starting in %s mode",
912 p_input->b_out_pace_control ? "asynch" : "synch" );
916 /* Get meta data from users */
917 p_meta_tmp = InputMetaUser( p_input );
919 /* Get meta data from master input */
920 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_META, &p_meta ) )
928 else if( p_meta_tmp )
930 vlc_meta_Merge( p_meta, p_meta_tmp );
931 vlc_meta_Delete( p_meta_tmp );
934 /* Access_file does not give any meta, and there are no slave */
937 if( !p_input->input.p_access ||
938 access2_Control( p_input->input.p_access, ACCESS_GET_META,
946 else if( p_meta_tmp )
948 vlc_meta_Merge( p_meta, p_meta_tmp );
949 vlc_meta_Delete( p_meta_tmp );
952 /* Get meta data from slave input */
953 for( i = 0; i < p_input->i_slave; i++ )
955 vlc_meta_t *p_meta_slave;
957 if( !demux2_Control( p_input->slave[i]->p_demux,
958 DEMUX_GET_META, &p_meta_slave ) )
962 p_meta = p_meta_slave;
964 else if( p_meta_slave )
966 vlc_meta_Merge( p_meta, p_meta_slave );
967 vlc_meta_Delete( p_meta_slave );
971 if( p_input->slave[i]->p_access &&
972 !access2_Control( p_input->slave[i]->p_access,
973 ACCESS_GET_META, &p_meta_slave ) )
977 p_meta = p_meta_slave;
979 else if( p_meta_slave )
981 vlc_meta_Merge( p_meta, p_meta_slave );
982 vlc_meta_Delete( p_meta_slave );
988 p_input->p_meta = p_meta;
989 UpdateMeta( p_input, b_quick );
993 msg_Dbg( p_input, "`%s' sucessfully opened",
994 p_input->input.p_item->psz_uri );
998 /* Trigger intf update for this item */
999 /* Playlist has a callback on this variable and will forward
1001 var_SetInteger( p_input, "item-change", p_input->input.p_item->i_id );
1003 /* initialization is complete */
1004 p_input->i_state = PLAYING_S;
1006 val.i_int = PLAYING_S;
1007 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1012 if( p_input->p_es_out )
1013 input_EsOutDelete( p_input->p_es_out );
1015 if( p_input->p_sout )
1016 sout_DeleteInstance( p_input->p_sout );
1018 /* Mark them deleted */
1019 p_input->input.p_demux = NULL;
1020 p_input->input.p_stream = NULL;
1021 p_input->input.p_access = NULL;
1022 p_input->p_es_out = NULL;
1023 p_input->p_sout = NULL;
1025 return VLC_EGENERIC;
1028 /*****************************************************************************
1029 * Error: RunThread() error loop
1030 *****************************************************************************
1031 * This function is called when an error occurred during thread main's loop.
1032 *****************************************************************************/
1033 static void Error( input_thread_t *p_input )
1035 while( !p_input->b_die )
1038 msleep( INPUT_IDLE_SLEEP );
1042 /*****************************************************************************
1043 * End: end the input thread
1044 *****************************************************************************/
1045 static void End( input_thread_t * p_input )
1050 msg_Dbg( p_input, "closing input" );
1052 /* We are at the end */
1053 p_input->i_state = END_S;
1056 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1058 /* Clean control variables */
1059 input_ControlVarClean( p_input );
1061 /* Clean up master */
1062 InputSourceClean( p_input, &p_input->input );
1065 for( i = 0; i < p_input->i_slave; i++ )
1067 InputSourceClean( p_input, p_input->slave[i] );
1068 free( p_input->slave[i] );
1070 if( p_input->slave ) free( p_input->slave );
1072 /* Unload all modules */
1073 if( p_input->p_es_out )
1074 input_EsOutDelete( p_input->p_es_out );
1076 /* Close optional stream output instance */
1077 if( p_input->p_sout )
1079 vlc_object_t *p_pl =
1080 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1083 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1085 /* attach sout to the playlist */
1086 msg_Warn( p_input, "keeping sout" );
1087 vlc_object_detach( p_input->p_sout );
1088 vlc_object_attach( p_input->p_sout, p_pl );
1092 msg_Warn( p_input, "destroying sout" );
1093 sout_DeleteInstance( p_input->p_sout );
1096 vlc_object_release( p_pl );
1100 if( p_input->p_meta )
1101 vlc_meta_Delete( p_input->p_meta );
1103 /* Tell we're dead */
1104 p_input->b_dead = VLC_TRUE;
1107 /*****************************************************************************
1109 *****************************************************************************/
1110 static inline int ControlPopNoLock( input_thread_t *p_input,
1111 int *pi_type, vlc_value_t *p_val )
1113 if( p_input->i_control <= 0 )
1115 return VLC_EGENERIC;
1118 *pi_type = p_input->control[0].i_type;
1119 *p_val = p_input->control[0].val;
1121 p_input->i_control--;
1122 if( p_input->i_control > 0 )
1126 for( i = 0; i < p_input->i_control; i++ )
1128 p_input->control[i].i_type = p_input->control[i+1].i_type;
1129 p_input->control[i].val = p_input->control[i+1].val;
1136 static void ControlReduce( input_thread_t *p_input )
1139 for( i = 1; i < p_input->i_control; i++ )
1141 const int i_lt = p_input->control[i-1].i_type;
1142 const int i_ct = p_input->control[i].i_type;
1144 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1145 msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1148 ( i_ct == INPUT_CONTROL_SET_STATE ||
1149 i_ct == INPUT_CONTROL_SET_RATE ||
1150 i_ct == INPUT_CONTROL_SET_POSITION ||
1151 i_ct == INPUT_CONTROL_SET_TIME ||
1152 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1153 i_ct == INPUT_CONTROL_SET_TITLE ||
1154 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1155 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1158 msg_Dbg( p_input, "merged at %d", i );
1159 /* Remove the i-1 */
1160 for( j = i; j < p_input->i_control; j++ )
1161 p_input->control[j-1] = p_input->control[j];
1162 p_input->i_control--;
1166 /* TODO but that's not that important
1167 - merge SET_X with SET_X_CMD
1168 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1169 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1176 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1179 vlc_bool_t b_force_update = VLC_FALSE;
1183 case INPUT_CONTROL_SET_DIE:
1184 msg_Dbg( p_input, "control: INPUT_CONTROL_SET_DIE proceed" );
1185 /* Mark all submodules to die */
1186 if( p_input->input.p_access )
1187 p_input->input.p_access->b_die = VLC_TRUE;
1188 if( p_input->input.p_stream )
1189 p_input->input.p_stream->b_die = VLC_TRUE;
1190 p_input->input.p_demux->b_die = VLC_TRUE;
1192 p_input->b_die = VLC_TRUE;
1195 case INPUT_CONTROL_SET_POSITION:
1196 case INPUT_CONTROL_SET_POSITION_OFFSET:
1199 if( i_type == INPUT_CONTROL_SET_POSITION )
1201 f_pos = val.f_float;
1205 /* Should not fail */
1206 demux2_Control( p_input->input.p_demux,
1207 DEMUX_GET_POSITION, &f_pos );
1208 f_pos += val.f_float;
1210 if( f_pos < 0.0 ) f_pos = 0.0;
1211 if( f_pos > 1.0 ) f_pos = 1.0;
1212 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1215 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1216 "%2.1f%% failed", f_pos * 100 );
1220 if( p_input->i_slave > 0 )
1221 SlaveSeek( p_input );
1223 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1224 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1225 b_force_update = VLC_TRUE;
1230 case INPUT_CONTROL_SET_TIME:
1231 case INPUT_CONTROL_SET_TIME_OFFSET:
1236 if( i_type == INPUT_CONTROL_SET_TIME )
1238 i_time = val.i_time;
1242 /* Should not fail */
1243 demux2_Control( p_input->input.p_demux,
1244 DEMUX_GET_TIME, &i_time );
1245 i_time += val.i_time;
1247 if( i_time < 0 ) i_time = 0;
1248 i_ret = demux2_Control( p_input->input.p_demux,
1249 DEMUX_SET_TIME, i_time );
1253 /* Emulate it with a SET_POS */
1255 demux2_Control( p_input->input.p_demux,
1256 DEMUX_GET_LENGTH, &i_length );
1259 double f_pos = (double)i_time / (double)i_length;
1260 i_ret = demux2_Control( p_input->input.p_demux,
1261 DEMUX_SET_POSITION, f_pos );
1266 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1267 " failed", i_time );
1271 if( p_input->i_slave > 0 )
1272 SlaveSeek( p_input );
1274 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1276 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1277 b_force_update = VLC_TRUE;
1282 case INPUT_CONTROL_SET_STATE:
1283 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1284 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1287 if( p_input->input.p_access )
1288 i_ret = access2_Control( p_input->input.p_access,
1289 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1291 i_ret = demux2_Control( p_input->input.p_demux,
1292 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1296 /* FIXME What to do ? */
1297 msg_Warn( p_input, "cannot unset pause -> EOF" );
1298 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1301 b_force_update = VLC_TRUE;
1303 /* Switch to play */
1304 p_input->i_state = PLAYING_S;
1305 val.i_int = PLAYING_S;
1306 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1309 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1311 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1312 p_input->b_can_pause )
1315 if( p_input->input.p_access )
1316 i_ret = access2_Control( p_input->input.p_access,
1317 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1319 i_ret = demux2_Control( p_input->input.p_demux,
1320 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1322 b_force_update = VLC_TRUE;
1326 msg_Warn( p_input, "cannot set pause state" );
1327 val.i_int = p_input->i_state;
1331 val.i_int = PAUSE_S;
1334 /* Switch to new state */
1335 p_input->i_state = val.i_int;
1336 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1338 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1340 b_force_update = VLC_TRUE;
1342 /* Correct "state" value */
1343 val.i_int = p_input->i_state;
1344 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1346 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1348 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1352 case INPUT_CONTROL_SET_RATE:
1353 case INPUT_CONTROL_SET_RATE_SLOWER:
1354 case INPUT_CONTROL_SET_RATE_FASTER:
1358 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1359 i_rate = p_input->i_rate * 2;
1360 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1361 i_rate = p_input->i_rate / 2;
1365 if( i_rate < INPUT_RATE_MIN )
1367 msg_Dbg( p_input, "cannot set rate faster" );
1368 i_rate = INPUT_RATE_MIN;
1370 else if( i_rate > INPUT_RATE_MAX )
1372 msg_Dbg( p_input, "cannot set rate slower" );
1373 i_rate = INPUT_RATE_MAX;
1375 if( i_rate != INPUT_RATE_DEFAULT &&
1376 ( !p_input->b_can_pace_control ||
1377 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1379 msg_Dbg( p_input, "cannot change rate" );
1380 i_rate = INPUT_RATE_DEFAULT;
1382 if( i_rate != p_input->i_rate )
1384 p_input->i_rate = i_rate;
1386 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1388 /* We haven't send data to decoder when rate != default */
1389 if( i_rate == INPUT_RATE_DEFAULT )
1390 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1393 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1395 b_force_update = VLC_TRUE;
1400 case INPUT_CONTROL_SET_PROGRAM:
1401 /* No need to force update, es_out does it if needed */
1402 es_out_Control( p_input->p_es_out,
1403 ES_OUT_SET_GROUP, val.i_int );
1405 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1409 case INPUT_CONTROL_SET_ES:
1410 /* No need to force update, es_out does it if needed */
1411 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1412 input_EsOutGetFromID( p_input->p_es_out,
1416 case INPUT_CONTROL_SET_AUDIO_DELAY:
1417 input_EsOutSetDelay( p_input->p_es_out,
1418 AUDIO_ES, val.i_time );
1419 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1422 case INPUT_CONTROL_SET_SPU_DELAY:
1423 input_EsOutSetDelay( p_input->p_es_out,
1424 SPU_ES, val.i_time );
1425 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1428 case INPUT_CONTROL_SET_TITLE:
1429 case INPUT_CONTROL_SET_TITLE_NEXT:
1430 case INPUT_CONTROL_SET_TITLE_PREV:
1431 if( p_input->input.b_title_demux &&
1432 p_input->input.i_title > 0 )
1435 /* FIXME handle demux title */
1436 demux_t *p_demux = p_input->input.p_demux;
1439 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1440 i_title = p_demux->info.i_title - 1;
1441 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1442 i_title = p_demux->info.i_title + 1;
1444 i_title = val.i_int;
1446 if( i_title >= 0 && i_title < p_input->input.i_title )
1448 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1450 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1451 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1453 input_ControlVarTitle( p_input, i_title );
1456 else if( p_input->input.i_title > 0 )
1458 access_t *p_access = p_input->input.p_access;
1461 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1462 i_title = p_access->info.i_title - 1;
1463 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1464 i_title = p_access->info.i_title + 1;
1466 i_title = val.i_int;
1468 if( i_title >= 0 && i_title < p_input->input.i_title )
1470 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1471 stream_AccessReset( p_input->input.p_stream );
1473 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1474 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1478 case INPUT_CONTROL_SET_SEEKPOINT:
1479 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1480 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1481 if( p_input->input.b_title_demux &&
1482 p_input->input.i_title > 0 )
1484 demux_t *p_demux = p_input->input.p_demux;
1487 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1488 i_seekpoint = p_demux->info.i_seekpoint - 1;
1489 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1490 i_seekpoint = p_demux->info.i_seekpoint + 1;
1492 i_seekpoint = val.i_int;
1494 if( i_seekpoint >= 0 && i_seekpoint <
1495 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1497 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1499 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1500 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1503 else if( p_input->input.i_title > 0 )
1505 access_t *p_access = p_input->input.p_access;
1508 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1509 i_seekpoint = p_access->info.i_seekpoint - 1;
1510 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1511 i_seekpoint = p_access->info.i_seekpoint + 1;
1513 i_seekpoint = val.i_int;
1515 if( i_seekpoint >= 0 && i_seekpoint <
1516 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1518 access2_Control( p_access, ACCESS_SET_SEEKPOINT, i_seekpoint );
1519 stream_AccessReset( p_input->input.p_stream );
1521 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1522 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1527 case INPUT_CONTROL_ADD_SLAVE:
1528 if( val.psz_string )
1530 input_source_t *slave = InputSourceNew( p_input );
1532 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1535 vlc_meta_t *p_meta_new = NULL;
1540 msg_Dbg( p_input, "adding %s as slave on the fly",
1544 if( demux2_Control( p_input->input.p_demux,
1545 DEMUX_GET_TIME, &i_time ) )
1547 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1548 InputSourceClean( p_input, slave );
1552 if( demux2_Control( slave->p_demux,
1553 DEMUX_SET_TIME, i_time ) )
1555 msg_Err( p_input, "seek failed for new slave" );
1556 InputSourceClean( p_input, slave );
1562 /* Get meta (access and demux) */
1563 if( access2_Control( slave->p_access,
1564 ACCESS_GET_META, &p_meta_new ) )
1566 if( !demux2_Control( slave->p_demux,
1567 DEMUX_GET_META, &p_meta ) )
1571 vlc_meta_Merge( p_meta_new, p_meta );
1572 vlc_meta_Delete( p_meta );
1576 p_meta_new = p_meta;
1582 if( p_input->p_meta )
1584 vlc_meta_Merge( p_input->p_meta, p_meta_new );
1585 vlc_meta_Delete( p_meta_new );
1589 p_input->p_meta = p_meta_new;
1591 UpdateMeta( p_input, VLC_FALSE );
1594 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1598 msg_Warn( p_input, "failed to add %s as slave",
1602 free( val.psz_string );
1606 case INPUT_CONTROL_SET_BOOKMARK:
1608 msg_Err( p_input, "not yet implemented" );
1612 return b_force_update;
1615 /*****************************************************************************
1617 *****************************************************************************/
1618 static int UpdateFromDemux( input_thread_t *p_input )
1620 demux_t *p_demux = p_input->input.p_demux;
1623 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1625 v.i_int = p_demux->info.i_title;
1626 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1628 input_ControlVarTitle( p_input, p_demux->info.i_title );
1630 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1632 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1634 v.i_int = p_demux->info.i_seekpoint;
1635 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1637 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1639 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1641 /* Hmmm only works with master input */
1642 if( p_input->input.p_demux == p_demux )
1644 int i_title_end = p_input->input.i_title_end -
1645 p_input->input.i_title_offset;
1646 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1647 p_input->input.i_seekpoint_offset;
1649 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1651 if( p_demux->info.i_title > i_title_end ||
1652 ( p_demux->info.i_title == i_title_end &&
1653 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1655 else if( i_seekpoint_end >=0 )
1657 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1659 else if( i_title_end >= 0 )
1661 if( p_demux->info.i_title > i_title_end ) return 0;
1668 /*****************************************************************************
1670 *****************************************************************************/
1671 static int UpdateFromAccess( input_thread_t *p_input )
1673 access_t *p_access = p_input->input.p_access;
1676 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1678 v.i_int = p_access->info.i_title;
1679 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1681 input_ControlVarTitle( p_input, p_access->info.i_title );
1683 stream_AccessUpdate( p_input->input.p_stream );
1685 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1687 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1689 v.i_int = p_access->info.i_seekpoint;
1690 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1692 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1694 if( p_access->info.i_update & INPUT_UPDATE_META )
1696 /* TODO maybe multi - access ? */
1698 if( !access2_Control( p_input->input.p_access,ACCESS_GET_META,&p_meta))
1700 if( p_input->p_meta )
1702 vlc_meta_Merge( p_input->p_meta, p_meta );
1703 vlc_meta_Delete( p_meta );
1707 p_input->p_meta = p_meta;
1710 UpdateMeta( p_input, VLC_FALSE );
1711 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1713 p_access->info.i_update &= ~INPUT_UPDATE_META;
1716 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1718 /* Hmmm only works with master input */
1719 if( p_input->input.p_access == p_access )
1721 int i_title_end = p_input->input.i_title_end -
1722 p_input->input.i_title_offset;
1723 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1724 p_input->input.i_seekpoint_offset;
1726 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1728 if( p_access->info.i_title > i_title_end ||
1729 ( p_access->info.i_title == i_title_end &&
1730 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1732 else if( i_seekpoint_end >=0 )
1734 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1736 else if( i_title_end >= 0 )
1738 if( p_access->info.i_title > i_title_end ) return 0;
1745 /*****************************************************************************
1747 *****************************************************************************/
1748 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1750 vlc_meta_t *p_meta = p_input->p_meta;
1753 if( !p_meta || p_meta->i_meta == 0 )
1756 if( !b_quick ) msg_Dbg( p_input, "meta information:" );
1757 for( i = 0; i < p_meta->i_meta; i++ )
1760 msg_Dbg( p_input, " - '%s' = '%s'",
1761 _(p_meta->name[i]), p_meta->value[i] );
1763 if( !strcmp(p_meta->name[i], VLC_META_TITLE) && p_meta->value[i] &&
1764 !p_input->input.p_item->b_fixed_name )
1765 input_Control( p_input, INPUT_SET_NAME, p_meta->value[i] );
1767 if( !strcmp( p_meta->name[i], VLC_META_AUTHOR ) )
1768 input_Control( p_input, INPUT_ADD_INFO, _("General"),
1769 _("Author"), p_meta->value[i] );
1771 input_Control( p_input, INPUT_ADD_INFO, _("Meta-information"),
1772 _(p_meta->name[i]), "%s", p_meta->value[i] );
1775 for( i = 0; i < p_meta->i_track; i++ )
1777 vlc_meta_t *tk = p_meta->track[i];
1780 if( tk->i_meta > 0 )
1782 char *psz_cat = malloc( strlen(_("Stream")) + 10 );
1784 msg_Dbg( p_input, " - track[%d]:", i );
1786 sprintf( psz_cat, "%s %d", _("Stream"), i );
1787 for( j = 0; j < tk->i_meta; j++ )
1789 msg_Dbg( p_input, " - '%s' = '%s'", _(tk->name[j]),
1792 input_Control( p_input, INPUT_ADD_INFO, psz_cat,
1793 _(tk->name[j]), "%s", tk->value[j] );
1798 if( p_input->p_sout && p_input->p_sout->p_meta == NULL )
1800 p_input->p_sout->p_meta = vlc_meta_Duplicate( p_meta );
1806 /*****************************************************************************
1808 *****************************************************************************/
1809 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1810 vlc_bool_t b_quick )
1812 playlist_t *p_playlist;
1813 char psz_buffer[MSTRTIME_MAX_SIZE];
1815 vlc_mutex_lock( &p_input->input.p_item->lock );
1816 p_input->input.p_item->i_duration = i_length;
1817 vlc_mutex_unlock( &p_input->input.p_item->lock );
1819 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1823 var_SetInteger( p_playlist, "item-change",
1824 p_input->input.p_item->i_id );
1825 vlc_object_release( p_playlist );
1828 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1829 msecstotimestr( psz_buffer, i_length / 1000 ) );
1832 /*****************************************************************************
1834 *****************************************************************************/
1835 static input_source_t *InputSourceNew( input_thread_t *p_input )
1837 input_source_t *in = malloc( sizeof( input_source_t ) );
1840 in->p_access = NULL;
1841 in->p_stream = NULL;
1843 in->b_title_demux = VLC_FALSE;
1846 in->b_can_pace_control = VLC_TRUE;
1847 in->b_eof = VLC_FALSE;
1848 in->i_cr_average = 0;
1853 /*****************************************************************************
1855 *****************************************************************************/
1856 static int InputSourceInit( input_thread_t *p_input,
1857 input_source_t *in, char *psz_mrl,
1858 char *psz_forced_demux, vlc_bool_t b_quick )
1860 char *psz_dup = strdup( psz_mrl );
1869 MRLSplit( p_input, psz_dup, &psz_access, &psz_demux, &psz_path );
1871 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1872 psz_mrl, psz_access, psz_demux, psz_path );
1874 /* Hack to allow udp://@:port syntax */
1876 (strncmp( psz_access, "udp", 3 ) &&
1877 strncmp( psz_access, "rtp", 3 )) )
1879 /* Find optional titles and seekpoints */
1880 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1881 &in->i_seekpoint_start, &in->i_seekpoint_end );
1883 if( psz_forced_demux && *psz_forced_demux )
1884 psz_demux = psz_forced_demux;
1886 /* Try access_demux if no demux given */
1887 if( *psz_demux == '\0' )
1889 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1890 NULL, p_input->p_es_out, VLC_FALSE );
1896 msg_Dbg( p_input, "trying to preparse %s", psz_path );
1897 psz_demux = strdup( "" );
1898 psz_access = strdup( "file" );
1903 int64_t i_pts_delay;
1905 /* Get infos from access_demux */
1906 demux2_Control( in->p_demux,
1907 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1908 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1910 in->b_title_demux = VLC_TRUE;
1911 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1912 &in->title, &in->i_title,
1913 &in->i_title_offset, &in->i_seekpoint_offset ) )
1918 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1919 &in->b_can_pace_control );
1920 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1924 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1930 int64_t i_pts_delay;
1932 /* Now try a real access */
1933 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
1936 /* Access failed, URL encoded ? */
1937 if( in->p_access == NULL && strchr( psz_path, '%' ) )
1939 DecodeUrl( psz_path );
1941 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
1942 psz_access, psz_demux, psz_path );
1944 in->p_access = access2_New( p_input,
1945 psz_access, psz_demux, psz_path,
1948 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
1949 * are forbidden in filenames on Win32. */
1951 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
1952 if( in->p_access == NULL &&
1953 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
1956 psz_dup = strdup( psz_mrl );
1961 in->p_access = access2_New( p_input,
1962 psz_access, psz_demux, psz_path,
1967 if( in->p_access == NULL )
1969 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
1973 /* Get infos from access */
1976 access2_Control( in->p_access,
1977 ACCESS_GET_PTS_DELAY, &i_pts_delay );
1978 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1980 in->b_title_demux = VLC_FALSE;
1981 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
1982 &in->title, &in->i_title,
1983 &in->i_title_offset, &in->i_seekpoint_offset ) )
1989 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
1990 &in->b_can_pace_control );
1991 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
1993 access2_Control( in->p_access, ACCESS_CAN_SEEK,
1995 var_Set( p_input, "seekable", val );
1998 /* Create the stream_t */
1999 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2000 if( in->p_stream == NULL )
2002 msg_Warn( p_input, "cannot create a stream_t from access" );
2006 /* Open a demuxer */
2007 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2009 psz_demux = in->p_access->psz_demux;
2011 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2012 in->p_stream, p_input->p_es_out, b_quick );
2013 if( in->p_demux == NULL )
2015 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2016 psz_access, psz_demux, psz_path );
2020 /* TODO get title from demux */
2021 if( !b_quick && in->i_title <= 0 )
2023 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2024 &in->title, &in->i_title,
2025 &in->i_title_offset, &in->i_seekpoint_offset ))
2032 in->b_title_demux = VLC_TRUE;
2041 demux2_Delete( in->p_demux );
2044 stream_AccessDelete( in->p_stream );
2047 access2_Delete( in->p_access );
2050 return VLC_EGENERIC;
2053 /*****************************************************************************
2055 *****************************************************************************/
2056 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2059 demux2_Delete( in->p_demux );
2062 stream_AccessDelete( in->p_stream );
2065 access2_Delete( in->p_access );
2067 if( in->i_title > 0 )
2070 for( i = 0; i < in->i_title; i++ )
2072 vlc_input_title_Delete( in->title[i] );
2078 static void SlaveDemux( input_thread_t *p_input )
2082 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2084 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2088 for( i = 0; i < p_input->i_slave; i++ )
2090 input_source_t *in = p_input->slave[i];
2096 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2101 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2103 msg_Err( p_input, "slave[%d] doesn't like "
2104 "DEMUX_GET_TIME -> EOF", i );
2109 if( i_stime >= i_time )
2112 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2118 i_ret = in->p_demux->pf_demux( in->p_demux );
2123 msg_Dbg( p_input, "slave %d EOF", i );
2124 in->b_eof = VLC_TRUE;
2129 static void SlaveSeek( input_thread_t *p_input )
2134 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2136 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2140 for( i = 0; i < p_input->i_slave; i++ )
2142 input_source_t *in = p_input->slave[i];
2144 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2146 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2147 in->b_eof = VLC_TRUE;
2151 /*****************************************************************************
2153 *****************************************************************************/
2154 static vlc_meta_t *InputMetaUser( input_thread_t *p_input )
2159 if( ( p_meta = vlc_meta_New() ) == NULL )
2162 /* Get meta information from user */
2163 #define GET_META( c, s ) \
2164 var_Get( p_input, (s), &val ); \
2165 if( *val.psz_string ) \
2166 vlc_meta_Add( p_meta, c, val.psz_string ); \
2167 free( val.psz_string )
2169 GET_META( VLC_META_TITLE, "meta-title" );
2170 GET_META( VLC_META_AUTHOR, "meta-author" );
2171 GET_META( VLC_META_ARTIST, "meta-artist" );
2172 GET_META( VLC_META_GENRE, "meta-genre" );
2173 GET_META( VLC_META_COPYRIGHT, "meta-copyright" );
2174 GET_META( VLC_META_DESCRIPTION, "meta-description" );
2175 GET_META( VLC_META_DATE, "meta-date" );
2176 GET_META( VLC_META_URL, "meta-url" );
2182 /*****************************************************************************
2183 * DecodeUrl: decode a given encoded url
2184 *****************************************************************************/
2185 static void DecodeUrl( char *psz )
2187 char *dup = strdup( psz );
2205 *psz++ = strtol( val, NULL, 16 );
2207 else if( *p == '+' )
2221 /*****************************************************************************
2222 * ParseOption: parses the options for the input
2223 *****************************************************************************
2224 * This function parses the input (config) options and creates their associated
2226 * Options are of the form "[no[-]]foo[=bar]" where foo is the option name and
2227 * bar is the value of the option.
2228 *****************************************************************************/
2229 static void ParseOption( input_thread_t *p_input, const char *psz_option )
2231 char *psz_name = (char *)psz_option;
2232 char *psz_value = strchr( psz_option, '=' );
2233 int i_name_len, i_type;
2234 vlc_bool_t b_isno = VLC_FALSE;
2237 if( psz_value ) i_name_len = psz_value - psz_option;
2238 else i_name_len = strlen( psz_option );
2240 /* It's too much of an hassle to remove the ':' when we parse
2241 * the cmd line :) */
2242 if( i_name_len && *psz_name == ':' )
2248 if( i_name_len == 0 ) return;
2250 psz_name = strndup( psz_name, i_name_len );
2251 if( psz_value ) psz_value++;
2253 i_type = config_GetType( p_input, psz_name );
2255 if( !i_type && !psz_value )
2257 /* check for "no-foo" or "nofoo" */
2258 if( !strncmp( psz_name, "no-", 3 ) )
2260 memmove( psz_name, psz_name + 3, strlen(psz_name) + 1 - 3 );
2262 else if( !strncmp( psz_name, "no", 2 ) )
2264 memmove( psz_name, psz_name + 2, strlen(psz_name) + 1 - 2 );
2266 else goto cleanup; /* Option doesn't exist */
2269 i_type = config_GetType( p_input, psz_name );
2271 if( !i_type ) goto cleanup; /* Option doesn't exist */
2273 else if( !i_type ) goto cleanup; /* Option doesn't exist */
2275 if( ( i_type != VLC_VAR_BOOL ) &&
2276 ( !psz_value || !*psz_value ) ) goto cleanup; /* Invalid value */
2278 /* Create the variable in the input object.
2279 * Children of the input object will be able to retreive this value
2280 * thanks to the inheritance property of the object variables. */
2281 var_Create( p_input, psz_name, i_type );
2286 val.b_bool = !b_isno;
2289 case VLC_VAR_INTEGER:
2290 val.i_int = atoi( psz_value );
2294 val.f_float = atof( psz_value );
2297 case VLC_VAR_STRING:
2298 case VLC_VAR_MODULE:
2300 case VLC_VAR_DIRECTORY:
2301 val.psz_string = psz_value;
2309 var_Set( p_input, psz_name, val );
2311 msg_Dbg( p_input, "set input option: %s to %s", psz_name, psz_value ? psz_value : ( val.b_bool ? "true" : "false") );
2314 if( psz_name ) free( psz_name );
2318 /*****************************************************************************
2319 * MRLSplit: parse the access, demux and url part of the
2320 * Media Resource Locator.
2321 *****************************************************************************/
2322 static void MRLSplit( input_thread_t *p_input, char *psz_dup,
2323 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2325 char *psz_access = NULL;
2326 char *psz_demux = NULL;
2327 char *psz_path = NULL;
2328 char *psz, *psz_check;
2330 psz = strchr( psz_dup, ':' );
2332 /* '@' not allowed in access/demux part */
2333 psz_check = strchr( psz_dup, '@' );
2334 if( psz_check && psz_check < psz ) psz = 0;
2336 #if defined( WIN32 ) || defined( UNDER_CE )
2337 if( psz - psz_dup == 1 )
2339 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2348 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2352 psz = strchr( psz_dup, '/' );
2359 psz_access = psz_dup;
2366 if( !psz_access ) *ppsz_access = "";
2367 else *ppsz_access = psz_access;
2369 if( !psz_demux ) *ppsz_demux = "";
2370 else *ppsz_demux = psz_demux;
2372 if( !psz_path ) *ppsz_path = "";
2373 else *ppsz_path = psz_path;
2376 /*****************************************************************************
2377 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2380 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2381 *****************************************************************************/
2382 static void MRLSections( input_thread_t *p_input, char *psz_source,
2383 int *pi_title_start, int *pi_title_end,
2384 int *pi_chapter_start, int *pi_chapter_end )
2386 char *psz, *psz_end, *psz_next, *psz_check;
2388 *pi_title_start = *pi_title_end = -1;
2389 *pi_chapter_start = *pi_chapter_end = -1;
2391 /* Start by parsing titles and chapters */
2392 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2394 /* Check we are really dealing with a title/chapter section */
2395 psz_check = psz + 1;
2396 if( !*psz_check ) return;
2397 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2398 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2399 if( *psz_check == ':' && ++psz_check )
2400 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2401 if( *psz_check != '-' && *psz_check ) return;
2402 if( *psz_check == '-' && ++psz_check )
2403 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2404 if( *psz_check != ':' && *psz_check ) return;
2405 if( *psz_check == ':' && ++psz_check )
2406 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2407 if( *psz_check ) return;
2409 /* Separate start and end */
2411 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2413 /* Look for the start title */
2414 *pi_title_start = strtol( psz, &psz_next, 0 );
2415 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2416 *pi_title_end = *pi_title_start;
2419 /* Look for the start chapter */
2421 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2422 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2423 *pi_chapter_end = *pi_chapter_start;
2427 /* Look for the end title */
2428 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2429 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2432 /* Look for the end chapter */
2433 if( *psz_end ) psz_end++;
2434 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2435 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2438 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2439 psz_source, *pi_title_start, *pi_chapter_start,
2440 *pi_title_end, *pi_chapter_end );