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 "../stream_output/stream_output.h"
43 #include <vlc_interface.h>
45 #include <vlc_charset.h>
46 #include <vlc_strings.h>
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
52 /*****************************************************************************
54 *****************************************************************************/
55 static void Destructor( input_thread_t * p_input );
57 static void* Run ( vlc_object_t *p_this );
58 static void* RunAndDestroy ( vlc_object_t *p_this );
60 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
61 const char *, bool, sout_instance_t * );
62 static int Init ( input_thread_t *p_input );
63 static void WaitDie ( input_thread_t *p_input );
64 static void End ( input_thread_t *p_input );
65 static void MainLoop( input_thread_t *p_input );
67 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline );
68 static void ControlReduce( input_thread_t * );
69 static bool Control( input_thread_t *, int, vlc_value_t );
71 static int UpdateFromAccess( input_thread_t * );
72 static int UpdateFromDemux( input_thread_t * );
74 static void UpdateItemLength( input_thread_t *, int64_t i_length );
76 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
78 static input_source_t *InputSourceNew( input_thread_t *);
79 static int InputSourceInit( input_thread_t *, input_source_t *,
80 const char *, const char *psz_forced_demux );
81 static void InputSourceClean( input_source_t * );
83 //static void InputGetAttachments( input_thread_t *, input_source_t * );
84 static void SlaveDemux( input_thread_t *p_input );
85 static void SlaveSeek( input_thread_t *p_input );
87 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
88 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
89 static char *InputGetExtraFiles( input_thread_t *p_input,
90 const char *psz_access, const char *psz_path );
92 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
93 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
94 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
95 int i_new, input_attachment_t **pp_new );
97 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced );
99 /*****************************************************************************
100 * This function creates a new input, and returns a pointer
101 * to its description. On error, it returns NULL.
103 * Variables for _public_ use:
106 * - rate,rate-slower, rate-faster
107 * - position, position-offset
108 * - time, time-offset
109 * - title,title-next,title-prev
110 * - chapter,chapter-next, chapter-prev
111 * - program, audio-es, video-es, spu-es
112 * - audio-delay, spu-delay
117 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
118 * or not, for that check position != 0.0)
120 * - can-record (if a stream can be recorded while playing)
121 * - teletext-es to get the index of spu track that is teletext --1 if no teletext)
122 * * For intf callback upon changes:
124 * - intf-change-vout for when a vout is created or destroyed
125 * - rate-change for when playback rate changes
126 * TODO explain when Callback is called
127 * TODO complete this list (?)
128 *****************************************************************************/
129 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
130 const char *psz_header, bool b_quick,
131 sout_instance_t *p_sout )
133 static const char input_name[] = "input";
134 input_thread_t *p_input = NULL; /* thread descriptor */
138 /* Allocate descriptor */
139 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
140 VLC_OBJECT_INPUT, input_name );
141 if( p_input == NULL )
144 /* Construct a nice name for the input timer */
145 char psz_timer_name[255];
146 char * psz_name = input_item_GetName( p_item );
147 snprintf( psz_timer_name, sizeof(psz_timer_name),
148 "input launching for '%s'", psz_name );
150 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
154 /* Start a timer to mesure how long it takes
155 * to launch an input */
156 stats_TimerStart( p_input, psz_timer_name,
157 STATS_TIMER_INPUT_LAUNCHING );
159 MALLOC_NULL( p_input->p, input_thread_private_t );
160 memset( p_input->p, 0, sizeof( input_thread_private_t ) );
162 /* One "randomly" selected input thread is responsible for computing
163 * the global stats. Check if there is already someone doing this */
164 if( p_input->p_libvlc->p_stats && !b_quick )
166 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
167 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
168 if( p_private->p_stats_computer == NULL )
169 p_private->p_stats_computer = p_input;
170 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
173 p_input->b_preparsing = b_quick;
174 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
177 vlc_event_manager_t * p_em = &p_input->p->event_manager;
178 vlc_event_manager_init_with_vlc_object( p_em, p_input );
179 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
180 vlc_event_manager_register_event_type( p_em, vlc_InputSelectedStreamChanged );
182 /* Init Common fields */
183 p_input->b_eof = false;
184 p_input->b_can_pace_control = true;
185 p_input->p->i_start = 0;
187 p_input->p->i_stop = 0;
188 p_input->p->i_run = 0;
189 p_input->p->i_title = 0;
190 p_input->p->title = NULL;
191 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
192 p_input->i_state = INIT_S;
193 p_input->p->i_rate = INPUT_RATE_DEFAULT;
194 p_input->p->b_recording = false;
195 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
196 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
197 p_input->p->p_es_out = NULL;
198 p_input->p->p_sout = NULL;
199 p_input->p->b_out_pace_control = false;
200 p_input->i_pts_delay = 0;
202 /* Init Input fields */
203 vlc_gc_incref( p_item ); /* Released in Destructor() */
204 p_input->p->input.p_item = p_item;
205 p_input->p->input.p_access = NULL;
206 p_input->p->input.p_stream = NULL;
207 p_input->p->input.p_demux = NULL;
208 p_input->p->input.b_title_demux = false;
209 p_input->p->input.i_title = 0;
210 p_input->p->input.title = NULL;
211 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
212 p_input->p->input.b_can_pace_control = true;
213 p_input->p->input.b_can_rate_control = true;
214 p_input->p->input.b_rescale_ts = true;
215 p_input->p->input.b_eof = false;
216 p_input->p->input.i_cr_average = 0;
218 vlc_mutex_lock( &p_item->lock );
220 if( !p_item->p_stats )
221 p_item->p_stats = stats_NewInputStats( p_input );
222 vlc_mutex_unlock( &p_item->lock );
225 p_input->p->i_slave = 0;
226 p_input->p->slave = NULL;
228 /* Init control buffer */
229 vlc_mutex_init( &p_input->p->lock_control );
230 vlc_cond_init( &p_input->p->wait_control );
231 p_input->p->i_control = 0;
233 /* Parse input options */
234 vlc_mutex_lock( &p_item->lock );
235 assert( (int)p_item->optflagc == p_item->i_options );
236 for( i = 0; i < p_item->i_options; i++ )
237 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
238 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
239 vlc_mutex_unlock( &p_item->lock );
241 /* Create Object Variables for private use only */
242 input_ConfigVarInit( p_input );
244 /* Create Objects variables for public Get and Set */
245 input_ControlVarInit( p_input );
248 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
250 if( !p_input->b_preparsing )
252 var_Get( p_input, "bookmarks", &val );
255 /* FIXME: have a common cfg parsing routine used by sout and others */
256 char *psz_parser, *psz_start, *psz_end;
257 psz_parser = val.psz_string;
258 while( (psz_start = strchr( psz_parser, '{' ) ) )
260 seekpoint_t *p_seekpoint;
263 psz_end = strchr( psz_start, '}' );
264 if( !psz_end ) break;
265 psz_parser = psz_end + 1;
266 backup = *psz_parser;
270 p_seekpoint = vlc_seekpoint_New();
271 while( (psz_end = strchr( psz_start, ',' ) ) )
274 if( !strncmp( psz_start, "name=", 5 ) )
276 p_seekpoint->psz_name = strdup(psz_start + 5);
278 else if( !strncmp( psz_start, "bytes=", 6 ) )
280 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
282 else if( !strncmp( psz_start, "time=", 5 ) )
284 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
287 psz_start = psz_end + 1;
289 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
290 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
291 p_seekpoint->i_time_offset );
292 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
293 vlc_seekpoint_Delete( p_seekpoint );
294 *psz_parser = backup;
296 free( val.psz_string );
300 /* Remove 'Now playing' info as it is probably outdated */
301 input_item_SetNowPlaying( p_item, NULL );
304 if( p_input->b_preparsing )
305 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
309 p_input->p->p_sout = p_sout;
311 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
312 vlc_mutex_init( &p_input->p->counters.counters_lock );
314 /* Set the destructor when we are sure we are initialized */
315 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
317 /* Attach only once we are ready */
318 vlc_object_attach( p_input, p_parent );
324 * Input destructor (called when the object's refcount reaches 0).
326 static void Destructor( input_thread_t * p_input )
329 char * psz_name = input_item_GetName( p_input->p->input.p_item );
330 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
334 vlc_event_manager_fini( &p_input->p->event_manager );
336 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
337 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
339 if( p_input->p->p_sout )
340 sout_DeleteInstance( p_input->p->p_sout );
342 vlc_gc_decref( p_input->p->input.p_item );
344 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
346 vlc_cond_destroy( &p_input->p->wait_control );
347 vlc_mutex_destroy( &p_input->p->lock_control );
352 * Initialize an input thread and run it. You will need to monitor the
353 * thread to clean up after it is done
355 * \param p_parent a vlc_object
356 * \param p_item an input item
357 * \return a pointer to the spawned input thread
359 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
360 input_item_t *p_item )
362 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
366 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
367 input_item_t *p_item,
368 const char *psz_log, sout_instance_t *p_sout )
370 input_thread_t *p_input;
372 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
376 /* Create thread and wait for its readiness. */
377 if( vlc_thread_create( p_input, "input", Run,
378 VLC_THREAD_PRIORITY_INPUT, false ) )
380 input_ChangeState( p_input, ERROR_S );
381 msg_Err( p_input, "cannot create input thread" );
382 vlc_object_detach( p_input );
383 vlc_object_release( p_input );
391 * Initialize an input thread and run it. This thread will clean after itself,
392 * you can forget about it. It can work either in blocking or non-blocking mode
394 * \param p_parent a vlc_object
395 * \param p_item an input item
396 * \param b_block should we block until read is finished ?
397 * \return an error code, VLC_SUCCESS on success
399 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
402 input_thread_t *p_input;
404 p_input = Create( p_parent, p_item, NULL, false, NULL );
410 RunAndDestroy( VLC_OBJECT(p_input) );
415 if( vlc_thread_create( p_input, "input", RunAndDestroy,
416 VLC_THREAD_PRIORITY_INPUT, false ) )
418 input_ChangeState( p_input, ERROR_S );
419 msg_Err( p_input, "cannot create input thread" );
420 vlc_object_release( p_input );
428 * Initialize an input and initialize it to preparse the item
429 * This function is blocking. It will only accept to parse files
431 * \param p_parent a vlc_object_t
432 * \param p_item an input item
433 * \return VLC_SUCCESS or an error
435 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
437 input_thread_t *p_input;
439 /* Allocate descriptor */
440 p_input = Create( p_parent, p_item, NULL, true, NULL );
444 if( !Init( p_input ) )
447 vlc_object_detach( p_input );
448 vlc_object_release( p_input );
454 * Request a running input thread to stop and die
456 * \param the input thread to stop
458 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
463 if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
464 p_obj->i_object_type == VLC_OBJECT_AOUT ||
465 p_obj == VLC_OBJECT(p_input->p->p_sout) )
468 vlc_object_kill( p_obj );
470 p_list = vlc_list_children( p_obj );
471 for( i = 0; i < p_list->i_count; i++ )
472 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
473 vlc_list_release( p_list );
475 void input_StopThread( input_thread_t *p_input )
477 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
478 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
479 * unlock the control loop */
480 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
482 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
485 sout_instance_t * input_DetachSout( input_thread_t *p_input )
487 sout_instance_t *p_sout = p_input->p->p_sout;
488 vlc_object_detach( p_sout );
489 p_input->p->p_sout = NULL;
493 /*****************************************************************************
494 * Run: main thread loop
495 * This is the "normal" thread that spawns the input processing chain,
496 * reads the stream, cleans up and waits
497 *****************************************************************************/
498 static void* Run( vlc_object_t *p_this )
500 input_thread_t *p_input = (input_thread_t *)p_this;
501 const int canc = vlc_savecancel();
503 if( Init( p_input ) )
505 /* If we failed, wait before we are killed, and exit */
512 /* Wait until we are asked to die */
513 if( !p_input->b_die )
520 p_input->b_dead = true;
521 vlc_restorecancel( canc );
525 /*****************************************************************************
526 * RunAndDestroy: main thread loop
527 * This is the "just forget me" thread that spawns the input processing chain,
528 * reads the stream, cleans up and releases memory
529 *****************************************************************************/
530 static void* RunAndDestroy( vlc_object_t *p_this )
532 input_thread_t *p_input = (input_thread_t *)p_this;
533 const int canc = vlc_savecancel();
535 if( Init( p_input ) )
545 vlc_object_release( p_input );
546 vlc_restorecancel( canc );
550 /*****************************************************************************
551 * Main loop: Fill buffers from access, and demux
552 *****************************************************************************/
556 * It asks the demuxer to demux some data
558 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t *pi_start_mdate )
564 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
565 ( p_input->p->i_run > 0 && *pi_start_mdate+p_input->p->i_run < mdate() ) )
568 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
573 if( p_input->p->input.b_title_demux &&
574 p_input->p->input.p_demux->info.i_update )
576 i_ret = UpdateFromDemux( p_input );
579 else if( !p_input->p->input.b_title_demux &&
580 p_input->p->input.p_access &&
581 p_input->p->input.p_access->info.i_update )
583 i_ret = UpdateFromAccess( p_input );
588 if( i_ret == 0 ) /* EOF */
592 var_Get( p_input, "input-repeat", &repeat );
593 if( repeat.i_int == 0 )
595 /* End of file - we do not set b_die because only the
596 * playlist is allowed to do so. */
597 msg_Dbg( p_input, "EOF reached" );
598 p_input->p->input.b_eof = true;
604 msg_Dbg( p_input, "repeating the same input (%d)",
606 if( repeat.i_int > 0 )
609 var_Set( p_input, "input-repeat", repeat );
612 /* Seek to start title/seekpoint */
613 val.i_int = p_input->p->input.i_title_start -
614 p_input->p->input.i_title_offset;
615 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
617 input_ControlPush( p_input,
618 INPUT_CONTROL_SET_TITLE, &val );
620 val.i_int = p_input->p->input.i_seekpoint_start -
621 p_input->p->input.i_seekpoint_offset;
622 if( val.i_int > 0 /* TODO: check upper boundary */ )
623 input_ControlPush( p_input,
624 INPUT_CONTROL_SET_SEEKPOINT, &val );
626 /* Seek to start position */
627 if( p_input->p->i_start > 0 )
629 val.i_time = p_input->p->i_start;
630 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
636 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
641 *pi_start_mdate = mdate();
646 input_ChangeState( p_input, ERROR_S );
649 if( i_ret > 0 && p_input->p->i_slave > 0 )
651 SlaveDemux( p_input );
657 * It update the variables used by the interfaces
659 static void MainLoopInterface( input_thread_t *p_input )
663 int64_t i_time, i_length;
665 /* update input status variables */
666 if( !demux_Control( p_input->p->input.p_demux,
667 DEMUX_GET_POSITION, &f_pos ) )
669 val.f_float = (float)f_pos;
670 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
672 if( !demux_Control( p_input->p->input.p_demux,
673 DEMUX_GET_TIME, &i_time ) )
675 p_input->i_time = i_time;
677 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
679 if( !demux_Control( p_input->p->input.p_demux,
680 DEMUX_GET_LENGTH, &i_length ) )
683 var_Get( p_input, "length", &old_val );
684 val.i_time = i_length;
685 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
687 if( old_val.i_time != val.i_time )
689 UpdateItemLength( p_input, i_length );
693 var_SetBool( p_input, "intf-change", true );
698 * It updates the globals statics
700 static void MainLoopStatistic( input_thread_t *p_input )
702 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
703 /* Are we the thread responsible for computing global stats ? */
704 if( libvlc_priv( p_input->p_libvlc )->p_stats_computer == p_input )
706 stats_ComputeGlobalStats( p_input->p_libvlc,
707 p_input->p_libvlc->p_stats );
713 * The main input loop.
715 static void MainLoop( input_thread_t *p_input )
717 mtime_t i_start_mdate = mdate();
718 mtime_t i_intf_update = 0;
719 mtime_t i_statistic_update = 0;
721 /* Start the timer */
722 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
724 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
735 b_force_update = false;
737 b_paused = p_input->i_state == PAUSE_S &&
738 !input_EsOutIsBuffering( p_input->p->p_es_out );
742 MainLoopDemux( p_input, &b_force_update, &i_start_mdate );
743 i_wakeup = input_EsOutGetWakeup( p_input->p->p_es_out );
748 i_deadline = i_wakeup;
750 i_deadline = __MIN( i_intf_update, i_statistic_update );
753 vlc_mutex_lock( &p_input->p->lock_control );
754 ControlReduce( p_input );
755 while( !ControlPopNoLock( p_input, &i_type, &val, i_deadline ) )
757 msg_Dbg( p_input, "control type=%d", i_type );
758 if( Control( p_input, i_type, val ) )
759 b_force_update = true;
761 vlc_mutex_unlock( &p_input->p->lock_control );
763 /* Update interface and statistics */
765 if( i_intf_update < i_current || b_force_update )
767 MainLoopInterface( p_input );
768 i_intf_update = i_current + INT64_C(250000);
769 b_force_update = false;
771 if( i_statistic_update < i_current )
773 MainLoopStatistic( p_input );
774 i_statistic_update = i_current + INT64_C(1000000);
777 /* Check if i_wakeup is still valid */
780 mtime_t i_new_wakeup = input_EsOutGetWakeup( p_input->p->p_es_out );
784 } while( i_current < i_wakeup );
787 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
789 /* We have finish to demux data but not to play them */
790 while( vlc_object_alive( p_input ) )
792 if( input_EsOutDecodersIsEmpty( p_input->p->p_es_out ) )
795 msg_Dbg( p_input, "waiting decoder fifos to empty" );
797 msleep( INPUT_IDLE_SLEEP );
800 /* We have finished */
801 input_ChangeState( p_input, END_S );
805 static void InitStatistics( input_thread_t * p_input )
807 if( p_input->b_preparsing ) return;
809 /* Prepare statistics */
810 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
811 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
812 if( libvlc_stats( p_input ) )
814 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
815 INIT_COUNTER( read_packets, INTEGER, COUNTER );
816 INIT_COUNTER( demux_read, INTEGER, COUNTER );
817 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
818 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
819 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
820 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
821 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
822 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
823 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
824 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
825 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
826 p_input->p->counters.p_sout_send_bitrate = NULL;
827 p_input->p->counters.p_sout_sent_packets = NULL;
828 p_input->p->counters.p_sout_sent_bytes = NULL;
829 if( p_input->p->counters.p_demux_bitrate )
830 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
831 if( p_input->p->counters.p_input_bitrate )
832 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
837 static int InitSout( input_thread_t * p_input )
841 if( p_input->b_preparsing ) return VLC_SUCCESS;
843 /* Find a usable sout and attach it to p_input */
844 psz = var_GetNonEmptyString( p_input, "sout" );
845 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
847 /* Check the validity of the provided sout */
848 if( p_input->p->p_sout )
850 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
852 msg_Dbg( p_input, "destroying unusable sout" );
854 sout_DeleteInstance( p_input->p->p_sout );
855 p_input->p->p_sout = NULL;
859 if( p_input->p->p_sout )
862 msg_Dbg( p_input, "sout keep: reusing sout" );
863 msg_Dbg( p_input, "sout keep: you probably want to use "
864 "gather stream_out" );
865 vlc_object_attach( p_input->p->p_sout, p_input );
869 /* Create a new one */
870 p_input->p->p_sout = sout_NewInstance( p_input, psz );
871 if( !p_input->p->p_sout )
873 input_ChangeState( p_input, ERROR_S );
874 msg_Err( p_input, "cannot start stream output instance, " \
880 if( libvlc_stats( p_input ) )
882 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
883 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
884 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
885 if( p_input->p->counters.p_sout_send_bitrate )
886 p_input->p->counters.p_sout_send_bitrate->update_interval =
890 else if( p_input->p->p_sout )
892 msg_Dbg( p_input, "destroying useless sout" );
894 sout_DeleteInstance( p_input->p->p_sout );
895 p_input->p->p_sout = NULL;
903 static void InitTitle( input_thread_t * p_input )
907 if( p_input->b_preparsing ) return;
909 /* Create global title (from master) */
910 p_input->p->i_title = p_input->p->input.i_title;
911 p_input->p->title = p_input->p->input.title;
912 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
913 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
914 if( p_input->p->i_title > 0 )
916 /* Setup variables */
917 input_ControlVarNavigation( p_input );
918 input_ControlVarTitle( p_input, 0 );
922 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
923 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
924 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
927 if( p_input->i_pts_delay < 0 )
928 p_input->i_pts_delay = 0;
930 /* If the desynchronisation requested by the user is < 0, we need to
931 * cache more data. */
932 var_Get( p_input, "audio-desync", &val );
934 p_input->i_pts_delay -= (val.i_int * 1000);
936 /* Update cr_average depending on the caching */
937 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
938 p_input->p->input.i_cr_average /= 10;
939 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
942 static void StartTitle( input_thread_t * p_input )
951 /* Start title/chapter */
953 if( p_input->b_preparsing )
955 p_input->p->i_start = 0;
959 val.i_int = p_input->p->input.i_title_start -
960 p_input->p->input.i_title_offset;
961 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
962 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
963 val.i_int = p_input->p->input.i_seekpoint_start -
964 p_input->p->input.i_seekpoint_offset;
965 if( val.i_int > 0 /* TODO: check upper boundary */ )
966 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
970 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
971 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
972 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
973 i_length = var_GetTime( p_input, "length" );
974 if( p_input->p->i_run < 0 )
976 msg_Warn( p_input, "invalid run-time ignored" );
977 p_input->p->i_run = 0;
980 if( p_input->p->i_start > 0 )
982 if( p_input->p->i_start >= i_length )
984 msg_Warn( p_input, "invalid start-time ignored" );
990 msg_Dbg( p_input, "starting at time: %ds",
991 (int)( p_input->p->i_start / INT64_C(1000000) ) );
993 s.i_time = p_input->p->i_start;
994 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
997 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
999 msg_Warn( p_input, "invalid stop-time ignored" );
1000 p_input->p->i_stop = 0;
1003 /* Load subtitles */
1004 /* Get fps and set it if not already set */
1005 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
1008 float f_requested_fps;
1010 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
1011 var_SetFloat( p_input, "sub-original-fps", f_fps );
1013 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1014 if( f_requested_fps != f_fps )
1016 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1017 VLC_VAR_DOINHERIT );
1018 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1022 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1025 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1028 /* Look for and add subtitle files */
1029 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1030 if( psz_subtitle != NULL )
1032 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1033 SubtitleAdd( p_input, psz_subtitle, true );
1036 var_Get( p_input, "sub-autodetect-file", &val );
1039 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1040 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1041 p_input->p->input.p_item->psz_uri );
1042 free( psz_autopath );
1044 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1046 /* Try to autoselect the first autodetected subtitles file
1047 * if no subtitles file was specified */
1048 bool b_forced = i == 0 && !psz_subtitle;
1050 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1051 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1053 free( ppsz_subs[i] );
1057 free( psz_subtitle );
1059 /* Look for slave */
1060 psz = var_GetNonEmptyString( p_input, "input-slave" );
1064 input_source_t *slave;
1065 while( psz && *psz )
1067 while( *psz == ' ' || *psz == '#' )
1071 if( ( psz_delim = strchr( psz, '#' ) ) )
1073 *psz_delim++ = '\0';
1080 msg_Dbg( p_input, "adding slave input '%s'", psz );
1081 slave = InputSourceNew( p_input );
1082 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1084 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1093 static void InitPrograms( input_thread_t * p_input )
1098 if( p_input->b_preparsing ) return;
1101 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1102 i_es_out_mode = ES_OUT_MODE_AUTO;
1104 if( p_input->p->p_sout )
1106 var_Get( p_input, "sout-all", &val );
1109 i_es_out_mode = ES_OUT_MODE_ALL;
1114 var_Get( p_input, "programs", &val );
1115 if( val.p_list && val.p_list->i_count )
1117 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1118 /* Note : we should remove the "program" callback. */
1122 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1127 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1129 /* Inform the demuxer about waited group (needed only for DVB) */
1130 if( i_es_out_mode == ES_OUT_MODE_ALL )
1132 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1134 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1136 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1141 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1142 (int) var_GetInteger( p_input, "program" ), NULL );
1146 static int Init( input_thread_t * p_input )
1152 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1154 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1156 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1157 var_SetString( p_input, "sout", "" );
1158 var_SetBool( p_input, "sout-all", false );
1159 var_SetString( p_input, "input-slave", "" );
1160 var_SetInteger( p_input, "input-repeat", 0 );
1161 var_SetString( p_input, "sub-file", "" );
1162 var_SetBool( p_input, "sub-autodetect-file", false );
1166 InitStatistics( p_input );
1168 ret = InitSout( p_input );
1169 if( ret != VLC_SUCCESS )
1170 return ret; /* FIXME: goto error; should be better here */
1174 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1175 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1176 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1178 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1179 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1181 if( InputSourceInit( p_input, &p_input->p->input,
1182 p_input->p->input.p_item->psz_uri, NULL ) )
1187 InitTitle( p_input );
1189 /* Load master infos */
1191 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1192 &val.i_time ) && val.i_time > 0 )
1194 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1195 UpdateItemLength( p_input, val.i_time );
1199 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1200 if( val.i_time > 0 )
1201 { /* fallback: gets length from metadata */
1202 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1203 UpdateItemLength( p_input, val.i_time );
1207 StartTitle( p_input );
1209 InitPrograms( p_input );
1211 if( !p_input->b_preparsing && p_input->p->p_sout )
1213 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1215 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1217 /* We don't want a high input priority here or we'll
1218 * end-up sucking up all the CPU time */
1219 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1222 msg_Dbg( p_input, "starting in %s mode",
1223 p_input->p->b_out_pace_control ? "async" : "sync" );
1226 p_meta = vlc_meta_New();
1228 /* Get meta data from users */
1229 InputMetaUser( p_input, p_meta );
1231 /* Get meta data from master input */
1232 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1234 /* Access_file does not give any meta, and there are no slave */
1235 AccessMeta( p_input, p_meta );
1237 InputUpdateMeta( p_input, p_meta );
1239 if( !p_input->b_preparsing )
1241 msg_Dbg( p_input, "`%s' successfully opened",
1242 p_input->p->input.p_item->psz_uri );
1246 /* initialization is complete */
1247 input_ChangeState( p_input, PLAYING_S );
1252 input_ChangeState( p_input, ERROR_S );
1254 if( p_input->p->p_es_out )
1255 input_EsOutDelete( p_input->p->p_es_out );
1257 if( p_input->p->p_sout )
1259 vlc_object_detach( p_input->p->p_sout );
1260 sout_DeleteInstance( p_input->p->p_sout );
1264 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1266 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1267 stats_CounterClean( p_input->p->counters.p_##c );\
1268 p_input->p->counters.p_##c = NULL; } while(0)
1269 EXIT_COUNTER( read_bytes );
1270 EXIT_COUNTER( read_packets );
1271 EXIT_COUNTER( demux_read );
1272 EXIT_COUNTER( input_bitrate );
1273 EXIT_COUNTER( demux_bitrate );
1274 EXIT_COUNTER( played_abuffers );
1275 EXIT_COUNTER( lost_abuffers );
1276 EXIT_COUNTER( displayed_pictures );
1277 EXIT_COUNTER( lost_pictures );
1278 EXIT_COUNTER( decoded_audio );
1279 EXIT_COUNTER( decoded_video );
1280 EXIT_COUNTER( decoded_sub );
1282 if( p_input->p->p_sout )
1284 EXIT_COUNTER( sout_sent_packets );
1285 EXIT_COUNTER( sout_sent_bytes );
1286 EXIT_COUNTER( sout_send_bitrate );
1291 /* Mark them deleted */
1292 p_input->p->input.p_demux = NULL;
1293 p_input->p->input.p_stream = NULL;
1294 p_input->p->input.p_access = NULL;
1295 p_input->p->p_es_out = NULL;
1296 p_input->p->p_sout = NULL;
1298 return VLC_EGENERIC;
1301 /*****************************************************************************
1302 * WaitDie: Wait until we are asked to die.
1303 *****************************************************************************
1304 * This function is called when an error occurred during thread main's loop.
1305 *****************************************************************************/
1306 static void WaitDie( input_thread_t *p_input )
1308 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1310 /* Wait a die order */
1311 vlc_object_lock( p_input );
1312 while( vlc_object_alive( p_input ) )
1313 vlc_object_wait( p_input );
1314 vlc_object_unlock( p_input );
1317 /*****************************************************************************
1318 * End: end the input thread
1319 *****************************************************************************/
1320 static void End( input_thread_t * p_input )
1324 /* We are at the end */
1325 input_ChangeState( p_input, END_S );
1327 /* Clean control variables */
1328 input_ControlVarStop( p_input );
1330 /* Clean up master */
1331 InputSourceClean( &p_input->p->input );
1334 for( i = 0; i < p_input->p->i_slave; i++ )
1336 InputSourceClean( p_input->p->slave[i] );
1337 free( p_input->p->slave[i] );
1339 free( p_input->p->slave );
1341 /* Unload all modules */
1342 if( p_input->p->p_es_out )
1343 input_EsOutDelete( p_input->p->p_es_out );
1345 if( !p_input->b_preparsing )
1347 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1348 if( libvlc_stats( p_input ) )
1350 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
1352 /* make sure we are up to date */
1353 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1354 if( p_private->p_stats_computer == p_input )
1356 stats_ComputeGlobalStats( p_input->p_libvlc,
1357 p_input->p_libvlc->p_stats );
1358 p_private->p_stats_computer = NULL;
1360 CL_CO( read_bytes );
1361 CL_CO( read_packets );
1362 CL_CO( demux_read );
1363 CL_CO( input_bitrate );
1364 CL_CO( demux_bitrate );
1365 CL_CO( played_abuffers );
1366 CL_CO( lost_abuffers );
1367 CL_CO( displayed_pictures );
1368 CL_CO( lost_pictures );
1369 CL_CO( decoded_audio) ;
1370 CL_CO( decoded_video );
1371 CL_CO( decoded_sub) ;
1374 /* Close optional stream output instance */
1375 if( p_input->p->p_sout )
1377 CL_CO( sout_sent_packets );
1378 CL_CO( sout_sent_bytes );
1379 CL_CO( sout_send_bitrate );
1381 vlc_object_detach( p_input->p->p_sout );
1386 if( p_input->p->i_attachment > 0 )
1388 for( i = 0; i < p_input->p->i_attachment; i++ )
1389 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1390 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1393 /* Tell we're dead */
1394 p_input->b_dead = true;
1397 /*****************************************************************************
1399 *****************************************************************************/
1400 static inline int ControlPopNoLock( input_thread_t *p_input,
1401 int *pi_type, vlc_value_t *p_val,
1402 mtime_t i_deadline )
1405 while( p_input->p->i_control <= 0 )
1407 if( !vlc_object_alive( p_input ) )
1408 return VLC_EGENERIC;
1410 if( i_deadline < 0 )
1411 return VLC_EGENERIC;
1413 if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1414 return VLC_EGENERIC;
1417 *pi_type = p_input->p->control[0].i_type;
1418 *p_val = p_input->p->control[0].val;
1420 p_input->p->i_control--;
1421 if( p_input->p->i_control > 0 )
1425 for( i = 0; i < p_input->p->i_control; i++ )
1427 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1428 p_input->p->control[i].val = p_input->p->control[i+1].val;
1435 static void ControlReduce( input_thread_t *p_input )
1442 for( i = 1; i < p_input->p->i_control; i++ )
1444 const int i_lt = p_input->p->control[i-1].i_type;
1445 const int i_ct = p_input->p->control[i].i_type;
1447 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1448 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1452 ( i_ct == INPUT_CONTROL_SET_STATE ||
1453 i_ct == INPUT_CONTROL_SET_RATE ||
1454 i_ct == INPUT_CONTROL_SET_POSITION ||
1455 i_ct == INPUT_CONTROL_SET_TIME ||
1456 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1457 i_ct == INPUT_CONTROL_SET_TITLE ||
1458 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1459 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1462 // msg_Dbg( p_input, "merged at %d", i );
1463 /* Remove the i-1 */
1464 for( j = i; j < p_input->p->i_control; j++ )
1465 p_input->p->control[j-1] = p_input->p->control[j];
1466 p_input->p->i_control--;
1470 /* TODO but that's not that important
1471 - merge SET_X with SET_X_CMD
1472 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1473 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1480 static bool Control( input_thread_t *p_input, int i_type,
1483 const mtime_t i_control_date = mdate();
1484 bool b_force_update = false;
1487 return b_force_update;
1491 case INPUT_CONTROL_SET_DIE:
1492 msg_Dbg( p_input, "control: stopping input" );
1494 /* Mark all submodules to die */
1495 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1498 case INPUT_CONTROL_SET_POSITION:
1499 case INPUT_CONTROL_SET_POSITION_OFFSET:
1503 if( p_input->p->b_recording )
1505 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1508 if( i_type == INPUT_CONTROL_SET_POSITION )
1510 f_pos = val.f_float;
1514 /* Should not fail */
1515 demux_Control( p_input->p->input.p_demux,
1516 DEMUX_GET_POSITION, &f_pos );
1517 f_pos += val.f_float;
1519 if( f_pos < 0.0 ) f_pos = 0.0;
1520 if( f_pos > 1.0 ) f_pos = 1.0;
1521 /* Reset the decoders states and clock sync (before calling the demuxer */
1522 input_EsOutChangePosition( p_input->p->p_es_out );
1523 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1526 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1527 "%2.1f%% failed", f_pos * 100 );
1531 if( p_input->p->i_slave > 0 )
1532 SlaveSeek( p_input );
1534 b_force_update = true;
1539 case INPUT_CONTROL_SET_TIME:
1540 case INPUT_CONTROL_SET_TIME_OFFSET:
1545 if( p_input->p->b_recording )
1547 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1551 if( i_type == INPUT_CONTROL_SET_TIME )
1553 i_time = val.i_time;
1557 /* Should not fail */
1558 demux_Control( p_input->p->input.p_demux,
1559 DEMUX_GET_TIME, &i_time );
1560 i_time += val.i_time;
1562 if( i_time < 0 ) i_time = 0;
1564 /* Reset the decoders states and clock sync (before calling the demuxer */
1565 input_EsOutChangePosition( p_input->p->p_es_out );
1567 i_ret = demux_Control( p_input->p->input.p_demux,
1568 DEMUX_SET_TIME, i_time );
1573 /* Emulate it with a SET_POS */
1574 demux_Control( p_input->p->input.p_demux,
1575 DEMUX_GET_LENGTH, &i_length );
1578 double f_pos = (double)i_time / (double)i_length;
1579 i_ret = demux_Control( p_input->p->input.p_demux,
1580 DEMUX_SET_POSITION, f_pos );
1585 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1586 " failed or not possible", i_time );
1590 if( p_input->p->i_slave > 0 )
1591 SlaveSeek( p_input );
1593 b_force_update = true;
1598 case INPUT_CONTROL_SET_STATE:
1599 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1600 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1603 if( p_input->p->input.p_access )
1604 i_ret = access_Control( p_input->p->input.p_access,
1605 ACCESS_SET_PAUSE_STATE, false );
1607 i_ret = demux_Control( p_input->p->input.p_demux,
1608 DEMUX_SET_PAUSE_STATE, false );
1612 /* FIXME What to do ? */
1613 msg_Warn( p_input, "cannot unset pause -> EOF" );
1614 vlc_mutex_unlock( &p_input->p->lock_control );
1615 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1616 vlc_mutex_lock( &p_input->p->lock_control );
1619 b_force_update = true;
1621 /* Switch to play */
1622 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1626 input_EsOutChangePause( p_input->p->p_es_out, false, i_control_date );
1628 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1629 p_input->p->b_can_pause )
1632 if( p_input->p->input.p_access )
1633 i_ret = access_Control( p_input->p->input.p_access,
1634 ACCESS_SET_PAUSE_STATE, true );
1636 i_ret = demux_Control( p_input->p->input.p_demux,
1637 DEMUX_SET_PAUSE_STATE, true );
1639 b_force_update = true;
1643 msg_Warn( p_input, "cannot set pause state" );
1644 state = p_input->i_state;
1651 /* Switch to new state */
1652 input_ChangeStateWithVarCallback( p_input, state, false );
1656 input_EsOutChangePause( p_input->p->p_es_out, true, i_control_date );
1658 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1660 b_force_update = true;
1662 /* Correct "state" value */
1663 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1665 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1667 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1671 case INPUT_CONTROL_SET_RATE:
1672 case INPUT_CONTROL_SET_RATE_SLOWER:
1673 case INPUT_CONTROL_SET_RATE_FASTER:
1677 if( i_type == INPUT_CONTROL_SET_RATE )
1683 static const int ppi_factor[][2] = {
1684 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1686 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1695 for( i = 0; ppi_factor[i][0] != 0; i++ )
1697 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1698 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1699 if( i_test_e < i_error )
1705 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1707 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1709 if( ppi_factor[i_idx+1][0] > 0 )
1710 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1712 i_rate = INPUT_RATE_MAX+1;
1716 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1718 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1720 i_rate = INPUT_RATE_MIN-1;
1724 if( i_rate < INPUT_RATE_MIN )
1726 msg_Dbg( p_input, "cannot set rate faster" );
1727 i_rate = INPUT_RATE_MIN;
1729 else if( i_rate > INPUT_RATE_MAX )
1731 msg_Dbg( p_input, "cannot set rate slower" );
1732 i_rate = INPUT_RATE_MAX;
1734 if( i_rate != INPUT_RATE_DEFAULT &&
1735 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1736 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1738 msg_Dbg( p_input, "cannot change rate" );
1739 i_rate = INPUT_RATE_DEFAULT;
1741 if( i_rate != p_input->p->i_rate &&
1742 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1745 if( p_input->p->input.p_access )
1746 i_ret = VLC_EGENERIC;
1748 i_ret = demux_Control( p_input->p->input.p_demux,
1749 DEMUX_SET_RATE, &i_rate );
1752 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1753 i_rate = p_input->p->i_rate;
1758 if( i_rate != p_input->p->i_rate )
1761 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1762 var_SetBool( p_input, "rate-change", true );
1764 p_input->p->i_rate = i_rate;
1766 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1767 if( p_input->p->input.b_rescale_ts )
1768 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1770 b_force_update = true;
1775 case INPUT_CONTROL_SET_PROGRAM:
1776 /* No need to force update, es_out does it if needed */
1777 es_out_Control( p_input->p->p_es_out,
1778 ES_OUT_SET_GROUP, val.i_int );
1780 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1784 case INPUT_CONTROL_SET_ES:
1785 /* No need to force update, es_out does it if needed */
1786 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1787 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1790 case INPUT_CONTROL_RESTART_ES:
1791 es_out_Control( p_input->p->p_es_out, ES_OUT_RESTART_ES,
1792 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1795 case INPUT_CONTROL_SET_AUDIO_DELAY:
1796 input_EsOutSetDelay( p_input->p->p_es_out,
1797 AUDIO_ES, val.i_time );
1798 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1801 case INPUT_CONTROL_SET_SPU_DELAY:
1802 input_EsOutSetDelay( p_input->p->p_es_out,
1803 SPU_ES, val.i_time );
1804 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1807 case INPUT_CONTROL_SET_TITLE:
1808 case INPUT_CONTROL_SET_TITLE_NEXT:
1809 case INPUT_CONTROL_SET_TITLE_PREV:
1810 if( p_input->p->b_recording )
1812 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1815 if( p_input->p->input.b_title_demux &&
1816 p_input->p->input.i_title > 0 )
1819 /* FIXME handle demux title */
1820 demux_t *p_demux = p_input->p->input.p_demux;
1823 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1824 i_title = p_demux->info.i_title - 1;
1825 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1826 i_title = p_demux->info.i_title + 1;
1828 i_title = val.i_int;
1830 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1832 input_EsOutChangePosition( p_input->p->p_es_out );
1834 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1835 input_ControlVarTitle( p_input, i_title );
1838 else if( p_input->p->input.i_title > 0 )
1840 access_t *p_access = p_input->p->input.p_access;
1843 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1844 i_title = p_access->info.i_title - 1;
1845 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1846 i_title = p_access->info.i_title + 1;
1848 i_title = val.i_int;
1850 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1852 input_EsOutChangePosition( p_input->p->p_es_out );
1854 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1855 stream_AccessReset( p_input->p->input.p_stream );
1859 case INPUT_CONTROL_SET_SEEKPOINT:
1860 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1861 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1862 if( p_input->p->b_recording )
1864 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1868 if( p_input->p->input.b_title_demux &&
1869 p_input->p->input.i_title > 0 )
1871 demux_t *p_demux = p_input->p->input.p_demux;
1873 int64_t i_input_time;
1874 int64_t i_seekpoint_time;
1876 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1878 i_seekpoint = p_demux->info.i_seekpoint;
1879 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1880 if( i_seekpoint_time >= 0 &&
1881 !demux_Control( p_demux,
1882 DEMUX_GET_TIME, &i_input_time ) )
1884 if( i_input_time < i_seekpoint_time + 3000000 )
1890 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1891 i_seekpoint = p_demux->info.i_seekpoint + 1;
1893 i_seekpoint = val.i_int;
1895 if( i_seekpoint >= 0 && i_seekpoint <
1896 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1899 input_EsOutChangePosition( p_input->p->p_es_out );
1901 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1904 else if( p_input->p->input.i_title > 0 )
1906 demux_t *p_demux = p_input->p->input.p_demux;
1907 access_t *p_access = p_input->p->input.p_access;
1909 int64_t i_input_time;
1910 int64_t i_seekpoint_time;
1912 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1914 i_seekpoint = p_access->info.i_seekpoint;
1915 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1916 if( i_seekpoint_time >= 0 &&
1917 demux_Control( p_demux,
1918 DEMUX_GET_TIME, &i_input_time ) )
1920 if( i_input_time < i_seekpoint_time + 3000000 )
1926 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1927 i_seekpoint = p_access->info.i_seekpoint + 1;
1929 i_seekpoint = val.i_int;
1931 if( i_seekpoint >= 0 && i_seekpoint <
1932 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1934 input_EsOutChangePosition( p_input->p->p_es_out );
1936 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1938 stream_AccessReset( p_input->p->input.p_stream );
1943 case INPUT_CONTROL_ADD_SUBTITLE:
1944 if( val.psz_string )
1946 SubtitleAdd( p_input, val.psz_string, true );
1947 free( val.psz_string );
1951 case INPUT_CONTROL_ADD_SLAVE:
1952 if( val.psz_string )
1954 input_source_t *slave = InputSourceNew( p_input );
1956 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1962 msg_Dbg( p_input, "adding %s as slave on the fly",
1966 if( demux_Control( p_input->p->input.p_demux,
1967 DEMUX_GET_TIME, &i_time ) )
1969 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1970 InputSourceClean( slave );
1974 if( demux_Control( slave->p_demux,
1975 DEMUX_SET_TIME, i_time ) )
1977 msg_Err( p_input, "seek failed for new slave" );
1978 InputSourceClean( slave );
1983 /* Get meta (access and demux) */
1984 p_meta = vlc_meta_New();
1985 access_Control( slave->p_access, ACCESS_GET_META,
1987 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1988 InputUpdateMeta( p_input, p_meta );
1990 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1995 msg_Warn( p_input, "failed to add %s as slave",
1999 free( val.psz_string );
2003 case INPUT_CONTROL_SET_RECORD_STATE:
2004 if( !!p_input->p->b_recording != !!val.b_bool )
2006 if( p_input->p->input.b_can_stream_record )
2008 if( demux_Control( p_input->p->input.p_demux,
2009 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2014 if( input_EsOutSetRecord( p_input->p->p_es_out, val.b_bool ) )
2017 p_input->p->b_recording = val.b_bool;
2019 var_Change( p_input, "record", VLC_VAR_SETVALUE, &val, NULL );
2021 b_force_update = true;
2025 case INPUT_CONTROL_SET_BOOKMARK:
2027 msg_Err( p_input, "not yet implemented" );
2031 return b_force_update;
2034 /*****************************************************************************
2036 *****************************************************************************/
2037 static int UpdateFromDemux( input_thread_t *p_input )
2039 demux_t *p_demux = p_input->p->input.p_demux;
2042 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2044 v.i_int = p_demux->info.i_title;
2045 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2047 input_ControlVarTitle( p_input, p_demux->info.i_title );
2049 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2051 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2053 v.i_int = p_demux->info.i_seekpoint;
2054 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2056 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2058 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2060 /* Hmmm only works with master input */
2061 if( p_input->p->input.p_demux == p_demux )
2063 int i_title_end = p_input->p->input.i_title_end -
2064 p_input->p->input.i_title_offset;
2065 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2066 p_input->p->input.i_seekpoint_offset;
2068 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2070 if( p_demux->info.i_title > i_title_end ||
2071 ( p_demux->info.i_title == i_title_end &&
2072 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2074 else if( i_seekpoint_end >=0 )
2076 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2078 else if( i_title_end >= 0 )
2080 if( p_demux->info.i_title > i_title_end ) return 0;
2087 /*****************************************************************************
2089 *****************************************************************************/
2090 static int UpdateFromAccess( input_thread_t *p_input )
2092 access_t *p_access = p_input->p->input.p_access;
2095 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2097 v.i_int = p_access->info.i_title;
2098 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2100 input_ControlVarTitle( p_input, p_access->info.i_title );
2102 stream_AccessUpdate( p_input->p->input.p_stream );
2104 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2106 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2108 v.i_int = p_access->info.i_seekpoint;
2109 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2110 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2112 if( p_access->info.i_update & INPUT_UPDATE_META )
2114 /* TODO maybe multi - access ? */
2115 vlc_meta_t *p_meta = vlc_meta_New();
2116 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2117 InputUpdateMeta( p_input, p_meta );
2118 p_access->info.i_update &= ~INPUT_UPDATE_META;
2120 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2125 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2126 f_quality = f_strength = -1;
2128 var_SetFloat( p_input, "signal-quality", f_quality );
2129 var_SetFloat( p_input, "signal-strength", f_strength );
2131 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2134 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2136 /* Hmmm only works with master input */
2137 if( p_input->p->input.p_access == p_access )
2139 int i_title_end = p_input->p->input.i_title_end -
2140 p_input->p->input.i_title_offset;
2141 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2142 p_input->p->input.i_seekpoint_offset;
2144 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2146 if( p_access->info.i_title > i_title_end ||
2147 ( p_access->info.i_title == i_title_end &&
2148 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2150 else if( i_seekpoint_end >=0 )
2152 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2154 else if( i_title_end >= 0 )
2156 if( p_access->info.i_title > i_title_end ) return 0;
2163 /*****************************************************************************
2165 *****************************************************************************/
2166 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2168 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2171 /*****************************************************************************
2173 *****************************************************************************/
2174 static input_source_t *InputSourceNew( input_thread_t *p_input )
2176 VLC_UNUSED(p_input);
2177 input_source_t *in = malloc( sizeof( input_source_t ) );
2179 memset( in, 0, sizeof( input_source_t ) );
2183 /*****************************************************************************
2185 *****************************************************************************/
2186 static int InputSourceInit( input_thread_t *p_input,
2187 input_source_t *in, const char *psz_mrl,
2188 const char *psz_forced_demux )
2190 const bool b_master = in == &p_input->p->input;
2192 char psz_dup[strlen(psz_mrl) + 1];
2193 const char *psz_access;
2194 const char *psz_demux;
2201 strcpy( psz_dup, psz_mrl );
2203 if( !in ) return VLC_EGENERIC;
2204 if( !p_input ) return VLC_EGENERIC;
2207 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2209 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2210 psz_mrl, psz_access, psz_demux, psz_path );
2211 if( !p_input->b_preparsing )
2213 /* Hack to allow udp://@:port syntax */
2215 (strncmp( psz_access, "udp", 3 ) &&
2216 strncmp( psz_access, "rtp", 3 )) )
2218 /* Find optional titles and seekpoints */
2219 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2220 &in->i_seekpoint_start, &in->i_seekpoint_end );
2223 if( psz_forced_demux && *psz_forced_demux )
2225 psz_demux = psz_forced_demux;
2227 else if( *psz_demux == '\0' )
2229 /* special hack for forcing a demuxer with --demux=module
2230 * (and do nothing with a list) */
2231 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2233 if( psz_var_demux != NULL &&
2234 !strchr(psz_var_demux, ',' ) &&
2235 !strchr(psz_var_demux, ':' ) )
2237 psz_demux = psz_var_demux;
2239 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2243 /* Try access_demux first */
2244 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2245 NULL, p_input->p->p_es_out, false );
2249 /* Preparsing is only for file:// */
2252 if( !*psz_access ) /* path without scheme:// */
2253 psz_access = "file";
2254 if( strcmp( psz_access, "file" ) )
2256 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2261 int64_t i_pts_delay;
2263 /* Get infos from access_demux */
2264 demux_Control( in->p_demux,
2265 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2266 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2268 in->b_title_demux = true;
2269 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2270 &in->title, &in->i_title,
2271 &in->i_title_offset, &in->i_seekpoint_offset ) )
2273 TAB_INIT( in->i_title, in->title );
2275 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2276 &in->b_can_pace_control ) )
2277 in->b_can_pace_control = false;
2279 if( !in->b_can_pace_control )
2281 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2282 &in->b_can_rate_control, &in->b_rescale_ts ) )
2284 in->b_can_rate_control = false;
2285 in->b_rescale_ts = true; /* not used */
2290 in->b_can_rate_control = true;
2291 in->b_rescale_ts = true;
2293 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2294 &in->b_can_pause ) )
2295 in->b_can_pause = false;
2296 var_SetBool( p_input, "can-pause", in->b_can_pause );
2298 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2300 if( ret != VLC_SUCCESS )
2302 var_Set( p_input, "seekable", val );
2306 int64_t i_pts_delay;
2309 input_ChangeState( p_input, OPENING_S );
2311 /* Now try a real access */
2312 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2314 /* Access failed, URL encoded ? */
2315 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2317 decode_URI( psz_path );
2319 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2320 psz_access, psz_demux, psz_path );
2322 in->p_access = access_New( p_input,
2323 psz_access, psz_demux, psz_path );
2325 if( in->p_access == NULL )
2327 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2329 intf_UserFatal( VLC_OBJECT( p_input), false,
2330 _("Your input can't be opened"),
2331 _("VLC is unable to open the MRL '%s'."
2332 " Check the log for details."), psz_mrl );
2337 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2338 while( psz && *psz )
2340 access_t *p_access = in->p_access;
2341 char *end = strchr( psz, ':' );
2346 in->p_access = access_FilterNew( in->p_access, psz );
2347 if( in->p_access == NULL )
2349 in->p_access = p_access;
2350 msg_Warn( p_input, "failed to insert access filter %s",
2358 /* Get infos from access */
2359 if( !p_input->b_preparsing )
2361 access_Control( in->p_access,
2362 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2363 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2365 in->b_title_demux = false;
2366 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2367 &in->title, &in->i_title,
2368 &in->i_title_offset, &in->i_seekpoint_offset ) )
2371 TAB_INIT( in->i_title, in->title );
2373 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2374 &in->b_can_pace_control );
2375 in->b_can_rate_control = in->b_can_pace_control;
2376 in->b_rescale_ts = true;
2378 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2380 var_SetBool( p_input, "can-pause", in->b_can_pause );
2381 access_Control( in->p_access, ACCESS_CAN_SEEK,
2383 var_Set( p_input, "seekable", val );
2387 input_ChangeState( p_input, BUFFERING_S );
2389 /* Autodetect extra files if none specified */
2390 char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2391 if( !psz_input_list )
2393 char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2394 if( psz_extra_files )
2395 var_SetString( p_input, "input-list", psz_extra_files );
2396 free( psz_extra_files );
2399 /* Create the stream_t */
2400 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2402 /* Restor old value */
2403 if( !psz_input_list )
2404 var_SetString( p_input, "input-list", "" );
2405 free( psz_input_list );
2407 if( in->p_stream == NULL )
2409 msg_Warn( p_input, "cannot create a stream_t from access" );
2413 /* Open a demuxer */
2414 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2416 psz_demux = in->p_access->psz_demux;
2420 /* Take access redirections into account */
2421 char *psz_real_path;
2422 char *psz_buf = NULL;
2423 if( in->p_access->psz_path )
2425 const char *psz_a, *psz_d;
2426 psz_buf = strdup( in->p_access->psz_path );
2427 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2431 psz_real_path = psz_path;
2433 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2435 in->p_stream, p_input->p->p_es_out,
2436 p_input->b_preparsing );
2440 if( in->p_demux == NULL )
2442 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2443 psz_access, psz_demux, psz_path );
2444 intf_UserFatal( VLC_OBJECT( p_input ), false,
2445 _("VLC can't recognize the input's format"),
2446 _("The format of '%s' cannot be detected. "
2447 "Have a look at the log for details."), psz_mrl );
2451 /* Get title from demux */
2452 if( !p_input->b_preparsing && in->i_title <= 0 )
2454 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2455 &in->title, &in->i_title,
2456 &in->i_title_offset, &in->i_seekpoint_offset ))
2458 TAB_INIT( in->i_title, in->title );
2462 in->b_title_demux = true;
2467 /* Set record capabilities */
2468 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2469 in->b_can_stream_record = false;
2471 if( !var_CreateGetBool( p_input, "input-record-native" ) )
2472 in->b_can_stream_record = false;
2473 var_SetBool( p_input, "can-record", true );
2475 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2479 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2480 if( 1 || !p_input->b_preparsing )
2483 input_attachment_t **attachment;
2484 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2485 &attachment, &i_attachment ) )
2487 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2488 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2489 i_attachment, attachment );
2490 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2493 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2495 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2497 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2500 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2501 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2507 input_ChangeState( p_input, ERROR_S );
2510 demux_Delete( in->p_demux );
2513 stream_Delete( in->p_stream );
2516 access_Delete( in->p_access );
2518 return VLC_EGENERIC;
2521 /*****************************************************************************
2523 *****************************************************************************/
2524 static void InputSourceClean( input_source_t *in )
2529 demux_Delete( in->p_demux );
2532 stream_Delete( in->p_stream );
2535 access_Delete( in->p_access );
2537 if( in->i_title > 0 )
2539 for( i = 0; i < in->i_title; i++ )
2540 vlc_input_title_Delete( in->title[i] );
2541 TAB_CLEAN( in->i_title, in->title );
2545 static void SlaveDemux( input_thread_t *p_input )
2549 bool b_set_time = true;
2551 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2553 /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2557 for( i = 0; i < p_input->p->i_slave; i++ )
2559 input_source_t *in = p_input->p->slave[i];
2565 if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2570 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2572 msg_Err( p_input, "slave[%d] doesn't like "
2573 "DEMUX_GET_TIME -> EOF", i );
2578 if( i_stime >= i_time )
2581 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2587 i_ret = in->p_demux->pf_demux( in->p_demux );
2592 msg_Dbg( p_input, "slave %d EOF", i );
2598 static void SlaveSeek( input_thread_t *p_input )
2603 if( !p_input ) return;
2605 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2607 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2611 for( i = 0; i < p_input->p->i_slave; i++ )
2613 input_source_t *in = p_input->p->slave[i];
2615 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2618 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2628 /*****************************************************************************
2630 *****************************************************************************/
2631 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2635 if( !p_meta ) return;
2637 /* Get meta information from user */
2638 #define GET_META( field, s ) \
2639 var_Get( p_input, (s), &val ); \
2640 if( *val.psz_string ) \
2641 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2642 free( val.psz_string )
2644 GET_META( Title, "meta-title" );
2645 GET_META( Artist, "meta-artist" );
2646 GET_META( Genre, "meta-genre" );
2647 GET_META( Copyright, "meta-copyright" );
2648 GET_META( Description, "meta-description" );
2649 GET_META( Date, "meta-date" );
2650 GET_META( URL, "meta-url" );
2654 /*****************************************************************************
2655 * InputGetExtraFiles
2656 * Autodetect extra input list
2657 *****************************************************************************/
2658 static char *InputGetExtraFiles( input_thread_t *p_input,
2659 const char *psz_access, const char *psz_path )
2661 char *psz_list = NULL;
2663 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2667 const char *psz_ext = strrchr( psz_path, '.' );
2668 if( !psz_ext || strcmp( psz_ext, ".001" ) )
2671 char *psz_file = strdup( psz_path );
2675 /* Try to list .xyz files */
2676 for( int i = 2; i < 999; i++ )
2678 char *psz_ext = strrchr( psz_file, '.' );
2681 snprintf( psz_ext, 5, ".%.3d", i );
2683 if( utf8_stat( psz_file, &st )
2684 || !S_ISREG( st.st_mode ) || !st.st_size )
2687 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2691 char *psz_old = psz_list;
2692 /* FIXME how to handle file with ',' ?*/
2693 if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2701 psz_list = strdup( psz_file );
2709 /*****************************************************************************
2710 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2711 * arturl and locking issue.
2712 *****************************************************************************/
2713 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2715 input_item_t *p_item = p_input->p->input.p_item;
2716 char * psz_arturl = NULL;
2717 char *psz_title = NULL;
2718 int i_arturl_event = false;
2723 psz_arturl = input_item_GetArtURL( p_item );
2725 vlc_mutex_lock( &p_item->lock );
2726 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2727 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2729 vlc_meta_Merge( p_item->p_meta, p_meta );
2731 if( psz_arturl && *psz_arturl )
2733 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2734 i_arturl_event = true;
2737 vlc_meta_Delete( p_meta );
2739 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2741 /* Don't look for art cover if sout
2742 * XXX It can change when sout has meta data support */
2743 if( p_input->p->p_sout && !p_input->b_preparsing )
2745 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2746 i_arturl_event = true;
2750 input_ExtractAttachmentAndCacheArt( p_input );
2756 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2758 p_meta = vlc_meta_New();
2759 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2761 vlc_mutex_unlock( &p_item->lock );
2763 input_item_SetPreparsed( p_item, true );
2765 if( i_arturl_event == true )
2769 /* Notify interested third parties */
2770 event.type = vlc_InputItemMetaChanged;
2771 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2772 vlc_event_send( &p_item->event_manager, &event );
2777 input_Control( p_input, INPUT_SET_NAME, psz_title );
2781 /** \todo handle sout meta */
2785 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2786 int i_new, input_attachment_t **pp_new )
2788 int i_attachment = *pi_attachment;
2789 input_attachment_t **attachment = *ppp_attachment;
2792 attachment = realloc( attachment,
2793 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2794 for( i = 0; i < i_new; i++ )
2795 attachment[i_attachment++] = pp_new[i];
2799 *pi_attachment = i_attachment;
2800 *ppp_attachment = attachment;
2803 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2807 if( p_input->b_preparsing )
2810 if( p_input->p->input.p_access )
2811 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2814 /* Get meta data from slave input */
2815 for( i = 0; i < p_input->p->i_slave; i++ )
2817 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2818 if( p_input->p->slave[i]->p_access )
2820 access_Control( p_input->p->slave[i]->p_access,
2821 ACCESS_GET_META, p_meta );
2826 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2833 /* XXX I am not sure it is a great idea, besides, there is more than that
2834 * if we want to do it right */
2835 vlc_mutex_lock( &p_item->lock );
2836 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2838 vlc_mutex_unlock( &p_item->lock );
2841 vlc_mutex_unlock( &p_item->lock );
2844 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2845 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2850 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2851 if(! p_demux->p_private )
2854 p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2857 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2859 if( p_demux_meta->p_meta )
2861 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2862 vlc_meta_Delete( p_demux_meta->p_meta );
2865 if( p_demux_meta->i_attachments > 0 )
2867 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2868 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2869 p_demux_meta->i_attachments, p_demux_meta->attachments );
2870 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2872 module_unneed( p_demux, p_id3 );
2874 free( p_demux->p_private );
2878 /*****************************************************************************
2879 * MRLSplit: parse the access, demux and url part of the
2880 * Media Resource Locator.
2881 *****************************************************************************/
2882 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2885 char *psz_access = NULL;
2886 char *psz_demux = NULL;
2889 /* Either there is an access/demux specification before ://
2890 * or we have a plain local file path. */
2891 psz_path = strstr( psz_dup, "://" );
2892 if( psz_path != NULL )
2895 psz_path += 3; /* skips "://" */
2897 /* Separate access from demux (<access>/<demux>://<path>) */
2898 psz_access = psz_dup;
2899 psz_demux = strchr( psz_access, '/' );
2901 *psz_demux++ = '\0';
2903 /* We really don't want module name substitution here! */
2904 if( psz_access[0] == '$' )
2906 if( psz_demux && psz_demux[0] == '$' )
2913 *ppsz_access = psz_access ? psz_access : (char*)"";
2914 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2915 *ppsz_path = psz_path;
2918 static inline bool next(char ** src)
2922 long result = strtol( *src, &end, 0 );
2923 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
2932 /*****************************************************************************
2933 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2936 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2937 *****************************************************************************/
2938 static void MRLSections( input_thread_t *p_input, char *psz_source,
2939 int *pi_title_start, int *pi_title_end,
2940 int *pi_chapter_start, int *pi_chapter_end )
2942 char *psz, *psz_end, *psz_next, *psz_check;
2944 *pi_title_start = *pi_title_end = -1;
2945 *pi_chapter_start = *pi_chapter_end = -1;
2947 /* Start by parsing titles and chapters */
2948 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2951 /* Check we are really dealing with a title/chapter section */
2952 psz_check = psz + 1;
2953 if( !*psz_check ) return;
2954 if( isdigit(*psz_check) )
2955 if(!next(&psz_check)) return;
2956 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2957 if( *psz_check == ':' && ++psz_check )
2959 if( isdigit(*psz_check) )
2960 if(!next(&psz_check)) return;
2962 if( *psz_check != '-' && *psz_check ) return;
2963 if( *psz_check == '-' && ++psz_check )
2965 if( isdigit(*psz_check) )
2966 if(!next(&psz_check)) return;
2968 if( *psz_check != ':' && *psz_check ) return;
2969 if( *psz_check == ':' && ++psz_check )
2971 if( isdigit(*psz_check) )
2972 if(!next(&psz_check)) return;
2974 if( *psz_check ) return;
2976 /* Separate start and end */
2978 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2980 /* Look for the start title */
2981 *pi_title_start = strtol( psz, &psz_next, 0 );
2982 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2983 *pi_title_end = *pi_title_start;
2986 /* Look for the start chapter */
2988 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2989 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2990 *pi_chapter_end = *pi_chapter_start;
2994 /* Look for the end title */
2995 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2996 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2999 /* Look for the end chapter */
3000 if( *psz_end ) psz_end++;
3001 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3002 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3005 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3006 psz_source, *pi_title_start, *pi_chapter_start,
3007 *pi_title_end, *pi_chapter_end );
3010 /*****************************************************************************
3011 * input_AddSubtitles: add a subtitles file and enable it
3012 *****************************************************************************/
3013 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3015 input_source_t *sub;
3018 char *psz_path, *psz_extension;
3020 /* if we are provided a subtitle.sub file,
3021 * see if we don't have a subtitle.idx and use it instead */
3022 psz_path = strdup( psz_subtitle );
3025 psz_extension = strrchr( psz_path, '.');
3026 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3030 strcpy( psz_extension, ".idx" );
3032 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3034 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3035 psz_path, psz_subtitle );
3036 strcpy( psz_subtitle, psz_path );
3042 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3044 sub = InputSourceNew( p_input );
3045 if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3050 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3053 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3055 if( count.i_int == 0 )
3057 /* if it was first one, there is disable too */
3059 if( count.i_int < list.p_list->i_count )
3061 int i_id = list.p_list->p_values[count.i_int].i_int;
3062 es_out_id_t *p_es = input_EsOutGetFromID( p_input->p->p_es_out, i_id );
3064 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_DEFAULT, p_es );
3065 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES, p_es );
3067 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3071 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3072 bool b_check_extension )
3076 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3079 assert( psz_subtitle != NULL );
3081 val.psz_string = strdup( psz_subtitle );
3082 if( val.psz_string )
3083 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3087 /*****************************************************************************
3088 * input_get_event_manager
3089 *****************************************************************************/
3090 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3092 return &p_input->p->event_manager;
3096 /* TODO FIXME nearly the same logic that snapshot code */
3097 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3102 path = utf8_opendir( psz_path );
3107 char *psz_tmp = str_format( p_obj, psz_prefix );
3111 filename_sanitize( psz_tmp );
3112 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3114 psz_extension ? "." : "",
3115 psz_extension ? psz_extension : "" ) < 0 )
3122 psz_file = str_format( p_obj, psz_path );
3123 path_sanitize( psz_file );