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 )
732 b_force_update = false;
733 if( p_input->i_state != PAUSE_S )
734 MainLoopDemux( p_input, &b_force_update, &i_start_mdate );
738 if( p_input->i_state == PAUSE_S )
739 i_deadline = __MIN( i_intf_update, i_statistic_update );
742 vlc_mutex_lock( &p_input->p->lock_control );
743 ControlReduce( p_input );
744 while( !ControlPopNoLock( p_input, &i_type, &val, i_deadline ) )
746 msg_Dbg( p_input, "control type=%d", i_type );
747 if( Control( p_input, i_type, val ) )
748 b_force_update = true;
750 vlc_mutex_unlock( &p_input->p->lock_control );
752 /* Update interface and statistics */
754 if( i_intf_update < i_current || b_force_update )
756 MainLoopInterface( p_input );
757 i_intf_update = i_current + INT64_C(250000);
759 if( i_statistic_update < i_current )
761 MainLoopStatistic( p_input );
762 i_statistic_update = i_current + INT64_C(1000000);
766 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
768 /* We have finish to demux data but not to play them */
769 while( !p_input->b_die )
771 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
774 msg_Dbg( p_input, "waiting decoder fifos to empty" );
776 msleep( INPUT_IDLE_SLEEP );
779 /* We have finished */
780 input_ChangeState( p_input, END_S );
784 static void InitStatistics( input_thread_t * p_input )
786 if( p_input->b_preparsing ) return;
788 /* Prepare statistics */
789 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
790 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
791 if( libvlc_stats( p_input ) )
793 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
794 INIT_COUNTER( read_packets, INTEGER, COUNTER );
795 INIT_COUNTER( demux_read, INTEGER, COUNTER );
796 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
797 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
798 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
799 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
800 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
801 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
802 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
803 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
804 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
805 p_input->p->counters.p_sout_send_bitrate = NULL;
806 p_input->p->counters.p_sout_sent_packets = NULL;
807 p_input->p->counters.p_sout_sent_bytes = NULL;
808 if( p_input->p->counters.p_demux_bitrate )
809 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
810 if( p_input->p->counters.p_input_bitrate )
811 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
816 static int InitSout( input_thread_t * p_input )
820 if( p_input->b_preparsing ) return VLC_SUCCESS;
822 /* Find a usable sout and attach it to p_input */
823 psz = var_GetNonEmptyString( p_input, "sout" );
824 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
826 /* Check the validity of the provided sout */
827 if( p_input->p->p_sout )
829 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
831 msg_Dbg( p_input, "destroying unusable sout" );
833 sout_DeleteInstance( p_input->p->p_sout );
834 p_input->p->p_sout = NULL;
838 if( p_input->p->p_sout )
841 msg_Dbg( p_input, "sout keep: reusing sout" );
842 msg_Dbg( p_input, "sout keep: you probably want to use "
843 "gather stream_out" );
844 vlc_object_attach( p_input->p->p_sout, p_input );
848 /* Create a new one */
849 p_input->p->p_sout = sout_NewInstance( p_input, psz );
850 if( !p_input->p->p_sout )
852 input_ChangeState( p_input, ERROR_S );
853 msg_Err( p_input, "cannot start stream output instance, " \
859 if( libvlc_stats( p_input ) )
861 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
862 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
863 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
864 if( p_input->p->counters.p_sout_send_bitrate )
865 p_input->p->counters.p_sout_send_bitrate->update_interval =
869 else if( p_input->p->p_sout )
871 msg_Dbg( p_input, "destroying useless sout" );
873 sout_DeleteInstance( p_input->p->p_sout );
874 p_input->p->p_sout = NULL;
882 static void InitTitle( input_thread_t * p_input )
886 if( p_input->b_preparsing ) return;
888 /* Create global title (from master) */
889 p_input->p->i_title = p_input->p->input.i_title;
890 p_input->p->title = p_input->p->input.title;
891 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
892 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
893 if( p_input->p->i_title > 0 )
895 /* Setup variables */
896 input_ControlVarNavigation( p_input );
897 input_ControlVarTitle( p_input, 0 );
901 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
902 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
903 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
906 if( p_input->i_pts_delay < 0 )
907 p_input->i_pts_delay = 0;
909 /* If the desynchronisation requested by the user is < 0, we need to
910 * cache more data. */
911 var_Get( p_input, "audio-desync", &val );
912 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
914 /* Update cr_average depending on the caching */
915 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
916 p_input->p->input.i_cr_average /= 10;
917 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
920 static void StartTitle( input_thread_t * p_input )
929 /* Start title/chapter */
931 if( p_input->b_preparsing )
933 p_input->p->i_start = 0;
937 val.i_int = p_input->p->input.i_title_start -
938 p_input->p->input.i_title_offset;
939 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
940 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
941 val.i_int = p_input->p->input.i_seekpoint_start -
942 p_input->p->input.i_seekpoint_offset;
943 if( val.i_int > 0 /* TODO: check upper boundary */ )
944 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
948 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
949 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
950 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
951 i_length = var_GetTime( p_input, "length" );
952 if( p_input->p->i_run < 0 )
954 msg_Warn( p_input, "invalid run-time ignored" );
955 p_input->p->i_run = 0;
958 if( p_input->p->i_start > 0 )
960 if( p_input->p->i_start >= i_length )
962 msg_Warn( p_input, "invalid start-time ignored" );
968 msg_Dbg( p_input, "starting at time: %ds",
969 (int)( p_input->p->i_start / INT64_C(1000000) ) );
971 s.i_time = p_input->p->i_start;
972 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
975 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
977 msg_Warn( p_input, "invalid stop-time ignored" );
978 p_input->p->i_stop = 0;
982 /* Get fps and set it if not already set */
983 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
986 float f_requested_fps;
988 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
989 var_SetFloat( p_input, "sub-original-fps", f_fps );
991 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
992 if( f_requested_fps != f_fps )
994 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
996 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1000 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1003 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1006 /* Look for and add subtitle files */
1007 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1008 if( psz_subtitle != NULL )
1010 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1011 SubtitleAdd( p_input, psz_subtitle, true );
1014 var_Get( p_input, "sub-autodetect-file", &val );
1017 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1018 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1019 p_input->p->input.p_item->psz_uri );
1020 free( psz_autopath );
1022 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1024 /* Try to autoselect the first autodetected subtitles file
1025 * if no subtitles file was specified */
1026 bool b_forced = i == 0 && !psz_subtitle;
1028 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1029 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1031 free( ppsz_subs[i] );
1035 free( psz_subtitle );
1037 /* Look for slave */
1038 psz = var_GetNonEmptyString( p_input, "input-slave" );
1042 input_source_t *slave;
1043 while( psz && *psz )
1045 while( *psz == ' ' || *psz == '#' )
1049 if( ( psz_delim = strchr( psz, '#' ) ) )
1051 *psz_delim++ = '\0';
1058 msg_Dbg( p_input, "adding slave input '%s'", psz );
1059 slave = InputSourceNew( p_input );
1060 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1062 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1071 static void InitPrograms( input_thread_t * p_input )
1076 if( p_input->b_preparsing ) return;
1079 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1080 i_es_out_mode = ES_OUT_MODE_AUTO;
1082 if( p_input->p->p_sout )
1084 var_Get( p_input, "sout-all", &val );
1087 i_es_out_mode = ES_OUT_MODE_ALL;
1092 var_Get( p_input, "programs", &val );
1093 if( val.p_list && val.p_list->i_count )
1095 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1096 /* Note : we should remove the "program" callback. */
1100 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1105 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1107 /* Inform the demuxer about waited group (needed only for DVB) */
1108 if( i_es_out_mode == ES_OUT_MODE_ALL )
1110 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1112 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1114 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1119 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1120 (int) var_GetInteger( p_input, "program" ), NULL );
1124 static int Init( input_thread_t * p_input )
1130 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1132 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1134 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1135 var_SetString( p_input, "sout", "" );
1136 var_SetBool( p_input, "sout-all", false );
1137 var_SetString( p_input, "input-slave", "" );
1138 var_SetInteger( p_input, "input-repeat", 0 );
1139 var_SetString( p_input, "sub-file", "" );
1140 var_SetBool( p_input, "sub-autodetect-file", false );
1144 InitStatistics( p_input );
1146 ret = InitSout( p_input );
1147 if( ret != VLC_SUCCESS )
1148 return ret; /* FIXME: goto error; should be better here */
1152 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1153 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1154 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1156 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1157 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1159 if( InputSourceInit( p_input, &p_input->p->input,
1160 p_input->p->input.p_item->psz_uri, NULL ) )
1165 InitTitle( p_input );
1167 /* Load master infos */
1169 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1170 &val.i_time ) && val.i_time > 0 )
1172 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1173 UpdateItemLength( p_input, val.i_time );
1177 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1178 if( val.i_time > 0 )
1179 { /* fallback: gets length from metadata */
1180 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1181 UpdateItemLength( p_input, val.i_time );
1185 StartTitle( p_input );
1187 InitPrograms( p_input );
1189 if( !p_input->b_preparsing && p_input->p->p_sout )
1191 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1193 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1195 /* We don't want a high input priority here or we'll
1196 * end-up sucking up all the CPU time */
1197 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1200 msg_Dbg( p_input, "starting in %s mode",
1201 p_input->p->b_out_pace_control ? "async" : "sync" );
1204 p_meta = vlc_meta_New();
1206 /* Get meta data from users */
1207 InputMetaUser( p_input, p_meta );
1209 /* Get meta data from master input */
1210 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1212 /* Access_file does not give any meta, and there are no slave */
1213 AccessMeta( p_input, p_meta );
1215 InputUpdateMeta( p_input, p_meta );
1217 if( !p_input->b_preparsing )
1219 msg_Dbg( p_input, "`%s' successfully opened",
1220 p_input->p->input.p_item->psz_uri );
1224 /* initialization is complete */
1225 input_ChangeState( p_input, PLAYING_S );
1230 input_ChangeState( p_input, ERROR_S );
1232 if( p_input->p->p_es_out )
1233 input_EsOutDelete( p_input->p->p_es_out );
1235 if( p_input->p->p_sout )
1237 vlc_object_detach( p_input->p->p_sout );
1238 sout_DeleteInstance( p_input->p->p_sout );
1242 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1244 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1245 stats_CounterClean( p_input->p->counters.p_##c );\
1246 p_input->p->counters.p_##c = NULL; } while(0)
1247 EXIT_COUNTER( read_bytes );
1248 EXIT_COUNTER( read_packets );
1249 EXIT_COUNTER( demux_read );
1250 EXIT_COUNTER( input_bitrate );
1251 EXIT_COUNTER( demux_bitrate );
1252 EXIT_COUNTER( played_abuffers );
1253 EXIT_COUNTER( lost_abuffers );
1254 EXIT_COUNTER( displayed_pictures );
1255 EXIT_COUNTER( lost_pictures );
1256 EXIT_COUNTER( decoded_audio );
1257 EXIT_COUNTER( decoded_video );
1258 EXIT_COUNTER( decoded_sub );
1260 if( p_input->p->p_sout )
1262 EXIT_COUNTER( sout_sent_packets );
1263 EXIT_COUNTER( sout_sent_bytes );
1264 EXIT_COUNTER( sout_send_bitrate );
1269 /* Mark them deleted */
1270 p_input->p->input.p_demux = NULL;
1271 p_input->p->input.p_stream = NULL;
1272 p_input->p->input.p_access = NULL;
1273 p_input->p->p_es_out = NULL;
1274 p_input->p->p_sout = NULL;
1276 return VLC_EGENERIC;
1279 /*****************************************************************************
1280 * WaitDie: Wait until we are asked to die.
1281 *****************************************************************************
1282 * This function is called when an error occurred during thread main's loop.
1283 *****************************************************************************/
1284 static void WaitDie( input_thread_t *p_input )
1286 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1288 /* Wait a die order */
1289 vlc_object_lock( p_input );
1290 while( vlc_object_alive( p_input ) )
1291 vlc_object_wait( p_input );
1292 vlc_object_unlock( p_input );
1295 /*****************************************************************************
1296 * End: end the input thread
1297 *****************************************************************************/
1298 static void End( input_thread_t * p_input )
1302 /* We are at the end */
1303 input_ChangeState( p_input, END_S );
1305 /* Clean control variables */
1306 input_ControlVarStop( p_input );
1308 /* Clean up master */
1309 InputSourceClean( &p_input->p->input );
1312 for( i = 0; i < p_input->p->i_slave; i++ )
1314 InputSourceClean( p_input->p->slave[i] );
1315 free( p_input->p->slave[i] );
1317 free( p_input->p->slave );
1319 /* Unload all modules */
1320 if( p_input->p->p_es_out )
1321 input_EsOutDelete( p_input->p->p_es_out );
1323 if( !p_input->b_preparsing )
1325 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1326 if( libvlc_stats( p_input ) )
1328 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
1330 /* make sure we are up to date */
1331 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1332 if( p_private->p_stats_computer == p_input )
1334 stats_ComputeGlobalStats( p_input->p_libvlc,
1335 p_input->p_libvlc->p_stats );
1336 p_private->p_stats_computer = NULL;
1338 CL_CO( read_bytes );
1339 CL_CO( read_packets );
1340 CL_CO( demux_read );
1341 CL_CO( input_bitrate );
1342 CL_CO( demux_bitrate );
1343 CL_CO( played_abuffers );
1344 CL_CO( lost_abuffers );
1345 CL_CO( displayed_pictures );
1346 CL_CO( lost_pictures );
1347 CL_CO( decoded_audio) ;
1348 CL_CO( decoded_video );
1349 CL_CO( decoded_sub) ;
1352 /* Close optional stream output instance */
1353 if( p_input->p->p_sout )
1355 CL_CO( sout_sent_packets );
1356 CL_CO( sout_sent_bytes );
1357 CL_CO( sout_send_bitrate );
1359 vlc_object_detach( p_input->p->p_sout );
1364 if( p_input->p->i_attachment > 0 )
1366 for( i = 0; i < p_input->p->i_attachment; i++ )
1367 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1368 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1371 /* Tell we're dead */
1372 p_input->b_dead = true;
1375 /*****************************************************************************
1377 *****************************************************************************/
1378 static inline int ControlPopNoLock( input_thread_t *p_input,
1379 int *pi_type, vlc_value_t *p_val,
1380 mtime_t i_deadline )
1383 while( p_input->p->i_control <= 0 )
1385 if( i_deadline <= 0 )
1386 return VLC_EGENERIC;
1388 if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1389 return VLC_EGENERIC;
1392 *pi_type = p_input->p->control[0].i_type;
1393 *p_val = p_input->p->control[0].val;
1395 p_input->p->i_control--;
1396 if( p_input->p->i_control > 0 )
1400 for( i = 0; i < p_input->p->i_control; i++ )
1402 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1403 p_input->p->control[i].val = p_input->p->control[i+1].val;
1410 static void ControlReduce( input_thread_t *p_input )
1417 for( i = 1; i < p_input->p->i_control; i++ )
1419 const int i_lt = p_input->p->control[i-1].i_type;
1420 const int i_ct = p_input->p->control[i].i_type;
1422 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1423 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1427 ( i_ct == INPUT_CONTROL_SET_STATE ||
1428 i_ct == INPUT_CONTROL_SET_RATE ||
1429 i_ct == INPUT_CONTROL_SET_POSITION ||
1430 i_ct == INPUT_CONTROL_SET_TIME ||
1431 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1432 i_ct == INPUT_CONTROL_SET_TITLE ||
1433 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1434 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1437 // msg_Dbg( p_input, "merged at %d", i );
1438 /* Remove the i-1 */
1439 for( j = i; j < p_input->p->i_control; j++ )
1440 p_input->p->control[j-1] = p_input->p->control[j];
1441 p_input->p->i_control--;
1445 /* TODO but that's not that important
1446 - merge SET_X with SET_X_CMD
1447 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1448 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1455 static bool Control( input_thread_t *p_input, int i_type,
1458 bool b_force_update = false;
1460 if( !p_input ) return b_force_update;
1464 case INPUT_CONTROL_SET_DIE:
1465 msg_Dbg( p_input, "control: stopping input" );
1467 /* Mark all submodules to die */
1468 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1471 case INPUT_CONTROL_SET_POSITION:
1472 case INPUT_CONTROL_SET_POSITION_OFFSET:
1476 if( p_input->p->b_recording )
1478 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1481 if( i_type == INPUT_CONTROL_SET_POSITION )
1483 f_pos = val.f_float;
1487 /* Should not fail */
1488 demux_Control( p_input->p->input.p_demux,
1489 DEMUX_GET_POSITION, &f_pos );
1490 f_pos += val.f_float;
1492 if( f_pos < 0.0 ) f_pos = 0.0;
1493 if( f_pos > 1.0 ) f_pos = 1.0;
1494 /* Reset the decoders states and clock sync (before calling the demuxer */
1495 input_EsOutChangePosition( p_input->p->p_es_out );
1496 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1499 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1500 "%2.1f%% failed", f_pos * 100 );
1504 if( p_input->p->i_slave > 0 )
1505 SlaveSeek( p_input );
1507 b_force_update = true;
1512 case INPUT_CONTROL_SET_TIME:
1513 case INPUT_CONTROL_SET_TIME_OFFSET:
1518 if( p_input->p->b_recording )
1520 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1524 if( i_type == INPUT_CONTROL_SET_TIME )
1526 i_time = val.i_time;
1530 /* Should not fail */
1531 demux_Control( p_input->p->input.p_demux,
1532 DEMUX_GET_TIME, &i_time );
1533 i_time += val.i_time;
1535 if( i_time < 0 ) i_time = 0;
1537 /* Reset the decoders states and clock sync (before calling the demuxer */
1538 input_EsOutChangePosition( p_input->p->p_es_out );
1540 i_ret = demux_Control( p_input->p->input.p_demux,
1541 DEMUX_SET_TIME, i_time );
1546 /* Emulate it with a SET_POS */
1547 demux_Control( p_input->p->input.p_demux,
1548 DEMUX_GET_LENGTH, &i_length );
1551 double f_pos = (double)i_time / (double)i_length;
1552 i_ret = demux_Control( p_input->p->input.p_demux,
1553 DEMUX_SET_POSITION, f_pos );
1558 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1559 " failed or not possible", i_time );
1563 if( p_input->p->i_slave > 0 )
1564 SlaveSeek( p_input );
1566 b_force_update = true;
1571 case INPUT_CONTROL_SET_STATE:
1572 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1573 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1576 if( p_input->p->input.p_access )
1577 i_ret = access_Control( p_input->p->input.p_access,
1578 ACCESS_SET_PAUSE_STATE, false );
1580 i_ret = demux_Control( p_input->p->input.p_demux,
1581 DEMUX_SET_PAUSE_STATE, false );
1585 /* FIXME What to do ? */
1586 msg_Warn( p_input, "cannot unset pause -> EOF" );
1587 vlc_mutex_unlock( &p_input->p->lock_control );
1588 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1589 vlc_mutex_lock( &p_input->p->lock_control );
1592 b_force_update = true;
1594 /* Switch to play */
1595 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1599 input_EsOutChangeState( p_input->p->p_es_out );
1601 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1602 p_input->p->b_can_pause )
1605 if( p_input->p->input.p_access )
1606 i_ret = access_Control( p_input->p->input.p_access,
1607 ACCESS_SET_PAUSE_STATE, true );
1609 i_ret = demux_Control( p_input->p->input.p_demux,
1610 DEMUX_SET_PAUSE_STATE, true );
1612 b_force_update = true;
1616 msg_Warn( p_input, "cannot set pause state" );
1617 state = p_input->i_state;
1624 /* Switch to new state */
1625 input_ChangeStateWithVarCallback( p_input, state, false );
1629 input_EsOutChangeState( p_input->p->p_es_out );
1631 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1633 b_force_update = true;
1635 /* Correct "state" value */
1636 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1638 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1640 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1644 case INPUT_CONTROL_SET_RATE:
1645 case INPUT_CONTROL_SET_RATE_SLOWER:
1646 case INPUT_CONTROL_SET_RATE_FASTER:
1650 if( i_type == INPUT_CONTROL_SET_RATE )
1656 static const int ppi_factor[][2] = {
1657 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1659 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1668 for( i = 0; ppi_factor[i][0] != 0; i++ )
1670 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1671 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1672 if( i_test_e < i_error )
1678 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1680 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1682 if( ppi_factor[i_idx+1][0] > 0 )
1683 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1685 i_rate = INPUT_RATE_MAX+1;
1689 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1691 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1693 i_rate = INPUT_RATE_MIN-1;
1697 if( i_rate < INPUT_RATE_MIN )
1699 msg_Dbg( p_input, "cannot set rate faster" );
1700 i_rate = INPUT_RATE_MIN;
1702 else if( i_rate > INPUT_RATE_MAX )
1704 msg_Dbg( p_input, "cannot set rate slower" );
1705 i_rate = INPUT_RATE_MAX;
1707 if( i_rate != INPUT_RATE_DEFAULT &&
1708 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1709 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1711 msg_Dbg( p_input, "cannot change rate" );
1712 i_rate = INPUT_RATE_DEFAULT;
1714 if( i_rate != p_input->p->i_rate &&
1715 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1718 if( p_input->p->input.p_access )
1719 i_ret = VLC_EGENERIC;
1721 i_ret = demux_Control( p_input->p->input.p_demux,
1722 DEMUX_SET_RATE, &i_rate );
1725 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1726 i_rate = p_input->p->i_rate;
1731 if( i_rate != p_input->p->i_rate )
1734 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1735 var_SetBool( p_input, "rate-change", true );
1737 p_input->p->i_rate = i_rate;
1739 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1740 if( p_input->p->input.b_rescale_ts )
1741 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1743 b_force_update = true;
1748 case INPUT_CONTROL_SET_PROGRAM:
1749 /* No need to force update, es_out does it if needed */
1750 es_out_Control( p_input->p->p_es_out,
1751 ES_OUT_SET_GROUP, val.i_int );
1753 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1757 case INPUT_CONTROL_SET_ES:
1758 /* No need to force update, es_out does it if needed */
1759 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1760 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1763 case INPUT_CONTROL_RESTART_ES:
1764 es_out_Control( p_input->p->p_es_out, ES_OUT_RESTART_ES,
1765 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1768 case INPUT_CONTROL_SET_AUDIO_DELAY:
1769 input_EsOutSetDelay( p_input->p->p_es_out,
1770 AUDIO_ES, val.i_time );
1771 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1774 case INPUT_CONTROL_SET_SPU_DELAY:
1775 input_EsOutSetDelay( p_input->p->p_es_out,
1776 SPU_ES, val.i_time );
1777 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1780 case INPUT_CONTROL_SET_TITLE:
1781 case INPUT_CONTROL_SET_TITLE_NEXT:
1782 case INPUT_CONTROL_SET_TITLE_PREV:
1783 if( p_input->p->b_recording )
1785 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1788 if( p_input->p->input.b_title_demux &&
1789 p_input->p->input.i_title > 0 )
1792 /* FIXME handle demux title */
1793 demux_t *p_demux = p_input->p->input.p_demux;
1796 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1797 i_title = p_demux->info.i_title - 1;
1798 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1799 i_title = p_demux->info.i_title + 1;
1801 i_title = val.i_int;
1803 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1805 input_EsOutChangePosition( p_input->p->p_es_out );
1807 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1808 input_ControlVarTitle( p_input, i_title );
1811 else if( p_input->p->input.i_title > 0 )
1813 access_t *p_access = p_input->p->input.p_access;
1816 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1817 i_title = p_access->info.i_title - 1;
1818 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1819 i_title = p_access->info.i_title + 1;
1821 i_title = val.i_int;
1823 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1825 input_EsOutChangePosition( p_input->p->p_es_out );
1827 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1828 stream_AccessReset( p_input->p->input.p_stream );
1832 case INPUT_CONTROL_SET_SEEKPOINT:
1833 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1834 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1835 if( p_input->p->b_recording )
1837 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1841 if( p_input->p->input.b_title_demux &&
1842 p_input->p->input.i_title > 0 )
1844 demux_t *p_demux = p_input->p->input.p_demux;
1846 int64_t i_input_time;
1847 int64_t i_seekpoint_time;
1849 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1851 i_seekpoint = p_demux->info.i_seekpoint;
1852 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1853 if( i_seekpoint_time >= 0 &&
1854 !demux_Control( p_demux,
1855 DEMUX_GET_TIME, &i_input_time ) )
1857 if( i_input_time < i_seekpoint_time + 3000000 )
1863 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1864 i_seekpoint = p_demux->info.i_seekpoint + 1;
1866 i_seekpoint = val.i_int;
1868 if( i_seekpoint >= 0 && i_seekpoint <
1869 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1872 input_EsOutChangePosition( p_input->p->p_es_out );
1874 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1877 else if( p_input->p->input.i_title > 0 )
1879 demux_t *p_demux = p_input->p->input.p_demux;
1880 access_t *p_access = p_input->p->input.p_access;
1882 int64_t i_input_time;
1883 int64_t i_seekpoint_time;
1885 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1887 i_seekpoint = p_access->info.i_seekpoint;
1888 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1889 if( i_seekpoint_time >= 0 &&
1890 demux_Control( p_demux,
1891 DEMUX_GET_TIME, &i_input_time ) )
1893 if( i_input_time < i_seekpoint_time + 3000000 )
1899 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1900 i_seekpoint = p_access->info.i_seekpoint + 1;
1902 i_seekpoint = val.i_int;
1904 if( i_seekpoint >= 0 && i_seekpoint <
1905 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1907 input_EsOutChangePosition( p_input->p->p_es_out );
1909 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1911 stream_AccessReset( p_input->p->input.p_stream );
1916 case INPUT_CONTROL_ADD_SUBTITLE:
1917 if( val.psz_string )
1919 SubtitleAdd( p_input, val.psz_string, true );
1920 free( val.psz_string );
1924 case INPUT_CONTROL_ADD_SLAVE:
1925 if( val.psz_string )
1927 input_source_t *slave = InputSourceNew( p_input );
1929 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1935 msg_Dbg( p_input, "adding %s as slave on the fly",
1939 if( demux_Control( p_input->p->input.p_demux,
1940 DEMUX_GET_TIME, &i_time ) )
1942 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1943 InputSourceClean( slave );
1947 if( demux_Control( slave->p_demux,
1948 DEMUX_SET_TIME, i_time ) )
1950 msg_Err( p_input, "seek failed for new slave" );
1951 InputSourceClean( slave );
1956 /* Get meta (access and demux) */
1957 p_meta = vlc_meta_New();
1958 access_Control( slave->p_access, ACCESS_GET_META,
1960 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1961 InputUpdateMeta( p_input, p_meta );
1963 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1968 msg_Warn( p_input, "failed to add %s as slave",
1972 free( val.psz_string );
1976 case INPUT_CONTROL_SET_RECORD_STATE:
1977 if( !!p_input->p->b_recording != !!val.b_bool )
1979 if( p_input->p->input.b_can_stream_record )
1981 if( demux_Control( p_input->p->input.p_demux,
1982 DEMUX_SET_RECORD_STATE, val.b_bool ) )
1987 if( input_EsOutSetRecord( p_input->p->p_es_out, val.b_bool ) )
1990 p_input->p->b_recording = val.b_bool;
1992 var_Change( p_input, "record", VLC_VAR_SETVALUE, &val, NULL );
1994 b_force_update = true;
1998 case INPUT_CONTROL_SET_BOOKMARK:
2000 msg_Err( p_input, "not yet implemented" );
2004 return b_force_update;
2007 /*****************************************************************************
2009 *****************************************************************************/
2010 static int UpdateFromDemux( input_thread_t *p_input )
2012 demux_t *p_demux = p_input->p->input.p_demux;
2015 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2017 v.i_int = p_demux->info.i_title;
2018 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2020 input_ControlVarTitle( p_input, p_demux->info.i_title );
2022 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2024 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2026 v.i_int = p_demux->info.i_seekpoint;
2027 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2029 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2031 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2033 /* Hmmm only works with master input */
2034 if( p_input->p->input.p_demux == p_demux )
2036 int i_title_end = p_input->p->input.i_title_end -
2037 p_input->p->input.i_title_offset;
2038 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2039 p_input->p->input.i_seekpoint_offset;
2041 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2043 if( p_demux->info.i_title > i_title_end ||
2044 ( p_demux->info.i_title == i_title_end &&
2045 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2047 else if( i_seekpoint_end >=0 )
2049 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2051 else if( i_title_end >= 0 )
2053 if( p_demux->info.i_title > i_title_end ) return 0;
2060 /*****************************************************************************
2062 *****************************************************************************/
2063 static int UpdateFromAccess( input_thread_t *p_input )
2065 access_t *p_access = p_input->p->input.p_access;
2068 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2070 v.i_int = p_access->info.i_title;
2071 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2073 input_ControlVarTitle( p_input, p_access->info.i_title );
2075 stream_AccessUpdate( p_input->p->input.p_stream );
2077 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2079 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2081 v.i_int = p_access->info.i_seekpoint;
2082 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2083 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2085 if( p_access->info.i_update & INPUT_UPDATE_META )
2087 /* TODO maybe multi - access ? */
2088 vlc_meta_t *p_meta = vlc_meta_New();
2089 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2090 InputUpdateMeta( p_input, p_meta );
2091 p_access->info.i_update &= ~INPUT_UPDATE_META;
2094 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2096 /* Hmmm only works with master input */
2097 if( p_input->p->input.p_access == p_access )
2099 int i_title_end = p_input->p->input.i_title_end -
2100 p_input->p->input.i_title_offset;
2101 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2102 p_input->p->input.i_seekpoint_offset;
2104 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2106 if( p_access->info.i_title > i_title_end ||
2107 ( p_access->info.i_title == i_title_end &&
2108 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2110 else if( i_seekpoint_end >=0 )
2112 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2114 else if( i_title_end >= 0 )
2116 if( p_access->info.i_title > i_title_end ) return 0;
2123 /*****************************************************************************
2125 *****************************************************************************/
2126 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2128 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2131 /*****************************************************************************
2133 *****************************************************************************/
2134 static input_source_t *InputSourceNew( input_thread_t *p_input )
2136 VLC_UNUSED(p_input);
2137 input_source_t *in = malloc( sizeof( input_source_t ) );
2139 memset( in, 0, sizeof( input_source_t ) );
2143 /*****************************************************************************
2145 *****************************************************************************/
2146 static int InputSourceInit( input_thread_t *p_input,
2147 input_source_t *in, const char *psz_mrl,
2148 const char *psz_forced_demux )
2150 const bool b_master = in == &p_input->p->input;
2152 char psz_dup[strlen(psz_mrl) + 1];
2153 const char *psz_access;
2154 const char *psz_demux;
2161 strcpy( psz_dup, psz_mrl );
2163 if( !in ) return VLC_EGENERIC;
2164 if( !p_input ) return VLC_EGENERIC;
2167 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2169 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2170 psz_mrl, psz_access, psz_demux, psz_path );
2171 if( !p_input->b_preparsing )
2173 /* Hack to allow udp://@:port syntax */
2175 (strncmp( psz_access, "udp", 3 ) &&
2176 strncmp( psz_access, "rtp", 3 )) )
2178 /* Find optional titles and seekpoints */
2179 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2180 &in->i_seekpoint_start, &in->i_seekpoint_end );
2183 if( psz_forced_demux && *psz_forced_demux )
2185 psz_demux = psz_forced_demux;
2187 else if( *psz_demux == '\0' )
2189 /* special hack for forcing a demuxer with --demux=module
2190 * (and do nothing with a list) */
2191 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2193 if( psz_var_demux != NULL &&
2194 !strchr(psz_var_demux, ',' ) &&
2195 !strchr(psz_var_demux, ':' ) )
2197 psz_demux = psz_var_demux;
2199 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2203 /* Try access_demux if no demux given */
2204 if( *psz_demux == '\0' )
2206 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2207 NULL, p_input->p->p_es_out, false );
2212 /* Preparsing is only for file:// */
2215 if( !*psz_access ) /* path without scheme:// */
2216 psz_access = "file";
2217 if( strcmp( psz_access, "file" ) )
2219 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2224 int64_t i_pts_delay;
2226 /* Get infos from access_demux */
2227 demux_Control( in->p_demux,
2228 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2229 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2231 in->b_title_demux = true;
2232 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2233 &in->title, &in->i_title,
2234 &in->i_title_offset, &in->i_seekpoint_offset ) )
2236 TAB_INIT( in->i_title, in->title );
2238 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2239 &in->b_can_pace_control ) )
2240 in->b_can_pace_control = false;
2242 if( !in->b_can_pace_control )
2244 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2245 &in->b_can_rate_control, &in->b_rescale_ts ) )
2247 in->b_can_rate_control = false;
2248 in->b_rescale_ts = true; /* not used */
2253 in->b_can_rate_control = true;
2254 in->b_rescale_ts = true;
2256 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2257 &in->b_can_pause ) )
2258 in->b_can_pause = false;
2259 var_SetBool( p_input, "can-pause", in->b_can_pause );
2261 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2263 if( ret != VLC_SUCCESS )
2265 var_Set( p_input, "seekable", val );
2269 int64_t i_pts_delay;
2272 input_ChangeState( p_input, OPENING_S );
2274 /* Now try a real access */
2275 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2277 /* Access failed, URL encoded ? */
2278 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2280 decode_URI( psz_path );
2282 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2283 psz_access, psz_demux, psz_path );
2285 in->p_access = access_New( p_input,
2286 psz_access, psz_demux, psz_path );
2288 if( in->p_access == NULL )
2290 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2292 intf_UserFatal( VLC_OBJECT( p_input), false,
2293 _("Your input can't be opened"),
2294 _("VLC is unable to open the MRL '%s'."
2295 " Check the log for details."), psz_mrl );
2300 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2301 while( psz && *psz )
2303 access_t *p_access = in->p_access;
2304 char *end = strchr( psz, ':' );
2309 in->p_access = access_FilterNew( in->p_access, psz );
2310 if( in->p_access == NULL )
2312 in->p_access = p_access;
2313 msg_Warn( p_input, "failed to insert access filter %s",
2321 /* Get infos from access */
2322 if( !p_input->b_preparsing )
2324 access_Control( in->p_access,
2325 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2326 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2328 in->b_title_demux = false;
2329 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2330 &in->title, &in->i_title,
2331 &in->i_title_offset, &in->i_seekpoint_offset ) )
2334 TAB_INIT( in->i_title, in->title );
2336 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2337 &in->b_can_pace_control );
2338 in->b_can_rate_control = in->b_can_pace_control;
2339 in->b_rescale_ts = true;
2341 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2343 var_SetBool( p_input, "can-pause", in->b_can_pause );
2344 access_Control( in->p_access, ACCESS_CAN_SEEK,
2346 var_Set( p_input, "seekable", val );
2350 input_ChangeState( p_input, BUFFERING_S );
2352 /* Autodetect extra files if none specified */
2353 char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2354 if( !psz_input_list )
2356 char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2357 if( psz_extra_files )
2358 var_SetString( p_input, "input-list", psz_extra_files );
2359 free( psz_extra_files );
2362 /* Create the stream_t */
2363 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2365 /* Restor old value */
2366 if( !psz_input_list )
2367 var_SetString( p_input, "input-list", "" );
2368 free( psz_input_list );
2370 if( in->p_stream == NULL )
2372 msg_Warn( p_input, "cannot create a stream_t from access" );
2376 /* Open a demuxer */
2377 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2379 psz_demux = in->p_access->psz_demux;
2383 /* Take access redirections into account */
2384 char *psz_real_path;
2385 char *psz_buf = NULL;
2386 if( in->p_access->psz_path )
2388 const char *psz_a, *psz_d;
2389 psz_buf = strdup( in->p_access->psz_path );
2390 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2394 psz_real_path = psz_path;
2396 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2398 in->p_stream, p_input->p->p_es_out,
2399 p_input->b_preparsing );
2403 if( in->p_demux == NULL )
2405 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2406 psz_access, psz_demux, psz_path );
2407 intf_UserFatal( VLC_OBJECT( p_input ), false,
2408 _("VLC can't recognize the input's format"),
2409 _("The format of '%s' cannot be detected. "
2410 "Have a look at the log for details."), psz_mrl );
2414 /* Get title from demux */
2415 if( !p_input->b_preparsing && in->i_title <= 0 )
2417 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2418 &in->title, &in->i_title,
2419 &in->i_title_offset, &in->i_seekpoint_offset ))
2421 TAB_INIT( in->i_title, in->title );
2425 in->b_title_demux = true;
2430 /* Set record capabilities */
2431 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2432 in->b_can_stream_record = false;
2434 if( !var_CreateGetBool( p_input, "input-record-native" ) )
2435 in->b_can_stream_record = false;
2436 var_SetBool( p_input, "can-record", true );
2438 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2442 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2443 if( 1 || !p_input->b_preparsing )
2446 input_attachment_t **attachment;
2447 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2448 &attachment, &i_attachment ) )
2450 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2451 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2452 i_attachment, attachment );
2453 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2456 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2458 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2460 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2463 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2464 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2470 input_ChangeState( p_input, ERROR_S );
2473 demux_Delete( in->p_demux );
2476 stream_Delete( in->p_stream );
2479 access_Delete( in->p_access );
2481 return VLC_EGENERIC;
2484 /*****************************************************************************
2486 *****************************************************************************/
2487 static void InputSourceClean( input_source_t *in )
2492 demux_Delete( in->p_demux );
2495 stream_Delete( in->p_stream );
2498 access_Delete( in->p_access );
2500 if( in->i_title > 0 )
2502 for( i = 0; i < in->i_title; i++ )
2503 vlc_input_title_Delete( in->title[i] );
2504 TAB_CLEAN( in->i_title, in->title );
2508 static void SlaveDemux( input_thread_t *p_input )
2512 bool b_set_time = true;
2514 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2516 /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2520 for( i = 0; i < p_input->p->i_slave; i++ )
2522 input_source_t *in = p_input->p->slave[i];
2528 if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2533 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2535 msg_Err( p_input, "slave[%d] doesn't like "
2536 "DEMUX_GET_TIME -> EOF", i );
2541 if( i_stime >= i_time )
2544 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2550 i_ret = in->p_demux->pf_demux( in->p_demux );
2555 msg_Dbg( p_input, "slave %d EOF", i );
2561 static void SlaveSeek( input_thread_t *p_input )
2566 if( !p_input ) return;
2568 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2570 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2574 for( i = 0; i < p_input->p->i_slave; i++ )
2576 input_source_t *in = p_input->p->slave[i];
2578 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2581 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2591 /*****************************************************************************
2593 *****************************************************************************/
2594 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2598 if( !p_meta ) return;
2600 /* Get meta information from user */
2601 #define GET_META( field, s ) \
2602 var_Get( p_input, (s), &val ); \
2603 if( *val.psz_string ) \
2604 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2605 free( val.psz_string )
2607 GET_META( Title, "meta-title" );
2608 GET_META( Artist, "meta-artist" );
2609 GET_META( Genre, "meta-genre" );
2610 GET_META( Copyright, "meta-copyright" );
2611 GET_META( Description, "meta-description" );
2612 GET_META( Date, "meta-date" );
2613 GET_META( URL, "meta-url" );
2617 /*****************************************************************************
2618 * InputGetExtraFiles
2619 * Autodetect extra input list
2620 *****************************************************************************/
2621 static char *InputGetExtraFiles( input_thread_t *p_input,
2622 const char *psz_access, const char *psz_path )
2624 char *psz_list = NULL;
2626 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2630 const char *psz_ext = strrchr( psz_path, '.' );
2631 if( !psz_ext || strcmp( psz_ext, ".001" ) )
2634 char *psz_file = strdup( psz_path );
2638 /* Try to list .xyz files */
2639 for( int i = 2; i < 999; i++ )
2641 char *psz_ext = strrchr( psz_file, '.' );
2644 snprintf( psz_ext, 5, ".%.3d", i );
2646 if( utf8_stat( psz_file, &st )
2647 || !S_ISREG( st.st_mode ) || !st.st_size )
2650 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2654 char *psz_old = psz_list;
2655 /* FIXME how to handle file with ',' ?*/
2656 if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2664 psz_list = strdup( psz_file );
2672 /*****************************************************************************
2673 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2674 * arturl and locking issue.
2675 *****************************************************************************/
2676 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2678 input_item_t *p_item = p_input->p->input.p_item;
2679 char * psz_arturl = NULL;
2680 char *psz_title = NULL;
2681 int i_arturl_event = false;
2686 psz_arturl = input_item_GetArtURL( p_item );
2688 vlc_mutex_lock( &p_item->lock );
2689 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2690 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2692 vlc_meta_Merge( p_item->p_meta, p_meta );
2694 if( psz_arturl && *psz_arturl )
2696 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2697 i_arturl_event = true;
2700 vlc_meta_Delete( p_meta );
2702 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2704 /* Don't look for art cover if sout
2705 * XXX It can change when sout has meta data support */
2706 if( p_input->p->p_sout && !p_input->b_preparsing )
2708 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2709 i_arturl_event = true;
2713 input_ExtractAttachmentAndCacheArt( p_input );
2719 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2721 p_meta = vlc_meta_New();
2722 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2724 vlc_mutex_unlock( &p_item->lock );
2726 input_item_SetPreparsed( p_item, true );
2728 if( i_arturl_event == true )
2732 /* Notify interested third parties */
2733 event.type = vlc_InputItemMetaChanged;
2734 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2735 vlc_event_send( &p_item->event_manager, &event );
2740 input_Control( p_input, INPUT_SET_NAME, psz_title );
2744 /** \todo handle sout meta */
2748 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2749 int i_new, input_attachment_t **pp_new )
2751 int i_attachment = *pi_attachment;
2752 input_attachment_t **attachment = *ppp_attachment;
2755 attachment = realloc( attachment,
2756 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2757 for( i = 0; i < i_new; i++ )
2758 attachment[i_attachment++] = pp_new[i];
2762 *pi_attachment = i_attachment;
2763 *ppp_attachment = attachment;
2766 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2770 if( p_input->b_preparsing )
2773 if( p_input->p->input.p_access )
2774 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2777 /* Get meta data from slave input */
2778 for( i = 0; i < p_input->p->i_slave; i++ )
2780 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2781 if( p_input->p->slave[i]->p_access )
2783 access_Control( p_input->p->slave[i]->p_access,
2784 ACCESS_GET_META, p_meta );
2789 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2796 /* XXX I am not sure it is a great idea, besides, there is more than that
2797 * if we want to do it right */
2798 vlc_mutex_lock( &p_item->lock );
2799 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2801 vlc_mutex_unlock( &p_item->lock );
2804 vlc_mutex_unlock( &p_item->lock );
2807 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2808 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2813 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2814 if(! p_demux->p_private )
2817 p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2820 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2822 if( p_demux_meta->p_meta )
2824 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2825 vlc_meta_Delete( p_demux_meta->p_meta );
2828 if( p_demux_meta->i_attachments > 0 )
2830 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2831 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2832 p_demux_meta->i_attachments, p_demux_meta->attachments );
2833 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2835 module_unneed( p_demux, p_id3 );
2837 free( p_demux->p_private );
2841 /*****************************************************************************
2842 * MRLSplit: parse the access, demux and url part of the
2843 * Media Resource Locator.
2844 *****************************************************************************/
2845 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2848 char *psz_access = NULL;
2849 char *psz_demux = NULL;
2852 /* Either there is an access/demux specification before ://
2853 * or we have a plain local file path. */
2854 psz_path = strstr( psz_dup, "://" );
2855 if( psz_path != NULL )
2858 psz_path += 3; /* skips "://" */
2860 /* Separate access from demux (<access>/<demux>://<path>) */
2861 psz_access = psz_dup;
2862 psz_demux = strchr( psz_access, '/' );
2864 *psz_demux++ = '\0';
2866 /* We really don't want module name substitution here! */
2867 if( psz_access[0] == '$' )
2869 if( psz_demux && psz_demux[0] == '$' )
2876 *ppsz_access = psz_access ? psz_access : (char*)"";
2877 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2878 *ppsz_path = psz_path;
2881 static inline bool next(char ** src)
2885 long result = strtol( *src, &end, 0 );
2886 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
2895 /*****************************************************************************
2896 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2899 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2900 *****************************************************************************/
2901 static void MRLSections( input_thread_t *p_input, char *psz_source,
2902 int *pi_title_start, int *pi_title_end,
2903 int *pi_chapter_start, int *pi_chapter_end )
2905 char *psz, *psz_end, *psz_next, *psz_check;
2907 *pi_title_start = *pi_title_end = -1;
2908 *pi_chapter_start = *pi_chapter_end = -1;
2910 /* Start by parsing titles and chapters */
2911 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2914 /* Check we are really dealing with a title/chapter section */
2915 psz_check = psz + 1;
2916 if( !*psz_check ) return;
2917 if( isdigit(*psz_check) )
2918 if(!next(&psz_check)) return;
2919 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2920 if( *psz_check == ':' && ++psz_check )
2922 if( isdigit(*psz_check) )
2923 if(!next(&psz_check)) return;
2925 if( *psz_check != '-' && *psz_check ) return;
2926 if( *psz_check == '-' && ++psz_check )
2928 if( isdigit(*psz_check) )
2929 if(!next(&psz_check)) return;
2931 if( *psz_check != ':' && *psz_check ) return;
2932 if( *psz_check == ':' && ++psz_check )
2934 if( isdigit(*psz_check) )
2935 if(!next(&psz_check)) return;
2937 if( *psz_check ) return;
2939 /* Separate start and end */
2941 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2943 /* Look for the start title */
2944 *pi_title_start = strtol( psz, &psz_next, 0 );
2945 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2946 *pi_title_end = *pi_title_start;
2949 /* Look for the start chapter */
2951 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2952 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2953 *pi_chapter_end = *pi_chapter_start;
2957 /* Look for the end title */
2958 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2959 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2962 /* Look for the end chapter */
2963 if( *psz_end ) psz_end++;
2964 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2965 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2968 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2969 psz_source, *pi_title_start, *pi_chapter_start,
2970 *pi_title_end, *pi_chapter_end );
2973 /*****************************************************************************
2974 * input_AddSubtitles: add a subtitles file and enable it
2975 *****************************************************************************/
2976 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
2978 input_source_t *sub;
2981 char *psz_path, *psz_extension;
2983 /* if we are provided a subtitle.sub file,
2984 * see if we don't have a subtitle.idx and use it instead */
2985 psz_path = strdup( psz_subtitle );
2988 psz_extension = strrchr( psz_path, '.');
2989 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2993 strcpy( psz_extension, ".idx" );
2995 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
2997 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2998 psz_path, psz_subtitle );
2999 strcpy( psz_subtitle, psz_path );
3005 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3007 sub = InputSourceNew( p_input );
3008 if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3013 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3016 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3018 if( count.i_int == 0 )
3020 /* if it was first one, there is disable too */
3022 if( count.i_int < list.p_list->i_count )
3024 int i_id = list.p_list->p_values[count.i_int].i_int;
3025 es_out_id_t *p_es = input_EsOutGetFromID( p_input->p->p_es_out, i_id );
3027 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_DEFAULT, p_es );
3028 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES, p_es );
3030 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3034 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3035 bool b_check_extension )
3039 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3042 assert( psz_subtitle != NULL );
3044 val.psz_string = strdup( psz_subtitle );
3045 if( val.psz_string )
3046 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3050 /*****************************************************************************
3051 * input_get_event_manager
3052 *****************************************************************************/
3053 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3055 return &p_input->p->event_manager;
3059 /* TODO FIXME nearly the same logic that snapshot code */
3060 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3065 path = utf8_opendir( psz_path );
3070 char *psz_tmp = str_format( p_obj, psz_prefix );
3074 filename_sanitize( psz_tmp );
3075 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3077 psz_extension ? "." : "",
3078 psz_extension ? psz_extension : "" ) < 0 )
3085 psz_file = str_format( p_obj, psz_path );
3086 path_sanitize( psz_file );