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 static void ObjectKillChildrens( vlc_object_t *p_obj )
441 vlc_object_kill( p_obj );
443 p_list = vlc_list_children( p_obj );
444 for( i = 0; i < p_list->i_count; i++ )
445 ObjectKillChildrens( p_list->p_values[i].p_object );
446 vlc_list_release( p_list );
448 void input_StopThread( input_thread_t *p_input )
450 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
451 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
452 * unlock the control loop */
453 ObjectKillChildrens( VLC_OBJECT(p_input) );
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_ControlVarStop( 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" );
1429 /* Mark all submodules to die */
1430 ObjectKillChildrens( p_input );
1433 case INPUT_CONTROL_SET_POSITION:
1434 case INPUT_CONTROL_SET_POSITION_OFFSET:
1437 if( i_type == INPUT_CONTROL_SET_POSITION )
1439 f_pos = val.f_float;
1443 /* Should not fail */
1444 demux_Control( p_input->p->input.p_demux,
1445 DEMUX_GET_POSITION, &f_pos );
1446 f_pos += val.f_float;
1448 if( f_pos < 0.0 ) f_pos = 0.0;
1449 if( f_pos > 1.0 ) f_pos = 1.0;
1450 /* Reset the decoders states and clock sync (before calling the demuxer */
1451 input_EsOutChangePosition( p_input->p->p_es_out );
1452 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1455 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1456 "%2.1f%% failed", f_pos * 100 );
1460 if( p_input->p->i_slave > 0 )
1461 SlaveSeek( p_input );
1463 b_force_update = true;
1468 case INPUT_CONTROL_SET_TIME:
1469 case INPUT_CONTROL_SET_TIME_OFFSET:
1474 if( i_type == INPUT_CONTROL_SET_TIME )
1476 i_time = val.i_time;
1480 /* Should not fail */
1481 demux_Control( p_input->p->input.p_demux,
1482 DEMUX_GET_TIME, &i_time );
1483 i_time += val.i_time;
1485 if( i_time < 0 ) i_time = 0;
1487 /* Reset the decoders states and clock sync (before calling the demuxer */
1488 input_EsOutChangePosition( p_input->p->p_es_out );
1490 i_ret = demux_Control( p_input->p->input.p_demux,
1491 DEMUX_SET_TIME, i_time );
1496 /* Emulate it with a SET_POS */
1497 demux_Control( p_input->p->input.p_demux,
1498 DEMUX_GET_LENGTH, &i_length );
1501 double f_pos = (double)i_time / (double)i_length;
1502 i_ret = demux_Control( p_input->p->input.p_demux,
1503 DEMUX_SET_POSITION, f_pos );
1508 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1509 " failed or not possible", i_time );
1513 if( p_input->p->i_slave > 0 )
1514 SlaveSeek( p_input );
1516 b_force_update = true;
1521 case INPUT_CONTROL_SET_STATE:
1522 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1523 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1526 if( p_input->p->input.p_access )
1527 i_ret = access_Control( p_input->p->input.p_access,
1528 ACCESS_SET_PAUSE_STATE, false );
1530 i_ret = demux_Control( p_input->p->input.p_demux,
1531 DEMUX_SET_PAUSE_STATE, false );
1535 /* FIXME What to do ? */
1536 msg_Warn( p_input, "cannot unset pause -> EOF" );
1537 vlc_mutex_unlock( &p_input->p->lock_control );
1538 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1539 vlc_mutex_lock( &p_input->p->lock_control );
1542 b_force_update = true;
1544 /* Switch to play */
1545 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1549 input_EsOutChangeState( p_input->p->p_es_out );
1551 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1552 p_input->p->b_can_pause )
1555 if( p_input->p->input.p_access )
1556 i_ret = access_Control( p_input->p->input.p_access,
1557 ACCESS_SET_PAUSE_STATE, true );
1559 i_ret = demux_Control( p_input->p->input.p_demux,
1560 DEMUX_SET_PAUSE_STATE, true );
1562 b_force_update = true;
1566 msg_Warn( p_input, "cannot set pause state" );
1567 state = p_input->i_state;
1574 /* Switch to new state */
1575 input_ChangeStateWithVarCallback( p_input, state, false );
1579 input_EsOutChangeState( p_input->p->p_es_out );
1581 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1583 b_force_update = true;
1585 /* Correct "state" value */
1586 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1588 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1590 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1594 case INPUT_CONTROL_SET_RATE:
1595 case INPUT_CONTROL_SET_RATE_SLOWER:
1596 case INPUT_CONTROL_SET_RATE_FASTER:
1600 if( i_type == INPUT_CONTROL_SET_RATE )
1606 static const int ppi_factor[][2] = {
1607 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1609 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1618 for( i = 0; ppi_factor[i][0] != 0; i++ )
1620 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1621 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1622 if( i_test_e < i_error )
1628 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1630 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1632 if( ppi_factor[i_idx+1][0] > 0 )
1633 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1635 i_rate = INPUT_RATE_MAX+1;
1639 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1641 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1643 i_rate = INPUT_RATE_MIN-1;
1647 if( i_rate < INPUT_RATE_MIN )
1649 msg_Dbg( p_input, "cannot set rate faster" );
1650 i_rate = INPUT_RATE_MIN;
1652 else if( i_rate > INPUT_RATE_MAX )
1654 msg_Dbg( p_input, "cannot set rate slower" );
1655 i_rate = INPUT_RATE_MAX;
1657 if( i_rate != INPUT_RATE_DEFAULT &&
1658 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1659 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1661 msg_Dbg( p_input, "cannot change rate" );
1662 i_rate = INPUT_RATE_DEFAULT;
1664 if( i_rate != p_input->p->i_rate &&
1665 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1668 if( p_input->p->input.p_access )
1669 i_ret = VLC_EGENERIC;
1671 i_ret = demux_Control( p_input->p->input.p_demux,
1672 DEMUX_SET_RATE, &i_rate );
1675 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1676 i_rate = p_input->p->i_rate;
1681 if( i_rate != p_input->p->i_rate )
1684 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1685 var_SetBool( p_input, "rate-change", true );
1687 p_input->p->i_rate = i_rate;
1689 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1690 if( p_input->p->input.b_rescale_ts )
1691 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1693 b_force_update = true;
1698 case INPUT_CONTROL_SET_PROGRAM:
1699 /* No need to force update, es_out does it if needed */
1700 es_out_Control( p_input->p->p_es_out,
1701 ES_OUT_SET_GROUP, val.i_int );
1703 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1707 case INPUT_CONTROL_SET_ES:
1708 /* No need to force update, es_out does it if needed */
1709 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1710 input_EsOutGetFromID( p_input->p->p_es_out,
1714 case INPUT_CONTROL_SET_AUDIO_DELAY:
1715 input_EsOutSetDelay( p_input->p->p_es_out,
1716 AUDIO_ES, val.i_time );
1717 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1720 case INPUT_CONTROL_SET_SPU_DELAY:
1721 input_EsOutSetDelay( p_input->p->p_es_out,
1722 SPU_ES, val.i_time );
1723 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1726 case INPUT_CONTROL_SET_TITLE:
1727 case INPUT_CONTROL_SET_TITLE_NEXT:
1728 case INPUT_CONTROL_SET_TITLE_PREV:
1729 if( p_input->p->input.b_title_demux &&
1730 p_input->p->input.i_title > 0 )
1733 /* FIXME handle demux title */
1734 demux_t *p_demux = p_input->p->input.p_demux;
1737 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1738 i_title = p_demux->info.i_title - 1;
1739 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1740 i_title = p_demux->info.i_title + 1;
1742 i_title = val.i_int;
1744 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1746 input_EsOutChangePosition( p_input->p->p_es_out );
1748 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1749 input_ControlVarTitle( p_input, i_title );
1752 else if( p_input->p->input.i_title > 0 )
1754 access_t *p_access = p_input->p->input.p_access;
1757 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1758 i_title = p_access->info.i_title - 1;
1759 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1760 i_title = p_access->info.i_title + 1;
1762 i_title = val.i_int;
1764 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1766 input_EsOutChangePosition( p_input->p->p_es_out );
1768 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1769 stream_AccessReset( p_input->p->input.p_stream );
1773 case INPUT_CONTROL_SET_SEEKPOINT:
1774 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1775 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1776 if( p_input->p->input.b_title_demux &&
1777 p_input->p->input.i_title > 0 )
1779 demux_t *p_demux = p_input->p->input.p_demux;
1781 int64_t i_input_time;
1782 int64_t i_seekpoint_time;
1784 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1786 i_seekpoint = p_demux->info.i_seekpoint;
1787 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1788 if( i_seekpoint_time >= 0 &&
1789 !demux_Control( p_demux,
1790 DEMUX_GET_TIME, &i_input_time ) )
1792 if ( i_input_time < i_seekpoint_time + 3000000 )
1798 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1799 i_seekpoint = p_demux->info.i_seekpoint + 1;
1801 i_seekpoint = val.i_int;
1803 if( i_seekpoint >= 0 && i_seekpoint <
1804 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1807 input_EsOutChangePosition( p_input->p->p_es_out );
1809 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1812 else if( p_input->p->input.i_title > 0 )
1814 demux_t *p_demux = p_input->p->input.p_demux;
1815 access_t *p_access = p_input->p->input.p_access;
1817 int64_t i_input_time;
1818 int64_t i_seekpoint_time;
1820 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1822 i_seekpoint = p_access->info.i_seekpoint;
1823 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1824 if( i_seekpoint_time >= 0 &&
1825 demux_Control( p_demux,
1826 DEMUX_GET_TIME, &i_input_time ) )
1828 if ( i_input_time < i_seekpoint_time + 3000000 )
1834 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1835 i_seekpoint = p_access->info.i_seekpoint + 1;
1837 i_seekpoint = val.i_int;
1839 if( i_seekpoint >= 0 && i_seekpoint <
1840 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1842 input_EsOutChangePosition( p_input->p->p_es_out );
1844 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1846 stream_AccessReset( p_input->p->input.p_stream );
1851 case INPUT_CONTROL_ADD_SLAVE:
1852 if( val.psz_string )
1854 input_source_t *slave = InputSourceNew( p_input );
1856 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1862 msg_Dbg( p_input, "adding %s as slave on the fly",
1866 if( demux_Control( p_input->p->input.p_demux,
1867 DEMUX_GET_TIME, &i_time ) )
1869 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1870 InputSourceClean( slave );
1874 if( demux_Control( slave->p_demux,
1875 DEMUX_SET_TIME, i_time ) )
1877 msg_Err( p_input, "seek failed for new slave" );
1878 InputSourceClean( slave );
1883 /* Get meta (access and demux) */
1884 p_meta = vlc_meta_New();
1885 access_Control( slave->p_access, ACCESS_GET_META,
1887 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1888 InputUpdateMeta( p_input, p_meta );
1890 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1895 msg_Warn( p_input, "failed to add %s as slave",
1899 free( val.psz_string );
1903 case INPUT_CONTROL_SET_BOOKMARK:
1905 msg_Err( p_input, "not yet implemented" );
1909 return b_force_update;
1912 /*****************************************************************************
1914 *****************************************************************************/
1915 static int UpdateFromDemux( input_thread_t *p_input )
1917 demux_t *p_demux = p_input->p->input.p_demux;
1920 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1922 v.i_int = p_demux->info.i_title;
1923 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1925 input_ControlVarTitle( p_input, p_demux->info.i_title );
1927 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1929 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1931 v.i_int = p_demux->info.i_seekpoint;
1932 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1934 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1936 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1938 /* Hmmm only works with master input */
1939 if( p_input->p->input.p_demux == p_demux )
1941 int i_title_end = p_input->p->input.i_title_end -
1942 p_input->p->input.i_title_offset;
1943 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1944 p_input->p->input.i_seekpoint_offset;
1946 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1948 if( p_demux->info.i_title > i_title_end ||
1949 ( p_demux->info.i_title == i_title_end &&
1950 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1952 else if( i_seekpoint_end >=0 )
1954 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1956 else if( i_title_end >= 0 )
1958 if( p_demux->info.i_title > i_title_end ) return 0;
1965 /*****************************************************************************
1967 *****************************************************************************/
1968 static int UpdateFromAccess( input_thread_t *p_input )
1970 access_t *p_access = p_input->p->input.p_access;
1973 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1975 v.i_int = p_access->info.i_title;
1976 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1978 input_ControlVarTitle( p_input, p_access->info.i_title );
1980 stream_AccessUpdate( p_input->p->input.p_stream );
1982 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1984 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1986 v.i_int = p_access->info.i_seekpoint;
1987 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1988 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1990 if( p_access->info.i_update & INPUT_UPDATE_META )
1992 /* TODO maybe multi - access ? */
1993 vlc_meta_t *p_meta = vlc_meta_New();
1994 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
1995 InputUpdateMeta( p_input, p_meta );
1996 p_access->info.i_update &= ~INPUT_UPDATE_META;
1999 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2001 /* Hmmm only works with master input */
2002 if( p_input->p->input.p_access == p_access )
2004 int i_title_end = p_input->p->input.i_title_end -
2005 p_input->p->input.i_title_offset;
2006 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2007 p_input->p->input.i_seekpoint_offset;
2009 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2011 if( p_access->info.i_title > i_title_end ||
2012 ( p_access->info.i_title == i_title_end &&
2013 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2015 else if( i_seekpoint_end >=0 )
2017 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2019 else if( i_title_end >= 0 )
2021 if( p_access->info.i_title > i_title_end ) return 0;
2028 /*****************************************************************************
2030 *****************************************************************************/
2031 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2033 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2036 /*****************************************************************************
2038 *****************************************************************************/
2039 static input_source_t *InputSourceNew( input_thread_t *p_input )
2041 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2043 memset( in, 0, sizeof( input_source_t ) );
2047 /*****************************************************************************
2049 *****************************************************************************/
2050 static int InputSourceInit( input_thread_t *p_input,
2051 input_source_t *in, const char *psz_mrl,
2052 const char *psz_forced_demux )
2054 char psz_dup[strlen (psz_mrl) + 1];
2055 const char *psz_access;
2056 const char *psz_demux;
2063 strcpy( psz_dup, psz_mrl );
2065 if( !in ) return VLC_EGENERIC;
2066 if( !p_input ) return VLC_EGENERIC;
2069 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2071 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2072 psz_mrl, psz_access, psz_demux, psz_path );
2073 if( !p_input->b_preparsing )
2075 /* Hack to allow udp://@:port syntax */
2077 (strncmp( psz_access, "udp", 3 ) &&
2078 strncmp( psz_access, "rtp", 3 )) )
2080 /* Find optional titles and seekpoints */
2081 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2082 &in->i_seekpoint_start, &in->i_seekpoint_end );
2085 if( psz_forced_demux && *psz_forced_demux )
2087 psz_demux = psz_forced_demux;
2089 else if( *psz_demux == '\0' )
2091 /* special hack for forcing a demuxer with --demux=module
2092 * (and do nothing with a list) */
2093 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2095 if( psz_var_demux != NULL &&
2096 !strchr(psz_var_demux, ',' ) &&
2097 !strchr(psz_var_demux, ':' ) )
2099 psz_demux = psz_var_demux;
2101 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2105 /* Try access_demux if no demux given */
2106 if( *psz_demux == '\0' )
2108 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2109 NULL, p_input->p->p_es_out, false );
2114 /* Preparsing is only for file:// */
2117 if( !*psz_access ) /* path without scheme:// */
2118 psz_access = "file";
2119 if( strcmp( psz_access, "file" ) )
2121 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2126 int64_t i_pts_delay;
2128 /* Get infos from access_demux */
2129 demux_Control( in->p_demux,
2130 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2131 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2133 in->b_title_demux = true;
2134 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2135 &in->title, &in->i_title,
2136 &in->i_title_offset, &in->i_seekpoint_offset ) )
2141 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2142 &in->b_can_pace_control ) )
2143 in->b_can_pace_control = false;
2145 if( !in->b_can_pace_control )
2147 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2148 &in->b_can_rate_control, &in->b_rescale_ts ) )
2150 in->b_can_rate_control = false;
2151 in->b_rescale_ts = true; /* not used */
2156 in->b_can_rate_control = true;
2157 in->b_rescale_ts = true;
2159 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2160 &in->b_can_pause ) )
2161 in->b_can_pause = false;
2162 var_SetBool( p_input, "can-pause", in->b_can_pause );
2164 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2166 if( ret != VLC_SUCCESS )
2168 var_Set( p_input, "seekable", val );
2172 int64_t i_pts_delay;
2174 input_ChangeState( p_input, OPENING_S );
2176 /* Now try a real access */
2177 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2179 /* Access failed, URL encoded ? */
2180 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2182 decode_URI( psz_path );
2184 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2185 psz_access, psz_demux, psz_path );
2187 in->p_access = access_New( p_input,
2188 psz_access, psz_demux, psz_path );
2190 if( in->p_access == NULL )
2192 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2194 intf_UserFatal( VLC_OBJECT( p_input), false,
2195 _("Your input can't be opened"),
2196 _("VLC is unable to open the MRL '%s'."
2197 " Check the log for details."), psz_mrl );
2202 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2203 while( psz && *psz )
2205 access_t *p_access = in->p_access;
2206 char *end = strchr( psz, ':' );
2211 in->p_access = access_FilterNew( in->p_access, psz );
2212 if( in->p_access == NULL )
2214 in->p_access = p_access;
2215 msg_Warn( p_input, "failed to insert access filter %s",
2223 /* Get infos from access */
2224 if( !p_input->b_preparsing )
2226 access_Control( in->p_access,
2227 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2228 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2230 in->b_title_demux = false;
2231 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2232 &in->title, &in->i_title,
2233 &in->i_title_offset, &in->i_seekpoint_offset ) )
2239 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2240 &in->b_can_pace_control );
2241 in->b_can_rate_control = in->b_can_pace_control;
2242 in->b_rescale_ts = true;
2244 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2246 var_SetBool( p_input, "can-pause", in->b_can_pause );
2247 access_Control( in->p_access, ACCESS_CAN_SEEK,
2249 var_Set( p_input, "seekable", val );
2252 input_ChangeState( p_input, BUFFERING_S );
2254 /* Create the stream_t */
2255 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2256 if( in->p_stream == NULL )
2258 msg_Warn( p_input, "cannot create a stream_t from access" );
2262 /* Open a demuxer */
2263 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2265 psz_demux = in->p_access->psz_demux;
2269 /* Take access redirections into account */
2270 char *psz_real_path;
2271 char *psz_buf = NULL;
2272 if( in->p_access->psz_path )
2274 const char *psz_a, *psz_d;
2275 psz_buf = strdup( in->p_access->psz_path );
2276 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2280 psz_real_path = psz_path;
2282 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2284 in->p_stream, p_input->p->p_es_out,
2285 p_input->b_preparsing );
2289 if( in->p_demux == NULL )
2291 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2292 psz_access, psz_demux, psz_path );
2293 intf_UserFatal( VLC_OBJECT( p_input ), false,
2294 _("VLC can't recognize the input's format"),
2295 _("The format of '%s' cannot be detected. "
2296 "Have a look the log for details."), psz_mrl );
2300 /* Get title from demux */
2301 if( !p_input->b_preparsing && in->i_title <= 0 )
2303 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2304 &in->title, &in->i_title,
2305 &in->i_title_offset, &in->i_seekpoint_offset ))
2307 TAB_INIT( in->i_title, in->title );
2311 in->b_title_demux = true;
2317 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2318 if( 1 || !p_input->b_preparsing )
2321 input_attachment_t **attachment;
2322 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2323 &attachment, &i_attachment ) )
2325 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2326 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2327 i_attachment, attachment );
2328 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2331 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2333 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2335 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2338 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2339 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2344 input_ChangeState( p_input, ERROR_S );
2347 demux_Delete( in->p_demux );
2350 stream_Delete( in->p_stream );
2353 access_Delete( in->p_access );
2355 return VLC_EGENERIC;
2358 /*****************************************************************************
2360 *****************************************************************************/
2361 static void InputSourceClean( input_source_t *in )
2366 demux_Delete( in->p_demux );
2369 stream_Delete( in->p_stream );
2372 access_Delete( in->p_access );
2374 if( in->i_title > 0 )
2376 for( i = 0; i < in->i_title; i++ )
2377 vlc_input_title_Delete( in->title[i] );
2378 TAB_CLEAN( in->i_title, in->title );
2382 static void SlaveDemux( input_thread_t *p_input )
2387 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2389 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2393 for( i = 0; i < p_input->p->i_slave; i++ )
2395 input_source_t *in = p_input->p->slave[i];
2401 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2406 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2408 msg_Err( p_input, "slave[%d] doesn't like "
2409 "DEMUX_GET_TIME -> EOF", i );
2414 if( i_stime >= i_time )
2417 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2423 i_ret = in->p_demux->pf_demux( in->p_demux );
2428 msg_Dbg( p_input, "slave %d EOF", i );
2434 static void SlaveSeek( input_thread_t *p_input )
2439 if( !p_input ) return;
2441 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2443 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2447 for( i = 0; i < p_input->p->i_slave; i++ )
2449 input_source_t *in = p_input->p->slave[i];
2451 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2453 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2459 /*****************************************************************************
2461 *****************************************************************************/
2462 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2466 if( !p_meta ) return;
2468 /* Get meta information from user */
2469 #define GET_META( field, s ) \
2470 var_Get( p_input, (s), &val ); \
2471 if( *val.psz_string ) \
2472 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2473 free( val.psz_string )
2475 GET_META( Title, "meta-title" );
2476 GET_META( Artist, "meta-artist" );
2477 GET_META( Genre, "meta-genre" );
2478 GET_META( Copyright, "meta-copyright" );
2479 GET_META( Description, "meta-description" );
2480 GET_META( Date, "meta-date" );
2481 GET_META( URL, "meta-url" );
2485 /*****************************************************************************
2486 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2487 * arturl and locking issue.
2488 *****************************************************************************/
2489 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2491 input_item_t *p_item = p_input->p->input.p_item;
2492 char * psz_arturl = NULL;
2493 char *psz_title = NULL;
2494 int i_arturl_event = false;
2499 psz_arturl = input_item_GetArtURL( p_item );
2501 vlc_mutex_lock( &p_item->lock );
2502 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2503 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2505 vlc_meta_Merge( p_item->p_meta, p_meta );
2507 if( psz_arturl && *psz_arturl )
2509 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2510 i_arturl_event = true;
2513 vlc_meta_Delete( p_meta );
2515 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2517 /* Don't look for art cover if sout
2518 * XXX It can change when sout has meta data support */
2519 if( p_input->p->p_sout && !p_input->b_preparsing )
2521 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2522 i_arturl_event = true;
2526 input_ExtractAttachmentAndCacheArt( p_input );
2532 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2534 p_meta = vlc_meta_New();
2535 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2537 vlc_mutex_unlock( &p_item->lock );
2539 input_item_SetPreparsed( p_item, true );
2541 if( i_arturl_event == true )
2545 /* Notify interested third parties */
2546 event.type = vlc_InputItemMetaChanged;
2547 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2548 vlc_event_send( &p_item->event_manager, &event );
2553 input_Control( p_input, INPUT_SET_NAME, psz_title );
2557 /** \todo handle sout meta */
2561 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2562 int i_new, input_attachment_t **pp_new )
2564 int i_attachment = *pi_attachment;
2565 input_attachment_t **attachment = *ppp_attachment;
2568 attachment = realloc( attachment,
2569 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2570 for( i = 0; i < i_new; i++ )
2571 attachment[i_attachment++] = pp_new[i];
2575 *pi_attachment = i_attachment;
2576 *ppp_attachment = attachment;
2579 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2583 if( p_input->b_preparsing )
2586 if( p_input->p->input.p_access )
2587 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2590 /* Get meta data from slave input */
2591 for( i = 0; i < p_input->p->i_slave; i++ )
2593 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2594 if( p_input->p->slave[i]->p_access )
2596 access_Control( p_input->p->slave[i]->p_access,
2597 ACCESS_GET_META, p_meta );
2602 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2609 /* XXX I am not sure it is a great idea, besides, there is more than that
2610 * if we want to do it right */
2611 vlc_mutex_lock( &p_item->lock );
2612 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2614 vlc_mutex_unlock( &p_item->lock );
2617 vlc_mutex_unlock( &p_item->lock );
2620 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2621 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2626 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2627 if(! p_demux->p_private )
2630 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2633 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2635 if( p_demux_meta->p_meta )
2637 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2638 vlc_meta_Delete( p_demux_meta->p_meta );
2641 if( p_demux_meta->i_attachments > 0 )
2643 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2644 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2645 p_demux_meta->i_attachments, p_demux_meta->attachments );
2646 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2648 module_Unneed( p_demux, p_id3 );
2650 free( p_demux->p_private );
2654 /*****************************************************************************
2655 * MRLSplit: parse the access, demux and url part of the
2656 * Media Resource Locator.
2657 *****************************************************************************/
2658 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2661 char *psz_access = NULL;
2662 char *psz_demux = NULL;
2665 /* Either there is an access/demux specification before ://
2666 * or we have a plain local file path. */
2667 psz_path = strstr( psz_dup, "://" );
2668 if( psz_path != NULL )
2671 psz_path += 3; /* skips "://" */
2673 /* Separate access from demux (<access>/<demux>://<path>) */
2674 psz_access = psz_dup;
2675 psz_demux = strchr( psz_access, '/' );
2677 *psz_demux++ = '\0';
2679 /* We really don't want module name substitution here! */
2680 if( psz_access[0] == '$' )
2682 if( psz_demux && psz_demux[0] == '$' )
2690 *ppsz_access = psz_access ? psz_access : "";
2691 *ppsz_demux = psz_demux ? psz_demux : "";
2692 *ppsz_path = psz_path ? psz_path : "";
2695 /*****************************************************************************
2696 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2699 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2700 *****************************************************************************/
2701 static void MRLSections( input_thread_t *p_input, char *psz_source,
2702 int *pi_title_start, int *pi_title_end,
2703 int *pi_chapter_start, int *pi_chapter_end )
2705 char *psz, *psz_end, *psz_next, *psz_check;
2707 *pi_title_start = *pi_title_end = -1;
2708 *pi_chapter_start = *pi_chapter_end = -1;
2710 /* Start by parsing titles and chapters */
2711 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2713 /* Check we are really dealing with a title/chapter section */
2714 psz_check = psz + 1;
2715 if( !*psz_check ) return;
2716 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2717 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2718 if( *psz_check == ':' && ++psz_check )
2719 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2720 if( *psz_check != '-' && *psz_check ) return;
2721 if( *psz_check == '-' && ++psz_check )
2722 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2723 if( *psz_check != ':' && *psz_check ) return;
2724 if( *psz_check == ':' && ++psz_check )
2725 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2726 if( *psz_check ) return;
2728 /* Separate start and end */
2730 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2732 /* Look for the start title */
2733 *pi_title_start = strtol( psz, &psz_next, 0 );
2734 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2735 *pi_title_end = *pi_title_start;
2738 /* Look for the start chapter */
2740 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2741 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2742 *pi_chapter_end = *pi_chapter_start;
2746 /* Look for the end title */
2747 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2748 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2751 /* Look for the end chapter */
2752 if( *psz_end ) psz_end++;
2753 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2754 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2757 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2758 psz_source, *pi_title_start, *pi_chapter_start,
2759 *pi_title_end, *pi_chapter_end );
2762 /*****************************************************************************
2763 * input_AddSubtitles: add a subtitles file and enable it
2764 *****************************************************************************/
2765 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2766 bool b_check_extension )
2768 input_source_t *sub;
2771 char *psz_path, *psz_extension;
2773 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2778 /* if we are provided a subtitle.sub file,
2779 * see if we don't have a subtitle.idx and use it instead */
2780 psz_path = strdup( psz_subtitle );
2783 psz_extension = strrchr( psz_path, '.');
2784 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2788 strcpy( psz_extension, ".idx" );
2789 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2790 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2793 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2794 psz_path, psz_subtitle );
2795 strcpy( psz_subtitle, psz_path );
2801 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2803 sub = InputSourceNew( p_input );
2804 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2806 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2809 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2811 if( count.i_int == 0 )
2813 /* if it was first one, there is disable too */
2815 if( count.i_int < list.p_list->i_count )
2817 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2818 &list.p_list->p_values[count.i_int] );
2820 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2828 /*****************************************************************************
2829 * input_get_event_manager
2830 *****************************************************************************/
2831 vlc_event_manager_t *
2832 input_get_event_manager( input_thread_t *p_input )
2834 return &p_input->p->event_manager;