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 void input_ControlPush( input_thread_t *p_input,
1402 int i_type, vlc_value_t *p_val )
1404 vlc_mutex_lock( &p_input->p->lock_control );
1405 if( i_type == INPUT_CONTROL_SET_DIE )
1407 /* Special case, empty the control */
1408 p_input->p->i_control = 1;
1409 p_input->p->control[0].i_type = i_type;
1410 memset( &p_input->p->control[0].val, 0, sizeof( vlc_value_t ) );
1412 else if( p_input->p->i_control >= INPUT_CONTROL_FIFO_SIZE )
1414 msg_Err( p_input, "input control fifo overflow, trashing type=%d",
1419 p_input->p->control[p_input->p->i_control].i_type = i_type;
1421 p_input->p->control[p_input->p->i_control].val = *p_val;
1423 memset( &p_input->p->control[p_input->p->i_control].val, 0,
1424 sizeof( vlc_value_t ) );
1426 p_input->p->i_control++;
1428 vlc_cond_signal( &p_input->p->wait_control );
1429 vlc_mutex_unlock( &p_input->p->lock_control );
1432 static inline int ControlPopNoLock( input_thread_t *p_input,
1433 int *pi_type, vlc_value_t *p_val,
1434 mtime_t i_deadline )
1437 while( p_input->p->i_control <= 0 )
1439 if( !vlc_object_alive( p_input ) )
1440 return VLC_EGENERIC;
1442 if( i_deadline < 0 )
1443 return VLC_EGENERIC;
1445 if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1446 return VLC_EGENERIC;
1449 *pi_type = p_input->p->control[0].i_type;
1450 *p_val = p_input->p->control[0].val;
1452 p_input->p->i_control--;
1453 if( p_input->p->i_control > 0 )
1457 for( i = 0; i < p_input->p->i_control; i++ )
1459 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1460 p_input->p->control[i].val = p_input->p->control[i+1].val;
1467 static void ControlReduce( input_thread_t *p_input )
1474 for( i = 1; i < p_input->p->i_control; i++ )
1476 const int i_lt = p_input->p->control[i-1].i_type;
1477 const int i_ct = p_input->p->control[i].i_type;
1479 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1480 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1484 ( i_ct == INPUT_CONTROL_SET_STATE ||
1485 i_ct == INPUT_CONTROL_SET_RATE ||
1486 i_ct == INPUT_CONTROL_SET_POSITION ||
1487 i_ct == INPUT_CONTROL_SET_TIME ||
1488 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1489 i_ct == INPUT_CONTROL_SET_TITLE ||
1490 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1491 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1494 // msg_Dbg( p_input, "merged at %d", i );
1495 /* Remove the i-1 */
1496 for( j = i; j < p_input->p->i_control; j++ )
1497 p_input->p->control[j-1] = p_input->p->control[j];
1498 p_input->p->i_control--;
1502 /* TODO but that's not that important
1503 - merge SET_X with SET_X_CMD
1504 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1505 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1512 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1514 int i_ret = VLC_SUCCESS;
1515 int i_state = PAUSE_S;
1517 if( p_input->p->b_can_pause )
1519 if( p_input->p->input.p_access )
1520 i_ret = access_Control( p_input->p->input.p_access,
1521 ACCESS_SET_PAUSE_STATE, true );
1523 i_ret = demux_Control( p_input->p->input.p_demux,
1524 DEMUX_SET_PAUSE_STATE, true );
1528 msg_Warn( p_input, "cannot set pause state" );
1529 i_state = p_input->i_state;
1536 i_ret = es_out_SetPauseState( p_input->p->p_es_out, p_input->p->b_can_pause, true, i_control_date );
1539 msg_Warn( p_input, "cannot set pause state at es_out level" );
1540 i_state = p_input->i_state;
1544 /* Switch to new state */
1545 input_ChangeState( p_input, i_state );
1549 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1551 int i_ret = VLC_SUCCESS;
1553 if( p_input->p->b_can_pause )
1555 if( p_input->p->input.p_access )
1556 i_ret = access_Control( p_input->p->input.p_access,
1557 ACCESS_SET_PAUSE_STATE, false );
1559 i_ret = demux_Control( p_input->p->input.p_demux,
1560 DEMUX_SET_PAUSE_STATE, false );
1563 /* FIXME What to do ? */
1564 msg_Warn( p_input, "cannot unset pause -> EOF" );
1565 vlc_mutex_unlock( &p_input->p->lock_control );
1566 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1567 vlc_mutex_lock( &p_input->p->lock_control );
1571 /* Switch to play */
1572 input_ChangeState( p_input, PLAYING_S );
1576 es_out_SetPauseState( p_input->p->p_es_out, false, false, i_control_date );
1579 static bool Control( input_thread_t *p_input, int i_type,
1582 const mtime_t i_control_date = mdate();
1583 /* FIXME b_force_update is abused, it should be carefully checked */
1584 bool b_force_update = false;
1587 return b_force_update;
1591 case INPUT_CONTROL_SET_DIE:
1592 msg_Dbg( p_input, "control: stopping input" );
1594 /* Mark all submodules to die */
1595 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1598 case INPUT_CONTROL_SET_POSITION:
1599 case INPUT_CONTROL_SET_POSITION_OFFSET:
1603 if( p_input->p->b_recording )
1605 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1608 if( i_type == INPUT_CONTROL_SET_POSITION )
1610 f_pos = val.f_float;
1614 /* Should not fail */
1615 demux_Control( p_input->p->input.p_demux,
1616 DEMUX_GET_POSITION, &f_pos );
1617 f_pos += val.f_float;
1619 if( f_pos < 0.0 ) f_pos = 0.0;
1620 if( f_pos > 1.0 ) f_pos = 1.0;
1621 /* Reset the decoders states and clock sync (before calling the demuxer */
1622 es_out_SetTime( p_input->p->p_es_out, -1 );
1623 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1626 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1627 "%2.1f%% failed", f_pos * 100 );
1631 if( p_input->p->i_slave > 0 )
1632 SlaveSeek( p_input );
1633 p_input->p->input.b_eof = false;
1635 b_force_update = true;
1640 case INPUT_CONTROL_SET_TIME:
1641 case INPUT_CONTROL_SET_TIME_OFFSET:
1646 if( p_input->p->b_recording )
1648 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1652 if( i_type == INPUT_CONTROL_SET_TIME )
1654 i_time = val.i_time;
1658 /* Should not fail */
1659 demux_Control( p_input->p->input.p_demux,
1660 DEMUX_GET_TIME, &i_time );
1661 i_time += val.i_time;
1663 if( i_time < 0 ) i_time = 0;
1665 /* Reset the decoders states and clock sync (before calling the demuxer */
1666 es_out_SetTime( p_input->p->p_es_out, -1 );
1668 i_ret = demux_Control( p_input->p->input.p_demux,
1669 DEMUX_SET_TIME, i_time );
1674 /* Emulate it with a SET_POS */
1675 demux_Control( p_input->p->input.p_demux,
1676 DEMUX_GET_LENGTH, &i_length );
1679 double f_pos = (double)i_time / (double)i_length;
1680 i_ret = demux_Control( p_input->p->input.p_demux,
1681 DEMUX_SET_POSITION, f_pos );
1686 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1687 " failed or not possible", i_time );
1691 if( p_input->p->i_slave > 0 )
1692 SlaveSeek( p_input );
1693 p_input->p->input.b_eof = false;
1695 b_force_update = true;
1700 case INPUT_CONTROL_SET_STATE:
1701 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1702 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1704 ControlUnpause( p_input, i_control_date );
1706 b_force_update = true;
1708 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S /* &&
1709 p_input->p->b_can_pause */ )
1711 ControlPause( p_input, i_control_date );
1713 b_force_update = true;
1715 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause && 0 )
1717 b_force_update = true;
1719 /* Correct "state" value */
1720 input_ChangeState( p_input, p_input->i_state );
1722 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1724 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1728 case INPUT_CONTROL_SET_RATE:
1729 case INPUT_CONTROL_SET_RATE_SLOWER:
1730 case INPUT_CONTROL_SET_RATE_FASTER:
1735 /* Get rate and direction */
1736 if( i_type == INPUT_CONTROL_SET_RATE )
1738 i_rate = abs( val.i_int );
1739 i_rate_sign = val.i_int < 0 ? -1 : 1;
1743 static const int ppi_factor[][2] = {
1744 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1746 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1753 i_rate_sign = p_input->p->i_rate < 0 ? -1 : 1;
1757 for( i = 0; ppi_factor[i][0] != 0; i++ )
1759 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1760 const int i_test_e = abs( abs( p_input->p->i_rate ) - i_test_r );
1761 if( i_test_e < i_error )
1768 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1770 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1772 if( ppi_factor[i_idx+1][0] > 0 )
1773 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1775 i_rate = INPUT_RATE_MAX+1;
1779 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1781 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1783 i_rate = INPUT_RATE_MIN-1;
1787 /* Check rate bound */
1788 if( i_rate < INPUT_RATE_MIN )
1790 msg_Dbg( p_input, "cannot set rate faster" );
1791 i_rate = INPUT_RATE_MIN;
1793 else if( i_rate > INPUT_RATE_MAX )
1795 msg_Dbg( p_input, "cannot set rate slower" );
1796 i_rate = INPUT_RATE_MAX;
1799 /* Apply direction */
1800 if( i_rate_sign < 0 )
1802 if( p_input->p->input.b_rescale_ts )
1804 msg_Dbg( p_input, "cannot set negative rate" );
1805 i_rate = p_input->p->i_rate;
1806 assert( i_rate > 0 );
1810 i_rate *= i_rate_sign;
1814 if( i_rate != INPUT_RATE_DEFAULT &&
1815 ( ( !p_input->p->b_can_rate_control && !p_input->p->input.b_rescale_ts ) ||
1816 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1818 msg_Dbg( p_input, "cannot change rate" );
1819 i_rate = INPUT_RATE_DEFAULT;
1821 if( i_rate != p_input->p->i_rate &&
1822 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1825 if( p_input->p->input.p_access )
1827 i_ret = VLC_EGENERIC;
1831 if( !p_input->p->input.b_rescale_ts )
1832 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1834 i_ret = demux_Control( p_input->p->input.p_demux,
1835 DEMUX_SET_RATE, &i_rate );
1839 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1840 i_rate = p_input->p->i_rate;
1845 if( i_rate != p_input->p->i_rate )
1847 p_input->p->i_rate = i_rate;
1848 input_SendEventRate( p_input, i_rate );
1850 if( p_input->p->input.b_rescale_ts )
1852 const int i_rate_source = (p_input->b_can_pace_control || p_input->p->b_can_rate_control ) ? i_rate : INPUT_RATE_DEFAULT;
1853 es_out_SetRate( p_input->p->p_es_out, i_rate_source, i_rate );
1856 b_force_update = true;
1861 case INPUT_CONTROL_SET_PROGRAM:
1862 /* No need to force update, es_out does it if needed */
1863 es_out_Control( p_input->p->p_es_out,
1864 ES_OUT_SET_GROUP, val.i_int );
1866 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1870 case INPUT_CONTROL_SET_ES:
1871 /* No need to force update, es_out does it if needed */
1872 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, val.i_int );
1875 case INPUT_CONTROL_RESTART_ES:
1876 es_out_Control( p_input->p->p_es_out_display, ES_OUT_RESTART_ES_BY_ID, val.i_int );
1879 case INPUT_CONTROL_SET_AUDIO_DELAY:
1880 if( !es_out_SetDelay( p_input->p->p_es_out_display, AUDIO_ES, val.i_time ) )
1881 input_SendEventAudioDelay( p_input, val.i_time );
1884 case INPUT_CONTROL_SET_SPU_DELAY:
1885 if( !es_out_SetDelay( p_input->p->p_es_out_display, SPU_ES, val.i_time ) )
1886 input_SendEventSubtitleDelay( p_input, val.i_time );
1889 case INPUT_CONTROL_SET_TITLE:
1890 case INPUT_CONTROL_SET_TITLE_NEXT:
1891 case INPUT_CONTROL_SET_TITLE_PREV:
1892 if( p_input->p->b_recording )
1894 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1897 if( p_input->p->input.b_title_demux &&
1898 p_input->p->input.i_title > 0 )
1901 /* FIXME handle demux title */
1902 demux_t *p_demux = p_input->p->input.p_demux;
1905 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1906 i_title = p_demux->info.i_title - 1;
1907 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1908 i_title = p_demux->info.i_title + 1;
1910 i_title = val.i_int;
1912 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1914 es_out_SetTime( p_input->p->p_es_out, -1 );
1916 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1917 input_ControlVarTitle( p_input, i_title );
1920 else if( p_input->p->input.i_title > 0 )
1922 access_t *p_access = p_input->p->input.p_access;
1925 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1926 i_title = p_access->info.i_title - 1;
1927 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1928 i_title = p_access->info.i_title + 1;
1930 i_title = val.i_int;
1932 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1934 es_out_SetTime( p_input->p->p_es_out, -1 );
1936 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1937 stream_AccessReset( p_input->p->input.p_stream );
1941 case INPUT_CONTROL_SET_SEEKPOINT:
1942 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1943 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1944 if( p_input->p->b_recording )
1946 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1950 if( p_input->p->input.b_title_demux &&
1951 p_input->p->input.i_title > 0 )
1953 demux_t *p_demux = p_input->p->input.p_demux;
1955 int64_t i_input_time;
1956 int64_t i_seekpoint_time;
1958 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1960 i_seekpoint = p_demux->info.i_seekpoint;
1961 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1962 if( i_seekpoint_time >= 0 &&
1963 !demux_Control( p_demux,
1964 DEMUX_GET_TIME, &i_input_time ) )
1966 if( i_input_time < i_seekpoint_time + 3000000 )
1972 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1973 i_seekpoint = p_demux->info.i_seekpoint + 1;
1975 i_seekpoint = val.i_int;
1977 if( i_seekpoint >= 0 && i_seekpoint <
1978 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1981 es_out_SetTime( p_input->p->p_es_out, -1 );
1983 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1986 else if( p_input->p->input.i_title > 0 )
1988 demux_t *p_demux = p_input->p->input.p_demux;
1989 access_t *p_access = p_input->p->input.p_access;
1991 int64_t i_input_time;
1992 int64_t i_seekpoint_time;
1994 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1996 i_seekpoint = p_access->info.i_seekpoint;
1997 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1998 if( i_seekpoint_time >= 0 &&
1999 demux_Control( p_demux,
2000 DEMUX_GET_TIME, &i_input_time ) )
2002 if( i_input_time < i_seekpoint_time + 3000000 )
2008 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
2009 i_seekpoint = p_access->info.i_seekpoint + 1;
2011 i_seekpoint = val.i_int;
2013 if( i_seekpoint >= 0 && i_seekpoint <
2014 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
2016 es_out_SetTime( p_input->p->p_es_out, -1 );
2018 access_Control( p_access, ACCESS_SET_SEEKPOINT,
2020 stream_AccessReset( p_input->p->input.p_stream );
2025 case INPUT_CONTROL_ADD_SUBTITLE:
2026 if( val.psz_string )
2028 SubtitleAdd( p_input, val.psz_string, true );
2029 free( val.psz_string );
2033 case INPUT_CONTROL_ADD_SLAVE:
2034 if( val.psz_string )
2036 input_source_t *slave = InputSourceNew( p_input );
2038 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
2044 msg_Dbg( p_input, "adding %s as slave on the fly",
2048 if( demux_Control( p_input->p->input.p_demux,
2049 DEMUX_GET_TIME, &i_time ) )
2051 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2052 InputSourceClean( slave );
2056 if( demux_Control( slave->p_demux,
2057 DEMUX_SET_TIME, i_time ) )
2059 msg_Err( p_input, "seek failed for new slave" );
2060 InputSourceClean( slave );
2065 /* Get meta (access and demux) */
2066 p_meta = vlc_meta_New();
2067 access_Control( slave->p_access, ACCESS_GET_META,
2069 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
2070 InputUpdateMeta( p_input, p_meta );
2072 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2077 msg_Warn( p_input, "failed to add %s as slave",
2081 free( val.psz_string );
2085 case INPUT_CONTROL_SET_RECORD_STATE:
2086 if( !!p_input->p->b_recording != !!val.b_bool )
2088 if( p_input->p->input.b_can_stream_record )
2090 if( demux_Control( p_input->p->input.p_demux,
2091 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2096 if( es_out_SetRecordState( p_input->p->p_es_out_display, val.b_bool ) )
2099 p_input->p->b_recording = val.b_bool;
2101 input_SendEventRecord( p_input, val.b_bool );
2103 b_force_update = true;
2107 case INPUT_CONTROL_SET_FRAME_NEXT:
2108 if( p_input->i_state == PAUSE_S )
2110 es_out_SetFrameNext( p_input->p->p_es_out );
2112 else if( p_input->i_state == PLAYING_S )
2114 ControlPause( p_input, i_control_date );
2118 msg_Err( p_input, "invalid state for frame next" );
2120 b_force_update = true;
2123 case INPUT_CONTROL_SET_BOOKMARK:
2125 msg_Err( p_input, "not yet implemented" );
2129 return b_force_update;
2132 /*****************************************************************************
2134 *****************************************************************************/
2135 static int UpdateTitleSeekpoint( input_thread_t *p_input,
2136 int i_title, int i_seekpoint )
2138 int i_title_end = p_input->p->input.i_title_end -
2139 p_input->p->input.i_title_offset;
2140 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2141 p_input->p->input.i_seekpoint_offset;
2143 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2145 if( i_title > i_title_end ||
2146 ( i_title == i_title_end && i_seekpoint > i_seekpoint_end ) )
2149 else if( i_seekpoint_end >= 0 )
2151 if( i_seekpoint > i_seekpoint_end )
2154 else if( i_title_end >= 0 )
2156 if( i_title > i_title_end )
2161 static int UpdateFromDemux( input_thread_t *p_input )
2163 demux_t *p_demux = p_input->p->input.p_demux;
2165 /* TODO event-like */
2166 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2168 input_SendEventTitle( p_input, p_demux->info.i_title );
2170 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2172 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2174 input_SendEventSeekpoint( p_input,
2175 p_demux->info.i_title, p_demux->info.i_seekpoint );
2177 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2179 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2181 /* Hmmm only works with master input */
2182 if( p_input->p->input.p_demux == p_demux )
2183 return UpdateTitleSeekpoint( p_input,
2184 p_demux->info.i_title,
2185 p_demux->info.i_seekpoint );
2189 /*****************************************************************************
2191 *****************************************************************************/
2192 static int UpdateFromAccess( input_thread_t *p_input )
2194 access_t *p_access = p_input->p->input.p_access;
2196 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2198 input_SendEventTitle( p_input, p_access->info.i_title );
2200 stream_AccessUpdate( p_input->p->input.p_stream );
2202 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2204 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2206 input_SendEventSeekpoint( p_input,
2207 p_access->info.i_title, p_access->info.i_seekpoint );
2209 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2211 if( p_access->info.i_update & INPUT_UPDATE_META )
2213 /* TODO maybe multi - access ? */
2214 vlc_meta_t *p_meta = vlc_meta_New();
2215 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2216 InputUpdateMeta( p_input, p_meta );
2217 p_access->info.i_update &= ~INPUT_UPDATE_META;
2219 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2224 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2225 f_quality = f_strength = -1;
2227 input_SendEventSignal( p_input, f_quality, f_strength );
2229 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2232 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2234 /* Hmmm only works with master input */
2235 if( p_input->p->input.p_access == p_access )
2236 return UpdateTitleSeekpoint( p_input,
2237 p_access->info.i_title,
2238 p_access->info.i_seekpoint );
2242 /*****************************************************************************
2244 *****************************************************************************/
2245 static input_source_t *InputSourceNew( input_thread_t *p_input )
2247 VLC_UNUSED(p_input);
2248 input_source_t *in = malloc( sizeof( input_source_t ) );
2250 memset( in, 0, sizeof( input_source_t ) );
2254 /*****************************************************************************
2256 *****************************************************************************/
2257 static int InputSourceInit( input_thread_t *p_input,
2258 input_source_t *in, const char *psz_mrl,
2259 const char *psz_forced_demux )
2261 const bool b_master = in == &p_input->p->input;
2263 char psz_dup[strlen(psz_mrl) + 1];
2264 const char *psz_access;
2265 const char *psz_demux;
2272 strcpy( psz_dup, psz_mrl );
2274 if( !in ) return VLC_EGENERIC;
2275 if( !p_input ) return VLC_EGENERIC;
2278 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2280 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2281 psz_mrl, psz_access, psz_demux, psz_path );
2282 if( !p_input->b_preparsing )
2284 /* Hack to allow udp://@:port syntax */
2286 (strncmp( psz_access, "udp", 3 ) &&
2287 strncmp( psz_access, "rtp", 3 )) )
2289 /* Find optional titles and seekpoints */
2290 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2291 &in->i_seekpoint_start, &in->i_seekpoint_end );
2294 if( psz_forced_demux && *psz_forced_demux )
2296 psz_demux = psz_forced_demux;
2298 else if( *psz_demux == '\0' )
2300 /* special hack for forcing a demuxer with --demux=module
2301 * (and do nothing with a list) */
2302 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2304 if( psz_var_demux != NULL &&
2305 !strchr(psz_var_demux, ',' ) &&
2306 !strchr(psz_var_demux, ':' ) )
2308 psz_demux = psz_var_demux;
2310 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2314 /* Try access_demux first */
2315 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2316 NULL, p_input->p->p_es_out, false );
2320 /* Preparsing is only for file:// */
2323 if( !*psz_access ) /* path without scheme:// */
2324 psz_access = "file";
2325 if( strcmp( psz_access, "file" ) )
2327 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2332 int64_t i_pts_delay;
2334 /* Get infos from access_demux */
2335 demux_Control( in->p_demux,
2336 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2337 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2339 in->b_title_demux = true;
2340 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2341 &in->title, &in->i_title,
2342 &in->i_title_offset, &in->i_seekpoint_offset ) )
2344 TAB_INIT( in->i_title, in->title );
2346 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2347 &in->b_can_pace_control ) )
2348 in->b_can_pace_control = false;
2350 if( !in->b_can_pace_control )
2352 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2353 &in->b_can_rate_control, &in->b_rescale_ts ) )
2355 in->b_can_rate_control = false;
2356 in->b_rescale_ts = true; /* not used */
2361 in->b_can_rate_control = true;
2362 in->b_rescale_ts = true;
2364 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2365 &in->b_can_pause ) )
2366 in->b_can_pause = false;
2367 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2368 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2369 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2371 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2373 if( ret != VLC_SUCCESS )
2375 var_Set( p_input, "seekable", val );
2379 int64_t i_pts_delay;
2382 input_ChangeState( p_input, OPENING_S );
2384 /* Now try a real access */
2385 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2387 /* Access failed, URL encoded ? */
2388 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2390 decode_URI( psz_path );
2392 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2393 psz_access, psz_demux, psz_path );
2395 in->p_access = access_New( p_input,
2396 psz_access, psz_demux, psz_path );
2398 if( in->p_access == NULL )
2400 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2402 intf_UserFatal( VLC_OBJECT( p_input), false,
2403 _("Your input can't be opened"),
2404 _("VLC is unable to open the MRL '%s'."
2405 " Check the log for details."), psz_mrl );
2410 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2411 while( psz && *psz )
2413 access_t *p_access = in->p_access;
2414 char *end = strchr( psz, ':' );
2419 in->p_access = access_FilterNew( in->p_access, psz );
2420 if( in->p_access == NULL )
2422 in->p_access = p_access;
2423 msg_Warn( p_input, "failed to insert access filter %s",
2431 /* Get infos from access */
2432 if( !p_input->b_preparsing )
2434 access_Control( in->p_access,
2435 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2436 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2438 in->b_title_demux = false;
2439 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2440 &in->title, &in->i_title,
2441 &in->i_title_offset, &in->i_seekpoint_offset ) )
2444 TAB_INIT( in->i_title, in->title );
2446 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2447 &in->b_can_pace_control );
2448 in->b_can_rate_control = in->b_can_pace_control;
2449 in->b_rescale_ts = true;
2451 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2453 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2454 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2455 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2457 access_Control( in->p_access, ACCESS_CAN_SEEK,
2459 var_Set( p_input, "seekable", val );
2463 input_ChangeState( p_input, BUFFERING_S );
2465 /* Autodetect extra files if none specified */
2466 char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2467 if( !psz_input_list )
2469 char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2470 if( psz_extra_files )
2471 var_SetString( p_input, "input-list", psz_extra_files );
2472 free( psz_extra_files );
2475 /* Create the stream_t */
2476 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2478 /* Restor old value */
2479 if( !psz_input_list )
2480 var_SetString( p_input, "input-list", "" );
2481 free( psz_input_list );
2483 if( in->p_stream == NULL )
2485 msg_Warn( p_input, "cannot create a stream_t from access" );
2489 /* Open a demuxer */
2490 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2492 psz_demux = in->p_access->psz_demux;
2496 /* Take access redirections into account */
2497 char *psz_real_path;
2498 char *psz_buf = NULL;
2499 if( in->p_access->psz_path )
2501 const char *psz_a, *psz_d;
2502 psz_buf = strdup( in->p_access->psz_path );
2503 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2507 psz_real_path = psz_path;
2509 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2511 in->p_stream, p_input->p->p_es_out,
2512 p_input->b_preparsing );
2516 if( in->p_demux == NULL )
2518 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2519 psz_access, psz_demux, psz_path );
2520 intf_UserFatal( VLC_OBJECT( p_input ), false,
2521 _("VLC can't recognize the input's format"),
2522 _("The format of '%s' cannot be detected. "
2523 "Have a look at the log for details."), psz_mrl );
2527 /* Get title from demux */
2528 if( !p_input->b_preparsing && in->i_title <= 0 )
2530 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2531 &in->title, &in->i_title,
2532 &in->i_title_offset, &in->i_seekpoint_offset ))
2534 TAB_INIT( in->i_title, in->title );
2538 in->b_title_demux = true;
2543 /* Set record capabilities */
2544 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2545 in->b_can_stream_record = false;
2547 if( !var_CreateGetBool( p_input, "input-record-native" ) )
2548 in->b_can_stream_record = false;
2549 var_SetBool( p_input, "can-record", true );
2551 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2555 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2556 if( 1 || !p_input->b_preparsing )
2559 input_attachment_t **attachment;
2560 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2561 &attachment, &i_attachment ) )
2563 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2564 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2565 i_attachment, attachment );
2566 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2569 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2571 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2573 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2576 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2577 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2583 input_ChangeState( p_input, ERROR_S );
2586 demux_Delete( in->p_demux );
2589 stream_Delete( in->p_stream );
2592 access_Delete( in->p_access );
2594 return VLC_EGENERIC;
2597 /*****************************************************************************
2599 *****************************************************************************/
2600 static void InputSourceClean( input_source_t *in )
2605 demux_Delete( in->p_demux );
2608 stream_Delete( in->p_stream );
2611 access_Delete( in->p_access );
2613 if( in->i_title > 0 )
2615 for( i = 0; i < in->i_title; i++ )
2616 vlc_input_title_Delete( in->title[i] );
2617 TAB_CLEAN( in->i_title, in->title );
2621 static void SlaveDemux( input_thread_t *p_input )
2625 bool b_set_time = true;
2627 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2629 /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2633 for( i = 0; i < p_input->p->i_slave; i++ )
2635 input_source_t *in = p_input->p->slave[i];
2641 if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2646 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2648 msg_Err( p_input, "slave[%d] doesn't like "
2649 "DEMUX_GET_TIME -> EOF", i );
2654 if( i_stime >= i_time )
2657 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2663 i_ret = in->p_demux->pf_demux( in->p_demux );
2668 msg_Dbg( p_input, "slave %d EOF", i );
2674 static void SlaveSeek( input_thread_t *p_input )
2679 if( !p_input ) return;
2681 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2683 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2687 for( i = 0; i < p_input->p->i_slave; i++ )
2689 input_source_t *in = p_input->p->slave[i];
2691 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2694 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2704 /*****************************************************************************
2706 *****************************************************************************/
2707 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2711 if( !p_meta ) return;
2713 /* Get meta information from user */
2714 #define GET_META( field, s ) \
2715 var_Get( p_input, (s), &val ); \
2716 if( *val.psz_string ) \
2717 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2718 free( val.psz_string )
2720 GET_META( Title, "meta-title" );
2721 GET_META( Artist, "meta-artist" );
2722 GET_META( Genre, "meta-genre" );
2723 GET_META( Copyright, "meta-copyright" );
2724 GET_META( Description, "meta-description" );
2725 GET_META( Date, "meta-date" );
2726 GET_META( URL, "meta-url" );
2730 /*****************************************************************************
2731 * InputGetExtraFiles
2732 * Autodetect extra input list
2733 *****************************************************************************/
2734 static char *InputGetExtraFiles( input_thread_t *p_input,
2735 const char *psz_access, const char *psz_path )
2737 char *psz_list = NULL;
2739 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2743 const char *psz_ext = strrchr( psz_path, '.' );
2744 if( !psz_ext || strcmp( psz_ext, ".001" ) )
2747 char *psz_file = strdup( psz_path );
2751 /* Try to list .xyz files */
2752 for( int i = 2; i < 999; i++ )
2754 char *psz_ext = strrchr( psz_file, '.' );
2757 snprintf( psz_ext, 5, ".%.3d", i );
2759 if( utf8_stat( psz_file, &st )
2760 || !S_ISREG( st.st_mode ) || !st.st_size )
2763 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2767 char *psz_old = psz_list;
2768 /* FIXME how to handle file with ',' ?*/
2769 if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2777 psz_list = strdup( psz_file );
2785 /*****************************************************************************
2786 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2787 * arturl and locking issue.
2788 *****************************************************************************/
2789 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2791 input_item_t *p_item = p_input->p->input.p_item;
2792 char * psz_arturl = NULL;
2793 char *psz_title = NULL;
2798 psz_arturl = input_item_GetArtURL( p_item );
2800 vlc_mutex_lock( &p_item->lock );
2802 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2803 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2805 vlc_meta_Merge( p_item->p_meta, p_meta );
2807 vlc_meta_Delete( p_meta );
2809 if( psz_arturl && *psz_arturl )
2811 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2813 if( !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2815 /* Don't look for art cover if sout
2816 * XXX It can change when sout has meta data support */
2817 if( p_input->p->p_sout && !p_input->b_preparsing )
2818 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2820 input_ExtractAttachmentAndCacheArt( p_input );
2825 vlc_mutex_unlock( &p_item->lock );
2829 input_item_SetName( p_item, psz_title );
2832 input_item_SetPreparsed( p_item, true );
2834 input_SendEventMeta( p_input );
2836 /** \todo handle sout meta */
2840 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2841 int i_new, input_attachment_t **pp_new )
2843 int i_attachment = *pi_attachment;
2844 input_attachment_t **attachment = *ppp_attachment;
2847 attachment = realloc( attachment,
2848 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2849 for( i = 0; i < i_new; i++ )
2850 attachment[i_attachment++] = pp_new[i];
2854 *pi_attachment = i_attachment;
2855 *ppp_attachment = attachment;
2858 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2862 if( p_input->b_preparsing )
2865 if( p_input->p->input.p_access )
2866 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2869 /* Get meta data from slave input */
2870 for( i = 0; i < p_input->p->i_slave; i++ )
2872 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2873 if( p_input->p->slave[i]->p_access )
2875 access_Control( p_input->p->slave[i]->p_access,
2876 ACCESS_GET_META, p_meta );
2881 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2887 /* XXX I am not sure it is a great idea, besides, there is more than that
2888 * if we want to do it right */
2889 vlc_mutex_lock( &p_item->lock );
2890 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2892 vlc_mutex_unlock( &p_item->lock );
2895 vlc_mutex_unlock( &p_item->lock );
2898 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2899 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2904 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2905 if(! p_demux->p_private )
2908 p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2911 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2913 if( p_demux_meta->p_meta )
2915 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2916 vlc_meta_Delete( p_demux_meta->p_meta );
2919 if( p_demux_meta->i_attachments > 0 )
2921 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2922 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2923 p_demux_meta->i_attachments, p_demux_meta->attachments );
2924 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2926 module_unneed( p_demux, p_id3 );
2928 free( p_demux->p_private );
2931 static void input_ChangeState( input_thread_t *p_input, int i_state )
2933 const bool b_changed = p_input->i_state != i_state;
2935 p_input->i_state = i_state;
2936 if( i_state == ERROR_S )
2937 p_input->b_error = true;
2938 else if( i_state == END_S )
2939 p_input->b_eof = true;
2943 input_item_SetErrorWhenReading( p_input->p->input.p_item, p_input->b_error );
2944 input_SendEventState( p_input, i_state );
2949 /*****************************************************************************
2950 * MRLSplit: parse the access, demux and url part of the
2951 * Media Resource Locator.
2952 *****************************************************************************/
2953 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2956 char *psz_access = NULL;
2957 char *psz_demux = NULL;
2960 /* Either there is an access/demux specification before ://
2961 * or we have a plain local file path. */
2962 psz_path = strstr( psz_dup, "://" );
2963 if( psz_path != NULL )
2966 psz_path += 3; /* skips "://" */
2968 /* Separate access from demux (<access>/<demux>://<path>) */
2969 psz_access = psz_dup;
2970 psz_demux = strchr( psz_access, '/' );
2972 *psz_demux++ = '\0';
2974 /* We really don't want module name substitution here! */
2975 if( psz_access[0] == '$' )
2977 if( psz_demux && psz_demux[0] == '$' )
2984 *ppsz_access = psz_access ? psz_access : (char*)"";
2985 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2986 *ppsz_path = psz_path;
2989 static inline bool next(char ** src)
2993 long result = strtol( *src, &end, 0 );
2994 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
3003 /*****************************************************************************
3004 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
3007 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
3008 *****************************************************************************/
3009 static void MRLSections( input_thread_t *p_input, char *psz_source,
3010 int *pi_title_start, int *pi_title_end,
3011 int *pi_chapter_start, int *pi_chapter_end )
3013 char *psz, *psz_end, *psz_next, *psz_check;
3015 *pi_title_start = *pi_title_end = -1;
3016 *pi_chapter_start = *pi_chapter_end = -1;
3018 /* Start by parsing titles and chapters */
3019 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
3022 /* Check we are really dealing with a title/chapter section */
3023 psz_check = psz + 1;
3024 if( !*psz_check ) return;
3025 if( isdigit(*psz_check) )
3026 if(!next(&psz_check)) return;
3027 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
3028 if( *psz_check == ':' && ++psz_check )
3030 if( isdigit(*psz_check) )
3031 if(!next(&psz_check)) return;
3033 if( *psz_check != '-' && *psz_check ) return;
3034 if( *psz_check == '-' && ++psz_check )
3036 if( isdigit(*psz_check) )
3037 if(!next(&psz_check)) return;
3039 if( *psz_check != ':' && *psz_check ) return;
3040 if( *psz_check == ':' && ++psz_check )
3042 if( isdigit(*psz_check) )
3043 if(!next(&psz_check)) return;
3045 if( *psz_check ) return;
3047 /* Separate start and end */
3049 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
3051 /* Look for the start title */
3052 *pi_title_start = strtol( psz, &psz_next, 0 );
3053 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
3054 *pi_title_end = *pi_title_start;
3057 /* Look for the start chapter */
3059 *pi_chapter_start = strtol( psz, &psz_next, 0 );
3060 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
3061 *pi_chapter_end = *pi_chapter_start;
3065 /* Look for the end title */
3066 *pi_title_end = strtol( psz_end, &psz_next, 0 );
3067 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3070 /* Look for the end chapter */
3071 if( *psz_end ) psz_end++;
3072 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3073 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3076 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3077 psz_source, *pi_title_start, *pi_chapter_start,
3078 *pi_title_end, *pi_chapter_end );
3081 /*****************************************************************************
3082 * input_AddSubtitles: add a subtitles file and enable it
3083 *****************************************************************************/
3084 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3086 input_source_t *sub;
3089 char *psz_path, *psz_extension;
3091 /* if we are provided a subtitle.sub file,
3092 * see if we don't have a subtitle.idx and use it instead */
3093 psz_path = strdup( psz_subtitle );
3096 psz_extension = strrchr( psz_path, '.');
3097 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3101 strcpy( psz_extension, ".idx" );
3103 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3105 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3106 psz_path, psz_subtitle );
3107 strcpy( psz_subtitle, psz_path );
3113 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3115 sub = InputSourceNew( p_input );
3116 if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3121 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3124 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3126 if( count.i_int == 0 )
3128 /* if it was first one, there is disable too */
3130 if( count.i_int < list.p_list->i_count )
3132 const int i_id = list.p_list->p_values[count.i_int].i_int;
3134 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3135 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3137 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3141 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3142 bool b_check_extension )
3146 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3149 assert( psz_subtitle != NULL );
3151 val.psz_string = strdup( psz_subtitle );
3152 if( val.psz_string )
3153 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3157 /*****************************************************************************
3159 *****************************************************************************/
3160 void input_UpdateStatistic( input_thread_t *p_input,
3161 input_statistic_t i_type, int i_delta )
3163 assert( p_input->i_state != INIT_S );
3165 vlc_mutex_lock( &p_input->p->counters.counters_lock);
3168 #define I(c) stats_UpdateInteger( p_input, p_input->p->counters.c, i_delta, NULL )
3169 case INPUT_STATISTIC_DECODED_VIDEO:
3172 case INPUT_STATISTIC_DECODED_AUDIO:
3175 case INPUT_STATISTIC_DECODED_SUBTITLE:
3178 case INPUT_STATISTIC_SENT_PACKET:
3179 I(p_sout_sent_packets);
3182 case INPUT_STATISTIC_SENT_BYTE:
3184 int i_bytes; /* That's pretty stupid to define it as an integer, it will overflow
3186 if( !stats_UpdateInteger( p_input, p_input->p->counters.p_sout_sent_bytes, i_delta, &i_bytes ) )
3187 stats_UpdateFloat( p_input, p_input->p->counters.p_sout_send_bitrate, i_bytes, NULL );
3191 msg_Err( p_input, "Invalid statistic type %d (internal error)", i_type );
3194 vlc_mutex_unlock( &p_input->p->counters.counters_lock);
3196 /*****************************************************************************
3197 * input_get_event_manager
3198 *****************************************************************************/
3199 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3201 return &p_input->p->event_manager;
3205 /* TODO FIXME nearly the same logic that snapshot code */
3206 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3211 path = utf8_opendir( psz_path );
3216 char *psz_tmp = str_format( p_obj, psz_prefix );
3220 filename_sanitize( psz_tmp );
3221 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3223 psz_extension ? "." : "",
3224 psz_extension ? psz_extension : "" ) < 0 )
3231 psz_file = str_format( p_obj, psz_path );
3232 path_sanitize( psz_file );