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 )
135 msg_Err( p_parent, "out of memory" );
139 /* Construct a nice name for the input timer */
140 char psz_timer_name[255];
141 char * psz_name = input_item_GetName( p_item );
142 snprintf( psz_timer_name, sizeof(psz_timer_name),
143 "input launching for '%s'", psz_name );
146 /* Start a timer to mesure how long it takes
147 * to launch an input */
148 stats_TimerStart( p_input, psz_timer_name,
149 STATS_TIMER_INPUT_LAUNCHING );
151 MALLOC_NULL( p_input->p, input_thread_private_t );
152 memset( p_input->p, 0, sizeof( input_thread_private_t ) );
154 /* One "randomly" selected input thread is responsible for computing
155 * the global stats. Check if there is already someone doing this */
156 if( p_input->p_libvlc->p_stats && !b_quick )
158 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
159 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
160 if( priv->p_stats_computer == NULL )
161 priv->p_stats_computer = p_input;
162 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
165 p_input->b_preparsing = b_quick;
166 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
169 vlc_event_manager_t * p_em = &p_input->p->event_manager;
170 vlc_event_manager_init_with_vlc_object( p_em, p_input );
171 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
173 /* Init Common fields */
174 p_input->b_eof = false;
175 p_input->b_can_pace_control = true;
176 p_input->p->i_start = 0;
178 p_input->p->i_stop = 0;
179 p_input->p->i_run = 0;
180 p_input->p->i_title = 0;
181 p_input->p->title = NULL;
182 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
183 p_input->i_state = INIT_S;
184 p_input->p->i_rate = INPUT_RATE_DEFAULT;
185 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
186 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
187 p_input->p->p_es_out = NULL;
188 p_input->p->p_sout = NULL;
189 p_input->p->b_out_pace_control = false;
190 p_input->i_pts_delay = 0;
192 /* Init Input fields */
193 vlc_gc_incref( p_item ); /* Released in Destructor() */
194 p_input->p->input.p_item = p_item;
195 p_input->p->input.p_access = NULL;
196 p_input->p->input.p_stream = NULL;
197 p_input->p->input.p_demux = NULL;
198 p_input->p->input.b_title_demux = false;
199 p_input->p->input.i_title = 0;
200 p_input->p->input.title = NULL;
201 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
202 p_input->p->input.b_can_pace_control = true;
203 p_input->p->input.b_can_rate_control = true;
204 p_input->p->input.b_rescale_ts = true;
205 p_input->p->input.b_eof = false;
206 p_input->p->input.i_cr_average = 0;
208 vlc_mutex_lock( &p_item->lock );
210 if( !p_item->p_stats )
211 p_item->p_stats = stats_NewInputStats( p_input );
212 vlc_mutex_unlock( &p_item->lock );
215 p_input->p->i_slave = 0;
216 p_input->p->slave = NULL;
218 /* Init control buffer */
219 vlc_mutex_init( &p_input->p->lock_control );
220 p_input->p->i_control = 0;
222 /* Parse input options */
223 vlc_mutex_lock( &p_item->lock );
224 assert( p_item->optflagc == p_item->i_options );
225 for( i = 0; i < p_item->i_options; i++ )
226 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
227 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
228 vlc_mutex_unlock( &p_item->lock );
230 /* Create Object Variables for private use only */
231 input_ConfigVarInit( p_input );
233 /* Create Objects variables for public Get and Set */
234 input_ControlVarInit( p_input );
236 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
238 if( !p_input->b_preparsing )
240 var_Get( p_input, "bookmarks", &val );
243 /* FIXME: have a common cfg parsing routine used by sout and others */
244 char *psz_parser, *psz_start, *psz_end;
245 psz_parser = val.psz_string;
246 while( (psz_start = strchr( psz_parser, '{' ) ) )
248 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
251 psz_end = strchr( psz_start, '}' );
252 if( !psz_end ) break;
253 psz_parser = psz_end + 1;
254 backup = *psz_parser;
257 while( (psz_end = strchr( psz_start, ',' ) ) )
260 if( !strncmp( psz_start, "name=", 5 ) )
262 p_seekpoint->psz_name = strdup(psz_start + 5);
264 else if( !strncmp( psz_start, "bytes=", 6 ) )
266 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
268 else if( !strncmp( psz_start, "time=", 5 ) )
270 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
273 psz_start = psz_end + 1;
275 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
276 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
277 p_seekpoint->i_time_offset );
278 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
279 vlc_seekpoint_Delete( p_seekpoint );
280 *psz_parser = backup;
282 free( val.psz_string );
286 /* Remove 'Now playing' info as it is probably outdated */
287 input_item_SetNowPlaying( p_item, NULL );
290 if( p_input->b_preparsing )
291 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
295 p_input->p->p_sout = p_sout;
297 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
298 vlc_mutex_init( &p_input->p->counters.counters_lock );
300 /* Attach only once we are ready */
301 vlc_object_attach( p_input, p_parent );
303 /* Set the destructor when we are sure we are initialized */
304 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
310 * Input destructor (called when the object's refcount reaches 0).
312 static void Destructor( input_thread_t * p_input )
314 input_thread_private_t *priv = p_input->p;
316 vlc_event_manager_fini( &p_input->p->event_manager );
318 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
319 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
322 sout_DeleteInstance( priv->p_sout );
324 vlc_gc_decref( p_input->p->input.p_item );
326 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
328 vlc_mutex_destroy( &priv->lock_control );
333 * Initialize an input thread and run it. You will need to monitor the
334 * thread to clean up after it is done
336 * \param p_parent a vlc_object
337 * \param p_item an input item
338 * \return a pointer to the spawned input thread
340 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
341 input_item_t *p_item )
343 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
347 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
348 input_item_t *p_item,
349 const char *psz_log, sout_instance_t *p_sout )
351 input_thread_t *p_input;
353 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
357 /* Create thread and wait for its readiness. */
358 if( vlc_thread_create( p_input, "input", Run,
359 VLC_THREAD_PRIORITY_INPUT, true ) )
361 input_ChangeState( p_input, ERROR_S );
362 msg_Err( p_input, "cannot create input thread" );
363 vlc_object_detach( p_input );
364 vlc_object_release( p_input );
372 * Initialize an input thread and run it. This thread will clean after itself,
373 * you can forget about it. It can work either in blocking or non-blocking mode
375 * \param p_parent a vlc_object
376 * \param p_item an input item
377 * \param b_block should we block until read is finished ?
378 * \return the input object id if non blocking, an error code else
380 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
383 input_thread_t *p_input;
385 p_input = Create( p_parent, p_item, NULL, false, NULL );
391 RunAndDestroy( p_input );
396 if( vlc_thread_create( p_input, "input", RunAndDestroy,
397 VLC_THREAD_PRIORITY_INPUT, true ) )
399 input_ChangeState( p_input, ERROR_S );
400 msg_Err( p_input, "cannot create input thread" );
401 vlc_object_release( p_input );
405 return p_input->i_object_id;
409 * Initialize an input and initialize it to preparse the item
410 * This function is blocking. It will only accept to parse files
412 * \param p_parent a vlc_object_t
413 * \param p_item an input item
414 * \return VLC_SUCCESS or an error
416 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
418 input_thread_t *p_input;
420 /* Allocate descriptor */
421 p_input = Create( p_parent, p_item, NULL, true, NULL );
425 if( !Init( p_input ) )
428 vlc_object_detach( p_input );
429 vlc_object_release( p_input );
435 * Request a running input thread to stop and die
437 * \param the input thread to stop
439 void input_StopThread( input_thread_t *p_input )
444 /* Set die for input */
445 vlc_object_kill( p_input );
446 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
448 /* We cannot touch p_input fields directly (we come from another thread),
449 * so use the vlc_object_find way, it's perfectly safe */
451 /* Set die for all access */
452 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
453 for( i = 0; i < p_list->i_count; i++ )
455 vlc_object_kill( p_list->p_values[i].p_object );
457 vlc_list_release( p_list );
459 /* Set die for all stream */
460 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
461 for( i = 0; i < p_list->i_count; i++ )
463 vlc_object_kill( p_list->p_values[i].p_object );
465 vlc_list_release( p_list );
467 /* Set die for all demux */
468 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
469 for( i = 0; i < p_list->i_count; i++ )
471 vlc_object_kill( p_list->p_values[i].p_object );
473 vlc_list_release( p_list );
475 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
478 sout_instance_t * input_DetachSout( input_thread_t *p_input )
480 sout_instance_t *p_sout = p_input->p->p_sout;
481 vlc_object_detach( p_sout );
482 p_input->p->p_sout = NULL;
486 /*****************************************************************************
487 * Run: main thread loop
488 * This is the "normal" thread that spawns the input processing chain,
489 * reads the stream, cleans up and waits
490 *****************************************************************************/
491 static int Run( input_thread_t *p_input )
493 /* Signal that the thread is launched */
494 vlc_thread_ready( p_input );
496 if( Init( p_input ) )
498 /* If we failed, wait before we are killed, and exit */
499 p_input->b_error = true;
503 /* Tell we're dead */
504 p_input->b_dead = true;
511 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
513 /* We have finish to demux data but not to play them */
514 while( !p_input->b_die )
516 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
519 msg_Dbg( p_input, "waiting decoder fifos to empty" );
521 msleep( INPUT_IDLE_SLEEP );
524 /* We have finished */
525 p_input->b_eof = true;
526 input_ChangeState( p_input, END_S );
529 /* Wait until we are asked to die */
530 if( !p_input->b_die )
541 /*****************************************************************************
542 * RunAndDestroy: main thread loop
543 * This is the "just forget me" thread that spawns the input processing chain,
544 * reads the stream, cleans up and releases memory
545 *****************************************************************************/
546 static int RunAndDestroy( input_thread_t *p_input )
548 /* Signal that the thread is launched */
549 vlc_thread_ready( p_input );
551 if( Init( p_input ) )
556 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
558 /* We have finished demuxing data but not playing it */
559 while( !p_input->b_die )
561 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
564 msg_Dbg( p_input, "waiting decoder fifos to empty" );
566 msleep( INPUT_IDLE_SLEEP );
569 /* We have finished */
570 p_input->b_eof = true;
578 vlc_object_release( p_input );
582 /*****************************************************************************
583 * Main loop: Fill buffers from access, and demux
584 *****************************************************************************/
585 static void MainLoop( input_thread_t *p_input )
587 int64_t i_start_mdate = mdate();
588 int64_t i_intf_update = 0;
592 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
594 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
596 bool b_force_update = false;
602 if( p_input->i_state != PAUSE_S )
604 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
605 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
608 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
613 if( p_input->p->input.b_title_demux &&
614 p_input->p->input.p_demux->info.i_update )
616 i_ret = UpdateFromDemux( p_input );
617 b_force_update = true;
619 else if( !p_input->p->input.b_title_demux &&
620 p_input->p->input.p_access &&
621 p_input->p->input.p_access->info.i_update )
623 i_ret = UpdateFromAccess( p_input );
624 b_force_update = true;
628 if( i_ret == 0 ) /* EOF */
632 var_Get( p_input, "input-repeat", &repeat );
633 if( repeat.i_int == 0 )
635 /* End of file - we do not set b_die because only the
636 * playlist is allowed to do so. */
637 msg_Dbg( p_input, "EOF reached" );
638 p_input->p->input.b_eof = true;
642 msg_Dbg( p_input, "repeating the same input (%d)",
644 if( repeat.i_int > 0 )
647 var_Set( p_input, "input-repeat", repeat );
650 /* Seek to start title/seekpoint */
651 val.i_int = p_input->p->input.i_title_start -
652 p_input->p->input.i_title_offset;
653 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
655 input_ControlPush( p_input,
656 INPUT_CONTROL_SET_TITLE, &val );
658 val.i_int = p_input->p->input.i_seekpoint_start -
659 p_input->p->input.i_seekpoint_offset;
660 if( val.i_int > 0 /* TODO: check upper boundary */ )
661 input_ControlPush( p_input,
662 INPUT_CONTROL_SET_SEEKPOINT, &val );
664 /* Seek to start position */
665 if( p_input->p->i_start > 0 )
667 val.i_time = p_input->p->i_start;
668 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
674 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
679 i_start_mdate = mdate();
684 p_input->b_error = true;
687 if( i_ret > 0 && p_input->p->i_slave > 0 )
689 SlaveDemux( p_input );
699 vlc_mutex_lock( &p_input->p->lock_control );
700 ControlReduce( p_input );
701 while( !ControlPopNoLock( p_input, &i_type, &val ) )
703 msg_Dbg( p_input, "control type=%d", i_type );
704 if( Control( p_input, i_type, val ) )
705 b_force_update = true;
707 vlc_mutex_unlock( &p_input->p->lock_control );
709 if( b_force_update || i_intf_update < mdate() )
713 int64_t i_time, i_length;
714 /* update input status variables */
715 if( !demux_Control( p_input->p->input.p_demux,
716 DEMUX_GET_POSITION, &f_pos ) )
718 val.f_float = (float)f_pos;
719 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
721 if( !demux_Control( p_input->p->input.p_demux,
722 DEMUX_GET_TIME, &i_time ) )
724 p_input->i_time = i_time;
726 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
728 if( !demux_Control( p_input->p->input.p_demux,
729 DEMUX_GET_LENGTH, &i_length ) )
732 var_Get( p_input, "length", &old_val );
733 val.i_time = i_length;
734 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
736 if( old_val.i_time != val.i_time )
738 UpdateItemLength( p_input, i_length );
742 var_SetBool( p_input, "intf-change", true );
743 i_intf_update = mdate() + INT64_C(150000);
745 /* 150ms * 8 = ~ 1 second */
746 if( ++i_updates % 8 == 0 )
748 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
749 /* Are we the thread responsible for computing global stats ? */
750 if( libvlc_priv (p_input->p_libvlc)->p_stats_computer == p_input )
752 stats_ComputeGlobalStats( p_input->p_libvlc,
753 p_input->p_libvlc->p_stats );
759 static void InitStatistics( input_thread_t * p_input )
761 if( p_input->b_preparsing ) return;
763 /* Prepare statistics */
764 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
765 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
766 if( libvlc_stats (p_input) )
768 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
769 INIT_COUNTER( read_packets, INTEGER, COUNTER );
770 INIT_COUNTER( demux_read, INTEGER, COUNTER );
771 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
772 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
773 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
774 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
775 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
776 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
777 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
778 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
779 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
780 p_input->p->counters.p_sout_send_bitrate = NULL;
781 p_input->p->counters.p_sout_sent_packets = NULL;
782 p_input->p->counters.p_sout_sent_bytes = NULL;
783 if( p_input->p->counters.p_demux_bitrate )
784 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
785 if( p_input->p->counters.p_input_bitrate )
786 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
791 static int InitSout( input_thread_t * p_input )
795 if( p_input->b_preparsing ) return VLC_SUCCESS;
797 /* Find a usable sout and attach it to p_input */
798 psz = var_GetNonEmptyString( p_input, "sout" );
799 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
801 /* Check the validity of the provided sout */
802 if( p_input->p->p_sout )
804 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
806 msg_Dbg( p_input, "destroying unusable sout" );
808 sout_DeleteInstance( p_input->p->p_sout );
809 p_input->p->p_sout = NULL;
813 if( p_input->p->p_sout )
816 msg_Dbg( p_input, "sout keep: reusing sout" );
817 msg_Dbg( p_input, "sout keep: you probably want to use "
818 "gather stream_out" );
819 vlc_object_attach( p_input->p->p_sout, p_input );
823 /* Create a new one */
824 p_input->p->p_sout = sout_NewInstance( p_input, psz );
825 if( !p_input->p->p_sout )
827 input_ChangeState( p_input, ERROR_S );
828 msg_Err( p_input, "cannot start stream output instance, " \
834 if( libvlc_stats (p_input) )
836 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
837 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
838 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
839 if( p_input->p->counters.p_sout_send_bitrate )
840 p_input->p->counters.p_sout_send_bitrate->update_interval =
844 else if( p_input->p->p_sout )
846 msg_Dbg( p_input, "destroying useless sout" );
848 sout_DeleteInstance( p_input->p->p_sout );
849 p_input->p->p_sout = NULL;
857 static void InitTitle( input_thread_t * p_input )
861 if( p_input->b_preparsing ) return;
863 /* Create global title (from master) */
864 p_input->p->i_title = p_input->p->input.i_title;
865 p_input->p->title = p_input->p->input.title;
866 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
867 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
868 if( p_input->p->i_title > 0 )
870 /* Setup variables */
871 input_ControlVarNavigation( p_input );
872 input_ControlVarTitle( p_input, 0 );
876 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
877 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
878 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
881 if( p_input->i_pts_delay < 0 )
882 p_input->i_pts_delay = 0;
884 /* If the desynchronisation requested by the user is < 0, we need to
885 * cache more data. */
886 var_Get( p_input, "audio-desync", &val );
887 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
889 /* Update cr_average depending on the caching */
890 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
891 p_input->p->input.i_cr_average /= 10;
892 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
895 static void StartTitle( input_thread_t * p_input )
904 /* Start title/chapter */
906 if( p_input->b_preparsing )
908 p_input->p->i_start = 0;
912 val.i_int = p_input->p->input.i_title_start -
913 p_input->p->input.i_title_offset;
914 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
915 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
916 val.i_int = p_input->p->input.i_seekpoint_start -
917 p_input->p->input.i_seekpoint_offset;
918 if( val.i_int > 0 /* TODO: check upper boundary */ )
919 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
923 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
924 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
925 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
926 i_length = var_GetTime( p_input, "length" );
927 if( p_input->p->i_run < 0 )
929 msg_Warn( p_input, "invalid run-time ignored" );
930 p_input->p->i_run = 0;
933 if( p_input->p->i_start > 0 )
935 if( p_input->p->i_start >= i_length )
937 msg_Warn( p_input, "invalid start-time ignored" );
943 msg_Dbg( p_input, "starting at time: %ds",
944 (int)( p_input->p->i_start / INT64_C(1000000) ) );
946 s.i_time = p_input->p->i_start;
947 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
950 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
952 msg_Warn( p_input, "invalid stop-time ignored" );
953 p_input->p->i_stop = 0;
957 /* Get fps and set it if not already set */
958 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
961 float f_requested_fps;
963 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
964 var_SetFloat( p_input, "sub-original-fps", f_fps );
966 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
967 if( f_requested_fps != f_fps )
969 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
971 var_SetFloat( p_input, "sub-fps", f_requested_fps );
975 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
978 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
981 /* Look for and add subtitle files */
982 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
983 if( psz_subtitle != NULL )
985 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
986 input_AddSubtitles( p_input, psz_subtitle, false );
989 var_Get( p_input, "sub-autodetect-file", &val );
992 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
993 char **subs = subtitles_Detect( p_input, psz_autopath,
994 p_input->p->input.p_item->psz_uri );
997 if( psz_autopath == NULL )
998 psz_autopath = strdup("");
1000 /* Try to autoselect the first autodetected subtitles file
1001 * if no subtitles file was specified */
1002 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1004 input_AddSubtitles( p_input, subs[0], false );
1009 /* Then, just add the following subtitles files */
1010 for( ; subs && subs[i]; i++ )
1012 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1014 sub = InputSourceNew( p_input );
1015 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1017 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1024 free( psz_autopath );
1026 free( psz_subtitle );
1028 /* Look for slave */
1029 psz = var_GetNonEmptyString( p_input, "input-slave" );
1033 input_source_t *slave;
1034 while( psz && *psz )
1036 while( *psz == ' ' || *psz == '#' )
1040 if( ( psz_delim = strchr( psz, '#' ) ) )
1042 *psz_delim++ = '\0';
1049 msg_Dbg( p_input, "adding slave input '%s'", psz );
1050 slave = InputSourceNew( p_input );
1051 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1053 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1062 static void InitPrograms( input_thread_t * p_input )
1067 if( p_input->b_preparsing ) return;
1070 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1071 i_es_out_mode = ES_OUT_MODE_AUTO;
1073 if( p_input->p->p_sout )
1075 var_Get( p_input, "sout-all", &val );
1078 i_es_out_mode = ES_OUT_MODE_ALL;
1083 var_Get( p_input, "programs", &val );
1084 if ( val.p_list && val.p_list->i_count )
1086 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1087 /* Note : we should remove the "program" callback. */
1090 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1094 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1096 /* Inform the demuxer about waited group (needed only for DVB) */
1097 if( i_es_out_mode == ES_OUT_MODE_ALL )
1099 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1101 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1103 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1108 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1109 (int) var_GetInteger( p_input, "program" ), NULL );
1113 static int Init( input_thread_t * p_input )
1119 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1121 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1123 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1124 var_SetString( p_input, "sout", "" );
1125 var_SetBool( p_input, "sout-all", false );
1126 var_SetString( p_input, "input-slave", "" );
1127 var_SetInteger( p_input, "input-repeat", 0 );
1128 var_SetString( p_input, "sub-file", "" );
1129 var_SetBool( p_input, "sub-autodetect-file", false );
1133 InitStatistics( p_input );
1135 ret = InitSout( p_input );
1136 if( ret != VLC_SUCCESS )
1137 return ret; /* FIXME: goto error; should be better here */
1141 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1142 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1143 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1145 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1146 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1148 if( InputSourceInit( p_input, &p_input->p->input,
1149 p_input->p->input.p_item->psz_uri, NULL ) )
1154 InitTitle( p_input );
1156 /* Load master infos */
1158 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1159 &val.i_time ) && val.i_time > 0 )
1161 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1162 UpdateItemLength( p_input, val.i_time );
1166 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1167 if( val.i_time > 0 )
1168 { /* fallback: gets length from metadata */
1169 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1170 UpdateItemLength( p_input, val.i_time );
1174 StartTitle( p_input );
1176 InitPrograms( p_input );
1178 if( !p_input->b_preparsing && p_input->p->p_sout )
1180 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1182 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1184 /* We don't want a high input priority here or we'll
1185 * end-up sucking up all the CPU time */
1186 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1189 msg_Dbg( p_input, "starting in %s mode",
1190 p_input->p->b_out_pace_control ? "async" : "sync" );
1193 p_meta = vlc_meta_New();
1195 /* Get meta data from users */
1196 InputMetaUser( p_input, p_meta );
1198 /* Get meta data from master input */
1199 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1201 /* Access_file does not give any meta, and there are no slave */
1202 AccessMeta( p_input, p_meta );
1204 InputUpdateMeta( p_input, p_meta );
1206 if( !p_input->b_preparsing )
1208 msg_Dbg( p_input, "`%s' successfully opened",
1209 p_input->p->input.p_item->psz_uri );
1213 /* initialization is complete */
1214 input_ChangeState( p_input, PLAYING_S );
1219 input_ChangeState( p_input, ERROR_S );
1221 if( p_input->p->p_es_out )
1222 input_EsOutDelete( p_input->p->p_es_out );
1224 if( p_input->p->p_sout )
1226 vlc_object_detach( p_input->p->p_sout );
1227 sout_DeleteInstance( p_input->p->p_sout );
1231 if( !p_input->b_preparsing && libvlc_stats (p_input) )
1233 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1234 stats_CounterClean( p_input->p->counters.p_##c );\
1235 p_input->p->counters.p_##c = NULL; } while(0)
1236 EXIT_COUNTER( read_bytes );
1237 EXIT_COUNTER( read_packets );
1238 EXIT_COUNTER( demux_read );
1239 EXIT_COUNTER( input_bitrate );
1240 EXIT_COUNTER( demux_bitrate );
1241 EXIT_COUNTER( played_abuffers );
1242 EXIT_COUNTER( lost_abuffers );
1243 EXIT_COUNTER( displayed_pictures );
1244 EXIT_COUNTER( lost_pictures );
1245 EXIT_COUNTER( decoded_audio );
1246 EXIT_COUNTER( decoded_video );
1247 EXIT_COUNTER( decoded_sub );
1249 if( p_input->p->p_sout )
1251 EXIT_COUNTER( sout_sent_packets );
1252 EXIT_COUNTER (sout_sent_bytes );
1253 EXIT_COUNTER( sout_send_bitrate );
1258 /* Mark them deleted */
1259 p_input->p->input.p_demux = NULL;
1260 p_input->p->input.p_stream = NULL;
1261 p_input->p->input.p_access = NULL;
1262 p_input->p->p_es_out = NULL;
1263 p_input->p->p_sout = NULL;
1265 return VLC_EGENERIC;
1268 /*****************************************************************************
1269 * Error: RunThread() error loop
1270 *****************************************************************************
1271 * This function is called when an error occurred during thread main's loop.
1272 *****************************************************************************/
1273 static void Error( input_thread_t *p_input )
1275 input_ChangeState( p_input, ERROR_S );
1276 while( !p_input->b_die )
1279 msleep( INPUT_IDLE_SLEEP );
1283 /*****************************************************************************
1284 * End: end the input thread
1285 *****************************************************************************/
1286 static void End( input_thread_t * p_input )
1290 /* We are at the end */
1291 input_ChangeState( p_input, END_S );
1293 /* Clean control variables */
1294 input_ControlVarClean( p_input );
1296 /* Clean up master */
1297 InputSourceClean( &p_input->p->input );
1300 for( i = 0; i < p_input->p->i_slave; i++ )
1302 InputSourceClean( p_input->p->slave[i] );
1303 free( p_input->p->slave[i] );
1305 free( p_input->p->slave );
1307 /* Unload all modules */
1308 if( p_input->p->p_es_out )
1309 input_EsOutDelete( p_input->p->p_es_out );
1311 if( !p_input->b_preparsing )
1313 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1314 if( libvlc_stats (p_input) )
1316 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
1318 /* make sure we are up to date */
1319 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1320 if( priv->p_stats_computer == p_input )
1322 stats_ComputeGlobalStats( p_input->p_libvlc,
1323 p_input->p_libvlc->p_stats );
1324 priv->p_stats_computer = NULL;
1326 CL_CO( read_bytes );
1327 CL_CO( read_packets );
1328 CL_CO( demux_read );
1329 CL_CO( input_bitrate );
1330 CL_CO( demux_bitrate );
1331 CL_CO( played_abuffers );
1332 CL_CO( lost_abuffers );
1333 CL_CO( displayed_pictures );
1334 CL_CO( lost_pictures );
1335 CL_CO( decoded_audio) ;
1336 CL_CO( decoded_video );
1337 CL_CO( decoded_sub) ;
1340 /* Close optional stream output instance */
1341 if( p_input->p->p_sout )
1343 CL_CO( sout_sent_packets );
1344 CL_CO( sout_sent_bytes );
1345 CL_CO( sout_send_bitrate );
1347 vlc_object_detach( p_input->p->p_sout );
1352 if( p_input->p->i_attachment > 0 )
1354 for( i = 0; i < p_input->p->i_attachment; i++ )
1355 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1356 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1359 /* Tell we're dead */
1360 p_input->b_dead = true;
1363 /*****************************************************************************
1365 *****************************************************************************/
1366 static inline int ControlPopNoLock( input_thread_t *p_input,
1367 int *pi_type, vlc_value_t *p_val )
1369 if( p_input->p->i_control <= 0 )
1371 return VLC_EGENERIC;
1374 *pi_type = p_input->p->control[0].i_type;
1375 *p_val = p_input->p->control[0].val;
1377 p_input->p->i_control--;
1378 if( p_input->p->i_control > 0 )
1382 for( i = 0; i < p_input->p->i_control; i++ )
1384 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1385 p_input->p->control[i].val = p_input->p->control[i+1].val;
1392 static void ControlReduce( input_thread_t *p_input )
1399 for( i = 1; i < p_input->p->i_control; i++ )
1401 const int i_lt = p_input->p->control[i-1].i_type;
1402 const int i_ct = p_input->p->control[i].i_type;
1404 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1405 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1409 ( i_ct == INPUT_CONTROL_SET_STATE ||
1410 i_ct == INPUT_CONTROL_SET_RATE ||
1411 i_ct == INPUT_CONTROL_SET_POSITION ||
1412 i_ct == INPUT_CONTROL_SET_TIME ||
1413 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1414 i_ct == INPUT_CONTROL_SET_TITLE ||
1415 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1416 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1419 // msg_Dbg( p_input, "merged at %d", i );
1420 /* Remove the i-1 */
1421 for( j = i; j < p_input->p->i_control; j++ )
1422 p_input->p->control[j-1] = p_input->p->control[j];
1423 p_input->p->i_control--;
1427 /* TODO but that's not that important
1428 - merge SET_X with SET_X_CMD
1429 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1430 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1437 static bool Control( input_thread_t *p_input, int i_type,
1440 bool b_force_update = false;
1442 if( !p_input ) return b_force_update;
1446 case INPUT_CONTROL_SET_DIE:
1447 msg_Dbg( p_input, "control: stopping input" );
1448 /* Mark all submodules to die */
1449 if( p_input->p->input.p_access )
1450 vlc_object_kill( p_input->p->input.p_access );
1451 if( p_input->p->input.p_stream )
1452 vlc_object_kill( p_input->p->input.p_stream );
1453 vlc_object_kill( p_input->p->input.p_demux );
1455 vlc_object_kill( p_input );
1458 case INPUT_CONTROL_SET_POSITION:
1459 case INPUT_CONTROL_SET_POSITION_OFFSET:
1462 if( i_type == INPUT_CONTROL_SET_POSITION )
1464 f_pos = val.f_float;
1468 /* Should not fail */
1469 demux_Control( p_input->p->input.p_demux,
1470 DEMUX_GET_POSITION, &f_pos );
1471 f_pos += val.f_float;
1473 if( f_pos < 0.0 ) f_pos = 0.0;
1474 if( f_pos > 1.0 ) f_pos = 1.0;
1475 /* Reset the decoders states and clock sync (before calling the demuxer */
1476 input_EsOutChangePosition( p_input->p->p_es_out );
1477 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1480 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1481 "%2.1f%% failed", f_pos * 100 );
1485 if( p_input->p->i_slave > 0 )
1486 SlaveSeek( p_input );
1488 b_force_update = true;
1493 case INPUT_CONTROL_SET_TIME:
1494 case INPUT_CONTROL_SET_TIME_OFFSET:
1499 if( i_type == INPUT_CONTROL_SET_TIME )
1501 i_time = val.i_time;
1505 /* Should not fail */
1506 demux_Control( p_input->p->input.p_demux,
1507 DEMUX_GET_TIME, &i_time );
1508 i_time += val.i_time;
1510 if( i_time < 0 ) i_time = 0;
1512 /* Reset the decoders states and clock sync (before calling the demuxer */
1513 input_EsOutChangePosition( p_input->p->p_es_out );
1515 i_ret = demux_Control( p_input->p->input.p_demux,
1516 DEMUX_SET_TIME, i_time );
1521 /* Emulate it with a SET_POS */
1522 demux_Control( p_input->p->input.p_demux,
1523 DEMUX_GET_LENGTH, &i_length );
1526 double f_pos = (double)i_time / (double)i_length;
1527 i_ret = demux_Control( p_input->p->input.p_demux,
1528 DEMUX_SET_POSITION, f_pos );
1533 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1534 " failed or not possible", i_time );
1538 if( p_input->p->i_slave > 0 )
1539 SlaveSeek( p_input );
1541 b_force_update = true;
1546 case INPUT_CONTROL_SET_STATE:
1547 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1548 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1551 if( p_input->p->input.p_access )
1552 i_ret = access_Control( p_input->p->input.p_access,
1553 ACCESS_SET_PAUSE_STATE, false );
1555 i_ret = demux_Control( p_input->p->input.p_demux,
1556 DEMUX_SET_PAUSE_STATE, false );
1560 /* FIXME What to do ? */
1561 msg_Warn( p_input, "cannot unset pause -> EOF" );
1562 vlc_mutex_unlock( &p_input->p->lock_control );
1563 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1564 vlc_mutex_lock( &p_input->p->lock_control );
1567 b_force_update = true;
1569 /* Switch to play */
1570 p_input->i_state = PLAYING_S;
1571 val.i_int = PLAYING_S;
1572 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1576 input_EsOutChangeState( p_input->p->p_es_out );
1578 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1579 p_input->p->b_can_pause )
1582 if( p_input->p->input.p_access )
1583 i_ret = access_Control( p_input->p->input.p_access,
1584 ACCESS_SET_PAUSE_STATE, true );
1586 i_ret = demux_Control( p_input->p->input.p_demux,
1587 DEMUX_SET_PAUSE_STATE, true );
1589 b_force_update = true;
1593 msg_Warn( p_input, "cannot set pause state" );
1594 val.i_int = p_input->i_state;
1598 val.i_int = PAUSE_S;
1601 /* Switch to new state */
1602 p_input->i_state = val.i_int;
1603 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1607 input_EsOutChangeState( p_input->p->p_es_out );
1609 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1611 b_force_update = true;
1613 /* Correct "state" value */
1614 val.i_int = p_input->i_state;
1615 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1617 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1619 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1623 case INPUT_CONTROL_SET_RATE:
1624 case INPUT_CONTROL_SET_RATE_SLOWER:
1625 case INPUT_CONTROL_SET_RATE_FASTER:
1629 if( i_type == INPUT_CONTROL_SET_RATE )
1635 static const int ppi_factor[][2] = {
1636 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1638 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1647 for( i = 0; ppi_factor[i][0] != 0; i++ )
1649 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1650 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1651 if( i_test_e < i_error )
1657 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1659 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1661 if( ppi_factor[i_idx+1][0] > 0 )
1662 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1664 i_rate = INPUT_RATE_MAX+1;
1668 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1670 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1672 i_rate = INPUT_RATE_MIN-1;
1676 if( i_rate < INPUT_RATE_MIN )
1678 msg_Dbg( p_input, "cannot set rate faster" );
1679 i_rate = INPUT_RATE_MIN;
1681 else if( i_rate > INPUT_RATE_MAX )
1683 msg_Dbg( p_input, "cannot set rate slower" );
1684 i_rate = INPUT_RATE_MAX;
1686 if( i_rate != INPUT_RATE_DEFAULT &&
1687 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1688 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1690 msg_Dbg( p_input, "cannot change rate" );
1691 i_rate = INPUT_RATE_DEFAULT;
1693 if( i_rate != p_input->p->i_rate &&
1694 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1697 if( p_input->p->input.p_access )
1698 i_ret = VLC_EGENERIC;
1700 i_ret = demux_Control( p_input->p->input.p_demux,
1701 DEMUX_SET_RATE, &i_rate );
1704 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1705 i_rate = p_input->p->i_rate;
1710 if( i_rate != p_input->p->i_rate )
1713 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1714 var_SetBool( p_input, "rate-change", true );
1716 p_input->p->i_rate = i_rate;
1718 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1719 if( p_input->p->input.b_rescale_ts )
1720 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1722 b_force_update = true;
1727 case INPUT_CONTROL_SET_PROGRAM:
1728 /* No need to force update, es_out does it if needed */
1729 es_out_Control( p_input->p->p_es_out,
1730 ES_OUT_SET_GROUP, val.i_int );
1732 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1736 case INPUT_CONTROL_SET_ES:
1737 /* No need to force update, es_out does it if needed */
1738 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1739 input_EsOutGetFromID( p_input->p->p_es_out,
1743 case INPUT_CONTROL_SET_AUDIO_DELAY:
1744 input_EsOutSetDelay( p_input->p->p_es_out,
1745 AUDIO_ES, val.i_time );
1746 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1749 case INPUT_CONTROL_SET_SPU_DELAY:
1750 input_EsOutSetDelay( p_input->p->p_es_out,
1751 SPU_ES, val.i_time );
1752 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1755 case INPUT_CONTROL_SET_TITLE:
1756 case INPUT_CONTROL_SET_TITLE_NEXT:
1757 case INPUT_CONTROL_SET_TITLE_PREV:
1758 if( p_input->p->input.b_title_demux &&
1759 p_input->p->input.i_title > 0 )
1762 /* FIXME handle demux title */
1763 demux_t *p_demux = p_input->p->input.p_demux;
1766 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1767 i_title = p_demux->info.i_title - 1;
1768 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1769 i_title = p_demux->info.i_title + 1;
1771 i_title = val.i_int;
1773 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1775 input_EsOutChangePosition( p_input->p->p_es_out );
1777 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1778 input_ControlVarTitle( p_input, i_title );
1781 else if( p_input->p->input.i_title > 0 )
1783 access_t *p_access = p_input->p->input.p_access;
1786 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1787 i_title = p_access->info.i_title - 1;
1788 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1789 i_title = p_access->info.i_title + 1;
1791 i_title = val.i_int;
1793 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1795 input_EsOutChangePosition( p_input->p->p_es_out );
1797 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1798 stream_AccessReset( p_input->p->input.p_stream );
1802 case INPUT_CONTROL_SET_SEEKPOINT:
1803 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1804 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1805 if( p_input->p->input.b_title_demux &&
1806 p_input->p->input.i_title > 0 )
1808 demux_t *p_demux = p_input->p->input.p_demux;
1810 int64_t i_input_time;
1811 int64_t i_seekpoint_time;
1813 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1815 i_seekpoint = p_demux->info.i_seekpoint;
1816 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1817 if( i_seekpoint_time >= 0 &&
1818 !demux_Control( p_demux,
1819 DEMUX_GET_TIME, &i_input_time ) )
1821 if ( i_input_time < i_seekpoint_time + 3000000 )
1827 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1828 i_seekpoint = p_demux->info.i_seekpoint + 1;
1830 i_seekpoint = val.i_int;
1832 if( i_seekpoint >= 0 && i_seekpoint <
1833 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1836 input_EsOutChangePosition( p_input->p->p_es_out );
1838 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1841 else if( p_input->p->input.i_title > 0 )
1843 demux_t *p_demux = p_input->p->input.p_demux;
1844 access_t *p_access = p_input->p->input.p_access;
1846 int64_t i_input_time;
1847 int64_t i_seekpoint_time;
1849 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1851 i_seekpoint = p_access->info.i_seekpoint;
1852 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1853 if( i_seekpoint_time >= 0 &&
1854 demux_Control( p_demux,
1855 DEMUX_GET_TIME, &i_input_time ) )
1857 if ( i_input_time < i_seekpoint_time + 3000000 )
1863 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1864 i_seekpoint = p_access->info.i_seekpoint + 1;
1866 i_seekpoint = val.i_int;
1868 if( i_seekpoint >= 0 && i_seekpoint <
1869 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1871 input_EsOutChangePosition( p_input->p->p_es_out );
1873 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1875 stream_AccessReset( p_input->p->input.p_stream );
1880 case INPUT_CONTROL_ADD_SLAVE:
1881 if( val.psz_string )
1883 input_source_t *slave = InputSourceNew( p_input );
1885 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1891 msg_Dbg( p_input, "adding %s as slave on the fly",
1895 if( demux_Control( p_input->p->input.p_demux,
1896 DEMUX_GET_TIME, &i_time ) )
1898 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1899 InputSourceClean( slave );
1903 if( demux_Control( slave->p_demux,
1904 DEMUX_SET_TIME, i_time ) )
1906 msg_Err( p_input, "seek failed for new slave" );
1907 InputSourceClean( slave );
1912 /* Get meta (access and demux) */
1913 p_meta = vlc_meta_New();
1914 access_Control( slave->p_access, ACCESS_GET_META,
1916 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1917 InputUpdateMeta( p_input, p_meta );
1919 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1924 msg_Warn( p_input, "failed to add %s as slave",
1928 free( val.psz_string );
1932 case INPUT_CONTROL_SET_BOOKMARK:
1934 msg_Err( p_input, "not yet implemented" );
1938 return b_force_update;
1941 /*****************************************************************************
1943 *****************************************************************************/
1944 static int UpdateFromDemux( input_thread_t *p_input )
1946 demux_t *p_demux = p_input->p->input.p_demux;
1949 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1951 v.i_int = p_demux->info.i_title;
1952 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1954 input_ControlVarTitle( p_input, p_demux->info.i_title );
1956 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1958 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1960 v.i_int = p_demux->info.i_seekpoint;
1961 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1963 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1965 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1967 /* Hmmm only works with master input */
1968 if( p_input->p->input.p_demux == p_demux )
1970 int i_title_end = p_input->p->input.i_title_end -
1971 p_input->p->input.i_title_offset;
1972 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1973 p_input->p->input.i_seekpoint_offset;
1975 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1977 if( p_demux->info.i_title > i_title_end ||
1978 ( p_demux->info.i_title == i_title_end &&
1979 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1981 else if( i_seekpoint_end >=0 )
1983 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1985 else if( i_title_end >= 0 )
1987 if( p_demux->info.i_title > i_title_end ) return 0;
1994 /*****************************************************************************
1996 *****************************************************************************/
1997 static int UpdateFromAccess( input_thread_t *p_input )
1999 access_t *p_access = p_input->p->input.p_access;
2002 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2004 v.i_int = p_access->info.i_title;
2005 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2007 input_ControlVarTitle( p_input, p_access->info.i_title );
2009 stream_AccessUpdate( p_input->p->input.p_stream );
2011 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2013 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2015 v.i_int = p_access->info.i_seekpoint;
2016 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2017 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2019 if( p_access->info.i_update & INPUT_UPDATE_META )
2021 /* TODO maybe multi - access ? */
2022 vlc_meta_t *p_meta = vlc_meta_New();
2023 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2024 InputUpdateMeta( p_input, p_meta );
2025 p_access->info.i_update &= ~INPUT_UPDATE_META;
2028 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2030 /* Hmmm only works with master input */
2031 if( p_input->p->input.p_access == p_access )
2033 int i_title_end = p_input->p->input.i_title_end -
2034 p_input->p->input.i_title_offset;
2035 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2036 p_input->p->input.i_seekpoint_offset;
2038 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2040 if( p_access->info.i_title > i_title_end ||
2041 ( p_access->info.i_title == i_title_end &&
2042 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2044 else if( i_seekpoint_end >=0 )
2046 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2048 else if( i_title_end >= 0 )
2050 if( p_access->info.i_title > i_title_end ) return 0;
2057 /*****************************************************************************
2059 *****************************************************************************/
2060 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2062 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2065 /*****************************************************************************
2067 *****************************************************************************/
2068 static input_source_t *InputSourceNew( input_thread_t *p_input )
2070 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2072 memset( in, 0, sizeof( input_source_t ) );
2076 /*****************************************************************************
2078 *****************************************************************************/
2079 static int InputSourceInit( input_thread_t *p_input,
2080 input_source_t *in, const char *psz_mrl,
2081 const char *psz_forced_demux )
2083 char psz_dup[strlen (psz_mrl) + 1];
2084 const char *psz_access;
2085 const char *psz_demux;
2092 strcpy( psz_dup, psz_mrl );
2094 if( !in ) return VLC_EGENERIC;
2095 if( !p_input ) return VLC_EGENERIC;
2098 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2100 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2101 psz_mrl, psz_access, psz_demux, psz_path );
2102 if( !p_input->b_preparsing )
2104 /* Hack to allow udp://@:port syntax */
2106 (strncmp( psz_access, "udp", 3 ) &&
2107 strncmp( psz_access, "rtp", 3 )) )
2109 /* Find optional titles and seekpoints */
2110 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2111 &in->i_seekpoint_start, &in->i_seekpoint_end );
2114 if( psz_forced_demux && *psz_forced_demux )
2116 psz_demux = psz_forced_demux;
2118 else if( *psz_demux == '\0' )
2120 /* special hack for forcing a demuxer with --demux=module
2121 * (and do nothing with a list) */
2122 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2124 if( psz_var_demux != NULL &&
2125 !strchr(psz_var_demux, ',' ) &&
2126 !strchr(psz_var_demux, ':' ) )
2128 psz_demux = psz_var_demux;
2130 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2134 /* Try access_demux if no demux given */
2135 if( *psz_demux == '\0' )
2137 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2138 NULL, p_input->p->p_es_out, false );
2143 /* Preparsing is only for file:// */
2146 if( !*psz_access ) /* path without scheme:// */
2147 psz_access = "file";
2148 if( strcmp( psz_access, "file" ) )
2150 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2155 int64_t i_pts_delay;
2157 /* Get infos from access_demux */
2158 demux_Control( in->p_demux,
2159 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2160 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2162 in->b_title_demux = true;
2163 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2164 &in->title, &in->i_title,
2165 &in->i_title_offset, &in->i_seekpoint_offset ) )
2170 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2171 &in->b_can_pace_control ) )
2172 in->b_can_pace_control = false;
2174 if( !in->b_can_pace_control )
2176 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2177 &in->b_can_rate_control, &in->b_rescale_ts ) )
2179 in->b_can_rate_control = false;
2180 in->b_rescale_ts = true; /* not used */
2185 in->b_can_rate_control = true;
2186 in->b_rescale_ts = true;
2188 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2189 &in->b_can_pause ) )
2190 in->b_can_pause = false;
2191 var_SetBool( p_input, "can-pause", in->b_can_pause );
2193 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2195 if( ret != VLC_SUCCESS )
2197 var_Set( p_input, "seekable", val );
2201 int64_t i_pts_delay;
2203 input_ChangeState( p_input, OPENING_S );
2205 /* Now try a real access */
2206 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2208 /* Access failed, URL encoded ? */
2209 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2211 decode_URI( psz_path );
2213 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2214 psz_access, psz_demux, psz_path );
2216 in->p_access = access_New( p_input,
2217 psz_access, psz_demux, psz_path );
2219 if( in->p_access == NULL )
2221 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2223 intf_UserFatal( VLC_OBJECT( p_input), false,
2224 _("Your input can't be opened"),
2225 _("VLC is unable to open the MRL '%s'."
2226 " Check the log for details."), psz_mrl );
2231 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2232 while( psz && *psz )
2234 access_t *p_access = in->p_access;
2235 char *end = strchr( psz, ':' );
2240 in->p_access = access_FilterNew( in->p_access, psz );
2241 if( in->p_access == NULL )
2243 in->p_access = p_access;
2244 msg_Warn( p_input, "failed to insert access filter %s",
2252 /* Get infos from access */
2253 if( !p_input->b_preparsing )
2255 access_Control( in->p_access,
2256 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2257 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2259 in->b_title_demux = false;
2260 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2261 &in->title, &in->i_title,
2262 &in->i_title_offset, &in->i_seekpoint_offset ) )
2268 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2269 &in->b_can_pace_control );
2270 in->b_can_rate_control = in->b_can_pace_control;
2271 in->b_rescale_ts = true;
2273 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2275 var_SetBool( p_input, "can-pause", in->b_can_pause );
2276 access_Control( in->p_access, ACCESS_CAN_SEEK,
2278 var_Set( p_input, "seekable", val );
2281 input_ChangeState( p_input, BUFFERING_S );
2283 /* Create the stream_t */
2284 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2285 if( in->p_stream == NULL )
2287 msg_Warn( p_input, "cannot create a stream_t from access" );
2291 /* Open a demuxer */
2292 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2294 psz_demux = in->p_access->psz_demux;
2298 /* Take access redirections into account */
2299 char *psz_real_path;
2300 char *psz_buf = NULL;
2301 if( in->p_access->psz_path )
2303 const char *psz_a, *psz_d;
2304 psz_buf = strdup( in->p_access->psz_path );
2305 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2309 psz_real_path = psz_path;
2311 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2313 in->p_stream, p_input->p->p_es_out,
2314 p_input->b_preparsing );
2318 if( in->p_demux == NULL )
2320 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2321 psz_access, psz_demux, psz_path );
2322 intf_UserFatal( VLC_OBJECT( p_input ), false,
2323 _("VLC can't recognize the input's format"),
2324 _("The format of '%s' cannot be detected. "
2325 "Have a look the log for details."), psz_mrl );
2329 /* Get title from demux */
2330 if( !p_input->b_preparsing && in->i_title <= 0 )
2332 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2333 &in->title, &in->i_title,
2334 &in->i_title_offset, &in->i_seekpoint_offset ))
2336 TAB_INIT( in->i_title, in->title );
2340 in->b_title_demux = true;
2346 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2347 if( 1 || !p_input->b_preparsing )
2350 input_attachment_t **attachment;
2351 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2352 &attachment, &i_attachment ) )
2354 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2355 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2356 i_attachment, attachment );
2357 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2360 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2362 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2364 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2367 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2368 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2373 input_ChangeState( p_input, ERROR_S );
2376 demux_Delete( in->p_demux );
2379 stream_Delete( in->p_stream );
2382 access_Delete( in->p_access );
2384 return VLC_EGENERIC;
2387 /*****************************************************************************
2389 *****************************************************************************/
2390 static void InputSourceClean( input_source_t *in )
2395 demux_Delete( in->p_demux );
2398 stream_Delete( in->p_stream );
2401 access_Delete( in->p_access );
2403 if( in->i_title > 0 )
2405 for( i = 0; i < in->i_title; i++ )
2406 vlc_input_title_Delete( in->title[i] );
2407 TAB_CLEAN( in->i_title, in->title );
2411 static void SlaveDemux( input_thread_t *p_input )
2416 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2418 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2422 for( i = 0; i < p_input->p->i_slave; i++ )
2424 input_source_t *in = p_input->p->slave[i];
2430 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2435 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2437 msg_Err( p_input, "slave[%d] doesn't like "
2438 "DEMUX_GET_TIME -> EOF", i );
2443 if( i_stime >= i_time )
2446 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2452 i_ret = in->p_demux->pf_demux( in->p_demux );
2457 msg_Dbg( p_input, "slave %d EOF", i );
2463 static void SlaveSeek( input_thread_t *p_input )
2468 if( !p_input ) return;
2470 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2472 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2476 for( i = 0; i < p_input->p->i_slave; i++ )
2478 input_source_t *in = p_input->p->slave[i];
2480 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2482 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2488 /*****************************************************************************
2490 *****************************************************************************/
2491 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2495 if( !p_meta ) return;
2497 /* Get meta information from user */
2498 #define GET_META( field, s ) \
2499 var_Get( p_input, (s), &val ); \
2500 if( *val.psz_string ) \
2501 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2502 free( val.psz_string )
2504 GET_META( Title, "meta-title" );
2505 GET_META( Artist, "meta-artist" );
2506 GET_META( Genre, "meta-genre" );
2507 GET_META( Copyright, "meta-copyright" );
2508 GET_META( Description, "meta-description" );
2509 GET_META( Date, "meta-date" );
2510 GET_META( URL, "meta-url" );
2514 /*****************************************************************************
2515 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2516 * arturl and locking issue.
2517 *****************************************************************************/
2518 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2520 input_item_t *p_item = p_input->p->input.p_item;
2521 char * psz_arturl = NULL;
2522 char *psz_title = NULL;
2523 int i_arturl_event = false;
2528 psz_arturl = input_item_GetArtURL( p_item );
2530 vlc_mutex_lock( &p_item->lock );
2531 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2532 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2534 vlc_meta_Merge( p_item->p_meta, p_meta );
2536 if( psz_arturl && *psz_arturl )
2538 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2539 i_arturl_event = true;
2542 vlc_meta_Delete( p_meta );
2544 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2546 /* Don't look for art cover if sout
2547 * XXX It can change when sout has meta data support */
2548 if( p_input->p->p_sout && !p_input->b_preparsing )
2550 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2551 i_arturl_event = true;
2555 input_ExtractAttachmentAndCacheArt( p_input );
2561 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2563 p_meta = vlc_meta_New();
2564 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2566 vlc_mutex_unlock( &p_item->lock );
2568 input_item_SetPreparsed( p_item, true );
2570 if( i_arturl_event == true )
2574 /* Notify interested third parties */
2575 event.type = vlc_InputItemMetaChanged;
2576 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2577 vlc_event_send( &p_item->event_manager, &event );
2582 input_Control( p_input, INPUT_SET_NAME, psz_title );
2586 /** \todo handle sout meta */
2590 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2591 int i_new, input_attachment_t **pp_new )
2593 int i_attachment = *pi_attachment;
2594 input_attachment_t **attachment = *ppp_attachment;
2597 attachment = realloc( attachment,
2598 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2599 for( i = 0; i < i_new; i++ )
2600 attachment[i_attachment++] = pp_new[i];
2604 *pi_attachment = i_attachment;
2605 *ppp_attachment = attachment;
2608 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2612 if( p_input->b_preparsing )
2615 if( p_input->p->input.p_access )
2616 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2619 /* Get meta data from slave input */
2620 for( i = 0; i < p_input->p->i_slave; i++ )
2622 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2623 if( p_input->p->slave[i]->p_access )
2625 access_Control( p_input->p->slave[i]->p_access,
2626 ACCESS_GET_META, p_meta );
2631 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2638 /* XXX I am not sure it is a great idea, besides, there is more than that
2639 * if we want to do it right */
2640 vlc_mutex_lock( &p_item->lock );
2641 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2643 vlc_mutex_unlock( &p_item->lock );
2646 vlc_mutex_unlock( &p_item->lock );
2649 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2650 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2655 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2656 if(! p_demux->p_private )
2659 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2662 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2664 if( p_demux_meta->p_meta )
2666 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2667 vlc_meta_Delete( p_demux_meta->p_meta );
2670 if( p_demux_meta->i_attachments > 0 )
2672 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2673 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2674 p_demux_meta->i_attachments, p_demux_meta->attachments );
2675 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2677 module_Unneed( p_demux, p_id3 );
2679 free( p_demux->p_private );
2683 /*****************************************************************************
2684 * MRLSplit: parse the access, demux and url part of the
2685 * Media Resource Locator.
2686 *****************************************************************************/
2687 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2690 char *psz_access = NULL;
2691 char *psz_demux = NULL;
2694 /* Either there is an access/demux specification before ://
2695 * or we have a plain local file path. */
2696 psz_path = strstr( psz_dup, "://" );
2697 if( psz_path != NULL )
2700 psz_path += 3; /* skips "://" */
2702 /* Separate access from demux (<access>/<demux>://<path>) */
2703 psz_access = psz_dup;
2704 psz_demux = strchr( psz_access, '/' );
2706 *psz_demux++ = '\0';
2708 /* We really don't want module name substitution here! */
2709 if( psz_access[0] == '$' )
2711 if( psz_demux && psz_demux[0] == '$' )
2719 *ppsz_access = psz_access ? psz_access : "";
2720 *ppsz_demux = psz_demux ? psz_demux : "";
2721 *ppsz_path = psz_path ? psz_path : "";
2724 /*****************************************************************************
2725 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2728 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2729 *****************************************************************************/
2730 static void MRLSections( input_thread_t *p_input, char *psz_source,
2731 int *pi_title_start, int *pi_title_end,
2732 int *pi_chapter_start, int *pi_chapter_end )
2734 char *psz, *psz_end, *psz_next, *psz_check;
2736 *pi_title_start = *pi_title_end = -1;
2737 *pi_chapter_start = *pi_chapter_end = -1;
2739 /* Start by parsing titles and chapters */
2740 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2742 /* Check we are really dealing with a title/chapter section */
2743 psz_check = psz + 1;
2744 if( !*psz_check ) return;
2745 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2746 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2747 if( *psz_check == ':' && ++psz_check )
2748 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2749 if( *psz_check != '-' && *psz_check ) return;
2750 if( *psz_check == '-' && ++psz_check )
2751 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2752 if( *psz_check != ':' && *psz_check ) return;
2753 if( *psz_check == ':' && ++psz_check )
2754 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2755 if( *psz_check ) return;
2757 /* Separate start and end */
2759 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2761 /* Look for the start title */
2762 *pi_title_start = strtol( psz, &psz_next, 0 );
2763 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2764 *pi_title_end = *pi_title_start;
2767 /* Look for the start chapter */
2769 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2770 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2771 *pi_chapter_end = *pi_chapter_start;
2775 /* Look for the end title */
2776 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2777 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2780 /* Look for the end chapter */
2781 if( *psz_end ) psz_end++;
2782 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2783 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2786 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2787 psz_source, *pi_title_start, *pi_chapter_start,
2788 *pi_title_end, *pi_chapter_end );
2791 /*****************************************************************************
2792 * input_AddSubtitles: add a subtitles file and enable it
2793 *****************************************************************************/
2794 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2795 bool b_check_extension )
2797 input_source_t *sub;
2800 char *psz_path, *psz_extension;
2802 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2807 /* if we are provided a subtitle.sub file,
2808 * see if we don't have a subtitle.idx and use it instead */
2809 psz_path = strdup( psz_subtitle );
2812 psz_extension = strrchr( psz_path, '.');
2813 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2817 strcpy( psz_extension, ".idx" );
2818 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2819 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2822 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2823 psz_path, psz_subtitle );
2824 strcpy( psz_subtitle, psz_path );
2830 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2832 sub = InputSourceNew( p_input );
2833 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2835 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2838 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2840 if( count.i_int == 0 )
2842 /* if it was first one, there is disable too */
2844 if( count.i_int < list.p_list->i_count )
2846 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2847 &list.p_list->p_values[count.i_int] );
2849 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2857 /*****************************************************************************
2858 * input_get_event_manager
2859 *****************************************************************************/
2860 vlc_event_manager_t *
2861 input_get_event_manager( input_thread_t *p_input )
2863 return &p_input->p->event_manager;