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 /* Create Objects variables for public Get and Set */
200 input_ControlVarInit( p_input );
202 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
204 if( !p_input->b_preparsing )
206 var_Get( p_input, "bookmarks", &val );
209 /* FIXME: have a common cfg parsing routine used by sout and others */
210 char *psz_parser, *psz_start, *psz_end;
211 psz_parser = val.psz_string;
212 while( (psz_start = strchr( psz_parser, '{' ) ) )
214 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
217 psz_end = strchr( psz_start, '}' );
218 if( !psz_end ) break;
219 psz_parser = psz_end + 1;
220 backup = *psz_parser;
223 while( (psz_end = strchr( psz_start, ',' ) ) )
226 if( !strncmp( psz_start, "name=", 5 ) )
228 p_seekpoint->psz_name = strdup(psz_start + 5);
230 else if( !strncmp( psz_start, "bytes=", 6 ) )
232 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
234 else if( !strncmp( psz_start, "time=", 5 ) )
236 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
239 psz_start = psz_end + 1;
241 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
242 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
243 p_seekpoint->i_time_offset );
244 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
245 vlc_seekpoint_Delete( p_seekpoint );
246 *psz_parser = backup;
248 free( val.psz_string );
252 /* Remove 'Now playing' info as it is probably outdated */
253 input_Control( p_input, INPUT_DEL_INFO,
254 _(VLC_META_INFO_CAT),
255 _(VLC_META_NOW_PLAYING) );
256 input_item_SetNowPlaying( p_item, NULL );
259 if( p_input->b_preparsing )
260 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
264 p_input->p->p_sout = p_sout;
266 /* Attach only once we are ready */
267 vlc_object_attach( p_input, p_parent );
272 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout )
274 vlc_object_detach( p_input );
275 input_thread_private_t *priv = p_input->p;
282 *pp_sout = priv->p_sout;
283 else if( priv->b_sout_keep )
284 SoutKeep( priv->p_sout );
286 sout_DeleteInstance( priv->p_sout );
289 vlc_object_destroy( p_input );
290 vlc_mutex_destroy( &priv->lock_control );
295 * Initialize an input thread and run it. You will need to monitor the
296 * thread to clean up after it is done
298 * \param p_parent a vlc_object
299 * \param p_item an input item
300 * \return a pointer to the spawned input thread
302 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
303 input_item_t *p_item )
305 vlc_bool_t b_sout_keep;
306 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
307 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
309 if( !p_input && p_sout )
312 p_input->p->b_sout_keep = b_sout_keep;
317 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
318 input_item_t *p_item,
319 const char *psz_log, sout_instance_t *p_sout )
321 input_thread_t *p_input;
323 p_input = Create( p_parent, p_item, psz_log, VLC_FALSE, p_sout );
327 /* Create thread and wait for its readiness. */
328 if( vlc_thread_create( p_input, "input", Run,
329 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
331 input_ChangeState( p_input, ERROR_S );
332 msg_Err( p_input, "cannot create input thread" );
333 Destroy( p_input, &p_sout );
341 * Initialize an input thread and run it. This thread will clean after himself,
342 * you can forget about it. It can work either in blocking or non-blocking mode
344 * \param p_parent a vlc_object
345 * \param p_item an input item
346 * \param b_block should we block until read is finished ?
347 * \return the input object id if non blocking, an error code else
349 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
352 vlc_bool_t b_sout_keep;
353 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
354 input_thread_t *p_input;
356 p_input = Create( p_parent, p_item, NULL, VLC_FALSE, p_sout );
357 if( !p_input && p_sout )
362 p_input->p->b_sout_keep = b_sout_keep;
366 RunAndDestroy( p_input );
371 if( vlc_thread_create( p_input, "input", RunAndDestroy,
372 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
374 input_ChangeState( p_input, ERROR_S );
375 msg_Err( p_input, "cannot create input thread" );
376 Destroy( p_input, NULL );
380 return p_input->i_object_id;
384 * Initialize an input and initialize it to preparse the item
385 * This function is blocking. It will only accept to parse files
387 * \param p_parent a vlc_object_t
388 * \param p_item an input item
389 * \return VLC_SUCCESS or an error
391 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
393 input_thread_t *p_input;
395 /* Allocate descriptor */
396 p_input = Create( p_parent, p_item, NULL, VLC_TRUE, NULL );
400 if( !Init( p_input ) )
403 Destroy( p_input, NULL );
409 * Request a running input thread to stop and die
411 * \param the input thread to stop
413 void input_StopThread( input_thread_t *p_input )
418 /* Set die for input */
419 vlc_object_kill( p_input );
420 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
422 /* We cannot touch p_input fields directly (we come from another thread),
423 * so use the vlc_object_find way, it's perfectly safe */
425 /* Set die for all access */
426 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
427 for( i = 0; i < p_list->i_count; i++ )
429 vlc_object_kill( p_list->p_values[i].p_object );
431 vlc_list_release( p_list );
433 /* Set die for all stream */
434 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
435 for( i = 0; i < p_list->i_count; i++ )
437 vlc_object_kill( p_list->p_values[i].p_object );
439 vlc_list_release( p_list );
441 /* Set die for all demux */
442 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
443 for( i = 0; i < p_list->i_count; i++ )
445 vlc_object_kill( p_list->p_values[i].p_object );
447 vlc_list_release( p_list );
449 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
453 * Clean up a dead input thread
454 * This function does not return until the thread is effectively cancelled.
456 * \param the input thread to kill
458 void input_DestroyThread( input_thread_t *p_input )
460 input_DestroyThreadExtended( p_input, NULL );
463 void input_DestroyThreadExtended( input_thread_t *p_input, sout_instance_t **pp_sout )
465 /* Join the thread */
466 vlc_thread_join( p_input );
469 Destroy( p_input, pp_sout );
472 /*****************************************************************************
473 * Run: main thread loop
474 * This is the "normal" thread that spawns the input processing chain,
475 * reads the stream, cleans up and waits
476 *****************************************************************************/
477 static int Run( input_thread_t *p_input )
479 /* Signal that the thread is launched */
480 vlc_thread_ready( p_input );
482 if( Init( p_input ) )
484 /* If we failed, wait before we are killed, and exit */
485 p_input->b_error = VLC_TRUE;
486 playlist_Signal( pl_Get( p_input ) );
490 /* Tell we're dead */
491 p_input->b_dead = VLC_TRUE;
498 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
500 /* We have finish to demux data but not to play them */
501 while( !p_input->b_die )
503 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
506 msg_Dbg( p_input, "waiting decoder fifos to empty" );
508 msleep( INPUT_IDLE_SLEEP );
511 /* We have finished */
512 p_input->b_eof = VLC_TRUE;
513 playlist_Signal( pl_Get( p_input ) );
516 /* Wait until we are asked to die */
517 if( !p_input->b_die )
528 /*****************************************************************************
529 * RunAndDestroy: main thread loop
530 * This is the "just forget me" thread that spawns the input processing chain,
531 * reads the stream, cleans up and releases memory
532 *****************************************************************************/
533 static int RunAndDestroy( input_thread_t *p_input )
535 /* Signal that the thread is launched */
536 vlc_thread_ready( p_input );
538 if( Init( p_input ) )
543 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
545 /* We have finished demuxing data but not playing it */
546 while( !p_input->b_die )
548 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
551 msg_Dbg( p_input, "waiting decoder fifos to empty" );
553 msleep( INPUT_IDLE_SLEEP );
556 /* We have finished */
557 p_input->b_eof = VLC_TRUE;
565 Destroy( p_input, NULL );
569 /*****************************************************************************
570 * Main loop: Fill buffers from access, and demux
571 *****************************************************************************/
572 static void MainLoop( input_thread_t *p_input )
574 int64_t i_start_mdate = mdate();
575 int64_t i_intf_update = 0;
578 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
580 vlc_bool_t b_force_update = VLC_FALSE;
586 if( p_input->i_state != PAUSE_S )
588 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
589 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
592 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
597 if( p_input->p->input.b_title_demux &&
598 p_input->p->input.p_demux->info.i_update )
600 i_ret = UpdateFromDemux( p_input );
601 b_force_update = VLC_TRUE;
603 else if( !p_input->p->input.b_title_demux &&
604 p_input->p->input.p_access &&
605 p_input->p->input.p_access->info.i_update )
607 i_ret = UpdateFromAccess( p_input );
608 b_force_update = VLC_TRUE;
612 if( i_ret == 0 ) /* EOF */
616 var_Get( p_input, "input-repeat", &repeat );
617 if( repeat.i_int == 0 )
619 /* End of file - we do not set b_die because only the
620 * playlist is allowed to do so. */
621 input_ChangeState( p_input, END_S );
622 msg_Dbg( p_input, "EOF reached" );
623 p_input->p->input.b_eof = VLC_TRUE;
627 msg_Dbg( p_input, "repeating the same input (%d)",
629 if( repeat.i_int > 0 )
632 var_Set( p_input, "input-repeat", repeat );
635 /* Seek to start title/seekpoint */
636 val.i_int = p_input->p->input.i_title_start -
637 p_input->p->input.i_title_offset;
638 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
640 input_ControlPush( p_input,
641 INPUT_CONTROL_SET_TITLE, &val );
643 val.i_int = p_input->p->input.i_seekpoint_start -
644 p_input->p->input.i_seekpoint_offset;
645 if( val.i_int > 0 /* TODO: check upper boundary */ )
646 input_ControlPush( p_input,
647 INPUT_CONTROL_SET_SEEKPOINT, &val );
649 /* Seek to start position */
650 if( p_input->p->i_start > 0 )
652 val.i_time = p_input->p->i_start;
653 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
659 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
664 i_start_mdate = mdate();
669 p_input->b_error = VLC_TRUE;
672 if( i_ret > 0 && p_input->p->i_slave > 0 )
674 SlaveDemux( p_input );
684 vlc_mutex_lock( &p_input->p->lock_control );
685 ControlReduce( p_input );
686 while( !ControlPopNoLock( p_input, &i_type, &val ) )
688 msg_Dbg( p_input, "control type=%d", i_type );
689 if( Control( p_input, i_type, val ) )
690 b_force_update = VLC_TRUE;
692 vlc_mutex_unlock( &p_input->p->lock_control );
694 if( b_force_update || i_intf_update < mdate() )
698 int64_t i_time, i_length;
699 /* update input status variables */
700 if( !demux2_Control( p_input->p->input.p_demux,
701 DEMUX_GET_POSITION, &f_pos ) )
703 val.f_float = (float)f_pos;
704 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
706 if( !demux2_Control( p_input->p->input.p_demux,
707 DEMUX_GET_TIME, &i_time ) )
709 p_input->i_time = i_time;
711 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
713 if( !demux2_Control( p_input->p->input.p_demux,
714 DEMUX_GET_LENGTH, &i_length ) )
717 var_Get( p_input, "length", &old_val );
718 val.i_time = i_length;
719 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
721 if( old_val.i_time != val.i_time )
723 UpdateItemLength( p_input, i_length );
727 var_SetBool( p_input, "intf-change", VLC_TRUE );
728 i_intf_update = mdate() + I64C(150000);
730 /* 150ms * 8 = ~ 1 second */
731 if( ++i_updates % 8 == 0 )
733 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
734 /* Are we the thread responsible for computing global stats ? */
735 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
737 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
738 p_input->p_libvlc->p_playlist->p_stats );
744 static int Init( input_thread_t * p_input )
754 /* Initialize optional stream output. (before access/demuxer)
755 * XXX: we add a special case if the uri starts by vlc.
756 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
757 * be destroyed in case of a file).
758 * (this will break playing of file starting by 'vlc:' but I don't
759 * want to add more logic, just force file by file:// or code it ;)
761 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
762 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
764 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
766 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
768 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
769 var_SetString( p_input, "sout", "" );
770 var_SetBool( p_input, "sout-all", VLC_FALSE );
771 var_SetString( p_input, "input-slave", "" );
772 var_SetInteger( p_input, "input-repeat", 0 );
773 var_SetString( p_input, "sub-file", "" );
774 var_SetBool( p_input, "sub-autodetect-file", VLC_FALSE );
778 if( !p_input->b_preparsing )
780 /* Prepare statistics */
781 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
782 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
783 if( p_input->p_libvlc->b_stats )
785 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
786 INIT_COUNTER( read_packets, INTEGER, COUNTER );
787 INIT_COUNTER( demux_read, INTEGER, COUNTER );
788 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
789 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
790 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
791 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
792 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
793 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
794 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
795 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
796 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
797 p_input->p->counters.p_sout_send_bitrate = NULL;
798 p_input->p->counters.p_sout_sent_packets = NULL;
799 p_input->p->counters.p_sout_sent_bytes = NULL;
800 if( p_input->p->counters.p_demux_bitrate )
801 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
802 if( p_input->p->counters.p_input_bitrate )
803 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
806 /* Find a usable sout and attach it to p_input */
807 psz = var_GetNonEmptyString( p_input, "sout" );
808 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
810 /* Check the validity of the provided sout */
811 if( p_input->p->p_sout )
813 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
815 msg_Dbg( p_input, "destroying unusable sout" );
817 sout_DeleteInstance( p_input->p->p_sout );
818 p_input->p->p_sout = NULL;
822 if( p_input->p->p_sout )
825 msg_Dbg( p_input, "sout keep: reusing sout" );
826 msg_Dbg( p_input, "sout keep: you probably want to use "
827 "gather stream_out" );
828 vlc_object_attach( p_input->p->p_sout, p_input );
832 /* Create a new one */
833 p_input->p->p_sout = sout_NewInstance( p_input, psz );
835 if( !p_input->p->p_sout )
837 input_ChangeState( p_input, ERROR_S );
838 msg_Err( p_input, "cannot start stream output instance, " \
845 if( p_input->p_libvlc->b_stats )
847 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
848 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
849 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
850 if( p_input->p->counters.p_sout_send_bitrate )
851 p_input->p->counters.p_sout_send_bitrate->update_interval =
855 else if( p_input->p->p_sout )
857 msg_Dbg( p_input, "destroying useless sout" );
859 sout_DeleteInstance( p_input->p->p_sout );
860 p_input->p->p_sout = NULL;
866 p_input->p->p_es_out = input_EsOutNew( p_input );
867 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
868 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
870 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
871 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
873 if( InputSourceInit( p_input, &p_input->p->input,
874 p_input->p->input.p_item->psz_uri, NULL ) )
879 /* Create global title (from master) */
880 if( !p_input->b_preparsing )
882 p_input->p->i_title = p_input->p->input.i_title;
883 p_input->p->title = p_input->p->input.title;
884 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
885 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
886 if( p_input->p->i_title > 0 )
888 /* Setup variables */
889 input_ControlVarNavigation( p_input );
890 input_ControlVarTitle( p_input, 0 );
894 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
895 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
898 if( p_input->i_pts_delay < 0 )
899 p_input->i_pts_delay = 0;
901 /* If the desynchronisation requested by the user is < 0, we need to
902 * cache more data. */
903 var_Get( p_input, "audio-desync", &val );
904 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
906 /* Update cr_average depending on the caching */
907 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
908 p_input->p->input.i_cr_average /= 10;
909 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
912 /* Load master infos */
914 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
915 &val.i_time ) && val.i_time > 0 )
917 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
918 UpdateItemLength( p_input, val.i_time );
922 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
924 { /* fallback: gets length from metadata */
925 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
926 UpdateItemLength( p_input, val.i_time );
930 /* Start title/chapter */
931 if( !p_input->b_preparsing )
933 val.i_int = p_input->p->input.i_title_start -
934 p_input->p->input.i_title_offset;
935 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
936 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
937 val.i_int = p_input->p->input.i_seekpoint_start -
938 p_input->p->input.i_seekpoint_offset;
939 if( val.i_int > 0 /* TODO: check upper boundary */ )
940 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
944 p_input->p->i_start = I64C(1000000) * var_GetInteger( p_input, "start-time" );
945 p_input->p->i_stop = I64C(1000000) * var_GetInteger( p_input, "stop-time" );
946 p_input->p->i_run = I64C(1000000) * var_GetInteger( p_input, "run-time" );
947 if( p_input->p->i_run < 0 )
949 msg_Warn( p_input, "invalid run-time ignored" );
950 p_input->p->i_run = 0;
953 if( p_input->p->i_start > 0 )
955 if( p_input->p->i_start >= val.i_time )
957 msg_Warn( p_input, "invalid start-time ignored" );
963 msg_Dbg( p_input, "starting at time: %ds",
964 (int)( p_input->p->i_start / I64C(1000000) ) );
966 s.i_time = p_input->p->i_start;
967 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
970 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
972 msg_Warn( p_input, "invalid stop-time ignored" );
973 p_input->p->i_stop = 0;
977 /* Get fps and set it if not already set */
978 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
981 float f_requested_fps;
983 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
984 var_SetFloat( p_input, "sub-original-fps", f_fps );
986 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
987 if( f_requested_fps != f_fps )
989 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
991 var_SetFloat( p_input, "sub-fps", f_requested_fps );
995 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
998 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1001 /* Look for and add subtitle files */
1002 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1003 if( psz_subtitle != NULL )
1005 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1006 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
1009 var_Get( p_input, "sub-autodetect-file", &val );
1012 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1013 char **subs = subtitles_Detect( p_input, psz_autopath,
1014 p_input->p->input.p_item->psz_uri );
1015 input_source_t *sub;
1017 if( psz_autopath == NULL )
1018 psz_autopath = strdup("");
1020 /* Try to autoselect the first autodetected subtitles file
1021 * if no subtitles file was specified */
1022 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1024 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
1029 /* Then, just add the following subtitles files */
1030 for( ; subs && subs[i]; i++ )
1032 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1034 sub = InputSourceNew( p_input );
1035 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1037 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1043 if( subs ) free( subs );
1044 if( psz_autopath ) free( psz_autopath );
1046 free( psz_subtitle );
1048 /* Look for slave */
1049 psz = var_GetNonEmptyString( p_input, "input-slave" );
1053 input_source_t *slave;
1054 while( psz && *psz )
1056 while( *psz == ' ' || *psz == '#' )
1060 if( ( psz_delim = strchr( psz, '#' ) ) )
1062 *psz_delim++ = '\0';
1069 msg_Dbg( p_input, "adding slave input '%s'", psz );
1070 slave = InputSourceNew( p_input );
1071 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1073 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1083 p_input->p->i_start = 0;
1084 p_input->p->i_start = 0;
1088 if( !p_input->b_preparsing )
1090 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1091 i_es_out_mode = ES_OUT_MODE_AUTO;
1093 if( p_input->p->p_sout )
1095 var_Get( p_input, "sout-all", &val );
1098 i_es_out_mode = ES_OUT_MODE_ALL;
1103 var_Get( p_input, "programs", &val );
1104 if ( val.p_list && val.p_list->i_count )
1106 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1107 /* Note : we should remove the "program" callback. */
1110 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1114 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1116 /* Inform the demuxer about waited group (needed only for DVB) */
1117 if( i_es_out_mode == ES_OUT_MODE_ALL )
1119 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1121 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1123 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1128 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1129 (int) var_GetInteger( p_input, "program" ), NULL );
1132 if( p_input->p->p_sout )
1134 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1136 p_input->p->b_out_pace_control = VLC_FALSE;
1140 p_input->p->b_out_pace_control = VLC_TRUE;
1143 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1145 /* We don't want a high input priority here or we'll
1146 * end-up sucking up all the CPU time */
1147 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1150 msg_Dbg( p_input, "starting in %s mode",
1151 p_input->p->b_out_pace_control ? "async" : "sync" );
1155 p_meta = vlc_meta_New();
1156 /* Get meta data from users */
1157 InputMetaUser( p_input, p_meta );
1159 /* Get meta data from master input */
1160 demux2_Control( p_input->p->input.p_demux, DEMUX_GET_META, p_meta );
1162 /* Access_file does not give any meta, and there are no slave */
1163 if( !p_input->b_preparsing )
1165 if( p_input->p->input.p_access )
1166 access2_Control( p_input->p->input.p_access, ACCESS_GET_META,
1169 /* Get meta data from slave input */
1170 for( i = 0; i < p_input->p->i_slave; i++ )
1172 demux2_Control( p_input->p->slave[i]->p_demux,
1173 DEMUX_GET_META, p_meta );
1174 if( p_input->p->slave[i]->p_access )
1176 access2_Control( p_input->p->slave[i]->p_access,
1177 ACCESS_GET_META, p_meta );
1182 InputUpdateMeta( p_input, p_meta );
1184 if( !p_input->b_preparsing )
1186 msg_Dbg( p_input, "`%s' successfully opened",
1187 p_input->p->input.p_item->psz_uri );
1191 /* initialization is complete */
1192 input_ChangeState( p_input, PLAYING_S );
1197 input_ChangeState( p_input, ERROR_S );
1199 if( p_input->p->p_es_out )
1200 input_EsOutDelete( p_input->p->p_es_out );
1202 if( p_input->p->p_sout )
1204 vlc_object_detach( p_input->p->p_sout );
1205 sout_DeleteInstance( p_input->p->p_sout );
1209 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1211 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1212 stats_CounterClean( p_input->p->counters.p_##c );\
1213 p_input->p->counters.p_##c = NULL; } while(0)
1214 EXIT_COUNTER( read_bytes );
1215 EXIT_COUNTER( read_packets );
1216 EXIT_COUNTER( demux_read );
1217 EXIT_COUNTER( input_bitrate );
1218 EXIT_COUNTER( demux_bitrate );
1219 EXIT_COUNTER( played_abuffers );
1220 EXIT_COUNTER( lost_abuffers );
1221 EXIT_COUNTER( displayed_pictures );
1222 EXIT_COUNTER( lost_pictures );
1223 EXIT_COUNTER( decoded_audio );
1224 EXIT_COUNTER( decoded_video );
1225 EXIT_COUNTER( decoded_sub );
1227 if( p_input->p->p_sout )
1229 EXIT_COUNTER( sout_sent_packets );
1230 EXIT_COUNTER (sout_sent_bytes );
1231 EXIT_COUNTER( sout_send_bitrate );
1236 /* Mark them deleted */
1237 p_input->p->input.p_demux = NULL;
1238 p_input->p->input.p_stream = NULL;
1239 p_input->p->input.p_access = NULL;
1240 p_input->p->p_es_out = NULL;
1241 p_input->p->p_sout = NULL;
1243 return VLC_EGENERIC;
1246 /*****************************************************************************
1247 * Error: RunThread() error loop
1248 *****************************************************************************
1249 * This function is called when an error occurred during thread main's loop.
1250 *****************************************************************************/
1251 static void Error( input_thread_t *p_input )
1253 while( !p_input->b_die )
1256 input_ChangeState( p_input, ERROR_S );
1257 msleep( INPUT_IDLE_SLEEP );
1261 /*****************************************************************************
1262 * End: end the input thread
1263 *****************************************************************************/
1264 static void End( input_thread_t * p_input )
1268 /* We are at the end */
1269 input_ChangeState( p_input, END_S );
1271 /* Clean control variables */
1272 input_ControlVarClean( p_input );
1274 /* Clean up master */
1275 InputSourceClean( &p_input->p->input );
1278 for( i = 0; i < p_input->p->i_slave; i++ )
1280 InputSourceClean( p_input->p->slave[i] );
1281 free( p_input->p->slave[i] );
1283 if( p_input->p->slave ) free( p_input->p->slave );
1285 /* Unload all modules */
1286 if( p_input->p->p_es_out )
1287 input_EsOutDelete( p_input->p->p_es_out );
1289 if( !p_input->b_preparsing )
1291 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1292 if( p_input->p_libvlc->b_stats )
1294 /* make sure we are up to date */
1295 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1296 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1298 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1299 p_input->p_libvlc->p_playlist->p_stats );
1300 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1302 CL_CO( read_bytes );
1303 CL_CO( read_packets );
1304 CL_CO( demux_read );
1305 CL_CO( input_bitrate );
1306 CL_CO( demux_bitrate );
1307 CL_CO( played_abuffers );
1308 CL_CO( lost_abuffers );
1309 CL_CO( displayed_pictures );
1310 CL_CO( lost_pictures );
1311 CL_CO( decoded_audio) ;
1312 CL_CO( decoded_video );
1313 CL_CO( decoded_sub) ;
1316 /* Close optional stream output instance */
1317 if( p_input->p->p_sout )
1319 CL_CO( sout_sent_packets );
1320 CL_CO( sout_sent_bytes );
1321 CL_CO( sout_send_bitrate );
1323 vlc_object_detach( p_input->p->p_sout );
1328 if( p_input->p->i_attachment > 0 )
1330 for( i = 0; i < p_input->p->i_attachment; i++ )
1331 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1332 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1335 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
1337 /* Tell we're dead */
1338 p_input->b_dead = VLC_TRUE;
1341 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t *pb_sout_keep )
1343 vlc_bool_t b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1344 sout_instance_t *p_sout = NULL;
1347 /* Search sout-keep options
1348 * XXX it has to be done here, but it is duplicated work :( */
1349 vlc_mutex_lock( &p_item->lock );
1350 for( i = 0; i < p_item->i_options; i++ )
1352 const char *psz_option = p_item->ppsz_options[i];
1355 if( *psz_option == ':' )
1358 if( !strcmp( psz_option, "sout-keep" ) )
1359 b_keep_sout = VLC_TRUE;
1360 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1361 b_keep_sout = VLC_FALSE;
1363 vlc_mutex_unlock( &p_item->lock );
1365 /* Find a potential sout to reuse
1366 * XXX it might be unusable but this will be checked later */
1369 /* Remove the sout from the playlist garbage collector */
1370 playlist_t *p_playlist = pl_Yield( p_parent );
1372 vlc_mutex_lock( &p_playlist->gc_lock );
1373 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1376 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1378 vlc_object_release( p_sout );
1383 vlc_object_detach( p_sout ); /* Remove it from the GC */
1385 vlc_object_release( p_sout );
1388 vlc_mutex_unlock( &p_playlist->gc_lock );
1390 pl_Release( p_parent );
1394 *pb_sout_keep = b_keep_sout;
1398 static void SoutKeep( sout_instance_t *p_sout )
1400 /* attach sout to the playlist */
1401 playlist_t *p_playlist = pl_Yield( p_sout );
1403 msg_Dbg( p_sout, "sout has been kept" );
1404 vlc_object_attach( p_sout, p_playlist );
1406 pl_Release( p_sout );
1409 /*****************************************************************************
1411 *****************************************************************************/
1412 static inline int ControlPopNoLock( input_thread_t *p_input,
1413 int *pi_type, vlc_value_t *p_val )
1415 if( p_input->p->i_control <= 0 )
1417 return VLC_EGENERIC;
1420 *pi_type = p_input->p->control[0].i_type;
1421 *p_val = p_input->p->control[0].val;
1423 p_input->p->i_control--;
1424 if( p_input->p->i_control > 0 )
1428 for( i = 0; i < p_input->p->i_control; i++ )
1430 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1431 p_input->p->control[i].val = p_input->p->control[i+1].val;
1438 static void ControlReduce( input_thread_t *p_input )
1445 for( i = 1; i < p_input->p->i_control; i++ )
1447 const int i_lt = p_input->p->control[i-1].i_type;
1448 const int i_ct = p_input->p->control[i].i_type;
1450 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1451 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1455 ( i_ct == INPUT_CONTROL_SET_STATE ||
1456 i_ct == INPUT_CONTROL_SET_RATE ||
1457 i_ct == INPUT_CONTROL_SET_POSITION ||
1458 i_ct == INPUT_CONTROL_SET_TIME ||
1459 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1460 i_ct == INPUT_CONTROL_SET_TITLE ||
1461 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1462 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1465 // msg_Dbg( p_input, "merged at %d", i );
1466 /* Remove the i-1 */
1467 for( j = i; j < p_input->p->i_control; j++ )
1468 p_input->p->control[j-1] = p_input->p->control[j];
1469 p_input->p->i_control--;
1473 /* TODO but that's not that important
1474 - merge SET_X with SET_X_CMD
1475 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1476 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1483 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1486 vlc_bool_t b_force_update = VLC_FALSE;
1488 if( !p_input ) return b_force_update;
1492 case INPUT_CONTROL_SET_DIE:
1493 msg_Dbg( p_input, "control: stopping input" );
1494 /* Mark all submodules to die */
1495 if( p_input->p->input.p_access )
1496 vlc_object_kill( p_input->p->input.p_access );
1497 if( p_input->p->input.p_stream )
1498 vlc_object_kill( p_input->p->input.p_stream );
1499 vlc_object_kill( p_input->p->input.p_demux );
1501 vlc_object_kill( p_input );
1504 case INPUT_CONTROL_SET_POSITION:
1505 case INPUT_CONTROL_SET_POSITION_OFFSET:
1508 if( i_type == INPUT_CONTROL_SET_POSITION )
1510 f_pos = val.f_float;
1514 /* Should not fail */
1515 demux2_Control( p_input->p->input.p_demux,
1516 DEMUX_GET_POSITION, &f_pos );
1517 f_pos += val.f_float;
1519 if( f_pos < 0.0 ) f_pos = 0.0;
1520 if( f_pos > 1.0 ) f_pos = 1.0;
1521 /* Reset the decoders states and clock sync (before calling the demuxer */
1522 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1523 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1524 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1527 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1528 "%2.1f%% failed", f_pos * 100 );
1532 if( p_input->p->i_slave > 0 )
1533 SlaveSeek( p_input );
1535 b_force_update = VLC_TRUE;
1540 case INPUT_CONTROL_SET_TIME:
1541 case INPUT_CONTROL_SET_TIME_OFFSET:
1546 if( i_type == INPUT_CONTROL_SET_TIME )
1548 i_time = val.i_time;
1552 /* Should not fail */
1553 demux2_Control( p_input->p->input.p_demux,
1554 DEMUX_GET_TIME, &i_time );
1555 i_time += val.i_time;
1557 if( i_time < 0 ) i_time = 0;
1559 /* Reset the decoders states and clock sync (before calling the demuxer */
1560 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1561 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1563 i_ret = demux2_Control( p_input->p->input.p_demux,
1564 DEMUX_SET_TIME, i_time );
1569 /* Emulate it with a SET_POS */
1570 demux2_Control( p_input->p->input.p_demux,
1571 DEMUX_GET_LENGTH, &i_length );
1574 double f_pos = (double)i_time / (double)i_length;
1575 i_ret = demux2_Control( p_input->p->input.p_demux,
1576 DEMUX_SET_POSITION, f_pos );
1581 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1582 " failed or not possible", i_time );
1586 if( p_input->p->i_slave > 0 )
1587 SlaveSeek( p_input );
1589 b_force_update = VLC_TRUE;
1594 case INPUT_CONTROL_SET_STATE:
1595 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1596 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1599 if( p_input->p->input.p_access )
1600 i_ret = access2_Control( p_input->p->input.p_access,
1601 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1603 i_ret = demux2_Control( p_input->p->input.p_demux,
1604 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1608 /* FIXME What to do ? */
1609 msg_Warn( p_input, "cannot unset pause -> EOF" );
1610 vlc_mutex_unlock( &p_input->p->lock_control );
1611 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1612 vlc_mutex_lock( &p_input->p->lock_control );
1615 b_force_update = VLC_TRUE;
1617 /* Switch to play */
1618 p_input->i_state = PLAYING_S;
1619 val.i_int = PLAYING_S;
1620 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1624 input_EsOutChangeState( p_input->p->p_es_out );
1626 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1627 p_input->p->b_can_pause )
1630 if( p_input->p->input.p_access )
1631 i_ret = access2_Control( p_input->p->input.p_access,
1632 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1634 i_ret = demux2_Control( p_input->p->input.p_demux,
1635 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1637 b_force_update = VLC_TRUE;
1641 msg_Warn( p_input, "cannot set pause state" );
1642 val.i_int = p_input->i_state;
1646 val.i_int = PAUSE_S;
1649 /* Switch to new state */
1650 p_input->i_state = val.i_int;
1651 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1655 input_EsOutChangeState( p_input->p->p_es_out );
1657 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1659 b_force_update = VLC_TRUE;
1661 /* Correct "state" value */
1662 val.i_int = p_input->i_state;
1663 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1665 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1667 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1671 case INPUT_CONTROL_SET_RATE:
1672 case INPUT_CONTROL_SET_RATE_SLOWER:
1673 case INPUT_CONTROL_SET_RATE_FASTER:
1677 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1678 i_rate = p_input->p->i_rate * 2;
1679 else if( i_type == INPUT_CONTROL_SET_RATE_FASTER )
1680 i_rate = p_input->p->i_rate / 2;
1684 if( i_rate < INPUT_RATE_MIN )
1686 msg_Dbg( p_input, "cannot set rate faster" );
1687 i_rate = INPUT_RATE_MIN;
1689 else if( i_rate > INPUT_RATE_MAX )
1691 msg_Dbg( p_input, "cannot set rate slower" );
1692 i_rate = INPUT_RATE_MAX;
1694 if( i_rate != INPUT_RATE_DEFAULT &&
1695 ( !p_input->b_can_pace_control ||
1696 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1698 msg_Dbg( p_input, "cannot change rate" );
1699 i_rate = INPUT_RATE_DEFAULT;
1701 if( i_rate != p_input->p->i_rate )
1704 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1706 p_input->p->i_rate = i_rate;
1708 input_EsOutChangeRate( p_input->p->p_es_out );
1710 b_force_update = VLC_TRUE;
1715 case INPUT_CONTROL_SET_PROGRAM:
1716 /* No need to force update, es_out does it if needed */
1717 es_out_Control( p_input->p->p_es_out,
1718 ES_OUT_SET_GROUP, val.i_int );
1720 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1724 case INPUT_CONTROL_SET_ES:
1725 /* No need to force update, es_out does it if needed */
1726 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1727 input_EsOutGetFromID( p_input->p->p_es_out,
1731 case INPUT_CONTROL_SET_AUDIO_DELAY:
1732 input_EsOutSetDelay( p_input->p->p_es_out,
1733 AUDIO_ES, val.i_time );
1734 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1737 case INPUT_CONTROL_SET_SPU_DELAY:
1738 input_EsOutSetDelay( p_input->p->p_es_out,
1739 SPU_ES, val.i_time );
1740 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1743 case INPUT_CONTROL_SET_TITLE:
1744 case INPUT_CONTROL_SET_TITLE_NEXT:
1745 case INPUT_CONTROL_SET_TITLE_PREV:
1746 if( p_input->p->input.b_title_demux &&
1747 p_input->p->input.i_title > 0 )
1750 /* FIXME handle demux title */
1751 demux_t *p_demux = p_input->p->input.p_demux;
1754 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1755 i_title = p_demux->info.i_title - 1;
1756 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1757 i_title = p_demux->info.i_title + 1;
1759 i_title = val.i_int;
1761 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1763 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1764 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1766 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1767 input_ControlVarTitle( p_input, i_title );
1770 else if( p_input->p->input.i_title > 0 )
1772 access_t *p_access = p_input->p->input.p_access;
1775 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1776 i_title = p_access->info.i_title - 1;
1777 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1778 i_title = p_access->info.i_title + 1;
1780 i_title = val.i_int;
1782 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1784 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1785 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1787 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1788 stream_AccessReset( p_input->p->input.p_stream );
1792 case INPUT_CONTROL_SET_SEEKPOINT:
1793 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1794 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1795 if( p_input->p->input.b_title_demux &&
1796 p_input->p->input.i_title > 0 )
1798 demux_t *p_demux = p_input->p->input.p_demux;
1800 int64_t i_input_time;
1801 int64_t i_seekpoint_time;
1803 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1805 i_seekpoint = p_demux->info.i_seekpoint;
1806 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1807 if( i_seekpoint_time >= 0 &&
1808 !demux2_Control( p_demux,
1809 DEMUX_GET_TIME, &i_input_time ) )
1811 if ( i_input_time < i_seekpoint_time + 3000000 )
1817 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1818 i_seekpoint = p_demux->info.i_seekpoint + 1;
1820 i_seekpoint = val.i_int;
1822 if( i_seekpoint >= 0 && i_seekpoint <
1823 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1825 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1826 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1828 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1831 else if( p_input->p->input.i_title > 0 )
1833 demux_t *p_demux = p_input->p->input.p_demux;
1834 access_t *p_access = p_input->p->input.p_access;
1836 int64_t i_input_time;
1837 int64_t i_seekpoint_time;
1839 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1841 i_seekpoint = p_access->info.i_seekpoint;
1842 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1843 if( i_seekpoint_time >= 0 &&
1844 demux2_Control( p_demux,
1845 DEMUX_GET_TIME, &i_input_time ) )
1847 if ( i_input_time < i_seekpoint_time + 3000000 )
1853 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1854 i_seekpoint = p_access->info.i_seekpoint + 1;
1856 i_seekpoint = val.i_int;
1858 if( i_seekpoint >= 0 && i_seekpoint <
1859 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1861 es_out_Control( p_input->p->p_es_out, ES_OUT_RESET_PCR );
1862 input_EsOutDiscontinuity( p_input->p->p_es_out, VLC_TRUE, VLC_FALSE );
1864 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1866 stream_AccessReset( p_input->p->input.p_stream );
1871 case INPUT_CONTROL_ADD_SLAVE:
1872 if( val.psz_string )
1874 input_source_t *slave = InputSourceNew( p_input );
1876 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1882 msg_Dbg( p_input, "adding %s as slave on the fly",
1886 if( demux2_Control( p_input->p->input.p_demux,
1887 DEMUX_GET_TIME, &i_time ) )
1889 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1890 InputSourceClean( slave );
1894 if( demux2_Control( slave->p_demux,
1895 DEMUX_SET_TIME, i_time ) )
1897 msg_Err( p_input, "seek failed for new slave" );
1898 InputSourceClean( slave );
1903 /* Get meta (access and demux) */
1904 p_meta = vlc_meta_New();
1905 access2_Control( slave->p_access, ACCESS_GET_META,
1907 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1908 InputUpdateMeta( p_input, p_meta );
1910 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1915 msg_Warn( p_input, "failed to add %s as slave",
1919 free( val.psz_string );
1923 case INPUT_CONTROL_SET_BOOKMARK:
1925 msg_Err( p_input, "not yet implemented" );
1929 return b_force_update;
1932 /*****************************************************************************
1934 *****************************************************************************/
1935 static int UpdateFromDemux( input_thread_t *p_input )
1937 demux_t *p_demux = p_input->p->input.p_demux;
1940 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
1942 v.i_int = p_demux->info.i_title;
1943 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1945 input_ControlVarTitle( p_input, p_demux->info.i_title );
1947 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
1949 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
1951 v.i_int = p_demux->info.i_seekpoint;
1952 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
1954 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
1956 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
1958 /* Hmmm only works with master input */
1959 if( p_input->p->input.p_demux == p_demux )
1961 int i_title_end = p_input->p->input.i_title_end -
1962 p_input->p->input.i_title_offset;
1963 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
1964 p_input->p->input.i_seekpoint_offset;
1966 if( i_title_end >= 0 && i_seekpoint_end >= 0 )
1968 if( p_demux->info.i_title > i_title_end ||
1969 ( p_demux->info.i_title == i_title_end &&
1970 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
1972 else if( i_seekpoint_end >=0 )
1974 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
1976 else if( i_title_end >= 0 )
1978 if( p_demux->info.i_title > i_title_end ) return 0;
1985 /*****************************************************************************
1987 *****************************************************************************/
1988 static int UpdateFromAccess( input_thread_t *p_input )
1990 access_t *p_access = p_input->p->input.p_access;
1993 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
1995 v.i_int = p_access->info.i_title;
1996 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
1998 input_ControlVarTitle( p_input, p_access->info.i_title );
2000 stream_AccessUpdate( p_input->p->input.p_stream );
2002 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2004 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2006 v.i_int = p_access->info.i_seekpoint;
2007 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2008 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2010 if( p_access->info.i_update & INPUT_UPDATE_META )
2012 /* TODO maybe multi - access ? */
2013 vlc_meta_t *p_meta = vlc_meta_New();
2014 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2015 InputUpdateMeta( p_input, p_meta );
2016 var_SetBool( p_input, "item-change", p_input->p->input.p_item->i_id );
2017 p_access->info.i_update &= ~INPUT_UPDATE_META;
2020 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2022 /* Hmmm only works with master input */
2023 if( p_input->p->input.p_access == p_access )
2025 int i_title_end = p_input->p->input.i_title_end -
2026 p_input->p->input.i_title_offset;
2027 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2028 p_input->p->input.i_seekpoint_offset;
2030 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2032 if( p_access->info.i_title > i_title_end ||
2033 ( p_access->info.i_title == i_title_end &&
2034 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2036 else if( i_seekpoint_end >=0 )
2038 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2040 else if( i_title_end >= 0 )
2042 if( p_access->info.i_title > i_title_end ) return 0;
2049 /*****************************************************************************
2051 *****************************************************************************/
2052 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2054 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2056 if( !p_input->b_preparsing )
2058 pl_Yield( p_input );
2059 var_SetInteger( pl_Get( p_input ), "item-change",
2060 p_input->p->input.p_item->i_id );
2061 pl_Release( p_input )
2065 /*****************************************************************************
2067 *****************************************************************************/
2068 static input_source_t *InputSourceNew( input_thread_t *p_input )
2070 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2074 msg_Err( p_input, "out of memory for new input source" );
2079 in->p_access = NULL;
2080 in->p_stream = NULL;
2082 in->b_title_demux = VLC_FALSE;
2083 TAB_INIT( in->i_title, in->title );
2084 in->b_can_pace_control = VLC_TRUE;
2085 in->b_eof = VLC_FALSE;
2087 in->i_cr_average = 0;
2092 /*****************************************************************************
2094 *****************************************************************************/
2095 static int InputSourceInit( input_thread_t *p_input,
2096 input_source_t *in, const char *psz_mrl,
2097 const char *psz_forced_demux )
2099 char psz_dup[strlen (psz_mrl) + 1];
2100 const char *psz_access;
2101 const char *psz_demux;
2108 strcpy( psz_dup, psz_mrl );
2110 if( !in ) return VLC_EGENERIC;
2111 if( !p_input ) return VLC_EGENERIC;
2114 if( !p_input->b_preparsing )
2116 MRLSplit( VLC_OBJECT(p_input), psz_dup,
2117 &psz_access, &psz_demux, &psz_path );
2119 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2120 psz_mrl, psz_access, psz_demux, psz_path );
2122 /* Hack to allow udp://@:port syntax */
2124 (strncmp( psz_access, "udp", 3 ) &&
2125 strncmp( psz_access, "rtp", 3 )) )
2127 /* Find optional titles and seekpoints */
2128 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2129 &in->i_seekpoint_start, &in->i_seekpoint_end );
2131 if( psz_forced_demux && *psz_forced_demux )
2133 psz_demux = psz_forced_demux;
2135 else if( !psz_demux || *psz_demux == '\0' )
2137 /* special hack for forcing a demuxer with --demux=module
2138 * (and do nothing with a list) */
2139 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2141 if( psz_var_demux != NULL &&
2142 !strchr(psz_var_demux, ',' ) &&
2143 !strchr(psz_var_demux, ':' ) )
2145 psz_demux = psz_var_demux;
2147 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2151 /* Try access_demux if no demux given */
2152 if( *psz_demux == '\0' )
2154 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2155 NULL, p_input->p->p_es_out, VLC_FALSE );
2161 if( !strncmp( psz_path, "file://", 7 ) )
2163 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2165 psz_access = "file";
2170 int64_t i_pts_delay;
2172 /* Get infos from access_demux */
2173 demux2_Control( in->p_demux,
2174 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2175 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2177 in->b_title_demux = VLC_TRUE;
2178 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2179 &in->title, &in->i_title,
2180 &in->i_title_offset, &in->i_seekpoint_offset ) )
2185 demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2186 &in->b_can_pace_control );
2187 demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2191 demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2197 int64_t i_pts_delay;
2199 input_ChangeState( p_input, OPENING_S );
2201 /* Now try a real access */
2202 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2203 p_input->b_preparsing );
2205 /* Access failed, URL encoded ? */
2206 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2208 decode_URI( psz_path );
2210 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2211 psz_access, psz_demux, psz_path );
2213 in->p_access = access2_New( p_input,
2214 psz_access, psz_demux, psz_path,
2215 p_input->b_preparsing );
2217 /* access failed, maybe our access detection was wrong.
2218 * Retry with the full name */
2219 if( in->p_access == NULL && strchr( psz_mrl, ':' ) )
2221 msg_Dbg( p_input, "retrying with access `' demux `' path `%s'",
2223 in->p_access = access2_New( p_input,
2225 p_input->b_preparsing );
2227 if( in->p_access == NULL )
2229 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2231 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2232 _("Your input can't be opened"),
2233 _("VLC is unable to open the MRL '%s'."
2234 " Check the log for details."), psz_mrl );
2239 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2240 while( psz && *psz )
2242 access_t *p_access = in->p_access;
2243 char *end = strchr( psz, ':' );
2248 in->p_access = access2_FilterNew( in->p_access, psz );
2249 if( in->p_access == NULL )
2251 in->p_access = p_access;
2252 msg_Warn( p_input, "failed to insert access filter %s",
2260 /* Get infos from access */
2261 if( !p_input->b_preparsing )
2263 access2_Control( in->p_access,
2264 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2265 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2267 in->b_title_demux = VLC_FALSE;
2268 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2269 &in->title, &in->i_title,
2270 &in->i_title_offset, &in->i_seekpoint_offset ) )
2276 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2277 &in->b_can_pace_control );
2278 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2280 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2282 var_Set( p_input, "seekable", val );
2285 input_ChangeState( p_input, BUFFERING_S );
2287 /* Create the stream_t */
2288 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2289 if( in->p_stream == NULL )
2291 msg_Warn( p_input, "cannot create a stream_t from access" );
2295 /* Open a demuxer */
2296 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2298 psz_demux = in->p_access->psz_demux;
2302 /* Take access redirections into account */
2303 char *psz_real_path;
2304 char *psz_buf = NULL;
2305 if( in->p_access->psz_path )
2307 const char *psz_a, *psz_d;
2308 psz_buf = strdup( in->p_access->psz_path );
2309 MRLSplit( VLC_OBJECT(p_input), psz_buf,
2310 &psz_a, &psz_d, &psz_real_path );
2314 psz_real_path = psz_path;
2316 in->p_demux = demux2_New( p_input, psz_access, psz_demux,
2318 in->p_stream, p_input->p->p_es_out,
2319 p_input->b_preparsing );
2323 if( in->p_demux == NULL )
2325 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2326 psz_access, psz_demux, psz_path );
2327 intf_UserFatal( VLC_OBJECT( p_input ), VLC_FALSE,
2328 _("Can't recognize the input's format"),
2329 _("The format of '%s' can't be detected. "
2330 "Have a look the log for details."), psz_mrl );
2334 /* Get title from demux */
2335 if( !p_input->b_preparsing && in->i_title <= 0 )
2337 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2338 &in->title, &in->i_title,
2339 &in->i_title_offset, &in->i_seekpoint_offset ))
2341 TAB_INIT( in->i_title, in->title );
2345 in->b_title_demux = VLC_TRUE;
2351 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2352 if( 1 || !p_input->b_preparsing )
2355 input_attachment_t **attachment;
2356 if( !demux2_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2357 &attachment, &i_attachment ) )
2360 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2361 p_input->p->attachment = realloc( p_input->p->attachment,
2362 sizeof(input_attachment_t**) * ( p_input->p->i_attachment + i_attachment ) );
2363 for( i = 0; i < i_attachment; i++ )
2364 p_input->p->attachment[p_input->p->i_attachment++] = attachment[i];
2367 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2370 if( !demux2_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2372 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2374 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2377 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2378 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2383 input_ChangeState( p_input, ERROR_S );
2386 demux2_Delete( in->p_demux );
2389 stream_Delete( in->p_stream );
2392 access2_Delete( in->p_access );
2394 return VLC_EGENERIC;
2397 /*****************************************************************************
2399 *****************************************************************************/
2400 static void InputSourceClean( input_source_t *in )
2405 demux2_Delete( in->p_demux );
2408 stream_Delete( in->p_stream );
2411 access2_Delete( in->p_access );
2413 if( in->i_title > 0 )
2415 for( i = 0; i < in->i_title; i++ )
2416 vlc_input_title_Delete( in->title[i] );
2417 TAB_CLEAN( in->i_title, in->title );
2421 static void SlaveDemux( input_thread_t *p_input )
2426 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2428 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2432 for( i = 0; i < p_input->p->i_slave; i++ )
2434 input_source_t *in = p_input->p->slave[i];
2440 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2445 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2447 msg_Err( p_input, "slave[%d] doesn't like "
2448 "DEMUX_GET_TIME -> EOF", i );
2453 if( i_stime >= i_time )
2456 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2462 i_ret = in->p_demux->pf_demux( in->p_demux );
2467 msg_Dbg( p_input, "slave %d EOF", i );
2468 in->b_eof = VLC_TRUE;
2473 static void SlaveSeek( input_thread_t *p_input )
2478 if( !p_input ) return;
2480 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2482 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2486 for( i = 0; i < p_input->p->i_slave; i++ )
2488 input_source_t *in = p_input->p->slave[i];
2490 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2492 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2493 in->b_eof = VLC_TRUE;
2498 /*****************************************************************************
2500 *****************************************************************************/
2501 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2505 if( !p_meta ) return;
2507 /* Get meta information from user */
2508 #define GET_META( field, s ) \
2509 var_Get( p_input, (s), &val ); \
2510 if( *val.psz_string ) \
2511 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2512 free( val.psz_string )
2514 GET_META( Title, "meta-title" );
2515 GET_META( Artist, "meta-artist" );
2516 GET_META( Genre, "meta-genre" );
2517 GET_META( Copyright, "meta-copyright" );
2518 GET_META( Description, "meta-description" );
2519 GET_META( Date, "meta-date" );
2520 GET_META( URL, "meta-url" );
2524 /*****************************************************************************
2525 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2526 * arturl and locking issue.
2527 *****************************************************************************/
2528 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2530 input_item_t *p_item = p_input->p->input.p_item;
2531 char * psz_arturl = NULL;
2532 char *psz_title = NULL;
2534 int i_arturl_event = VLC_FALSE;
2539 psz_arturl = input_item_GetArtURL( p_item );
2541 vlc_mutex_lock( &p_item->lock );
2542 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2543 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2545 vlc_meta_Merge( p_item->p_meta, p_meta );
2547 if( psz_arturl && *psz_arturl )
2549 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2550 i_arturl_event = VLC_TRUE;
2553 vlc_meta_Delete( p_meta );
2555 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2557 /* Don't look for art cover if sout
2558 * XXX It can change when sout has meta data support */
2559 if( p_input->p->p_sout && !p_input->b_preparsing )
2561 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2562 i_arturl_event = VLC_TRUE;
2566 input_ExtractAttachmentAndCacheArt( p_input );
2570 input_item_SetPreparsed( p_item, VLC_TRUE );
2574 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2576 p_meta = vlc_meta_New();
2577 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2579 vlc_mutex_unlock( &p_item->lock );
2581 if( i_arturl_event == VLC_TRUE )
2585 /* Notify interested third parties */
2586 event.type = vlc_InputItemMetaChanged;
2587 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2588 vlc_event_send( &p_item->event_manager, &event );
2593 input_Control( p_input, INPUT_SET_NAME, psz_title );
2599 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2600 for( i = 0; ppsz_all_keys[i]; i++ )
2602 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2603 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2604 free( ppsz_all_keys[i] );
2606 free( ppsz_all_keys );
2607 vlc_meta_Delete( p_meta );
2610 /** \todo handle sout meta */
2614 static inline vlc_bool_t IsValidAccess( const char *psz )
2618 while( ( c = *psz ) != '\0' )
2623 if( ( !isascii( c ) || !isalnum( c ) ) && c != '-' && ( c != '/' ) )
2627 /* should not happen though */
2632 /*****************************************************************************
2633 * MRLSplit: parse the access, demux and url part of the
2634 * Media Resource Locator.
2635 *****************************************************************************/
2636 void MRLSplit( vlc_object_t *p_input, char *psz_dup,
2637 const char **ppsz_access, const char **ppsz_demux,
2640 const char *psz_access = "";
2641 const char *psz_demux = "";
2645 psz = strchr( psz_dup, ':' );
2649 /* Guess whether ':' is part of a local filename, or separates
2650 * access/demux from path */
2651 if( !IsValidAccess( psz_dup ) )
2653 #if defined( WIN32 ) || defined( UNDER_CE )
2654 else if( ( psz - psz_dup == 1 ) && isalpha( psz_dup[0] ) )
2656 msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2665 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2669 psz = strchr( psz_dup, '/' );
2676 psz_access = psz_dup;
2683 *ppsz_access = psz_access;
2684 *ppsz_demux = psz_demux;
2685 *ppsz_path = psz_path;
2688 /*****************************************************************************
2689 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2692 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2693 *****************************************************************************/
2694 static void MRLSections( input_thread_t *p_input, char *psz_source,
2695 int *pi_title_start, int *pi_title_end,
2696 int *pi_chapter_start, int *pi_chapter_end )
2698 char *psz, *psz_end, *psz_next, *psz_check;
2700 *pi_title_start = *pi_title_end = -1;
2701 *pi_chapter_start = *pi_chapter_end = -1;
2703 /* Start by parsing titles and chapters */
2704 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2706 /* Check we are really dealing with a title/chapter section */
2707 psz_check = psz + 1;
2708 if( !*psz_check ) return;
2709 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2710 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2711 if( *psz_check == ':' && ++psz_check )
2712 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2713 if( *psz_check != '-' && *psz_check ) return;
2714 if( *psz_check == '-' && ++psz_check )
2715 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2716 if( *psz_check != ':' && *psz_check ) return;
2717 if( *psz_check == ':' && ++psz_check )
2718 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2719 if( *psz_check ) return;
2721 /* Separate start and end */
2723 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2725 /* Look for the start title */
2726 *pi_title_start = strtol( psz, &psz_next, 0 );
2727 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2728 *pi_title_end = *pi_title_start;
2731 /* Look for the start chapter */
2733 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2734 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2735 *pi_chapter_end = *pi_chapter_start;
2739 /* Look for the end title */
2740 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2741 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2744 /* Look for the end chapter */
2745 if( *psz_end ) psz_end++;
2746 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2747 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2750 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2751 psz_source, *pi_title_start, *pi_chapter_start,
2752 *pi_title_end, *pi_chapter_end );
2755 /*****************************************************************************
2756 * input_AddSubtitles: add a subtitles file and enable it
2757 *****************************************************************************/
2758 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2759 vlc_bool_t b_check_extension )
2761 input_source_t *sub;
2764 char *psz_path, *psz_extension;
2766 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2771 /* if we are provided a subtitle.sub file,
2772 * see if we don't have a subtitle.idx and use it instead */
2773 psz_path = strdup( psz_subtitle );
2776 psz_extension = strrchr( psz_path, '.');
2777 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2781 strcpy( psz_extension, ".idx" );
2782 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2783 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2786 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2787 psz_path, psz_subtitle );
2788 strcpy( psz_subtitle, psz_path );
2794 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2796 sub = InputSourceNew( p_input );
2797 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2799 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2802 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2804 if( count.i_int == 0 )
2806 /* if it was first one, there is disable too */
2808 if( count.i_int < list.p_list->i_count )
2810 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2811 &list.p_list->p_values[count.i_int] );
2813 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );