1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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"
41 #include "vlc_interaction.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Run ( input_thread_t *p_input );
47 static int RunAndClean ( input_thread_t *p_input );
49 static input_thread_t * Create ( vlc_object_t *, input_item_t *, char *,
51 static int Init ( input_thread_t *p_input, vlc_bool_t b_quick );
52 static void Error ( input_thread_t *p_input );
53 static void End ( input_thread_t *p_input );
54 static void MainLoop( input_thread_t *p_input );
56 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
57 static void ControlReduce( input_thread_t * );
58 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
60 static int UpdateFromAccess( input_thread_t * );
61 static int UpdateFromDemux( input_thread_t * );
62 static int UpdateMeta( input_thread_t *, vlc_bool_t );
64 static void UpdateItemLength( input_thread_t *, int64_t i_length, vlc_bool_t );
66 static void DecodeUrl( char * );
67 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
69 static input_source_t *InputSourceNew( input_thread_t *);
70 static int InputSourceInit( input_thread_t *, input_source_t *,
71 char *, char *psz_forced_demux,
73 static void InputSourceClean( input_thread_t *, input_source_t * );
75 static void SlaveDemux( input_thread_t *p_input );
76 static void SlaveSeek( input_thread_t *p_input );
78 static void InputMetaUser( input_thread_t *p_input );
80 /*****************************************************************************
81 * This function creates a new input, and returns a pointer
82 * to its description. On error, it returns NULL.
84 * Variables for _public_ use:
87 * - rate,rate-slower, rate-faster
88 * - position, position-offset
90 * - title,title-next,title-prev
91 * - chapter,chapter-next, chapter-prev
92 * - program, audio-es, video-es, spu-es
93 * - audio-delay, spu-delay
98 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
99 * * For intf callback upon changes
101 * TODO explain when Callback is called
102 * TODO complete this list (?)
103 *****************************************************************************/
104 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
105 char *psz_header, vlc_bool_t b_quick )
107 input_thread_t *p_input = NULL; /* thread descriptor */
111 /* Allocate descriptor */
112 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
113 if( p_input == NULL )
115 msg_Err( p_parent, "out of memory" );
118 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
120 /* Init Common fields */
121 p_input->b_eof = VLC_FALSE;
122 p_input->b_can_pace_control = VLC_TRUE;
123 p_input->i_start = 0;
126 p_input->i_title = 0;
127 p_input->title = NULL;
128 p_input->i_title_offset = p_input->i_seekpoint_offset = 0;
129 p_input->i_state = INIT_S;
130 p_input->i_rate = INPUT_RATE_DEFAULT;
131 p_input->i_bookmark = 0;
132 p_input->bookmark = NULL;
133 p_input->p_meta = NULL;
134 p_input->p_es_out = NULL;
135 p_input->p_sout = NULL;
136 p_input->b_out_pace_control = VLC_FALSE;
137 p_input->i_pts_delay = 0;
139 /* Init Input fields */
140 p_input->input.p_item = p_item;
141 p_input->input.p_access = NULL;
142 p_input->input.p_stream = NULL;
143 p_input->input.p_demux = NULL;
144 p_input->input.b_title_demux = VLC_FALSE;
145 p_input->input.i_title = 0;
146 p_input->input.title = NULL;
147 p_input->input.i_title_offset = p_input->input.i_seekpoint_offset = 0;
148 p_input->input.b_can_pace_control = VLC_TRUE;
149 p_input->input.b_eof = VLC_FALSE;
150 p_input->input.i_cr_average = 0;
152 p_input->input.p_item->p_meta = vlc_meta_New();
153 stats_ReinitInputStats( p_item->p_stats );
156 p_input->i_slave = 0;
157 p_input->slave = NULL;
159 /* Init control buffer */
160 vlc_mutex_init( p_input, &p_input->lock_control );
161 p_input->i_control = 0;
163 /* Parse input options */
164 vlc_mutex_lock( &p_item->lock );
165 for( i = 0; i < p_item->i_options; i++ )
167 var_OptionParse( p_input, p_item->ppsz_options[i] );
169 vlc_mutex_unlock( &p_item->lock );
171 /* Create Object Variables for private use only */
172 input_ConfigVarInit( p_input );
174 /* Create Objects variables for public Get and Set */
175 input_ControlVarInit( p_input );
176 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
180 var_Get( p_input, "bookmarks", &val );
183 /* FIXME: have a common cfg parsing routine used by sout and others */
184 char *psz_parser, *psz_start, *psz_end;
185 psz_parser = val.psz_string;
186 while( (psz_start = strchr( psz_parser, '{' ) ) )
188 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
191 psz_end = strchr( psz_start, '}' );
192 if( !psz_end ) break;
193 psz_parser = psz_end + 1;
194 backup = *psz_parser;
197 while( (psz_end = strchr( psz_start, ',' ) ) )
200 if( !strncmp( psz_start, "name=", 5 ) )
202 p_seekpoint->psz_name = psz_start + 5;
204 else if( !strncmp( psz_start, "bytes=", 6 ) )
206 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
208 else if( !strncmp( psz_start, "time=", 5 ) )
210 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
213 psz_start = psz_end + 1;
215 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
216 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
217 p_seekpoint->i_time_offset );
218 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
219 vlc_seekpoint_Delete( p_seekpoint );
220 *psz_parser = backup;
222 free( val.psz_string );
226 /* Remove 'Now playing' info as it is probably outdated */
227 input_Control( p_input, INPUT_DEL_INFO, _(VLC_META_INFO_CAT),
228 VLC_META_NOW_PLAYING ); /* ? Don't translate as it might has been copied ? */
234 * Initialize an input thread and run it. You will need to monitor the thread to clean
235 * up after it is done
237 * \param p_parent a vlc_object
238 * \param p_item an input item
239 * \return a pointer to the spawned input thread
241 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
242 input_item_t *p_item )
244 return __input_CreateThread2( p_parent, p_item, NULL );
249 input_thread_t *__input_CreateThread2( vlc_object_t *p_parent,
250 input_item_t *p_item,
253 input_thread_t *p_input = NULL; /* thread descriptor */
255 p_input = Create( p_parent, p_item, psz_header, VLC_FALSE );
256 /* Now we can attach our new input */
257 vlc_object_attach( p_input, p_parent );
259 /* Create thread and wait for its readiness. */
260 if( vlc_thread_create( p_input, "input", Run,
261 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
263 msg_Err( p_input, "cannot create input thread" );
264 vlc_object_detach( p_input );
265 vlc_object_destroy( p_input );
274 * Initialize an input thread and run it. This thread will clean after himself,
275 * you can forget about it. It can work either in blocking or non-blocking mode
277 * \param p_parent a vlc_object
278 * \param p_item an input item
279 * \param b_block should we block until read is finished ?
280 * \return the input object id if non blocking, an error code else
282 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
285 input_thread_t *p_input = NULL; /* thread descriptor */
287 p_input = Create( p_parent, p_item, NULL, VLC_FALSE );
288 /* Now we can attach our new input */
289 vlc_object_attach( p_input, p_parent );
293 RunAndClean( p_input );
298 if( vlc_thread_create( p_input, "input", RunAndClean,
299 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
301 msg_Err( p_input, "cannot create input thread" );
302 vlc_object_detach( p_input );
303 vlc_object_destroy( p_input );
307 return p_input->i_object_id;
311 * Initialize an input and initialize it to preparse the item
312 * This function is blocking. It will only accept to parse files
314 * \param p_parent a vlc_object_t
315 * \param p_item an input item
316 * \return VLC_SUCCESS or an error
318 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
320 input_thread_t *p_input = NULL; /* thread descriptor */
322 /* Allocate descriptor */
323 p_input = Create( p_parent, p_item, NULL, VLC_TRUE );
324 p_input->i_flags |= OBJECT_FLAGS_NODBG;
325 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
327 /* Now we can attach our new input */
328 vlc_object_attach( p_input, p_parent );
330 Init( p_input, VLC_TRUE );
332 /* Clean up master */
333 InputSourceClean( p_input, &p_input->input );
335 /* Unload all modules */
336 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
338 vlc_object_detach( p_input );
339 vlc_object_destroy( p_input );
345 * Request a running input thread to stop and die
347 * \param the input thread to stop
349 void input_StopThread( input_thread_t *p_input )
354 /* Set die for input */
355 p_input->b_die = VLC_TRUE;
357 /* We cannot touch p_input fields directly (we can from another thread),
358 * so use the vlc_object_find way, it's perfectly safe */
360 /* Set die for all access */
361 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
362 for( i = 0; i < p_list->i_count; i++ )
364 p_list->p_values[i].p_object->b_die = VLC_TRUE;
366 vlc_list_release( p_list );
368 /* Set die for all stream */
369 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
370 for( i = 0; i < p_list->i_count; i++ )
372 p_list->p_values[i].p_object->b_die = VLC_TRUE;
374 vlc_list_release( p_list );
376 /* Set die for all demux */
377 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
378 for( i = 0; i < p_list->i_count; i++ )
380 p_list->p_values[i].p_object->b_die = VLC_TRUE;
382 vlc_list_release( p_list );
384 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
388 * Clean up a dead input thread
389 * This function does not return until the thread is effectively cancelled.
391 * \param the input thread to kill
393 void input_DestroyThread( input_thread_t *p_input )
395 /* Join the thread */
396 vlc_thread_join( p_input );
398 /* Delete input lock (only after thread joined) */
399 vlc_mutex_destroy( &p_input->lock_control );
401 /* TODO: maybe input_DestroyThread should also delete p_input instead
402 * of the playlist but I'm not sure if it's possible */
405 /*****************************************************************************
406 * Run: main thread loop
407 * This is the "normal" thread that spawns the input processing chain,
408 * reads the stream, cleans up and waits
409 *****************************************************************************/
410 static int Run( input_thread_t *p_input )
412 /* Signal that the thread is launched */
413 vlc_thread_ready( p_input );
415 if( Init( p_input, VLC_FALSE ) )
417 /* If we failed, wait before we are killed, and exit */
418 p_input->b_error = VLC_TRUE;
422 /* Tell we're dead */
423 p_input->b_dead = VLC_TRUE;
430 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
432 /* We have finish to demux data but not to play them */
433 while( !p_input->b_die )
435 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
438 msg_Dbg( p_input, "waiting decoder fifos to empty" );
440 msleep( INPUT_IDLE_SLEEP );
443 /* We have finished */
444 p_input->b_eof = VLC_TRUE;
447 /* Wait we are asked to die */
448 if( !p_input->b_die )
459 /*****************************************************************************
460 * RunAndClean: main thread loop
461 * This is the "just forget me" thread that spawns the input processing chain,
462 * reads the stream, cleans up and releases memory
463 *****************************************************************************/
464 static int RunAndClean( input_thread_t *p_input )
466 /* Signal that the thread is launched */
467 vlc_thread_ready( p_input );
469 if( Init( p_input, VLC_FALSE ) )
471 /* If we failed, just exit */
477 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
479 /* We have finish to demux data but not to play them */
480 while( !p_input->b_die )
482 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
485 msg_Dbg( p_input, "waiting decoder fifos to empty" );
487 msleep( INPUT_IDLE_SLEEP );
489 /* We have finished */
490 p_input->b_eof = VLC_TRUE;
497 vlc_object_detach( p_input );
498 vlc_object_destroy( p_input );
504 /*****************************************************************************
505 * Main loop: Fill buffers from access, and demux
506 *****************************************************************************/
507 static void MainLoop( input_thread_t *p_input )
509 int64_t i_intf_update = 0;
510 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
512 vlc_bool_t b_force_update = VLC_FALSE;
518 if( p_input->i_state != PAUSE_S )
520 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
521 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
528 if( p_input->input.b_title_demux &&
529 p_input->input.p_demux->info.i_update )
531 i_ret = UpdateFromDemux( p_input );
532 b_force_update = VLC_TRUE;
534 else if( !p_input->input.b_title_demux &&
535 p_input->input.p_access &&
536 p_input->input.p_access->info.i_update )
538 i_ret = UpdateFromAccess( p_input );
539 b_force_update = VLC_TRUE;
543 if( i_ret == 0 ) /* EOF */
547 var_Get( p_input, "input-repeat", &repeat );
548 if( repeat.i_int == 0 )
550 /* End of file - we do not set b_die because only the
551 * playlist is allowed to do so. */
552 msg_Dbg( p_input, "EOF reached" );
553 p_input->input.b_eof = VLC_TRUE;
557 msg_Dbg( p_input, "repeating the same input (%d)",
559 if( repeat.i_int > 0 )
562 var_Set( p_input, "input-repeat", repeat );
565 /* Seek to start title/seekpoint */
566 val.i_int = p_input->input.i_title_start -
567 p_input->input.i_title_offset;
568 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
570 input_ControlPush( p_input,
571 INPUT_CONTROL_SET_TITLE, &val );
573 val.i_int = p_input->input.i_seekpoint_start -
574 p_input->input.i_seekpoint_offset;
575 if( val.i_int > 0 /* TODO: check upper boundary */ )
576 input_ControlPush( p_input,
577 INPUT_CONTROL_SET_SEEKPOINT, &val );
579 /* Seek to start position */
580 if( p_input->i_start > 0 )
582 val.i_time = p_input->i_start;
583 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
589 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
596 p_input->b_error = VLC_TRUE;
599 if( i_ret > 0 && p_input->i_slave > 0 )
601 SlaveDemux( p_input );
611 vlc_mutex_lock( &p_input->lock_control );
612 ControlReduce( p_input );
613 while( !ControlPopNoLock( p_input, &i_type, &val ) )
615 msg_Dbg( p_input, "control type=%d", i_type );
616 if( Control( p_input, i_type, val ) )
617 b_force_update = VLC_TRUE;
619 vlc_mutex_unlock( &p_input->lock_control );
621 if( b_force_update || i_intf_update < mdate() )
625 int64_t i_time, i_length;
626 /* update input status variables */
627 if( !demux2_Control( p_input->input.p_demux,
628 DEMUX_GET_POSITION, &f_pos ) )
630 val.f_float = (float)f_pos;
631 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
633 if( !demux2_Control( p_input->input.p_demux,
634 DEMUX_GET_TIME, &i_time ) )
636 p_input->i_time = i_time;
638 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
640 if( !demux2_Control( p_input->input.p_demux,
641 DEMUX_GET_LENGTH, &i_length ) )
644 var_Get( p_input, "length", &old_val );
645 val.i_time = i_length;
646 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
648 if( old_val.i_time != val.i_time )
650 UpdateItemLength( p_input, i_length, VLC_TRUE );
654 var_SetBool( p_input, "intf-change", VLC_TRUE );
655 i_intf_update = mdate() + I64C(150000);
661 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
671 /* Initialize optional stream output. (before access/demuxer)
672 * XXX: we add a special case if the uri starts by vlc.
673 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
674 * be destroyed in case of a file).
675 * (this will break playing of file starting by 'vlc:' but I don't
676 * want to add more logic, just force file by file:// or code it ;)
680 /* Prepare statistics */
681 counter_t *p_counter;
682 stats_Create( p_input, "read_bytes", STATS_READ_BYTES,
683 VLC_VAR_INTEGER, STATS_COUNTER );
684 stats_Create( p_input, "read_packets", STATS_READ_PACKETS,
685 VLC_VAR_INTEGER, STATS_COUNTER );
686 stats_Create( p_input, "demux_read", STATS_DEMUX_READ,
687 VLC_VAR_INTEGER, STATS_COUNTER );
688 stats_Create( p_input, "input_bitrate", STATS_INPUT_BITRATE,
689 VLC_VAR_FLOAT, STATS_DERIVATIVE );
690 stats_Create( p_input, "demux_bitrate", STATS_DEMUX_BITRATE,
691 VLC_VAR_FLOAT, STATS_DERIVATIVE );
693 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
694 STATS_INPUT_BITRATE );
695 if( p_counter ) p_counter->update_interval = 1000000;
696 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
697 STATS_DEMUX_BITRATE );
698 if( p_counter ) p_counter->update_interval = 1000000;
700 stats_Create( p_input, "played_abuffers", STATS_PLAYED_ABUFFERS,
701 VLC_VAR_INTEGER, STATS_COUNTER );
702 stats_Create( p_input, "lost_abuffers", STATS_LOST_ABUFFERS,
703 VLC_VAR_INTEGER, STATS_COUNTER );
706 psz = var_GetString( p_input, "sout" );
707 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
709 p_input->p_sout = sout_NewInstance( p_input, psz );
710 if( p_input->p_sout == NULL )
712 msg_Err( p_input, "cannot start stream output instance, " \
722 p_input->p_es_out = input_EsOutNew( p_input );
723 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
724 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
726 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
727 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
729 if( InputSourceInit( p_input, &p_input->input,
730 p_input->input.p_item->psz_uri, NULL, b_quick ) )
735 /* Create global title (from master) */
738 p_input->i_title = p_input->input.i_title;
739 p_input->title = p_input->input.title;
740 p_input->i_title_offset = p_input->input.i_title_offset;
741 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
742 if( p_input->i_title > 0 )
744 /* Setup variables */
745 input_ControlVarNavigation( p_input );
746 input_ControlVarTitle( p_input, 0 );
750 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
751 p_input->b_can_pause = p_input->input.b_can_pause;
754 if( p_input->i_pts_delay < 0 )
755 p_input->i_pts_delay = 0;
757 /* If the desynchronisation requested by the user is < 0, we need to
758 * cache more data. */
759 var_Get( p_input, "audio-desync", &val );
760 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
762 /* Update cr_average depending on the caching */
763 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
764 p_input->input.i_cr_average /= 10;
765 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
768 /* Load master infos */
770 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
771 &val.i_time ) && val.i_time > 0 )
773 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
774 UpdateItemLength( p_input, val.i_time, b_quick );
775 p_input->input.p_item->i_duration = val.i_time;
778 /* Start title/chapter */
781 val.i_int = p_input->input.i_title_start -
782 p_input->input.i_title_offset;
783 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
784 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
785 val.i_int = p_input->input.i_seekpoint_start -
786 p_input->input.i_seekpoint_offset;
787 if( val.i_int > 0 /* TODO: check upper boundary */ )
788 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
792 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
794 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
797 if( p_input->i_start > 0 )
799 if( p_input->i_start >= val.i_time )
801 msg_Warn( p_input, "invalid start-time ignored" );
807 msg_Dbg( p_input, "starting at time: %ds",
808 (int)( p_input->i_start / I64C(1000000) ) );
810 s.i_time = p_input->i_start;
811 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
814 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
816 msg_Warn( p_input, "invalid stop-time ignored" );
822 /* Get fps and set it if not already set */
823 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
826 float f_requested_fps;
828 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
829 var_SetFloat( p_input, "sub-original-fps", f_fps );
831 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
832 if( f_requested_fps != f_fps )
834 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
836 var_SetFloat( p_input, "sub-fps", f_requested_fps );
840 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
843 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
847 /* Look for and add subtitle files */
848 psz_subtitle = var_GetString( p_input, "sub-file" );
855 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
857 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
860 sub = InputSourceNew( p_input );
861 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
864 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
867 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
870 if( count.i_int == 0 )
872 /* if it was first one, there is disable too */
874 if( count.i_int < list.p_list->i_count )
876 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
877 &list.p_list->p_values[count.i_int] );
879 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
885 var_Get( p_input, "sub-autodetect-file", &val );
888 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
889 char **subs = subtitles_Detect( p_input, psz_autopath,
890 p_input->input.p_item->psz_uri );
893 for( i = 0; subs && subs[i]; i++ )
895 if( strcmp( psz_subtitle, subs[i] ) )
897 sub = InputSourceNew( p_input );
898 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
901 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
906 if( subs ) free( subs );
907 if( psz_autopath ) free( psz_autopath );
909 free( psz_subtitle );
912 psz = var_GetString( p_input, "input-slave" );
916 input_source_t *slave;
919 while( *psz == ' ' || *psz == '#' )
923 if( ( psz_delim = strchr( psz, '#' ) ) )
932 msg_Dbg( p_input, "adding slave input '%s'", psz );
933 slave = InputSourceNew( p_input );
934 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
936 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
941 if( psz ) free( psz );
945 p_input->i_start = 0;
946 p_input->i_start = 0;
952 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
953 i_es_out_mode = ES_OUT_MODE_AUTO;
955 if( p_input->p_sout )
957 var_Get( p_input, "sout-all", &val );
960 i_es_out_mode = ES_OUT_MODE_ALL;
965 var_Get( p_input, "programs", &val );
966 if ( val.p_list && val.p_list->i_count )
968 i_es_out_mode = ES_OUT_MODE_PARTIAL;
969 /* Note : we should remove the "program" callback. */
972 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
976 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
978 /* Inform the demuxer about waited group (needed only for DVB) */
979 if( i_es_out_mode == ES_OUT_MODE_ALL )
981 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
983 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
985 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
990 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
991 (int) var_GetInteger( p_input, "program" ), NULL );
994 if( p_input->p_sout )
996 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
998 p_input->b_out_pace_control = VLC_FALSE;
1002 p_input->b_out_pace_control = VLC_TRUE;
1005 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
1007 /* We don't want a high input priority here or we'll
1008 * end-up sucking up all the CPU time */
1009 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1012 msg_Dbg( p_input, "starting in %s mode",
1013 p_input->b_out_pace_control ? "asynch" : "synch" );
1017 p_meta = p_input->input.p_item->p_meta;
1018 /* Get meta data from users */
1019 InputMetaUser( p_input );
1020 /* Get meta data from master input */
1021 demux2_Control( p_input->input.p_demux, DEMUX_GET_META, p_meta );
1023 /* Access_file does not give any meta, and there are no slave */
1026 if( p_input->input.p_access )
1027 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1030 /* Get meta data from slave input */
1031 for( i = 0; i < p_input->i_slave; i++ )
1033 demux2_Control( p_input->slave[i]->p_demux,
1034 DEMUX_GET_META, p_meta );
1035 if( p_input->slave[i]->p_access )
1037 access2_Control( p_input->slave[i]->p_access,
1038 ACCESS_GET_META, p_meta );
1043 UpdateMeta( p_input, b_quick );
1047 msg_Dbg( p_input, "`%s' successfully opened",
1048 p_input->input.p_item->psz_uri );
1052 /* initialization is complete */
1053 p_input->i_state = PLAYING_S;
1055 val.i_int = PLAYING_S;
1056 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1061 if( p_input->p_es_out )
1062 input_EsOutDelete( p_input->p_es_out );
1064 if( p_input->p_sout )
1065 sout_DeleteInstance( p_input->p_sout );
1067 /* Mark them deleted */
1068 p_input->input.p_demux = NULL;
1069 p_input->input.p_stream = NULL;
1070 p_input->input.p_access = NULL;
1071 p_input->p_es_out = NULL;
1072 p_input->p_sout = NULL;
1074 return VLC_EGENERIC;
1077 /*****************************************************************************
1078 * Error: RunThread() error loop
1079 *****************************************************************************
1080 * This function is called when an error occurred during thread main's loop.
1081 *****************************************************************************/
1082 static void Error( input_thread_t *p_input )
1084 while( !p_input->b_die )
1087 msleep( INPUT_IDLE_SLEEP );
1091 /*****************************************************************************
1092 * End: end the input thread
1093 *****************************************************************************/
1094 static void End( input_thread_t * p_input )
1099 msg_Dbg( p_input, "closing input" );
1101 /* We are at the end */
1102 p_input->i_state = END_S;
1105 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1107 /* Clean control variables */
1108 input_ControlVarClean( p_input );
1110 /* Clean up master */
1111 InputSourceClean( p_input, &p_input->input );
1114 for( i = 0; i < p_input->i_slave; i++ )
1116 InputSourceClean( p_input, p_input->slave[i] );
1117 free( p_input->slave[i] );
1119 if( p_input->slave ) free( p_input->slave );
1121 /* Unload all modules */
1122 if( p_input->p_es_out )
1123 input_EsOutDelete( p_input->p_es_out );
1125 /* Close optional stream output instance */
1126 if( p_input->p_sout )
1128 vlc_object_t *p_pl =
1129 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1132 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1134 /* attach sout to the playlist */
1135 msg_Dbg( p_input, "keeping sout" );
1136 vlc_object_detach( p_input->p_sout );
1137 vlc_object_attach( p_input->p_sout, p_pl );
1141 msg_Dbg( p_input, "destroying sout" );
1142 sout_DeleteInstance( p_input->p_sout );
1145 vlc_object_release( p_pl );
1148 /* Tell we're dead */
1149 p_input->b_dead = VLC_TRUE;
1152 /*****************************************************************************
1154 *****************************************************************************/
1155 static inline int ControlPopNoLock( input_thread_t *p_input,
1156 int *pi_type, vlc_value_t *p_val )
1158 if( p_input->i_control <= 0 )
1160 return VLC_EGENERIC;
1163 *pi_type = p_input->control[0].i_type;
1164 *p_val = p_input->control[0].val;
1166 p_input->i_control--;
1167 if( p_input->i_control > 0 )
1171 for( i = 0; i < p_input->i_control; i++ )
1173 p_input->control[i].i_type = p_input->control[i+1].i_type;
1174 p_input->control[i].val = p_input->control[i+1].val;
1181 static void ControlReduce( input_thread_t *p_input )
1184 for( i = 1; i < p_input->i_control; i++ )
1186 const int i_lt = p_input->control[i-1].i_type;
1187 const int i_ct = p_input->control[i].i_type;
1189 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1190 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1194 ( i_ct == INPUT_CONTROL_SET_STATE ||
1195 i_ct == INPUT_CONTROL_SET_RATE ||
1196 i_ct == INPUT_CONTROL_SET_POSITION ||
1197 i_ct == INPUT_CONTROL_SET_TIME ||
1198 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1199 i_ct == INPUT_CONTROL_SET_TITLE ||
1200 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1201 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1204 // msg_Dbg( p_input, "merged at %d", i );
1205 /* Remove the i-1 */
1206 for( j = i; j < p_input->i_control; j++ )
1207 p_input->control[j-1] = p_input->control[j];
1208 p_input->i_control--;
1212 /* TODO but that's not that important
1213 - merge SET_X with SET_X_CMD
1214 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1215 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1222 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1225 vlc_bool_t b_force_update = VLC_FALSE;
1227 if( !p_input ) return b_force_update;
1231 case INPUT_CONTROL_SET_DIE:
1232 msg_Dbg( p_input, "control: stopping input" );
1233 /* Mark all submodules to die */
1234 if( p_input->input.p_access )
1235 p_input->input.p_access->b_die = VLC_TRUE;
1236 if( p_input->input.p_stream )
1237 p_input->input.p_stream->b_die = VLC_TRUE;
1238 p_input->input.p_demux->b_die = VLC_TRUE;
1240 p_input->b_die = VLC_TRUE;
1243 case INPUT_CONTROL_SET_POSITION:
1244 case INPUT_CONTROL_SET_POSITION_OFFSET:
1247 if( i_type == INPUT_CONTROL_SET_POSITION )
1249 f_pos = val.f_float;
1253 /* Should not fail */
1254 demux2_Control( p_input->input.p_demux,
1255 DEMUX_GET_POSITION, &f_pos );
1256 f_pos += val.f_float;
1258 if( f_pos < 0.0 ) f_pos = 0.0;
1259 if( f_pos > 1.0 ) f_pos = 1.0;
1260 /* Reset the decoders states and clock synch (before calling the demuxer */
1261 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1262 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1263 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1266 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1267 "%2.1f%% failed", f_pos * 100 );
1271 if( p_input->i_slave > 0 )
1272 SlaveSeek( p_input );
1274 b_force_update = VLC_TRUE;
1279 case INPUT_CONTROL_SET_TIME:
1280 case INPUT_CONTROL_SET_TIME_OFFSET:
1285 if( i_type == INPUT_CONTROL_SET_TIME )
1287 i_time = val.i_time;
1291 /* Should not fail */
1292 demux2_Control( p_input->input.p_demux,
1293 DEMUX_GET_TIME, &i_time );
1294 i_time += val.i_time;
1296 if( i_time < 0 ) i_time = 0;
1298 /* Reset the decoders states and clock synch (before calling the demuxer */
1299 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1300 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1302 i_ret = demux2_Control( p_input->input.p_demux,
1303 DEMUX_SET_TIME, i_time );
1308 /* Emulate it with a SET_POS */
1309 demux2_Control( p_input->input.p_demux,
1310 DEMUX_GET_LENGTH, &i_length );
1313 double f_pos = (double)i_time / (double)i_length;
1314 i_ret = demux2_Control( p_input->input.p_demux,
1315 DEMUX_SET_POSITION, f_pos );
1320 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1321 " failed or not possible", i_time );
1325 if( p_input->i_slave > 0 )
1326 SlaveSeek( p_input );
1328 b_force_update = VLC_TRUE;
1333 case INPUT_CONTROL_SET_STATE:
1334 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1335 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1338 if( p_input->input.p_access )
1339 i_ret = access2_Control( p_input->input.p_access,
1340 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1342 i_ret = demux2_Control( p_input->input.p_demux,
1343 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1347 /* FIXME What to do ? */
1348 msg_Warn( p_input, "cannot unset pause -> EOF" );
1349 vlc_mutex_unlock( &p_input->lock_control );
1350 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1351 vlc_mutex_lock( &p_input->lock_control );
1354 b_force_update = VLC_TRUE;
1356 /* Switch to play */
1357 p_input->i_state = PLAYING_S;
1358 val.i_int = PLAYING_S;
1359 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1362 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1363 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1365 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1366 p_input->b_can_pause )
1369 if( p_input->input.p_access )
1370 i_ret = access2_Control( p_input->input.p_access,
1371 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1373 i_ret = demux2_Control( p_input->input.p_demux,
1374 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1376 b_force_update = VLC_TRUE;
1380 msg_Warn( p_input, "cannot set pause state" );
1381 val.i_int = p_input->i_state;
1385 val.i_int = PAUSE_S;
1388 /* Switch to new state */
1389 p_input->i_state = val.i_int;
1390 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1392 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1394 b_force_update = VLC_TRUE;
1396 /* Correct "state" value */
1397 val.i_int = p_input->i_state;
1398 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1400 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1402 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1406 case INPUT_CONTROL_SET_RATE:
1407 case INPUT_CONTROL_SET_RATE_SLOWER:
1408 case INPUT_CONTROL_SET_RATE_FASTER:
1412 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1413 i_rate = p_input->i_rate * 2;
1414 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1415 i_rate = p_input->i_rate / 2;
1419 if( i_rate < INPUT_RATE_MIN )
1421 msg_Dbg( p_input, "cannot set rate faster" );
1422 i_rate = INPUT_RATE_MIN;
1424 else if( i_rate > INPUT_RATE_MAX )
1426 msg_Dbg( p_input, "cannot set rate slower" );
1427 i_rate = INPUT_RATE_MAX;
1429 if( i_rate != INPUT_RATE_DEFAULT &&
1430 ( !p_input->b_can_pace_control ||
1431 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1433 msg_Dbg( p_input, "cannot change rate" );
1434 i_rate = INPUT_RATE_DEFAULT;
1436 if( i_rate != p_input->i_rate )
1438 p_input->i_rate = i_rate;
1440 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1442 /* We haven't send data to decoder when rate != default */
1443 if( i_rate == INPUT_RATE_DEFAULT )
1444 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1447 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1449 b_force_update = VLC_TRUE;
1454 case INPUT_CONTROL_SET_PROGRAM:
1455 /* No need to force update, es_out does it if needed */
1456 es_out_Control( p_input->p_es_out,
1457 ES_OUT_SET_GROUP, val.i_int );
1459 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1463 case INPUT_CONTROL_SET_ES:
1464 /* No need to force update, es_out does it if needed */
1465 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1466 input_EsOutGetFromID( p_input->p_es_out,
1470 case INPUT_CONTROL_SET_AUDIO_DELAY:
1471 input_EsOutSetDelay( p_input->p_es_out,
1472 AUDIO_ES, val.i_time );
1473 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1476 case INPUT_CONTROL_SET_SPU_DELAY:
1477 input_EsOutSetDelay( p_input->p_es_out,
1478 SPU_ES, val.i_time );
1479 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1482 case INPUT_CONTROL_SET_TITLE:
1483 case INPUT_CONTROL_SET_TITLE_NEXT:
1484 case INPUT_CONTROL_SET_TITLE_PREV:
1485 if( p_input->input.b_title_demux &&
1486 p_input->input.i_title > 0 )
1489 /* FIXME handle demux title */
1490 demux_t *p_demux = p_input->input.p_demux;
1493 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1494 i_title = p_demux->info.i_title - 1;
1495 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1496 i_title = p_demux->info.i_title + 1;
1498 i_title = val.i_int;
1500 if( i_title >= 0 && i_title < p_input->input.i_title )
1502 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1503 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1505 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1506 input_ControlVarTitle( p_input, i_title );
1509 else if( p_input->input.i_title > 0 )
1511 access_t *p_access = p_input->input.p_access;
1514 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1515 i_title = p_access->info.i_title - 1;
1516 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1517 i_title = p_access->info.i_title + 1;
1519 i_title = val.i_int;
1521 if( i_title >= 0 && i_title < p_input->input.i_title )
1523 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1524 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1526 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1527 stream_AccessReset( p_input->input.p_stream );
1531 case INPUT_CONTROL_SET_SEEKPOINT:
1532 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1533 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1534 if( p_input->input.b_title_demux &&
1535 p_input->input.i_title > 0 )
1537 demux_t *p_demux = p_input->input.p_demux;
1539 int64_t i_input_time;
1540 int64_t i_seekpoint_time;
1542 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1544 i_seekpoint = p_demux->info.i_seekpoint;
1545 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1546 if( i_seekpoint_time >= 0 &&
1547 !demux2_Control( p_demux,
1548 DEMUX_GET_TIME, &i_input_time ) )
1550 if ( i_input_time < i_seekpoint_time + 3000000 )
1556 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1557 i_seekpoint = p_demux->info.i_seekpoint + 1;
1559 i_seekpoint = val.i_int;
1561 if( i_seekpoint >= 0 && i_seekpoint <
1562 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1564 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1565 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1567 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1570 else if( p_input->input.i_title > 0 )
1572 demux_t *p_demux = p_input->input.p_demux;
1573 access_t *p_access = p_input->input.p_access;
1575 int64_t i_input_time;
1576 int64_t i_seekpoint_time;
1578 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1580 i_seekpoint = p_access->info.i_seekpoint;
1581 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1582 if( i_seekpoint_time >= 0 &&
1583 demux2_Control( p_demux,
1584 DEMUX_GET_TIME, &i_input_time ) )
1586 if ( i_input_time < i_seekpoint_time + 3000000 )
1592 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1593 i_seekpoint = p_access->info.i_seekpoint + 1;
1595 i_seekpoint = val.i_int;
1597 if( i_seekpoint >= 0 && i_seekpoint <
1598 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1600 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1601 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1603 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1605 stream_AccessReset( p_input->input.p_stream );
1610 case INPUT_CONTROL_ADD_SLAVE:
1611 if( val.psz_string )
1613 input_source_t *slave = InputSourceNew( p_input );
1615 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1618 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1622 msg_Dbg( p_input, "adding %s as slave on the fly",
1626 if( demux2_Control( p_input->input.p_demux,
1627 DEMUX_GET_TIME, &i_time ) )
1629 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1630 InputSourceClean( p_input, slave );
1634 if( demux2_Control( slave->p_demux,
1635 DEMUX_SET_TIME, i_time ) )
1637 msg_Err( p_input, "seek failed for new slave" );
1638 InputSourceClean( p_input, slave );
1643 /* Get meta (access and demux) */
1644 access2_Control( slave->p_access, ACCESS_GET_META,
1646 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1647 UpdateMeta( p_input, VLC_FALSE );
1649 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1653 msg_Warn( p_input, "failed to add %s as slave",
1657 free( val.psz_string );
1661 case INPUT_CONTROL_SET_BOOKMARK:
1663 msg_Err( p_input, "not yet implemented" );
1667 return b_force_update;
1670 /*****************************************************************************
1672 *****************************************************************************/
1673 static int UpdateFromDemux( input_thread_t *p_input )
1675 demux_t *p_demux = p_input->input.p_demux;
1678 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1680 v.i_int = p_demux->info.i_title;
1681 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1683 input_ControlVarTitle( p_input, p_demux->info.i_title );
1685 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1687 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1689 v.i_int = p_demux->info.i_seekpoint;
1690 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1692 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1694 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1696 /* Hmmm only works with master input */
1697 if( p_input->input.p_demux == p_demux )
1699 int i_title_end = p_input->input.i_title_end -
1700 p_input->input.i_title_offset;
1701 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1702 p_input->input.i_seekpoint_offset;
1704 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1706 if( p_demux->info.i_title > i_title_end ||
1707 ( p_demux->info.i_title == i_title_end &&
1708 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1710 else if( i_seekpoint_end >=0 )
1712 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1714 else if( i_title_end >= 0 )
1716 if( p_demux->info.i_title > i_title_end ) return 0;
1723 /*****************************************************************************
1725 *****************************************************************************/
1726 static int UpdateFromAccess( input_thread_t *p_input )
1728 access_t *p_access = p_input->input.p_access;
1731 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1733 v.i_int = p_access->info.i_title;
1734 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1736 input_ControlVarTitle( p_input, p_access->info.i_title );
1738 stream_AccessUpdate( p_input->input.p_stream );
1740 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1742 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1744 v.i_int = p_access->info.i_seekpoint;
1745 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1746 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1748 if( p_access->info.i_update & INPUT_UPDATE_META )
1750 /* TODO maybe multi - access ? */
1751 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1752 access2_Control( p_input->input.p_access,ACCESS_GET_META, p_meta );
1753 UpdateMeta( p_input, VLC_FALSE );
1754 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1755 p_access->info.i_update &= ~INPUT_UPDATE_META;
1758 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1760 /* Hmmm only works with master input */
1761 if( p_input->input.p_access == p_access )
1763 int i_title_end = p_input->input.i_title_end -
1764 p_input->input.i_title_offset;
1765 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1766 p_input->input.i_seekpoint_offset;
1768 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1770 if( p_access->info.i_title > i_title_end ||
1771 ( p_access->info.i_title == i_title_end &&
1772 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1774 else if( i_seekpoint_end >=0 )
1776 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1778 else if( i_title_end >= 0 )
1780 if( p_access->info.i_title > i_title_end ) return 0;
1787 /*****************************************************************************
1789 *****************************************************************************/
1790 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1792 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1796 if( p_meta->psz_title && !p_input->input.p_item->b_fixed_name )
1797 input_Control( p_input, INPUT_SET_NAME, p_meta->psz_title );
1799 /** \todo handle sout meta */
1804 /*****************************************************************************
1806 *****************************************************************************/
1807 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1808 vlc_bool_t b_quick )
1810 playlist_t *p_playlist;
1811 char psz_buffer[MSTRTIME_MAX_SIZE];
1813 vlc_mutex_lock( &p_input->input.p_item->lock );
1814 p_input->input.p_item->i_duration = i_length;
1815 vlc_mutex_unlock( &p_input->input.p_item->lock );
1817 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1821 var_SetInteger( p_playlist, "item-change",
1822 p_input->input.p_item->i_id );
1823 vlc_object_release( p_playlist );
1826 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1827 msecstotimestr( psz_buffer, i_length / 1000 ) );
1830 /*****************************************************************************
1832 *****************************************************************************/
1833 static input_source_t *InputSourceNew( input_thread_t *p_input )
1835 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1839 msg_Err( p_input, "out of memory for new input source" );
1844 in->p_access = NULL;
1845 in->p_stream = NULL;
1847 in->b_title_demux = VLC_FALSE;
1850 in->b_can_pace_control = VLC_TRUE;
1851 in->b_eof = VLC_FALSE;
1852 in->i_cr_average = 0;
1857 /*****************************************************************************
1859 *****************************************************************************/
1860 static int InputSourceInit( input_thread_t *p_input,
1861 input_source_t *in, char *psz_mrl,
1862 char *psz_forced_demux, vlc_bool_t b_quick )
1864 char *psz_dup = strdup( psz_mrl );
1872 if( !in ) return VLC_EGENERIC;
1877 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1878 &psz_access, &psz_demux, &psz_path );
1880 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1881 psz_mrl, psz_access, psz_demux, psz_path );
1883 /* Hack to allow udp://@:port syntax */
1885 (strncmp( psz_access, "udp", 3 ) &&
1886 strncmp( psz_access, "rtp", 3 )) )
1888 /* Find optional titles and seekpoints */
1889 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1890 &in->i_seekpoint_start, &in->i_seekpoint_end );
1892 if( psz_forced_demux && *psz_forced_demux )
1894 psz_demux = psz_forced_demux;
1896 else if( !psz_demux || *psz_demux == '\0' )
1898 /* special hack for forcing a demuxer with --demux=module
1899 * (and do nothing with a list) */
1900 char *psz_var_demux = var_GetString( p_input, "demux" );
1902 if( *psz_var_demux != '\0' &&
1903 !strchr(psz_var_demux, ',' ) &&
1904 !strchr(psz_var_demux, ':' ) )
1906 psz_demux = psz_var_demux;
1908 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
1910 else if( psz_var_demux )
1912 free( psz_var_demux );
1916 /* Try access_demux if no demux given */
1917 if( *psz_demux == '\0' )
1919 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1920 NULL, p_input->p_es_out, VLC_FALSE );
1926 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
1927 psz_demux = strdup( "" );
1928 psz_access = strdup( "file" );
1933 int64_t i_pts_delay;
1935 /* Get infos from access_demux */
1936 demux2_Control( in->p_demux,
1937 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1938 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1940 in->b_title_demux = VLC_TRUE;
1941 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1942 &in->title, &in->i_title,
1943 &in->i_title_offset, &in->i_seekpoint_offset ) )
1948 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1949 &in->b_can_pace_control );
1950 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1954 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1960 int64_t i_pts_delay;
1962 /* Now try a real access */
1963 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
1966 /* Access failed, URL encoded ? */
1967 if( in->p_access == NULL && strchr( psz_path, '%' ) )
1969 DecodeUrl( psz_path );
1971 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
1972 psz_access, psz_demux, psz_path );
1974 in->p_access = access2_New( p_input,
1975 psz_access, psz_demux, psz_path,
1978 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
1979 * are forbidden in filenames on Win32. */
1981 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
1982 if( in->p_access == NULL &&
1983 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
1985 if( psz_dup ) free( psz_dup );
1986 psz_dup = strdup( psz_mrl );
1988 if( psz_forced_demux && *psz_forced_demux )
1990 psz_demux = psz_forced_demux;
1992 else psz_demux = "";
1995 in->p_access = access2_New( p_input,
1996 psz_access, psz_demux, psz_path,
2001 if( in->p_access == NULL )
2003 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2004 intf_UserFatal( VLC_OBJECT( p_input),
2005 _("Errors"),"Unable to open '%s'", psz_mrl );
2010 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2011 while( psz && *psz )
2013 access_t *p_access = in->p_access;
2014 char *end = strchr( psz, ':' );
2019 in->p_access = access2_FilterNew( in->p_access, psz );
2020 if( in->p_access == NULL )
2022 in->p_access = p_access;
2023 msg_Warn( p_input, "failed to insert access filter %s",
2029 if( psz_tmp ) free( psz_tmp );
2031 /* Get infos from access */
2034 access2_Control( in->p_access,
2035 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2036 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2038 in->b_title_demux = VLC_FALSE;
2039 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2040 &in->title, &in->i_title,
2041 &in->i_title_offset, &in->i_seekpoint_offset ) )
2047 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2048 &in->b_can_pace_control );
2049 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2051 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2053 var_Set( p_input, "seekable", val );
2056 /* Create the stream_t */
2057 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2058 if( in->p_stream == NULL )
2060 msg_Warn( p_input, "cannot create a stream_t from access" );
2064 /* Open a demuxer */
2065 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2067 psz_demux = in->p_access->psz_demux;
2069 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2070 in->p_stream, p_input->p_es_out, b_quick );
2071 if( in->p_demux == NULL )
2073 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2074 psz_access, psz_demux, psz_path );
2075 intf_UserFatal( VLC_OBJECT( p_input), _("Errors"),
2076 "Unrecognized format for '%s'", psz_mrl );
2080 /* TODO get title from demux */
2081 if( !b_quick && in->i_title <= 0 )
2083 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2084 &in->title, &in->i_title,
2085 &in->i_title_offset, &in->i_seekpoint_offset ))
2092 in->b_title_demux = VLC_TRUE;
2097 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2098 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2100 if( psz_dup ) free( psz_dup );
2105 demux2_Delete( in->p_demux );
2108 stream_Delete( in->p_stream );
2111 access2_Delete( in->p_access );
2112 if( psz_dup ) free( psz_dup );
2114 return VLC_EGENERIC;
2117 /*****************************************************************************
2119 *****************************************************************************/
2120 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2123 demux2_Delete( in->p_demux );
2126 stream_Delete( in->p_stream );
2129 access2_Delete( in->p_access );
2131 if( in->i_title > 0 )
2134 for( i = 0; i < in->i_title; i++ )
2136 vlc_input_title_Delete( in->title[i] );
2142 static void SlaveDemux( input_thread_t *p_input )
2147 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2149 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2153 for( i = 0; i < p_input->i_slave; i++ )
2155 input_source_t *in = p_input->slave[i];
2161 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2166 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2168 msg_Err( p_input, "slave[%d] doesn't like "
2169 "DEMUX_GET_TIME -> EOF", i );
2174 if( i_stime >= i_time )
2177 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2183 i_ret = in->p_demux->pf_demux( in->p_demux );
2188 msg_Dbg( p_input, "slave %d EOF", i );
2189 in->b_eof = VLC_TRUE;
2194 static void SlaveSeek( input_thread_t *p_input )
2199 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2201 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2205 for( i = 0; i < p_input->i_slave; i++ )
2207 input_source_t *in = p_input->slave[i];
2209 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2211 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2212 in->b_eof = VLC_TRUE;
2216 /*****************************************************************************
2218 *****************************************************************************/
2219 static void InputMetaUser( input_thread_t *p_input )
2221 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
2224 if( !p_meta ) return;
2226 /* Get meta information from user */
2227 #define GET_META( field, s ) \
2228 var_Get( p_input, (s), &val ); \
2229 if( *val.psz_string ) { \
2230 if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2231 p_meta->psz_ ## field = strdup( val.psz_string ); \
2233 free( val.psz_string )
2235 GET_META( title, "meta-title" );
2236 GET_META( author, "meta-author" );
2237 GET_META( artist, "meta-artist" );
2238 GET_META( genre, "meta-genre" );
2239 GET_META( copyright, "meta-copyright" );
2240 GET_META( description, "meta-description" );
2241 GET_META( date, "meta-date" );
2242 GET_META( url, "meta-url" );
2246 /*****************************************************************************
2247 * DecodeUrl: decode a given encoded url
2248 *****************************************************************************/
2249 static void DecodeUrl( char *psz )
2251 char *dup = strdup( psz );
2269 *psz++ = strtol( val, NULL, 16 );
2271 else if( *p == '+' )
2281 if( psz ) *psz++ ='\0';
2282 if( dup ) free( dup );
2285 /*****************************************************************************
2286 * MRLSplit: parse the access, demux and url part of the
2287 * Media Resource Locator.
2288 *****************************************************************************/
2289 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2290 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2292 char *psz_access = NULL;
2293 char *psz_demux = NULL;
2294 char *psz_path = NULL;
2295 char *psz, *psz_check;
2297 psz = strchr( psz_dup, ':' );
2299 /* '@' not allowed in access/demux part */
2300 psz_check = strchr( psz_dup, '@' );
2301 if( psz_check && psz_check < psz ) psz = 0;
2303 #if defined( WIN32 ) || defined( UNDER_CE )
2304 if( psz - psz_dup == 1 )
2306 msg_Warn( p_input, "drive letter %c: found in source", *psz_dup );
2315 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2319 psz = strchr( psz_dup, '/' );
2326 psz_access = psz_dup;
2333 if( !psz_access ) *ppsz_access = "";
2334 else *ppsz_access = psz_access;
2336 if( !psz_demux ) *ppsz_demux = "";
2337 else *ppsz_demux = psz_demux;
2339 if( !psz_path ) *ppsz_path = "";
2340 else *ppsz_path = psz_path;
2343 /*****************************************************************************
2344 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2347 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2348 *****************************************************************************/
2349 static void MRLSections( input_thread_t *p_input, char *psz_source,
2350 int *pi_title_start, int *pi_title_end,
2351 int *pi_chapter_start, int *pi_chapter_end )
2353 char *psz, *psz_end, *psz_next, *psz_check;
2355 *pi_title_start = *pi_title_end = -1;
2356 *pi_chapter_start = *pi_chapter_end = -1;
2358 /* Start by parsing titles and chapters */
2359 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2361 /* Check we are really dealing with a title/chapter section */
2362 psz_check = psz + 1;
2363 if( !*psz_check ) return;
2364 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2365 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2366 if( *psz_check == ':' && ++psz_check )
2367 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2368 if( *psz_check != '-' && *psz_check ) return;
2369 if( *psz_check == '-' && ++psz_check )
2370 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2371 if( *psz_check != ':' && *psz_check ) return;
2372 if( *psz_check == ':' && ++psz_check )
2373 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2374 if( *psz_check ) return;
2376 /* Separate start and end */
2378 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2380 /* Look for the start title */
2381 *pi_title_start = strtol( psz, &psz_next, 0 );
2382 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2383 *pi_title_end = *pi_title_start;
2386 /* Look for the start chapter */
2388 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2389 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2390 *pi_chapter_end = *pi_chapter_start;
2394 /* Look for the end title */
2395 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2396 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2399 /* Look for the end chapter */
2400 if( *psz_end ) psz_end++;
2401 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2402 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2405 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2406 psz_source, *pi_title_start, *pi_chapter_start,
2407 *pi_title_end, *pi_chapter_end );