1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
38 #include "input_internal.h"
41 #include "../stream_output/stream_output.h"
43 #include <vlc_interface.h>
45 #include <vlc_charset.h>
47 #ifdef HAVE_SYS_STAT_H
48 # include <sys/stat.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 static void Destructor( input_thread_t * p_input );
56 static int Run ( input_thread_t *p_input );
57 static int RunAndDestroy ( input_thread_t *p_input );
59 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
60 const char *, bool, sout_instance_t * );
61 static int Init ( input_thread_t *p_input );
62 static void Error ( input_thread_t *p_input );
63 static void End ( input_thread_t *p_input );
64 static void MainLoop( input_thread_t *p_input );
66 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
67 static void ControlReduce( input_thread_t * );
68 static bool Control( input_thread_t *, int, vlc_value_t );
70 static int UpdateFromAccess( input_thread_t * );
71 static int UpdateFromDemux( input_thread_t * );
73 static void UpdateItemLength( input_thread_t *, int64_t i_length );
75 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
77 static input_source_t *InputSourceNew( input_thread_t *);
78 static int InputSourceInit( input_thread_t *, input_source_t *,
79 const char *, const char *psz_forced_demux );
80 static void InputSourceClean( input_source_t * );
82 //static void InputGetAttachments( input_thread_t *, input_source_t * );
83 static void SlaveDemux( input_thread_t *p_input );
84 static void SlaveSeek( input_thread_t *p_input );
86 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
87 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
89 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
90 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
91 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
92 int i_new, input_attachment_t **pp_new );
94 /*****************************************************************************
95 * This function creates a new input, and returns a pointer
96 * to its description. On error, it returns NULL.
98 * Variables for _public_ use:
101 * - rate,rate-slower, rate-faster
102 * - position, position-offset
103 * - time, time-offset
104 * - title,title-next,title-prev
105 * - chapter,chapter-next, chapter-prev
106 * - program, audio-es, video-es, spu-es
107 * - audio-delay, spu-delay
112 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
113 * or not, for that check position != 0.0)
115 * * For intf callback upon changes
117 * - rate-change for when playback rate changes
118 * TODO explain when Callback is called
119 * TODO complete this list (?)
120 *****************************************************************************/
121 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
122 const char *psz_header, bool b_quick,
123 sout_instance_t *p_sout )
125 static const char input_name[] = "input";
126 input_thread_t *p_input = NULL; /* thread descriptor */
130 /* Allocate descriptor */
131 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
132 VLC_OBJECT_INPUT, input_name );
133 if( p_input == NULL )
136 /* Construct a nice name for the input timer */
137 char psz_timer_name[255];
138 char * psz_name = input_item_GetName( p_item );
139 snprintf( psz_timer_name, sizeof(psz_timer_name),
140 "input launching for '%s'", psz_name );
142 msg_Dbg( p_input, "Creating an input 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;
318 char * psz_name = input_item_GetName( p_input->p->input.p_item );
319 msg_Dbg( p_input, "Destroying the input for '%s'", psz_name);
323 vlc_event_manager_fini( &p_input->p->event_manager );
325 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
326 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
329 sout_DeleteInstance( priv->p_sout );
331 vlc_gc_decref( p_input->p->input.p_item );
333 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
335 vlc_mutex_destroy( &priv->lock_control );
340 * Initialize an input thread and run it. You will need to monitor the
341 * thread to clean up after it is done
343 * \param p_parent a vlc_object
344 * \param p_item an input item
345 * \return a pointer to the spawned input thread
347 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
348 input_item_t *p_item )
350 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
354 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
355 input_item_t *p_item,
356 const char *psz_log, sout_instance_t *p_sout )
358 input_thread_t *p_input;
360 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
364 /* Create thread and wait for its readiness. */
365 if( vlc_thread_create( p_input, "input", Run,
366 VLC_THREAD_PRIORITY_INPUT, true ) )
368 input_ChangeState( p_input, ERROR_S );
369 msg_Err( p_input, "cannot create input thread" );
370 vlc_object_detach( p_input );
371 vlc_object_release( p_input );
379 * Initialize an input thread and run it. This thread will clean after itself,
380 * you can forget about it. It can work either in blocking or non-blocking mode
382 * \param p_parent a vlc_object
383 * \param p_item an input item
384 * \param b_block should we block until read is finished ?
385 * \return the input object id if non blocking, an error code else
387 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
390 input_thread_t *p_input;
392 p_input = Create( p_parent, p_item, NULL, false, NULL );
398 RunAndDestroy( p_input );
403 if( vlc_thread_create( p_input, "input", RunAndDestroy,
404 VLC_THREAD_PRIORITY_INPUT, true ) )
406 input_ChangeState( p_input, ERROR_S );
407 msg_Err( p_input, "cannot create input thread" );
408 vlc_object_release( p_input );
412 return p_input->i_object_id;
416 * Initialize an input and initialize it to preparse the item
417 * This function is blocking. It will only accept to parse files
419 * \param p_parent a vlc_object_t
420 * \param p_item an input item
421 * \return VLC_SUCCESS or an error
423 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
425 input_thread_t *p_input;
427 /* Allocate descriptor */
428 p_input = Create( p_parent, p_item, NULL, true, NULL );
432 if( !Init( p_input ) )
435 vlc_object_detach( p_input );
436 vlc_object_release( p_input );
442 * Request a running input thread to stop and die
444 * \param the input thread to stop
446 static void ObjectKillChildrens( vlc_object_t *p_obj )
450 vlc_object_kill( p_obj );
452 p_list = vlc_list_children( p_obj );
453 for( i = 0; i < p_list->i_count; i++ )
454 ObjectKillChildrens( p_list->p_values[i].p_object );
455 vlc_list_release( p_list );
457 void input_StopThread( input_thread_t *p_input )
459 /* Set die for input and ALL of this childrens (even (grand-)grand-childrens)
460 * It is needed here even if it is done in INPUT_CONTROL_SET_DIE handler to
461 * unlock the control loop */
462 ObjectKillChildrens( VLC_OBJECT(p_input) );
464 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
467 sout_instance_t * input_DetachSout( input_thread_t *p_input )
469 sout_instance_t *p_sout = p_input->p->p_sout;
470 vlc_object_detach( p_sout );
471 p_input->p->p_sout = NULL;
475 /*****************************************************************************
476 * Run: main thread loop
477 * This is the "normal" thread that spawns the input processing chain,
478 * reads the stream, cleans up and waits
479 *****************************************************************************/
480 static int Run( input_thread_t *p_input )
482 /* Signal that the thread is launched */
483 vlc_thread_ready( p_input );
485 if( Init( p_input ) )
487 /* If we failed, wait before we are killed, and exit */
488 p_input->b_error = true;
492 /* Tell we're dead */
493 p_input->b_dead = true;
500 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
502 /* We have finish to demux data but not to play them */
503 while( !p_input->b_die )
505 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
508 msg_Dbg( p_input, "waiting decoder fifos to empty" );
510 msleep( INPUT_IDLE_SLEEP );
513 /* We have finished */
514 p_input->b_eof = true;
515 input_ChangeState( p_input, END_S );
518 /* Wait until we are asked to die */
519 if( !p_input->b_die )
530 /*****************************************************************************
531 * RunAndDestroy: main thread loop
532 * This is the "just forget me" thread that spawns the input processing chain,
533 * reads the stream, cleans up and releases memory
534 *****************************************************************************/
535 static int RunAndDestroy( input_thread_t *p_input )
537 /* Signal that the thread is launched */
538 vlc_thread_ready( p_input );
540 if( Init( p_input ) )
545 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
547 /* We have finished demuxing data but not playing it */
548 while( !p_input->b_die )
550 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
553 msg_Dbg( p_input, "waiting decoder fifos to empty" );
555 msleep( INPUT_IDLE_SLEEP );
558 /* We have finished */
559 p_input->b_eof = true;
567 vlc_object_release( p_input );
571 /*****************************************************************************
572 * Main loop: Fill buffers from access, and demux
573 *****************************************************************************/
574 static void MainLoop( input_thread_t *p_input )
576 int64_t i_start_mdate = mdate();
577 int64_t i_intf_update = 0;
581 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
583 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
585 bool b_force_update = false;
591 if( p_input->i_state != PAUSE_S )
593 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
594 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
597 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
602 if( p_input->p->input.b_title_demux &&
603 p_input->p->input.p_demux->info.i_update )
605 i_ret = UpdateFromDemux( p_input );
606 b_force_update = true;
608 else if( !p_input->p->input.b_title_demux &&
609 p_input->p->input.p_access &&
610 p_input->p->input.p_access->info.i_update )
612 i_ret = UpdateFromAccess( p_input );
613 b_force_update = true;
617 if( i_ret == 0 ) /* EOF */
621 var_Get( p_input, "input-repeat", &repeat );
622 if( repeat.i_int == 0 )
624 /* End of file - we do not set b_die because only the
625 * playlist is allowed to do so. */
626 msg_Dbg( p_input, "EOF reached" );
627 p_input->p->input.b_eof = true;
631 msg_Dbg( p_input, "repeating the same input (%d)",
633 if( repeat.i_int > 0 )
636 var_Set( p_input, "input-repeat", repeat );
639 /* Seek to start title/seekpoint */
640 val.i_int = p_input->p->input.i_title_start -
641 p_input->p->input.i_title_offset;
642 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
644 input_ControlPush( p_input,
645 INPUT_CONTROL_SET_TITLE, &val );
647 val.i_int = p_input->p->input.i_seekpoint_start -
648 p_input->p->input.i_seekpoint_offset;
649 if( val.i_int > 0 /* TODO: check upper boundary */ )
650 input_ControlPush( p_input,
651 INPUT_CONTROL_SET_SEEKPOINT, &val );
653 /* Seek to start position */
654 if( p_input->p->i_start > 0 )
656 val.i_time = p_input->p->i_start;
657 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
663 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
668 i_start_mdate = mdate();
673 p_input->b_error = true;
676 if( i_ret > 0 && p_input->p->i_slave > 0 )
678 SlaveDemux( p_input );
688 vlc_mutex_lock( &p_input->p->lock_control );
689 ControlReduce( p_input );
690 while( !ControlPopNoLock( p_input, &i_type, &val ) )
692 msg_Dbg( p_input, "control type=%d", i_type );
693 if( Control( p_input, i_type, val ) )
694 b_force_update = true;
696 vlc_mutex_unlock( &p_input->p->lock_control );
698 if( b_force_update || i_intf_update < mdate() )
702 int64_t i_time, i_length;
703 /* update input status variables */
704 if( !demux_Control( p_input->p->input.p_demux,
705 DEMUX_GET_POSITION, &f_pos ) )
707 val.f_float = (float)f_pos;
708 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
710 if( !demux_Control( p_input->p->input.p_demux,
711 DEMUX_GET_TIME, &i_time ) )
713 p_input->i_time = i_time;
715 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
717 if( !demux_Control( p_input->p->input.p_demux,
718 DEMUX_GET_LENGTH, &i_length ) )
721 var_Get( p_input, "length", &old_val );
722 val.i_time = i_length;
723 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
725 if( old_val.i_time != val.i_time )
727 UpdateItemLength( p_input, i_length );
731 var_SetBool( p_input, "intf-change", true );
732 i_intf_update = mdate() + INT64_C(150000);
734 /* 150ms * 8 = ~ 1 second */
735 if( ++i_updates % 8 == 0 )
737 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
738 /* Are we the thread responsible for computing global stats ? */
739 if( libvlc_priv (p_input->p_libvlc)->p_stats_computer == p_input )
741 stats_ComputeGlobalStats( p_input->p_libvlc,
742 p_input->p_libvlc->p_stats );
748 static void InitStatistics( input_thread_t * p_input )
750 if( p_input->b_preparsing ) return;
752 /* Prepare statistics */
753 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
754 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
755 if( libvlc_stats (p_input) )
757 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
758 INIT_COUNTER( read_packets, INTEGER, COUNTER );
759 INIT_COUNTER( demux_read, INTEGER, COUNTER );
760 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
761 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
762 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
763 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
764 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
765 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
766 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
767 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
768 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
769 p_input->p->counters.p_sout_send_bitrate = NULL;
770 p_input->p->counters.p_sout_sent_packets = NULL;
771 p_input->p->counters.p_sout_sent_bytes = NULL;
772 if( p_input->p->counters.p_demux_bitrate )
773 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
774 if( p_input->p->counters.p_input_bitrate )
775 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
780 static int InitSout( input_thread_t * p_input )
784 if( p_input->b_preparsing ) return VLC_SUCCESS;
786 /* Find a usable sout and attach it to p_input */
787 psz = var_GetNonEmptyString( p_input, "sout" );
788 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
790 /* Check the validity of the provided sout */
791 if( p_input->p->p_sout )
793 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
795 msg_Dbg( p_input, "destroying unusable sout" );
797 sout_DeleteInstance( p_input->p->p_sout );
798 p_input->p->p_sout = NULL;
802 if( p_input->p->p_sout )
805 msg_Dbg( p_input, "sout keep: reusing sout" );
806 msg_Dbg( p_input, "sout keep: you probably want to use "
807 "gather stream_out" );
808 vlc_object_attach( p_input->p->p_sout, p_input );
812 /* Create a new one */
813 p_input->p->p_sout = sout_NewInstance( p_input, psz );
814 if( !p_input->p->p_sout )
816 input_ChangeState( p_input, ERROR_S );
817 msg_Err( p_input, "cannot start stream output instance, " \
823 if( libvlc_stats (p_input) )
825 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
826 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
827 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
828 if( p_input->p->counters.p_sout_send_bitrate )
829 p_input->p->counters.p_sout_send_bitrate->update_interval =
833 else if( p_input->p->p_sout )
835 msg_Dbg( p_input, "destroying useless sout" );
837 sout_DeleteInstance( p_input->p->p_sout );
838 p_input->p->p_sout = NULL;
846 static void InitTitle( input_thread_t * p_input )
850 if( p_input->b_preparsing ) return;
852 /* Create global title (from master) */
853 p_input->p->i_title = p_input->p->input.i_title;
854 p_input->p->title = p_input->p->input.title;
855 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
856 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
857 if( p_input->p->i_title > 0 )
859 /* Setup variables */
860 input_ControlVarNavigation( p_input );
861 input_ControlVarTitle( p_input, 0 );
865 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
866 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
867 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
870 if( p_input->i_pts_delay < 0 )
871 p_input->i_pts_delay = 0;
873 /* If the desynchronisation requested by the user is < 0, we need to
874 * cache more data. */
875 var_Get( p_input, "audio-desync", &val );
876 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
878 /* Update cr_average depending on the caching */
879 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
880 p_input->p->input.i_cr_average /= 10;
881 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
884 static void StartTitle( input_thread_t * p_input )
893 /* Start title/chapter */
895 if( p_input->b_preparsing )
897 p_input->p->i_start = 0;
901 val.i_int = p_input->p->input.i_title_start -
902 p_input->p->input.i_title_offset;
903 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
904 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
905 val.i_int = p_input->p->input.i_seekpoint_start -
906 p_input->p->input.i_seekpoint_offset;
907 if( val.i_int > 0 /* TODO: check upper boundary */ )
908 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
912 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
913 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
914 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
915 i_length = var_GetTime( p_input, "length" );
916 if( p_input->p->i_run < 0 )
918 msg_Warn( p_input, "invalid run-time ignored" );
919 p_input->p->i_run = 0;
922 if( p_input->p->i_start > 0 )
924 if( p_input->p->i_start >= i_length )
926 msg_Warn( p_input, "invalid start-time ignored" );
932 msg_Dbg( p_input, "starting at time: %ds",
933 (int)( p_input->p->i_start / INT64_C(1000000) ) );
935 s.i_time = p_input->p->i_start;
936 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
939 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
941 msg_Warn( p_input, "invalid stop-time ignored" );
942 p_input->p->i_stop = 0;
946 /* Get fps and set it if not already set */
947 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
950 float f_requested_fps;
952 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
953 var_SetFloat( p_input, "sub-original-fps", f_fps );
955 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
956 if( f_requested_fps != f_fps )
958 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
960 var_SetFloat( p_input, "sub-fps", f_requested_fps );
964 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
967 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
970 /* Look for and add subtitle files */
971 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
972 if( psz_subtitle != NULL )
974 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
975 input_AddSubtitles( p_input, psz_subtitle, false );
978 var_Get( p_input, "sub-autodetect-file", &val );
981 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
982 char **subs = subtitles_Detect( p_input, psz_autopath,
983 p_input->p->input.p_item->psz_uri );
986 if( psz_autopath == NULL )
987 psz_autopath = strdup("");
989 /* Try to autoselect the first autodetected subtitles file
990 * if no subtitles file was specified */
991 if( ( psz_subtitle == NULL ) && subs && subs[0] )
993 input_AddSubtitles( p_input, subs[0], false );
998 /* Then, just add the following subtitles files */
999 for( ; subs && subs[i]; i++ )
1001 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1003 sub = InputSourceNew( p_input );
1004 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1006 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1013 free( psz_autopath );
1015 free( psz_subtitle );
1017 /* Look for slave */
1018 psz = var_GetNonEmptyString( p_input, "input-slave" );
1022 input_source_t *slave;
1023 while( psz && *psz )
1025 while( *psz == ' ' || *psz == '#' )
1029 if( ( psz_delim = strchr( psz, '#' ) ) )
1031 *psz_delim++ = '\0';
1038 msg_Dbg( p_input, "adding slave input '%s'", psz );
1039 slave = InputSourceNew( p_input );
1040 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1042 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1051 static void InitPrograms( input_thread_t * p_input )
1056 if( p_input->b_preparsing ) return;
1059 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1060 i_es_out_mode = ES_OUT_MODE_AUTO;
1062 if( p_input->p->p_sout )
1064 var_Get( p_input, "sout-all", &val );
1067 i_es_out_mode = ES_OUT_MODE_ALL;
1072 var_Get( p_input, "programs", &val );
1073 if ( val.p_list && val.p_list->i_count )
1075 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1076 /* Note : we should remove the "program" callback. */
1079 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1083 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1085 /* Inform the demuxer about waited group (needed only for DVB) */
1086 if( i_es_out_mode == ES_OUT_MODE_ALL )
1088 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1090 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1092 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1097 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1098 (int) var_GetInteger( p_input, "program" ), NULL );
1102 static int Init( input_thread_t * p_input )
1108 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1110 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1112 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1113 var_SetString( p_input, "sout", "" );
1114 var_SetBool( p_input, "sout-all", false );
1115 var_SetString( p_input, "input-slave", "" );
1116 var_SetInteger( p_input, "input-repeat", 0 );
1117 var_SetString( p_input, "sub-file", "" );
1118 var_SetBool( p_input, "sub-autodetect-file", false );
1122 InitStatistics( p_input );
1124 ret = InitSout( p_input );
1125 if( ret != VLC_SUCCESS )
1126 return ret; /* FIXME: goto error; should be better here */
1130 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1131 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1132 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1134 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1135 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1137 if( InputSourceInit( p_input, &p_input->p->input,
1138 p_input->p->input.p_item->psz_uri, NULL ) )
1143 InitTitle( p_input );
1145 /* Load master infos */
1147 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1148 &val.i_time ) && val.i_time > 0 )
1150 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1151 UpdateItemLength( p_input, val.i_time );
1155 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1156 if( val.i_time > 0 )
1157 { /* fallback: gets length from metadata */
1158 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1159 UpdateItemLength( p_input, val.i_time );
1163 StartTitle( p_input );
1165 InitPrograms( p_input );
1167 if( !p_input->b_preparsing && p_input->p->p_sout )
1169 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1171 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1173 /* We don't want a high input priority here or we'll
1174 * end-up sucking up all the CPU time */
1175 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1178 msg_Dbg( p_input, "starting in %s mode",
1179 p_input->p->b_out_pace_control ? "async" : "sync" );
1182 p_meta = vlc_meta_New();
1184 /* Get meta data from users */
1185 InputMetaUser( p_input, p_meta );
1187 /* Get meta data from master input */
1188 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1190 /* Access_file does not give any meta, and there are no slave */
1191 AccessMeta( p_input, p_meta );
1193 InputUpdateMeta( p_input, p_meta );
1195 if( !p_input->b_preparsing )
1197 msg_Dbg( p_input, "`%s' successfully opened",
1198 p_input->p->input.p_item->psz_uri );
1202 /* initialization is complete */
1203 input_ChangeState( p_input, PLAYING_S );
1208 input_ChangeState( p_input, ERROR_S );
1210 if( p_input->p->p_es_out )
1211 input_EsOutDelete( p_input->p->p_es_out );
1213 if( p_input->p->p_sout )
1215 vlc_object_detach( p_input->p->p_sout );
1216 sout_DeleteInstance( p_input->p->p_sout );
1220 if( !p_input->b_preparsing && libvlc_stats (p_input) )
1222 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1223 stats_CounterClean( p_input->p->counters.p_##c );\
1224 p_input->p->counters.p_##c = NULL; } while(0)
1225 EXIT_COUNTER( read_bytes );
1226 EXIT_COUNTER( read_packets );
1227 EXIT_COUNTER( demux_read );
1228 EXIT_COUNTER( input_bitrate );
1229 EXIT_COUNTER( demux_bitrate );
1230 EXIT_COUNTER( played_abuffers );
1231 EXIT_COUNTER( lost_abuffers );
1232 EXIT_COUNTER( displayed_pictures );
1233 EXIT_COUNTER( lost_pictures );
1234 EXIT_COUNTER( decoded_audio );
1235 EXIT_COUNTER( decoded_video );
1236 EXIT_COUNTER( decoded_sub );
1238 if( p_input->p->p_sout )
1240 EXIT_COUNTER( sout_sent_packets );
1241 EXIT_COUNTER (sout_sent_bytes );
1242 EXIT_COUNTER( sout_send_bitrate );
1247 /* Mark them deleted */
1248 p_input->p->input.p_demux = NULL;
1249 p_input->p->input.p_stream = NULL;
1250 p_input->p->input.p_access = NULL;
1251 p_input->p->p_es_out = NULL;
1252 p_input->p->p_sout = NULL;
1254 return VLC_EGENERIC;
1257 /*****************************************************************************
1258 * Error: RunThread() error loop
1259 *****************************************************************************
1260 * This function is called when an error occurred during thread main's loop.
1261 *****************************************************************************/
1262 static void Error( input_thread_t *p_input )
1264 input_ChangeState( p_input, ERROR_S );
1265 while( !p_input->b_die )
1268 msleep( INPUT_IDLE_SLEEP );
1272 /*****************************************************************************
1273 * End: end the input thread
1274 *****************************************************************************/
1275 static void End( input_thread_t * p_input )
1279 /* We are at the end */
1280 input_ChangeState( p_input, END_S );
1282 /* Clean control variables */
1283 input_ControlVarStop( p_input );
1285 /* Clean up master */
1286 InputSourceClean( &p_input->p->input );
1289 for( i = 0; i < p_input->p->i_slave; i++ )
1291 InputSourceClean( p_input->p->slave[i] );
1292 free( p_input->p->slave[i] );
1294 free( p_input->p->slave );
1296 /* Unload all modules */
1297 if( p_input->p->p_es_out )
1298 input_EsOutDelete( p_input->p->p_es_out );
1300 if( !p_input->b_preparsing )
1302 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1303 if( libvlc_stats (p_input) )
1305 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
1307 /* make sure we are up to date */
1308 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1309 if( priv->p_stats_computer == p_input )
1311 stats_ComputeGlobalStats( p_input->p_libvlc,
1312 p_input->p_libvlc->p_stats );
1313 priv->p_stats_computer = NULL;
1315 CL_CO( read_bytes );
1316 CL_CO( read_packets );
1317 CL_CO( demux_read );
1318 CL_CO( input_bitrate );
1319 CL_CO( demux_bitrate );
1320 CL_CO( played_abuffers );
1321 CL_CO( lost_abuffers );
1322 CL_CO( displayed_pictures );
1323 CL_CO( lost_pictures );
1324 CL_CO( decoded_audio) ;
1325 CL_CO( decoded_video );
1326 CL_CO( decoded_sub) ;
1329 /* Close optional stream output instance */
1330 if( p_input->p->p_sout )
1332 CL_CO( sout_sent_packets );
1333 CL_CO( sout_sent_bytes );
1334 CL_CO( sout_send_bitrate );
1336 vlc_object_detach( p_input->p->p_sout );
1341 if( p_input->p->i_attachment > 0 )
1343 for( i = 0; i < p_input->p->i_attachment; i++ )
1344 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1345 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1348 /* Tell we're dead */
1349 p_input->b_dead = true;
1352 /*****************************************************************************
1354 *****************************************************************************/
1355 static inline int ControlPopNoLock( input_thread_t *p_input,
1356 int *pi_type, vlc_value_t *p_val )
1358 if( p_input->p->i_control <= 0 )
1360 return VLC_EGENERIC;
1363 *pi_type = p_input->p->control[0].i_type;
1364 *p_val = p_input->p->control[0].val;
1366 p_input->p->i_control--;
1367 if( p_input->p->i_control > 0 )
1371 for( i = 0; i < p_input->p->i_control; i++ )
1373 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1374 p_input->p->control[i].val = p_input->p->control[i+1].val;
1381 static void ControlReduce( input_thread_t *p_input )
1388 for( i = 1; i < p_input->p->i_control; i++ )
1390 const int i_lt = p_input->p->control[i-1].i_type;
1391 const int i_ct = p_input->p->control[i].i_type;
1393 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1394 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1398 ( i_ct == INPUT_CONTROL_SET_STATE ||
1399 i_ct == INPUT_CONTROL_SET_RATE ||
1400 i_ct == INPUT_CONTROL_SET_POSITION ||
1401 i_ct == INPUT_CONTROL_SET_TIME ||
1402 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1403 i_ct == INPUT_CONTROL_SET_TITLE ||
1404 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1405 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1408 // msg_Dbg( p_input, "merged at %d", i );
1409 /* Remove the i-1 */
1410 for( j = i; j < p_input->p->i_control; j++ )
1411 p_input->p->control[j-1] = p_input->p->control[j];
1412 p_input->p->i_control--;
1416 /* TODO but that's not that important
1417 - merge SET_X with SET_X_CMD
1418 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1419 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1426 static bool Control( input_thread_t *p_input, int i_type,
1429 bool b_force_update = false;
1431 if( !p_input ) return b_force_update;
1435 case INPUT_CONTROL_SET_DIE:
1436 msg_Dbg( p_input, "control: stopping input" );
1438 /* Mark all submodules to die */
1439 ObjectKillChildrens( p_input );
1442 case INPUT_CONTROL_SET_POSITION:
1443 case INPUT_CONTROL_SET_POSITION_OFFSET:
1446 if( i_type == INPUT_CONTROL_SET_POSITION )
1448 f_pos = val.f_float;
1452 /* Should not fail */
1453 demux_Control( p_input->p->input.p_demux,
1454 DEMUX_GET_POSITION, &f_pos );
1455 f_pos += val.f_float;
1457 if( f_pos < 0.0 ) f_pos = 0.0;
1458 if( f_pos > 1.0 ) f_pos = 1.0;
1459 /* Reset the decoders states and clock sync (before calling the demuxer */
1460 input_EsOutChangePosition( p_input->p->p_es_out );
1461 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1464 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1465 "%2.1f%% failed", f_pos * 100 );
1469 if( p_input->p->i_slave > 0 )
1470 SlaveSeek( p_input );
1472 b_force_update = true;
1477 case INPUT_CONTROL_SET_TIME:
1478 case INPUT_CONTROL_SET_TIME_OFFSET:
1483 if( i_type == INPUT_CONTROL_SET_TIME )
1485 i_time = val.i_time;
1489 /* Should not fail */
1490 demux_Control( p_input->p->input.p_demux,
1491 DEMUX_GET_TIME, &i_time );
1492 i_time += val.i_time;
1494 if( i_time < 0 ) i_time = 0;
1496 /* Reset the decoders states and clock sync (before calling the demuxer */
1497 input_EsOutChangePosition( p_input->p->p_es_out );
1499 i_ret = demux_Control( p_input->p->input.p_demux,
1500 DEMUX_SET_TIME, i_time );
1505 /* Emulate it with a SET_POS */
1506 demux_Control( p_input->p->input.p_demux,
1507 DEMUX_GET_LENGTH, &i_length );
1510 double f_pos = (double)i_time / (double)i_length;
1511 i_ret = demux_Control( p_input->p->input.p_demux,
1512 DEMUX_SET_POSITION, f_pos );
1517 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1518 " failed or not possible", i_time );
1522 if( p_input->p->i_slave > 0 )
1523 SlaveSeek( p_input );
1525 b_force_update = true;
1530 case INPUT_CONTROL_SET_STATE:
1531 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1532 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1535 if( p_input->p->input.p_access )
1536 i_ret = access_Control( p_input->p->input.p_access,
1537 ACCESS_SET_PAUSE_STATE, false );
1539 i_ret = demux_Control( p_input->p->input.p_demux,
1540 DEMUX_SET_PAUSE_STATE, false );
1544 /* FIXME What to do ? */
1545 msg_Warn( p_input, "cannot unset pause -> EOF" );
1546 vlc_mutex_unlock( &p_input->p->lock_control );
1547 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1548 vlc_mutex_lock( &p_input->p->lock_control );
1551 b_force_update = true;
1553 /* Switch to play */
1554 input_ChangeStateWithVarCallback( p_input, PLAYING_S, false );
1558 input_EsOutChangeState( p_input->p->p_es_out );
1560 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1561 p_input->p->b_can_pause )
1564 if( p_input->p->input.p_access )
1565 i_ret = access_Control( p_input->p->input.p_access,
1566 ACCESS_SET_PAUSE_STATE, true );
1568 i_ret = demux_Control( p_input->p->input.p_demux,
1569 DEMUX_SET_PAUSE_STATE, true );
1571 b_force_update = true;
1575 msg_Warn( p_input, "cannot set pause state" );
1576 state = p_input->i_state;
1583 /* Switch to new state */
1584 input_ChangeStateWithVarCallback( p_input, state, false );
1588 input_EsOutChangeState( p_input->p->p_es_out );
1590 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1592 b_force_update = true;
1594 /* Correct "state" value */
1595 input_ChangeStateWithVarCallback( p_input, p_input->i_state, false );
1597 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1599 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1603 case INPUT_CONTROL_SET_RATE:
1604 case INPUT_CONTROL_SET_RATE_SLOWER:
1605 case INPUT_CONTROL_SET_RATE_FASTER:
1609 if( i_type == INPUT_CONTROL_SET_RATE )
1615 static const int ppi_factor[][2] = {
1616 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1618 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1627 for( i = 0; ppi_factor[i][0] != 0; i++ )
1629 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1630 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1631 if( i_test_e < i_error )
1637 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1639 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1641 if( ppi_factor[i_idx+1][0] > 0 )
1642 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1644 i_rate = INPUT_RATE_MAX+1;
1648 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1650 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1652 i_rate = INPUT_RATE_MIN-1;
1656 if( i_rate < INPUT_RATE_MIN )
1658 msg_Dbg( p_input, "cannot set rate faster" );
1659 i_rate = INPUT_RATE_MIN;
1661 else if( i_rate > INPUT_RATE_MAX )
1663 msg_Dbg( p_input, "cannot set rate slower" );
1664 i_rate = INPUT_RATE_MAX;
1666 if( i_rate != INPUT_RATE_DEFAULT &&
1667 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1668 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1670 msg_Dbg( p_input, "cannot change rate" );
1671 i_rate = INPUT_RATE_DEFAULT;
1673 if( i_rate != p_input->p->i_rate &&
1674 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1677 if( p_input->p->input.p_access )
1678 i_ret = VLC_EGENERIC;
1680 i_ret = demux_Control( p_input->p->input.p_demux,
1681 DEMUX_SET_RATE, &i_rate );
1684 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1685 i_rate = p_input->p->i_rate;
1690 if( i_rate != p_input->p->i_rate )
1693 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1694 var_SetBool( p_input, "rate-change", true );
1696 p_input->p->i_rate = i_rate;
1698 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1699 if( p_input->p->input.b_rescale_ts )
1700 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1702 b_force_update = true;
1707 case INPUT_CONTROL_SET_PROGRAM:
1708 /* No need to force update, es_out does it if needed */
1709 es_out_Control( p_input->p->p_es_out,
1710 ES_OUT_SET_GROUP, val.i_int );
1712 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1716 case INPUT_CONTROL_SET_ES:
1717 /* No need to force update, es_out does it if needed */
1718 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1719 input_EsOutGetFromID( p_input->p->p_es_out,
1723 case INPUT_CONTROL_SET_AUDIO_DELAY:
1724 input_EsOutSetDelay( p_input->p->p_es_out,
1725 AUDIO_ES, val.i_time );
1726 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1729 case INPUT_CONTROL_SET_SPU_DELAY:
1730 input_EsOutSetDelay( p_input->p->p_es_out,
1731 SPU_ES, val.i_time );
1732 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1735 case INPUT_CONTROL_SET_TITLE:
1736 case INPUT_CONTROL_SET_TITLE_NEXT:
1737 case INPUT_CONTROL_SET_TITLE_PREV:
1738 if( p_input->p->input.b_title_demux &&
1739 p_input->p->input.i_title > 0 )
1742 /* FIXME handle demux title */
1743 demux_t *p_demux = p_input->p->input.p_demux;
1746 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1747 i_title = p_demux->info.i_title - 1;
1748 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1749 i_title = p_demux->info.i_title + 1;
1751 i_title = val.i_int;
1753 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1755 input_EsOutChangePosition( p_input->p->p_es_out );
1757 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1758 input_ControlVarTitle( p_input, i_title );
1761 else if( p_input->p->input.i_title > 0 )
1763 access_t *p_access = p_input->p->input.p_access;
1766 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1767 i_title = p_access->info.i_title - 1;
1768 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1769 i_title = p_access->info.i_title + 1;
1771 i_title = val.i_int;
1773 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1775 input_EsOutChangePosition( p_input->p->p_es_out );
1777 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1778 stream_AccessReset( p_input->p->input.p_stream );
1782 case INPUT_CONTROL_SET_SEEKPOINT:
1783 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1784 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1785 if( p_input->p->input.b_title_demux &&
1786 p_input->p->input.i_title > 0 )
1788 demux_t *p_demux = p_input->p->input.p_demux;
1790 int64_t i_input_time;
1791 int64_t i_seekpoint_time;
1793 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1795 i_seekpoint = p_demux->info.i_seekpoint;
1796 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1797 if( i_seekpoint_time >= 0 &&
1798 !demux_Control( p_demux,
1799 DEMUX_GET_TIME, &i_input_time ) )
1801 if ( i_input_time < i_seekpoint_time + 3000000 )
1807 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1808 i_seekpoint = p_demux->info.i_seekpoint + 1;
1810 i_seekpoint = val.i_int;
1812 if( i_seekpoint >= 0 && i_seekpoint <
1813 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1816 input_EsOutChangePosition( p_input->p->p_es_out );
1818 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1821 else if( p_input->p->input.i_title > 0 )
1823 demux_t *p_demux = p_input->p->input.p_demux;
1824 access_t *p_access = p_input->p->input.p_access;
1826 int64_t i_input_time;
1827 int64_t i_seekpoint_time;
1829 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1831 i_seekpoint = p_access->info.i_seekpoint;
1832 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1833 if( i_seekpoint_time >= 0 &&
1834 demux_Control( p_demux,
1835 DEMUX_GET_TIME, &i_input_time ) )
1837 if ( i_input_time < i_seekpoint_time + 3000000 )
1843 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1844 i_seekpoint = p_access->info.i_seekpoint + 1;
1846 i_seekpoint = val.i_int;
1848 if( i_seekpoint >= 0 && i_seekpoint <
1849 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1851 input_EsOutChangePosition( p_input->p->p_es_out );
1853 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1855 stream_AccessReset( p_input->p->input.p_stream );
1860 case INPUT_CONTROL_ADD_SLAVE:
1861 if( val.psz_string )
1863 input_source_t *slave = InputSourceNew( p_input );
1865 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1871 msg_Dbg( p_input, "adding %s as slave on the fly",
1875 if( demux_Control( p_input->p->input.p_demux,
1876 DEMUX_GET_TIME, &i_time ) )
1878 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1879 InputSourceClean( slave );
1883 if( demux_Control( slave->p_demux,
1884 DEMUX_SET_TIME, i_time ) )
1886 msg_Err( p_input, "seek failed for new slave" );
1887 InputSourceClean( slave );
1892 /* Get meta (access and demux) */
1893 p_meta = vlc_meta_New();
1894 access_Control( slave->p_access, ACCESS_GET_META,
1896 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1897 InputUpdateMeta( p_input, p_meta );
1899 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1904 msg_Warn( p_input, "failed to add %s as slave",
1908 free( val.psz_string );
1912 case INPUT_CONTROL_SET_BOOKMARK:
1914 msg_Err( p_input, "not yet implemented" );
1918 return b_force_update;
1921 /*****************************************************************************
1923 *****************************************************************************/
1924 static int UpdateFromDemux( input_thread_t *p_input )
1926 demux_t *p_demux = p_input->p->input.p_demux;
1929 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1931 v.i_int = p_demux->info.i_title;
1932 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1934 input_ControlVarTitle( p_input, p_demux->info.i_title );
1936 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1938 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1940 v.i_int = p_demux->info.i_seekpoint;
1941 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1943 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1945 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1947 /* Hmmm only works with master input */
1948 if( p_input->p->input.p_demux == p_demux )
1950 int i_title_end = p_input->p->input.i_title_end -
1951 p_input->p->input.i_title_offset;
1952 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1953 p_input->p->input.i_seekpoint_offset;
1955 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1957 if( p_demux->info.i_title > i_title_end ||
1958 ( p_demux->info.i_title == i_title_end &&
1959 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1961 else if( i_seekpoint_end >=0 )
1963 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1965 else if( i_title_end >= 0 )
1967 if( p_demux->info.i_title > i_title_end ) return 0;
1974 /*****************************************************************************
1976 *****************************************************************************/
1977 static int UpdateFromAccess( input_thread_t *p_input )
1979 access_t *p_access = p_input->p->input.p_access;
1982 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1984 v.i_int = p_access->info.i_title;
1985 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1987 input_ControlVarTitle( p_input, p_access->info.i_title );
1989 stream_AccessUpdate( p_input->p->input.p_stream );
1991 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
1993 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
1995 v.i_int = p_access->info.i_seekpoint;
1996 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1997 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1999 if( p_access->info.i_update & INPUT_UPDATE_META )
2001 /* TODO maybe multi - access ? */
2002 vlc_meta_t *p_meta = vlc_meta_New();
2003 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2004 InputUpdateMeta( p_input, p_meta );
2005 p_access->info.i_update &= ~INPUT_UPDATE_META;
2008 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2010 /* Hmmm only works with master input */
2011 if( p_input->p->input.p_access == p_access )
2013 int i_title_end = p_input->p->input.i_title_end -
2014 p_input->p->input.i_title_offset;
2015 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2016 p_input->p->input.i_seekpoint_offset;
2018 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2020 if( p_access->info.i_title > i_title_end ||
2021 ( p_access->info.i_title == i_title_end &&
2022 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2024 else if( i_seekpoint_end >=0 )
2026 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2028 else if( i_title_end >= 0 )
2030 if( p_access->info.i_title > i_title_end ) return 0;
2037 /*****************************************************************************
2039 *****************************************************************************/
2040 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2042 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2045 /*****************************************************************************
2047 *****************************************************************************/
2048 static input_source_t *InputSourceNew( input_thread_t *p_input )
2050 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2052 memset( in, 0, sizeof( input_source_t ) );
2056 /*****************************************************************************
2058 *****************************************************************************/
2059 static int InputSourceInit( input_thread_t *p_input,
2060 input_source_t *in, const char *psz_mrl,
2061 const char *psz_forced_demux )
2063 char psz_dup[strlen (psz_mrl) + 1];
2064 const char *psz_access;
2065 const char *psz_demux;
2072 strcpy( psz_dup, psz_mrl );
2074 if( !in ) return VLC_EGENERIC;
2075 if( !p_input ) return VLC_EGENERIC;
2078 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2080 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2081 psz_mrl, psz_access, psz_demux, psz_path );
2082 if( !p_input->b_preparsing )
2084 /* Hack to allow udp://@:port syntax */
2086 (strncmp( psz_access, "udp", 3 ) &&
2087 strncmp( psz_access, "rtp", 3 )) )
2089 /* Find optional titles and seekpoints */
2090 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2091 &in->i_seekpoint_start, &in->i_seekpoint_end );
2094 if( psz_forced_demux && *psz_forced_demux )
2096 psz_demux = psz_forced_demux;
2098 else if( *psz_demux == '\0' )
2100 /* special hack for forcing a demuxer with --demux=module
2101 * (and do nothing with a list) */
2102 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2104 if( psz_var_demux != NULL &&
2105 !strchr(psz_var_demux, ',' ) &&
2106 !strchr(psz_var_demux, ':' ) )
2108 psz_demux = psz_var_demux;
2110 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2114 /* Try access_demux if no demux given */
2115 if( *psz_demux == '\0' )
2117 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2118 NULL, p_input->p->p_es_out, false );
2123 /* Preparsing is only for file:// */
2126 if( !*psz_access ) /* path without scheme:// */
2127 psz_access = "file";
2128 if( strcmp( psz_access, "file" ) )
2130 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2135 int64_t i_pts_delay;
2137 /* Get infos from access_demux */
2138 demux_Control( in->p_demux,
2139 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2140 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2142 in->b_title_demux = true;
2143 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2144 &in->title, &in->i_title,
2145 &in->i_title_offset, &in->i_seekpoint_offset ) )
2150 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2151 &in->b_can_pace_control ) )
2152 in->b_can_pace_control = false;
2154 if( !in->b_can_pace_control )
2156 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2157 &in->b_can_rate_control, &in->b_rescale_ts ) )
2159 in->b_can_rate_control = false;
2160 in->b_rescale_ts = true; /* not used */
2165 in->b_can_rate_control = true;
2166 in->b_rescale_ts = true;
2168 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2169 &in->b_can_pause ) )
2170 in->b_can_pause = false;
2171 var_SetBool( p_input, "can-pause", in->b_can_pause );
2173 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2175 if( ret != VLC_SUCCESS )
2177 var_Set( p_input, "seekable", val );
2181 int64_t i_pts_delay;
2183 input_ChangeState( p_input, OPENING_S );
2185 /* Now try a real access */
2186 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2188 /* Access failed, URL encoded ? */
2189 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2191 decode_URI( psz_path );
2193 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2194 psz_access, psz_demux, psz_path );
2196 in->p_access = access_New( p_input,
2197 psz_access, psz_demux, psz_path );
2199 if( in->p_access == NULL )
2201 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2203 intf_UserFatal( VLC_OBJECT( p_input), false,
2204 _("Your input can't be opened"),
2205 _("VLC is unable to open the MRL '%s'."
2206 " Check the log for details."), psz_mrl );
2211 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2212 while( psz && *psz )
2214 access_t *p_access = in->p_access;
2215 char *end = strchr( psz, ':' );
2220 in->p_access = access_FilterNew( in->p_access, psz );
2221 if( in->p_access == NULL )
2223 in->p_access = p_access;
2224 msg_Warn( p_input, "failed to insert access filter %s",
2232 /* Get infos from access */
2233 if( !p_input->b_preparsing )
2235 access_Control( in->p_access,
2236 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2237 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2239 in->b_title_demux = false;
2240 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2241 &in->title, &in->i_title,
2242 &in->i_title_offset, &in->i_seekpoint_offset ) )
2248 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2249 &in->b_can_pace_control );
2250 in->b_can_rate_control = in->b_can_pace_control;
2251 in->b_rescale_ts = true;
2253 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2255 var_SetBool( p_input, "can-pause", in->b_can_pause );
2256 access_Control( in->p_access, ACCESS_CAN_SEEK,
2258 var_Set( p_input, "seekable", val );
2261 input_ChangeState( p_input, BUFFERING_S );
2263 /* Create the stream_t */
2264 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2265 if( in->p_stream == NULL )
2267 msg_Warn( p_input, "cannot create a stream_t from access" );
2271 /* Open a demuxer */
2272 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2274 psz_demux = in->p_access->psz_demux;
2278 /* Take access redirections into account */
2279 char *psz_real_path;
2280 char *psz_buf = NULL;
2281 if( in->p_access->psz_path )
2283 const char *psz_a, *psz_d;
2284 psz_buf = strdup( in->p_access->psz_path );
2285 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2289 psz_real_path = psz_path;
2291 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2293 in->p_stream, p_input->p->p_es_out,
2294 p_input->b_preparsing );
2298 if( in->p_demux == NULL )
2300 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2301 psz_access, psz_demux, psz_path );
2302 intf_UserFatal( VLC_OBJECT( p_input ), false,
2303 _("VLC can't recognize the input's format"),
2304 _("The format of '%s' cannot be detected. "
2305 "Have a look the log for details."), psz_mrl );
2309 /* Get title from demux */
2310 if( !p_input->b_preparsing && in->i_title <= 0 )
2312 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2313 &in->title, &in->i_title,
2314 &in->i_title_offset, &in->i_seekpoint_offset ))
2316 TAB_INIT( in->i_title, in->title );
2320 in->b_title_demux = true;
2326 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2327 if( 1 || !p_input->b_preparsing )
2330 input_attachment_t **attachment;
2331 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2332 &attachment, &i_attachment ) )
2334 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2335 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2336 i_attachment, attachment );
2337 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2340 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2342 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2344 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2347 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2348 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2353 input_ChangeState( p_input, ERROR_S );
2356 demux_Delete( in->p_demux );
2359 stream_Delete( in->p_stream );
2362 access_Delete( in->p_access );
2364 return VLC_EGENERIC;
2367 /*****************************************************************************
2369 *****************************************************************************/
2370 static void InputSourceClean( input_source_t *in )
2375 demux_Delete( in->p_demux );
2378 stream_Delete( in->p_stream );
2381 access_Delete( in->p_access );
2383 if( in->i_title > 0 )
2385 for( i = 0; i < in->i_title; i++ )
2386 vlc_input_title_Delete( in->title[i] );
2387 TAB_CLEAN( in->i_title, in->title );
2391 static void SlaveDemux( input_thread_t *p_input )
2396 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2398 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2402 for( i = 0; i < p_input->p->i_slave; i++ )
2404 input_source_t *in = p_input->p->slave[i];
2410 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2415 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2417 msg_Err( p_input, "slave[%d] doesn't like "
2418 "DEMUX_GET_TIME -> EOF", i );
2423 if( i_stime >= i_time )
2426 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2432 i_ret = in->p_demux->pf_demux( in->p_demux );
2437 msg_Dbg( p_input, "slave %d EOF", i );
2443 static void SlaveSeek( input_thread_t *p_input )
2448 if( !p_input ) return;
2450 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2452 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2456 for( i = 0; i < p_input->p->i_slave; i++ )
2458 input_source_t *in = p_input->p->slave[i];
2460 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2462 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2468 /*****************************************************************************
2470 *****************************************************************************/
2471 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2475 if( !p_meta ) return;
2477 /* Get meta information from user */
2478 #define GET_META( field, s ) \
2479 var_Get( p_input, (s), &val ); \
2480 if( *val.psz_string ) \
2481 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2482 free( val.psz_string )
2484 GET_META( Title, "meta-title" );
2485 GET_META( Artist, "meta-artist" );
2486 GET_META( Genre, "meta-genre" );
2487 GET_META( Copyright, "meta-copyright" );
2488 GET_META( Description, "meta-description" );
2489 GET_META( Date, "meta-date" );
2490 GET_META( URL, "meta-url" );
2494 /*****************************************************************************
2495 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2496 * arturl and locking issue.
2497 *****************************************************************************/
2498 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2500 input_item_t *p_item = p_input->p->input.p_item;
2501 char * psz_arturl = NULL;
2502 char *psz_title = NULL;
2503 int i_arturl_event = false;
2508 psz_arturl = input_item_GetArtURL( p_item );
2510 vlc_mutex_lock( &p_item->lock );
2511 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2512 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2514 vlc_meta_Merge( p_item->p_meta, p_meta );
2516 if( psz_arturl && *psz_arturl )
2518 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2519 i_arturl_event = true;
2522 vlc_meta_Delete( p_meta );
2524 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2526 /* Don't look for art cover if sout
2527 * XXX It can change when sout has meta data support */
2528 if( p_input->p->p_sout && !p_input->b_preparsing )
2530 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2531 i_arturl_event = true;
2535 input_ExtractAttachmentAndCacheArt( p_input );
2541 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2543 p_meta = vlc_meta_New();
2544 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2546 vlc_mutex_unlock( &p_item->lock );
2548 input_item_SetPreparsed( p_item, true );
2550 if( i_arturl_event == true )
2554 /* Notify interested third parties */
2555 event.type = vlc_InputItemMetaChanged;
2556 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2557 vlc_event_send( &p_item->event_manager, &event );
2562 input_Control( p_input, INPUT_SET_NAME, psz_title );
2566 /** \todo handle sout meta */
2570 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2571 int i_new, input_attachment_t **pp_new )
2573 int i_attachment = *pi_attachment;
2574 input_attachment_t **attachment = *ppp_attachment;
2577 attachment = realloc( attachment,
2578 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2579 for( i = 0; i < i_new; i++ )
2580 attachment[i_attachment++] = pp_new[i];
2584 *pi_attachment = i_attachment;
2585 *ppp_attachment = attachment;
2588 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2592 if( p_input->b_preparsing )
2595 if( p_input->p->input.p_access )
2596 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2599 /* Get meta data from slave input */
2600 for( i = 0; i < p_input->p->i_slave; i++ )
2602 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2603 if( p_input->p->slave[i]->p_access )
2605 access_Control( p_input->p->slave[i]->p_access,
2606 ACCESS_GET_META, p_meta );
2611 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2618 /* XXX I am not sure it is a great idea, besides, there is more than that
2619 * if we want to do it right */
2620 vlc_mutex_lock( &p_item->lock );
2621 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2623 vlc_mutex_unlock( &p_item->lock );
2626 vlc_mutex_unlock( &p_item->lock );
2629 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2630 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2635 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2636 if(! p_demux->p_private )
2639 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2642 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2644 if( p_demux_meta->p_meta )
2646 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2647 vlc_meta_Delete( p_demux_meta->p_meta );
2650 if( p_demux_meta->i_attachments > 0 )
2652 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2653 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2654 p_demux_meta->i_attachments, p_demux_meta->attachments );
2655 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2657 module_Unneed( p_demux, p_id3 );
2659 free( p_demux->p_private );
2663 /*****************************************************************************
2664 * MRLSplit: parse the access, demux and url part of the
2665 * Media Resource Locator.
2666 *****************************************************************************/
2667 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2670 char *psz_access = NULL;
2671 char *psz_demux = NULL;
2674 /* Either there is an access/demux specification before ://
2675 * or we have a plain local file path. */
2676 psz_path = strstr( psz_dup, "://" );
2677 if( psz_path != NULL )
2680 psz_path += 3; /* skips "://" */
2682 /* Separate access from demux (<access>/<demux>://<path>) */
2683 psz_access = psz_dup;
2684 psz_demux = strchr( psz_access, '/' );
2686 *psz_demux++ = '\0';
2688 /* We really don't want module name substitution here! */
2689 if( psz_access[0] == '$' )
2691 if( psz_demux && psz_demux[0] == '$' )
2699 *ppsz_access = psz_access ? psz_access : "";
2700 *ppsz_demux = psz_demux ? psz_demux : "";
2701 *ppsz_path = psz_path ? psz_path : "";
2704 /*****************************************************************************
2705 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2708 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2709 *****************************************************************************/
2710 static void MRLSections( input_thread_t *p_input, char *psz_source,
2711 int *pi_title_start, int *pi_title_end,
2712 int *pi_chapter_start, int *pi_chapter_end )
2714 char *psz, *psz_end, *psz_next, *psz_check;
2716 *pi_title_start = *pi_title_end = -1;
2717 *pi_chapter_start = *pi_chapter_end = -1;
2719 /* Start by parsing titles and chapters */
2720 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2722 /* Check we are really dealing with a title/chapter section */
2723 psz_check = psz + 1;
2724 if( !*psz_check ) return;
2725 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2726 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2727 if( *psz_check == ':' && ++psz_check )
2728 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2729 if( *psz_check != '-' && *psz_check ) return;
2730 if( *psz_check == '-' && ++psz_check )
2731 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2732 if( *psz_check != ':' && *psz_check ) return;
2733 if( *psz_check == ':' && ++psz_check )
2734 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2735 if( *psz_check ) return;
2737 /* Separate start and end */
2739 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2741 /* Look for the start title */
2742 *pi_title_start = strtol( psz, &psz_next, 0 );
2743 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2744 *pi_title_end = *pi_title_start;
2747 /* Look for the start chapter */
2749 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2750 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2751 *pi_chapter_end = *pi_chapter_start;
2755 /* Look for the end title */
2756 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2757 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2760 /* Look for the end chapter */
2761 if( *psz_end ) psz_end++;
2762 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2763 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2766 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2767 psz_source, *pi_title_start, *pi_chapter_start,
2768 *pi_title_end, *pi_chapter_end );
2771 /*****************************************************************************
2772 * input_AddSubtitles: add a subtitles file and enable it
2773 *****************************************************************************/
2774 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2775 bool b_check_extension )
2777 input_source_t *sub;
2780 char *psz_path, *psz_extension;
2782 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2787 /* if we are provided a subtitle.sub file,
2788 * see if we don't have a subtitle.idx and use it instead */
2789 psz_path = strdup( psz_subtitle );
2792 psz_extension = strrchr( psz_path, '.');
2793 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2797 strcpy( psz_extension, ".idx" );
2798 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2799 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2802 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2803 psz_path, psz_subtitle );
2804 strcpy( psz_subtitle, psz_path );
2810 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2812 sub = InputSourceNew( p_input );
2813 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2815 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2818 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2820 if( count.i_int == 0 )
2822 /* if it was first one, there is disable too */
2824 if( count.i_int < list.p_list->i_count )
2826 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2827 &list.p_list->p_values[count.i_int] );
2829 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );
2837 /*****************************************************************************
2838 * input_get_event_manager
2839 *****************************************************************************/
2840 vlc_event_manager_t *
2841 input_get_event_manager( input_thread_t *p_input )
2843 return &p_input->p->event_manager;