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>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 static void Destructor( input_thread_t * p_input );
56 static int Run ( input_thread_t *p_input );
57 static int RunAndDestroy ( input_thread_t *p_input );
59 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
60 const char *, bool, sout_instance_t * );
61 static int Init ( input_thread_t *p_input );
62 static void Error ( input_thread_t *p_input );
63 static void End ( input_thread_t *p_input );
64 static void MainLoop( input_thread_t *p_input );
66 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
67 static void ControlReduce( input_thread_t * );
68 static bool Control( input_thread_t *, int, vlc_value_t );
70 static int UpdateFromAccess( input_thread_t * );
71 static int UpdateFromDemux( input_thread_t * );
73 static void UpdateItemLength( input_thread_t *, int64_t i_length );
75 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
77 static input_source_t *InputSourceNew( input_thread_t *);
78 static int InputSourceInit( input_thread_t *, input_source_t *,
79 const char *, const char *psz_forced_demux );
80 static void InputSourceClean( input_source_t * );
82 //static void InputGetAttachments( input_thread_t *, input_source_t * );
83 static void SlaveDemux( input_thread_t *p_input );
84 static void SlaveSeek( input_thread_t *p_input );
86 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
87 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
89 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
90 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
91 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
92 int i_new, input_attachment_t **pp_new );
94 /*****************************************************************************
95 * This function creates a new input, and returns a pointer
96 * to its description. On error, it returns NULL.
98 * Variables for _public_ use:
101 * - rate,rate-slower, rate-faster
102 * - position, position-offset
103 * - time, time-offset
104 * - title,title-next,title-prev
105 * - chapter,chapter-next, chapter-prev
106 * - program, audio-es, video-es, spu-es
107 * - audio-delay, spu-delay
112 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
113 * or not, for that check position != 0.0)
115 * * For intf callback upon changes
117 * - rate-change for when playback rate changes
118 * TODO explain when Callback is called
119 * TODO complete this list (?)
120 *****************************************************************************/
121 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
122 const char *psz_header, bool b_quick,
123 sout_instance_t *p_sout )
125 static const char input_name[] = "input";
126 input_thread_t *p_input = NULL; /* thread descriptor */
130 /* Allocate descriptor */
131 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
132 VLC_OBJECT_INPUT, input_name );
133 if( p_input == NULL )
136 /* Construct a nice name for the input timer */
137 char psz_timer_name[255];
138 char * psz_name = input_item_GetName( p_item );
139 snprintf( psz_timer_name, sizeof(psz_timer_name),
140 "input launching for '%s'", psz_name );
143 /* Start a timer to mesure how long it takes
144 * to launch an input */
145 stats_TimerStart( p_input, psz_timer_name,
146 STATS_TIMER_INPUT_LAUNCHING );
148 MALLOC_NULL( p_input->p, input_thread_private_t );
149 memset( p_input->p, 0, sizeof( input_thread_private_t ) );
151 /* One "randomly" selected input thread is responsible for computing
152 * the global stats. Check if there is already someone doing this */
153 if( p_input->p_libvlc->p_stats && !b_quick )
155 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
156 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
157 if( priv->p_stats_computer == NULL )
158 priv->p_stats_computer = p_input;
159 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
162 p_input->b_preparsing = b_quick;
163 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
166 vlc_event_manager_t * p_em = &p_input->p->event_manager;
167 vlc_event_manager_init_with_vlc_object( p_em, p_input );
168 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
169 vlc_event_manager_register_event_type( p_em, vlc_InputSelectedStreamChanged );
171 /* Init Common fields */
172 p_input->b_eof = false;
173 p_input->b_can_pace_control = true;
174 p_input->p->i_start = 0;
176 p_input->p->i_stop = 0;
177 p_input->p->i_run = 0;
178 p_input->p->i_title = 0;
179 p_input->p->title = NULL;
180 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
181 p_input->i_state = INIT_S;
182 p_input->p->i_rate = INPUT_RATE_DEFAULT;
183 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
184 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
185 p_input->p->p_es_out = NULL;
186 p_input->p->p_sout = NULL;
187 p_input->p->b_out_pace_control = false;
188 p_input->i_pts_delay = 0;
190 /* Init Input fields */
191 vlc_gc_incref( p_item ); /* Released in Destructor() */
192 p_input->p->input.p_item = p_item;
193 p_input->p->input.p_access = NULL;
194 p_input->p->input.p_stream = NULL;
195 p_input->p->input.p_demux = NULL;
196 p_input->p->input.b_title_demux = false;
197 p_input->p->input.i_title = 0;
198 p_input->p->input.title = NULL;
199 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
200 p_input->p->input.b_can_pace_control = true;
201 p_input->p->input.b_can_rate_control = true;
202 p_input->p->input.b_rescale_ts = true;
203 p_input->p->input.b_eof = false;
204 p_input->p->input.i_cr_average = 0;
206 vlc_mutex_lock( &p_item->lock );
208 if( !p_item->p_stats )
209 p_item->p_stats = stats_NewInputStats( p_input );
210 vlc_mutex_unlock( &p_item->lock );
213 p_input->p->i_slave = 0;
214 p_input->p->slave = NULL;
216 /* Init control buffer */
217 vlc_mutex_init( &p_input->p->lock_control );
218 p_input->p->i_control = 0;
220 /* Parse input options */
221 vlc_mutex_lock( &p_item->lock );
222 assert( p_item->optflagc == p_item->i_options );
223 for( i = 0; i < p_item->i_options; i++ )
224 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
225 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
226 vlc_mutex_unlock( &p_item->lock );
228 /* Create Object Variables for private use only */
229 input_ConfigVarInit( p_input );
231 /* Create Objects variables for public Get and Set */
232 input_ControlVarInit( p_input );
234 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
236 if( !p_input->b_preparsing )
238 var_Get( p_input, "bookmarks", &val );
241 /* FIXME: have a common cfg parsing routine used by sout and others */
242 char *psz_parser, *psz_start, *psz_end;
243 psz_parser = val.psz_string;
244 while( (psz_start = strchr( psz_parser, '{' ) ) )
246 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
249 psz_end = strchr( psz_start, '}' );
250 if( !psz_end ) break;
251 psz_parser = psz_end + 1;
252 backup = *psz_parser;
255 while( (psz_end = strchr( psz_start, ',' ) ) )
258 if( !strncmp( psz_start, "name=", 5 ) )
260 p_seekpoint->psz_name = strdup(psz_start + 5);
262 else if( !strncmp( psz_start, "bytes=", 6 ) )
264 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
266 else if( !strncmp( psz_start, "time=", 5 ) )
268 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
271 psz_start = psz_end + 1;
273 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
274 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
275 p_seekpoint->i_time_offset );
276 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
277 vlc_seekpoint_Delete( p_seekpoint );
278 *psz_parser = backup;
280 free( val.psz_string );
284 /* Remove 'Now playing' info as it is probably outdated */
285 input_item_SetNowPlaying( p_item, NULL );
288 if( p_input->b_preparsing )
289 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
293 p_input->p->p_sout = p_sout;
295 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
296 vlc_mutex_init( &p_input->p->counters.counters_lock );
298 /* Attach only once we are ready */
299 vlc_object_attach( p_input, p_parent );
301 /* Set the destructor when we are sure we are initialized */
302 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
308 * Input destructor (called when the object's refcount reaches 0).
310 static void Destructor( input_thread_t * p_input )
312 input_thread_private_t *priv = p_input->p;
314 vlc_event_manager_fini( &p_input->p->event_manager );
316 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
317 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
320 sout_DeleteInstance( priv->p_sout );
322 vlc_gc_decref( p_input->p->input.p_item );
324 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
326 vlc_mutex_destroy( &priv->lock_control );
331 * Initialize an input thread and run it. You will need to monitor the
332 * thread to clean up after it is done
334 * \param p_parent a vlc_object
335 * \param p_item an input item
336 * \return a pointer to the spawned input thread
338 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
339 input_item_t *p_item )
341 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
345 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
346 input_item_t *p_item,
347 const char *psz_log, sout_instance_t *p_sout )
349 input_thread_t *p_input;
351 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
355 /* Create thread and wait for its readiness. */
356 if( vlc_thread_create( p_input, "input", Run,
357 VLC_THREAD_PRIORITY_INPUT, true ) )
359 input_ChangeState( p_input, ERROR_S );
360 msg_Err( p_input, "cannot create input thread" );
361 vlc_object_detach( p_input );
362 vlc_object_release( p_input );
370 * Initialize an input thread and run it. This thread will clean after itself,
371 * you can forget about it. It can work either in blocking or non-blocking mode
373 * \param p_parent a vlc_object
374 * \param p_item an input item
375 * \param b_block should we block until read is finished ?
376 * \return the input object id if non blocking, an error code else
378 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
381 input_thread_t *p_input;
383 p_input = Create( p_parent, p_item, NULL, false, NULL );
389 RunAndDestroy( p_input );
394 if( vlc_thread_create( p_input, "input", RunAndDestroy,
395 VLC_THREAD_PRIORITY_INPUT, true ) )
397 input_ChangeState( p_input, ERROR_S );
398 msg_Err( p_input, "cannot create input thread" );
399 vlc_object_release( p_input );
403 return p_input->i_object_id;
407 * Initialize an input and initialize it to preparse the item
408 * This function is blocking. It will only accept to parse files
410 * \param p_parent a vlc_object_t
411 * \param p_item an input item
412 * \return VLC_SUCCESS or an error
414 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
416 input_thread_t *p_input;
418 /* Allocate descriptor */
419 p_input = Create( p_parent, p_item, NULL, true, NULL );
423 if( !Init( p_input ) )
426 vlc_object_detach( p_input );
427 vlc_object_release( p_input );
433 * Request a running input thread to stop and die
435 * \param the input thread to stop
437 void input_StopThread( input_thread_t *p_input )
442 /* Set die for input */
443 vlc_object_kill( p_input );
444 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
446 /* We cannot touch p_input fields directly (we come from another thread),
447 * so use the vlc_object_find way, it's perfectly safe */
449 /* Set die for all access, stream, demux, etc */
450 p_list = vlc_list_children( p_input );
451 for( i = 0; i < p_list->i_count; i++ )
452 vlc_object_kill( p_list->p_values[i].p_object );
453 vlc_list_release( p_list );
455 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
458 sout_instance_t * input_DetachSout( input_thread_t *p_input )
460 sout_instance_t *p_sout = p_input->p->p_sout;
461 vlc_object_detach( p_sout );
462 p_input->p->p_sout = NULL;
466 /*****************************************************************************
467 * Run: main thread loop
468 * This is the "normal" thread that spawns the input processing chain,
469 * reads the stream, cleans up and waits
470 *****************************************************************************/
471 static int Run( input_thread_t *p_input )
473 /* Signal that the thread is launched */
474 vlc_thread_ready( p_input );
476 if( Init( p_input ) )
478 /* If we failed, wait before we are killed, and exit */
479 p_input->b_error = true;
483 /* Tell we're dead */
484 p_input->b_dead = true;
491 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
493 /* We have finish to demux data but not to play them */
494 while( !p_input->b_die )
496 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
499 msg_Dbg( p_input, "waiting decoder fifos to empty" );
501 msleep( INPUT_IDLE_SLEEP );
504 /* We have finished */
505 p_input->b_eof = true;
506 input_ChangeState( p_input, END_S );
509 /* Wait until we are asked to die */
510 if( !p_input->b_die )
521 /*****************************************************************************
522 * RunAndDestroy: main thread loop
523 * This is the "just forget me" thread that spawns the input processing chain,
524 * reads the stream, cleans up and releases memory
525 *****************************************************************************/
526 static int RunAndDestroy( input_thread_t *p_input )
528 /* Signal that the thread is launched */
529 vlc_thread_ready( p_input );
531 if( Init( p_input ) )
536 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
538 /* We have finished demuxing data but not playing it */
539 while( !p_input->b_die )
541 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
544 msg_Dbg( p_input, "waiting decoder fifos to empty" );
546 msleep( INPUT_IDLE_SLEEP );
549 /* We have finished */
550 p_input->b_eof = true;
558 vlc_object_release( p_input );
562 /*****************************************************************************
563 * Main loop: Fill buffers from access, and demux
564 *****************************************************************************/
565 static void MainLoop( input_thread_t *p_input )
567 int64_t i_start_mdate = mdate();
568 int64_t i_intf_update = 0;
572 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
574 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
576 bool b_force_update = false;
582 if( p_input->i_state != PAUSE_S )
584 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
585 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
588 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
593 if( p_input->p->input.b_title_demux &&
594 p_input->p->input.p_demux->info.i_update )
596 i_ret = UpdateFromDemux( p_input );
597 b_force_update = true;
599 else if( !p_input->p->input.b_title_demux &&
600 p_input->p->input.p_access &&
601 p_input->p->input.p_access->info.i_update )
603 i_ret = UpdateFromAccess( p_input );
604 b_force_update = true;
608 if( i_ret == 0 ) /* EOF */
612 var_Get( p_input, "input-repeat", &repeat );
613 if( repeat.i_int == 0 )
615 /* End of file - we do not set b_die because only the
616 * playlist is allowed to do so. */
617 msg_Dbg( p_input, "EOF reached" );
618 p_input->p->input.b_eof = true;
622 msg_Dbg( p_input, "repeating the same input (%d)",
624 if( repeat.i_int > 0 )
627 var_Set( p_input, "input-repeat", repeat );
630 /* Seek to start title/seekpoint */
631 val.i_int = p_input->p->input.i_title_start -
632 p_input->p->input.i_title_offset;
633 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
635 input_ControlPush( p_input,
636 INPUT_CONTROL_SET_TITLE, &val );
638 val.i_int = p_input->p->input.i_seekpoint_start -
639 p_input->p->input.i_seekpoint_offset;
640 if( val.i_int > 0 /* TODO: check upper boundary */ )
641 input_ControlPush( p_input,
642 INPUT_CONTROL_SET_SEEKPOINT, &val );
644 /* Seek to start position */
645 if( p_input->p->i_start > 0 )
647 val.i_time = p_input->p->i_start;
648 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
654 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
659 i_start_mdate = mdate();
664 p_input->b_error = true;
667 if( i_ret > 0 && p_input->p->i_slave > 0 )
669 SlaveDemux( p_input );
679 vlc_mutex_lock( &p_input->p->lock_control );
680 ControlReduce( p_input );
681 while( !ControlPopNoLock( p_input, &i_type, &val ) )
683 msg_Dbg( p_input, "control type=%d", i_type );
684 if( Control( p_input, i_type, val ) )
685 b_force_update = true;
687 vlc_mutex_unlock( &p_input->p->lock_control );
689 if( b_force_update || i_intf_update < mdate() )
693 int64_t i_time, i_length;
694 /* update input status variables */
695 if( !demux_Control( p_input->p->input.p_demux,
696 DEMUX_GET_POSITION, &f_pos ) )
698 val.f_float = (float)f_pos;
699 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
701 if( !demux_Control( p_input->p->input.p_demux,
702 DEMUX_GET_TIME, &i_time ) )
704 p_input->i_time = i_time;
706 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
708 if( !demux_Control( p_input->p->input.p_demux,
709 DEMUX_GET_LENGTH, &i_length ) )
712 var_Get( p_input, "length", &old_val );
713 val.i_time = i_length;
714 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
716 if( old_val.i_time != val.i_time )
718 UpdateItemLength( p_input, i_length );
722 var_SetBool( p_input, "intf-change", true );
723 i_intf_update = mdate() + INT64_C(150000);
725 /* 150ms * 8 = ~ 1 second */
726 if( ++i_updates % 8 == 0 )
728 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
729 /* Are we the thread responsible for computing global stats ? */
730 if( libvlc_priv (p_input->p_libvlc)->p_stats_computer == p_input )
732 stats_ComputeGlobalStats( p_input->p_libvlc,
733 p_input->p_libvlc->p_stats );
739 static void InitStatistics( input_thread_t * p_input )
741 if( p_input->b_preparsing ) return;
743 /* Prepare statistics */
744 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
745 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
746 if( libvlc_stats (p_input) )
748 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
749 INIT_COUNTER( read_packets, INTEGER, COUNTER );
750 INIT_COUNTER( demux_read, INTEGER, COUNTER );
751 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
752 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
753 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
754 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
755 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
756 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
757 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
758 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
759 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
760 p_input->p->counters.p_sout_send_bitrate = NULL;
761 p_input->p->counters.p_sout_sent_packets = NULL;
762 p_input->p->counters.p_sout_sent_bytes = NULL;
763 if( p_input->p->counters.p_demux_bitrate )
764 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
765 if( p_input->p->counters.p_input_bitrate )
766 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
771 static int InitSout( input_thread_t * p_input )
775 if( p_input->b_preparsing ) return VLC_SUCCESS;
777 /* Find a usable sout and attach it to p_input */
778 psz = var_GetNonEmptyString( p_input, "sout" );
779 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
781 /* Check the validity of the provided sout */
782 if( p_input->p->p_sout )
784 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
786 msg_Dbg( p_input, "destroying unusable sout" );
788 sout_DeleteInstance( p_input->p->p_sout );
789 p_input->p->p_sout = NULL;
793 if( p_input->p->p_sout )
796 msg_Dbg( p_input, "sout keep: reusing sout" );
797 msg_Dbg( p_input, "sout keep: you probably want to use "
798 "gather stream_out" );
799 vlc_object_attach( p_input->p->p_sout, p_input );
803 /* Create a new one */
804 p_input->p->p_sout = sout_NewInstance( p_input, psz );
805 if( !p_input->p->p_sout )
807 input_ChangeState( p_input, ERROR_S );
808 msg_Err( p_input, "cannot start stream output instance, " \
814 if( libvlc_stats (p_input) )
816 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
817 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
818 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
819 if( p_input->p->counters.p_sout_send_bitrate )
820 p_input->p->counters.p_sout_send_bitrate->update_interval =
824 else if( p_input->p->p_sout )
826 msg_Dbg( p_input, "destroying useless sout" );
828 sout_DeleteInstance( p_input->p->p_sout );
829 p_input->p->p_sout = NULL;
837 static void InitTitle( input_thread_t * p_input )
841 if( p_input->b_preparsing ) return;
843 /* Create global title (from master) */
844 p_input->p->i_title = p_input->p->input.i_title;
845 p_input->p->title = p_input->p->input.title;
846 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
847 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
848 if( p_input->p->i_title > 0 )
850 /* Setup variables */
851 input_ControlVarNavigation( p_input );
852 input_ControlVarTitle( p_input, 0 );
856 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
857 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
858 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
861 if( p_input->i_pts_delay < 0 )
862 p_input->i_pts_delay = 0;
864 /* If the desynchronisation requested by the user is < 0, we need to
865 * cache more data. */
866 var_Get( p_input, "audio-desync", &val );
867 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
869 /* Update cr_average depending on the caching */
870 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
871 p_input->p->input.i_cr_average /= 10;
872 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
875 static void StartTitle( input_thread_t * p_input )
884 /* Start title/chapter */
886 if( p_input->b_preparsing )
888 p_input->p->i_start = 0;
892 val.i_int = p_input->p->input.i_title_start -
893 p_input->p->input.i_title_offset;
894 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
895 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
896 val.i_int = p_input->p->input.i_seekpoint_start -
897 p_input->p->input.i_seekpoint_offset;
898 if( val.i_int > 0 /* TODO: check upper boundary */ )
899 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
903 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
904 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
905 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
906 i_length = var_GetTime( p_input, "length" );
907 if( p_input->p->i_run < 0 )
909 msg_Warn( p_input, "invalid run-time ignored" );
910 p_input->p->i_run = 0;
913 if( p_input->p->i_start > 0 )
915 if( p_input->p->i_start >= i_length )
917 msg_Warn( p_input, "invalid start-time ignored" );
923 msg_Dbg( p_input, "starting at time: %ds",
924 (int)( p_input->p->i_start / INT64_C(1000000) ) );
926 s.i_time = p_input->p->i_start;
927 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
930 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
932 msg_Warn( p_input, "invalid stop-time ignored" );
933 p_input->p->i_stop = 0;
937 /* Get fps and set it if not already set */
938 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
941 float f_requested_fps;
943 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
944 var_SetFloat( p_input, "sub-original-fps", f_fps );
946 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
947 if( f_requested_fps != f_fps )
949 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
951 var_SetFloat( p_input, "sub-fps", f_requested_fps );
955 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
958 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
961 /* Look for and add subtitle files */
962 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
963 if( psz_subtitle != NULL )
965 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
966 input_AddSubtitles( p_input, psz_subtitle, false );
969 var_Get( p_input, "sub-autodetect-file", &val );
972 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
973 char **subs = subtitles_Detect( p_input, psz_autopath,
974 p_input->p->input.p_item->psz_uri );
977 if( psz_autopath == NULL )
978 psz_autopath = strdup("");
980 /* Try to autoselect the first autodetected subtitles file
981 * if no subtitles file was specified */
982 if( ( psz_subtitle == NULL ) && subs && subs[0] )
984 input_AddSubtitles( p_input, subs[0], false );
989 /* Then, just add the following subtitles files */
990 for( ; subs && subs[i]; i++ )
992 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
994 sub = InputSourceNew( p_input );
995 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
997 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1004 free( psz_autopath );
1006 free( psz_subtitle );
1008 /* Look for slave */
1009 psz = var_GetNonEmptyString( p_input, "input-slave" );
1013 input_source_t *slave;
1014 while( psz && *psz )
1016 while( *psz == ' ' || *psz == '#' )
1020 if( ( psz_delim = strchr( psz, '#' ) ) )
1022 *psz_delim++ = '\0';
1029 msg_Dbg( p_input, "adding slave input '%s'", psz );
1030 slave = InputSourceNew( p_input );
1031 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1033 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1042 static void InitPrograms( input_thread_t * p_input )
1047 if( p_input->b_preparsing ) return;
1050 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1051 i_es_out_mode = ES_OUT_MODE_AUTO;
1053 if( p_input->p->p_sout )
1055 var_Get( p_input, "sout-all", &val );
1058 i_es_out_mode = ES_OUT_MODE_ALL;
1063 var_Get( p_input, "programs", &val );
1064 if ( val.p_list && val.p_list->i_count )
1066 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1067 /* Note : we should remove the "program" callback. */
1070 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1074 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1076 /* Inform the demuxer about waited group (needed only for DVB) */
1077 if( i_es_out_mode == ES_OUT_MODE_ALL )
1079 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1081 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1083 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1088 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1089 (int) var_GetInteger( p_input, "program" ), NULL );
1093 static int Init( input_thread_t * p_input )
1099 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1101 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1103 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1104 var_SetString( p_input, "sout", "" );
1105 var_SetBool( p_input, "sout-all", false );
1106 var_SetString( p_input, "input-slave", "" );
1107 var_SetInteger( p_input, "input-repeat", 0 );
1108 var_SetString( p_input, "sub-file", "" );
1109 var_SetBool( p_input, "sub-autodetect-file", false );
1113 InitStatistics( p_input );
1115 ret = InitSout( p_input );
1116 if( ret != VLC_SUCCESS )
1117 return ret; /* FIXME: goto error; should be better here */
1121 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1122 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1123 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1125 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1126 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1128 if( InputSourceInit( p_input, &p_input->p->input,
1129 p_input->p->input.p_item->psz_uri, NULL ) )
1134 InitTitle( p_input );
1136 /* Load master infos */
1138 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1139 &val.i_time ) && val.i_time > 0 )
1141 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1142 UpdateItemLength( p_input, val.i_time );
1146 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1147 if( val.i_time > 0 )
1148 { /* fallback: gets length from metadata */
1149 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1150 UpdateItemLength( p_input, val.i_time );
1154 StartTitle( p_input );
1156 InitPrograms( p_input );
1158 if( !p_input->b_preparsing && p_input->p->p_sout )
1160 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1162 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1164 /* We don't want a high input priority here or we'll
1165 * end-up sucking up all the CPU time */
1166 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1169 msg_Dbg( p_input, "starting in %s mode",
1170 p_input->p->b_out_pace_control ? "async" : "sync" );
1173 p_meta = vlc_meta_New();
1175 /* Get meta data from users */
1176 InputMetaUser( p_input, p_meta );
1178 /* Get meta data from master input */
1179 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1181 /* Access_file does not give any meta, and there are no slave */
1182 AccessMeta( p_input, p_meta );
1184 InputUpdateMeta( p_input, p_meta );
1186 if( !p_input->b_preparsing )
1188 msg_Dbg( p_input, "`%s' successfully opened",
1189 p_input->p->input.p_item->psz_uri );
1193 /* initialization is complete */
1194 input_ChangeState( p_input, PLAYING_S );
1199 input_ChangeState( p_input, ERROR_S );
1201 if( p_input->p->p_es_out )
1202 input_EsOutDelete( p_input->p->p_es_out );
1204 if( p_input->p->p_sout )
1206 vlc_object_detach( p_input->p->p_sout );
1207 sout_DeleteInstance( p_input->p->p_sout );
1211 if( !p_input->b_preparsing && libvlc_stats (p_input) )
1213 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1214 stats_CounterClean( p_input->p->counters.p_##c );\
1215 p_input->p->counters.p_##c = NULL; } while(0)
1216 EXIT_COUNTER( read_bytes );
1217 EXIT_COUNTER( read_packets );
1218 EXIT_COUNTER( demux_read );
1219 EXIT_COUNTER( input_bitrate );
1220 EXIT_COUNTER( demux_bitrate );
1221 EXIT_COUNTER( played_abuffers );
1222 EXIT_COUNTER( lost_abuffers );
1223 EXIT_COUNTER( displayed_pictures );
1224 EXIT_COUNTER( lost_pictures );
1225 EXIT_COUNTER( decoded_audio );
1226 EXIT_COUNTER( decoded_video );
1227 EXIT_COUNTER( decoded_sub );
1229 if( p_input->p->p_sout )
1231 EXIT_COUNTER( sout_sent_packets );
1232 EXIT_COUNTER (sout_sent_bytes );
1233 EXIT_COUNTER( sout_send_bitrate );
1238 /* Mark them deleted */
1239 p_input->p->input.p_demux = NULL;
1240 p_input->p->input.p_stream = NULL;
1241 p_input->p->input.p_access = NULL;
1242 p_input->p->p_es_out = NULL;
1243 p_input->p->p_sout = NULL;
1245 return VLC_EGENERIC;
1248 /*****************************************************************************
1249 * Error: RunThread() error loop
1250 *****************************************************************************
1251 * This function is called when an error occurred during thread main's loop.
1252 *****************************************************************************/
1253 static void Error( input_thread_t *p_input )
1255 input_ChangeState( p_input, ERROR_S );
1256 while( !p_input->b_die )
1259 msleep( INPUT_IDLE_SLEEP );
1263 /*****************************************************************************
1264 * End: end the input thread
1265 *****************************************************************************/
1266 static void End( input_thread_t * p_input )
1270 /* We are at the end */
1271 input_ChangeState( p_input, END_S );
1273 /* Clean control variables */
1274 input_ControlVarClean( p_input );
1276 /* Clean up master */
1277 InputSourceClean( &p_input->p->input );
1280 for( i = 0; i < p_input->p->i_slave; i++ )
1282 InputSourceClean( p_input->p->slave[i] );
1283 free( p_input->p->slave[i] );
1285 free( p_input->p->slave );
1287 /* Unload all modules */
1288 if( p_input->p->p_es_out )
1289 input_EsOutDelete( p_input->p->p_es_out );
1291 if( !p_input->b_preparsing )
1293 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1294 if( libvlc_stats (p_input) )
1296 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
1298 /* make sure we are up to date */
1299 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1300 if( priv->p_stats_computer == p_input )
1302 stats_ComputeGlobalStats( p_input->p_libvlc,
1303 p_input->p_libvlc->p_stats );
1304 priv->p_stats_computer = NULL;
1306 CL_CO( read_bytes );
1307 CL_CO( read_packets );
1308 CL_CO( demux_read );
1309 CL_CO( input_bitrate );
1310 CL_CO( demux_bitrate );
1311 CL_CO( played_abuffers );
1312 CL_CO( lost_abuffers );
1313 CL_CO( displayed_pictures );
1314 CL_CO( lost_pictures );
1315 CL_CO( decoded_audio) ;
1316 CL_CO( decoded_video );
1317 CL_CO( decoded_sub) ;
1320 /* Close optional stream output instance */
1321 if( p_input->p->p_sout )
1323 CL_CO( sout_sent_packets );
1324 CL_CO( sout_sent_bytes );
1325 CL_CO( sout_send_bitrate );
1327 vlc_object_detach( p_input->p->p_sout );
1332 if( p_input->p->i_attachment > 0 )
1334 for( i = 0; i < p_input->p->i_attachment; i++ )
1335 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1336 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1339 /* Tell we're dead */
1340 p_input->b_dead = true;
1343 /*****************************************************************************
1345 *****************************************************************************/
1346 static inline int ControlPopNoLock( input_thread_t *p_input,
1347 int *pi_type, vlc_value_t *p_val )
1349 if( p_input->p->i_control <= 0 )
1351 return VLC_EGENERIC;
1354 *pi_type = p_input->p->control[0].i_type;
1355 *p_val = p_input->p->control[0].val;
1357 p_input->p->i_control--;
1358 if( p_input->p->i_control > 0 )
1362 for( i = 0; i < p_input->p->i_control; i++ )
1364 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1365 p_input->p->control[i].val = p_input->p->control[i+1].val;
1372 static void ControlReduce( input_thread_t *p_input )
1379 for( i = 1; i < p_input->p->i_control; i++ )
1381 const int i_lt = p_input->p->control[i-1].i_type;
1382 const int i_ct = p_input->p->control[i].i_type;
1384 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1385 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1389 ( i_ct == INPUT_CONTROL_SET_STATE ||
1390 i_ct == INPUT_CONTROL_SET_RATE ||
1391 i_ct == INPUT_CONTROL_SET_POSITION ||
1392 i_ct == INPUT_CONTROL_SET_TIME ||
1393 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1394 i_ct == INPUT_CONTROL_SET_TITLE ||
1395 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1396 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1399 // msg_Dbg( p_input, "merged at %d", i );
1400 /* Remove the i-1 */
1401 for( j = i; j < p_input->p->i_control; j++ )
1402 p_input->p->control[j-1] = p_input->p->control[j];
1403 p_input->p->i_control--;
1407 /* TODO but that's not that important
1408 - merge SET_X with SET_X_CMD
1409 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1410 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1417 static bool Control( input_thread_t *p_input, int i_type,
1420 bool b_force_update = false;
1422 if( !p_input ) return b_force_update;
1426 case INPUT_CONTROL_SET_DIE:
1427 msg_Dbg( p_input, "control: stopping input" );
1428 /* Mark all submodules to die */
1429 if( p_input->p->input.p_access )
1430 vlc_object_kill( p_input->p->input.p_access );
1431 if( p_input->p->input.p_stream )
1432 vlc_object_kill( p_input->p->input.p_stream );
1433 vlc_object_kill( p_input->p->input.p_demux );
1435 vlc_object_kill( p_input );
1438 case INPUT_CONTROL_SET_POSITION:
1439 case INPUT_CONTROL_SET_POSITION_OFFSET:
1442 if( i_type == INPUT_CONTROL_SET_POSITION )
1444 f_pos = val.f_float;
1448 /* Should not fail */
1449 demux_Control( p_input->p->input.p_demux,
1450 DEMUX_GET_POSITION, &f_pos );
1451 f_pos += val.f_float;
1453 if( f_pos < 0.0 ) f_pos = 0.0;
1454 if( f_pos > 1.0 ) f_pos = 1.0;
1455 /* Reset the decoders states and clock sync (before calling the demuxer */
1456 input_EsOutChangePosition( p_input->p->p_es_out );
1457 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1460 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1461 "%2.1f%% failed", f_pos * 100 );
1465 if( p_input->p->i_slave > 0 )
1466 SlaveSeek( p_input );
1468 b_force_update = true;
1473 case INPUT_CONTROL_SET_TIME:
1474 case INPUT_CONTROL_SET_TIME_OFFSET:
1479 if( i_type == INPUT_CONTROL_SET_TIME )
1481 i_time = val.i_time;
1485 /* Should not fail */
1486 demux_Control( p_input->p->input.p_demux,
1487 DEMUX_GET_TIME, &i_time );
1488 i_time += val.i_time;
1490 if( i_time < 0 ) i_time = 0;
1492 /* Reset the decoders states and clock sync (before calling the demuxer */
1493 input_EsOutChangePosition( p_input->p->p_es_out );
1495 i_ret = demux_Control( p_input->p->input.p_demux,
1496 DEMUX_SET_TIME, i_time );
1501 /* Emulate it with a SET_POS */
1502 demux_Control( p_input->p->input.p_demux,
1503 DEMUX_GET_LENGTH, &i_length );
1506 double f_pos = (double)i_time / (double)i_length;
1507 i_ret = demux_Control( p_input->p->input.p_demux,
1508 DEMUX_SET_POSITION, f_pos );
1513 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1514 " failed or not possible", i_time );
1518 if( p_input->p->i_slave > 0 )
1519 SlaveSeek( p_input );
1521 b_force_update = true;
1526 case INPUT_CONTROL_SET_STATE:
1527 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1528 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1531 if( p_input->p->input.p_access )
1532 i_ret = access_Control( p_input->p->input.p_access,
1533 ACCESS_SET_PAUSE_STATE, false );
1535 i_ret = demux_Control( p_input->p->input.p_demux,
1536 DEMUX_SET_PAUSE_STATE, false );
1540 /* FIXME What to do ? */
1541 msg_Warn( p_input, "cannot unset pause -> EOF" );
1542 vlc_mutex_unlock( &p_input->p->lock_control );
1543 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1544 vlc_mutex_lock( &p_input->p->lock_control );
1547 b_force_update = true;
1549 /* Switch to play */
1550 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1554 input_EsOutChangeState( p_input->p->p_es_out );
1556 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1557 p_input->p->b_can_pause )
1560 if( p_input->p->input.p_access )
1561 i_ret = access_Control( p_input->p->input.p_access,
1562 ACCESS_SET_PAUSE_STATE, true );
1564 i_ret = demux_Control( p_input->p->input.p_demux,
1565 DEMUX_SET_PAUSE_STATE, true );
1567 b_force_update = true;
1571 msg_Warn( p_input, "cannot set pause state" );
1572 state = p_input->i_state;
1579 /* Switch to new state */
1580 input_ChangeStateWithVarCallback( p_input, state, false );
1584 input_EsOutChangeState( p_input->p->p_es_out );
1586 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1588 b_force_update = true;
1590 /* Correct "state" value */
1591 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1593 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1595 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1599 case INPUT_CONTROL_SET_RATE:
1600 case INPUT_CONTROL_SET_RATE_SLOWER:
1601 case INPUT_CONTROL_SET_RATE_FASTER:
1605 if( i_type == INPUT_CONTROL_SET_RATE )
1611 static const int ppi_factor[][2] = {
1612 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1614 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1623 for( i = 0; ppi_factor[i][0] != 0; i++ )
1625 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1626 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1627 if( i_test_e < i_error )
1633 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1635 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1637 if( ppi_factor[i_idx+1][0] > 0 )
1638 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1640 i_rate = INPUT_RATE_MAX+1;
1644 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1646 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1648 i_rate = INPUT_RATE_MIN-1;
1652 if( i_rate < INPUT_RATE_MIN )
1654 msg_Dbg( p_input, "cannot set rate faster" );
1655 i_rate = INPUT_RATE_MIN;
1657 else if( i_rate > INPUT_RATE_MAX )
1659 msg_Dbg( p_input, "cannot set rate slower" );
1660 i_rate = INPUT_RATE_MAX;
1662 if( i_rate != INPUT_RATE_DEFAULT &&
1663 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1664 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1666 msg_Dbg( p_input, "cannot change rate" );
1667 i_rate = INPUT_RATE_DEFAULT;
1669 if( i_rate != p_input->p->i_rate &&
1670 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1673 if( p_input->p->input.p_access )
1674 i_ret = VLC_EGENERIC;
1676 i_ret = demux_Control( p_input->p->input.p_demux,
1677 DEMUX_SET_RATE, &i_rate );
1680 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1681 i_rate = p_input->p->i_rate;
1686 if( i_rate != p_input->p->i_rate )
1689 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1690 var_SetBool( p_input, "rate-change", true );
1692 p_input->p->i_rate = i_rate;
1694 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1695 if( p_input->p->input.b_rescale_ts )
1696 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1698 b_force_update = true;
1703 case INPUT_CONTROL_SET_PROGRAM:
1704 /* No need to force update, es_out does it if needed */
1705 es_out_Control( p_input->p->p_es_out,
1706 ES_OUT_SET_GROUP, val.i_int );
1708 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1712 case INPUT_CONTROL_SET_ES:
1713 /* No need to force update, es_out does it if needed */
1714 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1715 input_EsOutGetFromID( p_input->p->p_es_out,
1719 case INPUT_CONTROL_SET_AUDIO_DELAY:
1720 input_EsOutSetDelay( p_input->p->p_es_out,
1721 AUDIO_ES, val.i_time );
1722 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1725 case INPUT_CONTROL_SET_SPU_DELAY:
1726 input_EsOutSetDelay( p_input->p->p_es_out,
1727 SPU_ES, val.i_time );
1728 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1731 case INPUT_CONTROL_SET_TITLE:
1732 case INPUT_CONTROL_SET_TITLE_NEXT:
1733 case INPUT_CONTROL_SET_TITLE_PREV:
1734 if( p_input->p->input.b_title_demux &&
1735 p_input->p->input.i_title > 0 )
1738 /* FIXME handle demux title */
1739 demux_t *p_demux = p_input->p->input.p_demux;
1742 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1743 i_title = p_demux->info.i_title - 1;
1744 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1745 i_title = p_demux->info.i_title + 1;
1747 i_title = val.i_int;
1749 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1751 input_EsOutChangePosition( p_input->p->p_es_out );
1753 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1754 input_ControlVarTitle( p_input, i_title );
1757 else if( p_input->p->input.i_title > 0 )
1759 access_t *p_access = p_input->p->input.p_access;
1762 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1763 i_title = p_access->info.i_title - 1;
1764 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1765 i_title = p_access->info.i_title + 1;
1767 i_title = val.i_int;
1769 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1771 input_EsOutChangePosition( p_input->p->p_es_out );
1773 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1774 stream_AccessReset( p_input->p->input.p_stream );
1778 case INPUT_CONTROL_SET_SEEKPOINT:
1779 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1780 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1781 if( p_input->p->input.b_title_demux &&
1782 p_input->p->input.i_title > 0 )
1784 demux_t *p_demux = p_input->p->input.p_demux;
1786 int64_t i_input_time;
1787 int64_t i_seekpoint_time;
1789 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1791 i_seekpoint = p_demux->info.i_seekpoint;
1792 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1793 if( i_seekpoint_time >= 0 &&
1794 !demux_Control( p_demux,
1795 DEMUX_GET_TIME, &i_input_time ) )
1797 if ( i_input_time < i_seekpoint_time + 3000000 )
1803 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1804 i_seekpoint = p_demux->info.i_seekpoint + 1;
1806 i_seekpoint = val.i_int;
1808 if( i_seekpoint >= 0 && i_seekpoint <
1809 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1812 input_EsOutChangePosition( p_input->p->p_es_out );
1814 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1817 else if( p_input->p->input.i_title > 0 )
1819 demux_t *p_demux = p_input->p->input.p_demux;
1820 access_t *p_access = p_input->p->input.p_access;
1822 int64_t i_input_time;
1823 int64_t i_seekpoint_time;
1825 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1827 i_seekpoint = p_access->info.i_seekpoint;
1828 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1829 if( i_seekpoint_time >= 0 &&
1830 demux_Control( p_demux,
1831 DEMUX_GET_TIME, &i_input_time ) )
1833 if ( i_input_time < i_seekpoint_time + 3000000 )
1839 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1840 i_seekpoint = p_access->info.i_seekpoint + 1;
1842 i_seekpoint = val.i_int;
1844 if( i_seekpoint >= 0 && i_seekpoint <
1845 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1847 input_EsOutChangePosition( p_input->p->p_es_out );
1849 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1851 stream_AccessReset( p_input->p->input.p_stream );
1856 case INPUT_CONTROL_ADD_SLAVE:
1857 if( val.psz_string )
1859 input_source_t *slave = InputSourceNew( p_input );
1861 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1867 msg_Dbg( p_input, "adding %s as slave on the fly",
1871 if( demux_Control( p_input->p->input.p_demux,
1872 DEMUX_GET_TIME, &i_time ) )
1874 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1875 InputSourceClean( slave );
1879 if( demux_Control( slave->p_demux,
1880 DEMUX_SET_TIME, i_time ) )
1882 msg_Err( p_input, "seek failed for new slave" );
1883 InputSourceClean( slave );
1888 /* Get meta (access and demux) */
1889 p_meta = vlc_meta_New();
1890 access_Control( slave->p_access, ACCESS_GET_META,
1892 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1893 InputUpdateMeta( p_input, p_meta );
1895 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1900 msg_Warn( p_input, "failed to add %s as slave",
1904 free( val.psz_string );
1908 case INPUT_CONTROL_SET_BOOKMARK:
1910 msg_Err( p_input, "not yet implemented" );
1914 return b_force_update;
1917 /*****************************************************************************
1919 *****************************************************************************/
1920 static int UpdateFromDemux( input_thread_t *p_input )
1922 demux_t *p_demux = p_input->p->input.p_demux;
1925 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1927 v.i_int = p_demux->info.i_title;
1928 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1930 input_ControlVarTitle( p_input, p_demux->info.i_title );
1932 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1934 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1936 v.i_int = p_demux->info.i_seekpoint;
1937 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1939 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1941 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1943 /* Hmmm only works with master input */
1944 if( p_input->p->input.p_demux == p_demux )
1946 int i_title_end = p_input->p->input.i_title_end -
1947 p_input->p->input.i_title_offset;
1948 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1949 p_input->p->input.i_seekpoint_offset;
1951 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1953 if( p_demux->info.i_title > i_title_end ||
1954 ( p_demux->info.i_title == i_title_end &&
1955 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1957 else if( i_seekpoint_end >=0 )
1959 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1961 else if( i_title_end >= 0 )
1963 if( p_demux->info.i_title > i_title_end ) return 0;
1970 /*****************************************************************************
1972 *****************************************************************************/
1973 static int UpdateFromAccess( input_thread_t *p_input )
1975 access_t *p_access = p_input->p->input.p_access;
1978 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1980 v.i_int = p_access->info.i_title;
1981 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1983 input_ControlVarTitle( p_input, p_access->info.i_title );
1985 stream_AccessUpdate( p_input->p->input.p_stream );
1987 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1989 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1991 v.i_int = p_access->info.i_seekpoint;
1992 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1993 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1995 if( p_access->info.i_update & INPUT_UPDATE_META )
1997 /* TODO maybe multi - access ? */
1998 vlc_meta_t *p_meta = vlc_meta_New();
1999 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2000 InputUpdateMeta( p_input, p_meta );
2001 p_access->info.i_update &= ~INPUT_UPDATE_META;
2004 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2006 /* Hmmm only works with master input */
2007 if( p_input->p->input.p_access == p_access )
2009 int i_title_end = p_input->p->input.i_title_end -
2010 p_input->p->input.i_title_offset;
2011 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2012 p_input->p->input.i_seekpoint_offset;
2014 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2016 if( p_access->info.i_title > i_title_end ||
2017 ( p_access->info.i_title == i_title_end &&
2018 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2020 else if( i_seekpoint_end >=0 )
2022 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2024 else if( i_title_end >= 0 )
2026 if( p_access->info.i_title > i_title_end ) return 0;
2033 /*****************************************************************************
2035 *****************************************************************************/
2036 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2038 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2041 /*****************************************************************************
2043 *****************************************************************************/
2044 static input_source_t *InputSourceNew( input_thread_t *p_input )
2046 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2048 memset( in, 0, sizeof( input_source_t ) );
2052 /*****************************************************************************
2054 *****************************************************************************/
2055 static int InputSourceInit( input_thread_t *p_input,
2056 input_source_t *in, const char *psz_mrl,
2057 const char *psz_forced_demux )
2059 char psz_dup[strlen (psz_mrl) + 1];
2060 const char *psz_access;
2061 const char *psz_demux;
2068 strcpy( psz_dup, psz_mrl );
2070 if( !in ) return VLC_EGENERIC;
2071 if( !p_input ) return VLC_EGENERIC;
2074 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2076 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2077 psz_mrl, psz_access, psz_demux, psz_path );
2078 if( !p_input->b_preparsing )
2080 /* Hack to allow udp://@:port syntax */
2082 (strncmp( psz_access, "udp", 3 ) &&
2083 strncmp( psz_access, "rtp", 3 )) )
2085 /* Find optional titles and seekpoints */
2086 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2087 &in->i_seekpoint_start, &in->i_seekpoint_end );
2090 if( psz_forced_demux && *psz_forced_demux )
2092 psz_demux = psz_forced_demux;
2094 else if( *psz_demux == '\0' )
2096 /* special hack for forcing a demuxer with --demux=module
2097 * (and do nothing with a list) */
2098 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2100 if( psz_var_demux != NULL &&
2101 !strchr(psz_var_demux, ',' ) &&
2102 !strchr(psz_var_demux, ':' ) )
2104 psz_demux = psz_var_demux;
2106 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2110 /* Try access_demux if no demux given */
2111 if( *psz_demux == '\0' )
2113 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2114 NULL, p_input->p->p_es_out, false );
2119 /* Preparsing is only for file:// */
2122 if( !*psz_access ) /* path without scheme:// */
2123 psz_access = "file";
2124 if( strcmp( psz_access, "file" ) )
2126 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2131 int64_t i_pts_delay;
2133 /* Get infos from access_demux */
2134 demux_Control( in->p_demux,
2135 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2136 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2138 in->b_title_demux = true;
2139 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2140 &in->title, &in->i_title,
2141 &in->i_title_offset, &in->i_seekpoint_offset ) )
2146 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2147 &in->b_can_pace_control ) )
2148 in->b_can_pace_control = false;
2150 if( !in->b_can_pace_control )
2152 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2153 &in->b_can_rate_control, &in->b_rescale_ts ) )
2155 in->b_can_rate_control = false;
2156 in->b_rescale_ts = true; /* not used */
2161 in->b_can_rate_control = true;
2162 in->b_rescale_ts = true;
2164 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2165 &in->b_can_pause ) )
2166 in->b_can_pause = false;
2167 var_SetBool( p_input, "can-pause", in->b_can_pause );
2169 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2171 if( ret != VLC_SUCCESS )
2173 var_Set( p_input, "seekable", val );
2177 int64_t i_pts_delay;
2179 input_ChangeState( p_input, OPENING_S );
2181 /* Now try a real access */
2182 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2184 /* Access failed, URL encoded ? */
2185 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2187 decode_URI( psz_path );
2189 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2190 psz_access, psz_demux, psz_path );
2192 in->p_access = access_New( p_input,
2193 psz_access, psz_demux, psz_path );
2195 if( in->p_access == NULL )
2197 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2199 intf_UserFatal( VLC_OBJECT( p_input), false,
2200 _("Your input can't be opened"),
2201 _("VLC is unable to open the MRL '%s'."
2202 " Check the log for details."), psz_mrl );
2207 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2208 while( psz && *psz )
2210 access_t *p_access = in->p_access;
2211 char *end = strchr( psz, ':' );
2216 in->p_access = access_FilterNew( in->p_access, psz );
2217 if( in->p_access == NULL )
2219 in->p_access = p_access;
2220 msg_Warn( p_input, "failed to insert access filter %s",
2228 /* Get infos from access */
2229 if( !p_input->b_preparsing )
2231 access_Control( in->p_access,
2232 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2233 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2235 in->b_title_demux = false;
2236 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2237 &in->title, &in->i_title,
2238 &in->i_title_offset, &in->i_seekpoint_offset ) )
2244 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2245 &in->b_can_pace_control );
2246 in->b_can_rate_control = in->b_can_pace_control;
2247 in->b_rescale_ts = true;
2249 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2251 var_SetBool( p_input, "can-pause", in->b_can_pause );
2252 access_Control( in->p_access, ACCESS_CAN_SEEK,
2254 var_Set( p_input, "seekable", val );
2257 input_ChangeState( p_input, BUFFERING_S );
2259 /* Create the stream_t */
2260 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2261 if( in->p_stream == NULL )
2263 msg_Warn( p_input, "cannot create a stream_t from access" );
2267 /* Open a demuxer */
2268 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2270 psz_demux = in->p_access->psz_demux;
2274 /* Take access redirections into account */
2275 char *psz_real_path;
2276 char *psz_buf = NULL;
2277 if( in->p_access->psz_path )
2279 const char *psz_a, *psz_d;
2280 psz_buf = strdup( in->p_access->psz_path );
2281 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2285 psz_real_path = psz_path;
2287 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2289 in->p_stream, p_input->p->p_es_out,
2290 p_input->b_preparsing );
2294 if( in->p_demux == NULL )
2296 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2297 psz_access, psz_demux, psz_path );
2298 intf_UserFatal( VLC_OBJECT( p_input ), false,
2299 _("VLC can't recognize the input's format"),
2300 _("The format of '%s' cannot be detected. "
2301 "Have a look the log for details."), psz_mrl );
2305 /* Get title from demux */
2306 if( !p_input->b_preparsing && in->i_title <= 0 )
2308 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2309 &in->title, &in->i_title,
2310 &in->i_title_offset, &in->i_seekpoint_offset ))
2312 TAB_INIT( in->i_title, in->title );
2316 in->b_title_demux = true;
2322 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2323 if( 1 || !p_input->b_preparsing )
2326 input_attachment_t **attachment;
2327 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2328 &attachment, &i_attachment ) )
2330 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2331 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2332 i_attachment, attachment );
2333 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2336 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2338 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2340 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2343 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2344 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2349 input_ChangeState( p_input, ERROR_S );
2352 demux_Delete( in->p_demux );
2355 stream_Delete( in->p_stream );
2358 access_Delete( in->p_access );
2360 return VLC_EGENERIC;
2363 /*****************************************************************************
2365 *****************************************************************************/
2366 static void InputSourceClean( input_source_t *in )
2371 demux_Delete( in->p_demux );
2374 stream_Delete( in->p_stream );
2377 access_Delete( in->p_access );
2379 if( in->i_title > 0 )
2381 for( i = 0; i < in->i_title; i++ )
2382 vlc_input_title_Delete( in->title[i] );
2383 TAB_CLEAN( in->i_title, in->title );
2387 static void SlaveDemux( input_thread_t *p_input )
2392 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2394 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2398 for( i = 0; i < p_input->p->i_slave; i++ )
2400 input_source_t *in = p_input->p->slave[i];
2406 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2411 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2413 msg_Err( p_input, "slave[%d] doesn't like "
2414 "DEMUX_GET_TIME -> EOF", i );
2419 if( i_stime >= i_time )
2422 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2428 i_ret = in->p_demux->pf_demux( in->p_demux );
2433 msg_Dbg( p_input, "slave %d EOF", i );
2439 static void SlaveSeek( input_thread_t *p_input )
2444 if( !p_input ) return;
2446 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2448 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2452 for( i = 0; i < p_input->p->i_slave; i++ )
2454 input_source_t *in = p_input->p->slave[i];
2456 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2458 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2464 /*****************************************************************************
2466 *****************************************************************************/
2467 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2471 if( !p_meta ) return;
2473 /* Get meta information from user */
2474 #define GET_META( field, s ) \
2475 var_Get( p_input, (s), &val ); \
2476 if( *val.psz_string ) \
2477 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2478 free( val.psz_string )
2480 GET_META( Title, "meta-title" );
2481 GET_META( Artist, "meta-artist" );
2482 GET_META( Genre, "meta-genre" );
2483 GET_META( Copyright, "meta-copyright" );
2484 GET_META( Description, "meta-description" );
2485 GET_META( Date, "meta-date" );
2486 GET_META( URL, "meta-url" );
2490 /*****************************************************************************
2491 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2492 * arturl and locking issue.
2493 *****************************************************************************/
2494 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2496 input_item_t *p_item = p_input->p->input.p_item;
2497 char * psz_arturl = NULL;
2498 char *psz_title = NULL;
2499 int i_arturl_event = false;
2504 psz_arturl = input_item_GetArtURL( p_item );
2506 vlc_mutex_lock( &p_item->lock );
2507 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2508 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2510 vlc_meta_Merge( p_item->p_meta, p_meta );
2512 if( psz_arturl && *psz_arturl )
2514 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2515 i_arturl_event = true;
2518 vlc_meta_Delete( p_meta );
2520 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2522 /* Don't look for art cover if sout
2523 * XXX It can change when sout has meta data support */
2524 if( p_input->p->p_sout && !p_input->b_preparsing )
2526 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2527 i_arturl_event = true;
2531 input_ExtractAttachmentAndCacheArt( p_input );
2537 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2539 p_meta = vlc_meta_New();
2540 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2542 vlc_mutex_unlock( &p_item->lock );
2544 input_item_SetPreparsed( p_item, true );
2546 if( i_arturl_event == true )
2550 /* Notify interested third parties */
2551 event.type = vlc_InputItemMetaChanged;
2552 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2553 vlc_event_send( &p_item->event_manager, &event );
2558 input_Control( p_input, INPUT_SET_NAME, psz_title );
2562 /** \todo handle sout meta */
2566 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2567 int i_new, input_attachment_t **pp_new )
2569 int i_attachment = *pi_attachment;
2570 input_attachment_t **attachment = *ppp_attachment;
2573 attachment = realloc( attachment,
2574 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2575 for( i = 0; i < i_new; i++ )
2576 attachment[i_attachment++] = pp_new[i];
2580 *pi_attachment = i_attachment;
2581 *ppp_attachment = attachment;
2584 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2588 if( p_input->b_preparsing )
2591 if( p_input->p->input.p_access )
2592 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2595 /* Get meta data from slave input */
2596 for( i = 0; i < p_input->p->i_slave; i++ )
2598 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2599 if( p_input->p->slave[i]->p_access )
2601 access_Control( p_input->p->slave[i]->p_access,
2602 ACCESS_GET_META, p_meta );
2607 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2614 /* XXX I am not sure it is a great idea, besides, there is more than that
2615 * if we want to do it right */
2616 vlc_mutex_lock( &p_item->lock );
2617 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2619 vlc_mutex_unlock( &p_item->lock );
2622 vlc_mutex_unlock( &p_item->lock );
2625 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2626 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2631 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2632 if(! p_demux->p_private )
2635 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2638 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2640 if( p_demux_meta->p_meta )
2642 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2643 vlc_meta_Delete( p_demux_meta->p_meta );
2646 if( p_demux_meta->i_attachments > 0 )
2648 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2649 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2650 p_demux_meta->i_attachments, p_demux_meta->attachments );
2651 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2653 module_Unneed( p_demux, p_id3 );
2655 free( p_demux->p_private );
2659 /*****************************************************************************
2660 * MRLSplit: parse the access, demux and url part of the
2661 * Media Resource Locator.
2662 *****************************************************************************/
2663 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2666 char *psz_access = NULL;
2667 char *psz_demux = NULL;
2670 /* Either there is an access/demux specification before ://
2671 * or we have a plain local file path. */
2672 psz_path = strstr( psz_dup, "://" );
2673 if( psz_path != NULL )
2676 psz_path += 3; /* skips "://" */
2678 /* Separate access from demux (<access>/<demux>://<path>) */
2679 psz_access = psz_dup;
2680 psz_demux = strchr( psz_access, '/' );
2682 *psz_demux++ = '\0';
2684 /* We really don't want module name substitution here! */
2685 if( psz_access[0] == '$' )
2687 if( psz_demux && psz_demux[0] == '$' )
2695 *ppsz_access = psz_access ? psz_access : "";
2696 *ppsz_demux = psz_demux ? psz_demux : "";
2697 *ppsz_path = psz_path ? psz_path : "";
2700 /*****************************************************************************
2701 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2704 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2705 *****************************************************************************/
2706 static void MRLSections( input_thread_t *p_input, char *psz_source,
2707 int *pi_title_start, int *pi_title_end,
2708 int *pi_chapter_start, int *pi_chapter_end )
2710 char *psz, *psz_end, *psz_next, *psz_check;
2712 *pi_title_start = *pi_title_end = -1;
2713 *pi_chapter_start = *pi_chapter_end = -1;
2715 /* Start by parsing titles and chapters */
2716 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2718 /* Check we are really dealing with a title/chapter section */
2719 psz_check = psz + 1;
2720 if( !*psz_check ) return;
2721 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2722 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2723 if( *psz_check == ':' && ++psz_check )
2724 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2725 if( *psz_check != '-' && *psz_check ) return;
2726 if( *psz_check == '-' && ++psz_check )
2727 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2728 if( *psz_check != ':' && *psz_check ) return;
2729 if( *psz_check == ':' && ++psz_check )
2730 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2731 if( *psz_check ) return;
2733 /* Separate start and end */
2735 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2737 /* Look for the start title */
2738 *pi_title_start = strtol( psz, &psz_next, 0 );
2739 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2740 *pi_title_end = *pi_title_start;
2743 /* Look for the start chapter */
2745 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2746 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2747 *pi_chapter_end = *pi_chapter_start;
2751 /* Look for the end title */
2752 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2753 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2756 /* Look for the end chapter */
2757 if( *psz_end ) psz_end++;
2758 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2759 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2762 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2763 psz_source, *pi_title_start, *pi_chapter_start,
2764 *pi_title_end, *pi_chapter_end );
2767 /*****************************************************************************
2768 * input_AddSubtitles: add a subtitles file and enable it
2769 *****************************************************************************/
2770 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2771 bool b_check_extension )
2773 input_source_t *sub;
2776 char *psz_path, *psz_extension;
2778 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2783 /* if we are provided a subtitle.sub file,
2784 * see if we don't have a subtitle.idx and use it instead */
2785 psz_path = strdup( psz_subtitle );
2788 psz_extension = strrchr( psz_path, '.');
2789 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2793 strcpy( psz_extension, ".idx" );
2794 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2795 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2798 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2799 psz_path, psz_subtitle );
2800 strcpy( psz_subtitle, psz_path );
2806 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2808 sub = InputSourceNew( p_input );
2809 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2811 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2814 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2816 if( count.i_int == 0 )
2818 /* if it was first one, there is disable too */
2820 if( count.i_int < list.p_list->i_count )
2822 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2823 &list.p_list->p_values[count.i_int] );
2825 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2833 /*****************************************************************************
2834 * input_get_event_manager
2835 *****************************************************************************/
2836 vlc_event_manager_t *
2837 input_get_event_manager( input_thread_t *p_input )
2839 return &p_input->p->event_manager;