1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
38 #include "input_internal.h"
45 #include "../stream_output/stream_output.h"
47 #include <vlc_interface.h>
49 #include <vlc_charset.h>
50 #include <vlc_strings.h>
52 #ifdef HAVE_SYS_STAT_H
53 # include <sys/stat.h>
56 /*****************************************************************************
58 *****************************************************************************/
59 static void Destructor( input_thread_t * p_input );
61 static void* Run ( vlc_object_t *p_this );
62 static void* RunAndDestroy ( vlc_object_t *p_this );
64 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
65 const char *, bool, sout_instance_t * );
66 static int Init ( input_thread_t *p_input );
67 static void WaitDie ( input_thread_t *p_input );
68 static void End ( input_thread_t *p_input );
69 static void MainLoop( input_thread_t *p_input );
71 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t *, mtime_t i_deadline );
72 static void ControlReduce( input_thread_t * );
73 static bool Control( input_thread_t *, int, vlc_value_t );
75 static int UpdateFromAccess( input_thread_t * );
76 static int UpdateFromDemux( input_thread_t * );
78 static void UpdateItemLength( input_thread_t *, int64_t i_length );
80 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
82 static input_source_t *InputSourceNew( input_thread_t *);
83 static int InputSourceInit( input_thread_t *, input_source_t *,
84 const char *, const char *psz_forced_demux );
85 static void InputSourceClean( input_source_t * );
87 //static void InputGetAttachments( input_thread_t *, input_source_t * );
88 static void SlaveDemux( input_thread_t *p_input );
89 static void SlaveSeek( input_thread_t *p_input );
91 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
92 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
93 static char *InputGetExtraFiles( input_thread_t *p_input,
94 const char *psz_access, const char *psz_path );
96 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
97 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
98 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
99 int i_new, input_attachment_t **pp_new );
101 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced );
103 /*****************************************************************************
104 * This function creates a new input, and returns a pointer
105 * to its description. On error, it returns NULL.
107 * Variables for _public_ use:
110 * - rate,rate-slower, rate-faster
111 * - position, position-offset
112 * - time, time-offset
113 * - title,title-next,title-prev
114 * - chapter,chapter-next, chapter-prev
115 * - program, audio-es, video-es, spu-es
116 * - audio-delay, spu-delay
121 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
122 * or not, for that check position != 0.0)
124 * - can-record (if a stream can be recorded while playing)
125 * - teletext-es to get the index of spu track that is teletext --1 if no teletext)
126 * * For intf callback upon changes:
128 * - intf-change-vout for when a vout is created or destroyed
129 * - rate-change for when playback rate changes
130 * - stats-change for when statistics are updated
131 * TODO explain when Callback is called
132 * TODO complete this list (?)
133 *****************************************************************************/
134 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
135 const char *psz_header, bool b_quick,
136 sout_instance_t *p_sout )
138 static const char input_name[] = "input";
139 input_thread_t *p_input = NULL; /* thread descriptor */
143 /* Allocate descriptor */
144 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
145 VLC_OBJECT_INPUT, input_name );
146 if( p_input == NULL )
149 /* Construct a nice name for the input timer */
150 char psz_timer_name[255];
151 char * psz_name = input_item_GetName( p_item );
152 snprintf( psz_timer_name, sizeof(psz_timer_name),
153 "input launching for '%s'", psz_name );
155 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
159 /* Start a timer to mesure how long it takes
160 * to launch an input */
161 stats_TimerStart( p_input, psz_timer_name,
162 STATS_TIMER_INPUT_LAUNCHING );
164 MALLOC_NULL( p_input->p, input_thread_private_t );
165 memset( p_input->p, 0, sizeof( input_thread_private_t ) );
167 /* One "randomly" selected input thread is responsible for computing
168 * the global stats. Check if there is already someone doing this */
169 if( p_input->p_libvlc->p_stats && !b_quick )
171 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
172 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
173 if( p_private->p_stats_computer == NULL )
174 p_private->p_stats_computer = p_input;
175 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
178 p_input->b_preparsing = b_quick;
179 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
182 vlc_event_manager_t * p_em = &p_input->p->event_manager;
183 vlc_event_manager_init_with_vlc_object( p_em, p_input );
184 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
185 vlc_event_manager_register_event_type( p_em, vlc_InputSelectedStreamChanged );
187 /* Init Common fields */
188 p_input->b_eof = false;
189 p_input->b_can_pace_control = true;
190 p_input->p->i_start = 0;
192 p_input->p->i_stop = 0;
193 p_input->p->i_run = 0;
194 p_input->p->i_title = 0;
195 p_input->p->title = NULL;
196 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
197 p_input->i_state = INIT_S;
198 p_input->p->i_rate = INPUT_RATE_DEFAULT;
199 p_input->p->b_recording = false;
200 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
201 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
202 p_input->p->p_es_out = NULL;
203 p_input->p->p_sout = NULL;
204 p_input->p->b_out_pace_control = false;
205 p_input->i_pts_delay = 0;
207 /* Init Input fields */
208 vlc_gc_incref( p_item ); /* Released in Destructor() */
209 p_input->p->input.p_item = p_item;
210 p_input->p->input.p_access = NULL;
211 p_input->p->input.p_stream = NULL;
212 p_input->p->input.p_demux = NULL;
213 p_input->p->input.b_title_demux = false;
214 p_input->p->input.i_title = 0;
215 p_input->p->input.title = NULL;
216 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
217 p_input->p->input.b_can_pace_control = true;
218 p_input->p->input.b_can_rate_control = true;
219 p_input->p->input.b_rescale_ts = true;
220 p_input->p->input.b_eof = false;
221 p_input->p->input.i_cr_average = 0;
223 vlc_mutex_lock( &p_item->lock );
225 if( !p_item->p_stats )
226 p_item->p_stats = stats_NewInputStats( p_input );
227 vlc_mutex_unlock( &p_item->lock );
230 p_input->p->i_slave = 0;
231 p_input->p->slave = NULL;
233 /* Init control buffer */
234 vlc_mutex_init( &p_input->p->lock_control );
235 vlc_cond_init( &p_input->p->wait_control );
236 p_input->p->i_control = 0;
238 /* Parse input options */
239 vlc_mutex_lock( &p_item->lock );
240 assert( (int)p_item->optflagc == p_item->i_options );
241 for( i = 0; i < p_item->i_options; i++ )
242 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
243 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
244 vlc_mutex_unlock( &p_item->lock );
246 /* Create Object Variables for private use only */
247 input_ConfigVarInit( p_input );
249 /* Create Objects variables for public Get and Set */
250 input_ControlVarInit( p_input );
253 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
255 if( !p_input->b_preparsing )
257 var_Get( p_input, "bookmarks", &val );
260 /* FIXME: have a common cfg parsing routine used by sout and others */
261 char *psz_parser, *psz_start, *psz_end;
262 psz_parser = val.psz_string;
263 while( (psz_start = strchr( psz_parser, '{' ) ) )
265 seekpoint_t *p_seekpoint;
268 psz_end = strchr( psz_start, '}' );
269 if( !psz_end ) break;
270 psz_parser = psz_end + 1;
271 backup = *psz_parser;
275 p_seekpoint = vlc_seekpoint_New();
276 while( (psz_end = strchr( psz_start, ',' ) ) )
279 if( !strncmp( psz_start, "name=", 5 ) )
281 p_seekpoint->psz_name = strdup(psz_start + 5);
283 else if( !strncmp( psz_start, "bytes=", 6 ) )
285 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
287 else if( !strncmp( psz_start, "time=", 5 ) )
289 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
292 psz_start = psz_end + 1;
294 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
295 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
296 p_seekpoint->i_time_offset );
297 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
298 vlc_seekpoint_Delete( p_seekpoint );
299 *psz_parser = backup;
301 free( val.psz_string );
305 /* Remove 'Now playing' info as it is probably outdated */
306 input_item_SetNowPlaying( p_item, NULL );
309 if( p_input->b_preparsing )
310 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
314 p_input->p->p_sout = p_sout;
316 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
317 vlc_mutex_init( &p_input->p->counters.counters_lock );
319 /* Set the destructor when we are sure we are initialized */
320 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
322 /* Attach only once we are ready */
323 vlc_object_attach( p_input, p_parent );
329 * Input destructor (called when the object's refcount reaches 0).
331 static void Destructor( input_thread_t * p_input )
334 char * psz_name = input_item_GetName( p_input->p->input.p_item );
335 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
339 vlc_event_manager_fini( &p_input->p->event_manager );
341 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
342 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
344 if( p_input->p->p_sout )
345 sout_DeleteInstance( p_input->p->p_sout );
347 vlc_gc_decref( p_input->p->input.p_item );
349 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
351 vlc_cond_destroy( &p_input->p->wait_control );
352 vlc_mutex_destroy( &p_input->p->lock_control );
357 * Initialize an input thread and run it. You will need to monitor the
358 * thread to clean up after it is done
360 * \param p_parent a vlc_object
361 * \param p_item an input item
362 * \return a pointer to the spawned input thread
364 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
365 input_item_t *p_item )
367 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
371 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
372 input_item_t *p_item,
373 const char *psz_log, sout_instance_t *p_sout )
375 input_thread_t *p_input;
377 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
381 /* Create thread and wait for its readiness. */
382 if( vlc_thread_create( p_input, "input", Run,
383 VLC_THREAD_PRIORITY_INPUT, false ) )
385 input_ChangeState( p_input, ERROR_S );
386 msg_Err( p_input, "cannot create input thread" );
387 vlc_object_detach( p_input );
388 vlc_object_release( p_input );
396 * Initialize an input thread and run it. This thread will clean after itself,
397 * you can forget about it. It can work either in blocking or non-blocking mode
399 * \param p_parent a vlc_object
400 * \param p_item an input item
401 * \param b_block should we block until read is finished ?
402 * \return an error code, VLC_SUCCESS on success
404 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
407 input_thread_t *p_input;
409 p_input = Create( p_parent, p_item, NULL, false, NULL );
415 RunAndDestroy( VLC_OBJECT(p_input) );
420 if( vlc_thread_create( p_input, "input", RunAndDestroy,
421 VLC_THREAD_PRIORITY_INPUT, false ) )
423 input_ChangeState( p_input, ERROR_S );
424 msg_Err( p_input, "cannot create input thread" );
425 vlc_object_release( p_input );
433 * Initialize an input and initialize it to preparse the item
434 * This function is blocking. It will only accept to parse files
436 * \param p_parent a vlc_object_t
437 * \param p_item an input item
438 * \return VLC_SUCCESS or an error
440 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
442 input_thread_t *p_input;
444 /* Allocate descriptor */
445 p_input = Create( p_parent, p_item, NULL, true, NULL );
449 if( !Init( p_input ) )
452 vlc_object_detach( p_input );
453 vlc_object_release( p_input );
459 * Request a running input thread to stop and die
461 * \param the input thread to stop
463 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
468 if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
469 p_obj->i_object_type == VLC_OBJECT_AOUT ||
470 p_obj == VLC_OBJECT(p_input->p->p_sout) )
473 vlc_object_kill( p_obj );
475 p_list = vlc_list_children( p_obj );
476 for( i = 0; i < p_list->i_count; i++ )
477 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
478 vlc_list_release( p_list );
480 void input_StopThread( input_thread_t *p_input )
482 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
483 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
484 * unlock the control loop */
485 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
487 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
490 sout_instance_t * input_DetachSout( input_thread_t *p_input )
492 sout_instance_t *p_sout = p_input->p->p_sout;
493 vlc_object_detach( p_sout );
494 p_input->p->p_sout = NULL;
498 /*****************************************************************************
499 * Run: main thread loop
500 * This is the "normal" thread that spawns the input processing chain,
501 * reads the stream, cleans up and waits
502 *****************************************************************************/
503 static void* Run( vlc_object_t *p_this )
505 input_thread_t *p_input = (input_thread_t *)p_this;
506 const int canc = vlc_savecancel();
508 if( Init( p_input ) )
510 /* If we failed, wait before we are killed, and exit */
517 /* Wait until we are asked to die */
518 if( !p_input->b_die )
525 p_input->b_dead = true;
526 vlc_restorecancel( canc );
530 /*****************************************************************************
531 * RunAndDestroy: main thread loop
532 * This is the "just forget me" thread that spawns the input processing chain,
533 * reads the stream, cleans up and releases memory
534 *****************************************************************************/
535 static void* RunAndDestroy( vlc_object_t *p_this )
537 input_thread_t *p_input = (input_thread_t *)p_this;
538 const int canc = vlc_savecancel();
540 if( Init( p_input ) )
550 vlc_object_release( p_input );
551 vlc_restorecancel( canc );
555 /*****************************************************************************
556 * Main loop: Fill buffers from access, and demux
557 *****************************************************************************/
561 * It asks the demuxer to demux some data
563 static void MainLoopDemux( input_thread_t *p_input, bool *pb_changed, mtime_t *pi_start_mdate )
569 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
570 ( p_input->p->i_run > 0 && *pi_start_mdate+p_input->p->i_run < mdate() ) )
573 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
578 if( p_input->p->input.b_title_demux &&
579 p_input->p->input.p_demux->info.i_update )
581 i_ret = UpdateFromDemux( p_input );
584 else if( !p_input->p->input.b_title_demux &&
585 p_input->p->input.p_access &&
586 p_input->p->input.p_access->info.i_update )
588 i_ret = UpdateFromAccess( p_input );
593 if( i_ret == 0 ) /* EOF */
597 var_Get( p_input, "input-repeat", &repeat );
598 if( repeat.i_int == 0 )
600 /* End of file - we do not set b_die because only the
601 * playlist is allowed to do so. */
602 msg_Dbg( p_input, "EOF reached" );
603 p_input->p->input.b_eof = true;
609 msg_Dbg( p_input, "repeating the same input (%d)",
611 if( repeat.i_int > 0 )
614 var_Set( p_input, "input-repeat", repeat );
617 /* Seek to start title/seekpoint */
618 val.i_int = p_input->p->input.i_title_start -
619 p_input->p->input.i_title_offset;
620 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
622 input_ControlPush( p_input,
623 INPUT_CONTROL_SET_TITLE, &val );
625 val.i_int = p_input->p->input.i_seekpoint_start -
626 p_input->p->input.i_seekpoint_offset;
627 if( val.i_int > 0 /* TODO: check upper boundary */ )
628 input_ControlPush( p_input,
629 INPUT_CONTROL_SET_SEEKPOINT, &val );
631 /* Seek to start position */
632 if( p_input->p->i_start > 0 )
634 val.i_time = p_input->p->i_start;
635 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
641 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
646 *pi_start_mdate = mdate();
651 input_ChangeState( p_input, ERROR_S );
654 if( i_ret > 0 && p_input->p->i_slave > 0 )
656 SlaveDemux( p_input );
662 * It update the variables used by the interfaces
664 static void MainLoopInterface( input_thread_t *p_input )
668 int64_t i_time, i_length;
670 /* update input status variables */
671 if( !demux_Control( p_input->p->input.p_demux,
672 DEMUX_GET_POSITION, &f_pos ) )
674 val.f_float = (float)f_pos;
675 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
677 if( !demux_Control( p_input->p->input.p_demux,
678 DEMUX_GET_TIME, &i_time ) )
680 p_input->i_time = i_time;
682 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
684 if( !demux_Control( p_input->p->input.p_demux,
685 DEMUX_GET_LENGTH, &i_length ) )
688 var_Get( p_input, "length", &old_val );
689 val.i_time = i_length;
690 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
692 if( old_val.i_time != val.i_time )
694 UpdateItemLength( p_input, i_length );
698 var_SetBool( p_input, "intf-change", true );
703 * It updates the globals statics
705 static void MainLoopStatistic( input_thread_t *p_input )
707 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
708 /* Are we the thread responsible for computing global stats ? */
709 if( libvlc_priv( p_input->p_libvlc )->p_stats_computer == p_input )
711 stats_ComputeGlobalStats( p_input->p_libvlc,
712 p_input->p_libvlc->p_stats );
714 var_SetBool( p_input, "stats-change", true );
719 * The main input loop.
721 static void MainLoop( input_thread_t *p_input )
723 mtime_t i_start_mdate = mdate();
724 mtime_t i_intf_update = 0;
725 mtime_t i_statistic_update = 0;
727 /* Start the timer */
728 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
730 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
741 b_force_update = false;
743 /* FIXME if p_input->i_state == PAUSE_S the access/access_demux
744 * is paused -> this may cause problem with some of them
745 * The same problem can be seen when seeking while paused */
746 b_paused = p_input->i_state == PAUSE_S &&
747 !input_EsOutIsBuffering( p_input->p->p_es_out );
751 MainLoopDemux( p_input, &b_force_update, &i_start_mdate );
752 i_wakeup = input_EsOutGetWakeup( p_input->p->p_es_out );
757 i_deadline = i_wakeup;
759 i_deadline = __MIN( i_intf_update, i_statistic_update );
762 vlc_mutex_lock( &p_input->p->lock_control );
763 ControlReduce( p_input );
764 while( !ControlPopNoLock( p_input, &i_type, &val, i_deadline ) )
766 msg_Dbg( p_input, "control type=%d", i_type );
767 if( Control( p_input, i_type, val ) )
768 b_force_update = true;
770 vlc_mutex_unlock( &p_input->p->lock_control );
772 /* Update interface and statistics */
774 if( i_intf_update < i_current || b_force_update )
776 MainLoopInterface( p_input );
777 i_intf_update = i_current + INT64_C(250000);
778 b_force_update = false;
780 if( i_statistic_update < i_current )
782 MainLoopStatistic( p_input );
783 i_statistic_update = i_current + INT64_C(1000000);
786 /* Check if i_wakeup is still valid */
789 mtime_t i_new_wakeup = input_EsOutGetWakeup( p_input->p->p_es_out );
793 } while( i_current < i_wakeup );
796 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
798 /* We have finish to demux data but not to play them */
799 while( vlc_object_alive( p_input ) )
801 if( input_EsOutDecodersIsEmpty( p_input->p->p_es_out ) )
804 msg_Dbg( p_input, "waiting decoder fifos to empty" );
806 msleep( INPUT_IDLE_SLEEP );
809 /* We have finished */
810 input_ChangeState( p_input, END_S );
814 static void InitStatistics( input_thread_t * p_input )
816 if( p_input->b_preparsing ) return;
818 /* Prepare statistics */
819 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
820 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
821 if( libvlc_stats( p_input ) )
823 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
824 INIT_COUNTER( read_packets, INTEGER, COUNTER );
825 INIT_COUNTER( demux_read, INTEGER, COUNTER );
826 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
827 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
828 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
829 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
830 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
831 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
832 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
833 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
834 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
835 p_input->p->counters.p_sout_send_bitrate = NULL;
836 p_input->p->counters.p_sout_sent_packets = NULL;
837 p_input->p->counters.p_sout_sent_bytes = NULL;
838 if( p_input->p->counters.p_demux_bitrate )
839 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
840 if( p_input->p->counters.p_input_bitrate )
841 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
846 static int InitSout( input_thread_t * p_input )
850 if( p_input->b_preparsing ) return VLC_SUCCESS;
852 /* Find a usable sout and attach it to p_input */
853 psz = var_GetNonEmptyString( p_input, "sout" );
854 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
856 /* Check the validity of the provided sout */
857 if( p_input->p->p_sout )
859 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
861 msg_Dbg( p_input, "destroying unusable sout" );
863 sout_DeleteInstance( p_input->p->p_sout );
864 p_input->p->p_sout = NULL;
868 if( p_input->p->p_sout )
871 msg_Dbg( p_input, "sout keep: reusing sout" );
872 msg_Dbg( p_input, "sout keep: you probably want to use "
873 "gather stream_out" );
874 vlc_object_attach( p_input->p->p_sout, p_input );
878 /* Create a new one */
879 p_input->p->p_sout = sout_NewInstance( p_input, psz );
880 if( !p_input->p->p_sout )
882 input_ChangeState( p_input, ERROR_S );
883 msg_Err( p_input, "cannot start stream output instance, " \
889 if( libvlc_stats( p_input ) )
891 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
892 INIT_COUNTER( sout_sent_bytes, INTEGER, COUNTER );
893 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
894 if( p_input->p->counters.p_sout_send_bitrate )
895 p_input->p->counters.p_sout_send_bitrate->update_interval =
899 else if( p_input->p->p_sout )
901 msg_Dbg( p_input, "destroying useless sout" );
903 sout_DeleteInstance( p_input->p->p_sout );
904 p_input->p->p_sout = NULL;
912 static void InitTitle( input_thread_t * p_input )
916 if( p_input->b_preparsing ) return;
918 /* Create global title (from master) */
919 p_input->p->i_title = p_input->p->input.i_title;
920 p_input->p->title = p_input->p->input.title;
921 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
922 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
923 if( p_input->p->i_title > 0 )
925 /* Setup variables */
926 input_ControlVarNavigation( p_input );
927 input_ControlVarTitle( p_input, 0 );
931 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
932 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
933 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
936 if( p_input->i_pts_delay < 0 )
937 p_input->i_pts_delay = 0;
939 /* If the desynchronisation requested by the user is < 0, we need to
940 * cache more data. */
941 var_Get( p_input, "audio-desync", &val );
943 p_input->i_pts_delay -= (val.i_int * 1000);
945 /* Update cr_average depending on the caching */
946 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
947 p_input->p->input.i_cr_average /= 10;
948 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
951 static void StartTitle( input_thread_t * p_input )
960 /* Start title/chapter */
962 if( p_input->b_preparsing )
964 p_input->p->i_start = 0;
968 val.i_int = p_input->p->input.i_title_start -
969 p_input->p->input.i_title_offset;
970 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
971 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
972 val.i_int = p_input->p->input.i_seekpoint_start -
973 p_input->p->input.i_seekpoint_offset;
974 if( val.i_int > 0 /* TODO: check upper boundary */ )
975 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
979 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
980 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
981 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
982 i_length = var_GetTime( p_input, "length" );
983 if( p_input->p->i_run < 0 )
985 msg_Warn( p_input, "invalid run-time ignored" );
986 p_input->p->i_run = 0;
989 if( p_input->p->i_start > 0 )
991 if( p_input->p->i_start >= i_length )
993 msg_Warn( p_input, "invalid start-time ignored" );
999 msg_Dbg( p_input, "starting at time: %ds",
1000 (int)( p_input->p->i_start / INT64_C(1000000) ) );
1002 s.i_time = p_input->p->i_start;
1003 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
1006 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
1008 msg_Warn( p_input, "invalid stop-time ignored" );
1009 p_input->p->i_stop = 0;
1012 /* Load subtitles */
1013 /* Get fps and set it if not already set */
1014 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
1017 float f_requested_fps;
1019 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
1020 var_SetFloat( p_input, "sub-original-fps", f_fps );
1022 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1023 if( f_requested_fps != f_fps )
1025 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1026 VLC_VAR_DOINHERIT );
1027 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1031 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1034 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1037 /* Look for and add subtitle files */
1038 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1039 if( psz_subtitle != NULL )
1041 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1042 SubtitleAdd( p_input, psz_subtitle, true );
1045 var_Get( p_input, "sub-autodetect-file", &val );
1048 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1049 char **ppsz_subs = subtitles_Detect( p_input, psz_autopath,
1050 p_input->p->input.p_item->psz_uri );
1051 free( psz_autopath );
1053 for( int i = 0; ppsz_subs && ppsz_subs[i]; i++ )
1055 /* Try to autoselect the first autodetected subtitles file
1056 * if no subtitles file was specified */
1057 bool b_forced = i == 0 && !psz_subtitle;
1059 if( !psz_subtitle || strcmp( psz_subtitle, ppsz_subs[i] ) )
1060 SubtitleAdd( p_input, ppsz_subs[i], b_forced );
1062 free( ppsz_subs[i] );
1066 free( psz_subtitle );
1068 /* Look for slave */
1069 psz = var_GetNonEmptyString( p_input, "input-slave" );
1073 input_source_t *slave;
1074 while( psz && *psz )
1076 while( *psz == ' ' || *psz == '#' )
1080 if( ( psz_delim = strchr( psz, '#' ) ) )
1082 *psz_delim++ = '\0';
1089 msg_Dbg( p_input, "adding slave input '%s'", psz );
1090 slave = InputSourceNew( p_input );
1091 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1093 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1102 static void InitPrograms( input_thread_t * p_input )
1107 if( p_input->b_preparsing ) return;
1110 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1111 i_es_out_mode = ES_OUT_MODE_AUTO;
1113 if( p_input->p->p_sout )
1115 var_Get( p_input, "sout-all", &val );
1118 i_es_out_mode = ES_OUT_MODE_ALL;
1123 var_Get( p_input, "programs", &val );
1124 if( val.p_list && val.p_list->i_count )
1126 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1127 /* Note : we should remove the "program" callback. */
1131 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1136 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1138 /* Inform the demuxer about waited group (needed only for DVB) */
1139 if( i_es_out_mode == ES_OUT_MODE_ALL )
1141 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1143 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1145 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1150 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1151 (int) var_GetInteger( p_input, "program" ), NULL );
1155 static int Init( input_thread_t * p_input )
1161 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1163 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1165 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1166 var_SetString( p_input, "sout", "" );
1167 var_SetBool( p_input, "sout-all", false );
1168 var_SetString( p_input, "input-slave", "" );
1169 var_SetInteger( p_input, "input-repeat", 0 );
1170 var_SetString( p_input, "sub-file", "" );
1171 var_SetBool( p_input, "sub-autodetect-file", false );
1175 InitStatistics( p_input );
1177 ret = InitSout( p_input );
1178 if( ret != VLC_SUCCESS )
1179 return ret; /* FIXME: goto error; should be better here */
1183 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1184 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1185 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1187 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1188 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1190 if( InputSourceInit( p_input, &p_input->p->input,
1191 p_input->p->input.p_item->psz_uri, NULL ) )
1196 InitTitle( p_input );
1198 /* Load master infos */
1200 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1201 &val.i_time ) && val.i_time > 0 )
1203 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1204 UpdateItemLength( p_input, val.i_time );
1208 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1209 if( val.i_time > 0 )
1210 { /* fallback: gets length from metadata */
1211 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1212 UpdateItemLength( p_input, val.i_time );
1216 StartTitle( p_input );
1218 InitPrograms( p_input );
1220 if( !p_input->b_preparsing && p_input->p->p_sout )
1222 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1224 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1226 /* We don't want a high input priority here or we'll
1227 * end-up sucking up all the CPU time */
1228 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1231 msg_Dbg( p_input, "starting in %s mode",
1232 p_input->p->b_out_pace_control ? "async" : "sync" );
1235 p_meta = vlc_meta_New();
1237 /* Get meta data from users */
1238 InputMetaUser( p_input, p_meta );
1240 /* Get meta data from master input */
1241 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1243 /* Access_file does not give any meta, and there are no slave */
1244 AccessMeta( p_input, p_meta );
1246 InputUpdateMeta( p_input, p_meta );
1248 if( !p_input->b_preparsing )
1250 msg_Dbg( p_input, "`%s' successfully opened",
1251 p_input->p->input.p_item->psz_uri );
1255 /* initialization is complete */
1256 input_ChangeState( p_input, PLAYING_S );
1261 input_ChangeState( p_input, ERROR_S );
1263 if( p_input->p->p_es_out )
1264 input_EsOutDelete( p_input->p->p_es_out );
1266 if( p_input->p->p_sout )
1268 vlc_object_detach( p_input->p->p_sout );
1269 sout_DeleteInstance( p_input->p->p_sout );
1273 if( !p_input->b_preparsing && libvlc_stats( p_input ) )
1275 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1276 stats_CounterClean( p_input->p->counters.p_##c );\
1277 p_input->p->counters.p_##c = NULL; } while(0)
1278 EXIT_COUNTER( read_bytes );
1279 EXIT_COUNTER( read_packets );
1280 EXIT_COUNTER( demux_read );
1281 EXIT_COUNTER( input_bitrate );
1282 EXIT_COUNTER( demux_bitrate );
1283 EXIT_COUNTER( played_abuffers );
1284 EXIT_COUNTER( lost_abuffers );
1285 EXIT_COUNTER( displayed_pictures );
1286 EXIT_COUNTER( lost_pictures );
1287 EXIT_COUNTER( decoded_audio );
1288 EXIT_COUNTER( decoded_video );
1289 EXIT_COUNTER( decoded_sub );
1291 if( p_input->p->p_sout )
1293 EXIT_COUNTER( sout_sent_packets );
1294 EXIT_COUNTER( sout_sent_bytes );
1295 EXIT_COUNTER( sout_send_bitrate );
1300 /* Mark them deleted */
1301 p_input->p->input.p_demux = NULL;
1302 p_input->p->input.p_stream = NULL;
1303 p_input->p->input.p_access = NULL;
1304 p_input->p->p_es_out = NULL;
1305 p_input->p->p_sout = NULL;
1307 return VLC_EGENERIC;
1310 /*****************************************************************************
1311 * WaitDie: Wait until we are asked to die.
1312 *****************************************************************************
1313 * This function is called when an error occurred during thread main's loop.
1314 *****************************************************************************/
1315 static void WaitDie( input_thread_t *p_input )
1317 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1319 /* Wait a die order */
1320 vlc_object_lock( p_input );
1321 while( vlc_object_alive( p_input ) )
1322 vlc_object_wait( p_input );
1323 vlc_object_unlock( p_input );
1326 /*****************************************************************************
1327 * End: end the input thread
1328 *****************************************************************************/
1329 static void End( input_thread_t * p_input )
1333 /* We are at the end */
1334 input_ChangeState( p_input, END_S );
1336 /* Clean control variables */
1337 input_ControlVarStop( p_input );
1339 /* Clean up master */
1340 InputSourceClean( &p_input->p->input );
1343 for( i = 0; i < p_input->p->i_slave; i++ )
1345 InputSourceClean( p_input->p->slave[i] );
1346 free( p_input->p->slave[i] );
1348 free( p_input->p->slave );
1350 /* Unload all modules */
1351 if( p_input->p->p_es_out )
1352 input_EsOutDelete( p_input->p->p_es_out );
1354 if( !p_input->b_preparsing )
1356 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1357 if( libvlc_stats( p_input ) )
1359 libvlc_priv_t *p_private = libvlc_priv( p_input->p_libvlc );
1361 /* make sure we are up to date */
1362 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1363 if( p_private->p_stats_computer == p_input )
1365 stats_ComputeGlobalStats( p_input->p_libvlc,
1366 p_input->p_libvlc->p_stats );
1367 p_private->p_stats_computer = NULL;
1369 CL_CO( read_bytes );
1370 CL_CO( read_packets );
1371 CL_CO( demux_read );
1372 CL_CO( input_bitrate );
1373 CL_CO( demux_bitrate );
1374 CL_CO( played_abuffers );
1375 CL_CO( lost_abuffers );
1376 CL_CO( displayed_pictures );
1377 CL_CO( lost_pictures );
1378 CL_CO( decoded_audio) ;
1379 CL_CO( decoded_video );
1380 CL_CO( decoded_sub) ;
1383 /* Close optional stream output instance */
1384 if( p_input->p->p_sout )
1386 CL_CO( sout_sent_packets );
1387 CL_CO( sout_sent_bytes );
1388 CL_CO( sout_send_bitrate );
1390 vlc_object_detach( p_input->p->p_sout );
1395 if( p_input->p->i_attachment > 0 )
1397 for( i = 0; i < p_input->p->i_attachment; i++ )
1398 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1399 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1402 /* Tell we're dead */
1403 p_input->b_dead = true;
1406 /*****************************************************************************
1408 *****************************************************************************/
1409 static inline int ControlPopNoLock( input_thread_t *p_input,
1410 int *pi_type, vlc_value_t *p_val,
1411 mtime_t i_deadline )
1414 while( p_input->p->i_control <= 0 )
1416 if( !vlc_object_alive( p_input ) )
1417 return VLC_EGENERIC;
1419 if( i_deadline < 0 )
1420 return VLC_EGENERIC;
1422 if( vlc_cond_timedwait( &p_input->p->wait_control, &p_input->p->lock_control, i_deadline ) )
1423 return VLC_EGENERIC;
1426 *pi_type = p_input->p->control[0].i_type;
1427 *p_val = p_input->p->control[0].val;
1429 p_input->p->i_control--;
1430 if( p_input->p->i_control > 0 )
1434 for( i = 0; i < p_input->p->i_control; i++ )
1436 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1437 p_input->p->control[i].val = p_input->p->control[i+1].val;
1444 static void ControlReduce( input_thread_t *p_input )
1451 for( i = 1; i < p_input->p->i_control; i++ )
1453 const int i_lt = p_input->p->control[i-1].i_type;
1454 const int i_ct = p_input->p->control[i].i_type;
1456 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1457 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1461 ( i_ct == INPUT_CONTROL_SET_STATE ||
1462 i_ct == INPUT_CONTROL_SET_RATE ||
1463 i_ct == INPUT_CONTROL_SET_POSITION ||
1464 i_ct == INPUT_CONTROL_SET_TIME ||
1465 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1466 i_ct == INPUT_CONTROL_SET_TITLE ||
1467 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1468 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1471 // msg_Dbg( p_input, "merged at %d", i );
1472 /* Remove the i-1 */
1473 for( j = i; j < p_input->p->i_control; j++ )
1474 p_input->p->control[j-1] = p_input->p->control[j];
1475 p_input->p->i_control--;
1479 /* TODO but that's not that important
1480 - merge SET_X with SET_X_CMD
1481 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1482 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1489 static bool Control( input_thread_t *p_input, int i_type,
1492 const mtime_t i_control_date = mdate();
1493 bool b_force_update = false;
1496 return b_force_update;
1500 case INPUT_CONTROL_SET_DIE:
1501 msg_Dbg( p_input, "control: stopping input" );
1503 /* Mark all submodules to die */
1504 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1507 case INPUT_CONTROL_SET_POSITION:
1508 case INPUT_CONTROL_SET_POSITION_OFFSET:
1512 if( p_input->p->b_recording )
1514 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) ignored while recording" );
1517 if( i_type == INPUT_CONTROL_SET_POSITION )
1519 f_pos = val.f_float;
1523 /* Should not fail */
1524 demux_Control( p_input->p->input.p_demux,
1525 DEMUX_GET_POSITION, &f_pos );
1526 f_pos += val.f_float;
1528 if( f_pos < 0.0 ) f_pos = 0.0;
1529 if( f_pos > 1.0 ) f_pos = 1.0;
1530 /* Reset the decoders states and clock sync (before calling the demuxer */
1531 input_EsOutChangePosition( p_input->p->p_es_out );
1532 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1535 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1536 "%2.1f%% failed", f_pos * 100 );
1540 if( p_input->p->i_slave > 0 )
1541 SlaveSeek( p_input );
1543 b_force_update = true;
1548 case INPUT_CONTROL_SET_TIME:
1549 case INPUT_CONTROL_SET_TIME_OFFSET:
1554 if( p_input->p->b_recording )
1556 msg_Err( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) ignored while recording" );
1560 if( i_type == INPUT_CONTROL_SET_TIME )
1562 i_time = val.i_time;
1566 /* Should not fail */
1567 demux_Control( p_input->p->input.p_demux,
1568 DEMUX_GET_TIME, &i_time );
1569 i_time += val.i_time;
1571 if( i_time < 0 ) i_time = 0;
1573 /* Reset the decoders states and clock sync (before calling the demuxer */
1574 input_EsOutChangePosition( p_input->p->p_es_out );
1576 i_ret = demux_Control( p_input->p->input.p_demux,
1577 DEMUX_SET_TIME, i_time );
1582 /* Emulate it with a SET_POS */
1583 demux_Control( p_input->p->input.p_demux,
1584 DEMUX_GET_LENGTH, &i_length );
1587 double f_pos = (double)i_time / (double)i_length;
1588 i_ret = demux_Control( p_input->p->input.p_demux,
1589 DEMUX_SET_POSITION, f_pos );
1594 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1595 " failed or not possible", i_time );
1599 if( p_input->p->i_slave > 0 )
1600 SlaveSeek( p_input );
1602 b_force_update = true;
1607 case INPUT_CONTROL_SET_STATE:
1608 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1609 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1612 if( p_input->p->input.p_access )
1613 i_ret = access_Control( p_input->p->input.p_access,
1614 ACCESS_SET_PAUSE_STATE, false );
1616 i_ret = demux_Control( p_input->p->input.p_demux,
1617 DEMUX_SET_PAUSE_STATE, false );
1621 /* FIXME What to do ? */
1622 msg_Warn( p_input, "cannot unset pause -> EOF" );
1623 vlc_mutex_unlock( &p_input->p->lock_control );
1624 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1625 vlc_mutex_lock( &p_input->p->lock_control );
1628 b_force_update = true;
1630 /* Switch to play */
1631 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1635 input_EsOutChangePause( p_input->p->p_es_out, false, i_control_date );
1637 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1638 p_input->p->b_can_pause )
1641 if( p_input->p->input.p_access )
1642 i_ret = access_Control( p_input->p->input.p_access,
1643 ACCESS_SET_PAUSE_STATE, true );
1645 i_ret = demux_Control( p_input->p->input.p_demux,
1646 DEMUX_SET_PAUSE_STATE, true );
1648 b_force_update = true;
1652 msg_Warn( p_input, "cannot set pause state" );
1653 state = p_input->i_state;
1660 /* Switch to new state */
1661 input_ChangeStateWithVarCallback( p_input, state, false );
1665 input_EsOutChangePause( p_input->p->p_es_out, true, i_control_date );
1667 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1669 b_force_update = true;
1671 /* Correct "state" value */
1672 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1674 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1676 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1680 case INPUT_CONTROL_SET_RATE:
1681 case INPUT_CONTROL_SET_RATE_SLOWER:
1682 case INPUT_CONTROL_SET_RATE_FASTER:
1686 if( i_type == INPUT_CONTROL_SET_RATE )
1692 static const int ppi_factor[][2] = {
1693 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1695 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1704 for( i = 0; ppi_factor[i][0] != 0; i++ )
1706 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1707 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1708 if( i_test_e < i_error )
1714 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1716 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1718 if( ppi_factor[i_idx+1][0] > 0 )
1719 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1721 i_rate = INPUT_RATE_MAX+1;
1725 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1727 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1729 i_rate = INPUT_RATE_MIN-1;
1733 if( i_rate < INPUT_RATE_MIN )
1735 msg_Dbg( p_input, "cannot set rate faster" );
1736 i_rate = INPUT_RATE_MIN;
1738 else if( i_rate > INPUT_RATE_MAX )
1740 msg_Dbg( p_input, "cannot set rate slower" );
1741 i_rate = INPUT_RATE_MAX;
1743 if( i_rate != INPUT_RATE_DEFAULT &&
1744 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1745 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1747 msg_Dbg( p_input, "cannot change rate" );
1748 i_rate = INPUT_RATE_DEFAULT;
1750 if( i_rate != p_input->p->i_rate &&
1751 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1754 if( p_input->p->input.p_access )
1755 i_ret = VLC_EGENERIC;
1757 i_ret = demux_Control( p_input->p->input.p_demux,
1758 DEMUX_SET_RATE, &i_rate );
1761 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1762 i_rate = p_input->p->i_rate;
1767 if( i_rate != p_input->p->i_rate )
1770 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1771 var_SetBool( p_input, "rate-change", true );
1773 p_input->p->i_rate = i_rate;
1775 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1776 if( p_input->p->input.b_rescale_ts )
1777 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1779 b_force_update = true;
1784 case INPUT_CONTROL_SET_PROGRAM:
1785 /* No need to force update, es_out does it if needed */
1786 es_out_Control( p_input->p->p_es_out,
1787 ES_OUT_SET_GROUP, val.i_int );
1789 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1793 case INPUT_CONTROL_SET_ES:
1794 /* No need to force update, es_out does it if needed */
1795 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1796 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1799 case INPUT_CONTROL_RESTART_ES:
1800 es_out_Control( p_input->p->p_es_out, ES_OUT_RESTART_ES,
1801 input_EsOutGetFromID( p_input->p->p_es_out, val.i_int ) );
1804 case INPUT_CONTROL_SET_AUDIO_DELAY:
1805 input_EsOutSetDelay( p_input->p->p_es_out,
1806 AUDIO_ES, val.i_time );
1807 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1810 case INPUT_CONTROL_SET_SPU_DELAY:
1811 input_EsOutSetDelay( p_input->p->p_es_out,
1812 SPU_ES, val.i_time );
1813 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1816 case INPUT_CONTROL_SET_TITLE:
1817 case INPUT_CONTROL_SET_TITLE_NEXT:
1818 case INPUT_CONTROL_SET_TITLE_PREV:
1819 if( p_input->p->b_recording )
1821 msg_Err( p_input, "INPUT_CONTROL_SET_TITLE(*) ignored while recording" );
1824 if( p_input->p->input.b_title_demux &&
1825 p_input->p->input.i_title > 0 )
1828 /* FIXME handle demux title */
1829 demux_t *p_demux = p_input->p->input.p_demux;
1832 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1833 i_title = p_demux->info.i_title - 1;
1834 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1835 i_title = p_demux->info.i_title + 1;
1837 i_title = val.i_int;
1839 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1841 input_EsOutChangePosition( p_input->p->p_es_out );
1843 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1844 input_ControlVarTitle( p_input, i_title );
1847 else if( p_input->p->input.i_title > 0 )
1849 access_t *p_access = p_input->p->input.p_access;
1852 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1853 i_title = p_access->info.i_title - 1;
1854 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1855 i_title = p_access->info.i_title + 1;
1857 i_title = val.i_int;
1859 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1861 input_EsOutChangePosition( p_input->p->p_es_out );
1863 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1864 stream_AccessReset( p_input->p->input.p_stream );
1868 case INPUT_CONTROL_SET_SEEKPOINT:
1869 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1870 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1871 if( p_input->p->b_recording )
1873 msg_Err( p_input, "INPUT_CONTROL_SET_SEEKPOINT(*) ignored while recording" );
1877 if( p_input->p->input.b_title_demux &&
1878 p_input->p->input.i_title > 0 )
1880 demux_t *p_demux = p_input->p->input.p_demux;
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_demux->info.i_seekpoint;
1888 i_seekpoint_time = p_input->p->input.title[p_demux->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_demux->info.i_seekpoint + 1;
1902 i_seekpoint = val.i_int;
1904 if( i_seekpoint >= 0 && i_seekpoint <
1905 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1908 input_EsOutChangePosition( p_input->p->p_es_out );
1910 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1913 else if( p_input->p->input.i_title > 0 )
1915 demux_t *p_demux = p_input->p->input.p_demux;
1916 access_t *p_access = p_input->p->input.p_access;
1918 int64_t i_input_time;
1919 int64_t i_seekpoint_time;
1921 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1923 i_seekpoint = p_access->info.i_seekpoint;
1924 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1925 if( i_seekpoint_time >= 0 &&
1926 demux_Control( p_demux,
1927 DEMUX_GET_TIME, &i_input_time ) )
1929 if( i_input_time < i_seekpoint_time + 3000000 )
1935 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1936 i_seekpoint = p_access->info.i_seekpoint + 1;
1938 i_seekpoint = val.i_int;
1940 if( i_seekpoint >= 0 && i_seekpoint <
1941 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1943 input_EsOutChangePosition( p_input->p->p_es_out );
1945 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1947 stream_AccessReset( p_input->p->input.p_stream );
1952 case INPUT_CONTROL_ADD_SUBTITLE:
1953 if( val.psz_string )
1955 SubtitleAdd( p_input, val.psz_string, true );
1956 free( val.psz_string );
1960 case INPUT_CONTROL_ADD_SLAVE:
1961 if( val.psz_string )
1963 input_source_t *slave = InputSourceNew( p_input );
1965 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1971 msg_Dbg( p_input, "adding %s as slave on the fly",
1975 if( demux_Control( p_input->p->input.p_demux,
1976 DEMUX_GET_TIME, &i_time ) )
1978 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1979 InputSourceClean( slave );
1983 if( demux_Control( slave->p_demux,
1984 DEMUX_SET_TIME, i_time ) )
1986 msg_Err( p_input, "seek failed for new slave" );
1987 InputSourceClean( slave );
1992 /* Get meta (access and demux) */
1993 p_meta = vlc_meta_New();
1994 access_Control( slave->p_access, ACCESS_GET_META,
1996 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1997 InputUpdateMeta( p_input, p_meta );
1999 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2004 msg_Warn( p_input, "failed to add %s as slave",
2008 free( val.psz_string );
2012 case INPUT_CONTROL_SET_RECORD_STATE:
2013 if( !!p_input->p->b_recording != !!val.b_bool )
2015 if( p_input->p->input.b_can_stream_record )
2017 if( demux_Control( p_input->p->input.p_demux,
2018 DEMUX_SET_RECORD_STATE, val.b_bool ) )
2023 if( input_EsOutSetRecord( p_input->p->p_es_out, val.b_bool ) )
2026 p_input->p->b_recording = val.b_bool;
2028 var_Change( p_input, "record", VLC_VAR_SETVALUE, &val, NULL );
2030 b_force_update = true;
2034 case INPUT_CONTROL_SET_BOOKMARK:
2036 msg_Err( p_input, "not yet implemented" );
2040 return b_force_update;
2043 /*****************************************************************************
2045 *****************************************************************************/
2046 static int UpdateFromDemux( input_thread_t *p_input )
2048 demux_t *p_demux = p_input->p->input.p_demux;
2051 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2053 v.i_int = p_demux->info.i_title;
2054 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2056 input_ControlVarTitle( p_input, p_demux->info.i_title );
2058 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2060 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2062 v.i_int = p_demux->info.i_seekpoint;
2063 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2065 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2067 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2069 /* Hmmm only works with master input */
2070 if( p_input->p->input.p_demux == p_demux )
2072 int i_title_end = p_input->p->input.i_title_end -
2073 p_input->p->input.i_title_offset;
2074 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2075 p_input->p->input.i_seekpoint_offset;
2077 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2079 if( p_demux->info.i_title > i_title_end ||
2080 ( p_demux->info.i_title == i_title_end &&
2081 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2083 else if( i_seekpoint_end >=0 )
2085 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2087 else if( i_title_end >= 0 )
2089 if( p_demux->info.i_title > i_title_end ) return 0;
2096 /*****************************************************************************
2098 *****************************************************************************/
2099 static int UpdateFromAccess( input_thread_t *p_input )
2101 access_t *p_access = p_input->p->input.p_access;
2104 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2106 v.i_int = p_access->info.i_title;
2107 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2109 input_ControlVarTitle( p_input, p_access->info.i_title );
2111 stream_AccessUpdate( p_input->p->input.p_stream );
2113 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2115 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2117 v.i_int = p_access->info.i_seekpoint;
2118 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2119 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2121 if( p_access->info.i_update & INPUT_UPDATE_META )
2123 /* TODO maybe multi - access ? */
2124 vlc_meta_t *p_meta = vlc_meta_New();
2125 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2126 InputUpdateMeta( p_input, p_meta );
2127 p_access->info.i_update &= ~INPUT_UPDATE_META;
2129 if( p_access->info.i_update & INPUT_UPDATE_SIGNAL )
2134 if( access_Control( p_access, ACCESS_GET_SIGNAL, &f_quality, &f_strength ) )
2135 f_quality = f_strength = -1;
2137 var_SetFloat( p_input, "signal-quality", f_quality );
2138 var_SetFloat( p_input, "signal-strength", f_strength );
2140 p_access->info.i_update &= ~INPUT_UPDATE_SIGNAL;
2143 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2145 /* Hmmm only works with master input */
2146 if( p_input->p->input.p_access == p_access )
2148 int i_title_end = p_input->p->input.i_title_end -
2149 p_input->p->input.i_title_offset;
2150 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2151 p_input->p->input.i_seekpoint_offset;
2153 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2155 if( p_access->info.i_title > i_title_end ||
2156 ( p_access->info.i_title == i_title_end &&
2157 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2159 else if( i_seekpoint_end >=0 )
2161 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2163 else if( i_title_end >= 0 )
2165 if( p_access->info.i_title > i_title_end ) return 0;
2172 /*****************************************************************************
2174 *****************************************************************************/
2175 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2177 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2180 /*****************************************************************************
2182 *****************************************************************************/
2183 static input_source_t *InputSourceNew( input_thread_t *p_input )
2185 VLC_UNUSED(p_input);
2186 input_source_t *in = malloc( sizeof( input_source_t ) );
2188 memset( in, 0, sizeof( input_source_t ) );
2192 /*****************************************************************************
2194 *****************************************************************************/
2195 static int InputSourceInit( input_thread_t *p_input,
2196 input_source_t *in, const char *psz_mrl,
2197 const char *psz_forced_demux )
2199 const bool b_master = in == &p_input->p->input;
2201 char psz_dup[strlen(psz_mrl) + 1];
2202 const char *psz_access;
2203 const char *psz_demux;
2210 strcpy( psz_dup, psz_mrl );
2212 if( !in ) return VLC_EGENERIC;
2213 if( !p_input ) return VLC_EGENERIC;
2216 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2218 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2219 psz_mrl, psz_access, psz_demux, psz_path );
2220 if( !p_input->b_preparsing )
2222 /* Hack to allow udp://@:port syntax */
2224 (strncmp( psz_access, "udp", 3 ) &&
2225 strncmp( psz_access, "rtp", 3 )) )
2227 /* Find optional titles and seekpoints */
2228 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2229 &in->i_seekpoint_start, &in->i_seekpoint_end );
2232 if( psz_forced_demux && *psz_forced_demux )
2234 psz_demux = psz_forced_demux;
2236 else if( *psz_demux == '\0' )
2238 /* special hack for forcing a demuxer with --demux=module
2239 * (and do nothing with a list) */
2240 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2242 if( psz_var_demux != NULL &&
2243 !strchr(psz_var_demux, ',' ) &&
2244 !strchr(psz_var_demux, ':' ) )
2246 psz_demux = psz_var_demux;
2248 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2252 /* Try access_demux first */
2253 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2254 NULL, p_input->p->p_es_out, false );
2258 /* Preparsing is only for file:// */
2261 if( !*psz_access ) /* path without scheme:// */
2262 psz_access = "file";
2263 if( strcmp( psz_access, "file" ) )
2265 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2270 int64_t i_pts_delay;
2272 /* Get infos from access_demux */
2273 demux_Control( in->p_demux,
2274 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2275 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2277 in->b_title_demux = true;
2278 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2279 &in->title, &in->i_title,
2280 &in->i_title_offset, &in->i_seekpoint_offset ) )
2282 TAB_INIT( in->i_title, in->title );
2284 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2285 &in->b_can_pace_control ) )
2286 in->b_can_pace_control = false;
2288 if( !in->b_can_pace_control )
2290 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2291 &in->b_can_rate_control, &in->b_rescale_ts ) )
2293 in->b_can_rate_control = false;
2294 in->b_rescale_ts = true; /* not used */
2299 in->b_can_rate_control = true;
2300 in->b_rescale_ts = true;
2302 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2303 &in->b_can_pause ) )
2304 in->b_can_pause = false;
2305 var_SetBool( p_input, "can-pause", in->b_can_pause );
2307 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2309 if( ret != VLC_SUCCESS )
2311 var_Set( p_input, "seekable", val );
2315 int64_t i_pts_delay;
2318 input_ChangeState( p_input, OPENING_S );
2320 /* Now try a real access */
2321 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2323 /* Access failed, URL encoded ? */
2324 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2326 decode_URI( psz_path );
2328 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2329 psz_access, psz_demux, psz_path );
2331 in->p_access = access_New( p_input,
2332 psz_access, psz_demux, psz_path );
2334 if( in->p_access == NULL )
2336 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2338 intf_UserFatal( VLC_OBJECT( p_input), false,
2339 _("Your input can't be opened"),
2340 _("VLC is unable to open the MRL '%s'."
2341 " Check the log for details."), psz_mrl );
2346 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2347 while( psz && *psz )
2349 access_t *p_access = in->p_access;
2350 char *end = strchr( psz, ':' );
2355 in->p_access = access_FilterNew( in->p_access, psz );
2356 if( in->p_access == NULL )
2358 in->p_access = p_access;
2359 msg_Warn( p_input, "failed to insert access filter %s",
2367 /* Get infos from access */
2368 if( !p_input->b_preparsing )
2370 access_Control( in->p_access,
2371 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2372 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2374 in->b_title_demux = false;
2375 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2376 &in->title, &in->i_title,
2377 &in->i_title_offset, &in->i_seekpoint_offset ) )
2380 TAB_INIT( in->i_title, in->title );
2382 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2383 &in->b_can_pace_control );
2384 in->b_can_rate_control = in->b_can_pace_control;
2385 in->b_rescale_ts = true;
2387 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2389 var_SetBool( p_input, "can-pause", in->b_can_pause );
2390 access_Control( in->p_access, ACCESS_CAN_SEEK,
2392 var_Set( p_input, "seekable", val );
2396 input_ChangeState( p_input, BUFFERING_S );
2398 /* Autodetect extra files if none specified */
2399 char *psz_input_list = var_CreateGetNonEmptyString( p_input, "input-list" );
2400 if( !psz_input_list )
2402 char *psz_extra_files = InputGetExtraFiles( p_input, psz_access, psz_path );
2403 if( psz_extra_files )
2404 var_SetString( p_input, "input-list", psz_extra_files );
2405 free( psz_extra_files );
2408 /* Create the stream_t */
2409 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2411 /* Restor old value */
2412 if( !psz_input_list )
2413 var_SetString( p_input, "input-list", "" );
2414 free( psz_input_list );
2416 if( in->p_stream == NULL )
2418 msg_Warn( p_input, "cannot create a stream_t from access" );
2422 /* Open a demuxer */
2423 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2425 psz_demux = in->p_access->psz_demux;
2429 /* Take access redirections into account */
2430 char *psz_real_path;
2431 char *psz_buf = NULL;
2432 if( in->p_access->psz_path )
2434 const char *psz_a, *psz_d;
2435 psz_buf = strdup( in->p_access->psz_path );
2436 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2440 psz_real_path = psz_path;
2442 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2444 in->p_stream, p_input->p->p_es_out,
2445 p_input->b_preparsing );
2449 if( in->p_demux == NULL )
2451 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2452 psz_access, psz_demux, psz_path );
2453 intf_UserFatal( VLC_OBJECT( p_input ), false,
2454 _("VLC can't recognize the input's format"),
2455 _("The format of '%s' cannot be detected. "
2456 "Have a look at the log for details."), psz_mrl );
2460 /* Get title from demux */
2461 if( !p_input->b_preparsing && in->i_title <= 0 )
2463 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2464 &in->title, &in->i_title,
2465 &in->i_title_offset, &in->i_seekpoint_offset ))
2467 TAB_INIT( in->i_title, in->title );
2471 in->b_title_demux = true;
2476 /* Set record capabilities */
2477 if( demux_Control( in->p_demux, DEMUX_CAN_RECORD, &in->b_can_stream_record ) )
2478 in->b_can_stream_record = false;
2480 if( !var_CreateGetBool( p_input, "input-record-native" ) )
2481 in->b_can_stream_record = false;
2482 var_SetBool( p_input, "can-record", true );
2484 var_SetBool( p_input, "can-record", in->b_can_stream_record );
2488 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2489 if( 1 || !p_input->b_preparsing )
2492 input_attachment_t **attachment;
2493 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2494 &attachment, &i_attachment ) )
2496 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2497 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2498 i_attachment, attachment );
2499 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2502 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2504 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2506 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2509 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2510 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2516 input_ChangeState( p_input, ERROR_S );
2519 demux_Delete( in->p_demux );
2522 stream_Delete( in->p_stream );
2525 access_Delete( in->p_access );
2527 return VLC_EGENERIC;
2530 /*****************************************************************************
2532 *****************************************************************************/
2533 static void InputSourceClean( input_source_t *in )
2538 demux_Delete( in->p_demux );
2541 stream_Delete( in->p_stream );
2544 access_Delete( in->p_access );
2546 if( in->i_title > 0 )
2548 for( i = 0; i < in->i_title; i++ )
2549 vlc_input_title_Delete( in->title[i] );
2550 TAB_CLEAN( in->i_title, in->title );
2554 static void SlaveDemux( input_thread_t *p_input )
2558 bool b_set_time = true;
2560 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2562 /* msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" ); */
2566 for( i = 0; i < p_input->p->i_slave; i++ )
2568 input_source_t *in = p_input->p->slave[i];
2574 if( b_set_time && demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2579 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2581 msg_Err( p_input, "slave[%d] doesn't like "
2582 "DEMUX_GET_TIME -> EOF", i );
2587 if( i_stime >= i_time )
2590 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2596 i_ret = in->p_demux->pf_demux( in->p_demux );
2601 msg_Dbg( p_input, "slave %d EOF", i );
2607 static void SlaveSeek( input_thread_t *p_input )
2612 if( !p_input ) return;
2614 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2616 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2620 for( i = 0; i < p_input->p->i_slave; i++ )
2622 input_source_t *in = p_input->p->slave[i];
2624 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2627 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2637 /*****************************************************************************
2639 *****************************************************************************/
2640 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2644 if( !p_meta ) return;
2646 /* Get meta information from user */
2647 #define GET_META( field, s ) \
2648 var_Get( p_input, (s), &val ); \
2649 if( *val.psz_string ) \
2650 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2651 free( val.psz_string )
2653 GET_META( Title, "meta-title" );
2654 GET_META( Artist, "meta-artist" );
2655 GET_META( Genre, "meta-genre" );
2656 GET_META( Copyright, "meta-copyright" );
2657 GET_META( Description, "meta-description" );
2658 GET_META( Date, "meta-date" );
2659 GET_META( URL, "meta-url" );
2663 /*****************************************************************************
2664 * InputGetExtraFiles
2665 * Autodetect extra input list
2666 *****************************************************************************/
2667 static char *InputGetExtraFiles( input_thread_t *p_input,
2668 const char *psz_access, const char *psz_path )
2670 char *psz_list = NULL;
2672 if( ( psz_access && *psz_access && strcmp( psz_access, "file" ) ) || !psz_path )
2676 const char *psz_ext = strrchr( psz_path, '.' );
2677 if( !psz_ext || strcmp( psz_ext, ".001" ) )
2680 char *psz_file = strdup( psz_path );
2684 /* Try to list .xyz files */
2685 for( int i = 2; i < 999; i++ )
2687 char *psz_ext = strrchr( psz_file, '.' );
2690 snprintf( psz_ext, 5, ".%.3d", i );
2692 if( utf8_stat( psz_file, &st )
2693 || !S_ISREG( st.st_mode ) || !st.st_size )
2696 msg_Dbg( p_input, "Detected extra file `%s'", psz_file );
2700 char *psz_old = psz_list;
2701 /* FIXME how to handle file with ',' ?*/
2702 if( asprintf( &psz_list, "%s,%s", psz_old, psz_file ) < 0 )
2710 psz_list = strdup( psz_file );
2718 /*****************************************************************************
2719 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2720 * arturl and locking issue.
2721 *****************************************************************************/
2722 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2724 input_item_t *p_item = p_input->p->input.p_item;
2725 char * psz_arturl = NULL;
2726 char *psz_title = NULL;
2727 int i_arturl_event = false;
2732 psz_arturl = input_item_GetArtURL( p_item );
2734 vlc_mutex_lock( &p_item->lock );
2735 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2736 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2738 vlc_meta_Merge( p_item->p_meta, p_meta );
2740 if( psz_arturl && *psz_arturl )
2742 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2743 i_arturl_event = true;
2746 vlc_meta_Delete( p_meta );
2748 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2750 /* Don't look for art cover if sout
2751 * XXX It can change when sout has meta data support */
2752 if( p_input->p->p_sout && !p_input->b_preparsing )
2754 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2755 i_arturl_event = true;
2759 input_ExtractAttachmentAndCacheArt( p_input );
2765 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2767 p_meta = vlc_meta_New();
2768 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2770 vlc_mutex_unlock( &p_item->lock );
2772 input_item_SetPreparsed( p_item, true );
2774 if( i_arturl_event == true )
2778 /* Notify interested third parties */
2779 event.type = vlc_InputItemMetaChanged;
2780 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2781 vlc_event_send( &p_item->event_manager, &event );
2786 input_Control( p_input, INPUT_SET_NAME, psz_title );
2790 /** \todo handle sout meta */
2794 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2795 int i_new, input_attachment_t **pp_new )
2797 int i_attachment = *pi_attachment;
2798 input_attachment_t **attachment = *ppp_attachment;
2801 attachment = realloc( attachment,
2802 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2803 for( i = 0; i < i_new; i++ )
2804 attachment[i_attachment++] = pp_new[i];
2808 *pi_attachment = i_attachment;
2809 *ppp_attachment = attachment;
2812 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2816 if( p_input->b_preparsing )
2819 if( p_input->p->input.p_access )
2820 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2823 /* Get meta data from slave input */
2824 for( i = 0; i < p_input->p->i_slave; i++ )
2826 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2827 if( p_input->p->slave[i]->p_access )
2829 access_Control( p_input->p->slave[i]->p_access,
2830 ACCESS_GET_META, p_meta );
2835 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2842 /* XXX I am not sure it is a great idea, besides, there is more than that
2843 * if we want to do it right */
2844 vlc_mutex_lock( &p_item->lock );
2845 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2847 vlc_mutex_unlock( &p_item->lock );
2850 vlc_mutex_unlock( &p_item->lock );
2853 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2854 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2859 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2860 if(! p_demux->p_private )
2863 p_id3 = module_need( p_demux, "meta reader", NULL, 0 );
2866 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2868 if( p_demux_meta->p_meta )
2870 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2871 vlc_meta_Delete( p_demux_meta->p_meta );
2874 if( p_demux_meta->i_attachments > 0 )
2876 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2877 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2878 p_demux_meta->i_attachments, p_demux_meta->attachments );
2879 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2881 module_unneed( p_demux, p_id3 );
2883 free( p_demux->p_private );
2887 /*****************************************************************************
2888 * MRLSplit: parse the access, demux and url part of the
2889 * Media Resource Locator.
2890 *****************************************************************************/
2891 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2894 char *psz_access = NULL;
2895 char *psz_demux = NULL;
2898 /* Either there is an access/demux specification before ://
2899 * or we have a plain local file path. */
2900 psz_path = strstr( psz_dup, "://" );
2901 if( psz_path != NULL )
2904 psz_path += 3; /* skips "://" */
2906 /* Separate access from demux (<access>/<demux>://<path>) */
2907 psz_access = psz_dup;
2908 psz_demux = strchr( psz_access, '/' );
2910 *psz_demux++ = '\0';
2912 /* We really don't want module name substitution here! */
2913 if( psz_access[0] == '$' )
2915 if( psz_demux && psz_demux[0] == '$' )
2922 *ppsz_access = psz_access ? psz_access : (char*)"";
2923 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2924 *ppsz_path = psz_path;
2927 static inline bool next(char ** src)
2931 long result = strtol( *src, &end, 0 );
2932 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
2941 /*****************************************************************************
2942 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2945 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2946 *****************************************************************************/
2947 static void MRLSections( input_thread_t *p_input, char *psz_source,
2948 int *pi_title_start, int *pi_title_end,
2949 int *pi_chapter_start, int *pi_chapter_end )
2951 char *psz, *psz_end, *psz_next, *psz_check;
2953 *pi_title_start = *pi_title_end = -1;
2954 *pi_chapter_start = *pi_chapter_end = -1;
2956 /* Start by parsing titles and chapters */
2957 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2960 /* Check we are really dealing with a title/chapter section */
2961 psz_check = psz + 1;
2962 if( !*psz_check ) return;
2963 if( isdigit(*psz_check) )
2964 if(!next(&psz_check)) return;
2965 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2966 if( *psz_check == ':' && ++psz_check )
2968 if( isdigit(*psz_check) )
2969 if(!next(&psz_check)) return;
2971 if( *psz_check != '-' && *psz_check ) return;
2972 if( *psz_check == '-' && ++psz_check )
2974 if( isdigit(*psz_check) )
2975 if(!next(&psz_check)) return;
2977 if( *psz_check != ':' && *psz_check ) return;
2978 if( *psz_check == ':' && ++psz_check )
2980 if( isdigit(*psz_check) )
2981 if(!next(&psz_check)) return;
2983 if( *psz_check ) return;
2985 /* Separate start and end */
2987 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2989 /* Look for the start title */
2990 *pi_title_start = strtol( psz, &psz_next, 0 );
2991 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2992 *pi_title_end = *pi_title_start;
2995 /* Look for the start chapter */
2997 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2998 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2999 *pi_chapter_end = *pi_chapter_start;
3003 /* Look for the end title */
3004 *pi_title_end = strtol( psz_end, &psz_next, 0 );
3005 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
3008 /* Look for the end chapter */
3009 if( *psz_end ) psz_end++;
3010 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
3011 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
3014 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
3015 psz_source, *pi_title_start, *pi_chapter_start,
3016 *pi_title_end, *pi_chapter_end );
3019 /*****************************************************************************
3020 * input_AddSubtitles: add a subtitles file and enable it
3021 *****************************************************************************/
3022 static void SubtitleAdd( input_thread_t *p_input, char *psz_subtitle, bool b_forced )
3024 input_source_t *sub;
3027 char *psz_path, *psz_extension;
3029 /* if we are provided a subtitle.sub file,
3030 * see if we don't have a subtitle.idx and use it instead */
3031 psz_path = strdup( psz_subtitle );
3034 psz_extension = strrchr( psz_path, '.');
3035 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
3039 strcpy( psz_extension, ".idx" );
3041 if( !utf8_stat( psz_path, &st ) && S_ISREG( st.st_mode ) )
3043 msg_Dbg( p_input, "using %s subtitles file instead of %s",
3044 psz_path, psz_subtitle );
3045 strcpy( psz_subtitle, psz_path );
3051 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
3053 sub = InputSourceNew( p_input );
3054 if( InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
3059 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
3062 if( b_forced && !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
3064 if( count.i_int == 0 )
3066 /* if it was first one, there is disable too */
3068 if( count.i_int < list.p_list->i_count )
3070 int i_id = list.p_list->p_values[count.i_int].i_int;
3071 es_out_id_t *p_es = input_EsOutGetFromID( p_input->p->p_es_out, i_id );
3073 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_DEFAULT, p_es );
3074 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES, p_es );
3076 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
3080 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
3081 bool b_check_extension )
3085 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
3088 assert( psz_subtitle != NULL );
3090 val.psz_string = strdup( psz_subtitle );
3091 if( val.psz_string )
3092 input_ControlPush( p_input, INPUT_CONTROL_ADD_SUBTITLE, &val );
3096 /*****************************************************************************
3097 * input_get_event_manager
3098 *****************************************************************************/
3099 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
3101 return &p_input->p->event_manager;
3105 /* TODO FIXME nearly the same logic that snapshot code */
3106 char *input_CreateFilename( vlc_object_t *p_obj, const char *psz_path, const char *psz_prefix, const char *psz_extension )
3111 path = utf8_opendir( psz_path );
3116 char *psz_tmp = str_format( p_obj, psz_prefix );
3120 filename_sanitize( psz_tmp );
3121 if( asprintf( &psz_file, "%s"DIR_SEP"%s%s%s",
3123 psz_extension ? "." : "",
3124 psz_extension ? psz_extension : "" ) < 0 )
3131 psz_file = str_format( p_obj, psz_path );
3132 path_sanitize( psz_file );