1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 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_common.h>
38 #include "input_internal.h"
41 #include "es_out_timeshift.h"
47 #include "../stream_output/stream_output.h"
49 #include <vlc_interface.h>
51 #include <vlc_charset.h>
52 #include <vlc_strings.h>
54 #ifdef HAVE_SYS_STAT_H
55 # include <sys/stat.h>
58 /*****************************************************************************
60 *****************************************************************************/
61 static void Destructor( input_thread_t * p_input );
63 static void* Run ( vlc_object_t *p_this );
64 static void* RunAndDestroy ( vlc_object_t *p_this );
66 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
67 const char *, bool, sout_instance_t * );
68 static int Init ( input_thread_t *p_input );
69 static void WaitDie ( input_thread_t *p_input );
70 static void End ( input_thread_t *p_input );
71 static void MainLoop( input_thread_t *p_input );
73 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline );
74 static void ControlReduce( input_thread_t * );
75 static bool Control( input_thread_t *, int, vlc_value_t );
77 static int UpdateFromAccess( input_thread_t * );
78 static int UpdateFromDemux( input_thread_t * );
80 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
82 static input_source_t *InputSourceNew( input_thread_t *);
83 static int InputSourceInit( input_thread_t *, input_source_t *,
84 const char *, const char *psz_forced_demux );
85 static void InputSourceClean( input_source_t * );
87 //static void InputGetAttachments( input_thread_t *, input_source_t * );
88 static void SlaveDemux( input_thread_t *p_input );
89 static void SlaveSeek( input_thread_t *p_input );
91 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
92 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
93 static char *InputGetExtraFiles( input_thread_t *p_input,
94 const char *psz_access, const char *psz_path );
96 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
97 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
98 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
99 int i_new, input_attachment_t **pp_new );
101 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced );
103 static void input_ChangeState( input_thread_t *p_input, int i_state ); /* TODO fix name */
105 /*****************************************************************************
106 * This function creates a new input, and returns a pointer
107 * to its description. On error, it returns NULL.
109 * Variables for _public_ use:
112 * - rate,rate-slower, rate-faster
113 * - position, position-offset
114 * - time, time-offset
115 * - title,title-next,title-prev
116 * - chapter,chapter-next, chapter-prev
117 * - program, audio-es, video-es, spu-es
118 * - audio-delay, spu-delay
123 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown FIXME rename can-seek
124 * or not, for that check position != 0.0)
126 * - can-record (if a stream can be recorded while playing)
127 * - teletext-es to get the index of spu track that is teletext --1 if no teletext)
128 * * For intf callback upon changes:
130 * - intf-change-vout for when a vout is created or destroyed
131 * - rate-change for when playback rate changes
132 * - stats-change for when statistics are updated
133 * TODO explain when Callback is called
134 * TODO complete this list (?)
135 *****************************************************************************/
136 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
137 const char *psz_header, bool b_quick,
138 sout_instance_t *p_sout )
140 static const char input_name[] = "input";
141 input_thread_t *p_input = NULL; /* thread descriptor */
145 /* Allocate descriptor */
146 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
147 VLC_OBJECT_INPUT, input_name );
148 if( p_input == NULL )
151 /* Construct a nice name for the input timer */
152 char psz_timer_name[255];
153 char * psz_name = input_item_GetName( p_item );
154 snprintf( psz_timer_name, sizeof(psz_timer_name),
155 "input launching for '%s'", psz_name );
157 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
161 /* Start a timer to mesure how long it takes
162 * to launch an input */
163 stats_TimerStart( p_input, psz_timer_name,
164 STATS_TIMER_INPUT_LAUNCHING );
166 p_input->p = calloc( 1, sizeof( input_thread_private_t ) );
170 /* One "randomly" selected input thread is responsible for computing
171 * the global stats. Check if there is already someone doing this */
172 if( p_input->p_libvlc->p_stats && !b_quick )
174 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
175 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
176 if( p_private->p_stats_computer == NULL )
177 p_private->p_stats_computer = p_input;
178 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
181 p_input->b_preparsing = b_quick;
182 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
185 vlc_event_manager_t * p_em = &p_input->p->event_manager;
186 vlc_event_manager_init_with_vlc_object( p_em, p_input );
187 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
188 vlc_event_manager_register_event_type( p_em, vlc_InputSelectedStreamChanged );
190 /* Init Common fields */
191 p_input->b_eof = false;
192 p_input->b_can_pace_control = true;
193 p_input->p->i_start = 0;
195 p_input->p->i_stop = 0;
196 p_input->p->i_run = 0;
197 p_input->p->i_title = 0;
198 p_input->p->title = NULL;
199 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
200 p_input->i_state = INIT_S;
201 p_input->p->i_rate = INPUT_RATE_DEFAULT;
202 p_input->p->b_recording = false;
203 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
204 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
205 p_input->p->p_es_out_display = NULL;
206 p_input->p->p_es_out = NULL;
207 p_input->p->p_sout = NULL;
208 p_input->p->b_out_pace_control = false;
209 p_input->i_pts_delay = 0;
211 /* Init Input fields */
212 vlc_gc_incref( p_item ); /* Released in Destructor() */
213 p_input->p->input.p_item = p_item;
214 p_input->p->input.p_access = NULL;
215 p_input->p->input.p_stream = NULL;
216 p_input->p->input.p_demux = NULL;
217 p_input->p->input.b_title_demux = false;
218 p_input->p->input.i_title = 0;
219 p_input->p->input.title = NULL;
220 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
221 p_input->p->input.b_can_pace_control = true;
222 p_input->p->input.b_can_rate_control = true;
223 p_input->p->input.b_rescale_ts = true;
224 p_input->p->input.b_eof = false;
225 p_input->p->input.i_cr_average = 0;
227 vlc_mutex_lock( &p_item->lock );
229 if( !p_item->p_stats )
230 p_item->p_stats = stats_NewInputStats( p_input );
231 vlc_mutex_unlock( &p_item->lock );
234 p_input->p->i_slave = 0;
235 p_input->p->slave = NULL;
237 /* Init control buffer */
238 vlc_mutex_init( &p_input->p->lock_control );
239 vlc_cond_init( &p_input->p->wait_control );
240 p_input->p->i_control = 0;
242 /* Parse input options */
243 vlc_mutex_lock( &p_item->lock );
244 assert( (int)p_item->optflagc == p_item->i_options );
245 for( i = 0; i < p_item->i_options; i++ )
246 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
247 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
248 vlc_mutex_unlock( &p_item->lock );
250 /* Create Object Variables for private use only */
251 input_ConfigVarInit( p_input );
253 /* Create Objects variables for public Get and Set */
254 input_ControlVarInit( p_input );
257 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
259 if( !p_input->b_preparsing )
261 var_Get( p_input, "bookmarks", &val );
264 /* FIXME: have a common cfg parsing routine used by sout and others */
265 char *psz_parser, *psz_start, *psz_end;
266 psz_parser = val.psz_string;
267 while( (psz_start = strchr( psz_parser, '{' ) ) )
269 seekpoint_t *p_seekpoint;
272 psz_end = strchr( psz_start, '}' );
273 if( !psz_end ) break;
274 psz_parser = psz_end + 1;
275 backup = *psz_parser;
279 p_seekpoint = vlc_seekpoint_New();
280 while( (psz_end = strchr( psz_start, ',' ) ) )
283 if( !strncmp( psz_start, "name=", 5 ) )
285 p_seekpoint->psz_name = strdup(psz_start + 5);
287 else if( !strncmp( psz_start, "bytes=", 6 ) )
289 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
291 else if( !strncmp( psz_start, "time=", 5 ) )
293 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
296 psz_start = psz_end + 1;
298 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
299 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
300 p_seekpoint->i_time_offset );
301 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
302 vlc_seekpoint_Delete( p_seekpoint );
303 *psz_parser = backup;
305 free( val.psz_string );
309 /* Remove 'Now playing' info as it is probably outdated */
310 input_item_SetNowPlaying( p_item, NULL );
311 input_SendEventMeta( p_input );
314 if( p_input->b_preparsing )
315 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
319 p_input->p->p_sout = p_sout;
321 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
322 vlc_mutex_init( &p_input->p->counters.counters_lock );
324 /* Set the destructor when we are sure we are initialized */
325 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
327 /* Attach only once we are ready */
328 vlc_object_attach( p_input, p_parent );
334 * Input destructor (called when the object's refcount reaches 0).
336 static void Destructor( input_thread_t * p_input )
339 char * psz_name = input_item_GetName( p_input->p->input.p_item );
340 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
344 vlc_event_manager_fini( &p_input->p->event_manager );
346 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
347 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
349 if( p_input->p->p_sout )
350 sout_DeleteInstance( p_input->p->p_sout );
352 vlc_gc_decref( p_input->p->input.p_item );
354 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
356 vlc_cond_destroy( &p_input->p->wait_control );
357 vlc_mutex_destroy( &p_input->p->lock_control );
362 * Initialize an input thread and run it. You will need to monitor the
363 * thread to clean up after it is done
365 * \param p_parent a vlc_object
366 * \param p_item an input item
367 * \return a pointer to the spawned input thread
369 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
370 input_item_t *p_item )
372 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
376 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
377 input_item_t *p_item,
378 const char *psz_log, sout_instance_t *p_sout )
380 input_thread_t *p_input;
382 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
386 /* Create thread and wait for its readiness. */
387 if( vlc_thread_create( p_input, "input", Run,
388 VLC_THREAD_PRIORITY_INPUT, false ) )
390 input_ChangeState( p_input, ERROR_S );
391 msg_Err( p_input, "cannot create input thread" );
392 vlc_object_detach( p_input );
393 vlc_object_release( p_input );
401 * Initialize an input thread and run it. This thread will clean after itself,
402 * you can forget about it. It can work either in blocking or non-blocking mode
404 * \param p_parent a vlc_object
405 * \param p_item an input item
406 * \param b_block should we block until read is finished ?
407 * \return an error code, VLC_SUCCESS on success
409 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
412 input_thread_t *p_input;
414 p_input = Create( p_parent, p_item, NULL, false, NULL );
420 RunAndDestroy( VLC_OBJECT(p_input) );
425 if( vlc_thread_create( p_input, "input", RunAndDestroy,
426 VLC_THREAD_PRIORITY_INPUT, false ) )
428 input_ChangeState( p_input, ERROR_S );
429 msg_Err( p_input, "cannot create input thread" );
430 vlc_object_release( p_input );
438 * Initialize an input and initialize it to preparse the item
439 * This function is blocking. It will only accept to parse files
441 * \param p_parent a vlc_object_t
442 * \param p_item an input item
443 * \return VLC_SUCCESS or an error
445 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
447 input_thread_t *p_input;
449 /* Allocate descriptor */
450 p_input = Create( p_parent, p_item, NULL, true, NULL );
454 if( !Init( p_input ) )
457 vlc_object_detach( p_input );
458 vlc_object_release( p_input );
464 * Request a running input thread to stop and die
466 * \param the input thread to stop
468 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
473 if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
474 p_obj->i_object_type == VLC_OBJECT_AOUT ||
475 p_obj == VLC_OBJECT(p_input->p->p_sout) )
478 vlc_object_kill( p_obj );
480 p_list = vlc_list_children( p_obj );
481 for( i = 0; i < p_list->i_count; i++ )
482 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
483 vlc_list_release( p_list );
485 void input_StopThread( input_thread_t *p_input )
487 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
488 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
489 * unlock the control loop */
490 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
492 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
495 sout_instance_t *input_DetachSout( input_thread_t *p_input )
497 assert( p_input->b_dead );
498 sout_instance_t *p_sout = p_input->p->p_sout;
499 vlc_object_detach( p_sout );
500 p_input->p->p_sout = NULL;
504 /*****************************************************************************
505 * Run: main thread loop
506 * This is the "normal" thread that spawns the input processing chain,
507 * reads the stream, cleans up and waits
508 *****************************************************************************/
509 static void *Run( vlc_object_t *p_this )
511 input_thread_t *p_input = (input_thread_t *)p_this;
512 const int canc = vlc_savecancel();
514 if( Init( p_input ) )
516 /* If we failed, wait before we are killed, and exit */
523 /* Wait until we are asked to die */
524 if( !p_input->b_die )
531 p_input->b_dead = true;
532 vlc_restorecancel( canc );
536 /*****************************************************************************
537 * RunAndDestroy: main thread loop
538 * This is the "just forget me" thread that spawns the input processing chain,
539 * reads the stream, cleans up and releases memory
540 *****************************************************************************/
541 static void *RunAndDestroy( vlc_object_t *p_this )
543 input_thread_t *p_input = (input_thread_t *)p_this;
544 const int canc = vlc_savecancel();
546 if( Init( p_input ) )
556 vlc_object_release( p_input );
557 vlc_restorecancel( canc );
561 /*****************************************************************************
562 * Main loop: Fill buffers from access, and demux
563 *****************************************************************************/
567 * It asks the demuxer to demux some data
569 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t *pi_start_mdate )
575 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
576 ( p_input->p->i_run > 0 && *pi_start_mdate+p_input->p->i_run < mdate() ) )
579 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
584 if( p_input->p->input.b_title_demux &&
585 p_input->p->input.p_demux->info.i_update )
587 i_ret = UpdateFromDemux( p_input );
590 else if( !p_input->p->input.b_title_demux &&
591 p_input->p->input.p_access &&
592 p_input->p->input.p_access->info.i_update )
594 i_ret = UpdateFromAccess( p_input );
599 if( i_ret == 0 ) /* EOF */
603 var_Get( p_input, "input-repeat", &repeat );
604 if( repeat.i_int == 0 )
606 /* End of file - we do not set b_die because only the
607 * playlist is allowed to do so. */
608 msg_Dbg( p_input, "EOF reached" );
609 p_input->p->input.b_eof = true;
615 msg_Dbg( p_input, "repeating the same input (%d)",
617 if( repeat.i_int > 0 )
620 var_Set( p_input, "input-repeat", repeat );
623 /* Seek to start title/seekpoint */
624 val.i_int = p_input->p->input.i_title_start -
625 p_input->p->input.i_title_offset;
626 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
628 input_ControlPush( p_input,
629 INPUT_CONTROL_SET_TITLE, &val );
631 val.i_int = p_input->p->input.i_seekpoint_start -
632 p_input->p->input.i_seekpoint_offset;
633 if( val.i_int > 0 /* TODO: check upper boundary */ )
634 input_ControlPush( p_input,
635 INPUT_CONTROL_SET_SEEKPOINT, &val );
637 /* Seek to start position */
638 if( p_input->p->i_start > 0 )
640 val.i_time = p_input->p->i_start;
641 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
647 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
652 *pi_start_mdate = mdate();
657 input_ChangeState( p_input, ERROR_S );
660 if( i_ret > 0 && p_input->p->i_slave > 0 )
662 SlaveDemux( p_input );
668 * It update the variables used by the interfaces
670 static void MainLoopInterface( input_thread_t *p_input )
672 input_event_times_t ev;
678 /* update input status variables */
679 if( demux_Control( p_input->p->input.p_demux,
680 DEMUX_GET_POSITION, &ev.f_position ) )
683 if( demux_Control( p_input->p->input.p_demux,
684 DEMUX_GET_TIME, &ev.i_time ) )
687 if( demux_Control( p_input->p->input.p_demux,
688 DEMUX_GET_LENGTH, &ev.i_length ) )
691 input_SendEventTimes( p_input, &ev );
696 * It updates the globals statics
698 static void MainLoopStatistic( input_thread_t *p_input )
700 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
701 /* Are we the thread responsible for computing global stats ? */
702 if( libvlc_priv( p_input->p_libvlc )->p_stats_computer == p_input )
704 stats_ComputeGlobalStats( p_input->p_libvlc,
705 p_input->p_libvlc->p_stats );
707 input_SendEventStatistics( p_input );
712 * The main input loop.
714 static void MainLoop( input_thread_t *p_input )
716 mtime_t i_start_mdate = mdate();
717 mtime_t i_intf_update = 0;
718 mtime_t i_statistic_update = 0;
720 /* Start the timer */
721 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
723 while( vlc_object_alive( p_input ) && !p_input->b_error )
734 b_force_update = false;
736 /* FIXME if p_input->i_state == PAUSE_S the access/access_demux
737 * is paused -> this may cause problem with some of them
738 * The same problem can be seen when seeking while paused */
739 b_paused = p_input->i_state == PAUSE_S &&
740 !es_out_GetBuffering( p_input->p->p_es_out );
744 if( !p_input->p->input.b_eof )
746 MainLoopDemux( p_input, &b_force_update, &i_start_mdate );
748 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
750 else if( !p_input->b_eof && !es_out_GetEmpty( p_input->p->p_es_out ) )
752 msg_Dbg( p_input, "waiting decoder fifos to empty" );
753 i_wakeup = mdate() + INPUT_IDLE_SLEEP;
763 i_deadline = i_wakeup;
765 i_deadline = __MIN( i_intf_update, i_statistic_update );
768 vlc_mutex_lock( &p_input->p->lock_control );
769 ControlReduce( p_input );
770 while( !ControlPopNoLock( p_input, &i_type, &val, i_deadline ) )
772 msg_Dbg( p_input, "control type=%d", i_type );
774 if( Control( p_input, i_type, val ) )
775 b_force_update = true;
777 vlc_mutex_unlock( &p_input->p->lock_control );
779 /* Update interface and statistics */
781 if( i_intf_update < i_current || b_force_update )
783 MainLoopInterface( p_input );
784 i_intf_update = i_current + INT64_C(250000);
785 b_force_update = false;
787 if( i_statistic_update < i_current )
789 MainLoopStatistic( p_input );
790 i_statistic_update = i_current + INT64_C(1000000);
793 /* Check if i_wakeup is still valid */
796 mtime_t i_new_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
800 } while( i_current < i_wakeup );
803 if( !p_input->b_error )
804 input_ChangeState( p_input, END_S );
807 static void InitStatistics( input_thread_t * p_input )
809 if( p_input->b_preparsing ) return;
811 /* Prepare statistics */
812 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
813 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
814 if( libvlc_stats( p_input ) )
816 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
817 INIT_COUNTER( read_packets, INTEGER, COUNTER );
818 INIT_COUNTER( demux_read, INTEGER, COUNTER );
819 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
820 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
821 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
822 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
823 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
824 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
825 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
826 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
827 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
828 p_input->p->counters.p_sout_send_bitrate = NULL;
829 p_input->p->counters.p_sout_sent_packets = NULL;
830 p_input->p->counters.p_sout_sent_bytes = NULL;
831 if( p_input->p->counters.p_demux_bitrate )
832 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
833 if( p_input->p->counters.p_input_bitrate )
834 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
839 static int InitSout( input_thread_t * p_input )
843 if( p_input->b_preparsing ) return VLC_SUCCESS;
845 /* Find a usable sout and attach it to p_input */
846 psz = var_GetNonEmptyString( p_input, "sout" );
847 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
849 /* Check the validity of the provided sout */
850 if( p_input->p->p_sout )
852 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
854 msg_Dbg( p_input, "destroying unusable sout" );
856 sout_DeleteInstance( p_input->p->p_sout );
857 p_input->p->p_sout = NULL;
861 if( p_input->p->p_sout )
864 msg_Dbg( p_input, "sout keep: reusing sout" );
865 msg_Dbg( p_input, "sout keep: you probably want to use "
866 "gather stream_out" );
867 vlc_object_attach( p_input->p->p_sout, p_input );
871 /* Create a new one */
872 p_input->p->p_sout = sout_NewInstance( p_input, psz );
873 if( !p_input->p->p_sout )
875 input_ChangeState( p_input, ERROR_S );
876 msg_Err( p_input, "cannot start stream output instance, " \
882 if( libvlc_stats( p_input ) )
884 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
885 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
886 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
887 if( p_input->p->counters.p_sout_send_bitrate )
888 p_input->p->counters.p_sout_send_bitrate->update_interval =
892 else if( p_input->p->p_sout )
894 msg_Dbg( p_input, "destroying useless sout" );
896 sout_DeleteInstance( p_input->p->p_sout );
897 p_input->p->p_sout = NULL;
905 static void InitTitle( input_thread_t * p_input )
909 if( p_input->b_preparsing ) return;
911 /* Create global title (from master) */
912 p_input->p->i_title = p_input->p->input.i_title;
913 p_input->p->title = p_input->p->input.title;
914 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
915 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
916 if( p_input->p->i_title > 0 )
918 /* Setup variables */
919 input_ControlVarNavigation( p_input );
920 input_ControlVarTitle( p_input, 0 );
924 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
925 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
926 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
929 if( p_input->i_pts_delay < 0 )
930 p_input->i_pts_delay = 0;
932 /* If the desynchronisation requested by the user is < 0, we need to
933 * cache more data. */
934 var_Get( p_input, "audio-desync", &val );
936 p_input->i_pts_delay -= (val.i_int * 1000);
938 /* Update cr_average depending on the caching */
939 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
940 p_input->p->input.i_cr_average /= 10;
941 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
944 static void StartTitle( input_thread_t * p_input )
953 /* Start title/chapter */
955 if( p_input->b_preparsing )
957 p_input->p->i_start = 0;
961 val.i_int = p_input->p->input.i_title_start -
962 p_input->p->input.i_title_offset;
963 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
964 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
965 val.i_int = p_input->p->input.i_seekpoint_start -
966 p_input->p->input.i_seekpoint_offset;
967 if( val.i_int > 0 /* TODO: check upper boundary */ )
968 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
972 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
973 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
974 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
975 i_length = var_GetTime( p_input, "length" );
976 if( p_input->p->i_run < 0 )
978 msg_Warn( p_input, "invalid run-time ignored" );
979 p_input->p->i_run = 0;
982 if( p_input->p->i_start > 0 )
984 if( p_input->p->i_start >= i_length )
986 msg_Warn( p_input, "invalid start-time ignored" );
992 msg_Dbg( p_input, "starting at time: %ds",
993 (int)( p_input->p->i_start / INT64_C(1000000) ) );
995 s.i_time = p_input->p->i_start;
996 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
999 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
1001 msg_Warn( p_input, "invalid stop-time ignored" );
1002 p_input->p->i_stop = 0;
1005 /* Load subtitles */
1006 /* Get fps and set it if not already set */
1007 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
1010 float f_requested_fps;
1012 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
1013 var_SetFloat( p_input, "sub-original-fps", f_fps );
1015 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1016 if( f_requested_fps != f_fps )
1018 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1019 VLC_VAR_DOINHERIT );
1020 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1024 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1027 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1030 /* Look for and add subtitle files */
1031 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1032 if( psz_subtitle != NULL )
1034 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1035 SubtitleAdd( p_input, psz_subtitle, true );
1038 var_Get( p_input, "sub-autodetect-file", &val );
1041 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1042 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1043 p_input->p->input.p_item->psz_uri );
1044 free( psz_autopath );
1046 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1048 /* Try to autoselect the first autodetected subtitles file
1049 * if no subtitles file was specified */
1050 bool b_forced = i == 0 && !psz_subtitle;
1052 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1053 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1055 free( ppsz_subs[i] );
1059 free( psz_subtitle );
1061 /* Look for slave */
1062 psz = var_GetNonEmptyString( p_input, "input-slave" );
1066 input_source_t *slave;
1067 while( psz && *psz )
1069 while( *psz == ' ' || *psz == '#' )
1073 if( ( psz_delim = strchr( psz, '#' ) ) )
1075 *psz_delim++ = '\0';
1082 msg_Dbg( p_input, "adding slave input '%s'", psz );
1083 slave = InputSourceNew( p_input );
1084 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1086 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1095 static void InitPrograms( input_thread_t * p_input )
1100 if( p_input->b_preparsing ) return;
1103 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1104 i_es_out_mode = ES_OUT_MODE_AUTO;
1106 if( p_input->p->p_sout )
1108 var_Get( p_input, "sout-all", &val );
1111 i_es_out_mode = ES_OUT_MODE_ALL;
1116 var_Get( p_input, "programs", &val );
1117 if( val.p_list && val.p_list->i_count )
1119 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1120 /* Note : we should remove the "program" callback. */
1124 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1129 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1131 /* Inform the demuxer about waited group (needed only for DVB) */
1132 if( i_es_out_mode == ES_OUT_MODE_ALL )
1134 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1136 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1138 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1143 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1144 (int) var_GetInteger( p_input, "program" ), NULL );
1148 static int Init( input_thread_t * p_input )
1153 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1155 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1157 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1158 var_SetString( p_input, "sout", "" );
1159 var_SetBool( p_input, "sout-all", false );
1160 var_SetString( p_input, "input-slave", "" );
1161 var_SetInteger( p_input, "input-repeat", 0 );
1162 var_SetString( p_input, "sub-file", "" );
1163 var_SetBool( p_input, "sub-autodetect-file", false );
1167 InitStatistics( p_input );
1169 ret = InitSout( p_input );
1170 if( ret != VLC_SUCCESS )
1171 return ret; /* FIXME: goto error; should be better here */
1175 p_input->p->p_es_out_display = input_EsOutNew( p_input, p_input->p->i_rate );
1176 p_input->p->p_es_out = input_EsOutTimeshiftNew( p_input, p_input->p->p_es_out_display, p_input->p->i_rate );
1177 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1178 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1180 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1181 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1183 if( InputSourceInit( p_input, &p_input->p->input,
1184 p_input->p->input.p_item->psz_uri, NULL ) )
1189 InitTitle( p_input );
1191 /* Load master infos */
1193 input_event_times_t ev_times;
1194 ev_times.f_position = 0;
1195 ev_times.i_time = 0;
1196 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1197 &ev_times.i_length ) )
1198 ev_times.i_length = 0;
1199 if( ev_times.i_length <= 0 )
1200 ev_times.i_length = input_item_GetDuration( p_input->p->input.p_item );
1201 input_SendEventTimes( p_input, &ev_times );
1203 StartTitle( p_input );
1205 InitPrograms( p_input );
1207 if( !p_input->b_preparsing && p_input->p->p_sout )
1209 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1211 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1213 /* We don't want a high input priority here or we'll
1214 * end-up sucking up all the CPU time */
1215 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1218 msg_Dbg( p_input, "starting in %s mode",
1219 p_input->p->b_out_pace_control ? "async" : "sync" );
1222 p_meta = vlc_meta_New();
1224 /* Get meta data from users */
1225 InputMetaUser( p_input, p_meta );
1227 /* Get meta data from master input */
1228 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1230 /* Access_file does not give any meta, and there are no slave */
1231 AccessMeta( p_input, p_meta );
1233 InputUpdateMeta( p_input, p_meta );
1235 if( !p_input->b_preparsing )
1237 msg_Dbg( p_input, "`%s' successfully opened",
1238 p_input->p->input.p_item->psz_uri );
1242 /* initialization is complete */
1243 input_ChangeState( p_input, PLAYING_S );
1248 input_ChangeState( p_input, ERROR_S );
1250 if( p_input->p->p_es_out )
1251 es_out_Delete( p_input->p->p_es_out );
1252 if( p_input->p->p_es_out_display )
1253 es_out_Delete( p_input->p->p_es_out_display );
1255 if( p_input->p->p_sout )
1257 vlc_object_detach( p_input->p->p_sout );
1258 sout_DeleteInstance( p_input->p->p_sout );
1262 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1264 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1265 stats_CounterClean( p_input->p->counters.p_##c );\
1266 p_input->p->counters.p_##c = NULL; } while(0)
1267 EXIT_COUNTER( read_bytes );
1268 EXIT_COUNTER( read_packets );
1269 EXIT_COUNTER( demux_read );
1270 EXIT_COUNTER( input_bitrate );
1271 EXIT_COUNTER( demux_bitrate );
1272 EXIT_COUNTER( played_abuffers );
1273 EXIT_COUNTER( lost_abuffers );
1274 EXIT_COUNTER( displayed_pictures );
1275 EXIT_COUNTER( lost_pictures );
1276 EXIT_COUNTER( decoded_audio );
1277 EXIT_COUNTER( decoded_video );
1278 EXIT_COUNTER( decoded_sub );
1280 if( p_input->p->p_sout )
1282 EXIT_COUNTER( sout_sent_packets );
1283 EXIT_COUNTER( sout_sent_bytes );
1284 EXIT_COUNTER( sout_send_bitrate );
1289 /* Mark them deleted */
1290 p_input->p->input.p_demux = NULL;
1291 p_input->p->input.p_stream = NULL;
1292 p_input->p->input.p_access = NULL;
1293 p_input->p->p_es_out = NULL;
1294 p_input->p->p_es_out_display = NULL;
1295 p_input->p->p_sout = NULL;
1297 return VLC_EGENERIC;
1300 /*****************************************************************************
1301 * WaitDie: Wait until we are asked to die.
1302 *****************************************************************************
1303 * This function is called when an error occurred during thread main's loop.
1304 *****************************************************************************/
1305 static void WaitDie( input_thread_t *p_input )
1307 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1309 /* Wait a die order */
1310 vlc_object_lock( p_input );
1311 while( vlc_object_alive( p_input ) )
1312 vlc_object_wait( p_input );
1313 vlc_object_unlock( p_input );
1316 /*****************************************************************************
1317 * End: end the input thread
1318 *****************************************************************************/
1319 static void End( input_thread_t * p_input )
1323 /* We are at the end */
1324 input_ChangeState( p_input, END_S );
1326 /* Clean control variables */
1327 input_ControlVarStop( p_input );
1329 /* Clean up master */
1330 InputSourceClean( &p_input->p->input );
1333 for( i = 0; i < p_input->p->i_slave; i++ )
1335 InputSourceClean( p_input->p->slave[i] );
1336 free( p_input->p->slave[i] );
1338 free( p_input->p->slave );
1340 /* Unload all modules */
1341 if( p_input->p->p_es_out )
1342 es_out_Delete( p_input->p->p_es_out );
1343 if( p_input->p->p_es_out_display )
1344 es_out_Delete( p_input->p->p_es_out_display );
1346 if( !p_input->b_preparsing )
1348 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1349 if( libvlc_stats( p_input ) )
1351 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
1353 /* make sure we are up to date */
1354 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1355 if( p_private->p_stats_computer == p_input )
1357 stats_ComputeGlobalStats( p_input->p_libvlc,
1358 p_input->p_libvlc->p_stats );
1359 p_private->p_stats_computer = NULL;
1361 CL_CO( read_bytes );
1362 CL_CO( read_packets );
1363 CL_CO( demux_read );
1364 CL_CO( input_bitrate );
1365 CL_CO( demux_bitrate );
1366 CL_CO( played_abuffers );
1367 CL_CO( lost_abuffers );
1368 CL_CO( displayed_pictures );
1369 CL_CO( lost_pictures );
1370 CL_CO( decoded_audio) ;
1371 CL_CO( decoded_video );
1372 CL_CO( decoded_sub) ;
1375 /* Close optional stream output instance */
1376 if( p_input->p->p_sout )
1378 CL_CO( sout_sent_packets );
1379 CL_CO( sout_sent_bytes );
1380 CL_CO( sout_send_bitrate );
1382 vlc_object_detach( p_input->p->p_sout );
1387 if( p_input->p->i_attachment > 0 )
1389 for( i = 0; i < p_input->p->i_attachment; i++ )
1390 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1391 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1394 /* Tell we're dead */
1395 p_input->b_dead = true;
1398 /*****************************************************************************
1400 *****************************************************************************/
1401 static inline int ControlPopNoLock( input_thread_t *p_input,
1402 int *pi_type, vlc_value_t *p_val,
1403 mtime_t i_deadline )
1406 while( p_input->p->i_control <= 0 )
1408 if( !vlc_object_alive( p_input ) )
1409 return VLC_EGENERIC;
1411 if( i_deadline < 0 )
1412 return VLC_EGENERIC;
1414 if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1415 return VLC_EGENERIC;
1418 *pi_type = p_input->p->control[0].i_type;
1419 *p_val = p_input->p->control[0].val;
1421 p_input->p->i_control--;
1422 if( p_input->p->i_control > 0 )
1426 for( i = 0; i < p_input->p->i_control; i++ )
1428 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1429 p_input->p->control[i].val = p_input->p->control[i+1].val;
1436 static void ControlReduce( input_thread_t *p_input )
1443 for( i = 1; i < p_input->p->i_control; i++ )
1445 const int i_lt = p_input->p->control[i-1].i_type;
1446 const int i_ct = p_input->p->control[i].i_type;
1448 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1449 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1453 ( i_ct == INPUT_CONTROL_SET_STATE ||
1454 i_ct == INPUT_CONTROL_SET_RATE ||
1455 i_ct == INPUT_CONTROL_SET_POSITION ||
1456 i_ct == INPUT_CONTROL_SET_TIME ||
1457 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1458 i_ct == INPUT_CONTROL_SET_TITLE ||
1459 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1460 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1463 // msg_Dbg( p_input, "merged at %d", i );
1464 /* Remove the i-1 */
1465 for( j = i; j < p_input->p->i_control; j++ )
1466 p_input->p->control[j-1] = p_input->p->control[j];
1467 p_input->p->i_control--;
1471 /* TODO but that's not that important
1472 - merge SET_X with SET_X_CMD
1473 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1474 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1481 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1483 int i_ret = VLC_SUCCESS;
1484 int i_state = PAUSE_S;
1486 if( p_input->p->b_can_pause )
1488 if( p_input->p->input.p_access )
1489 i_ret = access_Control( p_input->p->input.p_access,
1490 ACCESS_SET_PAUSE_STATE, true );
1492 i_ret = demux_Control( p_input->p->input.p_demux,
1493 DEMUX_SET_PAUSE_STATE, true );
1497 msg_Warn( p_input, "cannot set pause state" );
1498 i_state = p_input->i_state;
1505 i_ret = es_out_SetPauseState( p_input->p->p_es_out, p_input->p->b_can_pause, true, i_control_date );
1508 msg_Warn( p_input, "cannot set pause state at es_out level" );
1509 i_state = p_input->i_state;
1513 /* Switch to new state */
1514 input_ChangeState( p_input, i_state );
1518 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1520 int i_ret = VLC_SUCCESS;
1522 if( p_input->p->b_can_pause )
1524 if( p_input->p->input.p_access )
1525 i_ret = access_Control( p_input->p->input.p_access,
1526 ACCESS_SET_PAUSE_STATE, false );
1528 i_ret = demux_Control( p_input->p->input.p_demux,
1529 DEMUX_SET_PAUSE_STATE, false );
1532 /* FIXME What to do ? */
1533 msg_Warn( p_input, "cannot unset pause -> EOF" );
1534 vlc_mutex_unlock( &p_input->p->lock_control );
1535 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1536 vlc_mutex_lock( &p_input->p->lock_control );
1540 /* Switch to play */
1541 input_ChangeState( p_input, PLAYING_S );
1545 es_out_SetPauseState( p_input->p->p_es_out, false, false, i_control_date );
1548 static bool Control( input_thread_t *p_input, int i_type,
1551 const mtime_t i_control_date = mdate();
1552 /* FIXME b_force_update is abused, it should be carefully checked */
1553 bool b_force_update = false;
1556 return b_force_update;
1560 case INPUT_CONTROL_SET_DIE:
1561 msg_Dbg( p_input, "control: stopping input" );
1563 /* Mark all submodules to die */
1564 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1567 case INPUT_CONTROL_SET_POSITION:
1568 case INPUT_CONTROL_SET_POSITION_OFFSET:
1572 if( p_input->p->b_recording )
1574 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1577 if( i_type == INPUT_CONTROL_SET_POSITION )
1579 f_pos = val.f_float;
1583 /* Should not fail */
1584 demux_Control( p_input->p->input.p_demux,
1585 DEMUX_GET_POSITION, &f_pos );
1586 f_pos += val.f_float;
1588 if( f_pos < 0.0 ) f_pos = 0.0;
1589 if( f_pos > 1.0 ) f_pos = 1.0;
1590 /* Reset the decoders states and clock sync (before calling the demuxer */
1591 es_out_SetTime( p_input->p->p_es_out, -1 );
1592 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1595 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1596 "%2.1f%% failed", f_pos * 100 );
1600 if( p_input->p->i_slave > 0 )
1601 SlaveSeek( p_input );
1602 p_input->p->input.b_eof = false;
1604 b_force_update = true;
1609 case INPUT_CONTROL_SET_TIME:
1610 case INPUT_CONTROL_SET_TIME_OFFSET:
1615 if( p_input->p->b_recording )
1617 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1621 if( i_type == INPUT_CONTROL_SET_TIME )
1623 i_time = val.i_time;
1627 /* Should not fail */
1628 demux_Control( p_input->p->input.p_demux,
1629 DEMUX_GET_TIME, &i_time );
1630 i_time += val.i_time;
1632 if( i_time < 0 ) i_time = 0;
1634 /* Reset the decoders states and clock sync (before calling the demuxer */
1635 es_out_SetTime( p_input->p->p_es_out, -1 );
1637 i_ret = demux_Control( p_input->p->input.p_demux,
1638 DEMUX_SET_TIME, i_time );
1643 /* Emulate it with a SET_POS */
1644 demux_Control( p_input->p->input.p_demux,
1645 DEMUX_GET_LENGTH, &i_length );
1648 double f_pos = (double)i_time / (double)i_length;
1649 i_ret = demux_Control( p_input->p->input.p_demux,
1650 DEMUX_SET_POSITION, f_pos );
1655 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1656 " failed or not possible", i_time );
1660 if( p_input->p->i_slave > 0 )
1661 SlaveSeek( p_input );
1662 p_input->p->input.b_eof = false;
1664 b_force_update = true;
1669 case INPUT_CONTROL_SET_STATE:
1670 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1671 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1673 ControlUnpause( p_input, i_control_date );
1675 b_force_update = true;
1677 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S /* &&
1678 p_input->p->b_can_pause */ )
1680 ControlPause( p_input, i_control_date );
1682 b_force_update = true;
1684 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause && 0 )
1686 b_force_update = true;
1688 /* Correct "state" value */
1689 input_ChangeState( p_input, p_input->i_state );
1691 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1693 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1697 case INPUT_CONTROL_SET_RATE:
1698 case INPUT_CONTROL_SET_RATE_SLOWER:
1699 case INPUT_CONTROL_SET_RATE_FASTER:
1704 /* Get rate and direction */
1705 if( i_type == INPUT_CONTROL_SET_RATE )
1707 i_rate = abs( val.i_int );
1708 i_rate_sign = val.i_int < 0 ? -1 : 1;
1712 static const int ppi_factor[][2] = {
1713 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1715 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1722 i_rate_sign = p_input->p->i_rate < 0 ? -1 : 1;
1726 for( i = 0; ppi_factor[i][0] != 0; i++ )
1728 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1729 const int i_test_e = abs( abs( p_input->p->i_rate ) - i_test_r );
1730 if( i_test_e < i_error )
1737 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1739 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1741 if( ppi_factor[i_idx+1][0] > 0 )
1742 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1744 i_rate = INPUT_RATE_MAX+1;
1748 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1750 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1752 i_rate = INPUT_RATE_MIN-1;
1756 /* Check rate bound */
1757 if( i_rate < INPUT_RATE_MIN )
1759 msg_Dbg( p_input, "cannot set rate faster" );
1760 i_rate = INPUT_RATE_MIN;
1762 else if( i_rate > INPUT_RATE_MAX )
1764 msg_Dbg( p_input, "cannot set rate slower" );
1765 i_rate = INPUT_RATE_MAX;
1768 /* Apply direction */
1769 if( i_rate_sign < 0 )
1771 if( p_input->p->input.b_rescale_ts )
1773 msg_Dbg( p_input, "cannot set negative rate" );
1774 i_rate = p_input->p->i_rate;
1775 assert( i_rate > 0 );
1779 i_rate *= i_rate_sign;
1783 if( i_rate != INPUT_RATE_DEFAULT &&
1784 ( ( !p_input->p->b_can_rate_control && !p_input->p->input.b_rescale_ts ) ||
1785 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1787 msg_Dbg( p_input, "cannot change rate" );
1788 i_rate = INPUT_RATE_DEFAULT;
1790 if( i_rate != p_input->p->i_rate &&
1791 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1794 if( p_input->p->input.p_access )
1796 i_ret = VLC_EGENERIC;
1800 if( !p_input->p->input.b_rescale_ts )
1801 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1803 i_ret = demux_Control( p_input->p->input.p_demux,
1804 DEMUX_SET_RATE, &i_rate );
1808 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1809 i_rate = p_input->p->i_rate;
1814 if( i_rate != p_input->p->i_rate )
1816 p_input->p->i_rate = i_rate;
1817 input_SendEventRate( p_input, i_rate );
1819 if( p_input->p->input.b_rescale_ts )
1821 const int i_rate_source = (p_input->b_can_pace_control || p_input->p->b_can_rate_control ) ? i_rate : INPUT_RATE_DEFAULT;
1822 es_out_SetRate( p_input->p->p_es_out, i_rate_source, i_rate );
1825 b_force_update = true;
1830 case INPUT_CONTROL_SET_PROGRAM:
1831 /* No need to force update, es_out does it if needed */
1832 es_out_Control( p_input->p->p_es_out,
1833 ES_OUT_SET_GROUP, val.i_int );
1835 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1839 case INPUT_CONTROL_SET_ES:
1840 /* No need to force update, es_out does it if needed */
1841 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, val.i_int );
1844 case INPUT_CONTROL_RESTART_ES:
1845 es_out_Control( p_input->p->p_es_out_display, ES_OUT_RESTART_ES_BY_ID, val.i_int );
1848 case INPUT_CONTROL_SET_AUDIO_DELAY:
1849 if( !es_out_SetDelay( p_input->p->p_es_out_display, AUDIO_ES, val.i_time ) )
1850 input_SendEventAudioDelay( p_input, val.i_time );
1853 case INPUT_CONTROL_SET_SPU_DELAY:
1854 if( !es_out_SetDelay( p_input->p->p_es_out_display, SPU_ES, val.i_time ) )
1855 input_SendEventSubtitleDelay( p_input, val.i_time );
1858 case INPUT_CONTROL_SET_TITLE:
1859 case INPUT_CONTROL_SET_TITLE_NEXT:
1860 case INPUT_CONTROL_SET_TITLE_PREV:
1861 if( p_input->p->b_recording )
1863 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1866 if( p_input->p->input.b_title_demux &&
1867 p_input->p->input.i_title > 0 )
1870 /* FIXME handle demux title */
1871 demux_t *p_demux = p_input->p->input.p_demux;
1874 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1875 i_title = p_demux->info.i_title - 1;
1876 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1877 i_title = p_demux->info.i_title + 1;
1879 i_title = val.i_int;
1881 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1883 es_out_SetTime( p_input->p->p_es_out, -1 );
1885 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1886 input_ControlVarTitle( p_input, i_title );
1889 else if( p_input->p->input.i_title > 0 )
1891 access_t *p_access = p_input->p->input.p_access;
1894 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1895 i_title = p_access->info.i_title - 1;
1896 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1897 i_title = p_access->info.i_title + 1;
1899 i_title = val.i_int;
1901 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1903 es_out_SetTime( p_input->p->p_es_out, -1 );
1905 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1906 stream_AccessReset( p_input->p->input.p_stream );
1910 case INPUT_CONTROL_SET_SEEKPOINT:
1911 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1912 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1913 if( p_input->p->b_recording )
1915 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1919 if( p_input->p->input.b_title_demux &&
1920 p_input->p->input.i_title > 0 )
1922 demux_t *p_demux = p_input->p->input.p_demux;
1924 int64_t i_input_time;
1925 int64_t i_seekpoint_time;
1927 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1929 i_seekpoint = p_demux->info.i_seekpoint;
1930 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1931 if( i_seekpoint_time >= 0 &&
1932 !demux_Control( p_demux,
1933 DEMUX_GET_TIME, &i_input_time ) )
1935 if( i_input_time < i_seekpoint_time + 3000000 )
1941 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1942 i_seekpoint = p_demux->info.i_seekpoint + 1;
1944 i_seekpoint = val.i_int;
1946 if( i_seekpoint >= 0 && i_seekpoint <
1947 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1950 es_out_SetTime( p_input->p->p_es_out, -1 );
1952 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1955 else if( p_input->p->input.i_title > 0 )
1957 demux_t *p_demux = p_input->p->input.p_demux;
1958 access_t *p_access = p_input->p->input.p_access;
1960 int64_t i_input_time;
1961 int64_t i_seekpoint_time;
1963 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1965 i_seekpoint = p_access->info.i_seekpoint;
1966 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1967 if( i_seekpoint_time >= 0 &&
1968 demux_Control( p_demux,
1969 DEMUX_GET_TIME, &i_input_time ) )
1971 if( i_input_time < i_seekpoint_time + 3000000 )
1977 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1978 i_seekpoint = p_access->info.i_seekpoint + 1;
1980 i_seekpoint = val.i_int;
1982 if( i_seekpoint >= 0 && i_seekpoint <
1983 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1985 es_out_SetTime( p_input->p->p_es_out, -1 );
1987 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1989 stream_AccessReset( p_input->p->input.p_stream );
1994 case INPUT_CONTROL_ADD_SUBTITLE:
1995 if( val.psz_string )
1997 SubtitleAdd( p_input, val.psz_string, true );
1998 free( val.psz_string );
2002 case INPUT_CONTROL_ADD_SLAVE:
2003 if( val.psz_string )
2005 input_source_t *slave = InputSourceNew( p_input );
2007 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
2013 msg_Dbg( p_input, "adding %s as slave on the fly",
2017 if( demux_Control( p_input->p->input.p_demux,
2018 DEMUX_GET_TIME, &i_time ) )
2020 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2021 InputSourceClean( slave );
2025 if( demux_Control( slave->p_demux,
2026 DEMUX_SET_TIME, i_time ) )
2028 msg_Err( p_input, "seek failed for new slave" );
2029 InputSourceClean( slave );
2034 /* Get meta (access and demux) */
2035 p_meta = vlc_meta_New();
2036 access_Control( slave->p_access, ACCESS_GET_META,
2038 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
2039 InputUpdateMeta( p_input, p_meta );
2041 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2046 msg_Warn( p_input, "failed to add %s as slave",
2050 free( val.psz_string );
2054 case INPUT_CONTROL_SET_RECORD_STATE:
2055 if( !!p_input->p->b_recording != !!val.b_bool )
2057 if( p_input->p->input.b_can_stream_record )
2059 if( demux_Control( p_input->p->input.p_demux,
2060 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2065 if( es_out_SetRecordState( p_input->p->p_es_out_display, val.b_bool ) )
2068 p_input->p->b_recording = val.b_bool;
2070 input_SendEventRecord( p_input, val.b_bool );
2072 b_force_update = true;
2076 case INPUT_CONTROL_SET_FRAME_NEXT:
2077 if( p_input->i_state == PAUSE_S )
2079 es_out_SetFrameNext( p_input->p->p_es_out );
2081 else if( p_input->i_state == PLAYING_S )
2083 ControlPause( p_input, i_control_date );
2087 msg_Err( p_input, "invalid state for frame next" );
2089 b_force_update = true;
2092 case INPUT_CONTROL_SET_BOOKMARK:
2094 msg_Err( p_input, "not yet implemented" );
2098 return b_force_update;
2101 /*****************************************************************************
2103 *****************************************************************************/
2104 static int UpdateTitleSeekpoint( input_thread_t *p_input,
2105 int i_title, int i_seekpoint )
2107 int i_title_end = p_input->p->input.i_title_end -
2108 p_input->p->input.i_title_offset;
2109 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2110 p_input->p->input.i_seekpoint_offset;
2112 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2114 if( i_title > i_title_end ||
2115 ( i_title == i_title_end && i_seekpoint > i_seekpoint_end ) )
2118 else if( i_seekpoint_end >= 0 )
2120 if( i_seekpoint > i_seekpoint_end )
2123 else if( i_title_end >= 0 )
2125 if( i_title > i_title_end )
2130 static int UpdateFromDemux( input_thread_t *p_input )
2132 demux_t *p_demux = p_input->p->input.p_demux;
2134 /* TODO event-like */
2135 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2137 input_SendEventTitle( p_input, p_demux->info.i_title );
2139 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2141 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2143 input_SendEventSeekpoint( p_input,
2144 p_demux->info.i_title, p_demux->info.i_seekpoint );
2146 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2148 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2150 /* Hmmm only works with master input */
2151 if( p_input->p->input.p_demux == p_demux )
2152 return UpdateTitleSeekpoint( p_input,
2153 p_demux->info.i_title,
2154 p_demux->info.i_seekpoint );
2158 /*****************************************************************************
2160 *****************************************************************************/
2161 static int UpdateFromAccess( input_thread_t *p_input )
2163 access_t *p_access = p_input->p->input.p_access;
2165 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2167 input_SendEventTitle( p_input, p_access->info.i_title );
2169 stream_AccessUpdate( p_input->p->input.p_stream );
2171 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2173 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2175 input_SendEventSeekpoint( p_input,
2176 p_access->info.i_title, p_access->info.i_seekpoint );
2178 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2180 if( p_access->info.i_update & INPUT_UPDATE_META )
2182 /* TODO maybe multi - access ? */
2183 vlc_meta_t *p_meta = vlc_meta_New();
2184 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2185 InputUpdateMeta( p_input, p_meta );
2186 p_access->info.i_update &= ~INPUT_UPDATE_META;
2188 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2193 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2194 f_quality = f_strength = -1;
2196 input_SendEventSignal( p_input, f_quality, f_strength );
2198 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2201 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2203 /* Hmmm only works with master input */
2204 if( p_input->p->input.p_access == p_access )
2205 return UpdateTitleSeekpoint( p_input,
2206 p_access->info.i_title,
2207 p_access->info.i_seekpoint );
2211 /*****************************************************************************
2213 *****************************************************************************/
2214 static input_source_t *InputSourceNew( input_thread_t *p_input )
2216 VLC_UNUSED(p_input);
2217 input_source_t *in = malloc( sizeof( input_source_t ) );
2219 memset( in, 0, sizeof( input_source_t ) );
2223 /*****************************************************************************
2225 *****************************************************************************/
2226 static int InputSourceInit( input_thread_t *p_input,
2227 input_source_t *in, const char *psz_mrl,
2228 const char *psz_forced_demux )
2230 const bool b_master = in == &p_input->p->input;
2232 char psz_dup[strlen(psz_mrl) + 1];
2233 const char *psz_access;
2234 const char *psz_demux;
2241 strcpy( psz_dup, psz_mrl );
2243 if( !in ) return VLC_EGENERIC;
2244 if( !p_input ) return VLC_EGENERIC;
2247 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2249 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2250 psz_mrl, psz_access, psz_demux, psz_path );
2251 if( !p_input->b_preparsing )
2253 /* Hack to allow udp://@:port syntax */
2255 (strncmp( psz_access, "udp", 3 ) &&
2256 strncmp( psz_access, "rtp", 3 )) )
2258 /* Find optional titles and seekpoints */
2259 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2260 &in->i_seekpoint_start, &in->i_seekpoint_end );
2263 if( psz_forced_demux && *psz_forced_demux )
2265 psz_demux = psz_forced_demux;
2267 else if( *psz_demux == '\0' )
2269 /* special hack for forcing a demuxer with --demux=module
2270 * (and do nothing with a list) */
2271 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2273 if( psz_var_demux != NULL &&
2274 !strchr(psz_var_demux, ',' ) &&
2275 !strchr(psz_var_demux, ':' ) )
2277 psz_demux = psz_var_demux;
2279 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2283 /* Try access_demux first */
2284 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2285 NULL, p_input->p->p_es_out, false );
2289 /* Preparsing is only for file:// */
2292 if( !*psz_access ) /* path without scheme:// */
2293 psz_access = "file";
2294 if( strcmp( psz_access, "file" ) )
2296 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2301 int64_t i_pts_delay;
2303 /* Get infos from access_demux */
2304 demux_Control( in->p_demux,
2305 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2306 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2308 in->b_title_demux = true;
2309 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2310 &in->title, &in->i_title,
2311 &in->i_title_offset, &in->i_seekpoint_offset ) )
2313 TAB_INIT( in->i_title, in->title );
2315 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2316 &in->b_can_pace_control ) )
2317 in->b_can_pace_control = false;
2319 if( !in->b_can_pace_control )
2321 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2322 &in->b_can_rate_control, &in->b_rescale_ts ) )
2324 in->b_can_rate_control = false;
2325 in->b_rescale_ts = true; /* not used */
2330 in->b_can_rate_control = true;
2331 in->b_rescale_ts = true;
2333 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2334 &in->b_can_pause ) )
2335 in->b_can_pause = false;
2336 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2337 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2338 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2340 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2342 if( ret != VLC_SUCCESS )
2344 var_Set( p_input, "seekable", val );
2348 int64_t i_pts_delay;
2351 input_ChangeState( p_input, OPENING_S );
2353 /* Now try a real access */
2354 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2356 /* Access failed, URL encoded ? */
2357 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2359 decode_URI( psz_path );
2361 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2362 psz_access, psz_demux, psz_path );
2364 in->p_access = access_New( p_input,
2365 psz_access, psz_demux, psz_path );
2367 if( in->p_access == NULL )
2369 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2371 intf_UserFatal( VLC_OBJECT( p_input), false,
2372 _("Your input can't be opened"),
2373 _("VLC is unable to open the MRL '%s'."
2374 " Check the log for details."), psz_mrl );
2379 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2380 while( psz && *psz )
2382 access_t *p_access = in->p_access;
2383 char *end = strchr( psz, ':' );
2388 in->p_access = access_FilterNew( in->p_access, psz );
2389 if( in->p_access == NULL )
2391 in->p_access = p_access;
2392 msg_Warn( p_input, "failed to insert access filter %s",
2400 /* Get infos from access */
2401 if( !p_input->b_preparsing )
2403 access_Control( in->p_access,
2404 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2405 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2407 in->b_title_demux = false;
2408 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2409 &in->title, &in->i_title,
2410 &in->i_title_offset, &in->i_seekpoint_offset ) )
2413 TAB_INIT( in->i_title, in->title );
2415 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2416 &in->b_can_pace_control );
2417 in->b_can_rate_control = in->b_can_pace_control;
2418 in->b_rescale_ts = true;
2420 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2422 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2423 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2424 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2426 access_Control( in->p_access, ACCESS_CAN_SEEK,
2428 var_Set( p_input, "seekable", val );
2432 input_ChangeState( p_input, BUFFERING_S );
2434 /* Autodetect extra files if none specified */
2435 char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2436 if( !psz_input_list )
2438 char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2439 if( psz_extra_files )
2440 var_SetString( p_input, "input-list", psz_extra_files );
2441 free( psz_extra_files );
2444 /* Create the stream_t */
2445 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2447 /* Restor old value */
2448 if( !psz_input_list )
2449 var_SetString( p_input, "input-list", "" );
2450 free( psz_input_list );
2452 if( in->p_stream == NULL )
2454 msg_Warn( p_input, "cannot create a stream_t from access" );
2458 /* Open a demuxer */
2459 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2461 psz_demux = in->p_access->psz_demux;
2465 /* Take access redirections into account */
2466 char *psz_real_path;
2467 char *psz_buf = NULL;
2468 if( in->p_access->psz_path )
2470 const char *psz_a, *psz_d;
2471 psz_buf = strdup( in->p_access->psz_path );
2472 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2476 psz_real_path = psz_path;
2478 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2480 in->p_stream, p_input->p->p_es_out,
2481 p_input->b_preparsing );
2485 if( in->p_demux == NULL )
2487 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2488 psz_access, psz_demux, psz_path );
2489 intf_UserFatal( VLC_OBJECT( p_input ), false,
2490 _("VLC can't recognize the input's format"),
2491 _("The format of '%s' cannot be detected. "
2492 "Have a look at the log for details."), psz_mrl );
2496 /* Get title from demux */
2497 if( !p_input->b_preparsing && in->i_title <= 0 )
2499 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2500 &in->title, &in->i_title,
2501 &in->i_title_offset, &in->i_seekpoint_offset ))
2503 TAB_INIT( in->i_title, in->title );
2507 in->b_title_demux = true;
2512 /* Set record capabilities */
2513 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2514 in->b_can_stream_record = false;
2516 if( !var_CreateGetBool( p_input, "input-record-native" ) )
2517 in->b_can_stream_record = false;
2518 var_SetBool( p_input, "can-record", true );
2520 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2524 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2525 if( 1 || !p_input->b_preparsing )
2528 input_attachment_t **attachment;
2529 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2530 &attachment, &i_attachment ) )
2532 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2533 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2534 i_attachment, attachment );
2535 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2538 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2540 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2542 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2545 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2546 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2552 input_ChangeState( p_input, ERROR_S );
2555 demux_Delete( in->p_demux );
2558 stream_Delete( in->p_stream );
2561 access_Delete( in->p_access );
2563 return VLC_EGENERIC;
2566 /*****************************************************************************
2568 *****************************************************************************/
2569 static void InputSourceClean( input_source_t *in )
2574 demux_Delete( in->p_demux );
2577 stream_Delete( in->p_stream );
2580 access_Delete( in->p_access );
2582 if( in->i_title > 0 )
2584 for( i = 0; i < in->i_title; i++ )
2585 vlc_input_title_Delete( in->title[i] );
2586 TAB_CLEAN( in->i_title, in->title );
2590 static void SlaveDemux( input_thread_t *p_input )
2594 bool b_set_time = true;
2596 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2598 /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2602 for( i = 0; i < p_input->p->i_slave; i++ )
2604 input_source_t *in = p_input->p->slave[i];
2610 if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2615 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2617 msg_Err( p_input, "slave[%d] doesn't like "
2618 "DEMUX_GET_TIME -> EOF", i );
2623 if( i_stime >= i_time )
2626 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2632 i_ret = in->p_demux->pf_demux( in->p_demux );
2637 msg_Dbg( p_input, "slave %d EOF", i );
2643 static void SlaveSeek( input_thread_t *p_input )
2648 if( !p_input ) return;
2650 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2652 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2656 for( i = 0; i < p_input->p->i_slave; i++ )
2658 input_source_t *in = p_input->p->slave[i];
2660 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2663 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2673 /*****************************************************************************
2675 *****************************************************************************/
2676 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2680 if( !p_meta ) return;
2682 /* Get meta information from user */
2683 #define GET_META( field, s ) \
2684 var_Get( p_input, (s), &val ); \
2685 if( *val.psz_string ) \
2686 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2687 free( val.psz_string )
2689 GET_META( Title, "meta-title" );
2690 GET_META( Artist, "meta-artist" );
2691 GET_META( Genre, "meta-genre" );
2692 GET_META( Copyright, "meta-copyright" );
2693 GET_META( Description, "meta-description" );
2694 GET_META( Date, "meta-date" );
2695 GET_META( URL, "meta-url" );
2699 /*****************************************************************************
2700 * InputGetExtraFiles
2701 * Autodetect extra input list
2702 *****************************************************************************/
2703 static char *InputGetExtraFiles( input_thread_t *p_input,
2704 const char *psz_access, const char *psz_path )
2706 char *psz_list = NULL;
2708 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2712 const char *psz_ext = strrchr( psz_path, '.' );
2713 if( !psz_ext || strcmp( psz_ext, ".001" ) )
2716 char *psz_file = strdup( psz_path );
2720 /* Try to list .xyz files */
2721 for( int i = 2; i < 999; i++ )
2723 char *psz_ext = strrchr( psz_file, '.' );
2726 snprintf( psz_ext, 5, ".%.3d", i );
2728 if( utf8_stat( psz_file, &st )
2729 || !S_ISREG( st.st_mode ) || !st.st_size )
2732 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2736 char *psz_old = psz_list;
2737 /* FIXME how to handle file with ',' ?*/
2738 if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2746 psz_list = strdup( psz_file );
2754 /*****************************************************************************
2755 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2756 * arturl and locking issue.
2757 *****************************************************************************/
2758 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2760 input_item_t *p_item = p_input->p->input.p_item;
2761 char * psz_arturl = NULL;
2762 char *psz_title = NULL;
2767 psz_arturl = input_item_GetArtURL( p_item );
2769 vlc_mutex_lock( &p_item->lock );
2771 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2772 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2774 vlc_meta_Merge( p_item->p_meta, p_meta );
2776 vlc_meta_Delete( p_meta );
2778 if( psz_arturl && *psz_arturl )
2780 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2782 if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2784 /* Don't look for art cover if sout
2785 * XXX It can change when sout has meta data support */
2786 if( p_input->p->p_sout && !p_input->b_preparsing )
2787 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2789 input_ExtractAttachmentAndCacheArt( p_input );
2796 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2798 p_meta = vlc_meta_New();
2799 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2803 input_item_SetName( p_item, psz_title );
2806 vlc_mutex_unlock( &p_item->lock );
2808 input_item_SetPreparsed( p_item, true );
2810 input_SendEventMeta( p_input );
2812 /** \todo handle sout meta */
2816 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2817 int i_new, input_attachment_t **pp_new )
2819 int i_attachment = *pi_attachment;
2820 input_attachment_t **attachment = *ppp_attachment;
2823 attachment = realloc( attachment,
2824 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2825 for( i = 0; i < i_new; i++ )
2826 attachment[i_attachment++] = pp_new[i];
2830 *pi_attachment = i_attachment;
2831 *ppp_attachment = attachment;
2834 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2838 if( p_input->b_preparsing )
2841 if( p_input->p->input.p_access )
2842 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2845 /* Get meta data from slave input */
2846 for( i = 0; i < p_input->p->i_slave; i++ )
2848 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2849 if( p_input->p->slave[i]->p_access )
2851 access_Control( p_input->p->slave[i]->p_access,
2852 ACCESS_GET_META, p_meta );
2857 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2863 /* XXX I am not sure it is a great idea, besides, there is more than that
2864 * if we want to do it right */
2865 vlc_mutex_lock( &p_item->lock );
2866 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2868 vlc_mutex_unlock( &p_item->lock );
2871 vlc_mutex_unlock( &p_item->lock );
2874 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2875 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2880 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2881 if(! p_demux->p_private )
2884 p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2887 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2889 if( p_demux_meta->p_meta )
2891 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2892 vlc_meta_Delete( p_demux_meta->p_meta );
2895 if( p_demux_meta->i_attachments > 0 )
2897 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2898 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2899 p_demux_meta->i_attachments, p_demux_meta->attachments );
2900 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2902 module_unneed( p_demux, p_id3 );
2904 free( p_demux->p_private );
2907 static void input_ChangeState( input_thread_t *p_input, int i_state )
2909 const bool b_changed = p_input->i_state != i_state;
2911 p_input->i_state = i_state;
2912 if( i_state == ERROR_S )
2913 p_input->b_error = true;
2914 else if( i_state == END_S )
2915 p_input->b_eof = true;
2917 input_item_SetHasErrorWhenReading( p_input->p->input.p_item, (i_state == ERROR_S) );
2920 input_SendEventState( p_input, i_state );
2924 /*****************************************************************************
2925 * MRLSplit: parse the access, demux and url part of the
2926 * Media Resource Locator.
2927 *****************************************************************************/
2928 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2931 char *psz_access = NULL;
2932 char *psz_demux = NULL;
2935 /* Either there is an access/demux specification before ://
2936 * or we have a plain local file path. */
2937 psz_path = strstr( psz_dup, "://" );
2938 if( psz_path != NULL )
2941 psz_path += 3; /* skips "://" */
2943 /* Separate access from demux (<access>/<demux>://<path>) */
2944 psz_access = psz_dup;
2945 psz_demux = strchr( psz_access, '/' );
2947 *psz_demux++ = '\0';
2949 /* We really don't want module name substitution here! */
2950 if( psz_access[0] == '$' )
2952 if( psz_demux && psz_demux[0] == '$' )
2959 *ppsz_access = psz_access ? psz_access : (char*)"";
2960 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2961 *ppsz_path = psz_path;
2964 static inline bool next(char ** src)
2968 long result = strtol( *src, &end, 0 );
2969 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
2978 /*****************************************************************************
2979 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2982 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2983 *****************************************************************************/
2984 static void MRLSections( input_thread_t *p_input, char *psz_source,
2985 int *pi_title_start, int *pi_title_end,
2986 int *pi_chapter_start, int *pi_chapter_end )
2988 char *psz, *psz_end, *psz_next, *psz_check;
2990 *pi_title_start = *pi_title_end = -1;
2991 *pi_chapter_start = *pi_chapter_end = -1;
2993 /* Start by parsing titles and chapters */
2994 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2997 /* Check we are really dealing with a title/chapter section */
2998 psz_check = psz + 1;
2999 if( !*psz_check ) return;
3000 if( isdigit(*psz_check) )
3001 if(!next(&psz_check)) return;
3002 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
3003 if( *psz_check == ':' && ++psz_check )
3005 if( isdigit(*psz_check) )
3006 if(!next(&psz_check)) return;
3008 if( *psz_check != '-' && *psz_check ) return;
3009 if( *psz_check == '-' && ++psz_check )
3011 if( isdigit(*psz_check) )
3012 if(!next(&psz_check)) return;
3014 if( *psz_check != ':' && *psz_check ) return;
3015 if( *psz_check == ':' && ++psz_check )
3017 if( isdigit(*psz_check) )
3018 if(!next(&psz_check)) return;
3020 if( *psz_check ) return;
3022 /* Separate start and end */
3024 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
3026 /* Look for the start title */
3027 *pi_title_start = strtol( psz, &psz_next, 0 );
3028 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
3029 *pi_title_end = *pi_title_start;
3032 /* Look for the start chapter */
3034 *pi_chapter_start = strtol( psz, &psz_next, 0 );
3035 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
3036 *pi_chapter_end = *pi_chapter_start;
3040 /* Look for the end title */
3041 *pi_title_end = strtol( psz_end, &psz_next, 0 );
3042 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3045 /* Look for the end chapter */
3046 if( *psz_end ) psz_end++;
3047 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3048 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3051 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3052 psz_source, *pi_title_start, *pi_chapter_start,
3053 *pi_title_end, *pi_chapter_end );
3056 /*****************************************************************************
3057 * input_AddSubtitles: add a subtitles file and enable it
3058 *****************************************************************************/
3059 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3061 input_source_t *sub;
3064 char *psz_path, *psz_extension;
3066 /* if we are provided a subtitle.sub file,
3067 * see if we don't have a subtitle.idx and use it instead */
3068 psz_path = strdup( psz_subtitle );
3071 psz_extension = strrchr( psz_path, '.');
3072 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3076 strcpy( psz_extension, ".idx" );
3078 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3080 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3081 psz_path, psz_subtitle );
3082 strcpy( psz_subtitle, psz_path );
3088 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3090 sub = InputSourceNew( p_input );
3091 if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3096 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3099 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3101 if( count.i_int == 0 )
3103 /* if it was first one, there is disable too */
3105 if( count.i_int < list.p_list->i_count )
3107 const int i_id = list.p_list->p_values[count.i_int].i_int;
3109 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3110 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3112 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3116 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3117 bool b_check_extension )
3121 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3124 assert( psz_subtitle != NULL );
3126 val.psz_string = strdup( psz_subtitle );
3127 if( val.psz_string )
3128 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3132 /*****************************************************************************
3133 * input_get_event_manager
3134 *****************************************************************************/
3135 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3137 return &p_input->p->event_manager;
3141 /* TODO FIXME nearly the same logic that snapshot code */
3142 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3147 path = utf8_opendir( psz_path );
3152 char *psz_tmp = str_format( p_obj, psz_prefix );
3156 filename_sanitize( psz_tmp );
3157 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3159 psz_extension ? "." : "",
3160 psz_extension ? psz_extension : "" ) < 0 )
3167 psz_file = str_format( p_obj, psz_path );
3168 path_sanitize( psz_file );