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 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( 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( 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 int Run( input_thread_t *p_input )
489 /* Signal that the thread is launched */
490 vlc_thread_ready( p_input );
492 if( Init( p_input ) )
494 /* If we failed, wait before we are killed, and exit */
495 p_input->b_error = true;
499 /* Tell we're dead */
500 p_input->b_dead = true;
507 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
509 /* We have finish to demux data but not to play them */
510 while( !p_input->b_die )
512 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
515 msg_Dbg( p_input, "waiting decoder fifos to empty" );
517 msleep( INPUT_IDLE_SLEEP );
520 /* We have finished */
521 p_input->b_eof = true;
522 input_ChangeState( p_input, END_S );
525 /* Wait until we are asked to die */
526 if( !p_input->b_die )
537 /*****************************************************************************
538 * RunAndDestroy: main thread loop
539 * This is the "just forget me" thread that spawns the input processing chain,
540 * reads the stream, cleans up and releases memory
541 *****************************************************************************/
542 static int RunAndDestroy( input_thread_t *p_input )
544 /* Signal that the thread is launched */
545 vlc_thread_ready( p_input );
547 if( Init( p_input ) )
552 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
554 /* We have finished demuxing data but not playing it */
555 while( !p_input->b_die )
557 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
560 msg_Dbg( p_input, "waiting decoder fifos to empty" );
562 msleep( INPUT_IDLE_SLEEP );
565 /* We have finished */
566 p_input->b_eof = true;
574 vlc_object_release( p_input );
578 /*****************************************************************************
579 * Main loop: Fill buffers from access, and demux
580 *****************************************************************************/
581 static void MainLoop( input_thread_t *p_input )
583 int64_t i_start_mdate = mdate();
584 int64_t i_intf_update = 0;
588 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
590 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
592 bool b_force_update = false;
598 if( p_input->i_state != PAUSE_S )
600 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
601 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
604 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
609 if( p_input->p->input.b_title_demux &&
610 p_input->p->input.p_demux->info.i_update )
612 i_ret = UpdateFromDemux( p_input );
613 b_force_update = true;
615 else if( !p_input->p->input.b_title_demux &&
616 p_input->p->input.p_access &&
617 p_input->p->input.p_access->info.i_update )
619 i_ret = UpdateFromAccess( p_input );
620 b_force_update = true;
624 if( i_ret == 0 ) /* EOF */
628 var_Get( p_input, "input-repeat", &repeat );
629 if( repeat.i_int == 0 )
631 /* End of file - we do not set b_die because only the
632 * playlist is allowed to do so. */
633 msg_Dbg( p_input, "EOF reached" );
634 p_input->p->input.b_eof = true;
638 msg_Dbg( p_input, "repeating the same input (%d)",
640 if( repeat.i_int > 0 )
643 var_Set( p_input, "input-repeat", repeat );
646 /* Seek to start title/seekpoint */
647 val.i_int = p_input->p->input.i_title_start -
648 p_input->p->input.i_title_offset;
649 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
651 input_ControlPush( p_input,
652 INPUT_CONTROL_SET_TITLE, &val );
654 val.i_int = p_input->p->input.i_seekpoint_start -
655 p_input->p->input.i_seekpoint_offset;
656 if( val.i_int > 0 /* TODO: check upper boundary */ )
657 input_ControlPush( p_input,
658 INPUT_CONTROL_SET_SEEKPOINT, &val );
660 /* Seek to start position */
661 if( p_input->p->i_start > 0 )
663 val.i_time = p_input->p->i_start;
664 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
670 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
675 i_start_mdate = mdate();
680 p_input->b_error = true;
683 if( i_ret > 0 && p_input->p->i_slave > 0 )
685 SlaveDemux( p_input );
695 vlc_mutex_lock( &p_input->p->lock_control );
696 ControlReduce( p_input );
697 while( !ControlPopNoLock( p_input, &i_type, &val ) )
699 msg_Dbg( p_input, "control type=%d", i_type );
700 if( Control( p_input, i_type, val ) )
701 b_force_update = true;
703 vlc_mutex_unlock( &p_input->p->lock_control );
705 if( b_force_update || i_intf_update < mdate() )
709 int64_t i_time, i_length;
710 /* update input status variables */
711 if( !demux_Control( p_input->p->input.p_demux,
712 DEMUX_GET_POSITION, &f_pos ) )
714 val.f_float = (float)f_pos;
715 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
717 if( !demux_Control( p_input->p->input.p_demux,
718 DEMUX_GET_TIME, &i_time ) )
720 p_input->i_time = i_time;
722 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
724 if( !demux_Control( p_input->p->input.p_demux,
725 DEMUX_GET_LENGTH, &i_length ) )
728 var_Get( p_input, "length", &old_val );
729 val.i_time = i_length;
730 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
732 if( old_val.i_time != val.i_time )
734 UpdateItemLength( p_input, i_length );
738 var_SetBool( p_input, "intf-change", true );
739 i_intf_update = mdate() + INT64_C(150000);
741 /* 150ms * 8 = ~ 1 second */
742 if( ++i_updates % 8 == 0 )
744 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
745 /* Are we the thread responsible for computing global stats ? */
746 if( libvlc_priv (p_input->p_libvlc)->p_stats_computer == p_input )
748 stats_ComputeGlobalStats( p_input->p_libvlc,
749 p_input->p_libvlc->p_stats );
755 static void InitStatistics( input_thread_t * p_input )
757 if( p_input->b_preparsing ) return;
759 /* Prepare statistics */
760 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
761 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
762 if( libvlc_stats (p_input) )
764 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
765 INIT_COUNTER( read_packets, INTEGER, COUNTER );
766 INIT_COUNTER( demux_read, INTEGER, COUNTER );
767 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
768 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
769 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
770 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
771 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
772 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
773 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
774 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
775 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
776 p_input->p->counters.p_sout_send_bitrate = NULL;
777 p_input->p->counters.p_sout_sent_packets = NULL;
778 p_input->p->counters.p_sout_sent_bytes = NULL;
779 if( p_input->p->counters.p_demux_bitrate )
780 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
781 if( p_input->p->counters.p_input_bitrate )
782 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
787 static int InitSout( input_thread_t * p_input )
791 if( p_input->b_preparsing ) return VLC_SUCCESS;
793 /* Find a usable sout and attach it to p_input */
794 psz = var_GetNonEmptyString( p_input, "sout" );
795 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
797 /* Check the validity of the provided sout */
798 if( p_input->p->p_sout )
800 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
802 msg_Dbg( p_input, "destroying unusable sout" );
804 sout_DeleteInstance( p_input->p->p_sout );
805 p_input->p->p_sout = NULL;
809 if( p_input->p->p_sout )
812 msg_Dbg( p_input, "sout keep: reusing sout" );
813 msg_Dbg( p_input, "sout keep: you probably want to use "
814 "gather stream_out" );
815 vlc_object_attach( p_input->p->p_sout, p_input );
819 /* Create a new one */
820 p_input->p->p_sout = sout_NewInstance( p_input, psz );
821 if( !p_input->p->p_sout )
823 input_ChangeState( p_input, ERROR_S );
824 msg_Err( p_input, "cannot start stream output instance, " \
830 if( libvlc_stats (p_input) )
832 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
833 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
834 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
835 if( p_input->p->counters.p_sout_send_bitrate )
836 p_input->p->counters.p_sout_send_bitrate->update_interval =
840 else if( p_input->p->p_sout )
842 msg_Dbg( p_input, "destroying useless sout" );
844 sout_DeleteInstance( p_input->p->p_sout );
845 p_input->p->p_sout = NULL;
853 static void InitTitle( input_thread_t * p_input )
857 if( p_input->b_preparsing ) return;
859 /* Create global title (from master) */
860 p_input->p->i_title = p_input->p->input.i_title;
861 p_input->p->title = p_input->p->input.title;
862 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
863 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
864 if( p_input->p->i_title > 0 )
866 /* Setup variables */
867 input_ControlVarNavigation( p_input );
868 input_ControlVarTitle( p_input, 0 );
872 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
873 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
874 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
877 if( p_input->i_pts_delay < 0 )
878 p_input->i_pts_delay = 0;
880 /* If the desynchronisation requested by the user is < 0, we need to
881 * cache more data. */
882 var_Get( p_input, "audio-desync", &val );
883 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
885 /* Update cr_average depending on the caching */
886 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
887 p_input->p->input.i_cr_average /= 10;
888 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
891 static void StartTitle( input_thread_t * p_input )
900 /* Start title/chapter */
902 if( p_input->b_preparsing )
904 p_input->p->i_start = 0;
908 val.i_int = p_input->p->input.i_title_start -
909 p_input->p->input.i_title_offset;
910 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
911 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
912 val.i_int = p_input->p->input.i_seekpoint_start -
913 p_input->p->input.i_seekpoint_offset;
914 if( val.i_int > 0 /* TODO: check upper boundary */ )
915 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
919 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
920 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
921 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
922 i_length = var_GetTime( p_input, "length" );
923 if( p_input->p->i_run < 0 )
925 msg_Warn( p_input, "invalid run-time ignored" );
926 p_input->p->i_run = 0;
929 if( p_input->p->i_start > 0 )
931 if( p_input->p->i_start >= i_length )
933 msg_Warn( p_input, "invalid start-time ignored" );
939 msg_Dbg( p_input, "starting at time: %ds",
940 (int)( p_input->p->i_start / INT64_C(1000000) ) );
942 s.i_time = p_input->p->i_start;
943 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
946 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
948 msg_Warn( p_input, "invalid stop-time ignored" );
949 p_input->p->i_stop = 0;
953 /* Get fps and set it if not already set */
954 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
957 float f_requested_fps;
959 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
960 var_SetFloat( p_input, "sub-original-fps", f_fps );
962 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
963 if( f_requested_fps != f_fps )
965 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
967 var_SetFloat( p_input, "sub-fps", f_requested_fps );
971 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
974 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
977 /* Look for and add subtitle files */
978 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
979 if( psz_subtitle != NULL )
981 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
982 input_AddSubtitles( p_input, psz_subtitle, false );
985 var_Get( p_input, "sub-autodetect-file", &val );
988 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
989 char **subs = subtitles_Detect( p_input, psz_autopath,
990 p_input->p->input.p_item->psz_uri );
993 if( psz_autopath == NULL )
994 psz_autopath = strdup("");
996 /* Try to autoselect the first autodetected subtitles file
997 * if no subtitles file was specified */
998 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1000 input_AddSubtitles( p_input, subs[0], false );
1005 /* Then, just add the following subtitles files */
1006 for( ; subs && subs[i]; i++ )
1008 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1010 sub = InputSourceNew( p_input );
1011 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1013 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1020 free( psz_autopath );
1022 free( psz_subtitle );
1024 /* Look for slave */
1025 psz = var_GetNonEmptyString( p_input, "input-slave" );
1029 input_source_t *slave;
1030 while( psz && *psz )
1032 while( *psz == ' ' || *psz == '#' )
1036 if( ( psz_delim = strchr( psz, '#' ) ) )
1038 *psz_delim++ = '\0';
1045 msg_Dbg( p_input, "adding slave input '%s'", psz );
1046 slave = InputSourceNew( p_input );
1047 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1049 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1058 static void InitPrograms( input_thread_t * p_input )
1063 if( p_input->b_preparsing ) return;
1066 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1067 i_es_out_mode = ES_OUT_MODE_AUTO;
1069 if( p_input->p->p_sout )
1071 var_Get( p_input, "sout-all", &val );
1074 i_es_out_mode = ES_OUT_MODE_ALL;
1079 var_Get( p_input, "programs", &val );
1080 if ( val.p_list && val.p_list->i_count )
1082 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1083 /* Note : we should remove the "program" callback. */
1086 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1090 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1092 /* Inform the demuxer about waited group (needed only for DVB) */
1093 if( i_es_out_mode == ES_OUT_MODE_ALL )
1095 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1097 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1099 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1104 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1105 (int) var_GetInteger( p_input, "program" ), NULL );
1109 static int Init( input_thread_t * p_input )
1115 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1117 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1119 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1120 var_SetString( p_input, "sout", "" );
1121 var_SetBool( p_input, "sout-all", false );
1122 var_SetString( p_input, "input-slave", "" );
1123 var_SetInteger( p_input, "input-repeat", 0 );
1124 var_SetString( p_input, "sub-file", "" );
1125 var_SetBool( p_input, "sub-autodetect-file", false );
1129 InitStatistics( p_input );
1131 ret = InitSout( p_input );
1132 if( ret != VLC_SUCCESS )
1133 return ret; /* FIXME: goto error; should be better here */
1137 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1138 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1139 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1141 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1142 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1144 if( InputSourceInit( p_input, &p_input->p->input,
1145 p_input->p->input.p_item->psz_uri, NULL ) )
1150 InitTitle( p_input );
1152 /* Load master infos */
1154 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1155 &val.i_time ) && val.i_time > 0 )
1157 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1158 UpdateItemLength( p_input, val.i_time );
1162 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1163 if( val.i_time > 0 )
1164 { /* fallback: gets length from metadata */
1165 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1166 UpdateItemLength( p_input, val.i_time );
1170 StartTitle( p_input );
1172 InitPrograms( p_input );
1174 if( !p_input->b_preparsing && p_input->p->p_sout )
1176 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1178 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1180 /* We don't want a high input priority here or we'll
1181 * end-up sucking up all the CPU time */
1182 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1185 msg_Dbg( p_input, "starting in %s mode",
1186 p_input->p->b_out_pace_control ? "async" : "sync" );
1189 p_meta = vlc_meta_New();
1191 /* Get meta data from users */
1192 InputMetaUser( p_input, p_meta );
1194 /* Get meta data from master input */
1195 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1197 /* Access_file does not give any meta, and there are no slave */
1198 AccessMeta( p_input, p_meta );
1200 InputUpdateMeta( p_input, p_meta );
1202 if( !p_input->b_preparsing )
1204 msg_Dbg( p_input, "`%s' successfully opened",
1205 p_input->p->input.p_item->psz_uri );
1209 /* initialization is complete */
1210 input_ChangeState( p_input, PLAYING_S );
1215 input_ChangeState( p_input, ERROR_S );
1217 if( p_input->p->p_es_out )
1218 input_EsOutDelete( p_input->p->p_es_out );
1220 if( p_input->p->p_sout )
1222 vlc_object_detach( p_input->p->p_sout );
1223 sout_DeleteInstance( p_input->p->p_sout );
1227 if( !p_input->b_preparsing && libvlc_stats (p_input) )
1229 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1230 stats_CounterClean( p_input->p->counters.p_##c );\
1231 p_input->p->counters.p_##c = NULL; } while(0)
1232 EXIT_COUNTER( read_bytes );
1233 EXIT_COUNTER( read_packets );
1234 EXIT_COUNTER( demux_read );
1235 EXIT_COUNTER( input_bitrate );
1236 EXIT_COUNTER( demux_bitrate );
1237 EXIT_COUNTER( played_abuffers );
1238 EXIT_COUNTER( lost_abuffers );
1239 EXIT_COUNTER( displayed_pictures );
1240 EXIT_COUNTER( lost_pictures );
1241 EXIT_COUNTER( decoded_audio );
1242 EXIT_COUNTER( decoded_video );
1243 EXIT_COUNTER( decoded_sub );
1245 if( p_input->p->p_sout )
1247 EXIT_COUNTER( sout_sent_packets );
1248 EXIT_COUNTER (sout_sent_bytes );
1249 EXIT_COUNTER( sout_send_bitrate );
1254 /* Mark them deleted */
1255 p_input->p->input.p_demux = NULL;
1256 p_input->p->input.p_stream = NULL;
1257 p_input->p->input.p_access = NULL;
1258 p_input->p->p_es_out = NULL;
1259 p_input->p->p_sout = NULL;
1261 return VLC_EGENERIC;
1264 /*****************************************************************************
1265 * WaitDie: Wait until we are asked to die.
1266 *****************************************************************************
1267 * This function is called when an error occurred during thread main's loop.
1268 *****************************************************************************/
1269 static void WaitDie( input_thread_t *p_input )
1271 input_ChangeState( p_input, p_input->b_error ? ERROR_S : END_S );
1272 while( !p_input->b_die )
1275 msleep( INPUT_IDLE_SLEEP );
1279 /*****************************************************************************
1280 * End: end the input thread
1281 *****************************************************************************/
1282 static void End( input_thread_t * p_input )
1286 /* We are at the end */
1287 input_ChangeState( p_input, END_S );
1289 /* Clean control variables */
1290 input_ControlVarStop( p_input );
1292 /* Clean up master */
1293 InputSourceClean( &p_input->p->input );
1296 for( i = 0; i < p_input->p->i_slave; i++ )
1298 InputSourceClean( p_input->p->slave[i] );
1299 free( p_input->p->slave[i] );
1301 free( p_input->p->slave );
1303 /* Unload all modules */
1304 if( p_input->p->p_es_out )
1305 input_EsOutDelete( p_input->p->p_es_out );
1307 if( !p_input->b_preparsing )
1309 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1310 if( libvlc_stats (p_input) )
1312 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
1314 /* make sure we are up to date */
1315 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1316 if( priv->p_stats_computer == p_input )
1318 stats_ComputeGlobalStats( p_input->p_libvlc,
1319 p_input->p_libvlc->p_stats );
1320 priv->p_stats_computer = NULL;
1322 CL_CO( read_bytes );
1323 CL_CO( read_packets );
1324 CL_CO( demux_read );
1325 CL_CO( input_bitrate );
1326 CL_CO( demux_bitrate );
1327 CL_CO( played_abuffers );
1328 CL_CO( lost_abuffers );
1329 CL_CO( displayed_pictures );
1330 CL_CO( lost_pictures );
1331 CL_CO( decoded_audio) ;
1332 CL_CO( decoded_video );
1333 CL_CO( decoded_sub) ;
1336 /* Close optional stream output instance */
1337 if( p_input->p->p_sout )
1339 CL_CO( sout_sent_packets );
1340 CL_CO( sout_sent_bytes );
1341 CL_CO( sout_send_bitrate );
1343 vlc_object_detach( p_input->p->p_sout );
1348 if( p_input->p->i_attachment > 0 )
1350 for( i = 0; i < p_input->p->i_attachment; i++ )
1351 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1352 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1355 /* Tell we're dead */
1356 p_input->b_dead = true;
1359 /*****************************************************************************
1361 *****************************************************************************/
1362 static inline int ControlPopNoLock( input_thread_t *p_input,
1363 int *pi_type, vlc_value_t *p_val )
1365 if( p_input->p->i_control <= 0 )
1367 return VLC_EGENERIC;
1370 *pi_type = p_input->p->control[0].i_type;
1371 *p_val = p_input->p->control[0].val;
1373 p_input->p->i_control--;
1374 if( p_input->p->i_control > 0 )
1378 for( i = 0; i < p_input->p->i_control; i++ )
1380 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1381 p_input->p->control[i].val = p_input->p->control[i+1].val;
1388 static void ControlReduce( input_thread_t *p_input )
1395 for( i = 1; i < p_input->p->i_control; i++ )
1397 const int i_lt = p_input->p->control[i-1].i_type;
1398 const int i_ct = p_input->p->control[i].i_type;
1400 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1401 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1405 ( i_ct == INPUT_CONTROL_SET_STATE ||
1406 i_ct == INPUT_CONTROL_SET_RATE ||
1407 i_ct == INPUT_CONTROL_SET_POSITION ||
1408 i_ct == INPUT_CONTROL_SET_TIME ||
1409 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1410 i_ct == INPUT_CONTROL_SET_TITLE ||
1411 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1412 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1415 // msg_Dbg( p_input, "merged at %d", i );
1416 /* Remove the i-1 */
1417 for( j = i; j < p_input->p->i_control; j++ )
1418 p_input->p->control[j-1] = p_input->p->control[j];
1419 p_input->p->i_control--;
1423 /* TODO but that's not that important
1424 - merge SET_X with SET_X_CMD
1425 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1426 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1433 static bool Control( input_thread_t *p_input, int i_type,
1436 bool b_force_update = false;
1438 if( !p_input ) return b_force_update;
1442 case INPUT_CONTROL_SET_DIE:
1443 msg_Dbg( p_input, "control: stopping input" );
1445 /* Mark all submodules to die */
1446 ObjectKillChildrens( p_input, VLC_OBJECT(p_input) );
1449 case INPUT_CONTROL_SET_POSITION:
1450 case INPUT_CONTROL_SET_POSITION_OFFSET:
1453 if( i_type == INPUT_CONTROL_SET_POSITION )
1455 f_pos = val.f_float;
1459 /* Should not fail */
1460 demux_Control( p_input->p->input.p_demux,
1461 DEMUX_GET_POSITION, &f_pos );
1462 f_pos += val.f_float;
1464 if( f_pos < 0.0 ) f_pos = 0.0;
1465 if( f_pos > 1.0 ) f_pos = 1.0;
1466 /* Reset the decoders states and clock sync (before calling the demuxer */
1467 input_EsOutChangePosition( p_input->p->p_es_out );
1468 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1471 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1472 "%2.1f%% failed", f_pos * 100 );
1476 if( p_input->p->i_slave > 0 )
1477 SlaveSeek( p_input );
1479 b_force_update = true;
1484 case INPUT_CONTROL_SET_TIME:
1485 case INPUT_CONTROL_SET_TIME_OFFSET:
1490 if( i_type == INPUT_CONTROL_SET_TIME )
1492 i_time = val.i_time;
1496 /* Should not fail */
1497 demux_Control( p_input->p->input.p_demux,
1498 DEMUX_GET_TIME, &i_time );
1499 i_time += val.i_time;
1501 if( i_time < 0 ) i_time = 0;
1503 /* Reset the decoders states and clock sync (before calling the demuxer */
1504 input_EsOutChangePosition( p_input->p->p_es_out );
1506 i_ret = demux_Control( p_input->p->input.p_demux,
1507 DEMUX_SET_TIME, i_time );
1512 /* Emulate it with a SET_POS */
1513 demux_Control( p_input->p->input.p_demux,
1514 DEMUX_GET_LENGTH, &i_length );
1517 double f_pos = (double)i_time / (double)i_length;
1518 i_ret = demux_Control( p_input->p->input.p_demux,
1519 DEMUX_SET_POSITION, f_pos );
1524 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1525 " failed or not possible", i_time );
1529 if( p_input->p->i_slave > 0 )
1530 SlaveSeek( p_input );
1532 b_force_update = true;
1537 case INPUT_CONTROL_SET_STATE:
1538 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1539 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1542 if( p_input->p->input.p_access )
1543 i_ret = access_Control( p_input->p->input.p_access,
1544 ACCESS_SET_PAUSE_STATE, false );
1546 i_ret = demux_Control( p_input->p->input.p_demux,
1547 DEMUX_SET_PAUSE_STATE, false );
1551 /* FIXME What to do ? */
1552 msg_Warn( p_input, "cannot unset pause -> EOF" );
1553 vlc_mutex_unlock( &p_input->p->lock_control );
1554 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1555 vlc_mutex_lock( &p_input->p->lock_control );
1558 b_force_update = true;
1560 /* Switch to play */
1561 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1565 input_EsOutChangeState( p_input->p->p_es_out );
1567 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1568 p_input->p->b_can_pause )
1571 if( p_input->p->input.p_access )
1572 i_ret = access_Control( p_input->p->input.p_access,
1573 ACCESS_SET_PAUSE_STATE, true );
1575 i_ret = demux_Control( p_input->p->input.p_demux,
1576 DEMUX_SET_PAUSE_STATE, true );
1578 b_force_update = true;
1582 msg_Warn( p_input, "cannot set pause state" );
1583 state = p_input->i_state;
1590 /* Switch to new state */
1591 input_ChangeStateWithVarCallback( p_input, state, false );
1595 input_EsOutChangeState( p_input->p->p_es_out );
1597 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1599 b_force_update = true;
1601 /* Correct "state" value */
1602 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1604 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1606 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1610 case INPUT_CONTROL_SET_RATE:
1611 case INPUT_CONTROL_SET_RATE_SLOWER:
1612 case INPUT_CONTROL_SET_RATE_FASTER:
1616 if( i_type == INPUT_CONTROL_SET_RATE )
1622 static const int ppi_factor[][2] = {
1623 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1625 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1634 for( i = 0; ppi_factor[i][0] != 0; i++ )
1636 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1637 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1638 if( i_test_e < i_error )
1644 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1646 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1648 if( ppi_factor[i_idx+1][0] > 0 )
1649 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1651 i_rate = INPUT_RATE_MAX+1;
1655 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1657 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1659 i_rate = INPUT_RATE_MIN-1;
1663 if( i_rate < INPUT_RATE_MIN )
1665 msg_Dbg( p_input, "cannot set rate faster" );
1666 i_rate = INPUT_RATE_MIN;
1668 else if( i_rate > INPUT_RATE_MAX )
1670 msg_Dbg( p_input, "cannot set rate slower" );
1671 i_rate = INPUT_RATE_MAX;
1673 if( i_rate != INPUT_RATE_DEFAULT &&
1674 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1675 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1677 msg_Dbg( p_input, "cannot change rate" );
1678 i_rate = INPUT_RATE_DEFAULT;
1680 if( i_rate != p_input->p->i_rate &&
1681 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1684 if( p_input->p->input.p_access )
1685 i_ret = VLC_EGENERIC;
1687 i_ret = demux_Control( p_input->p->input.p_demux,
1688 DEMUX_SET_RATE, &i_rate );
1691 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1692 i_rate = p_input->p->i_rate;
1697 if( i_rate != p_input->p->i_rate )
1700 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1701 var_SetBool( p_input, "rate-change", true );
1703 p_input->p->i_rate = i_rate;
1705 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1706 if( p_input->p->input.b_rescale_ts )
1707 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1709 b_force_update = true;
1714 case INPUT_CONTROL_SET_PROGRAM:
1715 /* No need to force update, es_out does it if needed */
1716 es_out_Control( p_input->p->p_es_out,
1717 ES_OUT_SET_GROUP, val.i_int );
1719 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1723 case INPUT_CONTROL_SET_ES:
1724 /* No need to force update, es_out does it if needed */
1725 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1726 input_EsOutGetFromID( p_input->p->p_es_out,
1730 case INPUT_CONTROL_SET_AUDIO_DELAY:
1731 input_EsOutSetDelay( p_input->p->p_es_out,
1732 AUDIO_ES, val.i_time );
1733 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1736 case INPUT_CONTROL_SET_SPU_DELAY:
1737 input_EsOutSetDelay( p_input->p->p_es_out,
1738 SPU_ES, val.i_time );
1739 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1742 case INPUT_CONTROL_SET_TITLE:
1743 case INPUT_CONTROL_SET_TITLE_NEXT:
1744 case INPUT_CONTROL_SET_TITLE_PREV:
1745 if( p_input->p->input.b_title_demux &&
1746 p_input->p->input.i_title > 0 )
1749 /* FIXME handle demux title */
1750 demux_t *p_demux = p_input->p->input.p_demux;
1753 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1754 i_title = p_demux->info.i_title - 1;
1755 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1756 i_title = p_demux->info.i_title + 1;
1758 i_title = val.i_int;
1760 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1762 input_EsOutChangePosition( p_input->p->p_es_out );
1764 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1765 input_ControlVarTitle( p_input, i_title );
1768 else if( p_input->p->input.i_title > 0 )
1770 access_t *p_access = p_input->p->input.p_access;
1773 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1774 i_title = p_access->info.i_title - 1;
1775 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1776 i_title = p_access->info.i_title + 1;
1778 i_title = val.i_int;
1780 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1782 input_EsOutChangePosition( p_input->p->p_es_out );
1784 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1785 stream_AccessReset( p_input->p->input.p_stream );
1789 case INPUT_CONTROL_SET_SEEKPOINT:
1790 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1791 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1792 if( p_input->p->input.b_title_demux &&
1793 p_input->p->input.i_title > 0 )
1795 demux_t *p_demux = p_input->p->input.p_demux;
1797 int64_t i_input_time;
1798 int64_t i_seekpoint_time;
1800 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1802 i_seekpoint = p_demux->info.i_seekpoint;
1803 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1804 if( i_seekpoint_time >= 0 &&
1805 !demux_Control( p_demux,
1806 DEMUX_GET_TIME, &i_input_time ) )
1808 if ( i_input_time < i_seekpoint_time + 3000000 )
1814 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1815 i_seekpoint = p_demux->info.i_seekpoint + 1;
1817 i_seekpoint = val.i_int;
1819 if( i_seekpoint >= 0 && i_seekpoint <
1820 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1823 input_EsOutChangePosition( p_input->p->p_es_out );
1825 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1828 else if( p_input->p->input.i_title > 0 )
1830 demux_t *p_demux = p_input->p->input.p_demux;
1831 access_t *p_access = p_input->p->input.p_access;
1833 int64_t i_input_time;
1834 int64_t i_seekpoint_time;
1836 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1838 i_seekpoint = p_access->info.i_seekpoint;
1839 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1840 if( i_seekpoint_time >= 0 &&
1841 demux_Control( p_demux,
1842 DEMUX_GET_TIME, &i_input_time ) )
1844 if ( i_input_time < i_seekpoint_time + 3000000 )
1850 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1851 i_seekpoint = p_access->info.i_seekpoint + 1;
1853 i_seekpoint = val.i_int;
1855 if( i_seekpoint >= 0 && i_seekpoint <
1856 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1858 input_EsOutChangePosition( p_input->p->p_es_out );
1860 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1862 stream_AccessReset( p_input->p->input.p_stream );
1867 case INPUT_CONTROL_ADD_SLAVE:
1868 if( val.psz_string )
1870 input_source_t *slave = InputSourceNew( p_input );
1872 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1878 msg_Dbg( p_input, "adding %s as slave on the fly",
1882 if( demux_Control( p_input->p->input.p_demux,
1883 DEMUX_GET_TIME, &i_time ) )
1885 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1886 InputSourceClean( slave );
1890 if( demux_Control( slave->p_demux,
1891 DEMUX_SET_TIME, i_time ) )
1893 msg_Err( p_input, "seek failed for new slave" );
1894 InputSourceClean( slave );
1899 /* Get meta (access and demux) */
1900 p_meta = vlc_meta_New();
1901 access_Control( slave->p_access, ACCESS_GET_META,
1903 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1904 InputUpdateMeta( p_input, p_meta );
1906 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1911 msg_Warn( p_input, "failed to add %s as slave",
1915 free( val.psz_string );
1919 case INPUT_CONTROL_SET_BOOKMARK:
1921 msg_Err( p_input, "not yet implemented" );
1925 return b_force_update;
1928 /*****************************************************************************
1930 *****************************************************************************/
1931 static int UpdateFromDemux( input_thread_t *p_input )
1933 demux_t *p_demux = p_input->p->input.p_demux;
1936 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1938 v.i_int = p_demux->info.i_title;
1939 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1941 input_ControlVarTitle( p_input, p_demux->info.i_title );
1943 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1945 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1947 v.i_int = p_demux->info.i_seekpoint;
1948 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1950 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1952 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1954 /* Hmmm only works with master input */
1955 if( p_input->p->input.p_demux == p_demux )
1957 int i_title_end = p_input->p->input.i_title_end -
1958 p_input->p->input.i_title_offset;
1959 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1960 p_input->p->input.i_seekpoint_offset;
1962 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1964 if( p_demux->info.i_title > i_title_end ||
1965 ( p_demux->info.i_title == i_title_end &&
1966 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1968 else if( i_seekpoint_end >=0 )
1970 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1972 else if( i_title_end >= 0 )
1974 if( p_demux->info.i_title > i_title_end ) return 0;
1981 /*****************************************************************************
1983 *****************************************************************************/
1984 static int UpdateFromAccess( input_thread_t *p_input )
1986 access_t *p_access = p_input->p->input.p_access;
1989 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1991 v.i_int = p_access->info.i_title;
1992 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1994 input_ControlVarTitle( p_input, p_access->info.i_title );
1996 stream_AccessUpdate( p_input->p->input.p_stream );
1998 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2000 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2002 v.i_int = p_access->info.i_seekpoint;
2003 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2004 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2006 if( p_access->info.i_update & INPUT_UPDATE_META )
2008 /* TODO maybe multi - access ? */
2009 vlc_meta_t *p_meta = vlc_meta_New();
2010 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2011 InputUpdateMeta( p_input, p_meta );
2012 p_access->info.i_update &= ~INPUT_UPDATE_META;
2015 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2017 /* Hmmm only works with master input */
2018 if( p_input->p->input.p_access == p_access )
2020 int i_title_end = p_input->p->input.i_title_end -
2021 p_input->p->input.i_title_offset;
2022 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2023 p_input->p->input.i_seekpoint_offset;
2025 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2027 if( p_access->info.i_title > i_title_end ||
2028 ( p_access->info.i_title == i_title_end &&
2029 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2031 else if( i_seekpoint_end >=0 )
2033 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2035 else if( i_title_end >= 0 )
2037 if( p_access->info.i_title > i_title_end ) return 0;
2044 /*****************************************************************************
2046 *****************************************************************************/
2047 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2049 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2052 /*****************************************************************************
2054 *****************************************************************************/
2055 static input_source_t *InputSourceNew( input_thread_t *p_input )
2057 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2059 memset( in, 0, sizeof( input_source_t ) );
2063 /*****************************************************************************
2065 *****************************************************************************/
2066 static int InputSourceInit( input_thread_t *p_input,
2067 input_source_t *in, const char *psz_mrl,
2068 const char *psz_forced_demux )
2070 char psz_dup[strlen (psz_mrl) + 1];
2071 const char *psz_access;
2072 const char *psz_demux;
2079 strcpy( psz_dup, psz_mrl );
2081 if( !in ) return VLC_EGENERIC;
2082 if( !p_input ) return VLC_EGENERIC;
2085 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2087 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2088 psz_mrl, psz_access, psz_demux, psz_path );
2089 if( !p_input->b_preparsing )
2091 /* Hack to allow udp://@:port syntax */
2093 (strncmp( psz_access, "udp", 3 ) &&
2094 strncmp( psz_access, "rtp", 3 )) )
2096 /* Find optional titles and seekpoints */
2097 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2098 &in->i_seekpoint_start, &in->i_seekpoint_end );
2101 if( psz_forced_demux && *psz_forced_demux )
2103 psz_demux = psz_forced_demux;
2105 else if( *psz_demux == '\0' )
2107 /* special hack for forcing a demuxer with --demux=module
2108 * (and do nothing with a list) */
2109 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2111 if( psz_var_demux != NULL &&
2112 !strchr(psz_var_demux, ',' ) &&
2113 !strchr(psz_var_demux, ':' ) )
2115 psz_demux = psz_var_demux;
2117 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2121 /* Try access_demux if no demux given */
2122 if( *psz_demux == '\0' )
2124 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2125 NULL, p_input->p->p_es_out, false );
2130 /* Preparsing is only for file:// */
2133 if( !*psz_access ) /* path without scheme:// */
2134 psz_access = "file";
2135 if( strcmp( psz_access, "file" ) )
2137 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2142 int64_t i_pts_delay;
2144 /* Get infos from access_demux */
2145 demux_Control( in->p_demux,
2146 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2147 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2149 in->b_title_demux = true;
2150 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2151 &in->title, &in->i_title,
2152 &in->i_title_offset, &in->i_seekpoint_offset ) )
2157 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2158 &in->b_can_pace_control ) )
2159 in->b_can_pace_control = false;
2161 if( !in->b_can_pace_control )
2163 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2164 &in->b_can_rate_control, &in->b_rescale_ts ) )
2166 in->b_can_rate_control = false;
2167 in->b_rescale_ts = true; /* not used */
2172 in->b_can_rate_control = true;
2173 in->b_rescale_ts = true;
2175 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2176 &in->b_can_pause ) )
2177 in->b_can_pause = false;
2178 var_SetBool( p_input, "can-pause", in->b_can_pause );
2180 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2182 if( ret != VLC_SUCCESS )
2184 var_Set( p_input, "seekable", val );
2188 int64_t i_pts_delay;
2190 input_ChangeState( p_input, OPENING_S );
2192 /* Now try a real access */
2193 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2195 /* Access failed, URL encoded ? */
2196 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2198 decode_URI( psz_path );
2200 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2201 psz_access, psz_demux, psz_path );
2203 in->p_access = access_New( p_input,
2204 psz_access, psz_demux, psz_path );
2206 if( in->p_access == NULL )
2208 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2210 intf_UserFatal( VLC_OBJECT( p_input), false,
2211 _("Your input can't be opened"),
2212 _("VLC is unable to open the MRL '%s'."
2213 " Check the log for details."), psz_mrl );
2218 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2219 while( psz && *psz )
2221 access_t *p_access = in->p_access;
2222 char *end = strchr( psz, ':' );
2227 in->p_access = access_FilterNew( in->p_access, psz );
2228 if( in->p_access == NULL )
2230 in->p_access = p_access;
2231 msg_Warn( p_input, "failed to insert access filter %s",
2239 /* Get infos from access */
2240 if( !p_input->b_preparsing )
2242 access_Control( in->p_access,
2243 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2244 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2246 in->b_title_demux = false;
2247 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2248 &in->title, &in->i_title,
2249 &in->i_title_offset, &in->i_seekpoint_offset ) )
2255 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2256 &in->b_can_pace_control );
2257 in->b_can_rate_control = in->b_can_pace_control;
2258 in->b_rescale_ts = true;
2260 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2262 var_SetBool( p_input, "can-pause", in->b_can_pause );
2263 access_Control( in->p_access, ACCESS_CAN_SEEK,
2265 var_Set( p_input, "seekable", val );
2268 input_ChangeState( p_input, BUFFERING_S );
2270 /* Create the stream_t */
2271 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2272 if( in->p_stream == NULL )
2274 msg_Warn( p_input, "cannot create a stream_t from access" );
2278 /* Open a demuxer */
2279 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2281 psz_demux = in->p_access->psz_demux;
2285 /* Take access redirections into account */
2286 char *psz_real_path;
2287 char *psz_buf = NULL;
2288 if( in->p_access->psz_path )
2290 const char *psz_a, *psz_d;
2291 psz_buf = strdup( in->p_access->psz_path );
2292 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2296 psz_real_path = psz_path;
2298 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2300 in->p_stream, p_input->p->p_es_out,
2301 p_input->b_preparsing );
2305 if( in->p_demux == NULL )
2307 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2308 psz_access, psz_demux, psz_path );
2309 intf_UserFatal( VLC_OBJECT( p_input ), false,
2310 _("VLC can't recognize the input's format"),
2311 _("The format of '%s' cannot be detected. "
2312 "Have a look the log for details."), psz_mrl );
2316 /* Get title from demux */
2317 if( !p_input->b_preparsing && in->i_title <= 0 )
2319 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2320 &in->title, &in->i_title,
2321 &in->i_title_offset, &in->i_seekpoint_offset ))
2323 TAB_INIT( in->i_title, in->title );
2327 in->b_title_demux = true;
2333 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2334 if( 1 || !p_input->b_preparsing )
2337 input_attachment_t **attachment;
2338 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2339 &attachment, &i_attachment ) )
2341 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2342 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2343 i_attachment, attachment );
2344 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2347 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2349 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2351 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2354 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2355 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2360 input_ChangeState( p_input, ERROR_S );
2363 demux_Delete( in->p_demux );
2366 stream_Delete( in->p_stream );
2369 access_Delete( in->p_access );
2371 return VLC_EGENERIC;
2374 /*****************************************************************************
2376 *****************************************************************************/
2377 static void InputSourceClean( input_source_t *in )
2382 demux_Delete( in->p_demux );
2385 stream_Delete( in->p_stream );
2388 access_Delete( in->p_access );
2390 if( in->i_title > 0 )
2392 for( i = 0; i < in->i_title; i++ )
2393 vlc_input_title_Delete( in->title[i] );
2394 TAB_CLEAN( in->i_title, in->title );
2398 static void SlaveDemux( input_thread_t *p_input )
2403 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2405 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2409 for( i = 0; i < p_input->p->i_slave; i++ )
2411 input_source_t *in = p_input->p->slave[i];
2417 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2422 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2424 msg_Err( p_input, "slave[%d] doesn't like "
2425 "DEMUX_GET_TIME -> EOF", i );
2430 if( i_stime >= i_time )
2433 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2439 i_ret = in->p_demux->pf_demux( in->p_demux );
2444 msg_Dbg( p_input, "slave %d EOF", i );
2450 static void SlaveSeek( input_thread_t *p_input )
2455 if( !p_input ) return;
2457 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2459 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2463 for( i = 0; i < p_input->p->i_slave; i++ )
2465 input_source_t *in = p_input->p->slave[i];
2467 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2469 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2475 /*****************************************************************************
2477 *****************************************************************************/
2478 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2482 if( !p_meta ) return;
2484 /* Get meta information from user */
2485 #define GET_META( field, s ) \
2486 var_Get( p_input, (s), &val ); \
2487 if( *val.psz_string ) \
2488 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2489 free( val.psz_string )
2491 GET_META( Title, "meta-title" );
2492 GET_META( Artist, "meta-artist" );
2493 GET_META( Genre, "meta-genre" );
2494 GET_META( Copyright, "meta-copyright" );
2495 GET_META( Description, "meta-description" );
2496 GET_META( Date, "meta-date" );
2497 GET_META( URL, "meta-url" );
2501 /*****************************************************************************
2502 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2503 * arturl and locking issue.
2504 *****************************************************************************/
2505 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2507 input_item_t *p_item = p_input->p->input.p_item;
2508 char * psz_arturl = NULL;
2509 char *psz_title = NULL;
2510 int i_arturl_event = false;
2515 psz_arturl = input_item_GetArtURL( p_item );
2517 vlc_mutex_lock( &p_item->lock );
2518 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2519 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2521 vlc_meta_Merge( p_item->p_meta, p_meta );
2523 if( psz_arturl && *psz_arturl )
2525 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2526 i_arturl_event = true;
2529 vlc_meta_Delete( p_meta );
2531 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2533 /* Don't look for art cover if sout
2534 * XXX It can change when sout has meta data support */
2535 if( p_input->p->p_sout && !p_input->b_preparsing )
2537 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2538 i_arturl_event = true;
2542 input_ExtractAttachmentAndCacheArt( p_input );
2548 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2550 p_meta = vlc_meta_New();
2551 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2553 vlc_mutex_unlock( &p_item->lock );
2555 input_item_SetPreparsed( p_item, true );
2557 if( i_arturl_event == true )
2561 /* Notify interested third parties */
2562 event.type = vlc_InputItemMetaChanged;
2563 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2564 vlc_event_send( &p_item->event_manager, &event );
2569 input_Control( p_input, INPUT_SET_NAME, psz_title );
2573 /** \todo handle sout meta */
2577 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2578 int i_new, input_attachment_t **pp_new )
2580 int i_attachment = *pi_attachment;
2581 input_attachment_t **attachment = *ppp_attachment;
2584 attachment = realloc( attachment,
2585 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2586 for( i = 0; i < i_new; i++ )
2587 attachment[i_attachment++] = pp_new[i];
2591 *pi_attachment = i_attachment;
2592 *ppp_attachment = attachment;
2595 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2599 if( p_input->b_preparsing )
2602 if( p_input->p->input.p_access )
2603 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2606 /* Get meta data from slave input */
2607 for( i = 0; i < p_input->p->i_slave; i++ )
2609 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2610 if( p_input->p->slave[i]->p_access )
2612 access_Control( p_input->p->slave[i]->p_access,
2613 ACCESS_GET_META, p_meta );
2618 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2625 /* XXX I am not sure it is a great idea, besides, there is more than that
2626 * if we want to do it right */
2627 vlc_mutex_lock( &p_item->lock );
2628 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2630 vlc_mutex_unlock( &p_item->lock );
2633 vlc_mutex_unlock( &p_item->lock );
2636 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2637 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2642 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2643 if(! p_demux->p_private )
2646 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2649 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2651 if( p_demux_meta->p_meta )
2653 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2654 vlc_meta_Delete( p_demux_meta->p_meta );
2657 if( p_demux_meta->i_attachments > 0 )
2659 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2660 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2661 p_demux_meta->i_attachments, p_demux_meta->attachments );
2662 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2664 module_Unneed( p_demux, p_id3 );
2666 free( p_demux->p_private );
2670 /*****************************************************************************
2671 * MRLSplit: parse the access, demux and url part of the
2672 * Media Resource Locator.
2673 *****************************************************************************/
2674 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2677 char *psz_access = NULL;
2678 char *psz_demux = NULL;
2681 /* Either there is an access/demux specification before ://
2682 * or we have a plain local file path. */
2683 psz_path = strstr( psz_dup, "://" );
2684 if( psz_path != NULL )
2687 psz_path += 3; /* skips "://" */
2689 /* Separate access from demux (<access>/<demux>://<path>) */
2690 psz_access = psz_dup;
2691 psz_demux = strchr( psz_access, '/' );
2693 *psz_demux++ = '\0';
2695 /* We really don't want module name substitution here! */
2696 if( psz_access[0] == '$' )
2698 if( psz_demux && psz_demux[0] == '$' )
2706 *ppsz_access = psz_access ? psz_access : "";
2707 *ppsz_demux = psz_demux ? psz_demux : "";
2708 *ppsz_path = psz_path ? psz_path : "";
2711 /*****************************************************************************
2712 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2715 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2716 *****************************************************************************/
2717 static void MRLSections( input_thread_t *p_input, char *psz_source,
2718 int *pi_title_start, int *pi_title_end,
2719 int *pi_chapter_start, int *pi_chapter_end )
2721 char *psz, *psz_end, *psz_next, *psz_check;
2723 *pi_title_start = *pi_title_end = -1;
2724 *pi_chapter_start = *pi_chapter_end = -1;
2726 /* Start by parsing titles and chapters */
2727 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2729 /* Check we are really dealing with a title/chapter section */
2730 psz_check = psz + 1;
2731 if( !*psz_check ) return;
2732 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2733 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2734 if( *psz_check == ':' && ++psz_check )
2735 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2736 if( *psz_check != '-' && *psz_check ) return;
2737 if( *psz_check == '-' && ++psz_check )
2738 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2739 if( *psz_check != ':' && *psz_check ) return;
2740 if( *psz_check == ':' && ++psz_check )
2741 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2742 if( *psz_check ) return;
2744 /* Separate start and end */
2746 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2748 /* Look for the start title */
2749 *pi_title_start = strtol( psz, &psz_next, 0 );
2750 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2751 *pi_title_end = *pi_title_start;
2754 /* Look for the start chapter */
2756 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2757 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2758 *pi_chapter_end = *pi_chapter_start;
2762 /* Look for the end title */
2763 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2764 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2767 /* Look for the end chapter */
2768 if( *psz_end ) psz_end++;
2769 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2770 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2773 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2774 psz_source, *pi_title_start, *pi_chapter_start,
2775 *pi_title_end, *pi_chapter_end );
2778 /*****************************************************************************
2779 * input_AddSubtitles: add a subtitles file and enable it
2780 *****************************************************************************/
2781 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2782 bool b_check_extension )
2784 input_source_t *sub;
2787 char *psz_path, *psz_extension;
2789 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2794 /* if we are provided a subtitle.sub file,
2795 * see if we don't have a subtitle.idx and use it instead */
2796 psz_path = strdup( psz_subtitle );
2799 psz_extension = strrchr( psz_path, '.');
2800 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2804 strcpy( psz_extension, ".idx" );
2805 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2806 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2809 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2810 psz_path, psz_subtitle );
2811 strcpy( psz_subtitle, psz_path );
2817 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2819 sub = InputSourceNew( p_input );
2820 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2822 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2825 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2827 if( count.i_int == 0 )
2829 /* if it was first one, there is disable too */
2831 if( count.i_int < list.p_list->i_count )
2833 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2834 &list.p_list->p_values[count.i_int] );
2836 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2844 /*****************************************************************************
2845 * input_get_event_manager
2846 *****************************************************************************/
2847 vlc_event_manager_t *
2848 input_get_event_manager( input_thread_t *p_input )
2850 return &p_input->p->event_manager;