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"
40 #include "es_out_timeshift.h"
46 #include "../stream_output/stream_output.h"
48 #include <vlc_interface.h>
50 #include <vlc_charset.h>
51 #include <vlc_strings.h>
53 #ifdef HAVE_SYS_STAT_H
54 # include <sys/stat.h>
57 /*****************************************************************************
59 *****************************************************************************/
60 static void Destructor( input_thread_t * p_input );
62 static void* Run ( vlc_object_t *p_this );
63 static void* RunAndDestroy ( vlc_object_t *p_this );
65 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
66 const char *, bool, sout_instance_t * );
67 static int Init ( input_thread_t *p_input );
68 static void WaitDie ( input_thread_t *p_input );
69 static void End ( input_thread_t *p_input );
70 static void MainLoop( input_thread_t *p_input );
72 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline );
73 static void ControlReduce( input_thread_t * );
74 static bool Control( input_thread_t *, int, vlc_value_t );
76 static int UpdateFromAccess( input_thread_t * );
77 static int UpdateFromDemux( input_thread_t * );
79 static void UpdateItemLength( input_thread_t *, int64_t i_length );
81 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
83 static input_source_t *InputSourceNew( input_thread_t *);
84 static int InputSourceInit( input_thread_t *, input_source_t *,
85 const char *, const char *psz_forced_demux );
86 static void InputSourceClean( input_source_t * );
88 //static void InputGetAttachments( input_thread_t *, input_source_t * );
89 static void SlaveDemux( input_thread_t *p_input );
90 static void SlaveSeek( input_thread_t *p_input );
92 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
93 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
94 static char *InputGetExtraFiles( input_thread_t *p_input,
95 const char *psz_access, const char *psz_path );
97 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
98 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
99 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
100 int i_new, input_attachment_t **pp_new );
102 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced );
104 /*****************************************************************************
105 * This function creates a new input, and returns a pointer
106 * to its description. On error, it returns NULL.
108 * Variables for _public_ use:
111 * - rate,rate-slower, rate-faster
112 * - position, position-offset
113 * - time, time-offset
114 * - title,title-next,title-prev
115 * - chapter,chapter-next, chapter-prev
116 * - program, audio-es, video-es, spu-es
117 * - audio-delay, spu-delay
122 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
123 * or not, for that check position != 0.0)
125 * - can-record (if a stream can be recorded while playing)
126 * - teletext-es to get the index of spu track that is teletext --1 if no teletext)
127 * * For intf callback upon changes:
129 * - intf-change-vout for when a vout is created or destroyed
130 * - rate-change for when playback rate changes
131 * - stats-change for when statistics are updated
132 * TODO explain when Callback is called
133 * TODO complete this list (?)
134 *****************************************************************************/
135 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
136 const char *psz_header, bool b_quick,
137 sout_instance_t *p_sout )
139 static const char input_name[] = "input";
140 input_thread_t *p_input = NULL; /* thread descriptor */
144 /* Allocate descriptor */
145 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
146 VLC_OBJECT_INPUT, input_name );
147 if( p_input == NULL )
150 /* Construct a nice name for the input timer */
151 char psz_timer_name[255];
152 char * psz_name = input_item_GetName( p_item );
153 snprintf( psz_timer_name, sizeof(psz_timer_name),
154 "input launching for '%s'", psz_name );
156 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
160 /* Start a timer to mesure how long it takes
161 * to launch an input */
162 stats_TimerStart( p_input, psz_timer_name,
163 STATS_TIMER_INPUT_LAUNCHING );
165 p_input->p = calloc( 1, sizeof( input_thread_private_t ) );
169 /* One "randomly" selected input thread is responsible for computing
170 * the global stats. Check if there is already someone doing this */
171 if( p_input->p_libvlc->p_stats && !b_quick )
173 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
174 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
175 if( p_private->p_stats_computer == NULL )
176 p_private->p_stats_computer = p_input;
177 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
180 p_input->b_preparsing = b_quick;
181 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
184 vlc_event_manager_t * p_em = &p_input->p->event_manager;
185 vlc_event_manager_init_with_vlc_object( p_em, p_input );
186 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
187 vlc_event_manager_register_event_type( p_em, vlc_InputSelectedStreamChanged );
189 /* Init Common fields */
190 p_input->b_eof = false;
191 p_input->b_can_pace_control = true;
192 p_input->p->i_start = 0;
194 p_input->p->i_stop = 0;
195 p_input->p->i_run = 0;
196 p_input->p->i_title = 0;
197 p_input->p->title = NULL;
198 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
199 p_input->i_state = INIT_S;
200 p_input->p->i_rate = INPUT_RATE_DEFAULT;
201 p_input->p->b_recording = false;
202 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
203 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
204 p_input->p->p_es_out_display = NULL;
205 p_input->p->p_es_out = NULL;
206 p_input->p->p_sout = NULL;
207 p_input->p->b_out_pace_control = false;
208 p_input->i_pts_delay = 0;
210 /* Init Input fields */
211 vlc_gc_incref( p_item ); /* Released in Destructor() */
212 p_input->p->input.p_item = p_item;
213 p_input->p->input.p_access = NULL;
214 p_input->p->input.p_stream = NULL;
215 p_input->p->input.p_demux = NULL;
216 p_input->p->input.b_title_demux = false;
217 p_input->p->input.i_title = 0;
218 p_input->p->input.title = NULL;
219 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
220 p_input->p->input.b_can_pace_control = true;
221 p_input->p->input.b_can_rate_control = true;
222 p_input->p->input.b_rescale_ts = true;
223 p_input->p->input.b_eof = false;
224 p_input->p->input.i_cr_average = 0;
226 vlc_mutex_lock( &p_item->lock );
228 if( !p_item->p_stats )
229 p_item->p_stats = stats_NewInputStats( p_input );
230 vlc_mutex_unlock( &p_item->lock );
233 p_input->p->i_slave = 0;
234 p_input->p->slave = NULL;
236 /* Init control buffer */
237 vlc_mutex_init( &p_input->p->lock_control );
238 vlc_cond_init( &p_input->p->wait_control );
239 p_input->p->i_control = 0;
241 /* Parse input options */
242 vlc_mutex_lock( &p_item->lock );
243 assert( (int)p_item->optflagc == p_item->i_options );
244 for( i = 0; i < p_item->i_options; i++ )
245 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
246 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
247 vlc_mutex_unlock( &p_item->lock );
249 /* Create Object Variables for private use only */
250 input_ConfigVarInit( p_input );
252 /* Create Objects variables for public Get and Set */
253 input_ControlVarInit( p_input );
256 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
258 if( !p_input->b_preparsing )
260 var_Get( p_input, "bookmarks", &val );
263 /* FIXME: have a common cfg parsing routine used by sout and others */
264 char *psz_parser, *psz_start, *psz_end;
265 psz_parser = val.psz_string;
266 while( (psz_start = strchr( psz_parser, '{' ) ) )
268 seekpoint_t *p_seekpoint;
271 psz_end = strchr( psz_start, '}' );
272 if( !psz_end ) break;
273 psz_parser = psz_end + 1;
274 backup = *psz_parser;
278 p_seekpoint = vlc_seekpoint_New();
279 while( (psz_end = strchr( psz_start, ',' ) ) )
282 if( !strncmp( psz_start, "name=", 5 ) )
284 p_seekpoint->psz_name = strdup(psz_start + 5);
286 else if( !strncmp( psz_start, "bytes=", 6 ) )
288 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
290 else if( !strncmp( psz_start, "time=", 5 ) )
292 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
295 psz_start = psz_end + 1;
297 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
298 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
299 p_seekpoint->i_time_offset );
300 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
301 vlc_seekpoint_Delete( p_seekpoint );
302 *psz_parser = backup;
304 free( val.psz_string );
308 /* Remove 'Now playing' info as it is probably outdated */
309 input_item_SetNowPlaying( p_item, NULL );
312 if( p_input->b_preparsing )
313 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
317 p_input->p->p_sout = p_sout;
319 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
320 vlc_mutex_init( &p_input->p->counters.counters_lock );
322 /* Set the destructor when we are sure we are initialized */
323 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
325 /* Attach only once we are ready */
326 vlc_object_attach( p_input, p_parent );
332 * Input destructor (called when the object's refcount reaches 0).
334 static void Destructor( input_thread_t * p_input )
337 char * psz_name = input_item_GetName( p_input->p->input.p_item );
338 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
342 vlc_event_manager_fini( &p_input->p->event_manager );
344 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
345 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
347 if( p_input->p->p_sout )
348 sout_DeleteInstance( p_input->p->p_sout );
350 vlc_gc_decref( p_input->p->input.p_item );
352 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
354 vlc_cond_destroy( &p_input->p->wait_control );
355 vlc_mutex_destroy( &p_input->p->lock_control );
360 * Initialize an input thread and run it. You will need to monitor the
361 * thread to clean up after it is done
363 * \param p_parent a vlc_object
364 * \param p_item an input item
365 * \return a pointer to the spawned input thread
367 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
368 input_item_t *p_item )
370 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
374 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
375 input_item_t *p_item,
376 const char *psz_log, sout_instance_t *p_sout )
378 input_thread_t *p_input;
380 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
384 /* Create thread and wait for its readiness. */
385 if( vlc_thread_create( p_input, "input", Run,
386 VLC_THREAD_PRIORITY_INPUT, false ) )
388 input_ChangeState( p_input, ERROR_S );
389 msg_Err( p_input, "cannot create input thread" );
390 vlc_object_detach( p_input );
391 vlc_object_release( p_input );
399 * Initialize an input thread and run it. This thread will clean after itself,
400 * you can forget about it. It can work either in blocking or non-blocking mode
402 * \param p_parent a vlc_object
403 * \param p_item an input item
404 * \param b_block should we block until read is finished ?
405 * \return an error code, VLC_SUCCESS on success
407 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
410 input_thread_t *p_input;
412 p_input = Create( p_parent, p_item, NULL, false, NULL );
418 RunAndDestroy( VLC_OBJECT(p_input) );
423 if( vlc_thread_create( p_input, "input", RunAndDestroy,
424 VLC_THREAD_PRIORITY_INPUT, false ) )
426 input_ChangeState( p_input, ERROR_S );
427 msg_Err( p_input, "cannot create input thread" );
428 vlc_object_release( p_input );
436 * Initialize an input and initialize it to preparse the item
437 * This function is blocking. It will only accept to parse files
439 * \param p_parent a vlc_object_t
440 * \param p_item an input item
441 * \return VLC_SUCCESS or an error
443 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
445 input_thread_t *p_input;
447 /* Allocate descriptor */
448 p_input = Create( p_parent, p_item, NULL, true, NULL );
452 if( !Init( p_input ) )
455 vlc_object_detach( p_input );
456 vlc_object_release( p_input );
462 * Request a running input thread to stop and die
464 * \param the input thread to stop
466 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
471 if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
472 p_obj->i_object_type == VLC_OBJECT_AOUT ||
473 p_obj == VLC_OBJECT(p_input->p->p_sout) )
476 vlc_object_kill( p_obj );
478 p_list = vlc_list_children( p_obj );
479 for( i = 0; i < p_list->i_count; i++ )
480 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
481 vlc_list_release( p_list );
483 void input_StopThread( input_thread_t *p_input )
485 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
486 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
487 * unlock the control loop */
488 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
490 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
493 sout_instance_t *input_DetachSout( input_thread_t *p_input )
495 assert( p_input->b_dead );
496 sout_instance_t *p_sout = p_input->p->p_sout;
497 vlc_object_detach( p_sout );
498 p_input->p->p_sout = NULL;
502 /*****************************************************************************
503 * Run: main thread loop
504 * This is the "normal" thread that spawns the input processing chain,
505 * reads the stream, cleans up and waits
506 *****************************************************************************/
507 static void *Run( vlc_object_t *p_this )
509 input_thread_t *p_input = (input_thread_t *)p_this;
510 const int canc = vlc_savecancel();
512 if( Init( p_input ) )
514 /* If we failed, wait before we are killed, and exit */
521 /* Wait until we are asked to die */
522 if( !p_input->b_die )
529 p_input->b_dead = true;
530 vlc_restorecancel( canc );
534 /*****************************************************************************
535 * RunAndDestroy: main thread loop
536 * This is the "just forget me" thread that spawns the input processing chain,
537 * reads the stream, cleans up and releases memory
538 *****************************************************************************/
539 static void *RunAndDestroy( vlc_object_t *p_this )
541 input_thread_t *p_input = (input_thread_t *)p_this;
542 const int canc = vlc_savecancel();
544 if( Init( p_input ) )
554 vlc_object_release( p_input );
555 vlc_restorecancel( canc );
559 /*****************************************************************************
560 * Main loop: Fill buffers from access, and demux
561 *****************************************************************************/
565 * It asks the demuxer to demux some data
567 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t *pi_start_mdate )
573 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
574 ( p_input->p->i_run > 0 && *pi_start_mdate+p_input->p->i_run < mdate() ) )
577 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
582 if( p_input->p->input.b_title_demux &&
583 p_input->p->input.p_demux->info.i_update )
585 i_ret = UpdateFromDemux( p_input );
588 else if( !p_input->p->input.b_title_demux &&
589 p_input->p->input.p_access &&
590 p_input->p->input.p_access->info.i_update )
592 i_ret = UpdateFromAccess( p_input );
597 if( i_ret == 0 ) /* EOF */
601 var_Get( p_input, "input-repeat", &repeat );
602 if( repeat.i_int == 0 )
604 /* End of file - we do not set b_die because only the
605 * playlist is allowed to do so. */
606 msg_Dbg( p_input, "EOF reached" );
607 p_input->p->input.b_eof = true;
613 msg_Dbg( p_input, "repeating the same input (%d)",
615 if( repeat.i_int > 0 )
618 var_Set( p_input, "input-repeat", repeat );
621 /* Seek to start title/seekpoint */
622 val.i_int = p_input->p->input.i_title_start -
623 p_input->p->input.i_title_offset;
624 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
626 input_ControlPush( p_input,
627 INPUT_CONTROL_SET_TITLE, &val );
629 val.i_int = p_input->p->input.i_seekpoint_start -
630 p_input->p->input.i_seekpoint_offset;
631 if( val.i_int > 0 /* TODO: check upper boundary */ )
632 input_ControlPush( p_input,
633 INPUT_CONTROL_SET_SEEKPOINT, &val );
635 /* Seek to start position */
636 if( p_input->p->i_start > 0 )
638 val.i_time = p_input->p->i_start;
639 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
645 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
650 *pi_start_mdate = mdate();
655 input_ChangeState( p_input, ERROR_S );
658 if( i_ret > 0 && p_input->p->i_slave > 0 )
660 SlaveDemux( p_input );
666 * It update the variables used by the interfaces
668 static void MainLoopInterface( input_thread_t *p_input )
672 int64_t i_time, i_length;
674 /* update input status variables */
675 if( !demux_Control( p_input->p->input.p_demux,
676 DEMUX_GET_POSITION, &f_pos ) )
678 val.f_float = (float)f_pos;
679 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
681 if( !demux_Control( p_input->p->input.p_demux,
682 DEMUX_GET_TIME, &i_time ) )
684 p_input->i_time = i_time;
686 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
688 if( !demux_Control( p_input->p->input.p_demux,
689 DEMUX_GET_LENGTH, &i_length ) )
692 var_Get( p_input, "length", &old_val );
693 val.i_time = i_length;
694 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
696 if( old_val.i_time != val.i_time )
698 UpdateItemLength( p_input, i_length );
702 var_SetBool( p_input, "intf-change", true );
707 * It updates the globals statics
709 static void MainLoopStatistic( input_thread_t *p_input )
711 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
712 /* Are we the thread responsible for computing global stats ? */
713 if( libvlc_priv( p_input->p_libvlc )->p_stats_computer == p_input )
715 stats_ComputeGlobalStats( p_input->p_libvlc,
716 p_input->p_libvlc->p_stats );
718 var_SetBool( p_input, "stats-change", true );
723 * The main input loop.
725 static void MainLoop( input_thread_t *p_input )
727 mtime_t i_start_mdate = mdate();
728 mtime_t i_intf_update = 0;
729 mtime_t i_statistic_update = 0;
731 /* Start the timer */
732 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
734 while( vlc_object_alive( p_input ) && !p_input->b_error )
745 b_force_update = false;
747 /* FIXME if p_input->i_state == PAUSE_S the access/access_demux
748 * is paused -> this may cause problem with some of them
749 * The same problem can be seen when seeking while paused */
750 b_paused = p_input->i_state == PAUSE_S &&
751 !es_out_GetBuffering( p_input->p->p_es_out );
755 if( !p_input->p->input.b_eof )
757 MainLoopDemux( p_input, &b_force_update, &i_start_mdate );
759 i_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
761 else if( !p_input->b_eof && !es_out_GetEmpty( p_input->p->p_es_out ) )
763 msg_Dbg( p_input, "waiting decoder fifos to empty" );
764 i_wakeup = mdate() + INPUT_IDLE_SLEEP;
774 i_deadline = i_wakeup;
776 i_deadline = __MIN( i_intf_update, i_statistic_update );
779 vlc_mutex_lock( &p_input->p->lock_control );
780 ControlReduce( p_input );
781 while( !ControlPopNoLock( p_input, &i_type, &val, i_deadline ) )
783 msg_Dbg( p_input, "control type=%d", i_type );
785 if( Control( p_input, i_type, val ) )
786 b_force_update = true;
788 vlc_mutex_unlock( &p_input->p->lock_control );
790 /* Update interface and statistics */
792 if( i_intf_update < i_current || b_force_update )
794 MainLoopInterface( p_input );
795 i_intf_update = i_current + INT64_C(250000);
796 b_force_update = false;
798 if( i_statistic_update < i_current )
800 MainLoopStatistic( p_input );
801 i_statistic_update = i_current + INT64_C(1000000);
804 /* Check if i_wakeup is still valid */
807 mtime_t i_new_wakeup = es_out_GetWakeup( p_input->p->p_es_out );
811 } while( i_current < i_wakeup );
814 if( !p_input->b_error )
815 input_ChangeState( p_input, END_S );
818 static void InitStatistics( input_thread_t * p_input )
820 if( p_input->b_preparsing ) return;
822 /* Prepare statistics */
823 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
824 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
825 if( libvlc_stats( p_input ) )
827 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
828 INIT_COUNTER( read_packets, INTEGER, COUNTER );
829 INIT_COUNTER( demux_read, INTEGER, COUNTER );
830 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
831 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
832 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
833 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
834 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
835 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
836 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
837 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
838 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
839 p_input->p->counters.p_sout_send_bitrate = NULL;
840 p_input->p->counters.p_sout_sent_packets = NULL;
841 p_input->p->counters.p_sout_sent_bytes = NULL;
842 if( p_input->p->counters.p_demux_bitrate )
843 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
844 if( p_input->p->counters.p_input_bitrate )
845 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
850 static int InitSout( input_thread_t * p_input )
854 if( p_input->b_preparsing ) return VLC_SUCCESS;
856 /* Find a usable sout and attach it to p_input */
857 psz = var_GetNonEmptyString( p_input, "sout" );
858 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
860 /* Check the validity of the provided sout */
861 if( p_input->p->p_sout )
863 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
865 msg_Dbg( p_input, "destroying unusable sout" );
867 sout_DeleteInstance( p_input->p->p_sout );
868 p_input->p->p_sout = NULL;
872 if( p_input->p->p_sout )
875 msg_Dbg( p_input, "sout keep: reusing sout" );
876 msg_Dbg( p_input, "sout keep: you probably want to use "
877 "gather stream_out" );
878 vlc_object_attach( p_input->p->p_sout, p_input );
882 /* Create a new one */
883 p_input->p->p_sout = sout_NewInstance( p_input, psz );
884 if( !p_input->p->p_sout )
886 input_ChangeState( p_input, ERROR_S );
887 msg_Err( p_input, "cannot start stream output instance, " \
893 if( libvlc_stats( p_input ) )
895 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
896 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
897 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
898 if( p_input->p->counters.p_sout_send_bitrate )
899 p_input->p->counters.p_sout_send_bitrate->update_interval =
903 else if( p_input->p->p_sout )
905 msg_Dbg( p_input, "destroying useless sout" );
907 sout_DeleteInstance( p_input->p->p_sout );
908 p_input->p->p_sout = NULL;
916 static void InitTitle( input_thread_t * p_input )
920 if( p_input->b_preparsing ) return;
922 /* Create global title (from master) */
923 p_input->p->i_title = p_input->p->input.i_title;
924 p_input->p->title = p_input->p->input.title;
925 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
926 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
927 if( p_input->p->i_title > 0 )
929 /* Setup variables */
930 input_ControlVarNavigation( p_input );
931 input_ControlVarTitle( p_input, 0 );
935 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
936 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
937 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
940 if( p_input->i_pts_delay < 0 )
941 p_input->i_pts_delay = 0;
943 /* If the desynchronisation requested by the user is < 0, we need to
944 * cache more data. */
945 var_Get( p_input, "audio-desync", &val );
947 p_input->i_pts_delay -= (val.i_int * 1000);
949 /* Update cr_average depending on the caching */
950 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
951 p_input->p->input.i_cr_average /= 10;
952 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
955 static void StartTitle( input_thread_t * p_input )
964 /* Start title/chapter */
966 if( p_input->b_preparsing )
968 p_input->p->i_start = 0;
972 val.i_int = p_input->p->input.i_title_start -
973 p_input->p->input.i_title_offset;
974 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
975 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
976 val.i_int = p_input->p->input.i_seekpoint_start -
977 p_input->p->input.i_seekpoint_offset;
978 if( val.i_int > 0 /* TODO: check upper boundary */ )
979 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
983 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
984 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
985 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
986 i_length = var_GetTime( p_input, "length" );
987 if( p_input->p->i_run < 0 )
989 msg_Warn( p_input, "invalid run-time ignored" );
990 p_input->p->i_run = 0;
993 if( p_input->p->i_start > 0 )
995 if( p_input->p->i_start >= i_length )
997 msg_Warn( p_input, "invalid start-time ignored" );
1003 msg_Dbg( p_input, "starting at time: %ds",
1004 (int)( p_input->p->i_start / INT64_C(1000000) ) );
1006 s.i_time = p_input->p->i_start;
1007 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
1010 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
1012 msg_Warn( p_input, "invalid stop-time ignored" );
1013 p_input->p->i_stop = 0;
1016 /* Load subtitles */
1017 /* Get fps and set it if not already set */
1018 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
1021 float f_requested_fps;
1023 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
1024 var_SetFloat( p_input, "sub-original-fps", f_fps );
1026 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1027 if( f_requested_fps != f_fps )
1029 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1030 VLC_VAR_DOINHERIT );
1031 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1035 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1038 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1041 /* Look for and add subtitle files */
1042 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1043 if( psz_subtitle != NULL )
1045 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1046 SubtitleAdd( p_input, psz_subtitle, true );
1049 var_Get( p_input, "sub-autodetect-file", &val );
1052 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1053 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1054 p_input->p->input.p_item->psz_uri );
1055 free( psz_autopath );
1057 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1059 /* Try to autoselect the first autodetected subtitles file
1060 * if no subtitles file was specified */
1061 bool b_forced = i == 0 && !psz_subtitle;
1063 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1064 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1066 free( ppsz_subs[i] );
1070 free( psz_subtitle );
1072 /* Look for slave */
1073 psz = var_GetNonEmptyString( p_input, "input-slave" );
1077 input_source_t *slave;
1078 while( psz && *psz )
1080 while( *psz == ' ' || *psz == '#' )
1084 if( ( psz_delim = strchr( psz, '#' ) ) )
1086 *psz_delim++ = '\0';
1093 msg_Dbg( p_input, "adding slave input '%s'", psz );
1094 slave = InputSourceNew( p_input );
1095 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1097 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1106 static void InitPrograms( input_thread_t * p_input )
1111 if( p_input->b_preparsing ) return;
1114 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1115 i_es_out_mode = ES_OUT_MODE_AUTO;
1117 if( p_input->p->p_sout )
1119 var_Get( p_input, "sout-all", &val );
1122 i_es_out_mode = ES_OUT_MODE_ALL;
1127 var_Get( p_input, "programs", &val );
1128 if( val.p_list && val.p_list->i_count )
1130 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1131 /* Note : we should remove the "program" callback. */
1135 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1140 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1142 /* Inform the demuxer about waited group (needed only for DVB) */
1143 if( i_es_out_mode == ES_OUT_MODE_ALL )
1145 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1147 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1149 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1154 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1155 (int) var_GetInteger( p_input, "program" ), NULL );
1159 static int Init( input_thread_t * p_input )
1165 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1167 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1169 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1170 var_SetString( p_input, "sout", "" );
1171 var_SetBool( p_input, "sout-all", false );
1172 var_SetString( p_input, "input-slave", "" );
1173 var_SetInteger( p_input, "input-repeat", 0 );
1174 var_SetString( p_input, "sub-file", "" );
1175 var_SetBool( p_input, "sub-autodetect-file", false );
1179 InitStatistics( p_input );
1181 ret = InitSout( p_input );
1182 if( ret != VLC_SUCCESS )
1183 return ret; /* FIXME: goto error; should be better here */
1187 p_input->p->p_es_out_display = input_EsOutNew( p_input, p_input->p->i_rate );
1188 p_input->p->p_es_out = input_EsOutTimeshiftNew( p_input, p_input->p->p_es_out_display, p_input->p->i_rate );
1189 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1190 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1192 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1193 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1195 if( InputSourceInit( p_input, &p_input->p->input,
1196 p_input->p->input.p_item->psz_uri, NULL ) )
1201 InitTitle( p_input );
1203 /* Load master infos */
1205 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1206 &val.i_time ) && val.i_time > 0 )
1208 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1209 UpdateItemLength( p_input, val.i_time );
1213 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1214 if( val.i_time > 0 )
1215 { /* fallback: gets length from metadata */
1216 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1217 UpdateItemLength( p_input, val.i_time );
1221 StartTitle( p_input );
1223 InitPrograms( p_input );
1225 if( !p_input->b_preparsing && p_input->p->p_sout )
1227 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1229 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1231 /* We don't want a high input priority here or we'll
1232 * end-up sucking up all the CPU time */
1233 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1236 msg_Dbg( p_input, "starting in %s mode",
1237 p_input->p->b_out_pace_control ? "async" : "sync" );
1240 p_meta = vlc_meta_New();
1242 /* Get meta data from users */
1243 InputMetaUser( p_input, p_meta );
1245 /* Get meta data from master input */
1246 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1248 /* Access_file does not give any meta, and there are no slave */
1249 AccessMeta( p_input, p_meta );
1251 InputUpdateMeta( p_input, p_meta );
1253 if( !p_input->b_preparsing )
1255 msg_Dbg( p_input, "`%s' successfully opened",
1256 p_input->p->input.p_item->psz_uri );
1260 /* initialization is complete */
1261 input_ChangeState( p_input, PLAYING_S );
1266 input_ChangeState( p_input, ERROR_S );
1268 if( p_input->p->p_es_out )
1269 es_out_Delete( p_input->p->p_es_out );
1270 if( p_input->p->p_es_out_display )
1271 es_out_Delete( p_input->p->p_es_out_display );
1273 if( p_input->p->p_sout )
1275 vlc_object_detach( p_input->p->p_sout );
1276 sout_DeleteInstance( p_input->p->p_sout );
1280 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1282 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1283 stats_CounterClean( p_input->p->counters.p_##c );\
1284 p_input->p->counters.p_##c = NULL; } while(0)
1285 EXIT_COUNTER( read_bytes );
1286 EXIT_COUNTER( read_packets );
1287 EXIT_COUNTER( demux_read );
1288 EXIT_COUNTER( input_bitrate );
1289 EXIT_COUNTER( demux_bitrate );
1290 EXIT_COUNTER( played_abuffers );
1291 EXIT_COUNTER( lost_abuffers );
1292 EXIT_COUNTER( displayed_pictures );
1293 EXIT_COUNTER( lost_pictures );
1294 EXIT_COUNTER( decoded_audio );
1295 EXIT_COUNTER( decoded_video );
1296 EXIT_COUNTER( decoded_sub );
1298 if( p_input->p->p_sout )
1300 EXIT_COUNTER( sout_sent_packets );
1301 EXIT_COUNTER( sout_sent_bytes );
1302 EXIT_COUNTER( sout_send_bitrate );
1307 /* Mark them deleted */
1308 p_input->p->input.p_demux = NULL;
1309 p_input->p->input.p_stream = NULL;
1310 p_input->p->input.p_access = NULL;
1311 p_input->p->p_es_out = NULL;
1312 p_input->p->p_es_out_display = NULL;
1313 p_input->p->p_sout = NULL;
1315 return VLC_EGENERIC;
1318 /*****************************************************************************
1319 * WaitDie: Wait until we are asked to die.
1320 *****************************************************************************
1321 * This function is called when an error occurred during thread main's loop.
1322 *****************************************************************************/
1323 static void WaitDie( input_thread_t *p_input )
1325 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1327 /* Wait a die order */
1328 vlc_object_lock( p_input );
1329 while( vlc_object_alive( p_input ) )
1330 vlc_object_wait( p_input );
1331 vlc_object_unlock( p_input );
1334 /*****************************************************************************
1335 * End: end the input thread
1336 *****************************************************************************/
1337 static void End( input_thread_t * p_input )
1341 /* We are at the end */
1342 input_ChangeState( p_input, END_S );
1344 /* Clean control variables */
1345 input_ControlVarStop( p_input );
1347 /* Clean up master */
1348 InputSourceClean( &p_input->p->input );
1351 for( i = 0; i < p_input->p->i_slave; i++ )
1353 InputSourceClean( p_input->p->slave[i] );
1354 free( p_input->p->slave[i] );
1356 free( p_input->p->slave );
1358 /* Unload all modules */
1359 if( p_input->p->p_es_out )
1360 es_out_Delete( p_input->p->p_es_out );
1361 if( p_input->p->p_es_out_display )
1362 es_out_Delete( p_input->p->p_es_out_display );
1364 if( !p_input->b_preparsing )
1366 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1367 if( libvlc_stats( p_input ) )
1369 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
1371 /* make sure we are up to date */
1372 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1373 if( p_private->p_stats_computer == p_input )
1375 stats_ComputeGlobalStats( p_input->p_libvlc,
1376 p_input->p_libvlc->p_stats );
1377 p_private->p_stats_computer = NULL;
1379 CL_CO( read_bytes );
1380 CL_CO( read_packets );
1381 CL_CO( demux_read );
1382 CL_CO( input_bitrate );
1383 CL_CO( demux_bitrate );
1384 CL_CO( played_abuffers );
1385 CL_CO( lost_abuffers );
1386 CL_CO( displayed_pictures );
1387 CL_CO( lost_pictures );
1388 CL_CO( decoded_audio) ;
1389 CL_CO( decoded_video );
1390 CL_CO( decoded_sub) ;
1393 /* Close optional stream output instance */
1394 if( p_input->p->p_sout )
1396 CL_CO( sout_sent_packets );
1397 CL_CO( sout_sent_bytes );
1398 CL_CO( sout_send_bitrate );
1400 vlc_object_detach( p_input->p->p_sout );
1405 if( p_input->p->i_attachment > 0 )
1407 for( i = 0; i < p_input->p->i_attachment; i++ )
1408 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1409 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1412 /* Tell we're dead */
1413 p_input->b_dead = true;
1416 /*****************************************************************************
1418 *****************************************************************************/
1419 static inline int ControlPopNoLock( input_thread_t *p_input,
1420 int *pi_type, vlc_value_t *p_val,
1421 mtime_t i_deadline )
1424 while( p_input->p->i_control <= 0 )
1426 if( !vlc_object_alive( p_input ) )
1427 return VLC_EGENERIC;
1429 if( i_deadline < 0 )
1430 return VLC_EGENERIC;
1432 if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1433 return VLC_EGENERIC;
1436 *pi_type = p_input->p->control[0].i_type;
1437 *p_val = p_input->p->control[0].val;
1439 p_input->p->i_control--;
1440 if( p_input->p->i_control > 0 )
1444 for( i = 0; i < p_input->p->i_control; i++ )
1446 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1447 p_input->p->control[i].val = p_input->p->control[i+1].val;
1454 static void ControlReduce( input_thread_t *p_input )
1461 for( i = 1; i < p_input->p->i_control; i++ )
1463 const int i_lt = p_input->p->control[i-1].i_type;
1464 const int i_ct = p_input->p->control[i].i_type;
1466 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1467 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1471 ( i_ct == INPUT_CONTROL_SET_STATE ||
1472 i_ct == INPUT_CONTROL_SET_RATE ||
1473 i_ct == INPUT_CONTROL_SET_POSITION ||
1474 i_ct == INPUT_CONTROL_SET_TIME ||
1475 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1476 i_ct == INPUT_CONTROL_SET_TITLE ||
1477 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1478 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1481 // msg_Dbg( p_input, "merged at %d", i );
1482 /* Remove the i-1 */
1483 for( j = i; j < p_input->p->i_control; j++ )
1484 p_input->p->control[j-1] = p_input->p->control[j];
1485 p_input->p->i_control--;
1489 /* TODO but that's not that important
1490 - merge SET_X with SET_X_CMD
1491 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1492 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1499 static void ControlPause( input_thread_t *p_input, mtime_t i_control_date )
1501 int i_ret = VLC_SUCCESS;
1502 int i_state = PAUSE_S;
1504 if( p_input->p->b_can_pause )
1506 if( p_input->p->input.p_access )
1507 i_ret = access_Control( p_input->p->input.p_access,
1508 ACCESS_SET_PAUSE_STATE, true );
1510 i_ret = demux_Control( p_input->p->input.p_demux,
1511 DEMUX_SET_PAUSE_STATE, true );
1515 msg_Warn( p_input, "cannot set pause state" );
1516 i_state = p_input->i_state;
1523 i_ret = es_out_SetPauseState( p_input->p->p_es_out, p_input->p->b_can_pause, true, i_control_date );
1526 msg_Warn( p_input, "cannot set pause state at es_out level" );
1527 i_state = p_input->i_state;
1531 /* Switch to new state */
1532 input_ChangeStateWithVarCallback( p_input, i_state, false );
1536 static void ControlUnpause( input_thread_t *p_input, mtime_t i_control_date )
1538 int i_ret = VLC_SUCCESS;
1540 if( p_input->p->b_can_pause )
1542 if( p_input->p->input.p_access )
1543 i_ret = access_Control( p_input->p->input.p_access,
1544 ACCESS_SET_PAUSE_STATE, false );
1546 i_ret = demux_Control( p_input->p->input.p_demux,
1547 DEMUX_SET_PAUSE_STATE, false );
1550 /* FIXME What to do ? */
1551 msg_Warn( p_input, "cannot unset pause -> EOF" );
1552 vlc_mutex_unlock( &p_input->p->lock_control );
1553 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1554 vlc_mutex_lock( &p_input->p->lock_control );
1558 /* Switch to play */
1559 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1563 es_out_SetPauseState( p_input->p->p_es_out, false, false, i_control_date );
1566 static bool Control( input_thread_t *p_input, int i_type,
1569 const mtime_t i_control_date = mdate();
1570 bool b_force_update = false;
1573 return b_force_update;
1577 case INPUT_CONTROL_SET_DIE:
1578 msg_Dbg( p_input, "control: stopping input" );
1580 /* Mark all submodules to die */
1581 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1584 case INPUT_CONTROL_SET_POSITION:
1585 case INPUT_CONTROL_SET_POSITION_OFFSET:
1589 if( p_input->p->b_recording )
1591 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1594 if( i_type == INPUT_CONTROL_SET_POSITION )
1596 f_pos = val.f_float;
1600 /* Should not fail */
1601 demux_Control( p_input->p->input.p_demux,
1602 DEMUX_GET_POSITION, &f_pos );
1603 f_pos += val.f_float;
1605 if( f_pos < 0.0 ) f_pos = 0.0;
1606 if( f_pos > 1.0 ) f_pos = 1.0;
1607 /* Reset the decoders states and clock sync (before calling the demuxer */
1608 es_out_SetTime( p_input->p->p_es_out, -1 );
1609 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1612 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1613 "%2.1f%% failed", f_pos * 100 );
1617 if( p_input->p->i_slave > 0 )
1618 SlaveSeek( p_input );
1619 p_input->p->input.b_eof = false;
1621 b_force_update = true;
1626 case INPUT_CONTROL_SET_TIME:
1627 case INPUT_CONTROL_SET_TIME_OFFSET:
1632 if( p_input->p->b_recording )
1634 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1638 if( i_type == INPUT_CONTROL_SET_TIME )
1640 i_time = val.i_time;
1644 /* Should not fail */
1645 demux_Control( p_input->p->input.p_demux,
1646 DEMUX_GET_TIME, &i_time );
1647 i_time += val.i_time;
1649 if( i_time < 0 ) i_time = 0;
1651 /* Reset the decoders states and clock sync (before calling the demuxer */
1652 es_out_SetTime( p_input->p->p_es_out, -1 );
1654 i_ret = demux_Control( p_input->p->input.p_demux,
1655 DEMUX_SET_TIME, i_time );
1660 /* Emulate it with a SET_POS */
1661 demux_Control( p_input->p->input.p_demux,
1662 DEMUX_GET_LENGTH, &i_length );
1665 double f_pos = (double)i_time / (double)i_length;
1666 i_ret = demux_Control( p_input->p->input.p_demux,
1667 DEMUX_SET_POSITION, f_pos );
1672 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1673 " failed or not possible", i_time );
1677 if( p_input->p->i_slave > 0 )
1678 SlaveSeek( p_input );
1679 p_input->p->input.b_eof = false;
1681 b_force_update = true;
1686 case INPUT_CONTROL_SET_STATE:
1687 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1688 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1690 ControlUnpause( p_input, i_control_date );
1692 b_force_update = true;
1694 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S /* &&
1695 p_input->p->b_can_pause */ )
1697 ControlPause( p_input, i_control_date );
1699 b_force_update = true;
1701 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause && 0 )
1703 b_force_update = true;
1705 /* Correct "state" value */
1706 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1708 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1710 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1714 case INPUT_CONTROL_SET_RATE:
1715 case INPUT_CONTROL_SET_RATE_SLOWER:
1716 case INPUT_CONTROL_SET_RATE_FASTER:
1721 /* Get rate and direction */
1722 if( i_type == INPUT_CONTROL_SET_RATE )
1724 i_rate = abs( val.i_int );
1725 i_rate_sign = val.i_int < 0 ? -1 : 1;
1729 static const int ppi_factor[][2] = {
1730 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1732 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1739 i_rate_sign = p_input->p->i_rate < 0 ? -1 : 1;
1743 for( i = 0; ppi_factor[i][0] != 0; i++ )
1745 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1746 const int i_test_e = abs( abs( p_input->p->i_rate ) - i_test_r );
1747 if( i_test_e < i_error )
1754 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1756 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1758 if( ppi_factor[i_idx+1][0] > 0 )
1759 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1761 i_rate = INPUT_RATE_MAX+1;
1765 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1767 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1769 i_rate = INPUT_RATE_MIN-1;
1773 /* Check rate bound */
1774 if( i_rate < INPUT_RATE_MIN )
1776 msg_Dbg( p_input, "cannot set rate faster" );
1777 i_rate = INPUT_RATE_MIN;
1779 else if( i_rate > INPUT_RATE_MAX )
1781 msg_Dbg( p_input, "cannot set rate slower" );
1782 i_rate = INPUT_RATE_MAX;
1785 /* Apply direction */
1786 if( i_rate_sign < 0 )
1788 if( p_input->p->input.b_rescale_ts )
1790 msg_Dbg( p_input, "cannot set negative rate" );
1791 i_rate = p_input->p->i_rate;
1792 assert( i_rate > 0 );
1796 i_rate *= i_rate_sign;
1800 if( i_rate != INPUT_RATE_DEFAULT &&
1801 ( ( !p_input->p->b_can_rate_control && !p_input->p->input.b_rescale_ts ) ||
1802 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1804 msg_Dbg( p_input, "cannot change rate" );
1805 i_rate = INPUT_RATE_DEFAULT;
1807 if( i_rate != p_input->p->i_rate &&
1808 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1811 if( p_input->p->input.p_access )
1813 i_ret = VLC_EGENERIC;
1817 if( !p_input->p->input.b_rescale_ts )
1818 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1820 i_ret = demux_Control( p_input->p->input.p_demux,
1821 DEMUX_SET_RATE, &i_rate );
1825 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1826 i_rate = p_input->p->i_rate;
1831 if( i_rate != p_input->p->i_rate )
1834 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1835 var_SetBool( p_input, "rate-change", true );
1837 p_input->p->i_rate = i_rate;
1839 if( p_input->p->input.b_rescale_ts )
1841 const int i_rate_source = (p_input->b_can_pace_control || p_input->p->b_can_rate_control ) ? i_rate : INPUT_RATE_DEFAULT;
1842 es_out_SetRate( p_input->p->p_es_out, i_rate_source, i_rate );
1845 b_force_update = true;
1850 case INPUT_CONTROL_SET_PROGRAM:
1851 /* No need to force update, es_out does it if needed */
1852 es_out_Control( p_input->p->p_es_out,
1853 ES_OUT_SET_GROUP, val.i_int );
1855 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1859 case INPUT_CONTROL_SET_ES:
1860 /* No need to force update, es_out does it if needed */
1861 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, val.i_int );
1864 case INPUT_CONTROL_RESTART_ES:
1865 es_out_Control( p_input->p->p_es_out_display, ES_OUT_RESTART_ES_BY_ID, val.i_int );
1868 case INPUT_CONTROL_SET_AUDIO_DELAY:
1869 if( !es_out_SetDelay( p_input->p->p_es_out_display, AUDIO_ES, val.i_time ) )
1870 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1873 case INPUT_CONTROL_SET_SPU_DELAY:
1874 if( !es_out_SetDelay( p_input->p->p_es_out_display, SPU_ES, val.i_time ) )
1875 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1878 case INPUT_CONTROL_SET_TITLE:
1879 case INPUT_CONTROL_SET_TITLE_NEXT:
1880 case INPUT_CONTROL_SET_TITLE_PREV:
1881 if( p_input->p->b_recording )
1883 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1886 if( p_input->p->input.b_title_demux &&
1887 p_input->p->input.i_title > 0 )
1890 /* FIXME handle demux title */
1891 demux_t *p_demux = p_input->p->input.p_demux;
1894 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1895 i_title = p_demux->info.i_title - 1;
1896 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1897 i_title = p_demux->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 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1906 input_ControlVarTitle( p_input, i_title );
1909 else if( p_input->p->input.i_title > 0 )
1911 access_t *p_access = p_input->p->input.p_access;
1914 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1915 i_title = p_access->info.i_title - 1;
1916 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1917 i_title = p_access->info.i_title + 1;
1919 i_title = val.i_int;
1921 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1923 es_out_SetTime( p_input->p->p_es_out, -1 );
1925 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1926 stream_AccessReset( p_input->p->input.p_stream );
1930 case INPUT_CONTROL_SET_SEEKPOINT:
1931 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1932 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1933 if( p_input->p->b_recording )
1935 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1939 if( p_input->p->input.b_title_demux &&
1940 p_input->p->input.i_title > 0 )
1942 demux_t *p_demux = p_input->p->input.p_demux;
1944 int64_t i_input_time;
1945 int64_t i_seekpoint_time;
1947 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1949 i_seekpoint = p_demux->info.i_seekpoint;
1950 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1951 if( i_seekpoint_time >= 0 &&
1952 !demux_Control( p_demux,
1953 DEMUX_GET_TIME, &i_input_time ) )
1955 if( i_input_time < i_seekpoint_time + 3000000 )
1961 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1962 i_seekpoint = p_demux->info.i_seekpoint + 1;
1964 i_seekpoint = val.i_int;
1966 if( i_seekpoint >= 0 && i_seekpoint <
1967 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1970 es_out_SetTime( p_input->p->p_es_out, -1 );
1972 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1975 else if( p_input->p->input.i_title > 0 )
1977 demux_t *p_demux = p_input->p->input.p_demux;
1978 access_t *p_access = p_input->p->input.p_access;
1980 int64_t i_input_time;
1981 int64_t i_seekpoint_time;
1983 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1985 i_seekpoint = p_access->info.i_seekpoint;
1986 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1987 if( i_seekpoint_time >= 0 &&
1988 demux_Control( p_demux,
1989 DEMUX_GET_TIME, &i_input_time ) )
1991 if( i_input_time < i_seekpoint_time + 3000000 )
1997 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1998 i_seekpoint = p_access->info.i_seekpoint + 1;
2000 i_seekpoint = val.i_int;
2002 if( i_seekpoint >= 0 && i_seekpoint <
2003 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
2005 es_out_SetTime( p_input->p->p_es_out, -1 );
2007 access_Control( p_access, ACCESS_SET_SEEKPOINT,
2009 stream_AccessReset( p_input->p->input.p_stream );
2014 case INPUT_CONTROL_ADD_SUBTITLE:
2015 if( val.psz_string )
2017 SubtitleAdd( p_input, val.psz_string, true );
2018 free( val.psz_string );
2022 case INPUT_CONTROL_ADD_SLAVE:
2023 if( val.psz_string )
2025 input_source_t *slave = InputSourceNew( p_input );
2027 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
2033 msg_Dbg( p_input, "adding %s as slave on the fly",
2037 if( demux_Control( p_input->p->input.p_demux,
2038 DEMUX_GET_TIME, &i_time ) )
2040 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2041 InputSourceClean( slave );
2045 if( demux_Control( slave->p_demux,
2046 DEMUX_SET_TIME, i_time ) )
2048 msg_Err( p_input, "seek failed for new slave" );
2049 InputSourceClean( slave );
2054 /* Get meta (access and demux) */
2055 p_meta = vlc_meta_New();
2056 access_Control( slave->p_access, ACCESS_GET_META,
2058 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
2059 InputUpdateMeta( p_input, p_meta );
2061 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2066 msg_Warn( p_input, "failed to add %s as slave",
2070 free( val.psz_string );
2074 case INPUT_CONTROL_SET_RECORD_STATE:
2075 if( !!p_input->p->b_recording != !!val.b_bool )
2077 if( p_input->p->input.b_can_stream_record )
2079 if( demux_Control( p_input->p->input.p_demux,
2080 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2085 if( es_out_SetRecordState( p_input->p->p_es_out_display, val.b_bool ) )
2088 p_input->p->b_recording = val.b_bool;
2090 var_Change( p_input, "record", VLC_VAR_SETVALUE, &val, NULL );
2092 b_force_update = true;
2096 case INPUT_CONTROL_SET_FRAME_NEXT:
2097 if( p_input->i_state == PAUSE_S )
2099 es_out_SetFrameNext( p_input->p->p_es_out );
2101 else if( p_input->i_state == PLAYING_S )
2103 ControlPause( p_input, i_control_date );
2107 msg_Err( p_input, "invalid state for frame next" );
2109 b_force_update = true;
2112 case INPUT_CONTROL_SET_BOOKMARK:
2114 msg_Err( p_input, "not yet implemented" );
2118 return b_force_update;
2121 /*****************************************************************************
2123 *****************************************************************************/
2124 static int UpdateFromDemux( input_thread_t *p_input )
2126 demux_t *p_demux = p_input->p->input.p_demux;
2129 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2131 v.i_int = p_demux->info.i_title;
2132 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2134 input_ControlVarTitle( p_input, p_demux->info.i_title );
2136 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2138 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2140 v.i_int = p_demux->info.i_seekpoint;
2141 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2143 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2145 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2147 /* Hmmm only works with master input */
2148 if( p_input->p->input.p_demux == p_demux )
2150 int i_title_end = p_input->p->input.i_title_end -
2151 p_input->p->input.i_title_offset;
2152 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2153 p_input->p->input.i_seekpoint_offset;
2155 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2157 if( p_demux->info.i_title > i_title_end ||
2158 ( p_demux->info.i_title == i_title_end &&
2159 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2161 else if( i_seekpoint_end >=0 )
2163 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2165 else if( i_title_end >= 0 )
2167 if( p_demux->info.i_title > i_title_end ) return 0;
2174 /*****************************************************************************
2176 *****************************************************************************/
2177 static int UpdateFromAccess( input_thread_t *p_input )
2179 access_t *p_access = p_input->p->input.p_access;
2182 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2184 v.i_int = p_access->info.i_title;
2185 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2187 input_ControlVarTitle( p_input, p_access->info.i_title );
2189 stream_AccessUpdate( p_input->p->input.p_stream );
2191 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2193 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2195 v.i_int = p_access->info.i_seekpoint;
2196 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2197 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2199 if( p_access->info.i_update & INPUT_UPDATE_META )
2201 /* TODO maybe multi - access ? */
2202 vlc_meta_t *p_meta = vlc_meta_New();
2203 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2204 InputUpdateMeta( p_input, p_meta );
2205 p_access->info.i_update &= ~INPUT_UPDATE_META;
2207 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2212 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2213 f_quality = f_strength = -1;
2215 var_SetFloat( p_input, "signal-quality", f_quality );
2216 var_SetFloat( p_input, "signal-strength", f_strength );
2218 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2221 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2223 /* Hmmm only works with master input */
2224 if( p_input->p->input.p_access == p_access )
2226 int i_title_end = p_input->p->input.i_title_end -
2227 p_input->p->input.i_title_offset;
2228 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2229 p_input->p->input.i_seekpoint_offset;
2231 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2233 if( p_access->info.i_title > i_title_end ||
2234 ( p_access->info.i_title == i_title_end &&
2235 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2237 else if( i_seekpoint_end >=0 )
2239 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2241 else if( i_title_end >= 0 )
2243 if( p_access->info.i_title > i_title_end ) return 0;
2250 /*****************************************************************************
2252 *****************************************************************************/
2253 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2255 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2258 /*****************************************************************************
2260 *****************************************************************************/
2261 static input_source_t *InputSourceNew( input_thread_t *p_input )
2263 VLC_UNUSED(p_input);
2264 input_source_t *in = malloc( sizeof( input_source_t ) );
2266 memset( in, 0, sizeof( input_source_t ) );
2270 /*****************************************************************************
2272 *****************************************************************************/
2273 static int InputSourceInit( input_thread_t *p_input,
2274 input_source_t *in, const char *psz_mrl,
2275 const char *psz_forced_demux )
2277 const bool b_master = in == &p_input->p->input;
2279 char psz_dup[strlen(psz_mrl) + 1];
2280 const char *psz_access;
2281 const char *psz_demux;
2288 strcpy( psz_dup, psz_mrl );
2290 if( !in ) return VLC_EGENERIC;
2291 if( !p_input ) return VLC_EGENERIC;
2294 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2296 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2297 psz_mrl, psz_access, psz_demux, psz_path );
2298 if( !p_input->b_preparsing )
2300 /* Hack to allow udp://@:port syntax */
2302 (strncmp( psz_access, "udp", 3 ) &&
2303 strncmp( psz_access, "rtp", 3 )) )
2305 /* Find optional titles and seekpoints */
2306 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2307 &in->i_seekpoint_start, &in->i_seekpoint_end );
2310 if( psz_forced_demux && *psz_forced_demux )
2312 psz_demux = psz_forced_demux;
2314 else if( *psz_demux == '\0' )
2316 /* special hack for forcing a demuxer with --demux=module
2317 * (and do nothing with a list) */
2318 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2320 if( psz_var_demux != NULL &&
2321 !strchr(psz_var_demux, ',' ) &&
2322 !strchr(psz_var_demux, ':' ) )
2324 psz_demux = psz_var_demux;
2326 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2330 /* Try access_demux first */
2331 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2332 NULL, p_input->p->p_es_out, false );
2336 /* Preparsing is only for file:// */
2339 if( !*psz_access ) /* path without scheme:// */
2340 psz_access = "file";
2341 if( strcmp( psz_access, "file" ) )
2343 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2348 int64_t i_pts_delay;
2350 /* Get infos from access_demux */
2351 demux_Control( in->p_demux,
2352 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2353 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2355 in->b_title_demux = true;
2356 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2357 &in->title, &in->i_title,
2358 &in->i_title_offset, &in->i_seekpoint_offset ) )
2360 TAB_INIT( in->i_title, in->title );
2362 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2363 &in->b_can_pace_control ) )
2364 in->b_can_pace_control = false;
2366 if( !in->b_can_pace_control )
2368 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2369 &in->b_can_rate_control, &in->b_rescale_ts ) )
2371 in->b_can_rate_control = false;
2372 in->b_rescale_ts = true; /* not used */
2377 in->b_can_rate_control = true;
2378 in->b_rescale_ts = true;
2380 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2381 &in->b_can_pause ) )
2382 in->b_can_pause = false;
2383 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2384 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2385 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2387 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2389 if( ret != VLC_SUCCESS )
2391 var_Set( p_input, "seekable", val );
2395 int64_t i_pts_delay;
2398 input_ChangeState( p_input, OPENING_S );
2400 /* Now try a real access */
2401 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2403 /* Access failed, URL encoded ? */
2404 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2406 decode_URI( psz_path );
2408 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2409 psz_access, psz_demux, psz_path );
2411 in->p_access = access_New( p_input,
2412 psz_access, psz_demux, psz_path );
2414 if( in->p_access == NULL )
2416 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2418 intf_UserFatal( VLC_OBJECT( p_input), false,
2419 _("Your input can't be opened"),
2420 _("VLC is unable to open the MRL '%s'."
2421 " Check the log for details."), psz_mrl );
2426 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2427 while( psz && *psz )
2429 access_t *p_access = in->p_access;
2430 char *end = strchr( psz, ':' );
2435 in->p_access = access_FilterNew( in->p_access, psz );
2436 if( in->p_access == NULL )
2438 in->p_access = p_access;
2439 msg_Warn( p_input, "failed to insert access filter %s",
2447 /* Get infos from access */
2448 if( !p_input->b_preparsing )
2450 access_Control( in->p_access,
2451 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2452 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2454 in->b_title_demux = false;
2455 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2456 &in->title, &in->i_title,
2457 &in->i_title_offset, &in->i_seekpoint_offset ) )
2460 TAB_INIT( in->i_title, in->title );
2462 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2463 &in->b_can_pace_control );
2464 in->b_can_rate_control = in->b_can_pace_control;
2465 in->b_rescale_ts = true;
2467 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2469 var_SetBool( p_input, "can-pause", in->b_can_pause || !in->b_can_pace_control ); /* XXX temporary because of es_out_timeshift*/
2470 var_SetBool( p_input, "can-rate", !in->b_can_pace_control || in->b_can_rate_control ); /* XXX temporary because of es_out_timeshift*/
2471 var_SetBool( p_input, "can-rewind", !in->b_rescale_ts && !in->b_can_pace_control );
2473 access_Control( in->p_access, ACCESS_CAN_SEEK,
2475 var_Set( p_input, "seekable", val );
2479 input_ChangeState( p_input, BUFFERING_S );
2481 /* Autodetect extra files if none specified */
2482 char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2483 if( !psz_input_list )
2485 char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2486 if( psz_extra_files )
2487 var_SetString( p_input, "input-list", psz_extra_files );
2488 free( psz_extra_files );
2491 /* Create the stream_t */
2492 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2494 /* Restor old value */
2495 if( !psz_input_list )
2496 var_SetString( p_input, "input-list", "" );
2497 free( psz_input_list );
2499 if( in->p_stream == NULL )
2501 msg_Warn( p_input, "cannot create a stream_t from access" );
2505 /* Open a demuxer */
2506 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2508 psz_demux = in->p_access->psz_demux;
2512 /* Take access redirections into account */
2513 char *psz_real_path;
2514 char *psz_buf = NULL;
2515 if( in->p_access->psz_path )
2517 const char *psz_a, *psz_d;
2518 psz_buf = strdup( in->p_access->psz_path );
2519 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2523 psz_real_path = psz_path;
2525 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2527 in->p_stream, p_input->p->p_es_out,
2528 p_input->b_preparsing );
2532 if( in->p_demux == NULL )
2534 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2535 psz_access, psz_demux, psz_path );
2536 intf_UserFatal( VLC_OBJECT( p_input ), false,
2537 _("VLC can't recognize the input's format"),
2538 _("The format of '%s' cannot be detected. "
2539 "Have a look at the log for details."), psz_mrl );
2543 /* Get title from demux */
2544 if( !p_input->b_preparsing && in->i_title <= 0 )
2546 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2547 &in->title, &in->i_title,
2548 &in->i_title_offset, &in->i_seekpoint_offset ))
2550 TAB_INIT( in->i_title, in->title );
2554 in->b_title_demux = true;
2559 /* Set record capabilities */
2560 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2561 in->b_can_stream_record = false;
2563 if( !var_CreateGetBool( p_input, "input-record-native" ) )
2564 in->b_can_stream_record = false;
2565 var_SetBool( p_input, "can-record", true );
2567 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2571 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2572 if( 1 || !p_input->b_preparsing )
2575 input_attachment_t **attachment;
2576 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2577 &attachment, &i_attachment ) )
2579 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2580 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2581 i_attachment, attachment );
2582 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2585 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2587 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2589 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2592 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2593 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2599 input_ChangeState( p_input, ERROR_S );
2602 demux_Delete( in->p_demux );
2605 stream_Delete( in->p_stream );
2608 access_Delete( in->p_access );
2610 return VLC_EGENERIC;
2613 /*****************************************************************************
2615 *****************************************************************************/
2616 static void InputSourceClean( input_source_t *in )
2621 demux_Delete( in->p_demux );
2624 stream_Delete( in->p_stream );
2627 access_Delete( in->p_access );
2629 if( in->i_title > 0 )
2631 for( i = 0; i < in->i_title; i++ )
2632 vlc_input_title_Delete( in->title[i] );
2633 TAB_CLEAN( in->i_title, in->title );
2637 static void SlaveDemux( input_thread_t *p_input )
2641 bool b_set_time = true;
2643 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2645 /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2649 for( i = 0; i < p_input->p->i_slave; i++ )
2651 input_source_t *in = p_input->p->slave[i];
2657 if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2662 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2664 msg_Err( p_input, "slave[%d] doesn't like "
2665 "DEMUX_GET_TIME -> EOF", i );
2670 if( i_stime >= i_time )
2673 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2679 i_ret = in->p_demux->pf_demux( in->p_demux );
2684 msg_Dbg( p_input, "slave %d EOF", i );
2690 static void SlaveSeek( input_thread_t *p_input )
2695 if( !p_input ) return;
2697 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2699 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2703 for( i = 0; i < p_input->p->i_slave; i++ )
2705 input_source_t *in = p_input->p->slave[i];
2707 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2710 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2720 /*****************************************************************************
2722 *****************************************************************************/
2723 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2727 if( !p_meta ) return;
2729 /* Get meta information from user */
2730 #define GET_META( field, s ) \
2731 var_Get( p_input, (s), &val ); \
2732 if( *val.psz_string ) \
2733 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2734 free( val.psz_string )
2736 GET_META( Title, "meta-title" );
2737 GET_META( Artist, "meta-artist" );
2738 GET_META( Genre, "meta-genre" );
2739 GET_META( Copyright, "meta-copyright" );
2740 GET_META( Description, "meta-description" );
2741 GET_META( Date, "meta-date" );
2742 GET_META( URL, "meta-url" );
2746 /*****************************************************************************
2747 * InputGetExtraFiles
2748 * Autodetect extra input list
2749 *****************************************************************************/
2750 static char *InputGetExtraFiles( input_thread_t *p_input,
2751 const char *psz_access, const char *psz_path )
2753 char *psz_list = NULL;
2755 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2759 const char *psz_ext = strrchr( psz_path, '.' );
2760 if( !psz_ext || strcmp( psz_ext, ".001" ) )
2763 char *psz_file = strdup( psz_path );
2767 /* Try to list .xyz files */
2768 for( int i = 2; i < 999; i++ )
2770 char *psz_ext = strrchr( psz_file, '.' );
2773 snprintf( psz_ext, 5, ".%.3d", i );
2775 if( utf8_stat( psz_file, &st )
2776 || !S_ISREG( st.st_mode ) || !st.st_size )
2779 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2783 char *psz_old = psz_list;
2784 /* FIXME how to handle file with ',' ?*/
2785 if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2793 psz_list = strdup( psz_file );
2801 /*****************************************************************************
2802 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2803 * arturl and locking issue.
2804 *****************************************************************************/
2805 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2807 input_item_t *p_item = p_input->p->input.p_item;
2808 char * psz_arturl = NULL;
2809 char *psz_title = NULL;
2810 int i_arturl_event = false;
2815 psz_arturl = input_item_GetArtURL( p_item );
2817 vlc_mutex_lock( &p_item->lock );
2818 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2819 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2821 vlc_meta_Merge( p_item->p_meta, p_meta );
2823 if( psz_arturl && *psz_arturl )
2825 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2826 i_arturl_event = true;
2829 vlc_meta_Delete( p_meta );
2831 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2833 /* Don't look for art cover if sout
2834 * XXX It can change when sout has meta data support */
2835 if( p_input->p->p_sout && !p_input->b_preparsing )
2837 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2838 i_arturl_event = true;
2842 input_ExtractAttachmentAndCacheArt( p_input );
2848 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2850 p_meta = vlc_meta_New();
2851 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2853 vlc_mutex_unlock( &p_item->lock );
2855 input_item_SetPreparsed( p_item, true );
2857 if( i_arturl_event == true )
2861 /* Notify interested third parties */
2862 event.type = vlc_InputItemMetaChanged;
2863 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2864 vlc_event_send( &p_item->event_manager, &event );
2869 input_Control( p_input, INPUT_SET_NAME, psz_title );
2873 /** \todo handle sout meta */
2877 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2878 int i_new, input_attachment_t **pp_new )
2880 int i_attachment = *pi_attachment;
2881 input_attachment_t **attachment = *ppp_attachment;
2884 attachment = realloc( attachment,
2885 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2886 for( i = 0; i < i_new; i++ )
2887 attachment[i_attachment++] = pp_new[i];
2891 *pi_attachment = i_attachment;
2892 *ppp_attachment = attachment;
2895 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2899 if( p_input->b_preparsing )
2902 if( p_input->p->input.p_access )
2903 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2906 /* Get meta data from slave input */
2907 for( i = 0; i < p_input->p->i_slave; i++ )
2909 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2910 if( p_input->p->slave[i]->p_access )
2912 access_Control( p_input->p->slave[i]->p_access,
2913 ACCESS_GET_META, p_meta );
2918 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2925 /* XXX I am not sure it is a great idea, besides, there is more than that
2926 * if we want to do it right */
2927 vlc_mutex_lock( &p_item->lock );
2928 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2930 vlc_mutex_unlock( &p_item->lock );
2933 vlc_mutex_unlock( &p_item->lock );
2936 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2937 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2942 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2943 if(! p_demux->p_private )
2946 p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2949 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2951 if( p_demux_meta->p_meta )
2953 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2954 vlc_meta_Delete( p_demux_meta->p_meta );
2957 if( p_demux_meta->i_attachments > 0 )
2959 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2960 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2961 p_demux_meta->i_attachments, p_demux_meta->attachments );
2962 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2964 module_unneed( p_demux, p_id3 );
2966 free( p_demux->p_private );
2970 /*****************************************************************************
2971 * MRLSplit: parse the access, demux and url part of the
2972 * Media Resource Locator.
2973 *****************************************************************************/
2974 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2977 char *psz_access = NULL;
2978 char *psz_demux = NULL;
2981 /* Either there is an access/demux specification before ://
2982 * or we have a plain local file path. */
2983 psz_path = strstr( psz_dup, "://" );
2984 if( psz_path != NULL )
2987 psz_path += 3; /* skips "://" */
2989 /* Separate access from demux (<access>/<demux>://<path>) */
2990 psz_access = psz_dup;
2991 psz_demux = strchr( psz_access, '/' );
2993 *psz_demux++ = '\0';
2995 /* We really don't want module name substitution here! */
2996 if( psz_access[0] == '$' )
2998 if( psz_demux && psz_demux[0] == '$' )
3005 *ppsz_access = psz_access ? psz_access : (char*)"";
3006 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
3007 *ppsz_path = psz_path;
3010 static inline bool next(char ** src)
3014 long result = strtol( *src, &end, 0 );
3015 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
3024 /*****************************************************************************
3025 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
3028 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
3029 *****************************************************************************/
3030 static void MRLSections( input_thread_t *p_input, char *psz_source,
3031 int *pi_title_start, int *pi_title_end,
3032 int *pi_chapter_start, int *pi_chapter_end )
3034 char *psz, *psz_end, *psz_next, *psz_check;
3036 *pi_title_start = *pi_title_end = -1;
3037 *pi_chapter_start = *pi_chapter_end = -1;
3039 /* Start by parsing titles and chapters */
3040 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
3043 /* Check we are really dealing with a title/chapter section */
3044 psz_check = psz + 1;
3045 if( !*psz_check ) return;
3046 if( isdigit(*psz_check) )
3047 if(!next(&psz_check)) return;
3048 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
3049 if( *psz_check == ':' && ++psz_check )
3051 if( isdigit(*psz_check) )
3052 if(!next(&psz_check)) return;
3054 if( *psz_check != '-' && *psz_check ) return;
3055 if( *psz_check == '-' && ++psz_check )
3057 if( isdigit(*psz_check) )
3058 if(!next(&psz_check)) return;
3060 if( *psz_check != ':' && *psz_check ) return;
3061 if( *psz_check == ':' && ++psz_check )
3063 if( isdigit(*psz_check) )
3064 if(!next(&psz_check)) return;
3066 if( *psz_check ) return;
3068 /* Separate start and end */
3070 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
3072 /* Look for the start title */
3073 *pi_title_start = strtol( psz, &psz_next, 0 );
3074 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
3075 *pi_title_end = *pi_title_start;
3078 /* Look for the start chapter */
3080 *pi_chapter_start = strtol( psz, &psz_next, 0 );
3081 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
3082 *pi_chapter_end = *pi_chapter_start;
3086 /* Look for the end title */
3087 *pi_title_end = strtol( psz_end, &psz_next, 0 );
3088 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3091 /* Look for the end chapter */
3092 if( *psz_end ) psz_end++;
3093 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3094 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3097 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3098 psz_source, *pi_title_start, *pi_chapter_start,
3099 *pi_title_end, *pi_chapter_end );
3102 /*****************************************************************************
3103 * input_AddSubtitles: add a subtitles file and enable it
3104 *****************************************************************************/
3105 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3107 input_source_t *sub;
3110 char *psz_path, *psz_extension;
3112 /* if we are provided a subtitle.sub file,
3113 * see if we don't have a subtitle.idx and use it instead */
3114 psz_path = strdup( psz_subtitle );
3117 psz_extension = strrchr( psz_path, '.');
3118 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3122 strcpy( psz_extension, ".idx" );
3124 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3126 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3127 psz_path, psz_subtitle );
3128 strcpy( psz_subtitle, psz_path );
3134 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3136 sub = InputSourceNew( p_input );
3137 if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3142 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3145 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3147 if( count.i_int == 0 )
3149 /* if it was first one, there is disable too */
3151 if( count.i_int < list.p_list->i_count )
3153 const int i_id = list.p_list->p_values[count.i_int].i_int;
3155 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_DEFAULT_BY_ID, i_id );
3156 es_out_Control( p_input->p->p_es_out_display, ES_OUT_SET_ES_BY_ID, i_id );
3158 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3162 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3163 bool b_check_extension )
3167 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3170 assert( psz_subtitle != NULL );
3172 val.psz_string = strdup( psz_subtitle );
3173 if( val.psz_string )
3174 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3178 /*****************************************************************************
3179 * input_get_event_manager
3180 *****************************************************************************/
3181 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3183 return &p_input->p->event_manager;
3187 /* TODO FIXME nearly the same logic that snapshot code */
3188 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3193 path = utf8_opendir( psz_path );
3198 char *psz_tmp = str_format( p_obj, psz_prefix );
3202 filename_sanitize( psz_tmp );
3203 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3205 psz_extension ? "." : "",
3206 psz_extension ? psz_extension : "" ) < 0 )
3213 psz_file = str_format( p_obj, psz_path );
3214 path_sanitize( psz_file );