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 void* Run ( vlc_object_t *p_this );
57 static void* RunAndDestroy ( vlc_object_t *p_this );
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 WaitDie ( 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 * - teletext-es to get the index of spu track that is teletext --1 if no teletext)
116 * * For intf callback upon changes
118 * - rate-change for when playback rate changes
119 * TODO explain when Callback is called
120 * TODO complete this list (?)
121 *****************************************************************************/
122 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
123 const char *psz_header, bool b_quick,
124 sout_instance_t *p_sout )
126 static const char input_name[] = "input";
127 input_thread_t *p_input = NULL; /* thread descriptor */
131 /* Allocate descriptor */
132 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
133 VLC_OBJECT_INPUT, input_name );
134 if( p_input == NULL )
137 /* Construct a nice name for the input timer */
138 char psz_timer_name[255];
139 char * psz_name = input_item_GetName( p_item );
140 snprintf( psz_timer_name, sizeof(psz_timer_name),
141 "input launching for '%s'", psz_name );
143 msg_Dbg( p_input, "Creating an input for '%s'", psz_name);
147 /* Start a timer to mesure how long it takes
148 * to launch an input */
149 stats_TimerStart( p_input, psz_timer_name,
150 STATS_TIMER_INPUT_LAUNCHING );
152 MALLOC_NULL( p_input->p, input_thread_private_t );
153 memset( p_input->p, 0, sizeof( input_thread_private_t ) );
155 /* One "randomly" selected input thread is responsible for computing
156 * the global stats. Check if there is already someone doing this */
157 if( p_input->p_libvlc->p_stats && !b_quick )
159 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
160 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
161 if( priv->p_stats_computer == NULL )
162 priv->p_stats_computer = p_input;
163 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
166 p_input->b_preparsing = b_quick;
167 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
170 vlc_event_manager_t * p_em = &p_input->p->event_manager;
171 vlc_event_manager_init_with_vlc_object( p_em, p_input );
172 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
173 vlc_event_manager_register_event_type( p_em, vlc_InputSelectedStreamChanged );
175 /* Init Common fields */
176 p_input->b_eof = false;
177 p_input->b_can_pace_control = true;
178 p_input->p->i_start = 0;
180 p_input->p->i_stop = 0;
181 p_input->p->i_run = 0;
182 p_input->p->i_title = 0;
183 p_input->p->title = NULL;
184 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
185 p_input->i_state = INIT_S;
186 p_input->p->i_rate = INPUT_RATE_DEFAULT;
187 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
188 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
189 p_input->p->p_es_out = NULL;
190 p_input->p->p_sout = NULL;
191 p_input->p->b_out_pace_control = false;
192 p_input->i_pts_delay = 0;
194 /* Init Input fields */
195 vlc_gc_incref( p_item ); /* Released in Destructor() */
196 p_input->p->input.p_item = p_item;
197 p_input->p->input.p_access = NULL;
198 p_input->p->input.p_stream = NULL;
199 p_input->p->input.p_demux = NULL;
200 p_input->p->input.b_title_demux = false;
201 p_input->p->input.i_title = 0;
202 p_input->p->input.title = NULL;
203 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
204 p_input->p->input.b_can_pace_control = true;
205 p_input->p->input.b_can_rate_control = true;
206 p_input->p->input.b_rescale_ts = true;
207 p_input->p->input.b_eof = false;
208 p_input->p->input.i_cr_average = 0;
210 vlc_mutex_lock( &p_item->lock );
212 if( !p_item->p_stats )
213 p_item->p_stats = stats_NewInputStats( p_input );
214 vlc_mutex_unlock( &p_item->lock );
217 p_input->p->i_slave = 0;
218 p_input->p->slave = NULL;
220 /* Init control buffer */
221 vlc_mutex_init( &p_input->p->lock_control );
222 p_input->p->i_control = 0;
224 /* Parse input options */
225 vlc_mutex_lock( &p_item->lock );
226 assert( (int)p_item->optflagc == p_item->i_options );
227 for( i = 0; i < p_item->i_options; i++ )
228 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
229 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
230 vlc_mutex_unlock( &p_item->lock );
232 /* Create Object Variables for private use only */
233 input_ConfigVarInit( p_input );
235 /* Create Objects variables for public Get and Set */
236 input_ControlVarInit( p_input );
238 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
240 if( !p_input->b_preparsing )
242 var_Get( p_input, "bookmarks", &val );
245 /* FIXME: have a common cfg parsing routine used by sout and others */
246 char *psz_parser, *psz_start, *psz_end;
247 psz_parser = val.psz_string;
248 while( (psz_start = strchr( psz_parser, '{' ) ) )
250 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
253 psz_end = strchr( psz_start, '}' );
254 if( !psz_end ) break;
255 psz_parser = psz_end + 1;
256 backup = *psz_parser;
259 while( (psz_end = strchr( psz_start, ',' ) ) )
262 if( !strncmp( psz_start, "name=", 5 ) )
264 p_seekpoint->psz_name = strdup(psz_start + 5);
266 else if( !strncmp( psz_start, "bytes=", 6 ) )
268 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
270 else if( !strncmp( psz_start, "time=", 5 ) )
272 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
275 psz_start = psz_end + 1;
277 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
278 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
279 p_seekpoint->i_time_offset );
280 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
281 vlc_seekpoint_Delete( p_seekpoint );
282 *psz_parser = backup;
284 free( val.psz_string );
288 /* Remove 'Now playing' info as it is probably outdated */
289 input_item_SetNowPlaying( p_item, NULL );
292 if( p_input->b_preparsing )
293 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
297 p_input->p->p_sout = p_sout;
299 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
300 vlc_mutex_init( &p_input->p->counters.counters_lock );
302 /* Set the destructor when we are sure we are initialized */
303 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
305 /* Attach only once we are ready */
306 vlc_object_attach( p_input, p_parent );
312 * Input destructor (called when the object's refcount reaches 0).
314 static void Destructor( input_thread_t * p_input )
316 input_thread_private_t *priv = p_input->p;
319 char * psz_name = input_item_GetName( p_input->p->input.p_item );
320 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
324 vlc_event_manager_fini( &p_input->p->event_manager );
326 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
327 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
330 sout_DeleteInstance( priv->p_sout );
332 vlc_gc_decref( p_input->p->input.p_item );
334 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
336 vlc_mutex_destroy( &priv->lock_control );
341 * Initialize an input thread and run it. You will need to monitor the
342 * thread to clean up after it is done
344 * \param p_parent a vlc_object
345 * \param p_item an input item
346 * \return a pointer to the spawned input thread
348 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
349 input_item_t *p_item )
351 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
355 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
356 input_item_t *p_item,
357 const char *psz_log, sout_instance_t *p_sout )
359 input_thread_t *p_input;
361 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
365 /* Create thread and wait for its readiness. */
366 if( vlc_thread_create( p_input, "input", Run,
367 VLC_THREAD_PRIORITY_INPUT, true ) )
369 input_ChangeState( p_input, ERROR_S );
370 msg_Err( p_input, "cannot create input thread" );
371 vlc_object_detach( p_input );
372 vlc_object_release( p_input );
380 * Initialize an input thread and run it. This thread will clean after itself,
381 * you can forget about it. It can work either in blocking or non-blocking mode
383 * \param p_parent a vlc_object
384 * \param p_item an input item
385 * \param b_block should we block until read is finished ?
386 * \return the input object id if non blocking, an error code else
388 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
391 input_thread_t *p_input;
393 p_input = Create( p_parent, p_item, NULL, false, NULL );
399 RunAndDestroy( VLC_OBJECT(p_input) );
404 if( vlc_thread_create( p_input, "input", RunAndDestroy,
405 VLC_THREAD_PRIORITY_INPUT, true ) )
407 input_ChangeState( p_input, ERROR_S );
408 msg_Err( p_input, "cannot create input thread" );
409 vlc_object_release( p_input );
413 return p_input->i_object_id;
417 * Initialize an input and initialize it to preparse the item
418 * This function is blocking. It will only accept to parse files
420 * \param p_parent a vlc_object_t
421 * \param p_item an input item
422 * \return VLC_SUCCESS or an error
424 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
426 input_thread_t *p_input;
428 /* Allocate descriptor */
429 p_input = Create( p_parent, p_item, NULL, true, NULL );
433 if( !Init( p_input ) )
436 vlc_object_detach( p_input );
437 vlc_object_release( p_input );
443 * Request a running input thread to stop and die
445 * \param the input thread to stop
447 static void ObjectKillChildrens( input_thread_t *p_input, vlc_object_t *p_obj )
452 if( p_obj->i_object_type == VLC_OBJECT_VOUT ||
453 p_obj->i_object_type == VLC_OBJECT_AOUT ||
454 p_obj == VLC_OBJECT(p_input->p->p_sout) )
457 vlc_object_kill( p_obj );
459 p_list = vlc_list_children( p_obj );
460 for( i = 0; i < p_list->i_count; i++ )
461 ObjectKillChildrens( p_input, p_list->p_values[i].p_object );
462 vlc_list_release( p_list );
464 void input_StopThread( input_thread_t *p_input )
466 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
467 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
468 * unlock the control loop */
469 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
471 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
474 sout_instance_t * input_DetachSout( input_thread_t *p_input )
476 sout_instance_t *p_sout = p_input->p->p_sout;
477 vlc_object_detach( p_sout );
478 p_input->p->p_sout = NULL;
482 /*****************************************************************************
483 * Run: main thread loop
484 * This is the "normal" thread that spawns the input processing chain,
485 * reads the stream, cleans up and waits
486 *****************************************************************************/
487 static void* Run( vlc_object_t *p_this )
489 input_thread_t *p_input = (input_thread_t *)p_this;
490 /* Signal that the thread is launched */
491 vlc_thread_ready( p_input );
493 if( Init( p_input ) )
495 /* If we failed, wait before we are killed, and exit */
496 p_input->b_error = true;
500 /* Tell we're dead */
501 p_input->b_dead = true;
508 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
510 /* We have finish to demux data but not to play them */
511 while( !p_input->b_die )
513 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
516 msg_Dbg( p_input, "waiting decoder fifos to empty" );
518 msleep( INPUT_IDLE_SLEEP );
521 /* We have finished */
522 p_input->b_eof = true;
523 input_ChangeState( p_input, END_S );
526 /* Wait until we are asked to die */
527 if( !p_input->b_die )
538 /*****************************************************************************
539 * RunAndDestroy: main thread loop
540 * This is the "just forget me" thread that spawns the input processing chain,
541 * reads the stream, cleans up and releases memory
542 *****************************************************************************/
543 static void* RunAndDestroy( vlc_object_t *p_this )
545 input_thread_t *p_input = (input_thread_t *)p_this;
546 /* Signal that the thread is launched */
547 vlc_thread_ready( p_input );
549 if( Init( p_input ) )
554 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
556 /* We have finished demuxing data but not playing it */
557 while( !p_input->b_die )
559 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
562 msg_Dbg( p_input, "waiting decoder fifos to empty" );
564 msleep( INPUT_IDLE_SLEEP );
567 /* We have finished */
568 p_input->b_eof = true;
576 vlc_object_release( p_input );
580 /*****************************************************************************
581 * Main loop: Fill buffers from access, and demux
582 *****************************************************************************/
583 static void MainLoop( input_thread_t *p_input )
585 int64_t i_start_mdate = mdate();
586 int64_t i_intf_update = 0;
590 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
592 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
594 bool b_force_update = false;
600 if( p_input->i_state != PAUSE_S )
602 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
603 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
606 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
611 if( p_input->p->input.b_title_demux &&
612 p_input->p->input.p_demux->info.i_update )
614 i_ret = UpdateFromDemux( p_input );
615 b_force_update = true;
617 else if( !p_input->p->input.b_title_demux &&
618 p_input->p->input.p_access &&
619 p_input->p->input.p_access->info.i_update )
621 i_ret = UpdateFromAccess( p_input );
622 b_force_update = true;
626 if( i_ret == 0 ) /* EOF */
630 var_Get( p_input, "input-repeat", &repeat );
631 if( repeat.i_int == 0 )
633 /* End of file - we do not set b_die because only the
634 * playlist is allowed to do so. */
635 msg_Dbg( p_input, "EOF reached" );
636 p_input->p->input.b_eof = true;
640 msg_Dbg( p_input, "repeating the same input (%d)",
642 if( repeat.i_int > 0 )
645 var_Set( p_input, "input-repeat", repeat );
648 /* Seek to start title/seekpoint */
649 val.i_int = p_input->p->input.i_title_start -
650 p_input->p->input.i_title_offset;
651 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
653 input_ControlPush( p_input,
654 INPUT_CONTROL_SET_TITLE, &val );
656 val.i_int = p_input->p->input.i_seekpoint_start -
657 p_input->p->input.i_seekpoint_offset;
658 if( val.i_int > 0 /* TODO: check upper boundary */ )
659 input_ControlPush( p_input,
660 INPUT_CONTROL_SET_SEEKPOINT, &val );
662 /* Seek to start position */
663 if( p_input->p->i_start > 0 )
665 val.i_time = p_input->p->i_start;
666 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
672 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
677 i_start_mdate = mdate();
682 p_input->b_error = true;
685 if( i_ret > 0 && p_input->p->i_slave > 0 )
687 SlaveDemux( p_input );
697 vlc_mutex_lock( &p_input->p->lock_control );
698 ControlReduce( p_input );
699 while( !ControlPopNoLock( p_input, &i_type, &val ) )
701 msg_Dbg( p_input, "control type=%d", i_type );
702 if( Control( p_input, i_type, val ) )
703 b_force_update = true;
705 vlc_mutex_unlock( &p_input->p->lock_control );
707 if( b_force_update || i_intf_update < mdate() )
711 int64_t i_time, i_length;
712 /* update input status variables */
713 if( !demux_Control( p_input->p->input.p_demux,
714 DEMUX_GET_POSITION, &f_pos ) )
716 val.f_float = (float)f_pos;
717 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
719 if( !demux_Control( p_input->p->input.p_demux,
720 DEMUX_GET_TIME, &i_time ) )
722 p_input->i_time = i_time;
724 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
726 if( !demux_Control( p_input->p->input.p_demux,
727 DEMUX_GET_LENGTH, &i_length ) )
730 var_Get( p_input, "length", &old_val );
731 val.i_time = i_length;
732 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
734 if( old_val.i_time != val.i_time )
736 UpdateItemLength( p_input, i_length );
740 var_SetBool( p_input, "intf-change", true );
741 i_intf_update = mdate() + INT64_C(150000);
743 /* 150ms * 8 = ~ 1 second */
744 if( ++i_updates % 8 == 0 )
746 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
747 /* Are we the thread responsible for computing global stats ? */
748 if( libvlc_priv (p_input->p_libvlc)->p_stats_computer == p_input )
750 stats_ComputeGlobalStats( p_input->p_libvlc,
751 p_input->p_libvlc->p_stats );
757 static void InitStatistics( input_thread_t * p_input )
759 if( p_input->b_preparsing ) return;
761 /* Prepare statistics */
762 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
763 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
764 if( libvlc_stats (p_input) )
766 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
767 INIT_COUNTER( read_packets, INTEGER, COUNTER );
768 INIT_COUNTER( demux_read, INTEGER, COUNTER );
769 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
770 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
771 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
772 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
773 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
774 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
775 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
776 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
777 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
778 p_input->p->counters.p_sout_send_bitrate = NULL;
779 p_input->p->counters.p_sout_sent_packets = NULL;
780 p_input->p->counters.p_sout_sent_bytes = NULL;
781 if( p_input->p->counters.p_demux_bitrate )
782 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
783 if( p_input->p->counters.p_input_bitrate )
784 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
789 static int InitSout( input_thread_t * p_input )
793 if( p_input->b_preparsing ) return VLC_SUCCESS;
795 /* Find a usable sout and attach it to p_input */
796 psz = var_GetNonEmptyString( p_input, "sout" );
797 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
799 /* Check the validity of the provided sout */
800 if( p_input->p->p_sout )
802 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
804 msg_Dbg( p_input, "destroying unusable sout" );
806 sout_DeleteInstance( p_input->p->p_sout );
807 p_input->p->p_sout = NULL;
811 if( p_input->p->p_sout )
814 msg_Dbg( p_input, "sout keep: reusing sout" );
815 msg_Dbg( p_input, "sout keep: you probably want to use "
816 "gather stream_out" );
817 vlc_object_attach( p_input->p->p_sout, p_input );
821 /* Create a new one */
822 p_input->p->p_sout = sout_NewInstance( p_input, psz );
823 if( !p_input->p->p_sout )
825 input_ChangeState( p_input, ERROR_S );
826 msg_Err( p_input, "cannot start stream output instance, " \
832 if( libvlc_stats (p_input) )
834 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
835 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
836 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
837 if( p_input->p->counters.p_sout_send_bitrate )
838 p_input->p->counters.p_sout_send_bitrate->update_interval =
842 else if( p_input->p->p_sout )
844 msg_Dbg( p_input, "destroying useless sout" );
846 sout_DeleteInstance( p_input->p->p_sout );
847 p_input->p->p_sout = NULL;
855 static void InitTitle( input_thread_t * p_input )
859 if( p_input->b_preparsing ) return;
861 /* Create global title (from master) */
862 p_input->p->i_title = p_input->p->input.i_title;
863 p_input->p->title = p_input->p->input.title;
864 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
865 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
866 if( p_input->p->i_title > 0 )
868 /* Setup variables */
869 input_ControlVarNavigation( p_input );
870 input_ControlVarTitle( p_input, 0 );
874 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
875 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
876 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
879 if( p_input->i_pts_delay < 0 )
880 p_input->i_pts_delay = 0;
882 /* If the desynchronisation requested by the user is < 0, we need to
883 * cache more data. */
884 var_Get( p_input, "audio-desync", &val );
885 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
887 /* Update cr_average depending on the caching */
888 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
889 p_input->p->input.i_cr_average /= 10;
890 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
893 static void StartTitle( input_thread_t * p_input )
902 /* Start title/chapter */
904 if( p_input->b_preparsing )
906 p_input->p->i_start = 0;
910 val.i_int = p_input->p->input.i_title_start -
911 p_input->p->input.i_title_offset;
912 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
913 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
914 val.i_int = p_input->p->input.i_seekpoint_start -
915 p_input->p->input.i_seekpoint_offset;
916 if( val.i_int > 0 /* TODO: check upper boundary */ )
917 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
921 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
922 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
923 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
924 i_length = var_GetTime( p_input, "length" );
925 if( p_input->p->i_run < 0 )
927 msg_Warn( p_input, "invalid run-time ignored" );
928 p_input->p->i_run = 0;
931 if( p_input->p->i_start > 0 )
933 if( p_input->p->i_start >= i_length )
935 msg_Warn( p_input, "invalid start-time ignored" );
941 msg_Dbg( p_input, "starting at time: %ds",
942 (int)( p_input->p->i_start / INT64_C(1000000) ) );
944 s.i_time = p_input->p->i_start;
945 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
948 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
950 msg_Warn( p_input, "invalid stop-time ignored" );
951 p_input->p->i_stop = 0;
955 /* Get fps and set it if not already set */
956 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
959 float f_requested_fps;
961 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
962 var_SetFloat( p_input, "sub-original-fps", f_fps );
964 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
965 if( f_requested_fps != f_fps )
967 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
969 var_SetFloat( p_input, "sub-fps", f_requested_fps );
973 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
976 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
979 /* Look for and add subtitle files */
980 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
981 if( psz_subtitle != NULL )
983 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
984 input_AddSubtitles( p_input, psz_subtitle, false );
987 var_Get( p_input, "sub-autodetect-file", &val );
990 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
991 char **subs = subtitles_Detect( p_input, psz_autopath,
992 p_input->p->input.p_item->psz_uri );
995 if( psz_autopath == NULL )
996 psz_autopath = strdup("");
998 /* Try to autoselect the first autodetected subtitles file
999 * if no subtitles file was specified */
1000 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1002 input_AddSubtitles( p_input, subs[0], false );
1007 /* Then, just add the following subtitles files */
1008 for( ; subs && subs[i]; i++ )
1010 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1012 sub = InputSourceNew( p_input );
1013 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1015 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1022 free( psz_autopath );
1024 free( psz_subtitle );
1026 /* Look for slave */
1027 psz = var_GetNonEmptyString( p_input, "input-slave" );
1031 input_source_t *slave;
1032 while( psz && *psz )
1034 while( *psz == ' ' || *psz == '#' )
1038 if( ( psz_delim = strchr( psz, '#' ) ) )
1040 *psz_delim++ = '\0';
1047 msg_Dbg( p_input, "adding slave input '%s'", psz );
1048 slave = InputSourceNew( p_input );
1049 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1051 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1060 static void InitPrograms( input_thread_t * p_input )
1065 if( p_input->b_preparsing ) return;
1068 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1069 i_es_out_mode = ES_OUT_MODE_AUTO;
1071 if( p_input->p->p_sout )
1073 var_Get( p_input, "sout-all", &val );
1076 i_es_out_mode = ES_OUT_MODE_ALL;
1081 var_Get( p_input, "programs", &val );
1082 if ( val.p_list && val.p_list->i_count )
1084 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1085 /* Note : we should remove the "program" callback. */
1088 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1092 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1094 /* Inform the demuxer about waited group (needed only for DVB) */
1095 if( i_es_out_mode == ES_OUT_MODE_ALL )
1097 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1099 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1101 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1106 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1107 (int) var_GetInteger( p_input, "program" ), NULL );
1111 static int Init( input_thread_t * p_input )
1117 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1119 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1121 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1122 var_SetString( p_input, "sout", "" );
1123 var_SetBool( p_input, "sout-all", false );
1124 var_SetString( p_input, "input-slave", "" );
1125 var_SetInteger( p_input, "input-repeat", 0 );
1126 var_SetString( p_input, "sub-file", "" );
1127 var_SetBool( p_input, "sub-autodetect-file", false );
1131 InitStatistics( p_input );
1133 ret = InitSout( p_input );
1134 if( ret != VLC_SUCCESS )
1135 return ret; /* FIXME: goto error; should be better here */
1139 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1140 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1141 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1143 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1144 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1146 if( InputSourceInit( p_input, &p_input->p->input,
1147 p_input->p->input.p_item->psz_uri, NULL ) )
1152 InitTitle( p_input );
1154 /* Load master infos */
1156 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1157 &val.i_time ) && val.i_time > 0 )
1159 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1160 UpdateItemLength( p_input, val.i_time );
1164 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1165 if( val.i_time > 0 )
1166 { /* fallback: gets length from metadata */
1167 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1168 UpdateItemLength( p_input, val.i_time );
1172 StartTitle( p_input );
1174 InitPrograms( p_input );
1176 if( !p_input->b_preparsing && p_input->p->p_sout )
1178 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1180 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1182 /* We don't want a high input priority here or we'll
1183 * end-up sucking up all the CPU time */
1184 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1187 msg_Dbg( p_input, "starting in %s mode",
1188 p_input->p->b_out_pace_control ? "async" : "sync" );
1191 p_meta = vlc_meta_New();
1193 /* Get meta data from users */
1194 InputMetaUser( p_input, p_meta );
1196 /* Get meta data from master input */
1197 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1199 /* Access_file does not give any meta, and there are no slave */
1200 AccessMeta( p_input, p_meta );
1202 InputUpdateMeta( p_input, p_meta );
1204 if( !p_input->b_preparsing )
1206 msg_Dbg( p_input, "`%s' successfully opened",
1207 p_input->p->input.p_item->psz_uri );
1211 /* initialization is complete */
1212 input_ChangeState( p_input, PLAYING_S );
1217 input_ChangeState( p_input, ERROR_S );
1219 if( p_input->p->p_es_out )
1220 input_EsOutDelete( p_input->p->p_es_out );
1222 if( p_input->p->p_sout )
1224 vlc_object_detach( p_input->p->p_sout );
1225 sout_DeleteInstance( p_input->p->p_sout );
1229 if( !p_input->b_preparsing && libvlc_stats (p_input) )
1231 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1232 stats_CounterClean( p_input->p->counters.p_##c );\
1233 p_input->p->counters.p_##c = NULL; } while(0)
1234 EXIT_COUNTER( read_bytes );
1235 EXIT_COUNTER( read_packets );
1236 EXIT_COUNTER( demux_read );
1237 EXIT_COUNTER( input_bitrate );
1238 EXIT_COUNTER( demux_bitrate );
1239 EXIT_COUNTER( played_abuffers );
1240 EXIT_COUNTER( lost_abuffers );
1241 EXIT_COUNTER( displayed_pictures );
1242 EXIT_COUNTER( lost_pictures );
1243 EXIT_COUNTER( decoded_audio );
1244 EXIT_COUNTER( decoded_video );
1245 EXIT_COUNTER( decoded_sub );
1247 if( p_input->p->p_sout )
1249 EXIT_COUNTER( sout_sent_packets );
1250 EXIT_COUNTER (sout_sent_bytes );
1251 EXIT_COUNTER( sout_send_bitrate );
1256 /* Mark them deleted */
1257 p_input->p->input.p_demux = NULL;
1258 p_input->p->input.p_stream = NULL;
1259 p_input->p->input.p_access = NULL;
1260 p_input->p->p_es_out = NULL;
1261 p_input->p->p_sout = NULL;
1263 return VLC_EGENERIC;
1266 /*****************************************************************************
1267 * WaitDie: Wait until we are asked to die.
1268 *****************************************************************************
1269 * This function is called when an error occurred during thread main's loop.
1270 *****************************************************************************/
1271 static void WaitDie( input_thread_t *p_input )
1273 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1274 while( !p_input->b_die )
1277 msleep( INPUT_IDLE_SLEEP );
1281 /*****************************************************************************
1282 * End: end the input thread
1283 *****************************************************************************/
1284 static void End( input_thread_t * p_input )
1288 /* We are at the end */
1289 input_ChangeState( p_input, END_S );
1291 /* Clean control variables */
1292 input_ControlVarStop( p_input );
1294 /* Clean up master */
1295 InputSourceClean( &p_input->p->input );
1298 for( i = 0; i < p_input->p->i_slave; i++ )
1300 InputSourceClean( p_input->p->slave[i] );
1301 free( p_input->p->slave[i] );
1303 free( p_input->p->slave );
1305 /* Unload all modules */
1306 if( p_input->p->p_es_out )
1307 input_EsOutDelete( p_input->p->p_es_out );
1309 if( !p_input->b_preparsing )
1311 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1312 if( libvlc_stats (p_input) )
1314 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
1316 /* make sure we are up to date */
1317 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1318 if( priv->p_stats_computer == p_input )
1320 stats_ComputeGlobalStats( p_input->p_libvlc,
1321 p_input->p_libvlc->p_stats );
1322 priv->p_stats_computer = NULL;
1324 CL_CO( read_bytes );
1325 CL_CO( read_packets );
1326 CL_CO( demux_read );
1327 CL_CO( input_bitrate );
1328 CL_CO( demux_bitrate );
1329 CL_CO( played_abuffers );
1330 CL_CO( lost_abuffers );
1331 CL_CO( displayed_pictures );
1332 CL_CO( lost_pictures );
1333 CL_CO( decoded_audio) ;
1334 CL_CO( decoded_video );
1335 CL_CO( decoded_sub) ;
1338 /* Close optional stream output instance */
1339 if( p_input->p->p_sout )
1341 CL_CO( sout_sent_packets );
1342 CL_CO( sout_sent_bytes );
1343 CL_CO( sout_send_bitrate );
1345 vlc_object_detach( p_input->p->p_sout );
1350 if( p_input->p->i_attachment > 0 )
1352 for( i = 0; i < p_input->p->i_attachment; i++ )
1353 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1354 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1357 /* Tell we're dead */
1358 p_input->b_dead = true;
1361 /*****************************************************************************
1363 *****************************************************************************/
1364 static inline int ControlPopNoLock( input_thread_t *p_input,
1365 int *pi_type, vlc_value_t *p_val )
1367 if( p_input->p->i_control <= 0 )
1369 return VLC_EGENERIC;
1372 *pi_type = p_input->p->control[0].i_type;
1373 *p_val = p_input->p->control[0].val;
1375 p_input->p->i_control--;
1376 if( p_input->p->i_control > 0 )
1380 for( i = 0; i < p_input->p->i_control; i++ )
1382 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1383 p_input->p->control[i].val = p_input->p->control[i+1].val;
1390 static void ControlReduce( input_thread_t *p_input )
1397 for( i = 1; i < p_input->p->i_control; i++ )
1399 const int i_lt = p_input->p->control[i-1].i_type;
1400 const int i_ct = p_input->p->control[i].i_type;
1402 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1403 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1407 ( i_ct == INPUT_CONTROL_SET_STATE ||
1408 i_ct == INPUT_CONTROL_SET_RATE ||
1409 i_ct == INPUT_CONTROL_SET_POSITION ||
1410 i_ct == INPUT_CONTROL_SET_TIME ||
1411 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1412 i_ct == INPUT_CONTROL_SET_TITLE ||
1413 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1414 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1417 // msg_Dbg( p_input, "merged at %d", i );
1418 /* Remove the i-1 */
1419 for( j = i; j < p_input->p->i_control; j++ )
1420 p_input->p->control[j-1] = p_input->p->control[j];
1421 p_input->p->i_control--;
1425 /* TODO but that's not that important
1426 - merge SET_X with SET_X_CMD
1427 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1428 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1435 static bool Control( input_thread_t *p_input, int i_type,
1438 bool b_force_update = false;
1440 if( !p_input ) return b_force_update;
1444 case INPUT_CONTROL_SET_DIE:
1445 msg_Dbg( p_input, "control: stopping input" );
1447 /* Mark all submodules to die */
1448 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1451 case INPUT_CONTROL_SET_POSITION:
1452 case INPUT_CONTROL_SET_POSITION_OFFSET:
1455 if( i_type == INPUT_CONTROL_SET_POSITION )
1457 f_pos = val.f_float;
1461 /* Should not fail */
1462 demux_Control( p_input->p->input.p_demux,
1463 DEMUX_GET_POSITION, &f_pos );
1464 f_pos += val.f_float;
1466 if( f_pos < 0.0 ) f_pos = 0.0;
1467 if( f_pos > 1.0 ) f_pos = 1.0;
1468 /* Reset the decoders states and clock sync (before calling the demuxer */
1469 input_EsOutChangePosition( p_input->p->p_es_out );
1470 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1473 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1474 "%2.1f%% failed", f_pos * 100 );
1478 if( p_input->p->i_slave > 0 )
1479 SlaveSeek( p_input );
1481 b_force_update = true;
1486 case INPUT_CONTROL_SET_TIME:
1487 case INPUT_CONTROL_SET_TIME_OFFSET:
1492 if( i_type == INPUT_CONTROL_SET_TIME )
1494 i_time = val.i_time;
1498 /* Should not fail */
1499 demux_Control( p_input->p->input.p_demux,
1500 DEMUX_GET_TIME, &i_time );
1501 i_time += val.i_time;
1503 if( i_time < 0 ) i_time = 0;
1505 /* Reset the decoders states and clock sync (before calling the demuxer */
1506 input_EsOutChangePosition( p_input->p->p_es_out );
1508 i_ret = demux_Control( p_input->p->input.p_demux,
1509 DEMUX_SET_TIME, i_time );
1514 /* Emulate it with a SET_POS */
1515 demux_Control( p_input->p->input.p_demux,
1516 DEMUX_GET_LENGTH, &i_length );
1519 double f_pos = (double)i_time / (double)i_length;
1520 i_ret = demux_Control( p_input->p->input.p_demux,
1521 DEMUX_SET_POSITION, f_pos );
1526 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1527 " failed or not possible", i_time );
1531 if( p_input->p->i_slave > 0 )
1532 SlaveSeek( p_input );
1534 b_force_update = true;
1539 case INPUT_CONTROL_SET_STATE:
1540 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1541 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1544 if( p_input->p->input.p_access )
1545 i_ret = access_Control( p_input->p->input.p_access,
1546 ACCESS_SET_PAUSE_STATE, false );
1548 i_ret = demux_Control( p_input->p->input.p_demux,
1549 DEMUX_SET_PAUSE_STATE, false );
1553 /* FIXME What to do ? */
1554 msg_Warn( p_input, "cannot unset pause -> EOF" );
1555 vlc_mutex_unlock( &p_input->p->lock_control );
1556 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1557 vlc_mutex_lock( &p_input->p->lock_control );
1560 b_force_update = true;
1562 /* Switch to play */
1563 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1567 input_EsOutChangeState( p_input->p->p_es_out );
1569 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1570 p_input->p->b_can_pause )
1573 if( p_input->p->input.p_access )
1574 i_ret = access_Control( p_input->p->input.p_access,
1575 ACCESS_SET_PAUSE_STATE, true );
1577 i_ret = demux_Control( p_input->p->input.p_demux,
1578 DEMUX_SET_PAUSE_STATE, true );
1580 b_force_update = true;
1584 msg_Warn( p_input, "cannot set pause state" );
1585 state = p_input->i_state;
1592 /* Switch to new state */
1593 input_ChangeStateWithVarCallback( p_input, state, false );
1597 input_EsOutChangeState( p_input->p->p_es_out );
1599 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1601 b_force_update = true;
1603 /* Correct "state" value */
1604 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1606 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1608 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1612 case INPUT_CONTROL_SET_RATE:
1613 case INPUT_CONTROL_SET_RATE_SLOWER:
1614 case INPUT_CONTROL_SET_RATE_FASTER:
1618 if( i_type == INPUT_CONTROL_SET_RATE )
1624 static const int ppi_factor[][2] = {
1625 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1627 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1636 for( i = 0; ppi_factor[i][0] != 0; i++ )
1638 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1639 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1640 if( i_test_e < i_error )
1646 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1648 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1650 if( ppi_factor[i_idx+1][0] > 0 )
1651 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1653 i_rate = INPUT_RATE_MAX+1;
1657 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1659 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1661 i_rate = INPUT_RATE_MIN-1;
1665 if( i_rate < INPUT_RATE_MIN )
1667 msg_Dbg( p_input, "cannot set rate faster" );
1668 i_rate = INPUT_RATE_MIN;
1670 else if( i_rate > INPUT_RATE_MAX )
1672 msg_Dbg( p_input, "cannot set rate slower" );
1673 i_rate = INPUT_RATE_MAX;
1675 if( i_rate != INPUT_RATE_DEFAULT &&
1676 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1677 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1679 msg_Dbg( p_input, "cannot change rate" );
1680 i_rate = INPUT_RATE_DEFAULT;
1682 if( i_rate != p_input->p->i_rate &&
1683 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1686 if( p_input->p->input.p_access )
1687 i_ret = VLC_EGENERIC;
1689 i_ret = demux_Control( p_input->p->input.p_demux,
1690 DEMUX_SET_RATE, &i_rate );
1693 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1694 i_rate = p_input->p->i_rate;
1699 if( i_rate != p_input->p->i_rate )
1702 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1703 var_SetBool( p_input, "rate-change", true );
1705 p_input->p->i_rate = i_rate;
1707 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1708 if( p_input->p->input.b_rescale_ts )
1709 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1711 b_force_update = true;
1716 case INPUT_CONTROL_SET_PROGRAM:
1717 /* No need to force update, es_out does it if needed */
1718 es_out_Control( p_input->p->p_es_out,
1719 ES_OUT_SET_GROUP, val.i_int );
1721 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1725 case INPUT_CONTROL_SET_ES:
1726 /* No need to force update, es_out does it if needed */
1727 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1728 input_EsOutGetFromID( p_input->p->p_es_out,
1732 case INPUT_CONTROL_SET_AUDIO_DELAY:
1733 input_EsOutSetDelay( p_input->p->p_es_out,
1734 AUDIO_ES, val.i_time );
1735 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1738 case INPUT_CONTROL_SET_SPU_DELAY:
1739 input_EsOutSetDelay( p_input->p->p_es_out,
1740 SPU_ES, val.i_time );
1741 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1744 case INPUT_CONTROL_SET_TITLE:
1745 case INPUT_CONTROL_SET_TITLE_NEXT:
1746 case INPUT_CONTROL_SET_TITLE_PREV:
1747 if( p_input->p->input.b_title_demux &&
1748 p_input->p->input.i_title > 0 )
1751 /* FIXME handle demux title */
1752 demux_t *p_demux = p_input->p->input.p_demux;
1755 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1756 i_title = p_demux->info.i_title - 1;
1757 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1758 i_title = p_demux->info.i_title + 1;
1760 i_title = val.i_int;
1762 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1764 input_EsOutChangePosition( p_input->p->p_es_out );
1766 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1767 input_ControlVarTitle( p_input, i_title );
1770 else if( p_input->p->input.i_title > 0 )
1772 access_t *p_access = p_input->p->input.p_access;
1775 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1776 i_title = p_access->info.i_title - 1;
1777 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1778 i_title = p_access->info.i_title + 1;
1780 i_title = val.i_int;
1782 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1784 input_EsOutChangePosition( p_input->p->p_es_out );
1786 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1787 stream_AccessReset( p_input->p->input.p_stream );
1791 case INPUT_CONTROL_SET_SEEKPOINT:
1792 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1793 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1794 if( p_input->p->input.b_title_demux &&
1795 p_input->p->input.i_title > 0 )
1797 demux_t *p_demux = p_input->p->input.p_demux;
1799 int64_t i_input_time;
1800 int64_t i_seekpoint_time;
1802 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1804 i_seekpoint = p_demux->info.i_seekpoint;
1805 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1806 if( i_seekpoint_time >= 0 &&
1807 !demux_Control( p_demux,
1808 DEMUX_GET_TIME, &i_input_time ) )
1810 if ( i_input_time < i_seekpoint_time + 3000000 )
1816 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1817 i_seekpoint = p_demux->info.i_seekpoint + 1;
1819 i_seekpoint = val.i_int;
1821 if( i_seekpoint >= 0 && i_seekpoint <
1822 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1825 input_EsOutChangePosition( p_input->p->p_es_out );
1827 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1830 else if( p_input->p->input.i_title > 0 )
1832 demux_t *p_demux = p_input->p->input.p_demux;
1833 access_t *p_access = p_input->p->input.p_access;
1835 int64_t i_input_time;
1836 int64_t i_seekpoint_time;
1838 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1840 i_seekpoint = p_access->info.i_seekpoint;
1841 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1842 if( i_seekpoint_time >= 0 &&
1843 demux_Control( p_demux,
1844 DEMUX_GET_TIME, &i_input_time ) )
1846 if ( i_input_time < i_seekpoint_time + 3000000 )
1852 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1853 i_seekpoint = p_access->info.i_seekpoint + 1;
1855 i_seekpoint = val.i_int;
1857 if( i_seekpoint >= 0 && i_seekpoint <
1858 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1860 input_EsOutChangePosition( p_input->p->p_es_out );
1862 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1864 stream_AccessReset( p_input->p->input.p_stream );
1869 case INPUT_CONTROL_ADD_SLAVE:
1870 if( val.psz_string )
1872 input_source_t *slave = InputSourceNew( p_input );
1874 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1880 msg_Dbg( p_input, "adding %s as slave on the fly",
1884 if( demux_Control( p_input->p->input.p_demux,
1885 DEMUX_GET_TIME, &i_time ) )
1887 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1888 InputSourceClean( slave );
1892 if( demux_Control( slave->p_demux,
1893 DEMUX_SET_TIME, i_time ) )
1895 msg_Err( p_input, "seek failed for new slave" );
1896 InputSourceClean( slave );
1901 /* Get meta (access and demux) */
1902 p_meta = vlc_meta_New();
1903 access_Control( slave->p_access, ACCESS_GET_META,
1905 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1906 InputUpdateMeta( p_input, p_meta );
1908 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1913 msg_Warn( p_input, "failed to add %s as slave",
1917 free( val.psz_string );
1921 case INPUT_CONTROL_SET_BOOKMARK:
1923 msg_Err( p_input, "not yet implemented" );
1927 return b_force_update;
1930 /*****************************************************************************
1932 *****************************************************************************/
1933 static int UpdateFromDemux( input_thread_t *p_input )
1935 demux_t *p_demux = p_input->p->input.p_demux;
1938 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1940 v.i_int = p_demux->info.i_title;
1941 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1943 input_ControlVarTitle( p_input, p_demux->info.i_title );
1945 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1947 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1949 v.i_int = p_demux->info.i_seekpoint;
1950 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1952 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1954 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1956 /* Hmmm only works with master input */
1957 if( p_input->p->input.p_demux == p_demux )
1959 int i_title_end = p_input->p->input.i_title_end -
1960 p_input->p->input.i_title_offset;
1961 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1962 p_input->p->input.i_seekpoint_offset;
1964 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1966 if( p_demux->info.i_title > i_title_end ||
1967 ( p_demux->info.i_title == i_title_end &&
1968 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1970 else if( i_seekpoint_end >=0 )
1972 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1974 else if( i_title_end >= 0 )
1976 if( p_demux->info.i_title > i_title_end ) return 0;
1983 /*****************************************************************************
1985 *****************************************************************************/
1986 static int UpdateFromAccess( input_thread_t *p_input )
1988 access_t *p_access = p_input->p->input.p_access;
1991 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1993 v.i_int = p_access->info.i_title;
1994 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1996 input_ControlVarTitle( p_input, p_access->info.i_title );
1998 stream_AccessUpdate( p_input->p->input.p_stream );
2000 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2002 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2004 v.i_int = p_access->info.i_seekpoint;
2005 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2006 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2008 if( p_access->info.i_update & INPUT_UPDATE_META )
2010 /* TODO maybe multi - access ? */
2011 vlc_meta_t *p_meta = vlc_meta_New();
2012 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2013 InputUpdateMeta( p_input, p_meta );
2014 p_access->info.i_update &= ~INPUT_UPDATE_META;
2017 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2019 /* Hmmm only works with master input */
2020 if( p_input->p->input.p_access == p_access )
2022 int i_title_end = p_input->p->input.i_title_end -
2023 p_input->p->input.i_title_offset;
2024 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2025 p_input->p->input.i_seekpoint_offset;
2027 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2029 if( p_access->info.i_title > i_title_end ||
2030 ( p_access->info.i_title == i_title_end &&
2031 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2033 else if( i_seekpoint_end >=0 )
2035 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2037 else if( i_title_end >= 0 )
2039 if( p_access->info.i_title > i_title_end ) return 0;
2046 /*****************************************************************************
2048 *****************************************************************************/
2049 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2051 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2054 /*****************************************************************************
2056 *****************************************************************************/
2057 static input_source_t *InputSourceNew( input_thread_t *p_input )
2060 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2062 memset( in, 0, sizeof( input_source_t ) );
2066 /*****************************************************************************
2068 *****************************************************************************/
2069 static int InputSourceInit( input_thread_t *p_input,
2070 input_source_t *in, const char *psz_mrl,
2071 const char *psz_forced_demux )
2073 char psz_dup[strlen (psz_mrl) + 1];
2074 const char *psz_access;
2075 const char *psz_demux;
2082 strcpy( psz_dup, psz_mrl );
2084 if( !in ) return VLC_EGENERIC;
2085 if( !p_input ) return VLC_EGENERIC;
2088 input_SplitMRL( &psz_access, &psz_demux, &psz_path, psz_dup );
2090 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2091 psz_mrl, psz_access, psz_demux, psz_path );
2092 if( !p_input->b_preparsing )
2094 /* Hack to allow udp://@:port syntax */
2096 (strncmp( psz_access, "udp", 3 ) &&
2097 strncmp( psz_access, "rtp", 3 )) )
2099 /* Find optional titles and seekpoints */
2100 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2101 &in->i_seekpoint_start, &in->i_seekpoint_end );
2104 if( psz_forced_demux && *psz_forced_demux )
2106 psz_demux = psz_forced_demux;
2108 else if( *psz_demux == '\0' )
2110 /* special hack for forcing a demuxer with --demux=module
2111 * (and do nothing with a list) */
2112 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2114 if( psz_var_demux != NULL &&
2115 !strchr(psz_var_demux, ',' ) &&
2116 !strchr(psz_var_demux, ':' ) )
2118 psz_demux = psz_var_demux;
2120 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2124 /* Try access_demux if no demux given */
2125 if( *psz_demux == '\0' )
2127 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2128 NULL, p_input->p->p_es_out, false );
2133 /* Preparsing is only for file:// */
2136 if( !*psz_access ) /* path without scheme:// */
2137 psz_access = "file";
2138 if( strcmp( psz_access, "file" ) )
2140 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2145 int64_t i_pts_delay;
2147 /* Get infos from access_demux */
2148 demux_Control( in->p_demux,
2149 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2150 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2152 in->b_title_demux = true;
2153 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2154 &in->title, &in->i_title,
2155 &in->i_title_offset, &in->i_seekpoint_offset ) )
2160 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2161 &in->b_can_pace_control ) )
2162 in->b_can_pace_control = false;
2164 if( !in->b_can_pace_control )
2166 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2167 &in->b_can_rate_control, &in->b_rescale_ts ) )
2169 in->b_can_rate_control = false;
2170 in->b_rescale_ts = true; /* not used */
2175 in->b_can_rate_control = true;
2176 in->b_rescale_ts = true;
2178 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2179 &in->b_can_pause ) )
2180 in->b_can_pause = false;
2181 var_SetBool( p_input, "can-pause", in->b_can_pause );
2183 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2185 if( ret != VLC_SUCCESS )
2187 var_Set( p_input, "seekable", val );
2191 int64_t i_pts_delay;
2193 input_ChangeState( p_input, OPENING_S );
2195 /* Now try a real access */
2196 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2198 /* Access failed, URL encoded ? */
2199 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2201 decode_URI( psz_path );
2203 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2204 psz_access, psz_demux, psz_path );
2206 in->p_access = access_New( p_input,
2207 psz_access, psz_demux, psz_path );
2209 if( in->p_access == NULL )
2211 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2213 intf_UserFatal( VLC_OBJECT( p_input), false,
2214 _("Your input can't be opened"),
2215 _("VLC is unable to open the MRL '%s'."
2216 " Check the log for details."), psz_mrl );
2221 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2222 while( psz && *psz )
2224 access_t *p_access = in->p_access;
2225 char *end = strchr( psz, ':' );
2230 in->p_access = access_FilterNew( in->p_access, psz );
2231 if( in->p_access == NULL )
2233 in->p_access = p_access;
2234 msg_Warn( p_input, "failed to insert access filter %s",
2242 /* Get infos from access */
2243 if( !p_input->b_preparsing )
2245 access_Control( in->p_access,
2246 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2247 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2249 in->b_title_demux = false;
2250 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2251 &in->title, &in->i_title,
2252 &in->i_title_offset, &in->i_seekpoint_offset ) )
2258 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2259 &in->b_can_pace_control );
2260 in->b_can_rate_control = in->b_can_pace_control;
2261 in->b_rescale_ts = true;
2263 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2265 var_SetBool( p_input, "can-pause", in->b_can_pause );
2266 access_Control( in->p_access, ACCESS_CAN_SEEK,
2268 var_Set( p_input, "seekable", val );
2271 input_ChangeState( p_input, BUFFERING_S );
2273 /* Create the stream_t */
2274 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2275 if( in->p_stream == NULL )
2277 msg_Warn( p_input, "cannot create a stream_t from access" );
2281 /* Open a demuxer */
2282 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2284 psz_demux = in->p_access->psz_demux;
2288 /* Take access redirections into account */
2289 char *psz_real_path;
2290 char *psz_buf = NULL;
2291 if( in->p_access->psz_path )
2293 const char *psz_a, *psz_d;
2294 psz_buf = strdup( in->p_access->psz_path );
2295 input_SplitMRL( &psz_a, &psz_d, &psz_real_path, psz_buf );
2299 psz_real_path = psz_path;
2301 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2303 in->p_stream, p_input->p->p_es_out,
2304 p_input->b_preparsing );
2308 if( in->p_demux == NULL )
2310 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2311 psz_access, psz_demux, psz_path );
2312 intf_UserFatal( VLC_OBJECT( p_input ), false,
2313 _("VLC can't recognize the input's format"),
2314 _("The format of '%s' cannot be detected. "
2315 "Have a look the log for details."), psz_mrl );
2319 /* Get title from demux */
2320 if( !p_input->b_preparsing && in->i_title <= 0 )
2322 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2323 &in->title, &in->i_title,
2324 &in->i_title_offset, &in->i_seekpoint_offset ))
2326 TAB_INIT( in->i_title, in->title );
2330 in->b_title_demux = true;
2336 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2337 if( 1 || !p_input->b_preparsing )
2340 input_attachment_t **attachment;
2341 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2342 &attachment, &i_attachment ) )
2344 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2345 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2346 i_attachment, attachment );
2347 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2350 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2352 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2354 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2357 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2358 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2363 input_ChangeState( p_input, ERROR_S );
2366 demux_Delete( in->p_demux );
2369 stream_Delete( in->p_stream );
2372 access_Delete( in->p_access );
2374 return VLC_EGENERIC;
2377 /*****************************************************************************
2379 *****************************************************************************/
2380 static void InputSourceClean( input_source_t *in )
2385 demux_Delete( in->p_demux );
2388 stream_Delete( in->p_stream );
2391 access_Delete( in->p_access );
2393 if( in->i_title > 0 )
2395 for( i = 0; i < in->i_title; i++ )
2396 vlc_input_title_Delete( in->title[i] );
2397 TAB_CLEAN( in->i_title, in->title );
2401 static void SlaveDemux( input_thread_t *p_input )
2406 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2408 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2412 for( i = 0; i < p_input->p->i_slave; i++ )
2414 input_source_t *in = p_input->p->slave[i];
2420 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2425 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2427 msg_Err( p_input, "slave[%d] doesn't like "
2428 "DEMUX_GET_TIME -> EOF", i );
2433 if( i_stime >= i_time )
2436 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2442 i_ret = in->p_demux->pf_demux( in->p_demux );
2447 msg_Dbg( p_input, "slave %d EOF", i );
2453 static void SlaveSeek( input_thread_t *p_input )
2458 if( !p_input ) return;
2460 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2462 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2466 for( i = 0; i < p_input->p->i_slave; i++ )
2468 input_source_t *in = p_input->p->slave[i];
2470 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2472 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2478 /*****************************************************************************
2480 *****************************************************************************/
2481 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2485 if( !p_meta ) return;
2487 /* Get meta information from user */
2488 #define GET_META( field, s ) \
2489 var_Get( p_input, (s), &val ); \
2490 if( *val.psz_string ) \
2491 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2492 free( val.psz_string )
2494 GET_META( Title, "meta-title" );
2495 GET_META( Artist, "meta-artist" );
2496 GET_META( Genre, "meta-genre" );
2497 GET_META( Copyright, "meta-copyright" );
2498 GET_META( Description, "meta-description" );
2499 GET_META( Date, "meta-date" );
2500 GET_META( URL, "meta-url" );
2504 /*****************************************************************************
2505 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2506 * arturl and locking issue.
2507 *****************************************************************************/
2508 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2510 input_item_t *p_item = p_input->p->input.p_item;
2511 char * psz_arturl = NULL;
2512 char *psz_title = NULL;
2513 int i_arturl_event = false;
2518 psz_arturl = input_item_GetArtURL( p_item );
2520 vlc_mutex_lock( &p_item->lock );
2521 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2522 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2524 vlc_meta_Merge( p_item->p_meta, p_meta );
2526 if( psz_arturl && *psz_arturl )
2528 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2529 i_arturl_event = true;
2532 vlc_meta_Delete( p_meta );
2534 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2536 /* Don't look for art cover if sout
2537 * XXX It can change when sout has meta data support */
2538 if( p_input->p->p_sout && !p_input->b_preparsing )
2540 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2541 i_arturl_event = true;
2545 input_ExtractAttachmentAndCacheArt( p_input );
2551 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2553 p_meta = vlc_meta_New();
2554 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2556 vlc_mutex_unlock( &p_item->lock );
2558 input_item_SetPreparsed( p_item, true );
2560 if( i_arturl_event == true )
2564 /* Notify interested third parties */
2565 event.type = vlc_InputItemMetaChanged;
2566 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2567 vlc_event_send( &p_item->event_manager, &event );
2572 input_Control( p_input, INPUT_SET_NAME, psz_title );
2576 /** \todo handle sout meta */
2580 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2581 int i_new, input_attachment_t **pp_new )
2583 int i_attachment = *pi_attachment;
2584 input_attachment_t **attachment = *ppp_attachment;
2587 attachment = realloc( attachment,
2588 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2589 for( i = 0; i < i_new; i++ )
2590 attachment[i_attachment++] = pp_new[i];
2594 *pi_attachment = i_attachment;
2595 *ppp_attachment = attachment;
2598 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2602 if( p_input->b_preparsing )
2605 if( p_input->p->input.p_access )
2606 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2609 /* Get meta data from slave input */
2610 for( i = 0; i < p_input->p->i_slave; i++ )
2612 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2613 if( p_input->p->slave[i]->p_access )
2615 access_Control( p_input->p->slave[i]->p_access,
2616 ACCESS_GET_META, p_meta );
2621 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2628 /* XXX I am not sure it is a great idea, besides, there is more than that
2629 * if we want to do it right */
2630 vlc_mutex_lock( &p_item->lock );
2631 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2633 vlc_mutex_unlock( &p_item->lock );
2636 vlc_mutex_unlock( &p_item->lock );
2639 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2640 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2645 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2646 if(! p_demux->p_private )
2649 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2652 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2654 if( p_demux_meta->p_meta )
2656 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2657 vlc_meta_Delete( p_demux_meta->p_meta );
2660 if( p_demux_meta->i_attachments > 0 )
2662 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2663 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2664 p_demux_meta->i_attachments, p_demux_meta->attachments );
2665 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2667 module_Unneed( p_demux, p_id3 );
2669 free( p_demux->p_private );
2673 /*****************************************************************************
2674 * MRLSplit: parse the access, demux and url part of the
2675 * Media Resource Locator.
2676 *****************************************************************************/
2677 void input_SplitMRL( const char **ppsz_access, const char **ppsz_demux, char **ppsz_path,
2680 char *psz_access = NULL;
2681 char *psz_demux = NULL;
2684 /* Either there is an access/demux specification before ://
2685 * or we have a plain local file path. */
2686 psz_path = strstr( psz_dup, "://" );
2687 if( psz_path != NULL )
2690 psz_path += 3; /* skips "://" */
2692 /* Separate access from demux (<access>/<demux>://<path>) */
2693 psz_access = psz_dup;
2694 psz_demux = strchr( psz_access, '/' );
2696 *psz_demux++ = '\0';
2698 /* We really don't want module name substitution here! */
2699 if( psz_access[0] == '$' )
2701 if( psz_demux && psz_demux[0] == '$' )
2708 *ppsz_access = psz_access ? psz_access : (char*)"";
2709 *ppsz_demux = psz_demux ? psz_demux : (char*)"";
2710 *ppsz_path = psz_path;
2713 static inline bool next(char ** src)
2717 long result = strtol( *src, &end, 0 );
2718 if( errno != 0 || result >= LONG_MAX || result <= LONG_MIN ||
2727 /*****************************************************************************
2728 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2731 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2732 *****************************************************************************/
2733 static void MRLSections( input_thread_t *p_input, char *psz_source,
2734 int *pi_title_start, int *pi_title_end,
2735 int *pi_chapter_start, int *pi_chapter_end )
2737 char *psz, *psz_end, *psz_next, *psz_check;
2739 *pi_title_start = *pi_title_end = -1;
2740 *pi_chapter_start = *pi_chapter_end = -1;
2742 /* Start by parsing titles and chapters */
2743 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2746 /* Check we are really dealing with a title/chapter section */
2747 psz_check = psz + 1;
2748 if( !*psz_check ) return;
2749 if( isdigit(*psz_check) )
2750 if(!next(&psz_check)) return;
2751 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2752 if( *psz_check == ':' && ++psz_check )
2754 if( isdigit(*psz_check) )
2755 if(!next(&psz_check)) return;
2757 if( *psz_check != '-' && *psz_check ) return;
2758 if( *psz_check == '-' && ++psz_check )
2760 if( isdigit(*psz_check) )
2761 if(!next(&psz_check)) return;
2763 if( *psz_check != ':' && *psz_check ) return;
2764 if( *psz_check == ':' && ++psz_check )
2766 if( isdigit(*psz_check) )
2767 if(!next(&psz_check)) return;
2769 if( *psz_check ) return;
2771 /* Separate start and end */
2773 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2775 /* Look for the start title */
2776 *pi_title_start = strtol( psz, &psz_next, 0 );
2777 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2778 *pi_title_end = *pi_title_start;
2781 /* Look for the start chapter */
2783 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2784 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2785 *pi_chapter_end = *pi_chapter_start;
2789 /* Look for the end title */
2790 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2791 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2794 /* Look for the end chapter */
2795 if( *psz_end ) psz_end++;
2796 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2797 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2800 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2801 psz_source, *pi_title_start, *pi_chapter_start,
2802 *pi_title_end, *pi_chapter_end );
2805 /*****************************************************************************
2806 * input_AddSubtitles: add a subtitles file and enable it
2807 *****************************************************************************/
2808 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2809 bool b_check_extension )
2811 input_source_t *sub;
2814 char *psz_path, *psz_extension;
2816 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2821 /* if we are provided a subtitle.sub file,
2822 * see if we don't have a subtitle.idx and use it instead */
2823 psz_path = strdup( psz_subtitle );
2826 psz_extension = strrchr( psz_path, '.');
2827 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2831 strcpy( psz_extension, ".idx" );
2832 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2833 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2836 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2837 psz_path, psz_subtitle );
2838 strcpy( psz_subtitle, psz_path );
2844 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2846 sub = InputSourceNew( p_input );
2847 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2849 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2852 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2854 if( count.i_int == 0 )
2856 /* if it was first one, there is disable too */
2858 if( count.i_int < list.p_list->i_count )
2860 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2861 &list.p_list->p_values[count.i_int] );
2863 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2871 /*****************************************************************************
2872 * input_get_event_manager
2873 *****************************************************************************/
2874 vlc_event_manager_t *input_get_event_manager( input_thread_t *p_input )
2876 return &p_input->p->event_manager;