1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
38 #include "input_internal.h"
41 #include "../stream_output/stream_output.h"
43 #include <vlc_interface.h>
45 #include <vlc_charset.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 static void Destructor( input_thread_t * p_input );
56 static int Run ( input_thread_t *p_input );
57 static int RunAndDestroy ( input_thread_t *p_input );
59 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
60 const char *, bool, sout_instance_t * );
61 static int Init ( input_thread_t *p_input );
62 static void Error ( input_thread_t *p_input );
63 static void End ( input_thread_t *p_input );
64 static void MainLoop( input_thread_t *p_input );
66 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
67 static void ControlReduce( input_thread_t * );
68 static bool Control( input_thread_t *, int, vlc_value_t );
70 static int UpdateFromAccess( input_thread_t * );
71 static int UpdateFromDemux( input_thread_t * );
73 static void UpdateItemLength( input_thread_t *, int64_t i_length );
75 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
77 static input_source_t *InputSourceNew( input_thread_t *);
78 static int InputSourceInit( input_thread_t *, input_source_t *,
79 const char *, const char *psz_forced_demux );
80 static void InputSourceClean( input_source_t * );
82 //static void InputGetAttachments( input_thread_t *, input_source_t * );
83 static void SlaveDemux( input_thread_t *p_input );
84 static void SlaveSeek( input_thread_t *p_input );
86 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
87 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
89 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
90 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
91 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
92 int i_new, input_attachment_t **pp_new );
94 /*****************************************************************************
95 * This function creates a new input, and returns a pointer
96 * to its description. On error, it returns NULL.
98 * Variables for _public_ use:
101 * - rate,rate-slower, rate-faster
102 * - position, position-offset
103 * - time, time-offset
104 * - title,title-next,title-prev
105 * - chapter,chapter-next, chapter-prev
106 * - program, audio-es, video-es, spu-es
107 * - audio-delay, spu-delay
112 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
113 * or not, for that check position != 0.0)
115 * * For intf callback upon changes
117 * - rate-change for when playback rate changes
118 * TODO explain when Callback is called
119 * TODO complete this list (?)
120 *****************************************************************************/
121 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
122 const char *psz_header, bool b_quick,
123 sout_instance_t *p_sout )
125 static const char input_name[] = "input";
126 input_thread_t *p_input = NULL; /* thread descriptor */
130 /* Allocate descriptor */
131 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
132 VLC_OBJECT_INPUT, input_name );
133 if( p_input == NULL )
135 msg_Err( p_parent, "out of memory" );
139 /* Construct a nice name for the input timer */
140 char psz_timer_name[255];
141 char * psz_name = input_item_GetName( p_item );
142 snprintf( psz_timer_name, sizeof(psz_timer_name),
143 "input launching for '%s'", psz_name );
146 /* Start a timer to mesure how long it takes
147 * to launch an input */
148 stats_TimerStart( p_input, psz_timer_name,
149 STATS_TIMER_INPUT_LAUNCHING );
151 MALLOC_NULL( p_input->p, input_thread_private_t );
152 memset( p_input->p, 0, sizeof( input_thread_private_t ) );
154 /* One "randomly" selected input thread is responsible for computing
155 * the global stats. Check if there is already someone doing this */
156 if( p_input->p_libvlc->p_stats && !b_quick )
158 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
159 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
160 if( priv->p_stats_computer == NULL )
161 priv->p_stats_computer = p_input;
162 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
165 p_input->b_preparsing = b_quick;
166 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
169 vlc_event_manager_t * p_em = &p_input->p->event_manager;
170 vlc_event_manager_init_with_vlc_object( p_em, p_input );
171 vlc_event_manager_register_event_type( p_em, vlc_InputStateChanged );
172 vlc_event_manager_register_event_type( p_em, vlc_InputSelectedStreamChanged );
174 /* Init Common fields */
175 p_input->b_eof = false;
176 p_input->b_can_pace_control = true;
177 p_input->p->i_start = 0;
179 p_input->p->i_stop = 0;
180 p_input->p->i_run = 0;
181 p_input->p->i_title = 0;
182 p_input->p->title = NULL;
183 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
184 p_input->i_state = INIT_S;
185 p_input->p->i_rate = INPUT_RATE_DEFAULT;
186 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
187 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
188 p_input->p->p_es_out = NULL;
189 p_input->p->p_sout = NULL;
190 p_input->p->b_out_pace_control = false;
191 p_input->i_pts_delay = 0;
193 /* Init Input fields */
194 vlc_gc_incref( p_item ); /* Released in Destructor() */
195 p_input->p->input.p_item = p_item;
196 p_input->p->input.p_access = NULL;
197 p_input->p->input.p_stream = NULL;
198 p_input->p->input.p_demux = NULL;
199 p_input->p->input.b_title_demux = false;
200 p_input->p->input.i_title = 0;
201 p_input->p->input.title = NULL;
202 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
203 p_input->p->input.b_can_pace_control = true;
204 p_input->p->input.b_can_rate_control = true;
205 p_input->p->input.b_rescale_ts = true;
206 p_input->p->input.b_eof = false;
207 p_input->p->input.i_cr_average = 0;
209 vlc_mutex_lock( &p_item->lock );
211 if( !p_item->p_stats )
212 p_item->p_stats = stats_NewInputStats( p_input );
213 vlc_mutex_unlock( &p_item->lock );
216 p_input->p->i_slave = 0;
217 p_input->p->slave = NULL;
219 /* Init control buffer */
220 vlc_mutex_init( &p_input->p->lock_control );
221 p_input->p->i_control = 0;
223 /* Parse input options */
224 vlc_mutex_lock( &p_item->lock );
225 assert( p_item->optflagc == p_item->i_options );
226 for( i = 0; i < p_item->i_options; i++ )
227 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
228 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
229 vlc_mutex_unlock( &p_item->lock );
231 /* Create Object Variables for private use only */
232 input_ConfigVarInit( p_input );
234 /* Create Objects variables for public Get and Set */
235 input_ControlVarInit( p_input );
237 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
239 if( !p_input->b_preparsing )
241 var_Get( p_input, "bookmarks", &val );
244 /* FIXME: have a common cfg parsing routine used by sout and others */
245 char *psz_parser, *psz_start, *psz_end;
246 psz_parser = val.psz_string;
247 while( (psz_start = strchr( psz_parser, '{' ) ) )
249 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
252 psz_end = strchr( psz_start, '}' );
253 if( !psz_end ) break;
254 psz_parser = psz_end + 1;
255 backup = *psz_parser;
258 while( (psz_end = strchr( psz_start, ',' ) ) )
261 if( !strncmp( psz_start, "name=", 5 ) )
263 p_seekpoint->psz_name = strdup(psz_start + 5);
265 else if( !strncmp( psz_start, "bytes=", 6 ) )
267 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
269 else if( !strncmp( psz_start, "time=", 5 ) )
271 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
274 psz_start = psz_end + 1;
276 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
277 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
278 p_seekpoint->i_time_offset );
279 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
280 vlc_seekpoint_Delete( p_seekpoint );
281 *psz_parser = backup;
283 free( val.psz_string );
287 /* Remove 'Now playing' info as it is probably outdated */
288 input_item_SetNowPlaying( p_item, NULL );
291 if( p_input->b_preparsing )
292 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
296 p_input->p->p_sout = p_sout;
298 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
299 vlc_mutex_init( &p_input->p->counters.counters_lock );
301 /* Attach only once we are ready */
302 vlc_object_attach( p_input, p_parent );
304 /* Set the destructor when we are sure we are initialized */
305 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
311 * Input destructor (called when the object's refcount reaches 0).
313 static void Destructor( input_thread_t * p_input )
315 input_thread_private_t *priv = p_input->p;
317 vlc_event_manager_fini( &p_input->p->event_manager );
319 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
320 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
323 sout_DeleteInstance( priv->p_sout );
325 vlc_gc_decref( p_input->p->input.p_item );
327 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
329 vlc_mutex_destroy( &priv->lock_control );
334 * Initialize an input thread and run it. You will need to monitor the
335 * thread to clean up after it is done
337 * \param p_parent a vlc_object
338 * \param p_item an input item
339 * \return a pointer to the spawned input thread
341 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
342 input_item_t *p_item )
344 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
348 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
349 input_item_t *p_item,
350 const char *psz_log, sout_instance_t *p_sout )
352 input_thread_t *p_input;
354 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
358 /* Create thread and wait for its readiness. */
359 if( vlc_thread_create( p_input, "input", Run,
360 VLC_THREAD_PRIORITY_INPUT, true ) )
362 input_ChangeState( p_input, ERROR_S );
363 msg_Err( p_input, "cannot create input thread" );
364 vlc_object_detach( p_input );
365 vlc_object_release( p_input );
373 * Initialize an input thread and run it. This thread will clean after itself,
374 * you can forget about it. It can work either in blocking or non-blocking mode
376 * \param p_parent a vlc_object
377 * \param p_item an input item
378 * \param b_block should we block until read is finished ?
379 * \return the input object id if non blocking, an error code else
381 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
384 input_thread_t *p_input;
386 p_input = Create( p_parent, p_item, NULL, false, NULL );
392 RunAndDestroy( p_input );
397 if( vlc_thread_create( p_input, "input", RunAndDestroy,
398 VLC_THREAD_PRIORITY_INPUT, true ) )
400 input_ChangeState( p_input, ERROR_S );
401 msg_Err( p_input, "cannot create input thread" );
402 vlc_object_release( p_input );
406 return p_input->i_object_id;
410 * Initialize an input and initialize it to preparse the item
411 * This function is blocking. It will only accept to parse files
413 * \param p_parent a vlc_object_t
414 * \param p_item an input item
415 * \return VLC_SUCCESS or an error
417 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
419 input_thread_t *p_input;
421 /* Allocate descriptor */
422 p_input = Create( p_parent, p_item, NULL, true, NULL );
426 if( !Init( p_input ) )
429 vlc_object_detach( p_input );
430 vlc_object_release( p_input );
436 * Request a running input thread to stop and die
438 * \param the input thread to stop
440 void input_StopThread( input_thread_t *p_input )
445 /* Set die for input */
446 vlc_object_kill( p_input );
447 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
449 /* We cannot touch p_input fields directly (we come from another thread),
450 * so use the vlc_object_find way, it's perfectly safe */
452 /* Set die for all access, stream, demux, etc */
453 p_list = vlc_list_children( p_input );
454 for( i = 0; i < p_list->i_count; i++ )
455 vlc_object_kill( p_list->p_values[i].p_object );
456 vlc_list_release( p_list );
458 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
461 sout_instance_t * input_DetachSout( input_thread_t *p_input )
463 sout_instance_t *p_sout = p_input->p->p_sout;
464 vlc_object_detach( p_sout );
465 p_input->p->p_sout = NULL;
469 /*****************************************************************************
470 * Run: main thread loop
471 * This is the "normal" thread that spawns the input processing chain,
472 * reads the stream, cleans up and waits
473 *****************************************************************************/
474 static int Run( input_thread_t *p_input )
476 /* Signal that the thread is launched */
477 vlc_thread_ready( p_input );
479 if( Init( p_input ) )
481 /* If we failed, wait before we are killed, and exit */
482 p_input->b_error = true;
486 /* Tell we're dead */
487 p_input->b_dead = true;
494 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
496 /* We have finish to demux data but not to play them */
497 while( !p_input->b_die )
499 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
502 msg_Dbg( p_input, "waiting decoder fifos to empty" );
504 msleep( INPUT_IDLE_SLEEP );
507 /* We have finished */
508 p_input->b_eof = true;
509 input_ChangeState( p_input, END_S );
512 /* Wait until we are asked to die */
513 if( !p_input->b_die )
524 /*****************************************************************************
525 * RunAndDestroy: main thread loop
526 * This is the "just forget me" thread that spawns the input processing chain,
527 * reads the stream, cleans up and releases memory
528 *****************************************************************************/
529 static int RunAndDestroy( input_thread_t *p_input )
531 /* Signal that the thread is launched */
532 vlc_thread_ready( p_input );
534 if( Init( p_input ) )
539 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
541 /* We have finished demuxing data but not playing it */
542 while( !p_input->b_die )
544 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
547 msg_Dbg( p_input, "waiting decoder fifos to empty" );
549 msleep( INPUT_IDLE_SLEEP );
552 /* We have finished */
553 p_input->b_eof = true;
561 vlc_object_release( p_input );
565 /*****************************************************************************
566 * Main loop: Fill buffers from access, and demux
567 *****************************************************************************/
568 static void MainLoop( input_thread_t *p_input )
570 int64_t i_start_mdate = mdate();
571 int64_t i_intf_update = 0;
575 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
577 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
579 bool b_force_update = false;
585 if( p_input->i_state != PAUSE_S )
587 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
588 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
591 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
596 if( p_input->p->input.b_title_demux &&
597 p_input->p->input.p_demux->info.i_update )
599 i_ret = UpdateFromDemux( p_input );
600 b_force_update = true;
602 else if( !p_input->p->input.b_title_demux &&
603 p_input->p->input.p_access &&
604 p_input->p->input.p_access->info.i_update )
606 i_ret = UpdateFromAccess( p_input );
607 b_force_update = true;
611 if( i_ret == 0 ) /* EOF */
615 var_Get( p_input, "input-repeat", &repeat );
616 if( repeat.i_int == 0 )
618 /* End of file - we do not set b_die because only the
619 * playlist is allowed to do so. */
620 msg_Dbg( p_input, "EOF reached" );
621 p_input->p->input.b_eof = true;
625 msg_Dbg( p_input, "repeating the same input (%d)",
627 if( repeat.i_int > 0 )
630 var_Set( p_input, "input-repeat", repeat );
633 /* Seek to start title/seekpoint */
634 val.i_int = p_input->p->input.i_title_start -
635 p_input->p->input.i_title_offset;
636 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
638 input_ControlPush( p_input,
639 INPUT_CONTROL_SET_TITLE, &val );
641 val.i_int = p_input->p->input.i_seekpoint_start -
642 p_input->p->input.i_seekpoint_offset;
643 if( val.i_int > 0 /* TODO: check upper boundary */ )
644 input_ControlPush( p_input,
645 INPUT_CONTROL_SET_SEEKPOINT, &val );
647 /* Seek to start position */
648 if( p_input->p->i_start > 0 )
650 val.i_time = p_input->p->i_start;
651 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
657 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
662 i_start_mdate = mdate();
667 p_input->b_error = true;
670 if( i_ret > 0 && p_input->p->i_slave > 0 )
672 SlaveDemux( p_input );
682 vlc_mutex_lock( &p_input->p->lock_control );
683 ControlReduce( p_input );
684 while( !ControlPopNoLock( p_input, &i_type, &val ) )
686 msg_Dbg( p_input, "control type=%d", i_type );
687 if( Control( p_input, i_type, val ) )
688 b_force_update = true;
690 vlc_mutex_unlock( &p_input->p->lock_control );
692 if( b_force_update || i_intf_update < mdate() )
696 int64_t i_time, i_length;
697 /* update input status variables */
698 if( !demux_Control( p_input->p->input.p_demux,
699 DEMUX_GET_POSITION, &f_pos ) )
701 val.f_float = (float)f_pos;
702 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
704 if( !demux_Control( p_input->p->input.p_demux,
705 DEMUX_GET_TIME, &i_time ) )
707 p_input->i_time = i_time;
709 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
711 if( !demux_Control( p_input->p->input.p_demux,
712 DEMUX_GET_LENGTH, &i_length ) )
715 var_Get( p_input, "length", &old_val );
716 val.i_time = i_length;
717 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
719 if( old_val.i_time != val.i_time )
721 UpdateItemLength( p_input, i_length );
725 var_SetBool( p_input, "intf-change", true );
726 i_intf_update = mdate() + INT64_C(150000);
728 /* 150ms * 8 = ~ 1 second */
729 if( ++i_updates % 8 == 0 )
731 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
732 /* Are we the thread responsible for computing global stats ? */
733 if( libvlc_priv (p_input->p_libvlc)->p_stats_computer == p_input )
735 stats_ComputeGlobalStats( p_input->p_libvlc,
736 p_input->p_libvlc->p_stats );
742 static void InitStatistics( input_thread_t * p_input )
744 if( p_input->b_preparsing ) return;
746 /* Prepare statistics */
747 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
748 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
749 if( libvlc_stats (p_input) )
751 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
752 INIT_COUNTER( read_packets, INTEGER, COUNTER );
753 INIT_COUNTER( demux_read, INTEGER, COUNTER );
754 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
755 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
756 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
757 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
758 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
759 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
760 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
761 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
762 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
763 p_input->p->counters.p_sout_send_bitrate = NULL;
764 p_input->p->counters.p_sout_sent_packets = NULL;
765 p_input->p->counters.p_sout_sent_bytes = NULL;
766 if( p_input->p->counters.p_demux_bitrate )
767 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
768 if( p_input->p->counters.p_input_bitrate )
769 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
774 static int InitSout( input_thread_t * p_input )
778 if( p_input->b_preparsing ) return VLC_SUCCESS;
780 /* Find a usable sout and attach it to p_input */
781 psz = var_GetNonEmptyString( p_input, "sout" );
782 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
784 /* Check the validity of the provided sout */
785 if( p_input->p->p_sout )
787 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
789 msg_Dbg( p_input, "destroying unusable sout" );
791 sout_DeleteInstance( p_input->p->p_sout );
792 p_input->p->p_sout = NULL;
796 if( p_input->p->p_sout )
799 msg_Dbg( p_input, "sout keep: reusing sout" );
800 msg_Dbg( p_input, "sout keep: you probably want to use "
801 "gather stream_out" );
802 vlc_object_attach( p_input->p->p_sout, p_input );
806 /* Create a new one */
807 p_input->p->p_sout = sout_NewInstance( p_input, psz );
808 if( !p_input->p->p_sout )
810 input_ChangeState( p_input, ERROR_S );
811 msg_Err( p_input, "cannot start stream output instance, " \
817 if( libvlc_stats (p_input) )
819 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
820 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
821 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
822 if( p_input->p->counters.p_sout_send_bitrate )
823 p_input->p->counters.p_sout_send_bitrate->update_interval =
827 else if( p_input->p->p_sout )
829 msg_Dbg( p_input, "destroying useless sout" );
831 sout_DeleteInstance( p_input->p->p_sout );
832 p_input->p->p_sout = NULL;
840 static void InitTitle( input_thread_t * p_input )
844 if( p_input->b_preparsing ) return;
846 /* Create global title (from master) */
847 p_input->p->i_title = p_input->p->input.i_title;
848 p_input->p->title = p_input->p->input.title;
849 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
850 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
851 if( p_input->p->i_title > 0 )
853 /* Setup variables */
854 input_ControlVarNavigation( p_input );
855 input_ControlVarTitle( p_input, 0 );
859 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
860 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
861 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
864 if( p_input->i_pts_delay < 0 )
865 p_input->i_pts_delay = 0;
867 /* If the desynchronisation requested by the user is < 0, we need to
868 * cache more data. */
869 var_Get( p_input, "audio-desync", &val );
870 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
872 /* Update cr_average depending on the caching */
873 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
874 p_input->p->input.i_cr_average /= 10;
875 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
878 static void StartTitle( input_thread_t * p_input )
887 /* Start title/chapter */
889 if( p_input->b_preparsing )
891 p_input->p->i_start = 0;
895 val.i_int = p_input->p->input.i_title_start -
896 p_input->p->input.i_title_offset;
897 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
898 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
899 val.i_int = p_input->p->input.i_seekpoint_start -
900 p_input->p->input.i_seekpoint_offset;
901 if( val.i_int > 0 /* TODO: check upper boundary */ )
902 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
906 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
907 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
908 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
909 i_length = var_GetTime( p_input, "length" );
910 if( p_input->p->i_run < 0 )
912 msg_Warn( p_input, "invalid run-time ignored" );
913 p_input->p->i_run = 0;
916 if( p_input->p->i_start > 0 )
918 if( p_input->p->i_start >= i_length )
920 msg_Warn( p_input, "invalid start-time ignored" );
926 msg_Dbg( p_input, "starting at time: %ds",
927 (int)( p_input->p->i_start / INT64_C(1000000) ) );
929 s.i_time = p_input->p->i_start;
930 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
933 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
935 msg_Warn( p_input, "invalid stop-time ignored" );
936 p_input->p->i_stop = 0;
940 /* Get fps and set it if not already set */
941 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
944 float f_requested_fps;
946 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
947 var_SetFloat( p_input, "sub-original-fps", f_fps );
949 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
950 if( f_requested_fps != f_fps )
952 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
954 var_SetFloat( p_input, "sub-fps", f_requested_fps );
958 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
961 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
964 /* Look for and add subtitle files */
965 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
966 if( psz_subtitle != NULL )
968 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
969 input_AddSubtitles( p_input, psz_subtitle, false );
972 var_Get( p_input, "sub-autodetect-file", &val );
975 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
976 char **subs = subtitles_Detect( p_input, psz_autopath,
977 p_input->p->input.p_item->psz_uri );
980 if( psz_autopath == NULL )
981 psz_autopath = strdup("");
983 /* Try to autoselect the first autodetected subtitles file
984 * if no subtitles file was specified */
985 if( ( psz_subtitle == NULL ) && subs && subs[0] )
987 input_AddSubtitles( p_input, subs[0], false );
992 /* Then, just add the following subtitles files */
993 for( ; subs && subs[i]; i++ )
995 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
997 sub = InputSourceNew( p_input );
998 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1000 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1007 free( psz_autopath );
1009 free( psz_subtitle );
1011 /* Look for slave */
1012 psz = var_GetNonEmptyString( p_input, "input-slave" );
1016 input_source_t *slave;
1017 while( psz && *psz )
1019 while( *psz == ' ' || *psz == '#' )
1023 if( ( psz_delim = strchr( psz, '#' ) ) )
1025 *psz_delim++ = '\0';
1032 msg_Dbg( p_input, "adding slave input '%s'", psz );
1033 slave = InputSourceNew( p_input );
1034 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1036 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1045 static void InitPrograms( input_thread_t * p_input )
1050 if( p_input->b_preparsing ) return;
1053 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1054 i_es_out_mode = ES_OUT_MODE_AUTO;
1056 if( p_input->p->p_sout )
1058 var_Get( p_input, "sout-all", &val );
1061 i_es_out_mode = ES_OUT_MODE_ALL;
1066 var_Get( p_input, "programs", &val );
1067 if ( val.p_list && val.p_list->i_count )
1069 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1070 /* Note : we should remove the "program" callback. */
1073 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1077 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1079 /* Inform the demuxer about waited group (needed only for DVB) */
1080 if( i_es_out_mode == ES_OUT_MODE_ALL )
1082 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1084 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1086 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1091 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1092 (int) var_GetInteger( p_input, "program" ), NULL );
1096 static int Init( input_thread_t * p_input )
1102 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1104 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1106 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1107 var_SetString( p_input, "sout", "" );
1108 var_SetBool( p_input, "sout-all", false );
1109 var_SetString( p_input, "input-slave", "" );
1110 var_SetInteger( p_input, "input-repeat", 0 );
1111 var_SetString( p_input, "sub-file", "" );
1112 var_SetBool( p_input, "sub-autodetect-file", false );
1116 InitStatistics( p_input );
1118 ret = InitSout( p_input );
1119 if( ret != VLC_SUCCESS )
1120 return ret; /* FIXME: goto error; should be better here */
1124 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1125 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1126 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1128 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1129 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1131 if( InputSourceInit( p_input, &p_input->p->input,
1132 p_input->p->input.p_item->psz_uri, NULL ) )
1137 InitTitle( p_input );
1139 /* Load master infos */
1141 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1142 &val.i_time ) && val.i_time > 0 )
1144 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1145 UpdateItemLength( p_input, val.i_time );
1149 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1150 if( val.i_time > 0 )
1151 { /* fallback: gets length from metadata */
1152 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1153 UpdateItemLength( p_input, val.i_time );
1157 StartTitle( p_input );
1159 InitPrograms( p_input );
1161 if( !p_input->b_preparsing && p_input->p->p_sout )
1163 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1165 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1167 /* We don't want a high input priority here or we'll
1168 * end-up sucking up all the CPU time */
1169 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1172 msg_Dbg( p_input, "starting in %s mode",
1173 p_input->p->b_out_pace_control ? "async" : "sync" );
1176 p_meta = vlc_meta_New();
1178 /* Get meta data from users */
1179 InputMetaUser( p_input, p_meta );
1181 /* Get meta data from master input */
1182 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1184 /* Access_file does not give any meta, and there are no slave */
1185 AccessMeta( p_input, p_meta );
1187 InputUpdateMeta( p_input, p_meta );
1189 if( !p_input->b_preparsing )
1191 msg_Dbg( p_input, "`%s' successfully opened",
1192 p_input->p->input.p_item->psz_uri );
1196 /* initialization is complete */
1197 input_ChangeState( p_input, PLAYING_S );
1202 input_ChangeState( p_input, ERROR_S );
1204 if( p_input->p->p_es_out )
1205 input_EsOutDelete( p_input->p->p_es_out );
1207 if( p_input->p->p_sout )
1209 vlc_object_detach( p_input->p->p_sout );
1210 sout_DeleteInstance( p_input->p->p_sout );
1214 if( !p_input->b_preparsing && libvlc_stats (p_input) )
1216 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1217 stats_CounterClean( p_input->p->counters.p_##c );\
1218 p_input->p->counters.p_##c = NULL; } while(0)
1219 EXIT_COUNTER( read_bytes );
1220 EXIT_COUNTER( read_packets );
1221 EXIT_COUNTER( demux_read );
1222 EXIT_COUNTER( input_bitrate );
1223 EXIT_COUNTER( demux_bitrate );
1224 EXIT_COUNTER( played_abuffers );
1225 EXIT_COUNTER( lost_abuffers );
1226 EXIT_COUNTER( displayed_pictures );
1227 EXIT_COUNTER( lost_pictures );
1228 EXIT_COUNTER( decoded_audio );
1229 EXIT_COUNTER( decoded_video );
1230 EXIT_COUNTER( decoded_sub );
1232 if( p_input->p->p_sout )
1234 EXIT_COUNTER( sout_sent_packets );
1235 EXIT_COUNTER (sout_sent_bytes );
1236 EXIT_COUNTER( sout_send_bitrate );
1241 /* Mark them deleted */
1242 p_input->p->input.p_demux = NULL;
1243 p_input->p->input.p_stream = NULL;
1244 p_input->p->input.p_access = NULL;
1245 p_input->p->p_es_out = NULL;
1246 p_input->p->p_sout = NULL;
1248 return VLC_EGENERIC;
1251 /*****************************************************************************
1252 * Error: RunThread() error loop
1253 *****************************************************************************
1254 * This function is called when an error occurred during thread main's loop.
1255 *****************************************************************************/
1256 static void Error( input_thread_t *p_input )
1258 input_ChangeState( p_input, ERROR_S );
1259 while( !p_input->b_die )
1262 msleep( INPUT_IDLE_SLEEP );
1266 /*****************************************************************************
1267 * End: end the input thread
1268 *****************************************************************************/
1269 static void End( input_thread_t * p_input )
1273 /* We are at the end */
1274 input_ChangeState( p_input, END_S );
1276 /* Clean control variables */
1277 input_ControlVarClean( p_input );
1279 /* Clean up master */
1280 InputSourceClean( &p_input->p->input );
1283 for( i = 0; i < p_input->p->i_slave; i++ )
1285 InputSourceClean( p_input->p->slave[i] );
1286 free( p_input->p->slave[i] );
1288 free( p_input->p->slave );
1290 /* Unload all modules */
1291 if( p_input->p->p_es_out )
1292 input_EsOutDelete( p_input->p->p_es_out );
1294 if( !p_input->b_preparsing )
1296 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1297 if( libvlc_stats (p_input) )
1299 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
1301 /* make sure we are up to date */
1302 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1303 if( priv->p_stats_computer == p_input )
1305 stats_ComputeGlobalStats( p_input->p_libvlc,
1306 p_input->p_libvlc->p_stats );
1307 priv->p_stats_computer = NULL;
1309 CL_CO( read_bytes );
1310 CL_CO( read_packets );
1311 CL_CO( demux_read );
1312 CL_CO( input_bitrate );
1313 CL_CO( demux_bitrate );
1314 CL_CO( played_abuffers );
1315 CL_CO( lost_abuffers );
1316 CL_CO( displayed_pictures );
1317 CL_CO( lost_pictures );
1318 CL_CO( decoded_audio) ;
1319 CL_CO( decoded_video );
1320 CL_CO( decoded_sub) ;
1323 /* Close optional stream output instance */
1324 if( p_input->p->p_sout )
1326 CL_CO( sout_sent_packets );
1327 CL_CO( sout_sent_bytes );
1328 CL_CO( sout_send_bitrate );
1330 vlc_object_detach( p_input->p->p_sout );
1335 if( p_input->p->i_attachment > 0 )
1337 for( i = 0; i < p_input->p->i_attachment; i++ )
1338 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1339 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1342 /* Tell we're dead */
1343 p_input->b_dead = true;
1346 /*****************************************************************************
1348 *****************************************************************************/
1349 static inline int ControlPopNoLock( input_thread_t *p_input,
1350 int *pi_type, vlc_value_t *p_val )
1352 if( p_input->p->i_control <= 0 )
1354 return VLC_EGENERIC;
1357 *pi_type = p_input->p->control[0].i_type;
1358 *p_val = p_input->p->control[0].val;
1360 p_input->p->i_control--;
1361 if( p_input->p->i_control > 0 )
1365 for( i = 0; i < p_input->p->i_control; i++ )
1367 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1368 p_input->p->control[i].val = p_input->p->control[i+1].val;
1375 static void ControlReduce( input_thread_t *p_input )
1382 for( i = 1; i < p_input->p->i_control; i++ )
1384 const int i_lt = p_input->p->control[i-1].i_type;
1385 const int i_ct = p_input->p->control[i].i_type;
1387 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1388 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1392 ( i_ct == INPUT_CONTROL_SET_STATE ||
1393 i_ct == INPUT_CONTROL_SET_RATE ||
1394 i_ct == INPUT_CONTROL_SET_POSITION ||
1395 i_ct == INPUT_CONTROL_SET_TIME ||
1396 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1397 i_ct == INPUT_CONTROL_SET_TITLE ||
1398 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1399 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1402 // msg_Dbg( p_input, "merged at %d", i );
1403 /* Remove the i-1 */
1404 for( j = i; j < p_input->p->i_control; j++ )
1405 p_input->p->control[j-1] = p_input->p->control[j];
1406 p_input->p->i_control--;
1410 /* TODO but that's not that important
1411 - merge SET_X with SET_X_CMD
1412 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1413 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1420 static bool Control( input_thread_t *p_input, int i_type,
1423 bool b_force_update = false;
1425 if( !p_input ) return b_force_update;
1429 case INPUT_CONTROL_SET_DIE:
1430 msg_Dbg( p_input, "control: stopping input" );
1431 /* Mark all submodules to die */
1432 if( p_input->p->input.p_access )
1433 vlc_object_kill( p_input->p->input.p_access );
1434 if( p_input->p->input.p_stream )
1435 vlc_object_kill( p_input->p->input.p_stream );
1436 vlc_object_kill( p_input->p->input.p_demux );
1438 vlc_object_kill( p_input );
1441 case INPUT_CONTROL_SET_POSITION:
1442 case INPUT_CONTROL_SET_POSITION_OFFSET:
1445 if( i_type == INPUT_CONTROL_SET_POSITION )
1447 f_pos = val.f_float;
1451 /* Should not fail */
1452 demux_Control( p_input->p->input.p_demux,
1453 DEMUX_GET_POSITION, &f_pos );
1454 f_pos += val.f_float;
1456 if( f_pos < 0.0 ) f_pos = 0.0;
1457 if( f_pos > 1.0 ) f_pos = 1.0;
1458 /* Reset the decoders states and clock sync (before calling the demuxer */
1459 input_EsOutChangePosition( p_input->p->p_es_out );
1460 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1463 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1464 "%2.1f%% failed", f_pos * 100 );
1468 if( p_input->p->i_slave > 0 )
1469 SlaveSeek( p_input );
1471 b_force_update = true;
1476 case INPUT_CONTROL_SET_TIME:
1477 case INPUT_CONTROL_SET_TIME_OFFSET:
1482 if( i_type == INPUT_CONTROL_SET_TIME )
1484 i_time = val.i_time;
1488 /* Should not fail */
1489 demux_Control( p_input->p->input.p_demux,
1490 DEMUX_GET_TIME, &i_time );
1491 i_time += val.i_time;
1493 if( i_time < 0 ) i_time = 0;
1495 /* Reset the decoders states and clock sync (before calling the demuxer */
1496 input_EsOutChangePosition( p_input->p->p_es_out );
1498 i_ret = demux_Control( p_input->p->input.p_demux,
1499 DEMUX_SET_TIME, i_time );
1504 /* Emulate it with a SET_POS */
1505 demux_Control( p_input->p->input.p_demux,
1506 DEMUX_GET_LENGTH, &i_length );
1509 double f_pos = (double)i_time / (double)i_length;
1510 i_ret = demux_Control( p_input->p->input.p_demux,
1511 DEMUX_SET_POSITION, f_pos );
1516 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1517 " failed or not possible", i_time );
1521 if( p_input->p->i_slave > 0 )
1522 SlaveSeek( p_input );
1524 b_force_update = true;
1529 case INPUT_CONTROL_SET_STATE:
1530 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1531 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1534 if( p_input->p->input.p_access )
1535 i_ret = access_Control( p_input->p->input.p_access,
1536 ACCESS_SET_PAUSE_STATE, false );
1538 i_ret = demux_Control( p_input->p->input.p_demux,
1539 DEMUX_SET_PAUSE_STATE, false );
1543 /* FIXME What to do ? */
1544 msg_Warn( p_input, "cannot unset pause -> EOF" );
1545 vlc_mutex_unlock( &p_input->p->lock_control );
1546 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1547 vlc_mutex_lock( &p_input->p->lock_control );
1550 b_force_update = true;
1552 /* Switch to play */
1553 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1557 input_EsOutChangeState( p_input->p->p_es_out );
1559 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1560 p_input->p->b_can_pause )
1563 if( p_input->p->input.p_access )
1564 i_ret = access_Control( p_input->p->input.p_access,
1565 ACCESS_SET_PAUSE_STATE, true );
1567 i_ret = demux_Control( p_input->p->input.p_demux,
1568 DEMUX_SET_PAUSE_STATE, true );
1570 b_force_update = true;
1574 msg_Warn( p_input, "cannot set pause state" );
1575 state = p_input->i_state;
1582 /* Switch to new state */
1583 input_ChangeStateWithVarCallback( p_input, state, false );
1587 input_EsOutChangeState( p_input->p->p_es_out );
1589 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1591 b_force_update = true;
1593 /* Correct "state" value */
1594 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1596 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1598 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1602 case INPUT_CONTROL_SET_RATE:
1603 case INPUT_CONTROL_SET_RATE_SLOWER:
1604 case INPUT_CONTROL_SET_RATE_FASTER:
1608 if( i_type == INPUT_CONTROL_SET_RATE )
1614 static const int ppi_factor[][2] = {
1615 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1617 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1626 for( i = 0; ppi_factor[i][0] != 0; i++ )
1628 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1629 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1630 if( i_test_e < i_error )
1636 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1638 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1640 if( ppi_factor[i_idx+1][0] > 0 )
1641 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1643 i_rate = INPUT_RATE_MAX+1;
1647 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1649 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1651 i_rate = INPUT_RATE_MIN-1;
1655 if( i_rate < INPUT_RATE_MIN )
1657 msg_Dbg( p_input, "cannot set rate faster" );
1658 i_rate = INPUT_RATE_MIN;
1660 else if( i_rate > INPUT_RATE_MAX )
1662 msg_Dbg( p_input, "cannot set rate slower" );
1663 i_rate = INPUT_RATE_MAX;
1665 if( i_rate != INPUT_RATE_DEFAULT &&
1666 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1667 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1669 msg_Dbg( p_input, "cannot change rate" );
1670 i_rate = INPUT_RATE_DEFAULT;
1672 if( i_rate != p_input->p->i_rate &&
1673 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1676 if( p_input->p->input.p_access )
1677 i_ret = VLC_EGENERIC;
1679 i_ret = demux_Control( p_input->p->input.p_demux,
1680 DEMUX_SET_RATE, &i_rate );
1683 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1684 i_rate = p_input->p->i_rate;
1689 if( i_rate != p_input->p->i_rate )
1692 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1693 var_SetBool( p_input, "rate-change", true );
1695 p_input->p->i_rate = i_rate;
1697 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1698 if( p_input->p->input.b_rescale_ts )
1699 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1701 b_force_update = true;
1706 case INPUT_CONTROL_SET_PROGRAM:
1707 /* No need to force update, es_out does it if needed */
1708 es_out_Control( p_input->p->p_es_out,
1709 ES_OUT_SET_GROUP, val.i_int );
1711 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1715 case INPUT_CONTROL_SET_ES:
1716 /* No need to force update, es_out does it if needed */
1717 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1718 input_EsOutGetFromID( p_input->p->p_es_out,
1722 case INPUT_CONTROL_SET_AUDIO_DELAY:
1723 input_EsOutSetDelay( p_input->p->p_es_out,
1724 AUDIO_ES, val.i_time );
1725 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1728 case INPUT_CONTROL_SET_SPU_DELAY:
1729 input_EsOutSetDelay( p_input->p->p_es_out,
1730 SPU_ES, val.i_time );
1731 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1734 case INPUT_CONTROL_SET_TITLE:
1735 case INPUT_CONTROL_SET_TITLE_NEXT:
1736 case INPUT_CONTROL_SET_TITLE_PREV:
1737 if( p_input->p->input.b_title_demux &&
1738 p_input->p->input.i_title > 0 )
1741 /* FIXME handle demux title */
1742 demux_t *p_demux = p_input->p->input.p_demux;
1745 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1746 i_title = p_demux->info.i_title - 1;
1747 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1748 i_title = p_demux->info.i_title + 1;
1750 i_title = val.i_int;
1752 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1754 input_EsOutChangePosition( p_input->p->p_es_out );
1756 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1757 input_ControlVarTitle( p_input, i_title );
1760 else if( p_input->p->input.i_title > 0 )
1762 access_t *p_access = p_input->p->input.p_access;
1765 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1766 i_title = p_access->info.i_title - 1;
1767 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1768 i_title = p_access->info.i_title + 1;
1770 i_title = val.i_int;
1772 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1774 input_EsOutChangePosition( p_input->p->p_es_out );
1776 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1777 stream_AccessReset( p_input->p->input.p_stream );
1781 case INPUT_CONTROL_SET_SEEKPOINT:
1782 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1783 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1784 if( p_input->p->input.b_title_demux &&
1785 p_input->p->input.i_title > 0 )
1787 demux_t *p_demux = p_input->p->input.p_demux;
1789 int64_t i_input_time;
1790 int64_t i_seekpoint_time;
1792 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1794 i_seekpoint = p_demux->info.i_seekpoint;
1795 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1796 if( i_seekpoint_time >= 0 &&
1797 !demux_Control( p_demux,
1798 DEMUX_GET_TIME, &i_input_time ) )
1800 if ( i_input_time < i_seekpoint_time + 3000000 )
1806 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1807 i_seekpoint = p_demux->info.i_seekpoint + 1;
1809 i_seekpoint = val.i_int;
1811 if( i_seekpoint >= 0 && i_seekpoint <
1812 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1815 input_EsOutChangePosition( p_input->p->p_es_out );
1817 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1820 else if( p_input->p->input.i_title > 0 )
1822 demux_t *p_demux = p_input->p->input.p_demux;
1823 access_t *p_access = p_input->p->input.p_access;
1825 int64_t i_input_time;
1826 int64_t i_seekpoint_time;
1828 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1830 i_seekpoint = p_access->info.i_seekpoint;
1831 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1832 if( i_seekpoint_time >= 0 &&
1833 demux_Control( p_demux,
1834 DEMUX_GET_TIME, &i_input_time ) )
1836 if ( i_input_time < i_seekpoint_time + 3000000 )
1842 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1843 i_seekpoint = p_access->info.i_seekpoint + 1;
1845 i_seekpoint = val.i_int;
1847 if( i_seekpoint >= 0 && i_seekpoint <
1848 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1850 input_EsOutChangePosition( p_input->p->p_es_out );
1852 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1854 stream_AccessReset( p_input->p->input.p_stream );
1859 case INPUT_CONTROL_ADD_SLAVE:
1860 if( val.psz_string )
1862 input_source_t *slave = InputSourceNew( p_input );
1864 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1870 msg_Dbg( p_input, "adding %s as slave on the fly",
1874 if( demux_Control( p_input->p->input.p_demux,
1875 DEMUX_GET_TIME, &i_time ) )
1877 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1878 InputSourceClean( slave );
1882 if( demux_Control( slave->p_demux,
1883 DEMUX_SET_TIME, i_time ) )
1885 msg_Err( p_input, "seek failed for new slave" );
1886 InputSourceClean( slave );
1891 /* Get meta (access and demux) */
1892 p_meta = vlc_meta_New();
1893 access_Control( slave->p_access, ACCESS_GET_META,
1895 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1896 InputUpdateMeta( p_input, p_meta );
1898 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1903 msg_Warn( p_input, "failed to add %s as slave",
1907 free( val.psz_string );
1911 case INPUT_CONTROL_SET_BOOKMARK:
1913 msg_Err( p_input, "not yet implemented" );
1917 return b_force_update;
1920 /*****************************************************************************
1922 *****************************************************************************/
1923 static int UpdateFromDemux( input_thread_t *p_input )
1925 demux_t *p_demux = p_input->p->input.p_demux;
1928 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1930 v.i_int = p_demux->info.i_title;
1931 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1933 input_ControlVarTitle( p_input, p_demux->info.i_title );
1935 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1937 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1939 v.i_int = p_demux->info.i_seekpoint;
1940 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1942 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1944 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1946 /* Hmmm only works with master input */
1947 if( p_input->p->input.p_demux == p_demux )
1949 int i_title_end = p_input->p->input.i_title_end -
1950 p_input->p->input.i_title_offset;
1951 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1952 p_input->p->input.i_seekpoint_offset;
1954 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1956 if( p_demux->info.i_title > i_title_end ||
1957 ( p_demux->info.i_title == i_title_end &&
1958 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1960 else if( i_seekpoint_end >=0 )
1962 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1964 else if( i_title_end >= 0 )
1966 if( p_demux->info.i_title > i_title_end ) return 0;
1973 /*****************************************************************************
1975 *****************************************************************************/
1976 static int UpdateFromAccess( input_thread_t *p_input )
1978 access_t *p_access = p_input->p->input.p_access;
1981 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1983 v.i_int = p_access->info.i_title;
1984 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1986 input_ControlVarTitle( p_input, p_access->info.i_title );
1988 stream_AccessUpdate( p_input->p->input.p_stream );
1990 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1992 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1994 v.i_int = p_access->info.i_seekpoint;
1995 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1996 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1998 if( p_access->info.i_update & INPUT_UPDATE_META )
2000 /* TODO maybe multi - access ? */
2001 vlc_meta_t *p_meta = vlc_meta_New();
2002 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2003 InputUpdateMeta( p_input, p_meta );
2004 p_access->info.i_update &= ~INPUT_UPDATE_META;
2007 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2009 /* Hmmm only works with master input */
2010 if( p_input->p->input.p_access == p_access )
2012 int i_title_end = p_input->p->input.i_title_end -
2013 p_input->p->input.i_title_offset;
2014 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2015 p_input->p->input.i_seekpoint_offset;
2017 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2019 if( p_access->info.i_title > i_title_end ||
2020 ( p_access->info.i_title == i_title_end &&
2021 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2023 else if( i_seekpoint_end >=0 )
2025 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2027 else if( i_title_end >= 0 )
2029 if( p_access->info.i_title > i_title_end ) return 0;
2036 /*****************************************************************************
2038 *****************************************************************************/
2039 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2041 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2044 /*****************************************************************************
2046 *****************************************************************************/
2047 static input_source_t *InputSourceNew( input_thread_t *p_input )
2049 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2051 memset( in, 0, sizeof( input_source_t ) );
2055 /*****************************************************************************
2057 *****************************************************************************/
2058 static int InputSourceInit( input_thread_t *p_input,
2059 input_source_t *in, const char *psz_mrl,
2060 const char *psz_forced_demux )
2062 char psz_dup[strlen (psz_mrl) + 1];
2063 const char *psz_access;
2064 const char *psz_demux;
2071 strcpy( psz_dup, psz_mrl );
2073 if( !in ) return VLC_EGENERIC;
2074 if( !p_input ) return VLC_EGENERIC;
2077 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2079 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2080 psz_mrl, psz_access, psz_demux, psz_path );
2081 if( !p_input->b_preparsing )
2083 /* Hack to allow udp://@:port syntax */
2085 (strncmp( psz_access, "udp", 3 ) &&
2086 strncmp( psz_access, "rtp", 3 )) )
2088 /* Find optional titles and seekpoints */
2089 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2090 &in->i_seekpoint_start, &in->i_seekpoint_end );
2093 if( psz_forced_demux && *psz_forced_demux )
2095 psz_demux = psz_forced_demux;
2097 else if( *psz_demux == '\0' )
2099 /* special hack for forcing a demuxer with --demux=module
2100 * (and do nothing with a list) */
2101 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2103 if( psz_var_demux != NULL &&
2104 !strchr(psz_var_demux, ',' ) &&
2105 !strchr(psz_var_demux, ':' ) )
2107 psz_demux = psz_var_demux;
2109 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2113 /* Try access_demux if no demux given */
2114 if( *psz_demux == '\0' )
2116 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2117 NULL, p_input->p->p_es_out, false );
2122 /* Preparsing is only for file:// */
2125 if( !*psz_access ) /* path without scheme:// */
2126 psz_access = "file";
2127 if( strcmp( psz_access, "file" ) )
2129 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2134 int64_t i_pts_delay;
2136 /* Get infos from access_demux */
2137 demux_Control( in->p_demux,
2138 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2139 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2141 in->b_title_demux = true;
2142 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2143 &in->title, &in->i_title,
2144 &in->i_title_offset, &in->i_seekpoint_offset ) )
2149 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2150 &in->b_can_pace_control ) )
2151 in->b_can_pace_control = false;
2153 if( !in->b_can_pace_control )
2155 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2156 &in->b_can_rate_control, &in->b_rescale_ts ) )
2158 in->b_can_rate_control = false;
2159 in->b_rescale_ts = true; /* not used */
2164 in->b_can_rate_control = true;
2165 in->b_rescale_ts = true;
2167 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2168 &in->b_can_pause ) )
2169 in->b_can_pause = false;
2170 var_SetBool( p_input, "can-pause", in->b_can_pause );
2172 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2174 if( ret != VLC_SUCCESS )
2176 var_Set( p_input, "seekable", val );
2180 int64_t i_pts_delay;
2182 input_ChangeState( p_input, OPENING_S );
2184 /* Now try a real access */
2185 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2187 /* Access failed, URL encoded ? */
2188 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2190 decode_URI( psz_path );
2192 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2193 psz_access, psz_demux, psz_path );
2195 in->p_access = access_New( p_input,
2196 psz_access, psz_demux, psz_path );
2198 if( in->p_access == NULL )
2200 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2202 intf_UserFatal( VLC_OBJECT( p_input), false,
2203 _("Your input can't be opened"),
2204 _("VLC is unable to open the MRL '%s'."
2205 " Check the log for details."), psz_mrl );
2210 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2211 while( psz && *psz )
2213 access_t *p_access = in->p_access;
2214 char *end = strchr( psz, ':' );
2219 in->p_access = access_FilterNew( in->p_access, psz );
2220 if( in->p_access == NULL )
2222 in->p_access = p_access;
2223 msg_Warn( p_input, "failed to insert access filter %s",
2231 /* Get infos from access */
2232 if( !p_input->b_preparsing )
2234 access_Control( in->p_access,
2235 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2236 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2238 in->b_title_demux = false;
2239 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2240 &in->title, &in->i_title,
2241 &in->i_title_offset, &in->i_seekpoint_offset ) )
2247 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2248 &in->b_can_pace_control );
2249 in->b_can_rate_control = in->b_can_pace_control;
2250 in->b_rescale_ts = true;
2252 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2254 var_SetBool( p_input, "can-pause", in->b_can_pause );
2255 access_Control( in->p_access, ACCESS_CAN_SEEK,
2257 var_Set( p_input, "seekable", val );
2260 input_ChangeState( p_input, BUFFERING_S );
2262 /* Create the stream_t */
2263 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2264 if( in->p_stream == NULL )
2266 msg_Warn( p_input, "cannot create a stream_t from access" );
2270 /* Open a demuxer */
2271 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2273 psz_demux = in->p_access->psz_demux;
2277 /* Take access redirections into account */
2278 char *psz_real_path;
2279 char *psz_buf = NULL;
2280 if( in->p_access->psz_path )
2282 const char *psz_a, *psz_d;
2283 psz_buf = strdup( in->p_access->psz_path );
2284 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2288 psz_real_path = psz_path;
2290 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2292 in->p_stream, p_input->p->p_es_out,
2293 p_input->b_preparsing );
2297 if( in->p_demux == NULL )
2299 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2300 psz_access, psz_demux, psz_path );
2301 intf_UserFatal( VLC_OBJECT( p_input ), false,
2302 _("VLC can't recognize the input's format"),
2303 _("The format of '%s' cannot be detected. "
2304 "Have a look the log for details."), psz_mrl );
2308 /* Get title from demux */
2309 if( !p_input->b_preparsing && in->i_title <= 0 )
2311 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2312 &in->title, &in->i_title,
2313 &in->i_title_offset, &in->i_seekpoint_offset ))
2315 TAB_INIT( in->i_title, in->title );
2319 in->b_title_demux = true;
2325 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2326 if( 1 || !p_input->b_preparsing )
2329 input_attachment_t **attachment;
2330 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2331 &attachment, &i_attachment ) )
2333 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2334 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2335 i_attachment, attachment );
2336 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2339 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2341 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2343 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2346 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2347 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2352 input_ChangeState( p_input, ERROR_S );
2355 demux_Delete( in->p_demux );
2358 stream_Delete( in->p_stream );
2361 access_Delete( in->p_access );
2363 return VLC_EGENERIC;
2366 /*****************************************************************************
2368 *****************************************************************************/
2369 static void InputSourceClean( input_source_t *in )
2374 demux_Delete( in->p_demux );
2377 stream_Delete( in->p_stream );
2380 access_Delete( in->p_access );
2382 if( in->i_title > 0 )
2384 for( i = 0; i < in->i_title; i++ )
2385 vlc_input_title_Delete( in->title[i] );
2386 TAB_CLEAN( in->i_title, in->title );
2390 static void SlaveDemux( input_thread_t *p_input )
2395 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2397 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2401 for( i = 0; i < p_input->p->i_slave; i++ )
2403 input_source_t *in = p_input->p->slave[i];
2409 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2414 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2416 msg_Err( p_input, "slave[%d] doesn't like "
2417 "DEMUX_GET_TIME -> EOF", i );
2422 if( i_stime >= i_time )
2425 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2431 i_ret = in->p_demux->pf_demux( in->p_demux );
2436 msg_Dbg( p_input, "slave %d EOF", i );
2442 static void SlaveSeek( input_thread_t *p_input )
2447 if( !p_input ) return;
2449 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2451 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2455 for( i = 0; i < p_input->p->i_slave; i++ )
2457 input_source_t *in = p_input->p->slave[i];
2459 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2461 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2467 /*****************************************************************************
2469 *****************************************************************************/
2470 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2474 if( !p_meta ) return;
2476 /* Get meta information from user */
2477 #define GET_META( field, s ) \
2478 var_Get( p_input, (s), &val ); \
2479 if( *val.psz_string ) \
2480 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2481 free( val.psz_string )
2483 GET_META( Title, "meta-title" );
2484 GET_META( Artist, "meta-artist" );
2485 GET_META( Genre, "meta-genre" );
2486 GET_META( Copyright, "meta-copyright" );
2487 GET_META( Description, "meta-description" );
2488 GET_META( Date, "meta-date" );
2489 GET_META( URL, "meta-url" );
2493 /*****************************************************************************
2494 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2495 * arturl and locking issue.
2496 *****************************************************************************/
2497 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2499 input_item_t *p_item = p_input->p->input.p_item;
2500 char * psz_arturl = NULL;
2501 char *psz_title = NULL;
2502 int i_arturl_event = false;
2507 psz_arturl = input_item_GetArtURL( p_item );
2509 vlc_mutex_lock( &p_item->lock );
2510 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2511 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2513 vlc_meta_Merge( p_item->p_meta, p_meta );
2515 if( psz_arturl && *psz_arturl )
2517 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2518 i_arturl_event = true;
2521 vlc_meta_Delete( p_meta );
2523 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2525 /* Don't look for art cover if sout
2526 * XXX It can change when sout has meta data support */
2527 if( p_input->p->p_sout && !p_input->b_preparsing )
2529 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2530 i_arturl_event = true;
2534 input_ExtractAttachmentAndCacheArt( p_input );
2540 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2542 p_meta = vlc_meta_New();
2543 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2545 vlc_mutex_unlock( &p_item->lock );
2547 input_item_SetPreparsed( p_item, true );
2549 if( i_arturl_event == true )
2553 /* Notify interested third parties */
2554 event.type = vlc_InputItemMetaChanged;
2555 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2556 vlc_event_send( &p_item->event_manager, &event );
2561 input_Control( p_input, INPUT_SET_NAME, psz_title );
2565 /** \todo handle sout meta */
2569 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2570 int i_new, input_attachment_t **pp_new )
2572 int i_attachment = *pi_attachment;
2573 input_attachment_t **attachment = *ppp_attachment;
2576 attachment = realloc( attachment,
2577 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2578 for( i = 0; i < i_new; i++ )
2579 attachment[i_attachment++] = pp_new[i];
2583 *pi_attachment = i_attachment;
2584 *ppp_attachment = attachment;
2587 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2591 if( p_input->b_preparsing )
2594 if( p_input->p->input.p_access )
2595 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2598 /* Get meta data from slave input */
2599 for( i = 0; i < p_input->p->i_slave; i++ )
2601 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2602 if( p_input->p->slave[i]->p_access )
2604 access_Control( p_input->p->slave[i]->p_access,
2605 ACCESS_GET_META, p_meta );
2610 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2617 /* XXX I am not sure it is a great idea, besides, there is more than that
2618 * if we want to do it right */
2619 vlc_mutex_lock( &p_item->lock );
2620 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2622 vlc_mutex_unlock( &p_item->lock );
2625 vlc_mutex_unlock( &p_item->lock );
2628 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2629 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2634 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2635 if(! p_demux->p_private )
2638 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2641 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2643 if( p_demux_meta->p_meta )
2645 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2646 vlc_meta_Delete( p_demux_meta->p_meta );
2649 if( p_demux_meta->i_attachments > 0 )
2651 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2652 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2653 p_demux_meta->i_attachments, p_demux_meta->attachments );
2654 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2656 module_Unneed( p_demux, p_id3 );
2658 free( p_demux->p_private );
2662 /*****************************************************************************
2663 * MRLSplit: parse the access, demux and url part of the
2664 * Media Resource Locator.
2665 *****************************************************************************/
2666 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2669 char *psz_access = NULL;
2670 char *psz_demux = NULL;
2673 /* Either there is an access/demux specification before ://
2674 * or we have a plain local file path. */
2675 psz_path = strstr( psz_dup, "://" );
2676 if( psz_path != NULL )
2679 psz_path += 3; /* skips "://" */
2681 /* Separate access from demux (<access>/<demux>://<path>) */
2682 psz_access = psz_dup;
2683 psz_demux = strchr( psz_access, '/' );
2685 *psz_demux++ = '\0';
2687 /* We really don't want module name substitution here! */
2688 if( psz_access[0] == '$' )
2690 if( psz_demux && psz_demux[0] == '$' )
2698 *ppsz_access = psz_access ? psz_access : "";
2699 *ppsz_demux = psz_demux ? psz_demux : "";
2700 *ppsz_path = psz_path ? psz_path : "";
2703 /*****************************************************************************
2704 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2707 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2708 *****************************************************************************/
2709 static void MRLSections( input_thread_t *p_input, char *psz_source,
2710 int *pi_title_start, int *pi_title_end,
2711 int *pi_chapter_start, int *pi_chapter_end )
2713 char *psz, *psz_end, *psz_next, *psz_check;
2715 *pi_title_start = *pi_title_end = -1;
2716 *pi_chapter_start = *pi_chapter_end = -1;
2718 /* Start by parsing titles and chapters */
2719 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2721 /* Check we are really dealing with a title/chapter section */
2722 psz_check = psz + 1;
2723 if( !*psz_check ) return;
2724 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2725 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2726 if( *psz_check == ':' && ++psz_check )
2727 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2728 if( *psz_check != '-' && *psz_check ) return;
2729 if( *psz_check == '-' && ++psz_check )
2730 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2731 if( *psz_check != ':' && *psz_check ) return;
2732 if( *psz_check == ':' && ++psz_check )
2733 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2734 if( *psz_check ) return;
2736 /* Separate start and end */
2738 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2740 /* Look for the start title */
2741 *pi_title_start = strtol( psz, &psz_next, 0 );
2742 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2743 *pi_title_end = *pi_title_start;
2746 /* Look for the start chapter */
2748 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2749 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2750 *pi_chapter_end = *pi_chapter_start;
2754 /* Look for the end title */
2755 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2756 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2759 /* Look for the end chapter */
2760 if( *psz_end ) psz_end++;
2761 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2762 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2765 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2766 psz_source, *pi_title_start, *pi_chapter_start,
2767 *pi_title_end, *pi_chapter_end );
2770 /*****************************************************************************
2771 * input_AddSubtitles: add a subtitles file and enable it
2772 *****************************************************************************/
2773 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2774 bool b_check_extension )
2776 input_source_t *sub;
2779 char *psz_path, *psz_extension;
2781 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2786 /* if we are provided a subtitle.sub file,
2787 * see if we don't have a subtitle.idx and use it instead */
2788 psz_path = strdup( psz_subtitle );
2791 psz_extension = strrchr( psz_path, '.');
2792 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2796 strcpy( psz_extension, ".idx" );
2797 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2798 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2801 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2802 psz_path, psz_subtitle );
2803 strcpy( psz_subtitle, psz_path );
2809 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2811 sub = InputSourceNew( p_input );
2812 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2814 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2817 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2819 if( count.i_int == 0 )
2821 /* if it was first one, there is disable too */
2823 if( count.i_int < list.p_list->i_count )
2825 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2826 &list.p_list->p_values[count.i_int] );
2828 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2836 /*****************************************************************************
2837 * input_get_event_manager
2838 *****************************************************************************/
2839 vlc_event_manager_t *
2840 input_get_event_manager( input_thread_t *p_input )
2842 return &p_input->p->event_manager;