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);
771 } while( i_current < i_wakeup );
774 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
776 /* We have finish to demux data but not to play them */
777 while( !p_input->b_die )
779 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
782 msg_Dbg( p_input, "waiting decoder fifos to empty" );
784 msleep( INPUT_IDLE_SLEEP );
787 /* We have finished */
788 input_ChangeState( p_input, END_S );
792 static void InitStatistics( input_thread_t * p_input )
794 if( p_input->b_preparsing ) return;
796 /* Prepare statistics */
797 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
798 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
799 if( libvlc_stats( p_input ) )
801 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
802 INIT_COUNTER( read_packets, INTEGER, COUNTER );
803 INIT_COUNTER( demux_read, INTEGER, COUNTER );
804 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
805 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
806 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
807 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
808 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
809 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
810 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
811 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
812 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
813 p_input->p->counters.p_sout_send_bitrate = NULL;
814 p_input->p->counters.p_sout_sent_packets = NULL;
815 p_input->p->counters.p_sout_sent_bytes = NULL;
816 if( p_input->p->counters.p_demux_bitrate )
817 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
818 if( p_input->p->counters.p_input_bitrate )
819 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
824 static int InitSout( input_thread_t * p_input )
828 if( p_input->b_preparsing ) return VLC_SUCCESS;
830 /* Find a usable sout and attach it to p_input */
831 psz = var_GetNonEmptyString( p_input, "sout" );
832 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
834 /* Check the validity of the provided sout */
835 if( p_input->p->p_sout )
837 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
839 msg_Dbg( p_input, "destroying unusable sout" );
841 sout_DeleteInstance( p_input->p->p_sout );
842 p_input->p->p_sout = NULL;
846 if( p_input->p->p_sout )
849 msg_Dbg( p_input, "sout keep: reusing sout" );
850 msg_Dbg( p_input, "sout keep: you probably want to use "
851 "gather stream_out" );
852 vlc_object_attach( p_input->p->p_sout, p_input );
856 /* Create a new one */
857 p_input->p->p_sout = sout_NewInstance( p_input, psz );
858 if( !p_input->p->p_sout )
860 input_ChangeState( p_input, ERROR_S );
861 msg_Err( p_input, "cannot start stream output instance, " \
867 if( libvlc_stats( p_input ) )
869 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
870 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
871 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
872 if( p_input->p->counters.p_sout_send_bitrate )
873 p_input->p->counters.p_sout_send_bitrate->update_interval =
877 else if( p_input->p->p_sout )
879 msg_Dbg( p_input, "destroying useless sout" );
881 sout_DeleteInstance( p_input->p->p_sout );
882 p_input->p->p_sout = NULL;
890 static void InitTitle( input_thread_t * p_input )
894 if( p_input->b_preparsing ) return;
896 /* Create global title (from master) */
897 p_input->p->i_title = p_input->p->input.i_title;
898 p_input->p->title = p_input->p->input.title;
899 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
900 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
901 if( p_input->p->i_title > 0 )
903 /* Setup variables */
904 input_ControlVarNavigation( p_input );
905 input_ControlVarTitle( p_input, 0 );
909 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
910 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
911 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
914 if( p_input->i_pts_delay < 0 )
915 p_input->i_pts_delay = 0;
917 /* If the desynchronisation requested by the user is < 0, we need to
918 * cache more data. */
919 var_Get( p_input, "audio-desync", &val );
920 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
922 /* Update cr_average depending on the caching */
923 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
924 p_input->p->input.i_cr_average /= 10;
925 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
928 static void StartTitle( input_thread_t * p_input )
937 /* Start title/chapter */
939 if( p_input->b_preparsing )
941 p_input->p->i_start = 0;
945 val.i_int = p_input->p->input.i_title_start -
946 p_input->p->input.i_title_offset;
947 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
948 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
949 val.i_int = p_input->p->input.i_seekpoint_start -
950 p_input->p->input.i_seekpoint_offset;
951 if( val.i_int > 0 /* TODO: check upper boundary */ )
952 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
956 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
957 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
958 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
959 i_length = var_GetTime( p_input, "length" );
960 if( p_input->p->i_run < 0 )
962 msg_Warn( p_input, "invalid run-time ignored" );
963 p_input->p->i_run = 0;
966 if( p_input->p->i_start > 0 )
968 if( p_input->p->i_start >= i_length )
970 msg_Warn( p_input, "invalid start-time ignored" );
976 msg_Dbg( p_input, "starting at time: %ds",
977 (int)( p_input->p->i_start / INT64_C(1000000) ) );
979 s.i_time = p_input->p->i_start;
980 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
983 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
985 msg_Warn( p_input, "invalid stop-time ignored" );
986 p_input->p->i_stop = 0;
990 /* Get fps and set it if not already set */
991 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
994 float f_requested_fps;
996 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
997 var_SetFloat( p_input, "sub-original-fps", f_fps );
999 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1000 if( f_requested_fps != f_fps )
1002 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1003 VLC_VAR_DOINHERIT );
1004 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1008 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1011 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1014 /* Look for and add subtitle files */
1015 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1016 if( psz_subtitle != NULL )
1018 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1019 SubtitleAdd( p_input, psz_subtitle, true );
1022 var_Get( p_input, "sub-autodetect-file", &val );
1025 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1026 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1027 p_input->p->input.p_item->psz_uri );
1028 free( psz_autopath );
1030 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1032 /* Try to autoselect the first autodetected subtitles file
1033 * if no subtitles file was specified */
1034 bool b_forced = i == 0 && !psz_subtitle;
1036 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1037 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1039 free( ppsz_subs[i] );
1043 free( psz_subtitle );
1045 /* Look for slave */
1046 psz = var_GetNonEmptyString( p_input, "input-slave" );
1050 input_source_t *slave;
1051 while( psz && *psz )
1053 while( *psz == ' ' || *psz == '#' )
1057 if( ( psz_delim = strchr( psz, '#' ) ) )
1059 *psz_delim++ = '\0';
1066 msg_Dbg( p_input, "adding slave input '%s'", psz );
1067 slave = InputSourceNew( p_input );
1068 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1070 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1079 static void InitPrograms( input_thread_t * p_input )
1084 if( p_input->b_preparsing ) return;
1087 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1088 i_es_out_mode = ES_OUT_MODE_AUTO;
1090 if( p_input->p->p_sout )
1092 var_Get( p_input, "sout-all", &val );
1095 i_es_out_mode = ES_OUT_MODE_ALL;
1100 var_Get( p_input, "programs", &val );
1101 if( val.p_list && val.p_list->i_count )
1103 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1104 /* Note : we should remove the "program" callback. */
1108 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1113 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1115 /* Inform the demuxer about waited group (needed only for DVB) */
1116 if( i_es_out_mode == ES_OUT_MODE_ALL )
1118 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1120 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1122 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1127 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1128 (int) var_GetInteger( p_input, "program" ), NULL );
1132 static int Init( input_thread_t * p_input )
1138 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1140 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1142 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1143 var_SetString( p_input, "sout", "" );
1144 var_SetBool( p_input, "sout-all", false );
1145 var_SetString( p_input, "input-slave", "" );
1146 var_SetInteger( p_input, "input-repeat", 0 );
1147 var_SetString( p_input, "sub-file", "" );
1148 var_SetBool( p_input, "sub-autodetect-file", false );
1152 InitStatistics( p_input );
1154 ret = InitSout( p_input );
1155 if( ret != VLC_SUCCESS )
1156 return ret; /* FIXME: goto error; should be better here */
1160 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1161 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1162 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1164 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1165 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1167 if( InputSourceInit( p_input, &p_input->p->input,
1168 p_input->p->input.p_item->psz_uri, NULL ) )
1173 InitTitle( p_input );
1175 /* Load master infos */
1177 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1178 &val.i_time ) && val.i_time > 0 )
1180 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1181 UpdateItemLength( p_input, val.i_time );
1185 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1186 if( val.i_time > 0 )
1187 { /* fallback: gets length from metadata */
1188 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1189 UpdateItemLength( p_input, val.i_time );
1193 StartTitle( p_input );
1195 InitPrograms( p_input );
1197 if( !p_input->b_preparsing && p_input->p->p_sout )
1199 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1201 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1203 /* We don't want a high input priority here or we'll
1204 * end-up sucking up all the CPU time */
1205 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1208 msg_Dbg( p_input, "starting in %s mode",
1209 p_input->p->b_out_pace_control ? "async" : "sync" );
1212 p_meta = vlc_meta_New();
1214 /* Get meta data from users */
1215 InputMetaUser( p_input, p_meta );
1217 /* Get meta data from master input */
1218 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1220 /* Access_file does not give any meta, and there are no slave */
1221 AccessMeta( p_input, p_meta );
1223 InputUpdateMeta( p_input, p_meta );
1225 if( !p_input->b_preparsing )
1227 msg_Dbg( p_input, "`%s' successfully opened",
1228 p_input->p->input.p_item->psz_uri );
1232 /* initialization is complete */
1233 input_ChangeState( p_input, PLAYING_S );
1238 input_ChangeState( p_input, ERROR_S );
1240 if( p_input->p->p_es_out )
1241 input_EsOutDelete( p_input->p->p_es_out );
1243 if( p_input->p->p_sout )
1245 vlc_object_detach( p_input->p->p_sout );
1246 sout_DeleteInstance( p_input->p->p_sout );
1250 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1252 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1253 stats_CounterClean( p_input->p->counters.p_##c );\
1254 p_input->p->counters.p_##c = NULL; } while(0)
1255 EXIT_COUNTER( read_bytes );
1256 EXIT_COUNTER( read_packets );
1257 EXIT_COUNTER( demux_read );
1258 EXIT_COUNTER( input_bitrate );
1259 EXIT_COUNTER( demux_bitrate );
1260 EXIT_COUNTER( played_abuffers );
1261 EXIT_COUNTER( lost_abuffers );
1262 EXIT_COUNTER( displayed_pictures );
1263 EXIT_COUNTER( lost_pictures );
1264 EXIT_COUNTER( decoded_audio );
1265 EXIT_COUNTER( decoded_video );
1266 EXIT_COUNTER( decoded_sub );
1268 if( p_input->p->p_sout )
1270 EXIT_COUNTER( sout_sent_packets );
1271 EXIT_COUNTER( sout_sent_bytes );
1272 EXIT_COUNTER( sout_send_bitrate );
1277 /* Mark them deleted */
1278 p_input->p->input.p_demux = NULL;
1279 p_input->p->input.p_stream = NULL;
1280 p_input->p->input.p_access = NULL;
1281 p_input->p->p_es_out = NULL;
1282 p_input->p->p_sout = NULL;
1284 return VLC_EGENERIC;
1287 /*****************************************************************************
1288 * WaitDie: Wait until we are asked to die.
1289 *****************************************************************************
1290 * This function is called when an error occurred during thread main's loop.
1291 *****************************************************************************/
1292 static void WaitDie( input_thread_t *p_input )
1294 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1296 /* Wait a die order */
1297 vlc_object_lock( p_input );
1298 while( vlc_object_alive( p_input ) )
1299 vlc_object_wait( p_input );
1300 vlc_object_unlock( p_input );
1303 /*****************************************************************************
1304 * End: end the input thread
1305 *****************************************************************************/
1306 static void End( input_thread_t * p_input )
1310 /* We are at the end */
1311 input_ChangeState( p_input, END_S );
1313 /* Clean control variables */
1314 input_ControlVarStop( p_input );
1316 /* Clean up master */
1317 InputSourceClean( &p_input->p->input );
1320 for( i = 0; i < p_input->p->i_slave; i++ )
1322 InputSourceClean( p_input->p->slave[i] );
1323 free( p_input->p->slave[i] );
1325 free( p_input->p->slave );
1327 /* Unload all modules */
1328 if( p_input->p->p_es_out )
1329 input_EsOutDelete( p_input->p->p_es_out );
1331 if( !p_input->b_preparsing )
1333 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1334 if( libvlc_stats( p_input ) )
1336 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
1338 /* make sure we are up to date */
1339 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1340 if( p_private->p_stats_computer == p_input )
1342 stats_ComputeGlobalStats( p_input->p_libvlc,
1343 p_input->p_libvlc->p_stats );
1344 p_private->p_stats_computer = NULL;
1346 CL_CO( read_bytes );
1347 CL_CO( read_packets );
1348 CL_CO( demux_read );
1349 CL_CO( input_bitrate );
1350 CL_CO( demux_bitrate );
1351 CL_CO( played_abuffers );
1352 CL_CO( lost_abuffers );
1353 CL_CO( displayed_pictures );
1354 CL_CO( lost_pictures );
1355 CL_CO( decoded_audio) ;
1356 CL_CO( decoded_video );
1357 CL_CO( decoded_sub) ;
1360 /* Close optional stream output instance */
1361 if( p_input->p->p_sout )
1363 CL_CO( sout_sent_packets );
1364 CL_CO( sout_sent_bytes );
1365 CL_CO( sout_send_bitrate );
1367 vlc_object_detach( p_input->p->p_sout );
1372 if( p_input->p->i_attachment > 0 )
1374 for( i = 0; i < p_input->p->i_attachment; i++ )
1375 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1376 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1379 /* Tell we're dead */
1380 p_input->b_dead = true;
1383 /*****************************************************************************
1385 *****************************************************************************/
1386 static inline int ControlPopNoLock( input_thread_t *p_input,
1387 int *pi_type, vlc_value_t *p_val,
1388 mtime_t i_deadline )
1391 while( p_input->p->i_control <= 0 )
1393 if( i_deadline <= 0 )
1394 return VLC_EGENERIC;
1396 if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1397 return VLC_EGENERIC;
1400 *pi_type = p_input->p->control[0].i_type;
1401 *p_val = p_input->p->control[0].val;
1403 p_input->p->i_control--;
1404 if( p_input->p->i_control > 0 )
1408 for( i = 0; i < p_input->p->i_control; i++ )
1410 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1411 p_input->p->control[i].val = p_input->p->control[i+1].val;
1418 static void ControlReduce( input_thread_t *p_input )
1425 for( i = 1; i < p_input->p->i_control; i++ )
1427 const int i_lt = p_input->p->control[i-1].i_type;
1428 const int i_ct = p_input->p->control[i].i_type;
1430 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1431 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1435 ( i_ct == INPUT_CONTROL_SET_STATE ||
1436 i_ct == INPUT_CONTROL_SET_RATE ||
1437 i_ct == INPUT_CONTROL_SET_POSITION ||
1438 i_ct == INPUT_CONTROL_SET_TIME ||
1439 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1440 i_ct == INPUT_CONTROL_SET_TITLE ||
1441 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1442 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1445 // msg_Dbg( p_input, "merged at %d", i );
1446 /* Remove the i-1 */
1447 for( j = i; j < p_input->p->i_control; j++ )
1448 p_input->p->control[j-1] = p_input->p->control[j];
1449 p_input->p->i_control--;
1453 /* TODO but that's not that important
1454 - merge SET_X with SET_X_CMD
1455 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1456 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1463 static bool Control( input_thread_t *p_input, int i_type,
1466 bool b_force_update = false;
1468 if( !p_input ) return b_force_update;
1472 case INPUT_CONTROL_SET_DIE:
1473 msg_Dbg( p_input, "control: stopping input" );
1475 /* Mark all submodules to die */
1476 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1479 case INPUT_CONTROL_SET_POSITION:
1480 case INPUT_CONTROL_SET_POSITION_OFFSET:
1484 if( p_input->p->b_recording )
1486 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1489 if( i_type == INPUT_CONTROL_SET_POSITION )
1491 f_pos = val.f_float;
1495 /* Should not fail */
1496 demux_Control( p_input->p->input.p_demux,
1497 DEMUX_GET_POSITION, &f_pos );
1498 f_pos += val.f_float;
1500 if( f_pos < 0.0 ) f_pos = 0.0;
1501 if( f_pos > 1.0 ) f_pos = 1.0;
1502 /* Reset the decoders states and clock sync (before calling the demuxer */
1503 input_EsOutChangePosition( p_input->p->p_es_out );
1504 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1507 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1508 "%2.1f%% failed", f_pos * 100 );
1512 if( p_input->p->i_slave > 0 )
1513 SlaveSeek( p_input );
1515 b_force_update = true;
1520 case INPUT_CONTROL_SET_TIME:
1521 case INPUT_CONTROL_SET_TIME_OFFSET:
1526 if( p_input->p->b_recording )
1528 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1532 if( i_type == INPUT_CONTROL_SET_TIME )
1534 i_time = val.i_time;
1538 /* Should not fail */
1539 demux_Control( p_input->p->input.p_demux,
1540 DEMUX_GET_TIME, &i_time );
1541 i_time += val.i_time;
1543 if( i_time < 0 ) i_time = 0;
1545 /* Reset the decoders states and clock sync (before calling the demuxer */
1546 input_EsOutChangePosition( p_input->p->p_es_out );
1548 i_ret = demux_Control( p_input->p->input.p_demux,
1549 DEMUX_SET_TIME, i_time );
1554 /* Emulate it with a SET_POS */
1555 demux_Control( p_input->p->input.p_demux,
1556 DEMUX_GET_LENGTH, &i_length );
1559 double f_pos = (double)i_time / (double)i_length;
1560 i_ret = demux_Control( p_input->p->input.p_demux,
1561 DEMUX_SET_POSITION, f_pos );
1566 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1567 " failed or not possible", i_time );
1571 if( p_input->p->i_slave > 0 )
1572 SlaveSeek( p_input );
1574 b_force_update = true;
1579 case INPUT_CONTROL_SET_STATE:
1580 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1581 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1584 if( p_input->p->input.p_access )
1585 i_ret = access_Control( p_input->p->input.p_access,
1586 ACCESS_SET_PAUSE_STATE, false );
1588 i_ret = demux_Control( p_input->p->input.p_demux,
1589 DEMUX_SET_PAUSE_STATE, false );
1593 /* FIXME What to do ? */
1594 msg_Warn( p_input, "cannot unset pause -> EOF" );
1595 vlc_mutex_unlock( &p_input->p->lock_control );
1596 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1597 vlc_mutex_lock( &p_input->p->lock_control );
1600 b_force_update = true;
1602 /* Switch to play */
1603 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1607 input_EsOutChangeState( p_input->p->p_es_out );
1609 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1610 p_input->p->b_can_pause )
1613 if( p_input->p->input.p_access )
1614 i_ret = access_Control( p_input->p->input.p_access,
1615 ACCESS_SET_PAUSE_STATE, true );
1617 i_ret = demux_Control( p_input->p->input.p_demux,
1618 DEMUX_SET_PAUSE_STATE, true );
1620 b_force_update = true;
1624 msg_Warn( p_input, "cannot set pause state" );
1625 state = p_input->i_state;
1632 /* Switch to new state */
1633 input_ChangeStateWithVarCallback( p_input, state, false );
1637 input_EsOutChangeState( p_input->p->p_es_out );
1639 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1641 b_force_update = true;
1643 /* Correct "state" value */
1644 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1646 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1648 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1652 case INPUT_CONTROL_SET_RATE:
1653 case INPUT_CONTROL_SET_RATE_SLOWER:
1654 case INPUT_CONTROL_SET_RATE_FASTER:
1658 if( i_type == INPUT_CONTROL_SET_RATE )
1664 static const int ppi_factor[][2] = {
1665 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1667 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1676 for( i = 0; ppi_factor[i][0] != 0; i++ )
1678 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1679 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1680 if( i_test_e < i_error )
1686 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1688 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1690 if( ppi_factor[i_idx+1][0] > 0 )
1691 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1693 i_rate = INPUT_RATE_MAX+1;
1697 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1699 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1701 i_rate = INPUT_RATE_MIN-1;
1705 if( i_rate < INPUT_RATE_MIN )
1707 msg_Dbg( p_input, "cannot set rate faster" );
1708 i_rate = INPUT_RATE_MIN;
1710 else if( i_rate > INPUT_RATE_MAX )
1712 msg_Dbg( p_input, "cannot set rate slower" );
1713 i_rate = INPUT_RATE_MAX;
1715 if( i_rate != INPUT_RATE_DEFAULT &&
1716 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1717 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1719 msg_Dbg( p_input, "cannot change rate" );
1720 i_rate = INPUT_RATE_DEFAULT;
1722 if( i_rate != p_input->p->i_rate &&
1723 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1726 if( p_input->p->input.p_access )
1727 i_ret = VLC_EGENERIC;
1729 i_ret = demux_Control( p_input->p->input.p_demux,
1730 DEMUX_SET_RATE, &i_rate );
1733 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1734 i_rate = p_input->p->i_rate;
1739 if( i_rate != p_input->p->i_rate )
1742 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1743 var_SetBool( p_input, "rate-change", true );
1745 p_input->p->i_rate = i_rate;
1747 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1748 if( p_input->p->input.b_rescale_ts )
1749 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1751 b_force_update = true;
1756 case INPUT_CONTROL_SET_PROGRAM:
1757 /* No need to force update, es_out does it if needed */
1758 es_out_Control( p_input->p->p_es_out,
1759 ES_OUT_SET_GROUP, val.i_int );
1761 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1765 case INPUT_CONTROL_SET_ES:
1766 /* No need to force update, es_out does it if needed */
1767 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1768 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1771 case INPUT_CONTROL_RESTART_ES:
1772 es_out_Control( p_input->p->p_es_out, ES_OUT_RESTART_ES,
1773 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1776 case INPUT_CONTROL_SET_AUDIO_DELAY:
1777 input_EsOutSetDelay( p_input->p->p_es_out,
1778 AUDIO_ES, val.i_time );
1779 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1782 case INPUT_CONTROL_SET_SPU_DELAY:
1783 input_EsOutSetDelay( p_input->p->p_es_out,
1784 SPU_ES, val.i_time );
1785 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1788 case INPUT_CONTROL_SET_TITLE:
1789 case INPUT_CONTROL_SET_TITLE_NEXT:
1790 case INPUT_CONTROL_SET_TITLE_PREV:
1791 if( p_input->p->b_recording )
1793 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1796 if( p_input->p->input.b_title_demux &&
1797 p_input->p->input.i_title > 0 )
1800 /* FIXME handle demux title */
1801 demux_t *p_demux = p_input->p->input.p_demux;
1804 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1805 i_title = p_demux->info.i_title - 1;
1806 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1807 i_title = p_demux->info.i_title + 1;
1809 i_title = val.i_int;
1811 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1813 input_EsOutChangePosition( p_input->p->p_es_out );
1815 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1816 input_ControlVarTitle( p_input, i_title );
1819 else if( p_input->p->input.i_title > 0 )
1821 access_t *p_access = p_input->p->input.p_access;
1824 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1825 i_title = p_access->info.i_title - 1;
1826 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1827 i_title = p_access->info.i_title + 1;
1829 i_title = val.i_int;
1831 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1833 input_EsOutChangePosition( p_input->p->p_es_out );
1835 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1836 stream_AccessReset( p_input->p->input.p_stream );
1840 case INPUT_CONTROL_SET_SEEKPOINT:
1841 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1842 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1843 if( p_input->p->b_recording )
1845 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1849 if( p_input->p->input.b_title_demux &&
1850 p_input->p->input.i_title > 0 )
1852 demux_t *p_demux = p_input->p->input.p_demux;
1854 int64_t i_input_time;
1855 int64_t i_seekpoint_time;
1857 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1859 i_seekpoint = p_demux->info.i_seekpoint;
1860 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1861 if( i_seekpoint_time >= 0 &&
1862 !demux_Control( p_demux,
1863 DEMUX_GET_TIME, &i_input_time ) )
1865 if( i_input_time < i_seekpoint_time + 3000000 )
1871 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1872 i_seekpoint = p_demux->info.i_seekpoint + 1;
1874 i_seekpoint = val.i_int;
1876 if( i_seekpoint >= 0 && i_seekpoint <
1877 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1880 input_EsOutChangePosition( p_input->p->p_es_out );
1882 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1885 else if( p_input->p->input.i_title > 0 )
1887 demux_t *p_demux = p_input->p->input.p_demux;
1888 access_t *p_access = p_input->p->input.p_access;
1890 int64_t i_input_time;
1891 int64_t i_seekpoint_time;
1893 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1895 i_seekpoint = p_access->info.i_seekpoint;
1896 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1897 if( i_seekpoint_time >= 0 &&
1898 demux_Control( p_demux,
1899 DEMUX_GET_TIME, &i_input_time ) )
1901 if( i_input_time < i_seekpoint_time + 3000000 )
1907 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1908 i_seekpoint = p_access->info.i_seekpoint + 1;
1910 i_seekpoint = val.i_int;
1912 if( i_seekpoint >= 0 && i_seekpoint <
1913 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1915 input_EsOutChangePosition( p_input->p->p_es_out );
1917 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1919 stream_AccessReset( p_input->p->input.p_stream );
1924 case INPUT_CONTROL_ADD_SUBTITLE:
1925 if( val.psz_string )
1927 SubtitleAdd( p_input, val.psz_string, true );
1928 free( val.psz_string );
1932 case INPUT_CONTROL_ADD_SLAVE:
1933 if( val.psz_string )
1935 input_source_t *slave = InputSourceNew( p_input );
1937 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1943 msg_Dbg( p_input, "adding %s as slave on the fly",
1947 if( demux_Control( p_input->p->input.p_demux,
1948 DEMUX_GET_TIME, &i_time ) )
1950 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1951 InputSourceClean( slave );
1955 if( demux_Control( slave->p_demux,
1956 DEMUX_SET_TIME, i_time ) )
1958 msg_Err( p_input, "seek failed for new slave" );
1959 InputSourceClean( slave );
1964 /* Get meta (access and demux) */
1965 p_meta = vlc_meta_New();
1966 access_Control( slave->p_access, ACCESS_GET_META,
1968 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1969 InputUpdateMeta( p_input, p_meta );
1971 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1976 msg_Warn( p_input, "failed to add %s as slave",
1980 free( val.psz_string );
1984 case INPUT_CONTROL_SET_RECORD_STATE:
1985 if( !!p_input->p->b_recording != !!val.b_bool )
1987 if( p_input->p->input.b_can_stream_record )
1989 if( demux_Control( p_input->p->input.p_demux,
1990 DEMUX_SET_RECORD_STATE, val.b_bool ) )
1995 if( input_EsOutSetRecord( p_input->p->p_es_out, val.b_bool ) )
1998 p_input->p->b_recording = val.b_bool;
2000 var_Change( p_input, "record", VLC_VAR_SETVALUE, &val, NULL );
2002 b_force_update = true;
2006 case INPUT_CONTROL_SET_BOOKMARK:
2008 msg_Err( p_input, "not yet implemented" );
2012 return b_force_update;
2015 /*****************************************************************************
2017 *****************************************************************************/
2018 static int UpdateFromDemux( input_thread_t *p_input )
2020 demux_t *p_demux = p_input->p->input.p_demux;
2023 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2025 v.i_int = p_demux->info.i_title;
2026 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2028 input_ControlVarTitle( p_input, p_demux->info.i_title );
2030 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2032 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2034 v.i_int = p_demux->info.i_seekpoint;
2035 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2037 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2039 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2041 /* Hmmm only works with master input */
2042 if( p_input->p->input.p_demux == p_demux )
2044 int i_title_end = p_input->p->input.i_title_end -
2045 p_input->p->input.i_title_offset;
2046 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2047 p_input->p->input.i_seekpoint_offset;
2049 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2051 if( p_demux->info.i_title > i_title_end ||
2052 ( p_demux->info.i_title == i_title_end &&
2053 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2055 else if( i_seekpoint_end >=0 )
2057 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2059 else if( i_title_end >= 0 )
2061 if( p_demux->info.i_title > i_title_end ) return 0;
2068 /*****************************************************************************
2070 *****************************************************************************/
2071 static int UpdateFromAccess( input_thread_t *p_input )
2073 access_t *p_access = p_input->p->input.p_access;
2076 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2078 v.i_int = p_access->info.i_title;
2079 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2081 input_ControlVarTitle( p_input, p_access->info.i_title );
2083 stream_AccessUpdate( p_input->p->input.p_stream );
2085 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2087 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2089 v.i_int = p_access->info.i_seekpoint;
2090 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2091 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2093 if( p_access->info.i_update & INPUT_UPDATE_META )
2095 /* TODO maybe multi - access ? */
2096 vlc_meta_t *p_meta = vlc_meta_New();
2097 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2098 InputUpdateMeta( p_input, p_meta );
2099 p_access->info.i_update &= ~INPUT_UPDATE_META;
2101 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2106 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2107 f_quality = f_strength = -1;
2109 var_SetFloat( p_input, "signal-quality", f_quality );
2110 var_SetFloat( p_input, "signal-strength", f_strength );
2112 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2115 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2117 /* Hmmm only works with master input */
2118 if( p_input->p->input.p_access == p_access )
2120 int i_title_end = p_input->p->input.i_title_end -
2121 p_input->p->input.i_title_offset;
2122 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2123 p_input->p->input.i_seekpoint_offset;
2125 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2127 if( p_access->info.i_title > i_title_end ||
2128 ( p_access->info.i_title == i_title_end &&
2129 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2131 else if( i_seekpoint_end >=0 )
2133 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2135 else if( i_title_end >= 0 )
2137 if( p_access->info.i_title > i_title_end ) return 0;
2144 /*****************************************************************************
2146 *****************************************************************************/
2147 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2149 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2152 /*****************************************************************************
2154 *****************************************************************************/
2155 static input_source_t *InputSourceNew( input_thread_t *p_input )
2157 VLC_UNUSED(p_input);
2158 input_source_t *in = malloc( sizeof( input_source_t ) );
2160 memset( in, 0, sizeof( input_source_t ) );
2164 /*****************************************************************************
2166 *****************************************************************************/
2167 static int InputSourceInit( input_thread_t *p_input,
2168 input_source_t *in, const char *psz_mrl,
2169 const char *psz_forced_demux )
2171 const bool b_master = in == &p_input->p->input;
2173 char psz_dup[strlen(psz_mrl) + 1];
2174 const char *psz_access;
2175 const char *psz_demux;
2182 strcpy( psz_dup, psz_mrl );
2184 if( !in ) return VLC_EGENERIC;
2185 if( !p_input ) return VLC_EGENERIC;
2188 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2190 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2191 psz_mrl, psz_access, psz_demux, psz_path );
2192 if( !p_input->b_preparsing )
2194 /* Hack to allow udp://@:port syntax */
2196 (strncmp( psz_access, "udp", 3 ) &&
2197 strncmp( psz_access, "rtp", 3 )) )
2199 /* Find optional titles and seekpoints */
2200 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2201 &in->i_seekpoint_start, &in->i_seekpoint_end );
2204 if( psz_forced_demux && *psz_forced_demux )
2206 psz_demux = psz_forced_demux;
2208 else if( *psz_demux == '\0' )
2210 /* special hack for forcing a demuxer with --demux=module
2211 * (and do nothing with a list) */
2212 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2214 if( psz_var_demux != NULL &&
2215 !strchr(psz_var_demux, ',' ) &&
2216 !strchr(psz_var_demux, ':' ) )
2218 psz_demux = psz_var_demux;
2220 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2224 /* Try access_demux first */
2225 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2226 NULL, p_input->p->p_es_out, false );
2230 /* Preparsing is only for file:// */
2233 if( !*psz_access ) /* path without scheme:// */
2234 psz_access = "file";
2235 if( strcmp( psz_access, "file" ) )
2237 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2242 int64_t i_pts_delay;
2244 /* Get infos from access_demux */
2245 demux_Control( in->p_demux,
2246 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2247 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2249 in->b_title_demux = true;
2250 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2251 &in->title, &in->i_title,
2252 &in->i_title_offset, &in->i_seekpoint_offset ) )
2254 TAB_INIT( in->i_title, in->title );
2256 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2257 &in->b_can_pace_control ) )
2258 in->b_can_pace_control = false;
2260 if( !in->b_can_pace_control )
2262 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2263 &in->b_can_rate_control, &in->b_rescale_ts ) )
2265 in->b_can_rate_control = false;
2266 in->b_rescale_ts = true; /* not used */
2271 in->b_can_rate_control = true;
2272 in->b_rescale_ts = true;
2274 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2275 &in->b_can_pause ) )
2276 in->b_can_pause = false;
2277 var_SetBool( p_input, "can-pause", in->b_can_pause );
2279 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2281 if( ret != VLC_SUCCESS )
2283 var_Set( p_input, "seekable", val );
2287 int64_t i_pts_delay;
2290 input_ChangeState( p_input, OPENING_S );
2292 /* Now try a real access */
2293 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2295 /* Access failed, URL encoded ? */
2296 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2298 decode_URI( psz_path );
2300 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2301 psz_access, psz_demux, psz_path );
2303 in->p_access = access_New( p_input,
2304 psz_access, psz_demux, psz_path );
2306 if( in->p_access == NULL )
2308 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2310 intf_UserFatal( VLC_OBJECT( p_input), false,
2311 _("Your input can't be opened"),
2312 _("VLC is unable to open the MRL '%s'."
2313 " Check the log for details."), psz_mrl );
2318 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2319 while( psz && *psz )
2321 access_t *p_access = in->p_access;
2322 char *end = strchr( psz, ':' );
2327 in->p_access = access_FilterNew( in->p_access, psz );
2328 if( in->p_access == NULL )
2330 in->p_access = p_access;
2331 msg_Warn( p_input, "failed to insert access filter %s",
2339 /* Get infos from access */
2340 if( !p_input->b_preparsing )
2342 access_Control( in->p_access,
2343 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2344 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2346 in->b_title_demux = false;
2347 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2348 &in->title, &in->i_title,
2349 &in->i_title_offset, &in->i_seekpoint_offset ) )
2352 TAB_INIT( in->i_title, in->title );
2354 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2355 &in->b_can_pace_control );
2356 in->b_can_rate_control = in->b_can_pace_control;
2357 in->b_rescale_ts = true;
2359 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2361 var_SetBool( p_input, "can-pause", in->b_can_pause );
2362 access_Control( in->p_access, ACCESS_CAN_SEEK,
2364 var_Set( p_input, "seekable", val );
2368 input_ChangeState( p_input, BUFFERING_S );
2370 /* Autodetect extra files if none specified */
2371 char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2372 if( !psz_input_list )
2374 char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2375 if( psz_extra_files )
2376 var_SetString( p_input, "input-list", psz_extra_files );
2377 free( psz_extra_files );
2380 /* Create the stream_t */
2381 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2383 /* Restor old value */
2384 if( !psz_input_list )
2385 var_SetString( p_input, "input-list", "" );
2386 free( psz_input_list );
2388 if( in->p_stream == NULL )
2390 msg_Warn( p_input, "cannot create a stream_t from access" );
2394 /* Open a demuxer */
2395 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2397 psz_demux = in->p_access->psz_demux;
2401 /* Take access redirections into account */
2402 char *psz_real_path;
2403 char *psz_buf = NULL;
2404 if( in->p_access->psz_path )
2406 const char *psz_a, *psz_d;
2407 psz_buf = strdup( in->p_access->psz_path );
2408 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2412 psz_real_path = psz_path;
2414 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2416 in->p_stream, p_input->p->p_es_out,
2417 p_input->b_preparsing );
2421 if( in->p_demux == NULL )
2423 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2424 psz_access, psz_demux, psz_path );
2425 intf_UserFatal( VLC_OBJECT( p_input ), false,
2426 _("VLC can't recognize the input's format"),
2427 _("The format of '%s' cannot be detected. "
2428 "Have a look at the log for details."), psz_mrl );
2432 /* Get title from demux */
2433 if( !p_input->b_preparsing && in->i_title <= 0 )
2435 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2436 &in->title, &in->i_title,
2437 &in->i_title_offset, &in->i_seekpoint_offset ))
2439 TAB_INIT( in->i_title, in->title );
2443 in->b_title_demux = true;
2448 /* Set record capabilities */
2449 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2450 in->b_can_stream_record = false;
2452 if( !var_CreateGetBool( p_input, "input-record-native" ) )
2453 in->b_can_stream_record = false;
2454 var_SetBool( p_input, "can-record", true );
2456 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2460 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2461 if( 1 || !p_input->b_preparsing )
2464 input_attachment_t **attachment;
2465 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2466 &attachment, &i_attachment ) )
2468 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2469 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2470 i_attachment, attachment );
2471 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2474 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2476 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2478 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2481 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2482 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2488 input_ChangeState( p_input, ERROR_S );
2491 demux_Delete( in->p_demux );
2494 stream_Delete( in->p_stream );
2497 access_Delete( in->p_access );
2499 return VLC_EGENERIC;
2502 /*****************************************************************************
2504 *****************************************************************************/
2505 static void InputSourceClean( input_source_t *in )
2510 demux_Delete( in->p_demux );
2513 stream_Delete( in->p_stream );
2516 access_Delete( in->p_access );
2518 if( in->i_title > 0 )
2520 for( i = 0; i < in->i_title; i++ )
2521 vlc_input_title_Delete( in->title[i] );
2522 TAB_CLEAN( in->i_title, in->title );
2526 static void SlaveDemux( input_thread_t *p_input )
2530 bool b_set_time = true;
2532 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2534 /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2538 for( i = 0; i < p_input->p->i_slave; i++ )
2540 input_source_t *in = p_input->p->slave[i];
2546 if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2551 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2553 msg_Err( p_input, "slave[%d] doesn't like "
2554 "DEMUX_GET_TIME -> EOF", i );
2559 if( i_stime >= i_time )
2562 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2568 i_ret = in->p_demux->pf_demux( in->p_demux );
2573 msg_Dbg( p_input, "slave %d EOF", i );
2579 static void SlaveSeek( input_thread_t *p_input )
2584 if( !p_input ) return;
2586 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2588 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2592 for( i = 0; i < p_input->p->i_slave; i++ )
2594 input_source_t *in = p_input->p->slave[i];
2596 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2599 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2609 /*****************************************************************************
2611 *****************************************************************************/
2612 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2616 if( !p_meta ) return;
2618 /* Get meta information from user */
2619 #define GET_META( field, s ) \
2620 var_Get( p_input, (s), &val ); \
2621 if( *val.psz_string ) \
2622 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2623 free( val.psz_string )
2625 GET_META( Title, "meta-title" );
2626 GET_META( Artist, "meta-artist" );
2627 GET_META( Genre, "meta-genre" );
2628 GET_META( Copyright, "meta-copyright" );
2629 GET_META( Description, "meta-description" );
2630 GET_META( Date, "meta-date" );
2631 GET_META( URL, "meta-url" );
2635 /*****************************************************************************
2636 * InputGetExtraFiles
2637 * Autodetect extra input list
2638 *****************************************************************************/
2639 static char *InputGetExtraFiles( input_thread_t *p_input,
2640 const char *psz_access, const char *psz_path )
2642 char *psz_list = NULL;
2644 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2648 const char *psz_ext = strrchr( psz_path, '.' );
2649 if( !psz_ext || strcmp( psz_ext, ".001" ) )
2652 char *psz_file = strdup( psz_path );
2656 /* Try to list .xyz files */
2657 for( int i = 2; i < 999; i++ )
2659 char *psz_ext = strrchr( psz_file, '.' );
2662 snprintf( psz_ext, 5, ".%.3d", i );
2664 if( utf8_stat( psz_file, &st )
2665 || !S_ISREG( st.st_mode ) || !st.st_size )
2668 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2672 char *psz_old = psz_list;
2673 /* FIXME how to handle file with ',' ?*/
2674 if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2682 psz_list = strdup( psz_file );
2690 /*****************************************************************************
2691 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2692 * arturl and locking issue.
2693 *****************************************************************************/
2694 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2696 input_item_t *p_item = p_input->p->input.p_item;
2697 char * psz_arturl = NULL;
2698 char *psz_title = NULL;
2699 int i_arturl_event = false;
2704 psz_arturl = input_item_GetArtURL( p_item );
2706 vlc_mutex_lock( &p_item->lock );
2707 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2708 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2710 vlc_meta_Merge( p_item->p_meta, p_meta );
2712 if( psz_arturl && *psz_arturl )
2714 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2715 i_arturl_event = true;
2718 vlc_meta_Delete( p_meta );
2720 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2722 /* Don't look for art cover if sout
2723 * XXX It can change when sout has meta data support */
2724 if( p_input->p->p_sout && !p_input->b_preparsing )
2726 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2727 i_arturl_event = true;
2731 input_ExtractAttachmentAndCacheArt( p_input );
2737 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2739 p_meta = vlc_meta_New();
2740 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2742 vlc_mutex_unlock( &p_item->lock );
2744 input_item_SetPreparsed( p_item, true );
2746 if( i_arturl_event == true )
2750 /* Notify interested third parties */
2751 event.type = vlc_InputItemMetaChanged;
2752 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2753 vlc_event_send( &p_item->event_manager, &event );
2758 input_Control( p_input, INPUT_SET_NAME, psz_title );
2762 /** \todo handle sout meta */
2766 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2767 int i_new, input_attachment_t **pp_new )
2769 int i_attachment = *pi_attachment;
2770 input_attachment_t **attachment = *ppp_attachment;
2773 attachment = realloc( attachment,
2774 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2775 for( i = 0; i < i_new; i++ )
2776 attachment[i_attachment++] = pp_new[i];
2780 *pi_attachment = i_attachment;
2781 *ppp_attachment = attachment;
2784 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2788 if( p_input->b_preparsing )
2791 if( p_input->p->input.p_access )
2792 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2795 /* Get meta data from slave input */
2796 for( i = 0; i < p_input->p->i_slave; i++ )
2798 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2799 if( p_input->p->slave[i]->p_access )
2801 access_Control( p_input->p->slave[i]->p_access,
2802 ACCESS_GET_META, p_meta );
2807 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2814 /* XXX I am not sure it is a great idea, besides, there is more than that
2815 * if we want to do it right */
2816 vlc_mutex_lock( &p_item->lock );
2817 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2819 vlc_mutex_unlock( &p_item->lock );
2822 vlc_mutex_unlock( &p_item->lock );
2825 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2826 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2831 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2832 if(! p_demux->p_private )
2835 p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2838 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2840 if( p_demux_meta->p_meta )
2842 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2843 vlc_meta_Delete( p_demux_meta->p_meta );
2846 if( p_demux_meta->i_attachments > 0 )
2848 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2849 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2850 p_demux_meta->i_attachments, p_demux_meta->attachments );
2851 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2853 module_unneed( p_demux, p_id3 );
2855 free( p_demux->p_private );
2859 /*****************************************************************************
2860 * MRLSplit: parse the access, demux and url part of the
2861 * Media Resource Locator.
2862 *****************************************************************************/
2863 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2866 char *psz_access = NULL;
2867 char *psz_demux = NULL;
2870 /* Either there is an access/demux specification before ://
2871 * or we have a plain local file path. */
2872 psz_path = strstr( psz_dup, "://" );
2873 if( psz_path != NULL )
2876 psz_path += 3; /* skips "://" */
2878 /* Separate access from demux (<access>/<demux>://<path>) */
2879 psz_access = psz_dup;
2880 psz_demux = strchr( psz_access, '/' );
2882 *psz_demux++ = '\0';
2884 /* We really don't want module name substitution here! */
2885 if( psz_access[0] == '$' )
2887 if( psz_demux && psz_demux[0] == '$' )
2894 *ppsz_access = psz_access ? psz_access : (char*)"";
2895 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2896 *ppsz_path = psz_path;
2899 static inline bool next(char ** src)
2903 long result = strtol( *src, &end, 0 );
2904 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
2913 /*****************************************************************************
2914 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2917 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2918 *****************************************************************************/
2919 static void MRLSections( input_thread_t *p_input, char *psz_source,
2920 int *pi_title_start, int *pi_title_end,
2921 int *pi_chapter_start, int *pi_chapter_end )
2923 char *psz, *psz_end, *psz_next, *psz_check;
2925 *pi_title_start = *pi_title_end = -1;
2926 *pi_chapter_start = *pi_chapter_end = -1;
2928 /* Start by parsing titles and chapters */
2929 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2932 /* Check we are really dealing with a title/chapter section */
2933 psz_check = psz + 1;
2934 if( !*psz_check ) return;
2935 if( isdigit(*psz_check) )
2936 if(!next(&psz_check)) return;
2937 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2938 if( *psz_check == ':' && ++psz_check )
2940 if( isdigit(*psz_check) )
2941 if(!next(&psz_check)) return;
2943 if( *psz_check != '-' && *psz_check ) return;
2944 if( *psz_check == '-' && ++psz_check )
2946 if( isdigit(*psz_check) )
2947 if(!next(&psz_check)) return;
2949 if( *psz_check != ':' && *psz_check ) return;
2950 if( *psz_check == ':' && ++psz_check )
2952 if( isdigit(*psz_check) )
2953 if(!next(&psz_check)) return;
2955 if( *psz_check ) return;
2957 /* Separate start and end */
2959 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2961 /* Look for the start title */
2962 *pi_title_start = strtol( psz, &psz_next, 0 );
2963 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2964 *pi_title_end = *pi_title_start;
2967 /* Look for the start chapter */
2969 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2970 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2971 *pi_chapter_end = *pi_chapter_start;
2975 /* Look for the end title */
2976 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2977 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2980 /* Look for the end chapter */
2981 if( *psz_end ) psz_end++;
2982 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2983 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2986 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2987 psz_source, *pi_title_start, *pi_chapter_start,
2988 *pi_title_end, *pi_chapter_end );
2991 /*****************************************************************************
2992 * input_AddSubtitles: add a subtitles file and enable it
2993 *****************************************************************************/
2994 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
2996 input_source_t *sub;
2999 char *psz_path, *psz_extension;
3001 /* if we are provided a subtitle.sub file,
3002 * see if we don't have a subtitle.idx and use it instead */
3003 psz_path = strdup( psz_subtitle );
3006 psz_extension = strrchr( psz_path, '.');
3007 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3011 strcpy( psz_extension, ".idx" );
3013 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3015 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3016 psz_path, psz_subtitle );
3017 strcpy( psz_subtitle, psz_path );
3023 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3025 sub = InputSourceNew( p_input );
3026 if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3031 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3034 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3036 if( count.i_int == 0 )
3038 /* if it was first one, there is disable too */
3040 if( count.i_int < list.p_list->i_count )
3042 int i_id = list.p_list->p_values[count.i_int].i_int;
3043 es_out_id_t *p_es = input_EsOutGetFromID( p_input->p->p_es_out, i_id );
3045 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_DEFAULT, p_es );
3046 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES, p_es );
3048 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3052 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3053 bool b_check_extension )
3057 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3060 assert( psz_subtitle != NULL );
3062 val.psz_string = strdup( psz_subtitle );
3063 if( val.psz_string )
3064 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3068 /*****************************************************************************
3069 * input_get_event_manager
3070 *****************************************************************************/
3071 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3073 return &p_input->p->event_manager;
3077 /* TODO FIXME nearly the same logic that snapshot code */
3078 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3083 path = utf8_opendir( psz_path );
3088 char *psz_tmp = str_format( p_obj, psz_prefix );
3092 filename_sanitize( psz_tmp );
3093 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3095 psz_extension ? "." : "",
3096 psz_extension ? psz_extension : "" ) < 0 )
3103 psz_file = str_format( p_obj, psz_path );
3104 path_sanitize( psz_file );