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>
46 #include <vlc_playlist.h>
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
52 /*****************************************************************************
54 *****************************************************************************/
55 static void Destructor( input_thread_t * p_input );
57 static int Run ( input_thread_t *p_input );
58 static int RunAndDestroy ( input_thread_t *p_input );
60 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
61 const char *, bool, sout_instance_t * );
62 static int Init ( input_thread_t *p_input );
63 static void Error ( input_thread_t *p_input );
64 static void End ( input_thread_t *p_input );
65 static void MainLoop( input_thread_t *p_input );
67 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
68 static void ControlReduce( input_thread_t * );
69 static bool Control( input_thread_t *, int, vlc_value_t );
71 static int UpdateFromAccess( input_thread_t * );
72 static int UpdateFromDemux( input_thread_t * );
74 static void UpdateItemLength( input_thread_t *, int64_t i_length );
76 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
78 static input_source_t *InputSourceNew( input_thread_t *);
79 static int InputSourceInit( input_thread_t *, input_source_t *,
80 const char *, const char *psz_forced_demux );
81 static void InputSourceClean( input_source_t * );
83 //static void InputGetAttachments( input_thread_t *, input_source_t * );
84 static void SlaveDemux( input_thread_t *p_input );
85 static void SlaveSeek( input_thread_t *p_input );
87 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
88 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
90 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
91 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
92 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
93 int i_new, input_attachment_t **pp_new );
95 /*****************************************************************************
96 * This function creates a new input, and returns a pointer
97 * to its description. On error, it returns NULL.
99 * Variables for _public_ use:
102 * - rate,rate-slower, rate-faster
103 * - position, position-offset
104 * - time, time-offset
105 * - title,title-next,title-prev
106 * - chapter,chapter-next, chapter-prev
107 * - program, audio-es, video-es, spu-es
108 * - audio-delay, spu-delay
113 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
114 * or not, for that check position != 0.0)
116 * * For intf callback upon changes
118 * - rate-change for when playback rate changes
119 * TODO explain when Callback is called
120 * TODO complete this list (?)
121 *****************************************************************************/
122 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
123 const char *psz_header, bool b_quick,
124 sout_instance_t *p_sout )
126 static const char input_name[] = "input";
127 input_thread_t *p_input = NULL; /* thread descriptor */
131 /* Allocate descriptor */
132 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
133 VLC_OBJECT_INPUT, input_name );
134 if( p_input == NULL )
136 msg_Err( p_parent, "out of memory" );
140 /* Construct a nice name for the input timer */
141 char psz_timer_name[255];
142 char * psz_name = input_item_GetName( p_item );
143 snprintf( psz_timer_name, sizeof(psz_timer_name),
144 "input launching for '%s'", psz_name );
147 /* Start a timer to mesure how long it takes
148 * to launch an input */
149 stats_TimerStart( p_input, psz_timer_name,
150 STATS_TIMER_INPUT_LAUNCHING );
152 MALLOC_NULL( p_input->p, input_thread_private_t );
153 memset( p_input->p, 0, sizeof( input_thread_private_t ) );
155 /* One "randomly" selected input thread is responsible for computing
156 * the global stats. Check if there is already someone doing this */
157 if( p_input->p_libvlc->p_stats && !b_quick )
159 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
160 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
161 if( priv->p_stats_computer == NULL )
162 priv->p_stats_computer = p_input;
163 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
166 p_input->b_preparsing = b_quick;
167 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
170 vlc_event_manager_init_with_vlc_object(
171 &p_input->p->event_manager, p_input );
173 /* Init Common fields */
174 p_input->b_eof = false;
175 p_input->b_can_pace_control = true;
176 p_input->p->i_start = 0;
178 p_input->p->i_stop = 0;
179 p_input->p->i_run = 0;
180 p_input->p->i_title = 0;
181 p_input->p->title = NULL;
182 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
183 p_input->i_state = INIT_S;
184 p_input->p->i_rate = INPUT_RATE_DEFAULT;
185 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
186 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
187 p_input->p->p_es_out = NULL;
188 p_input->p->p_sout = NULL;
189 p_input->p->b_out_pace_control = false;
190 p_input->i_pts_delay = 0;
192 /* Init Input fields */
193 vlc_gc_incref( p_item ); /* Released in Destructor() */
194 p_input->p->input.p_item = p_item;
195 p_input->p->input.p_access = NULL;
196 p_input->p->input.p_stream = NULL;
197 p_input->p->input.p_demux = NULL;
198 p_input->p->input.b_title_demux = false;
199 p_input->p->input.i_title = 0;
200 p_input->p->input.title = NULL;
201 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
202 p_input->p->input.b_can_pace_control = true;
203 p_input->p->input.b_can_rate_control = true;
204 p_input->p->input.b_rescale_ts = true;
205 p_input->p->input.b_eof = false;
206 p_input->p->input.i_cr_average = 0;
208 vlc_mutex_lock( &p_item->lock );
210 if( !p_item->p_stats )
211 p_item->p_stats = stats_NewInputStats( p_input );
212 vlc_mutex_unlock( &p_item->lock );
215 p_input->p->i_slave = 0;
216 p_input->p->slave = NULL;
218 /* Init control buffer */
219 vlc_mutex_init( &p_input->p->lock_control );
220 p_input->p->i_control = 0;
222 /* Parse input options */
223 vlc_mutex_lock( &p_item->lock );
224 assert( p_item->optflagc == p_item->i_options );
225 for( i = 0; i < p_item->i_options; i++ )
226 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
227 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
228 vlc_mutex_unlock( &p_item->lock );
230 /* Create Object Variables for private use only */
231 input_ConfigVarInit( p_input );
233 /* Create Objects variables for public Get and Set */
234 input_ControlVarInit( p_input );
236 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
238 if( !p_input->b_preparsing )
240 var_Get( p_input, "bookmarks", &val );
243 /* FIXME: have a common cfg parsing routine used by sout and others */
244 char *psz_parser, *psz_start, *psz_end;
245 psz_parser = val.psz_string;
246 while( (psz_start = strchr( psz_parser, '{' ) ) )
248 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
251 psz_end = strchr( psz_start, '}' );
252 if( !psz_end ) break;
253 psz_parser = psz_end + 1;
254 backup = *psz_parser;
257 while( (psz_end = strchr( psz_start, ',' ) ) )
260 if( !strncmp( psz_start, "name=", 5 ) )
262 p_seekpoint->psz_name = strdup(psz_start + 5);
264 else if( !strncmp( psz_start, "bytes=", 6 ) )
266 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
268 else if( !strncmp( psz_start, "time=", 5 ) )
270 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
273 psz_start = psz_end + 1;
275 msg_Dbg( p_input, "adding bookmark: %s, bytes=%"PRId64", time=%"PRId64,
276 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
277 p_seekpoint->i_time_offset );
278 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
279 vlc_seekpoint_Delete( p_seekpoint );
280 *psz_parser = backup;
282 free( val.psz_string );
286 /* Remove 'Now playing' info as it is probably outdated */
287 input_item_SetNowPlaying( p_item, NULL );
290 if( p_input->b_preparsing )
291 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
295 p_input->p->p_sout = p_sout;
297 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
298 vlc_mutex_init( &p_input->p->counters.counters_lock );
300 /* Attach only once we are ready */
301 vlc_object_attach( p_input, p_parent );
303 /* Set the destructor when we are sure we are initialized */
304 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
310 * Input destructor (called when the object's refcount reaches 0).
312 static void Destructor( input_thread_t * p_input )
314 input_thread_private_t *priv = p_input->p;
316 vlc_event_manager_fini( &p_input->p->event_manager );
318 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
319 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
322 sout_DeleteInstance( priv->p_sout );
324 vlc_gc_decref( p_input->p->input.p_item );
326 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
328 vlc_mutex_destroy( &priv->lock_control );
333 * Initialize an input thread and run it. You will need to monitor the
334 * thread to clean up after it is done
336 * \param p_parent a vlc_object
337 * \param p_item an input item
338 * \return a pointer to the spawned input thread
340 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
341 input_item_t *p_item )
343 return __input_CreateThreadExtended( p_parent, p_item, NULL, NULL );
347 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
348 input_item_t *p_item,
349 const char *psz_log, sout_instance_t *p_sout )
351 input_thread_t *p_input;
353 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
357 /* Create thread and wait for its readiness. */
358 if( vlc_thread_create( p_input, "input", Run,
359 VLC_THREAD_PRIORITY_INPUT, true ) )
361 input_ChangeState( p_input, ERROR_S );
362 msg_Err( p_input, "cannot create input thread" );
363 vlc_object_detach( p_input );
364 vlc_object_release( p_input );
372 * Initialize an input thread and run it. This thread will clean after itself,
373 * you can forget about it. It can work either in blocking or non-blocking mode
375 * \param p_parent a vlc_object
376 * \param p_item an input item
377 * \param b_block should we block until read is finished ?
378 * \return the input object id if non blocking, an error code else
380 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
383 input_thread_t *p_input;
385 p_input = Create( p_parent, p_item, NULL, false, NULL );
391 RunAndDestroy( p_input );
396 if( vlc_thread_create( p_input, "input", RunAndDestroy,
397 VLC_THREAD_PRIORITY_INPUT, true ) )
399 input_ChangeState( p_input, ERROR_S );
400 msg_Err( p_input, "cannot create input thread" );
401 vlc_object_release( p_input );
405 return p_input->i_object_id;
409 * Initialize an input and initialize it to preparse the item
410 * This function is blocking. It will only accept to parse files
412 * \param p_parent a vlc_object_t
413 * \param p_item an input item
414 * \return VLC_SUCCESS or an error
416 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
418 input_thread_t *p_input;
420 /* Allocate descriptor */
421 p_input = Create( p_parent, p_item, NULL, true, NULL );
425 if( !Init( p_input ) )
428 vlc_object_detach( p_input );
429 vlc_object_release( p_input );
435 * Request a running input thread to stop and die
437 * \param the input thread to stop
439 void input_StopThread( input_thread_t *p_input )
444 /* Set die for input */
445 vlc_object_kill( p_input );
446 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
448 /* We cannot touch p_input fields directly (we come from another thread),
449 * so use the vlc_object_find way, it's perfectly safe */
451 /* Set die for all access */
452 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
453 for( i = 0; i < p_list->i_count; i++ )
455 vlc_object_kill( p_list->p_values[i].p_object );
457 vlc_list_release( p_list );
459 /* Set die for all stream */
460 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
461 for( i = 0; i < p_list->i_count; i++ )
463 vlc_object_kill( p_list->p_values[i].p_object );
465 vlc_list_release( p_list );
467 /* Set die for all demux */
468 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
469 for( i = 0; i < p_list->i_count; i++ )
471 vlc_object_kill( p_list->p_values[i].p_object );
473 vlc_list_release( p_list );
475 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
478 sout_instance_t * input_DetachSout( input_thread_t *p_input )
480 sout_instance_t *p_sout = p_input->p->p_sout;
481 vlc_object_detach( p_sout );
482 p_input->p->p_sout = NULL;
486 /*****************************************************************************
487 * Run: main thread loop
488 * This is the "normal" thread that spawns the input processing chain,
489 * reads the stream, cleans up and waits
490 *****************************************************************************/
491 static int Run( input_thread_t *p_input )
493 /* Signal that the thread is launched */
494 vlc_thread_ready( p_input );
496 if( Init( p_input ) )
498 /* If we failed, wait before we are killed, and exit */
499 p_input->b_error = true;
501 /* FIXME: we don't want to depend on the playlist */
502 playlist_t * p_playlist = vlc_object_find( p_input,
503 VLC_OBJECT_PLAYLIST, FIND_PARENT );
506 playlist_Signal( p_playlist );
507 vlc_object_release( p_playlist );
512 /* Tell we're dead */
513 p_input->b_dead = true;
520 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
522 /* We have finish to demux data but not to play them */
523 while( !p_input->b_die )
525 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
528 msg_Dbg( p_input, "waiting decoder fifos to empty" );
530 msleep( INPUT_IDLE_SLEEP );
533 /* We have finished */
534 p_input->b_eof = true;
535 playlist_Signal( libvlc_priv (p_input->p_libvlc)->p_playlist );
538 /* Wait until we are asked to die */
539 if( !p_input->b_die )
550 /*****************************************************************************
551 * RunAndDestroy: main thread loop
552 * This is the "just forget me" thread that spawns the input processing chain,
553 * reads the stream, cleans up and releases memory
554 *****************************************************************************/
555 static int RunAndDestroy( input_thread_t *p_input )
557 /* Signal that the thread is launched */
558 vlc_thread_ready( p_input );
560 if( Init( p_input ) )
565 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
567 /* We have finished demuxing data but not playing it */
568 while( !p_input->b_die )
570 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
573 msg_Dbg( p_input, "waiting decoder fifos to empty" );
575 msleep( INPUT_IDLE_SLEEP );
578 /* We have finished */
579 p_input->b_eof = true;
587 vlc_object_release( p_input );
591 /*****************************************************************************
592 * Main loop: Fill buffers from access, and demux
593 *****************************************************************************/
594 static void MainLoop( input_thread_t *p_input )
596 int64_t i_start_mdate = mdate();
597 int64_t i_intf_update = 0;
601 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
603 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
605 bool b_force_update = false;
611 if( p_input->i_state != PAUSE_S )
613 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
614 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
617 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
622 if( p_input->p->input.b_title_demux &&
623 p_input->p->input.p_demux->info.i_update )
625 i_ret = UpdateFromDemux( p_input );
626 b_force_update = true;
628 else if( !p_input->p->input.b_title_demux &&
629 p_input->p->input.p_access &&
630 p_input->p->input.p_access->info.i_update )
632 i_ret = UpdateFromAccess( p_input );
633 b_force_update = true;
637 if( i_ret == 0 ) /* EOF */
641 var_Get( p_input, "input-repeat", &repeat );
642 if( repeat.i_int == 0 )
644 /* End of file - we do not set b_die because only the
645 * playlist is allowed to do so. */
646 input_ChangeState( p_input, END_S );
647 msg_Dbg( p_input, "EOF reached" );
648 p_input->p->input.b_eof = true;
652 msg_Dbg( p_input, "repeating the same input (%d)",
654 if( repeat.i_int > 0 )
657 var_Set( p_input, "input-repeat", repeat );
660 /* Seek to start title/seekpoint */
661 val.i_int = p_input->p->input.i_title_start -
662 p_input->p->input.i_title_offset;
663 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
665 input_ControlPush( p_input,
666 INPUT_CONTROL_SET_TITLE, &val );
668 val.i_int = p_input->p->input.i_seekpoint_start -
669 p_input->p->input.i_seekpoint_offset;
670 if( val.i_int > 0 /* TODO: check upper boundary */ )
671 input_ControlPush( p_input,
672 INPUT_CONTROL_SET_SEEKPOINT, &val );
674 /* Seek to start position */
675 if( p_input->p->i_start > 0 )
677 val.i_time = p_input->p->i_start;
678 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
684 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
689 i_start_mdate = mdate();
694 p_input->b_error = true;
697 if( i_ret > 0 && p_input->p->i_slave > 0 )
699 SlaveDemux( p_input );
709 vlc_mutex_lock( &p_input->p->lock_control );
710 ControlReduce( p_input );
711 while( !ControlPopNoLock( p_input, &i_type, &val ) )
713 msg_Dbg( p_input, "control type=%d", i_type );
714 if( Control( p_input, i_type, val ) )
715 b_force_update = true;
717 vlc_mutex_unlock( &p_input->p->lock_control );
719 if( b_force_update || i_intf_update < mdate() )
723 int64_t i_time, i_length;
724 /* update input status variables */
725 if( !demux_Control( p_input->p->input.p_demux,
726 DEMUX_GET_POSITION, &f_pos ) )
728 val.f_float = (float)f_pos;
729 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
731 if( !demux_Control( p_input->p->input.p_demux,
732 DEMUX_GET_TIME, &i_time ) )
734 p_input->i_time = i_time;
736 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
738 if( !demux_Control( p_input->p->input.p_demux,
739 DEMUX_GET_LENGTH, &i_length ) )
742 var_Get( p_input, "length", &old_val );
743 val.i_time = i_length;
744 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
746 if( old_val.i_time != val.i_time )
748 UpdateItemLength( p_input, i_length );
752 var_SetBool( p_input, "intf-change", true );
753 i_intf_update = mdate() + INT64_C(150000);
755 /* 150ms * 8 = ~ 1 second */
756 if( ++i_updates % 8 == 0 )
758 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
759 /* Are we the thread responsible for computing global stats ? */
760 if( libvlc_priv (p_input->p_libvlc)->p_stats_computer == p_input )
762 stats_ComputeGlobalStats( p_input->p_libvlc,
763 p_input->p_libvlc->p_stats );
769 static void InitStatistics( input_thread_t * p_input )
771 if( p_input->b_preparsing ) return;
773 /* Prepare statistics */
774 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
775 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
776 if( libvlc_stats (p_input) )
778 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
779 INIT_COUNTER( read_packets, INTEGER, COUNTER );
780 INIT_COUNTER( demux_read, INTEGER, COUNTER );
781 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
782 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
783 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
784 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
785 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
786 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
787 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
788 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
789 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
790 p_input->p->counters.p_sout_send_bitrate = NULL;
791 p_input->p->counters.p_sout_sent_packets = NULL;
792 p_input->p->counters.p_sout_sent_bytes = NULL;
793 if( p_input->p->counters.p_demux_bitrate )
794 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
795 if( p_input->p->counters.p_input_bitrate )
796 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
801 static int InitSout( input_thread_t * p_input )
805 if( p_input->b_preparsing ) return VLC_SUCCESS;
807 /* Find a usable sout and attach it to p_input */
808 psz = var_GetNonEmptyString( p_input, "sout" );
809 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
811 /* Check the validity of the provided sout */
812 if( p_input->p->p_sout )
814 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
816 msg_Dbg( p_input, "destroying unusable sout" );
818 sout_DeleteInstance( p_input->p->p_sout );
819 p_input->p->p_sout = NULL;
823 if( p_input->p->p_sout )
826 msg_Dbg( p_input, "sout keep: reusing sout" );
827 msg_Dbg( p_input, "sout keep: you probably want to use "
828 "gather stream_out" );
829 vlc_object_attach( p_input->p->p_sout, p_input );
833 /* Create a new one */
834 p_input->p->p_sout = sout_NewInstance( p_input, psz );
835 if( !p_input->p->p_sout )
837 input_ChangeState( p_input, ERROR_S );
838 msg_Err( p_input, "cannot start stream output instance, " \
844 if( libvlc_stats (p_input) )
846 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
847 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
848 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
849 if( p_input->p->counters.p_sout_send_bitrate )
850 p_input->p->counters.p_sout_send_bitrate->update_interval =
854 else if( p_input->p->p_sout )
856 msg_Dbg( p_input, "destroying useless sout" );
858 sout_DeleteInstance( p_input->p->p_sout );
859 p_input->p->p_sout = NULL;
867 static void InitTitle( input_thread_t * p_input )
871 if( p_input->b_preparsing ) return;
873 /* Create global title (from master) */
874 p_input->p->i_title = p_input->p->input.i_title;
875 p_input->p->title = p_input->p->input.title;
876 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
877 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
878 if( p_input->p->i_title > 0 )
880 /* Setup variables */
881 input_ControlVarNavigation( p_input );
882 input_ControlVarTitle( p_input, 0 );
886 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
887 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
888 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
891 if( p_input->i_pts_delay < 0 )
892 p_input->i_pts_delay = 0;
894 /* If the desynchronisation requested by the user is < 0, we need to
895 * cache more data. */
896 var_Get( p_input, "audio-desync", &val );
897 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
899 /* Update cr_average depending on the caching */
900 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
901 p_input->p->input.i_cr_average /= 10;
902 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
905 static void StartTitle( input_thread_t * p_input )
914 /* Start title/chapter */
916 if( p_input->b_preparsing )
918 p_input->p->i_start = 0;
922 val.i_int = p_input->p->input.i_title_start -
923 p_input->p->input.i_title_offset;
924 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
925 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
926 val.i_int = p_input->p->input.i_seekpoint_start -
927 p_input->p->input.i_seekpoint_offset;
928 if( val.i_int > 0 /* TODO: check upper boundary */ )
929 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
933 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
934 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
935 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
936 i_length = var_GetTime( p_input, "length" );
937 if( p_input->p->i_run < 0 )
939 msg_Warn( p_input, "invalid run-time ignored" );
940 p_input->p->i_run = 0;
943 if( p_input->p->i_start > 0 )
945 if( p_input->p->i_start >= i_length )
947 msg_Warn( p_input, "invalid start-time ignored" );
953 msg_Dbg( p_input, "starting at time: %ds",
954 (int)( p_input->p->i_start / INT64_C(1000000) ) );
956 s.i_time = p_input->p->i_start;
957 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
960 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
962 msg_Warn( p_input, "invalid stop-time ignored" );
963 p_input->p->i_stop = 0;
967 /* Get fps and set it if not already set */
968 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
971 float f_requested_fps;
973 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
974 var_SetFloat( p_input, "sub-original-fps", f_fps );
976 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
977 if( f_requested_fps != f_fps )
979 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
981 var_SetFloat( p_input, "sub-fps", f_requested_fps );
985 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
988 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
991 /* Look for and add subtitle files */
992 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
993 if( psz_subtitle != NULL )
995 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
996 input_AddSubtitles( p_input, psz_subtitle, false );
999 var_Get( p_input, "sub-autodetect-file", &val );
1002 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1003 char **subs = subtitles_Detect( p_input, psz_autopath,
1004 p_input->p->input.p_item->psz_uri );
1005 input_source_t *sub;
1007 if( psz_autopath == NULL )
1008 psz_autopath = strdup("");
1010 /* Try to autoselect the first autodetected subtitles file
1011 * if no subtitles file was specified */
1012 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1014 input_AddSubtitles( p_input, subs[0], false );
1019 /* Then, just add the following subtitles files */
1020 for( ; subs && subs[i]; i++ )
1022 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1024 sub = InputSourceNew( p_input );
1025 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1027 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1034 free( psz_autopath );
1036 free( psz_subtitle );
1038 /* Look for slave */
1039 psz = var_GetNonEmptyString( p_input, "input-slave" );
1043 input_source_t *slave;
1044 while( psz && *psz )
1046 while( *psz == ' ' || *psz == '#' )
1050 if( ( psz_delim = strchr( psz, '#' ) ) )
1052 *psz_delim++ = '\0';
1059 msg_Dbg( p_input, "adding slave input '%s'", psz );
1060 slave = InputSourceNew( p_input );
1061 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1063 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1072 static void InitPrograms( input_thread_t * p_input )
1077 if( p_input->b_preparsing ) return;
1080 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1081 i_es_out_mode = ES_OUT_MODE_AUTO;
1083 if( p_input->p->p_sout )
1085 var_Get( p_input, "sout-all", &val );
1088 i_es_out_mode = ES_OUT_MODE_ALL;
1093 var_Get( p_input, "programs", &val );
1094 if ( val.p_list && val.p_list->i_count )
1096 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1097 /* Note : we should remove the "program" callback. */
1100 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1104 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1106 /* Inform the demuxer about waited group (needed only for DVB) */
1107 if( i_es_out_mode == ES_OUT_MODE_ALL )
1109 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1111 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1113 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1118 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1119 (int) var_GetInteger( p_input, "program" ), NULL );
1123 static int Init( input_thread_t * p_input )
1129 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1131 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1133 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1134 var_SetString( p_input, "sout", "" );
1135 var_SetBool( p_input, "sout-all", false );
1136 var_SetString( p_input, "input-slave", "" );
1137 var_SetInteger( p_input, "input-repeat", 0 );
1138 var_SetString( p_input, "sub-file", "" );
1139 var_SetBool( p_input, "sub-autodetect-file", false );
1143 InitStatistics( p_input );
1145 ret = InitSout( p_input );
1146 if( ret != VLC_SUCCESS )
1147 return ret; /* FIXME: goto error; should be better here */
1151 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1152 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1153 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1155 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1156 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1158 if( InputSourceInit( p_input, &p_input->p->input,
1159 p_input->p->input.p_item->psz_uri, NULL ) )
1164 InitTitle( p_input );
1166 /* Load master infos */
1168 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1169 &val.i_time ) && val.i_time > 0 )
1171 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1172 UpdateItemLength( p_input, val.i_time );
1176 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1177 if( val.i_time > 0 )
1178 { /* fallback: gets length from metadata */
1179 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1180 UpdateItemLength( p_input, val.i_time );
1184 StartTitle( p_input );
1186 InitPrograms( p_input );
1188 if( !p_input->b_preparsing && p_input->p->p_sout )
1190 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1192 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1194 /* We don't want a high input priority here or we'll
1195 * end-up sucking up all the CPU time */
1196 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1199 msg_Dbg( p_input, "starting in %s mode",
1200 p_input->p->b_out_pace_control ? "async" : "sync" );
1203 p_meta = vlc_meta_New();
1205 /* Get meta data from users */
1206 InputMetaUser( p_input, p_meta );
1208 /* Get meta data from master input */
1209 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1211 /* Access_file does not give any meta, and there are no slave */
1212 AccessMeta( p_input, p_meta );
1214 InputUpdateMeta( p_input, p_meta );
1216 if( !p_input->b_preparsing )
1218 msg_Dbg( p_input, "`%s' successfully opened",
1219 p_input->p->input.p_item->psz_uri );
1223 /* initialization is complete */
1224 input_ChangeState( p_input, PLAYING_S );
1229 input_ChangeState( p_input, ERROR_S );
1231 if( p_input->p->p_es_out )
1232 input_EsOutDelete( p_input->p->p_es_out );
1234 if( p_input->p->p_sout )
1236 vlc_object_detach( p_input->p->p_sout );
1237 sout_DeleteInstance( p_input->p->p_sout );
1241 if( !p_input->b_preparsing && libvlc_stats (p_input) )
1243 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1244 stats_CounterClean( p_input->p->counters.p_##c );\
1245 p_input->p->counters.p_##c = NULL; } while(0)
1246 EXIT_COUNTER( read_bytes );
1247 EXIT_COUNTER( read_packets );
1248 EXIT_COUNTER( demux_read );
1249 EXIT_COUNTER( input_bitrate );
1250 EXIT_COUNTER( demux_bitrate );
1251 EXIT_COUNTER( played_abuffers );
1252 EXIT_COUNTER( lost_abuffers );
1253 EXIT_COUNTER( displayed_pictures );
1254 EXIT_COUNTER( lost_pictures );
1255 EXIT_COUNTER( decoded_audio );
1256 EXIT_COUNTER( decoded_video );
1257 EXIT_COUNTER( decoded_sub );
1259 if( p_input->p->p_sout )
1261 EXIT_COUNTER( sout_sent_packets );
1262 EXIT_COUNTER (sout_sent_bytes );
1263 EXIT_COUNTER( sout_send_bitrate );
1268 /* Mark them deleted */
1269 p_input->p->input.p_demux = NULL;
1270 p_input->p->input.p_stream = NULL;
1271 p_input->p->input.p_access = NULL;
1272 p_input->p->p_es_out = NULL;
1273 p_input->p->p_sout = NULL;
1275 return VLC_EGENERIC;
1278 /*****************************************************************************
1279 * Error: RunThread() error loop
1280 *****************************************************************************
1281 * This function is called when an error occurred during thread main's loop.
1282 *****************************************************************************/
1283 static void Error( input_thread_t *p_input )
1285 input_ChangeState( p_input, ERROR_S );
1286 while( !p_input->b_die )
1289 msleep( INPUT_IDLE_SLEEP );
1293 /*****************************************************************************
1294 * End: end the input thread
1295 *****************************************************************************/
1296 static void End( input_thread_t * p_input )
1300 /* We are at the end */
1301 input_ChangeState( p_input, END_S );
1303 /* Clean control variables */
1304 input_ControlVarClean( p_input );
1306 /* Clean up master */
1307 InputSourceClean( &p_input->p->input );
1310 for( i = 0; i < p_input->p->i_slave; i++ )
1312 InputSourceClean( p_input->p->slave[i] );
1313 free( p_input->p->slave[i] );
1315 free( p_input->p->slave );
1317 /* Unload all modules */
1318 if( p_input->p->p_es_out )
1319 input_EsOutDelete( p_input->p->p_es_out );
1321 if( !p_input->b_preparsing )
1323 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1324 if( libvlc_stats (p_input) )
1326 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
1328 /* make sure we are up to date */
1329 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1330 if( priv->p_stats_computer == p_input )
1332 stats_ComputeGlobalStats( p_input->p_libvlc,
1333 p_input->p_libvlc->p_stats );
1334 priv->p_stats_computer = NULL;
1336 CL_CO( read_bytes );
1337 CL_CO( read_packets );
1338 CL_CO( demux_read );
1339 CL_CO( input_bitrate );
1340 CL_CO( demux_bitrate );
1341 CL_CO( played_abuffers );
1342 CL_CO( lost_abuffers );
1343 CL_CO( displayed_pictures );
1344 CL_CO( lost_pictures );
1345 CL_CO( decoded_audio) ;
1346 CL_CO( decoded_video );
1347 CL_CO( decoded_sub) ;
1350 /* Close optional stream output instance */
1351 if( p_input->p->p_sout )
1353 CL_CO( sout_sent_packets );
1354 CL_CO( sout_sent_bytes );
1355 CL_CO( sout_send_bitrate );
1357 vlc_object_detach( p_input->p->p_sout );
1362 if( p_input->p->i_attachment > 0 )
1364 for( i = 0; i < p_input->p->i_attachment; i++ )
1365 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1366 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1369 /* Tell we're dead */
1370 p_input->b_dead = true;
1373 /*****************************************************************************
1375 *****************************************************************************/
1376 static inline int ControlPopNoLock( input_thread_t *p_input,
1377 int *pi_type, vlc_value_t *p_val )
1379 if( p_input->p->i_control <= 0 )
1381 return VLC_EGENERIC;
1384 *pi_type = p_input->p->control[0].i_type;
1385 *p_val = p_input->p->control[0].val;
1387 p_input->p->i_control--;
1388 if( p_input->p->i_control > 0 )
1392 for( i = 0; i < p_input->p->i_control; i++ )
1394 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1395 p_input->p->control[i].val = p_input->p->control[i+1].val;
1402 static void ControlReduce( input_thread_t *p_input )
1409 for( i = 1; i < p_input->p->i_control; i++ )
1411 const int i_lt = p_input->p->control[i-1].i_type;
1412 const int i_ct = p_input->p->control[i].i_type;
1414 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1415 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1419 ( i_ct == INPUT_CONTROL_SET_STATE ||
1420 i_ct == INPUT_CONTROL_SET_RATE ||
1421 i_ct == INPUT_CONTROL_SET_POSITION ||
1422 i_ct == INPUT_CONTROL_SET_TIME ||
1423 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1424 i_ct == INPUT_CONTROL_SET_TITLE ||
1425 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1426 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1429 // msg_Dbg( p_input, "merged at %d", i );
1430 /* Remove the i-1 */
1431 for( j = i; j < p_input->p->i_control; j++ )
1432 p_input->p->control[j-1] = p_input->p->control[j];
1433 p_input->p->i_control--;
1437 /* TODO but that's not that important
1438 - merge SET_X with SET_X_CMD
1439 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1440 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1447 static bool Control( input_thread_t *p_input, int i_type,
1450 bool b_force_update = false;
1452 if( !p_input ) return b_force_update;
1456 case INPUT_CONTROL_SET_DIE:
1457 msg_Dbg( p_input, "control: stopping input" );
1458 /* Mark all submodules to die */
1459 if( p_input->p->input.p_access )
1460 vlc_object_kill( p_input->p->input.p_access );
1461 if( p_input->p->input.p_stream )
1462 vlc_object_kill( p_input->p->input.p_stream );
1463 vlc_object_kill( p_input->p->input.p_demux );
1465 vlc_object_kill( p_input );
1468 case INPUT_CONTROL_SET_POSITION:
1469 case INPUT_CONTROL_SET_POSITION_OFFSET:
1472 if( i_type == INPUT_CONTROL_SET_POSITION )
1474 f_pos = val.f_float;
1478 /* Should not fail */
1479 demux_Control( p_input->p->input.p_demux,
1480 DEMUX_GET_POSITION, &f_pos );
1481 f_pos += val.f_float;
1483 if( f_pos < 0.0 ) f_pos = 0.0;
1484 if( f_pos > 1.0 ) f_pos = 1.0;
1485 /* Reset the decoders states and clock sync (before calling the demuxer */
1486 input_EsOutChangePosition( p_input->p->p_es_out );
1487 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1490 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1491 "%2.1f%% failed", f_pos * 100 );
1495 if( p_input->p->i_slave > 0 )
1496 SlaveSeek( p_input );
1498 b_force_update = true;
1503 case INPUT_CONTROL_SET_TIME:
1504 case INPUT_CONTROL_SET_TIME_OFFSET:
1509 if( i_type == INPUT_CONTROL_SET_TIME )
1511 i_time = val.i_time;
1515 /* Should not fail */
1516 demux_Control( p_input->p->input.p_demux,
1517 DEMUX_GET_TIME, &i_time );
1518 i_time += val.i_time;
1520 if( i_time < 0 ) i_time = 0;
1522 /* Reset the decoders states and clock sync (before calling the demuxer */
1523 input_EsOutChangePosition( p_input->p->p_es_out );
1525 i_ret = demux_Control( p_input->p->input.p_demux,
1526 DEMUX_SET_TIME, i_time );
1531 /* Emulate it with a SET_POS */
1532 demux_Control( p_input->p->input.p_demux,
1533 DEMUX_GET_LENGTH, &i_length );
1536 double f_pos = (double)i_time / (double)i_length;
1537 i_ret = demux_Control( p_input->p->input.p_demux,
1538 DEMUX_SET_POSITION, f_pos );
1543 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1544 " failed or not possible", i_time );
1548 if( p_input->p->i_slave > 0 )
1549 SlaveSeek( p_input );
1551 b_force_update = true;
1556 case INPUT_CONTROL_SET_STATE:
1557 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1558 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1561 if( p_input->p->input.p_access )
1562 i_ret = access_Control( p_input->p->input.p_access,
1563 ACCESS_SET_PAUSE_STATE, false );
1565 i_ret = demux_Control( p_input->p->input.p_demux,
1566 DEMUX_SET_PAUSE_STATE, false );
1570 /* FIXME What to do ? */
1571 msg_Warn( p_input, "cannot unset pause -> EOF" );
1572 vlc_mutex_unlock( &p_input->p->lock_control );
1573 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1574 vlc_mutex_lock( &p_input->p->lock_control );
1577 b_force_update = true;
1579 /* Switch to play */
1580 p_input->i_state = PLAYING_S;
1581 val.i_int = PLAYING_S;
1582 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1586 input_EsOutChangeState( p_input->p->p_es_out );
1588 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1589 p_input->p->b_can_pause )
1592 if( p_input->p->input.p_access )
1593 i_ret = access_Control( p_input->p->input.p_access,
1594 ACCESS_SET_PAUSE_STATE, true );
1596 i_ret = demux_Control( p_input->p->input.p_demux,
1597 DEMUX_SET_PAUSE_STATE, true );
1599 b_force_update = true;
1603 msg_Warn( p_input, "cannot set pause state" );
1604 val.i_int = p_input->i_state;
1608 val.i_int = PAUSE_S;
1611 /* Switch to new state */
1612 p_input->i_state = val.i_int;
1613 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1617 input_EsOutChangeState( p_input->p->p_es_out );
1619 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1621 b_force_update = true;
1623 /* Correct "state" value */
1624 val.i_int = p_input->i_state;
1625 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1627 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1629 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1633 case INPUT_CONTROL_SET_RATE:
1634 case INPUT_CONTROL_SET_RATE_SLOWER:
1635 case INPUT_CONTROL_SET_RATE_FASTER:
1639 if( i_type == INPUT_CONTROL_SET_RATE )
1645 static const int ppi_factor[][2] = {
1646 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1648 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1657 for( i = 0; ppi_factor[i][0] != 0; i++ )
1659 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1660 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1661 if( i_test_e < i_error )
1667 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1669 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1671 if( ppi_factor[i_idx+1][0] > 0 )
1672 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1674 i_rate = INPUT_RATE_MAX+1;
1678 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1680 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1682 i_rate = INPUT_RATE_MIN-1;
1686 if( i_rate < INPUT_RATE_MIN )
1688 msg_Dbg( p_input, "cannot set rate faster" );
1689 i_rate = INPUT_RATE_MIN;
1691 else if( i_rate > INPUT_RATE_MAX )
1693 msg_Dbg( p_input, "cannot set rate slower" );
1694 i_rate = INPUT_RATE_MAX;
1696 if( i_rate != INPUT_RATE_DEFAULT &&
1697 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1698 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1700 msg_Dbg( p_input, "cannot change rate" );
1701 i_rate = INPUT_RATE_DEFAULT;
1703 if( i_rate != p_input->p->i_rate &&
1704 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1707 if( p_input->p->input.p_access )
1708 i_ret = VLC_EGENERIC;
1710 i_ret = demux_Control( p_input->p->input.p_demux,
1711 DEMUX_SET_RATE, &i_rate );
1714 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1715 i_rate = p_input->p->i_rate;
1720 if( i_rate != p_input->p->i_rate )
1723 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1724 var_SetBool( p_input, "rate-change", true );
1726 p_input->p->i_rate = i_rate;
1728 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1729 if( p_input->p->input.b_rescale_ts )
1730 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1732 b_force_update = true;
1737 case INPUT_CONTROL_SET_PROGRAM:
1738 /* No need to force update, es_out does it if needed */
1739 es_out_Control( p_input->p->p_es_out,
1740 ES_OUT_SET_GROUP, val.i_int );
1742 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1746 case INPUT_CONTROL_SET_ES:
1747 /* No need to force update, es_out does it if needed */
1748 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1749 input_EsOutGetFromID( p_input->p->p_es_out,
1753 case INPUT_CONTROL_SET_AUDIO_DELAY:
1754 input_EsOutSetDelay( p_input->p->p_es_out,
1755 AUDIO_ES, val.i_time );
1756 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1759 case INPUT_CONTROL_SET_SPU_DELAY:
1760 input_EsOutSetDelay( p_input->p->p_es_out,
1761 SPU_ES, val.i_time );
1762 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1765 case INPUT_CONTROL_SET_TITLE:
1766 case INPUT_CONTROL_SET_TITLE_NEXT:
1767 case INPUT_CONTROL_SET_TITLE_PREV:
1768 if( p_input->p->input.b_title_demux &&
1769 p_input->p->input.i_title > 0 )
1772 /* FIXME handle demux title */
1773 demux_t *p_demux = p_input->p->input.p_demux;
1776 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1777 i_title = p_demux->info.i_title - 1;
1778 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1779 i_title = p_demux->info.i_title + 1;
1781 i_title = val.i_int;
1783 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1785 input_EsOutChangePosition( p_input->p->p_es_out );
1787 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1788 input_ControlVarTitle( p_input, i_title );
1791 else if( p_input->p->input.i_title > 0 )
1793 access_t *p_access = p_input->p->input.p_access;
1796 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1797 i_title = p_access->info.i_title - 1;
1798 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1799 i_title = p_access->info.i_title + 1;
1801 i_title = val.i_int;
1803 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1805 input_EsOutChangePosition( p_input->p->p_es_out );
1807 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1808 stream_AccessReset( p_input->p->input.p_stream );
1812 case INPUT_CONTROL_SET_SEEKPOINT:
1813 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1814 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1815 if( p_input->p->input.b_title_demux &&
1816 p_input->p->input.i_title > 0 )
1818 demux_t *p_demux = p_input->p->input.p_demux;
1820 int64_t i_input_time;
1821 int64_t i_seekpoint_time;
1823 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1825 i_seekpoint = p_demux->info.i_seekpoint;
1826 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1827 if( i_seekpoint_time >= 0 &&
1828 !demux_Control( p_demux,
1829 DEMUX_GET_TIME, &i_input_time ) )
1831 if ( i_input_time < i_seekpoint_time + 3000000 )
1837 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1838 i_seekpoint = p_demux->info.i_seekpoint + 1;
1840 i_seekpoint = val.i_int;
1842 if( i_seekpoint >= 0 && i_seekpoint <
1843 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1846 input_EsOutChangePosition( p_input->p->p_es_out );
1848 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1851 else if( p_input->p->input.i_title > 0 )
1853 demux_t *p_demux = p_input->p->input.p_demux;
1854 access_t *p_access = p_input->p->input.p_access;
1856 int64_t i_input_time;
1857 int64_t i_seekpoint_time;
1859 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1861 i_seekpoint = p_access->info.i_seekpoint;
1862 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1863 if( i_seekpoint_time >= 0 &&
1864 demux_Control( p_demux,
1865 DEMUX_GET_TIME, &i_input_time ) )
1867 if ( i_input_time < i_seekpoint_time + 3000000 )
1873 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1874 i_seekpoint = p_access->info.i_seekpoint + 1;
1876 i_seekpoint = val.i_int;
1878 if( i_seekpoint >= 0 && i_seekpoint <
1879 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1881 input_EsOutChangePosition( p_input->p->p_es_out );
1883 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1885 stream_AccessReset( p_input->p->input.p_stream );
1890 case INPUT_CONTROL_ADD_SLAVE:
1891 if( val.psz_string )
1893 input_source_t *slave = InputSourceNew( p_input );
1895 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1901 msg_Dbg( p_input, "adding %s as slave on the fly",
1905 if( demux_Control( p_input->p->input.p_demux,
1906 DEMUX_GET_TIME, &i_time ) )
1908 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1909 InputSourceClean( slave );
1913 if( demux_Control( slave->p_demux,
1914 DEMUX_SET_TIME, i_time ) )
1916 msg_Err( p_input, "seek failed for new slave" );
1917 InputSourceClean( slave );
1922 /* Get meta (access and demux) */
1923 p_meta = vlc_meta_New();
1924 access_Control( slave->p_access, ACCESS_GET_META,
1926 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1927 InputUpdateMeta( p_input, p_meta );
1929 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1934 msg_Warn( p_input, "failed to add %s as slave",
1938 free( val.psz_string );
1942 case INPUT_CONTROL_SET_BOOKMARK:
1944 msg_Err( p_input, "not yet implemented" );
1948 return b_force_update;
1951 /*****************************************************************************
1953 *****************************************************************************/
1954 static int UpdateFromDemux( input_thread_t *p_input )
1956 demux_t *p_demux = p_input->p->input.p_demux;
1959 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1961 v.i_int = p_demux->info.i_title;
1962 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1964 input_ControlVarTitle( p_input, p_demux->info.i_title );
1966 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1968 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1970 v.i_int = p_demux->info.i_seekpoint;
1971 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1973 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1975 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1977 /* Hmmm only works with master input */
1978 if( p_input->p->input.p_demux == p_demux )
1980 int i_title_end = p_input->p->input.i_title_end -
1981 p_input->p->input.i_title_offset;
1982 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1983 p_input->p->input.i_seekpoint_offset;
1985 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1987 if( p_demux->info.i_title > i_title_end ||
1988 ( p_demux->info.i_title == i_title_end &&
1989 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1991 else if( i_seekpoint_end >=0 )
1993 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1995 else if( i_title_end >= 0 )
1997 if( p_demux->info.i_title > i_title_end ) return 0;
2004 /*****************************************************************************
2006 *****************************************************************************/
2007 static int UpdateFromAccess( input_thread_t *p_input )
2009 access_t *p_access = p_input->p->input.p_access;
2012 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2014 v.i_int = p_access->info.i_title;
2015 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2017 input_ControlVarTitle( p_input, p_access->info.i_title );
2019 stream_AccessUpdate( p_input->p->input.p_stream );
2021 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2023 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2025 v.i_int = p_access->info.i_seekpoint;
2026 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2027 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2029 if( p_access->info.i_update & INPUT_UPDATE_META )
2031 /* TODO maybe multi - access ? */
2032 vlc_meta_t *p_meta = vlc_meta_New();
2033 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2034 InputUpdateMeta( p_input, p_meta );
2035 p_access->info.i_update &= ~INPUT_UPDATE_META;
2038 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2040 /* Hmmm only works with master input */
2041 if( p_input->p->input.p_access == p_access )
2043 int i_title_end = p_input->p->input.i_title_end -
2044 p_input->p->input.i_title_offset;
2045 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2046 p_input->p->input.i_seekpoint_offset;
2048 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2050 if( p_access->info.i_title > i_title_end ||
2051 ( p_access->info.i_title == i_title_end &&
2052 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2054 else if( i_seekpoint_end >=0 )
2056 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2058 else if( i_title_end >= 0 )
2060 if( p_access->info.i_title > i_title_end ) return 0;
2067 /*****************************************************************************
2069 *****************************************************************************/
2070 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2072 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2075 /*****************************************************************************
2077 *****************************************************************************/
2078 static input_source_t *InputSourceNew( input_thread_t *p_input )
2080 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2082 memset( in, 0, sizeof( input_source_t ) );
2086 /*****************************************************************************
2088 *****************************************************************************/
2089 static int InputSourceInit( input_thread_t *p_input,
2090 input_source_t *in, const char *psz_mrl,
2091 const char *psz_forced_demux )
2093 char psz_dup[strlen (psz_mrl) + 1];
2094 const char *psz_access;
2095 const char *psz_demux;
2102 strcpy( psz_dup, psz_mrl );
2104 if( !in ) return VLC_EGENERIC;
2105 if( !p_input ) return VLC_EGENERIC;
2108 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2110 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2111 psz_mrl, psz_access, psz_demux, psz_path );
2112 if( !p_input->b_preparsing )
2114 /* Hack to allow udp://@:port syntax */
2116 (strncmp( psz_access, "udp", 3 ) &&
2117 strncmp( psz_access, "rtp", 3 )) )
2119 /* Find optional titles and seekpoints */
2120 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2121 &in->i_seekpoint_start, &in->i_seekpoint_end );
2124 if( psz_forced_demux && *psz_forced_demux )
2126 psz_demux = psz_forced_demux;
2128 else if( *psz_demux == '\0' )
2130 /* special hack for forcing a demuxer with --demux=module
2131 * (and do nothing with a list) */
2132 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2134 if( psz_var_demux != NULL &&
2135 !strchr(psz_var_demux, ',' ) &&
2136 !strchr(psz_var_demux, ':' ) )
2138 psz_demux = psz_var_demux;
2140 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2144 /* Try access_demux if no demux given */
2145 if( *psz_demux == '\0' )
2147 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2148 NULL, p_input->p->p_es_out, false );
2153 /* Preparsing is only for file:// */
2156 if( !*psz_access ) /* path without scheme:// */
2157 psz_access = "file";
2158 if( strcmp( psz_access, "file" ) )
2160 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2165 int64_t i_pts_delay;
2167 /* Get infos from access_demux */
2168 demux_Control( in->p_demux,
2169 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2170 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2172 in->b_title_demux = true;
2173 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2174 &in->title, &in->i_title,
2175 &in->i_title_offset, &in->i_seekpoint_offset ) )
2180 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2181 &in->b_can_pace_control ) )
2182 in->b_can_pace_control = false;
2184 if( !in->b_can_pace_control )
2186 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2187 &in->b_can_rate_control, &in->b_rescale_ts ) )
2189 in->b_can_rate_control = false;
2190 in->b_rescale_ts = true; /* not used */
2195 in->b_can_rate_control = true;
2196 in->b_rescale_ts = true;
2198 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2199 &in->b_can_pause ) )
2200 in->b_can_pause = false;
2201 var_SetBool( p_input, "can-pause", in->b_can_pause );
2203 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2205 if( ret != VLC_SUCCESS )
2207 var_Set( p_input, "seekable", val );
2211 int64_t i_pts_delay;
2213 input_ChangeState( p_input, OPENING_S );
2215 /* Now try a real access */
2216 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2218 /* Access failed, URL encoded ? */
2219 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2221 decode_URI( psz_path );
2223 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2224 psz_access, psz_demux, psz_path );
2226 in->p_access = access_New( p_input,
2227 psz_access, psz_demux, psz_path );
2229 if( in->p_access == NULL )
2231 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2233 intf_UserFatal( VLC_OBJECT( p_input), false,
2234 _("Your input can't be opened"),
2235 _("VLC is unable to open the MRL '%s'."
2236 " Check the log for details."), psz_mrl );
2241 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2242 while( psz && *psz )
2244 access_t *p_access = in->p_access;
2245 char *end = strchr( psz, ':' );
2250 in->p_access = access_FilterNew( in->p_access, psz );
2251 if( in->p_access == NULL )
2253 in->p_access = p_access;
2254 msg_Warn( p_input, "failed to insert access filter %s",
2262 /* Get infos from access */
2263 if( !p_input->b_preparsing )
2265 access_Control( in->p_access,
2266 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2267 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2269 in->b_title_demux = false;
2270 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2271 &in->title, &in->i_title,
2272 &in->i_title_offset, &in->i_seekpoint_offset ) )
2278 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2279 &in->b_can_pace_control );
2280 in->b_can_rate_control = in->b_can_pace_control;
2281 in->b_rescale_ts = true;
2283 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2285 var_SetBool( p_input, "can-pause", in->b_can_pause );
2286 access_Control( in->p_access, ACCESS_CAN_SEEK,
2288 var_Set( p_input, "seekable", val );
2291 input_ChangeState( p_input, BUFFERING_S );
2293 /* Create the stream_t */
2294 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2295 if( in->p_stream == NULL )
2297 msg_Warn( p_input, "cannot create a stream_t from access" );
2301 /* Open a demuxer */
2302 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2304 psz_demux = in->p_access->psz_demux;
2308 /* Take access redirections into account */
2309 char *psz_real_path;
2310 char *psz_buf = NULL;
2311 if( in->p_access->psz_path )
2313 const char *psz_a, *psz_d;
2314 psz_buf = strdup( in->p_access->psz_path );
2315 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2319 psz_real_path = psz_path;
2321 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2323 in->p_stream, p_input->p->p_es_out,
2324 p_input->b_preparsing );
2328 if( in->p_demux == NULL )
2330 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2331 psz_access, psz_demux, psz_path );
2332 intf_UserFatal( VLC_OBJECT( p_input ), false,
2333 _("VLC can't recognize the input's format"),
2334 _("The format of '%s' cannot be detected. "
2335 "Have a look the log for details."), psz_mrl );
2339 /* Get title from demux */
2340 if( !p_input->b_preparsing && in->i_title <= 0 )
2342 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2343 &in->title, &in->i_title,
2344 &in->i_title_offset, &in->i_seekpoint_offset ))
2346 TAB_INIT( in->i_title, in->title );
2350 in->b_title_demux = true;
2356 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2357 if( 1 || !p_input->b_preparsing )
2360 input_attachment_t **attachment;
2361 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2362 &attachment, &i_attachment ) )
2364 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2365 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2366 i_attachment, attachment );
2367 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2370 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2372 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2374 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2377 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2378 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2383 input_ChangeState( p_input, ERROR_S );
2386 demux_Delete( in->p_demux );
2389 stream_Delete( in->p_stream );
2392 access_Delete( in->p_access );
2394 return VLC_EGENERIC;
2397 /*****************************************************************************
2399 *****************************************************************************/
2400 static void InputSourceClean( input_source_t *in )
2405 demux_Delete( in->p_demux );
2408 stream_Delete( in->p_stream );
2411 access_Delete( in->p_access );
2413 if( in->i_title > 0 )
2415 for( i = 0; i < in->i_title; i++ )
2416 vlc_input_title_Delete( in->title[i] );
2417 TAB_CLEAN( in->i_title, in->title );
2421 static void SlaveDemux( input_thread_t *p_input )
2426 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2428 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2432 for( i = 0; i < p_input->p->i_slave; i++ )
2434 input_source_t *in = p_input->p->slave[i];
2440 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2445 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2447 msg_Err( p_input, "slave[%d] doesn't like "
2448 "DEMUX_GET_TIME -> EOF", i );
2453 if( i_stime >= i_time )
2456 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2462 i_ret = in->p_demux->pf_demux( in->p_demux );
2467 msg_Dbg( p_input, "slave %d EOF", i );
2473 static void SlaveSeek( input_thread_t *p_input )
2478 if( !p_input ) return;
2480 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2482 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2486 for( i = 0; i < p_input->p->i_slave; i++ )
2488 input_source_t *in = p_input->p->slave[i];
2490 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2492 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2498 /*****************************************************************************
2500 *****************************************************************************/
2501 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2505 if( !p_meta ) return;
2507 /* Get meta information from user */
2508 #define GET_META( field, s ) \
2509 var_Get( p_input, (s), &val ); \
2510 if( *val.psz_string ) \
2511 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2512 free( val.psz_string )
2514 GET_META( Title, "meta-title" );
2515 GET_META( Artist, "meta-artist" );
2516 GET_META( Genre, "meta-genre" );
2517 GET_META( Copyright, "meta-copyright" );
2518 GET_META( Description, "meta-description" );
2519 GET_META( Date, "meta-date" );
2520 GET_META( URL, "meta-url" );
2524 /*****************************************************************************
2525 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2526 * arturl and locking issue.
2527 *****************************************************************************/
2528 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2530 input_item_t *p_item = p_input->p->input.p_item;
2531 char * psz_arturl = NULL;
2532 char *psz_title = NULL;
2533 int i_arturl_event = false;
2538 psz_arturl = input_item_GetArtURL( p_item );
2540 vlc_mutex_lock( &p_item->lock );
2541 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2542 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2544 vlc_meta_Merge( p_item->p_meta, p_meta );
2546 if( psz_arturl && *psz_arturl )
2548 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2549 i_arturl_event = true;
2552 vlc_meta_Delete( p_meta );
2554 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2556 /* Don't look for art cover if sout
2557 * XXX It can change when sout has meta data support */
2558 if( p_input->p->p_sout && !p_input->b_preparsing )
2560 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2561 i_arturl_event = true;
2565 input_ExtractAttachmentAndCacheArt( p_input );
2571 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2573 p_meta = vlc_meta_New();
2574 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2576 vlc_mutex_unlock( &p_item->lock );
2578 input_item_SetPreparsed( p_item, true );
2580 if( i_arturl_event == true )
2584 /* Notify interested third parties */
2585 event.type = vlc_InputItemMetaChanged;
2586 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2587 vlc_event_send( &p_item->event_manager, &event );
2592 input_Control( p_input, INPUT_SET_NAME, psz_title );
2596 /** \todo handle sout meta */
2600 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2601 int i_new, input_attachment_t **pp_new )
2603 int i_attachment = *pi_attachment;
2604 input_attachment_t **attachment = *ppp_attachment;
2607 attachment = realloc( attachment,
2608 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2609 for( i = 0; i < i_new; i++ )
2610 attachment[i_attachment++] = pp_new[i];
2614 *pi_attachment = i_attachment;
2615 *ppp_attachment = attachment;
2618 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2622 if( p_input->b_preparsing )
2625 if( p_input->p->input.p_access )
2626 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2629 /* Get meta data from slave input */
2630 for( i = 0; i < p_input->p->i_slave; i++ )
2632 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2633 if( p_input->p->slave[i]->p_access )
2635 access_Control( p_input->p->slave[i]->p_access,
2636 ACCESS_GET_META, p_meta );
2641 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2648 /* XXX I am not sure it is a great idea, besides, there is more than that
2649 * if we want to do it right */
2650 vlc_mutex_lock( &p_item->lock );
2651 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2653 vlc_mutex_unlock( &p_item->lock );
2656 vlc_mutex_unlock( &p_item->lock );
2659 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2660 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2665 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2666 if(! p_demux->p_private )
2669 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2672 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2674 if( p_demux_meta->p_meta )
2676 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2677 vlc_meta_Delete( p_demux_meta->p_meta );
2680 if( p_demux_meta->i_attachments > 0 )
2682 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2683 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2684 p_demux_meta->i_attachments, p_demux_meta->attachments );
2685 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2687 module_Unneed( p_demux, p_id3 );
2689 free( p_demux->p_private );
2693 /*****************************************************************************
2694 * MRLSplit: parse the access, demux and url part of the
2695 * Media Resource Locator.
2696 *****************************************************************************/
2697 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2700 char *psz_access = NULL;
2701 char *psz_demux = NULL;
2704 /* Either there is an access/demux specification before ://
2705 * or we have a plain local file path. */
2706 psz_path = strstr( psz_dup, "://" );
2707 if( psz_path != NULL )
2710 psz_path += 3; /* skips "://" */
2712 /* Separate access from demux (<access>/<demux>://<path>) */
2713 psz_access = psz_dup;
2714 psz_demux = strchr( psz_access, '/' );
2716 *psz_demux++ = '\0';
2718 /* We really don't want module name substitution here! */
2719 if( psz_access[0] == '$' )
2721 if( psz_demux && psz_demux[0] == '$' )
2729 *ppsz_access = psz_access ? psz_access : "";
2730 *ppsz_demux = psz_demux ? psz_demux : "";
2731 *ppsz_path = psz_path ? psz_path : "";
2734 /*****************************************************************************
2735 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2738 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2739 *****************************************************************************/
2740 static void MRLSections( input_thread_t *p_input, char *psz_source,
2741 int *pi_title_start, int *pi_title_end,
2742 int *pi_chapter_start, int *pi_chapter_end )
2744 char *psz, *psz_end, *psz_next, *psz_check;
2746 *pi_title_start = *pi_title_end = -1;
2747 *pi_chapter_start = *pi_chapter_end = -1;
2749 /* Start by parsing titles and chapters */
2750 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2752 /* Check we are really dealing with a title/chapter section */
2753 psz_check = psz + 1;
2754 if( !*psz_check ) return;
2755 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2756 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2757 if( *psz_check == ':' && ++psz_check )
2758 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2759 if( *psz_check != '-' && *psz_check ) return;
2760 if( *psz_check == '-' && ++psz_check )
2761 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2762 if( *psz_check != ':' && *psz_check ) return;
2763 if( *psz_check == ':' && ++psz_check )
2764 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2765 if( *psz_check ) return;
2767 /* Separate start and end */
2769 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2771 /* Look for the start title */
2772 *pi_title_start = strtol( psz, &psz_next, 0 );
2773 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2774 *pi_title_end = *pi_title_start;
2777 /* Look for the start chapter */
2779 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2780 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2781 *pi_chapter_end = *pi_chapter_start;
2785 /* Look for the end title */
2786 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2787 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2790 /* Look for the end chapter */
2791 if( *psz_end ) psz_end++;
2792 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2793 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2796 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2797 psz_source, *pi_title_start, *pi_chapter_start,
2798 *pi_title_end, *pi_chapter_end );
2801 /*****************************************************************************
2802 * input_AddSubtitles: add a subtitles file and enable it
2803 *****************************************************************************/
2804 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2805 bool b_check_extension )
2807 input_source_t *sub;
2810 char *psz_path, *psz_extension;
2812 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2817 /* if we are provided a subtitle.sub file,
2818 * see if we don't have a subtitle.idx and use it instead */
2819 psz_path = strdup( psz_subtitle );
2822 psz_extension = strrchr( psz_path, '.');
2823 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2827 strcpy( psz_extension, ".idx" );
2828 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2829 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2832 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2833 psz_path, psz_subtitle );
2834 strcpy( psz_subtitle, psz_path );
2840 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2842 sub = InputSourceNew( p_input );
2843 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2845 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2848 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2850 if( count.i_int == 0 )
2852 /* if it was first one, there is disable too */
2854 if( count.i_int < list.p_list->i_count )
2856 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2857 &list.p_list->p_values[count.i_int] );
2859 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );