1 /*****************************************************************************
2 * input.c: input thread
3 *****************************************************************************
4 * Copyright (C) 1998-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
38 #include "input_internal.h"
41 #include "../stream_output/stream_output.h"
43 #include <vlc_playlist.h>
44 #include <vlc_interface.h>
46 #include <vlc_demux.h>
47 #include <vlc_charset.h>
49 #ifdef HAVE_SYS_STAT_H
50 # include <sys/stat.h>
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Run ( input_thread_t *p_input );
57 static int RunAndDestroy ( input_thread_t *p_input );
59 static input_thread_t * Create ( vlc_object_t *, input_item_t *,
60 const char *, vlc_bool_t, sout_instance_t * );
61 static int Init ( input_thread_t *p_input );
62 static void Error ( input_thread_t *p_input );
63 static void End ( input_thread_t *p_input );
64 static void MainLoop( input_thread_t *p_input );
65 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout );
67 static inline int ControlPopNoLock( input_thread_t *, int *, vlc_value_t * );
68 static void ControlReduce( input_thread_t * );
69 static vlc_bool_t Control( input_thread_t *, int, vlc_value_t );
71 static int UpdateFromAccess( input_thread_t * );
72 static int UpdateFromDemux( input_thread_t * );
74 static void UpdateItemLength( input_thread_t *, int64_t i_length );
76 static void MRLSections( input_thread_t *, char *, int *, int *, int *, int *);
78 static input_source_t *InputSourceNew( input_thread_t *);
79 static int InputSourceInit( input_thread_t *, input_source_t *,
80 const char *, const char *psz_forced_demux );
81 static void InputSourceClean( input_source_t * );
83 //static void InputGetAttachments( input_thread_t *, input_source_t * );
84 static void SlaveDemux( input_thread_t *p_input );
85 static void SlaveSeek( input_thread_t *p_input );
87 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta );
88 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta );
90 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t * );
91 static void SoutKeep( sout_instance_t * );
93 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux );
94 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
95 int i_new, input_attachment_t **pp_new );
97 /*****************************************************************************
98 * This function creates a new input, and returns a pointer
99 * to its description. On error, it returns NULL.
101 * Variables for _public_ use:
104 * - rate,rate-slower, rate-faster
105 * - position, position-offset
106 * - time, time-offset
107 * - title,title-next,title-prev
108 * - chapter,chapter-next, chapter-prev
109 * - program, audio-es, video-es, spu-es
110 * - audio-delay, spu-delay
115 * - seekable (if you can seek, it doesn't say if 'bar display' has be shown or not, for that check position != 0.0)
117 * * For intf callback upon changes
119 * TODO explain when Callback is called
120 * TODO complete this list (?)
121 *****************************************************************************/
122 static input_thread_t *Create( vlc_object_t *p_parent, input_item_t *p_item,
123 const char *psz_header, vlc_bool_t b_quick, sout_instance_t *p_sout )
125 input_thread_t *p_input = NULL; /* thread descriptor */
129 /* Allocate descriptor */
130 p_input = vlc_object_create( p_parent, VLC_OBJECT_INPUT );
131 if( p_input == NULL )
133 msg_Err( p_parent, "out of memory" );
136 MALLOC_NULL( p_input->p, input_thread_private_t );
138 /* One "randomly" selected input thread is responsible for computing
139 * the global stats. Check if there is already someone doing this */
140 if( p_input->p_libvlc->p_playlist->p_stats && !b_quick )
142 vlc_mutex_lock( &p_input->p_libvlc->p_playlist->p_stats->lock );
143 if( p_input->p_libvlc->p_playlist->p_stats_computer == NULL )
145 p_input->p_libvlc->p_playlist->p_stats_computer = p_input;
147 vlc_mutex_unlock( &p_input->p_libvlc->p_playlist->p_stats->lock );
150 p_input->b_preparsing = b_quick;
151 p_input->psz_header = psz_header ? strdup( psz_header ) : NULL;
153 /* Init Common fields */
154 p_input->b_eof = VLC_FALSE;
155 p_input->b_can_pace_control = VLC_TRUE;
156 p_input->p->i_start = 0;
158 p_input->p->i_stop = 0;
159 p_input->p->i_run = 0;
160 p_input->p->i_title = 0;
161 p_input->p->title = NULL;
162 p_input->p->i_title_offset = p_input->p->i_seekpoint_offset = 0;
163 p_input->i_state = INIT_S;
164 p_input->p->i_rate = INPUT_RATE_DEFAULT;
165 TAB_INIT( p_input->p->i_bookmark, p_input->p->bookmark );
166 TAB_INIT( p_input->p->i_attachment, p_input->p->attachment );
167 p_input->p->p_es_out = NULL;
168 p_input->p->p_sout = NULL;
169 p_input->p->b_sout_keep = VLC_FALSE;
170 p_input->p->b_out_pace_control = VLC_FALSE;
171 p_input->i_pts_delay = 0;
173 /* Init Input fields */
174 p_input->p->input.p_item = p_item;
175 p_input->p->input.p_access = NULL;
176 p_input->p->input.p_stream = NULL;
177 p_input->p->input.p_demux = NULL;
178 p_input->p->input.b_title_demux = VLC_FALSE;
179 p_input->p->input.i_title = 0;
180 p_input->p->input.title = NULL;
181 p_input->p->input.i_title_offset = p_input->p->input.i_seekpoint_offset = 0;
182 p_input->p->input.b_can_pace_control = VLC_TRUE;
183 p_input->p->input.b_can_rate_control = VLC_TRUE;
184 p_input->p->input.b_rescale_ts = VLC_TRUE;
185 p_input->p->input.b_eof = VLC_FALSE;
186 p_input->p->input.i_cr_average = 0;
188 vlc_mutex_lock( &p_item->lock );
190 if( !p_item->p_stats )
191 p_item->p_stats = stats_NewInputStats( p_input );
192 vlc_mutex_unlock( &p_item->lock );
195 p_input->p->i_slave = 0;
196 p_input->p->slave = NULL;
198 /* Init control buffer */
199 vlc_mutex_init( p_input, &p_input->p->lock_control );
200 p_input->p->i_control = 0;
202 /* Parse input options */
203 vlc_mutex_lock( &p_item->lock );
204 for( i = 0; i < p_item->i_options; i++ )
205 var_OptionParse( p_input, p_item->ppsz_options[i] );
206 vlc_mutex_unlock( &p_item->lock );
208 /* Create Object Variables for private use only */
209 input_ConfigVarInit( p_input );
211 /* Create Objects variables for public Get and Set */
212 input_ControlVarInit( p_input );
214 p_input->p->input.i_cr_average = var_GetInteger( p_input, "cr-average" );
216 if( !p_input->b_preparsing )
218 var_Get( p_input, "bookmarks", &val );
221 /* FIXME: have a common cfg parsing routine used by sout and others */
222 char *psz_parser, *psz_start, *psz_end;
223 psz_parser = val.psz_string;
224 while( (psz_start = strchr( psz_parser, '{' ) ) )
226 seekpoint_t *p_seekpoint = vlc_seekpoint_New();
229 psz_end = strchr( psz_start, '}' );
230 if( !psz_end ) break;
231 psz_parser = psz_end + 1;
232 backup = *psz_parser;
235 while( (psz_end = strchr( psz_start, ',' ) ) )
238 if( !strncmp( psz_start, "name=", 5 ) )
240 p_seekpoint->psz_name = strdup(psz_start + 5);
242 else if( !strncmp( psz_start, "bytes=", 6 ) )
244 p_seekpoint->i_byte_offset = atoll(psz_start + 6);
246 else if( !strncmp( psz_start, "time=", 5 ) )
248 p_seekpoint->i_time_offset = atoll(psz_start + 5) *
251 psz_start = psz_end + 1;
253 msg_Dbg( p_input, "adding bookmark: %s, bytes="I64Fd", time="I64Fd,
254 p_seekpoint->psz_name, p_seekpoint->i_byte_offset,
255 p_seekpoint->i_time_offset );
256 input_Control( p_input, INPUT_ADD_BOOKMARK, p_seekpoint );
257 vlc_seekpoint_Delete( p_seekpoint );
258 *psz_parser = backup;
260 free( val.psz_string );
264 /* Remove 'Now playing' info as it is probably outdated */
265 input_Control( p_input, INPUT_DEL_INFO,
266 _(VLC_META_INFO_CAT),
267 _(VLC_META_NOW_PLAYING) );
268 input_item_SetNowPlaying( p_item, NULL );
271 if( p_input->b_preparsing )
272 p_input->i_flags |= OBJECT_FLAGS_QUIET | OBJECT_FLAGS_NOINTERACT;
276 p_input->p->p_sout = p_sout;
278 /* Attach only once we are ready */
279 vlc_object_attach( p_input, p_parent );
284 static void Destroy( input_thread_t *p_input, sout_instance_t **pp_sout )
286 vlc_object_detach( p_input );
287 input_thread_private_t *priv = p_input->p;
294 *pp_sout = priv->p_sout;
295 else if( priv->b_sout_keep )
296 SoutKeep( priv->p_sout );
298 sout_DeleteInstance( priv->p_sout );
301 vlc_object_destroy( p_input );
302 vlc_mutex_destroy( &priv->lock_control );
307 * Initialize an input thread and run it. You will need to monitor the
308 * thread to clean up after it is done
310 * \param p_parent a vlc_object
311 * \param p_item an input item
312 * \return a pointer to the spawned input thread
314 input_thread_t *__input_CreateThread( vlc_object_t *p_parent,
315 input_item_t *p_item )
317 vlc_bool_t b_sout_keep;
318 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
319 input_thread_t *p_input = __input_CreateThreadExtended( p_parent, p_item, NULL, p_sout );
321 if( !p_input && p_sout )
324 p_input->p->b_sout_keep = b_sout_keep;
329 input_thread_t *__input_CreateThreadExtended( vlc_object_t *p_parent,
330 input_item_t *p_item,
331 const char *psz_log, sout_instance_t *p_sout )
333 input_thread_t *p_input;
335 p_input = Create( p_parent, p_item, psz_log, VLC_FALSE, p_sout );
339 /* Create thread and wait for its readiness. */
340 if( vlc_thread_create( p_input, "input", Run,
341 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
343 input_ChangeState( p_input, ERROR_S );
344 msg_Err( p_input, "cannot create input thread" );
345 Destroy( p_input, &p_sout );
353 * Initialize an input thread and run it. This thread will clean after himself,
354 * you can forget about it. It can work either in blocking or non-blocking mode
356 * \param p_parent a vlc_object
357 * \param p_item an input item
358 * \param b_block should we block until read is finished ?
359 * \return the input object id if non blocking, an error code else
361 int __input_Read( vlc_object_t *p_parent, input_item_t *p_item,
364 vlc_bool_t b_sout_keep;
365 sout_instance_t *p_sout = SoutFind( p_parent, p_item, &b_sout_keep );
366 input_thread_t *p_input;
368 p_input = Create( p_parent, p_item, NULL, VLC_FALSE, p_sout );
369 if( !p_input && p_sout )
374 p_input->p->b_sout_keep = b_sout_keep;
378 RunAndDestroy( p_input );
383 if( vlc_thread_create( p_input, "input", RunAndDestroy,
384 VLC_THREAD_PRIORITY_INPUT, VLC_TRUE ) )
386 input_ChangeState( p_input, ERROR_S );
387 msg_Err( p_input, "cannot create input thread" );
388 Destroy( p_input, NULL );
392 return p_input->i_object_id;
396 * Initialize an input and initialize it to preparse the item
397 * This function is blocking. It will only accept to parse files
399 * \param p_parent a vlc_object_t
400 * \param p_item an input item
401 * \return VLC_SUCCESS or an error
403 int __input_Preparse( vlc_object_t *p_parent, input_item_t *p_item )
405 input_thread_t *p_input;
407 /* Allocate descriptor */
408 p_input = Create( p_parent, p_item, NULL, VLC_TRUE, NULL );
412 if( !Init( p_input ) )
415 Destroy( p_input, NULL );
421 * Request a running input thread to stop and die
423 * \param the input thread to stop
425 void input_StopThread( input_thread_t *p_input )
430 /* Set die for input */
431 vlc_object_kill( p_input );
432 /* FIXME: seems to be duplicated in ControlPush(INPUT_CONTROL_SET_DIE) */
434 /* We cannot touch p_input fields directly (we come from another thread),
435 * so use the vlc_object_find way, it's perfectly safe */
437 /* Set die for all access */
438 p_list = vlc_list_find( p_input, VLC_OBJECT_ACCESS, FIND_CHILD );
439 for( i = 0; i < p_list->i_count; i++ )
441 vlc_object_kill( p_list->p_values[i].p_object );
443 vlc_list_release( p_list );
445 /* Set die for all stream */
446 p_list = vlc_list_find( p_input, VLC_OBJECT_STREAM, FIND_CHILD );
447 for( i = 0; i < p_list->i_count; i++ )
449 vlc_object_kill( p_list->p_values[i].p_object );
451 vlc_list_release( p_list );
453 /* Set die for all demux */
454 p_list = vlc_list_find( p_input, VLC_OBJECT_DEMUX, FIND_CHILD );
455 for( i = 0; i < p_list->i_count; i++ )
457 vlc_object_kill( p_list->p_values[i].p_object );
459 vlc_list_release( p_list );
461 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
465 * Clean up a dead input thread
466 * This function does not return until the thread is effectively cancelled.
468 * \param the input thread to kill
470 void input_DestroyThread( input_thread_t *p_input )
472 input_DestroyThreadExtended( p_input, NULL );
475 void input_DestroyThreadExtended( input_thread_t *p_input, sout_instance_t **pp_sout )
477 /* Join the thread */
478 vlc_thread_join( p_input );
481 Destroy( p_input, pp_sout );
484 /*****************************************************************************
485 * Run: main thread loop
486 * This is the "normal" thread that spawns the input processing chain,
487 * reads the stream, cleans up and waits
488 *****************************************************************************/
489 static int Run( input_thread_t *p_input )
491 /* Signal that the thread is launched */
492 vlc_thread_ready( p_input );
494 if( Init( p_input ) )
496 /* If we failed, wait before we are killed, and exit */
497 p_input->b_error = VLC_TRUE;
498 playlist_Signal( pl_Get( p_input ) );
502 /* Tell we're dead */
503 p_input->b_dead = VLC_TRUE;
510 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
512 /* We have finish to demux data but not to play them */
513 while( !p_input->b_die )
515 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
518 msg_Dbg( p_input, "waiting decoder fifos to empty" );
520 msleep( INPUT_IDLE_SLEEP );
523 /* We have finished */
524 p_input->b_eof = VLC_TRUE;
525 playlist_Signal( pl_Get( p_input ) );
528 /* Wait until we are asked to die */
529 if( !p_input->b_die )
540 /*****************************************************************************
541 * RunAndDestroy: main thread loop
542 * This is the "just forget me" thread that spawns the input processing chain,
543 * reads the stream, cleans up and releases memory
544 *****************************************************************************/
545 static int RunAndDestroy( input_thread_t *p_input )
547 /* Signal that the thread is launched */
548 vlc_thread_ready( p_input );
550 if( Init( p_input ) )
555 if( !p_input->b_eof && !p_input->b_error && p_input->p->input.b_eof )
557 /* We have finished demuxing data but not playing it */
558 while( !p_input->b_die )
560 if( input_EsOutDecodersEmpty( p_input->p->p_es_out ) )
563 msg_Dbg( p_input, "waiting decoder fifos to empty" );
565 msleep( INPUT_IDLE_SLEEP );
568 /* We have finished */
569 p_input->b_eof = VLC_TRUE;
577 Destroy( p_input, NULL );
581 /*****************************************************************************
582 * Main loop: Fill buffers from access, and demux
583 *****************************************************************************/
584 static void MainLoop( input_thread_t *p_input )
586 int64_t i_start_mdate = mdate();
587 int64_t i_intf_update = 0;
590 while( !p_input->b_die && !p_input->b_error && !p_input->p->input.b_eof )
592 vlc_bool_t b_force_update = VLC_FALSE;
598 if( p_input->i_state != PAUSE_S )
600 if( ( p_input->p->i_stop > 0 && p_input->i_time >= p_input->p->i_stop ) ||
601 ( p_input->p->i_run > 0 && i_start_mdate+p_input->p->i_run < mdate() ) )
604 i_ret = p_input->p->input.p_demux->pf_demux(p_input->p->input.p_demux);
609 if( p_input->p->input.b_title_demux &&
610 p_input->p->input.p_demux->info.i_update )
612 i_ret = UpdateFromDemux( p_input );
613 b_force_update = VLC_TRUE;
615 else if( !p_input->p->input.b_title_demux &&
616 p_input->p->input.p_access &&
617 p_input->p->input.p_access->info.i_update )
619 i_ret = UpdateFromAccess( p_input );
620 b_force_update = VLC_TRUE;
624 if( i_ret == 0 ) /* EOF */
628 var_Get( p_input, "input-repeat", &repeat );
629 if( repeat.i_int == 0 )
631 /* End of file - we do not set b_die because only the
632 * playlist is allowed to do so. */
633 input_ChangeState( p_input, END_S );
634 msg_Dbg( p_input, "EOF reached" );
635 p_input->p->input.b_eof = VLC_TRUE;
639 msg_Dbg( p_input, "repeating the same input (%d)",
641 if( repeat.i_int > 0 )
644 var_Set( p_input, "input-repeat", repeat );
647 /* Seek to start title/seekpoint */
648 val.i_int = p_input->p->input.i_title_start -
649 p_input->p->input.i_title_offset;
650 if( val.i_int < 0 || val.i_int >= p_input->p->input.i_title )
652 input_ControlPush( p_input,
653 INPUT_CONTROL_SET_TITLE, &val );
655 val.i_int = p_input->p->input.i_seekpoint_start -
656 p_input->p->input.i_seekpoint_offset;
657 if( val.i_int > 0 /* TODO: check upper boundary */ )
658 input_ControlPush( p_input,
659 INPUT_CONTROL_SET_SEEKPOINT, &val );
661 /* Seek to start position */
662 if( p_input->p->i_start > 0 )
664 val.i_time = p_input->p->i_start;
665 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME,
671 input_ControlPush( p_input, INPUT_CONTROL_SET_POSITION,
676 i_start_mdate = mdate();
681 p_input->b_error = VLC_TRUE;
684 if( i_ret > 0 && p_input->p->i_slave > 0 )
686 SlaveDemux( p_input );
696 vlc_mutex_lock( &p_input->p->lock_control );
697 ControlReduce( p_input );
698 while( !ControlPopNoLock( p_input, &i_type, &val ) )
700 msg_Dbg( p_input, "control type=%d", i_type );
701 if( Control( p_input, i_type, val ) )
702 b_force_update = VLC_TRUE;
704 vlc_mutex_unlock( &p_input->p->lock_control );
706 if( b_force_update || i_intf_update < mdate() )
710 int64_t i_time, i_length;
711 /* update input status variables */
712 if( !demux2_Control( p_input->p->input.p_demux,
713 DEMUX_GET_POSITION, &f_pos ) )
715 val.f_float = (float)f_pos;
716 var_Change( p_input, "position", VLC_VAR_SETVALUE, &val, NULL );
718 if( !demux2_Control( p_input->p->input.p_demux,
719 DEMUX_GET_TIME, &i_time ) )
721 p_input->i_time = i_time;
723 var_Change( p_input, "time", VLC_VAR_SETVALUE, &val, NULL );
725 if( !demux2_Control( p_input->p->input.p_demux,
726 DEMUX_GET_LENGTH, &i_length ) )
729 var_Get( p_input, "length", &old_val );
730 val.i_time = i_length;
731 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
733 if( old_val.i_time != val.i_time )
735 UpdateItemLength( p_input, i_length );
739 var_SetBool( p_input, "intf-change", VLC_TRUE );
740 i_intf_update = mdate() + I64C(150000);
742 /* 150ms * 8 = ~ 1 second */
743 if( ++i_updates % 8 == 0 )
745 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
746 /* Are we the thread responsible for computing global stats ? */
747 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
749 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
750 p_input->p_libvlc->p_playlist->p_stats );
756 static int Init( input_thread_t * p_input )
766 /* Initialize optional stream output. (before access/demuxer)
767 * XXX: we add a special case if the uri starts by vlc.
768 * else 'vlc in.file --sout "" vlc:quit' cannot work (the output will
769 * be destroyed in case of a file).
770 * (this will break playing of file starting by 'vlc:' but I don't
771 * want to add more logic, just force file by file:// or code it ;)
773 memset( &p_input->p->counters, 0, sizeof( p_input->p->counters ) );
774 vlc_mutex_init( p_input, &p_input->p->counters.counters_lock );
776 for( i = 0; i < p_input->p->input.p_item->i_options; i++ )
778 if( !strncmp( p_input->p->input.p_item->ppsz_options[i], "meta-file", 9 ) )
780 msg_Dbg( p_input, "Input is a meta file: disabling unneeded options" );
781 var_SetString( p_input, "sout", "" );
782 var_SetBool( p_input, "sout-all", VLC_FALSE );
783 var_SetString( p_input, "input-slave", "" );
784 var_SetInteger( p_input, "input-repeat", 0 );
785 var_SetString( p_input, "sub-file", "" );
786 var_SetBool( p_input, "sub-autodetect-file", VLC_FALSE );
790 if( !p_input->b_preparsing )
792 /* Prepare statistics */
793 #define INIT_COUNTER( c, type, compute ) p_input->p->counters.p_##c = \
794 stats_CounterCreate( p_input, VLC_VAR_##type, STATS_##compute);
795 if( p_input->p_libvlc->b_stats )
797 INIT_COUNTER( read_bytes, INTEGER, COUNTER );
798 INIT_COUNTER( read_packets, INTEGER, COUNTER );
799 INIT_COUNTER( demux_read, INTEGER, COUNTER );
800 INIT_COUNTER( input_bitrate, FLOAT, DERIVATIVE );
801 INIT_COUNTER( demux_bitrate, FLOAT, DERIVATIVE );
802 INIT_COUNTER( played_abuffers, INTEGER, COUNTER );
803 INIT_COUNTER( lost_abuffers, INTEGER, COUNTER );
804 INIT_COUNTER( displayed_pictures, INTEGER, COUNTER );
805 INIT_COUNTER( lost_pictures, INTEGER, COUNTER );
806 INIT_COUNTER( decoded_audio, INTEGER, COUNTER );
807 INIT_COUNTER( decoded_video, INTEGER, COUNTER );
808 INIT_COUNTER( decoded_sub, INTEGER, COUNTER );
809 p_input->p->counters.p_sout_send_bitrate = NULL;
810 p_input->p->counters.p_sout_sent_packets = NULL;
811 p_input->p->counters.p_sout_sent_bytes = NULL;
812 if( p_input->p->counters.p_demux_bitrate )
813 p_input->p->counters.p_demux_bitrate->update_interval = 1000000;
814 if( p_input->p->counters.p_input_bitrate )
815 p_input->p->counters.p_input_bitrate->update_interval = 1000000;
818 /* Find a usable sout and attach it to p_input */
819 psz = var_GetNonEmptyString( p_input, "sout" );
820 if( psz && strncasecmp( p_input->p->input.p_item->psz_uri, "vlc:", 4 ) )
822 /* Check the validity of the provided sout */
823 if( p_input->p->p_sout )
825 if( strcmp( p_input->p->p_sout->psz_sout, psz ) )
827 msg_Dbg( p_input, "destroying unusable sout" );
829 sout_DeleteInstance( p_input->p->p_sout );
830 p_input->p->p_sout = NULL;
834 if( p_input->p->p_sout )
837 msg_Dbg( p_input, "sout keep: reusing sout" );
838 msg_Dbg( p_input, "sout keep: you probably want to use "
839 "gather stream_out" );
840 vlc_object_attach( p_input->p->p_sout, p_input );
844 /* Create a new one */
845 p_input->p->p_sout = sout_NewInstance( p_input, psz );
846 if( !p_input->p->p_sout )
848 input_ChangeState( p_input, ERROR_S );
849 msg_Err( p_input, "cannot start stream output instance, " \
856 if( p_input->p_libvlc->b_stats )
858 INIT_COUNTER( sout_sent_packets, INTEGER, COUNTER );
859 INIT_COUNTER (sout_sent_bytes, INTEGER, COUNTER );
860 INIT_COUNTER( sout_send_bitrate, FLOAT, DERIVATIVE );
861 if( p_input->p->counters.p_sout_send_bitrate )
862 p_input->p->counters.p_sout_send_bitrate->update_interval =
866 else if( p_input->p->p_sout )
868 msg_Dbg( p_input, "destroying useless sout" );
870 sout_DeleteInstance( p_input->p->p_sout );
871 p_input->p->p_sout = NULL;
877 p_input->p->p_es_out = input_EsOutNew( p_input, p_input->p->i_rate );
878 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_FALSE );
879 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, ES_OUT_MODE_NONE );
881 var_Create( p_input, "bit-rate", VLC_VAR_INTEGER );
882 var_Create( p_input, "sample-rate", VLC_VAR_INTEGER );
884 if( InputSourceInit( p_input, &p_input->p->input,
885 p_input->p->input.p_item->psz_uri, NULL ) )
890 /* Create global title (from master) */
891 if( !p_input->b_preparsing )
893 p_input->p->i_title = p_input->p->input.i_title;
894 p_input->p->title = p_input->p->input.title;
895 p_input->p->i_title_offset = p_input->p->input.i_title_offset;
896 p_input->p->i_seekpoint_offset = p_input->p->input.i_seekpoint_offset;
897 if( p_input->p->i_title > 0 )
899 /* Setup variables */
900 input_ControlVarNavigation( p_input );
901 input_ControlVarTitle( p_input, 0 );
905 p_input->b_can_pace_control = p_input->p->input.b_can_pace_control;
906 p_input->p->b_can_pause = p_input->p->input.b_can_pause;
907 p_input->p->b_can_rate_control = p_input->p->input.b_can_rate_control;
910 if( p_input->i_pts_delay < 0 )
911 p_input->i_pts_delay = 0;
913 /* If the desynchronisation requested by the user is < 0, we need to
914 * cache more data. */
915 var_Get( p_input, "audio-desync", &val );
916 if( val.i_int < 0 ) p_input->i_pts_delay -= (val.i_int * 1000);
918 /* Update cr_average depending on the caching */
919 p_input->p->input.i_cr_average *= (10 * p_input->i_pts_delay / 200000);
920 p_input->p->input.i_cr_average /= 10;
921 if( p_input->p->input.i_cr_average < 10 ) p_input->p->input.i_cr_average = 10;
924 /* Load master infos */
926 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_LENGTH,
927 &val.i_time ) && val.i_time > 0 )
929 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
930 UpdateItemLength( p_input, val.i_time );
934 val.i_time = input_item_GetDuration( p_input->p->input.p_item );
936 { /* fallback: gets length from metadata */
937 var_Change( p_input, "length", VLC_VAR_SETVALUE, &val, NULL );
938 UpdateItemLength( p_input, val.i_time );
942 /* Start title/chapter */
943 if( !p_input->b_preparsing )
945 val.i_int = p_input->p->input.i_title_start -
946 p_input->p->input.i_title_offset;
947 if( val.i_int > 0 && val.i_int < p_input->p->input.i_title )
948 input_ControlPush( p_input, INPUT_CONTROL_SET_TITLE, &val );
949 val.i_int = p_input->p->input.i_seekpoint_start -
950 p_input->p->input.i_seekpoint_offset;
951 if( val.i_int > 0 /* TODO: check upper boundary */ )
952 input_ControlPush( p_input, INPUT_CONTROL_SET_SEEKPOINT, &val );
956 p_input->p->i_start = I64C(1000000) * var_GetInteger( p_input, "start-time" );
957 p_input->p->i_stop = I64C(1000000) * var_GetInteger( p_input, "stop-time" );
958 p_input->p->i_run = I64C(1000000) * var_GetInteger( p_input, "run-time" );
959 if( p_input->p->i_run < 0 )
961 msg_Warn( p_input, "invalid run-time ignored" );
962 p_input->p->i_run = 0;
965 if( p_input->p->i_start > 0 )
967 if( p_input->p->i_start >= val.i_time )
969 msg_Warn( p_input, "invalid start-time ignored" );
975 msg_Dbg( p_input, "starting at time: %ds",
976 (int)( p_input->p->i_start / I64C(1000000) ) );
978 s.i_time = p_input->p->i_start;
979 input_ControlPush( p_input, INPUT_CONTROL_SET_TIME, &s );
982 if( p_input->p->i_stop > 0 && p_input->p->i_stop <= p_input->p->i_start )
984 msg_Warn( p_input, "invalid stop-time ignored" );
985 p_input->p->i_stop = 0;
989 /* Get fps and set it if not already set */
990 if( !demux2_Control( p_input->p->input.p_demux, DEMUX_GET_FPS, &f_fps ) &&
993 float f_requested_fps;
995 var_Create( p_input, "sub-original-fps", VLC_VAR_FLOAT );
996 var_SetFloat( p_input, "sub-original-fps", f_fps );
998 f_requested_fps = var_CreateGetFloat( p_input, "sub-fps" );
999 if( f_requested_fps != f_fps )
1001 var_Create( p_input, "sub-fps", VLC_VAR_FLOAT|
1002 VLC_VAR_DOINHERIT );
1003 var_SetFloat( p_input, "sub-fps", f_requested_fps );
1007 i_delay = var_CreateGetInteger( p_input, "sub-delay" );
1010 var_SetTime( p_input, "spu-delay", (mtime_t)i_delay * 100000 );
1013 /* Look for and add subtitle files */
1014 psz_subtitle = var_GetNonEmptyString( p_input, "sub-file" );
1015 if( psz_subtitle != NULL )
1017 msg_Dbg( p_input, "forced subtitle: %s", psz_subtitle );
1018 input_AddSubtitles( p_input, psz_subtitle, VLC_FALSE );
1021 var_Get( p_input, "sub-autodetect-file", &val );
1024 char *psz_autopath = var_GetNonEmptyString( p_input, "sub-autodetect-path" );
1025 char **subs = subtitles_Detect( p_input, psz_autopath,
1026 p_input->p->input.p_item->psz_uri );
1027 input_source_t *sub;
1029 if( psz_autopath == NULL )
1030 psz_autopath = strdup("");
1032 /* Try to autoselect the first autodetected subtitles file
1033 * if no subtitles file was specified */
1034 if( ( psz_subtitle == NULL ) && subs && subs[0] )
1036 input_AddSubtitles( p_input, subs[0], VLC_FALSE );
1041 /* Then, just add the following subtitles files */
1042 for( ; subs && subs[i]; i++ )
1044 if( !psz_subtitle || strcmp( psz_subtitle, subs[i] ) )
1046 sub = InputSourceNew( p_input );
1047 if( !InputSourceInit( p_input, sub, subs[i], "subtitle" ) )
1049 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
1055 if( subs ) free( subs );
1056 if( psz_autopath ) free( psz_autopath );
1058 free( psz_subtitle );
1060 /* Look for slave */
1061 psz = var_GetNonEmptyString( p_input, "input-slave" );
1065 input_source_t *slave;
1066 while( psz && *psz )
1068 while( *psz == ' ' || *psz == '#' )
1072 if( ( psz_delim = strchr( psz, '#' ) ) )
1074 *psz_delim++ = '\0';
1081 msg_Dbg( p_input, "adding slave input '%s'", psz );
1082 slave = InputSourceNew( p_input );
1083 if( !InputSourceInit( p_input, slave, psz, NULL ) )
1085 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1095 p_input->p->i_start = 0;
1096 p_input->p->i_start = 0;
1100 if( !p_input->b_preparsing )
1102 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ACTIVE, VLC_TRUE );
1103 i_es_out_mode = ES_OUT_MODE_AUTO;
1105 if( p_input->p->p_sout )
1107 var_Get( p_input, "sout-all", &val );
1110 i_es_out_mode = ES_OUT_MODE_ALL;
1115 var_Get( p_input, "programs", &val );
1116 if ( val.p_list && val.p_list->i_count )
1118 i_es_out_mode = ES_OUT_MODE_PARTIAL;
1119 /* Note : we should remove the "program" callback. */
1122 var_Change( p_input, "programs", VLC_VAR_FREELIST, &val,
1126 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_MODE, i_es_out_mode );
1128 /* Inform the demuxer about waited group (needed only for DVB) */
1129 if( i_es_out_mode == ES_OUT_MODE_ALL )
1131 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1, NULL );
1133 else if( i_es_out_mode == ES_OUT_MODE_PARTIAL )
1135 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, -1,
1140 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP,
1141 (int) var_GetInteger( p_input, "program" ), NULL );
1144 if( p_input->p->p_sout )
1146 if( p_input->p->p_sout->i_out_pace_nocontrol > 0 )
1148 p_input->p->b_out_pace_control = VLC_FALSE;
1152 p_input->p->b_out_pace_control = VLC_TRUE;
1155 if( p_input->b_can_pace_control && p_input->p->b_out_pace_control )
1157 /* We don't want a high input priority here or we'll
1158 * end-up sucking up all the CPU time */
1159 vlc_thread_set_priority( p_input, VLC_THREAD_PRIORITY_LOW );
1162 msg_Dbg( p_input, "starting in %s mode",
1163 p_input->p->b_out_pace_control ? "async" : "sync" );
1167 p_meta = vlc_meta_New();
1168 /* Get meta data from users */
1169 InputMetaUser( p_input, p_meta );
1171 /* Get meta data from master input */
1172 DemuxMeta( p_input, p_meta, p_input->p->input.p_demux );
1174 /* Access_file does not give any meta, and there are no slave */
1175 if( !p_input->b_preparsing )
1177 if( p_input->p->input.p_access )
1178 access2_Control( p_input->p->input.p_access, ACCESS_GET_META,
1181 /* Get meta data from slave input */
1182 for( i = 0; i < p_input->p->i_slave; i++ )
1184 DemuxMeta( p_input, p_meta, p_input->p->slave[i]->p_demux );
1185 if( p_input->p->slave[i]->p_access )
1187 access2_Control( p_input->p->slave[i]->p_access,
1188 ACCESS_GET_META, p_meta );
1193 InputUpdateMeta( p_input, p_meta );
1195 if( !p_input->b_preparsing )
1197 msg_Dbg( p_input, "`%s' successfully opened",
1198 p_input->p->input.p_item->psz_uri );
1202 /* initialization is complete */
1203 input_ChangeState( p_input, PLAYING_S );
1208 input_ChangeState( p_input, ERROR_S );
1210 if( p_input->p->p_es_out )
1211 input_EsOutDelete( p_input->p->p_es_out );
1213 if( p_input->p->p_sout )
1215 vlc_object_detach( p_input->p->p_sout );
1216 sout_DeleteInstance( p_input->p->p_sout );
1220 if( !p_input->b_preparsing && p_input->p_libvlc->b_stats )
1222 #define EXIT_COUNTER( c ) do { if( p_input->p->counters.p_##c ) \
1223 stats_CounterClean( p_input->p->counters.p_##c );\
1224 p_input->p->counters.p_##c = NULL; } while(0)
1225 EXIT_COUNTER( read_bytes );
1226 EXIT_COUNTER( read_packets );
1227 EXIT_COUNTER( demux_read );
1228 EXIT_COUNTER( input_bitrate );
1229 EXIT_COUNTER( demux_bitrate );
1230 EXIT_COUNTER( played_abuffers );
1231 EXIT_COUNTER( lost_abuffers );
1232 EXIT_COUNTER( displayed_pictures );
1233 EXIT_COUNTER( lost_pictures );
1234 EXIT_COUNTER( decoded_audio );
1235 EXIT_COUNTER( decoded_video );
1236 EXIT_COUNTER( decoded_sub );
1238 if( p_input->p->p_sout )
1240 EXIT_COUNTER( sout_sent_packets );
1241 EXIT_COUNTER (sout_sent_bytes );
1242 EXIT_COUNTER( sout_send_bitrate );
1247 /* Mark them deleted */
1248 p_input->p->input.p_demux = NULL;
1249 p_input->p->input.p_stream = NULL;
1250 p_input->p->input.p_access = NULL;
1251 p_input->p->p_es_out = NULL;
1252 p_input->p->p_sout = NULL;
1254 return VLC_EGENERIC;
1257 /*****************************************************************************
1258 * Error: RunThread() error loop
1259 *****************************************************************************
1260 * This function is called when an error occurred during thread main's loop.
1261 *****************************************************************************/
1262 static void Error( input_thread_t *p_input )
1264 input_ChangeState( p_input, ERROR_S );
1265 while( !p_input->b_die )
1268 msleep( INPUT_IDLE_SLEEP );
1272 /*****************************************************************************
1273 * End: end the input thread
1274 *****************************************************************************/
1275 static void End( input_thread_t * p_input )
1279 /* We are at the end */
1280 input_ChangeState( p_input, END_S );
1282 /* Clean control variables */
1283 input_ControlVarClean( p_input );
1285 /* Clean up master */
1286 InputSourceClean( &p_input->p->input );
1289 for( i = 0; i < p_input->p->i_slave; i++ )
1291 InputSourceClean( p_input->p->slave[i] );
1292 free( p_input->p->slave[i] );
1294 if( p_input->p->slave ) free( p_input->p->slave );
1296 /* Unload all modules */
1297 if( p_input->p->p_es_out )
1298 input_EsOutDelete( p_input->p->p_es_out );
1300 if( !p_input->b_preparsing )
1302 #define CL_CO( c ) stats_CounterClean( p_input->p->counters.p_##c ); p_input->p->counters.p_##c = NULL;
1303 if( p_input->p_libvlc->b_stats )
1305 /* make sure we are up to date */
1306 stats_ComputeInputStats( p_input, p_input->p->input.p_item->p_stats );
1307 if( p_input->p_libvlc->p_playlist->p_stats_computer == p_input )
1309 stats_ComputeGlobalStats( p_input->p_libvlc->p_playlist,
1310 p_input->p_libvlc->p_playlist->p_stats );
1311 p_input->p_libvlc->p_playlist->p_stats_computer = NULL;
1313 CL_CO( read_bytes );
1314 CL_CO( read_packets );
1315 CL_CO( demux_read );
1316 CL_CO( input_bitrate );
1317 CL_CO( demux_bitrate );
1318 CL_CO( played_abuffers );
1319 CL_CO( lost_abuffers );
1320 CL_CO( displayed_pictures );
1321 CL_CO( lost_pictures );
1322 CL_CO( decoded_audio) ;
1323 CL_CO( decoded_video );
1324 CL_CO( decoded_sub) ;
1327 /* Close optional stream output instance */
1328 if( p_input->p->p_sout )
1330 CL_CO( sout_sent_packets );
1331 CL_CO( sout_sent_bytes );
1332 CL_CO( sout_send_bitrate );
1334 vlc_object_detach( p_input->p->p_sout );
1339 if( p_input->p->i_attachment > 0 )
1341 for( i = 0; i < p_input->p->i_attachment; i++ )
1342 vlc_input_attachment_Delete( p_input->p->attachment[i] );
1343 TAB_CLEAN( p_input->p->i_attachment, p_input->p->attachment );
1346 vlc_mutex_destroy( &p_input->p->counters.counters_lock );
1348 /* Tell we're dead */
1349 p_input->b_dead = VLC_TRUE;
1352 static sout_instance_t *SoutFind( vlc_object_t *p_parent, input_item_t *p_item, vlc_bool_t *pb_sout_keep )
1354 vlc_bool_t b_keep_sout = var_CreateGetBool( p_parent, "sout-keep" );
1355 sout_instance_t *p_sout = NULL;
1358 /* Search sout-keep options
1359 * XXX it has to be done here, but it is duplicated work :( */
1360 vlc_mutex_lock( &p_item->lock );
1361 for( i = 0; i < p_item->i_options; i++ )
1363 const char *psz_option = p_item->ppsz_options[i];
1366 if( *psz_option == ':' )
1369 if( !strcmp( psz_option, "sout-keep" ) )
1370 b_keep_sout = VLC_TRUE;
1371 else if( !strcmp( psz_option, "no-sout-keep" ) || !strcmp( psz_option, "nosout-keep" ) )
1372 b_keep_sout = VLC_FALSE;
1374 vlc_mutex_unlock( &p_item->lock );
1376 /* Find a potential sout to reuse
1377 * XXX it might be unusable but this will be checked later */
1380 /* Remove the sout from the playlist garbage collector */
1381 playlist_t *p_playlist = pl_Yield( p_parent );
1383 vlc_mutex_lock( &p_playlist->gc_lock );
1384 p_sout = vlc_object_find( p_playlist, VLC_OBJECT_SOUT, FIND_CHILD );
1387 if( p_sout->p_parent != VLC_OBJECT(p_playlist) )
1389 vlc_object_release( p_sout );
1394 vlc_object_detach( p_sout ); /* Remove it from the GC */
1396 vlc_object_release( p_sout );
1399 vlc_mutex_unlock( &p_playlist->gc_lock );
1401 pl_Release( p_parent );
1405 *pb_sout_keep = b_keep_sout;
1409 static void SoutKeep( sout_instance_t *p_sout )
1411 /* attach sout to the playlist */
1412 playlist_t *p_playlist = pl_Yield( p_sout );
1414 msg_Dbg( p_sout, "sout has been kept" );
1415 vlc_object_attach( p_sout, p_playlist );
1417 pl_Release( p_sout );
1420 /*****************************************************************************
1422 *****************************************************************************/
1423 static inline int ControlPopNoLock( input_thread_t *p_input,
1424 int *pi_type, vlc_value_t *p_val )
1426 if( p_input->p->i_control <= 0 )
1428 return VLC_EGENERIC;
1431 *pi_type = p_input->p->control[0].i_type;
1432 *p_val = p_input->p->control[0].val;
1434 p_input->p->i_control--;
1435 if( p_input->p->i_control > 0 )
1439 for( i = 0; i < p_input->p->i_control; i++ )
1441 p_input->p->control[i].i_type = p_input->p->control[i+1].i_type;
1442 p_input->p->control[i].val = p_input->p->control[i+1].val;
1449 static void ControlReduce( input_thread_t *p_input )
1456 for( i = 1; i < p_input->p->i_control; i++ )
1458 const int i_lt = p_input->p->control[i-1].i_type;
1459 const int i_ct = p_input->p->control[i].i_type;
1461 /* XXX We can't merge INPUT_CONTROL_SET_ES */
1462 /* msg_Dbg( p_input, "[%d/%d] l=%d c=%d", i, p_input->p->i_control,
1466 ( i_ct == INPUT_CONTROL_SET_STATE ||
1467 i_ct == INPUT_CONTROL_SET_RATE ||
1468 i_ct == INPUT_CONTROL_SET_POSITION ||
1469 i_ct == INPUT_CONTROL_SET_TIME ||
1470 i_ct == INPUT_CONTROL_SET_PROGRAM ||
1471 i_ct == INPUT_CONTROL_SET_TITLE ||
1472 i_ct == INPUT_CONTROL_SET_SEEKPOINT ||
1473 i_ct == INPUT_CONTROL_SET_BOOKMARK ) )
1476 // msg_Dbg( p_input, "merged at %d", i );
1477 /* Remove the i-1 */
1478 for( j = i; j < p_input->p->i_control; j++ )
1479 p_input->p->control[j-1] = p_input->p->control[j];
1480 p_input->p->i_control--;
1484 /* TODO but that's not that important
1485 - merge SET_X with SET_X_CMD
1486 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before a SET_TITLE
1487 - remove SET_SEEKPOINT/SET_POSITION/SET_TIME before another among them
1494 static vlc_bool_t Control( input_thread_t *p_input, int i_type,
1497 vlc_bool_t b_force_update = VLC_FALSE;
1499 if( !p_input ) return b_force_update;
1503 case INPUT_CONTROL_SET_DIE:
1504 msg_Dbg( p_input, "control: stopping input" );
1505 /* Mark all submodules to die */
1506 if( p_input->p->input.p_access )
1507 vlc_object_kill( p_input->p->input.p_access );
1508 if( p_input->p->input.p_stream )
1509 vlc_object_kill( p_input->p->input.p_stream );
1510 vlc_object_kill( p_input->p->input.p_demux );
1512 vlc_object_kill( p_input );
1515 case INPUT_CONTROL_SET_POSITION:
1516 case INPUT_CONTROL_SET_POSITION_OFFSET:
1519 if( i_type == INPUT_CONTROL_SET_POSITION )
1521 f_pos = val.f_float;
1525 /* Should not fail */
1526 demux2_Control( p_input->p->input.p_demux,
1527 DEMUX_GET_POSITION, &f_pos );
1528 f_pos += val.f_float;
1530 if( f_pos < 0.0 ) f_pos = 0.0;
1531 if( f_pos > 1.0 ) f_pos = 1.0;
1532 /* Reset the decoders states and clock sync (before calling the demuxer */
1533 input_EsOutChangePosition( p_input->p->p_es_out );
1534 if( demux2_Control( p_input->p->input.p_demux, DEMUX_SET_POSITION,
1537 msg_Err( p_input, "INPUT_CONTROL_SET_POSITION(_OFFSET) "
1538 "%2.1f%% failed", f_pos * 100 );
1542 if( p_input->p->i_slave > 0 )
1543 SlaveSeek( p_input );
1545 b_force_update = VLC_TRUE;
1550 case INPUT_CONTROL_SET_TIME:
1551 case INPUT_CONTROL_SET_TIME_OFFSET:
1556 if( i_type == INPUT_CONTROL_SET_TIME )
1558 i_time = val.i_time;
1562 /* Should not fail */
1563 demux2_Control( p_input->p->input.p_demux,
1564 DEMUX_GET_TIME, &i_time );
1565 i_time += val.i_time;
1567 if( i_time < 0 ) i_time = 0;
1569 /* Reset the decoders states and clock sync (before calling the demuxer */
1570 input_EsOutChangePosition( p_input->p->p_es_out );
1572 i_ret = demux2_Control( p_input->p->input.p_demux,
1573 DEMUX_SET_TIME, i_time );
1578 /* Emulate it with a SET_POS */
1579 demux2_Control( p_input->p->input.p_demux,
1580 DEMUX_GET_LENGTH, &i_length );
1583 double f_pos = (double)i_time / (double)i_length;
1584 i_ret = demux2_Control( p_input->p->input.p_demux,
1585 DEMUX_SET_POSITION, f_pos );
1590 msg_Warn( p_input, "INPUT_CONTROL_SET_TIME(_OFFSET) "I64Fd
1591 " failed or not possible", i_time );
1595 if( p_input->p->i_slave > 0 )
1596 SlaveSeek( p_input );
1598 b_force_update = VLC_TRUE;
1603 case INPUT_CONTROL_SET_STATE:
1604 if( ( val.i_int == PLAYING_S && p_input->i_state == PAUSE_S ) ||
1605 ( val.i_int == PAUSE_S && p_input->i_state == PAUSE_S ) )
1608 if( p_input->p->input.p_access )
1609 i_ret = access2_Control( p_input->p->input.p_access,
1610 ACCESS_SET_PAUSE_STATE, VLC_FALSE );
1612 i_ret = demux2_Control( p_input->p->input.p_demux,
1613 DEMUX_SET_PAUSE_STATE, VLC_FALSE );
1617 /* FIXME What to do ? */
1618 msg_Warn( p_input, "cannot unset pause -> EOF" );
1619 vlc_mutex_unlock( &p_input->p->lock_control );
1620 input_ControlPush( p_input, INPUT_CONTROL_SET_DIE, NULL );
1621 vlc_mutex_lock( &p_input->p->lock_control );
1624 b_force_update = VLC_TRUE;
1626 /* Switch to play */
1627 p_input->i_state = PLAYING_S;
1628 val.i_int = PLAYING_S;
1629 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1633 input_EsOutChangeState( p_input->p->p_es_out );
1635 else if( val.i_int == PAUSE_S && p_input->i_state == PLAYING_S &&
1636 p_input->p->b_can_pause )
1639 if( p_input->p->input.p_access )
1640 i_ret = access2_Control( p_input->p->input.p_access,
1641 ACCESS_SET_PAUSE_STATE, VLC_TRUE );
1643 i_ret = demux2_Control( p_input->p->input.p_demux,
1644 DEMUX_SET_PAUSE_STATE, VLC_TRUE );
1646 b_force_update = VLC_TRUE;
1650 msg_Warn( p_input, "cannot set pause state" );
1651 val.i_int = p_input->i_state;
1655 val.i_int = PAUSE_S;
1658 /* Switch to new state */
1659 p_input->i_state = val.i_int;
1660 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1664 input_EsOutChangeState( p_input->p->p_es_out );
1666 else if( val.i_int == PAUSE_S && !p_input->p->b_can_pause )
1668 b_force_update = VLC_TRUE;
1670 /* Correct "state" value */
1671 val.i_int = p_input->i_state;
1672 var_Change( p_input, "state", VLC_VAR_SETVALUE, &val, NULL );
1674 else if( val.i_int != PLAYING_S && val.i_int != PAUSE_S )
1676 msg_Err( p_input, "invalid state in INPUT_CONTROL_SET_STATE" );
1680 case INPUT_CONTROL_SET_RATE:
1681 case INPUT_CONTROL_SET_RATE_SLOWER:
1682 case INPUT_CONTROL_SET_RATE_FASTER:
1686 if( i_type == INPUT_CONTROL_SET_RATE )
1692 static const int ppi_factor[][2] = {
1693 {1,64}, {1,32}, {1,16}, {1,8}, {1,4}, {1,3}, {1,2}, {2,3},
1695 {3,2}, {2,1}, {3,1}, {4,1}, {8,1}, {16,1}, {32,1}, {64,1},
1704 for( i = 0; ppi_factor[i][0] != 0; i++ )
1706 const int i_test_r = INPUT_RATE_DEFAULT * ppi_factor[i][0] / ppi_factor[i][1];
1707 const int i_test_e = abs(p_input->p->i_rate - i_test_r);
1708 if( i_test_e < i_error )
1714 assert( i_idx >= 0 && ppi_factor[i_idx][0] != 0 );
1716 if( i_type == INPUT_CONTROL_SET_RATE_SLOWER )
1718 if( ppi_factor[i_idx+1][0] > 0 )
1719 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx+1][0] / ppi_factor[i_idx+1][1];
1721 i_rate = INPUT_RATE_MAX+1;
1725 assert( i_type == INPUT_CONTROL_SET_RATE_FASTER );
1727 i_rate = INPUT_RATE_DEFAULT * ppi_factor[i_idx-1][0] / ppi_factor[i_idx-1][1];
1729 i_rate = INPUT_RATE_MIN-1;
1733 if( i_rate < INPUT_RATE_MIN )
1735 msg_Dbg( p_input, "cannot set rate faster" );
1736 i_rate = INPUT_RATE_MIN;
1738 else if( i_rate > INPUT_RATE_MAX )
1740 msg_Dbg( p_input, "cannot set rate slower" );
1741 i_rate = INPUT_RATE_MAX;
1743 if( i_rate != INPUT_RATE_DEFAULT &&
1744 ( ( !p_input->b_can_pace_control && !p_input->p->b_can_rate_control ) ||
1745 ( p_input->p->p_sout && !p_input->p->b_out_pace_control ) ) )
1747 msg_Dbg( p_input, "cannot change rate" );
1748 i_rate = INPUT_RATE_DEFAULT;
1750 if( i_rate != p_input->p->i_rate &&
1751 !p_input->b_can_pace_control && p_input->p->b_can_rate_control )
1754 if( p_input->p->input.p_access )
1755 i_ret = VLC_EGENERIC;
1757 i_ret = demux2_Control( p_input->p->input.p_demux,
1758 DEMUX_SET_RATE, &i_rate );
1761 msg_Warn( p_input, "ACCESS/DEMUX_SET_RATE failed" );
1762 i_rate = p_input->p->i_rate;
1767 if( i_rate != p_input->p->i_rate )
1770 var_Change( p_input, "rate", VLC_VAR_SETVALUE, &val, NULL );
1772 p_input->p->i_rate = i_rate;
1774 /* FIXME do we need a RESET_PCR when !p_input->p->input.b_rescale_ts ? */
1775 if( p_input->p->input.b_rescale_ts )
1776 input_EsOutChangeRate( p_input->p->p_es_out, i_rate );
1778 b_force_update = VLC_TRUE;
1783 case INPUT_CONTROL_SET_PROGRAM:
1784 /* No need to force update, es_out does it if needed */
1785 es_out_Control( p_input->p->p_es_out,
1786 ES_OUT_SET_GROUP, val.i_int );
1788 demux2_Control( p_input->p->input.p_demux, DEMUX_SET_GROUP, val.i_int,
1792 case INPUT_CONTROL_SET_ES:
1793 /* No need to force update, es_out does it if needed */
1794 es_out_Control( p_input->p->p_es_out, ES_OUT_SET_ES,
1795 input_EsOutGetFromID( p_input->p->p_es_out,
1799 case INPUT_CONTROL_SET_AUDIO_DELAY:
1800 input_EsOutSetDelay( p_input->p->p_es_out,
1801 AUDIO_ES, val.i_time );
1802 var_Change( p_input, "audio-delay", VLC_VAR_SETVALUE, &val, NULL );
1805 case INPUT_CONTROL_SET_SPU_DELAY:
1806 input_EsOutSetDelay( p_input->p->p_es_out,
1807 SPU_ES, val.i_time );
1808 var_Change( p_input, "spu-delay", VLC_VAR_SETVALUE, &val, NULL );
1811 case INPUT_CONTROL_SET_TITLE:
1812 case INPUT_CONTROL_SET_TITLE_NEXT:
1813 case INPUT_CONTROL_SET_TITLE_PREV:
1814 if( p_input->p->input.b_title_demux &&
1815 p_input->p->input.i_title > 0 )
1818 /* FIXME handle demux title */
1819 demux_t *p_demux = p_input->p->input.p_demux;
1822 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1823 i_title = p_demux->info.i_title - 1;
1824 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1825 i_title = p_demux->info.i_title + 1;
1827 i_title = val.i_int;
1829 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1831 input_EsOutChangePosition( p_input->p->p_es_out );
1833 demux2_Control( p_demux, DEMUX_SET_TITLE, i_title );
1834 input_ControlVarTitle( p_input, i_title );
1837 else if( p_input->p->input.i_title > 0 )
1839 access_t *p_access = p_input->p->input.p_access;
1842 if( i_type == INPUT_CONTROL_SET_TITLE_PREV )
1843 i_title = p_access->info.i_title - 1;
1844 else if( i_type == INPUT_CONTROL_SET_TITLE_NEXT )
1845 i_title = p_access->info.i_title + 1;
1847 i_title = val.i_int;
1849 if( i_title >= 0 && i_title < p_input->p->input.i_title )
1851 input_EsOutChangePosition( p_input->p->p_es_out );
1853 access2_Control( p_access, ACCESS_SET_TITLE, i_title );
1854 stream_AccessReset( p_input->p->input.p_stream );
1858 case INPUT_CONTROL_SET_SEEKPOINT:
1859 case INPUT_CONTROL_SET_SEEKPOINT_NEXT:
1860 case INPUT_CONTROL_SET_SEEKPOINT_PREV:
1861 if( p_input->p->input.b_title_demux &&
1862 p_input->p->input.i_title > 0 )
1864 demux_t *p_demux = p_input->p->input.p_demux;
1866 int64_t i_input_time;
1867 int64_t i_seekpoint_time;
1869 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1871 i_seekpoint = p_demux->info.i_seekpoint;
1872 i_seekpoint_time = p_input->p->input.title[p_demux->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1873 if( i_seekpoint_time >= 0 &&
1874 !demux2_Control( p_demux,
1875 DEMUX_GET_TIME, &i_input_time ) )
1877 if ( i_input_time < i_seekpoint_time + 3000000 )
1883 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1884 i_seekpoint = p_demux->info.i_seekpoint + 1;
1886 i_seekpoint = val.i_int;
1888 if( i_seekpoint >= 0 && i_seekpoint <
1889 p_input->p->input.title[p_demux->info.i_title]->i_seekpoint )
1892 input_EsOutChangePosition( p_input->p->p_es_out );
1894 demux2_Control( p_demux, DEMUX_SET_SEEKPOINT, i_seekpoint );
1897 else if( p_input->p->input.i_title > 0 )
1899 demux_t *p_demux = p_input->p->input.p_demux;
1900 access_t *p_access = p_input->p->input.p_access;
1902 int64_t i_input_time;
1903 int64_t i_seekpoint_time;
1905 if( i_type == INPUT_CONTROL_SET_SEEKPOINT_PREV )
1907 i_seekpoint = p_access->info.i_seekpoint;
1908 i_seekpoint_time = p_input->p->input.title[p_access->info.i_title]->seekpoint[i_seekpoint]->i_time_offset;
1909 if( i_seekpoint_time >= 0 &&
1910 demux2_Control( p_demux,
1911 DEMUX_GET_TIME, &i_input_time ) )
1913 if ( i_input_time < i_seekpoint_time + 3000000 )
1919 else if( i_type == INPUT_CONTROL_SET_SEEKPOINT_NEXT )
1920 i_seekpoint = p_access->info.i_seekpoint + 1;
1922 i_seekpoint = val.i_int;
1924 if( i_seekpoint >= 0 && i_seekpoint <
1925 p_input->p->input.title[p_access->info.i_title]->i_seekpoint )
1927 input_EsOutChangePosition( p_input->p->p_es_out );
1929 access2_Control( p_access, ACCESS_SET_SEEKPOINT,
1931 stream_AccessReset( p_input->p->input.p_stream );
1936 case INPUT_CONTROL_ADD_SLAVE:
1937 if( val.psz_string )
1939 input_source_t *slave = InputSourceNew( p_input );
1941 if( !InputSourceInit( p_input, slave, val.psz_string, NULL ) )
1947 msg_Dbg( p_input, "adding %s as slave on the fly",
1951 if( demux2_Control( p_input->p->input.p_demux,
1952 DEMUX_GET_TIME, &i_time ) )
1954 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
1955 InputSourceClean( slave );
1959 if( demux2_Control( slave->p_demux,
1960 DEMUX_SET_TIME, i_time ) )
1962 msg_Err( p_input, "seek failed for new slave" );
1963 InputSourceClean( slave );
1968 /* Get meta (access and demux) */
1969 p_meta = vlc_meta_New();
1970 access2_Control( slave->p_access, ACCESS_GET_META,
1972 demux2_Control( slave->p_demux, DEMUX_GET_META, p_meta );
1973 InputUpdateMeta( p_input, p_meta );
1975 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, slave );
1980 msg_Warn( p_input, "failed to add %s as slave",
1984 free( val.psz_string );
1988 case INPUT_CONTROL_SET_BOOKMARK:
1990 msg_Err( p_input, "not yet implemented" );
1994 return b_force_update;
1997 /*****************************************************************************
1999 *****************************************************************************/
2000 static int UpdateFromDemux( input_thread_t *p_input )
2002 demux_t *p_demux = p_input->p->input.p_demux;
2005 if( p_demux->info.i_update & INPUT_UPDATE_TITLE )
2007 v.i_int = p_demux->info.i_title;
2008 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2010 input_ControlVarTitle( p_input, p_demux->info.i_title );
2012 p_demux->info.i_update &= ~INPUT_UPDATE_TITLE;
2014 if( p_demux->info.i_update & INPUT_UPDATE_SEEKPOINT )
2016 v.i_int = p_demux->info.i_seekpoint;
2017 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2019 p_demux->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2021 p_demux->info.i_update &= ~INPUT_UPDATE_SIZE;
2023 /* Hmmm only works with master input */
2024 if( p_input->p->input.p_demux == p_demux )
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_demux->info.i_title > i_title_end ||
2034 ( p_demux->info.i_title == i_title_end &&
2035 p_demux->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2037 else if( i_seekpoint_end >=0 )
2039 if( p_demux->info.i_seekpoint > i_seekpoint_end ) return 0;
2041 else if( i_title_end >= 0 )
2043 if( p_demux->info.i_title > i_title_end ) return 0;
2050 /*****************************************************************************
2052 *****************************************************************************/
2053 static int UpdateFromAccess( input_thread_t *p_input )
2055 access_t *p_access = p_input->p->input.p_access;
2058 if( p_access->info.i_update & INPUT_UPDATE_TITLE )
2060 v.i_int = p_access->info.i_title;
2061 var_Change( p_input, "title", VLC_VAR_SETVALUE, &v, NULL );
2063 input_ControlVarTitle( p_input, p_access->info.i_title );
2065 stream_AccessUpdate( p_input->p->input.p_stream );
2067 p_access->info.i_update &= ~INPUT_UPDATE_TITLE;
2069 if( p_access->info.i_update & INPUT_UPDATE_SEEKPOINT )
2071 v.i_int = p_access->info.i_seekpoint;
2072 var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &v, NULL);
2073 p_access->info.i_update &= ~INPUT_UPDATE_SEEKPOINT;
2075 if( p_access->info.i_update & INPUT_UPDATE_META )
2077 /* TODO maybe multi - access ? */
2078 vlc_meta_t *p_meta = vlc_meta_New();
2079 access2_Control( p_input->p->input.p_access,ACCESS_GET_META, p_meta );
2080 InputUpdateMeta( p_input, p_meta );
2081 var_SetInteger( pl_Get( p_input ), "item-change", p_input->p->input.p_item->i_id );
2082 p_access->info.i_update &= ~INPUT_UPDATE_META;
2085 p_access->info.i_update &= ~INPUT_UPDATE_SIZE;
2087 /* Hmmm only works with master input */
2088 if( p_input->p->input.p_access == p_access )
2090 int i_title_end = p_input->p->input.i_title_end -
2091 p_input->p->input.i_title_offset;
2092 int i_seekpoint_end = p_input->p->input.i_seekpoint_end -
2093 p_input->p->input.i_seekpoint_offset;
2095 if( i_title_end >= 0 && i_seekpoint_end >=0 )
2097 if( p_access->info.i_title > i_title_end ||
2098 ( p_access->info.i_title == i_title_end &&
2099 p_access->info.i_seekpoint > i_seekpoint_end ) ) return 0;
2101 else if( i_seekpoint_end >=0 )
2103 if( p_access->info.i_seekpoint > i_seekpoint_end ) return 0;
2105 else if( i_title_end >= 0 )
2107 if( p_access->info.i_title > i_title_end ) return 0;
2114 /*****************************************************************************
2116 *****************************************************************************/
2117 static void UpdateItemLength( input_thread_t *p_input, int64_t i_length )
2119 input_item_SetDuration( p_input->p->input.p_item, (mtime_t) i_length );
2121 if( !p_input->b_preparsing )
2123 pl_Yield( p_input );
2124 var_SetInteger( pl_Get( p_input ), "item-change",
2125 p_input->p->input.p_item->i_id );
2126 pl_Release( p_input );
2130 /*****************************************************************************
2132 *****************************************************************************/
2133 static input_source_t *InputSourceNew( input_thread_t *p_input )
2135 input_source_t *in = (input_source_t*) malloc( sizeof( input_source_t ) );
2139 msg_Err( p_input, "out of memory for new input source" );
2144 in->p_access = NULL;
2145 in->p_stream = NULL;
2147 in->b_title_demux = VLC_FALSE;
2148 TAB_INIT( in->i_title, in->title );
2149 in->b_can_pause = VLC_TRUE;
2150 in->b_can_pace_control = VLC_TRUE;
2151 in->b_can_rate_control = VLC_TRUE;
2152 in->b_rescale_ts = VLC_TRUE;
2153 in->b_eof = VLC_FALSE;
2155 in->i_cr_average = 0;
2160 /*****************************************************************************
2162 *****************************************************************************/
2163 static int InputSourceInit( input_thread_t *p_input,
2164 input_source_t *in, const char *psz_mrl,
2165 const char *psz_forced_demux )
2167 char psz_dup[strlen (psz_mrl) + 1];
2168 const char *psz_access;
2169 const char *psz_demux;
2176 strcpy( psz_dup, psz_mrl );
2178 if( !in ) return VLC_EGENERIC;
2179 if( !p_input ) return VLC_EGENERIC;
2182 if( !p_input->b_preparsing )
2184 MRLSplit( psz_dup, &psz_access, &psz_demux, &psz_path );
2186 msg_Dbg( p_input, "`%s' gives access `%s' demux `%s' path `%s'",
2187 psz_mrl, psz_access, psz_demux, psz_path );
2189 /* Hack to allow udp://@:port syntax */
2191 (strncmp( psz_access, "udp", 3 ) &&
2192 strncmp( psz_access, "rtp", 3 )) )
2194 /* Find optional titles and seekpoints */
2195 MRLSections( p_input, psz_path, &in->i_title_start, &in->i_title_end,
2196 &in->i_seekpoint_start, &in->i_seekpoint_end );
2198 if( psz_forced_demux && *psz_forced_demux )
2200 psz_demux = psz_forced_demux;
2202 else if( !psz_demux || *psz_demux == '\0' )
2204 /* special hack for forcing a demuxer with --demux=module
2205 * (and do nothing with a list) */
2206 char *psz_var_demux = var_GetNonEmptyString( p_input, "demux" );
2208 if( psz_var_demux != NULL &&
2209 !strchr(psz_var_demux, ',' ) &&
2210 !strchr(psz_var_demux, ':' ) )
2212 psz_demux = psz_var_demux;
2214 msg_Dbg( p_input, "enforced demux ` %s'", psz_demux );
2218 /* Try access_demux if no demux given */
2219 if( *psz_demux == '\0' )
2221 in->p_demux = demux2_New( p_input, psz_access, psz_demux, psz_path,
2222 NULL, p_input->p->p_es_out, VLC_FALSE );
2228 if( !strncmp( psz_path, "file://", 7 ) )
2230 msg_Dbg( p_input, "trying to pre-parse %s", psz_path );
2232 psz_access = "file";
2237 int64_t i_pts_delay;
2239 /* Get infos from access_demux */
2240 demux2_Control( in->p_demux,
2241 DEMUX_GET_PTS_DELAY, &i_pts_delay );
2242 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2244 in->b_title_demux = VLC_TRUE;
2245 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2246 &in->title, &in->i_title,
2247 &in->i_title_offset, &in->i_seekpoint_offset ) )
2252 if( demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_PACE,
2253 &in->b_can_pace_control ) )
2254 in->b_can_pace_control = VLC_FALSE;
2256 if( !in->b_can_pace_control )
2258 if( demux2_Control( in->p_demux, DEMUX_CAN_CONTROL_RATE,
2259 &in->b_can_rate_control, &in->b_rescale_ts ) )
2261 in->b_can_rate_control = VLC_FALSE;
2262 in->b_rescale_ts = VLC_TRUE; /* not used */
2267 in->b_can_rate_control = VLC_TRUE;
2268 in->b_rescale_ts = VLC_TRUE;
2270 if( demux2_Control( in->p_demux, DEMUX_CAN_PAUSE,
2271 &in->b_can_pause ) )
2272 in->b_can_pause = VLC_FALSE;
2273 var_SetBool( p_input, "can-pause", in->b_can_pause );
2275 int ret = demux2_Control( in->p_demux, DEMUX_CAN_SEEK,
2277 if( ret != VLC_SUCCESS )
2278 val.b_bool = VLC_FALSE;
2279 var_Set( p_input, "seekable", val );
2283 int64_t i_pts_delay;
2285 input_ChangeState( p_input, OPENING_S );
2287 /* Now try a real access */
2288 in->p_access = access2_New( p_input, psz_access, psz_demux, psz_path,
2289 p_input->b_preparsing );
2291 /* Access failed, URL encoded ? */
2292 if( in->p_access == NULL && strchr( psz_path, '%' ) )
2294 decode_URI( psz_path );
2296 msg_Dbg( p_input, "retrying with access `%s' demux `%s' path `%s'",
2297 psz_access, psz_demux, psz_path );
2299 in->p_access = access2_New( p_input,
2300 psz_access, psz_demux, psz_path,
2301 p_input->b_preparsing );
2303 /* access failed, maybe our access detection was wrong.
2304 * Retry with the full name */
2305 if( in->p_access == NULL && strchr( psz_mrl, ':' ) )
2307 msg_Dbg( p_input, "retrying with access `' demux `' path `%s'",
2310 in->p_access = access2_New( p_input,
2312 p_input->b_preparsing );
2314 if( in->p_access == NULL )
2316 msg_Err( p_input, "open of `%s' failed: %s", psz_mrl,
2318 intf_UserFatal( VLC_OBJECT( p_input), VLC_FALSE,
2319 _("Your input can't be opened"),
2320 _("VLC is unable to open the MRL '%s'."
2321 " Check the log for details."), psz_mrl );
2326 psz_tmp = psz = var_GetNonEmptyString( p_input, "access-filter" );
2327 while( psz && *psz )
2329 access_t *p_access = in->p_access;
2330 char *end = strchr( psz, ':' );
2335 in->p_access = access2_FilterNew( in->p_access, psz );
2336 if( in->p_access == NULL )
2338 in->p_access = p_access;
2339 msg_Warn( p_input, "failed to insert access filter %s",
2347 /* Get infos from access */
2348 if( !p_input->b_preparsing )
2350 access2_Control( in->p_access,
2351 ACCESS_GET_PTS_DELAY, &i_pts_delay );
2352 p_input->i_pts_delay = __MAX( p_input->i_pts_delay, i_pts_delay );
2354 in->b_title_demux = VLC_FALSE;
2355 if( access2_Control( in->p_access, ACCESS_GET_TITLE_INFO,
2356 &in->title, &in->i_title,
2357 &in->i_title_offset, &in->i_seekpoint_offset ) )
2363 access2_Control( in->p_access, ACCESS_CAN_CONTROL_PACE,
2364 &in->b_can_pace_control );
2365 in->b_can_rate_control = in->b_can_pace_control;
2366 in->b_rescale_ts = VLC_TRUE;
2368 access2_Control( in->p_access, ACCESS_CAN_PAUSE,
2370 var_SetBool( p_input, "can-pause", in->b_can_pause );
2371 access2_Control( in->p_access, ACCESS_CAN_SEEK,
2373 var_Set( p_input, "seekable", val );
2376 input_ChangeState( p_input, BUFFERING_S );
2378 /* Create the stream_t */
2379 in->p_stream = stream_AccessNew( in->p_access, p_input->b_preparsing );
2380 if( in->p_stream == NULL )
2382 msg_Warn( p_input, "cannot create a stream_t from access" );
2386 /* Open a demuxer */
2387 if( *psz_demux == '\0' && *in->p_access->psz_demux )
2389 psz_demux = in->p_access->psz_demux;
2393 /* Take access redirections into account */
2394 char *psz_real_path;
2395 char *psz_buf = NULL;
2396 if( in->p_access->psz_path )
2398 const char *psz_a, *psz_d;
2399 psz_buf = strdup( in->p_access->psz_path );
2400 MRLSplit( psz_buf, &psz_a, &psz_d, &psz_real_path );
2404 psz_real_path = psz_path;
2406 in->p_demux = demux2_New( p_input, psz_access, psz_demux,
2408 in->p_stream, p_input->p->p_es_out,
2409 p_input->b_preparsing );
2413 if( in->p_demux == NULL )
2415 msg_Err( p_input, "no suitable demux module for `%s/%s://%s'",
2416 psz_access, psz_demux, psz_path );
2417 intf_UserFatal( VLC_OBJECT( p_input ), VLC_FALSE,
2418 _("VLC can't recognize the input's format"),
2419 _("The format of '%s' cannot be detected. "
2420 "Have a look the log for details."), psz_mrl );
2424 /* Get title from demux */
2425 if( !p_input->b_preparsing && in->i_title <= 0 )
2427 if( demux2_Control( in->p_demux, DEMUX_GET_TITLE_INFO,
2428 &in->title, &in->i_title,
2429 &in->i_title_offset, &in->i_seekpoint_offset ))
2431 TAB_INIT( in->i_title, in->title );
2435 in->b_title_demux = VLC_TRUE;
2441 * FIXME improve for b_preparsing: move it after GET_META and check psz_arturl */
2442 if( 1 || !p_input->b_preparsing )
2445 input_attachment_t **attachment;
2446 if( !demux2_Control( in->p_demux, DEMUX_GET_ATTACHMENTS,
2447 &attachment, &i_attachment ) )
2449 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2450 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2451 i_attachment, attachment );
2452 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2455 if( !demux2_Control( in->p_demux, DEMUX_GET_FPS, &f_fps ) )
2457 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2459 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2462 if( var_GetInteger( p_input, "clock-synchro" ) != -1 )
2463 in->b_can_pace_control = !var_GetInteger( p_input, "clock-synchro" );
2468 input_ChangeState( p_input, ERROR_S );
2471 demux2_Delete( in->p_demux );
2474 stream_Delete( in->p_stream );
2477 access2_Delete( in->p_access );
2479 return VLC_EGENERIC;
2482 /*****************************************************************************
2484 *****************************************************************************/
2485 static void InputSourceClean( input_source_t *in )
2490 demux2_Delete( in->p_demux );
2493 stream_Delete( in->p_stream );
2496 access2_Delete( in->p_access );
2498 if( in->i_title > 0 )
2500 for( i = 0; i < in->i_title; i++ )
2501 vlc_input_title_Delete( in->title[i] );
2502 TAB_CLEAN( in->i_title, in->title );
2506 static void SlaveDemux( input_thread_t *p_input )
2511 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2513 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2517 for( i = 0; i < p_input->p->i_slave; i++ )
2519 input_source_t *in = p_input->p->slave[i];
2525 if( demux2_Control( in->p_demux, DEMUX_SET_NEXT_DEMUX_TIME, i_time ) )
2530 if( demux2_Control( in->p_demux, DEMUX_GET_TIME, &i_stime ) )
2532 msg_Err( p_input, "slave[%d] doesn't like "
2533 "DEMUX_GET_TIME -> EOF", i );
2538 if( i_stime >= i_time )
2541 if( ( i_ret = in->p_demux->pf_demux( in->p_demux ) ) <= 0 )
2547 i_ret = in->p_demux->pf_demux( in->p_demux );
2552 msg_Dbg( p_input, "slave %d EOF", i );
2553 in->b_eof = VLC_TRUE;
2558 static void SlaveSeek( input_thread_t *p_input )
2563 if( !p_input ) return;
2565 if( demux2_Control( p_input->p->input.p_demux, DEMUX_GET_TIME, &i_time ) )
2567 msg_Err( p_input, "demux doesn't like DEMUX_GET_TIME" );
2571 for( i = 0; i < p_input->p->i_slave; i++ )
2573 input_source_t *in = p_input->p->slave[i];
2575 if( demux2_Control( in->p_demux, DEMUX_SET_TIME, i_time ) )
2577 msg_Err( p_input, "seek failed for slave %d -> EOF", i );
2578 in->b_eof = VLC_TRUE;
2583 /*****************************************************************************
2585 *****************************************************************************/
2586 static void InputMetaUser( input_thread_t *p_input, vlc_meta_t *p_meta )
2590 if( !p_meta ) return;
2592 /* Get meta information from user */
2593 #define GET_META( field, s ) \
2594 var_Get( p_input, (s), &val ); \
2595 if( *val.psz_string ) \
2596 vlc_meta_Set( p_meta, vlc_meta_ ## field, val.psz_string ); \
2597 free( val.psz_string )
2599 GET_META( Title, "meta-title" );
2600 GET_META( Artist, "meta-artist" );
2601 GET_META( Genre, "meta-genre" );
2602 GET_META( Copyright, "meta-copyright" );
2603 GET_META( Description, "meta-description" );
2604 GET_META( Date, "meta-date" );
2605 GET_META( URL, "meta-url" );
2609 /*****************************************************************************
2610 * InputUpdateMeta: merge p_item meta data with p_meta taking care of
2611 * arturl and locking issue.
2612 *****************************************************************************/
2613 static void InputUpdateMeta( input_thread_t *p_input, vlc_meta_t *p_meta )
2615 input_item_t *p_item = p_input->p->input.p_item;
2616 char * psz_arturl = NULL;
2617 char *psz_title = NULL;
2619 int i_arturl_event = VLC_FALSE;
2624 psz_arturl = input_item_GetArtURL( p_item );
2626 vlc_mutex_lock( &p_item->lock );
2627 if( vlc_meta_Get( p_meta, vlc_meta_Title ) && !p_item->b_fixed_name )
2628 psz_title = strdup( vlc_meta_Get( p_meta, vlc_meta_Title ) );
2630 vlc_meta_Merge( p_item->p_meta, p_meta );
2632 if( psz_arturl && *psz_arturl )
2634 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_arturl );
2635 i_arturl_event = VLC_TRUE;
2638 vlc_meta_Delete( p_meta );
2640 if( psz_arturl && !strncmp( psz_arturl, "attachment://", strlen("attachment") ) )
2642 /* Don't look for art cover if sout
2643 * XXX It can change when sout has meta data support */
2644 if( p_input->p->p_sout && !p_input->b_preparsing )
2646 vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, "" );
2647 i_arturl_event = VLC_TRUE;
2651 input_ExtractAttachmentAndCacheArt( p_input );
2657 if( vlc_dictionary_keys_count( &p_item->p_meta->extra_tags ) > 0 )
2659 p_meta = vlc_meta_New();
2660 vlc_meta_Merge( p_meta, input_item_GetMetaObject( p_item ) );
2662 vlc_mutex_unlock( &p_item->lock );
2664 input_item_SetPreparsed( p_item, VLC_TRUE );
2666 if( i_arturl_event == VLC_TRUE )
2670 /* Notify interested third parties */
2671 event.type = vlc_InputItemMetaChanged;
2672 event.u.input_item_meta_changed.meta_type = vlc_meta_ArtworkURL;
2673 vlc_event_send( &p_item->event_manager, &event );
2678 input_Control( p_input, INPUT_SET_NAME, psz_title );
2684 char ** ppsz_all_keys = vlc_dictionary_all_keys( &p_meta->extra_tags );
2685 for( i = 0; ppsz_all_keys[i]; i++ )
2687 input_Control( p_input, INPUT_ADD_INFO, _(VLC_META_INFO_CAT), _(ppsz_all_keys[i]),
2688 vlc_dictionary_value_for_key( &p_meta->extra_tags, ppsz_all_keys[i] ) );
2689 free( ppsz_all_keys[i] );
2691 free( ppsz_all_keys );
2692 vlc_meta_Delete( p_meta );
2695 /** \todo handle sout meta */
2699 static inline vlc_bool_t IsValidAccess( const char *psz )
2703 while( ( c = *psz ) != '\0' )
2708 if( ( !isascii( c ) || !isalnum( c ) ) && c != '-' && ( c != '/' ) )
2712 /* should not happen though */
2716 static void AppendAttachment( int *pi_attachment, input_attachment_t ***ppp_attachment,
2717 int i_new, input_attachment_t **pp_new )
2719 int i_attachment = *pi_attachment;
2720 input_attachment_t **attachment = *ppp_attachment;
2723 attachment = realloc( attachment,
2724 sizeof(input_attachment_t**) * ( i_attachment + i_new ) );
2725 for( i = 0; i < i_new; i++ )
2726 attachment[i_attachment++] = pp_new[i];
2731 *pi_attachment = i_attachment;
2732 *ppp_attachment = attachment;
2735 static void DemuxMeta( input_thread_t *p_input, vlc_meta_t *p_meta, demux_t *p_demux )
2742 /* XXX I am not sure it is a great idea, besides, there is more than that
2743 * if we want to do it right */
2744 vlc_mutex_lock( &p_item->lock );
2745 if( p_item->p_meta && (p_item->p_meta->i_status & ITEM_PREPARSED ) )
2747 vlc_mutex_unlock( &p_item->lock );
2750 vlc_mutex_unlock( &p_item->lock );
2753 demux2_Control( p_demux, DEMUX_GET_META, p_meta );
2754 if( demux2_Control( p_demux, DEMUX_HAS_UNSUPPORTED_META, &b_bool ) )
2759 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
2760 if(! p_demux->p_private )
2763 p_id3 = module_Need( p_demux, "meta reader", NULL, 0 );
2766 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
2768 if( p_demux_meta->p_meta )
2770 vlc_meta_Merge( p_meta, p_demux_meta->p_meta );
2771 vlc_meta_Delete( p_demux_meta->p_meta );
2774 if( p_demux_meta->i_attachments > 0 )
2776 vlc_mutex_lock( &p_input->p->input.p_item->lock );
2777 AppendAttachment( &p_input->p->i_attachment, &p_input->p->attachment,
2778 p_demux_meta->i_attachments, p_demux_meta->attachments );
2779 vlc_mutex_unlock( &p_input->p->input.p_item->lock );
2781 module_Unneed( p_demux, p_id3 );
2783 free( p_demux->p_private );
2787 /*****************************************************************************
2788 * MRLSplit: parse the access, demux and url part of the
2789 * Media Resource Locator.
2790 *****************************************************************************/
2791 void MRLSplit( char *psz_dup, const char **ppsz_access, const char **ppsz_demux,
2794 const char *psz_access = "";
2795 const char *psz_demux = "";
2799 psz = strchr( psz_dup, ':' );
2803 /* Guess whether ':' is part of a local filename, or separates
2804 * access/demux from path */
2805 if( !IsValidAccess( psz_dup ) )
2807 #if defined( WIN32 ) || defined( UNDER_CE )
2808 else if( ( psz - psz_dup == 1 ) && isalpha( psz_dup[0] ) )
2810 //msg_Dbg( p_input, "drive letter %c: found in source", *psz_dup );
2819 if( psz[0] == '/' && psz[1] == '/' ) psz += 2;
2823 psz = strchr( psz_dup, '/' );
2830 psz_access = psz_dup;
2837 *ppsz_access = psz_access;
2838 *ppsz_demux = psz_demux;
2839 *ppsz_path = psz_path;
2842 /*****************************************************************************
2843 * MRLSections: parse title and seekpoint info from the Media Resource Locator.
2846 * [url][@[title-start][:chapter-start][-[title-end][:chapter-end]]]
2847 *****************************************************************************/
2848 static void MRLSections( input_thread_t *p_input, char *psz_source,
2849 int *pi_title_start, int *pi_title_end,
2850 int *pi_chapter_start, int *pi_chapter_end )
2852 char *psz, *psz_end, *psz_next, *psz_check;
2854 *pi_title_start = *pi_title_end = -1;
2855 *pi_chapter_start = *pi_chapter_end = -1;
2857 /* Start by parsing titles and chapters */
2858 if( !psz_source || !( psz = strrchr( psz_source, '@' ) ) ) return;
2860 /* Check we are really dealing with a title/chapter section */
2861 psz_check = psz + 1;
2862 if( !*psz_check ) return;
2863 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2864 if( *psz_check != ':' && *psz_check != '-' && *psz_check ) return;
2865 if( *psz_check == ':' && ++psz_check )
2866 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2867 if( *psz_check != '-' && *psz_check ) return;
2868 if( *psz_check == '-' && ++psz_check )
2869 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2870 if( *psz_check != ':' && *psz_check ) return;
2871 if( *psz_check == ':' && ++psz_check )
2872 if( isdigit(*psz_check) ) strtol( psz_check, &psz_check, 0 );
2873 if( *psz_check ) return;
2875 /* Separate start and end */
2877 if( ( psz_end = strchr( psz, '-' ) ) ) *psz_end++ = 0;
2879 /* Look for the start title */
2880 *pi_title_start = strtol( psz, &psz_next, 0 );
2881 if( !*pi_title_start && psz == psz_next ) *pi_title_start = -1;
2882 *pi_title_end = *pi_title_start;
2885 /* Look for the start chapter */
2887 *pi_chapter_start = strtol( psz, &psz_next, 0 );
2888 if( !*pi_chapter_start && psz == psz_next ) *pi_chapter_start = -1;
2889 *pi_chapter_end = *pi_chapter_start;
2893 /* Look for the end title */
2894 *pi_title_end = strtol( psz_end, &psz_next, 0 );
2895 if( !*pi_title_end && psz_end == psz_next ) *pi_title_end = -1;
2898 /* Look for the end chapter */
2899 if( *psz_end ) psz_end++;
2900 *pi_chapter_end = strtol( psz_end, &psz_next, 0 );
2901 if( !*pi_chapter_end && psz_end == psz_next ) *pi_chapter_end = -1;
2904 msg_Dbg( p_input, "source=`%s' title=%d/%d seekpoint=%d/%d",
2905 psz_source, *pi_title_start, *pi_chapter_start,
2906 *pi_title_end, *pi_chapter_end );
2909 /*****************************************************************************
2910 * input_AddSubtitles: add a subtitles file and enable it
2911 *****************************************************************************/
2912 vlc_bool_t input_AddSubtitles( input_thread_t *p_input, char *psz_subtitle,
2913 vlc_bool_t b_check_extension )
2915 input_source_t *sub;
2918 char *psz_path, *psz_extension;
2920 if( b_check_extension && !subtitles_Filter( psz_subtitle ) )
2925 /* if we are provided a subtitle.sub file,
2926 * see if we don't have a subtitle.idx and use it instead */
2927 psz_path = strdup( psz_subtitle );
2930 psz_extension = strrchr( psz_path, '.');
2931 if( psz_extension && strcmp( psz_extension, ".sub" ) == 0 )
2935 strcpy( psz_extension, ".idx" );
2936 /* FIXME: a portable wrapper for stat() or access() would be more suited */
2937 if( ( f = utf8_fopen( psz_path, "rt" ) ) )
2940 msg_Dbg( p_input, "using %s subtitles file instead of %s",
2941 psz_path, psz_subtitle );
2942 strcpy( psz_subtitle, psz_path );
2948 var_Change( p_input, "spu-es", VLC_VAR_CHOICESCOUNT, &count, NULL );
2950 sub = InputSourceNew( p_input );
2951 if( !InputSourceInit( p_input, sub, psz_subtitle, "subtitle" ) )
2953 TAB_APPEND( p_input->p->i_slave, p_input->p->slave, sub );
2956 if( !var_Change( p_input, "spu-es", VLC_VAR_GETLIST, &list, NULL ) )
2958 if( count.i_int == 0 )
2960 /* if it was first one, there is disable too */
2962 if( count.i_int < list.p_list->i_count )
2964 input_ControlPush( p_input, INPUT_CONTROL_SET_ES,
2965 &list.p_list->p_values[count.i_int] );
2967 var_Change( p_input, "spu-es", VLC_VAR_FREELIST, &list, NULL );