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 if( !p_input->input.p_item->p_meta )
153 p_input->input.p_item->p_meta = vlc_meta_New();
154 stats_ReinitInputStats( p_item->p_stats );
157 p_input->i_slave = 0;
158 p_input->slave = NULL;
160 /* Init control buffer */
161 vlc_mutex_init( p_input, &p_input->lock_control );
162 p_input->i_control = 0;
164 /* Parse input options */
165 vlc_mutex_lock( &p_item->lock );
166 for( i = 0; i < p_item->i_options; i++ )
168 var_OptionParse( p_input, p_item->ppsz_options[i] );
170 vlc_mutex_unlock( &p_item->lock );
172 /* Create Object Variables for private use only */
173 input_ConfigVarInit( p_input );
175 /* Create Objects variables for public Get and Set */
176 input_ControlVarInit( p_input );
177 p_input->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
181 var_Get( p_input, "bookmarks", &val );
184 /* FIXME: have a common cfg parsing routine used by sout and others */
185 char *psz_parser, *psz_start, *psz_end;
186 psz_parser = val.psz_string;
187 while( (psz_start = strchr( psz_parser, '{' ) ) )
189 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
192 psz_end = strchr( psz_start, '}' );
193 if( !psz_end ) break;
194 psz_parser = psz_end + 1;
195 backup = *psz_parser;
198 while( (psz_end = strchr( psz_start, ',' ) ) )
201 if( !strncmp( psz_start, "name=", 5 ) )
203 p_seekpoint->psz_name = psz_start + 5;
205 else if( !strncmp( psz_start, "bytes=", 6 ) )
207 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
209 else if( !strncmp( psz_start, "time=", 5 ) )
211 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
214 psz_start = psz_end + 1;
216 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
217 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
218 p_seekpoint->i_time_offset );
219 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
220 vlc_seekpoint_Delete( p_seekpoint );
221 *psz_parser = backup;
223 free( val.psz_string );
227 /* Remove 'Now playing' info as it is probably outdated */
228 input_Control( p_input, INPUT_DEL_INFO, _(VLC_META_INFO_CAT),
229 VLC_META_NOW_PLAYING ); /* ? Don't translate as it might has been copied ? */
235 * Initialize an input thread and run it. You will need to monitor the thread to clean
236 * up after it is done
238 * \param p_parent a vlc_object
239 * \param p_item an input item
240 * \return a pointer to the spawned input thread
242 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
243 input_item_t *p_item )
245 return __input_CreateThread2( p_parent, p_item, NULL );
250 input_thread_t *__input_CreateThread2( vlc_object_t *p_parent,
251 input_item_t *p_item,
254 input_thread_t *p_input = NULL; /* thread descriptor */
256 p_input = Create( p_parent, p_item, psz_header, VLC_FALSE );
257 /* Now we can attach our new input */
258 vlc_object_attach( p_input, p_parent );
260 /* Create thread and wait for its readiness. */
261 if( vlc_thread_create( p_input, "input", Run,
262 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
264 msg_Err( p_input, "cannot create input thread" );
265 vlc_object_detach( p_input );
266 vlc_object_destroy( p_input );
275 * Initialize an input thread and run it. This thread will clean after himself,
276 * you can forget about it. It can work either in blocking or non-blocking mode
278 * \param p_parent a vlc_object
279 * \param p_item an input item
280 * \param b_block should we block until read is finished ?
281 * \return the input object id if non blocking, an error code else
283 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
286 input_thread_t *p_input = NULL; /* thread descriptor */
288 p_input = Create( p_parent, p_item, NULL, VLC_FALSE );
289 /* Now we can attach our new input */
290 vlc_object_attach( p_input, p_parent );
294 RunAndClean( p_input );
299 if( vlc_thread_create( p_input, "input", RunAndClean,
300 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
302 msg_Err( p_input, "cannot create input thread" );
303 vlc_object_detach( p_input );
304 vlc_object_destroy( p_input );
308 return p_input->i_object_id;
312 * Initialize an input and initialize it to preparse the item
313 * This function is blocking. It will only accept to parse files
315 * \param p_parent a vlc_object_t
316 * \param p_item an input item
317 * \return VLC_SUCCESS or an error
319 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
321 input_thread_t *p_input = NULL; /* thread descriptor */
323 /* Allocate descriptor */
324 p_input = Create( p_parent, p_item, NULL, VLC_TRUE );
325 p_input->i_flags |= OBJECT_FLAGS_NODBG;
326 p_input->i_flags |= OBJECT_FLAGS_NOINTERACT;
328 /* Now we can attach our new input */
329 vlc_object_attach( p_input, p_parent );
331 Init( p_input, VLC_TRUE );
333 /* Clean up master */
334 InputSourceClean( p_input, &p_input->input );
336 /* Unload all modules */
337 if( p_input->p_es_out ) input_EsOutDelete( p_input->p_es_out );
339 vlc_object_detach( p_input );
340 vlc_object_destroy( p_input );
346 * Request a running input thread to stop and die
348 * \param the input thread to stop
350 void input_StopThread( input_thread_t *p_input )
355 /* Set die for input */
356 p_input->b_die = VLC_TRUE;
358 /* We cannot touch p_input fields directly (we can from another thread),
359 * so use the vlc_object_find way, it's perfectly safe */
361 /* Set die for all access */
362 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
363 for( i = 0; i < p_list->i_count; i++ )
365 p_list->p_values[i].p_object->b_die = VLC_TRUE;
367 vlc_list_release( p_list );
369 /* Set die for all stream */
370 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
371 for( i = 0; i < p_list->i_count; i++ )
373 p_list->p_values[i].p_object->b_die = VLC_TRUE;
375 vlc_list_release( p_list );
377 /* Set die for all demux */
378 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
379 for( i = 0; i < p_list->i_count; i++ )
381 p_list->p_values[i].p_object->b_die = VLC_TRUE;
383 vlc_list_release( p_list );
385 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
389 * Clean up a dead input thread
390 * This function does not return until the thread is effectively cancelled.
392 * \param the input thread to kill
394 void input_DestroyThread( input_thread_t *p_input )
396 /* Join the thread */
397 vlc_thread_join( p_input );
399 /* Delete input lock (only after thread joined) */
400 vlc_mutex_destroy( &p_input->lock_control );
402 /* TODO: maybe input_DestroyThread should also delete p_input instead
403 * of the playlist but I'm not sure if it's possible */
406 /*****************************************************************************
407 * Run: main thread loop
408 * This is the "normal" thread that spawns the input processing chain,
409 * reads the stream, cleans up and waits
410 *****************************************************************************/
411 static int Run( input_thread_t *p_input )
413 /* Signal that the thread is launched */
414 vlc_thread_ready( p_input );
416 if( Init( p_input, VLC_FALSE ) )
418 /* If we failed, wait before we are killed, and exit */
419 p_input->b_error = VLC_TRUE;
423 /* Tell we're dead */
424 p_input->b_dead = VLC_TRUE;
431 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
433 /* We have finish to demux data but not to play them */
434 while( !p_input->b_die )
436 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
439 msg_Dbg( p_input, "waiting decoder fifos to empty" );
441 msleep( INPUT_IDLE_SLEEP );
444 /* We have finished */
445 p_input->b_eof = VLC_TRUE;
448 /* Wait we are asked to die */
449 if( !p_input->b_die )
460 /*****************************************************************************
461 * RunAndClean: main thread loop
462 * This is the "just forget me" thread that spawns the input processing chain,
463 * reads the stream, cleans up and releases memory
464 *****************************************************************************/
465 static int RunAndClean( input_thread_t *p_input )
467 /* Signal that the thread is launched */
468 vlc_thread_ready( p_input );
470 if( Init( p_input, VLC_FALSE ) )
472 /* If we failed, just exit */
478 if( !p_input->b_eof && !p_input->b_error && p_input->input.b_eof )
480 /* We have finish to demux data but not to play them */
481 while( !p_input->b_die )
483 if( input_EsOutDecodersEmpty( p_input->p_es_out ) )
486 msg_Dbg( p_input, "waiting decoder fifos to empty" );
488 msleep( INPUT_IDLE_SLEEP );
490 /* We have finished */
491 p_input->b_eof = VLC_TRUE;
498 vlc_object_detach( p_input );
499 vlc_object_destroy( p_input );
505 /*****************************************************************************
506 * Main loop: Fill buffers from access, and demux
507 *****************************************************************************/
508 static void MainLoop( input_thread_t *p_input )
510 int64_t i_intf_update = 0;
511 while( !p_input->b_die && !p_input->b_error && !p_input->input.b_eof )
513 vlc_bool_t b_force_update = VLC_FALSE;
519 if( p_input->i_state != PAUSE_S )
521 if( p_input->i_stop <= 0 || p_input->i_time < p_input->i_stop )
522 i_ret=p_input->input.p_demux->pf_demux(p_input->input.p_demux);
529 if( p_input->input.b_title_demux &&
530 p_input->input.p_demux->info.i_update )
532 i_ret = UpdateFromDemux( p_input );
533 b_force_update = VLC_TRUE;
535 else if( !p_input->input.b_title_demux &&
536 p_input->input.p_access &&
537 p_input->input.p_access->info.i_update )
539 i_ret = UpdateFromAccess( p_input );
540 b_force_update = VLC_TRUE;
544 if( i_ret == 0 ) /* EOF */
548 var_Get( p_input, "input-repeat", &repeat );
549 if( repeat.i_int == 0 )
551 /* End of file - we do not set b_die because only the
552 * playlist is allowed to do so. */
553 msg_Dbg( p_input, "EOF reached" );
554 p_input->input.b_eof = VLC_TRUE;
558 msg_Dbg( p_input, "repeating the same input (%d)",
560 if( repeat.i_int > 0 )
563 var_Set( p_input, "input-repeat", repeat );
566 /* Seek to start title/seekpoint */
567 val.i_int = p_input->input.i_title_start -
568 p_input->input.i_title_offset;
569 if( val.i_int < 0 || val.i_int >= p_input->input.i_title )
571 input_ControlPush( p_input,
572 INPUT_CONTROL_SET_TITLE, &val );
574 val.i_int = p_input->input.i_seekpoint_start -
575 p_input->input.i_seekpoint_offset;
576 if( val.i_int > 0 /* TODO: check upper boundary */ )
577 input_ControlPush( p_input,
578 INPUT_CONTROL_SET_SEEKPOINT, &val );
580 /* Seek to start position */
581 if( p_input->i_start > 0 )
583 val.i_time = p_input->i_start;
584 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
590 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
597 p_input->b_error = VLC_TRUE;
600 if( i_ret > 0 && p_input->i_slave > 0 )
602 SlaveDemux( p_input );
612 vlc_mutex_lock( &p_input->lock_control );
613 ControlReduce( p_input );
614 while( !ControlPopNoLock( p_input, &i_type, &val ) )
616 msg_Dbg( p_input, "control type=%d", i_type );
617 if( Control( p_input, i_type, val ) )
618 b_force_update = VLC_TRUE;
620 vlc_mutex_unlock( &p_input->lock_control );
622 if( b_force_update || i_intf_update < mdate() )
626 int64_t i_time, i_length;
627 /* update input status variables */
628 if( !demux2_Control( p_input->input.p_demux,
629 DEMUX_GET_POSITION, &f_pos ) )
631 val.f_float = (float)f_pos;
632 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
634 if( !demux2_Control( p_input->input.p_demux,
635 DEMUX_GET_TIME, &i_time ) )
637 p_input->i_time = i_time;
639 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
641 if( !demux2_Control( p_input->input.p_demux,
642 DEMUX_GET_LENGTH, &i_length ) )
645 var_Get( p_input, "length", &old_val );
646 val.i_time = i_length;
647 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
649 if( old_val.i_time != val.i_time )
651 UpdateItemLength( p_input, i_length, VLC_TRUE );
655 var_SetBool( p_input, "intf-change", VLC_TRUE );
656 i_intf_update = mdate() + I64C(150000);
662 static int Init( input_thread_t * p_input, vlc_bool_t b_quick )
672 /* Initialize optional stream output. (before access/demuxer)
673 * XXX: we add a special case if the uri starts by vlc.
674 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
675 * be destroyed in case of a file).
676 * (this will break playing of file starting by 'vlc:' but I don't
677 * want to add more logic, just force file by file:// or code it ;)
681 /* Prepare statistics */
682 counter_t *p_counter;
683 stats_Create( p_input, "read_bytes", STATS_READ_BYTES,
684 VLC_VAR_INTEGER, STATS_COUNTER );
685 stats_Create( p_input, "read_packets", STATS_READ_PACKETS,
686 VLC_VAR_INTEGER, STATS_COUNTER );
687 stats_Create( p_input, "demux_read", STATS_DEMUX_READ,
688 VLC_VAR_INTEGER, STATS_COUNTER );
689 stats_Create( p_input, "input_bitrate", STATS_INPUT_BITRATE,
690 VLC_VAR_FLOAT, STATS_DERIVATIVE );
691 stats_Create( p_input, "demux_bitrate", STATS_DEMUX_BITRATE,
692 VLC_VAR_FLOAT, STATS_DERIVATIVE );
694 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
695 STATS_INPUT_BITRATE );
696 if( p_counter ) p_counter->update_interval = 1000000;
697 p_counter = stats_CounterGet( p_input, p_input->i_object_id,
698 STATS_DEMUX_BITRATE );
699 if( p_counter ) p_counter->update_interval = 1000000;
701 stats_Create( p_input, "played_abuffers", STATS_PLAYED_ABUFFERS,
702 VLC_VAR_INTEGER, STATS_COUNTER );
703 stats_Create( p_input, "lost_abuffers", STATS_LOST_ABUFFERS,
704 VLC_VAR_INTEGER, STATS_COUNTER );
707 psz = var_GetString( p_input, "sout" );
708 if( *psz && strncasecmp( p_input->input.p_item->psz_uri, "vlc:", 4 ) )
710 p_input->p_sout = sout_NewInstance( p_input, psz );
711 if( p_input->p_sout == NULL )
713 msg_Err( p_input, "cannot start stream output instance, " \
723 p_input->p_es_out = input_EsOutNew( p_input );
724 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
725 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
727 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
728 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
730 if( InputSourceInit( p_input, &p_input->input,
731 p_input->input.p_item->psz_uri, NULL, b_quick ) )
736 /* Create global title (from master) */
739 p_input->i_title = p_input->input.i_title;
740 p_input->title = p_input->input.title;
741 p_input->i_title_offset = p_input->input.i_title_offset;
742 p_input->i_seekpoint_offset = p_input->input.i_seekpoint_offset;
743 if( p_input->i_title > 0 )
745 /* Setup variables */
746 input_ControlVarNavigation( p_input );
747 input_ControlVarTitle( p_input, 0 );
751 p_input->b_can_pace_control = p_input->input.b_can_pace_control;
752 p_input->b_can_pause = p_input->input.b_can_pause;
755 if( p_input->i_pts_delay < 0 )
756 p_input->i_pts_delay = 0;
758 /* If the desynchronisation requested by the user is < 0, we need to
759 * cache more data. */
760 var_Get( p_input, "audio-desync", &val );
761 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
763 /* Update cr_average depending on the caching */
764 p_input->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
765 p_input->input.i_cr_average /= 10;
766 if( p_input->input.i_cr_average < 10 ) p_input->input.i_cr_average = 10;
769 /* Load master infos */
771 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_LENGTH,
772 &val.i_time ) && val.i_time > 0 )
774 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
775 UpdateItemLength( p_input, val.i_time, b_quick );
776 p_input->input.p_item->i_duration = val.i_time;
779 /* Start title/chapter */
782 val.i_int = p_input->input.i_title_start -
783 p_input->input.i_title_offset;
784 if( val.i_int > 0 && val.i_int < p_input->input.i_title )
785 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
786 val.i_int = p_input->input.i_seekpoint_start -
787 p_input->input.i_seekpoint_offset;
788 if( val.i_int > 0 /* TODO: check upper boundary */ )
789 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
793 p_input->i_start = (int64_t)var_GetInteger( p_input, "start-time" ) *
795 p_input->i_stop = (int64_t)var_GetInteger( p_input, "stop-time" ) *
798 if( p_input->i_start > 0 )
800 if( p_input->i_start >= val.i_time )
802 msg_Warn( p_input, "invalid start-time ignored" );
808 msg_Dbg( p_input, "starting at time: %ds",
809 (int)( p_input->i_start / I64C(1000000) ) );
811 s.i_time = p_input->i_start;
812 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
815 if( p_input->i_stop > 0 && p_input->i_stop <= p_input->i_start )
817 msg_Warn( p_input, "invalid stop-time ignored" );
823 /* Get fps and set it if not already set */
824 if( !demux2_Control( p_input->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
827 float f_requested_fps;
829 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
830 var_SetFloat( p_input, "sub-original-fps", f_fps );
832 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
833 if( f_requested_fps != f_fps )
835 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
837 var_SetFloat( p_input, "sub-fps", f_requested_fps );
841 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
844 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
848 /* Look for and add subtitle files */
849 psz_subtitle = var_GetString( p_input, "sub-file" );
856 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
858 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
861 sub = InputSourceNew( p_input );
862 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle",
865 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
868 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list,
871 if( count.i_int == 0 )
873 /* if it was first one, there is disable too */
875 if( count.i_int < list.p_list->i_count )
877 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
878 &list.p_list->p_values[count.i_int] );
880 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list,
886 var_Get( p_input, "sub-autodetect-file", &val );
889 char *psz_autopath = var_GetString( p_input, "sub-autodetect-path" );
890 char **subs = subtitles_Detect( p_input, psz_autopath,
891 p_input->input.p_item->psz_uri );
896 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
898 for( i = 0; subs && subs[i]; i++ )
900 if( strcmp( psz_subtitle, subs[i] ) )
902 sub = InputSourceNew( p_input );
903 if( !InputSourceInit( p_input, sub, subs[i], "subtitle",
906 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
908 /* If no subtitle was specified, select the first
909 * autodetected subtitle. */
910 if( i == 0 && psz_subtitle != NULL )
913 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
915 if( count.i_int == 0 )
917 /* if it was first one, there is disable too */
919 if( count.i_int < list.p_list->i_count )
921 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
922 &list.p_list->p_values[count.i_int] );
924 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
931 if( subs ) free( subs );
932 if( psz_autopath ) free( psz_autopath );
934 free( psz_subtitle );
937 psz = var_GetString( p_input, "input-slave" );
941 input_source_t *slave;
944 while( *psz == ' ' || *psz == '#' )
948 if( ( psz_delim = strchr( psz, '#' ) ) )
957 msg_Dbg( p_input, "adding slave input '%s'", psz );
958 slave = InputSourceNew( p_input );
959 if( !InputSourceInit( p_input, slave, psz, NULL, VLC_FALSE ) )
961 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
966 if( psz ) free( psz );
970 p_input->i_start = 0;
971 p_input->i_start = 0;
977 es_out_Control( p_input->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
978 i_es_out_mode = ES_OUT_MODE_AUTO;
980 if( p_input->p_sout )
982 var_Get( p_input, "sout-all", &val );
985 i_es_out_mode = ES_OUT_MODE_ALL;
990 var_Get( p_input, "programs", &val );
991 if ( val.p_list && val.p_list->i_count )
993 i_es_out_mode = ES_OUT_MODE_PARTIAL;
994 /* Note : we should remove the "program" callback. */
997 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1001 es_out_Control( p_input->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1003 /* Inform the demuxer about waited group (needed only for DVB) */
1004 if( i_es_out_mode == ES_OUT_MODE_ALL )
1006 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1008 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1010 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, -1,
1015 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP,
1016 (int) var_GetInteger( p_input, "program" ), NULL );
1019 if( p_input->p_sout )
1021 if( p_input->p_sout->i_out_pace_nocontrol > 0 )
1023 p_input->b_out_pace_control = VLC_FALSE;
1027 p_input->b_out_pace_control = VLC_TRUE;
1030 if( p_input->b_can_pace_control && p_input->b_out_pace_control )
1032 /* We don't want a high input priority here or we'll
1033 * end-up sucking up all the CPU time */
1034 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1037 msg_Dbg( p_input, "starting in %s mode",
1038 p_input->b_out_pace_control ? "asynch" : "synch" );
1042 p_meta = p_input->input.p_item->p_meta;
1043 /* Get meta data from users */
1044 InputMetaUser( p_input );
1045 /* Get meta data from master input */
1046 demux2_Control( p_input->input.p_demux, DEMUX_GET_META, p_meta );
1048 /* Access_file does not give any meta, and there are no slave */
1051 if( p_input->input.p_access )
1052 access2_Control( p_input->input.p_access, ACCESS_GET_META,
1055 /* Get meta data from slave input */
1056 for( i = 0; i < p_input->i_slave; i++ )
1058 demux2_Control( p_input->slave[i]->p_demux,
1059 DEMUX_GET_META, p_meta );
1060 if( p_input->slave[i]->p_access )
1062 access2_Control( p_input->slave[i]->p_access,
1063 ACCESS_GET_META, p_meta );
1068 UpdateMeta( p_input, b_quick );
1072 msg_Dbg( p_input, "`%s' successfully opened",
1073 p_input->input.p_item->psz_uri );
1077 /* initialization is complete */
1078 p_input->i_state = PLAYING_S;
1080 val.i_int = PLAYING_S;
1081 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1086 if( p_input->p_es_out )
1087 input_EsOutDelete( p_input->p_es_out );
1089 if( p_input->p_sout )
1090 sout_DeleteInstance( p_input->p_sout );
1092 /* Mark them deleted */
1093 p_input->input.p_demux = NULL;
1094 p_input->input.p_stream = NULL;
1095 p_input->input.p_access = NULL;
1096 p_input->p_es_out = NULL;
1097 p_input->p_sout = NULL;
1099 return VLC_EGENERIC;
1102 /*****************************************************************************
1103 * Error: RunThread() error loop
1104 *****************************************************************************
1105 * This function is called when an error occurred during thread main's loop.
1106 *****************************************************************************/
1107 static void Error( input_thread_t *p_input )
1109 while( !p_input->b_die )
1112 msleep( INPUT_IDLE_SLEEP );
1116 /*****************************************************************************
1117 * End: end the input thread
1118 *****************************************************************************/
1119 static void End( input_thread_t * p_input )
1124 msg_Dbg( p_input, "closing input" );
1126 /* We are at the end */
1127 p_input->i_state = END_S;
1130 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1132 /* Clean control variables */
1133 input_ControlVarClean( p_input );
1135 /* Clean up master */
1136 InputSourceClean( p_input, &p_input->input );
1139 for( i = 0; i < p_input->i_slave; i++ )
1141 InputSourceClean( p_input, p_input->slave[i] );
1142 free( p_input->slave[i] );
1144 if( p_input->slave ) free( p_input->slave );
1146 /* Unload all modules */
1147 if( p_input->p_es_out )
1148 input_EsOutDelete( p_input->p_es_out );
1150 /* Close optional stream output instance */
1151 if( p_input->p_sout )
1153 vlc_object_t *p_pl =
1154 vlc_object_find( p_input, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
1157 if( var_Get( p_input, "sout-keep", &keep ) >= 0 && keep.b_bool && p_pl )
1159 /* attach sout to the playlist */
1160 msg_Dbg( p_input, "keeping sout" );
1161 vlc_object_detach( p_input->p_sout );
1162 vlc_object_attach( p_input->p_sout, p_pl );
1166 msg_Dbg( p_input, "destroying sout" );
1167 sout_DeleteInstance( p_input->p_sout );
1170 vlc_object_release( p_pl );
1173 /* Tell we're dead */
1174 p_input->b_dead = VLC_TRUE;
1177 /*****************************************************************************
1179 *****************************************************************************/
1180 static inline int ControlPopNoLock( input_thread_t *p_input,
1181 int *pi_type, vlc_value_t *p_val )
1183 if( p_input->i_control <= 0 )
1185 return VLC_EGENERIC;
1188 *pi_type = p_input->control[0].i_type;
1189 *p_val = p_input->control[0].val;
1191 p_input->i_control--;
1192 if( p_input->i_control > 0 )
1196 for( i = 0; i < p_input->i_control; i++ )
1198 p_input->control[i].i_type = p_input->control[i+1].i_type;
1199 p_input->control[i].val = p_input->control[i+1].val;
1206 static void ControlReduce( input_thread_t *p_input )
1209 for( i = 1; i < p_input->i_control; i++ )
1211 const int i_lt = p_input->control[i-1].i_type;
1212 const int i_ct = p_input->control[i].i_type;
1214 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1215 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->i_control,
1219 ( i_ct == INPUT_CONTROL_SET_STATE ||
1220 i_ct == INPUT_CONTROL_SET_RATE ||
1221 i_ct == INPUT_CONTROL_SET_POSITION ||
1222 i_ct == INPUT_CONTROL_SET_TIME ||
1223 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1224 i_ct == INPUT_CONTROL_SET_TITLE ||
1225 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1226 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1229 // msg_Dbg( p_input, "merged at %d", i );
1230 /* Remove the i-1 */
1231 for( j = i; j < p_input->i_control; j++ )
1232 p_input->control[j-1] = p_input->control[j];
1233 p_input->i_control--;
1237 /* TODO but that's not that important
1238 - merge SET_X with SET_X_CMD
1239 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1240 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1247 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1250 vlc_bool_t b_force_update = VLC_FALSE;
1252 if( !p_input ) return b_force_update;
1256 case INPUT_CONTROL_SET_DIE:
1257 msg_Dbg( p_input, "control: stopping input" );
1258 /* Mark all submodules to die */
1259 if( p_input->input.p_access )
1260 p_input->input.p_access->b_die = VLC_TRUE;
1261 if( p_input->input.p_stream )
1262 p_input->input.p_stream->b_die = VLC_TRUE;
1263 p_input->input.p_demux->b_die = VLC_TRUE;
1265 p_input->b_die = VLC_TRUE;
1268 case INPUT_CONTROL_SET_POSITION:
1269 case INPUT_CONTROL_SET_POSITION_OFFSET:
1272 if( i_type == INPUT_CONTROL_SET_POSITION )
1274 f_pos = val.f_float;
1278 /* Should not fail */
1279 demux2_Control( p_input->input.p_demux,
1280 DEMUX_GET_POSITION, &f_pos );
1281 f_pos += val.f_float;
1283 if( f_pos < 0.0 ) f_pos = 0.0;
1284 if( f_pos > 1.0 ) f_pos = 1.0;
1285 /* Reset the decoders states and clock synch (before calling the demuxer */
1286 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1287 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1288 if( demux2_Control( p_input->input.p_demux, DEMUX_SET_POSITION,
1291 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1292 "%2.1f%% failed", f_pos * 100 );
1296 if( p_input->i_slave > 0 )
1297 SlaveSeek( p_input );
1299 b_force_update = VLC_TRUE;
1304 case INPUT_CONTROL_SET_TIME:
1305 case INPUT_CONTROL_SET_TIME_OFFSET:
1310 if( i_type == INPUT_CONTROL_SET_TIME )
1312 i_time = val.i_time;
1316 /* Should not fail */
1317 demux2_Control( p_input->input.p_demux,
1318 DEMUX_GET_TIME, &i_time );
1319 i_time += val.i_time;
1321 if( i_time < 0 ) i_time = 0;
1323 /* Reset the decoders states and clock synch (before calling the demuxer */
1324 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1325 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1327 i_ret = demux2_Control( p_input->input.p_demux,
1328 DEMUX_SET_TIME, i_time );
1333 /* Emulate it with a SET_POS */
1334 demux2_Control( p_input->input.p_demux,
1335 DEMUX_GET_LENGTH, &i_length );
1338 double f_pos = (double)i_time / (double)i_length;
1339 i_ret = demux2_Control( p_input->input.p_demux,
1340 DEMUX_SET_POSITION, f_pos );
1345 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1346 " failed or not possible", i_time );
1350 if( p_input->i_slave > 0 )
1351 SlaveSeek( p_input );
1353 b_force_update = VLC_TRUE;
1358 case INPUT_CONTROL_SET_STATE:
1359 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1360 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1363 if( p_input->input.p_access )
1364 i_ret = access2_Control( p_input->input.p_access,
1365 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1367 i_ret = demux2_Control( p_input->input.p_demux,
1368 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1372 /* FIXME What to do ? */
1373 msg_Warn( p_input, "cannot unset pause -> EOF" );
1374 vlc_mutex_unlock( &p_input->lock_control );
1375 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1376 vlc_mutex_lock( &p_input->lock_control );
1379 b_force_update = VLC_TRUE;
1381 /* Switch to play */
1382 p_input->i_state = PLAYING_S;
1383 val.i_int = PLAYING_S;
1384 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1387 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1388 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1390 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1391 p_input->b_can_pause )
1394 if( p_input->input.p_access )
1395 i_ret = access2_Control( p_input->input.p_access,
1396 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1398 i_ret = demux2_Control( p_input->input.p_demux,
1399 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1401 b_force_update = VLC_TRUE;
1405 msg_Warn( p_input, "cannot set pause state" );
1406 val.i_int = p_input->i_state;
1410 val.i_int = PAUSE_S;
1413 /* Switch to new state */
1414 p_input->i_state = val.i_int;
1415 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1417 else if( val.i_int == PAUSE_S && !p_input->b_can_pause )
1419 b_force_update = VLC_TRUE;
1421 /* Correct "state" value */
1422 val.i_int = p_input->i_state;
1423 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1425 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1427 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1431 case INPUT_CONTROL_SET_RATE:
1432 case INPUT_CONTROL_SET_RATE_SLOWER:
1433 case INPUT_CONTROL_SET_RATE_FASTER:
1437 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1438 i_rate = p_input->i_rate * 2;
1439 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1440 i_rate = p_input->i_rate / 2;
1444 if( i_rate < INPUT_RATE_MIN )
1446 msg_Dbg( p_input, "cannot set rate faster" );
1447 i_rate = INPUT_RATE_MIN;
1449 else if( i_rate > INPUT_RATE_MAX )
1451 msg_Dbg( p_input, "cannot set rate slower" );
1452 i_rate = INPUT_RATE_MAX;
1454 if( i_rate != INPUT_RATE_DEFAULT &&
1455 ( !p_input->b_can_pace_control ||
1456 ( p_input->p_sout && !p_input->b_out_pace_control ) ) )
1458 msg_Dbg( p_input, "cannot change rate" );
1459 i_rate = INPUT_RATE_DEFAULT;
1461 if( i_rate != p_input->i_rate )
1463 p_input->i_rate = i_rate;
1465 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1467 /* We haven't send data to decoder when rate != default */
1468 if( i_rate == INPUT_RATE_DEFAULT )
1469 input_EsOutDiscontinuity( p_input->p_es_out, VLC_TRUE );
1472 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1474 b_force_update = VLC_TRUE;
1479 case INPUT_CONTROL_SET_PROGRAM:
1480 /* No need to force update, es_out does it if needed */
1481 es_out_Control( p_input->p_es_out,
1482 ES_OUT_SET_GROUP, val.i_int );
1484 demux2_Control( p_input->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1488 case INPUT_CONTROL_SET_ES:
1489 /* No need to force update, es_out does it if needed */
1490 es_out_Control( p_input->p_es_out, ES_OUT_SET_ES,
1491 input_EsOutGetFromID( p_input->p_es_out,
1495 case INPUT_CONTROL_SET_AUDIO_DELAY:
1496 input_EsOutSetDelay( p_input->p_es_out,
1497 AUDIO_ES, val.i_time );
1498 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1501 case INPUT_CONTROL_SET_SPU_DELAY:
1502 input_EsOutSetDelay( p_input->p_es_out,
1503 SPU_ES, val.i_time );
1504 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1507 case INPUT_CONTROL_SET_TITLE:
1508 case INPUT_CONTROL_SET_TITLE_NEXT:
1509 case INPUT_CONTROL_SET_TITLE_PREV:
1510 if( p_input->input.b_title_demux &&
1511 p_input->input.i_title > 0 )
1514 /* FIXME handle demux title */
1515 demux_t *p_demux = p_input->input.p_demux;
1518 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1519 i_title = p_demux->info.i_title - 1;
1520 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1521 i_title = p_demux->info.i_title + 1;
1523 i_title = val.i_int;
1525 if( i_title >= 0 && i_title < p_input->input.i_title )
1527 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1528 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1530 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1531 input_ControlVarTitle( p_input, i_title );
1534 else if( p_input->input.i_title > 0 )
1536 access_t *p_access = p_input->input.p_access;
1539 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1540 i_title = p_access->info.i_title - 1;
1541 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1542 i_title = p_access->info.i_title + 1;
1544 i_title = val.i_int;
1546 if( i_title >= 0 && i_title < p_input->input.i_title )
1548 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1549 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1551 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1552 stream_AccessReset( p_input->input.p_stream );
1556 case INPUT_CONTROL_SET_SEEKPOINT:
1557 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1558 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1559 if( p_input->input.b_title_demux &&
1560 p_input->input.i_title > 0 )
1562 demux_t *p_demux = p_input->input.p_demux;
1564 int64_t i_input_time;
1565 int64_t i_seekpoint_time;
1567 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1569 i_seekpoint = p_demux->info.i_seekpoint;
1570 i_seekpoint_time = p_input->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1571 if( i_seekpoint_time >= 0 &&
1572 !demux2_Control( p_demux,
1573 DEMUX_GET_TIME, &i_input_time ) )
1575 if ( i_input_time < i_seekpoint_time + 3000000 )
1581 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1582 i_seekpoint = p_demux->info.i_seekpoint + 1;
1584 i_seekpoint = val.i_int;
1586 if( i_seekpoint >= 0 && i_seekpoint <
1587 p_input->input.title[p_demux->info.i_title]->i_seekpoint )
1589 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1590 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1592 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1595 else if( p_input->input.i_title > 0 )
1597 demux_t *p_demux = p_input->input.p_demux;
1598 access_t *p_access = p_input->input.p_access;
1600 int64_t i_input_time;
1601 int64_t i_seekpoint_time;
1603 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1605 i_seekpoint = p_access->info.i_seekpoint;
1606 i_seekpoint_time = p_input->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1607 if( i_seekpoint_time >= 0 &&
1608 demux2_Control( p_demux,
1609 DEMUX_GET_TIME, &i_input_time ) )
1611 if ( i_input_time < i_seekpoint_time + 3000000 )
1617 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1618 i_seekpoint = p_access->info.i_seekpoint + 1;
1620 i_seekpoint = val.i_int;
1622 if( i_seekpoint >= 0 && i_seekpoint <
1623 p_input->input.title[p_access->info.i_title]->i_seekpoint )
1625 input_EsOutDiscontinuity( p_input->p_es_out, VLC_FALSE );
1626 es_out_Control( p_input->p_es_out, ES_OUT_RESET_PCR );
1628 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1630 stream_AccessReset( p_input->input.p_stream );
1635 case INPUT_CONTROL_ADD_SLAVE:
1636 if( val.psz_string )
1638 input_source_t *slave = InputSourceNew( p_input );
1640 if( !InputSourceInit( p_input, slave, val.psz_string, NULL,
1643 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1647 msg_Dbg( p_input, "adding %s as slave on the fly",
1651 if( demux2_Control( p_input->input.p_demux,
1652 DEMUX_GET_TIME, &i_time ) )
1654 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1655 InputSourceClean( p_input, slave );
1659 if( demux2_Control( slave->p_demux,
1660 DEMUX_SET_TIME, i_time ) )
1662 msg_Err( p_input, "seek failed for new slave" );
1663 InputSourceClean( p_input, slave );
1668 /* Get meta (access and demux) */
1669 access2_Control( slave->p_access, ACCESS_GET_META,
1671 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1672 UpdateMeta( p_input, VLC_FALSE );
1674 TAB_APPEND( p_input->i_slave, p_input->slave, slave );
1678 msg_Warn( p_input, "failed to add %s as slave",
1682 free( val.psz_string );
1686 case INPUT_CONTROL_SET_BOOKMARK:
1688 msg_Err( p_input, "not yet implemented" );
1692 return b_force_update;
1695 /*****************************************************************************
1697 *****************************************************************************/
1698 static int UpdateFromDemux( input_thread_t *p_input )
1700 demux_t *p_demux = p_input->input.p_demux;
1703 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1705 v.i_int = p_demux->info.i_title;
1706 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1708 input_ControlVarTitle( p_input, p_demux->info.i_title );
1710 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1712 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1714 v.i_int = p_demux->info.i_seekpoint;
1715 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1717 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1719 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1721 /* Hmmm only works with master input */
1722 if( p_input->input.p_demux == p_demux )
1724 int i_title_end = p_input->input.i_title_end -
1725 p_input->input.i_title_offset;
1726 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1727 p_input->input.i_seekpoint_offset;
1729 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1731 if( p_demux->info.i_title > i_title_end ||
1732 ( p_demux->info.i_title == i_title_end &&
1733 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1735 else if( i_seekpoint_end >=0 )
1737 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1739 else if( i_title_end >= 0 )
1741 if( p_demux->info.i_title > i_title_end ) return 0;
1748 /*****************************************************************************
1750 *****************************************************************************/
1751 static int UpdateFromAccess( input_thread_t *p_input )
1753 access_t *p_access = p_input->input.p_access;
1756 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1758 v.i_int = p_access->info.i_title;
1759 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1761 input_ControlVarTitle( p_input, p_access->info.i_title );
1763 stream_AccessUpdate( p_input->input.p_stream );
1765 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1767 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1769 v.i_int = p_access->info.i_seekpoint;
1770 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1771 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1773 if( p_access->info.i_update & INPUT_UPDATE_META )
1775 /* TODO maybe multi - access ? */
1776 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1777 access2_Control( p_input->input.p_access,ACCESS_GET_META, p_meta );
1778 UpdateMeta( p_input, VLC_FALSE );
1779 var_SetBool( p_input, "item-change", p_input->input.p_item->i_id );
1780 p_access->info.i_update &= ~INPUT_UPDATE_META;
1783 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
1785 /* Hmmm only works with master input */
1786 if( p_input->input.p_access == p_access )
1788 int i_title_end = p_input->input.i_title_end -
1789 p_input->input.i_title_offset;
1790 int i_seekpoint_end = p_input->input.i_seekpoint_end -
1791 p_input->input.i_seekpoint_offset;
1793 if( i_title_end >= 0 && i_seekpoint_end >=0 )
1795 if( p_access->info.i_title > i_title_end ||
1796 ( p_access->info.i_title == i_title_end &&
1797 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1799 else if( i_seekpoint_end >=0 )
1801 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
1803 else if( i_title_end >= 0 )
1805 if( p_access->info.i_title > i_title_end ) return 0;
1812 /*****************************************************************************
1814 *****************************************************************************/
1815 static int UpdateMeta( input_thread_t *p_input, vlc_bool_t b_quick )
1817 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
1821 if( p_meta->psz_title && !p_input->input.p_item->b_fixed_name )
1822 input_Control( p_input, INPUT_SET_NAME, p_meta->psz_title );
1824 /** \todo handle sout meta */
1829 /*****************************************************************************
1831 *****************************************************************************/
1832 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length,
1833 vlc_bool_t b_quick )
1835 playlist_t *p_playlist;
1836 char psz_buffer[MSTRTIME_MAX_SIZE];
1838 vlc_mutex_lock( &p_input->input.p_item->lock );
1839 p_input->input.p_item->i_duration = i_length;
1840 vlc_mutex_unlock( &p_input->input.p_item->lock );
1842 p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
1846 var_SetInteger( p_playlist, "item-change",
1847 p_input->input.p_item->i_id );
1848 vlc_object_release( p_playlist );
1851 input_Control( p_input, INPUT_ADD_INFO, _("General"), _("Duration"),
1852 msecstotimestr( psz_buffer, i_length / 1000 ) );
1855 /*****************************************************************************
1857 *****************************************************************************/
1858 static input_source_t *InputSourceNew( input_thread_t *p_input )
1860 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
1864 msg_Err( p_input, "out of memory for new input source" );
1869 in->p_access = NULL;
1870 in->p_stream = NULL;
1872 in->b_title_demux = VLC_FALSE;
1875 in->b_can_pace_control = VLC_TRUE;
1876 in->b_eof = VLC_FALSE;
1877 in->i_cr_average = 0;
1882 /*****************************************************************************
1884 *****************************************************************************/
1885 static int InputSourceInit( input_thread_t *p_input,
1886 input_source_t *in, char *psz_mrl,
1887 char *psz_forced_demux, vlc_bool_t b_quick )
1889 char *psz_dup = strdup( psz_mrl );
1897 if( !in ) return VLC_EGENERIC;
1902 MRLSplit( VLC_OBJECT(p_input), psz_dup,
1903 &psz_access, &psz_demux, &psz_path );
1905 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
1906 psz_mrl, psz_access, psz_demux, psz_path );
1908 /* Hack to allow udp://@:port syntax */
1910 (strncmp( psz_access, "udp", 3 ) &&
1911 strncmp( psz_access, "rtp", 3 )) )
1913 /* Find optional titles and seekpoints */
1914 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
1915 &in->i_seekpoint_start, &in->i_seekpoint_end );
1917 if( psz_forced_demux && *psz_forced_demux )
1919 psz_demux = psz_forced_demux;
1921 else if( !psz_demux || *psz_demux == '\0' )
1923 /* special hack for forcing a demuxer with --demux=module
1924 * (and do nothing with a list) */
1925 char *psz_var_demux = var_GetString( p_input, "demux" );
1927 if( *psz_var_demux != '\0' &&
1928 !strchr(psz_var_demux, ',' ) &&
1929 !strchr(psz_var_demux, ':' ) )
1931 psz_demux = psz_var_demux;
1933 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
1935 else if( psz_var_demux )
1937 free( psz_var_demux );
1941 /* Try access_demux if no demux given */
1942 if( *psz_demux == '\0' )
1944 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
1945 NULL, p_input->p_es_out, VLC_FALSE );
1951 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
1952 psz_demux = strdup( "" );
1953 psz_access = strdup( "file" );
1958 int64_t i_pts_delay;
1960 /* Get infos from access_demux */
1961 demux2_Control( in->p_demux,
1962 DEMUX_GET_PTS_DELAY, &i_pts_delay );
1963 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
1965 in->b_title_demux = VLC_TRUE;
1966 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
1967 &in->title, &in->i_title,
1968 &in->i_title_offset, &in->i_seekpoint_offset ) )
1973 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
1974 &in->b_can_pace_control );
1975 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
1979 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
1985 int64_t i_pts_delay;
1987 /* Now try a real access */
1988 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
1991 /* Access failed, URL encoded ? */
1992 if( in->p_access == NULL && strchr( psz_path, '%' ) )
1994 DecodeUrl( psz_path );
1996 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
1997 psz_access, psz_demux, psz_path );
1999 in->p_access = access2_New( p_input,
2000 psz_access, psz_demux, psz_path,
2003 #ifndef WIN32 /* Remove this gross hack from the win32 build as colons
2004 * are forbidden in filenames on Win32. */
2006 /* Maybe we got something like: /Volumes/toto:titi/gabu.mpg */
2007 if( in->p_access == NULL &&
2008 *psz_access == '\0' && ( *psz_demux || *psz_path ) )
2010 if( psz_dup ) free( psz_dup );
2011 psz_dup = strdup( psz_mrl );
2013 if( psz_forced_demux && *psz_forced_demux )
2015 psz_demux = psz_forced_demux;
2017 else psz_demux = "";
2020 in->p_access = access2_New( p_input,
2021 psz_access, psz_demux, psz_path,
2026 if( in->p_access == NULL )
2028 msg_Err( p_input, "no suitable access module for `%s'", psz_mrl );
2029 intf_UserFatal( VLC_OBJECT( p_input),
2030 _("Errors"),"Unable to open '%s'", psz_mrl );
2035 psz_tmp = psz = var_GetString( p_input, "access-filter" );
2036 while( psz && *psz )
2038 access_t *p_access = in->p_access;
2039 char *end = strchr( psz, ':' );
2044 in->p_access = access2_FilterNew( in->p_access, psz );
2045 if( in->p_access == NULL )
2047 in->p_access = p_access;
2048 msg_Warn( p_input, "failed to insert access filter %s",
2054 if( psz_tmp ) free( psz_tmp );
2056 /* Get infos from access */
2059 access2_Control( in->p_access,
2060 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2061 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2063 in->b_title_demux = VLC_FALSE;
2064 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2065 &in->title, &in->i_title,
2066 &in->i_title_offset, &in->i_seekpoint_offset ) )
2072 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2073 &in->b_can_pace_control );
2074 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2076 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2078 var_Set( p_input, "seekable", val );
2081 /* Create the stream_t */
2082 in->p_stream = stream_AccessNew( in->p_access, b_quick );
2083 if( in->p_stream == NULL )
2085 msg_Warn( p_input, "cannot create a stream_t from access" );
2089 /* Open a demuxer */
2090 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2092 psz_demux = in->p_access->psz_demux;
2094 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2095 in->p_stream, p_input->p_es_out, b_quick );
2096 if( in->p_demux == NULL )
2098 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2099 psz_access, psz_demux, psz_path );
2100 intf_UserFatal( VLC_OBJECT( p_input), _("Errors"),
2101 "Unrecognized format for '%s'", psz_mrl );
2105 /* TODO get title from demux */
2106 if( !b_quick && in->i_title <= 0 )
2108 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2109 &in->title, &in->i_title,
2110 &in->i_title_offset, &in->i_seekpoint_offset ))
2117 in->b_title_demux = VLC_TRUE;
2122 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2123 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2125 if( psz_dup ) free( psz_dup );
2130 demux2_Delete( in->p_demux );
2133 stream_Delete( in->p_stream );
2136 access2_Delete( in->p_access );
2137 if( psz_dup ) free( psz_dup );
2139 return VLC_EGENERIC;
2142 /*****************************************************************************
2144 *****************************************************************************/
2145 static void InputSourceClean( input_thread_t *p_input, input_source_t *in )
2148 demux2_Delete( in->p_demux );
2151 stream_Delete( in->p_stream );
2154 access2_Delete( in->p_access );
2156 if( in->i_title > 0 )
2159 for( i = 0; i < in->i_title; i++ )
2161 vlc_input_title_Delete( in->title[i] );
2167 static void SlaveDemux( input_thread_t *p_input )
2172 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2174 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2178 for( i = 0; i < p_input->i_slave; i++ )
2180 input_source_t *in = p_input->slave[i];
2186 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2191 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2193 msg_Err( p_input, "slave[%d] doesn't like "
2194 "DEMUX_GET_TIME -> EOF", i );
2199 if( i_stime >= i_time )
2202 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2208 i_ret = in->p_demux->pf_demux( in->p_demux );
2213 msg_Dbg( p_input, "slave %d EOF", i );
2214 in->b_eof = VLC_TRUE;
2219 static void SlaveSeek( input_thread_t *p_input )
2224 if( demux2_Control( p_input->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2226 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2230 for( i = 0; i < p_input->i_slave; i++ )
2232 input_source_t *in = p_input->slave[i];
2234 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2236 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2237 in->b_eof = VLC_TRUE;
2241 /*****************************************************************************
2243 *****************************************************************************/
2244 static void InputMetaUser( input_thread_t *p_input )
2246 vlc_meta_t *p_meta = p_input->input.p_item->p_meta;
2249 if( !p_meta ) return;
2251 /* Get meta information from user */
2252 #define GET_META( field, s ) \
2253 var_Get( p_input, (s), &val ); \
2254 if( *val.psz_string ) { \
2255 if( p_meta->psz_ ## field ) free ( p_meta->psz_ ## field ); \
2256 p_meta->psz_ ## field = strdup( val.psz_string ); \
2258 free( val.psz_string )
2260 GET_META( title, "meta-title" );
2261 GET_META( author, "meta-author" );
2262 GET_META( artist, "meta-artist" );
2263 GET_META( genre, "meta-genre" );
2264 GET_META( copyright, "meta-copyright" );
2265 GET_META( description, "meta-description" );
2266 GET_META( date, "meta-date" );
2267 GET_META( url, "meta-url" );
2271 /*****************************************************************************
2272 * DecodeUrl: decode a given encoded url
2273 *****************************************************************************/
2274 static void DecodeUrl( char *psz )
2276 char *dup = strdup( psz );
2294 *psz++ = strtol( val, NULL, 16 );
2296 else if( *p == '+' )
2306 if( psz ) *psz++ ='\0';
2307 if( dup ) free( dup );
2310 /*****************************************************************************
2311 * MRLSplit: parse the access, demux and url part of the
2312 * Media Resource Locator.
2313 *****************************************************************************/
2314 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2315 char **ppsz_access, char **ppsz_demux, char **ppsz_path )
2317 char *psz_access = NULL;
2318 char *psz_demux = NULL;
2319 char *psz_path = NULL;
2320 char *psz, *psz_check;
2322 psz = strchr( psz_dup, ':' );
2324 /* '@' not allowed in access/demux part */
2325 psz_check = strchr( psz_dup, '@' );
2326 if( psz_check && psz_check < psz ) psz = 0;
2328 #if defined( WIN32 ) || defined( UNDER_CE )
2329 if( psz - psz_dup == 1 )
2331 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2340 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2344 psz = strchr( psz_dup, '/' );
2351 psz_access = psz_dup;
2358 if( !psz_access ) *ppsz_access = "";
2359 else *ppsz_access = psz_access;
2361 if( !psz_demux ) *ppsz_demux = "";
2362 else *ppsz_demux = psz_demux;
2364 if( !psz_path ) *ppsz_path = "";
2365 else *ppsz_path = psz_path;
2368 /*****************************************************************************
2369 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2372 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2373 *****************************************************************************/
2374 static void MRLSections( input_thread_t *p_input, char *psz_source,
2375 int *pi_title_start, int *pi_title_end,
2376 int *pi_chapter_start, int *pi_chapter_end )
2378 char *psz, *psz_end, *psz_next, *psz_check;
2380 *pi_title_start = *pi_title_end = -1;
2381 *pi_chapter_start = *pi_chapter_end = -1;
2383 /* Start by parsing titles and chapters */
2384 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2386 /* Check we are really dealing with a title/chapter section */
2387 psz_check = psz + 1;
2388 if( !*psz_check ) return;
2389 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2390 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2391 if( *psz_check == ':' && ++psz_check )
2392 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2393 if( *psz_check != '-' && *psz_check ) return;
2394 if( *psz_check == '-' && ++psz_check )
2395 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2396 if( *psz_check != ':' && *psz_check ) return;
2397 if( *psz_check == ':' && ++psz_check )
2398 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2399 if( *psz_check ) return;
2401 /* Separate start and end */
2403 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2405 /* Look for the start title */
2406 *pi_title_start = strtol( psz, &psz_next, 0 );
2407 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2408 *pi_title_end = *pi_title_start;
2411 /* Look for the start chapter */
2413 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2414 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2415 *pi_chapter_end = *pi_chapter_start;
2419 /* Look for the end title */
2420 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2421 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2424 /* Look for the end chapter */
2425 if( *psz_end ) psz_end++;
2426 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2427 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2430 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2431 psz_source, *pi_title_start, *pi_chapter_start,
2432 *pi_title_end, *pi_chapter_end );
2435 /*****************************************************************************
2436 * input_AddSubtitles: add a subtitles file and enable it
2437 *****************************************************************************/
2438 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2439 vlc_bool_t b_check_extension )
2441 input_source_t *sub;
2445 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2450 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2452 sub = InputSourceNew( p_input );
2453 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle", VLC_FALSE ) )
2455 TAB_APPEND( p_input->i_slave, p_input->slave, sub );
2458 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2460 if( count.i_int == 0 )
2462 /* if it was first one, there is disable too */
2464 if( count.i_int < list.p_list->i_count )
2466 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2467 &list.p_list->p_values[count.i_int] );
2469 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );