1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include "input_internal.h"
35 #include "../stream_output/stream_output.h"
37 #include <vlc_playlist.h>
38 #include <vlc_interface.h>
40 #include <vlc_demux.h>
41 #include <vlc_charset.h>
43 #ifdef HAVE_SYS_STAT_H
44 # include <sys/stat.h>
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Run ( input_thread_t *p_input );
51 static int RunAndDestroy ( input_thread_t *p_input );
53 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
54 const char *, vlc_bool_t, sout_instance_t * );
55 static int Init ( input_thread_t *p_input );
56 static void Error ( input_thread_t *p_input );
57 static void End ( input_thread_t *p_input );
58 static void MainLoop( input_thread_t *p_input );
59 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout );
61 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
62 static void ControlReduce( input_thread_t * );
63 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
65 static int UpdateFromAccess( input_thread_t * );
66 static int UpdateFromDemux( input_thread_t * );
68 static void UpdateItemLength( input_thread_t *, int64_t i_length );
70 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
72 static input_source_t *InputSourceNew( input_thread_t *);
73 static int InputSourceInit( input_thread_t *, input_source_t *,
74 const char *, const char *psz_forced_demux );
75 static void InputSourceClean( input_source_t * );
77 //static void InputGetAttachments( input_thread_t *, input_source_t * );
79 static void SlaveDemux( input_thread_t *p_input );
80 static void SlaveSeek( input_thread_t *p_input );
82 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
83 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
85 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t * );
86 static void SoutKeep( sout_instance_t * );
88 /*****************************************************************************
89 * This function creates a new input, and returns a pointer
90 * to its description. On error, it returns NULL.
92 * Variables for _public_ use:
95 * - rate,rate-slower, rate-faster
96 * - position, position-offset
98 * - title,title-next,title-prev
99 * - chapter,chapter-next, chapter-prev
100 * - program, audio-es, video-es, spu-es
101 * - audio-delay, spu-delay
106 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
107 * * For intf callback upon changes
109 * TODO explain when Callback is called
110 * TODO complete this list (?)
111 *****************************************************************************/
112 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
113 const char *psz_header, vlc_bool_t b_quick, sout_instance_t *p_sout )
115 input_thread_t *p_input = NULL; /* thread descriptor */
119 /* Allocate descriptor */
120 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
121 if( p_input == NULL )
123 msg_Err( p_parent, "out of memory" );
126 MALLOC_NULL( p_input->p, input_thread_private_t );
128 /* One "randomly" selected input thread is responsible for computing
129 * the global stats. Check if there is already someone doing this */
130 if( p_input->p_libvlc->p_playlist->p_stats && !b_quick )
132 vlc_mutex_lock( &p_input->p_libvlc->p_playlist->p_stats->lock );
133 if( p_input->p_libvlc->p_playlist->p_stats_computer == NULL )
135 p_input->p_libvlc->p_playlist->p_stats_computer = p_input;
137 vlc_mutex_unlock( &p_input->p_libvlc->p_playlist->p_stats->lock );
140 p_input->b_preparsing = b_quick;
141 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
143 /* Init Common fields */
144 p_input->b_eof = VLC_FALSE;
145 p_input->b_can_pace_control = VLC_TRUE;
146 p_input->p->i_start = 0;
148 p_input->p->i_stop = 0;
149 p_input->p->i_run = 0;
150 p_input->p->i_title = 0;
151 p_input->p->title = NULL;
152 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
153 p_input->i_state = INIT_S;
154 p_input->p->i_rate = INPUT_RATE_DEFAULT;
155 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
156 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
157 p_input->p->p_es_out = NULL;
158 p_input->p->p_sout = NULL;
159 p_input->p->b_sout_keep = VLC_FALSE;
160 p_input->p->b_out_pace_control = VLC_FALSE;
161 p_input->i_pts_delay = 0;
163 /* Init Input fields */
164 p_input->p->input.p_item = p_item;
165 p_input->p->input.p_access = NULL;
166 p_input->p->input.p_stream = NULL;
167 p_input->p->input.p_demux = NULL;
168 p_input->p->input.b_title_demux = VLC_FALSE;
169 p_input->p->input.i_title = 0;
170 p_input->p->input.title = NULL;
171 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
172 p_input->p->input.b_can_pace_control = VLC_TRUE;
173 p_input->p->input.b_eof = VLC_FALSE;
174 p_input->p->input.i_cr_average = 0;
176 vlc_mutex_lock( &p_item->lock );
178 if( !p_item->p_stats )
179 p_item->p_stats = stats_NewInputStats( p_input );
180 vlc_mutex_unlock( &p_item->lock );
183 p_input->p->i_slave = 0;
184 p_input->p->slave = NULL;
186 /* Init control buffer */
187 vlc_mutex_init( p_input, &p_input->p->lock_control );
188 p_input->p->i_control = 0;
190 /* Parse input options */
191 vlc_mutex_lock( &p_item->lock );
192 for( i = 0; i < p_item->i_options; i++ )
193 var_OptionParse( p_input, p_item->ppsz_options[i] );
194 vlc_mutex_unlock( &p_item->lock );
196 /* Create Object Variables for private use only */
197 input_ConfigVarInit( p_input );
199 vlc_mutex_lock( &p_item->lock );
200 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
201 var_SetBool( p_input, "meta-preparsed", VLC_TRUE );
202 vlc_mutex_unlock( &p_item->lock );
205 /* Create Objects variables for public Get and Set */
206 input_ControlVarInit( p_input );
208 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
210 if( !p_input->b_preparsing )
212 var_Get( p_input, "bookmarks", &val );
215 /* FIXME: have a common cfg parsing routine used by sout and others */
216 char *psz_parser, *psz_start, *psz_end;
217 psz_parser = val.psz_string;
218 while( (psz_start = strchr( psz_parser, '{' ) ) )
220 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
223 psz_end = strchr( psz_start, '}' );
224 if( !psz_end ) break;
225 psz_parser = psz_end + 1;
226 backup = *psz_parser;
229 while( (psz_end = strchr( psz_start, ',' ) ) )
232 if( !strncmp( psz_start, "name=", 5 ) )
234 p_seekpoint->psz_name = strdup(psz_start + 5);
236 else if( !strncmp( psz_start, "bytes=", 6 ) )
238 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
240 else if( !strncmp( psz_start, "time=", 5 ) )
242 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
245 psz_start = psz_end + 1;
247 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
248 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
249 p_seekpoint->i_time_offset );
250 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
251 vlc_seekpoint_Delete( p_seekpoint );
252 *psz_parser = backup;
254 free( val.psz_string );
258 /* Remove 'Now playing' info as it is probably outdated */
259 input_Control( p_input, INPUT_DEL_INFO,
260 _(VLC_META_INFO_CAT),
261 _(VLC_META_NOW_PLAYING) );
262 input_item_SetNowPlaying( p_item, NULL );
265 if( p_input->b_preparsing )
266 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
270 p_input->p->p_sout = p_sout;
272 /* Attach only once we are ready */
273 vlc_object_attach( p_input, p_parent );
278 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout )
280 vlc_object_detach( p_input );
281 input_thread_private_t *priv = p_input->p;
288 *pp_sout = priv->p_sout;
289 else if( priv->b_sout_keep )
290 SoutKeep( priv->p_sout );
292 sout_DeleteInstance( priv->p_sout );
295 vlc_object_destroy( p_input );
296 vlc_mutex_destroy( &priv->lock_control );
301 * Initialize an input thread and run it. You will need to monitor the
302 * thread to clean up after it is done
304 * \param p_parent a vlc_object
305 * \param p_item an input item
306 * \return a pointer to the spawned input thread
308 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
309 input_item_t *p_item )
311 vlc_bool_t b_sout_keep;
312 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
313 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
315 if( !p_input && p_sout )
318 p_input->p->b_sout_keep = b_sout_keep;
323 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
324 input_item_t *p_item,
325 const char *psz_log, sout_instance_t *p_sout )
327 input_thread_t *p_input;
329 p_input = Create( p_parent, p_item, psz_log, VLC_FALSE, p_sout );
333 /* Create thread and wait for its readiness. */
334 if( vlc_thread_create( p_input, "input", Run,
335 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
337 input_ChangeState( p_input, ERROR_S );
338 msg_Err( p_input, "cannot create input thread" );
339 Destroy( p_input, &p_sout );
347 * Initialize an input thread and run it. This thread will clean after himself,
348 * you can forget about it. It can work either in blocking or non-blocking mode
350 * \param p_parent a vlc_object
351 * \param p_item an input item
352 * \param b_block should we block until read is finished ?
353 * \return the input object id if non blocking, an error code else
355 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
358 vlc_bool_t b_sout_keep;
359 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
360 input_thread_t *p_input;
362 p_input = Create( p_parent, p_item, NULL, VLC_FALSE, p_sout );
363 if( !p_input && p_sout )
368 p_input->p->b_sout_keep = b_sout_keep;
372 RunAndDestroy( p_input );
377 if( vlc_thread_create( p_input, "input", RunAndDestroy,
378 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
380 input_ChangeState( p_input, ERROR_S );
381 msg_Err( p_input, "cannot create input thread" );
382 Destroy( p_input, NULL );
386 return p_input->i_object_id;
390 * Initialize an input and initialize it to preparse the item
391 * This function is blocking. It will only accept to parse files
393 * \param p_parent a vlc_object_t
394 * \param p_item an input item
395 * \return VLC_SUCCESS or an error
397 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
399 input_thread_t *p_input;
401 /* Allocate descriptor */
402 p_input = Create( p_parent, p_item, NULL, VLC_TRUE, NULL );
406 if( !Init( p_input ) )
409 Destroy( p_input, NULL );
415 * Request a running input thread to stop and die
417 * \param the input thread to stop
419 void input_StopThread( input_thread_t *p_input )
424 /* Set die for input */
425 vlc_object_kill( p_input );
426 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
428 /* We cannot touch p_input fields directly (we come from another thread),
429 * so use the vlc_object_find way, it's perfectly safe */
431 /* Set die for all access */
432 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
433 for( i = 0; i < p_list->i_count; i++ )
435 vlc_object_kill( p_list->p_values[i].p_object );
437 vlc_list_release( p_list );
439 /* Set die for all stream */
440 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
441 for( i = 0; i < p_list->i_count; i++ )
443 vlc_object_kill( p_list->p_values[i].p_object );
445 vlc_list_release( p_list );
447 /* Set die for all demux */
448 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
449 for( i = 0; i < p_list->i_count; i++ )
451 vlc_object_kill( p_list->p_values[i].p_object );
453 vlc_list_release( p_list );
455 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
459 * Clean up a dead input thread
460 * This function does not return until the thread is effectively cancelled.
462 * \param the input thread to kill
464 void input_DestroyThread( input_thread_t *p_input )
466 input_DestroyThreadExtended( p_input, NULL );
469 void input_DestroyThreadExtended( input_thread_t *p_input, sout_instance_t **pp_sout )
471 /* Join the thread */
472 vlc_thread_join( p_input );
475 Destroy( p_input, pp_sout );
478 /*****************************************************************************
479 * Run: main thread loop
480 * This is the "normal" thread that spawns the input processing chain,
481 * reads the stream, cleans up and waits
482 *****************************************************************************/
483 static int Run( input_thread_t *p_input )
485 /* Signal that the thread is launched */
486 vlc_thread_ready( p_input );
488 if( Init( p_input ) )
490 /* If we failed, wait before we are killed, and exit */
491 p_input->b_error = VLC_TRUE;
492 playlist_Signal( pl_Get( p_input ) );
496 /* Tell we're dead */
497 p_input->b_dead = VLC_TRUE;
504 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
506 /* We have finish to demux data but not to play them */
507 while( !p_input->b_die )
509 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
512 msg_Dbg( p_input, "waiting decoder fifos to empty" );
514 msleep( INPUT_IDLE_SLEEP );
517 /* We have finished */
518 p_input->b_eof = VLC_TRUE;
519 playlist_Signal( pl_Get( p_input ) );
522 /* Wait until we are asked to die */
523 if( !p_input->b_die )
534 /*****************************************************************************
535 * RunAndDestroy: main thread loop
536 * This is the "just forget me" thread that spawns the input processing chain,
537 * reads the stream, cleans up and releases memory
538 *****************************************************************************/
539 static int RunAndDestroy( input_thread_t *p_input )
541 /* Signal that the thread is launched */
542 vlc_thread_ready( p_input );
544 if( Init( p_input ) )
549 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
551 /* We have finished demuxing data but not playing it */
552 while( !p_input->b_die )
554 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
557 msg_Dbg( p_input, "waiting decoder fifos to empty" );
559 msleep( INPUT_IDLE_SLEEP );
562 /* We have finished */
563 p_input->b_eof = VLC_TRUE;
571 Destroy( p_input, NULL );
575 /*****************************************************************************
576 * Main loop: Fill buffers from access, and demux
577 *****************************************************************************/
578 static void MainLoop( input_thread_t *p_input )
580 int64_t i_start_mdate = mdate();
581 int64_t i_intf_update = 0;
584 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
586 vlc_bool_t b_force_update = VLC_FALSE;
592 if( p_input->i_state != PAUSE_S )
594 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
595 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
598 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
603 if( p_input->p->input.b_title_demux &&
604 p_input->p->input.p_demux->info.i_update )
606 i_ret = UpdateFromDemux( p_input );
607 b_force_update = VLC_TRUE;
609 else if( !p_input->p->input.b_title_demux &&
610 p_input->p->input.p_access &&
611 p_input->p->input.p_access->info.i_update )
613 i_ret = UpdateFromAccess( p_input );
614 b_force_update = VLC_TRUE;
618 if( i_ret == 0 ) /* EOF */
622 var_Get( p_input, "input-repeat", &repeat );
623 if( repeat.i_int == 0 )
625 /* End of file - we do not set b_die because only the
626 * playlist is allowed to do so. */
627 input_ChangeState( p_input, END_S );
628 msg_Dbg( p_input, "EOF reached" );
629 p_input->p->input.b_eof = VLC_TRUE;
633 msg_Dbg( p_input, "repeating the same input (%d)",
635 if( repeat.i_int > 0 )
638 var_Set( p_input, "input-repeat", repeat );
641 /* Seek to start title/seekpoint */
642 val.i_int = p_input->p->input.i_title_start -
643 p_input->p->input.i_title_offset;
644 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
646 input_ControlPush( p_input,
647 INPUT_CONTROL_SET_TITLE, &val );
649 val.i_int = p_input->p->input.i_seekpoint_start -
650 p_input->p->input.i_seekpoint_offset;
651 if( val.i_int > 0 /* TODO: check upper boundary */ )
652 input_ControlPush( p_input,
653 INPUT_CONTROL_SET_SEEKPOINT, &val );
655 /* Seek to start position */
656 if( p_input->p->i_start > 0 )
658 val.i_time = p_input->p->i_start;
659 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
665 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
670 i_start_mdate = mdate();
675 p_input->b_error = VLC_TRUE;
678 if( i_ret > 0 && p_input->p->i_slave > 0 )
680 SlaveDemux( p_input );
690 vlc_mutex_lock( &p_input->p->lock_control );
691 ControlReduce( p_input );
692 while( !ControlPopNoLock( p_input, &i_type, &val ) )
694 msg_Dbg( p_input, "control type=%d", i_type );
695 if( Control( p_input, i_type, val ) )
696 b_force_update = VLC_TRUE;
698 vlc_mutex_unlock( &p_input->p->lock_control );
700 if( b_force_update || i_intf_update < mdate() )
704 int64_t i_time, i_length;
705 /* update input status variables */
706 if( !demux2_Control( p_input->p->input.p_demux,
707 DEMUX_GET_POSITION, &f_pos ) )
709 val.f_float = (float)f_pos;
710 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
712 if( !demux2_Control( p_input->p->input.p_demux,
713 DEMUX_GET_TIME, &i_time ) )
715 p_input->i_time = i_time;
717 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
719 if( !demux2_Control( p_input->p->input.p_demux,
720 DEMUX_GET_LENGTH, &i_length ) )
723 var_Get( p_input, "length", &old_val );
724 val.i_time = i_length;
725 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
727 if( old_val.i_time != val.i_time )
729 UpdateItemLength( p_input, i_length );
733 var_SetBool( p_input, "intf-change", VLC_TRUE );
734 i_intf_update = mdate() + I64C(150000);
736 /* 150ms * 8 = ~ 1 second */
737 if( ++i_updates % 8 == 0 )
739 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
740 /* Are we the thread responsible for computing global stats ? */
741 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
743 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
744 p_input->p_libvlc->p_playlist->p_stats );
750 static int Init( input_thread_t * p_input )
760 /* Initialize optional stream output. (before access/demuxer)
761 * XXX: we add a special case if the uri starts by vlc.
762 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
763 * be destroyed in case of a file).
764 * (this will break playing of file starting by 'vlc:' but I don't
765 * want to add more logic, just force file by file:// or code it ;)
767 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
768 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
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 );
841 if( !p_input->p->p_sout )
843 input_ChangeState( p_input, ERROR_S );
844 msg_Err( p_input, "cannot start stream output instance, " \
851 if( p_input->p_libvlc->b_stats )
853 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
854 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
855 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
856 if( p_input->p->counters.p_sout_send_bitrate )
857 p_input->p->counters.p_sout_send_bitrate->update_interval =
861 else if( p_input->p->p_sout )
863 msg_Dbg( p_input, "destroying useless sout" );
865 sout_DeleteInstance( p_input->p->p_sout );
866 p_input->p->p_sout = NULL;
872 p_input->p->p_es_out = input_EsOutNew( p_input );
873 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
874 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
876 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
877 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
879 if( InputSourceInit( p_input, &p_input->p->input,
880 p_input->p->input.p_item->psz_uri, NULL ) )
885 /* Create global title (from master) */
886 if( !p_input->b_preparsing )
888 p_input->p->i_title = p_input->p->input.i_title;
889 p_input->p->title = p_input->p->input.title;
890 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
891 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
892 if( p_input->p->i_title > 0 )
894 /* Setup variables */
895 input_ControlVarNavigation( p_input );
896 input_ControlVarTitle( p_input, 0 );
900 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
901 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
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 );
1049 if( subs ) free( subs );
1050 if( psz_autopath ) 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 demux2_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
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 demux2_Control( p_input->p->slave[i]->p_demux,
1179 DEMUX_GET_META, p_meta );
1180 if( p_input->p->slave[i]->p_access )
1182 access2_Control( p_input->p->slave[i]->p_access,
1183 ACCESS_GET_META, p_meta );
1188 InputUpdateMeta( p_input, p_meta );
1190 if( !p_input->b_preparsing )
1192 msg_Dbg( p_input, "`%s' successfully opened",
1193 p_input->p->input.p_item->psz_uri );
1197 /* initialization is complete */
1198 input_ChangeState( p_input, PLAYING_S );
1203 input_ChangeState( p_input, ERROR_S );
1205 if( p_input->p->p_es_out )
1206 input_EsOutDelete( p_input->p->p_es_out );
1208 if( p_input->p->p_sout )
1210 vlc_object_detach( p_input->p->p_sout );
1211 sout_DeleteInstance( p_input->p->p_sout );
1215 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1217 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1218 stats_CounterClean( p_input->p->counters.p_##c );\
1219 p_input->p->counters.p_##c = NULL; } while(0)
1220 EXIT_COUNTER( read_bytes );
1221 EXIT_COUNTER( read_packets );
1222 EXIT_COUNTER( demux_read );
1223 EXIT_COUNTER( input_bitrate );
1224 EXIT_COUNTER( demux_bitrate );
1225 EXIT_COUNTER( played_abuffers );
1226 EXIT_COUNTER( lost_abuffers );
1227 EXIT_COUNTER( displayed_pictures );
1228 EXIT_COUNTER( lost_pictures );
1229 EXIT_COUNTER( decoded_audio );
1230 EXIT_COUNTER( decoded_video );
1231 EXIT_COUNTER( decoded_sub );
1233 if( p_input->p->p_sout )
1235 EXIT_COUNTER( sout_sent_packets );
1236 EXIT_COUNTER (sout_sent_bytes );
1237 EXIT_COUNTER( sout_send_bitrate );
1242 /* Mark them deleted */
1243 p_input->p->input.p_demux = NULL;
1244 p_input->p->input.p_stream = NULL;
1245 p_input->p->input.p_access = NULL;
1246 p_input->p->p_es_out = NULL;
1247 p_input->p->p_sout = NULL;
1249 return VLC_EGENERIC;
1252 /*****************************************************************************
1253 * Error: RunThread() error loop
1254 *****************************************************************************
1255 * This function is called when an error occurred during thread main's loop.
1256 *****************************************************************************/
1257 static void Error( input_thread_t *p_input )
1259 while( !p_input->b_die )
1262 input_ChangeState( p_input, ERROR_S );
1263 msleep( INPUT_IDLE_SLEEP );
1267 /*****************************************************************************
1268 * End: end the input thread
1269 *****************************************************************************/
1270 static void End( input_thread_t * p_input )
1274 /* We are at the end */
1275 input_ChangeState( p_input, END_S );
1277 /* Clean control variables */
1278 input_ControlVarClean( p_input );
1280 /* Clean up master */
1281 InputSourceClean( &p_input->p->input );
1284 for( i = 0; i < p_input->p->i_slave; i++ )
1286 InputSourceClean( p_input->p->slave[i] );
1287 free( p_input->p->slave[i] );
1289 if( p_input->p->slave ) free( p_input->p->slave );
1291 /* Unload all modules */
1292 if( p_input->p->p_es_out )
1293 input_EsOutDelete( p_input->p->p_es_out );
1295 if( !p_input->b_preparsing )
1297 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1298 if( p_input->p_libvlc->b_stats )
1300 /* make sure we are up to date */
1301 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1302 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1304 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1305 p_input->p_libvlc->p_playlist->p_stats );
1306 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1308 CL_CO( read_bytes );
1309 CL_CO( read_packets );
1310 CL_CO( demux_read );
1311 CL_CO( input_bitrate );
1312 CL_CO( demux_bitrate );
1313 CL_CO( played_abuffers );
1314 CL_CO( lost_abuffers );
1315 CL_CO( displayed_pictures );
1316 CL_CO( lost_pictures );
1317 CL_CO( decoded_audio) ;
1318 CL_CO( decoded_video );
1319 CL_CO( decoded_sub) ;
1322 /* Close optional stream output instance */
1323 if( p_input->p->p_sout )
1325 CL_CO( sout_sent_packets );
1326 CL_CO( sout_sent_bytes );
1327 CL_CO( sout_send_bitrate );
1329 vlc_object_detach( p_input->p->p_sout );
1334 if( p_input->p->i_attachment > 0 )
1336 for( i = 0; i < p_input->p->i_attachment; i++ )
1337 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1338 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1341 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
1343 /* Tell we're dead */
1344 p_input->b_dead = VLC_TRUE;
1347 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t *pb_sout_keep )
1349 vlc_bool_t b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1350 sout_instance_t *p_sout = NULL;
1353 /* Search sout-keep options
1354 * XXX it has to be done here, but it is duplicated work :( */
1355 vlc_mutex_lock( &p_item->lock );
1356 for( i = 0; i < p_item->i_options; i++ )
1358 const char *psz_option = p_item->ppsz_options[i];
1361 if( *psz_option == ':' )
1364 if( !strcmp( psz_option, "sout-keep" ) )
1365 b_keep_sout = VLC_TRUE;
1366 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1367 b_keep_sout = VLC_FALSE;
1369 vlc_mutex_unlock( &p_item->lock );
1371 /* Find a potential sout to reuse
1372 * XXX it might be unusable but this will be checked later */
1375 /* Remove the sout from the playlist garbage collector */
1376 playlist_t *p_playlist = pl_Yield( p_parent );
1378 vlc_mutex_lock( &p_playlist->gc_lock );
1379 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1382 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1384 vlc_object_release( p_sout );
1389 vlc_object_detach( p_sout ); /* Remove it from the GC */
1391 vlc_object_release( p_sout );
1394 vlc_mutex_unlock( &p_playlist->gc_lock );
1396 pl_Release( p_parent );
1400 *pb_sout_keep = b_keep_sout;
1404 static void SoutKeep( sout_instance_t *p_sout )
1406 /* attach sout to the playlist */
1407 playlist_t *p_playlist = pl_Yield( p_sout );
1409 msg_Dbg( p_sout, "sout has been kept" );
1410 vlc_object_attach( p_sout, p_playlist );
1412 pl_Release( p_sout );
1415 /*****************************************************************************
1417 *****************************************************************************/
1418 static inline int ControlPopNoLock( input_thread_t *p_input,
1419 int *pi_type, vlc_value_t *p_val )
1421 if( p_input->p->i_control <= 0 )
1423 return VLC_EGENERIC;
1426 *pi_type = p_input->p->control[0].i_type;
1427 *p_val = p_input->p->control[0].val;
1429 p_input->p->i_control--;
1430 if( p_input->p->i_control > 0 )
1434 for( i = 0; i < p_input->p->i_control; i++ )
1436 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1437 p_input->p->control[i].val = p_input->p->control[i+1].val;
1444 static void ControlReduce( input_thread_t *p_input )
1451 for( i = 1; i < p_input->p->i_control; i++ )
1453 const int i_lt = p_input->p->control[i-1].i_type;
1454 const int i_ct = p_input->p->control[i].i_type;
1456 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1457 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1461 ( i_ct == INPUT_CONTROL_SET_STATE ||
1462 i_ct == INPUT_CONTROL_SET_RATE ||
1463 i_ct == INPUT_CONTROL_SET_POSITION ||
1464 i_ct == INPUT_CONTROL_SET_TIME ||
1465 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1466 i_ct == INPUT_CONTROL_SET_TITLE ||
1467 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1468 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1471 // msg_Dbg( p_input, "merged at %d", i );
1472 /* Remove the i-1 */
1473 for( j = i; j < p_input->p->i_control; j++ )
1474 p_input->p->control[j-1] = p_input->p->control[j];
1475 p_input->p->i_control--;
1479 /* TODO but that's not that important
1480 - merge SET_X with SET_X_CMD
1481 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1482 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1489 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1492 vlc_bool_t b_force_update = VLC_FALSE;
1494 if( !p_input ) return b_force_update;
1498 case INPUT_CONTROL_SET_DIE:
1499 msg_Dbg( p_input, "control: stopping input" );
1500 /* Mark all submodules to die */
1501 if( p_input->p->input.p_access )
1502 vlc_object_kill( p_input->p->input.p_access );
1503 if( p_input->p->input.p_stream )
1504 vlc_object_kill( p_input->p->input.p_stream );
1505 vlc_object_kill( p_input->p->input.p_demux );
1507 vlc_object_kill( p_input );
1510 case INPUT_CONTROL_SET_POSITION:
1511 case INPUT_CONTROL_SET_POSITION_OFFSET:
1514 if( i_type == INPUT_CONTROL_SET_POSITION )
1516 f_pos = val.f_float;
1520 /* Should not fail */
1521 demux2_Control( p_input->p->input.p_demux,
1522 DEMUX_GET_POSITION, &f_pos );
1523 f_pos += val.f_float;
1525 if( f_pos < 0.0 ) f_pos = 0.0;
1526 if( f_pos > 1.0 ) f_pos = 1.0;
1527 /* Reset the decoders states and clock sync (before calling the demuxer */
1528 input_EsOutChangePosition( p_input->p->p_es_out );
1529 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1532 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1533 "%2.1f%% failed", f_pos * 100 );
1537 if( p_input->p->i_slave > 0 )
1538 SlaveSeek( p_input );
1540 b_force_update = VLC_TRUE;
1545 case INPUT_CONTROL_SET_TIME:
1546 case INPUT_CONTROL_SET_TIME_OFFSET:
1551 if( i_type == INPUT_CONTROL_SET_TIME )
1553 i_time = val.i_time;
1557 /* Should not fail */
1558 demux2_Control( p_input->p->input.p_demux,
1559 DEMUX_GET_TIME, &i_time );
1560 i_time += val.i_time;
1562 if( i_time < 0 ) i_time = 0;
1564 /* Reset the decoders states and clock sync (before calling the demuxer */
1565 input_EsOutChangePosition( p_input->p->p_es_out );
1567 i_ret = demux2_Control( p_input->p->input.p_demux,
1568 DEMUX_SET_TIME, i_time );
1573 /* Emulate it with a SET_POS */
1574 demux2_Control( p_input->p->input.p_demux,
1575 DEMUX_GET_LENGTH, &i_length );
1578 double f_pos = (double)i_time / (double)i_length;
1579 i_ret = demux2_Control( p_input->p->input.p_demux,
1580 DEMUX_SET_POSITION, f_pos );
1585 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1586 " failed or not possible", i_time );
1590 if( p_input->p->i_slave > 0 )
1591 SlaveSeek( p_input );
1593 b_force_update = VLC_TRUE;
1598 case INPUT_CONTROL_SET_STATE:
1599 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1600 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1603 if( p_input->p->input.p_access )
1604 i_ret = access2_Control( p_input->p->input.p_access,
1605 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1607 i_ret = demux2_Control( p_input->p->input.p_demux,
1608 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1612 /* FIXME What to do ? */
1613 msg_Warn( p_input, "cannot unset pause -> EOF" );
1614 vlc_mutex_unlock( &p_input->p->lock_control );
1615 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1616 vlc_mutex_lock( &p_input->p->lock_control );
1619 b_force_update = VLC_TRUE;
1621 /* Switch to play */
1622 p_input->i_state = PLAYING_S;
1623 val.i_int = PLAYING_S;
1624 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1628 input_EsOutChangeState( p_input->p->p_es_out );
1630 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1631 p_input->p->b_can_pause )
1634 if( p_input->p->input.p_access )
1635 i_ret = access2_Control( p_input->p->input.p_access,
1636 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1638 i_ret = demux2_Control( p_input->p->input.p_demux,
1639 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1641 b_force_update = VLC_TRUE;
1645 msg_Warn( p_input, "cannot set pause state" );
1646 val.i_int = p_input->i_state;
1650 val.i_int = PAUSE_S;
1653 /* Switch to new state */
1654 p_input->i_state = val.i_int;
1655 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1659 input_EsOutChangeState( p_input->p->p_es_out );
1661 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1663 b_force_update = VLC_TRUE;
1665 /* Correct "state" value */
1666 val.i_int = p_input->i_state;
1667 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1669 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1671 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1675 case INPUT_CONTROL_SET_RATE:
1676 case INPUT_CONTROL_SET_RATE_SLOWER:
1677 case INPUT_CONTROL_SET_RATE_FASTER:
1681 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1682 i_rate = p_input->p->i_rate * 2;
1683 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1684 i_rate = p_input->p->i_rate / 2;
1688 if( i_rate < INPUT_RATE_MIN )
1690 msg_Dbg( p_input, "cannot set rate faster" );
1691 i_rate = INPUT_RATE_MIN;
1693 else if( i_rate > INPUT_RATE_MAX )
1695 msg_Dbg( p_input, "cannot set rate slower" );
1696 i_rate = INPUT_RATE_MAX;
1698 if( i_rate != INPUT_RATE_DEFAULT &&
1699 ( !p_input->b_can_pace_control ||
1700 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1702 msg_Dbg( p_input, "cannot change rate" );
1703 i_rate = INPUT_RATE_DEFAULT;
1705 if( i_rate != p_input->p->i_rate )
1708 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1710 p_input->p->i_rate = i_rate;
1712 input_EsOutChangeRate( p_input->p->p_es_out );
1714 b_force_update = VLC_TRUE;
1719 case INPUT_CONTROL_SET_PROGRAM:
1720 /* No need to force update, es_out does it if needed */
1721 es_out_Control( p_input->p->p_es_out,
1722 ES_OUT_SET_GROUP, val.i_int );
1724 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1728 case INPUT_CONTROL_SET_ES:
1729 /* No need to force update, es_out does it if needed */
1730 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1731 input_EsOutGetFromID( p_input->p->p_es_out,
1735 case INPUT_CONTROL_SET_AUDIO_DELAY:
1736 input_EsOutSetDelay( p_input->p->p_es_out,
1737 AUDIO_ES, val.i_time );
1738 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1741 case INPUT_CONTROL_SET_SPU_DELAY:
1742 input_EsOutSetDelay( p_input->p->p_es_out,
1743 SPU_ES, val.i_time );
1744 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1747 case INPUT_CONTROL_SET_TITLE:
1748 case INPUT_CONTROL_SET_TITLE_NEXT:
1749 case INPUT_CONTROL_SET_TITLE_PREV:
1750 if( p_input->p->input.b_title_demux &&
1751 p_input->p->input.i_title > 0 )
1754 /* FIXME handle demux title */
1755 demux_t *p_demux = p_input->p->input.p_demux;
1758 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1759 i_title = p_demux->info.i_title - 1;
1760 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1761 i_title = p_demux->info.i_title + 1;
1763 i_title = val.i_int;
1765 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1767 input_EsOutChangePosition( p_input->p->p_es_out );
1769 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1770 input_ControlVarTitle( p_input, i_title );
1773 else if( p_input->p->input.i_title > 0 )
1775 access_t *p_access = p_input->p->input.p_access;
1778 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1779 i_title = p_access->info.i_title - 1;
1780 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1781 i_title = p_access->info.i_title + 1;
1783 i_title = val.i_int;
1785 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1787 input_EsOutChangePosition( p_input->p->p_es_out );
1789 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1790 stream_AccessReset( p_input->p->input.p_stream );
1794 case INPUT_CONTROL_SET_SEEKPOINT:
1795 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1796 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1797 if( p_input->p->input.b_title_demux &&
1798 p_input->p->input.i_title > 0 )
1800 demux_t *p_demux = p_input->p->input.p_demux;
1802 int64_t i_input_time;
1803 int64_t i_seekpoint_time;
1805 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1807 i_seekpoint = p_demux->info.i_seekpoint;
1808 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1809 if( i_seekpoint_time >= 0 &&
1810 !demux2_Control( p_demux,
1811 DEMUX_GET_TIME, &i_input_time ) )
1813 if ( i_input_time < i_seekpoint_time + 3000000 )
1819 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1820 i_seekpoint = p_demux->info.i_seekpoint + 1;
1822 i_seekpoint = val.i_int;
1824 if( i_seekpoint >= 0 && i_seekpoint <
1825 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1828 input_EsOutChangePosition( p_input->p->p_es_out );
1830 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1833 else if( p_input->p->input.i_title > 0 )
1835 demux_t *p_demux = p_input->p->input.p_demux;
1836 access_t *p_access = p_input->p->input.p_access;
1838 int64_t i_input_time;
1839 int64_t i_seekpoint_time;
1841 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1843 i_seekpoint = p_access->info.i_seekpoint;
1844 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1845 if( i_seekpoint_time >= 0 &&
1846 demux2_Control( p_demux,
1847 DEMUX_GET_TIME, &i_input_time ) )
1849 if ( i_input_time < i_seekpoint_time + 3000000 )
1855 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1856 i_seekpoint = p_access->info.i_seekpoint + 1;
1858 i_seekpoint = val.i_int;
1860 if( i_seekpoint >= 0 && i_seekpoint <
1861 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1863 input_EsOutChangePosition( p_input->p->p_es_out );
1865 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1867 stream_AccessReset( p_input->p->input.p_stream );
1872 case INPUT_CONTROL_ADD_SLAVE:
1873 if( val.psz_string )
1875 input_source_t *slave = InputSourceNew( p_input );
1877 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1883 msg_Dbg( p_input, "adding %s as slave on the fly",
1887 if( demux2_Control( p_input->p->input.p_demux,
1888 DEMUX_GET_TIME, &i_time ) )
1890 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1891 InputSourceClean( slave );
1895 if( demux2_Control( slave->p_demux,
1896 DEMUX_SET_TIME, i_time ) )
1898 msg_Err( p_input, "seek failed for new slave" );
1899 InputSourceClean( slave );
1904 /* Get meta (access and demux) */
1905 p_meta = vlc_meta_New();
1906 access2_Control( slave->p_access, ACCESS_GET_META,
1908 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1909 InputUpdateMeta( p_input, p_meta );
1911 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1916 msg_Warn( p_input, "failed to add %s as slave",
1920 free( val.psz_string );
1924 case INPUT_CONTROL_SET_BOOKMARK:
1926 msg_Err( p_input, "not yet implemented" );
1930 return b_force_update;
1933 /*****************************************************************************
1935 *****************************************************************************/
1936 static int UpdateFromDemux( input_thread_t *p_input )
1938 demux_t *p_demux = p_input->p->input.p_demux;
1941 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1943 v.i_int = p_demux->info.i_title;
1944 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1946 input_ControlVarTitle( p_input, p_demux->info.i_title );
1948 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1950 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1952 v.i_int = p_demux->info.i_seekpoint;
1953 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1955 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1957 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1959 /* Hmmm only works with master input */
1960 if( p_input->p->input.p_demux == p_demux )
1962 int i_title_end = p_input->p->input.i_title_end -
1963 p_input->p->input.i_title_offset;
1964 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1965 p_input->p->input.i_seekpoint_offset;
1967 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1969 if( p_demux->info.i_title > i_title_end ||
1970 ( p_demux->info.i_title == i_title_end &&
1971 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1973 else if( i_seekpoint_end >=0 )
1975 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1977 else if( i_title_end >= 0 )
1979 if( p_demux->info.i_title > i_title_end ) return 0;
1986 /*****************************************************************************
1988 *****************************************************************************/
1989 static int UpdateFromAccess( input_thread_t *p_input )
1991 access_t *p_access = p_input->p->input.p_access;
1994 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1996 v.i_int = p_access->info.i_title;
1997 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1999 input_ControlVarTitle( p_input, p_access->info.i_title );
2001 stream_AccessUpdate( p_input->p->input.p_stream );
2003 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2005 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2007 v.i_int = p_access->info.i_seekpoint;
2008 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2009 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2011 if( p_access->info.i_update & INPUT_UPDATE_META )
2013 /* TODO maybe multi - access ? */
2014 vlc_meta_t *p_meta = vlc_meta_New();
2015 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2016 InputUpdateMeta( p_input, p_meta );
2017 var_SetInteger( pl_Get( p_input ), "item-change", p_input->p->input.p_item->i_id );
2018 p_access->info.i_update &= ~INPUT_UPDATE_META;
2021 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2023 /* Hmmm only works with master input */
2024 if( p_input->p->input.p_access == p_access )
2026 int i_title_end = p_input->p->input.i_title_end -
2027 p_input->p->input.i_title_offset;
2028 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2029 p_input->p->input.i_seekpoint_offset;
2031 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2033 if( p_access->info.i_title > i_title_end ||
2034 ( p_access->info.i_title == i_title_end &&
2035 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2037 else if( i_seekpoint_end >=0 )
2039 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2041 else if( i_title_end >= 0 )
2043 if( p_access->info.i_title > i_title_end ) return 0;
2050 /*****************************************************************************
2052 *****************************************************************************/
2053 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2055 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2057 if( !p_input->b_preparsing )
2059 pl_Yield( p_input );
2060 var_SetInteger( pl_Get( p_input ), "item-change",
2061 p_input->p->input.p_item->i_id );
2062 pl_Release( p_input )
2066 /*****************************************************************************
2068 *****************************************************************************/
2069 static input_source_t *InputSourceNew( input_thread_t *p_input )
2071 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2075 msg_Err( p_input, "out of memory for new input source" );
2080 in->p_access = NULL;
2081 in->p_stream = NULL;
2083 in->b_title_demux = VLC_FALSE;
2084 TAB_INIT( in->i_title, in->title );
2085 in->b_can_pace_control = VLC_TRUE;
2086 in->b_eof = VLC_FALSE;
2088 in->i_cr_average = 0;
2093 /*****************************************************************************
2095 *****************************************************************************/
2096 static int InputSourceInit( input_thread_t *p_input,
2097 input_source_t *in, const char *psz_mrl,
2098 const char *psz_forced_demux )
2100 char psz_dup[strlen (psz_mrl) + 1];
2101 const char *psz_access;
2102 const char *psz_demux;
2109 strcpy( psz_dup, psz_mrl );
2111 if( !in ) return VLC_EGENERIC;
2112 if( !p_input ) return VLC_EGENERIC;
2115 if( !p_input->b_preparsing )
2117 MRLSplit( VLC_OBJECT(p_input), psz_dup,
2118 &psz_access, &psz_demux, &psz_path );
2120 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2121 psz_mrl, psz_access, psz_demux, psz_path );
2123 /* Hack to allow udp://@:port syntax */
2125 (strncmp( psz_access, "udp", 3 ) &&
2126 strncmp( psz_access, "rtp", 3 )) )
2128 /* Find optional titles and seekpoints */
2129 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2130 &in->i_seekpoint_start, &in->i_seekpoint_end );
2132 if( psz_forced_demux && *psz_forced_demux )
2134 psz_demux = psz_forced_demux;
2136 else if( !psz_demux || *psz_demux == '\0' )
2138 /* special hack for forcing a demuxer with --demux=module
2139 * (and do nothing with a list) */
2140 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2142 if( psz_var_demux != NULL &&
2143 !strchr(psz_var_demux, ',' ) &&
2144 !strchr(psz_var_demux, ':' ) )
2146 psz_demux = psz_var_demux;
2148 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2152 /* Try access_demux if no demux given */
2153 if( *psz_demux == '\0' )
2155 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2156 NULL, p_input->p->p_es_out, VLC_FALSE );
2162 if( !strncmp( psz_path, "file://", 7 ) )
2164 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2166 psz_access = "file";
2171 int64_t i_pts_delay;
2173 /* Get infos from access_demux */
2174 demux2_Control( in->p_demux,
2175 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2176 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2178 in->b_title_demux = VLC_TRUE;
2179 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2180 &in->title, &in->i_title,
2181 &in->i_title_offset, &in->i_seekpoint_offset ) )
2186 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2187 &in->b_can_pace_control );
2188 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2192 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2198 int64_t i_pts_delay;
2200 input_ChangeState( p_input, OPENING_S );
2202 /* Now try a real access */
2203 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2204 p_input->b_preparsing );
2206 /* Access failed, URL encoded ? */
2207 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2209 decode_URI( psz_path );
2211 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2212 psz_access, psz_demux, psz_path );
2214 in->p_access = access2_New( p_input,
2215 psz_access, psz_demux, psz_path,
2216 p_input->b_preparsing );
2218 /* access failed, maybe our access detection was wrong.
2219 * Retry with the full name */
2220 if( in->p_access == NULL && strchr( psz_mrl, ':' ) )
2222 msg_Dbg( p_input, "retrying with access `' demux `' path `%s'",
2224 in->p_access = access2_New( p_input,
2226 p_input->b_preparsing );
2228 if( in->p_access == NULL )
2230 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2232 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2233 _("Your input can't be opened"),
2234 _("VLC is unable to open the MRL '%s'."
2235 " Check the log for details."), psz_mrl );
2240 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2241 while( psz && *psz )
2243 access_t *p_access = in->p_access;
2244 char *end = strchr( psz, ':' );
2249 in->p_access = access2_FilterNew( in->p_access, psz );
2250 if( in->p_access == NULL )
2252 in->p_access = p_access;
2253 msg_Warn( p_input, "failed to insert access filter %s",
2261 /* Get infos from access */
2262 if( !p_input->b_preparsing )
2264 access2_Control( in->p_access,
2265 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2266 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2268 in->b_title_demux = VLC_FALSE;
2269 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2270 &in->title, &in->i_title,
2271 &in->i_title_offset, &in->i_seekpoint_offset ) )
2277 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2278 &in->b_can_pace_control );
2279 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2281 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2283 var_Set( p_input, "seekable", val );
2286 input_ChangeState( p_input, BUFFERING_S );
2288 /* Create the stream_t */
2289 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2290 if( in->p_stream == NULL )
2292 msg_Warn( p_input, "cannot create a stream_t from access" );
2296 /* Open a demuxer */
2297 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2299 psz_demux = in->p_access->psz_demux;
2303 /* Take access redirections into account */
2304 char *psz_real_path;
2305 char *psz_buf = NULL;
2306 if( in->p_access->psz_path )
2308 const char *psz_a, *psz_d;
2309 psz_buf = strdup( in->p_access->psz_path );
2310 MRLSplit( VLC_OBJECT(p_input), psz_buf,
2311 &psz_a, &psz_d, &psz_real_path );
2315 psz_real_path = psz_path;
2317 in->p_demux = demux2_New( p_input, psz_access, psz_demux,
2319 in->p_stream, p_input->p->p_es_out,
2320 p_input->b_preparsing );
2324 if( in->p_demux == NULL )
2326 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2327 psz_access, psz_demux, psz_path );
2328 intf_UserFatal( VLC_OBJECT( p_input ), VLC_FALSE,
2329 _("Can't recognize the input's format"),
2330 _("The format of '%s' can't be detected. "
2331 "Have a look the log for details."), psz_mrl );
2335 /* Get title from demux */
2336 if( !p_input->b_preparsing && in->i_title <= 0 )
2338 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2339 &in->title, &in->i_title,
2340 &in->i_title_offset, &in->i_seekpoint_offset ))
2342 TAB_INIT( in->i_title, in->title );
2346 in->b_title_demux = VLC_TRUE;
2352 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2353 if( 1 || !p_input->b_preparsing )
2356 input_attachment_t **attachment;
2357 if( !demux2_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2358 &attachment, &i_attachment ) )
2361 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2362 p_input->p->attachment = realloc( p_input->p->attachment,
2363 sizeof(input_attachment_t**) * ( p_input->p->i_attachment + i_attachment ) );
2364 for( i = 0; i < i_attachment; i++ )
2365 p_input->p->attachment[p_input->p->i_attachment++] = attachment[i];
2368 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2371 if( !demux2_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2373 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2375 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2378 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2379 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2384 input_ChangeState( p_input, ERROR_S );
2387 demux2_Delete( in->p_demux );
2390 stream_Delete( in->p_stream );
2393 access2_Delete( in->p_access );
2395 return VLC_EGENERIC;
2398 /*****************************************************************************
2400 *****************************************************************************/
2401 static void InputSourceClean( input_source_t *in )
2406 demux2_Delete( in->p_demux );
2409 stream_Delete( in->p_stream );
2412 access2_Delete( in->p_access );
2414 if( in->i_title > 0 )
2416 for( i = 0; i < in->i_title; i++ )
2417 vlc_input_title_Delete( in->title[i] );
2418 TAB_CLEAN( in->i_title, in->title );
2422 static void SlaveDemux( input_thread_t *p_input )
2427 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2429 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2433 for( i = 0; i < p_input->p->i_slave; i++ )
2435 input_source_t *in = p_input->p->slave[i];
2441 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2446 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2448 msg_Err( p_input, "slave[%d] doesn't like "
2449 "DEMUX_GET_TIME -> EOF", i );
2454 if( i_stime >= i_time )
2457 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2463 i_ret = in->p_demux->pf_demux( in->p_demux );
2468 msg_Dbg( p_input, "slave %d EOF", i );
2469 in->b_eof = VLC_TRUE;
2474 static void SlaveSeek( input_thread_t *p_input )
2479 if( !p_input ) return;
2481 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2483 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2487 for( i = 0; i < p_input->p->i_slave; i++ )
2489 input_source_t *in = p_input->p->slave[i];
2491 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2493 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2494 in->b_eof = VLC_TRUE;
2499 /*****************************************************************************
2501 *****************************************************************************/
2502 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2506 if( !p_meta ) return;
2508 /* Get meta information from user */
2509 #define GET_META( field, s ) \
2510 var_Get( p_input, (s), &val ); \
2511 if( *val.psz_string ) \
2512 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2513 free( val.psz_string )
2515 GET_META( Title, "meta-title" );
2516 GET_META( Artist, "meta-artist" );
2517 GET_META( Genre, "meta-genre" );
2518 GET_META( Copyright, "meta-copyright" );
2519 GET_META( Description, "meta-description" );
2520 GET_META( Date, "meta-date" );
2521 GET_META( URL, "meta-url" );
2525 /*****************************************************************************
2526 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2527 * arturl and locking issue.
2528 *****************************************************************************/
2529 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2531 input_item_t *p_item = p_input->p->input.p_item;
2532 char * psz_arturl = NULL;
2533 char *psz_title = NULL;
2535 int i_arturl_event = VLC_FALSE;
2540 psz_arturl = input_item_GetArtURL( p_item );
2542 vlc_mutex_lock( &p_item->lock );
2543 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2544 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2546 vlc_meta_Merge( p_item->p_meta, p_meta );
2548 if( psz_arturl && *psz_arturl )
2550 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2551 i_arturl_event = VLC_TRUE;
2554 vlc_meta_Delete( p_meta );
2556 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2558 /* Don't look for art cover if sout
2559 * XXX It can change when sout has meta data support */
2560 if( p_input->p->p_sout && !p_input->b_preparsing )
2562 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2563 i_arturl_event = VLC_TRUE;
2567 input_ExtractAttachmentAndCacheArt( p_input );
2571 input_item_SetPreparsed( p_item, VLC_TRUE );
2575 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2577 p_meta = vlc_meta_New();
2578 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2580 vlc_mutex_unlock( &p_item->lock );
2582 if( i_arturl_event == VLC_TRUE )
2586 /* Notify interested third parties */
2587 event.type = vlc_InputItemMetaChanged;
2588 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2589 vlc_event_send( &p_item->event_manager, &event );
2594 input_Control( p_input, INPUT_SET_NAME, psz_title );
2600 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2601 for( i = 0; ppsz_all_keys[i]; i++ )
2603 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2604 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2605 free( ppsz_all_keys[i] );
2607 free( ppsz_all_keys );
2608 vlc_meta_Delete( p_meta );
2611 /** \todo handle sout meta */
2615 static inline vlc_bool_t IsValidAccess( const char *psz )
2619 while( ( c = *psz ) != '\0' )
2624 if( ( !isascii( c ) || !isalnum( c ) ) && c != '-' && ( c != '/' ) )
2628 /* should not happen though */
2633 /*****************************************************************************
2634 * MRLSplit: parse the access, demux and url part of the
2635 * Media Resource Locator.
2636 *****************************************************************************/
2637 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2638 const char **ppsz_access, const char **ppsz_demux,
2641 const char *psz_access = "";
2642 const char *psz_demux = "";
2646 psz = strchr( psz_dup, ':' );
2650 /* Guess whether ':' is part of a local filename, or separates
2651 * access/demux from path */
2652 if( !IsValidAccess( psz_dup ) )
2654 #if defined( WIN32 ) || defined( UNDER_CE )
2655 else if( ( psz - psz_dup == 1 ) && isalpha( psz_dup[0] ) )
2657 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2666 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2670 psz = strchr( psz_dup, '/' );
2677 psz_access = psz_dup;
2684 *ppsz_access = psz_access;
2685 *ppsz_demux = psz_demux;
2686 *ppsz_path = psz_path;
2689 /*****************************************************************************
2690 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2693 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2694 *****************************************************************************/
2695 static void MRLSections( input_thread_t *p_input, char *psz_source,
2696 int *pi_title_start, int *pi_title_end,
2697 int *pi_chapter_start, int *pi_chapter_end )
2699 char *psz, *psz_end, *psz_next, *psz_check;
2701 *pi_title_start = *pi_title_end = -1;
2702 *pi_chapter_start = *pi_chapter_end = -1;
2704 /* Start by parsing titles and chapters */
2705 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2707 /* Check we are really dealing with a title/chapter section */
2708 psz_check = psz + 1;
2709 if( !*psz_check ) return;
2710 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2711 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2712 if( *psz_check == ':' && ++psz_check )
2713 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2714 if( *psz_check != '-' && *psz_check ) return;
2715 if( *psz_check == '-' && ++psz_check )
2716 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2717 if( *psz_check != ':' && *psz_check ) return;
2718 if( *psz_check == ':' && ++psz_check )
2719 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2720 if( *psz_check ) return;
2722 /* Separate start and end */
2724 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2726 /* Look for the start title */
2727 *pi_title_start = strtol( psz, &psz_next, 0 );
2728 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2729 *pi_title_end = *pi_title_start;
2732 /* Look for the start chapter */
2734 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2735 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2736 *pi_chapter_end = *pi_chapter_start;
2740 /* Look for the end title */
2741 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2742 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2745 /* Look for the end chapter */
2746 if( *psz_end ) psz_end++;
2747 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2748 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2751 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2752 psz_source, *pi_title_start, *pi_chapter_start,
2753 *pi_title_end, *pi_chapter_end );
2756 /*****************************************************************************
2757 * input_AddSubtitles: add a subtitles file and enable it
2758 *****************************************************************************/
2759 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2760 vlc_bool_t b_check_extension )
2762 input_source_t *sub;
2765 char *psz_path, *psz_extension;
2767 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2772 /* if we are provided a subtitle.sub file,
2773 * see if we don't have a subtitle.idx and use it instead */
2774 psz_path = strdup( psz_subtitle );
2777 psz_extension = strrchr( psz_path, '.');
2778 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2782 strcpy( psz_extension, ".idx" );
2783 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2784 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2787 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2788 psz_path, psz_subtitle );
2789 strcpy( psz_subtitle, psz_path );
2795 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2797 sub = InputSourceNew( p_input );
2798 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2800 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2803 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2805 if( count.i_int == 0 )
2807 /* if it was first one, there is disable too */
2809 if( count.i_int < list.p_list->i_count )
2811 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2812 &list.p_list->p_values[count.i_int] );
2814 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );