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 *****************************************************************************/
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 sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, bool * );
91 static void SoutKeep( sout_instance_t * );
93 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
94 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta );
95 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
96 int i_new, input_attachment_t **pp_new );
98 /*****************************************************************************
99 * This function creates a new input, and returns a pointer
100 * to its description. On error, it returns NULL.
102 * Variables for _public_ use:
105 * - rate,rate-slower, rate-faster
106 * - position, position-offset
107 * - time, time-offset
108 * - title,title-next,title-prev
109 * - chapter,chapter-next, chapter-prev
110 * - program, audio-es, video-es, spu-es
111 * - audio-delay, spu-delay
116 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
117 * or not, for that check position != 0.0)
119 * * For intf callback upon changes
121 * - rate-change for when playback rate changes
122 * TODO explain when Callback is called
123 * TODO complete this list (?)
124 *****************************************************************************/
125 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
126 const char *psz_header, bool b_quick,
127 sout_instance_t *p_sout )
129 static const char input_name[] = "input";
130 input_thread_t *p_input = NULL; /* thread descriptor */
134 /* Allocate descriptor */
135 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
136 VLC_OBJECT_INPUT, input_name );
137 if( p_input == NULL )
139 msg_Err( p_parent, "out of memory" );
143 /* Construct a nice name for the input timer */
144 char psz_timer_name[255];
145 char * psz_name = input_item_GetName( p_item );
146 snprintf( psz_timer_name, sizeof(psz_timer_name),
147 "input launching for '%s'", psz_name );
150 /* Start a timer to mesure how long it takes
151 * to launch an input */
152 stats_TimerStart( p_input, psz_timer_name,
153 STATS_TIMER_INPUT_LAUNCHING );
155 MALLOC_NULL( p_input->p, input_thread_private_t );
157 /* One "randomly" selected input thread is responsible for computing
158 * the global stats. Check if there is already someone doing this */
159 if( p_input->p_libvlc->p_stats && !b_quick )
161 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
162 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
163 if( priv->p_stats_computer == NULL )
164 priv->p_stats_computer = p_input;
165 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
168 p_input->b_preparsing = b_quick;
169 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
171 /* Init Common fields */
172 p_input->b_eof = false;
173 p_input->b_can_pace_control = true;
174 p_input->p->i_start = 0;
176 p_input->p->i_stop = 0;
177 p_input->p->i_run = 0;
178 p_input->p->i_title = 0;
179 p_input->p->title = NULL;
180 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
181 p_input->i_state = INIT_S;
182 p_input->p->i_rate = INPUT_RATE_DEFAULT;
183 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
184 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
185 p_input->p->p_es_out = NULL;
186 p_input->p->p_sout = NULL;
187 p_input->p->b_owns_its_sout = true;
188 p_input->p->b_sout_keep = false;
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;
296 p_input->p->p_sout = p_sout;
297 p_input->p->b_owns_its_sout = false;
300 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
301 vlc_mutex_init( &p_input->p->counters.counters_lock );
303 /* Attach only once we are ready */
304 vlc_object_attach( p_input, p_parent );
306 /* Set the destructor when we are sure we are initialized */
307 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
313 * Input destructor (called when the object's refcount reaches 0).
315 static void Destructor( input_thread_t * p_input )
317 input_thread_private_t *priv = p_input->p;
319 stats_TimerDump( p_input, STATS_TIMER_INPUT_LAUNCHING );
320 stats_TimerClean( p_input, STATS_TIMER_INPUT_LAUNCHING );
322 if( priv->b_owns_its_sout && priv->p_sout )
324 if( priv->b_sout_keep )
325 SoutKeep( priv->p_sout );
328 sout_DeleteInstance( priv->p_sout );
333 vlc_gc_decref( p_input->p->input.p_item );
335 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
337 vlc_mutex_destroy( &priv->lock_control );
342 * Initialize an input thread and run it. You will need to monitor the
343 * thread to clean up after it is done
345 * \param p_parent a vlc_object
346 * \param p_item an input item
347 * \return a pointer to the spawned input thread
349 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
350 input_item_t *p_item )
353 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
354 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
356 if( !p_input && p_sout )
359 p_input->p->b_sout_keep = b_sout_keep;
364 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
365 input_item_t *p_item,
366 const char *psz_log, sout_instance_t *p_sout )
368 input_thread_t *p_input;
370 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
374 /* Create thread and wait for its readiness. */
375 if( vlc_thread_create( p_input, "input", Run,
376 VLC_THREAD_PRIORITY_INPUT, true ) )
378 input_ChangeState( p_input, ERROR_S );
379 msg_Err( p_input, "cannot create input thread" );
380 vlc_object_detach( p_input );
381 vlc_object_release( p_input );
389 * Initialize an input thread and run it. This thread will clean after himself,
390 * you can forget about it. It can work either in blocking or non-blocking mode
392 * \param p_parent a vlc_object
393 * \param p_item an input item
394 * \param b_block should we block until read is finished ?
395 * \return the input object id if non blocking, an error code else
397 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
401 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
402 input_thread_t *p_input;
404 p_input = Create( p_parent, p_item, NULL, false, p_sout );
405 if( !p_input && p_sout )
410 p_input->p->b_sout_keep = b_sout_keep;
414 RunAndDestroy( p_input );
419 if( vlc_thread_create( p_input, "input", RunAndDestroy,
420 VLC_THREAD_PRIORITY_INPUT, true ) )
422 input_ChangeState( p_input, ERROR_S );
423 msg_Err( p_input, "cannot create input thread" );
424 vlc_object_release( p_input );
428 return p_input->i_object_id;
432 * Initialize an input and initialize it to preparse the item
433 * This function is blocking. It will only accept to parse files
435 * \param p_parent a vlc_object_t
436 * \param p_item an input item
437 * \return VLC_SUCCESS or an error
439 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
441 input_thread_t *p_input;
443 /* Allocate descriptor */
444 p_input = Create( p_parent, p_item, NULL, true, NULL );
448 if( !Init( p_input ) )
451 vlc_object_detach( p_input );
452 vlc_object_release( p_input );
458 * Request a running input thread to stop and die
460 * \param the input thread to stop
462 void input_StopThread( input_thread_t *p_input )
467 /* Set die for input */
468 vlc_object_kill( p_input );
469 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
471 /* We cannot touch p_input fields directly (we come from another thread),
472 * so use the vlc_object_find way, it's perfectly safe */
474 /* Set die for all access */
475 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
476 for( i = 0; i < p_list->i_count; i++ )
478 vlc_object_kill( p_list->p_values[i].p_object );
480 vlc_list_release( p_list );
482 /* Set die for all stream */
483 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
484 for( i = 0; i < p_list->i_count; i++ )
486 vlc_object_kill( p_list->p_values[i].p_object );
488 vlc_list_release( p_list );
490 /* Set die for all demux */
491 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
492 for( i = 0; i < p_list->i_count; i++ )
494 vlc_object_kill( p_list->p_values[i].p_object );
496 vlc_list_release( p_list );
498 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
501 sout_instance_t * input_DetachSout( input_thread_t *p_input )
503 p_input->p->b_owns_its_sout = false;
504 return p_input->p->p_sout;
507 /*****************************************************************************
508 * Run: main thread loop
509 * This is the "normal" thread that spawns the input processing chain,
510 * reads the stream, cleans up and waits
511 *****************************************************************************/
512 static int Run( input_thread_t *p_input )
514 /* Signal that the thread is launched */
515 vlc_thread_ready( p_input );
517 if( Init( p_input ) )
519 /* If we failed, wait before we are killed, and exit */
520 p_input->b_error = true;
522 /* FIXME: we don't want to depend on the playlist */
523 playlist_t * p_playlist = vlc_object_find( p_input,
524 VLC_OBJECT_PLAYLIST, FIND_PARENT );
527 playlist_Signal( p_playlist );
528 vlc_object_release( p_playlist );
533 /* Tell we're dead */
534 p_input->b_dead = true;
541 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
543 /* We have finish to demux data but not to play them */
544 while( !p_input->b_die )
546 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
549 msg_Dbg( p_input, "waiting decoder fifos to empty" );
551 msleep( INPUT_IDLE_SLEEP );
554 /* We have finished */
555 p_input->b_eof = true;
556 playlist_Signal( pl_Get( p_input ) );
559 /* Wait until we are asked to die */
560 if( !p_input->b_die )
571 /*****************************************************************************
572 * RunAndDestroy: main thread loop
573 * This is the "just forget me" thread that spawns the input processing chain,
574 * reads the stream, cleans up and releases memory
575 *****************************************************************************/
576 static int RunAndDestroy( input_thread_t *p_input )
578 /* Signal that the thread is launched */
579 vlc_thread_ready( p_input );
581 if( Init( p_input ) )
586 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
588 /* We have finished demuxing data but not playing it */
589 while( !p_input->b_die )
591 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
594 msg_Dbg( p_input, "waiting decoder fifos to empty" );
596 msleep( INPUT_IDLE_SLEEP );
599 /* We have finished */
600 p_input->b_eof = true;
608 vlc_object_release( p_input );
612 /*****************************************************************************
613 * Main loop: Fill buffers from access, and demux
614 *****************************************************************************/
615 static void MainLoop( input_thread_t *p_input )
617 int64_t i_start_mdate = mdate();
618 int64_t i_intf_update = 0;
622 stats_TimerStop( p_input, STATS_TIMER_INPUT_LAUNCHING );
624 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
626 bool b_force_update = false;
632 if( p_input->i_state != PAUSE_S )
634 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
635 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
638 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
643 if( p_input->p->input.b_title_demux &&
644 p_input->p->input.p_demux->info.i_update )
646 i_ret = UpdateFromDemux( p_input );
647 b_force_update = true;
649 else if( !p_input->p->input.b_title_demux &&
650 p_input->p->input.p_access &&
651 p_input->p->input.p_access->info.i_update )
653 i_ret = UpdateFromAccess( p_input );
654 b_force_update = true;
658 if( i_ret == 0 ) /* EOF */
662 var_Get( p_input, "input-repeat", &repeat );
663 if( repeat.i_int == 0 )
665 /* End of file - we do not set b_die because only the
666 * playlist is allowed to do so. */
667 input_ChangeState( p_input, END_S );
668 msg_Dbg( p_input, "EOF reached" );
669 p_input->p->input.b_eof = true;
673 msg_Dbg( p_input, "repeating the same input (%d)",
675 if( repeat.i_int > 0 )
678 var_Set( p_input, "input-repeat", repeat );
681 /* Seek to start title/seekpoint */
682 val.i_int = p_input->p->input.i_title_start -
683 p_input->p->input.i_title_offset;
684 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
686 input_ControlPush( p_input,
687 INPUT_CONTROL_SET_TITLE, &val );
689 val.i_int = p_input->p->input.i_seekpoint_start -
690 p_input->p->input.i_seekpoint_offset;
691 if( val.i_int > 0 /* TODO: check upper boundary */ )
692 input_ControlPush( p_input,
693 INPUT_CONTROL_SET_SEEKPOINT, &val );
695 /* Seek to start position */
696 if( p_input->p->i_start > 0 )
698 val.i_time = p_input->p->i_start;
699 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
705 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
710 i_start_mdate = mdate();
715 p_input->b_error = true;
718 if( i_ret > 0 && p_input->p->i_slave > 0 )
720 SlaveDemux( p_input );
730 vlc_mutex_lock( &p_input->p->lock_control );
731 ControlReduce( p_input );
732 while( !ControlPopNoLock( p_input, &i_type, &val ) )
734 msg_Dbg( p_input, "control type=%d", i_type );
735 if( Control( p_input, i_type, val ) )
736 b_force_update = true;
738 vlc_mutex_unlock( &p_input->p->lock_control );
740 if( b_force_update || i_intf_update < mdate() )
744 int64_t i_time, i_length;
745 /* update input status variables */
746 if( !demux_Control( p_input->p->input.p_demux,
747 DEMUX_GET_POSITION, &f_pos ) )
749 val.f_float = (float)f_pos;
750 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
752 if( !demux_Control( p_input->p->input.p_demux,
753 DEMUX_GET_TIME, &i_time ) )
755 p_input->i_time = i_time;
757 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
759 if( !demux_Control( p_input->p->input.p_demux,
760 DEMUX_GET_LENGTH, &i_length ) )
763 var_Get( p_input, "length", &old_val );
764 val.i_time = i_length;
765 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
767 if( old_val.i_time != val.i_time )
769 UpdateItemLength( p_input, i_length );
773 var_SetBool( p_input, "intf-change", true );
774 i_intf_update = mdate() + INT64_C(150000);
776 /* 150ms * 8 = ~ 1 second */
777 if( ++i_updates % 8 == 0 )
779 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
780 /* Are we the thread responsible for computing global stats ? */
781 if( libvlc_priv (p_input->p_libvlc)->p_stats_computer == p_input )
783 stats_ComputeGlobalStats( p_input->p_libvlc,
784 p_input->p_libvlc->p_stats );
790 static void InitStatistics( input_thread_t * p_input )
792 if( p_input->b_preparsing ) return;
794 /* Prepare statistics */
795 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
796 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
797 if( libvlc_stats (p_input) )
799 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
800 INIT_COUNTER( read_packets, INTEGER, COUNTER );
801 INIT_COUNTER( demux_read, INTEGER, COUNTER );
802 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
803 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
804 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
805 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
806 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
807 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
808 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
809 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
810 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
811 p_input->p->counters.p_sout_send_bitrate = NULL;
812 p_input->p->counters.p_sout_sent_packets = NULL;
813 p_input->p->counters.p_sout_sent_bytes = NULL;
814 if( p_input->p->counters.p_demux_bitrate )
815 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
816 if( p_input->p->counters.p_input_bitrate )
817 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
822 static int InitSout( input_thread_t * p_input )
826 if( p_input->b_preparsing ) return VLC_SUCCESS;
828 /* Find a usable sout and attach it to p_input */
829 psz = var_GetNonEmptyString( p_input, "sout" );
830 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
832 /* Check the validity of the provided sout */
833 if( p_input->p->p_sout )
835 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
837 msg_Dbg( p_input, "destroying unusable sout" );
839 sout_DeleteInstance( p_input->p->p_sout );
840 p_input->p->p_sout = NULL;
844 if( p_input->p->p_sout )
847 msg_Dbg( p_input, "sout keep: reusing sout" );
848 msg_Dbg( p_input, "sout keep: you probably want to use "
849 "gather stream_out" );
850 vlc_object_attach( p_input->p->p_sout, p_input );
854 /* Create a new one */
855 p_input->p->p_sout = sout_NewInstance( p_input, psz );
856 if( !p_input->p->p_sout )
858 input_ChangeState( p_input, ERROR_S );
859 msg_Err( p_input, "cannot start stream output instance, " \
865 if( libvlc_stats (p_input) )
867 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
868 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
869 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
870 if( p_input->p->counters.p_sout_send_bitrate )
871 p_input->p->counters.p_sout_send_bitrate->update_interval =
875 else if( p_input->p->p_sout )
877 msg_Dbg( p_input, "destroying useless sout" );
879 sout_DeleteInstance( p_input->p->p_sout );
880 p_input->p->p_sout = NULL;
888 static void InitTitle( input_thread_t * p_input )
892 if( p_input->b_preparsing ) return;
894 /* Create global title (from master) */
895 p_input->p->i_title = p_input->p->input.i_title;
896 p_input->p->title = p_input->p->input.title;
897 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
898 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
899 if( p_input->p->i_title > 0 )
901 /* Setup variables */
902 input_ControlVarNavigation( p_input );
903 input_ControlVarTitle( p_input, 0 );
907 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
908 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
909 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
912 if( p_input->i_pts_delay < 0 )
913 p_input->i_pts_delay = 0;
915 /* If the desynchronisation requested by the user is < 0, we need to
916 * cache more data. */
917 var_Get( p_input, "audio-desync", &val );
918 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
920 /* Update cr_average depending on the caching */
921 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
922 p_input->p->input.i_cr_average /= 10;
923 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
926 static void StartTitle( input_thread_t * p_input )
935 /* Start title/chapter */
937 if( p_input->b_preparsing )
939 p_input->p->i_start = 0;
943 val.i_int = p_input->p->input.i_title_start -
944 p_input->p->input.i_title_offset;
945 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
946 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
947 val.i_int = p_input->p->input.i_seekpoint_start -
948 p_input->p->input.i_seekpoint_offset;
949 if( val.i_int > 0 /* TODO: check upper boundary */ )
950 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
954 p_input->p->i_start = INT64_C(1000000) * var_GetInteger( p_input, "start-time" );
955 p_input->p->i_stop = INT64_C(1000000) * var_GetInteger( p_input, "stop-time" );
956 p_input->p->i_run = INT64_C(1000000) * var_GetInteger( p_input, "run-time" );
957 i_length = var_GetTime( p_input, "length" );
958 if( p_input->p->i_run < 0 )
960 msg_Warn( p_input, "invalid run-time ignored" );
961 p_input->p->i_run = 0;
964 if( p_input->p->i_start > 0 )
966 if( p_input->p->i_start >= i_length )
968 msg_Warn( p_input, "invalid start-time ignored" );
974 msg_Dbg( p_input, "starting at time: %ds",
975 (int)( p_input->p->i_start / INT64_C(1000000) ) );
977 s.i_time = p_input->p->i_start;
978 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
981 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
983 msg_Warn( p_input, "invalid stop-time ignored" );
984 p_input->p->i_stop = 0;
988 /* Get fps and set it if not already set */
989 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
992 float f_requested_fps;
994 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
995 var_SetFloat( p_input, "sub-original-fps", f_fps );
997 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
998 if( f_requested_fps != f_fps )
1000 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1001 VLC_VAR_DOINHERIT );
1002 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1006 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1009 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1012 /* Look for and add subtitle files */
1013 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1014 if( psz_subtitle != NULL )
1016 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1017 input_AddSubtitles( p_input, psz_subtitle, false );
1020 var_Get( p_input, "sub-autodetect-file", &val );
1023 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1024 char **subs = subtitles_Detect( p_input, psz_autopath,
1025 p_input->p->input.p_item->psz_uri );
1026 input_source_t *sub;
1028 if( psz_autopath == NULL )
1029 psz_autopath = strdup("");
1031 /* Try to autoselect the first autodetected subtitles file
1032 * if no subtitles file was specified */
1033 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1035 input_AddSubtitles( p_input, subs[0], false );
1040 /* Then, just add the following subtitles files */
1041 for( ; subs && subs[i]; i++ )
1043 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1045 sub = InputSourceNew( p_input );
1046 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1048 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1055 free( psz_autopath );
1057 free( psz_subtitle );
1059 /* Look for slave */
1060 psz = var_GetNonEmptyString( p_input, "input-slave" );
1064 input_source_t *slave;
1065 while( psz && *psz )
1067 while( *psz == ' ' || *psz == '#' )
1071 if( ( psz_delim = strchr( psz, '#' ) ) )
1073 *psz_delim++ = '\0';
1080 msg_Dbg( p_input, "adding slave input '%s'", psz );
1081 slave = InputSourceNew( p_input );
1082 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1084 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1093 static void InitPrograms( input_thread_t * p_input )
1098 if( p_input->b_preparsing ) return;
1101 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1102 i_es_out_mode = ES_OUT_MODE_AUTO;
1104 if( p_input->p->p_sout )
1106 var_Get( p_input, "sout-all", &val );
1109 i_es_out_mode = ES_OUT_MODE_ALL;
1114 var_Get( p_input, "programs", &val );
1115 if ( val.p_list && val.p_list->i_count )
1117 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1118 /* Note : we should remove the "program" callback. */
1121 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1125 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1127 /* Inform the demuxer about waited group (needed only for DVB) */
1128 if( i_es_out_mode == ES_OUT_MODE_ALL )
1130 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1132 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1134 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1139 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1140 (int) var_GetInteger( p_input, "program" ), NULL );
1144 static int Init( input_thread_t * p_input )
1150 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
1152 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
1154 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
1155 var_SetString( p_input, "sout", "" );
1156 var_SetBool( p_input, "sout-all", false );
1157 var_SetString( p_input, "input-slave", "" );
1158 var_SetInteger( p_input, "input-repeat", 0 );
1159 var_SetString( p_input, "sub-file", "" );
1160 var_SetBool( p_input, "sub-autodetect-file", false );
1164 InitStatistics( p_input );
1166 ret = InitSout( p_input );
1167 if( ret != VLC_SUCCESS )
1168 return ret; /* FIXME: goto error; should be better here */
1172 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
1173 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
1174 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
1176 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
1177 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
1179 if( InputSourceInit( p_input, &p_input->p->input,
1180 p_input->p->input.p_item->psz_uri, NULL ) )
1185 InitTitle( p_input );
1187 /* Load master infos */
1189 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
1190 &val.i_time ) && val.i_time > 0 )
1192 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1193 UpdateItemLength( p_input, val.i_time );
1197 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
1198 if( val.i_time > 0 )
1199 { /* fallback: gets length from metadata */
1200 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
1201 UpdateItemLength( p_input, val.i_time );
1205 StartTitle( p_input );
1207 InitPrograms( p_input );
1209 if( !p_input->b_preparsing && p_input->p->p_sout )
1211 p_input->p->b_out_pace_control = (p_input->p->p_sout->i_out_pace_nocontrol > 0);
1213 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1215 /* We don't want a high input priority here or we'll
1216 * end-up sucking up all the CPU time */
1217 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1220 msg_Dbg( p_input, "starting in %s mode",
1221 p_input->p->b_out_pace_control ? "async" : "sync" );
1224 p_meta = vlc_meta_New();
1226 /* Get meta data from users */
1227 InputMetaUser( p_input, p_meta );
1229 /* Get meta data from master input */
1230 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1232 /* Access_file does not give any meta, and there are no slave */
1233 AccessMeta( p_input, p_meta );
1235 InputUpdateMeta( p_input, p_meta );
1237 if( !p_input->b_preparsing )
1239 msg_Dbg( p_input, "`%s' successfully opened",
1240 p_input->p->input.p_item->psz_uri );
1244 /* initialization is complete */
1245 input_ChangeState( p_input, PLAYING_S );
1250 input_ChangeState( p_input, ERROR_S );
1252 if( p_input->p->p_es_out )
1253 input_EsOutDelete( p_input->p->p_es_out );
1255 if( p_input->p->p_sout )
1257 vlc_object_detach( p_input->p->p_sout );
1258 sout_DeleteInstance( p_input->p->p_sout );
1262 if( !p_input->b_preparsing && libvlc_stats (p_input) )
1264 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1265 stats_CounterClean( p_input->p->counters.p_##c );\
1266 p_input->p->counters.p_##c = NULL; } while(0)
1267 EXIT_COUNTER( read_bytes );
1268 EXIT_COUNTER( read_packets );
1269 EXIT_COUNTER( demux_read );
1270 EXIT_COUNTER( input_bitrate );
1271 EXIT_COUNTER( demux_bitrate );
1272 EXIT_COUNTER( played_abuffers );
1273 EXIT_COUNTER( lost_abuffers );
1274 EXIT_COUNTER( displayed_pictures );
1275 EXIT_COUNTER( lost_pictures );
1276 EXIT_COUNTER( decoded_audio );
1277 EXIT_COUNTER( decoded_video );
1278 EXIT_COUNTER( decoded_sub );
1280 if( p_input->p->p_sout )
1282 EXIT_COUNTER( sout_sent_packets );
1283 EXIT_COUNTER (sout_sent_bytes );
1284 EXIT_COUNTER( sout_send_bitrate );
1289 /* Mark them deleted */
1290 p_input->p->input.p_demux = NULL;
1291 p_input->p->input.p_stream = NULL;
1292 p_input->p->input.p_access = NULL;
1293 p_input->p->p_es_out = NULL;
1294 p_input->p->p_sout = NULL;
1296 return VLC_EGENERIC;
1299 /*****************************************************************************
1300 * Error: RunThread() error loop
1301 *****************************************************************************
1302 * This function is called when an error occurred during thread main's loop.
1303 *****************************************************************************/
1304 static void Error( input_thread_t *p_input )
1306 input_ChangeState( p_input, ERROR_S );
1307 while( !p_input->b_die )
1310 msleep( INPUT_IDLE_SLEEP );
1314 /*****************************************************************************
1315 * End: end the input thread
1316 *****************************************************************************/
1317 static void End( input_thread_t * p_input )
1321 /* We are at the end */
1322 input_ChangeState( p_input, END_S );
1324 /* Clean control variables */
1325 input_ControlVarClean( p_input );
1327 /* Clean up master */
1328 InputSourceClean( &p_input->p->input );
1331 for( i = 0; i < p_input->p->i_slave; i++ )
1333 InputSourceClean( p_input->p->slave[i] );
1334 free( p_input->p->slave[i] );
1336 free( p_input->p->slave );
1338 /* Unload all modules */
1339 if( p_input->p->p_es_out )
1340 input_EsOutDelete( p_input->p->p_es_out );
1342 if( !p_input->b_preparsing )
1344 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1345 if( libvlc_stats (p_input) )
1347 libvlc_priv_t *priv = libvlc_priv (p_input->p_libvlc);
1349 /* make sure we are up to date */
1350 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1351 if( priv->p_stats_computer == p_input )
1353 stats_ComputeGlobalStats( p_input->p_libvlc,
1354 p_input->p_libvlc->p_stats );
1355 priv->p_stats_computer = NULL;
1357 CL_CO( read_bytes );
1358 CL_CO( read_packets );
1359 CL_CO( demux_read );
1360 CL_CO( input_bitrate );
1361 CL_CO( demux_bitrate );
1362 CL_CO( played_abuffers );
1363 CL_CO( lost_abuffers );
1364 CL_CO( displayed_pictures );
1365 CL_CO( lost_pictures );
1366 CL_CO( decoded_audio) ;
1367 CL_CO( decoded_video );
1368 CL_CO( decoded_sub) ;
1371 /* Close optional stream output instance */
1372 if( p_input->p->p_sout )
1374 CL_CO( sout_sent_packets );
1375 CL_CO( sout_sent_bytes );
1376 CL_CO( sout_send_bitrate );
1378 vlc_object_detach( p_input->p->p_sout );
1383 if( p_input->p->i_attachment > 0 )
1385 for( i = 0; i < p_input->p->i_attachment; i++ )
1386 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1387 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1390 /* Tell we're dead */
1391 p_input->b_dead = true;
1394 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, bool *pb_sout_keep )
1396 bool b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1397 sout_instance_t *p_sout = NULL;
1400 /* Search sout-keep options
1401 * XXX it has to be done here, but it is duplicated work :( */
1402 vlc_mutex_lock( &p_item->lock );
1403 for( i = 0; i < p_item->i_options; i++ )
1405 const char *psz_option = p_item->ppsz_options[i];
1408 if( *psz_option == ':' )
1411 if( !strcmp( psz_option, "sout-keep" ) )
1413 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1414 b_keep_sout = false;
1416 vlc_mutex_unlock( &p_item->lock );
1418 /* Find a potential sout to reuse
1419 * XXX it might be unusable but this will be checked later */
1422 p_sout = vlc_object_find( p_parent->p_libvlc, VLC_OBJECT_SOUT,
1426 if( p_sout->p_parent != VLC_OBJECT( p_sout->p_libvlc ) )
1428 vlc_object_release( p_sout );
1433 vlc_object_detach( p_sout ); /* Remove it from the GC */
1434 vlc_object_release( p_sout );
1440 *pb_sout_keep = b_keep_sout;
1445 static void SoutKeep( sout_instance_t *p_sout )
1447 msg_Dbg( p_sout, "sout has been kept" );
1448 vlc_object_attach( p_sout, p_sout->p_libvlc );
1451 /*****************************************************************************
1453 *****************************************************************************/
1454 static inline int ControlPopNoLock( input_thread_t *p_input,
1455 int *pi_type, vlc_value_t *p_val )
1457 if( p_input->p->i_control <= 0 )
1459 return VLC_EGENERIC;
1462 *pi_type = p_input->p->control[0].i_type;
1463 *p_val = p_input->p->control[0].val;
1465 p_input->p->i_control--;
1466 if( p_input->p->i_control > 0 )
1470 for( i = 0; i < p_input->p->i_control; i++ )
1472 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1473 p_input->p->control[i].val = p_input->p->control[i+1].val;
1480 static void ControlReduce( input_thread_t *p_input )
1487 for( i = 1; i < p_input->p->i_control; i++ )
1489 const int i_lt = p_input->p->control[i-1].i_type;
1490 const int i_ct = p_input->p->control[i].i_type;
1492 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1493 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1497 ( i_ct == INPUT_CONTROL_SET_STATE ||
1498 i_ct == INPUT_CONTROL_SET_RATE ||
1499 i_ct == INPUT_CONTROL_SET_POSITION ||
1500 i_ct == INPUT_CONTROL_SET_TIME ||
1501 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1502 i_ct == INPUT_CONTROL_SET_TITLE ||
1503 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1504 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1507 // msg_Dbg( p_input, "merged at %d", i );
1508 /* Remove the i-1 */
1509 for( j = i; j < p_input->p->i_control; j++ )
1510 p_input->p->control[j-1] = p_input->p->control[j];
1511 p_input->p->i_control--;
1515 /* TODO but that's not that important
1516 - merge SET_X with SET_X_CMD
1517 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1518 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1525 static bool Control( input_thread_t *p_input, int i_type,
1528 bool b_force_update = false;
1530 if( !p_input ) return b_force_update;
1534 case INPUT_CONTROL_SET_DIE:
1535 msg_Dbg( p_input, "control: stopping input" );
1536 /* Mark all submodules to die */
1537 if( p_input->p->input.p_access )
1538 vlc_object_kill( p_input->p->input.p_access );
1539 if( p_input->p->input.p_stream )
1540 vlc_object_kill( p_input->p->input.p_stream );
1541 vlc_object_kill( p_input->p->input.p_demux );
1543 vlc_object_kill( p_input );
1546 case INPUT_CONTROL_SET_POSITION:
1547 case INPUT_CONTROL_SET_POSITION_OFFSET:
1550 if( i_type == INPUT_CONTROL_SET_POSITION )
1552 f_pos = val.f_float;
1556 /* Should not fail */
1557 demux_Control( p_input->p->input.p_demux,
1558 DEMUX_GET_POSITION, &f_pos );
1559 f_pos += val.f_float;
1561 if( f_pos < 0.0 ) f_pos = 0.0;
1562 if( f_pos > 1.0 ) f_pos = 1.0;
1563 /* Reset the decoders states and clock sync (before calling the demuxer */
1564 input_EsOutChangePosition( p_input->p->p_es_out );
1565 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1568 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1569 "%2.1f%% failed", f_pos * 100 );
1573 if( p_input->p->i_slave > 0 )
1574 SlaveSeek( p_input );
1576 b_force_update = true;
1581 case INPUT_CONTROL_SET_TIME:
1582 case INPUT_CONTROL_SET_TIME_OFFSET:
1587 if( i_type == INPUT_CONTROL_SET_TIME )
1589 i_time = val.i_time;
1593 /* Should not fail */
1594 demux_Control( p_input->p->input.p_demux,
1595 DEMUX_GET_TIME, &i_time );
1596 i_time += val.i_time;
1598 if( i_time < 0 ) i_time = 0;
1600 /* Reset the decoders states and clock sync (before calling the demuxer */
1601 input_EsOutChangePosition( p_input->p->p_es_out );
1603 i_ret = demux_Control( p_input->p->input.p_demux,
1604 DEMUX_SET_TIME, i_time );
1609 /* Emulate it with a SET_POS */
1610 demux_Control( p_input->p->input.p_demux,
1611 DEMUX_GET_LENGTH, &i_length );
1614 double f_pos = (double)i_time / (double)i_length;
1615 i_ret = demux_Control( p_input->p->input.p_demux,
1616 DEMUX_SET_POSITION, f_pos );
1621 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) %"PRId64
1622 " failed or not possible", i_time );
1626 if( p_input->p->i_slave > 0 )
1627 SlaveSeek( p_input );
1629 b_force_update = true;
1634 case INPUT_CONTROL_SET_STATE:
1635 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1636 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1639 if( p_input->p->input.p_access )
1640 i_ret = access_Control( p_input->p->input.p_access,
1641 ACCESS_SET_PAUSE_STATE, false );
1643 i_ret = demux_Control( p_input->p->input.p_demux,
1644 DEMUX_SET_PAUSE_STATE, false );
1648 /* FIXME What to do ? */
1649 msg_Warn( p_input, "cannot unset pause -> EOF" );
1650 vlc_mutex_unlock( &p_input->p->lock_control );
1651 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1652 vlc_mutex_lock( &p_input->p->lock_control );
1655 b_force_update = true;
1657 /* Switch to play */
1658 p_input->i_state = PLAYING_S;
1659 val.i_int = PLAYING_S;
1660 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1664 input_EsOutChangeState( p_input->p->p_es_out );
1666 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1667 p_input->p->b_can_pause )
1670 if( p_input->p->input.p_access )
1671 i_ret = access_Control( p_input->p->input.p_access,
1672 ACCESS_SET_PAUSE_STATE, true );
1674 i_ret = demux_Control( p_input->p->input.p_demux,
1675 DEMUX_SET_PAUSE_STATE, true );
1677 b_force_update = true;
1681 msg_Warn( p_input, "cannot set pause state" );
1682 val.i_int = p_input->i_state;
1686 val.i_int = PAUSE_S;
1689 /* Switch to new state */
1690 p_input->i_state = val.i_int;
1691 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1695 input_EsOutChangeState( p_input->p->p_es_out );
1697 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1699 b_force_update = true;
1701 /* Correct "state" value */
1702 val.i_int = p_input->i_state;
1703 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1705 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1707 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1711 case INPUT_CONTROL_SET_RATE:
1712 case INPUT_CONTROL_SET_RATE_SLOWER:
1713 case INPUT_CONTROL_SET_RATE_FASTER:
1717 if( i_type == INPUT_CONTROL_SET_RATE )
1723 static const int ppi_factor[][2] = {
1724 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1726 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1735 for( i = 0; ppi_factor[i][0] != 0; i++ )
1737 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1738 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1739 if( i_test_e < i_error )
1745 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1747 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1749 if( ppi_factor[i_idx+1][0] > 0 )
1750 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1752 i_rate = INPUT_RATE_MAX+1;
1756 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1758 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1760 i_rate = INPUT_RATE_MIN-1;
1764 if( i_rate < INPUT_RATE_MIN )
1766 msg_Dbg( p_input, "cannot set rate faster" );
1767 i_rate = INPUT_RATE_MIN;
1769 else if( i_rate > INPUT_RATE_MAX )
1771 msg_Dbg( p_input, "cannot set rate slower" );
1772 i_rate = INPUT_RATE_MAX;
1774 if( i_rate != INPUT_RATE_DEFAULT &&
1775 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1776 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1778 msg_Dbg( p_input, "cannot change rate" );
1779 i_rate = INPUT_RATE_DEFAULT;
1781 if( i_rate != p_input->p->i_rate &&
1782 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1785 if( p_input->p->input.p_access )
1786 i_ret = VLC_EGENERIC;
1788 i_ret = demux_Control( p_input->p->input.p_demux,
1789 DEMUX_SET_RATE, &i_rate );
1792 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1793 i_rate = p_input->p->i_rate;
1798 if( i_rate != p_input->p->i_rate )
1801 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1802 var_SetBool( p_input, "rate-change", true );
1804 p_input->p->i_rate = i_rate;
1806 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1807 if( p_input->p->input.b_rescale_ts )
1808 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1810 b_force_update = true;
1815 case INPUT_CONTROL_SET_PROGRAM:
1816 /* No need to force update, es_out does it if needed */
1817 es_out_Control( p_input->p->p_es_out,
1818 ES_OUT_SET_GROUP, val.i_int );
1820 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1824 case INPUT_CONTROL_SET_ES:
1825 /* No need to force update, es_out does it if needed */
1826 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1827 input_EsOutGetFromID( p_input->p->p_es_out,
1831 case INPUT_CONTROL_SET_AUDIO_DELAY:
1832 input_EsOutSetDelay( p_input->p->p_es_out,
1833 AUDIO_ES, val.i_time );
1834 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1837 case INPUT_CONTROL_SET_SPU_DELAY:
1838 input_EsOutSetDelay( p_input->p->p_es_out,
1839 SPU_ES, val.i_time );
1840 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1843 case INPUT_CONTROL_SET_TITLE:
1844 case INPUT_CONTROL_SET_TITLE_NEXT:
1845 case INPUT_CONTROL_SET_TITLE_PREV:
1846 if( p_input->p->input.b_title_demux &&
1847 p_input->p->input.i_title > 0 )
1850 /* FIXME handle demux title */
1851 demux_t *p_demux = p_input->p->input.p_demux;
1854 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1855 i_title = p_demux->info.i_title - 1;
1856 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1857 i_title = p_demux->info.i_title + 1;
1859 i_title = val.i_int;
1861 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1863 input_EsOutChangePosition( p_input->p->p_es_out );
1865 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1866 input_ControlVarTitle( p_input, i_title );
1869 else if( p_input->p->input.i_title > 0 )
1871 access_t *p_access = p_input->p->input.p_access;
1874 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1875 i_title = p_access->info.i_title - 1;
1876 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1877 i_title = p_access->info.i_title + 1;
1879 i_title = val.i_int;
1881 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1883 input_EsOutChangePosition( p_input->p->p_es_out );
1885 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1886 stream_AccessReset( p_input->p->input.p_stream );
1890 case INPUT_CONTROL_SET_SEEKPOINT:
1891 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1892 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1893 if( p_input->p->input.b_title_demux &&
1894 p_input->p->input.i_title > 0 )
1896 demux_t *p_demux = p_input->p->input.p_demux;
1898 int64_t i_input_time;
1899 int64_t i_seekpoint_time;
1901 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1903 i_seekpoint = p_demux->info.i_seekpoint;
1904 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1905 if( i_seekpoint_time >= 0 &&
1906 !demux_Control( p_demux,
1907 DEMUX_GET_TIME, &i_input_time ) )
1909 if ( i_input_time < i_seekpoint_time + 3000000 )
1915 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1916 i_seekpoint = p_demux->info.i_seekpoint + 1;
1918 i_seekpoint = val.i_int;
1920 if( i_seekpoint >= 0 && i_seekpoint <
1921 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1924 input_EsOutChangePosition( p_input->p->p_es_out );
1926 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1929 else if( p_input->p->input.i_title > 0 )
1931 demux_t *p_demux = p_input->p->input.p_demux;
1932 access_t *p_access = p_input->p->input.p_access;
1934 int64_t i_input_time;
1935 int64_t i_seekpoint_time;
1937 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1939 i_seekpoint = p_access->info.i_seekpoint;
1940 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1941 if( i_seekpoint_time >= 0 &&
1942 demux_Control( p_demux,
1943 DEMUX_GET_TIME, &i_input_time ) )
1945 if ( i_input_time < i_seekpoint_time + 3000000 )
1951 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1952 i_seekpoint = p_access->info.i_seekpoint + 1;
1954 i_seekpoint = val.i_int;
1956 if( i_seekpoint >= 0 && i_seekpoint <
1957 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1959 input_EsOutChangePosition( p_input->p->p_es_out );
1961 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1963 stream_AccessReset( p_input->p->input.p_stream );
1968 case INPUT_CONTROL_ADD_SLAVE:
1969 if( val.psz_string )
1971 input_source_t *slave = InputSourceNew( p_input );
1973 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1979 msg_Dbg( p_input, "adding %s as slave on the fly",
1983 if( demux_Control( p_input->p->input.p_demux,
1984 DEMUX_GET_TIME, &i_time ) )
1986 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1987 InputSourceClean( slave );
1991 if( demux_Control( slave->p_demux,
1992 DEMUX_SET_TIME, i_time ) )
1994 msg_Err( p_input, "seek failed for new slave" );
1995 InputSourceClean( slave );
2000 /* Get meta (access and demux) */
2001 p_meta = vlc_meta_New();
2002 access_Control( slave->p_access, ACCESS_GET_META,
2004 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
2005 InputUpdateMeta( p_input, p_meta );
2007 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
2012 msg_Warn( p_input, "failed to add %s as slave",
2016 free( val.psz_string );
2020 case INPUT_CONTROL_SET_BOOKMARK:
2022 msg_Err( p_input, "not yet implemented" );
2026 return b_force_update;
2029 /*****************************************************************************
2031 *****************************************************************************/
2032 static int UpdateFromDemux( input_thread_t *p_input )
2034 demux_t *p_demux = p_input->p->input.p_demux;
2037 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2039 v.i_int = p_demux->info.i_title;
2040 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2042 input_ControlVarTitle( p_input, p_demux->info.i_title );
2044 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2046 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2048 v.i_int = p_demux->info.i_seekpoint;
2049 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2051 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2053 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2055 /* Hmmm only works with master input */
2056 if( p_input->p->input.p_demux == p_demux )
2058 int i_title_end = p_input->p->input.i_title_end -
2059 p_input->p->input.i_title_offset;
2060 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2061 p_input->p->input.i_seekpoint_offset;
2063 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2065 if( p_demux->info.i_title > i_title_end ||
2066 ( p_demux->info.i_title == i_title_end &&
2067 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2069 else if( i_seekpoint_end >=0 )
2071 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2073 else if( i_title_end >= 0 )
2075 if( p_demux->info.i_title > i_title_end ) return 0;
2082 /*****************************************************************************
2084 *****************************************************************************/
2085 static int UpdateFromAccess( input_thread_t *p_input )
2087 access_t *p_access = p_input->p->input.p_access;
2090 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2092 v.i_int = p_access->info.i_title;
2093 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2095 input_ControlVarTitle( p_input, p_access->info.i_title );
2097 stream_AccessUpdate( p_input->p->input.p_stream );
2099 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2101 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2103 v.i_int = p_access->info.i_seekpoint;
2104 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2105 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2107 if( p_access->info.i_update & INPUT_UPDATE_META )
2109 /* TODO maybe multi - access ? */
2110 vlc_meta_t *p_meta = vlc_meta_New();
2111 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2112 InputUpdateMeta( p_input, p_meta );
2113 var_SetInteger( pl_Get( p_input ), "item-change", p_input->p->input.p_item->i_id );
2114 p_access->info.i_update &= ~INPUT_UPDATE_META;
2117 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2119 /* Hmmm only works with master input */
2120 if( p_input->p->input.p_access == p_access )
2122 int i_title_end = p_input->p->input.i_title_end -
2123 p_input->p->input.i_title_offset;
2124 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2125 p_input->p->input.i_seekpoint_offset;
2127 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2129 if( p_access->info.i_title > i_title_end ||
2130 ( p_access->info.i_title == i_title_end &&
2131 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2133 else if( i_seekpoint_end >=0 )
2135 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2137 else if( i_title_end >= 0 )
2139 if( p_access->info.i_title > i_title_end ) return 0;
2146 /*****************************************************************************
2148 *****************************************************************************/
2149 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2151 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2153 if( !p_input->b_preparsing )
2155 pl_Yield( p_input );
2156 var_SetInteger( pl_Get( p_input ), "item-change",
2157 p_input->p->input.p_item->i_id );
2158 pl_Release( p_input );
2162 /*****************************************************************************
2164 *****************************************************************************/
2165 static input_source_t *InputSourceNew( input_thread_t *p_input )
2167 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2171 msg_Err( p_input, "out of memory for new input source" );
2176 in->p_access = NULL;
2177 in->p_stream = NULL;
2179 in->b_title_demux = false;
2180 TAB_INIT( in->i_title, in->title );
2181 in->b_can_pause = true;
2182 in->b_can_pace_control = true;
2183 in->b_can_rate_control = true;
2184 in->b_rescale_ts = true;
2187 in->i_cr_average = 0;
2192 /*****************************************************************************
2194 *****************************************************************************/
2195 static int InputSourceInit( input_thread_t *p_input,
2196 input_source_t *in, const char *psz_mrl,
2197 const char *psz_forced_demux )
2199 char psz_dup[strlen (psz_mrl) + 1];
2200 const char *psz_access;
2201 const char *psz_demux;
2208 strcpy( psz_dup, psz_mrl );
2210 if( !in ) return VLC_EGENERIC;
2211 if( !p_input ) return VLC_EGENERIC;
2214 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2216 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2217 psz_mrl, psz_access, psz_demux, psz_path );
2218 if( !p_input->b_preparsing )
2220 /* Hack to allow udp://@:port syntax */
2222 (strncmp( psz_access, "udp", 3 ) &&
2223 strncmp( psz_access, "rtp", 3 )) )
2225 /* Find optional titles and seekpoints */
2226 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2227 &in->i_seekpoint_start, &in->i_seekpoint_end );
2230 if( psz_forced_demux && *psz_forced_demux )
2232 psz_demux = psz_forced_demux;
2234 else if( *psz_demux == '\0' )
2236 /* special hack for forcing a demuxer with --demux=module
2237 * (and do nothing with a list) */
2238 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2240 if( psz_var_demux != NULL &&
2241 !strchr(psz_var_demux, ',' ) &&
2242 !strchr(psz_var_demux, ':' ) )
2244 psz_demux = psz_var_demux;
2246 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2250 /* Try access_demux if no demux given */
2251 if( *psz_demux == '\0' )
2253 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2254 NULL, p_input->p->p_es_out, false );
2259 /* Preparsing is only for file:// */
2262 if( !*psz_access ) /* path without scheme:// */
2263 psz_access = "file";
2264 if( strcmp( psz_access, "file" ) )
2266 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2271 int64_t i_pts_delay;
2273 /* Get infos from access_demux */
2274 demux_Control( in->p_demux,
2275 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2276 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2278 in->b_title_demux = true;
2279 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2280 &in->title, &in->i_title,
2281 &in->i_title_offset, &in->i_seekpoint_offset ) )
2286 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2287 &in->b_can_pace_control ) )
2288 in->b_can_pace_control = false;
2290 if( !in->b_can_pace_control )
2292 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2293 &in->b_can_rate_control, &in->b_rescale_ts ) )
2295 in->b_can_rate_control = false;
2296 in->b_rescale_ts = true; /* not used */
2301 in->b_can_rate_control = true;
2302 in->b_rescale_ts = true;
2304 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2305 &in->b_can_pause ) )
2306 in->b_can_pause = false;
2307 var_SetBool( p_input, "can-pause", in->b_can_pause );
2309 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2311 if( ret != VLC_SUCCESS )
2313 var_Set( p_input, "seekable", val );
2317 int64_t i_pts_delay;
2319 input_ChangeState( p_input, OPENING_S );
2321 /* Now try a real access */
2322 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2324 /* Access failed, URL encoded ? */
2325 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2327 decode_URI( psz_path );
2329 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2330 psz_access, psz_demux, psz_path );
2332 in->p_access = access_New( p_input,
2333 psz_access, psz_demux, psz_path );
2335 if( in->p_access == NULL )
2337 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2339 intf_UserFatal( VLC_OBJECT( p_input), false,
2340 _("Your input can't be opened"),
2341 _("VLC is unable to open the MRL '%s'."
2342 " Check the log for details."), psz_mrl );
2347 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2348 while( psz && *psz )
2350 access_t *p_access = in->p_access;
2351 char *end = strchr( psz, ':' );
2356 in->p_access = access_FilterNew( in->p_access, psz );
2357 if( in->p_access == NULL )
2359 in->p_access = p_access;
2360 msg_Warn( p_input, "failed to insert access filter %s",
2368 /* Get infos from access */
2369 if( !p_input->b_preparsing )
2371 access_Control( in->p_access,
2372 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2373 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2375 in->b_title_demux = false;
2376 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2377 &in->title, &in->i_title,
2378 &in->i_title_offset, &in->i_seekpoint_offset ) )
2384 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2385 &in->b_can_pace_control );
2386 in->b_can_rate_control = in->b_can_pace_control;
2387 in->b_rescale_ts = true;
2389 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2391 var_SetBool( p_input, "can-pause", in->b_can_pause );
2392 access_Control( in->p_access, ACCESS_CAN_SEEK,
2394 var_Set( p_input, "seekable", val );
2397 input_ChangeState( p_input, BUFFERING_S );
2399 /* Create the stream_t */
2400 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2401 if( in->p_stream == NULL )
2403 msg_Warn( p_input, "cannot create a stream_t from access" );
2407 /* Open a demuxer */
2408 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2410 psz_demux = in->p_access->psz_demux;
2414 /* Take access redirections into account */
2415 char *psz_real_path;
2416 char *psz_buf = NULL;
2417 if( in->p_access->psz_path )
2419 const char *psz_a, *psz_d;
2420 psz_buf = strdup( in->p_access->psz_path );
2421 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2425 psz_real_path = psz_path;
2427 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2429 in->p_stream, p_input->p->p_es_out,
2430 p_input->b_preparsing );
2434 if( in->p_demux == NULL )
2436 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2437 psz_access, psz_demux, psz_path );
2438 intf_UserFatal( VLC_OBJECT( p_input ), false,
2439 _("VLC can't recognize the input's format"),
2440 _("The format of '%s' cannot be detected. "
2441 "Have a look the log for details."), psz_mrl );
2445 /* Get title from demux */
2446 if( !p_input->b_preparsing && in->i_title <= 0 )
2448 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2449 &in->title, &in->i_title,
2450 &in->i_title_offset, &in->i_seekpoint_offset ))
2452 TAB_INIT( in->i_title, in->title );
2456 in->b_title_demux = true;
2462 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2463 if( 1 || !p_input->b_preparsing )
2466 input_attachment_t **attachment;
2467 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2468 &attachment, &i_attachment ) )
2470 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2471 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2472 i_attachment, attachment );
2473 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2476 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2478 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2480 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2483 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2484 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2489 input_ChangeState( p_input, ERROR_S );
2492 demux_Delete( in->p_demux );
2495 stream_Delete( in->p_stream );
2498 access_Delete( in->p_access );
2500 return VLC_EGENERIC;
2503 /*****************************************************************************
2505 *****************************************************************************/
2506 static void InputSourceClean( input_source_t *in )
2511 demux_Delete( in->p_demux );
2514 stream_Delete( in->p_stream );
2517 access_Delete( in->p_access );
2519 if( in->i_title > 0 )
2521 for( i = 0; i < in->i_title; i++ )
2522 vlc_input_title_Delete( in->title[i] );
2523 TAB_CLEAN( in->i_title, in->title );
2527 static void SlaveDemux( input_thread_t *p_input )
2532 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2534 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2538 for( i = 0; i < p_input->p->i_slave; i++ )
2540 input_source_t *in = p_input->p->slave[i];
2546 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2551 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2553 msg_Err( p_input, "slave[%d] doesn't like "
2554 "DEMUX_GET_TIME -> EOF", i );
2559 if( i_stime >= i_time )
2562 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2568 i_ret = in->p_demux->pf_demux( in->p_demux );
2573 msg_Dbg( p_input, "slave %d EOF", i );
2579 static void SlaveSeek( input_thread_t *p_input )
2584 if( !p_input ) return;
2586 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2588 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2592 for( i = 0; i < p_input->p->i_slave; i++ )
2594 input_source_t *in = p_input->p->slave[i];
2596 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2598 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2604 /*****************************************************************************
2606 *****************************************************************************/
2607 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2611 if( !p_meta ) return;
2613 /* Get meta information from user */
2614 #define GET_META( field, s ) \
2615 var_Get( p_input, (s), &val ); \
2616 if( *val.psz_string ) \
2617 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2618 free( val.psz_string )
2620 GET_META( Title, "meta-title" );
2621 GET_META( Artist, "meta-artist" );
2622 GET_META( Genre, "meta-genre" );
2623 GET_META( Copyright, "meta-copyright" );
2624 GET_META( Description, "meta-description" );
2625 GET_META( Date, "meta-date" );
2626 GET_META( URL, "meta-url" );
2630 /*****************************************************************************
2631 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2632 * arturl and locking issue.
2633 *****************************************************************************/
2634 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2636 input_item_t *p_item = p_input->p->input.p_item;
2637 char * psz_arturl = NULL;
2638 char *psz_title = NULL;
2639 int i_arturl_event = false;
2644 psz_arturl = input_item_GetArtURL( p_item );
2646 vlc_mutex_lock( &p_item->lock );
2647 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2648 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2650 vlc_meta_Merge( p_item->p_meta, p_meta );
2652 if( psz_arturl && *psz_arturl )
2654 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2655 i_arturl_event = true;
2658 vlc_meta_Delete( p_meta );
2660 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2662 /* Don't look for art cover if sout
2663 * XXX It can change when sout has meta data support */
2664 if( p_input->p->p_sout && !p_input->b_preparsing )
2666 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2667 i_arturl_event = true;
2671 input_ExtractAttachmentAndCacheArt( p_input );
2677 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2679 p_meta = vlc_meta_New();
2680 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2682 vlc_mutex_unlock( &p_item->lock );
2684 input_item_SetPreparsed( p_item, true );
2686 if( i_arturl_event == true )
2690 /* Notify interested third parties */
2691 event.type = vlc_InputItemMetaChanged;
2692 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2693 vlc_event_send( &p_item->event_manager, &event );
2698 input_Control( p_input, INPUT_SET_NAME, psz_title );
2702 /** \todo handle sout meta */
2706 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2707 int i_new, input_attachment_t **pp_new )
2709 int i_attachment = *pi_attachment;
2710 input_attachment_t **attachment = *ppp_attachment;
2713 attachment = realloc( attachment,
2714 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2715 for( i = 0; i < i_new; i++ )
2716 attachment[i_attachment++] = pp_new[i];
2720 *pi_attachment = i_attachment;
2721 *ppp_attachment = attachment;
2724 static void AccessMeta( input_thread_t * p_input, vlc_meta_t *p_meta )
2728 if( p_input->b_preparsing )
2731 if( p_input->p->input.p_access )
2732 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
2735 /* Get meta data from slave input */
2736 for( i = 0; i < p_input->p->i_slave; i++ )
2738 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
2739 if( p_input->p->slave[i]->p_access )
2741 access_Control( p_input->p->slave[i]->p_access,
2742 ACCESS_GET_META, p_meta );
2747 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2754 /* XXX I am not sure it is a great idea, besides, there is more than that
2755 * if we want to do it right */
2756 vlc_mutex_lock( &p_item->lock );
2757 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2759 vlc_mutex_unlock( &p_item->lock );
2762 vlc_mutex_unlock( &p_item->lock );
2765 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2766 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2771 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2772 if(! p_demux->p_private )
2775 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2778 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2780 if( p_demux_meta->p_meta )
2782 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2783 vlc_meta_Delete( p_demux_meta->p_meta );
2786 if( p_demux_meta->i_attachments > 0 )
2788 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2789 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2790 p_demux_meta->i_attachments, p_demux_meta->attachments );
2791 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2793 module_Unneed( p_demux, p_id3 );
2795 free( p_demux->p_private );
2799 /*****************************************************************************
2800 * MRLSplit: parse the access, demux and url part of the
2801 * Media Resource Locator.
2802 *****************************************************************************/
2803 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2806 char *psz_access = NULL;
2807 char *psz_demux = NULL;
2810 /* Either there is an access/demux specification before ://
2811 * or we have a plain local file path. */
2812 psz_path = strstr( psz_dup, "://" );
2813 if( psz_path != NULL )
2816 psz_path += 3; /* skips "://" */
2818 /* Separate access from demux (<access>/<demux>://<path>) */
2819 psz_access = psz_dup;
2820 psz_demux = strchr( psz_access, '/' );
2822 *psz_demux++ = '\0';
2829 *ppsz_access = psz_access ? psz_access : "";
2830 *ppsz_demux = psz_demux ? psz_demux : "";
2831 *ppsz_path = psz_path ? psz_path : "";
2834 /*****************************************************************************
2835 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2838 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2839 *****************************************************************************/
2840 static void MRLSections( input_thread_t *p_input, char *psz_source,
2841 int *pi_title_start, int *pi_title_end,
2842 int *pi_chapter_start, int *pi_chapter_end )
2844 char *psz, *psz_end, *psz_next, *psz_check;
2846 *pi_title_start = *pi_title_end = -1;
2847 *pi_chapter_start = *pi_chapter_end = -1;
2849 /* Start by parsing titles and chapters */
2850 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2852 /* Check we are really dealing with a title/chapter section */
2853 psz_check = psz + 1;
2854 if( !*psz_check ) return;
2855 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2856 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2857 if( *psz_check == ':' && ++psz_check )
2858 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2859 if( *psz_check != '-' && *psz_check ) return;
2860 if( *psz_check == '-' && ++psz_check )
2861 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2862 if( *psz_check != ':' && *psz_check ) return;
2863 if( *psz_check == ':' && ++psz_check )
2864 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2865 if( *psz_check ) return;
2867 /* Separate start and end */
2869 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2871 /* Look for the start title */
2872 *pi_title_start = strtol( psz, &psz_next, 0 );
2873 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2874 *pi_title_end = *pi_title_start;
2877 /* Look for the start chapter */
2879 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2880 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2881 *pi_chapter_end = *pi_chapter_start;
2885 /* Look for the end title */
2886 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2887 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2890 /* Look for the end chapter */
2891 if( *psz_end ) psz_end++;
2892 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2893 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2896 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2897 psz_source, *pi_title_start, *pi_chapter_start,
2898 *pi_title_end, *pi_chapter_end );
2901 /*****************************************************************************
2902 * input_AddSubtitles: add a subtitles file and enable it
2903 *****************************************************************************/
2904 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2905 bool b_check_extension )
2907 input_source_t *sub;
2910 char *psz_path, *psz_extension;
2912 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2917 /* if we are provided a subtitle.sub file,
2918 * see if we don't have a subtitle.idx and use it instead */
2919 psz_path = strdup( psz_subtitle );
2922 psz_extension = strrchr( psz_path, '.');
2923 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2927 strcpy( psz_extension, ".idx" );
2928 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2929 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2932 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2933 psz_path, psz_subtitle );
2934 strcpy( psz_subtitle, psz_path );
2940 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2942 sub = InputSourceNew( p_input );
2943 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2945 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2948 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2950 if( count.i_int == 0 )
2952 /* if it was first one, there is disable too */
2954 if( count.i_int < list.p_list->i_count )
2956 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2957 &list.p_list->p_values[count.i_int] );
2959 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );