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_playlist.h>
44 #include <vlc_interface.h>
46 #include <vlc_charset.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 *, vlc_bool_t, 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 vlc_bool_t 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, vlc_bool_t * );
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 AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
95 int i_new, input_attachment_t **pp_new );
97 /*****************************************************************************
98 * This function creates a new input, and returns a pointer
99 * to its description. On error, it returns NULL.
101 * Variables for _public_ use:
104 * - rate,rate-slower, rate-faster
105 * - position, position-offset
106 * - time, time-offset
107 * - title,title-next,title-prev
108 * - chapter,chapter-next, chapter-prev
109 * - program, audio-es, video-es, spu-es
110 * - audio-delay, spu-delay
115 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown
116 * or not, for that check position != 0.0)
118 * * For intf callback upon changes
120 * - rate-change for when playback rate changes
121 * TODO explain when Callback is called
122 * TODO complete this list (?)
123 *****************************************************************************/
124 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
125 const char *psz_header, vlc_bool_t b_quick,
126 sout_instance_t *p_sout )
128 static const char input_name[] = "input";
129 input_thread_t *p_input = NULL; /* thread descriptor */
133 /* Allocate descriptor */
134 p_input = vlc_custom_create( p_parent, sizeof( *p_input ),
135 VLC_OBJECT_INPUT, input_name );
136 if( p_input == NULL )
138 msg_Err( p_parent, "out of memory" );
141 MALLOC_NULL( p_input->p, input_thread_private_t );
143 /* One "randomly" selected input thread is responsible for computing
144 * the global stats. Check if there is already someone doing this */
145 if( p_input->p_libvlc->p_stats && !b_quick )
147 vlc_mutex_lock( &p_input->p_libvlc->p_stats->lock );
148 if( p_input->p_libvlc->p_stats_computer == NULL )
150 p_input->p_libvlc->p_stats_computer = p_input;
152 vlc_mutex_unlock( &p_input->p_libvlc->p_stats->lock );
155 p_input->b_preparsing = b_quick;
156 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
158 /* Init Common fields */
159 p_input->b_eof = VLC_FALSE;
160 p_input->b_can_pace_control = VLC_TRUE;
161 p_input->p->i_start = 0;
163 p_input->p->i_stop = 0;
164 p_input->p->i_run = 0;
165 p_input->p->i_title = 0;
166 p_input->p->title = NULL;
167 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
168 p_input->i_state = INIT_S;
169 p_input->p->i_rate = INPUT_RATE_DEFAULT;
170 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
171 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
172 p_input->p->p_es_out = NULL;
173 p_input->p->p_sout = NULL;
174 p_input->p->b_owns_its_sout = VLC_TRUE;
175 p_input->p->b_sout_keep = VLC_FALSE;
176 p_input->p->b_out_pace_control = VLC_FALSE;
177 p_input->i_pts_delay = 0;
179 /* Init Input fields */
180 p_input->p->input.p_item = p_item;
181 p_input->p->input.p_access = NULL;
182 p_input->p->input.p_stream = NULL;
183 p_input->p->input.p_demux = NULL;
184 p_input->p->input.b_title_demux = VLC_FALSE;
185 p_input->p->input.i_title = 0;
186 p_input->p->input.title = NULL;
187 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
188 p_input->p->input.b_can_pace_control = VLC_TRUE;
189 p_input->p->input.b_can_rate_control = VLC_TRUE;
190 p_input->p->input.b_rescale_ts = VLC_TRUE;
191 p_input->p->input.b_eof = VLC_FALSE;
192 p_input->p->input.i_cr_average = 0;
194 vlc_mutex_lock( &p_item->lock );
196 if( !p_item->p_stats )
197 p_item->p_stats = stats_NewInputStats( p_input );
198 vlc_mutex_unlock( &p_item->lock );
201 p_input->p->i_slave = 0;
202 p_input->p->slave = NULL;
204 /* Init control buffer */
205 vlc_mutex_init( p_input, &p_input->p->lock_control );
206 p_input->p->i_control = 0;
208 /* Parse input options */
209 vlc_mutex_lock( &p_item->lock );
210 assert( p_item->optflagc == p_item->i_options );
211 for( i = 0; i < p_item->i_options; i++ )
212 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
213 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
214 vlc_mutex_unlock( &p_item->lock );
216 /* Create Object Variables for private use only */
217 input_ConfigVarInit( p_input );
219 /* Create Objects variables for public Get and Set */
220 input_ControlVarInit( p_input );
222 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
224 if( !p_input->b_preparsing )
226 var_Get( p_input, "bookmarks", &val );
229 /* FIXME: have a common cfg parsing routine used by sout and others */
230 char *psz_parser, *psz_start, *psz_end;
231 psz_parser = val.psz_string;
232 while( (psz_start = strchr( psz_parser, '{' ) ) )
234 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
237 psz_end = strchr( psz_start, '}' );
238 if( !psz_end ) break;
239 psz_parser = psz_end + 1;
240 backup = *psz_parser;
243 while( (psz_end = strchr( psz_start, ',' ) ) )
246 if( !strncmp( psz_start, "name=", 5 ) )
248 p_seekpoint->psz_name = strdup(psz_start + 5);
250 else if( !strncmp( psz_start, "bytes=", 6 ) )
252 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
254 else if( !strncmp( psz_start, "time=", 5 ) )
256 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
259 psz_start = psz_end + 1;
261 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
262 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
263 p_seekpoint->i_time_offset );
264 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
265 vlc_seekpoint_Delete( p_seekpoint );
266 *psz_parser = backup;
268 free( val.psz_string );
272 /* Remove 'Now playing' info as it is probably outdated */
273 input_Control( p_input, INPUT_DEL_INFO,
274 _(VLC_META_INFO_CAT),
275 _(VLC_META_NOW_PLAYING) );
276 input_item_SetNowPlaying( p_item, NULL );
279 if( p_input->b_preparsing )
280 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
285 p_input->p->p_sout = p_sout;
286 p_input->p->b_owns_its_sout = VLC_FALSE;
289 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
290 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
292 /* Attach only once we are ready */
293 vlc_object_attach( p_input, p_parent );
295 /* Set the destructor when we are sure we are initialized */
296 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
302 * Input destructor (called when the object's refcount reaches 0).
304 static void Destructor( input_thread_t * p_input )
306 input_thread_private_t *priv = p_input->p;
308 if( priv->b_owns_its_sout && priv->p_sout )
310 if( priv->b_sout_keep )
311 SoutKeep( priv->p_sout );
313 sout_DeleteInstance( priv->p_sout );
316 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
318 vlc_mutex_destroy( &priv->lock_control );
323 * Initialize an input thread and run it. You will need to monitor the
324 * thread to clean up after it is done
326 * \param p_parent a vlc_object
327 * \param p_item an input item
328 * \return a pointer to the spawned input thread
330 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
331 input_item_t *p_item )
333 vlc_bool_t b_sout_keep;
334 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
335 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
337 if( !p_input && p_sout )
340 p_input->p->b_sout_keep = b_sout_keep;
345 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
346 input_item_t *p_item,
347 const char *psz_log, sout_instance_t *p_sout )
349 input_thread_t *p_input;
351 p_input = Create( p_parent, p_item, psz_log, VLC_FALSE, p_sout );
355 /* Create thread and wait for its readiness. */
356 if( vlc_thread_create( p_input, "input", Run,
357 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
359 input_ChangeState( p_input, ERROR_S );
360 msg_Err( p_input, "cannot create input thread" );
361 vlc_object_detach( p_input );
362 vlc_object_release( p_input );
370 * Initialize an input thread and run it. This thread will clean after himself,
371 * you can forget about it. It can work either in blocking or non-blocking mode
373 * \param p_parent a vlc_object
374 * \param p_item an input item
375 * \param b_block should we block until read is finished ?
376 * \return the input object id if non blocking, an error code else
378 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
381 vlc_bool_t b_sout_keep;
382 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
383 input_thread_t *p_input;
385 p_input = Create( p_parent, p_item, NULL, VLC_FALSE, p_sout );
386 if( !p_input && p_sout )
391 p_input->p->b_sout_keep = b_sout_keep;
395 RunAndDestroy( p_input );
400 if( vlc_thread_create( p_input, "input", RunAndDestroy,
401 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
403 input_ChangeState( p_input, ERROR_S );
404 msg_Err( p_input, "cannot create input thread" );
405 vlc_object_release( p_input );
409 return p_input->i_object_id;
413 * Initialize an input and initialize it to preparse the item
414 * This function is blocking. It will only accept to parse files
416 * \param p_parent a vlc_object_t
417 * \param p_item an input item
418 * \return VLC_SUCCESS or an error
420 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
422 input_thread_t *p_input;
424 /* Allocate descriptor */
425 p_input = Create( p_parent, p_item, NULL, VLC_TRUE, NULL );
429 if( !Init( p_input ) )
432 vlc_object_detach( p_input );
433 vlc_object_release( p_input );
439 * Request a running input thread to stop and die
441 * \param the input thread to stop
443 void input_StopThread( input_thread_t *p_input )
448 /* Set die for input */
449 vlc_object_kill( p_input );
450 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
452 /* We cannot touch p_input fields directly (we come from another thread),
453 * so use the vlc_object_find way, it's perfectly safe */
455 /* Set die for all access */
456 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
457 for( i = 0; i < p_list->i_count; i++ )
459 vlc_object_kill( p_list->p_values[i].p_object );
461 vlc_list_release( p_list );
463 /* Set die for all stream */
464 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
465 for( i = 0; i < p_list->i_count; i++ )
467 vlc_object_kill( p_list->p_values[i].p_object );
469 vlc_list_release( p_list );
471 /* Set die for all demux */
472 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
473 for( i = 0; i < p_list->i_count; i++ )
475 vlc_object_kill( p_list->p_values[i].p_object );
477 vlc_list_release( p_list );
479 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
482 sout_instance_t * input_DetachSout( input_thread_t *p_input )
484 p_input->p->b_owns_its_sout = VLC_FALSE;
485 return p_input->p->p_sout;
488 /*****************************************************************************
489 * Run: main thread loop
490 * This is the "normal" thread that spawns the input processing chain,
491 * reads the stream, cleans up and waits
492 *****************************************************************************/
493 static int Run( input_thread_t *p_input )
495 /* Signal that the thread is launched */
496 vlc_thread_ready( p_input );
498 if( Init( p_input ) )
500 /* If we failed, wait before we are killed, and exit */
501 p_input->b_error = VLC_TRUE;
502 playlist_Signal( pl_Get( p_input ) );
506 /* Tell we're dead */
507 p_input->b_dead = VLC_TRUE;
514 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
516 /* We have finish to demux data but not to play them */
517 while( !p_input->b_die )
519 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
522 msg_Dbg( p_input, "waiting decoder fifos to empty" );
524 msleep( INPUT_IDLE_SLEEP );
527 /* We have finished */
528 p_input->b_eof = VLC_TRUE;
529 playlist_Signal( pl_Get( p_input ) );
532 /* Wait until we are asked to die */
533 if( !p_input->b_die )
544 /*****************************************************************************
545 * RunAndDestroy: main thread loop
546 * This is the "just forget me" thread that spawns the input processing chain,
547 * reads the stream, cleans up and releases memory
548 *****************************************************************************/
549 static int RunAndDestroy( input_thread_t *p_input )
551 /* Signal that the thread is launched */
552 vlc_thread_ready( p_input );
554 if( Init( p_input ) )
559 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
561 /* We have finished demuxing data but not playing it */
562 while( !p_input->b_die )
564 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
567 msg_Dbg( p_input, "waiting decoder fifos to empty" );
569 msleep( INPUT_IDLE_SLEEP );
572 /* We have finished */
573 p_input->b_eof = VLC_TRUE;
581 vlc_object_release( p_input );
585 /*****************************************************************************
586 * Main loop: Fill buffers from access, and demux
587 *****************************************************************************/
588 static void MainLoop( input_thread_t *p_input )
590 int64_t i_start_mdate = mdate();
591 int64_t i_intf_update = 0;
594 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
596 vlc_bool_t b_force_update = VLC_FALSE;
602 if( p_input->i_state != PAUSE_S )
604 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
605 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
608 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
613 if( p_input->p->input.b_title_demux &&
614 p_input->p->input.p_demux->info.i_update )
616 i_ret = UpdateFromDemux( p_input );
617 b_force_update = VLC_TRUE;
619 else if( !p_input->p->input.b_title_demux &&
620 p_input->p->input.p_access &&
621 p_input->p->input.p_access->info.i_update )
623 i_ret = UpdateFromAccess( p_input );
624 b_force_update = VLC_TRUE;
628 if( i_ret == 0 ) /* EOF */
632 var_Get( p_input, "input-repeat", &repeat );
633 if( repeat.i_int == 0 )
635 /* End of file - we do not set b_die because only the
636 * playlist is allowed to do so. */
637 input_ChangeState( p_input, END_S );
638 msg_Dbg( p_input, "EOF reached" );
639 p_input->p->input.b_eof = VLC_TRUE;
643 msg_Dbg( p_input, "repeating the same input (%d)",
645 if( repeat.i_int > 0 )
648 var_Set( p_input, "input-repeat", repeat );
651 /* Seek to start title/seekpoint */
652 val.i_int = p_input->p->input.i_title_start -
653 p_input->p->input.i_title_offset;
654 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
656 input_ControlPush( p_input,
657 INPUT_CONTROL_SET_TITLE, &val );
659 val.i_int = p_input->p->input.i_seekpoint_start -
660 p_input->p->input.i_seekpoint_offset;
661 if( val.i_int > 0 /* TODO: check upper boundary */ )
662 input_ControlPush( p_input,
663 INPUT_CONTROL_SET_SEEKPOINT, &val );
665 /* Seek to start position */
666 if( p_input->p->i_start > 0 )
668 val.i_time = p_input->p->i_start;
669 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
675 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
680 i_start_mdate = mdate();
685 p_input->b_error = VLC_TRUE;
688 if( i_ret > 0 && p_input->p->i_slave > 0 )
690 SlaveDemux( p_input );
700 vlc_mutex_lock( &p_input->p->lock_control );
701 ControlReduce( p_input );
702 while( !ControlPopNoLock( p_input, &i_type, &val ) )
704 msg_Dbg( p_input, "control type=%d", i_type );
705 if( Control( p_input, i_type, val ) )
706 b_force_update = VLC_TRUE;
708 vlc_mutex_unlock( &p_input->p->lock_control );
710 if( b_force_update || i_intf_update < mdate() )
714 int64_t i_time, i_length;
715 /* update input status variables */
716 if( !demux2_Control( p_input->p->input.p_demux,
717 DEMUX_GET_POSITION, &f_pos ) )
719 val.f_float = (float)f_pos;
720 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
722 if( !demux2_Control( p_input->p->input.p_demux,
723 DEMUX_GET_TIME, &i_time ) )
725 p_input->i_time = i_time;
727 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
729 if( !demux2_Control( p_input->p->input.p_demux,
730 DEMUX_GET_LENGTH, &i_length ) )
733 var_Get( p_input, "length", &old_val );
734 val.i_time = i_length;
735 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
737 if( old_val.i_time != val.i_time )
739 UpdateItemLength( p_input, i_length );
743 var_SetBool( p_input, "intf-change", VLC_TRUE );
744 i_intf_update = mdate() + I64C(150000);
746 /* 150ms * 8 = ~ 1 second */
747 if( ++i_updates % 8 == 0 )
749 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
750 /* Are we the thread responsible for computing global stats ? */
751 if( p_input->p_libvlc->p_stats_computer == p_input )
753 stats_ComputeGlobalStats( p_input->p_libvlc,
754 p_input->p_libvlc->p_stats );
760 static int Init( input_thread_t * p_input )
770 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
772 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
774 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
775 var_SetString( p_input, "sout", "" );
776 var_SetBool( p_input, "sout-all", VLC_FALSE );
777 var_SetString( p_input, "input-slave", "" );
778 var_SetInteger( p_input, "input-repeat", 0 );
779 var_SetString( p_input, "sub-file", "" );
780 var_SetBool( p_input, "sub-autodetect-file", VLC_FALSE );
784 if( !p_input->b_preparsing )
786 /* Prepare statistics */
787 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
788 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
789 if( p_input->p_libvlc->b_stats )
791 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
792 INIT_COUNTER( read_packets, INTEGER, COUNTER );
793 INIT_COUNTER( demux_read, INTEGER, COUNTER );
794 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
795 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
796 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
797 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
798 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
799 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
800 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
801 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
802 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
803 p_input->p->counters.p_sout_send_bitrate = NULL;
804 p_input->p->counters.p_sout_sent_packets = NULL;
805 p_input->p->counters.p_sout_sent_bytes = NULL;
806 if( p_input->p->counters.p_demux_bitrate )
807 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
808 if( p_input->p->counters.p_input_bitrate )
809 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
812 /* Find a usable sout and attach it to p_input */
813 psz = var_GetNonEmptyString( p_input, "sout" );
814 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
816 /* Check the validity of the provided sout */
817 if( p_input->p->p_sout )
819 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
821 msg_Dbg( p_input, "destroying unusable sout" );
823 sout_DeleteInstance( p_input->p->p_sout );
824 p_input->p->p_sout = NULL;
828 if( p_input->p->p_sout )
831 msg_Dbg( p_input, "sout keep: reusing sout" );
832 msg_Dbg( p_input, "sout keep: you probably want to use "
833 "gather stream_out" );
834 vlc_object_attach( p_input->p->p_sout, p_input );
838 /* Create a new one */
839 p_input->p->p_sout = sout_NewInstance( p_input, psz );
840 if( !p_input->p->p_sout )
842 input_ChangeState( p_input, ERROR_S );
843 msg_Err( p_input, "cannot start stream output instance, " \
850 if( p_input->p_libvlc->b_stats )
852 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
853 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
854 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
855 if( p_input->p->counters.p_sout_send_bitrate )
856 p_input->p->counters.p_sout_send_bitrate->update_interval =
860 else if( p_input->p->p_sout )
862 msg_Dbg( p_input, "destroying useless sout" );
864 sout_DeleteInstance( p_input->p->p_sout );
865 p_input->p->p_sout = NULL;
871 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
872 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
873 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
875 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
876 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
878 if( InputSourceInit( p_input, &p_input->p->input,
879 p_input->p->input.p_item->psz_uri, NULL ) )
884 /* Create global title (from master) */
885 if( !p_input->b_preparsing )
887 p_input->p->i_title = p_input->p->input.i_title;
888 p_input->p->title = p_input->p->input.title;
889 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
890 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
891 if( p_input->p->i_title > 0 )
893 /* Setup variables */
894 input_ControlVarNavigation( p_input );
895 input_ControlVarTitle( p_input, 0 );
899 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
900 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
901 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
904 if( p_input->i_pts_delay < 0 )
905 p_input->i_pts_delay = 0;
907 /* If the desynchronisation requested by the user is < 0, we need to
908 * cache more data. */
909 var_Get( p_input, "audio-desync", &val );
910 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
912 /* Update cr_average depending on the caching */
913 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
914 p_input->p->input.i_cr_average /= 10;
915 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
918 /* Load master infos */
920 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
921 &val.i_time ) && val.i_time > 0 )
923 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
924 UpdateItemLength( p_input, val.i_time );
928 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
930 { /* fallback: gets length from metadata */
931 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
932 UpdateItemLength( p_input, val.i_time );
936 /* Start title/chapter */
937 if( !p_input->b_preparsing )
939 val.i_int = p_input->p->input.i_title_start -
940 p_input->p->input.i_title_offset;
941 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
942 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
943 val.i_int = p_input->p->input.i_seekpoint_start -
944 p_input->p->input.i_seekpoint_offset;
945 if( val.i_int > 0 /* TODO: check upper boundary */ )
946 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
950 p_input->p->i_start = I64C(1000000) * var_GetInteger( p_input, "start-time" );
951 p_input->p->i_stop = I64C(1000000) * var_GetInteger( p_input, "stop-time" );
952 p_input->p->i_run = I64C(1000000) * var_GetInteger( p_input, "run-time" );
953 if( p_input->p->i_run < 0 )
955 msg_Warn( p_input, "invalid run-time ignored" );
956 p_input->p->i_run = 0;
959 if( p_input->p->i_start > 0 )
961 if( p_input->p->i_start >= val.i_time )
963 msg_Warn( p_input, "invalid start-time ignored" );
969 msg_Dbg( p_input, "starting at time: %ds",
970 (int)( p_input->p->i_start / I64C(1000000) ) );
972 s.i_time = p_input->p->i_start;
973 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
976 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
978 msg_Warn( p_input, "invalid stop-time ignored" );
979 p_input->p->i_stop = 0;
983 /* Get fps and set it if not already set */
984 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
987 float f_requested_fps;
989 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
990 var_SetFloat( p_input, "sub-original-fps", f_fps );
992 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
993 if( f_requested_fps != f_fps )
995 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
997 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1001 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1004 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1007 /* Look for and add subtitle files */
1008 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1009 if( psz_subtitle != NULL )
1011 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1012 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
1015 var_Get( p_input, "sub-autodetect-file", &val );
1018 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1019 char **subs = subtitles_Detect( p_input, psz_autopath,
1020 p_input->p->input.p_item->psz_uri );
1021 input_source_t *sub;
1023 if( psz_autopath == NULL )
1024 psz_autopath = strdup("");
1026 /* Try to autoselect the first autodetected subtitles file
1027 * if no subtitles file was specified */
1028 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1030 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
1035 /* Then, just add the following subtitles files */
1036 for( ; subs && subs[i]; i++ )
1038 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1040 sub = InputSourceNew( p_input );
1041 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1043 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1050 free( psz_autopath );
1052 free( psz_subtitle );
1054 /* Look for slave */
1055 psz = var_GetNonEmptyString( p_input, "input-slave" );
1059 input_source_t *slave;
1060 while( psz && *psz )
1062 while( *psz == ' ' || *psz == '#' )
1066 if( ( psz_delim = strchr( psz, '#' ) ) )
1068 *psz_delim++ = '\0';
1075 msg_Dbg( p_input, "adding slave input '%s'", psz );
1076 slave = InputSourceNew( p_input );
1077 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1079 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1089 p_input->p->i_start = 0;
1090 p_input->p->i_start = 0;
1094 if( !p_input->b_preparsing )
1096 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1097 i_es_out_mode = ES_OUT_MODE_AUTO;
1099 if( p_input->p->p_sout )
1101 var_Get( p_input, "sout-all", &val );
1104 i_es_out_mode = ES_OUT_MODE_ALL;
1109 var_Get( p_input, "programs", &val );
1110 if ( val.p_list && val.p_list->i_count )
1112 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1113 /* Note : we should remove the "program" callback. */
1116 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1120 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1122 /* Inform the demuxer about waited group (needed only for DVB) */
1123 if( i_es_out_mode == ES_OUT_MODE_ALL )
1125 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1127 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1129 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1134 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1135 (int) var_GetInteger( p_input, "program" ), NULL );
1138 if( p_input->p->p_sout )
1140 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1142 p_input->p->b_out_pace_control = VLC_FALSE;
1146 p_input->p->b_out_pace_control = VLC_TRUE;
1149 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1151 /* We don't want a high input priority here or we'll
1152 * end-up sucking up all the CPU time */
1153 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1156 msg_Dbg( p_input, "starting in %s mode",
1157 p_input->p->b_out_pace_control ? "async" : "sync" );
1161 p_meta = vlc_meta_New();
1162 /* Get meta data from users */
1163 InputMetaUser( p_input, p_meta );
1165 /* Get meta data from master input */
1166 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1168 /* Access_file does not give any meta, and there are no slave */
1169 if( !p_input->b_preparsing )
1171 if( p_input->p->input.p_access )
1172 access2_Control( p_input->p->input.p_access, ACCESS_GET_META,
1175 /* Get meta data from slave input */
1176 for( i = 0; i < p_input->p->i_slave; i++ )
1178 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
1179 if( p_input->p->slave[i]->p_access )
1181 access2_Control( p_input->p->slave[i]->p_access,
1182 ACCESS_GET_META, p_meta );
1187 InputUpdateMeta( p_input, p_meta );
1189 if( !p_input->b_preparsing )
1191 msg_Dbg( p_input, "`%s' successfully opened",
1192 p_input->p->input.p_item->psz_uri );
1196 /* initialization is complete */
1197 input_ChangeState( p_input, PLAYING_S );
1202 input_ChangeState( p_input, ERROR_S );
1204 if( p_input->p->p_es_out )
1205 input_EsOutDelete( p_input->p->p_es_out );
1207 if( p_input->p->p_sout )
1209 vlc_object_detach( p_input->p->p_sout );
1210 sout_DeleteInstance( p_input->p->p_sout );
1214 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1216 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1217 stats_CounterClean( p_input->p->counters.p_##c );\
1218 p_input->p->counters.p_##c = NULL; } while(0)
1219 EXIT_COUNTER( read_bytes );
1220 EXIT_COUNTER( read_packets );
1221 EXIT_COUNTER( demux_read );
1222 EXIT_COUNTER( input_bitrate );
1223 EXIT_COUNTER( demux_bitrate );
1224 EXIT_COUNTER( played_abuffers );
1225 EXIT_COUNTER( lost_abuffers );
1226 EXIT_COUNTER( displayed_pictures );
1227 EXIT_COUNTER( lost_pictures );
1228 EXIT_COUNTER( decoded_audio );
1229 EXIT_COUNTER( decoded_video );
1230 EXIT_COUNTER( decoded_sub );
1232 if( p_input->p->p_sout )
1234 EXIT_COUNTER( sout_sent_packets );
1235 EXIT_COUNTER (sout_sent_bytes );
1236 EXIT_COUNTER( sout_send_bitrate );
1241 /* Mark them deleted */
1242 p_input->p->input.p_demux = NULL;
1243 p_input->p->input.p_stream = NULL;
1244 p_input->p->input.p_access = NULL;
1245 p_input->p->p_es_out = NULL;
1246 p_input->p->p_sout = NULL;
1248 return VLC_EGENERIC;
1251 /*****************************************************************************
1252 * Error: RunThread() error loop
1253 *****************************************************************************
1254 * This function is called when an error occurred during thread main's loop.
1255 *****************************************************************************/
1256 static void Error( input_thread_t *p_input )
1258 input_ChangeState( p_input, ERROR_S );
1259 while( !p_input->b_die )
1262 msleep( INPUT_IDLE_SLEEP );
1266 /*****************************************************************************
1267 * End: end the input thread
1268 *****************************************************************************/
1269 static void End( input_thread_t * p_input )
1273 /* We are at the end */
1274 input_ChangeState( p_input, END_S );
1276 /* Clean control variables */
1277 input_ControlVarClean( p_input );
1279 /* Clean up master */
1280 InputSourceClean( &p_input->p->input );
1283 for( i = 0; i < p_input->p->i_slave; i++ )
1285 InputSourceClean( p_input->p->slave[i] );
1286 free( p_input->p->slave[i] );
1288 free( p_input->p->slave );
1290 /* Unload all modules */
1291 if( p_input->p->p_es_out )
1292 input_EsOutDelete( p_input->p->p_es_out );
1294 if( !p_input->b_preparsing )
1296 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1297 if( p_input->p_libvlc->b_stats )
1299 /* make sure we are up to date */
1300 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1301 if( p_input->p_libvlc->p_stats_computer == p_input )
1303 stats_ComputeGlobalStats( p_input->p_libvlc,
1304 p_input->p_libvlc->p_stats );
1305 p_input->p_libvlc->p_stats_computer = NULL;
1307 CL_CO( read_bytes );
1308 CL_CO( read_packets );
1309 CL_CO( demux_read );
1310 CL_CO( input_bitrate );
1311 CL_CO( demux_bitrate );
1312 CL_CO( played_abuffers );
1313 CL_CO( lost_abuffers );
1314 CL_CO( displayed_pictures );
1315 CL_CO( lost_pictures );
1316 CL_CO( decoded_audio) ;
1317 CL_CO( decoded_video );
1318 CL_CO( decoded_sub) ;
1321 /* Close optional stream output instance */
1322 if( p_input->p->p_sout )
1324 CL_CO( sout_sent_packets );
1325 CL_CO( sout_sent_bytes );
1326 CL_CO( sout_send_bitrate );
1328 vlc_object_detach( p_input->p->p_sout );
1333 if( p_input->p->i_attachment > 0 )
1335 for( i = 0; i < p_input->p->i_attachment; i++ )
1336 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1337 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1340 /* Tell we're dead */
1341 p_input->b_dead = VLC_TRUE;
1344 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t *pb_sout_keep )
1346 vlc_bool_t b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1347 sout_instance_t *p_sout = NULL;
1350 /* Search sout-keep options
1351 * XXX it has to be done here, but it is duplicated work :( */
1352 vlc_mutex_lock( &p_item->lock );
1353 for( i = 0; i < p_item->i_options; i++ )
1355 const char *psz_option = p_item->ppsz_options[i];
1358 if( *psz_option == ':' )
1361 if( !strcmp( psz_option, "sout-keep" ) )
1362 b_keep_sout = VLC_TRUE;
1363 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1364 b_keep_sout = VLC_FALSE;
1366 vlc_mutex_unlock( &p_item->lock );
1368 /* Find a potential sout to reuse
1369 * XXX it might be unusable but this will be checked later */
1372 /* Remove the sout from the playlist garbage collector */
1373 playlist_t *p_playlist = pl_Yield( p_parent );
1375 vlc_mutex_lock( &p_playlist->gc_lock );
1376 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1379 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1381 vlc_object_release( p_sout );
1386 vlc_object_detach( p_sout ); /* Remove it from the GC */
1388 vlc_object_release( p_sout );
1391 vlc_mutex_unlock( &p_playlist->gc_lock );
1393 pl_Release( p_parent );
1397 *pb_sout_keep = b_keep_sout;
1401 static void SoutKeep( sout_instance_t *p_sout )
1403 /* attach sout to the playlist */
1404 playlist_t *p_playlist = pl_Yield( p_sout );
1406 msg_Dbg( p_sout, "sout has been kept" );
1407 vlc_object_attach( p_sout, p_playlist );
1409 pl_Release( p_sout );
1412 /*****************************************************************************
1414 *****************************************************************************/
1415 static inline int ControlPopNoLock( input_thread_t *p_input,
1416 int *pi_type, vlc_value_t *p_val )
1418 if( p_input->p->i_control <= 0 )
1420 return VLC_EGENERIC;
1423 *pi_type = p_input->p->control[0].i_type;
1424 *p_val = p_input->p->control[0].val;
1426 p_input->p->i_control--;
1427 if( p_input->p->i_control > 0 )
1431 for( i = 0; i < p_input->p->i_control; i++ )
1433 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1434 p_input->p->control[i].val = p_input->p->control[i+1].val;
1441 static void ControlReduce( input_thread_t *p_input )
1448 for( i = 1; i < p_input->p->i_control; i++ )
1450 const int i_lt = p_input->p->control[i-1].i_type;
1451 const int i_ct = p_input->p->control[i].i_type;
1453 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1454 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1458 ( i_ct == INPUT_CONTROL_SET_STATE ||
1459 i_ct == INPUT_CONTROL_SET_RATE ||
1460 i_ct == INPUT_CONTROL_SET_POSITION ||
1461 i_ct == INPUT_CONTROL_SET_TIME ||
1462 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1463 i_ct == INPUT_CONTROL_SET_TITLE ||
1464 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1465 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1468 // msg_Dbg( p_input, "merged at %d", i );
1469 /* Remove the i-1 */
1470 for( j = i; j < p_input->p->i_control; j++ )
1471 p_input->p->control[j-1] = p_input->p->control[j];
1472 p_input->p->i_control--;
1476 /* TODO but that's not that important
1477 - merge SET_X with SET_X_CMD
1478 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1479 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1486 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1489 vlc_bool_t b_force_update = VLC_FALSE;
1491 if( !p_input ) return b_force_update;
1495 case INPUT_CONTROL_SET_DIE:
1496 msg_Dbg( p_input, "control: stopping input" );
1497 /* Mark all submodules to die */
1498 if( p_input->p->input.p_access )
1499 vlc_object_kill( p_input->p->input.p_access );
1500 if( p_input->p->input.p_stream )
1501 vlc_object_kill( p_input->p->input.p_stream );
1502 vlc_object_kill( p_input->p->input.p_demux );
1504 vlc_object_kill( p_input );
1507 case INPUT_CONTROL_SET_POSITION:
1508 case INPUT_CONTROL_SET_POSITION_OFFSET:
1511 if( i_type == INPUT_CONTROL_SET_POSITION )
1513 f_pos = val.f_float;
1517 /* Should not fail */
1518 demux2_Control( p_input->p->input.p_demux,
1519 DEMUX_GET_POSITION, &f_pos );
1520 f_pos += val.f_float;
1522 if( f_pos < 0.0 ) f_pos = 0.0;
1523 if( f_pos > 1.0 ) f_pos = 1.0;
1524 /* Reset the decoders states and clock sync (before calling the demuxer */
1525 input_EsOutChangePosition( p_input->p->p_es_out );
1526 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1529 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1530 "%2.1f%% failed", f_pos * 100 );
1534 if( p_input->p->i_slave > 0 )
1535 SlaveSeek( p_input );
1537 b_force_update = VLC_TRUE;
1542 case INPUT_CONTROL_SET_TIME:
1543 case INPUT_CONTROL_SET_TIME_OFFSET:
1548 if( i_type == INPUT_CONTROL_SET_TIME )
1550 i_time = val.i_time;
1554 /* Should not fail */
1555 demux2_Control( p_input->p->input.p_demux,
1556 DEMUX_GET_TIME, &i_time );
1557 i_time += val.i_time;
1559 if( i_time < 0 ) i_time = 0;
1561 /* Reset the decoders states and clock sync (before calling the demuxer */
1562 input_EsOutChangePosition( p_input->p->p_es_out );
1564 i_ret = demux2_Control( p_input->p->input.p_demux,
1565 DEMUX_SET_TIME, i_time );
1570 /* Emulate it with a SET_POS */
1571 demux2_Control( p_input->p->input.p_demux,
1572 DEMUX_GET_LENGTH, &i_length );
1575 double f_pos = (double)i_time / (double)i_length;
1576 i_ret = demux2_Control( p_input->p->input.p_demux,
1577 DEMUX_SET_POSITION, f_pos );
1582 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1583 " failed or not possible", i_time );
1587 if( p_input->p->i_slave > 0 )
1588 SlaveSeek( p_input );
1590 b_force_update = VLC_TRUE;
1595 case INPUT_CONTROL_SET_STATE:
1596 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1597 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1600 if( p_input->p->input.p_access )
1601 i_ret = access2_Control( p_input->p->input.p_access,
1602 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1604 i_ret = demux2_Control( p_input->p->input.p_demux,
1605 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1609 /* FIXME What to do ? */
1610 msg_Warn( p_input, "cannot unset pause -> EOF" );
1611 vlc_mutex_unlock( &p_input->p->lock_control );
1612 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1613 vlc_mutex_lock( &p_input->p->lock_control );
1616 b_force_update = VLC_TRUE;
1618 /* Switch to play */
1619 p_input->i_state = PLAYING_S;
1620 val.i_int = PLAYING_S;
1621 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1625 input_EsOutChangeState( p_input->p->p_es_out );
1627 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1628 p_input->p->b_can_pause )
1631 if( p_input->p->input.p_access )
1632 i_ret = access2_Control( p_input->p->input.p_access,
1633 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1635 i_ret = demux2_Control( p_input->p->input.p_demux,
1636 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1638 b_force_update = VLC_TRUE;
1642 msg_Warn( p_input, "cannot set pause state" );
1643 val.i_int = p_input->i_state;
1647 val.i_int = PAUSE_S;
1650 /* Switch to new state */
1651 p_input->i_state = val.i_int;
1652 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1656 input_EsOutChangeState( p_input->p->p_es_out );
1658 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1660 b_force_update = VLC_TRUE;
1662 /* Correct "state" value */
1663 val.i_int = p_input->i_state;
1664 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1666 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1668 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1672 case INPUT_CONTROL_SET_RATE:
1673 case INPUT_CONTROL_SET_RATE_SLOWER:
1674 case INPUT_CONTROL_SET_RATE_FASTER:
1678 if( i_type == INPUT_CONTROL_SET_RATE )
1684 static const int ppi_factor[][2] = {
1685 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1687 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1696 for( i = 0; ppi_factor[i][0] != 0; i++ )
1698 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1699 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1700 if( i_test_e < i_error )
1706 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1708 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1710 if( ppi_factor[i_idx+1][0] > 0 )
1711 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1713 i_rate = INPUT_RATE_MAX+1;
1717 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1719 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1721 i_rate = INPUT_RATE_MIN-1;
1725 if( i_rate < INPUT_RATE_MIN )
1727 msg_Dbg( p_input, "cannot set rate faster" );
1728 i_rate = INPUT_RATE_MIN;
1730 else if( i_rate > INPUT_RATE_MAX )
1732 msg_Dbg( p_input, "cannot set rate slower" );
1733 i_rate = INPUT_RATE_MAX;
1735 if( i_rate != INPUT_RATE_DEFAULT &&
1736 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1737 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1739 msg_Dbg( p_input, "cannot change rate" );
1740 i_rate = INPUT_RATE_DEFAULT;
1742 if( i_rate != p_input->p->i_rate &&
1743 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1746 if( p_input->p->input.p_access )
1747 i_ret = VLC_EGENERIC;
1749 i_ret = demux2_Control( p_input->p->input.p_demux,
1750 DEMUX_SET_RATE, &i_rate );
1753 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1754 i_rate = p_input->p->i_rate;
1759 if( i_rate != p_input->p->i_rate )
1762 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1763 var_SetBool( p_input, "rate-change", VLC_TRUE );
1765 p_input->p->i_rate = i_rate;
1767 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1768 if( p_input->p->input.b_rescale_ts )
1769 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1771 b_force_update = VLC_TRUE;
1776 case INPUT_CONTROL_SET_PROGRAM:
1777 /* No need to force update, es_out does it if needed */
1778 es_out_Control( p_input->p->p_es_out,
1779 ES_OUT_SET_GROUP, val.i_int );
1781 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1785 case INPUT_CONTROL_SET_ES:
1786 /* No need to force update, es_out does it if needed */
1787 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1788 input_EsOutGetFromID( p_input->p->p_es_out,
1792 case INPUT_CONTROL_SET_AUDIO_DELAY:
1793 input_EsOutSetDelay( p_input->p->p_es_out,
1794 AUDIO_ES, val.i_time );
1795 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1798 case INPUT_CONTROL_SET_SPU_DELAY:
1799 input_EsOutSetDelay( p_input->p->p_es_out,
1800 SPU_ES, val.i_time );
1801 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1804 case INPUT_CONTROL_SET_TITLE:
1805 case INPUT_CONTROL_SET_TITLE_NEXT:
1806 case INPUT_CONTROL_SET_TITLE_PREV:
1807 if( p_input->p->input.b_title_demux &&
1808 p_input->p->input.i_title > 0 )
1811 /* FIXME handle demux title */
1812 demux_t *p_demux = p_input->p->input.p_demux;
1815 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1816 i_title = p_demux->info.i_title - 1;
1817 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1818 i_title = p_demux->info.i_title + 1;
1820 i_title = val.i_int;
1822 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1824 input_EsOutChangePosition( p_input->p->p_es_out );
1826 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1827 input_ControlVarTitle( p_input, i_title );
1830 else if( p_input->p->input.i_title > 0 )
1832 access_t *p_access = p_input->p->input.p_access;
1835 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1836 i_title = p_access->info.i_title - 1;
1837 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1838 i_title = p_access->info.i_title + 1;
1840 i_title = val.i_int;
1842 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1844 input_EsOutChangePosition( p_input->p->p_es_out );
1846 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1847 stream_AccessReset( p_input->p->input.p_stream );
1851 case INPUT_CONTROL_SET_SEEKPOINT:
1852 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1853 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1854 if( p_input->p->input.b_title_demux &&
1855 p_input->p->input.i_title > 0 )
1857 demux_t *p_demux = p_input->p->input.p_demux;
1859 int64_t i_input_time;
1860 int64_t i_seekpoint_time;
1862 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1864 i_seekpoint = p_demux->info.i_seekpoint;
1865 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1866 if( i_seekpoint_time >= 0 &&
1867 !demux2_Control( p_demux,
1868 DEMUX_GET_TIME, &i_input_time ) )
1870 if ( i_input_time < i_seekpoint_time + 3000000 )
1876 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1877 i_seekpoint = p_demux->info.i_seekpoint + 1;
1879 i_seekpoint = val.i_int;
1881 if( i_seekpoint >= 0 && i_seekpoint <
1882 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1885 input_EsOutChangePosition( p_input->p->p_es_out );
1887 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1890 else if( p_input->p->input.i_title > 0 )
1892 demux_t *p_demux = p_input->p->input.p_demux;
1893 access_t *p_access = p_input->p->input.p_access;
1895 int64_t i_input_time;
1896 int64_t i_seekpoint_time;
1898 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1900 i_seekpoint = p_access->info.i_seekpoint;
1901 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1902 if( i_seekpoint_time >= 0 &&
1903 demux2_Control( p_demux,
1904 DEMUX_GET_TIME, &i_input_time ) )
1906 if ( i_input_time < i_seekpoint_time + 3000000 )
1912 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1913 i_seekpoint = p_access->info.i_seekpoint + 1;
1915 i_seekpoint = val.i_int;
1917 if( i_seekpoint >= 0 && i_seekpoint <
1918 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1920 input_EsOutChangePosition( p_input->p->p_es_out );
1922 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1924 stream_AccessReset( p_input->p->input.p_stream );
1929 case INPUT_CONTROL_ADD_SLAVE:
1930 if( val.psz_string )
1932 input_source_t *slave = InputSourceNew( p_input );
1934 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1940 msg_Dbg( p_input, "adding %s as slave on the fly",
1944 if( demux2_Control( p_input->p->input.p_demux,
1945 DEMUX_GET_TIME, &i_time ) )
1947 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1948 InputSourceClean( slave );
1952 if( demux2_Control( slave->p_demux,
1953 DEMUX_SET_TIME, i_time ) )
1955 msg_Err( p_input, "seek failed for new slave" );
1956 InputSourceClean( slave );
1961 /* Get meta (access and demux) */
1962 p_meta = vlc_meta_New();
1963 access2_Control( slave->p_access, ACCESS_GET_META,
1965 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1966 InputUpdateMeta( p_input, p_meta );
1968 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1973 msg_Warn( p_input, "failed to add %s as slave",
1977 free( val.psz_string );
1981 case INPUT_CONTROL_SET_BOOKMARK:
1983 msg_Err( p_input, "not yet implemented" );
1987 return b_force_update;
1990 /*****************************************************************************
1992 *****************************************************************************/
1993 static int UpdateFromDemux( input_thread_t *p_input )
1995 demux_t *p_demux = p_input->p->input.p_demux;
1998 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2000 v.i_int = p_demux->info.i_title;
2001 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2003 input_ControlVarTitle( p_input, p_demux->info.i_title );
2005 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2007 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2009 v.i_int = p_demux->info.i_seekpoint;
2010 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2012 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2014 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2016 /* Hmmm only works with master input */
2017 if( p_input->p->input.p_demux == p_demux )
2019 int i_title_end = p_input->p->input.i_title_end -
2020 p_input->p->input.i_title_offset;
2021 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2022 p_input->p->input.i_seekpoint_offset;
2024 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2026 if( p_demux->info.i_title > i_title_end ||
2027 ( p_demux->info.i_title == i_title_end &&
2028 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2030 else if( i_seekpoint_end >=0 )
2032 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2034 else if( i_title_end >= 0 )
2036 if( p_demux->info.i_title > i_title_end ) return 0;
2043 /*****************************************************************************
2045 *****************************************************************************/
2046 static int UpdateFromAccess( input_thread_t *p_input )
2048 access_t *p_access = p_input->p->input.p_access;
2051 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2053 v.i_int = p_access->info.i_title;
2054 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2056 input_ControlVarTitle( p_input, p_access->info.i_title );
2058 stream_AccessUpdate( p_input->p->input.p_stream );
2060 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2062 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2064 v.i_int = p_access->info.i_seekpoint;
2065 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2066 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2068 if( p_access->info.i_update & INPUT_UPDATE_META )
2070 /* TODO maybe multi - access ? */
2071 vlc_meta_t *p_meta = vlc_meta_New();
2072 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2073 InputUpdateMeta( p_input, p_meta );
2074 var_SetInteger( pl_Get( p_input ), "item-change", p_input->p->input.p_item->i_id );
2075 p_access->info.i_update &= ~INPUT_UPDATE_META;
2078 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2080 /* Hmmm only works with master input */
2081 if( p_input->p->input.p_access == p_access )
2083 int i_title_end = p_input->p->input.i_title_end -
2084 p_input->p->input.i_title_offset;
2085 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2086 p_input->p->input.i_seekpoint_offset;
2088 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2090 if( p_access->info.i_title > i_title_end ||
2091 ( p_access->info.i_title == i_title_end &&
2092 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2094 else if( i_seekpoint_end >=0 )
2096 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2098 else if( i_title_end >= 0 )
2100 if( p_access->info.i_title > i_title_end ) return 0;
2107 /*****************************************************************************
2109 *****************************************************************************/
2110 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2112 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2114 if( !p_input->b_preparsing )
2116 pl_Yield( p_input );
2117 var_SetInteger( pl_Get( p_input ), "item-change",
2118 p_input->p->input.p_item->i_id );
2119 pl_Release( p_input );
2123 /*****************************************************************************
2125 *****************************************************************************/
2126 static input_source_t *InputSourceNew( input_thread_t *p_input )
2128 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2132 msg_Err( p_input, "out of memory for new input source" );
2137 in->p_access = NULL;
2138 in->p_stream = NULL;
2140 in->b_title_demux = VLC_FALSE;
2141 TAB_INIT( in->i_title, in->title );
2142 in->b_can_pause = VLC_TRUE;
2143 in->b_can_pace_control = VLC_TRUE;
2144 in->b_can_rate_control = VLC_TRUE;
2145 in->b_rescale_ts = VLC_TRUE;
2146 in->b_eof = VLC_FALSE;
2148 in->i_cr_average = 0;
2153 /*****************************************************************************
2155 *****************************************************************************/
2156 static int InputSourceInit( input_thread_t *p_input,
2157 input_source_t *in, const char *psz_mrl,
2158 const char *psz_forced_demux )
2160 char psz_dup[strlen (psz_mrl) + 1];
2161 const char *psz_access;
2162 const char *psz_demux;
2169 strcpy( psz_dup, psz_mrl );
2171 if( !in ) return VLC_EGENERIC;
2172 if( !p_input ) return VLC_EGENERIC;
2175 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2177 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2178 psz_mrl, psz_access, psz_demux, psz_path );
2179 if( !p_input->b_preparsing )
2181 /* Hack to allow udp://@:port syntax */
2183 (strncmp( psz_access, "udp", 3 ) &&
2184 strncmp( psz_access, "rtp", 3 )) )
2186 /* Find optional titles and seekpoints */
2187 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2188 &in->i_seekpoint_start, &in->i_seekpoint_end );
2191 if( psz_forced_demux && *psz_forced_demux )
2193 psz_demux = psz_forced_demux;
2195 else if( *psz_demux == '\0' )
2197 /* special hack for forcing a demuxer with --demux=module
2198 * (and do nothing with a list) */
2199 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2201 if( psz_var_demux != NULL &&
2202 !strchr(psz_var_demux, ',' ) &&
2203 !strchr(psz_var_demux, ':' ) )
2205 psz_demux = psz_var_demux;
2207 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2211 /* Try access_demux if no demux given */
2212 if( *psz_demux == '\0' )
2214 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2215 NULL, p_input->p->p_es_out, VLC_FALSE );
2220 /* Preparsing is only for file:// */
2223 if( !*psz_access ) /* path without scheme:// */
2224 psz_access = "file";
2225 if( strcmp( psz_access, "file" ) )
2227 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2232 int64_t i_pts_delay;
2234 /* Get infos from access_demux */
2235 demux2_Control( in->p_demux,
2236 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2237 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2239 in->b_title_demux = VLC_TRUE;
2240 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2241 &in->title, &in->i_title,
2242 &in->i_title_offset, &in->i_seekpoint_offset ) )
2247 if( demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2248 &in->b_can_pace_control ) )
2249 in->b_can_pace_control = VLC_FALSE;
2251 if( !in->b_can_pace_control )
2253 if( demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2254 &in->b_can_rate_control, &in->b_rescale_ts ) )
2256 in->b_can_rate_control = VLC_FALSE;
2257 in->b_rescale_ts = VLC_TRUE; /* not used */
2262 in->b_can_rate_control = VLC_TRUE;
2263 in->b_rescale_ts = VLC_TRUE;
2265 if( demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2266 &in->b_can_pause ) )
2267 in->b_can_pause = VLC_FALSE;
2268 var_SetBool( p_input, "can-pause", in->b_can_pause );
2270 int ret = demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2272 if( ret != VLC_SUCCESS )
2273 val.b_bool = VLC_FALSE;
2274 var_Set( p_input, "seekable", val );
2278 int64_t i_pts_delay;
2280 input_ChangeState( p_input, OPENING_S );
2282 /* Now try a real access */
2283 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path );
2285 /* Access failed, URL encoded ? */
2286 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2288 decode_URI( psz_path );
2290 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2291 psz_access, psz_demux, psz_path );
2293 in->p_access = access2_New( p_input,
2294 psz_access, psz_demux, psz_path );
2296 if( in->p_access == NULL )
2298 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2300 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2301 _("Your input can't be opened"),
2302 _("VLC is unable to open the MRL '%s'."
2303 " Check the log for details."), psz_mrl );
2308 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2309 while( psz && *psz )
2311 access_t *p_access = in->p_access;
2312 char *end = strchr( psz, ':' );
2317 in->p_access = access2_FilterNew( in->p_access, psz );
2318 if( in->p_access == NULL )
2320 in->p_access = p_access;
2321 msg_Warn( p_input, "failed to insert access filter %s",
2329 /* Get infos from access */
2330 if( !p_input->b_preparsing )
2332 access2_Control( in->p_access,
2333 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2334 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2336 in->b_title_demux = VLC_FALSE;
2337 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2338 &in->title, &in->i_title,
2339 &in->i_title_offset, &in->i_seekpoint_offset ) )
2345 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2346 &in->b_can_pace_control );
2347 in->b_can_rate_control = in->b_can_pace_control;
2348 in->b_rescale_ts = VLC_TRUE;
2350 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2352 var_SetBool( p_input, "can-pause", in->b_can_pause );
2353 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2355 var_Set( p_input, "seekable", val );
2358 input_ChangeState( p_input, BUFFERING_S );
2360 /* Create the stream_t */
2361 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2362 if( in->p_stream == NULL )
2364 msg_Warn( p_input, "cannot create a stream_t from access" );
2368 /* Open a demuxer */
2369 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2371 psz_demux = in->p_access->psz_demux;
2375 /* Take access redirections into account */
2376 char *psz_real_path;
2377 char *psz_buf = NULL;
2378 if( in->p_access->psz_path )
2380 const char *psz_a, *psz_d;
2381 psz_buf = strdup( in->p_access->psz_path );
2382 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2386 psz_real_path = psz_path;
2388 in->p_demux = demux2_New( p_input, psz_access, psz_demux,
2390 in->p_stream, p_input->p->p_es_out,
2391 p_input->b_preparsing );
2395 if( in->p_demux == NULL )
2397 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2398 psz_access, psz_demux, psz_path );
2399 intf_UserFatal( VLC_OBJECT( p_input ), VLC_FALSE,
2400 _("VLC can't recognize the input's format"),
2401 _("The format of '%s' cannot be detected. "
2402 "Have a look the log for details."), psz_mrl );
2406 /* Get title from demux */
2407 if( !p_input->b_preparsing && in->i_title <= 0 )
2409 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2410 &in->title, &in->i_title,
2411 &in->i_title_offset, &in->i_seekpoint_offset ))
2413 TAB_INIT( in->i_title, in->title );
2417 in->b_title_demux = VLC_TRUE;
2423 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2424 if( 1 || !p_input->b_preparsing )
2427 input_attachment_t **attachment;
2428 if( !demux2_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2429 &attachment, &i_attachment ) )
2431 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2432 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2433 i_attachment, attachment );
2434 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2437 if( !demux2_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2439 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2441 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2444 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2445 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2450 input_ChangeState( p_input, ERROR_S );
2453 demux2_Delete( in->p_demux );
2456 stream_Delete( in->p_stream );
2459 access2_Delete( in->p_access );
2461 return VLC_EGENERIC;
2464 /*****************************************************************************
2466 *****************************************************************************/
2467 static void InputSourceClean( input_source_t *in )
2472 demux2_Delete( in->p_demux );
2475 stream_Delete( in->p_stream );
2478 access2_Delete( in->p_access );
2480 if( in->i_title > 0 )
2482 for( i = 0; i < in->i_title; i++ )
2483 vlc_input_title_Delete( in->title[i] );
2484 TAB_CLEAN( in->i_title, in->title );
2488 static void SlaveDemux( input_thread_t *p_input )
2493 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2495 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2499 for( i = 0; i < p_input->p->i_slave; i++ )
2501 input_source_t *in = p_input->p->slave[i];
2507 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2512 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2514 msg_Err( p_input, "slave[%d] doesn't like "
2515 "DEMUX_GET_TIME -> EOF", i );
2520 if( i_stime >= i_time )
2523 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2529 i_ret = in->p_demux->pf_demux( in->p_demux );
2534 msg_Dbg( p_input, "slave %d EOF", i );
2535 in->b_eof = VLC_TRUE;
2540 static void SlaveSeek( input_thread_t *p_input )
2545 if( !p_input ) return;
2547 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2549 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2553 for( i = 0; i < p_input->p->i_slave; i++ )
2555 input_source_t *in = p_input->p->slave[i];
2557 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2559 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2560 in->b_eof = VLC_TRUE;
2565 /*****************************************************************************
2567 *****************************************************************************/
2568 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2572 if( !p_meta ) return;
2574 /* Get meta information from user */
2575 #define GET_META( field, s ) \
2576 var_Get( p_input, (s), &val ); \
2577 if( *val.psz_string ) \
2578 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2579 free( val.psz_string )
2581 GET_META( Title, "meta-title" );
2582 GET_META( Artist, "meta-artist" );
2583 GET_META( Genre, "meta-genre" );
2584 GET_META( Copyright, "meta-copyright" );
2585 GET_META( Description, "meta-description" );
2586 GET_META( Date, "meta-date" );
2587 GET_META( URL, "meta-url" );
2591 /*****************************************************************************
2592 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2593 * arturl and locking issue.
2594 *****************************************************************************/
2595 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2597 input_item_t *p_item = p_input->p->input.p_item;
2598 char * psz_arturl = NULL;
2599 char *psz_title = NULL;
2601 int i_arturl_event = VLC_FALSE;
2606 psz_arturl = input_item_GetArtURL( p_item );
2608 vlc_mutex_lock( &p_item->lock );
2609 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2610 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2612 vlc_meta_Merge( p_item->p_meta, p_meta );
2614 if( psz_arturl && *psz_arturl )
2616 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2617 i_arturl_event = VLC_TRUE;
2620 vlc_meta_Delete( p_meta );
2622 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2624 /* Don't look for art cover if sout
2625 * XXX It can change when sout has meta data support */
2626 if( p_input->p->p_sout && !p_input->b_preparsing )
2628 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2629 i_arturl_event = VLC_TRUE;
2633 input_ExtractAttachmentAndCacheArt( p_input );
2639 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2641 p_meta = vlc_meta_New();
2642 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2644 vlc_mutex_unlock( &p_item->lock );
2646 input_item_SetPreparsed( p_item, VLC_TRUE );
2648 if( i_arturl_event == VLC_TRUE )
2652 /* Notify interested third parties */
2653 event.type = vlc_InputItemMetaChanged;
2654 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2655 vlc_event_send( &p_item->event_manager, &event );
2660 input_Control( p_input, INPUT_SET_NAME, psz_title );
2666 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2667 for( i = 0; ppsz_all_keys[i]; i++ )
2669 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2670 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2671 free( ppsz_all_keys[i] );
2673 free( ppsz_all_keys );
2674 vlc_meta_Delete( p_meta );
2677 /** \todo handle sout meta */
2681 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2682 int i_new, input_attachment_t **pp_new )
2684 int i_attachment = *pi_attachment;
2685 input_attachment_t **attachment = *ppp_attachment;
2688 attachment = realloc( attachment,
2689 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2690 for( i = 0; i < i_new; i++ )
2691 attachment[i_attachment++] = pp_new[i];
2695 *pi_attachment = i_attachment;
2696 *ppp_attachment = attachment;
2699 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2706 /* XXX I am not sure it is a great idea, besides, there is more than that
2707 * if we want to do it right */
2708 vlc_mutex_lock( &p_item->lock );
2709 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2711 vlc_mutex_unlock( &p_item->lock );
2714 vlc_mutex_unlock( &p_item->lock );
2717 demux2_Control( p_demux, DEMUX_GET_META, p_meta );
2718 if( demux2_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2723 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2724 if(! p_demux->p_private )
2727 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2730 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2732 if( p_demux_meta->p_meta )
2734 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2735 vlc_meta_Delete( p_demux_meta->p_meta );
2738 if( p_demux_meta->i_attachments > 0 )
2740 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2741 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2742 p_demux_meta->i_attachments, p_demux_meta->attachments );
2743 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2745 module_Unneed( p_demux, p_id3 );
2747 free( p_demux->p_private );
2751 /*****************************************************************************
2752 * MRLSplit: parse the access, demux and url part of the
2753 * Media Resource Locator.
2754 *****************************************************************************/
2755 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2758 char *psz_access = NULL;
2759 char *psz_demux = NULL;
2762 /* Either there is an access/demux specification before ://
2763 * or we have a plain local file path. */
2764 psz_path = strstr( psz_dup, "://" );
2765 if( psz_path != NULL )
2768 psz_path += 3; /* skips "://" */
2770 /* Separate access from demux (<access>/<demux>://<path>) */
2771 psz_access = psz_dup;
2772 psz_demux = strchr( psz_access, '/' );
2774 *psz_demux++ = '\0';
2781 *ppsz_access = psz_access ? psz_access : "";
2782 *ppsz_demux = psz_demux ? psz_demux : "";
2783 *ppsz_path = psz_path ? psz_path : "";
2786 /*****************************************************************************
2787 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2790 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2791 *****************************************************************************/
2792 static void MRLSections( input_thread_t *p_input, char *psz_source,
2793 int *pi_title_start, int *pi_title_end,
2794 int *pi_chapter_start, int *pi_chapter_end )
2796 char *psz, *psz_end, *psz_next, *psz_check;
2798 *pi_title_start = *pi_title_end = -1;
2799 *pi_chapter_start = *pi_chapter_end = -1;
2801 /* Start by parsing titles and chapters */
2802 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2804 /* Check we are really dealing with a title/chapter section */
2805 psz_check = psz + 1;
2806 if( !*psz_check ) return;
2807 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2808 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2809 if( *psz_check == ':' && ++psz_check )
2810 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2811 if( *psz_check != '-' && *psz_check ) return;
2812 if( *psz_check == '-' && ++psz_check )
2813 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2814 if( *psz_check != ':' && *psz_check ) return;
2815 if( *psz_check == ':' && ++psz_check )
2816 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2817 if( *psz_check ) return;
2819 /* Separate start and end */
2821 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2823 /* Look for the start title */
2824 *pi_title_start = strtol( psz, &psz_next, 0 );
2825 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2826 *pi_title_end = *pi_title_start;
2829 /* Look for the start chapter */
2831 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2832 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2833 *pi_chapter_end = *pi_chapter_start;
2837 /* Look for the end title */
2838 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2839 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2842 /* Look for the end chapter */
2843 if( *psz_end ) psz_end++;
2844 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2845 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2848 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2849 psz_source, *pi_title_start, *pi_chapter_start,
2850 *pi_title_end, *pi_chapter_end );
2853 /*****************************************************************************
2854 * input_AddSubtitles: add a subtitles file and enable it
2855 *****************************************************************************/
2856 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2857 vlc_bool_t b_check_extension )
2859 input_source_t *sub;
2862 char *psz_path, *psz_extension;
2864 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2869 /* if we are provided a subtitle.sub file,
2870 * see if we don't have a subtitle.idx and use it instead */
2871 psz_path = strdup( psz_subtitle );
2874 psz_extension = strrchr( psz_path, '.');
2875 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2879 strcpy( psz_extension, ".idx" );
2880 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2881 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2884 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2885 psz_path, psz_subtitle );
2886 strcpy( psz_subtitle, psz_path );
2892 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2894 sub = InputSourceNew( p_input );
2895 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2897 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2900 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2902 if( count.i_int == 0 )
2904 /* if it was first one, there is disable too */
2906 if( count.i_int < list.p_list->i_count )
2908 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2909 &list.p_list->p_values[count.i_int] );
2911 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );