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 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, bool 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 = false;
160 p_input->b_can_pace_control = 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 = true;
175 p_input->p->b_sout_keep = false;
176 p_input->p->b_out_pace_control = false;
177 p_input->i_pts_delay = 0;
179 /* Init Input fields */
180 vlc_gc_incref( p_item ); /* Released in Destructor() */
181 p_input->p->input.p_item = p_item;
182 p_input->p->input.p_access = NULL;
183 p_input->p->input.p_stream = NULL;
184 p_input->p->input.p_demux = NULL;
185 p_input->p->input.b_title_demux = false;
186 p_input->p->input.i_title = 0;
187 p_input->p->input.title = NULL;
188 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
189 p_input->p->input.b_can_pace_control = true;
190 p_input->p->input.b_can_rate_control = true;
191 p_input->p->input.b_rescale_ts = true;
192 p_input->p->input.b_eof = false;
193 p_input->p->input.i_cr_average = 0;
195 vlc_mutex_lock( &p_item->lock );
197 if( !p_item->p_stats )
198 p_item->p_stats = stats_NewInputStats( p_input );
199 vlc_mutex_unlock( &p_item->lock );
202 p_input->p->i_slave = 0;
203 p_input->p->slave = NULL;
205 /* Init control buffer */
206 vlc_mutex_init( p_input, &p_input->p->lock_control );
207 p_input->p->i_control = 0;
209 /* Parse input options */
210 vlc_mutex_lock( &p_item->lock );
211 assert( p_item->optflagc == p_item->i_options );
212 for( i = 0; i < p_item->i_options; i++ )
213 var_OptionParse( VLC_OBJECT(p_input), p_item->ppsz_options[i],
214 !!(p_item->optflagv[i] & VLC_INPUT_OPTION_TRUSTED) );
215 vlc_mutex_unlock( &p_item->lock );
217 /* Create Object Variables for private use only */
218 input_ConfigVarInit( p_input );
220 /* Create Objects variables for public Get and Set */
221 input_ControlVarInit( p_input );
223 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
225 if( !p_input->b_preparsing )
227 var_Get( p_input, "bookmarks", &val );
230 /* FIXME: have a common cfg parsing routine used by sout and others */
231 char *psz_parser, *psz_start, *psz_end;
232 psz_parser = val.psz_string;
233 while( (psz_start = strchr( psz_parser, '{' ) ) )
235 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
238 psz_end = strchr( psz_start, '}' );
239 if( !psz_end ) break;
240 psz_parser = psz_end + 1;
241 backup = *psz_parser;
244 while( (psz_end = strchr( psz_start, ',' ) ) )
247 if( !strncmp( psz_start, "name=", 5 ) )
249 p_seekpoint->psz_name = strdup(psz_start + 5);
251 else if( !strncmp( psz_start, "bytes=", 6 ) )
253 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
255 else if( !strncmp( psz_start, "time=", 5 ) )
257 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
260 psz_start = psz_end + 1;
262 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
263 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
264 p_seekpoint->i_time_offset );
265 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
266 vlc_seekpoint_Delete( p_seekpoint );
267 *psz_parser = backup;
269 free( val.psz_string );
273 /* Remove 'Now playing' info as it is probably outdated */
274 input_Control( p_input, INPUT_DEL_INFO,
275 _(VLC_META_INFO_CAT),
276 _(VLC_META_NOW_PLAYING) );
277 input_item_SetNowPlaying( p_item, NULL );
280 if( p_input->b_preparsing )
281 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
286 p_input->p->p_sout = p_sout;
287 p_input->p->b_owns_its_sout = false;
290 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
291 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
293 /* Attach only once we are ready */
294 vlc_object_attach( p_input, p_parent );
296 /* Set the destructor when we are sure we are initialized */
297 vlc_object_set_destructor( p_input, (vlc_destructor_t)Destructor );
303 * Input destructor (called when the object's refcount reaches 0).
305 static void Destructor( input_thread_t * p_input )
307 input_thread_private_t *priv = p_input->p;
309 if( priv->b_owns_its_sout && priv->p_sout )
311 if( priv->b_sout_keep )
312 SoutKeep( priv->p_sout );
314 sout_DeleteInstance( priv->p_sout );
317 vlc_gc_decref( p_input->p->input.p_item );
319 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
321 vlc_mutex_destroy( &priv->lock_control );
326 * Initialize an input thread and run it. You will need to monitor the
327 * thread to clean up after it is done
329 * \param p_parent a vlc_object
330 * \param p_item an input item
331 * \return a pointer to the spawned input thread
333 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
334 input_item_t *p_item )
337 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
338 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
340 if( !p_input && p_sout )
343 p_input->p->b_sout_keep = b_sout_keep;
348 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
349 input_item_t *p_item,
350 const char *psz_log, sout_instance_t *p_sout )
352 input_thread_t *p_input;
354 p_input = Create( p_parent, p_item, psz_log, false, p_sout );
358 /* Create thread and wait for its readiness. */
359 if( vlc_thread_create( p_input, "input", Run,
360 VLC_THREAD_PRIORITY_INPUT, true ) )
362 input_ChangeState( p_input, ERROR_S );
363 msg_Err( p_input, "cannot create input thread" );
364 vlc_object_detach( p_input );
365 vlc_object_release( p_input );
373 * Initialize an input thread and run it. This thread will clean after himself,
374 * you can forget about it. It can work either in blocking or non-blocking mode
376 * \param p_parent a vlc_object
377 * \param p_item an input item
378 * \param b_block should we block until read is finished ?
379 * \return the input object id if non blocking, an error code else
381 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
385 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
386 input_thread_t *p_input;
388 p_input = Create( p_parent, p_item, NULL, false, p_sout );
389 if( !p_input && p_sout )
394 p_input->p->b_sout_keep = b_sout_keep;
398 RunAndDestroy( p_input );
403 if( vlc_thread_create( p_input, "input", RunAndDestroy,
404 VLC_THREAD_PRIORITY_INPUT, true ) )
406 input_ChangeState( p_input, ERROR_S );
407 msg_Err( p_input, "cannot create input thread" );
408 vlc_object_release( p_input );
412 return p_input->i_object_id;
416 * Initialize an input and initialize it to preparse the item
417 * This function is blocking. It will only accept to parse files
419 * \param p_parent a vlc_object_t
420 * \param p_item an input item
421 * \return VLC_SUCCESS or an error
423 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
425 input_thread_t *p_input;
427 /* Allocate descriptor */
428 p_input = Create( p_parent, p_item, NULL, true, NULL );
432 if( !Init( p_input ) )
435 vlc_object_detach( p_input );
436 vlc_object_release( p_input );
442 * Request a running input thread to stop and die
444 * \param the input thread to stop
446 void input_StopThread( input_thread_t *p_input )
451 /* Set die for input */
452 vlc_object_kill( p_input );
453 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
455 /* We cannot touch p_input fields directly (we come from another thread),
456 * so use the vlc_object_find way, it's perfectly safe */
458 /* Set die for all access */
459 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
460 for( i = 0; i < p_list->i_count; i++ )
462 vlc_object_kill( p_list->p_values[i].p_object );
464 vlc_list_release( p_list );
466 /* Set die for all stream */
467 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
468 for( i = 0; i < p_list->i_count; i++ )
470 vlc_object_kill( p_list->p_values[i].p_object );
472 vlc_list_release( p_list );
474 /* Set die for all demux */
475 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, 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 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
485 sout_instance_t * input_DetachSout( input_thread_t *p_input )
487 p_input->p->b_owns_its_sout = false;
488 return p_input->p->p_sout;
491 /*****************************************************************************
492 * Run: main thread loop
493 * This is the "normal" thread that spawns the input processing chain,
494 * reads the stream, cleans up and waits
495 *****************************************************************************/
496 static int Run( input_thread_t *p_input )
498 /* Signal that the thread is launched */
499 vlc_thread_ready( p_input );
501 if( Init( p_input ) )
503 /* If we failed, wait before we are killed, and exit */
504 p_input->b_error = true;
506 /* FIXME: we don't want to depend on the playlist */
507 playlist_t * p_playlist = vlc_object_find( p_input,
508 VLC_OBJECT_PLAYLIST, FIND_PARENT );
511 playlist_Signal( p_playlist );
512 vlc_object_release( p_playlist );
517 /* Tell we're dead */
518 p_input->b_dead = true;
525 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
527 /* We have finish to demux data but not to play them */
528 while( !p_input->b_die )
530 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
533 msg_Dbg( p_input, "waiting decoder fifos to empty" );
535 msleep( INPUT_IDLE_SLEEP );
538 /* We have finished */
539 p_input->b_eof = true;
540 playlist_Signal( pl_Get( p_input ) );
543 /* Wait until we are asked to die */
544 if( !p_input->b_die )
555 /*****************************************************************************
556 * RunAndDestroy: main thread loop
557 * This is the "just forget me" thread that spawns the input processing chain,
558 * reads the stream, cleans up and releases memory
559 *****************************************************************************/
560 static int RunAndDestroy( input_thread_t *p_input )
562 /* Signal that the thread is launched */
563 vlc_thread_ready( p_input );
565 if( Init( p_input ) )
570 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
572 /* We have finished demuxing data but not playing it */
573 while( !p_input->b_die )
575 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
578 msg_Dbg( p_input, "waiting decoder fifos to empty" );
580 msleep( INPUT_IDLE_SLEEP );
583 /* We have finished */
584 p_input->b_eof = true;
592 vlc_object_release( p_input );
596 /*****************************************************************************
597 * Main loop: Fill buffers from access, and demux
598 *****************************************************************************/
599 static void MainLoop( input_thread_t *p_input )
601 int64_t i_start_mdate = mdate();
602 int64_t i_intf_update = 0;
605 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
607 bool b_force_update = false;
613 if( p_input->i_state != PAUSE_S )
615 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
616 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
619 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
624 if( p_input->p->input.b_title_demux &&
625 p_input->p->input.p_demux->info.i_update )
627 i_ret = UpdateFromDemux( p_input );
628 b_force_update = true;
630 else if( !p_input->p->input.b_title_demux &&
631 p_input->p->input.p_access &&
632 p_input->p->input.p_access->info.i_update )
634 i_ret = UpdateFromAccess( p_input );
635 b_force_update = true;
639 if( i_ret == 0 ) /* EOF */
643 var_Get( p_input, "input-repeat", &repeat );
644 if( repeat.i_int == 0 )
646 /* End of file - we do not set b_die because only the
647 * playlist is allowed to do so. */
648 input_ChangeState( p_input, END_S );
649 msg_Dbg( p_input, "EOF reached" );
650 p_input->p->input.b_eof = true;
654 msg_Dbg( p_input, "repeating the same input (%d)",
656 if( repeat.i_int > 0 )
659 var_Set( p_input, "input-repeat", repeat );
662 /* Seek to start title/seekpoint */
663 val.i_int = p_input->p->input.i_title_start -
664 p_input->p->input.i_title_offset;
665 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
667 input_ControlPush( p_input,
668 INPUT_CONTROL_SET_TITLE, &val );
670 val.i_int = p_input->p->input.i_seekpoint_start -
671 p_input->p->input.i_seekpoint_offset;
672 if( val.i_int > 0 /* TODO: check upper boundary */ )
673 input_ControlPush( p_input,
674 INPUT_CONTROL_SET_SEEKPOINT, &val );
676 /* Seek to start position */
677 if( p_input->p->i_start > 0 )
679 val.i_time = p_input->p->i_start;
680 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
686 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
691 i_start_mdate = mdate();
696 p_input->b_error = true;
699 if( i_ret > 0 && p_input->p->i_slave > 0 )
701 SlaveDemux( p_input );
711 vlc_mutex_lock( &p_input->p->lock_control );
712 ControlReduce( p_input );
713 while( !ControlPopNoLock( p_input, &i_type, &val ) )
715 msg_Dbg( p_input, "control type=%d", i_type );
716 if( Control( p_input, i_type, val ) )
717 b_force_update = true;
719 vlc_mutex_unlock( &p_input->p->lock_control );
721 if( b_force_update || i_intf_update < mdate() )
725 int64_t i_time, i_length;
726 /* update input status variables */
727 if( !demux_Control( p_input->p->input.p_demux,
728 DEMUX_GET_POSITION, &f_pos ) )
730 val.f_float = (float)f_pos;
731 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
733 if( !demux_Control( p_input->p->input.p_demux,
734 DEMUX_GET_TIME, &i_time ) )
736 p_input->i_time = i_time;
738 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
740 if( !demux_Control( p_input->p->input.p_demux,
741 DEMUX_GET_LENGTH, &i_length ) )
744 var_Get( p_input, "length", &old_val );
745 val.i_time = i_length;
746 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
748 if( old_val.i_time != val.i_time )
750 UpdateItemLength( p_input, i_length );
754 var_SetBool( p_input, "intf-change", true );
755 i_intf_update = mdate() + I64C(150000);
757 /* 150ms * 8 = ~ 1 second */
758 if( ++i_updates % 8 == 0 )
760 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
761 /* Are we the thread responsible for computing global stats ? */
762 if( p_input->p_libvlc->p_stats_computer == p_input )
764 stats_ComputeGlobalStats( p_input->p_libvlc,
765 p_input->p_libvlc->p_stats );
771 static int Init( input_thread_t * p_input )
781 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
783 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
785 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
786 var_SetString( p_input, "sout", "" );
787 var_SetBool( p_input, "sout-all", false );
788 var_SetString( p_input, "input-slave", "" );
789 var_SetInteger( p_input, "input-repeat", 0 );
790 var_SetString( p_input, "sub-file", "" );
791 var_SetBool( p_input, "sub-autodetect-file", false );
795 if( !p_input->b_preparsing )
797 /* Prepare statistics */
798 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
799 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
800 if( p_input->p_libvlc->b_stats )
802 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
803 INIT_COUNTER( read_packets, INTEGER, COUNTER );
804 INIT_COUNTER( demux_read, INTEGER, COUNTER );
805 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
806 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
807 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
808 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
809 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
810 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
811 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
812 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
813 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
814 p_input->p->counters.p_sout_send_bitrate = NULL;
815 p_input->p->counters.p_sout_sent_packets = NULL;
816 p_input->p->counters.p_sout_sent_bytes = NULL;
817 if( p_input->p->counters.p_demux_bitrate )
818 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
819 if( p_input->p->counters.p_input_bitrate )
820 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
823 /* Find a usable sout and attach it to p_input */
824 psz = var_GetNonEmptyString( p_input, "sout" );
825 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
827 /* Check the validity of the provided sout */
828 if( p_input->p->p_sout )
830 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
832 msg_Dbg( p_input, "destroying unusable sout" );
834 sout_DeleteInstance( p_input->p->p_sout );
835 p_input->p->p_sout = NULL;
839 if( p_input->p->p_sout )
842 msg_Dbg( p_input, "sout keep: reusing sout" );
843 msg_Dbg( p_input, "sout keep: you probably want to use "
844 "gather stream_out" );
845 vlc_object_attach( p_input->p->p_sout, p_input );
849 /* Create a new one */
850 p_input->p->p_sout = sout_NewInstance( p_input, psz );
851 if( !p_input->p->p_sout )
853 input_ChangeState( p_input, ERROR_S );
854 msg_Err( p_input, "cannot start stream output instance, " \
861 if( p_input->p_libvlc->b_stats )
863 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
864 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
865 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
866 if( p_input->p->counters.p_sout_send_bitrate )
867 p_input->p->counters.p_sout_send_bitrate->update_interval =
871 else if( p_input->p->p_sout )
873 msg_Dbg( p_input, "destroying useless sout" );
875 sout_DeleteInstance( p_input->p->p_sout );
876 p_input->p->p_sout = NULL;
882 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
883 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, false );
884 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
886 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
887 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
889 if( InputSourceInit( p_input, &p_input->p->input,
890 p_input->p->input.p_item->psz_uri, NULL ) )
895 /* Create global title (from master) */
896 if( !p_input->b_preparsing )
898 p_input->p->i_title = p_input->p->input.i_title;
899 p_input->p->title = p_input->p->input.title;
900 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
901 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
902 if( p_input->p->i_title > 0 )
904 /* Setup variables */
905 input_ControlVarNavigation( p_input );
906 input_ControlVarTitle( p_input, 0 );
910 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
911 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
912 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
915 if( p_input->i_pts_delay < 0 )
916 p_input->i_pts_delay = 0;
918 /* If the desynchronisation requested by the user is < 0, we need to
919 * cache more data. */
920 var_Get( p_input, "audio-desync", &val );
921 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
923 /* Update cr_average depending on the caching */
924 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
925 p_input->p->input.i_cr_average /= 10;
926 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
929 /* Load master infos */
931 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
932 &val.i_time ) && val.i_time > 0 )
934 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
935 UpdateItemLength( p_input, val.i_time );
939 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
941 { /* fallback: gets length from metadata */
942 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
943 UpdateItemLength( p_input, val.i_time );
947 /* Start title/chapter */
948 if( !p_input->b_preparsing )
950 val.i_int = p_input->p->input.i_title_start -
951 p_input->p->input.i_title_offset;
952 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
953 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
954 val.i_int = p_input->p->input.i_seekpoint_start -
955 p_input->p->input.i_seekpoint_offset;
956 if( val.i_int > 0 /* TODO: check upper boundary */ )
957 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
961 p_input->p->i_start = I64C(1000000) * var_GetInteger( p_input, "start-time" );
962 p_input->p->i_stop = I64C(1000000) * var_GetInteger( p_input, "stop-time" );
963 p_input->p->i_run = I64C(1000000) * var_GetInteger( p_input, "run-time" );
964 if( p_input->p->i_run < 0 )
966 msg_Warn( p_input, "invalid run-time ignored" );
967 p_input->p->i_run = 0;
970 if( p_input->p->i_start > 0 )
972 if( p_input->p->i_start >= val.i_time )
974 msg_Warn( p_input, "invalid start-time ignored" );
980 msg_Dbg( p_input, "starting at time: %ds",
981 (int)( p_input->p->i_start / I64C(1000000) ) );
983 s.i_time = p_input->p->i_start;
984 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
987 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
989 msg_Warn( p_input, "invalid stop-time ignored" );
990 p_input->p->i_stop = 0;
994 /* Get fps and set it if not already set */
995 if( !demux_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
998 float f_requested_fps;
1000 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
1001 var_SetFloat( p_input, "sub-original-fps", f_fps );
1003 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
1004 if( f_requested_fps != f_fps )
1006 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1007 VLC_VAR_DOINHERIT );
1008 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1012 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1015 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1018 /* Look for and add subtitle files */
1019 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1020 if( psz_subtitle != NULL )
1022 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1023 input_AddSubtitles( p_input, psz_subtitle, false );
1026 var_Get( p_input, "sub-autodetect-file", &val );
1029 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1030 char **subs = subtitles_Detect( p_input, psz_autopath,
1031 p_input->p->input.p_item->psz_uri );
1032 input_source_t *sub;
1034 if( psz_autopath == NULL )
1035 psz_autopath = strdup("");
1037 /* Try to autoselect the first autodetected subtitles file
1038 * if no subtitles file was specified */
1039 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1041 input_AddSubtitles( p_input, subs[0], false );
1046 /* Then, just add the following subtitles files */
1047 for( ; subs && subs[i]; i++ )
1049 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1051 sub = InputSourceNew( p_input );
1052 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1054 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1061 free( psz_autopath );
1063 free( psz_subtitle );
1065 /* Look for slave */
1066 psz = var_GetNonEmptyString( p_input, "input-slave" );
1070 input_source_t *slave;
1071 while( psz && *psz )
1073 while( *psz == ' ' || *psz == '#' )
1077 if( ( psz_delim = strchr( psz, '#' ) ) )
1079 *psz_delim++ = '\0';
1086 msg_Dbg( p_input, "adding slave input '%s'", psz );
1087 slave = InputSourceNew( p_input );
1088 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1090 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1100 p_input->p->i_start = 0;
1101 p_input->p->i_start = 0;
1105 if( !p_input->b_preparsing )
1107 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, true );
1108 i_es_out_mode = ES_OUT_MODE_AUTO;
1110 if( p_input->p->p_sout )
1112 var_Get( p_input, "sout-all", &val );
1115 i_es_out_mode = ES_OUT_MODE_ALL;
1120 var_Get( p_input, "programs", &val );
1121 if ( val.p_list && val.p_list->i_count )
1123 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1124 /* Note : we should remove the "program" callback. */
1127 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1131 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1133 /* Inform the demuxer about waited group (needed only for DVB) */
1134 if( i_es_out_mode == ES_OUT_MODE_ALL )
1136 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1138 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1140 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1145 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1146 (int) var_GetInteger( p_input, "program" ), NULL );
1149 if( p_input->p->p_sout )
1151 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1153 p_input->p->b_out_pace_control = false;
1157 p_input->p->b_out_pace_control = true;
1160 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1162 /* We don't want a high input priority here or we'll
1163 * end-up sucking up all the CPU time */
1164 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1167 msg_Dbg( p_input, "starting in %s mode",
1168 p_input->p->b_out_pace_control ? "async" : "sync" );
1172 p_meta = vlc_meta_New();
1173 /* Get meta data from users */
1174 InputMetaUser( p_input, p_meta );
1176 /* Get meta data from master input */
1177 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1179 /* Access_file does not give any meta, and there are no slave */
1180 if( !p_input->b_preparsing )
1182 if( p_input->p->input.p_access )
1183 access_Control( p_input->p->input.p_access, ACCESS_GET_META,
1186 /* Get meta data from slave input */
1187 for( i = 0; i < p_input->p->i_slave; i++ )
1189 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
1190 if( p_input->p->slave[i]->p_access )
1192 access_Control( p_input->p->slave[i]->p_access,
1193 ACCESS_GET_META, p_meta );
1198 InputUpdateMeta( p_input, p_meta );
1200 if( !p_input->b_preparsing )
1202 msg_Dbg( p_input, "`%s' successfully opened",
1203 p_input->p->input.p_item->psz_uri );
1207 /* initialization is complete */
1208 input_ChangeState( p_input, PLAYING_S );
1213 input_ChangeState( p_input, ERROR_S );
1215 if( p_input->p->p_es_out )
1216 input_EsOutDelete( p_input->p->p_es_out );
1218 if( p_input->p->p_sout )
1220 vlc_object_detach( p_input->p->p_sout );
1221 sout_DeleteInstance( p_input->p->p_sout );
1225 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1227 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1228 stats_CounterClean( p_input->p->counters.p_##c );\
1229 p_input->p->counters.p_##c = NULL; } while(0)
1230 EXIT_COUNTER( read_bytes );
1231 EXIT_COUNTER( read_packets );
1232 EXIT_COUNTER( demux_read );
1233 EXIT_COUNTER( input_bitrate );
1234 EXIT_COUNTER( demux_bitrate );
1235 EXIT_COUNTER( played_abuffers );
1236 EXIT_COUNTER( lost_abuffers );
1237 EXIT_COUNTER( displayed_pictures );
1238 EXIT_COUNTER( lost_pictures );
1239 EXIT_COUNTER( decoded_audio );
1240 EXIT_COUNTER( decoded_video );
1241 EXIT_COUNTER( decoded_sub );
1243 if( p_input->p->p_sout )
1245 EXIT_COUNTER( sout_sent_packets );
1246 EXIT_COUNTER (sout_sent_bytes );
1247 EXIT_COUNTER( sout_send_bitrate );
1252 /* Mark them deleted */
1253 p_input->p->input.p_demux = NULL;
1254 p_input->p->input.p_stream = NULL;
1255 p_input->p->input.p_access = NULL;
1256 p_input->p->p_es_out = NULL;
1257 p_input->p->p_sout = NULL;
1259 return VLC_EGENERIC;
1262 /*****************************************************************************
1263 * Error: RunThread() error loop
1264 *****************************************************************************
1265 * This function is called when an error occurred during thread main's loop.
1266 *****************************************************************************/
1267 static void Error( input_thread_t *p_input )
1269 input_ChangeState( p_input, ERROR_S );
1270 while( !p_input->b_die )
1273 msleep( INPUT_IDLE_SLEEP );
1277 /*****************************************************************************
1278 * End: end the input thread
1279 *****************************************************************************/
1280 static void End( input_thread_t * p_input )
1284 /* We are at the end */
1285 input_ChangeState( p_input, END_S );
1287 /* Clean control variables */
1288 input_ControlVarClean( p_input );
1290 /* Clean up master */
1291 InputSourceClean( &p_input->p->input );
1294 for( i = 0; i < p_input->p->i_slave; i++ )
1296 InputSourceClean( p_input->p->slave[i] );
1297 free( p_input->p->slave[i] );
1299 free( p_input->p->slave );
1301 /* Unload all modules */
1302 if( p_input->p->p_es_out )
1303 input_EsOutDelete( p_input->p->p_es_out );
1305 if( !p_input->b_preparsing )
1307 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1308 if( p_input->p_libvlc->b_stats )
1310 /* make sure we are up to date */
1311 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1312 if( p_input->p_libvlc->p_stats_computer == p_input )
1314 stats_ComputeGlobalStats( p_input->p_libvlc,
1315 p_input->p_libvlc->p_stats );
1316 p_input->p_libvlc->p_stats_computer = NULL;
1318 CL_CO( read_bytes );
1319 CL_CO( read_packets );
1320 CL_CO( demux_read );
1321 CL_CO( input_bitrate );
1322 CL_CO( demux_bitrate );
1323 CL_CO( played_abuffers );
1324 CL_CO( lost_abuffers );
1325 CL_CO( displayed_pictures );
1326 CL_CO( lost_pictures );
1327 CL_CO( decoded_audio) ;
1328 CL_CO( decoded_video );
1329 CL_CO( decoded_sub) ;
1332 /* Close optional stream output instance */
1333 if( p_input->p->p_sout )
1335 CL_CO( sout_sent_packets );
1336 CL_CO( sout_sent_bytes );
1337 CL_CO( sout_send_bitrate );
1339 vlc_object_detach( p_input->p->p_sout );
1344 if( p_input->p->i_attachment > 0 )
1346 for( i = 0; i < p_input->p->i_attachment; i++ )
1347 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1348 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1351 /* Tell we're dead */
1352 p_input->b_dead = true;
1355 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, bool *pb_sout_keep )
1357 bool b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1358 sout_instance_t *p_sout = NULL;
1361 /* Search sout-keep options
1362 * XXX it has to be done here, but it is duplicated work :( */
1363 vlc_mutex_lock( &p_item->lock );
1364 for( i = 0; i < p_item->i_options; i++ )
1366 const char *psz_option = p_item->ppsz_options[i];
1369 if( *psz_option == ':' )
1372 if( !strcmp( psz_option, "sout-keep" ) )
1374 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1375 b_keep_sout = false;
1377 vlc_mutex_unlock( &p_item->lock );
1379 /* Find a potential sout to reuse
1380 * XXX it might be unusable but this will be checked later */
1383 /* Remove the sout from the playlist garbage collector */
1384 /* FIXME: we don't want to depend on the playlist, sout
1385 * should be attached to libvlc */
1386 playlist_t * p_playlist = vlc_object_find( p_parent,
1387 VLC_OBJECT_PLAYLIST, FIND_PARENT );
1390 vlc_mutex_lock( &p_playlist->gc_lock );
1391 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1394 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1396 vlc_object_release( p_sout );
1401 vlc_object_detach( p_sout ); /* Remove it from the GC */
1403 vlc_object_release( p_sout );
1406 vlc_mutex_unlock( &p_playlist->gc_lock );
1408 vlc_object_release( p_playlist );
1413 *pb_sout_keep = b_keep_sout;
1418 static void SoutKeep( sout_instance_t *p_sout )
1420 /* FIXME: we don't want to depend on the playlist, sout
1421 * should be attached to libvlc */
1422 playlist_t * p_playlist = vlc_object_find( p_sout, VLC_OBJECT_PLAYLIST,
1424 if( !p_playlist ) return;
1426 msg_Dbg( p_sout, "sout has been kept" );
1427 vlc_object_attach( p_sout, p_playlist );
1429 pl_Release( p_sout );
1432 /*****************************************************************************
1434 *****************************************************************************/
1435 static inline int ControlPopNoLock( input_thread_t *p_input,
1436 int *pi_type, vlc_value_t *p_val )
1438 if( p_input->p->i_control <= 0 )
1440 return VLC_EGENERIC;
1443 *pi_type = p_input->p->control[0].i_type;
1444 *p_val = p_input->p->control[0].val;
1446 p_input->p->i_control--;
1447 if( p_input->p->i_control > 0 )
1451 for( i = 0; i < p_input->p->i_control; i++ )
1453 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1454 p_input->p->control[i].val = p_input->p->control[i+1].val;
1461 static void ControlReduce( input_thread_t *p_input )
1468 for( i = 1; i < p_input->p->i_control; i++ )
1470 const int i_lt = p_input->p->control[i-1].i_type;
1471 const int i_ct = p_input->p->control[i].i_type;
1473 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1474 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1478 ( i_ct == INPUT_CONTROL_SET_STATE ||
1479 i_ct == INPUT_CONTROL_SET_RATE ||
1480 i_ct == INPUT_CONTROL_SET_POSITION ||
1481 i_ct == INPUT_CONTROL_SET_TIME ||
1482 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1483 i_ct == INPUT_CONTROL_SET_TITLE ||
1484 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1485 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1488 // msg_Dbg( p_input, "merged at %d", i );
1489 /* Remove the i-1 */
1490 for( j = i; j < p_input->p->i_control; j++ )
1491 p_input->p->control[j-1] = p_input->p->control[j];
1492 p_input->p->i_control--;
1496 /* TODO but that's not that important
1497 - merge SET_X with SET_X_CMD
1498 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1499 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1506 static bool Control( input_thread_t *p_input, int i_type,
1509 bool b_force_update = false;
1511 if( !p_input ) return b_force_update;
1515 case INPUT_CONTROL_SET_DIE:
1516 msg_Dbg( p_input, "control: stopping input" );
1517 /* Mark all submodules to die */
1518 if( p_input->p->input.p_access )
1519 vlc_object_kill( p_input->p->input.p_access );
1520 if( p_input->p->input.p_stream )
1521 vlc_object_kill( p_input->p->input.p_stream );
1522 vlc_object_kill( p_input->p->input.p_demux );
1524 vlc_object_kill( p_input );
1527 case INPUT_CONTROL_SET_POSITION:
1528 case INPUT_CONTROL_SET_POSITION_OFFSET:
1531 if( i_type == INPUT_CONTROL_SET_POSITION )
1533 f_pos = val.f_float;
1537 /* Should not fail */
1538 demux_Control( p_input->p->input.p_demux,
1539 DEMUX_GET_POSITION, &f_pos );
1540 f_pos += val.f_float;
1542 if( f_pos < 0.0 ) f_pos = 0.0;
1543 if( f_pos > 1.0 ) f_pos = 1.0;
1544 /* Reset the decoders states and clock sync (before calling the demuxer */
1545 input_EsOutChangePosition( p_input->p->p_es_out );
1546 if( demux_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1549 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1550 "%2.1f%% failed", f_pos * 100 );
1554 if( p_input->p->i_slave > 0 )
1555 SlaveSeek( p_input );
1557 b_force_update = true;
1562 case INPUT_CONTROL_SET_TIME:
1563 case INPUT_CONTROL_SET_TIME_OFFSET:
1568 if( i_type == INPUT_CONTROL_SET_TIME )
1570 i_time = val.i_time;
1574 /* Should not fail */
1575 demux_Control( p_input->p->input.p_demux,
1576 DEMUX_GET_TIME, &i_time );
1577 i_time += val.i_time;
1579 if( i_time < 0 ) i_time = 0;
1581 /* Reset the decoders states and clock sync (before calling the demuxer */
1582 input_EsOutChangePosition( p_input->p->p_es_out );
1584 i_ret = demux_Control( p_input->p->input.p_demux,
1585 DEMUX_SET_TIME, i_time );
1590 /* Emulate it with a SET_POS */
1591 demux_Control( p_input->p->input.p_demux,
1592 DEMUX_GET_LENGTH, &i_length );
1595 double f_pos = (double)i_time / (double)i_length;
1596 i_ret = demux_Control( p_input->p->input.p_demux,
1597 DEMUX_SET_POSITION, f_pos );
1602 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1603 " failed or not possible", i_time );
1607 if( p_input->p->i_slave > 0 )
1608 SlaveSeek( p_input );
1610 b_force_update = true;
1615 case INPUT_CONTROL_SET_STATE:
1616 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1617 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1620 if( p_input->p->input.p_access )
1621 i_ret = access_Control( p_input->p->input.p_access,
1622 ACCESS_SET_PAUSE_STATE, false );
1624 i_ret = demux_Control( p_input->p->input.p_demux,
1625 DEMUX_SET_PAUSE_STATE, false );
1629 /* FIXME What to do ? */
1630 msg_Warn( p_input, "cannot unset pause -> EOF" );
1631 vlc_mutex_unlock( &p_input->p->lock_control );
1632 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1633 vlc_mutex_lock( &p_input->p->lock_control );
1636 b_force_update = true;
1638 /* Switch to play */
1639 p_input->i_state = PLAYING_S;
1640 val.i_int = PLAYING_S;
1641 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1645 input_EsOutChangeState( p_input->p->p_es_out );
1647 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1648 p_input->p->b_can_pause )
1651 if( p_input->p->input.p_access )
1652 i_ret = access_Control( p_input->p->input.p_access,
1653 ACCESS_SET_PAUSE_STATE, true );
1655 i_ret = demux_Control( p_input->p->input.p_demux,
1656 DEMUX_SET_PAUSE_STATE, true );
1658 b_force_update = true;
1662 msg_Warn( p_input, "cannot set pause state" );
1663 val.i_int = p_input->i_state;
1667 val.i_int = PAUSE_S;
1670 /* Switch to new state */
1671 p_input->i_state = val.i_int;
1672 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1676 input_EsOutChangeState( p_input->p->p_es_out );
1678 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1680 b_force_update = true;
1682 /* Correct "state" value */
1683 val.i_int = p_input->i_state;
1684 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1686 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1688 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1692 case INPUT_CONTROL_SET_RATE:
1693 case INPUT_CONTROL_SET_RATE_SLOWER:
1694 case INPUT_CONTROL_SET_RATE_FASTER:
1698 if( i_type == INPUT_CONTROL_SET_RATE )
1704 static const int ppi_factor[][2] = {
1705 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1707 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1716 for( i = 0; ppi_factor[i][0] != 0; i++ )
1718 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1719 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1720 if( i_test_e < i_error )
1726 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1728 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1730 if( ppi_factor[i_idx+1][0] > 0 )
1731 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1733 i_rate = INPUT_RATE_MAX+1;
1737 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1739 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1741 i_rate = INPUT_RATE_MIN-1;
1745 if( i_rate < INPUT_RATE_MIN )
1747 msg_Dbg( p_input, "cannot set rate faster" );
1748 i_rate = INPUT_RATE_MIN;
1750 else if( i_rate > INPUT_RATE_MAX )
1752 msg_Dbg( p_input, "cannot set rate slower" );
1753 i_rate = INPUT_RATE_MAX;
1755 if( i_rate != INPUT_RATE_DEFAULT &&
1756 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1757 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1759 msg_Dbg( p_input, "cannot change rate" );
1760 i_rate = INPUT_RATE_DEFAULT;
1762 if( i_rate != p_input->p->i_rate &&
1763 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1766 if( p_input->p->input.p_access )
1767 i_ret = VLC_EGENERIC;
1769 i_ret = demux_Control( p_input->p->input.p_demux,
1770 DEMUX_SET_RATE, &i_rate );
1773 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1774 i_rate = p_input->p->i_rate;
1779 if( i_rate != p_input->p->i_rate )
1782 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1783 var_SetBool( p_input, "rate-change", true );
1785 p_input->p->i_rate = i_rate;
1787 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1788 if( p_input->p->input.b_rescale_ts )
1789 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1791 b_force_update = true;
1796 case INPUT_CONTROL_SET_PROGRAM:
1797 /* No need to force update, es_out does it if needed */
1798 es_out_Control( p_input->p->p_es_out,
1799 ES_OUT_SET_GROUP, val.i_int );
1801 demux_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1805 case INPUT_CONTROL_SET_ES:
1806 /* No need to force update, es_out does it if needed */
1807 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1808 input_EsOutGetFromID( p_input->p->p_es_out,
1812 case INPUT_CONTROL_SET_AUDIO_DELAY:
1813 input_EsOutSetDelay( p_input->p->p_es_out,
1814 AUDIO_ES, val.i_time );
1815 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1818 case INPUT_CONTROL_SET_SPU_DELAY:
1819 input_EsOutSetDelay( p_input->p->p_es_out,
1820 SPU_ES, val.i_time );
1821 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1824 case INPUT_CONTROL_SET_TITLE:
1825 case INPUT_CONTROL_SET_TITLE_NEXT:
1826 case INPUT_CONTROL_SET_TITLE_PREV:
1827 if( p_input->p->input.b_title_demux &&
1828 p_input->p->input.i_title > 0 )
1831 /* FIXME handle demux title */
1832 demux_t *p_demux = p_input->p->input.p_demux;
1835 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1836 i_title = p_demux->info.i_title - 1;
1837 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1838 i_title = p_demux->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 demux_Control( p_demux, DEMUX_SET_TITLE, i_title );
1847 input_ControlVarTitle( p_input, i_title );
1850 else if( p_input->p->input.i_title > 0 )
1852 access_t *p_access = p_input->p->input.p_access;
1855 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1856 i_title = p_access->info.i_title - 1;
1857 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1858 i_title = p_access->info.i_title + 1;
1860 i_title = val.i_int;
1862 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1864 input_EsOutChangePosition( p_input->p->p_es_out );
1866 access_Control( p_access, ACCESS_SET_TITLE, i_title );
1867 stream_AccessReset( p_input->p->input.p_stream );
1871 case INPUT_CONTROL_SET_SEEKPOINT:
1872 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1873 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1874 if( p_input->p->input.b_title_demux &&
1875 p_input->p->input.i_title > 0 )
1877 demux_t *p_demux = p_input->p->input.p_demux;
1879 int64_t i_input_time;
1880 int64_t i_seekpoint_time;
1882 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1884 i_seekpoint = p_demux->info.i_seekpoint;
1885 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1886 if( i_seekpoint_time >= 0 &&
1887 !demux_Control( p_demux,
1888 DEMUX_GET_TIME, &i_input_time ) )
1890 if ( i_input_time < i_seekpoint_time + 3000000 )
1896 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1897 i_seekpoint = p_demux->info.i_seekpoint + 1;
1899 i_seekpoint = val.i_int;
1901 if( i_seekpoint >= 0 && i_seekpoint <
1902 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1905 input_EsOutChangePosition( p_input->p->p_es_out );
1907 demux_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1910 else if( p_input->p->input.i_title > 0 )
1912 demux_t *p_demux = p_input->p->input.p_demux;
1913 access_t *p_access = p_input->p->input.p_access;
1915 int64_t i_input_time;
1916 int64_t i_seekpoint_time;
1918 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1920 i_seekpoint = p_access->info.i_seekpoint;
1921 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1922 if( i_seekpoint_time >= 0 &&
1923 demux_Control( p_demux,
1924 DEMUX_GET_TIME, &i_input_time ) )
1926 if ( i_input_time < i_seekpoint_time + 3000000 )
1932 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1933 i_seekpoint = p_access->info.i_seekpoint + 1;
1935 i_seekpoint = val.i_int;
1937 if( i_seekpoint >= 0 && i_seekpoint <
1938 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1940 input_EsOutChangePosition( p_input->p->p_es_out );
1942 access_Control( p_access, ACCESS_SET_SEEKPOINT,
1944 stream_AccessReset( p_input->p->input.p_stream );
1949 case INPUT_CONTROL_ADD_SLAVE:
1950 if( val.psz_string )
1952 input_source_t *slave = InputSourceNew( p_input );
1954 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1960 msg_Dbg( p_input, "adding %s as slave on the fly",
1964 if( demux_Control( p_input->p->input.p_demux,
1965 DEMUX_GET_TIME, &i_time ) )
1967 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1968 InputSourceClean( slave );
1972 if( demux_Control( slave->p_demux,
1973 DEMUX_SET_TIME, i_time ) )
1975 msg_Err( p_input, "seek failed for new slave" );
1976 InputSourceClean( slave );
1981 /* Get meta (access and demux) */
1982 p_meta = vlc_meta_New();
1983 access_Control( slave->p_access, ACCESS_GET_META,
1985 demux_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1986 InputUpdateMeta( p_input, p_meta );
1988 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1993 msg_Warn( p_input, "failed to add %s as slave",
1997 free( val.psz_string );
2001 case INPUT_CONTROL_SET_BOOKMARK:
2003 msg_Err( p_input, "not yet implemented" );
2007 return b_force_update;
2010 /*****************************************************************************
2012 *****************************************************************************/
2013 static int UpdateFromDemux( input_thread_t *p_input )
2015 demux_t *p_demux = p_input->p->input.p_demux;
2018 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2020 v.i_int = p_demux->info.i_title;
2021 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2023 input_ControlVarTitle( p_input, p_demux->info.i_title );
2025 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2027 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2029 v.i_int = p_demux->info.i_seekpoint;
2030 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2032 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2034 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2036 /* Hmmm only works with master input */
2037 if( p_input->p->input.p_demux == p_demux )
2039 int i_title_end = p_input->p->input.i_title_end -
2040 p_input->p->input.i_title_offset;
2041 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2042 p_input->p->input.i_seekpoint_offset;
2044 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
2046 if( p_demux->info.i_title > i_title_end ||
2047 ( p_demux->info.i_title == i_title_end &&
2048 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2050 else if( i_seekpoint_end >=0 )
2052 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2054 else if( i_title_end >= 0 )
2056 if( p_demux->info.i_title > i_title_end ) return 0;
2063 /*****************************************************************************
2065 *****************************************************************************/
2066 static int UpdateFromAccess( input_thread_t *p_input )
2068 access_t *p_access = p_input->p->input.p_access;
2071 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2073 v.i_int = p_access->info.i_title;
2074 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2076 input_ControlVarTitle( p_input, p_access->info.i_title );
2078 stream_AccessUpdate( p_input->p->input.p_stream );
2080 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2082 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2084 v.i_int = p_access->info.i_seekpoint;
2085 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2086 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2088 if( p_access->info.i_update & INPUT_UPDATE_META )
2090 /* TODO maybe multi - access ? */
2091 vlc_meta_t *p_meta = vlc_meta_New();
2092 access_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2093 InputUpdateMeta( p_input, p_meta );
2094 var_SetInteger( pl_Get( p_input ), "item-change", p_input->p->input.p_item->i_id );
2095 p_access->info.i_update &= ~INPUT_UPDATE_META;
2098 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2100 /* Hmmm only works with master input */
2101 if( p_input->p->input.p_access == p_access )
2103 int i_title_end = p_input->p->input.i_title_end -
2104 p_input->p->input.i_title_offset;
2105 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2106 p_input->p->input.i_seekpoint_offset;
2108 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2110 if( p_access->info.i_title > i_title_end ||
2111 ( p_access->info.i_title == i_title_end &&
2112 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2114 else if( i_seekpoint_end >=0 )
2116 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2118 else if( i_title_end >= 0 )
2120 if( p_access->info.i_title > i_title_end ) return 0;
2127 /*****************************************************************************
2129 *****************************************************************************/
2130 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2132 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2134 if( !p_input->b_preparsing )
2136 pl_Yield( p_input );
2137 var_SetInteger( pl_Get( p_input ), "item-change",
2138 p_input->p->input.p_item->i_id );
2139 pl_Release( p_input );
2143 /*****************************************************************************
2145 *****************************************************************************/
2146 static input_source_t *InputSourceNew( input_thread_t *p_input )
2148 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2152 msg_Err( p_input, "out of memory for new input source" );
2157 in->p_access = NULL;
2158 in->p_stream = NULL;
2160 in->b_title_demux = false;
2161 TAB_INIT( in->i_title, in->title );
2162 in->b_can_pause = true;
2163 in->b_can_pace_control = true;
2164 in->b_can_rate_control = true;
2165 in->b_rescale_ts = true;
2168 in->i_cr_average = 0;
2173 /*****************************************************************************
2175 *****************************************************************************/
2176 static int InputSourceInit( input_thread_t *p_input,
2177 input_source_t *in, const char *psz_mrl,
2178 const char *psz_forced_demux )
2180 char psz_dup[strlen (psz_mrl) + 1];
2181 const char *psz_access;
2182 const char *psz_demux;
2189 strcpy( psz_dup, psz_mrl );
2191 if( !in ) return VLC_EGENERIC;
2192 if( !p_input ) return VLC_EGENERIC;
2195 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2197 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2198 psz_mrl, psz_access, psz_demux, psz_path );
2199 if( !p_input->b_preparsing )
2201 /* Hack to allow udp://@:port syntax */
2203 (strncmp( psz_access, "udp", 3 ) &&
2204 strncmp( psz_access, "rtp", 3 )) )
2206 /* Find optional titles and seekpoints */
2207 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2208 &in->i_seekpoint_start, &in->i_seekpoint_end );
2211 if( psz_forced_demux && *psz_forced_demux )
2213 psz_demux = psz_forced_demux;
2215 else if( *psz_demux == '\0' )
2217 /* special hack for forcing a demuxer with --demux=module
2218 * (and do nothing with a list) */
2219 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2221 if( psz_var_demux != NULL &&
2222 !strchr(psz_var_demux, ',' ) &&
2223 !strchr(psz_var_demux, ':' ) )
2225 psz_demux = psz_var_demux;
2227 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2231 /* Try access_demux if no demux given */
2232 if( *psz_demux == '\0' )
2234 in->p_demux = demux_New( p_input, psz_access, psz_demux, psz_path,
2235 NULL, p_input->p->p_es_out, false );
2240 /* Preparsing is only for file:// */
2243 if( !*psz_access ) /* path without scheme:// */
2244 psz_access = "file";
2245 if( strcmp( psz_access, "file" ) )
2247 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2252 int64_t i_pts_delay;
2254 /* Get infos from access_demux */
2255 demux_Control( in->p_demux,
2256 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2257 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2259 in->b_title_demux = true;
2260 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2261 &in->title, &in->i_title,
2262 &in->i_title_offset, &in->i_seekpoint_offset ) )
2267 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2268 &in->b_can_pace_control ) )
2269 in->b_can_pace_control = false;
2271 if( !in->b_can_pace_control )
2273 if( demux_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2274 &in->b_can_rate_control, &in->b_rescale_ts ) )
2276 in->b_can_rate_control = false;
2277 in->b_rescale_ts = true; /* not used */
2282 in->b_can_rate_control = true;
2283 in->b_rescale_ts = true;
2285 if( demux_Control( in->p_demux, DEMUX_CAN_PAUSE,
2286 &in->b_can_pause ) )
2287 in->b_can_pause = false;
2288 var_SetBool( p_input, "can-pause", in->b_can_pause );
2290 int ret = demux_Control( in->p_demux, DEMUX_CAN_SEEK,
2292 if( ret != VLC_SUCCESS )
2294 var_Set( p_input, "seekable", val );
2298 int64_t i_pts_delay;
2300 input_ChangeState( p_input, OPENING_S );
2302 /* Now try a real access */
2303 in->p_access = access_New( p_input, psz_access, psz_demux, psz_path );
2305 /* Access failed, URL encoded ? */
2306 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2308 decode_URI( psz_path );
2310 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2311 psz_access, psz_demux, psz_path );
2313 in->p_access = access_New( p_input,
2314 psz_access, psz_demux, psz_path );
2316 if( in->p_access == NULL )
2318 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2320 intf_UserFatal( VLC_OBJECT( p_input), false,
2321 _("Your input can't be opened"),
2322 _("VLC is unable to open the MRL '%s'."
2323 " Check the log for details."), psz_mrl );
2328 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2329 while( psz && *psz )
2331 access_t *p_access = in->p_access;
2332 char *end = strchr( psz, ':' );
2337 in->p_access = access_FilterNew( in->p_access, psz );
2338 if( in->p_access == NULL )
2340 in->p_access = p_access;
2341 msg_Warn( p_input, "failed to insert access filter %s",
2349 /* Get infos from access */
2350 if( !p_input->b_preparsing )
2352 access_Control( in->p_access,
2353 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2354 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2356 in->b_title_demux = false;
2357 if( access_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2358 &in->title, &in->i_title,
2359 &in->i_title_offset, &in->i_seekpoint_offset ) )
2365 access_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2366 &in->b_can_pace_control );
2367 in->b_can_rate_control = in->b_can_pace_control;
2368 in->b_rescale_ts = true;
2370 access_Control( in->p_access, ACCESS_CAN_PAUSE,
2372 var_SetBool( p_input, "can-pause", in->b_can_pause );
2373 access_Control( in->p_access, ACCESS_CAN_SEEK,
2375 var_Set( p_input, "seekable", val );
2378 input_ChangeState( p_input, BUFFERING_S );
2380 /* Create the stream_t */
2381 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2382 if( in->p_stream == NULL )
2384 msg_Warn( p_input, "cannot create a stream_t from access" );
2388 /* Open a demuxer */
2389 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2391 psz_demux = in->p_access->psz_demux;
2395 /* Take access redirections into account */
2396 char *psz_real_path;
2397 char *psz_buf = NULL;
2398 if( in->p_access->psz_path )
2400 const char *psz_a, *psz_d;
2401 psz_buf = strdup( in->p_access->psz_path );
2402 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2406 psz_real_path = psz_path;
2408 in->p_demux = demux_New( p_input, psz_access, psz_demux,
2410 in->p_stream, p_input->p->p_es_out,
2411 p_input->b_preparsing );
2415 if( in->p_demux == NULL )
2417 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2418 psz_access, psz_demux, psz_path );
2419 intf_UserFatal( VLC_OBJECT( p_input ), false,
2420 _("VLC can't recognize the input's format"),
2421 _("The format of '%s' cannot be detected. "
2422 "Have a look the log for details."), psz_mrl );
2426 /* Get title from demux */
2427 if( !p_input->b_preparsing && in->i_title <= 0 )
2429 if( demux_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2430 &in->title, &in->i_title,
2431 &in->i_title_offset, &in->i_seekpoint_offset ))
2433 TAB_INIT( in->i_title, in->title );
2437 in->b_title_demux = true;
2443 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2444 if( 1 || !p_input->b_preparsing )
2447 input_attachment_t **attachment;
2448 if( !demux_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2449 &attachment, &i_attachment ) )
2451 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2452 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2453 i_attachment, attachment );
2454 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2457 if( !demux_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2459 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2461 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2464 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2465 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2470 input_ChangeState( p_input, ERROR_S );
2473 demux_Delete( in->p_demux );
2476 stream_Delete( in->p_stream );
2479 access_Delete( in->p_access );
2481 return VLC_EGENERIC;
2484 /*****************************************************************************
2486 *****************************************************************************/
2487 static void InputSourceClean( input_source_t *in )
2492 demux_Delete( in->p_demux );
2495 stream_Delete( in->p_stream );
2498 access_Delete( in->p_access );
2500 if( in->i_title > 0 )
2502 for( i = 0; i < in->i_title; i++ )
2503 vlc_input_title_Delete( in->title[i] );
2504 TAB_CLEAN( in->i_title, in->title );
2508 static void SlaveDemux( input_thread_t *p_input )
2513 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2515 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2519 for( i = 0; i < p_input->p->i_slave; i++ )
2521 input_source_t *in = p_input->p->slave[i];
2527 if( demux_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2532 if( demux_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2534 msg_Err( p_input, "slave[%d] doesn't like "
2535 "DEMUX_GET_TIME -> EOF", i );
2540 if( i_stime >= i_time )
2543 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2549 i_ret = in->p_demux->pf_demux( in->p_demux );
2554 msg_Dbg( p_input, "slave %d EOF", i );
2560 static void SlaveSeek( input_thread_t *p_input )
2565 if( !p_input ) return;
2567 if( demux_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2569 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2573 for( i = 0; i < p_input->p->i_slave; i++ )
2575 input_source_t *in = p_input->p->slave[i];
2577 if( demux_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2579 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2585 /*****************************************************************************
2587 *****************************************************************************/
2588 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2592 if( !p_meta ) return;
2594 /* Get meta information from user */
2595 #define GET_META( field, s ) \
2596 var_Get( p_input, (s), &val ); \
2597 if( *val.psz_string ) \
2598 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2599 free( val.psz_string )
2601 GET_META( Title, "meta-title" );
2602 GET_META( Artist, "meta-artist" );
2603 GET_META( Genre, "meta-genre" );
2604 GET_META( Copyright, "meta-copyright" );
2605 GET_META( Description, "meta-description" );
2606 GET_META( Date, "meta-date" );
2607 GET_META( URL, "meta-url" );
2611 /*****************************************************************************
2612 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2613 * arturl and locking issue.
2614 *****************************************************************************/
2615 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2617 input_item_t *p_item = p_input->p->input.p_item;
2618 char * psz_arturl = NULL;
2619 char *psz_title = NULL;
2621 int i_arturl_event = false;
2626 psz_arturl = input_item_GetArtURL( p_item );
2628 vlc_mutex_lock( &p_item->lock );
2629 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2630 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2632 vlc_meta_Merge( p_item->p_meta, p_meta );
2634 if( psz_arturl && *psz_arturl )
2636 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2637 i_arturl_event = true;
2640 vlc_meta_Delete( p_meta );
2642 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2644 /* Don't look for art cover if sout
2645 * XXX It can change when sout has meta data support */
2646 if( p_input->p->p_sout && !p_input->b_preparsing )
2648 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2649 i_arturl_event = true;
2653 input_ExtractAttachmentAndCacheArt( p_input );
2659 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2661 p_meta = vlc_meta_New();
2662 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2664 vlc_mutex_unlock( &p_item->lock );
2666 input_item_SetPreparsed( p_item, true );
2668 if( i_arturl_event == true )
2672 /* Notify interested third parties */
2673 event.type = vlc_InputItemMetaChanged;
2674 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2675 vlc_event_send( &p_item->event_manager, &event );
2680 input_Control( p_input, INPUT_SET_NAME, psz_title );
2686 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2687 for( i = 0; ppsz_all_keys[i]; i++ )
2689 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2690 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2691 free( ppsz_all_keys[i] );
2693 free( ppsz_all_keys );
2694 vlc_meta_Delete( p_meta );
2697 /** \todo handle sout meta */
2701 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2702 int i_new, input_attachment_t **pp_new )
2704 int i_attachment = *pi_attachment;
2705 input_attachment_t **attachment = *ppp_attachment;
2708 attachment = realloc( attachment,
2709 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2710 for( i = 0; i < i_new; i++ )
2711 attachment[i_attachment++] = pp_new[i];
2715 *pi_attachment = i_attachment;
2716 *ppp_attachment = attachment;
2719 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2726 /* XXX I am not sure it is a great idea, besides, there is more than that
2727 * if we want to do it right */
2728 vlc_mutex_lock( &p_item->lock );
2729 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2731 vlc_mutex_unlock( &p_item->lock );
2734 vlc_mutex_unlock( &p_item->lock );
2737 demux_Control( p_demux, DEMUX_GET_META, p_meta );
2738 if( demux_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2743 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2744 if(! p_demux->p_private )
2747 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2750 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2752 if( p_demux_meta->p_meta )
2754 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2755 vlc_meta_Delete( p_demux_meta->p_meta );
2758 if( p_demux_meta->i_attachments > 0 )
2760 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2761 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2762 p_demux_meta->i_attachments, p_demux_meta->attachments );
2763 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2765 module_Unneed( p_demux, p_id3 );
2767 free( p_demux->p_private );
2771 /*****************************************************************************
2772 * MRLSplit: parse the access, demux and url part of the
2773 * Media Resource Locator.
2774 *****************************************************************************/
2775 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2778 char *psz_access = NULL;
2779 char *psz_demux = NULL;
2782 /* Either there is an access/demux specification before ://
2783 * or we have a plain local file path. */
2784 psz_path = strstr( psz_dup, "://" );
2785 if( psz_path != NULL )
2788 psz_path += 3; /* skips "://" */
2790 /* Separate access from demux (<access>/<demux>://<path>) */
2791 psz_access = psz_dup;
2792 psz_demux = strchr( psz_access, '/' );
2794 *psz_demux++ = '\0';
2801 *ppsz_access = psz_access ? psz_access : "";
2802 *ppsz_demux = psz_demux ? psz_demux : "";
2803 *ppsz_path = psz_path ? psz_path : "";
2806 /*****************************************************************************
2807 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2810 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2811 *****************************************************************************/
2812 static void MRLSections( input_thread_t *p_input, char *psz_source,
2813 int *pi_title_start, int *pi_title_end,
2814 int *pi_chapter_start, int *pi_chapter_end )
2816 char *psz, *psz_end, *psz_next, *psz_check;
2818 *pi_title_start = *pi_title_end = -1;
2819 *pi_chapter_start = *pi_chapter_end = -1;
2821 /* Start by parsing titles and chapters */
2822 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2824 /* Check we are really dealing with a title/chapter section */
2825 psz_check = psz + 1;
2826 if( !*psz_check ) return;
2827 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2828 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2829 if( *psz_check == ':' && ++psz_check )
2830 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2831 if( *psz_check != '-' && *psz_check ) return;
2832 if( *psz_check == '-' && ++psz_check )
2833 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2834 if( *psz_check != ':' && *psz_check ) return;
2835 if( *psz_check == ':' && ++psz_check )
2836 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2837 if( *psz_check ) return;
2839 /* Separate start and end */
2841 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2843 /* Look for the start title */
2844 *pi_title_start = strtol( psz, &psz_next, 0 );
2845 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2846 *pi_title_end = *pi_title_start;
2849 /* Look for the start chapter */
2851 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2852 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2853 *pi_chapter_end = *pi_chapter_start;
2857 /* Look for the end title */
2858 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2859 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2862 /* Look for the end chapter */
2863 if( *psz_end ) psz_end++;
2864 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2865 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2868 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2869 psz_source, *pi_title_start, *pi_chapter_start,
2870 *pi_title_end, *pi_chapter_end );
2873 /*****************************************************************************
2874 * input_AddSubtitles: add a subtitles file and enable it
2875 *****************************************************************************/
2876 bool input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2877 bool b_check_extension )
2879 input_source_t *sub;
2882 char *psz_path, *psz_extension;
2884 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2889 /* if we are provided a subtitle.sub file,
2890 * see if we don't have a subtitle.idx and use it instead */
2891 psz_path = strdup( psz_subtitle );
2894 psz_extension = strrchr( psz_path, '.');
2895 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2899 strcpy( psz_extension, ".idx" );
2900 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2901 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2904 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2905 psz_path, psz_subtitle );
2906 strcpy( psz_subtitle, psz_path );
2912 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2914 sub = InputSourceNew( p_input );
2915 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2917 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2920 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2922 if( count.i_int == 0 )
2924 /* if it was first one, there is disable too */
2926 if( count.i_int < list.p_list->i_count )
2928 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2929 &list.p_list->p_values[count.i_int] );
2931 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );