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->pts_adjust.b_auto_adjust = var_GetBool( p_input, "auto-adjust-pts-delay" );
249 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
251 if( !p_input->b_preparsing )
253 var_Get( p_input, "bookmarks", &val );
256 /* FIXME: have a common cfg parsing routine used by sout and others */
257 char *psz_parser, *psz_start, *psz_end;
258 psz_parser = val.psz_string;
259 while( (psz_start = strchr( psz_parser, '{' ) ) )
261 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
264 psz_end = strchr( psz_start, '}' );
265 if( !psz_end ) break;
266 psz_parser = psz_end + 1;
267 backup = *psz_parser;
270 while( (psz_end = strchr( psz_start, ',' ) ) )
273 if( !strncmp( psz_start, "name=", 5 ) )
275 p_seekpoint->psz_name = strdup(psz_start + 5);
277 else if( !strncmp( psz_start, "bytes=", 6 ) )
279 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
281 else if( !strncmp( psz_start, "time=", 5 ) )
283 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
286 psz_start = psz_end + 1;
288 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
289 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
290 p_seekpoint->i_time_offset );
291 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
292 vlc_seekpoint_Delete( p_seekpoint );
293 *psz_parser = backup;
295 free( val.psz_string );
299 /* Remove 'Now playing' info as it is probably outdated */
300 input_item_SetNowPlaying( p_item, NULL );
303 if( p_input->b_preparsing )
304 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
308 p_input->p->p_sout = p_sout;
310 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
311 vlc_mutex_init( &p_input->p->counters.counters_lock );
313 /* Set the destructor when we are sure we are initialized */
314 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
316 /* Attach only once we are ready */
317 vlc_object_attach( p_input, p_parent );
323 * Input destructor (called when the object's refcount reaches 0).
325 static void Destructor( input_thread_t * p_input )
328 char * psz_name = input_item_GetName( p_input->p->input.p_item );
329 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
333 vlc_event_manager_fini( &p_input->p->event_manager );
335 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
336 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
338 if( p_input->p->p_sout )
339 sout_DeleteInstance( p_input->p->p_sout );
341 vlc_gc_decref( p_input->p->input.p_item );
343 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
345 vlc_cond_destroy( &p_input->p->wait_control );
346 vlc_mutex_destroy( &p_input->p->lock_control );
351 * Initialize an input thread and run it. You will need to monitor the
352 * thread to clean up after it is done
354 * \param p_parent a vlc_object
355 * \param p_item an input item
356 * \return a pointer to the spawned input thread
358 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
359 input_item_t *p_item )
361 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
365 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
366 input_item_t *p_item,
367 const char *psz_log, sout_instance_t *p_sout )
369 input_thread_t *p_input;
371 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
375 /* Create thread and wait for its readiness. */
376 if( vlc_thread_create( p_input, "input", Run,
377 VLC_THREAD_PRIORITY_INPUT, false ) )
379 input_ChangeState( p_input, ERROR_S );
380 msg_Err( p_input, "cannot create input thread" );
381 vlc_object_detach( p_input );
382 vlc_object_release( p_input );
390 * Initialize an input thread and run it. This thread will clean after itself,
391 * you can forget about it. It can work either in blocking or non-blocking mode
393 * \param p_parent a vlc_object
394 * \param p_item an input item
395 * \param b_block should we block until read is finished ?
396 * \return an error code, VLC_SUCCESS on success
398 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
401 input_thread_t *p_input;
403 p_input = Create( p_parent, p_item, NULL, false, NULL );
409 RunAndDestroy( VLC_OBJECT(p_input) );
414 if( vlc_thread_create( p_input, "input", RunAndDestroy,
415 VLC_THREAD_PRIORITY_INPUT, false ) )
417 input_ChangeState( p_input, ERROR_S );
418 msg_Err( p_input, "cannot create input thread" );
419 vlc_object_release( p_input );
427 * Initialize an input and initialize it to preparse the item
428 * This function is blocking. It will only accept to parse files
430 * \param p_parent a vlc_object_t
431 * \param p_item an input item
432 * \return VLC_SUCCESS or an error
434 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
436 input_thread_t *p_input;
438 /* Allocate descriptor */
439 p_input = Create( p_parent, p_item, NULL, true, NULL );
443 if( !Init( p_input ) )
446 vlc_object_detach( p_input );
447 vlc_object_release( p_input );
453 * Request a running input thread to stop and die
455 * \param the input thread to stop
457 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
462 if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
463 p_obj->i_object_type == VLC_OBJECT_AOUT ||
464 p_obj == VLC_OBJECT(p_input->p->p_sout) )
467 vlc_object_kill( p_obj );
469 p_list = vlc_list_children( p_obj );
470 for( i = 0; i < p_list->i_count; i++ )
471 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
472 vlc_list_release( p_list );
474 void input_StopThread( input_thread_t *p_input )
476 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
477 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
478 * unlock the control loop */
479 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
481 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
484 sout_instance_t * input_DetachSout( input_thread_t *p_input )
486 sout_instance_t *p_sout = p_input->p->p_sout;
487 vlc_object_detach( p_sout );
488 p_input->p->p_sout = NULL;
492 /*****************************************************************************
493 * Run: main thread loop
494 * This is the "normal" thread that spawns the input processing chain,
495 * reads the stream, cleans up and waits
496 *****************************************************************************/
497 static void* Run( vlc_object_t *p_this )
499 input_thread_t *p_input = (input_thread_t *)p_this;
500 const int canc = vlc_savecancel();
502 if( Init( p_input ) )
504 /* If we failed, wait before we are killed, and exit */
511 /* Wait until we are asked to die */
512 if( !p_input->b_die )
519 p_input->b_dead = true;
520 vlc_restorecancel( canc );
524 /*****************************************************************************
525 * RunAndDestroy: main thread loop
526 * This is the "just forget me" thread that spawns the input processing chain,
527 * reads the stream, cleans up and releases memory
528 *****************************************************************************/
529 static void* RunAndDestroy( vlc_object_t *p_this )
531 input_thread_t *p_input = (input_thread_t *)p_this;
532 const int canc = vlc_savecancel();
534 if( Init( p_input ) )
544 vlc_object_release( p_input );
545 vlc_restorecancel( canc );
549 /*****************************************************************************
550 * Main loop: Fill buffers from access, and demux
551 *****************************************************************************/
555 * It asks the demuxer to demux some data
557 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t *pi_start_mdate )
563 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
564 ( p_input->p->i_run > 0 && *pi_start_mdate+p_input->p->i_run < mdate() ) )
567 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
572 if( p_input->p->input.b_title_demux &&
573 p_input->p->input.p_demux->info.i_update )
575 i_ret = UpdateFromDemux( p_input );
578 else if( !p_input->p->input.b_title_demux &&
579 p_input->p->input.p_access &&
580 p_input->p->input.p_access->info.i_update )
582 i_ret = UpdateFromAccess( p_input );
587 if( i_ret == 0 ) /* EOF */
591 var_Get( p_input, "input-repeat", &repeat );
592 if( repeat.i_int == 0 )
594 /* End of file - we do not set b_die because only the
595 * playlist is allowed to do so. */
596 msg_Dbg( p_input, "EOF reached" );
597 p_input->p->input.b_eof = true;
603 msg_Dbg( p_input, "repeating the same input (%d)",
605 if( repeat.i_int > 0 )
608 var_Set( p_input, "input-repeat", repeat );
611 /* Seek to start title/seekpoint */
612 val.i_int = p_input->p->input.i_title_start -
613 p_input->p->input.i_title_offset;
614 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
616 input_ControlPush( p_input,
617 INPUT_CONTROL_SET_TITLE, &val );
619 val.i_int = p_input->p->input.i_seekpoint_start -
620 p_input->p->input.i_seekpoint_offset;
621 if( val.i_int > 0 /* TODO: check upper boundary */ )
622 input_ControlPush( p_input,
623 INPUT_CONTROL_SET_SEEKPOINT, &val );
625 /* Seek to start position */
626 if( p_input->p->i_start > 0 )
628 val.i_time = p_input->p->i_start;
629 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
635 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
640 *pi_start_mdate = mdate();
645 input_ChangeState( p_input, ERROR_S );
648 if( i_ret > 0 && p_input->p->i_slave > 0 )
650 SlaveDemux( p_input );
656 * It update the variables used by the interfaces
658 static void MainLoopInterface( input_thread_t *p_input )
662 int64_t i_time, i_length;
664 /* update input status variables */
665 if( !demux_Control( p_input->p->input.p_demux,
666 DEMUX_GET_POSITION, &f_pos ) )
668 val.f_float = (float)f_pos;
669 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
671 if( !demux_Control( p_input->p->input.p_demux,
672 DEMUX_GET_TIME, &i_time ) )
674 p_input->i_time = i_time;
676 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
678 if( !demux_Control( p_input->p->input.p_demux,
679 DEMUX_GET_LENGTH, &i_length ) )
682 var_Get( p_input, "length", &old_val );
683 val.i_time = i_length;
684 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
686 if( old_val.i_time != val.i_time )
688 UpdateItemLength( p_input, i_length );
692 var_SetBool( p_input, "intf-change", true );
697 * It updates the globals statics
699 static void MainLoopStatistic( input_thread_t *p_input )
701 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
702 /* Are we the thread responsible for computing global stats ? */
703 if( libvlc_priv( p_input->p_libvlc )->p_stats_computer == p_input )
705 stats_ComputeGlobalStats( p_input->p_libvlc,
706 p_input->p_libvlc->p_stats );
712 * The main input loop.
714 static void MainLoop( input_thread_t *p_input )
716 mtime_t i_start_mdate = mdate();
717 mtime_t i_intf_update = 0;
718 mtime_t i_statistic_update = 0;
720 /* Start the timer */
721 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
723 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
733 b_force_update = false;
735 if( p_input->i_state != PAUSE_S )
737 MainLoopDemux( p_input, &b_force_update, &i_start_mdate );
738 i_wakeup = input_EsOutGetWakeup( p_input->p->p_es_out );
743 i_deadline = i_wakeup;
744 if( p_input->i_state == PAUSE_S )
745 i_deadline = __MIN( i_intf_update, i_statistic_update );
748 vlc_mutex_lock( &p_input->p->lock_control );
749 ControlReduce( p_input );
750 while( !ControlPopNoLock( p_input, &i_type, &val, i_deadline ) )
752 msg_Dbg( p_input, "control type=%d", i_type );
753 if( Control( p_input, i_type, val ) )
754 b_force_update = true;
756 vlc_mutex_unlock( &p_input->p->lock_control );
758 /* Update interface and statistics */
760 if( i_intf_update < i_current || b_force_update )
762 MainLoopInterface( p_input );
763 i_intf_update = i_current + INT64_C(250000);
764 b_force_update = false;
766 if( i_statistic_update < i_current )
768 MainLoopStatistic( p_input );
769 i_statistic_update = i_current + INT64_C(1000000);
772 /* Check if i_wakeup is still valid */
775 mtime_t i_new_wakeup = input_EsOutGetWakeup( p_input->p->p_es_out );
778 msg_Err( p_input, "RESET" );
782 } while( i_current < i_wakeup );
785 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
787 /* We have finish to demux data but not to play them */
788 while( !p_input->b_die )
790 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
793 msg_Dbg( p_input, "waiting decoder fifos to empty" );
795 msleep( INPUT_IDLE_SLEEP );
798 /* We have finished */
799 input_ChangeState( p_input, END_S );
803 static void InitStatistics( input_thread_t * p_input )
805 if( p_input->b_preparsing ) return;
807 /* Prepare statistics */
808 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
809 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
810 if( libvlc_stats( p_input ) )
812 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
813 INIT_COUNTER( read_packets, INTEGER, COUNTER );
814 INIT_COUNTER( demux_read, INTEGER, COUNTER );
815 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
816 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
817 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
818 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
819 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
820 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
821 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
822 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
823 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
824 p_input->p->counters.p_sout_send_bitrate = NULL;
825 p_input->p->counters.p_sout_sent_packets = NULL;
826 p_input->p->counters.p_sout_sent_bytes = NULL;
827 if( p_input->p->counters.p_demux_bitrate )
828 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
829 if( p_input->p->counters.p_input_bitrate )
830 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
835 static int InitSout( input_thread_t * p_input )
839 if( p_input->b_preparsing ) return VLC_SUCCESS;
841 /* Find a usable sout and attach it to p_input */
842 psz = var_GetNonEmptyString( p_input, "sout" );
843 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
845 /* Check the validity of the provided sout */
846 if( p_input->p->p_sout )
848 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
850 msg_Dbg( p_input, "destroying unusable sout" );
852 sout_DeleteInstance( p_input->p->p_sout );
853 p_input->p->p_sout = NULL;
857 if( p_input->p->p_sout )
860 msg_Dbg( p_input, "sout keep: reusing sout" );
861 msg_Dbg( p_input, "sout keep: you probably want to use "
862 "gather stream_out" );
863 vlc_object_attach( p_input->p->p_sout, p_input );
867 /* Create a new one */
868 p_input->p->p_sout = sout_NewInstance( p_input, psz );
869 if( !p_input->p->p_sout )
871 input_ChangeState( p_input, ERROR_S );
872 msg_Err( p_input, "cannot start stream output instance, " \
878 if( libvlc_stats( p_input ) )
880 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
881 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
882 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
883 if( p_input->p->counters.p_sout_send_bitrate )
884 p_input->p->counters.p_sout_send_bitrate->update_interval =
888 else if( p_input->p->p_sout )
890 msg_Dbg( p_input, "destroying useless sout" );
892 sout_DeleteInstance( p_input->p->p_sout );
893 p_input->p->p_sout = NULL;
901 static void InitTitle( input_thread_t * p_input )
905 if( p_input->b_preparsing ) return;
907 /* Create global title (from master) */
908 p_input->p->i_title = p_input->p->input.i_title;
909 p_input->p->title = p_input->p->input.title;
910 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
911 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
912 if( p_input->p->i_title > 0 )
914 /* Setup variables */
915 input_ControlVarNavigation( p_input );
916 input_ControlVarTitle( p_input, 0 );
920 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
921 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
922 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
925 if( p_input->i_pts_delay < 0 )
926 p_input->i_pts_delay = 0;
928 /* If the desynchronisation requested by the user is < 0, we need to
929 * cache more data. */
930 var_Get( p_input, "audio-desync", &val );
931 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
933 /* Update cr_average depending on the caching */
934 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
935 p_input->p->input.i_cr_average /= 10;
936 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
939 static void StartTitle( input_thread_t * p_input )
948 /* Start title/chapter */
950 if( p_input->b_preparsing )
952 p_input->p->i_start = 0;
956 val.i_int = p_input->p->input.i_title_start -
957 p_input->p->input.i_title_offset;
958 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
959 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
960 val.i_int = p_input->p->input.i_seekpoint_start -
961 p_input->p->input.i_seekpoint_offset;
962 if( val.i_int > 0 /* TODO: check upper boundary */ )
963 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
967 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
968 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
969 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
970 i_length = var_GetTime( p_input, "length" );
971 if( p_input->p->i_run < 0 )
973 msg_Warn( p_input, "invalid run-time ignored" );
974 p_input->p->i_run = 0;
977 if( p_input->p->i_start > 0 )
979 if( p_input->p->i_start >= i_length )
981 msg_Warn( p_input, "invalid start-time ignored" );
987 msg_Dbg( p_input, "starting at time: %ds",
988 (int)( p_input->p->i_start / INT64_C(1000000) ) );
990 s.i_time = p_input->p->i_start;
991 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
994 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
996 msg_Warn( p_input, "invalid stop-time ignored" );
997 p_input->p->i_stop = 0;
1000 /* Load subtitles */
1001 /* Get fps and set it if not already set */
1002 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
1005 float f_requested_fps;
1007 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
1008 var_SetFloat( p_input, "sub-original-fps", f_fps );
1010 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1011 if( f_requested_fps != f_fps )
1013 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1014 VLC_VAR_DOINHERIT );
1015 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1019 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1022 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1025 /* Look for and add subtitle files */
1026 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1027 if( psz_subtitle != NULL )
1029 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1030 SubtitleAdd( p_input, psz_subtitle, true );
1033 var_Get( p_input, "sub-autodetect-file", &val );
1036 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1037 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1038 p_input->p->input.p_item->psz_uri );
1039 free( psz_autopath );
1041 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1043 /* Try to autoselect the first autodetected subtitles file
1044 * if no subtitles file was specified */
1045 bool b_forced = i == 0 && !psz_subtitle;
1047 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1048 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1050 free( ppsz_subs[i] );
1054 free( psz_subtitle );
1056 /* Look for slave */
1057 psz = var_GetNonEmptyString( p_input, "input-slave" );
1061 input_source_t *slave;
1062 while( psz && *psz )
1064 while( *psz == ' ' || *psz == '#' )
1068 if( ( psz_delim = strchr( psz, '#' ) ) )
1070 *psz_delim++ = '\0';
1077 msg_Dbg( p_input, "adding slave input '%s'", psz );
1078 slave = InputSourceNew( p_input );
1079 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1081 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1090 static void InitPrograms( input_thread_t * p_input )
1095 if( p_input->b_preparsing ) return;
1098 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1099 i_es_out_mode = ES_OUT_MODE_AUTO;
1101 if( p_input->p->p_sout )
1103 var_Get( p_input, "sout-all", &val );
1106 i_es_out_mode = ES_OUT_MODE_ALL;
1111 var_Get( p_input, "programs", &val );
1112 if( val.p_list && val.p_list->i_count )
1114 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1115 /* Note : we should remove the "program" callback. */
1119 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1124 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1126 /* Inform the demuxer about waited group (needed only for DVB) */
1127 if( i_es_out_mode == ES_OUT_MODE_ALL )
1129 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1131 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1133 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1138 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1139 (int) var_GetInteger( p_input, "program" ), NULL );
1143 static int Init( input_thread_t * p_input )
1149 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1151 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1153 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1154 var_SetString( p_input, "sout", "" );
1155 var_SetBool( p_input, "sout-all", false );
1156 var_SetString( p_input, "input-slave", "" );
1157 var_SetInteger( p_input, "input-repeat", 0 );
1158 var_SetString( p_input, "sub-file", "" );
1159 var_SetBool( p_input, "sub-autodetect-file", false );
1163 InitStatistics( p_input );
1165 ret = InitSout( p_input );
1166 if( ret != VLC_SUCCESS )
1167 return ret; /* FIXME: goto error; should be better here */
1171 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1172 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1173 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1175 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1176 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1178 if( InputSourceInit( p_input, &p_input->p->input,
1179 p_input->p->input.p_item->psz_uri, NULL ) )
1184 InitTitle( p_input );
1186 /* Load master infos */
1188 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1189 &val.i_time ) && val.i_time > 0 )
1191 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1192 UpdateItemLength( p_input, val.i_time );
1196 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1197 if( val.i_time > 0 )
1198 { /* fallback: gets length from metadata */
1199 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1200 UpdateItemLength( p_input, val.i_time );
1204 StartTitle( p_input );
1206 InitPrograms( p_input );
1208 if( !p_input->b_preparsing && p_input->p->p_sout )
1210 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1212 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1214 /* We don't want a high input priority here or we'll
1215 * end-up sucking up all the CPU time */
1216 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1219 msg_Dbg( p_input, "starting in %s mode",
1220 p_input->p->b_out_pace_control ? "async" : "sync" );
1223 p_meta = vlc_meta_New();
1225 /* Get meta data from users */
1226 InputMetaUser( p_input, p_meta );
1228 /* Get meta data from master input */
1229 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1231 /* Access_file does not give any meta, and there are no slave */
1232 AccessMeta( p_input, p_meta );
1234 InputUpdateMeta( p_input, p_meta );
1236 if( !p_input->b_preparsing )
1238 msg_Dbg( p_input, "`%s' successfully opened",
1239 p_input->p->input.p_item->psz_uri );
1243 /* initialization is complete */
1244 input_ChangeState( p_input, PLAYING_S );
1249 input_ChangeState( p_input, ERROR_S );
1251 if( p_input->p->p_es_out )
1252 input_EsOutDelete( p_input->p->p_es_out );
1254 if( p_input->p->p_sout )
1256 vlc_object_detach( p_input->p->p_sout );
1257 sout_DeleteInstance( p_input->p->p_sout );
1261 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1263 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1264 stats_CounterClean( p_input->p->counters.p_##c );\
1265 p_input->p->counters.p_##c = NULL; } while(0)
1266 EXIT_COUNTER( read_bytes );
1267 EXIT_COUNTER( read_packets );
1268 EXIT_COUNTER( demux_read );
1269 EXIT_COUNTER( input_bitrate );
1270 EXIT_COUNTER( demux_bitrate );
1271 EXIT_COUNTER( played_abuffers );
1272 EXIT_COUNTER( lost_abuffers );
1273 EXIT_COUNTER( displayed_pictures );
1274 EXIT_COUNTER( lost_pictures );
1275 EXIT_COUNTER( decoded_audio );
1276 EXIT_COUNTER( decoded_video );
1277 EXIT_COUNTER( decoded_sub );
1279 if( p_input->p->p_sout )
1281 EXIT_COUNTER( sout_sent_packets );
1282 EXIT_COUNTER( sout_sent_bytes );
1283 EXIT_COUNTER( sout_send_bitrate );
1288 /* Mark them deleted */
1289 p_input->p->input.p_demux = NULL;
1290 p_input->p->input.p_stream = NULL;
1291 p_input->p->input.p_access = NULL;
1292 p_input->p->p_es_out = NULL;
1293 p_input->p->p_sout = NULL;
1295 return VLC_EGENERIC;
1298 /*****************************************************************************
1299 * WaitDie: Wait until we are asked to die.
1300 *****************************************************************************
1301 * This function is called when an error occurred during thread main's loop.
1302 *****************************************************************************/
1303 static void WaitDie( input_thread_t *p_input )
1305 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1307 /* Wait a die order */
1308 vlc_object_lock( p_input );
1309 while( vlc_object_alive( p_input ) )
1310 vlc_object_wait( p_input );
1311 vlc_object_unlock( p_input );
1314 /*****************************************************************************
1315 * End: end the input thread
1316 *****************************************************************************/
1317 static void End( input_thread_t * p_input )
1321 /* We are at the end */
1322 input_ChangeState( p_input, END_S );
1324 /* Clean control variables */
1325 input_ControlVarStop( p_input );
1327 /* Clean up master */
1328 InputSourceClean( &p_input->p->input );
1331 for( i = 0; i < p_input->p->i_slave; i++ )
1333 InputSourceClean( p_input->p->slave[i] );
1334 free( p_input->p->slave[i] );
1336 free( p_input->p->slave );
1338 /* Unload all modules */
1339 if( p_input->p->p_es_out )
1340 input_EsOutDelete( p_input->p->p_es_out );
1342 if( !p_input->b_preparsing )
1344 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1345 if( libvlc_stats( p_input ) )
1347 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
1349 /* make sure we are up to date */
1350 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1351 if( p_private->p_stats_computer == p_input )
1353 stats_ComputeGlobalStats( p_input->p_libvlc,
1354 p_input->p_libvlc->p_stats );
1355 p_private->p_stats_computer = NULL;
1357 CL_CO( read_bytes );
1358 CL_CO( read_packets );
1359 CL_CO( demux_read );
1360 CL_CO( input_bitrate );
1361 CL_CO( demux_bitrate );
1362 CL_CO( played_abuffers );
1363 CL_CO( lost_abuffers );
1364 CL_CO( displayed_pictures );
1365 CL_CO( lost_pictures );
1366 CL_CO( decoded_audio) ;
1367 CL_CO( decoded_video );
1368 CL_CO( decoded_sub) ;
1371 /* Close optional stream output instance */
1372 if( p_input->p->p_sout )
1374 CL_CO( sout_sent_packets );
1375 CL_CO( sout_sent_bytes );
1376 CL_CO( sout_send_bitrate );
1378 vlc_object_detach( p_input->p->p_sout );
1383 if( p_input->p->i_attachment > 0 )
1385 for( i = 0; i < p_input->p->i_attachment; i++ )
1386 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1387 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1390 /* Tell we're dead */
1391 p_input->b_dead = true;
1394 /*****************************************************************************
1396 *****************************************************************************/
1397 static inline int ControlPopNoLock( input_thread_t *p_input,
1398 int *pi_type, vlc_value_t *p_val,
1399 mtime_t i_deadline )
1402 while( p_input->p->i_control <= 0 )
1404 if( i_deadline <= 0 )
1405 return VLC_EGENERIC;
1407 if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1408 return VLC_EGENERIC;
1411 *pi_type = p_input->p->control[0].i_type;
1412 *p_val = p_input->p->control[0].val;
1414 p_input->p->i_control--;
1415 if( p_input->p->i_control > 0 )
1419 for( i = 0; i < p_input->p->i_control; i++ )
1421 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1422 p_input->p->control[i].val = p_input->p->control[i+1].val;
1429 static void ControlReduce( input_thread_t *p_input )
1436 for( i = 1; i < p_input->p->i_control; i++ )
1438 const int i_lt = p_input->p->control[i-1].i_type;
1439 const int i_ct = p_input->p->control[i].i_type;
1441 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1442 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1446 ( i_ct == INPUT_CONTROL_SET_STATE ||
1447 i_ct == INPUT_CONTROL_SET_RATE ||
1448 i_ct == INPUT_CONTROL_SET_POSITION ||
1449 i_ct == INPUT_CONTROL_SET_TIME ||
1450 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1451 i_ct == INPUT_CONTROL_SET_TITLE ||
1452 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1453 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1456 // msg_Dbg( p_input, "merged at %d", i );
1457 /* Remove the i-1 */
1458 for( j = i; j < p_input->p->i_control; j++ )
1459 p_input->p->control[j-1] = p_input->p->control[j];
1460 p_input->p->i_control--;
1464 /* TODO but that's not that important
1465 - merge SET_X with SET_X_CMD
1466 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1467 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1474 static bool Control( input_thread_t *p_input, int i_type,
1477 bool b_force_update = false;
1479 if( !p_input ) return b_force_update;
1483 case INPUT_CONTROL_SET_DIE:
1484 msg_Dbg( p_input, "control: stopping input" );
1486 /* Mark all submodules to die */
1487 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1490 case INPUT_CONTROL_SET_POSITION:
1491 case INPUT_CONTROL_SET_POSITION_OFFSET:
1495 if( p_input->p->b_recording )
1497 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1500 if( i_type == INPUT_CONTROL_SET_POSITION )
1502 f_pos = val.f_float;
1506 /* Should not fail */
1507 demux_Control( p_input->p->input.p_demux,
1508 DEMUX_GET_POSITION, &f_pos );
1509 f_pos += val.f_float;
1511 if( f_pos < 0.0 ) f_pos = 0.0;
1512 if( f_pos > 1.0 ) f_pos = 1.0;
1513 /* Reset the decoders states and clock sync (before calling the demuxer */
1514 input_EsOutChangePosition( p_input->p->p_es_out );
1515 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1518 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1519 "%2.1f%% failed", f_pos * 100 );
1523 if( p_input->p->i_slave > 0 )
1524 SlaveSeek( p_input );
1526 b_force_update = true;
1531 case INPUT_CONTROL_SET_TIME:
1532 case INPUT_CONTROL_SET_TIME_OFFSET:
1537 if( p_input->p->b_recording )
1539 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1543 if( i_type == INPUT_CONTROL_SET_TIME )
1545 i_time = val.i_time;
1549 /* Should not fail */
1550 demux_Control( p_input->p->input.p_demux,
1551 DEMUX_GET_TIME, &i_time );
1552 i_time += val.i_time;
1554 if( i_time < 0 ) i_time = 0;
1556 /* Reset the decoders states and clock sync (before calling the demuxer */
1557 input_EsOutChangePosition( p_input->p->p_es_out );
1559 i_ret = demux_Control( p_input->p->input.p_demux,
1560 DEMUX_SET_TIME, i_time );
1565 /* Emulate it with a SET_POS */
1566 demux_Control( p_input->p->input.p_demux,
1567 DEMUX_GET_LENGTH, &i_length );
1570 double f_pos = (double)i_time / (double)i_length;
1571 i_ret = demux_Control( p_input->p->input.p_demux,
1572 DEMUX_SET_POSITION, f_pos );
1577 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1578 " failed or not possible", i_time );
1582 if( p_input->p->i_slave > 0 )
1583 SlaveSeek( p_input );
1585 b_force_update = true;
1590 case INPUT_CONTROL_SET_STATE:
1591 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1592 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1595 if( p_input->p->input.p_access )
1596 i_ret = access_Control( p_input->p->input.p_access,
1597 ACCESS_SET_PAUSE_STATE, false );
1599 i_ret = demux_Control( p_input->p->input.p_demux,
1600 DEMUX_SET_PAUSE_STATE, false );
1604 /* FIXME What to do ? */
1605 msg_Warn( p_input, "cannot unset pause -> EOF" );
1606 vlc_mutex_unlock( &p_input->p->lock_control );
1607 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1608 vlc_mutex_lock( &p_input->p->lock_control );
1611 b_force_update = true;
1613 /* Switch to play */
1614 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1618 input_EsOutChangeState( p_input->p->p_es_out );
1620 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1621 p_input->p->b_can_pause )
1624 if( p_input->p->input.p_access )
1625 i_ret = access_Control( p_input->p->input.p_access,
1626 ACCESS_SET_PAUSE_STATE, true );
1628 i_ret = demux_Control( p_input->p->input.p_demux,
1629 DEMUX_SET_PAUSE_STATE, true );
1631 b_force_update = true;
1635 msg_Warn( p_input, "cannot set pause state" );
1636 state = p_input->i_state;
1643 /* Switch to new state */
1644 input_ChangeStateWithVarCallback( p_input, state, false );
1648 input_EsOutChangeState( p_input->p->p_es_out );
1650 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1652 b_force_update = true;
1654 /* Correct "state" value */
1655 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1657 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1659 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1663 case INPUT_CONTROL_SET_RATE:
1664 case INPUT_CONTROL_SET_RATE_SLOWER:
1665 case INPUT_CONTROL_SET_RATE_FASTER:
1669 if( i_type == INPUT_CONTROL_SET_RATE )
1675 static const int ppi_factor[][2] = {
1676 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1678 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1687 for( i = 0; ppi_factor[i][0] != 0; i++ )
1689 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1690 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1691 if( i_test_e < i_error )
1697 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1699 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1701 if( ppi_factor[i_idx+1][0] > 0 )
1702 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1704 i_rate = INPUT_RATE_MAX+1;
1708 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1710 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1712 i_rate = INPUT_RATE_MIN-1;
1716 if( i_rate < INPUT_RATE_MIN )
1718 msg_Dbg( p_input, "cannot set rate faster" );
1719 i_rate = INPUT_RATE_MIN;
1721 else if( i_rate > INPUT_RATE_MAX )
1723 msg_Dbg( p_input, "cannot set rate slower" );
1724 i_rate = INPUT_RATE_MAX;
1726 if( i_rate != INPUT_RATE_DEFAULT &&
1727 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1728 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1730 msg_Dbg( p_input, "cannot change rate" );
1731 i_rate = INPUT_RATE_DEFAULT;
1733 if( i_rate != p_input->p->i_rate &&
1734 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1737 if( p_input->p->input.p_access )
1738 i_ret = VLC_EGENERIC;
1740 i_ret = demux_Control( p_input->p->input.p_demux,
1741 DEMUX_SET_RATE, &i_rate );
1744 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1745 i_rate = p_input->p->i_rate;
1750 if( i_rate != p_input->p->i_rate )
1753 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1754 var_SetBool( p_input, "rate-change", true );
1756 p_input->p->i_rate = i_rate;
1758 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1759 if( p_input->p->input.b_rescale_ts )
1760 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1762 b_force_update = true;
1767 case INPUT_CONTROL_SET_PROGRAM:
1768 /* No need to force update, es_out does it if needed */
1769 es_out_Control( p_input->p->p_es_out,
1770 ES_OUT_SET_GROUP, val.i_int );
1772 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1776 case INPUT_CONTROL_SET_ES:
1777 /* No need to force update, es_out does it if needed */
1778 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1779 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1782 case INPUT_CONTROL_RESTART_ES:
1783 es_out_Control( p_input->p->p_es_out, ES_OUT_RESTART_ES,
1784 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1787 case INPUT_CONTROL_SET_AUDIO_DELAY:
1788 input_EsOutSetDelay( p_input->p->p_es_out,
1789 AUDIO_ES, val.i_time );
1790 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1793 case INPUT_CONTROL_SET_SPU_DELAY:
1794 input_EsOutSetDelay( p_input->p->p_es_out,
1795 SPU_ES, val.i_time );
1796 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1799 case INPUT_CONTROL_SET_TITLE:
1800 case INPUT_CONTROL_SET_TITLE_NEXT:
1801 case INPUT_CONTROL_SET_TITLE_PREV:
1802 if( p_input->p->b_recording )
1804 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1807 if( p_input->p->input.b_title_demux &&
1808 p_input->p->input.i_title > 0 )
1811 /* FIXME handle demux title */
1812 demux_t *p_demux = p_input->p->input.p_demux;
1815 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1816 i_title = p_demux->info.i_title - 1;
1817 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1818 i_title = p_demux->info.i_title + 1;
1820 i_title = val.i_int;
1822 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1824 input_EsOutChangePosition( p_input->p->p_es_out );
1826 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1827 input_ControlVarTitle( p_input, i_title );
1830 else if( p_input->p->input.i_title > 0 )
1832 access_t *p_access = p_input->p->input.p_access;
1835 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1836 i_title = p_access->info.i_title - 1;
1837 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1838 i_title = p_access->info.i_title + 1;
1840 i_title = val.i_int;
1842 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1844 input_EsOutChangePosition( p_input->p->p_es_out );
1846 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1847 stream_AccessReset( p_input->p->input.p_stream );
1851 case INPUT_CONTROL_SET_SEEKPOINT:
1852 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1853 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1854 if( p_input->p->b_recording )
1856 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1860 if( p_input->p->input.b_title_demux &&
1861 p_input->p->input.i_title > 0 )
1863 demux_t *p_demux = p_input->p->input.p_demux;
1865 int64_t i_input_time;
1866 int64_t i_seekpoint_time;
1868 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1870 i_seekpoint = p_demux->info.i_seekpoint;
1871 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1872 if( i_seekpoint_time >= 0 &&
1873 !demux_Control( p_demux,
1874 DEMUX_GET_TIME, &i_input_time ) )
1876 if( i_input_time < i_seekpoint_time + 3000000 )
1882 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1883 i_seekpoint = p_demux->info.i_seekpoint + 1;
1885 i_seekpoint = val.i_int;
1887 if( i_seekpoint >= 0 && i_seekpoint <
1888 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1891 input_EsOutChangePosition( p_input->p->p_es_out );
1893 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1896 else if( p_input->p->input.i_title > 0 )
1898 demux_t *p_demux = p_input->p->input.p_demux;
1899 access_t *p_access = p_input->p->input.p_access;
1901 int64_t i_input_time;
1902 int64_t i_seekpoint_time;
1904 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1906 i_seekpoint = p_access->info.i_seekpoint;
1907 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1908 if( i_seekpoint_time >= 0 &&
1909 demux_Control( p_demux,
1910 DEMUX_GET_TIME, &i_input_time ) )
1912 if( i_input_time < i_seekpoint_time + 3000000 )
1918 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1919 i_seekpoint = p_access->info.i_seekpoint + 1;
1921 i_seekpoint = val.i_int;
1923 if( i_seekpoint >= 0 && i_seekpoint <
1924 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1926 input_EsOutChangePosition( p_input->p->p_es_out );
1928 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1930 stream_AccessReset( p_input->p->input.p_stream );
1935 case INPUT_CONTROL_ADD_SUBTITLE:
1936 if( val.psz_string )
1938 SubtitleAdd( p_input, val.psz_string, true );
1939 free( val.psz_string );
1943 case INPUT_CONTROL_ADD_SLAVE:
1944 if( val.psz_string )
1946 input_source_t *slave = InputSourceNew( p_input );
1948 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1954 msg_Dbg( p_input, "adding %s as slave on the fly",
1958 if( demux_Control( p_input->p->input.p_demux,
1959 DEMUX_GET_TIME, &i_time ) )
1961 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1962 InputSourceClean( slave );
1966 if( demux_Control( slave->p_demux,
1967 DEMUX_SET_TIME, i_time ) )
1969 msg_Err( p_input, "seek failed for new slave" );
1970 InputSourceClean( slave );
1975 /* Get meta (access and demux) */
1976 p_meta = vlc_meta_New();
1977 access_Control( slave->p_access, ACCESS_GET_META,
1979 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1980 InputUpdateMeta( p_input, p_meta );
1982 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1987 msg_Warn( p_input, "failed to add %s as slave",
1991 free( val.psz_string );
1995 case INPUT_CONTROL_SET_RECORD_STATE:
1996 if( !!p_input->p->b_recording != !!val.b_bool )
1998 if( p_input->p->input.b_can_stream_record )
2000 if( demux_Control( p_input->p->input.p_demux,
2001 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2006 if( input_EsOutSetRecord( p_input->p->p_es_out, val.b_bool ) )
2009 p_input->p->b_recording = val.b_bool;
2011 var_Change( p_input, "record", VLC_VAR_SETVALUE, &val, NULL );
2013 b_force_update = true;
2017 case INPUT_CONTROL_SET_BOOKMARK:
2019 msg_Err( p_input, "not yet implemented" );
2023 return b_force_update;
2026 /*****************************************************************************
2028 *****************************************************************************/
2029 static int UpdateFromDemux( input_thread_t *p_input )
2031 demux_t *p_demux = p_input->p->input.p_demux;
2034 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2036 v.i_int = p_demux->info.i_title;
2037 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2039 input_ControlVarTitle( p_input, p_demux->info.i_title );
2041 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2043 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2045 v.i_int = p_demux->info.i_seekpoint;
2046 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2048 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2050 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2052 /* Hmmm only works with master input */
2053 if( p_input->p->input.p_demux == p_demux )
2055 int i_title_end = p_input->p->input.i_title_end -
2056 p_input->p->input.i_title_offset;
2057 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2058 p_input->p->input.i_seekpoint_offset;
2060 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2062 if( p_demux->info.i_title > i_title_end ||
2063 ( p_demux->info.i_title == i_title_end &&
2064 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2066 else if( i_seekpoint_end >=0 )
2068 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2070 else if( i_title_end >= 0 )
2072 if( p_demux->info.i_title > i_title_end ) return 0;
2079 /*****************************************************************************
2081 *****************************************************************************/
2082 static int UpdateFromAccess( input_thread_t *p_input )
2084 access_t *p_access = p_input->p->input.p_access;
2087 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2089 v.i_int = p_access->info.i_title;
2090 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2092 input_ControlVarTitle( p_input, p_access->info.i_title );
2094 stream_AccessUpdate( p_input->p->input.p_stream );
2096 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2098 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2100 v.i_int = p_access->info.i_seekpoint;
2101 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2102 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2104 if( p_access->info.i_update & INPUT_UPDATE_META )
2106 /* TODO maybe multi - access ? */
2107 vlc_meta_t *p_meta = vlc_meta_New();
2108 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2109 InputUpdateMeta( p_input, p_meta );
2110 p_access->info.i_update &= ~INPUT_UPDATE_META;
2112 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2117 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2118 f_quality = f_strength = -1;
2120 var_SetFloat( p_input, "signal-quality", f_quality );
2121 var_SetFloat( p_input, "signal-strength", f_strength );
2123 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2126 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2128 /* Hmmm only works with master input */
2129 if( p_input->p->input.p_access == p_access )
2131 int i_title_end = p_input->p->input.i_title_end -
2132 p_input->p->input.i_title_offset;
2133 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2134 p_input->p->input.i_seekpoint_offset;
2136 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2138 if( p_access->info.i_title > i_title_end ||
2139 ( p_access->info.i_title == i_title_end &&
2140 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2142 else if( i_seekpoint_end >=0 )
2144 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2146 else if( i_title_end >= 0 )
2148 if( p_access->info.i_title > i_title_end ) return 0;
2155 /*****************************************************************************
2157 *****************************************************************************/
2158 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2160 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2163 /*****************************************************************************
2165 *****************************************************************************/
2166 static input_source_t *InputSourceNew( input_thread_t *p_input )
2168 VLC_UNUSED(p_input);
2169 input_source_t *in = malloc( sizeof( input_source_t ) );
2171 memset( in, 0, sizeof( input_source_t ) );
2175 /*****************************************************************************
2177 *****************************************************************************/
2178 static int InputSourceInit( input_thread_t *p_input,
2179 input_source_t *in, const char *psz_mrl,
2180 const char *psz_forced_demux )
2182 const bool b_master = in == &p_input->p->input;
2184 char psz_dup[strlen(psz_mrl) + 1];
2185 const char *psz_access;
2186 const char *psz_demux;
2193 strcpy( psz_dup, psz_mrl );
2195 if( !in ) return VLC_EGENERIC;
2196 if( !p_input ) return VLC_EGENERIC;
2199 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2201 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2202 psz_mrl, psz_access, psz_demux, psz_path );
2203 if( !p_input->b_preparsing )
2205 /* Hack to allow udp://@:port syntax */
2207 (strncmp( psz_access, "udp", 3 ) &&
2208 strncmp( psz_access, "rtp", 3 )) )
2210 /* Find optional titles and seekpoints */
2211 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2212 &in->i_seekpoint_start, &in->i_seekpoint_end );
2215 if( psz_forced_demux && *psz_forced_demux )
2217 psz_demux = psz_forced_demux;
2219 else if( *psz_demux == '\0' )
2221 /* special hack for forcing a demuxer with --demux=module
2222 * (and do nothing with a list) */
2223 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2225 if( psz_var_demux != NULL &&
2226 !strchr(psz_var_demux, ',' ) &&
2227 !strchr(psz_var_demux, ':' ) )
2229 psz_demux = psz_var_demux;
2231 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2235 /* Try access_demux first */
2236 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2237 NULL, p_input->p->p_es_out, false );
2241 /* Preparsing is only for file:// */
2244 if( !*psz_access ) /* path without scheme:// */
2245 psz_access = "file";
2246 if( strcmp( psz_access, "file" ) )
2248 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2253 int64_t i_pts_delay;
2255 /* Get infos from access_demux */
2256 demux_Control( in->p_demux,
2257 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2258 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2260 in->b_title_demux = true;
2261 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2262 &in->title, &in->i_title,
2263 &in->i_title_offset, &in->i_seekpoint_offset ) )
2265 TAB_INIT( in->i_title, in->title );
2267 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2268 &in->b_can_pace_control ) )
2269 in->b_can_pace_control = false;
2271 if( !in->b_can_pace_control )
2273 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2274 &in->b_can_rate_control, &in->b_rescale_ts ) )
2276 in->b_can_rate_control = false;
2277 in->b_rescale_ts = true; /* not used */
2282 in->b_can_rate_control = true;
2283 in->b_rescale_ts = true;
2285 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2286 &in->b_can_pause ) )
2287 in->b_can_pause = false;
2288 var_SetBool( p_input, "can-pause", in->b_can_pause );
2290 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2292 if( ret != VLC_SUCCESS )
2294 var_Set( p_input, "seekable", val );
2298 int64_t i_pts_delay;
2301 input_ChangeState( p_input, OPENING_S );
2303 /* Now try a real access */
2304 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2306 /* Access failed, URL encoded ? */
2307 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2309 decode_URI( psz_path );
2311 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2312 psz_access, psz_demux, psz_path );
2314 in->p_access = access_New( p_input,
2315 psz_access, psz_demux, psz_path );
2317 if( in->p_access == NULL )
2319 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2321 intf_UserFatal( VLC_OBJECT( p_input), false,
2322 _("Your input can't be opened"),
2323 _("VLC is unable to open the MRL '%s'."
2324 " Check the log for details."), psz_mrl );
2329 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2330 while( psz && *psz )
2332 access_t *p_access = in->p_access;
2333 char *end = strchr( psz, ':' );
2338 in->p_access = access_FilterNew( in->p_access, psz );
2339 if( in->p_access == NULL )
2341 in->p_access = p_access;
2342 msg_Warn( p_input, "failed to insert access filter %s",
2350 /* Get infos from access */
2351 if( !p_input->b_preparsing )
2353 access_Control( in->p_access,
2354 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2355 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2357 in->b_title_demux = false;
2358 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2359 &in->title, &in->i_title,
2360 &in->i_title_offset, &in->i_seekpoint_offset ) )
2363 TAB_INIT( in->i_title, in->title );
2365 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2366 &in->b_can_pace_control );
2367 in->b_can_rate_control = in->b_can_pace_control;
2368 in->b_rescale_ts = true;
2370 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2372 var_SetBool( p_input, "can-pause", in->b_can_pause );
2373 access_Control( in->p_access, ACCESS_CAN_SEEK,
2375 var_Set( p_input, "seekable", val );
2379 input_ChangeState( p_input, BUFFERING_S );
2381 /* Autodetect extra files if none specified */
2382 char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2383 if( !psz_input_list )
2385 char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2386 if( psz_extra_files )
2387 var_SetString( p_input, "input-list", psz_extra_files );
2388 free( psz_extra_files );
2391 /* Create the stream_t */
2392 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2394 /* Restor old value */
2395 if( !psz_input_list )
2396 var_SetString( p_input, "input-list", "" );
2397 free( psz_input_list );
2399 if( in->p_stream == NULL )
2401 msg_Warn( p_input, "cannot create a stream_t from access" );
2405 /* Open a demuxer */
2406 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2408 psz_demux = in->p_access->psz_demux;
2412 /* Take access redirections into account */
2413 char *psz_real_path;
2414 char *psz_buf = NULL;
2415 if( in->p_access->psz_path )
2417 const char *psz_a, *psz_d;
2418 psz_buf = strdup( in->p_access->psz_path );
2419 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2423 psz_real_path = psz_path;
2425 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2427 in->p_stream, p_input->p->p_es_out,
2428 p_input->b_preparsing );
2432 if( in->p_demux == NULL )
2434 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2435 psz_access, psz_demux, psz_path );
2436 intf_UserFatal( VLC_OBJECT( p_input ), false,
2437 _("VLC can't recognize the input's format"),
2438 _("The format of '%s' cannot be detected. "
2439 "Have a look at the log for details."), psz_mrl );
2443 /* Get title from demux */
2444 if( !p_input->b_preparsing && in->i_title <= 0 )
2446 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2447 &in->title, &in->i_title,
2448 &in->i_title_offset, &in->i_seekpoint_offset ))
2450 TAB_INIT( in->i_title, in->title );
2454 in->b_title_demux = true;
2459 /* Set record capabilities */
2460 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2461 in->b_can_stream_record = false;
2463 if( !var_CreateGetBool( p_input, "input-record-native" ) )
2464 in->b_can_stream_record = false;
2465 var_SetBool( p_input, "can-record", true );
2467 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2471 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2472 if( 1 || !p_input->b_preparsing )
2475 input_attachment_t **attachment;
2476 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2477 &attachment, &i_attachment ) )
2479 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2480 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2481 i_attachment, attachment );
2482 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2485 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2487 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2489 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2492 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2493 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2499 input_ChangeState( p_input, ERROR_S );
2502 demux_Delete( in->p_demux );
2505 stream_Delete( in->p_stream );
2508 access_Delete( in->p_access );
2510 return VLC_EGENERIC;
2513 /*****************************************************************************
2515 *****************************************************************************/
2516 static void InputSourceClean( input_source_t *in )
2521 demux_Delete( in->p_demux );
2524 stream_Delete( in->p_stream );
2527 access_Delete( in->p_access );
2529 if( in->i_title > 0 )
2531 for( i = 0; i < in->i_title; i++ )
2532 vlc_input_title_Delete( in->title[i] );
2533 TAB_CLEAN( in->i_title, in->title );
2537 static void SlaveDemux( input_thread_t *p_input )
2541 bool b_set_time = true;
2543 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2545 /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2549 for( i = 0; i < p_input->p->i_slave; i++ )
2551 input_source_t *in = p_input->p->slave[i];
2557 if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2562 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2564 msg_Err( p_input, "slave[%d] doesn't like "
2565 "DEMUX_GET_TIME -> EOF", i );
2570 if( i_stime >= i_time )
2573 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2579 i_ret = in->p_demux->pf_demux( in->p_demux );
2584 msg_Dbg( p_input, "slave %d EOF", i );
2590 static void SlaveSeek( input_thread_t *p_input )
2595 if( !p_input ) return;
2597 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2599 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2603 for( i = 0; i < p_input->p->i_slave; i++ )
2605 input_source_t *in = p_input->p->slave[i];
2607 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2610 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2620 /*****************************************************************************
2622 *****************************************************************************/
2623 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2627 if( !p_meta ) return;
2629 /* Get meta information from user */
2630 #define GET_META( field, s ) \
2631 var_Get( p_input, (s), &val ); \
2632 if( *val.psz_string ) \
2633 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2634 free( val.psz_string )
2636 GET_META( Title, "meta-title" );
2637 GET_META( Artist, "meta-artist" );
2638 GET_META( Genre, "meta-genre" );
2639 GET_META( Copyright, "meta-copyright" );
2640 GET_META( Description, "meta-description" );
2641 GET_META( Date, "meta-date" );
2642 GET_META( URL, "meta-url" );
2646 /*****************************************************************************
2647 * InputGetExtraFiles
2648 * Autodetect extra input list
2649 *****************************************************************************/
2650 static char *InputGetExtraFiles( input_thread_t *p_input,
2651 const char *psz_access, const char *psz_path )
2653 char *psz_list = NULL;
2655 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2659 const char *psz_ext = strrchr( psz_path, '.' );
2660 if( !psz_ext || strcmp( psz_ext, ".001" ) )
2663 char *psz_file = strdup( psz_path );
2667 /* Try to list .xyz files */
2668 for( int i = 2; i < 999; i++ )
2670 char *psz_ext = strrchr( psz_file, '.' );
2673 snprintf( psz_ext, 5, ".%.3d", i );
2675 if( utf8_stat( psz_file, &st )
2676 || !S_ISREG( st.st_mode ) || !st.st_size )
2679 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2683 char *psz_old = psz_list;
2684 /* FIXME how to handle file with ',' ?*/
2685 if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2693 psz_list = strdup( psz_file );
2701 /*****************************************************************************
2702 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2703 * arturl and locking issue.
2704 *****************************************************************************/
2705 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2707 input_item_t *p_item = p_input->p->input.p_item;
2708 char * psz_arturl = NULL;
2709 char *psz_title = NULL;
2710 int i_arturl_event = false;
2715 psz_arturl = input_item_GetArtURL( p_item );
2717 vlc_mutex_lock( &p_item->lock );
2718 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2719 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2721 vlc_meta_Merge( p_item->p_meta, p_meta );
2723 if( psz_arturl && *psz_arturl )
2725 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2726 i_arturl_event = true;
2729 vlc_meta_Delete( p_meta );
2731 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2733 /* Don't look for art cover if sout
2734 * XXX It can change when sout has meta data support */
2735 if( p_input->p->p_sout && !p_input->b_preparsing )
2737 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2738 i_arturl_event = true;
2742 input_ExtractAttachmentAndCacheArt( p_input );
2748 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2750 p_meta = vlc_meta_New();
2751 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2753 vlc_mutex_unlock( &p_item->lock );
2755 input_item_SetPreparsed( p_item, true );
2757 if( i_arturl_event == true )
2761 /* Notify interested third parties */
2762 event.type = vlc_InputItemMetaChanged;
2763 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2764 vlc_event_send( &p_item->event_manager, &event );
2769 input_Control( p_input, INPUT_SET_NAME, psz_title );
2773 /** \todo handle sout meta */
2777 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2778 int i_new, input_attachment_t **pp_new )
2780 int i_attachment = *pi_attachment;
2781 input_attachment_t **attachment = *ppp_attachment;
2784 attachment = realloc( attachment,
2785 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2786 for( i = 0; i < i_new; i++ )
2787 attachment[i_attachment++] = pp_new[i];
2791 *pi_attachment = i_attachment;
2792 *ppp_attachment = attachment;
2795 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2799 if( p_input->b_preparsing )
2802 if( p_input->p->input.p_access )
2803 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2806 /* Get meta data from slave input */
2807 for( i = 0; i < p_input->p->i_slave; i++ )
2809 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2810 if( p_input->p->slave[i]->p_access )
2812 access_Control( p_input->p->slave[i]->p_access,
2813 ACCESS_GET_META, p_meta );
2818 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2825 /* XXX I am not sure it is a great idea, besides, there is more than that
2826 * if we want to do it right */
2827 vlc_mutex_lock( &p_item->lock );
2828 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2830 vlc_mutex_unlock( &p_item->lock );
2833 vlc_mutex_unlock( &p_item->lock );
2836 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2837 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2842 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2843 if(! p_demux->p_private )
2846 p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2849 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2851 if( p_demux_meta->p_meta )
2853 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2854 vlc_meta_Delete( p_demux_meta->p_meta );
2857 if( p_demux_meta->i_attachments > 0 )
2859 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2860 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2861 p_demux_meta->i_attachments, p_demux_meta->attachments );
2862 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2864 module_unneed( p_demux, p_id3 );
2866 free( p_demux->p_private );
2870 /*****************************************************************************
2871 * MRLSplit: parse the access, demux and url part of the
2872 * Media Resource Locator.
2873 *****************************************************************************/
2874 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2877 char *psz_access = NULL;
2878 char *psz_demux = NULL;
2881 /* Either there is an access/demux specification before ://
2882 * or we have a plain local file path. */
2883 psz_path = strstr( psz_dup, "://" );
2884 if( psz_path != NULL )
2887 psz_path += 3; /* skips "://" */
2889 /* Separate access from demux (<access>/<demux>://<path>) */
2890 psz_access = psz_dup;
2891 psz_demux = strchr( psz_access, '/' );
2893 *psz_demux++ = '\0';
2895 /* We really don't want module name substitution here! */
2896 if( psz_access[0] == '$' )
2898 if( psz_demux && psz_demux[0] == '$' )
2905 *ppsz_access = psz_access ? psz_access : (char*)"";
2906 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2907 *ppsz_path = psz_path;
2910 static inline bool next(char ** src)
2914 long result = strtol( *src, &end, 0 );
2915 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
2924 /*****************************************************************************
2925 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2928 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2929 *****************************************************************************/
2930 static void MRLSections( input_thread_t *p_input, char *psz_source,
2931 int *pi_title_start, int *pi_title_end,
2932 int *pi_chapter_start, int *pi_chapter_end )
2934 char *psz, *psz_end, *psz_next, *psz_check;
2936 *pi_title_start = *pi_title_end = -1;
2937 *pi_chapter_start = *pi_chapter_end = -1;
2939 /* Start by parsing titles and chapters */
2940 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2943 /* Check we are really dealing with a title/chapter section */
2944 psz_check = psz + 1;
2945 if( !*psz_check ) return;
2946 if( isdigit(*psz_check) )
2947 if(!next(&psz_check)) return;
2948 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2949 if( *psz_check == ':' && ++psz_check )
2951 if( isdigit(*psz_check) )
2952 if(!next(&psz_check)) return;
2954 if( *psz_check != '-' && *psz_check ) return;
2955 if( *psz_check == '-' && ++psz_check )
2957 if( isdigit(*psz_check) )
2958 if(!next(&psz_check)) return;
2960 if( *psz_check != ':' && *psz_check ) return;
2961 if( *psz_check == ':' && ++psz_check )
2963 if( isdigit(*psz_check) )
2964 if(!next(&psz_check)) return;
2966 if( *psz_check ) return;
2968 /* Separate start and end */
2970 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2972 /* Look for the start title */
2973 *pi_title_start = strtol( psz, &psz_next, 0 );
2974 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2975 *pi_title_end = *pi_title_start;
2978 /* Look for the start chapter */
2980 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2981 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2982 *pi_chapter_end = *pi_chapter_start;
2986 /* Look for the end title */
2987 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2988 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2991 /* Look for the end chapter */
2992 if( *psz_end ) psz_end++;
2993 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2994 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2997 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2998 psz_source, *pi_title_start, *pi_chapter_start,
2999 *pi_title_end, *pi_chapter_end );
3002 /*****************************************************************************
3003 * input_AddSubtitles: add a subtitles file and enable it
3004 *****************************************************************************/
3005 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3007 input_source_t *sub;
3010 char *psz_path, *psz_extension;
3012 /* if we are provided a subtitle.sub file,
3013 * see if we don't have a subtitle.idx and use it instead */
3014 psz_path = strdup( psz_subtitle );
3017 psz_extension = strrchr( psz_path, '.');
3018 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3022 strcpy( psz_extension, ".idx" );
3024 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3026 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3027 psz_path, psz_subtitle );
3028 strcpy( psz_subtitle, psz_path );
3034 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3036 sub = InputSourceNew( p_input );
3037 if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3042 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3045 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3047 if( count.i_int == 0 )
3049 /* if it was first one, there is disable too */
3051 if( count.i_int < list.p_list->i_count )
3053 int i_id = list.p_list->p_values[count.i_int].i_int;
3054 es_out_id_t *p_es = input_EsOutGetFromID( p_input->p->p_es_out, i_id );
3056 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_DEFAULT, p_es );
3057 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES, p_es );
3059 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3063 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3064 bool b_check_extension )
3068 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3071 assert( psz_subtitle != NULL );
3073 val.psz_string = strdup( psz_subtitle );
3074 if( val.psz_string )
3075 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3079 /*****************************************************************************
3080 * input_get_event_manager
3081 *****************************************************************************/
3082 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3084 return &p_input->p->event_manager;
3088 /* TODO FIXME nearly the same logic that snapshot code */
3089 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3094 path = utf8_opendir( psz_path );
3099 char *psz_tmp = str_format( p_obj, psz_prefix );
3103 filename_sanitize( psz_tmp );
3104 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3106 psz_extension ? "." : "",
3107 psz_extension ? psz_extension : "" ) < 0 )
3114 psz_file = str_format( p_obj, psz_path );
3115 path_sanitize( psz_file );