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"
45 #include "../stream_output/stream_output.h"
47 #include <vlc_interface.h>
49 #include <vlc_charset.h>
50 #include <vlc_strings.h>
52 #ifdef HAVE_SYS_STAT_H
53 # include <sys/stat.h>
56 /*****************************************************************************
58 *****************************************************************************/
59 static void Destructor( input_thread_t * p_input );
61 static void* Run ( vlc_object_t *p_this );
62 static void* RunAndDestroy ( vlc_object_t *p_this );
64 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
65 const char *, bool, sout_instance_t * );
66 static int Init ( input_thread_t *p_input );
67 static void WaitDie ( input_thread_t *p_input );
68 static void End ( input_thread_t *p_input );
69 static void MainLoop( input_thread_t *p_input );
71 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline );
72 static void ControlReduce( input_thread_t * );
73 static bool Control( input_thread_t *, int, vlc_value_t );
75 static int UpdateFromAccess( input_thread_t * );
76 static int UpdateFromDemux( input_thread_t * );
78 static void UpdateItemLength( input_thread_t *, int64_t i_length );
80 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
82 static input_source_t *InputSourceNew( input_thread_t *);
83 static int InputSourceInit( input_thread_t *, input_source_t *,
84 const char *, const char *psz_forced_demux );
85 static void InputSourceClean( input_source_t * );
87 //static void InputGetAttachments( input_thread_t *, input_source_t * );
88 static void SlaveDemux( input_thread_t *p_input );
89 static void SlaveSeek( input_thread_t *p_input );
91 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
92 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
93 static char *InputGetExtraFiles( input_thread_t *p_input,
94 const char *psz_access, const char *psz_path );
96 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
97 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
98 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
99 int i_new, input_attachment_t **pp_new );
101 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced );
103 /*****************************************************************************
104 * This function creates a new input, and returns a pointer
105 * to its description. On error, it returns NULL.
107 * Variables for _public_ use:
110 * - rate,rate-slower, rate-faster
111 * - position, position-offset
112 * - time, time-offset
113 * - title,title-next,title-prev
114 * - chapter,chapter-next, chapter-prev
115 * - program, audio-es, video-es, spu-es
116 * - audio-delay, spu-delay
121 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
122 * or not, for that check position != 0.0)
124 * - can-record (if a stream can be recorded while playing)
125 * - teletext-es to get the index of spu track that is teletext --1 if no teletext)
126 * * For intf callback upon changes:
128 * - intf-change-vout for when a vout is created or destroyed
129 * - rate-change for when playback rate changes
130 * - stats-change for when statistics are updated
131 * TODO explain when Callback is called
132 * TODO complete this list (?)
133 *****************************************************************************/
134 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
135 const char *psz_header, bool b_quick,
136 sout_instance_t *p_sout )
138 static const char input_name[] = "input";
139 input_thread_t *p_input = NULL; /* thread descriptor */
143 /* Allocate descriptor */
144 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
145 VLC_OBJECT_INPUT, input_name );
146 if( p_input == NULL )
149 /* Construct a nice name for the input timer */
150 char psz_timer_name[255];
151 char * psz_name = input_item_GetName( p_item );
152 snprintf( psz_timer_name, sizeof(psz_timer_name),
153 "input launching for '%s'", psz_name );
155 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
159 /* Start a timer to mesure how long it takes
160 * to launch an input */
161 stats_TimerStart( p_input, psz_timer_name,
162 STATS_TIMER_INPUT_LAUNCHING );
164 MALLOC_NULL( p_input->p, input_thread_private_t );
165 memset( p_input->p, 0, sizeof( input_thread_private_t ) );
167 /* One "randomly" selected input thread is responsible for computing
168 * the global stats. Check if there is already someone doing this */
169 if( p_input->p_libvlc->p_stats && !b_quick )
171 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
172 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
173 if( p_private->p_stats_computer == NULL )
174 p_private->p_stats_computer = p_input;
175 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
178 p_input->b_preparsing = b_quick;
179 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
182 vlc_event_manager_t * p_em = &p_input->p->event_manager;
183 vlc_event_manager_init_with_vlc_object( p_em, p_input );
184 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
185 vlc_event_manager_register_event_type( p_em, vlc_InputSelectedStreamChanged );
187 /* Init Common fields */
188 p_input->b_eof = false;
189 p_input->b_can_pace_control = true;
190 p_input->p->i_start = 0;
192 p_input->p->i_stop = 0;
193 p_input->p->i_run = 0;
194 p_input->p->i_title = 0;
195 p_input->p->title = NULL;
196 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
197 p_input->i_state = INIT_S;
198 p_input->p->i_rate = INPUT_RATE_DEFAULT;
199 p_input->p->b_recording = false;
200 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
201 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
202 p_input->p->p_es_out = NULL;
203 p_input->p->p_sout = NULL;
204 p_input->p->b_out_pace_control = false;
205 p_input->i_pts_delay = 0;
207 /* Init Input fields */
208 vlc_gc_incref( p_item ); /* Released in Destructor() */
209 p_input->p->input.p_item = p_item;
210 p_input->p->input.p_access = NULL;
211 p_input->p->input.p_stream = NULL;
212 p_input->p->input.p_demux = NULL;
213 p_input->p->input.b_title_demux = false;
214 p_input->p->input.i_title = 0;
215 p_input->p->input.title = NULL;
216 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
217 p_input->p->input.b_can_pace_control = true;
218 p_input->p->input.b_can_rate_control = true;
219 p_input->p->input.b_rescale_ts = true;
220 p_input->p->input.b_eof = false;
221 p_input->p->input.i_cr_average = 0;
223 vlc_mutex_lock( &p_item->lock );
225 if( !p_item->p_stats )
226 p_item->p_stats = stats_NewInputStats( p_input );
227 vlc_mutex_unlock( &p_item->lock );
230 p_input->p->i_slave = 0;
231 p_input->p->slave = NULL;
233 /* Init control buffer */
234 vlc_mutex_init( &p_input->p->lock_control );
235 vlc_cond_init( &p_input->p->wait_control );
236 p_input->p->i_control = 0;
238 /* Parse input options */
239 vlc_mutex_lock( &p_item->lock );
240 assert( (int)p_item->optflagc == p_item->i_options );
241 for( i = 0; i < p_item->i_options; i++ )
242 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
243 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
244 vlc_mutex_unlock( &p_item->lock );
246 /* Create Object Variables for private use only */
247 input_ConfigVarInit( p_input );
249 /* Create Objects variables for public Get and Set */
250 input_ControlVarInit( p_input );
253 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
255 if( !p_input->b_preparsing )
257 var_Get( p_input, "bookmarks", &val );
260 /* FIXME: have a common cfg parsing routine used by sout and others */
261 char *psz_parser, *psz_start, *psz_end;
262 psz_parser = val.psz_string;
263 while( (psz_start = strchr( psz_parser, '{' ) ) )
265 seekpoint_t *p_seekpoint;
268 psz_end = strchr( psz_start, '}' );
269 if( !psz_end ) break;
270 psz_parser = psz_end + 1;
271 backup = *psz_parser;
275 p_seekpoint = vlc_seekpoint_New();
276 while( (psz_end = strchr( psz_start, ',' ) ) )
279 if( !strncmp( psz_start, "name=", 5 ) )
281 p_seekpoint->psz_name = strdup(psz_start + 5);
283 else if( !strncmp( psz_start, "bytes=", 6 ) )
285 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
287 else if( !strncmp( psz_start, "time=", 5 ) )
289 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
292 psz_start = psz_end + 1;
294 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
295 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
296 p_seekpoint->i_time_offset );
297 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
298 vlc_seekpoint_Delete( p_seekpoint );
299 *psz_parser = backup;
301 free( val.psz_string );
305 /* Remove 'Now playing' info as it is probably outdated */
306 input_item_SetNowPlaying( p_item, NULL );
309 if( p_input->b_preparsing )
310 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
314 p_input->p->p_sout = p_sout;
316 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
317 vlc_mutex_init( &p_input->p->counters.counters_lock );
319 /* Set the destructor when we are sure we are initialized */
320 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
322 /* Attach only once we are ready */
323 vlc_object_attach( p_input, p_parent );
329 * Input destructor (called when the object's refcount reaches 0).
331 static void Destructor( input_thread_t * p_input )
334 char * psz_name = input_item_GetName( p_input->p->input.p_item );
335 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
339 vlc_event_manager_fini( &p_input->p->event_manager );
341 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
342 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
344 if( p_input->p->p_sout )
345 sout_DeleteInstance( p_input->p->p_sout );
347 vlc_gc_decref( p_input->p->input.p_item );
349 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
351 vlc_cond_destroy( &p_input->p->wait_control );
352 vlc_mutex_destroy( &p_input->p->lock_control );
357 * Initialize an input thread and run it. You will need to monitor the
358 * thread to clean up after it is done
360 * \param p_parent a vlc_object
361 * \param p_item an input item
362 * \return a pointer to the spawned input thread
364 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
365 input_item_t *p_item )
367 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
371 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
372 input_item_t *p_item,
373 const char *psz_log, sout_instance_t *p_sout )
375 input_thread_t *p_input;
377 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
381 /* Create thread and wait for its readiness. */
382 if( vlc_thread_create( p_input, "input", Run,
383 VLC_THREAD_PRIORITY_INPUT, false ) )
385 input_ChangeState( p_input, ERROR_S );
386 msg_Err( p_input, "cannot create input thread" );
387 vlc_object_detach( p_input );
388 vlc_object_release( p_input );
396 * Initialize an input thread and run it. This thread will clean after itself,
397 * you can forget about it. It can work either in blocking or non-blocking mode
399 * \param p_parent a vlc_object
400 * \param p_item an input item
401 * \param b_block should we block until read is finished ?
402 * \return an error code, VLC_SUCCESS on success
404 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
407 input_thread_t *p_input;
409 p_input = Create( p_parent, p_item, NULL, false, NULL );
415 RunAndDestroy( VLC_OBJECT(p_input) );
420 if( vlc_thread_create( p_input, "input", RunAndDestroy,
421 VLC_THREAD_PRIORITY_INPUT, false ) )
423 input_ChangeState( p_input, ERROR_S );
424 msg_Err( p_input, "cannot create input thread" );
425 vlc_object_release( p_input );
433 * Initialize an input and initialize it to preparse the item
434 * This function is blocking. It will only accept to parse files
436 * \param p_parent a vlc_object_t
437 * \param p_item an input item
438 * \return VLC_SUCCESS or an error
440 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
442 input_thread_t *p_input;
444 /* Allocate descriptor */
445 p_input = Create( p_parent, p_item, NULL, true, NULL );
449 if( !Init( p_input ) )
452 vlc_object_detach( p_input );
453 vlc_object_release( p_input );
459 * Request a running input thread to stop and die
461 * \param the input thread to stop
463 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
468 if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
469 p_obj->i_object_type == VLC_OBJECT_AOUT ||
470 p_obj == VLC_OBJECT(p_input->p->p_sout) )
473 vlc_object_kill( p_obj );
475 p_list = vlc_list_children( p_obj );
476 for( i = 0; i < p_list->i_count; i++ )
477 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
478 vlc_list_release( p_list );
480 void input_StopThread( input_thread_t *p_input )
482 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
483 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
484 * unlock the control loop */
485 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
487 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
490 sout_instance_t * input_DetachSout( input_thread_t *p_input )
492 sout_instance_t *p_sout = p_input->p->p_sout;
493 vlc_object_detach( p_sout );
494 p_input->p->p_sout = NULL;
498 /*****************************************************************************
499 * Run: main thread loop
500 * This is the "normal" thread that spawns the input processing chain,
501 * reads the stream, cleans up and waits
502 *****************************************************************************/
503 static void* Run( vlc_object_t *p_this )
505 input_thread_t *p_input = (input_thread_t *)p_this;
506 const int canc = vlc_savecancel();
508 if( Init( p_input ) )
510 /* If we failed, wait before we are killed, and exit */
517 /* Wait until we are asked to die */
518 if( !p_input->b_die )
525 p_input->b_dead = true;
526 vlc_restorecancel( canc );
530 /*****************************************************************************
531 * RunAndDestroy: main thread loop
532 * This is the "just forget me" thread that spawns the input processing chain,
533 * reads the stream, cleans up and releases memory
534 *****************************************************************************/
535 static void* RunAndDestroy( vlc_object_t *p_this )
537 input_thread_t *p_input = (input_thread_t *)p_this;
538 const int canc = vlc_savecancel();
540 if( Init( p_input ) )
550 vlc_object_release( p_input );
551 vlc_restorecancel( canc );
555 /*****************************************************************************
556 * Main loop: Fill buffers from access, and demux
557 *****************************************************************************/
561 * It asks the demuxer to demux some data
563 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t *pi_start_mdate )
569 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
570 ( p_input->p->i_run > 0 && *pi_start_mdate+p_input->p->i_run < mdate() ) )
573 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
578 if( p_input->p->input.b_title_demux &&
579 p_input->p->input.p_demux->info.i_update )
581 i_ret = UpdateFromDemux( p_input );
584 else if( !p_input->p->input.b_title_demux &&
585 p_input->p->input.p_access &&
586 p_input->p->input.p_access->info.i_update )
588 i_ret = UpdateFromAccess( p_input );
593 if( i_ret == 0 ) /* EOF */
597 var_Get( p_input, "input-repeat", &repeat );
598 if( repeat.i_int == 0 )
600 /* End of file - we do not set b_die because only the
601 * playlist is allowed to do so. */
602 msg_Dbg( p_input, "EOF reached" );
603 p_input->p->input.b_eof = true;
609 msg_Dbg( p_input, "repeating the same input (%d)",
611 if( repeat.i_int > 0 )
614 var_Set( p_input, "input-repeat", repeat );
617 /* Seek to start title/seekpoint */
618 val.i_int = p_input->p->input.i_title_start -
619 p_input->p->input.i_title_offset;
620 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
622 input_ControlPush( p_input,
623 INPUT_CONTROL_SET_TITLE, &val );
625 val.i_int = p_input->p->input.i_seekpoint_start -
626 p_input->p->input.i_seekpoint_offset;
627 if( val.i_int > 0 /* TODO: check upper boundary */ )
628 input_ControlPush( p_input,
629 INPUT_CONTROL_SET_SEEKPOINT, &val );
631 /* Seek to start position */
632 if( p_input->p->i_start > 0 )
634 val.i_time = p_input->p->i_start;
635 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
641 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
646 *pi_start_mdate = mdate();
651 input_ChangeState( p_input, ERROR_S );
654 if( i_ret > 0 && p_input->p->i_slave > 0 )
656 SlaveDemux( p_input );
662 * It update the variables used by the interfaces
664 static void MainLoopInterface( input_thread_t *p_input )
668 int64_t i_time, i_length;
670 /* update input status variables */
671 if( !demux_Control( p_input->p->input.p_demux,
672 DEMUX_GET_POSITION, &f_pos ) )
674 val.f_float = (float)f_pos;
675 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
677 if( !demux_Control( p_input->p->input.p_demux,
678 DEMUX_GET_TIME, &i_time ) )
680 p_input->i_time = i_time;
682 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
684 if( !demux_Control( p_input->p->input.p_demux,
685 DEMUX_GET_LENGTH, &i_length ) )
688 var_Get( p_input, "length", &old_val );
689 val.i_time = i_length;
690 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
692 if( old_val.i_time != val.i_time )
694 UpdateItemLength( p_input, i_length );
698 var_SetBool( p_input, "intf-change", true );
703 * It updates the globals statics
705 static void MainLoopStatistic( input_thread_t *p_input )
707 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
708 /* Are we the thread responsible for computing global stats ? */
709 if( libvlc_priv( p_input->p_libvlc )->p_stats_computer == p_input )
711 stats_ComputeGlobalStats( p_input->p_libvlc,
712 p_input->p_libvlc->p_stats );
714 var_SetBool( p_input, "stats-change", true );
719 * The main input loop.
721 static void MainLoop( input_thread_t *p_input )
723 mtime_t i_start_mdate = mdate();
724 mtime_t i_intf_update = 0;
725 mtime_t i_statistic_update = 0;
727 /* Start the timer */
728 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
730 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
741 b_force_update = false;
743 b_paused = p_input->i_state == PAUSE_S &&
744 !input_EsOutIsBuffering( p_input->p->p_es_out );
748 MainLoopDemux( p_input, &b_force_update, &i_start_mdate );
749 i_wakeup = input_EsOutGetWakeup( p_input->p->p_es_out );
754 i_deadline = i_wakeup;
756 i_deadline = __MIN( i_intf_update, i_statistic_update );
759 vlc_mutex_lock( &p_input->p->lock_control );
760 ControlReduce( p_input );
761 while( !ControlPopNoLock( p_input, &i_type, &val, i_deadline ) )
763 msg_Dbg( p_input, "control type=%d", i_type );
764 if( Control( p_input, i_type, val ) )
765 b_force_update = true;
767 vlc_mutex_unlock( &p_input->p->lock_control );
769 /* Update interface and statistics */
771 if( i_intf_update < i_current || b_force_update )
773 MainLoopInterface( p_input );
774 i_intf_update = i_current + INT64_C(250000);
775 b_force_update = false;
777 if( i_statistic_update < i_current )
779 MainLoopStatistic( p_input );
780 i_statistic_update = i_current + INT64_C(1000000);
783 /* Check if i_wakeup is still valid */
786 mtime_t i_new_wakeup = input_EsOutGetWakeup( p_input->p->p_es_out );
790 } while( i_current < i_wakeup );
793 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
795 /* We have finish to demux data but not to play them */
796 while( vlc_object_alive( p_input ) )
798 if( input_EsOutDecodersIsEmpty( p_input->p->p_es_out ) )
801 msg_Dbg( p_input, "waiting decoder fifos to empty" );
803 msleep( INPUT_IDLE_SLEEP );
806 /* We have finished */
807 input_ChangeState( p_input, END_S );
811 static void InitStatistics( input_thread_t * p_input )
813 if( p_input->b_preparsing ) return;
815 /* Prepare statistics */
816 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
817 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
818 if( libvlc_stats( p_input ) )
820 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
821 INIT_COUNTER( read_packets, INTEGER, COUNTER );
822 INIT_COUNTER( demux_read, INTEGER, COUNTER );
823 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
824 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
825 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
826 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
827 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
828 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
829 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
830 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
831 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
832 p_input->p->counters.p_sout_send_bitrate = NULL;
833 p_input->p->counters.p_sout_sent_packets = NULL;
834 p_input->p->counters.p_sout_sent_bytes = NULL;
835 if( p_input->p->counters.p_demux_bitrate )
836 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
837 if( p_input->p->counters.p_input_bitrate )
838 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
843 static int InitSout( input_thread_t * p_input )
847 if( p_input->b_preparsing ) return VLC_SUCCESS;
849 /* Find a usable sout and attach it to p_input */
850 psz = var_GetNonEmptyString( p_input, "sout" );
851 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
853 /* Check the validity of the provided sout */
854 if( p_input->p->p_sout )
856 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
858 msg_Dbg( p_input, "destroying unusable sout" );
860 sout_DeleteInstance( p_input->p->p_sout );
861 p_input->p->p_sout = NULL;
865 if( p_input->p->p_sout )
868 msg_Dbg( p_input, "sout keep: reusing sout" );
869 msg_Dbg( p_input, "sout keep: you probably want to use "
870 "gather stream_out" );
871 vlc_object_attach( p_input->p->p_sout, p_input );
875 /* Create a new one */
876 p_input->p->p_sout = sout_NewInstance( p_input, psz );
877 if( !p_input->p->p_sout )
879 input_ChangeState( p_input, ERROR_S );
880 msg_Err( p_input, "cannot start stream output instance, " \
886 if( libvlc_stats( p_input ) )
888 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
889 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
890 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
891 if( p_input->p->counters.p_sout_send_bitrate )
892 p_input->p->counters.p_sout_send_bitrate->update_interval =
896 else if( p_input->p->p_sout )
898 msg_Dbg( p_input, "destroying useless sout" );
900 sout_DeleteInstance( p_input->p->p_sout );
901 p_input->p->p_sout = NULL;
909 static void InitTitle( input_thread_t * p_input )
913 if( p_input->b_preparsing ) return;
915 /* Create global title (from master) */
916 p_input->p->i_title = p_input->p->input.i_title;
917 p_input->p->title = p_input->p->input.title;
918 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
919 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
920 if( p_input->p->i_title > 0 )
922 /* Setup variables */
923 input_ControlVarNavigation( p_input );
924 input_ControlVarTitle( p_input, 0 );
928 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
929 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
930 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
933 if( p_input->i_pts_delay < 0 )
934 p_input->i_pts_delay = 0;
936 /* If the desynchronisation requested by the user is < 0, we need to
937 * cache more data. */
938 var_Get( p_input, "audio-desync", &val );
940 p_input->i_pts_delay -= (val.i_int * 1000);
942 /* Update cr_average depending on the caching */
943 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
944 p_input->p->input.i_cr_average /= 10;
945 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
948 static void StartTitle( input_thread_t * p_input )
957 /* Start title/chapter */
959 if( p_input->b_preparsing )
961 p_input->p->i_start = 0;
965 val.i_int = p_input->p->input.i_title_start -
966 p_input->p->input.i_title_offset;
967 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
968 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
969 val.i_int = p_input->p->input.i_seekpoint_start -
970 p_input->p->input.i_seekpoint_offset;
971 if( val.i_int > 0 /* TODO: check upper boundary */ )
972 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
976 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
977 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
978 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
979 i_length = var_GetTime( p_input, "length" );
980 if( p_input->p->i_run < 0 )
982 msg_Warn( p_input, "invalid run-time ignored" );
983 p_input->p->i_run = 0;
986 if( p_input->p->i_start > 0 )
988 if( p_input->p->i_start >= i_length )
990 msg_Warn( p_input, "invalid start-time ignored" );
996 msg_Dbg( p_input, "starting at time: %ds",
997 (int)( p_input->p->i_start / INT64_C(1000000) ) );
999 s.i_time = p_input->p->i_start;
1000 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
1003 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
1005 msg_Warn( p_input, "invalid stop-time ignored" );
1006 p_input->p->i_stop = 0;
1009 /* Load subtitles */
1010 /* Get fps and set it if not already set */
1011 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
1014 float f_requested_fps;
1016 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
1017 var_SetFloat( p_input, "sub-original-fps", f_fps );
1019 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1020 if( f_requested_fps != f_fps )
1022 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1023 VLC_VAR_DOINHERIT );
1024 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1028 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1031 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1034 /* Look for and add subtitle files */
1035 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1036 if( psz_subtitle != NULL )
1038 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1039 SubtitleAdd( p_input, psz_subtitle, true );
1042 var_Get( p_input, "sub-autodetect-file", &val );
1045 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1046 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1047 p_input->p->input.p_item->psz_uri );
1048 free( psz_autopath );
1050 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1052 /* Try to autoselect the first autodetected subtitles file
1053 * if no subtitles file was specified */
1054 bool b_forced = i == 0 && !psz_subtitle;
1056 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1057 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1059 free( ppsz_subs[i] );
1063 free( psz_subtitle );
1065 /* Look for slave */
1066 psz = var_GetNonEmptyString( p_input, "input-slave" );
1070 input_source_t *slave;
1071 while( psz && *psz )
1073 while( *psz == ' ' || *psz == '#' )
1077 if( ( psz_delim = strchr( psz, '#' ) ) )
1079 *psz_delim++ = '\0';
1086 msg_Dbg( p_input, "adding slave input '%s'", psz );
1087 slave = InputSourceNew( p_input );
1088 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1090 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1099 static void InitPrograms( input_thread_t * p_input )
1104 if( p_input->b_preparsing ) return;
1107 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1108 i_es_out_mode = ES_OUT_MODE_AUTO;
1110 if( p_input->p->p_sout )
1112 var_Get( p_input, "sout-all", &val );
1115 i_es_out_mode = ES_OUT_MODE_ALL;
1120 var_Get( p_input, "programs", &val );
1121 if( val.p_list && val.p_list->i_count )
1123 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1124 /* Note : we should remove the "program" callback. */
1128 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1133 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1135 /* Inform the demuxer about waited group (needed only for DVB) */
1136 if( i_es_out_mode == ES_OUT_MODE_ALL )
1138 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1140 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1142 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1147 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1148 (int) var_GetInteger( p_input, "program" ), NULL );
1152 static int Init( input_thread_t * p_input )
1158 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1160 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1162 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1163 var_SetString( p_input, "sout", "" );
1164 var_SetBool( p_input, "sout-all", false );
1165 var_SetString( p_input, "input-slave", "" );
1166 var_SetInteger( p_input, "input-repeat", 0 );
1167 var_SetString( p_input, "sub-file", "" );
1168 var_SetBool( p_input, "sub-autodetect-file", false );
1172 InitStatistics( p_input );
1174 ret = InitSout( p_input );
1175 if( ret != VLC_SUCCESS )
1176 return ret; /* FIXME: goto error; should be better here */
1180 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1181 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1182 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1184 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1185 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1187 if( InputSourceInit( p_input, &p_input->p->input,
1188 p_input->p->input.p_item->psz_uri, NULL ) )
1193 InitTitle( p_input );
1195 /* Load master infos */
1197 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1198 &val.i_time ) && val.i_time > 0 )
1200 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1201 UpdateItemLength( p_input, val.i_time );
1205 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1206 if( val.i_time > 0 )
1207 { /* fallback: gets length from metadata */
1208 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1209 UpdateItemLength( p_input, val.i_time );
1213 StartTitle( p_input );
1215 InitPrograms( p_input );
1217 if( !p_input->b_preparsing && p_input->p->p_sout )
1219 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1221 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1223 /* We don't want a high input priority here or we'll
1224 * end-up sucking up all the CPU time */
1225 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1228 msg_Dbg( p_input, "starting in %s mode",
1229 p_input->p->b_out_pace_control ? "async" : "sync" );
1232 p_meta = vlc_meta_New();
1234 /* Get meta data from users */
1235 InputMetaUser( p_input, p_meta );
1237 /* Get meta data from master input */
1238 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1240 /* Access_file does not give any meta, and there are no slave */
1241 AccessMeta( p_input, p_meta );
1243 InputUpdateMeta( p_input, p_meta );
1245 if( !p_input->b_preparsing )
1247 msg_Dbg( p_input, "`%s' successfully opened",
1248 p_input->p->input.p_item->psz_uri );
1252 /* initialization is complete */
1253 input_ChangeState( p_input, PLAYING_S );
1258 input_ChangeState( p_input, ERROR_S );
1260 if( p_input->p->p_es_out )
1261 input_EsOutDelete( p_input->p->p_es_out );
1263 if( p_input->p->p_sout )
1265 vlc_object_detach( p_input->p->p_sout );
1266 sout_DeleteInstance( p_input->p->p_sout );
1270 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1272 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1273 stats_CounterClean( p_input->p->counters.p_##c );\
1274 p_input->p->counters.p_##c = NULL; } while(0)
1275 EXIT_COUNTER( read_bytes );
1276 EXIT_COUNTER( read_packets );
1277 EXIT_COUNTER( demux_read );
1278 EXIT_COUNTER( input_bitrate );
1279 EXIT_COUNTER( demux_bitrate );
1280 EXIT_COUNTER( played_abuffers );
1281 EXIT_COUNTER( lost_abuffers );
1282 EXIT_COUNTER( displayed_pictures );
1283 EXIT_COUNTER( lost_pictures );
1284 EXIT_COUNTER( decoded_audio );
1285 EXIT_COUNTER( decoded_video );
1286 EXIT_COUNTER( decoded_sub );
1288 if( p_input->p->p_sout )
1290 EXIT_COUNTER( sout_sent_packets );
1291 EXIT_COUNTER( sout_sent_bytes );
1292 EXIT_COUNTER( sout_send_bitrate );
1297 /* Mark them deleted */
1298 p_input->p->input.p_demux = NULL;
1299 p_input->p->input.p_stream = NULL;
1300 p_input->p->input.p_access = NULL;
1301 p_input->p->p_es_out = NULL;
1302 p_input->p->p_sout = NULL;
1304 return VLC_EGENERIC;
1307 /*****************************************************************************
1308 * WaitDie: Wait until we are asked to die.
1309 *****************************************************************************
1310 * This function is called when an error occurred during thread main's loop.
1311 *****************************************************************************/
1312 static void WaitDie( input_thread_t *p_input )
1314 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1316 /* Wait a die order */
1317 vlc_object_lock( p_input );
1318 while( vlc_object_alive( p_input ) )
1319 vlc_object_wait( p_input );
1320 vlc_object_unlock( p_input );
1323 /*****************************************************************************
1324 * End: end the input thread
1325 *****************************************************************************/
1326 static void End( input_thread_t * p_input )
1330 /* We are at the end */
1331 input_ChangeState( p_input, END_S );
1333 /* Clean control variables */
1334 input_ControlVarStop( p_input );
1336 /* Clean up master */
1337 InputSourceClean( &p_input->p->input );
1340 for( i = 0; i < p_input->p->i_slave; i++ )
1342 InputSourceClean( p_input->p->slave[i] );
1343 free( p_input->p->slave[i] );
1345 free( p_input->p->slave );
1347 /* Unload all modules */
1348 if( p_input->p->p_es_out )
1349 input_EsOutDelete( p_input->p->p_es_out );
1351 if( !p_input->b_preparsing )
1353 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1354 if( libvlc_stats( p_input ) )
1356 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
1358 /* make sure we are up to date */
1359 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1360 if( p_private->p_stats_computer == p_input )
1362 stats_ComputeGlobalStats( p_input->p_libvlc,
1363 p_input->p_libvlc->p_stats );
1364 p_private->p_stats_computer = NULL;
1366 CL_CO( read_bytes );
1367 CL_CO( read_packets );
1368 CL_CO( demux_read );
1369 CL_CO( input_bitrate );
1370 CL_CO( demux_bitrate );
1371 CL_CO( played_abuffers );
1372 CL_CO( lost_abuffers );
1373 CL_CO( displayed_pictures );
1374 CL_CO( lost_pictures );
1375 CL_CO( decoded_audio) ;
1376 CL_CO( decoded_video );
1377 CL_CO( decoded_sub) ;
1380 /* Close optional stream output instance */
1381 if( p_input->p->p_sout )
1383 CL_CO( sout_sent_packets );
1384 CL_CO( sout_sent_bytes );
1385 CL_CO( sout_send_bitrate );
1387 vlc_object_detach( p_input->p->p_sout );
1392 if( p_input->p->i_attachment > 0 )
1394 for( i = 0; i < p_input->p->i_attachment; i++ )
1395 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1396 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1399 /* Tell we're dead */
1400 p_input->b_dead = true;
1403 /*****************************************************************************
1405 *****************************************************************************/
1406 static inline int ControlPopNoLock( input_thread_t *p_input,
1407 int *pi_type, vlc_value_t *p_val,
1408 mtime_t i_deadline )
1411 while( p_input->p->i_control <= 0 )
1413 if( !vlc_object_alive( p_input ) )
1414 return VLC_EGENERIC;
1416 if( i_deadline < 0 )
1417 return VLC_EGENERIC;
1419 if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1420 return VLC_EGENERIC;
1423 *pi_type = p_input->p->control[0].i_type;
1424 *p_val = p_input->p->control[0].val;
1426 p_input->p->i_control--;
1427 if( p_input->p->i_control > 0 )
1431 for( i = 0; i < p_input->p->i_control; i++ )
1433 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1434 p_input->p->control[i].val = p_input->p->control[i+1].val;
1441 static void ControlReduce( input_thread_t *p_input )
1448 for( i = 1; i < p_input->p->i_control; i++ )
1450 const int i_lt = p_input->p->control[i-1].i_type;
1451 const int i_ct = p_input->p->control[i].i_type;
1453 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1454 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1458 ( i_ct == INPUT_CONTROL_SET_STATE ||
1459 i_ct == INPUT_CONTROL_SET_RATE ||
1460 i_ct == INPUT_CONTROL_SET_POSITION ||
1461 i_ct == INPUT_CONTROL_SET_TIME ||
1462 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1463 i_ct == INPUT_CONTROL_SET_TITLE ||
1464 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1465 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1468 // msg_Dbg( p_input, "merged at %d", i );
1469 /* Remove the i-1 */
1470 for( j = i; j < p_input->p->i_control; j++ )
1471 p_input->p->control[j-1] = p_input->p->control[j];
1472 p_input->p->i_control--;
1476 /* TODO but that's not that important
1477 - merge SET_X with SET_X_CMD
1478 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1479 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1486 static bool Control( input_thread_t *p_input, int i_type,
1489 const mtime_t i_control_date = mdate();
1490 bool b_force_update = false;
1493 return b_force_update;
1497 case INPUT_CONTROL_SET_DIE:
1498 msg_Dbg( p_input, "control: stopping input" );
1500 /* Mark all submodules to die */
1501 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1504 case INPUT_CONTROL_SET_POSITION:
1505 case INPUT_CONTROL_SET_POSITION_OFFSET:
1509 if( p_input->p->b_recording )
1511 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1514 if( i_type == INPUT_CONTROL_SET_POSITION )
1516 f_pos = val.f_float;
1520 /* Should not fail */
1521 demux_Control( p_input->p->input.p_demux,
1522 DEMUX_GET_POSITION, &f_pos );
1523 f_pos += val.f_float;
1525 if( f_pos < 0.0 ) f_pos = 0.0;
1526 if( f_pos > 1.0 ) f_pos = 1.0;
1527 /* Reset the decoders states and clock sync (before calling the demuxer */
1528 input_EsOutChangePosition( p_input->p->p_es_out );
1529 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1532 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1533 "%2.1f%% failed", f_pos * 100 );
1537 if( p_input->p->i_slave > 0 )
1538 SlaveSeek( p_input );
1540 b_force_update = true;
1545 case INPUT_CONTROL_SET_TIME:
1546 case INPUT_CONTROL_SET_TIME_OFFSET:
1551 if( p_input->p->b_recording )
1553 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1557 if( i_type == INPUT_CONTROL_SET_TIME )
1559 i_time = val.i_time;
1563 /* Should not fail */
1564 demux_Control( p_input->p->input.p_demux,
1565 DEMUX_GET_TIME, &i_time );
1566 i_time += val.i_time;
1568 if( i_time < 0 ) i_time = 0;
1570 /* Reset the decoders states and clock sync (before calling the demuxer */
1571 input_EsOutChangePosition( p_input->p->p_es_out );
1573 i_ret = demux_Control( p_input->p->input.p_demux,
1574 DEMUX_SET_TIME, i_time );
1579 /* Emulate it with a SET_POS */
1580 demux_Control( p_input->p->input.p_demux,
1581 DEMUX_GET_LENGTH, &i_length );
1584 double f_pos = (double)i_time / (double)i_length;
1585 i_ret = demux_Control( p_input->p->input.p_demux,
1586 DEMUX_SET_POSITION, f_pos );
1591 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1592 " failed or not possible", i_time );
1596 if( p_input->p->i_slave > 0 )
1597 SlaveSeek( p_input );
1599 b_force_update = true;
1604 case INPUT_CONTROL_SET_STATE:
1605 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1606 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1609 if( p_input->p->input.p_access )
1610 i_ret = access_Control( p_input->p->input.p_access,
1611 ACCESS_SET_PAUSE_STATE, false );
1613 i_ret = demux_Control( p_input->p->input.p_demux,
1614 DEMUX_SET_PAUSE_STATE, false );
1618 /* FIXME What to do ? */
1619 msg_Warn( p_input, "cannot unset pause -> EOF" );
1620 vlc_mutex_unlock( &p_input->p->lock_control );
1621 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1622 vlc_mutex_lock( &p_input->p->lock_control );
1625 b_force_update = true;
1627 /* Switch to play */
1628 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1632 input_EsOutChangePause( p_input->p->p_es_out, false, i_control_date );
1634 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1635 p_input->p->b_can_pause )
1638 if( p_input->p->input.p_access )
1639 i_ret = access_Control( p_input->p->input.p_access,
1640 ACCESS_SET_PAUSE_STATE, true );
1642 i_ret = demux_Control( p_input->p->input.p_demux,
1643 DEMUX_SET_PAUSE_STATE, true );
1645 b_force_update = true;
1649 msg_Warn( p_input, "cannot set pause state" );
1650 state = p_input->i_state;
1657 /* Switch to new state */
1658 input_ChangeStateWithVarCallback( p_input, state, false );
1662 input_EsOutChangePause( p_input->p->p_es_out, true, i_control_date );
1664 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1666 b_force_update = true;
1668 /* Correct "state" value */
1669 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1671 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1673 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1677 case INPUT_CONTROL_SET_RATE:
1678 case INPUT_CONTROL_SET_RATE_SLOWER:
1679 case INPUT_CONTROL_SET_RATE_FASTER:
1683 if( i_type == INPUT_CONTROL_SET_RATE )
1689 static const int ppi_factor[][2] = {
1690 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1692 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1701 for( i = 0; ppi_factor[i][0] != 0; i++ )
1703 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1704 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1705 if( i_test_e < i_error )
1711 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1713 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1715 if( ppi_factor[i_idx+1][0] > 0 )
1716 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1718 i_rate = INPUT_RATE_MAX+1;
1722 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1724 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1726 i_rate = INPUT_RATE_MIN-1;
1730 if( i_rate < INPUT_RATE_MIN )
1732 msg_Dbg( p_input, "cannot set rate faster" );
1733 i_rate = INPUT_RATE_MIN;
1735 else if( i_rate > INPUT_RATE_MAX )
1737 msg_Dbg( p_input, "cannot set rate slower" );
1738 i_rate = INPUT_RATE_MAX;
1740 if( i_rate != INPUT_RATE_DEFAULT &&
1741 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1742 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1744 msg_Dbg( p_input, "cannot change rate" );
1745 i_rate = INPUT_RATE_DEFAULT;
1747 if( i_rate != p_input->p->i_rate &&
1748 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1751 if( p_input->p->input.p_access )
1752 i_ret = VLC_EGENERIC;
1754 i_ret = demux_Control( p_input->p->input.p_demux,
1755 DEMUX_SET_RATE, &i_rate );
1758 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1759 i_rate = p_input->p->i_rate;
1764 if( i_rate != p_input->p->i_rate )
1767 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1768 var_SetBool( p_input, "rate-change", true );
1770 p_input->p->i_rate = i_rate;
1772 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1773 if( p_input->p->input.b_rescale_ts )
1774 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1776 b_force_update = true;
1781 case INPUT_CONTROL_SET_PROGRAM:
1782 /* No need to force update, es_out does it if needed */
1783 es_out_Control( p_input->p->p_es_out,
1784 ES_OUT_SET_GROUP, val.i_int );
1786 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1790 case INPUT_CONTROL_SET_ES:
1791 /* No need to force update, es_out does it if needed */
1792 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1793 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1796 case INPUT_CONTROL_RESTART_ES:
1797 es_out_Control( p_input->p->p_es_out, ES_OUT_RESTART_ES,
1798 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1801 case INPUT_CONTROL_SET_AUDIO_DELAY:
1802 input_EsOutSetDelay( p_input->p->p_es_out,
1803 AUDIO_ES, val.i_time );
1804 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1807 case INPUT_CONTROL_SET_SPU_DELAY:
1808 input_EsOutSetDelay( p_input->p->p_es_out,
1809 SPU_ES, val.i_time );
1810 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1813 case INPUT_CONTROL_SET_TITLE:
1814 case INPUT_CONTROL_SET_TITLE_NEXT:
1815 case INPUT_CONTROL_SET_TITLE_PREV:
1816 if( p_input->p->b_recording )
1818 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1821 if( p_input->p->input.b_title_demux &&
1822 p_input->p->input.i_title > 0 )
1825 /* FIXME handle demux title */
1826 demux_t *p_demux = p_input->p->input.p_demux;
1829 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1830 i_title = p_demux->info.i_title - 1;
1831 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1832 i_title = p_demux->info.i_title + 1;
1834 i_title = val.i_int;
1836 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1838 input_EsOutChangePosition( p_input->p->p_es_out );
1840 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1841 input_ControlVarTitle( p_input, i_title );
1844 else if( p_input->p->input.i_title > 0 )
1846 access_t *p_access = p_input->p->input.p_access;
1849 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1850 i_title = p_access->info.i_title - 1;
1851 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1852 i_title = p_access->info.i_title + 1;
1854 i_title = val.i_int;
1856 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1858 input_EsOutChangePosition( p_input->p->p_es_out );
1860 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1861 stream_AccessReset( p_input->p->input.p_stream );
1865 case INPUT_CONTROL_SET_SEEKPOINT:
1866 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1867 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1868 if( p_input->p->b_recording )
1870 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1874 if( p_input->p->input.b_title_demux &&
1875 p_input->p->input.i_title > 0 )
1877 demux_t *p_demux = p_input->p->input.p_demux;
1879 int64_t i_input_time;
1880 int64_t i_seekpoint_time;
1882 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1884 i_seekpoint = p_demux->info.i_seekpoint;
1885 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1886 if( i_seekpoint_time >= 0 &&
1887 !demux_Control( p_demux,
1888 DEMUX_GET_TIME, &i_input_time ) )
1890 if( i_input_time < i_seekpoint_time + 3000000 )
1896 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1897 i_seekpoint = p_demux->info.i_seekpoint + 1;
1899 i_seekpoint = val.i_int;
1901 if( i_seekpoint >= 0 && i_seekpoint <
1902 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1905 input_EsOutChangePosition( p_input->p->p_es_out );
1907 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1910 else if( p_input->p->input.i_title > 0 )
1912 demux_t *p_demux = p_input->p->input.p_demux;
1913 access_t *p_access = p_input->p->input.p_access;
1915 int64_t i_input_time;
1916 int64_t i_seekpoint_time;
1918 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1920 i_seekpoint = p_access->info.i_seekpoint;
1921 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1922 if( i_seekpoint_time >= 0 &&
1923 demux_Control( p_demux,
1924 DEMUX_GET_TIME, &i_input_time ) )
1926 if( i_input_time < i_seekpoint_time + 3000000 )
1932 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1933 i_seekpoint = p_access->info.i_seekpoint + 1;
1935 i_seekpoint = val.i_int;
1937 if( i_seekpoint >= 0 && i_seekpoint <
1938 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1940 input_EsOutChangePosition( p_input->p->p_es_out );
1942 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1944 stream_AccessReset( p_input->p->input.p_stream );
1949 case INPUT_CONTROL_ADD_SUBTITLE:
1950 if( val.psz_string )
1952 SubtitleAdd( p_input, val.psz_string, true );
1953 free( val.psz_string );
1957 case INPUT_CONTROL_ADD_SLAVE:
1958 if( val.psz_string )
1960 input_source_t *slave = InputSourceNew( p_input );
1962 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1968 msg_Dbg( p_input, "adding %s as slave on the fly",
1972 if( demux_Control( p_input->p->input.p_demux,
1973 DEMUX_GET_TIME, &i_time ) )
1975 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1976 InputSourceClean( slave );
1980 if( demux_Control( slave->p_demux,
1981 DEMUX_SET_TIME, i_time ) )
1983 msg_Err( p_input, "seek failed for new slave" );
1984 InputSourceClean( slave );
1989 /* Get meta (access and demux) */
1990 p_meta = vlc_meta_New();
1991 access_Control( slave->p_access, ACCESS_GET_META,
1993 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1994 InputUpdateMeta( p_input, p_meta );
1996 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2001 msg_Warn( p_input, "failed to add %s as slave",
2005 free( val.psz_string );
2009 case INPUT_CONTROL_SET_RECORD_STATE:
2010 if( !!p_input->p->b_recording != !!val.b_bool )
2012 if( p_input->p->input.b_can_stream_record )
2014 if( demux_Control( p_input->p->input.p_demux,
2015 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2020 if( input_EsOutSetRecord( p_input->p->p_es_out, val.b_bool ) )
2023 p_input->p->b_recording = val.b_bool;
2025 var_Change( p_input, "record", VLC_VAR_SETVALUE, &val, NULL );
2027 b_force_update = true;
2031 case INPUT_CONTROL_SET_BOOKMARK:
2033 msg_Err( p_input, "not yet implemented" );
2037 return b_force_update;
2040 /*****************************************************************************
2042 *****************************************************************************/
2043 static int UpdateFromDemux( input_thread_t *p_input )
2045 demux_t *p_demux = p_input->p->input.p_demux;
2048 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2050 v.i_int = p_demux->info.i_title;
2051 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2053 input_ControlVarTitle( p_input, p_demux->info.i_title );
2055 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2057 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2059 v.i_int = p_demux->info.i_seekpoint;
2060 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2062 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2064 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2066 /* Hmmm only works with master input */
2067 if( p_input->p->input.p_demux == p_demux )
2069 int i_title_end = p_input->p->input.i_title_end -
2070 p_input->p->input.i_title_offset;
2071 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2072 p_input->p->input.i_seekpoint_offset;
2074 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2076 if( p_demux->info.i_title > i_title_end ||
2077 ( p_demux->info.i_title == i_title_end &&
2078 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2080 else if( i_seekpoint_end >=0 )
2082 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2084 else if( i_title_end >= 0 )
2086 if( p_demux->info.i_title > i_title_end ) return 0;
2093 /*****************************************************************************
2095 *****************************************************************************/
2096 static int UpdateFromAccess( input_thread_t *p_input )
2098 access_t *p_access = p_input->p->input.p_access;
2101 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2103 v.i_int = p_access->info.i_title;
2104 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2106 input_ControlVarTitle( p_input, p_access->info.i_title );
2108 stream_AccessUpdate( p_input->p->input.p_stream );
2110 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2112 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2114 v.i_int = p_access->info.i_seekpoint;
2115 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2116 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2118 if( p_access->info.i_update & INPUT_UPDATE_META )
2120 /* TODO maybe multi - access ? */
2121 vlc_meta_t *p_meta = vlc_meta_New();
2122 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2123 InputUpdateMeta( p_input, p_meta );
2124 p_access->info.i_update &= ~INPUT_UPDATE_META;
2126 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2131 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2132 f_quality = f_strength = -1;
2134 var_SetFloat( p_input, "signal-quality", f_quality );
2135 var_SetFloat( p_input, "signal-strength", f_strength );
2137 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2140 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2142 /* Hmmm only works with master input */
2143 if( p_input->p->input.p_access == p_access )
2145 int i_title_end = p_input->p->input.i_title_end -
2146 p_input->p->input.i_title_offset;
2147 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2148 p_input->p->input.i_seekpoint_offset;
2150 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2152 if( p_access->info.i_title > i_title_end ||
2153 ( p_access->info.i_title == i_title_end &&
2154 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2156 else if( i_seekpoint_end >=0 )
2158 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2160 else if( i_title_end >= 0 )
2162 if( p_access->info.i_title > i_title_end ) return 0;
2169 /*****************************************************************************
2171 *****************************************************************************/
2172 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2174 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2177 /*****************************************************************************
2179 *****************************************************************************/
2180 static input_source_t *InputSourceNew( input_thread_t *p_input )
2182 VLC_UNUSED(p_input);
2183 input_source_t *in = malloc( sizeof( input_source_t ) );
2185 memset( in, 0, sizeof( input_source_t ) );
2189 /*****************************************************************************
2191 *****************************************************************************/
2192 static int InputSourceInit( input_thread_t *p_input,
2193 input_source_t *in, const char *psz_mrl,
2194 const char *psz_forced_demux )
2196 const bool b_master = in == &p_input->p->input;
2198 char psz_dup[strlen(psz_mrl) + 1];
2199 const char *psz_access;
2200 const char *psz_demux;
2207 strcpy( psz_dup, psz_mrl );
2209 if( !in ) return VLC_EGENERIC;
2210 if( !p_input ) return VLC_EGENERIC;
2213 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2215 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2216 psz_mrl, psz_access, psz_demux, psz_path );
2217 if( !p_input->b_preparsing )
2219 /* Hack to allow udp://@:port syntax */
2221 (strncmp( psz_access, "udp", 3 ) &&
2222 strncmp( psz_access, "rtp", 3 )) )
2224 /* Find optional titles and seekpoints */
2225 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2226 &in->i_seekpoint_start, &in->i_seekpoint_end );
2229 if( psz_forced_demux && *psz_forced_demux )
2231 psz_demux = psz_forced_demux;
2233 else if( *psz_demux == '\0' )
2235 /* special hack for forcing a demuxer with --demux=module
2236 * (and do nothing with a list) */
2237 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2239 if( psz_var_demux != NULL &&
2240 !strchr(psz_var_demux, ',' ) &&
2241 !strchr(psz_var_demux, ':' ) )
2243 psz_demux = psz_var_demux;
2245 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2249 /* Try access_demux first */
2250 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2251 NULL, p_input->p->p_es_out, false );
2255 /* Preparsing is only for file:// */
2258 if( !*psz_access ) /* path without scheme:// */
2259 psz_access = "file";
2260 if( strcmp( psz_access, "file" ) )
2262 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2267 int64_t i_pts_delay;
2269 /* Get infos from access_demux */
2270 demux_Control( in->p_demux,
2271 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2272 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2274 in->b_title_demux = true;
2275 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2276 &in->title, &in->i_title,
2277 &in->i_title_offset, &in->i_seekpoint_offset ) )
2279 TAB_INIT( in->i_title, in->title );
2281 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2282 &in->b_can_pace_control ) )
2283 in->b_can_pace_control = false;
2285 if( !in->b_can_pace_control )
2287 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2288 &in->b_can_rate_control, &in->b_rescale_ts ) )
2290 in->b_can_rate_control = false;
2291 in->b_rescale_ts = true; /* not used */
2296 in->b_can_rate_control = true;
2297 in->b_rescale_ts = true;
2299 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2300 &in->b_can_pause ) )
2301 in->b_can_pause = false;
2302 var_SetBool( p_input, "can-pause", in->b_can_pause );
2304 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2306 if( ret != VLC_SUCCESS )
2308 var_Set( p_input, "seekable", val );
2312 int64_t i_pts_delay;
2315 input_ChangeState( p_input, OPENING_S );
2317 /* Now try a real access */
2318 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2320 /* Access failed, URL encoded ? */
2321 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2323 decode_URI( psz_path );
2325 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2326 psz_access, psz_demux, psz_path );
2328 in->p_access = access_New( p_input,
2329 psz_access, psz_demux, psz_path );
2331 if( in->p_access == NULL )
2333 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2335 intf_UserFatal( VLC_OBJECT( p_input), false,
2336 _("Your input can't be opened"),
2337 _("VLC is unable to open the MRL '%s'."
2338 " Check the log for details."), psz_mrl );
2343 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2344 while( psz && *psz )
2346 access_t *p_access = in->p_access;
2347 char *end = strchr( psz, ':' );
2352 in->p_access = access_FilterNew( in->p_access, psz );
2353 if( in->p_access == NULL )
2355 in->p_access = p_access;
2356 msg_Warn( p_input, "failed to insert access filter %s",
2364 /* Get infos from access */
2365 if( !p_input->b_preparsing )
2367 access_Control( in->p_access,
2368 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2369 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2371 in->b_title_demux = false;
2372 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2373 &in->title, &in->i_title,
2374 &in->i_title_offset, &in->i_seekpoint_offset ) )
2377 TAB_INIT( in->i_title, in->title );
2379 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2380 &in->b_can_pace_control );
2381 in->b_can_rate_control = in->b_can_pace_control;
2382 in->b_rescale_ts = true;
2384 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2386 var_SetBool( p_input, "can-pause", in->b_can_pause );
2387 access_Control( in->p_access, ACCESS_CAN_SEEK,
2389 var_Set( p_input, "seekable", val );
2393 input_ChangeState( p_input, BUFFERING_S );
2395 /* Autodetect extra files if none specified */
2396 char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2397 if( !psz_input_list )
2399 char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2400 if( psz_extra_files )
2401 var_SetString( p_input, "input-list", psz_extra_files );
2402 free( psz_extra_files );
2405 /* Create the stream_t */
2406 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2408 /* Restor old value */
2409 if( !psz_input_list )
2410 var_SetString( p_input, "input-list", "" );
2411 free( psz_input_list );
2413 if( in->p_stream == NULL )
2415 msg_Warn( p_input, "cannot create a stream_t from access" );
2419 /* Open a demuxer */
2420 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2422 psz_demux = in->p_access->psz_demux;
2426 /* Take access redirections into account */
2427 char *psz_real_path;
2428 char *psz_buf = NULL;
2429 if( in->p_access->psz_path )
2431 const char *psz_a, *psz_d;
2432 psz_buf = strdup( in->p_access->psz_path );
2433 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2437 psz_real_path = psz_path;
2439 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2441 in->p_stream, p_input->p->p_es_out,
2442 p_input->b_preparsing );
2446 if( in->p_demux == NULL )
2448 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2449 psz_access, psz_demux, psz_path );
2450 intf_UserFatal( VLC_OBJECT( p_input ), false,
2451 _("VLC can't recognize the input's format"),
2452 _("The format of '%s' cannot be detected. "
2453 "Have a look at the log for details."), psz_mrl );
2457 /* Get title from demux */
2458 if( !p_input->b_preparsing && in->i_title <= 0 )
2460 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2461 &in->title, &in->i_title,
2462 &in->i_title_offset, &in->i_seekpoint_offset ))
2464 TAB_INIT( in->i_title, in->title );
2468 in->b_title_demux = true;
2473 /* Set record capabilities */
2474 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2475 in->b_can_stream_record = false;
2477 if( !var_CreateGetBool( p_input, "input-record-native" ) )
2478 in->b_can_stream_record = false;
2479 var_SetBool( p_input, "can-record", true );
2481 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2485 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2486 if( 1 || !p_input->b_preparsing )
2489 input_attachment_t **attachment;
2490 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2491 &attachment, &i_attachment ) )
2493 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2494 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2495 i_attachment, attachment );
2496 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2499 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2501 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2503 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2506 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2507 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2513 input_ChangeState( p_input, ERROR_S );
2516 demux_Delete( in->p_demux );
2519 stream_Delete( in->p_stream );
2522 access_Delete( in->p_access );
2524 return VLC_EGENERIC;
2527 /*****************************************************************************
2529 *****************************************************************************/
2530 static void InputSourceClean( input_source_t *in )
2535 demux_Delete( in->p_demux );
2538 stream_Delete( in->p_stream );
2541 access_Delete( in->p_access );
2543 if( in->i_title > 0 )
2545 for( i = 0; i < in->i_title; i++ )
2546 vlc_input_title_Delete( in->title[i] );
2547 TAB_CLEAN( in->i_title, in->title );
2551 static void SlaveDemux( input_thread_t *p_input )
2555 bool b_set_time = true;
2557 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2559 /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2563 for( i = 0; i < p_input->p->i_slave; i++ )
2565 input_source_t *in = p_input->p->slave[i];
2571 if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2576 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2578 msg_Err( p_input, "slave[%d] doesn't like "
2579 "DEMUX_GET_TIME -> EOF", i );
2584 if( i_stime >= i_time )
2587 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2593 i_ret = in->p_demux->pf_demux( in->p_demux );
2598 msg_Dbg( p_input, "slave %d EOF", i );
2604 static void SlaveSeek( input_thread_t *p_input )
2609 if( !p_input ) return;
2611 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2613 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2617 for( i = 0; i < p_input->p->i_slave; i++ )
2619 input_source_t *in = p_input->p->slave[i];
2621 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2624 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2634 /*****************************************************************************
2636 *****************************************************************************/
2637 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2641 if( !p_meta ) return;
2643 /* Get meta information from user */
2644 #define GET_META( field, s ) \
2645 var_Get( p_input, (s), &val ); \
2646 if( *val.psz_string ) \
2647 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2648 free( val.psz_string )
2650 GET_META( Title, "meta-title" );
2651 GET_META( Artist, "meta-artist" );
2652 GET_META( Genre, "meta-genre" );
2653 GET_META( Copyright, "meta-copyright" );
2654 GET_META( Description, "meta-description" );
2655 GET_META( Date, "meta-date" );
2656 GET_META( URL, "meta-url" );
2660 /*****************************************************************************
2661 * InputGetExtraFiles
2662 * Autodetect extra input list
2663 *****************************************************************************/
2664 static char *InputGetExtraFiles( input_thread_t *p_input,
2665 const char *psz_access, const char *psz_path )
2667 char *psz_list = NULL;
2669 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2673 const char *psz_ext = strrchr( psz_path, '.' );
2674 if( !psz_ext || strcmp( psz_ext, ".001" ) )
2677 char *psz_file = strdup( psz_path );
2681 /* Try to list .xyz files */
2682 for( int i = 2; i < 999; i++ )
2684 char *psz_ext = strrchr( psz_file, '.' );
2687 snprintf( psz_ext, 5, ".%.3d", i );
2689 if( utf8_stat( psz_file, &st )
2690 || !S_ISREG( st.st_mode ) || !st.st_size )
2693 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2697 char *psz_old = psz_list;
2698 /* FIXME how to handle file with ',' ?*/
2699 if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2707 psz_list = strdup( psz_file );
2715 /*****************************************************************************
2716 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2717 * arturl and locking issue.
2718 *****************************************************************************/
2719 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2721 input_item_t *p_item = p_input->p->input.p_item;
2722 char * psz_arturl = NULL;
2723 char *psz_title = NULL;
2724 int i_arturl_event = false;
2729 psz_arturl = input_item_GetArtURL( p_item );
2731 vlc_mutex_lock( &p_item->lock );
2732 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2733 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2735 vlc_meta_Merge( p_item->p_meta, p_meta );
2737 if( psz_arturl && *psz_arturl )
2739 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2740 i_arturl_event = true;
2743 vlc_meta_Delete( p_meta );
2745 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2747 /* Don't look for art cover if sout
2748 * XXX It can change when sout has meta data support */
2749 if( p_input->p->p_sout && !p_input->b_preparsing )
2751 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2752 i_arturl_event = true;
2756 input_ExtractAttachmentAndCacheArt( p_input );
2762 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2764 p_meta = vlc_meta_New();
2765 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2767 vlc_mutex_unlock( &p_item->lock );
2769 input_item_SetPreparsed( p_item, true );
2771 if( i_arturl_event == true )
2775 /* Notify interested third parties */
2776 event.type = vlc_InputItemMetaChanged;
2777 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2778 vlc_event_send( &p_item->event_manager, &event );
2783 input_Control( p_input, INPUT_SET_NAME, psz_title );
2787 /** \todo handle sout meta */
2791 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2792 int i_new, input_attachment_t **pp_new )
2794 int i_attachment = *pi_attachment;
2795 input_attachment_t **attachment = *ppp_attachment;
2798 attachment = realloc( attachment,
2799 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2800 for( i = 0; i < i_new; i++ )
2801 attachment[i_attachment++] = pp_new[i];
2805 *pi_attachment = i_attachment;
2806 *ppp_attachment = attachment;
2809 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2813 if( p_input->b_preparsing )
2816 if( p_input->p->input.p_access )
2817 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2820 /* Get meta data from slave input */
2821 for( i = 0; i < p_input->p->i_slave; i++ )
2823 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2824 if( p_input->p->slave[i]->p_access )
2826 access_Control( p_input->p->slave[i]->p_access,
2827 ACCESS_GET_META, p_meta );
2832 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2839 /* XXX I am not sure it is a great idea, besides, there is more than that
2840 * if we want to do it right */
2841 vlc_mutex_lock( &p_item->lock );
2842 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2844 vlc_mutex_unlock( &p_item->lock );
2847 vlc_mutex_unlock( &p_item->lock );
2850 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2851 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2856 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2857 if(! p_demux->p_private )
2860 p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2863 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2865 if( p_demux_meta->p_meta )
2867 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2868 vlc_meta_Delete( p_demux_meta->p_meta );
2871 if( p_demux_meta->i_attachments > 0 )
2873 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2874 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2875 p_demux_meta->i_attachments, p_demux_meta->attachments );
2876 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2878 module_unneed( p_demux, p_id3 );
2880 free( p_demux->p_private );
2884 /*****************************************************************************
2885 * MRLSplit: parse the access, demux and url part of the
2886 * Media Resource Locator.
2887 *****************************************************************************/
2888 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2891 char *psz_access = NULL;
2892 char *psz_demux = NULL;
2895 /* Either there is an access/demux specification before ://
2896 * or we have a plain local file path. */
2897 psz_path = strstr( psz_dup, "://" );
2898 if( psz_path != NULL )
2901 psz_path += 3; /* skips "://" */
2903 /* Separate access from demux (<access>/<demux>://<path>) */
2904 psz_access = psz_dup;
2905 psz_demux = strchr( psz_access, '/' );
2907 *psz_demux++ = '\0';
2909 /* We really don't want module name substitution here! */
2910 if( psz_access[0] == '$' )
2912 if( psz_demux && psz_demux[0] == '$' )
2919 *ppsz_access = psz_access ? psz_access : (char*)"";
2920 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2921 *ppsz_path = psz_path;
2924 static inline bool next(char ** src)
2928 long result = strtol( *src, &end, 0 );
2929 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
2938 /*****************************************************************************
2939 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2942 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2943 *****************************************************************************/
2944 static void MRLSections( input_thread_t *p_input, char *psz_source,
2945 int *pi_title_start, int *pi_title_end,
2946 int *pi_chapter_start, int *pi_chapter_end )
2948 char *psz, *psz_end, *psz_next, *psz_check;
2950 *pi_title_start = *pi_title_end = -1;
2951 *pi_chapter_start = *pi_chapter_end = -1;
2953 /* Start by parsing titles and chapters */
2954 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2957 /* Check we are really dealing with a title/chapter section */
2958 psz_check = psz + 1;
2959 if( !*psz_check ) return;
2960 if( isdigit(*psz_check) )
2961 if(!next(&psz_check)) return;
2962 if( *psz_check != ':' && *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 != ':' && *psz_check ) return;
2975 if( *psz_check == ':' && ++psz_check )
2977 if( isdigit(*psz_check) )
2978 if(!next(&psz_check)) return;
2980 if( *psz_check ) return;
2982 /* Separate start and end */
2984 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2986 /* Look for the start title */
2987 *pi_title_start = strtol( psz, &psz_next, 0 );
2988 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2989 *pi_title_end = *pi_title_start;
2992 /* Look for the start chapter */
2994 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2995 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2996 *pi_chapter_end = *pi_chapter_start;
3000 /* Look for the end title */
3001 *pi_title_end = strtol( psz_end, &psz_next, 0 );
3002 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3005 /* Look for the end chapter */
3006 if( *psz_end ) psz_end++;
3007 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3008 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3011 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3012 psz_source, *pi_title_start, *pi_chapter_start,
3013 *pi_title_end, *pi_chapter_end );
3016 /*****************************************************************************
3017 * input_AddSubtitles: add a subtitles file and enable it
3018 *****************************************************************************/
3019 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3021 input_source_t *sub;
3024 char *psz_path, *psz_extension;
3026 /* if we are provided a subtitle.sub file,
3027 * see if we don't have a subtitle.idx and use it instead */
3028 psz_path = strdup( psz_subtitle );
3031 psz_extension = strrchr( psz_path, '.');
3032 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3036 strcpy( psz_extension, ".idx" );
3038 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3040 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3041 psz_path, psz_subtitle );
3042 strcpy( psz_subtitle, psz_path );
3048 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3050 sub = InputSourceNew( p_input );
3051 if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3056 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3059 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3061 if( count.i_int == 0 )
3063 /* if it was first one, there is disable too */
3065 if( count.i_int < list.p_list->i_count )
3067 int i_id = list.p_list->p_values[count.i_int].i_int;
3068 es_out_id_t *p_es = input_EsOutGetFromID( p_input->p->p_es_out, i_id );
3070 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_DEFAULT, p_es );
3071 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES, p_es );
3073 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3077 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3078 bool b_check_extension )
3082 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3085 assert( psz_subtitle != NULL );
3087 val.psz_string = strdup( psz_subtitle );
3088 if( val.psz_string )
3089 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3093 /*****************************************************************************
3094 * input_get_event_manager
3095 *****************************************************************************/
3096 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3098 return &p_input->p->event_manager;
3102 /* TODO FIXME nearly the same logic that snapshot code */
3103 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3108 path = utf8_opendir( psz_path );
3113 char *psz_tmp = str_format( p_obj, psz_prefix );
3117 filename_sanitize( psz_tmp );
3118 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3120 psz_extension ? "." : "",
3121 psz_extension ? psz_extension : "" ) < 0 )
3128 psz_file = str_format( p_obj, psz_path );
3129 path_sanitize( psz_file );